[gtk/wip/chergert/macos-fixes] macos: use CGContext to draw cairo surfaces



commit 641d27379f4bc2582f5ad369b4620a611fc8fa5a
Author: Christian Hergert <christian hergert me>
Date:   Thu Feb 3 18:31:09 2022 -0800

    macos: use CGContext to draw cairo surfaces
    
    Instead of relying on cairo_t to perform drawing from our backing
    image surface to the Core Graphics context, we can convert the
    cairo_image_surface_t into a CGImageRef without having to copy
    data if we are certain of the alignment of the image up front.
    
    Without this, there are many situations, based on the size of the
    window that could cause cairo to take a slow path and malloc/copy
    the data to ensure that alignment.
    
    The previous commit titled "macos: align image surface rowstride to
    16-bytes" ensures that this invariant is true so that our drawing
    code can assume we can reference the framebuffer from the
    cairo_image_surface_t using a CGDataProvider.
    
    Since GdkMacosCairoContext and GdkMacosCairoSubview are coordinating,
    we can also setup the transformation/scale early when drawing the
    cairo_image_surface_t instead of when copying it to Core Graphics.
    
    Furthermore, the CGImageRef is created with an RGB colorspace so
    that we are not performing colorspace conversion to the output
    device. We don't get color matching between displays, but we don't
    expect that anyway, particularly with the software renderer.

 gdk/macos/GdkMacosCairoSubview.c | 107 ++++++++++++---------------------------
 gdk/macos/GdkMacosCairoSubview.h |   5 +-
 gdk/macos/GdkMacosCairoView.c    |  74 +++++++++++++++++++++++++--
 gdk/macos/gdkmacoscairocontext.c |  22 ++++++--
 4 files changed, 125 insertions(+), 83 deletions(-)
---
diff --git a/gdk/macos/GdkMacosCairoSubview.c b/gdk/macos/GdkMacosCairoSubview.c
index af79f5e879..af22e89887 100644
--- a/gdk/macos/GdkMacosCairoSubview.c
+++ b/gdk/macos/GdkMacosCairoSubview.c
@@ -23,7 +23,6 @@
 #include <CoreGraphics/CoreGraphics.h>
 #include <cairo-quartz.h>
 
-
 #import "GdkMacosCairoSubview.h"
 #import "GdkMacosCairoView.h"
 
@@ -34,6 +33,7 @@
 -(void)dealloc
 {
   g_clear_pointer (&self->clip, cairo_region_destroy);
+  g_clear_pointer (&self->image, CGImageRelease);
   [super dealloc];
 }
 
