tracker-native: double-buffer preview to stop flicker
This commit is contained in:
1 parent
6dc472921a
commit
b5c7b74faf
1 file changed
+55
-32
@@ -256,7 +256,6 @@ void StopCapture() {
|
||||
}
|
||||
|
||||
void PaintPreview(HWND hwnd, HDC hdc) {
|
||||
std::lock_guard<std::mutex> lock(g_app.preview_mutex);
|
||||
RECT rc;
|
||||
GetClientRect(hwnd, &rc);
|
||||
const int top = 44;
|
||||
@@ -264,40 +263,53 @@ void PaintPreview(HWND hwnd, HDC hdc) {
|
||||
const int avail_h = rc.bottom - top;
|
||||
if (avail_w <= 0 || avail_h <= 0) return;
|
||||
|
||||
// Fill the preview area black first so letterbox bars (and stale pixels from a
|
||||
// previous resolution) don't show through.
|
||||
const RECT area{0, top, rc.right, rc.bottom};
|
||||
FillRect(hdc, &area, reinterpret_cast<HBRUSH>(GetStockObject(BLACK_BRUSH)));
|
||||
// Compose the whole preview area in an off-screen bitmap, then blit it once.
|
||||
// Painting the black fill and the image straight to the window produced a
|
||||
// visible flash each frame; double-buffering removes it.
|
||||
HDC mem = CreateCompatibleDC(hdc);
|
||||
HBITMAP bmp = CreateCompatibleBitmap(hdc, avail_w, avail_h);
|
||||
HBITMAP old_bmp = static_cast<HBITMAP>(SelectObject(mem, bmp));
|
||||
|
||||
if (g_app.preview_bgra.empty() || g_app.preview_w <= 0 || g_app.preview_h <= 0) {
|
||||
return;
|
||||
const RECT full{0, 0, avail_w, avail_h};
|
||||
FillRect(mem, &full, reinterpret_cast<HBRUSH>(GetStockObject(BLACK_BRUSH)));
|
||||
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_app.preview_mutex);
|
||||
if (!g_app.preview_bgra.empty() && g_app.preview_w > 0 &&
|
||||
g_app.preview_h > 0) {
|
||||
// Scale to fit the box while preserving the frame's aspect ratio, centered.
|
||||
const double src_ar =
|
||||
static_cast<double>(g_app.preview_w) / g_app.preview_h;
|
||||
const double dst_ar = static_cast<double>(avail_w) / avail_h;
|
||||
int draw_w, draw_h;
|
||||
if (src_ar > dst_ar) {
|
||||
draw_w = avail_w;
|
||||
draw_h = static_cast<int>(avail_w / src_ar + 0.5);
|
||||
} else {
|
||||
draw_h = avail_h;
|
||||
draw_w = static_cast<int>(avail_h * src_ar + 0.5);
|
||||
}
|
||||
const int off_x = (avail_w - draw_w) / 2;
|
||||
const int off_y = (avail_h - draw_h) / 2;
|
||||
|
||||
BITMAPINFO bmi{};
|
||||
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
|
||||
bmi.bmiHeader.biWidth = g_app.preview_w;
|
||||
bmi.bmiHeader.biHeight = -g_app.preview_h;
|
||||
bmi.bmiHeader.biPlanes = 1;
|
||||
bmi.bmiHeader.biBitCount = 32;
|
||||
bmi.bmiHeader.biCompression = BI_RGB;
|
||||
SetStretchBltMode(mem, HALFTONE);
|
||||
StretchDIBits(mem, off_x, off_y, draw_w, draw_h, 0, 0, g_app.preview_w,
|
||||
g_app.preview_h, g_app.preview_bgra.data(), &bmi,
|
||||
DIB_RGB_COLORS, SRCCOPY);
|
||||
}
|
||||
}
|
||||
|
||||
// Scale to fit the box while preserving the frame's aspect ratio, centered.
|
||||
const double src_ar = static_cast<double>(g_app.preview_w) / g_app.preview_h;
|
||||
const double dst_ar = static_cast<double>(avail_w) / avail_h;
|
||||
int draw_w, draw_h;
|
||||
if (src_ar > dst_ar) {
|
||||
draw_w = avail_w;
|
||||
draw_h = static_cast<int>(avail_w / src_ar + 0.5);
|
||||
} else {
|
||||
draw_h = avail_h;
|
||||
draw_w = static_cast<int>(avail_h * src_ar + 0.5);
|
||||
}
|
||||
const int off_x = (avail_w - draw_w) / 2;
|
||||
const int off_y = top + (avail_h - draw_h) / 2;
|
||||
|
||||
BITMAPINFO bmi{};
|
||||
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
|
||||
bmi.bmiHeader.biWidth = g_app.preview_w;
|
||||
bmi.bmiHeader.biHeight = -g_app.preview_h;
|
||||
bmi.bmiHeader.biPlanes = 1;
|
||||
bmi.bmiHeader.biBitCount = 32;
|
||||
bmi.bmiHeader.biCompression = BI_RGB;
|
||||
SetStretchBltMode(hdc, HALFTONE);
|
||||
StretchDIBits(hdc, off_x, off_y, draw_w, draw_h, 0, 0, g_app.preview_w,
|
||||
g_app.preview_h, g_app.preview_bgra.data(), &bmi, DIB_RGB_COLORS,
|
||||
SRCCOPY);
|
||||
BitBlt(hdc, 0, top, avail_w, avail_h, mem, 0, 0, SRCCOPY);
|
||||
SelectObject(mem, old_bmp);
|
||||
DeleteObject(bmp);
|
||||
DeleteDC(mem);
|
||||
}
|
||||
|
||||
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) {
|
||||
@@ -358,6 +370,17 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) {
|
||||
InvalidateRect(hwnd, nullptr, FALSE);
|
||||
return 0;
|
||||
}
|
||||
case WM_ERASEBKGND: {
|
||||
// Erase only the top control strip; the preview area below is fully
|
||||
// repainted (double-buffered) in WM_PAINT, so erasing it here only
|
||||
// causes flicker.
|
||||
HDC hdc = reinterpret_cast<HDC>(wparam);
|
||||
RECT rc;
|
||||
GetClientRect(hwnd, &rc);
|
||||
RECT strip{0, 0, rc.right, 44};
|
||||
FillRect(hdc, &strip, reinterpret_cast<HBRUSH>(COLOR_WINDOW + 1));
|
||||
return 1;
|
||||
}
|
||||
case WM_PAINT: {
|
||||
PAINTSTRUCT ps;
|
||||
HDC hdc = BeginPaint(hwnd, &ps);
|
||||
|
||||
Reference in new issue
Block a user