Improved simultaneous support for OpenGL and OpenGL ES

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.
This commit is contained in:
Sam Lantinga 2012-07-18 15:17:27 -07:00
parent cb83481887
commit b3219a6db1
9 changed files with 127 additions and 103 deletions

View file

@ -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"
@ -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;
@ -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
@ -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);
@ -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;
@ -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) {
@ -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;
}
@ -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;
}
@ -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) {
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_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;
}
/* crappy fix */
X11_GLES_UnloadLibrary(_this);
}
_this->gles_data->egl_active = 0;
/* crappy fix */
X11_GLES_UnloadLibrary(_this);
}