src/video/SDL_egl.c
changeset 7753 e4c38f17bfad
parent 7702 83878644c8e6
child 7782 e4ed6e54101e
     1.1 --- a/src/video/SDL_egl.c	Sat Sep 28 12:55:32 2013 +0200
     1.2 +++ b/src/video/SDL_egl.c	Sat Sep 28 13:28:19 2013 -0300
     1.3 @@ -25,13 +25,20 @@
     1.4  #include "SDL_sysvideo.h"
     1.5  #include "SDL_egl.h"
     1.6  
     1.7 -#define DEFAULT_EGL "libEGL.so"
     1.8 -#define DEFAULT_OGL_ES2 "libGLESv2.so"
     1.9 -#define DEFAULT_OGL_ES_PVR "libGLES_CM.so"
    1.10 -#define DEFAULT_OGL_ES "libGLESv1_CM.so"
    1.11 +#if SDL_VIDEO_DRIVER_RPI
    1.12 +#define DEFAULT_EGL "/opt/vc/lib/libEGL.so"
    1.13 +#define DEFAULT_OGL_ES2 "/opt/vc/lib/libGLESv2.so"
    1.14 +#define DEFAULT_OGL_ES_PVR "/opt/vc/lib/libGLES_CM.so"
    1.15 +#define DEFAULT_OGL_ES "/opt/vc/lib/libGLESv1_CM.so"
    1.16 +#else
    1.17 +#define DEFAULT_EGL "libEGL.so.1"
    1.18 +#define DEFAULT_OGL_ES2 "libGLESv2.so.2"
    1.19 +#define DEFAULT_OGL_ES_PVR "libGLES_CM.so.1"
    1.20 +#define DEFAULT_OGL_ES "libGLESv1_CM.so.1"
    1.21 +#endif /* SDL_VIDEO_DRIVER_RPI */
    1.22  
    1.23  #define LOAD_FUNC(NAME) \
    1.24 -*((void**)&_this->egl_data->NAME) = dlsym(handle, #NAME); \
    1.25 +*((void**)&_this->egl_data->NAME) = dlsym(dll_handle, #NAME); \
    1.26  if (!_this->egl_data->NAME) \
    1.27  { \
    1.28      return SDL_SetError("Could not retrieve EGL function " #NAME); \
    1.29 @@ -88,9 +95,10 @@
    1.30  }
    1.31  
    1.32  int
    1.33 -SDL_EGL_LoadLibrary(_THIS, const char *path, NativeDisplayType native_display)
    1.34 +SDL_EGL_LoadLibrary(_THIS, const char *egl_path, NativeDisplayType native_display)
    1.35  {
    1.36 -    void *handle;
    1.37 +    void *dll_handle, *egl_dll_handle; /* The naming is counter intuitive, but hey, I just work here -- Gabriel */
    1.38 +    char *path;
    1.39      int dlopen_flags;
    1.40      
    1.41      if (_this->egl_data) {
    1.42 @@ -105,22 +113,44 @@
    1.43      #else
    1.44      dlopen_flags = RTLD_LAZY;
    1.45      #endif
    1.46 -    handle = dlopen(path, dlopen_flags);
    1.47 +    
    1.48 +    /* A funny thing, loading EGL.so first does not work on the Raspberry, so we load libGL* first */
    1.49 +    path = getenv("SDL_VIDEO_GL_DRIVER");
    1.50 +    egl_dll_handle = dlopen(path, dlopen_flags);
    1.51 +    if ((path == NULL) | (egl_dll_handle == NULL)) {
    1.52 +        if (_this->gl_config.major_version > 1) {
    1.53 +            path = DEFAULT_OGL_ES2;
    1.54 +            egl_dll_handle = dlopen(path, dlopen_flags);
    1.55 +        } else {
    1.56 +            path = DEFAULT_OGL_ES;
    1.57 +            egl_dll_handle = dlopen(path, dlopen_flags);
    1.58 +            if (egl_dll_handle == NULL) {
    1.59 +                path = DEFAULT_OGL_ES_PVR;
    1.60 +                egl_dll_handle = dlopen(path, dlopen_flags);
    1.61 +            }
    1.62 +        }
    1.63 +    }
    1.64 +
    1.65 +    if (egl_dll_handle == NULL) {
    1.66 +        return SDL_SetError("Could not initialize OpenGL ES library: %s", dlerror());
    1.67 +    }
    1.68 +    
    1.69 +    /* Loading libGL* in the previous step took care of loading libEGL.so, but we future proof by double checking */
    1.70 +    dll_handle = dlopen(egl_path, dlopen_flags);
    1.71      /* Catch the case where the application isn't linked with EGL */
    1.72 -    if ((dlsym(handle, "eglChooseConfig") == NULL) && (path == NULL)) {
    1.73 -        
    1.74 -        dlclose(handle);
    1.75 +    if ((dlsym(dll_handle, "eglChooseConfig") == NULL) && (egl_path == NULL)) {
    1.76 +        dlclose(dll_handle);
    1.77          path = getenv("SDL_VIDEO_EGL_DRIVER");
    1.78          if (path == NULL) {
    1.79              path = DEFAULT_EGL;
    1.80          }
    1.81 -        handle = dlopen(path, dlopen_flags);
    1.82 +        dll_handle = dlopen(path, dlopen_flags);
    1.83 +    }
    1.84 +    
    1.85 +    if (dll_handle == NULL) {
    1.86 +        return SDL_SetError("Could not load EGL library: %s", dlerror());
    1.87      }
    1.88  
    1.89 -    if (handle == NULL) {
    1.90 -        return SDL_SetError("Could not load OpenGL ES/EGL library");
    1.91 -    }
    1.92 -    
    1.93      _this->egl_data = (struct SDL_EGL_VideoData *) SDL_calloc(1, sizeof(SDL_EGL_VideoData));
    1.94      if (!_this->egl_data) {
    1.95          return SDL_OutOfMemory();
    1.96 @@ -153,36 +183,14 @@
    1.97          return SDL_SetError("Could not initialize EGL");
    1.98      }
    1.99  
   1.100 -    _this->egl_data->egl_dll_handle = handle;
   1.101 -
   1.102 -    path = getenv("SDL_VIDEO_GL_DRIVER");
   1.103 -    handle = dlopen(path, dlopen_flags);
   1.104 -    if ((path == NULL) | (handle == NULL)) {
   1.105 -      if (_this->gl_config.major_version > 1) {
   1.106 -          path = DEFAULT_OGL_ES2;
   1.107 -          handle = dlopen(path, dlopen_flags);
   1.108 -      } else {
   1.109 -          path = DEFAULT_OGL_ES;
   1.110 -          handle = dlopen(path, dlopen_flags);
   1.111 -          if (handle == NULL) {
   1.112 -              path = DEFAULT_OGL_ES_PVR;
   1.113 -              handle = dlopen(path, dlopen_flags);
   1.114 -          }
   1.115 -      }
   1.116 -    }
   1.117 -
   1.118 -    if (handle == NULL) {
   1.119 -      return SDL_SetError("Could not initialize OpenGL ES library");
   1.120 -    }
   1.121 -
   1.122 -    _this->gl_config.dll_handle = handle;
   1.123 +    _this->gl_config.dll_handle = dll_handle;
   1.124 +    _this->egl_data->egl_dll_handle = egl_dll_handle;
   1.125      _this->gl_config.driver_loaded = 1;
   1.126  
   1.127      if (path) {
   1.128 -      strncpy(_this->gl_config.driver_path, path,
   1.129 -              sizeof(_this->gl_config.driver_path) - 1);
   1.130 +        strncpy(_this->gl_config.driver_path, path, sizeof(_this->gl_config.driver_path) - 1);
   1.131      } else {
   1.132 -      strcpy(_this->gl_config.driver_path, "");
   1.133 +        strcpy(_this->gl_config.driver_path, "");
   1.134      }
   1.135      
   1.136      /* We need to select a config here to satisfy some video backends such as X11 */