-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCenterCanvas.cpp
More file actions
186 lines (164 loc) · 5.66 KB
/
Copy pathCenterCanvas.cpp
File metadata and controls
186 lines (164 loc) · 5.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
//
// Copyright 2022-2025 SHAO Liming <lmshao@163.com>. All rights reserved.
//
#include "CenterCanvas.h"
#include <fstream>
#include "Utils.h"
CenterCanvas::CenterCanvas(MainFrame *parent) : wxPanel(parent), parent_(parent)
{
window_ = SDL_CreateWindowFrom(GetHandle());
if (!window_) {
wxLogMessage("error %s", SDL_GetError());
return;
}
renderer_ = SDL_CreateRenderer(window_, -1, 0);
if (!renderer_) {
wxLogMessage("error %s", SDL_GetError());
return;
}
SDL_SetRenderDrawBlendMode(renderer_, SDL_BLENDMODE_BLEND);
Bind(wxEVT_PAINT, &CenterCanvas::OnPaint, this);
Bind(wxEVT_SIZE, &CenterCanvas::OnSize, this);
}
void CenterCanvas::OnSize(wxSizeEvent &event)
{
surface_ = nullptr;
Refresh();
event.Skip();
}
bool CenterCanvas::InitSDL()
{
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
printf("SDL_Init failed. SDL_Error: %s\n", SDL_GetError());
return false;
}
return true;
}
void CenterCanvas::OnPaint(wxPaintEvent &event)
{
LOG("OnPaint enter");
if (!texture_) {
LOG("SDL_CreateTexture with %d x %d, format: %d", width_, height_, (int)pixelFormat_ - (int)ID_FORMAT_I420);
SDL_PixelFormatEnum format;
switch (pixelFormat_) {
case ID_FORMAT_I420:
format = SDL_PIXELFORMAT_IYUV;
break;
case ID_FORMAT_YV12:
format = SDL_PIXELFORMAT_YV12;
break;
case ID_FORMAT_NV12:
format = SDL_PIXELFORMAT_NV12;
break;
case ID_FORMAT_NV21:
format = SDL_PIXELFORMAT_NV21;
break;
case ID_FORMAT_I422:
needTranscode_ = true;
format = SDL_PIXELFORMAT_IYUV;
break;
case ID_FORMAT_UYVY:
needTranscode_ = true;
format = SDL_PIXELFORMAT_IYUV;
break;
case ID_FORMAT_YUY2:
needTranscode_ = true;
format = SDL_PIXELFORMAT_IYUV;
break;
case ID_FORMAT_YVYU:
needTranscode_ = true;
format = SDL_PIXELFORMAT_IYUV;
break;
case ID_FORMAT_I444:
needTranscode_ = true;
format = SDL_PIXELFORMAT_IYUV;
break;
default:
format = SDL_PIXELFORMAT_IYUV;
break;
}
texture_ = SDL_CreateTexture(renderer_, format, SDL_TEXTUREACCESS_STREAMING, width_, height_);
if (!texture_) {
printf("SDL_CreateTexture failed, SDL_Error: %s\n", SDL_GetError());
return;
}
}
static bool hasPlayed = false;
auto frame = parent_->GetOneFrame();
if (frame.first) {
LOG("after texture");
hasPlayed = true;
if (needTranscode_) {
if (pixelFormat_ == ID_FORMAT_I422) {
I422ToI420((uint8_t *)frame.first, width_, height_);
} else if (pixelFormat_ == ID_FORMAT_I444) {
I444ToI420((uint8_t *)frame.first, width_, height_);
} else if (pixelFormat_ == ID_FORMAT_UYVY) {
UYVYToI422((uint8_t *)frame.first, width_, height_);
I422ToI420((uint8_t *)frame.first, width_, height_);
} else if (pixelFormat_ == ID_FORMAT_YUY2) {
YUY2ToI422((uint8_t *)frame.first, width_, height_);
I422ToI420((uint8_t *)frame.first, width_, height_);
} else if (pixelFormat_ == ID_FORMAT_YVYU) {
YVYUToI422((uint8_t *)frame.first, width_, height_);
I422ToI420((uint8_t *)frame.first, width_, height_);
}
}
int res = SDL_UpdateTexture(texture_, nullptr, (uint8_t *)frame.first, width_);
if (res != 0) {
printf("SDL_UpdateYUVTexture failed, SDL_Error: %s\n", SDL_GetError());
return;
}
LOG("after SDL_UpdateYUVTexture");
SDL_RenderClear(renderer_);
int canvasW = 0, canvasH = 0;
GetClientSize(&canvasW, &canvasH);
float scale = std::min((float)canvasW / width_, (float)canvasH / height_);
int drawW = (int)(width_ * scale);
int drawH = (int)(height_ * scale);
int drawX = (canvasW - drawW) / 2;
int drawY = (canvasH - drawH) / 2;
rectangle_ = {drawX, drawY, drawW, drawH};
SDL_RenderCopy(renderer_, texture_, nullptr, &rectangle_);
SDL_RenderPresent(renderer_);
} else {
if (hasPlayed) {
return;
}
LOG("set color");
if (!surface_) {
surface_ = SDL_GetWindowSurface(window_);
}
if (!surface_) {
LOG("Surface is null");
return;
}
SDL_FillRect(surface_, nullptr, SDL_MapRGB(surface_->format, 0x93, 0xb8, 0xe5));
SDL_UpdateWindowSurface(window_);
}
}
void CenterCanvas::SetImageSize(int width, int height)
{
LOG("Update Image Size enter, %dx%d => %dx%d", width_, height_, width, height);
if (width == width_ && height_ == height) {
return;
}
width_ = width;
height_ = height;
if (texture_) {
SDL_DestroyTexture(texture_);
texture_ = nullptr;
}
surface_ = nullptr;
Refresh();
}
void CenterCanvas::SetPixelFormat(PIXEL_FORMAT format)
{
if (pixelFormat_ == format)
return;
pixelFormat_ = format;
if (texture_) {
SDL_DestroyTexture(texture_);
texture_ = nullptr;
}
}