@@ -55,104 +55,64 @@
 -(void)drawRect:(NSRect)rect
 {
   CGContextRef cgContext;
-  GdkSurface *gdk_surface;
-  cairo_surface_t *dest;
-  const NSRect *rects = NULL;
   NSView *root_view;
-  NSInteger n_rects = 0;
-  NSRect abs_bounds;
-  cairo_t *cr;
+  CGRect image_rect;
+  CGRect abs_bounds;
   CGSize scale;
-  int scale_factor;
+  int abs_height;
 
-  if (self->cairoSurface == NULL)
+  if (self->image == NULL)
     return;
 
-  /* Acquire everything we need to do translations, drawing, etc */
-  gdk_surface = [self gdkSurface];
-  scale_factor = gdk_surface_get_scale_factor (gdk_surface);
-  root_view = [[self window] contentView];
   cgContext = [[NSGraphicsContext currentContext] CGContext];
+  root_view = [[self window] contentView];
   abs_bounds = [self convertRect:[self bounds] toView:root_view];
+  abs_height = CGImageGetHeight (self->image);
 
   CGContextSaveGState (cgContext);
 
-  /* Translate scaling to remove HiDPI scaling from CGContext as
-   * cairo will be doing that for us already.
-   */
-  scale = CGSizeMake (1.0, 1.0);
-  scale = CGContextConvertSizeToDeviceSpace (cgContext, scale);
-  CGContextScaleCTM (cgContext, 1.0 / scale.width, 1.0 / scale.height);
-
-  /* Create the cairo surface to draw to the CGContext and translate
-   * coordinates so we can pretend we are in the same coordinate system
-   * as the GDK surface.
+  /* Clip while our context is still using matching coordinates
+   * to the self->clip region. This is usually just on the views
+   * for the shadow areas.
    */
-  dest = cairo_quartz_surface_create_for_cg_context (cgContext,
-                                                     gdk_surface->width * scale_factor,
-                                                     gdk_surface->height * scale_factor);
-  cairo_surface_set_device_scale (dest, scale_factor, scale_factor);
-
-  /* Create cairo context and translate things into the origin of
-   * the topmost contentView so that we just draw at 0,0 with a
-   * clip region to paint the surface.
-   */
-  cr = cairo_create (dest);
-  cairo_translate (cr, -abs_bounds.origin.x, -abs_bounds.origin.y);
-
-  /* Apply the clip if provided one */
   if (self->clip != NULL)
     {
-      cairo_rectangle_int_t area;
+      guint n_rects = cairo_region_num_rectangles (self->clip);
 
-      n_rects = cairo_region_num_rectangles (self->clip);
       for (guint i = 0; i < n_rects; i++)
         {
+          cairo_rectangle_int_t area;
+
           cairo_region_get_rectangle (self->clip, i, &area);
-          cairo_rectangle (cr, area.x, area.y, area.width, area.height);
+          CGContextAddRect (cgContext,
+                            CGRectMake (area.x, area.y, area.width, area.height));
         }
 
-      cairo_clip (cr);
-    }
-
-  /* Clip the cairo context based on the rectangles to be drawn
-   * within the bounding box :rect.
-   */
-  [self getRectsBeingDrawn:&rects count:&n_rects];
-  for (NSInteger i = 0; i < n_rects; i++)
-    {
-      NSRect area = [self convertRect:rects[i] toView:root_view];
-      cairo_rectangle (cr,
-                       area.origin.x, area.origin.y,
-                       area.size.width, area.size.height);
+      CGContextClip (cgContext);
     }
-  cairo_clip (cr);
 
-  /* Now paint the surface (without blending) as we do not need
-   * any compositing here. The transparent regions (like shadows)
-   * are already on non-opaque layers.
-   */
-  cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
-  cairo_set_source_surface (cr, self->cairoSurface, 0, 0);
-  cairo_paint (cr);
+  /* Scale/Translate so that the CGImageRef draws in proper format/placement */
+  scale = CGSizeMake (1.0, 1.0);
+  scale = CGContextConvertSizeToDeviceSpace (cgContext, scale);
+  CGContextScaleCTM (cgContext, 1.0 / scale.width, 1.0 / scale.height);
+  CGContextTranslateCTM (cgContext, -abs_bounds.origin.x, -abs_bounds.origin.y);
+  image_rect = CGRectMake (-abs_bounds.origin.x,
+                           -abs_bounds.origin.y,
+                           CGImageGetWidth (self->image),
+                           CGImageGetHeight (self->image));
+  CGContextDrawImage (cgContext, image_rect, self->image);
 
-  /* Cleanup state, flush the surface to the backing layer, and
-   * restore GState for future use.
-   */
-  cairo_destroy (cr);
-  cairo_surface_flush (dest);
-  cairo_surface_destroy (dest);
   CGContextRestoreGState (cgContext);
 }
 
--(void)setCairoSurface:(cairo_surface_t *)surface
-            withDamage:(cairo_region_t *)region
+-(void)setImage:(CGImageRef)theImage
+     withDamage:(cairo_region_t *)region
 {
-  if (surface != self->cairoSurface)
+  if (theImage != image)
     {
-      g_clear_pointer (&self->cairoSurface, cairo_surface_destroy);
-      if (surface != NULL)
-        self->cairoSurface = cairo_surface_reference (surface);
+      g_clear_pointer (&image, CGImageRelease);
+      if (theImage)
+        image = CGImageRetain (theImage);
     }
 
   if (region != NULL)
@@ -179,8 +139,7 @@
     }
 
   for (id view in [self subviews])
-    [(GdkMacosCairoSubview *)view setCairoSurface:surface
-                                       withDamage:region];
+    [(GdkMacosCairoSubview *)view setImage:theImage withDamage:region];
 }
 
 -(void)setOpaque:(BOOL)opaque
diff --git a/gdk/macos/GdkMacosCairoSubview.h b/gdk/macos/GdkMacosCairoSubview.h
index 5eae734f8f..e6fda6ac43 100644
--- a/gdk/macos/GdkMacosCairoSubview.h
+++ b/gdk/macos/GdkMacosCairoSubview.h
@@ -25,13 +25,12 @@
 @interface GdkMacosCairoSubview : NSView
 {
   BOOL             _isOpaque;
-  cairo_surface_t *cairoSurface;
   cairo_region_t  *clip;
+  CGImageRef       image;
 }
 
 -(void)setOpaque:(BOOL)opaque;
--(void)setCairoSurface:(cairo_surface_t *)cairoSurface
-            withDamage:(cairo_region_t *)region;
+-(void)setImage:(CGImageRef)theImage withDamage:(cairo_region_t *)region;
 -(void)setClip:(cairo_region_t*)region;
 
 @end
diff --git a/gdk/macos/GdkMacosCairoView.c b/gdk/macos/GdkMacosCairoView.c
index 3e830c6a28..d3573f5950 100644
--- a/gdk/macos/GdkMacosCairoView.c
+++ b/gdk/macos/GdkMacosCairoView.c
@@ -58,12 +58,81 @@
     [child setNeedsDisplay:needsDisplay];
 }
 
