fix(sender): 串流窗口改用离屏双缓冲重绘,消除操作时画面闪黑
- Paint() 原先直接在窗口 DC 上先 FillRect(黑) 再 StretchDIBits, 两次独立 GDI 调用之间可能被合成出「已填黑、还没拉伸」的中间态; 操作串流窗口时触摸事件把帧率顶高,闪黑更容易被看到 - 新增 CreateCompatibleDC/CreateCompatibleBitmap 离屏缓冲,整帧画完再一次 BitBlt, 缓冲按客户区尺寸复用,尺寸变化时才重建 - Close()/WM_DESTROY 释放内存 DC 与位图,避免泄漏 - README 8.1 记录该绘制约定
This commit is contained in:
1 parent
b6d73e6303
commit
dd4cdf431c
3 files changed
+80
-12
No files matched your search
@@ -132,6 +132,7 @@ void VideoWindow::Close() {
|
||||
}
|
||||
open_.store(false);
|
||||
repaint_pending_.store(false);
|
||||
ReleaseBackBuffer();
|
||||
provider_ = nullptr;
|
||||
aspect_ratio_ = 0.0;
|
||||
applied_aspect_ = 0.0;
|
||||
@@ -267,6 +268,7 @@ LRESULT VideoWindow::WndProc(HWND hwnd, UINT message, WPARAM wparam,
|
||||
hwnd_ = nullptr;
|
||||
open_.store(false);
|
||||
repaint_pending_.store(false);
|
||||
ReleaseBackBuffer();
|
||||
provider_ = nullptr;
|
||||
aspect_ratio_ = 0.0;
|
||||
applied_aspect_ = 0.0;
|
||||
@@ -292,15 +294,26 @@ void VideoWindow::Paint() {
|
||||
|
||||
RECT client = {};
|
||||
GetClientRect(hwnd_, &client);
|
||||
FillRect(hdc, &client, static_cast<HBRUSH>(GetStockObject(BLACK_BRUSH)));
|
||||
const int client_width = client.right - client.left;
|
||||
const int client_height = client.bottom - client.top;
|
||||
|
||||
// 先整帧画进离屏缓冲,最后一次性 BitBlt 上去。
|
||||
// FillRect(黑) 和 StretchDIBits(画面) 是两次独立的 GDI 调用,直接画在窗口上时
|
||||
// DWM 可能正好合成到「已填黑、还没拉伸」的中间态,表现为操作时画面闪一下黑。
|
||||
HDC target = hdc;
|
||||
if (client_width > 0 && client_height > 0) {
|
||||
EnsureBackBuffer(hdc, client_width, client_height);
|
||||
if (back_dc_ != nullptr) {
|
||||
target = back_dc_;
|
||||
}
|
||||
}
|
||||
FillRect(target, &client, static_cast<HBRUSH>(GetStockObject(BLACK_BRUSH)));
|
||||
|
||||
int width = 0;
|
||||
int height = 0;
|
||||
std::shared_ptr<const std::vector<uint8_t>> frame =
|
||||
provider_ ? provider_(&width, &height) : nullptr;
|
||||
|
||||
const int client_width = client.right - client.left;
|
||||
const int client_height = client.bottom - client.top;
|
||||
if (frame && !frame->empty() && width > 0 && height > 0 && client_width > 0 &&
|
||||
client_height > 0) {
|
||||
const double aspect =
|
||||
@@ -335,9 +348,9 @@ void VideoWindow::Paint() {
|
||||
info.bmiHeader.biBitCount = 32;
|
||||
info.bmiHeader.biCompression = BI_RGB;
|
||||
|
||||
SetStretchBltMode(hdc, COLORONCOLOR);
|
||||
StretchDIBits(hdc, offset_x, offset_y, draw_width, draw_height, 0, 0, width,
|
||||
height, frame->data(), &info, DIB_RGB_COLORS, SRCCOPY);
|
||||
SetStretchBltMode(target, COLORONCOLOR);
|
||||
StretchDIBits(target, offset_x, offset_y, draw_width, draw_height, 0, 0,
|
||||
width, height, frame->data(), &info, DIB_RGB_COLORS, SRCCOPY);
|
||||
} else {
|
||||
// 还没有画面:清掉坐标映射,避免拿上一路的尺寸误点。
|
||||
frame_width_ = 0;
|
||||
@@ -345,18 +358,60 @@ void VideoWindow::Paint() {
|
||||
image_rect_ = RECT{};
|
||||
// 还没画面时给个提示:窗口是在点「请求投屏」后就弹出的,
|
||||
// 纯黑会让人以为卡死。
|
||||
HGDIOBJ previous = SelectObject(hdc, GetStockObject(DEFAULT_GUI_FONT));
|
||||
SetBkMode(hdc, TRANSPARENT);
|
||||
SetTextColor(hdc, RGB(170, 170, 170));
|
||||
HGDIOBJ previous = SelectObject(target, GetStockObject(DEFAULT_GUI_FONT));
|
||||
SetBkMode(target, TRANSPARENT);
|
||||
SetTextColor(target, RGB(170, 170, 170));
|
||||
RECT text_rect = client;
|
||||
DrawTextW(hdc, L"等待被控端画面…\r\n请在手机弹窗中点击「允许」", -1, &text_rect,
|
||||
DT_CENTER | DT_VCENTER | DT_WORDBREAK);
|
||||
SelectObject(hdc, previous);
|
||||
DrawTextW(target, L"等待被控端画面…\r\n请在手机弹窗中点击「允许」", -1,
|
||||
&text_rect, DT_CENTER | DT_VCENTER | DT_WORDBREAK);
|
||||
SelectObject(target, previous);
|
||||
}
|
||||
|
||||
if (target != hdc) {
|
||||
BitBlt(hdc, 0, 0, client_width, client_height, target, 0, 0, SRCCOPY);
|
||||
}
|
||||
|
||||
EndPaint(hwnd_, &paint);
|
||||
}
|
||||
|
||||
void VideoWindow::EnsureBackBuffer(HDC reference, int width, int height) {
|
||||
if (back_dc_ != nullptr && back_width_ == width && back_height_ == height) {
|
||||
return;
|
||||
}
|
||||
ReleaseBackBuffer();
|
||||
HDC dc = CreateCompatibleDC(reference);
|
||||
if (dc == nullptr) {
|
||||
return;
|
||||
}
|
||||
HBITMAP bitmap = CreateCompatibleBitmap(reference, width, height);
|
||||
if (bitmap == nullptr) {
|
||||
DeleteDC(dc);
|
||||
return;
|
||||
}
|
||||
back_previous_ = SelectObject(dc, bitmap);
|
||||
back_dc_ = dc;
|
||||
back_bitmap_ = bitmap;
|
||||
back_width_ = width;
|
||||
back_height_ = height;
|
||||
}
|
||||
|
||||
void VideoWindow::ReleaseBackBuffer() {
|
||||
if (back_dc_ != nullptr) {
|
||||
if (back_previous_ != nullptr) {
|
||||
SelectObject(back_dc_, back_previous_);
|
||||
}
|
||||
DeleteDC(back_dc_);
|
||||
}
|
||||
back_dc_ = nullptr;
|
||||
back_previous_ = nullptr;
|
||||
if (back_bitmap_ != nullptr) {
|
||||
DeleteObject(back_bitmap_);
|
||||
}
|
||||
back_bitmap_ = nullptr;
|
||||
back_width_ = 0;
|
||||
back_height_ = 0;
|
||||
}
|
||||
|
||||
bool VideoWindow::ClientToFrame(POINT point, int* x, int* y) const {
|
||||
const int width = image_rect_.right - image_rect_.left;
|
||||
const int height = image_rect_.bottom - image_rect_.top;
|
||||
|
||||
Reference in new issue
Block a user