GTK Win32: EGL backend never sets the EGL native window, GL renderer presents nothing (regression since 4.20)

Version: 4.24.1 (MSYS2 CLANG64), also present on main.

Symptom: When WGL is unavailable and GDK falls back to EGL, with GDK_DEBUG=dcomp
the GL renderer realizes successfully but every frame logs and nothing is drawn.

Gdk-CRITICAL: gdk_gl_context_ensure_egl_surface: assertion 'priv->egl_native_window != NULL' failed

Expected result
Frames are presented, or GL fails to realize so that GSK falls back to Cairo.

Analysis

  • gdk_win32_display_init_gl() tries WGL, then gdk_display_init_egl().
    eglGetPlatformDisplay(EGL_PLATFORM_ANGLE_ANGLE) fails on a non-ANGLE EGL, the
    eglGetDisplay(HDC) fallback succeeds, and a GdkWin32GLContextEGL is returned.
  • gdk_win32_gl_context_surface_attach(), shared by WGL and EGL, creates the cloaked child
    HWND and the DComp surface. It succeeds because DComp is enabled.
  • Nothing in gdk/win32/ calls gdk_gl_context_set_egl_native_window(), so
    gdk_gl_context_real_begin_frame() → gdk_gl_context_ensure_egl_surface() hits the
    g_return_if_fail() on every frame.

History

  • 4.18: gdksurface-win32.c called gdk_surface_set_egl_native_window (surface, impl->handle).
  • bdb94baea9 “win32: Update WGL context to use DirectComposition”: added the shared
    surface_attach with the cloaked child HWND.
  • 387460ab55 “gdk: Move EGL surface handling to GLContext” (!8644): moved that call into a new
    gdk_x11_gl_context_egl_surface_attach() in gdkglcontext-win32-egl.c, but never assigned
    it to draw_context_class->surface_attach.
  • 92d453e209 “win32: Remove unused function” (!8722): deleted it.
  • 4.20.0 is the first affected release. Through 4.22.x DComp was enabled by default, so this
    affected default setups. Since 914cb8dc (!9593, DComp opt-in) the shared attach fails without
    DComp (“OpenGL requires Direct Composition”) and GSK falls back to Cairo, which hides the bug
    unless GDK_DEBUG=dcomp is set.

Possible fix (untested)
In gdkglcontext-win32-egl.c:

  • surface_attach: chain up to GdkWin32GLContext, then call
    gdk_gl_context_set_egl_native_window (context, GDK_WIN32_GL_CONTEXT (context)->handle).
    That is the window WGL presents into (SwapBuffers (GetDC (handle))).
  • surface_detach: call gdk_gl_context_set_egl_native_window (context, NULL) before chaining
    up, since the parent destroys the HWND. GdkWin32GLContext’s surface_detach does not chain
    up to GdkGLContext’s, which is where the EGL surface would otherwise be torn down.

Workaround
GDK_DISABLE=egl. When WGL is unavailable, GSK then falls back to Cairo.