[gtk/wip/chergert/gdk-macos-gl-renderer: 9/26] macos: create NSOpenGLPixelFormat once




commit 7499fc8a443e80d44dd71bb0c9a63ae99d651379
Author: Christian Hergert <chergert redhat com>
Date:   Thu Oct 22 14:43:31 2020 -0700

    macos: create NSOpenGLPixelFormat once
    
    This creates the pixel format up front so that we refuse to create
    a GdkGLContext at all if we cannot get the format we need. It has
    the added benefit that we can reuse the pixel format.

 gdk/macos/gdkmacosglcontext.c | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)
---
diff --git a/gdk/macos/gdkmacosglcontext.c b/gdk/macos/gdkmacosglcontext.c
index e41b4d2ee9..d56e0b435f 100644
--- a/gdk/macos/gdkmacosglcontext.c
+++ b/gdk/macos/gdkmacosglcontext.c
@@ -33,6 +33,8 @@ G_GNUC_BEGIN_IGNORE_DEPRECATIONS
 
 G_DEFINE_TYPE (GdkMacosGLContext, gdk_macos_gl_context, GDK_TYPE_GL_CONTEXT)
 
+static NSOpenGLPixelFormat *pixelFormat;
+
 static NSOpenGLContext *
 get_ns_open_gl_context (GdkMacosGLContext  *self,
                         GError            **error)
@@ -73,7 +75,6 @@ gdk_macos_gl_context_real_realize (GdkGLContext  *context,
 
   if (!GDK_IS_MACOS_GL_VIEW (nsview))
     {
-      NSOpenGLPixelFormat *pixelFormat;
       NSOpenGLContext *shared_gl_context = nil;
       NSOpenGLContext *gl_context;
       GdkMacosGLView *gl_view;
@@ -98,13 +99,11 @@ gdk_macos_gl_context_real_realize (GdkGLContext  *context,
         }
 
       nswindow = _gdk_macos_surface_get_native (GDK_MACOS_SURFACE (surface));
-      pixelFormat = [GdkMacosGLView defaultPixelFormat];
       gl_context = [[NSOpenGLContext alloc] initWithFormat:pixelFormat
                                               shareContext:shared_gl_context];
 
       if (gl_context == nil)
         {
-          [pixelFormat release];
           g_set_error_literal (error,
                                GDK_GL_ERROR,
                                GDK_GL_ERROR_NOT_AVAILABLE,
@@ -130,7 +129,6 @@ gdk_macos_gl_context_real_realize (GdkGLContext  *context,
       [gl_context makeCurrentContext];
 
       [gl_view release];
-      [pixelFormat release];
     }
 
   g_assert (get_ns_open_gl_context (self, NULL) != NULL);
@@ -228,6 +226,20 @@ _gdk_macos_gl_context_new (GdkMacosSurface  *surface,
   g_return_val_if_fail (GDK_IS_MACOS_SURFACE (surface), NULL);
   g_return_val_if_fail (!share || GDK_IS_MACOS_GL_CONTEXT (share), NULL);
 
+  if (pixelFormat == NULL)
+    {
+      pixelFormat = [GdkMacosGLView defaultPixelFormat];
+
+      if (pixelFormat == NULL)
+        {
+          g_set_error_literal (error,
+                               GDK_GL_ERROR,
+                               GDK_GL_ERROR_NOT_AVAILABLE,
+                               "Failed to create pixel format");
+          return NULL;
+        }
+    }
+
   context = g_object_new (GDK_TYPE_MACOS_GL_CONTEXT,
                           "surface", surface,
                           "shared-context", share,


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