[gtk+/wip/ebassi/legacy-gl: 4/5] x11: Create legacy GLX contexts



commit 66eb087f19c93f4a7d65a86c94530bb2e2ec9054
Author: Emmanuele Bassi <ebassi gnome org>
Date:   Tue Oct 6 18:59:35 2015 +0100

    x11: Create legacy GLX contexts
    
    If GLX has support for the GLX_ARB_create_context_profile extension,
    then we use the GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB; if it does
    not, we fall back to the old glXCreateNewContext() API.
    
    We use the shared GdkGLContext to decide whether the GLX context should
    use the legacy bit or not.

 gdk/x11/gdkglcontext-x11.c |   83 +++++++++++++++++++++++++++++++++++--------
 1 files changed, 67 insertions(+), 16 deletions(-)
---
diff --git a/gdk/x11/gdkglcontext-x11.c b/gdk/x11/gdkglcontext-x11.c
index 139fae6..d55944e 100644
--- a/gdk/x11/gdkglcontext-x11.c
+++ b/gdk/x11/gdkglcontext-x11.c
@@ -586,6 +586,31 @@ create_gl3_context (GdkDisplay   *display,
   return res;
 }
 
+static GLXContext
+create_legacy_context (GdkDisplay   *display,
+                       GLXFBConfig   config,
+                       GdkGLContext *share)
+{
+  GdkX11GLContext *share_x11 = NULL;
+  GLXContext res;
+
+  if (share != NULL)
+    share_x11 = GDK_X11_GL_CONTEXT (share);
+
+  gdk_x11_display_error_trap_push (display);
+
+  res = glXCreateNewContext (gdk_x11_display_get_xdisplay (display),
+                             config,
+                             GLX_RGBA_TYPE,
+                             share_x11 != NULL ? share_x11->glx_context : NULL,
+                             TRUE);
+
+  if (gdk_x11_display_error_trap_pop (display))
+    return NULL;
+
+  return res;
+}
+
 static gboolean
 gdk_x11_gl_context_realize (GdkGLContext  *context,
                             GError       **error)
@@ -598,7 +623,7 @@ gdk_x11_gl_context_realize (GdkGLContext  *context,
   DrawableInfo *info;
   GdkGLContext *share;
   GdkWindow *window;
-  gboolean debug_bit, compat_bit;
+  gboolean debug_bit, compat_bit, legacy_bit;
   int major, minor, flags;
 
   window = gdk_gl_context_get_window (context);
@@ -611,22 +636,54 @@ gdk_x11_gl_context_realize (GdkGLContext  *context,
   debug_bit = gdk_gl_context_get_debug_enabled (context);
   compat_bit = gdk_gl_context_get_forward_compatible (context);
 
+  /* If there is no glXCreateContextAttribsARB() then we default to legacy */
+  legacy_bit = !GDK_X11_DISPLAY (display)->has_glx_create_context;
+
+  /* We cannot share legacy contexts with core profile ones, so the
+   * shared context is the one that decides if we're going to create
+   * a legacy context or not.
+   */
+  if (share != NULL && gdk_gl_context_is_legacy (share))
+    legacy_bit = TRUE;
+
   flags = 0;
   if (debug_bit)
     flags |= GLX_CONTEXT_DEBUG_BIT_ARB;
   if (compat_bit)
     flags |= GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB;
+  if (legacy_bit)
+    flags |= GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB;
 
   GDK_NOTE (OPENGL,
-            g_print ("Creating core GLX context (version:%d.%d, debug:%s, forward:%s)\n",
+            g_print ("Creating core GLX context (version:%d.%d, debug:%s, forward:%s, legacy:%s)\n",
                      major, minor,
                      debug_bit ? "yes" : "no",
-                     compat_bit ? "yes" : "no"));
+                     compat_bit ? "yes" : "no",
+                     legacy_bit ? "yes" : "no"));
+
+  /* If we have access to GLX_ARB_create_context_profile then we can ask for
+   * a compatibility profile; if we don't, then we have to fall back to the
+   * old GLX 1.3 API.
+   */
+  if (legacy_bit && !GDK_X11_DISPLAY (display)->has_glx_create_context)
+    {
+      context_x11->glx_context = create_legacy_context (display, context_x11->glx_config, share);
+    }
+  else
+    {
+      context_x11->glx_context = create_gl3_context (display,
+                                                     context_x11->glx_config,
+                                                     share,
+                                                     flags, major, minor);
+    }
+
+  /* Fall back to legacy in case the GL3 context creation failed */
+  if (context_x11->glx_context == NULL && !legacy_bit)
+    {
+      context_x11->glx_context = create_legacy_context (display, context_x11->glx_config, share);
+      legacy_bit = TRUE;
+    }
 
-  context_x11->glx_context = create_gl3_context (display,
-                                                 context_x11->glx_config,
-                                                 share,
-                                                 flags, major, minor);
   if (context_x11->glx_context == NULL)
     {
       g_set_error_literal (error, GDK_GL_ERROR,
@@ -635,6 +692,9 @@ gdk_x11_gl_context_realize (GdkGLContext  *context,
       return FALSE;
     }
 
+  /* Ensure that any other context is created with a legacy bit set */
+  gdk_gl_context_set_is_legacy (context, legacy_bit);
+
   xvisinfo = find_xvisinfo_for_fbconfig (display, context_x11->glx_config);
 
   info = get_glx_drawable_info (window->impl_window);
@@ -1179,15 +1239,6 @@ gdk_x11_window_create_gl_context (GdkWindow    *window,
       return NULL;
     }
 
-  if (!GDK_X11_DISPLAY (display)->has_glx_create_context)
-    {
-      g_set_error_literal (error, GDK_GL_ERROR,
-                           GDK_GL_ERROR_UNSUPPORTED_PROFILE,
-                           _("The GLX_ARB_create_context_profile extension "
-                             "needed to create core profiles is not available"));
-      return NULL;
-    }
-
   visual = gdk_window_get_visual (window);
   if (!find_fbconfig_for_visual (display, visual, &config, error))
     return NULL;


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