Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
Improved simultaneous support for OpenGL and OpenGL ES
Browse files Browse the repository at this point in the history
From Scott Percival

Okay, I think I have something for this. Tested it on GL and GLES
machines, it seems to work okay.

- Add a new SDL GL attribute SDL_GL_CONTEXT_EGL:
        - Only useful for the X11 video driver at the moment
        - Set to 1 for an EGL context, 0 to use the default for the video driver
        - Default is 0, unless library is built for EGL only
        - Should be set after SDL init, but before window/context
creation (i.e. same place you'd specify attributes for major/minor GL
version)
- After a lot of agony pondering the least-terrible way to go about
it, made it so that X11_GL_LoadLibrary and X11_GLES_LoadLibrary check
SDL_GL_CONTEXT_EGL. If no GL context exists yet, and the attribute
choice doesn't match with the checking function, then it changes all
the function pointers in the video driver and passes control on to the
new LoadLibrary method.
- Likewise, make X11_CreateWindow check this attribute before firing
off a call to X11_GL_GetVisual/X11_GLES_GetVisual
- Added a sanity check to the start of X11_GL_LoadLibrary
- Tidied up SDL_x11opengles.h
- Moved ownership of the gles_data structure over to
X11_GLES_LoadLibrary/UnloadLibrary
- Should incorporate the 3 fixes posted by Andre Heider

This is obviously quite a bit to take in, but is (at least) a proof of
concept for the approach I think EGL/GLX mingling should take. Any
comments/criticism is much appreciated.
  • Loading branch information
slouken committed Jul 18, 2012
1 parent a46a010 commit 180c5ca
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 104 deletions.
1 change: 1 addition & 0 deletions include/SDL_video.h
Expand Up @@ -183,6 +183,7 @@ typedef enum
SDL_GL_RETAINED_BACKING,
SDL_GL_CONTEXT_MAJOR_VERSION,
SDL_GL_CONTEXT_MINOR_VERSION,
SDL_GL_CONTEXT_EGL,
SDL_GL_CONTEXT_FLAGS,
SDL_GL_CONTEXT_PROFILE_MASK
} SDL_GLattr;
Expand Down
3 changes: 2 additions & 1 deletion src/video/SDL_sysvideo.h
Expand Up @@ -272,6 +272,7 @@ struct SDL_VideoDevice
int minor_version;
int flags;
int profile_mask;
int use_egl;
int retained_backing;
int driver_loaded;
char driver_path[256];
Expand All @@ -288,7 +289,7 @@ struct SDL_VideoDevice
void *driverdata;
struct SDL_GLDriverData *gl_data;

#if SDL_VIDEO_OPENGL_ES
#if SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2
struct SDL_PrivateGLESData *gles_data;
#endif

