[gtk/wip/otte/gleanup: 2/2] x11: Store the EGL config in the display




commit b27cce8e5730d0eadd93a615302346e4cc10d5e5
Author: Benjamin Otte <otte redhat com>
Date:   Wed Jun 9 13:42:19 2021 +0200

    x11: Store the EGL config in the display
    
    We only have one config, because we use the same Visual everywhere.
    Store this config in the GdkDisplayX11 strut for easy access.
    
    This also simplifies a lot of code as we can share Visual, Colormap, etc
    across surfaces.

 gdk/x11/gdkdisplay-x11.h   |  1 +
 gdk/x11/gdkglcontext-egl.c | 79 ++++++++++++++++++++++------------------------
 2 files changed, 38 insertions(+), 42 deletions(-)
---
diff --git a/gdk/x11/gdkdisplay-x11.h b/gdk/x11/gdkdisplay-x11.h
index a88a728112..a2936d7915 100644
--- a/gdk/x11/gdkdisplay-x11.h
+++ b/gdk/x11/gdkdisplay-x11.h
@@ -139,6 +139,7 @@ struct _GdkX11Display
   /* EGL information */
   /* We use gpointer here so we don't have to pull in EGL headers (which glx doesn't like) */
   /* EGLDisplay */ gpointer egl_display;
+  /* EGLConfig */ gpointer egl_config;
   int egl_version;
 
   /* Translation between X server time and system-local monotonic time */
diff --git a/gdk/x11/gdkglcontext-egl.c b/gdk/x11/gdkglcontext-egl.c
index fd6109f0a3..3dc86cd9ed 100644
--- a/gdk/x11/gdkglcontext-egl.c
+++ b/gdk/x11/gdkglcontext-egl.c
@@ -35,7 +35,6 @@ struct _GdkX11GLContextEGL
 {
   GdkX11GLContext parent_instance;
 
-  EGLConfig egl_config;
   EGLContext egl_context;
 };
 
