Subject: bug#57364: [PATCH] (was: bug#57364: 28.1.91; asynchronous X server error when creating a second frame on alternate DISPLAY)

From: andrés ramírez <[email-masked]>
Date: Tue, 30 Aug 2022 07:29:50 +0000

Hi. Po.

>>>>> "Po" == Po Lu <[email-masked]> writes:

    Po> andrés ramírez <[email-masked]> writes:

[...]

    Po> We already know it's cairo-side, since it goes away upon switching Cairo backends, and Emacs
    Po> doesn't make RenderFreeGlyphs requests itself.

Ok. Should we let the cairo guys know about it. For giving them the
opportunity of fixing it?.

    Po> That's unnecessary, since the Cairo XCB surface is present everywhere.  I'd rather put a
    Po> mention of the crash in etc/PROBLEMS for Emacs 28.

Are You going to apply the patch?

--- /tmp/emacs-28.1.91/configure.ac.bak	2022-08-01 06:29:22.000000000 -0500
+++ /tmp/emacs-28.1.91/configure.ac	2022-08-30 00:53:07.706555433 -0500
@@ -3320,6 +3320,13 @@
     CAIRO_MODULE="cairo >= $CAIRO_REQUIRED"
     EMACS_CHECK_MODULES(CAIRO, $CAIRO_MODULE)
     if test $HAVE_CAIRO = yes; then
+      CAIRO_XCB_MODULE="cairo-xcb >= $CAIRO_REQUIRED"
+      EMACS_CHECK_MODULES(CAIRO_XCB, $CAIRO_XCB_MODULE)
+      if test $HAVE_CAIRO_XCB = yes; then
+	CAIRO_CFLAGS="$CAIRO_CFLAGS $CAIRO_XCB_CFLAGS"
+	CAIRO_LIBS="$CAIRO_LIBS $CAIRO_XCB_LIBS"
+	AC_DEFINE(USE_CAIRO_XCB, 1, [Define to 1 if cairo XCB surfaces are available.])
+      fi
       AC_DEFINE(USE_CAIRO, 1, [Define to 1 if using cairo.])
       CFLAGS="$CFLAGS $CAIRO_CFLAGS"
       LIBS="$LIBS $CAIRO_LIBS"
--- /tmp/emacs-28.1.91/src/xterm.h.bak	2022-08-01 06:29:23.000000000 -0500
+++ /tmp/emacs-28.1.91/src/xterm.h	2022-08-30 01:14:37.623171629 -0500
@@ -78,6 +78,9 @@
 #ifdef CAIRO_HAS_SVG_SURFACE
 #include <cairo-svg.h>
 #endif
+#ifdef USE_CAIRO_XCB
+#include <cairo-xcb.h>
+#endif
 #endif
 
 #ifdef HAVE_X_I18N
@@ -469,6 +472,7 @@
 
 #ifdef USE_XCB
   xcb_connection_t *xcb_connection;
+  xcb_visualtype_t *xcb_visual;
 #endif
 
 #ifdef HAVE_XDBE
diff -u /tmp/emacs-28.1.91/src/xterm.c.bak /tmp/emacs-28.1.91/src/xterm.c
--- /tmp/emacs-28.1.91/src/xterm.c.bak	2022-08-01 06:29:23.000000000 -0500
+++ /tmp/emacs-28.1.91/src/xterm.c	2022-08-30 01:11:39.213178617 -0500
@@ -33,6 +33,10 @@
 #include "xterm.h"
 #include <X11/cursorfont.h>
 
+#ifdef USE_XCB
+#include <xcb/xproto.h>
+#endif
+
 /* If we have Xfixes extension, use it for pointer blanking.  */
 #ifdef HAVE_XFIXES
 #include <X11/extensions/Xfixes.h>
@@ -132,6 +136,10 @@
 #include <X11/XKBlib.h>
 #endif
 
+#if defined USE_XCB && defined USE_CAIRO_XCB
+#define USE_CAIRO_XCB_SURFACE
+#endif
+
 /* Default to using XIM if available.  */
 #ifdef USE_XIM
 bool use_xim = true;
