diff -r 98899a226da5 src/video/SDL_egl.c --- a/src/video/SDL_egl.c Mon Feb 03 16:38:07 2020 -0800 +++ b/src/video/SDL_egl.c Tue Feb 04 17:51:46 2020 +0100 @@ -82,6 +82,10 @@ #define DEFAULT_OGL_ES "libGLESv1_CM.so.1" #endif /* SDL_VIDEO_DRIVER_RPI */ +#if SDL_VIDEO_OPENGL +#include "SDL_opengl.h" +#endif + /** If we happen to not have this defined because of an older EGL version, just define it 0x0 as eglGetPlatformDisplayEXT will most likely be NULL if this is missing */ @@ -942,6 +946,34 @@ return NULL; } + /* Check whether making contexts current without a surface is supported. + * First condition: EGL must support it. That's the case for EGL 1.5 + * or later, or if the EGL_KHR_surfaceless_context extension is present. */ + if ((_this->egl_data->egl_version_major > 1) || + ((_this->egl_data->egl_version_major == 1) && (_this->egl_data->egl_version_minor >= 5)) || + SDL_EGL_HasExtension(_this, SDL_EGL_DISPLAY_EXTENSION, "EGL_KHR_surfaceless_context")) + { + /* Secondary condition: The client API must support it. */ + if (profile_es) { + /* On OpenGL ES, the GL_OES_surfaceless_context extension must be + * present. */ + if (SDL_GL_ExtensionSupported("GL_OES_surfaceless_context")) { + _this->gl_allow_no_surface = 1; + } + } else { + /* Desktop OpenGL supports it by default from version 3.0 on. */ + void (APIENTRY * glGetIntegervFunc) (GLenum pname, GLint * params); + glGetIntegervFunc = SDL_GL_GetProcAddress("glGetIntegerv"); + if (glGetIntegervFunc) { + GLint v = 0; + glGetIntegervFunc(GL_MAJOR_VERSION, &v); + if (v >= 3) { + _this->gl_allow_no_surface = 1; + } + } + } + } + return (SDL_GLContext) egl_context; } @@ -957,7 +989,7 @@ /* The android emulator crashes badly if you try to eglMakeCurrent * with a valid context and invalid surface, so we have to check for both here. */ - if (!egl_context || !egl_surface) { + if (!egl_context || (!egl_surface && !_this->gl_allow_no_surface)) { _this->egl_data->eglMakeCurrent(_this->egl_data->egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); } else { if (!_this->egl_data->eglMakeCurrent(_this->egl_data->egl_display, diff -r 98899a226da5 src/video/SDL_egl_c.h --- a/src/video/SDL_egl_c.h Mon Feb 03 16:38:07 2020 -0800 +++ b/src/video/SDL_egl_c.h Tue Feb 04 17:51:46 2020 +0100 @@ -147,12 +147,7 @@ #define SDL_EGL_MakeCurrent_impl(BACKEND) int \ BACKEND ## _GLES_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context) \ {\ - if (window && context) { \ - return SDL_EGL_MakeCurrent(_this, ((SDL_WindowData *) window->driverdata)->egl_surface, context); \ - }\ - else {\ - return SDL_EGL_MakeCurrent(_this, NULL, NULL);\ - }\ + return SDL_EGL_MakeCurrent(_this, window ? ((SDL_WindowData *) window->driverdata)->egl_surface : EGL_NO_SURFACE, context);\ } #define SDL_EGL_CreateContext_impl(BACKEND) SDL_GLContext \ diff -r 98899a226da5 src/video/SDL_sysvideo.h --- a/src/video/SDL_sysvideo.h Mon Feb 03 16:38:07 2020 -0800 +++ b/src/video/SDL_sysvideo.h Tue Feb 04 17:51:46 2020 +0100 @@ -373,6 +373,11 @@ SDL_TLSID current_glwin_tls; SDL_TLSID current_glctx_tls; + /* Flag that stores whether it's allowed to call SDL_GL_MakeCurrent() + * with a NULL window, but a non-NULL context. (Not allowed in most cases, + * except on EGL under some circumstances.) */ + int gl_allow_no_surface; + /* * * */ /* Data used by the Vulkan drivers */ struct diff -r 98899a226da5 src/video/SDL_video.c --- a/src/video/SDL_video.c Mon Feb 03 16:38:07 2020 -0800 +++ b/src/video/SDL_video.c Tue Feb 04 17:51:46 2020 +0100 @@ -3546,12 +3546,14 @@ if (!ctx) { window = NULL; - } else { + } else if (window) { CHECK_WINDOW_MAGIC(window, -1); if (!(window->flags & SDL_WINDOW_OPENGL)) { return SDL_SetError("The specified window isn't an OpenGL window"); } + } else if (!_this->gl_allow_no_surface) { + return SDL_SetError("Use of OpenGL without a window is not supported on this platform"); } retval = _this->GL_MakeCurrent(_this, window, ctx);