x11: Don't share cached GLX visuals with GTK3
authorBenjamin Otte <otte@redhat.com>
Thu, 3 Jun 2021 17:20:51 +0000 (19:20 +0200)
committerBenjamin Otte <otte@redhat.com>
Thu, 22 Jul 2021 14:06:05 +0000 (16:06 +0200)
We don't want to bind ourselves to GTK3 - both because we don't want to
accidentally cause bugs in a different codebase and because we want to
deviate from it.

While doing so, also store visuals as visuals and not as integers.
And only store one Visual because GTK4 only uses one visual.

And then remove the code that is leftover for dealing with the
compatibility Visual for GTK3.

PS: I'm kinda proud of my STRINGIFY_WITHOUT_BRACKETS hack.

gdk/x11/gdkglcontext-glx.c
gdk/x11/gdkscreen-x11.h
gdk/x11/gdkvisual-x11.c

index eece4ce3602c32d615e58548c2411dba273ec556..f18b63f814574d8047a6be9b5e66ecd93f602d67 100644 (file)
@@ -947,38 +947,36 @@ pick_better_visual_for_gl (GdkX11Screen *x11_screen,
   return compatible;
 }
 
-static gboolean
-get_cached_gl_visuals (GdkDisplay *display, int *system, int *rgba)
+#define STRINGIFY_WITHOUT_BRACKETS(x) G_STRINGIFY x
+#define CACHED_VISUAL_ATOM_NAME "GDK" STRINGIFY_WITHOUT_BRACKETS(GDK_MAJOR_VERSION) "_GLX_VISUAL"
+
+static VisualID
+get_cached_gl_visual (GdkDisplay *display)
 {
-  gboolean found;
   Atom type_return;
   int format_return;
   gulong nitems_return;
   gulong bytes_after_return;
   guchar *data = NULL;
   Display *dpy;
+  VisualID result;
 
   dpy = gdk_x11_display_get_xdisplay (display);
-
-  found = FALSE;
+  result = 0;
 
   gdk_x11_display_error_trap_push (display);
   if (XGetWindowProperty (dpy, DefaultRootWindow (dpy),
-                          gdk_x11_get_xatom_by_name_for_display (display, "GDK_VISUALS"),
-                          0, 2, False, XA_INTEGER, &type_return,
+                          gdk_x11_get_xatom_by_name_for_display (display, CACHED_VISUAL_ATOM_NAME),
+                          0, 1, False, XA_VISUALID, &type_return,
                           &format_return, &nitems_return,
                           &bytes_after_return, &data) == Success)
     {
-      if (type_return == XA_INTEGER &&
+      if (type_return == XA_VISUALID &&
           format_return == 32 &&
-          nitems_return == 2 &&
+          nitems_return == 1 &&
           data != NULL)
         {
-          long *visuals = (long *) data;
-
-          *system = (int)visuals[0];
-          *rgba = (int)visuals[1];
-          found = TRUE;
+          result = *(unsigned long *) data;
         }
     }
   gdk_x11_display_error_trap_pop_ignored (display);
@@ -986,25 +984,24 @@ get_cached_gl_visuals (GdkDisplay *display, int *system, int *rgba)
   if (data)
     XFree (data);
 
-  return found;
+  return result;
 }
 
 static void
-save_cached_gl_visuals (GdkDisplay *display, int system, int rgba)
+save_cached_gl_visual (GdkDisplay *display, VisualID visual)
 {
-  long visualdata[2];
+  unsigned long visualdata[1];
   Display *dpy;
 
   dpy = gdk_x11_display_get_xdisplay (display);
 
-  visualdata[0] = system;
-  visualdata[1] = rgba;
+  visualdata[0] = visual;
 
   gdk_x11_display_error_trap_push (display);
   XChangeProperty (dpy, DefaultRootWindow (dpy),
-                   gdk_x11_get_xatom_by_name_for_display (display, "GDK_VISUALS"),
-                   XA_INTEGER, 32, PropModeReplace,
-                   (unsigned char *)visualdata, 2);
+                   gdk_x11_get_xatom_by_name_for_display (display, CACHED_VISUAL_ATOM_NAME),
+                   XA_VISUALID, 32, PropModeReplace,
+                   (unsigned char *) visualdata, 2);
   gdk_x11_display_error_trap_pop_ignored (display);
 }
 