@@ -385,11 +393,20 @@
     {
       int width = FRAME_CR_SURFACE_DESIRED_WIDTH (f);
       int height = FRAME_CR_SURFACE_DESIRED_HEIGHT (f);
-      cairo_surface_t *surface
-	= cairo_xlib_surface_create (FRAME_X_DISPLAY (f),
-				     FRAME_X_RAW_DRAWABLE (f),
-				     FRAME_X_VISUAL (f),
-				     width, height);
+      cairo_surface_t *surface;
+
+#ifdef USE_CAIRO_XCB_SURFACE
+      if (FRAME_DISPLAY_INFO (f)->xcb_visual)
+	surface = cairo_xcb_surface_create (FRAME_DISPLAY_INFO (f)->xcb_connection,
+					    (xcb_drawable_t) FRAME_X_RAW_DRAWABLE (f),
+					    FRAME_DISPLAY_INFO (f)->xcb_visual,
+					    width, height);
+      else
+#endif
+	surface = cairo_xlib_surface_create (FRAME_X_DISPLAY (f),
+					     FRAME_X_RAW_DRAWABLE (f),
+					     FRAME_X_VISUAL (f),
+					     width, height);
 
       cr = FRAME_CR_CONTEXT (f) = cairo_create (surface);
       cairo_surface_destroy (surface);
@@ -458,6 +475,10 @@
   switch (cairo_surface_get_type (surface))
     {
     case CAIRO_SURFACE_TYPE_XLIB:
+#ifdef USE_CAIRO_XCB_SURFACE
+    case CAIRO_SURFACE_TYPE_XCB:
+#endif
+      
       cairo_surface_flush (surface);
       return true;
 
@@ -4436,6 +4457,19 @@
 		     x, to_y);
 	  cairo_surface_mark_dirty_rectangle (surface, x, to_y, width, height);
 	}
+#ifdef USE_CAIRO_XCB_SURFACE
+      else if (cairo_surface_get_type (surface) == CAIRO_SURFACE_TYPE_XCB)
+	{
+	  cairo_surface_flush (surface);
+	  xcb_copy_area (FRAME_DISPLAY_INFO (f)->xcb_connection,
+			 (xcb_drawable_t) FRAME_X_DRAWABLE (f),
+			 (xcb_drawable_t) FRAME_X_DRAWABLE (f),
+			 (xcb_gcontext_t) XGContextFromGC (f->output_data.x->normal_gc),
+			 x, from_y, x, to_y, width, height);
+	  cairo_surface_mark_dirty_rectangle (surface, x, to_y, width, height);
+	}
+#endif
+
       else
 	{
 	  cairo_surface_t *s
@@ -13062,6 +13096,40 @@
     dpyinfo->supports_xdbe = true;
 #endif
 
+#ifdef USE_XCB
+  xcb_screen_t *xcb_screen = NULL;
+  xcb_screen_iterator_t iter;
+  xcb_visualid_t wanted = { XVisualIDFromVisual (dpyinfo->visual) };
+  xcb_depth_iterator_t depth_iter;
+  xcb_visualtype_iterator_t visual_iter;
+
+  int screen = DefaultScreen (dpyinfo->display);
+
+  iter = xcb_setup_roots_iterator (xcb_get_setup (dpyinfo->xcb_connection));
+  for (; iter.rem; --screen, xcb_screen_next (&iter))
+    {
+      if (!screen)
+	xcb_screen = iter.data;
+    }
+
+  if (xcb_screen)
+    {
+      depth_iter = xcb_screen_allowed_depths_iterator (xcb_screen);
+      for (; depth_iter.rem; xcb_depth_next (&depth_iter))
+	{
+	  visual_iter = xcb_depth_visuals_iterator (depth_iter.data);
+	  for (; visual_iter.rem; xcb_visualtype_next (&visual_iter))
+	    {
+	      if (wanted == visual_iter.data->visual_id)
+		{
+		  dpyinfo->xcb_visual = visual_iter.data;
+		  break;
+		}
+	    }
+	}
+    }
+#endif
+
 #if defined USE_CAIRO || defined HAVE_XFT
   {
     /* If we are using Xft, the following precautions should be made:

Diff finished.  Tue Aug 30 01:36:19 2022

Diff finished.  Tue Aug 30 01:36:19 2022

Diff finished.  Tue Aug 30 01:36:20 2022

Best Regards