@@ -190,6 +189,7 @@ static EGLSurface
 gdk_x11_display_get_egl_dummy_surface (GdkDisplay *display,
                                        EGLConfig   egl_config)
 {
+  GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
   DrawableInfo *info;
   XVisualInfo *xvisinfo;
   XSetWindowAttributes attrs;
@@ -209,10 +209,7 @@ gdk_x11_display_get_egl_dummy_surface (GdkDisplay *display,
   info->egl_config = egl_config;
 
   attrs.override_redirect = True;
-  attrs.colormap = XCreateColormap (info->xdisplay,
-                                    DefaultRootWindow (info->xdisplay),
-                                    xvisinfo->visual,
-                                    AllocNone);
+  attrs.colormap = gdk_x11_display_get_window_colormap (display_x11);
   attrs.border_pixel = 0;
 
   info->dummy_xwin =
@@ -220,9 +217,9 @@ gdk_x11_display_get_egl_dummy_surface (GdkDisplay *display,
                    DefaultRootWindow (info->xdisplay),
                    -100, -100, 1, 1,
                    0,
-                   xvisinfo->depth,
+                   gdk_x11_display_get_window_depth (display_x11),
                    CopyFromParent,
-                   xvisinfo->visual,
+                   gdk_x11_display_get_window_visual (display_x11),
                    CWOverrideRedirect | CWColormap | CWBorderPixel,
                    &attrs);
 
@@ -240,11 +237,10 @@ gdk_x11_display_get_egl_dummy_surface (GdkDisplay *display,
 }
 
 static EGLSurface
-gdk_x11_surface_get_egl_surface (GdkSurface *surface,
-                                 EGLConfig   config)
+gdk_x11_surface_get_egl_surface (GdkSurface *surface)
 {
   GdkDisplay *display = gdk_surface_get_display (surface);
-  EGLDisplay egl_display = gdk_x11_display_get_egl_display (display);
+  GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
   DrawableInfo *info;
 
   info = g_object_get_data (G_OBJECT (surface), "-gdk-x11-egl-drawable");
@@ -252,10 +248,10 @@ gdk_x11_surface_get_egl_surface (GdkSurface *surface,
     return info->egl_surface;
 
   info = g_new0 (DrawableInfo, 1);
-  info->egl_display = egl_display;
-  info->egl_config = config;
+  info->egl_display = display_x11->egl_display;
+  info->egl_config = display_x11->egl_config;
   info->egl_surface =
-    eglCreateWindowSurface (info->egl_display, config,
+    eglCreateWindowSurface (info->egl_display, info->egl_config,
                             (EGLNativeWindowType) gdk_x11_surface_get_xid (surface),
                             NULL);
 
@@ -271,7 +267,6 @@ gdk_x11_gl_context_egl_end_frame (GdkDrawContext *draw_context,
                                   cairo_region_t *painted)
 {
   GdkGLContext *context = GDK_GL_CONTEXT (draw_context);
-  GdkX11GLContextEGL *context_egl = GDK_X11_GL_CONTEXT_EGL (context);
   GdkSurface *surface = gdk_gl_context_get_surface (context);
   GdkDisplay *display = gdk_surface_get_display (surface);
   GdkX11Display *display_x11 = GDK_X11_DISPLAY (display);
@@ -284,7 +279,7 @@ gdk_x11_gl_context_egl_end_frame (GdkDrawContext *draw_context,
 
   gdk_gl_context_make_current (context);
 
-  egl_surface = gdk_x11_surface_get_egl_surface (surface, context_egl->egl_config);
+  egl_surface = gdk_x11_surface_get_egl_surface (surface);
 
   gdk_profiler_add_mark (GDK_PROFILER_CURRENT_TIME, 0, "x11", "swap buffers");
   if (display_x11->has_egl_swap_buffers_with_damage)
@@ -330,16 +325,14 @@ gdk_x11_gl_context_egl_get_damage (GdkGLContext *context)
     {
       GdkSurface *surface = gdk_draw_context_get_surface (GDK_DRAW_CONTEXT (context));
       GdkGLContext *shared = gdk_gl_context_get_shared_context (context);
-      GdkX11GLContextEGL *shared_egl;
       EGLSurface egl_surface;
       int buffer_age = 0;
 
       shared = gdk_gl_context_get_shared_context (context);
       if (shared == NULL)
         shared = context;
-      shared_egl = GDK_X11_GL_CONTEXT_EGL (shared);
 
-      egl_surface = gdk_x11_surface_get_egl_surface (surface, shared_egl->egl_config);
+      egl_surface = gdk_x11_surface_get_egl_surface (surface);
       gdk_gl_context_make_current (shared);
 
       eglQuerySurface (gdk_x11_display_get_egl_display (display),
@@ -463,7 +456,7 @@ gdk_x11_gl_context_egl_realize (GdkGLContext  *context,
 
   context_egl->egl_context =
     eglCreateContext (egl_display,
-                      context_egl->egl_config,
+                      display_x11->egl_config,
                       share != NULL
                         ? GDK_X11_GL_CONTEXT_EGL (share)->egl_context
                         : shared_data_context != NULL
@@ -488,7 +481,7 @@ gdk_x11_gl_context_egl_realize (GdkGLContext  *context,
 
       context_egl->egl_context =
         eglCreateContext (egl_display,
-                          context_egl->egl_config,
+                          display_x11->egl_config,
                           share != NULL
                             ? GDK_X11_GL_CONTEXT_EGL (share)->egl_context
                             : shared_data_context != NULL
@@ -626,17 +619,21 @@ gdk_x11_display_init_egl (GdkX11Display *display_x11)
 
 #define MAX_EGL_ATTRS   30
 
-static gboolean
-find_eglconfig_for_display (GdkDisplay  *display,
-                            EGLConfig   *egl_config_out,
-                            GError     **error)
+static EGLConfig
+gdk_x11_display_get_egl_config (GdkX11Display *display)
 {
   EGLint attrs[MAX_EGL_ATTRS];
   EGLint count;
-  EGLConfig egl_config;
   EGLDisplay egl_display;
   int i = 0;
 
+  if (display->egl_config)
+    return display->egl_config;
+
+  egl_display = gdk_x11_display_get_egl_display (GDK_DISPLAY (display));
+  if (egl_display == NULL)
+    return NULL;
+
   attrs[i++] = EGL_SURFACE_TYPE;
   attrs[i++] = EGL_WINDOW_BIT;
 
@@ -656,19 +653,13 @@ find_eglconfig_for_display (GdkDisplay  *display,
   g_assert (i < MAX_EGL_ATTRS);
 
   /* Pick first valid configuration that the driver returns us */
-  egl_display = gdk_x11_display_get_egl_display (display);
-  if (!eglChooseConfig (egl_display, attrs, &egl_config, 1, &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;
-    }
+  egl_display = gdk_x11_display_get_egl_display (GDK_DISPLAY (display));
+  if (eglChooseConfig (egl_display, attrs, &display->egl_config, 1, &count) && count >= 1)
+    return display->egl_config;
 
-  g_assert (egl_config_out);
-  *egl_config_out = egl_config;
+  display->egl_config = NULL;
 
-  return TRUE;
+  return NULL;
 }
 
 #undef MAX_EGL_ATTRS
@@ -685,16 +676,20 @@ gdk_x11_gl_context_egl_new (GdkSurface    *surface,
 
   display = gdk_surface_get_display (surface);
 
-  if (!find_eglconfig_for_display (display, &egl_config, error))
-    return NULL;
+  egl_config = gdk_x11_display_get_egl_config (GDK_X11_DISPLAY (display));
+  if (egl_config == NULL)
+    {
+      g_set_error_literal (error, GDK_GL_ERROR,
+                           GDK_GL_ERROR_UNSUPPORTED_FORMAT,
+                           _("No available configurations for the given pixel format"));
+      return NULL;
+    }
 
   context = g_object_new (GDK_TYPE_X11_GL_CONTEXT_EGL,
                           "surface", surface,
                           "shared-context", share,
                           NULL);
 
-  context->egl_config = egl_config;
-
   return GDK_X11_GL_CONTEXT (context);
 }
 
@@ -727,13 +722,13 @@ gdk_x11_gl_context_egl_make_current (GdkDisplay   *display,
   surface = gdk_gl_context_get_surface (context);
 
   if (context_x11->is_attached || gdk_draw_context_is_in_frame (GDK_DRAW_CONTEXT (context)))
-    egl_surface = gdk_x11_surface_get_egl_surface (surface, context_egl->egl_config);
+    egl_surface = gdk_x11_surface_get_egl_surface (surface);
   else
     {
       if (display_x11->has_egl_surfaceless_context)
         egl_surface = EGL_NO_SURFACE;
       else
-        egl_surface = gdk_x11_display_get_egl_dummy_surface (display, context_egl->egl_config);
+        egl_surface = gdk_x11_display_get_egl_dummy_surface (display, display_x11->egl_config);
     }
 
   GDK_DISPLAY_NOTE (display, OPENGL,


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