[gtk/wip.win32.fixes: 145/146] GDK-Win32: Port to common EGL handling code




commit 726602b240565b55c6ab9a2908f8c64fc114dee2
Author: Chun-wei Fan <fanchunwei src gnome org>
Date:   Thu Oct 7 15:44:38 2021 +0800

    GDK-Win32: Port to common EGL handling code
    
    This will port the EGL code in GDK-Win32 to use the common GDK code to
    initialize EGL.  However, at the current state, although EGL is
    correctly initialized, this code is disabled for now since
    gdk_gl_context_make_current() fails as the shaders do not work for EGL
    via libANGLE on Windows.
    
    We can now clean things up in gdkglcontext-win32-egl.c as a result.

 gdk/win32/gdkdisplay-win32.c       |  64 +++++-----
 gdk/win32/gdkdisplay-win32.h       |   8 --
 gdk/win32/gdkglcontext-win32-egl.c | 255 +------------------------------------
 gdk/win32/gdkglcontext-win32-wgl.c |   3 -
 gdk/win32/gdksurface-win32.c       |  49 -------
 gdk/win32/gdksurface-win32.h       |   2 -
 6 files changed, 42 insertions(+), 339 deletions(-)
---
diff --git a/gdk/win32/gdkdisplay-win32.c b/gdk/win32/gdkdisplay-win32.c
index e89e35bb14..dd40b5c13a 100644
--- a/gdk/win32/gdkdisplay-win32.c
+++ b/gdk/win32/gdkdisplay-win32.c
@@ -645,14 +645,6 @@ gdk_win32_display_dispose (GObject *object)
 {
   GdkWin32Display *display_win32 = GDK_WIN32_DISPLAY (object);
 
-#ifdef HAVE_EGL
-  if (display_win32->egl_disp != EGL_NO_DISPLAY)
-    {
-      eglTerminate (display_win32->egl_disp);
-      display_win32->egl_disp = EGL_NO_DISPLAY;
-    }
-#endif
-
   if (display_win32->hwnd != NULL)
     {
       if (display_win32->dummy_context_wgl.hglrc != NULL)
@@ -1146,23 +1138,52 @@ gdk_win32_display_get_setting (GdkDisplay  *display,
   return _gdk_win32_get_setting (name, value);
 }
 
+#ifndef EGL_PLATFORM_ANGLE_ANGLE
+#define EGL_PLATFORM_ANGLE_ANGLE          0x3202
+#endif
+
 static gboolean
 gdk_win32_display_init_gl_backend (GdkDisplay  *display,
                                    GError     **error)
 {
   gboolean result = FALSE;
+  GdkWin32Display *display_win32 = GDK_WIN32_DISPLAY (display);
+
+  if (display_win32->dummy_context_wgl.hdc == NULL)
+    display_win32->dummy_context_wgl.hdc = GetDC (display_win32->hwnd);
 
   /* No env vars set, do the regular GL initialization, first WGL and then EGL,
    * as WGL is the more tried-and-tested configuration.
    */
 
-  result = gdk_win32_display_init_wgl (display, error);
+/*
+ * Disable defaulting to EGL for now, since shaders need to be fixed for
+ * usage against libANGLE EGL.  EGL is used more as a compatibility layer
+ * on Windows rather than being a native citizen on Windows
+ */
+#if 0
+  result = gdk_display_init_egl (display,
+                                 EGL_PLATFORM_ANGLE_ANGLE,
+                                 display_win32->dummy_context_wgl.hdc,
+                                 FALSE,
+                                 error);
+#endif
+
+  if (!result)
+    {
+      g_clear_error (error);
+      result = gdk_win32_display_init_wgl (display, error);
+    }
 
 #ifdef HAVE_EGL
   if (!result)
     {
       g_clear_error (error);
-      result = gdk_win32_display_init_egl (display, error);
+      result = gdk_display_init_egl (display,
+                                     EGL_PLATFORM_ANGLE_ANGLE,
+                                     display_win32->dummy_context_wgl.hdc,
+                                     TRUE,
+                                     error);
     }
 #endif
 
@@ -1179,13 +1200,12 @@ gdk_win32_display_init_gl (GdkDisplay  *display,
   if (!gdk_win32_display_init_gl_backend (display, error))
     return NULL;
 
+  if (display_win32->wgl_pixel_format != 0)
+    gl_context = g_object_new (GDK_TYPE_WIN32_GL_CONTEXT_WGL, "display", display, NULL);
 #ifdef HAVE_EGL
-  if (display_win32->egl_disp)
+  else if (gdk_display_get_egl_display (display))
     gl_context = g_object_new (GDK_TYPE_WIN32_GL_CONTEXT_EGL, "display", display, NULL);
-  else
 #endif
-  if (display_win32->wgl_pixel_format != 0)
-    gl_context = g_object_new (GDK_TYPE_WIN32_GL_CONTEXT_WGL, "display", display, NULL);
 
   g_return_val_if_fail (gl_context != NULL, NULL);
 
@@ -1203,23 +1223,9 @@ gdk_win32_display_init_gl (GdkDisplay  *display,
 gpointer
 gdk_win32_display_get_egl_display (GdkDisplay *display)
 {
-#ifdef HAVE_EGL
-  GdkWin32Display *display_win32;
-#endif
-
   g_return_val_if_fail (GDK_IS_WIN32_DISPLAY (display), NULL);
 
-#ifdef HAVE_EGL
-  display_win32 = GDK_WIN32_DISPLAY (display);
-
-  if (display_win32->wgl_pixel_format != 0)
-    return NULL;
-
-  return display_win32->egl_disp;
-#else
-  /* no EGL support */
-  return NULL;
-#endif
+  return gdk_display_get_egl_display (display);
 }
 
 static void
diff --git a/gdk/win32/gdkdisplay-win32.h b/gdk/win32/gdkdisplay-win32.h
index d8d3d121e6..22532cf39a 100644
--- a/gdk/win32/gdkdisplay-win32.h
+++ b/gdk/win32/gdkdisplay-win32.h
@@ -135,14 +135,6 @@ struct _GdkWin32Display
   int wgl_pixel_format;
   guint gl_version;
 
-#ifdef HAVE_EGL
-  /* EGL (Angle) Items */
-  guint egl_version;
-  EGLDisplay egl_disp;
-  EGLConfig egl_config;
-  HDC hdc_egl_temp;
-#endif
-
   GListModel *monitors;
 
   guint hasWglARBCreateContext : 1;
diff --git a/gdk/win32/gdkglcontext-win32-egl.c b/gdk/win32/gdkglcontext-win32-egl.c
index 1507dd8225..72b3381dfd 100644
--- a/gdk/win32/gdkglcontext-win32-egl.c
+++ b/gdk/win32/gdkglcontext-win32-egl.c
@@ -53,30 +53,6 @@ typedef struct _GdkWin32GLContextClass    GdkWin32GLContextEGLClass;
 
 G_DEFINE_TYPE (GdkWin32GLContextEGL, gdk_win32_gl_context_egl, GDK_TYPE_WIN32_GL_CONTEXT)
 
-static void
-gdk_win32_gl_context_egl_dispose (GObject *gobject)
-{
-  GdkGLContext *context = GDK_GL_CONTEXT (gobject);
-  GdkWin32GLContextEGL *context_egl = GDK_WIN32_GL_CONTEXT_EGL (gobject);
-  GdkWin32Display *display_win32 = GDK_WIN32_DISPLAY (gdk_gl_context_get_display (context));
-  GdkSurface *surface = gdk_gl_context_get_surface (context);
-
-  if (display_win32 != NULL)
-    {
-      if (eglGetCurrentContext () == context_egl->egl_context)
-        eglMakeCurrent(display_win32->egl_disp, EGL_NO_SURFACE, EGL_NO_SURFACE,
-                       EGL_NO_CONTEXT);
-
-      GDK_NOTE (OPENGL, g_message ("Destroying EGL (ANGLE) context"));
-
-      eglDestroyContext (display_win32->egl_disp,
-                         context_egl->egl_context);
-      context_egl->egl_context = EGL_NO_CONTEXT;
-    }
-
-  G_OBJECT_CLASS (gdk_win32_gl_context_egl_parent_class)->dispose (gobject);
-}
-
 static gboolean
 is_egl_force_redraw (GdkSurface *surface)
 {
@@ -109,7 +85,7 @@ gdk_win32_gl_context_egl_end_frame (GdkDrawContext *draw_context,
   GdkGLContext *context = GDK_GL_CONTEXT (draw_context);
   GdkWin32GLContextEGL *context_egl = GDK_WIN32_GL_CONTEXT_EGL (context);
   GdkSurface *surface = gdk_gl_context_get_surface (context);
-  GdkWin32Display *display_win32 = (GDK_WIN32_DISPLAY (gdk_gl_context_get_display (context)));
+  GdkDisplay *display = gdk_gl_context_get_display (context);
   cairo_rectangle_int_t whole_window;
   EGLSurface egl_surface;
 
@@ -122,7 +98,7 @@ gdk_win32_gl_context_egl_end_frame (GdkDrawContext *draw_context,
                                         gdk_surface_get_height (surface)
                    };
 
-  egl_surface = gdk_win32_surface_get_egl_surface (surface, display_win32->egl_config, FALSE);
+  egl_surface = gdk_surface_get_egl_surface (surface);
 
   if (is_egl_force_redraw (surface))
     {
@@ -135,167 +111,7 @@ gdk_win32_gl_context_egl_end_frame (GdkDrawContext *draw_context,
       reset_egl_force_redraw (surface);
     }
 
-  eglSwapBuffers (display_win32->egl_disp, egl_surface);
-}
-
-#ifndef EGL_PLATFORM_ANGLE_ANGLE
-#define EGL_PLATFORM_ANGLE_ANGLE          0x3202
-#endif
-
-#ifndef EGL_PLATFORM_ANGLE_TYPE_ANGLE
-#define EGL_PLATFORM_ANGLE_TYPE_ANGLE     0x3203
-#endif
-
-#ifndef EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE
-#define EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE 0x3208
-#endif
-
-static EGLDisplay
-gdk_win32_get_egl_display (GdkWin32Display *display)
-{
-  EGLDisplay disp;
-
-  if (epoxy_has_egl_extension (NULL, "EGL_EXT_platform_base"))
-    {
-      PFNEGLGETPLATFORMDISPLAYEXTPROC getPlatformDisplay = (void *) eglGetProcAddress 
("eglGetPlatformDisplayEXT");
-      if (getPlatformDisplay)
-        {
-          EGLint disp_attr[] = {EGL_PLATFORM_ANGLE_TYPE_ANGLE, EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE, 
EGL_NONE};
-
-          disp = getPlatformDisplay (EGL_PLATFORM_ANGLE_ANGLE, display->hdc_egl_temp, disp_attr);
-
-          if (disp != EGL_NO_DISPLAY)
-            return disp;
-        }
-    }
-
-  return eglGetDisplay (display->hdc_egl_temp);
-}
-
-#define MAX_EGL_ATTRS   30
-
-static gboolean
-find_eglconfig_for_window (GdkWin32Display  *display,
-                           EGLConfig        *egl_config_out,
-                           EGLint           *min_swap_interval_out,
-                           GError          **error)
-{
-  EGLint attrs[MAX_EGL_ATTRS];
-  EGLint count;
-  EGLConfig *configs, chosen_config;
-
-  int i = 0;
-
-  EGLDisplay egl_disp = display->egl_disp;
-
-  attrs[i++] = EGL_CONFORMANT;
-  attrs[i++] = EGL_OPENGL_ES2_BIT;
-  attrs[i++] = EGL_SURFACE_TYPE;
-  attrs[i++] = EGL_WINDOW_BIT;
-
-  attrs[i++] = EGL_COLOR_BUFFER_TYPE;
-  attrs[i++] = EGL_RGB_BUFFER;
-
-  attrs[i++] = EGL_RED_SIZE;
-  attrs[i++] = 1;
-  attrs[i++] = EGL_GREEN_SIZE;
-  attrs[i++] = 1;
-  attrs[i++] = EGL_BLUE_SIZE;
-  attrs[i++] = 1;
-  attrs[i++] = EGL_ALPHA_SIZE;
-  attrs[i++] = 1;
-
-  attrs[i++] = EGL_NONE;
-  g_assert (i < MAX_EGL_ATTRS);
-
-  if (!eglChooseConfig (display->egl_disp, attrs, NULL, 0, &count) || count < 1)
-    {
-      g_set_error_literal (error, GDK_GL_ERROR,
-                           GDK_GL_ERROR_UNSUPPORTED_FORMAT,
-                           _("No available configurations for the given pixel format"));
-      return FALSE;
-    }
-
-  configs = g_new (EGLConfig, count);
-
-  if (!eglChooseConfig (display->egl_disp, attrs, configs, count, &count) || count < 1)
-    {
-      g_set_error_literal (error, GDK_GL_ERROR,
-                           GDK_GL_ERROR_UNSUPPORTED_FORMAT,
-                           _("No available configurations for the given pixel format"));
-      return FALSE;
-    }
-
-  /* Pick first valid configuration i guess? */
-  chosen_config = configs[0];
-
-  if (!eglGetConfigAttrib (display->egl_disp, chosen_config,
-                           EGL_MIN_SWAP_INTERVAL, min_swap_interval_out))
-    {
-      g_set_error_literal (error, GDK_GL_ERROR,
-                           GDK_GL_ERROR_NOT_AVAILABLE,
-                           "Could not retrieve the minimum swap interval");
-      g_free (configs);
-      return FALSE;
-    }
-
-  if (egl_config_out != NULL)
-    *egl_config_out = chosen_config;
-
-  g_free (configs);
-
-  return TRUE;
-}
-
-gboolean
-gdk_win32_display_init_egl (GdkDisplay  *display,
-                            GError     **error)
-{
-  GdkWin32Display *display_win32 = GDK_WIN32_DISPLAY (display);
-  int best_idx = 0;
-  EGLDisplay egl_disp;
-
-  if (!gdk_gl_backend_can_be_used (GDK_GL_EGL, error))
-    return FALSE;
-
-  if (display_win32->egl_disp != EGL_NO_DISPLAY)
-    return TRUE;
-  
-  egl_disp = gdk_win32_get_egl_display (display_win32);
-
-  if (egl_disp == EGL_NO_DISPLAY)
-    return FALSE;
-
-  if (!eglInitialize (egl_disp, NULL, NULL))
-    {
-      eglTerminate (egl_disp);
-      egl_disp = EGL_NO_DISPLAY;
-      g_set_error_literal (error, GDK_GL_ERROR,
-                           GDK_GL_ERROR_NOT_AVAILABLE,
-                           _("No GL implementation is available"));
-      return FALSE;
-    }
-
-  display_win32->egl_disp = egl_disp;
-  display_win32->egl_version = epoxy_egl_version (egl_disp);
-
-  eglBindAPI (EGL_OPENGL_ES_API);
-
-  display_win32->hasEglSurfacelessContext =
-    epoxy_has_egl_extension (egl_disp, "EGL_KHR_surfaceless_context");
-
-  GDK_NOTE (OPENGL,
-            g_print ("EGL API version %d.%d found\n"
-                     " - Vendor: %s\n"
-                     " - Checked extensions:\n"
-                     "\t* EGL_KHR_surfaceless_context: %s\n",
-                     display_win32->egl_version / 10,
-                     display_win32->egl_version % 10,
-                     eglQueryString (display_win32->egl_disp, EGL_VENDOR),
-                     display_win32->hasEglSurfacelessContext ? "yes" : "no"));
-
-  return find_eglconfig_for_window (display_win32, &display_win32->egl_config,
-                                   &display_win32->egl_min_swap_interval, error);
+  eglSwapBuffers (gdk_display_get_egl_display (display), egl_surface);
 }
 
 #define N_EGL_ATTRS     16
@@ -355,7 +171,8 @@ gdk_win32_gl_context_egl_realize (GdkGLContext *context,
   GdkSurface *surface = gdk_gl_context_get_surface (context);
   GdkWin32Surface *impl = GDK_WIN32_SURFACE (surface);
   GdkDisplay *display = gdk_gl_context_get_display (context);
-  GdkWin32Display *display_win32 = GDK_WIN32_DISPLAY (display);
+  EGLDisplay egl_display = gdk_display_get_egl_display (display);
+  EGLConfig egl_config = gdk_display_get_egl_config (display);
   GdkGLContext *share = gdk_display_get_gl_context (display);
 
   gdk_gl_context_get_required_version (context, &major, &minor);
@@ -386,8 +203,8 @@ gdk_win32_gl_context_egl_realize (GdkGLContext *context,
                                compat_bit ? "yes" : "no",
                                legacy_bit ? "yes" : "no"));
 
-  ctx = create_egl_context (display_win32->egl_disp,
-                            display_win32->egl_config,
+  ctx = create_egl_context (egl_display,
+                            egl_config,
                             share,
                             flags,
                             major,
@@ -415,60 +232,6 @@ gdk_win32_gl_context_egl_realize (GdkGLContext *context,
   return TRUE;
 }
 
-static gboolean
-gdk_win32_gl_context_egl_clear_current (GdkGLContext *context)
-{
-  GdkDisplay *display = gdk_gl_context_get_display (context);
-  GdkWin32Display *display_win32 = GDK_WIN32_DISPLAY (display);
-
-  if (display_win32->egl_disp != EGL_NO_DISPLAY)
-    return eglMakeCurrent (display_win32->egl_disp,
-                           EGL_NO_SURFACE, 
-                           EGL_NO_SURFACE, 
-                           EGL_NO_CONTEXT);
-  else
-    return TRUE;
-}
-
-static gboolean
-gdk_win32_gl_context_egl_make_current (GdkGLContext *context,
-                                       gboolean      surfaceless)
-{
-  GdkWin32GLContextEGL *context_egl = GDK_WIN32_GL_CONTEXT_EGL (context);
-  GdkDisplay *display = gdk_gl_context_get_display (context);
-  GdkWin32Display *display_win32 = GDK_WIN32_DISPLAY (display);
-  GdkSurface *surface;
-
-  gboolean do_frame_sync = FALSE;
-
-  EGLSurface egl_surface;
-
-  surface = gdk_gl_context_get_surface (context);
-
-  if (!surfaceless)
-    egl_surface = gdk_win32_surface_get_egl_surface (surface, display_win32->egl_config, FALSE);
-  else
-    {
-      if (display_win32->hasEglSurfacelessContext)
-        egl_surface = EGL_NO_SURFACE;
-      else
-        egl_surface = gdk_win32_surface_get_egl_surface (surface, display_win32->egl_config, TRUE);
-    }
-
-  if (!eglMakeCurrent (display_win32->egl_disp,
-                       egl_surface,
-                       egl_surface,
-                       context_egl->egl_context))
-    return FALSE;
-
-  if (display_win32->egl_min_swap_interval == 0)
-    eglSwapInterval (display_win32->egl_disp, 0);
-  else
-    g_debug ("Can't disable GL swap interval");
-
-  return TRUE;
-}
-
 static void
 gdk_win32_gl_context_egl_begin_frame (GdkDrawContext *draw_context,
                                       gboolean        prefers_high_depth,
@@ -489,13 +252,9 @@ gdk_win32_gl_context_egl_class_init (GdkWin32GLContextClass *klass)
   context_class->backend_type = GDK_GL_EGL;
 
   context_class->realize = gdk_win32_gl_context_egl_realize;
-  context_class->make_current = gdk_win32_gl_context_egl_make_current;
-  context_class->clear_current = gdk_win32_gl_context_egl_clear_current;
 
   draw_context_class->begin_frame = gdk_win32_gl_context_egl_begin_frame;
   draw_context_class->end_frame = gdk_win32_gl_context_egl_end_frame;
-
-  gobject_class->dispose = gdk_win32_gl_context_egl_dispose;
 }
 
 static void
diff --git a/gdk/win32/gdkglcontext-win32-wgl.c b/gdk/win32/gdkglcontext-win32-wgl.c
index 0edc40c55a..d443a2c7ff 100644
--- a/gdk/win32/gdkglcontext-win32-wgl.c
+++ b/gdk/win32/gdkglcontext-win32-wgl.c
@@ -226,9 +226,6 @@ gdk_init_dummy_wgl_context (GdkWin32Display *display_win32)
   gboolean set_pixel_format_result = FALSE;
   int best_idx = 0;
 
-  if (display_win32->dummy_context_wgl.hdc == NULL)
-    display_win32->dummy_context_wgl.hdc = GetDC (display_win32->hwnd);
-
   memset (&pfd, 0, sizeof (PIXELFORMATDESCRIPTOR));
 
   best_idx = get_wgl_pfd (display_win32->dummy_context_wgl.hdc, &pfd, NULL);
diff --git a/gdk/win32/gdksurface-win32.c b/gdk/win32/gdksurface-win32.c
index 72a794beab..379f5886a7 100644
--- a/gdk/win32/gdksurface-win32.c
+++ b/gdk/win32/gdksurface-win32.c
@@ -689,22 +689,6 @@ gdk_win32_surface_destroy (GdkSurface *window,
       gdk_win32_surface_set_transient_for (child, NULL);
     }
 
-#ifdef HAVE_EGL
-  GdkWin32Display *display = GDK_WIN32_DISPLAY (gdk_surface_get_display (window));
-
-  /* Get rid of any EGLSurfaces that we might have created */
-  if (surface->egl_surface != EGL_NO_SURFACE)
-    {
-      eglDestroySurface (display->egl_disp, surface->egl_surface);
-      surface->egl_surface = EGL_NO_SURFACE;
-    }
-  if (surface->egl_dummy_surface != EGL_NO_SURFACE)
-    {
-      eglDestroySurface (display->egl_disp, surface->egl_dummy_surface);
-      surface->egl_dummy_surface = EGL_NO_SURFACE;
-    }
-#endif
-
   /* Remove ourself from our transient owner */
   if (surface->transient_owner != NULL)
     {
@@ -5099,39 +5083,6 @@ gdk_win32_drag_surface_iface_init (GdkDragSurfaceInterface *iface)
   iface->present = gdk_win32_drag_surface_present;
 }
 
-#ifdef HAVE_EGL
-EGLSurface
-gdk_win32_surface_get_egl_surface (GdkSurface *surface,
-                                   EGLConfig   config,
-                                   gboolean    is_dummy)
-{
-  GdkWin32Display *display = GDK_WIN32_DISPLAY (gdk_surface_get_display (surface));
-  GdkWin32Surface *impl = GDK_WIN32_SURFACE (surface);
-
-  if (is_dummy)
-    {
-      if (impl->egl_dummy_surface == EGL_NO_SURFACE)
-        {
-          EGLint attribs[] = {EGL_WIDTH, 1, EGL_WIDTH, 1, EGL_NONE};
-          impl->egl_dummy_surface = eglCreatePbufferSurface (display->egl_disp,
-                                                             config,
-                                                             attribs);
-        }
-      return impl->egl_dummy_surface;
-    }
-  else
-    {
-      if (impl->egl_surface == EGL_NO_SURFACE)
-        impl->egl_surface = eglCreateWindowSurface (display->egl_disp,
-                                                    config,
-                                                    GDK_SURFACE_HWND (surface),
-                                                    NULL);
-
-      return impl->egl_surface;
-    }
-
-}
-#endif
 
 static void
 gdk_win32_surface_get_queued_window_rect (GdkSurface *surface,
diff --git a/gdk/win32/gdksurface-win32.h b/gdk/win32/gdksurface-win32.h
index 6039698b0f..08bd0a20b7 100644
--- a/gdk/win32/gdksurface-win32.h
+++ b/gdk/win32/gdksurface-win32.h
@@ -340,8 +340,6 @@ struct _GdkWin32Surface
   gboolean force_recompute_size;
 
 #ifdef HAVE_EGL
-  EGLSurface egl_surface;
-  EGLSurface egl_dummy_surface;
   guint egl_force_redraw_all : 1;
 #endif
 };


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]