Fixed bug 1749 - SDL_GL_CreateContext() causes fatal X11 protocol errors that should just be caught instead
authorSam Lantinga
Sun, 10 Mar 2013 09:07:23 -0700
changeset 69882aed1beaf5bc
parent 6987 7084af936d82
child 6989 be9c51af4f94
Fixed bug 1749 - SDL_GL_CreateContext() causes fatal X11 protocol errors that should just be caught instead

Lee Salzman

When using SDL_GL_CreateContext() to create a >= 3.0 version or core/forward-compatible context, internally glXCreateContextAttribsARB is used. Mesa in particular seems to be having trouble with this call and returning all sorts of errors, so it is dangerous to poll for the highest GL version by using calls to SDL_GL_CreateContext unless you are sure, a priori, that the call will suceed, defeating the point of its use.

X11 protocol errors are of the following form, with varying details depending on user, but the cause is always SDL_GL_CreateContext as above...

X Error of failed request: GLXBadFBConfig
Major opcode of failed request: 153 (GLX)
Minor opcode of failed request: 34 ()
Serial number of failed request: 215
Current serial number in output stream: 221

These sorts of errors can be temporarily filtered out by setting an X11 error handler to catch and ignore them, which is safe with respect to SDL_GL_CreateContext behavior because this function is allowed to return NULL to indicate failure.

A patch is attached to do this temporary filtering/catching of errors generated by trying to use glXCreateContextAttribs and friends...
src/video/x11/SDL_x11opengl.c
     1.1 --- a/src/video/x11/SDL_x11opengl.c	Sun Mar 10 09:03:38 2013 -0700
     1.2 +++ b/src/video/x11/SDL_x11opengl.c	Sun Mar 10 09:07:23 2013 -0700
     1.3 @@ -496,6 +496,33 @@
     1.4      return vinfo;
     1.5  }
     1.6  
     1.7 +#ifndef GLXBadContext
     1.8 +#define GLXBadContext 0
     1.9 +#endif
    1.10 +#ifndef GLXBadFBConfig
    1.11 +#define GLXBadFBConfig 9
    1.12 +#endif
    1.13 +#ifndef GLXBadProfileARB
    1.14 +#define GLXBadProfileARB 13
    1.15 +#endif
    1.16 +static int (*handler) (Display *, XErrorEvent *) = NULL;
    1.17 +static int
    1.18 +X11_GL_CreateContextErrorHandler(Display * d, XErrorEvent * e)
    1.19 +{
    1.20 +    switch (e->error_code) {
    1.21 +    case GLXBadContext:
    1.22 +    case GLXBadFBConfig:
    1.23 +    case GLXBadProfileARB:
    1.24 +    case BadRequest:
    1.25 +    case BadMatch:
    1.26 +    case BadValue:
    1.27 +    case BadAlloc:
    1.28 +        return (0);
    1.29 +    default:
    1.30 +        return (handler(d, e));
    1.31 +    }
    1.32 +}
    1.33 +
    1.34  SDL_GLContext
    1.35  X11_GL_CreateContext(_THIS, SDL_Window * window)
    1.36  {
    1.37 @@ -516,6 +543,7 @@
    1.38  
    1.39      /* We do this to create a clean separation between X and GLX errors. */
    1.40      XSync(display, False);
    1.41 +    handler = XSetErrorHandler(X11_GL_CreateContextErrorHandler);
    1.42      XGetWindowAttributes(display, data->xwindow, &xattr);
    1.43      v.screen = screen;
    1.44      v.visualid = XVisualIDFromVisual(xattr.visual);
    1.45 @@ -532,10 +560,7 @@
    1.46                 context to grab the new context creation function */
    1.47              GLXContext temp_context =
    1.48                  _this->gl_data->glXCreateContext(display, vinfo, NULL, True);
    1.49 -            if (!temp_context) {
    1.50 -                SDL_SetError("Could not create GL context");
    1.51 -                return NULL;
    1.52 -            } else {
    1.53 +            if (temp_context) {
    1.54                  /* max 8 attributes plus terminator */
    1.55                  int attribs[9] = {
    1.56                      GLX_CONTEXT_MAJOR_VERSION_ARB,
    1.57 @@ -609,7 +634,8 @@
    1.58          XFree(vinfo);
    1.59      }
    1.60      XSync(display, False);
    1.61 -
    1.62 +    XSetErrorHandler(handler);
    1.63 +    
    1.64      if (!context) {
    1.65          SDL_SetError("Could not create GL context");
    1.66          return NULL;