+static void
+release_surface_provider (void       *info,
+                          const void *data,
+                          size_t      size)
+{
+  cairo_surface_destroy (info);
+}
+
 -(void)setCairoSurface:(cairo_surface_t *)cairoSurface
             withDamage:(cairo_region_t *)cairoRegion
 {
+  CGImageRef image = NULL;
+
+  if (cairoSurface != NULL)
+    {
+      const CGColorRenderingIntent intent = kCGRenderingIntentDefault;
+      CGDataProviderRef provider;
+      CGColorSpaceRef rgb;
+      cairo_format_t format;
+      CGBitmapInfo bitmap = kCGBitmapByteOrder32Host;
+      guint8 *framebuffer;
+      size_t width;
+      size_t height;
+      int rowstride;
+      int bpp;
+      int bpc;
+
+      cairo_surface_flush (cairoSurface);
+
+      format = cairo_image_surface_get_format (cairoSurface);
+      framebuffer = cairo_image_surface_get_data (cairoSurface);
+      rowstride = cairo_image_surface_get_stride (cairoSurface);
+      width = cairo_image_surface_get_width (cairoSurface);
+      height = cairo_image_surface_get_height (cairoSurface);
+      rgb = CGColorSpaceCreateDeviceRGB ();
+
+      /* Assert that our image surface was created correctly with
+       * 16-byte aligned pointers and strides. This is needed to
+       * ensure that we're working with fast paths in CoreGraphics.
+       */
+      g_assert (format == CAIRO_FORMAT_ARGB32 || format == CAIRO_FORMAT_RGB24);
+      g_assert (framebuffer != NULL);
+      g_assert (((intptr_t)framebuffer & (intptr_t)~0xF) == (intptr_t)framebuffer);
+      g_assert ((rowstride & ~0xF) == rowstride);
+
+      if (format == CAIRO_FORMAT_ARGB32)
+        {
+          bitmap |= kCGImageAlphaPremultipliedFirst;
+          bpp = 32;
+          bpc = 8;
+        }
+      else
+        {
+          bitmap |= kCGImageAlphaNoneSkipFirst;
+          bpp = 32;
+          bpc = 8;
+        }
+
+      provider = CGDataProviderCreateWithData (cairo_surface_reference (cairoSurface),
+                                               framebuffer,
+                                               rowstride * height,
+                                               release_surface_provider);
+
+      image = CGImageCreate (width, height, bpc, bpp, rowstride, rgb, bitmap, provider, NULL, FALSE, intent);
+
+      CGDataProviderRelease (provider);
+      CGColorSpaceRelease (rgb);
+    }
+
   for (id view in [self subviews])
-    [(GdkMacosCairoSubview *)view setCairoSurface:cairoSurface
-                                       withDamage:cairoRegion];
+    [(GdkMacosCairoSubview *)view setImage:image
+                                withDamage:cairoRegion];
+
+  if (image != NULL)
+    CGImageRelease (image);
 }
 
 -(void)removeOpaqueChildren
@@ -162,7 +231,6 @@
        */
       self->transparent = [[GdkMacosCairoSubview alloc] initWithFrame:frame];
       [self addSubview:self->transparent];
-
     }
 
   return self;
diff --git a/gdk/macos/gdkmacoscairocontext.c b/gdk/macos/gdkmacoscairocontext.c
index dadbfe4132..c1dd7f677c 100644
--- a/gdk/macos/gdkmacoscairocontext.c
+++ b/gdk/macos/gdkmacoscairocontext.c
@@ -86,9 +86,25 @@ create_cairo_surface_for_surface (GdkSurface *surface)
   cairo_surface_set_user_data (cairo_surface, &buffer_key, data, g_free);
   cairo_surface_set_device_scale (cairo_surface, scale, scale);
 
+  return cairo_surface;
+}
+
+static cairo_t *
+do_cairo_create (GdkMacosCairoContext *self)
+{
+  GdkSurface *surface;
+  cairo_t *cr;
 
+  g_assert (GDK_IS_MACOS_CAIRO_CONTEXT (self));
 
-  return cairo_surface;
+  surface = gdk_draw_context_get_surface (GDK_DRAW_CONTEXT (self));
+  cr = cairo_create (self->window_surface);
+
+  /* Draw upside down as quartz prefers */
+  cairo_translate (cr, 0, surface->height);
+  cairo_scale (cr, 1.0, -1.0);
+
+  return cr;
 }
 
 static cairo_t *
@@ -101,7 +117,7 @@ _gdk_macos_cairo_context_cairo_create (GdkCairoContext *cairo_context)
   if (self->cr != NULL)
     return cairo_reference (self->cr);
 
-  return cairo_create (self->window_surface);
+  return do_cairo_create (self);
 }
 
 static void
@@ -121,7 +137,7 @@ _gdk_macos_cairo_context_begin_frame (GdkDrawContext *draw_context,
   if (self->window_surface == NULL)
     self->window_surface = create_cairo_surface_for_surface (surface);
 
-  self->cr = cairo_create (self->window_surface);
+  self->cr = do_cairo_create (self);
 
   if (![nswindow isOpaque])
     {


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