x11: Clean up sacrificial GL context code.
authorRyan C. Gordon <icculus@icculus.org>
Fri, 01 Sep 2017 13:57:40 -0400
changeset 1143937688701d4ce
parent 11438 1717e5011161
child 11440 71be1b793327
x11: Clean up sacrificial GL context code.

Check for failures, restore any previously-current context.
src/video/x11/SDL_x11opengl.c
     1.1 --- a/src/video/x11/SDL_x11opengl.c	Fri Sep 01 13:27:53 2017 -0400
     1.2 +++ b/src/video/x11/SDL_x11opengl.c	Fri Sep 01 13:57:40 2017 -0400
     1.3 @@ -331,32 +331,42 @@
     1.4  {
     1.5      Display *display = ((SDL_VideoData *) _this->driverdata)->display;
     1.6      const int screen = DefaultScreen(display);
     1.7 -    XVisualInfo *vinfo;
     1.8 -    XSetWindowAttributes xattr;
     1.9 -    Window w;
    1.10 -    GLXContext context;
    1.11 +    XVisualInfo *vinfo = NULL;
    1.12 +    Window w = 0;
    1.13 +    GLXContext current_context = 0;
    1.14 +    GLXContext context = 0;
    1.15      const char *(*glXQueryExtensionsStringFunc) (Display *, int);
    1.16      const char *extensions;
    1.17  
    1.18      vinfo = X11_GL_GetVisual(_this, display, screen);
    1.19 -    if (!vinfo) {
    1.20 -        return;
    1.21 +    if (vinfo) {
    1.22 +        GLXContext (*glXGetCurrentContextFunc) (void) =
    1.23 +            (GLXContext(*)(void))
    1.24 +                X11_GL_GetProcAddress(_this, "glXGetCurrentContextFunc");
    1.25 +
    1.26 +        if (glXGetCurrentContextFunc) {
    1.27 +            XSetWindowAttributes xattr;
    1.28 +            current_context = glXGetCurrentContextFunc();
    1.29 +
    1.30 +            xattr.background_pixel = 0;
    1.31 +            xattr.border_pixel = 0;
    1.32 +            xattr.colormap =
    1.33 +                X11_XCreateColormap(display, RootWindow(display, screen),
    1.34 +                                    vinfo->visual, AllocNone);
    1.35 +            w = X11_XCreateWindow(display, RootWindow(display, screen), 0, 0,
    1.36 +                        32, 32, 0, vinfo->depth, InputOutput, vinfo->visual,
    1.37 +                        (CWBackPixel | CWBorderPixel | CWColormap), &xattr);
    1.38 +
    1.39 +            context = _this->gl_data->glXCreateContext(display, vinfo,
    1.40 +                                                        NULL, True);
    1.41 +            if (context) {
    1.42 +                _this->gl_data->glXMakeCurrent(display, w, context);
    1.43 +            }
    1.44 +        }
    1.45 +
    1.46 +        X11_XFree(vinfo);
    1.47      }
    1.48  
    1.49 -    xattr.background_pixel = 0;
    1.50 -    xattr.border_pixel = 0;
    1.51 -    xattr.colormap =
    1.52 -        X11_XCreateColormap(display, RootWindow(display, screen), vinfo->visual,
    1.53 -                        AllocNone);
    1.54 -    w = X11_XCreateWindow(display, RootWindow(display, screen), 0, 0, 32, 32, 0,
    1.55 -                      vinfo->depth, InputOutput, vinfo->visual,
    1.56 -                      (CWBackPixel | CWBorderPixel | CWColormap), &xattr);
    1.57 -    context = _this->gl_data->glXCreateContext(display, vinfo, NULL, True);
    1.58 -    if (context) {
    1.59 -        _this->gl_data->glXMakeCurrent(display, w, context);
    1.60 -    }
    1.61 -    X11_XFree(vinfo);
    1.62 -
    1.63      glXQueryExtensionsStringFunc =
    1.64          (const char *(*)(Display *, int)) X11_GL_GetProcAddress(_this,
    1.65                                                                  "glXQueryExtensionsString");
    1.66 @@ -442,6 +452,7 @@
    1.67      if (context) {
    1.68          _this->gl_data->glXMakeCurrent(display, None, NULL);
    1.69          _this->gl_data->glXDestroyContext(display, context);
    1.70 +        _this->gl_data->glXMakeCurrent(display, w, current_context);
    1.71      }
    1.72  
    1.73      X11_XDestroyWindow(display, w);