Cleanup "GDK/Win32: Try to fix initializing GLES contexts"
authorChun-wei Fan <fanchunwei@src.gnome.org>
Tue, 18 Jan 2022 02:41:28 +0000 (10:41 +0800)
committerChun-wei Fan <fanchunwei@src.gnome.org>
Wed, 19 Jan 2022 03:56:32 +0000 (11:56 +0800)
As per Benjamin's suggestions, cleanup the previous implementation on
initializing the GLES context on Windows, so that we use more items that are
already in GDK proper and integrate two functions into one.

gdk/win32/gdkdisplay-win32.c
gdk/win32/gdkdisplay-win32.h
gdk/win32/gdkglcontext-win32-wgl.c

index 14de69ff7b4f69ee7778120fd512826ddc38df6c..aee962bd80eec861423549ca49459b7a337a7062 100644 (file)
@@ -1175,17 +1175,19 @@ gdk_win32_display_get_setting (GdkDisplay  *display,
 #define EGL_PLATFORM_ANGLE_ANGLE          0x3202
 #endif
 
-static gboolean
-gdk_win32_display_init_gl_backend (GdkDisplay  *display,
-                                   GError     **error)
+static GdkGLContext *
+gdk_win32_display_init_gl (GdkDisplay  *display,
+                           GError     **error)
 {
-  gboolean result = FALSE;
   GdkWin32Display *display_win32 = GDK_WIN32_DISPLAY (display);
-  GdkWin32GLType gl_type = GDK_WIN32_GL_TYPE_NONE;
+  HDC init_gl_hdc = NULL;
+  gboolean is_egl  = FALSE;
 
   if (display_win32->dummy_context_wgl.hdc == NULL)
     display_win32->dummy_context_wgl.hdc = GetDC (display_win32->hwnd);
 
+  init_gl_hdc = display_win32->dummy_context_wgl.hdc;
+
   /*
    * No env vars set, do the regular GL initialization, first WGL and then EGL,
    * as WGL is the more tried-and-tested configuration.
@@ -1193,78 +1195,63 @@ gdk_win32_display_init_gl_backend (GdkDisplay  *display,
 
 #ifdef HAVE_EGL
   /*
-   * 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
+   * Disable defaulting to EGL as EGL is used more as a compatibility layer
    * on Windows rather than being a native citizen on Windows
    */
   if (GDK_DEBUG_CHECK (GL_EGL) || GDK_DEBUG_CHECK (GL_GLES))
     {
-      result = gdk_display_init_egl (display,
-                                     EGL_PLATFORM_ANGLE_ANGLE,
-                                     display_win32->dummy_context_wgl.hdc,
-                                     FALSE,
-                                     error);
-
-      if (result)
-        gl_type = GDK_WIN32_GL_TYPE_EGL;
+      if (gdk_display_init_egl (display,
+                                EGL_PLATFORM_ANGLE_ANGLE,
+                                init_gl_hdc,
+                                FALSE,
+                                error))
+        is_egl = TRUE;
     }
 #endif
 
-  if (!result)
+  if (!is_egl)
     {
       g_clear_error (error);
-      result = gdk_win32_display_init_wgl (display, error);
 
-      if (result)
-        gl_type = GDK_WIN32_GL_TYPE_WGL;
+      if (gdk_win32_display_init_wgl (display, error))
+        {
+          gdk_gl_backend_use (GDK_GL_WGL);
+          return g_object_new (GDK_TYPE_WIN32_GL_CONTEXT_WGL,
+                               "display", display,
+                               NULL);
+        }
     }
 
-
 #ifdef HAVE_EGL
-  if (!result)
+  if (!is_egl)
     {
       g_clear_error (error);
-      result = gdk_display_init_egl (display,
-                                     EGL_PLATFORM_ANGLE_ANGLE,
-                                     display_win32->dummy_context_wgl.hdc,
-                                     TRUE,
-                                     error);
-
-      if (result)
-        gl_type = GDK_WIN32_GL_TYPE_EGL;
-    }
-#endif
 
-  display_win32->gl_type = gl_type;
-  return result;
-}
-
-static GdkGLContext *
-gdk_win32_display_init_gl (GdkDisplay  *display,
-                           GError     **error)
-{
-  GdkWin32Display *display_win32 = GDK_WIN32_DISPLAY (display);
-  GdkGLContext *gl_context = NULL;
-
-  if (!gdk_win32_display_init_gl_backend (display, error))
-    return NULL;
+      if (gdk_display_init_egl (display,
+                                EGL_PLATFORM_ANGLE_ANGLE,
+                                init_gl_hdc,
+                                TRUE,
+                                error))
+        is_egl = TRUE;
+    }
 
-  if (display_win32->gl_type == GDK_WIN32_GL_TYPE_WGL)
-    gl_context = g_object_new (GDK_TYPE_WIN32_GL_CONTEXT_WGL, "display", display, NULL);
-#ifdef HAVE_EGL
-  else if (display_win32->gl_type == GDK_WIN32_GL_TYPE_EGL)
+  if (is_egl)
     {
-      gl_context = g_object_new (GDK_TYPE_WIN32_GL_CONTEXT_EGL, "display", display, NULL);
+      GdkGLContext *gl_context = NULL;
 
-      /* We want to use a GLES 3.0+ context */
+      /* We want to use a GLES 3.0+ context for libANGLE GLES */
+      gdk_gl_backend_use (GDK_GL_EGL);
+      gl_context = g_object_new (GDK_TYPE_WIN32_GL_CONTEXT_EGL,
+                                 "display", display,
+                                 NULL);
       gdk_gl_context_set_allowed_apis (gl_context, GDK_GL_API_GLES);
       gdk_gl_context_set_required_version (gl_context, 3, 0);
+
+      return gl_context;
     }
 #endif
 
-  g_return_val_if_fail (gl_context != NULL, NULL);
-
-  return gl_context;
+  g_return_val_if_reached (NULL);
 }
 
 /**
index 88757e03e0a39cac41c8267489f66ce8e7631320..19f11e5f9fef755071a3b89049bdb1f67b46f2a7 100644 (file)
@@ -105,13 +105,6 @@ typedef enum {
   GDK_WIN32_TABLET_INPUT_API_WINPOINTER
 } GdkWin32TabletInputAPI;
 
-typedef enum
-{
-  GDK_WIN32_GL_TYPE_NONE,
-  GDK_WIN32_GL_TYPE_WGL,
-  GDK_WIN32_GL_TYPE_EGL,
-} GdkWin32GLType;
-
 typedef struct
 {
   HDC hdc;
@@ -132,7 +125,6 @@ struct _GdkWin32Display
 
   /* WGL/OpenGL Items */
   GdkWin32GLDummyContextWGL dummy_context_wgl;
-  GdkWin32GLType gl_type;
   guint gl_version;
 
   GListModel *monitors;
index 9a90ccffc622805fba72364070ace8e9ce7ad85d..231f2a6a01ebca1098fcc4f05c80d664055d8b3b 100644 (file)
@@ -258,9 +258,6 @@ gdk_win32_display_init_wgl (GdkDisplay  *display,
   if (!gdk_gl_backend_can_be_used (GDK_GL_WGL, error))
     return FALSE;
 
-  if (display_win32->gl_type == GDK_WIN32_GL_TYPE_WGL)
-    return TRUE;
-
   /* acquire and cache dummy Window (HWND & HDC) and
    * dummy GL Context, it is used to query functions
    * and used for other stuff as well
@@ -724,7 +721,7 @@ gdk_win32_display_get_wgl_version (GdkDisplay *display,
   if (!GDK_IS_WIN32_DISPLAY (display))
     return FALSE;
 
-  if (!gdk_win32_display_init_wgl (display, NULL))
+  if (!gdk_gl_backend_can_be_used (GDK_GL_WGL, NULL))
     return FALSE;
 
   display_win32 = GDK_WIN32_DISPLAY (display);