@@ -1016,7 +1013,7 @@ gdk_x11_screen_update_visuals_for_glx (GdkX11Screen *x11_screen)
   Display *dpy;
   struct glvisualinfo *gl_info;
   int i;
-  int system_visual_id, rgba_visual_id;
+  int rgba_visual_id;
 
   display = x11_screen->display;
   display_x11 = GDK_X11_DISPLAY (display);
@@ -1027,20 +1024,20 @@ gdk_x11_screen_update_visuals_for_glx (GdkX11Screen *x11_screen)
 
   /* We save the default visuals as a property on the root window to avoid
      having to initialize GL each time, as it may not be used later. */
-  if (get_cached_gl_visuals (display, &system_visual_id, &rgba_visual_id))
+  rgba_visual_id = get_cached_gl_visual (display);
+  if (rgba_visual_id)
     {
       for (i = 0; i < x11_screen->nvisuals; i++)
         {
           GdkX11Visual *visual = x11_screen->visuals[i];
           int visual_id = gdk_x11_visual_get_xvisual (visual)->visualid;
 
-          if (visual_id == system_visual_id)
-            x11_screen->system_visual = visual;
           if (visual_id == rgba_visual_id)
-            x11_screen->rgba_visual = visual;
+            {
+              x11_screen->rgba_visual = visual;
+              return;
+            }
         }
-
-      return;
     }
 
   if (!gdk_x11_screen_init_glx (x11_screen))
@@ -1079,17 +1076,13 @@ gdk_x11_screen_update_visuals_for_glx (GdkX11Screen *x11_screen)
       XFree (visual_list);
     }
 
-  x11_screen->system_visual = pick_better_visual_for_gl (x11_screen, gl_info, x11_screen->system_visual);
   if (x11_screen->rgba_visual)
-    x11_screen->rgba_visual = pick_better_visual_for_gl (x11_screen, gl_info, x11_screen->rgba_visual);
+    {
+      x11_screen->rgba_visual = pick_better_visual_for_gl (x11_screen, gl_info, x11_screen->rgba_visual);
+      save_cached_gl_visual (display, gdk_x11_visual_get_xvisual (x11_screen->rgba_visual)->visualid);
+    }
 
   g_free (gl_info);
-
-  save_cached_gl_visuals (display,
-                          gdk_x11_visual_get_xvisual (x11_screen->system_visual)->visualid,
-                          x11_screen->rgba_visual
-                            ? gdk_x11_visual_get_xvisual (x11_screen->rgba_visual)->visualid
-                            : 0);
 }
 
 GdkX11GLContext *
index 2965385d2fbda978e36f272dedb619bbd56db36c..03cbee667869ba40c4571284536a1b04af25240a 100644 (file)
@@ -74,7 +74,6 @@ struct _GdkX11Screen
   /* Visual Part */
   int nvisuals;
   GdkX11Visual **visuals;
-  GdkX11Visual *system_visual;
   int available_depths[7];
   GdkVisualType available_types[6];
   gint16 navailable_depths;
index c97913ad1f151ee954831722cfcbbe888c9eea1c..d8dba33c2d8bb487a7bafdb2658b9675ca39e473 100644 (file)
@@ -65,7 +65,6 @@ _gdk_x11_screen_init_visuals (GdkX11Screen *x11_screen)
 
   XVisualInfo *visual_list;
   XVisualInfo visual_template;
-  Visual *default_xvisual;
   GdkX11Visual **visuals;
   int nxvisuals;
   int nvisuals;
@@ -79,8 +78,6 @@ _gdk_x11_screen_init_visuals (GdkX11Screen *x11_screen)
   for (i = 0; i < nxvisuals; i++)
     visuals[i] = g_object_new (GDK_TYPE_X11_VISUAL, NULL);
 
-  default_xvisual = DefaultVisual (x11_screen->xdisplay, x11_screen->screen_num);
-
   nvisuals = 0;
   for (i = 0; i < nxvisuals; i++)
     {
@@ -143,9 +140,6 @@ _gdk_x11_screen_init_visuals (GdkX11Screen *x11_screen)
 
   for (i = 0; i < nvisuals; i++)
     {
-      if (default_xvisual->visualid == GDK_X11_VISUAL (visuals[i])->xvisual->visualid)
-        x11_screen->system_visual = visuals[i];
-
       /* For now, we only support 8888 ARGB for the "rgba visual".
        * Additional formats (like ABGR) could be added later if they
        * turn up.