From 4b585e75d94bab1af65574cefc818cb7f861044a Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Mon, 3 Feb 2020 08:06:52 -0800 Subject: [PATCH] Fixed bug 4833 - Use EGL for X11? Martin Fiedler To be precise, this is about *desktop OpenGL* on X11. For OpenGL ES, EGL is already used (as it's the only way to get an OpenGL ES context), as Sylvain noted above. To shine some light on why this is needed: In 99% of all cases, using GLX on X11 is fine, even though it's effectively deprecated in favor of EGL [1]. However, there's at least one use case that *requires* the OpenGL context being created with EGL instead of GLX, and that's DRM_PRIME interoperability: The function glEGLImageTargetTexture2DOES simply doesn't work with GLX. (Currently, Mesa actually crashes when trying that.) Some example code: https://gist.github.com/kajott/d1b29c613be30893c855621edd1f212e Runs on Intel and open-source AMD drivers just fine (others unconfirmed), but with #define USE_EGL 0 (i.e. forcing it to GLX), it crashes. The same happens when using SDL for window and context creation. The good news is that most of the pieces for EGL support on X11 are already in place: SDL_egl.c is pretty complete (and used for desktop OpenGL on Wayland, for example), and SDL_x11opengl.c has the aforementioned OpenGL-ES-on-EGL support. However, when it comes to desktop OpenGL, it's hardcoded to fall back to GLX. I'm not advocating to make EGL the default for desktop OpenGL on X11; don't fix what ain't broken. But something like an SDL_HINT_VIDEO_X11_FORCE_EGL would be very appreciated to make use cases like the above work with SDL. [1] source: Eric Anholt, major Linux graphics stack developer, 7 years ago already - see last paragraph of https://www.phoronix.com/scan.php?page=news_item&px=MTE3MTI --- include/SDL_hints.h | 11 +++++++++++ src/video/x11/SDL_x11opengl.c | 10 ++++++++-- src/video/x11/SDL_x11opengles.c | 4 +++- src/video/x11/SDL_x11video.c | 28 ++++++++++++++++++---------- src/video/x11/SDL_x11window.c | 8 +++++--- 5 files changed, 45 insertions(+), 16 deletions(-) diff --git a/include/SDL_hints.h b/include/SDL_hints.h index fcc04e901..04e92e163 100644 --- a/include/SDL_hints.h +++ b/include/SDL_hints.h @@ -244,6 +244,17 @@ extern "C" { */ #define SDL_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR "SDL_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR" +/** + * \brief A variable controlling whether X11 should use GLX or EGL by default + * + * This variable can be set to the following values: + * "0" - Use GLX + * "1" - Use EGL + * + * By default SDL will use GLX when both are present. + */ +#define SDL_HINT_VIDEO_X11_FORCE_EGL "SDL_VIDEO_X11_FORCE_EGL" + /** * \brief A variable controlling whether the window frame and title bar are interactive when the cursor is hidden * diff --git a/src/video/x11/SDL_x11opengl.c b/src/video/x11/SDL_x11opengl.c index 02364c245..8747ff2db 100644 --- a/src/video/x11/SDL_x11opengl.c +++ b/src/video/x11/SDL_x11opengl.c @@ -238,7 +238,8 @@ X11_GL_LoadLibrary(_THIS, const char *path) /* If we need a GL ES context and there's no * GLX_EXT_create_context_es2_profile extension, switch over to X11_GLES functions */ - if (_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES && + if (((_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES) || + SDL_GetHintBoolean(SDL_HINT_VIDEO_X11_FORCE_EGL, SDL_FALSE)) && X11_GL_UseEGL(_this) ) { #if SDL_VIDEO_OPENGL_EGL X11_GL_UnloadLibrary(_this); @@ -649,8 +650,13 @@ SDL_bool X11_GL_UseEGL(_THIS) { SDL_assert(_this->gl_data != NULL); - SDL_assert(_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES); + if (SDL_GetHintBoolean(SDL_HINT_VIDEO_X11_FORCE_EGL, SDL_FALSE)) + { + /* use of EGL has been requested, even for desktop GL */ + return SDL_TRUE; + } + SDL_assert(_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES); return (SDL_GetHintBoolean(SDL_HINT_OPENGL_ES_DRIVER, SDL_FALSE) || _this->gl_config.major_version == 1 /* No GLX extension for OpenGL ES 1.x profiles. */ || _this->gl_config.major_version > _this->gl_data->es_profile_max_supported_version.major diff --git a/src/video/x11/SDL_x11opengles.c b/src/video/x11/SDL_x11opengles.c index 5c4890e1c..d3bdaa648 100644 --- a/src/video/x11/SDL_x11opengles.c +++ b/src/video/x11/SDL_x11opengles.c @@ -22,6 +22,7 @@ #if SDL_VIDEO_DRIVER_X11 && SDL_VIDEO_OPENGL_EGL +#include "SDL_hints.h" #include "SDL_x11video.h" #include "SDL_x11opengles.h" #include "SDL_x11opengl.h" @@ -34,7 +35,8 @@ X11_GLES_LoadLibrary(_THIS, const char *path) SDL_VideoData *data = (SDL_VideoData *) _this->driverdata; /* If the profile requested is not GL ES, switch over to X11_GL functions */ - if (_this->gl_config.profile_mask != SDL_GL_CONTEXT_PROFILE_ES) { + if ((_this->gl_config.profile_mask != SDL_GL_CONTEXT_PROFILE_ES) && + !SDL_GetHintBoolean(SDL_HINT_VIDEO_X11_FORCE_EGL, SDL_FALSE)) { #if SDL_VIDEO_OPENGL_GLX X11_GLES_UnloadLibrary(_this); _this->GL_LoadLibrary = X11_GL_LoadLibrary; diff --git a/src/video/x11/SDL_x11video.c b/src/video/x11/SDL_x11video.c index 059b05d95..76cce5831 100644 --- a/src/video/x11/SDL_x11video.c +++ b/src/video/x11/SDL_x11video.c @@ -27,6 +27,7 @@ #include "SDL_video.h" #include "SDL_mouse.h" #include "SDL_timer.h" +#include "SDL_hints.h" #include "../SDL_sysvideo.h" #include "../SDL_pixels_c.h" @@ -276,16 +277,23 @@ X11_CreateDevice(int devindex) device->GL_GetSwapInterval = X11_GL_GetSwapInterval; device->GL_SwapWindow = X11_GL_SwapWindow; device->GL_DeleteContext = X11_GL_DeleteContext; -#elif SDL_VIDEO_OPENGL_EGL - device->GL_LoadLibrary = X11_GLES_LoadLibrary; - device->GL_GetProcAddress = X11_GLES_GetProcAddress; - device->GL_UnloadLibrary = X11_GLES_UnloadLibrary; - device->GL_CreateContext = X11_GLES_CreateContext; - device->GL_MakeCurrent = X11_GLES_MakeCurrent; - device->GL_SetSwapInterval = X11_GLES_SetSwapInterval; - device->GL_GetSwapInterval = X11_GLES_GetSwapInterval; - device->GL_SwapWindow = X11_GLES_SwapWindow; - device->GL_DeleteContext = X11_GLES_DeleteContext; +#endif +#if SDL_VIDEO_OPENGL_EGL +#if SDL_VIDEO_OPENGL_GLX + if (SDL_GetHintBoolean(SDL_HINT_VIDEO_X11_FORCE_EGL, SDL_FALSE)) { +#endif + device->GL_LoadLibrary = X11_GLES_LoadLibrary; + device->GL_GetProcAddress = X11_GLES_GetProcAddress; + device->GL_UnloadLibrary = X11_GLES_UnloadLibrary; + device->GL_CreateContext = X11_GLES_CreateContext; + device->GL_MakeCurrent = X11_GLES_MakeCurrent; + device->GL_SetSwapInterval = X11_GLES_SetSwapInterval; + device->GL_GetSwapInterval = X11_GLES_GetSwapInterval; + device->GL_SwapWindow = X11_GLES_SwapWindow; + device->GL_DeleteContext = X11_GLES_DeleteContext; +#if SDL_VIDEO_OPENGL_GLX + } +#endif #endif device->SetClipboardText = X11_SetClipboardText; diff --git a/src/video/x11/SDL_x11window.c b/src/video/x11/SDL_x11window.c index d5b7fd04f..554da0661 100644 --- a/src/video/x11/SDL_x11window.c +++ b/src/video/x11/SDL_x11window.c @@ -418,7 +418,8 @@ X11_CreateWindow(_THIS, SDL_Window * window) XVisualInfo *vinfo = NULL; #if SDL_VIDEO_OPENGL_EGL - if (_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES + if (((_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES) || + SDL_GetHintBoolean(SDL_HINT_VIDEO_X11_FORCE_EGL, SDL_FALSE)) #if SDL_VIDEO_OPENGL_GLX && ( !_this->gl_data || X11_GL_UseEGL(_this) ) #endif @@ -628,9 +629,10 @@ X11_CreateWindow(_THIS, SDL_Window * window) } windowdata = (SDL_WindowData *) window->driverdata; -#if SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2 +#if SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2 || SDL_VIDEO_OPENGL_EGL if ((window->flags & SDL_WINDOW_OPENGL) && - _this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES + ((_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES) || + SDL_GetHintBoolean(SDL_HINT_VIDEO_X11_FORCE_EGL, SDL_FALSE)) #if SDL_VIDEO_OPENGL_GLX && ( !_this->gl_data || X11_GL_UseEGL(_this) ) #endif