Expand Down
11 changes: 11 additions & 0 deletions src/video/SDL_video.c
Expand Up @@ -493,12 +493,15 @@ SDL_VideoInit(const char *driver_name)
#if SDL_VIDEO_OPENGL
_this->gl_config.major_version = 2;
_this->gl_config.minor_version = 1;
_this->gl_config.use_egl = 0;
#elif SDL_VIDEO_OPENGL_ES
_this->gl_config.major_version = 1;
_this->gl_config.minor_version = 1;
_this->gl_config.use_egl = 1;
#elif SDL_VIDEO_OPENGL_ES2
_this->gl_config.major_version = 2;
_this->gl_config.minor_version = 0;
_this->gl_config.use_egl = 1;
#endif
_this->gl_config.flags = 0;
_this->gl_config.profile_mask = 0;
Expand Down Expand Up @@ -2302,6 +2305,9 @@ SDL_GL_SetAttribute(SDL_GLattr attr, int value)
case SDL_GL_CONTEXT_MINOR_VERSION:
_this->gl_config.minor_version = value;
break;
case SDL_GL_CONTEXT_EGL:
_this->gl_config.use_egl = value;
break;
case SDL_GL_CONTEXT_FLAGS:
_this->gl_config.flags = value;
break;
Expand Down Expand Up @@ -2454,6 +2460,11 @@ SDL_GL_GetAttribute(SDL_GLattr attr, int *value)
*value = _this->gl_config.minor_version;
return 0;
}
case SDL_GL_CONTEXT_EGL:
{
*value = _this->gl_config.use_egl;
return 0;
}
case SDL_GL_CONTEXT_FLAGS:
{
*value = _this->gl_config.flags;
Expand Down
23 changes: 23 additions & 0 deletions src/video/x11/SDL_x11opengl.c
Expand Up @@ -29,6 +29,7 @@

#if SDL_VIDEO_OPENGL_GLX
#include "SDL_loadso.h"
#include "SDL_x11opengles.h"

#if defined(__IRIX__)
/* IRIX doesn't have a GL library versioning system */
Expand Down Expand Up @@ -122,6 +123,28 @@ X11_GL_LoadLibrary(_THIS, const char *path)
{
void *handle;

if (_this->gl_data) {
SDL_SetError("OpenGL context already created");
return -1;
}

#if SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2
/* If SDL_GL_CONTEXT_EGL has been changed to 1, switch over to X11_GLES functions */
if (_this->gl_config.use_egl == 1) {
_this->GL_LoadLibrary = X11_GLES_LoadLibrary;
_this->GL_GetProcAddress = X11_GLES_GetProcAddress;
_this->GL_UnloadLibrary = X11_GLES_UnloadLibrary;
_this->GL_CreateContext = X11_GLES_CreateContext;
_this->GL_MakeCurrent = X11_GLES_MakeCurrent;
_this->GL_SetSwapInterval = X11_GLES_SetSwapInterval;
_this->GL_GetSwapInterval = X11_GLES_GetSwapInterval;
_this->GL_SwapWindow = X11_GLES_SwapWindow;
_this->GL_DeleteContext = X11_GLES_DeleteContext;
return X11_GLES_LoadLibrary(_this, path);
}
#endif


/* Load the OpenGL library */
if (path == NULL) {
path = SDL_getenv("SDL_OPENGL_LIBRARY");
Expand Down
97 changes: 55 additions & 42 deletions src/video/x11/SDL_x11opengles.c
Expand Up @@ -24,6 +24,7 @@

#include "SDL_x11video.h"
#include "SDL_x11opengles.h"
#include "SDL_x11opengl.h"

#define DEFAULT_EGL "libEGL.so"
#define DEFAULT_OGL_ES2 "libGLESv2.so"
Expand Down Expand Up @@ -71,22 +72,14 @@ X11_GLES_GetProcAddress(_THIS, const char *proc)
void
X11_GLES_UnloadLibrary(_THIS)
{
if (_this->gl_config.driver_loaded) {
if ((_this->gles_data) && (_this->gl_config.driver_loaded)) {
_this->gles_data->eglTerminate(_this->gles_data->egl_display);

dlclose(_this->gl_config.dll_handle);
dlclose(_this->gles_data->egl_dll_handle);

_this->gles_data->eglGetProcAddress = NULL;
_this->gles_data->eglChooseConfig = NULL;
_this->gles_data->eglCreateContext = NULL;
_this->gles_data->eglCreateWindowSurface = NULL;
_this->gles_data->eglDestroyContext = NULL;
_this->gles_data->eglDestroySurface = NULL;
_this->gles_data->eglMakeCurrent = NULL;
_this->gles_data->eglSwapBuffers = NULL;
_this->gles_data->eglGetDisplay = NULL;
_this->gles_data->eglTerminate = NULL;
SDL_free(_this->gles_data);
_this->gles_data = NULL;

_this->gl_config.dll_handle = NULL;
_this->gl_config.driver_loaded = 0;
Expand All @@ -101,10 +94,27 @@ X11_GLES_LoadLibrary(_THIS, const char *path)

SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;

if (_this->gles_data->egl_active) {
if (_this->gles_data) {
SDL_SetError("OpenGL ES context already created");
return -1;
}

#if SDL_VIDEO_OPENGL_GLX
/* If SDL_GL_CONTEXT_EGL has been changed to 0, switch over to X11_GL functions */
if (_this->gl_config.use_egl == 0) {
_this->GL_LoadLibrary = X11_GL_LoadLibrary;
_this->GL_GetProcAddress = X11_GL_GetProcAddress;
_this->GL_UnloadLibrary = X11_GL_UnloadLibrary;
_this->GL_CreateContext = X11_GL_CreateContext;
_this->GL_MakeCurrent = X11_GL_MakeCurrent;
_this->GL_SetSwapInterval = X11_GL_SetSwapInterval;
_this->GL_GetSwapInterval = X11_GL_GetSwapInterval;
_this->GL_SwapWindow = X11_GL_SwapWindow;
_this->GL_DeleteContext = X11_GL_DeleteContext;
return X11_GL_LoadLibrary(_this, path);
}
#endif

#ifdef RTLD_GLOBAL
dlopen_flags = RTLD_LAZY | RTLD_GLOBAL;
#else
Expand All @@ -130,6 +140,12 @@ X11_GLES_LoadLibrary(_THIS, const char *path)
/* Unload the old driver and reset the pointers */
X11_GLES_UnloadLibrary(_this);

_this->gles_data = (struct SDL_PrivateGLESData *) SDL_calloc(1, sizeof(SDL_PrivateGLESData));
if (!_this->gles_data) {
SDL_OutOfMemory();
return -1;
}

/* Load new function pointers */
LOAD_FUNC(eglGetDisplay);
LOAD_FUNC(eglInitialize);
Expand Down Expand Up @@ -204,12 +220,9 @@ X11_GLES_GetVisual(_THIS, Display * display, int screen)
VisualID visual_id;
int i;

/* load the gl driver from a default path */
if (!_this->gl_config.driver_loaded) {
/* no driver has been loaded, use default (ourselves) */
if (X11_GLES_LoadLibrary(_this, NULL) < 0) {
return NULL;
}
if (!_this->gles_data) {
/* The EGL library wasn't loaded, SDL_GetError() should have info */
return NULL;
}

i = 0;
Expand Down Expand Up @@ -324,7 +337,6 @@ X11_GLES_CreateContext(_THIS, SDL_Window * window)
return NULL;
}

_this->gles_data->egl_active = 1;
_this->gles_data->egl_swapinterval = 0;

if (X11_GLES_MakeCurrent(_this, window, context) < 0) {
Expand Down Expand Up @@ -359,7 +371,7 @@ X11_GLES_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context)
int
X11_GLES_SetSwapInterval(_THIS, int interval)
{
if (_this->gles_data->egl_active != 1) {
if (_this->gles_data) {
SDL_SetError("OpenGL ES context not active");
return -1;
}
Expand All @@ -378,7 +390,7 @@ X11_GLES_SetSwapInterval(_THIS, int interval)
int
X11_GLES_GetSwapInterval(_THIS)
{
if (_this->gles_data->egl_active != 1) {
if (_this->gles_data) {
SDL_SetError("OpenGL ES context not active");
return -1;
}
Expand All @@ -397,30 +409,31 @@ void
X11_GLES_DeleteContext(_THIS, SDL_GLContext context)
{
/* Clean up GLES and EGL */
if (_this->gles_data->egl_context != EGL_NO_CONTEXT ||
_this->gles_data->egl_surface != EGL_NO_SURFACE) {
_this->gles_data->eglMakeCurrent(_this->gles_data->egl_display,
EGL_NO_SURFACE, EGL_NO_SURFACE,
EGL_NO_CONTEXT);

if (_this->gles_data->egl_context != EGL_NO_CONTEXT) {
_this->gles_data->eglDestroyContext(_this->gles_data->egl_display,
_this->gles_data->
egl_context);
_this->gles_data->egl_context = EGL_NO_CONTEXT;
}
if (_this->gles_data) {
if (_this->gles_data->egl_context != EGL_NO_CONTEXT ||
_this->gles_data->egl_surface != EGL_NO_SURFACE) {
_this->gles_data->eglMakeCurrent(_this->gles_data->egl_display,
EGL_NO_SURFACE, EGL_NO_SURFACE,
EGL_NO_CONTEXT);

if (_this->gles_data->egl_context != EGL_NO_CONTEXT) {
_this->gles_data->eglDestroyContext(_this->gles_data->egl_display,
_this->gles_data->
egl_context);
_this->gles_data->egl_context = EGL_NO_CONTEXT;
}

if (_this->gles_data->egl_surface != EGL_NO_SURFACE) {
_this->gles_data->eglDestroySurface(_this->gles_data->egl_display,
_this->gles_data->
egl_surface);
_this->gles_data->egl_surface = EGL_NO_SURFACE;
if (_this->gles_data->egl_surface != EGL_NO_SURFACE) {
_this->gles_data->eglDestroySurface(_this->gles_data->egl_display,
_this->gles_data->
egl_surface);
_this->gles_data->egl_surface = EGL_NO_SURFACE;
}
}
}
_this->gles_data->egl_active = 0;

/* crappy fix */
X11_GLES_UnloadLibrary(_this);
/* crappy fix */
X11_GLES_UnloadLibrary(_this);
}

}

Expand Down
12 changes: 11 additions & 1 deletion src/video/x11/SDL_x11opengles.h
Expand Up @@ -18,7 +18,12 @@
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "SDL_config.h"

#ifndef _SDL_x11opengles_h
#define _SDL_x11opengles_h

#if SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2
#include <GLES/gl.h>
#include <GLES/egl.h>
#include <dlfcn.h>
Expand All @@ -30,7 +35,6 @@

typedef struct SDL_PrivateGLESData
{
int egl_active; /* to stop switching drivers while we have a valid context */
XVisualInfo *egl_visualinfo;
void *egl_dll_handle;
EGLDisplay egl_display;
Expand Down Expand Up @@ -92,3 +96,9 @@ extern int X11_GLES_SetSwapInterval(_THIS, int interval);
extern int X11_GLES_GetSwapInterval(_THIS);
extern void X11_GLES_SwapWindow(_THIS, SDL_Window * window);
extern void X11_GLES_DeleteContext(_THIS, SDL_GLContext context);

#endif /* SDL_VIDEO_OPENGL_ES || SDL_VIDEO_OPENGL_ES2 */

#endif /* _SDL_x11opengles_h */

/* vi: set ts=4 sw=4 expandtab: */

0 comments on commit 180c5ca

Please sign in to comment.