src/video/x11/SDL_x11opengles.c
changeset 6370 93187f7f7d5d
parent 6368 fcbbd0e2da5e
child 6373 494e0436525f
--- a/src/video/x11/SDL_x11opengles.c	Wed Jul 18 15:02:48 2012 -0700
+++ b/src/video/x11/SDL_x11opengles.c	Wed Jul 18 15:17:27 2012 -0700
@@ -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 @@
 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 @@
 
     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 @@
     /* 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 @@
     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 @@
         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 @@
 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 @@
 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 @@
 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);
 
 }