[grits] Fix crash while initializing OpenGL context



commit 9bcf411430cca69197a7756c1ce8ca925456cc93
Author: Andy Spencer <andy753421 gmail com>
Date:   Sat Dec 15 06:55:33 2012 +0000

    Fix crash while initializing OpenGL context
    
    GL was crashing with a BadAlloc, this seems to be related to the Intel
    graphics drivers when using indirect rendering. Using a direct rendering
    context fixes the problem, however we can at least attempt both way if
    one or the other fails.

 src/gtkgl.c |   32 +++++++++++++++++++++++++++++++-
 1 files changed, 31 insertions(+), 1 deletions(-)
---
diff --git a/src/gtkgl.c b/src/gtkgl.c
index 6a62e86..af2c62b 100644
--- a/src/gtkgl.c
+++ b/src/gtkgl.c
@@ -56,6 +56,26 @@ void gtk_gl_disable(GtkWidget *widget)
 #elif defined(SYS_X11)
 #include <GL/glx.h>
 #include <gdk/gdkx.h>
+static gboolean gtk_gl_errors;
+static int gtk_gl_handler(Display *xdisplay, XErrorEvent *xerror)
+{
+	gtk_gl_errors = TRUE;
+	return 0;
+}
+static GLXContext gtk_gl_create_context(Display *xdisplay, XVisualInfo *xvinfo,
+		GLXContext shared, Bool direct)
+{
+	gtk_gl_errors = FALSE;
+	XSync(xdisplay, False);
+	void *handler = XSetErrorHandler(gtk_gl_handler);
+	GLXContext context = glXCreateContext(xdisplay, xvinfo, shared, direct);
+	XSync(xdisplay, False);
+	XSetErrorHandler(handler);
+
+	if (gtk_gl_errors)
+		return 0;
+	return context;
+}
 void gtk_gl_enable(GtkWidget *widget)
 {
 	g_debug("GtkGl: enable");
@@ -72,10 +92,20 @@ void gtk_gl_enable(GtkWidget *widget)
 	                 GLX_DOUBLEBUFFER,
 	                 GLX_DEPTH_SIZE,  1,
 	                 None};
+
 	XVisualInfo *xvinfo  = glXChooseVisual(xdisplay, nscreen, attribs);
 	if (!xvinfo)
 		g_error("GtkGl: enable - unable to get valid OpenGL Visual");
-	GLXContext   context = glXCreateContext(xdisplay, xvinfo, NULL, True);
+	GLXContext context = 0;
+	if (!context)
+		context = gtk_gl_create_context(xdisplay, xvinfo, NULL, True);
+	if (!context)
+		context = gtk_gl_create_context(xdisplay, xvinfo, NULL, False);
+	if (!context)
+		g_error("Unable to create OpenGL context,"
+		        "possible graphics driver problem?");
+	g_debug("GtkGl: direct rendering = %d\n", glXIsDirect(xdisplay, context));
+
 	g_object_set_data(G_OBJECT(widget), "glcontext", context);
 
 	/* Fix up colormap */



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