Commit 507659c6acbf4651ca8af42ce033c952e8b88ec4

Ryan C. Gordon 2017-09-01T13:27:53

x11: Make a sacrificial glX context to check for extensions during init. This is necessary because we need to see if GLES compat extensions exist. All of this code (including ShouldUseTextureFramebuffer()) should be revisited after 2.0.6 ships; ideally we don't make throwaway contexts if we can avoid it...but maybe we can't. I hear Vulkan is pretty cool. Fixes Bugzilla #3725.

diff --git a/src/video/SDL_video.c b/src/video/SDL_video.c
index 8008e6e..321eb25 100644
--- a/src/video/SDL_video.c
+++ b/src/video/SDL_video.c
@@ -2941,8 +2941,8 @@ SDL_GL_ExtensionSupported(const char *extension)
 void
 SDL_GL_DeduceMaxSupportedESProfile(int* major, int* minor)
 {
-/* This function breaks games because OpenGL functions are being called before a context is current - see bug 3725 */
-#if 0
+/* THIS REQUIRES AN EXISTING GL CONTEXT THAT HAS BEEN MADE CURRENT. */
+/*  Please refer to https://bugzilla.libsdl.org/show_bug.cgi?id=3725 for discussion. */
 #if SDL_VIDEO_OPENGL || SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2
 	/* XXX This is fragile; it will break in the event of release of
 	 * new versions of OpenGL ES.
@@ -2961,10 +2961,6 @@ SDL_GL_DeduceMaxSupportedESProfile(int* major, int* minor)
         *minor = 0;
     }
 #endif
-#else
-        *major = 2;
-        *minor = 0;
-#endif /* 0 */
 }
 
 void
diff --git a/src/video/x11/SDL_x11opengl.c b/src/video/x11/SDL_x11opengl.c
index 9bbcb05..05f83c8 100644
--- a/src/video/x11/SDL_x11opengl.c
+++ b/src/video/x11/SDL_x11opengl.c
@@ -331,9 +331,32 @@ X11_GL_InitExtensions(_THIS)
 {
     Display *display = ((SDL_VideoData *) _this->driverdata)->display;
     const int screen = DefaultScreen(display);
+    XVisualInfo *vinfo;
+    XSetWindowAttributes xattr;
+    Window w;
+    GLXContext context;
     const char *(*glXQueryExtensionsStringFunc) (Display *, int);
     const char *extensions;
 
+    vinfo = X11_GL_GetVisual(_this, display, screen);
+    if (!vinfo) {
+        return;
+    }
+
+    xattr.background_pixel = 0;
+    xattr.border_pixel = 0;
+    xattr.colormap =
+        X11_XCreateColormap(display, RootWindow(display, screen), vinfo->visual,
+                        AllocNone);
+    w = X11_XCreateWindow(display, RootWindow(display, screen), 0, 0, 32, 32, 0,
+                      vinfo->depth, InputOutput, vinfo->visual,
+                      (CWBackPixel | CWBorderPixel | CWColormap), &xattr);
+    context = _this->gl_data->glXCreateContext(display, vinfo, NULL, True);
+    if (context) {
+        _this->gl_data->glXMakeCurrent(display, w, context);
+    }
+    X11_XFree(vinfo);
+
     glXQueryExtensionsStringFunc =
         (const char *(*)(Display *, int)) X11_GL_GetProcAddress(_this,
                                                                 "glXQueryExtensionsString");
@@ -391,10 +414,14 @@ X11_GL_InitExtensions(_THIS)
     
     /* Check for GLX_EXT_create_context_es2_profile */
     if (HasExtension("GLX_EXT_create_context_es2_profile", extensions)) {
-        SDL_GL_DeduceMaxSupportedESProfile(
-            &_this->gl_data->es_profile_max_supported_version.major,
-            &_this->gl_data->es_profile_max_supported_version.minor
-        );
+        /* this wants to call glGetString(), so it needs a context. */
+        /* !!! FIXME: it would be nice not to make a context here though! */
+        if (context) {
+            SDL_GL_DeduceMaxSupportedESProfile(
+                &_this->gl_data->es_profile_max_supported_version.major,
+                &_this->gl_data->es_profile_max_supported_version.minor
+            );
+        }
     }
 
     /* Check for GLX_ARB_context_flush_control */
@@ -411,6 +438,14 @@ X11_GL_InitExtensions(_THIS)
     if (HasExtension("GLX_ARB_create_context_no_error", extensions)) {
         _this->gl_data->HAS_GLX_ARB_create_context_no_error = SDL_TRUE;
     }
+
+    if (context) {
+        _this->gl_data->glXMakeCurrent(display, None, NULL);
+        _this->gl_data->glXDestroyContext(display, context);
+    }
+
+    X11_XDestroyWindow(display, w);
+    X11_PumpEvents(_this);
 }
 
 /* glXChooseVisual and glXChooseFBConfig have some small differences in