Skip to content

Commit

Permalink
Fixed bug 4833 - Use EGL for X11?
Browse files Browse the repository at this point in the history
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
  • Loading branch information
slouken committed Feb 3, 2020
1 parent 67f4478 commit 4b585e7
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 16 deletions.
11 changes: 11 additions & 0 deletions include/SDL_hints.h
Expand Up @@ -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
*
Expand Down
10 changes: 8 additions & 2 deletions src/video/x11/SDL_x11opengl.c
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion src/video/x11/SDL_x11opengles.c
Expand Up @@ -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"
Expand All @@ -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;
Expand Down
28 changes: 18 additions & 10 deletions src/video/x11/SDL_x11video.c
Expand Up @@ -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"

Expand Down Expand Up @@ -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;
Expand Down
8 changes: 5 additions & 3 deletions src/video/x11/SDL_x11window.c
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 4b585e7

Please sign in to comment.