From f05272983418db371314896997ad874c0e58212c Mon Sep 17 00:00:00 2001 From: Linductor <202200171251@mail.sdu.edu.cn> Date: Thu, 15 Oct 2026 00:22:32 +0800 Subject: [PATCH 1/2] fix(window): expose SDL move resize fallback outside X11 --- core/window/window_backend.cpp | 2 +- tests/probes/window_only_probe.cpp | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/core/window/window_backend.cpp b/core/window/window_backend.cpp index 3ec9238c..7f47920e 100644 --- a/core/window/window_backend.cpp +++ b/core/window/window_backend.cpp @@ -274,6 +274,7 @@ const X11ResourceApi& x11ResourceApi() { static const X11ResourceApi api = loadX11ResourceApi(); return api; } +#endif bool sendX11MoveResizeForWindow(Handle window, int direction) { SDL_Window* sdlWindow = static_cast(window); @@ -295,7 +296,6 @@ bool sendX11MoveResizeForWindow(Handle window, int direction) { return false; #endif } -#endif } // namespace diff --git a/tests/probes/window_only_probe.cpp b/tests/probes/window_only_probe.cpp index 7307381e..72aa3203 100644 --- a/tests/probes/window_only_probe.cpp +++ b/tests/probes/window_only_probe.cpp @@ -67,6 +67,11 @@ bool pollOnce(core::window::Handle window) { #if defined(EUI_WINDOW_BACKEND_SDL2) bool verifySdlWindowInput(core::window::Handle firstWindow, const core::window::WindowCreateRequest& request) { + // Non-X11 builds must compile and honestly decline WM move/resize. + if (core::window::beginWindowMove(nullptr) || + core::window::beginWindowResize(nullptr, core::window::WindowResizeEdge::TopLeft)) { + return false; + } core::window::Handle secondWindow = core::window::createWindow(request); if (secondWindow == nullptr) { return false; From bcfaa2ead595077a3edb0b8cc3a20f4142fb4afd Mon Sep 17 00:00:00 2001 From: Linductor <202200171251@mail.sdu.edu.cn> Date: Thu, 15 Oct 2026 00:24:23 +0800 Subject: [PATCH 2/2] fix(window): initialize transparent Win32 redirection alpha --- 3rd/glfw/src/win32_window.c | 15 ++ CMakeLists.txt | 6 + .../windows-transparent-framebuffer.md | 24 +++ tests/probes/transparent_cache_probe.cpp | 137 ++++++++++++++++++ 4 files changed, 182 insertions(+) create mode 100644 docs/platform/windows-transparent-framebuffer.md create mode 100644 tests/probes/transparent_cache_probe.cpp diff --git a/3rd/glfw/src/win32_window.c b/3rd/glfw/src/win32_window.c index e6a9496c..b7c0faf5 100644 --- a/3rd/glfw/src/win32_window.c +++ b/3rd/glfw/src/win32_window.c @@ -1142,6 +1142,21 @@ static LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM l case WM_PAINT: { _glfwInputWindowDamage(window); + // Backport glfw/glfw#2815 (jeweg): initialize the DWM + // redirection surface's alpha independently of the GL buffer. + if (window->win32.transparent) + { + PAINTSTRUCT ps; + HDC hdc = BeginPaint(hWnd, &ps); + PatBlt(hdc, + ps.rcPaint.left, + ps.rcPaint.top, + ps.rcPaint.right - ps.rcPaint.left, + ps.rcPaint.bottom - ps.rcPaint.top, + BLACKNESS); + EndPaint(hWnd, &ps); + return 0; + } break; } diff --git a/CMakeLists.txt b/CMakeLists.txt index 05b9ecf7..cdf6aadc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -639,6 +639,9 @@ if(EUI_BUILD_TEST_FIXTURES) get_filename_component(probe_name "${probe_source}" NAME_WE) add_executable(${probe_name} "${probe_source}") target_link_libraries(${probe_name} PRIVATE eui::neo) + if(WIN32 AND probe_name STREQUAL "transparent_cache_probe") + target_link_libraries(${probe_name} PRIVATE dwmapi) + endif() eui_apply_compile_options(${probe_name}) eui_neo_copy_assets(${probe_name}) eui_neo_copy_runtime_dependencies(${probe_name}) @@ -650,6 +653,9 @@ if(EUI_BUILD_TEST_FIXTURES) target_compile_definitions(${probe_name} PRIVATE EUI_TEST_SOURCE_DIR="${CMAKE_CURRENT_SOURCE_DIR}") add_test(NAME ${probe_name} COMMAND ${probe_name} --frames 3) set_tests_properties(${probe_name} PROPERTIES LABELS "probe") + if(probe_name STREQUAL "transparent_cache_probe") + set_tests_properties(${probe_name} PROPERTIES SKIP_RETURN_CODE 77) + endif() if(probe_name STREQUAL "gpu_image_probe") set_tests_properties(${probe_name} PROPERTIES FAIL_REGULAR_EXPRESSION "Validation Error;VUID-") endif() diff --git a/docs/platform/windows-transparent-framebuffer.md b/docs/platform/windows-transparent-framebuffer.md new file mode 100644 index 00000000..e73438f8 --- /dev/null +++ b/docs/platform/windows-transparent-framebuffer.md @@ -0,0 +1,24 @@ +# Windows transparent framebuffer presentation + +The bundled GLFW Win32 backend includes the small WM_PAINT repair proposed in +[glfw/glfw#2815](https://github.com/glfw/glfw/pull/2815) by jeweg. A transparent +window has two distinct alpha surfaces: the renderer's framebuffer and DWM's +redirection surface. Correct GL alpha alone does not initialize the latter. +Undefined redirection alpha can produce intermittent black transparent margins, +including on NVIDIA or virtual display paths. + +For transparent windows, WM_PAINT clears the invalidated redirection area with +PatBlt(BLACKNESS), which defines its RGB and alpha as zero. The normal GLFW damage +notification still requests renderer repaint. Opaque windows keep their previous +paint handling. No window region, color key or geometry crop is introduced. + +External GLFW targets do not receive this bundled-source fix automatically. +Consumers supplying GLFW must carry the corresponding repair themselves. +SDL2 continues to report framebufferTransparent=false; its transparency contract +is unchanged. Vulkan swapchain composite-alpha support remains device-dependent. + +`transparent_cache_probe` checks the Win32 GLFW/OpenGL path against an owned, +known-color background, including initial paint and subsequent cached frames. +It returns CTest skip code 77 on other backend combinations. `--manual` retains +the test windows for visual inspection; normal CTest `--frames` arguments do not. +The separate `transparent_window_viewer` fixture shows actual desktop margins. diff --git a/tests/probes/transparent_cache_probe.cpp b/tests/probes/transparent_cache_probe.cpp new file mode 100644 index 00000000..92a7beb4 --- /dev/null +++ b/tests/probes/transparent_cache_probe.cpp @@ -0,0 +1,137 @@ +#include "core/render/render_backend.h" +#include "core/window/window_backend.h" +#include "core/app/main_window_runtime.h" +#include "eui/detail/dsl_app_impl.h" +#include "eui_neo.h" + +namespace app { +const DslAppConfig& dslAppConfig() { + static const auto config = DslAppConfig{}.iconPath("").tray(false).transparent(true) + .clearColor({0, 0, 0, 0}); + return config; +} +void compose(eui::Ui& ui, const eui::Screen&) { + ui.rect("panel").position(40, 40).size(240, 140).radius(28).color({0.8f, 0.2f, 0.1f, 1}).build(); +} +} + +#if defined(_WIN32) && defined(EUI_RENDER_BACKEND_OPENGL) && !defined(EUI_WINDOW_BACKEND_SDL2) +#define GLFW_INCLUDE_NONE +#include +#define GLFW_EXPOSE_NATIVE_WIN32 +#include +#include +#include +#include + +#if defined(_MSC_VER) +namespace { +int redirectionClears = 0; +BOOL WINAPI observeRedirectionClear(HDC dc, int x, int y, int width, int height, DWORD operation) { + using Blit = BOOL(WINAPI *)(HDC, int, int, int, int, DWORD); + const auto actual = reinterpret_cast(GetProcAddress(GetModuleHandleW(L"gdi32.dll"), "PatBlt")); + const BOOL result = actual(dc, x, y, width, height, operation); + if (result && operation == BLACKNESS && width > 0 && height > 0) ++redirectionClears; + return result; +} +} +// Observe and forward the real GDI clear. Without the Win32 repair, correctly +// zeroed allocations could make a pixels-only regression accidentally pass. +extern "C" { BOOL(WINAPI *__imp_PatBlt)(HDC, int, int, int, int, DWORD) = observeRedirectionClear; } +#endif + +int main(int argc, char** argv) { + if (!glfwInit()) return 1; + const COLORREF backingColor = RGB(80, 160, 100); + HBRUSH brush = CreateSolidBrush(backingColor); + WNDCLASSW wc{}; + wc.hInstance = GetModuleHandleW(nullptr); + wc.lpfnWndProc = DefWindowProcW; + wc.lpszClassName = L"EuiAlphaProbeBacking"; + wc.hbrBackground = brush; + RegisterClassW(&wc); + HWND backing = CreateWindowExW(WS_EX_TOPMOST, wc.lpszClassName, L"EUI alpha probe backing", WS_POPUP, + 120, 120, 420, 320, nullptr, nullptr, wc.hInstance, nullptr); + if (!backing) return 2; + ShowWindow(backing, SW_SHOWNOACTIVATE); + UpdateWindow(backing); + + core::window::WindowCreateRequest request; + request.width = 320; + request.height = 220; + request.title = "EUI transparent cache probe"; + request.decorated = false; + request.transparent = true; + request.visible = false; + request.alwaysOnTop = true; + request.positionSet = true; + request.x = 160; + request.y = 160; + auto window = core::window::createWindow(request); + if (!window || !core::window::framebufferTransparent(window)) return 3; + auto backend = core::render::createRenderBackend(window, nullptr, true); + if (!backend || !backend->initialize()) return 4; + auto *glfw = static_cast(window); + const HWND native = glfwGetWin32Window(glfw); + // Model a newly allocated redirection surface containing opaque black. + // This is separate from the GL framebuffer's correctly zeroed alpha. + BITMAPINFO info{}; + info.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); + info.bmiHeader.biWidth = 1; + info.bmiHeader.biHeight = 1; + info.bmiHeader.biPlanes = 1; + info.bmiHeader.biBitCount = 32; + info.bmiHeader.biCompression = BI_RGB; + const unsigned int opaqueBlack = 0xff000000u; + HDC redirection = GetDC(native); + StretchDIBits(redirection, 0, 0, 320, 220, 0, 0, 1, 1, &opaqueBlack, &info, + DIB_RGB_COLORS, SRCCOPY); + ReleaseDC(native, redirection); + InvalidateRect(native, nullptr, FALSE); + SetWindowPos(backing, HWND_TOP, 120, 120, 420, 320, SWP_NOACTIVATE); + app::initialize(window); + app::AppRunner runner; + app::MainWindowRuntime runtime(runner, [&] { + glfwShowWindow(glfw); + SetWindowPos(native, HWND_TOP, 160, 160, 320, 220, SWP_NOACTIVATE); + InvalidateRect(native, nullptr, FALSE); + UpdateWindow(native); + }); + int failures = 0; + for (int frame = 0; frame < 6; ++frame) { + runner.paintRequested = true; + runtime.updateAndRender(window, *backend, {320, 220, 1, 1}, 0, false, true, + [] {}, [] { return app::MainWindowMetrics{320, 220, 1, 1}; }); + glfwPollEvents(); + DwmFlush(); + POINT origin{}; + ClientToScreen(native, &origin); + HDC screen = GetDC(nullptr); + const COLORREF margin = GetPixel(screen, origin.x + 8, origin.y + 8); + const COLORREF center = GetPixel(screen, origin.x + 160, origin.y + 110); + ReleaseDC(nullptr, screen); + std::printf("frame %d: margin=%06lx expected=%06lx center=%06lx\n", + frame, margin, backingColor, center); + // WM_PAINT initializes the redirection surface and requests a GL + // repaint. From the next frame onward the panel must also be visible. + if (margin != backingColor || (frame > 0 && (GetRValue(center) < 180 || GetGValue(center) > 80))) ++failures; + } +#if defined(_MSC_VER) + std::printf("redirection initialization clears: %d\n", redirectionClears); + if (redirectionClears == 0) ++failures; +#endif + if (argc > 1 && std::strcmp(argv[1], "--manual") == 0) { + while (!glfwWindowShouldClose(glfw)) glfwWaitEvents(); + } + app::shutdown(); + backend.reset(); + core::window::destroyWindow(window); + DestroyWindow(backing); + UnregisterClassW(wc.lpszClassName, wc.hInstance); + DeleteObject(brush); + glfwTerminate(); + return failures ? 1 : 0; +} +#else +int main() { return 77; } // Not applicable without the Win32 GLFW/OpenGL compositor. +#endif