From 285781724fb30616bff2075c8aaceed3ab2a60d3 Mon Sep 17 00:00:00 2001 From: Christian Hergert Date: Sun, 6 Dec 2020 20:14:52 -0800 Subject: [PATCH 1/3] macos: set opengl view as opaque in opaque windows We don't need the OpenGL view to be transparent if the window itself is not transparent. This has the potential to speed up the compositing of the GL view onto the NSWindow. --- gdk/macos/GdkMacosGLView.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/gdk/macos/GdkMacosGLView.c b/gdk/macos/GdkMacosGLView.c index 64bd0e89c3..7945184c0b 100644 --- a/gdk/macos/GdkMacosGLView.c +++ b/gdk/macos/GdkMacosGLView.c @@ -82,7 +82,9 @@ G_GNUC_BEGIN_IGNORE_DEPRECATIONS -(BOOL)isOpaque { - return NO; + if ([self window]) + return [[self window] isOpaque]; + return YES; } -(BOOL)isFlipped From b431e39d4e6624677684ec47746d0b1d7f64626b Mon Sep 17 00:00:00 2001 From: Christian Hergert Date: Mon, 7 Dec 2020 11:27:11 -0800 Subject: [PATCH 2/3] macos: we only need 24-bit for color --- gdk/macos/gdkmacosglcontext.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gdk/macos/gdkmacosglcontext.c b/gdk/macos/gdkmacosglcontext.c index 00ddfefcde..8f7f317cec 100644 --- a/gdk/macos/gdkmacosglcontext.c +++ b/gdk/macos/gdkmacosglcontext.c @@ -101,7 +101,7 @@ create_pixel_format (int major, NSOpenGLPFAAccelerated, NSOpenGLPFADoubleBuffer, NSOpenGLPFABackingStore, - NSOpenGLPFAColorSize, 32, + NSOpenGLPFAColorSize, 24, NSOpenGLPFAAlphaSize, 8, (NSOpenGLPixelFormatAttribute)nil From 6e0fffa0f84ee9aff67ba75ab79bdb2eb3c27cfd Mon Sep 17 00:00:00 2001 From: Christian Hergert Date: Mon, 7 Dec 2020 11:47:51 -0800 Subject: [PATCH 3/3] 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]; }