[gtk/wip/chergert/gdk-macos-for-master: 3/3] macos: use CGLSetParameter and CGLEnable




commit 6e0fffa0f84ee9aff67ba75ab79bdb2eb3c27cfd
Author: Christian Hergert <chergert redhat com>
Date:   Mon Dec 7 11:47:51 2020 -0800

    macos: use CGLSetParameter and CGLEnable
    
    We don't need to go through the NSOpenGLContext for these.
    We can just use the C API directly. It's also clearer what is using
    CGLEnable() vs CGLSetParameter().

 gdk/macos/gdkmacosglcontext.c | 50 +++++++++++++++++++++++++++++++------------
 1 file changed, 36 insertions(+), 14 deletions(-)
---
diff --git a/gdk/macos/gdkmacosglcontext.c b/gdk/macos/gdkmacosglcontext.c
index 8f7f317cec..2cd68e31d4 100644
--- a/gdk/macos/gdkmacosglcontext.c
+++ b/gdk/macos/gdkmacosglcontext.c
@@ -173,11 +173,13 @@ gdk_macos_gl_context_real_realize (GdkGLContext  *context,
   NSOpenGLContext *shared_gl_context = nil;
   NSOpenGLContext *gl_context;
   NSOpenGLPixelFormat *pixelFormat;
+  CGLContextObj cgl_context;
   GdkGLContext *shared;
   GdkGLContext *shared_data;
   NSOpenGLContext *existing;
   GLint sync_to_framerate = 1;
   GLint validate = 0;
+  GLint swapRect[4];
   int major, minor;
 
   g_assert (GDK_IS_MACOS_GL_CONTEXT (self));
@@ -226,8 +228,19 @@ gdk_macos_gl_context_real_realize (GdkGLContext  *context,
       return FALSE;
     }
 
-  [gl_context setValues:&sync_to_framerate forParameter:NSOpenGLCPSwapInterval];
-  [gl_context setValues:&validate forParameter:NSOpenGLContextParameterStateValidation];
+  cgl_context = [gl_context CGLContextObj];
+
+  swapRect[0] = 0;
+  swapRect[1] = 0;
+  swapRect[2] = surface->width;
+  swapRect[3] = surface->height;
+
+  CGLSetParameter (cgl_context, kCGLCPSwapRectangle, swapRect);
+  CGLSetParameter (cgl_context, kCGLCPSwapInterval, &sync_to_framerate);
+
+  CGLEnable (cgl_context, kCGLCESwapRectangle);
+  if (validate)
+    CGLEnable (cgl_context, kCGLCEStateValidation);
 
   self->dummy_window = [[NSWindow alloc] initWithContentRect:NSZeroRect
                                                    styleMask:0
@@ -259,9 +272,12 @@ gdk_macos_gl_context_begin_frame (GdkDrawContext *context,
                                   cairo_region_t *painted)
 {
   GdkMacosGLContext *self = (GdkMacosGLContext *)context;
+  GdkSurface *surface;
 
   g_assert (GDK_IS_MACOS_GL_CONTEXT (self));
 
+  surface = gdk_draw_context_get_surface (context);
+
   /* If begin frame is called, that means we are trying to draw to
    * the NSWindow using our view. That might be a GdkMacosCairoView
    * but we need it to be a GL view. Also, only in this case do we
@@ -271,17 +287,31 @@ gdk_macos_gl_context_begin_frame (GdkDrawContext *context,
   if (!self->is_attached &&
       gdk_gl_context_get_shared_context (GDK_GL_CONTEXT (context)))
     {
+      CGLContextObj glctx = [self->gl_context CGLContextObj];
+      GLint swapRect[4];
+
       ensure_gl_view (self);
 
       g_clear_pointer (&self->damage, cairo_region_destroy);
       self->damage = cairo_region_copy (painted);
 
       cairo_region_get_extents (painted, &self->flush_rect);
+
+      /* Coordinates are in display coordinates, where as flush_rect is
+       * in GDK coordinates. Must flip Y to match display coordinates where
+       * 0,0 is the bottom-left corner.
+       */
+      swapRect[0] = self->flush_rect.x;                   /* left */
+      swapRect[1] = surface->height - self->flush_rect.y; /* bottom */
+      swapRect[2] = self->flush_rect.width;               /* width */
+      swapRect[3] = self->flush_rect.height;              /* height */
+
+      CGLSetParameter (glctx, kCGLCPSwapRectangle, swapRect);
     }
 
   if (self->needs_resize)
     {
-      GdkSurface *surface = gdk_draw_context_get_surface (context);
+      CGLContextObj cgl_context = [self->gl_context CGLContextObj];
       GLint opaque;
 
       self->needs_resize = FALSE;
@@ -302,7 +332,8 @@ gdk_macos_gl_context_begin_frame (GdkDrawContext *context,
         opaque = GDK_MACOS_TOPLEVEL_SURFACE (surface)->decorated;
       else
         opaque = FALSE;
-      [self->gl_context setValues:&opaque forParameter:NSOpenGLCPSurfaceOpacity];
+
+      CGLSetParameter (cgl_context, kCGLCPSurfaceOpacity, &opaque);
 
       [self->gl_context update];
     }
@@ -311,8 +342,7 @@ gdk_macos_gl_context_begin_frame (GdkDrawContext *context,
 
   if (!self->is_attached)
     {
-      GdkMacosSurface *surface = GDK_MACOS_SURFACE (gdk_draw_context_get_surface (context));
-      NSView *nsview = _gdk_macos_surface_get_view (surface);
+      NSView *nsview = _gdk_macos_surface_get_view (GDK_MACOS_SURFACE (surface));
 
       g_assert (self->gl_context != NULL);
       g_assert (GDK_IS_MACOS_GL_VIEW (nsview));
@@ -332,14 +362,6 @@ gdk_macos_gl_context_end_frame (GdkDrawContext *context,
 
   GDK_DRAW_CONTEXT_CLASS (gdk_macos_gl_context_parent_class)->end_frame (context, painted);
 
-  /* We want to limit how much gets moved to the front buffer so here
-   * we adjust the clip rectangle before flushBuffer is called.
-   */
-  G_STATIC_ASSERT (sizeof (GLint) == sizeof (int));
-  [self->gl_context
-         setValues:(GLint *)&self->flush_rect
-      forParameter:NSOpenGLCPSwapRectangle];
-
   [self->gl_context flushBuffer];
 }
 


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