src/video/SDL_egl.c
changeset 7659 ac4ce59c40e7
child 7679 b1fe132bc6a4
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/video/SDL_egl.c	Mon Aug 19 16:29:46 2013 -0300
     1.3 @@ -0,0 +1,407 @@
     1.4 +/*
     1.5 + *  Simple DirectMedia Layer
     1.6 + *  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
     1.7 + * 
     1.8 + *  This software is provided 'as-is', without any express or implied
     1.9 + *  warranty.  In no event will the authors be held liable for any damages
    1.10 + *  arising from the use of this software.
    1.11 + * 
    1.12 + *  Permission is granted to anyone to use this software for any purpose,
    1.13 + *  including commercial applications, and to alter it and redistribute it
    1.14 + *  freely, subject to the following restrictions:
    1.15 + * 
    1.16 + *  1. The origin of this software must not be misrepresented; you must not
    1.17 + *     claim that you wrote the original software. If you use this software
    1.18 + *     in a product, an acknowledgment in the product documentation would be
    1.19 + *     appreciated but is not required.
    1.20 + *  2. Altered source versions must be plainly marked as such, and must not be
    1.21 + *     misrepresented as being the original software.
    1.22 + *  3. This notice may not be removed or altered from any source distribution.
    1.23 + */
    1.24 +#include "SDL_config.h"
    1.25 +
    1.26 +#if SDL_VIDEO_OPENGL_EGL
    1.27 +
    1.28 +#include "SDL_sysvideo.h"
    1.29 +#include "SDL_egl.h"
    1.30 +
    1.31 +#define DEFAULT_EGL "libEGL.so"
    1.32 +#define DEFAULT_OGL_ES2 "libGLESv2.so"
    1.33 +#define DEFAULT_OGL_ES_PVR "libGLES_CM.so"
    1.34 +#define DEFAULT_OGL_ES "libGLESv1_CM.so"
    1.35 +
    1.36 +#define LOAD_FUNC(NAME) \
    1.37 +*((void**)&_this->egl_data->NAME) = dlsym(handle, #NAME); \
    1.38 +if (!_this->egl_data->NAME) \
    1.39 +{ \
    1.40 +    return SDL_SetError("Could not retrieve EGL function " #NAME); \
    1.41 +}
    1.42 +    
    1.43 +/* EGL implementation of SDL OpenGL ES support */
    1.44 +
    1.45 +void *
    1.46 +SDL_EGL_GetProcAddress(_THIS, const char *proc)
    1.47 +{
    1.48 +    static char procname[1024];
    1.49 +    void *handle;
    1.50 +    void *retval;
    1.51 +    
    1.52 +    /* eglGetProcAddress is busted on Android http://code.google.com/p/android/issues/detail?id=7681 */
    1.53 +#if !defined(SDL_VIDEO_DRIVER_ANDROID)
    1.54 +    handle = _this->egl_data->egl_dll_handle;
    1.55 +    if (_this->egl_data->eglGetProcAddress) {
    1.56 +        retval = _this->egl_data->eglGetProcAddress(proc);
    1.57 +        if (retval) {
    1.58 +            return retval;
    1.59 +        }
    1.60 +    }
    1.61 +#endif
    1.62 +    
    1.63 +    handle = _this->gl_config.dll_handle;
    1.64 +    #if defined(__OpenBSD__) && !defined(__ELF__)
    1.65 +    #undef dlsym(x,y);
    1.66 +    #endif
    1.67 +    retval = dlsym(handle, proc);
    1.68 +    if (!retval && strlen(proc) <= 1022) {
    1.69 +        procname[0] = '_';
    1.70 +        strcpy(procname + 1, proc);
    1.71 +        retval = dlsym(handle, procname);
    1.72 +    }
    1.73 +    return retval;
    1.74 +}
    1.75 +
    1.76 +void
    1.77 +SDL_EGL_UnloadLibrary(_THIS)
    1.78 +{
    1.79 +    if ((_this->egl_data) && (_this->gl_config.driver_loaded)) {
    1.80 +        _this->egl_data->eglTerminate(_this->egl_data->egl_display);
    1.81 +        
    1.82 +        dlclose(_this->gl_config.dll_handle);
    1.83 +        dlclose(_this->egl_data->egl_dll_handle);
    1.84 +        
    1.85 +        SDL_free(_this->egl_data);
    1.86 +        _this->egl_data = NULL;
    1.87 +        
    1.88 +        _this->gl_config.dll_handle = NULL;
    1.89 +        _this->gl_config.driver_loaded = 0;
    1.90 +    }
    1.91 +}
    1.92 +
    1.93 +int
    1.94 +SDL_EGL_LoadLibrary(_THIS, const char *path, NativeDisplayType native_display)
    1.95 +{
    1.96 +    void *handle;
    1.97 +    int dlopen_flags;
    1.98 +    
    1.99 +    if (_this->egl_data) {
   1.100 +        return SDL_SetError("OpenGL ES context already created");
   1.101 +    }
   1.102 +    
   1.103 +    /* Unload the old driver and reset the pointers */
   1.104 +    SDL_EGL_UnloadLibrary(_this);
   1.105 +    
   1.106 +    #ifdef RTLD_GLOBAL
   1.107 +    dlopen_flags = RTLD_LAZY | RTLD_GLOBAL;
   1.108 +    #else
   1.109 +    dlopen_flags = RTLD_LAZY;
   1.110 +    #endif
   1.111 +    handle = dlopen(path, dlopen_flags);
   1.112 +    /* Catch the case where the application isn't linked with EGL */
   1.113 +    if ((dlsym(handle, "eglChooseConfig") == NULL) && (path == NULL)) {
   1.114 +        
   1.115 +        dlclose(handle);
   1.116 +        path = getenv("SDL_VIDEO_EGL_DRIVER");
   1.117 +        if (path == NULL) {
   1.118 +            path = DEFAULT_EGL;
   1.119 +        }
   1.120 +        handle = dlopen(path, dlopen_flags);
   1.121 +    }
   1.122 +
   1.123 +    if (handle == NULL) {
   1.124 +        return SDL_SetError("Could not load OpenGL ES/EGL library");
   1.125 +    }
   1.126 +    
   1.127 +    _this->egl_data = (struct SDL_EGL_VideoData *) SDL_calloc(1, sizeof(SDL_EGL_VideoData));
   1.128 +    if (!_this->egl_data) {
   1.129 +        return SDL_OutOfMemory();
   1.130 +    }
   1.131 +    
   1.132 +    /* Load new function pointers */
   1.133 +    LOAD_FUNC(eglGetDisplay);
   1.134 +    LOAD_FUNC(eglInitialize);
   1.135 +    LOAD_FUNC(eglTerminate);
   1.136 +    LOAD_FUNC(eglGetProcAddress);
   1.137 +    LOAD_FUNC(eglChooseConfig);
   1.138 +    LOAD_FUNC(eglGetConfigAttrib);
   1.139 +    LOAD_FUNC(eglCreateContext);
   1.140 +    LOAD_FUNC(eglDestroyContext);
   1.141 +    LOAD_FUNC(eglCreateWindowSurface);
   1.142 +    LOAD_FUNC(eglDestroySurface);
   1.143 +    LOAD_FUNC(eglMakeCurrent);
   1.144 +    LOAD_FUNC(eglSwapBuffers);
   1.145 +    LOAD_FUNC(eglSwapInterval);
   1.146 +    LOAD_FUNC(eglWaitNative);
   1.147 +    LOAD_FUNC(eglWaitGL);
   1.148 +    
   1.149 +    _this->egl_data->egl_display = _this->egl_data->eglGetDisplay(native_display);
   1.150 +    
   1.151 +    if (!_this->egl_data->egl_display) {
   1.152 +        return SDL_SetError("Could not get EGL display");
   1.153 +    }
   1.154 +    
   1.155 +    if (_this->egl_data->eglInitialize(_this->egl_data->egl_display, NULL, NULL) != EGL_TRUE) {
   1.156 +        return SDL_SetError("Could not initialize EGL");
   1.157 +    }
   1.158 +
   1.159 +    _this->egl_data->egl_dll_handle = handle;
   1.160 +
   1.161 +    path = getenv("SDL_VIDEO_GL_DRIVER");
   1.162 +    handle = dlopen(path, dlopen_flags);
   1.163 +    if ((path == NULL) | (handle == NULL)) {
   1.164 +      if (_this->gl_config.major_version > 1) {
   1.165 +          path = DEFAULT_OGL_ES2;
   1.166 +          handle = dlopen(path, dlopen_flags);
   1.167 +      } else {
   1.168 +          path = DEFAULT_OGL_ES;
   1.169 +          handle = dlopen(path, dlopen_flags);
   1.170 +          if (handle == NULL) {
   1.171 +              path = DEFAULT_OGL_ES_PVR;
   1.172 +              handle = dlopen(path, dlopen_flags);
   1.173 +          }
   1.174 +      }
   1.175 +    }
   1.176 +
   1.177 +    if (handle == NULL) {
   1.178 +      return SDL_SetError("Could not initialize OpenGL ES library");
   1.179 +    }
   1.180 +
   1.181 +    _this->gl_config.dll_handle = handle;
   1.182 +    _this->gl_config.driver_loaded = 1;
   1.183 +
   1.184 +    if (path) {
   1.185 +      strncpy(_this->gl_config.driver_path, path,
   1.186 +              sizeof(_this->gl_config.driver_path) - 1);
   1.187 +    } else {
   1.188 +      strcpy(_this->gl_config.driver_path, "");
   1.189 +    }
   1.190 +    
   1.191 +    /* We need to select a config here to satisfy some video backends such as X11 */
   1.192 +    SDL_EGL_ChooseConfig(_this);
   1.193 +    
   1.194 +    return 0;
   1.195 +}
   1.196 +
   1.197 +int
   1.198 +SDL_EGL_ChooseConfig(_THIS) 
   1.199 +{
   1.200 +    /* 64 seems nice. */
   1.201 +    EGLint attribs[64];
   1.202 +    EGLint found_configs = 0;
   1.203 +    int i;
   1.204 +    
   1.205 +    if (!_this->egl_data) {
   1.206 +        /* The EGL library wasn't loaded, SDL_GetError() should have info */
   1.207 +        return -1;
   1.208 +    }
   1.209 +  
   1.210 +    /* Get a valid EGL configuration */
   1.211 +    i = 0;
   1.212 +    attribs[i++] = EGL_RED_SIZE;
   1.213 +    attribs[i++] = _this->gl_config.red_size;
   1.214 +    attribs[i++] = EGL_GREEN_SIZE;
   1.215 +    attribs[i++] = _this->gl_config.green_size;
   1.216 +    attribs[i++] = EGL_BLUE_SIZE;
   1.217 +    attribs[i++] = _this->gl_config.blue_size;
   1.218 +    
   1.219 +    if (_this->gl_config.alpha_size) {
   1.220 +        attribs[i++] = EGL_ALPHA_SIZE;
   1.221 +        attribs[i++] = _this->gl_config.alpha_size;
   1.222 +    }
   1.223 +    
   1.224 +    if (_this->gl_config.buffer_size) {
   1.225 +        attribs[i++] = EGL_BUFFER_SIZE;
   1.226 +        attribs[i++] = _this->gl_config.buffer_size;
   1.227 +    }
   1.228 +    
   1.229 +    attribs[i++] = EGL_DEPTH_SIZE;
   1.230 +    attribs[i++] = _this->gl_config.depth_size;
   1.231 +    
   1.232 +    if (_this->gl_config.stencil_size) {
   1.233 +        attribs[i++] = EGL_STENCIL_SIZE;
   1.234 +        attribs[i++] = _this->gl_config.stencil_size;
   1.235 +    }
   1.236 +    
   1.237 +    if (_this->gl_config.multisamplebuffers) {
   1.238 +        attribs[i++] = EGL_SAMPLE_BUFFERS;
   1.239 +        attribs[i++] = _this->gl_config.multisamplebuffers;
   1.240 +    }
   1.241 +    
   1.242 +    if (_this->gl_config.multisamplesamples) {
   1.243 +        attribs[i++] = EGL_SAMPLES;
   1.244 +        attribs[i++] = _this->gl_config.multisamplesamples;
   1.245 +    }
   1.246 +    
   1.247 +    attribs[i++] = EGL_RENDERABLE_TYPE;
   1.248 +    if (_this->gl_config.major_version == 2) {
   1.249 +        attribs[i++] = EGL_OPENGL_ES2_BIT;
   1.250 +    } else {
   1.251 +        attribs[i++] = EGL_OPENGL_ES_BIT;
   1.252 +    }
   1.253 +    
   1.254 +    attribs[i++] = EGL_NONE;
   1.255 +    
   1.256 +    if (_this->egl_data->eglChooseConfig(_this->egl_data->egl_display,
   1.257 +        attribs,
   1.258 +        &_this->egl_data->egl_config, 1,
   1.259 +        &found_configs) == EGL_FALSE ||
   1.260 +        found_configs == 0) {
   1.261 +        return SDL_SetError("Couldn't find matching EGL config");
   1.262 +    }
   1.263 +    
   1.264 +    return 0;
   1.265 +}
   1.266 +
   1.267 +SDL_GLContext
   1.268 +SDL_EGL_CreateContext(_THIS, EGLSurface egl_surface)
   1.269 +{
   1.270 +    EGLint context_attrib_list[] = {
   1.271 +        EGL_CONTEXT_CLIENT_VERSION,
   1.272 +        1,
   1.273 +        EGL_NONE
   1.274 +    };
   1.275 +    
   1.276 +    EGLContext egl_context;
   1.277 +    
   1.278 +    if (!_this->egl_data) {
   1.279 +        /* The EGL library wasn't loaded, SDL_GetError() should have info */
   1.280 +        return NULL;
   1.281 +    }
   1.282 +    
   1.283 +    if (_this->gl_config.major_version) {
   1.284 +        context_attrib_list[1] = _this->gl_config.major_version;
   1.285 +    }
   1.286 +
   1.287 +    egl_context =
   1.288 +    _this->egl_data->eglCreateContext(_this->egl_data->egl_display,
   1.289 +                                      _this->egl_data->egl_config,
   1.290 +                                      EGL_NO_CONTEXT, context_attrib_list);
   1.291 +    
   1.292 +    if (egl_context == EGL_NO_CONTEXT) {
   1.293 +        SDL_SetError("Could not create EGL context");
   1.294 +        return NULL;
   1.295 +    }
   1.296 +    
   1.297 +    _this->egl_data->egl_swapinterval = 0;
   1.298 +    
   1.299 +    if (SDL_EGL_MakeCurrent(_this, egl_surface, egl_context) < 0) {
   1.300 +        SDL_EGL_DeleteContext(_this, egl_context);
   1.301 +        SDL_SetError("Could not make EGL context current");
   1.302 +        return NULL;
   1.303 +    }
   1.304 +  
   1.305 +    return (SDL_GLContext) egl_context;
   1.306 +}
   1.307 +
   1.308 +int
   1.309 +SDL_EGL_MakeCurrent(_THIS, EGLSurface egl_surface, SDL_GLContext context)
   1.310 +{
   1.311 +    if (!_this->egl_data) {
   1.312 +        return SDL_SetError("OpenGL not initialized");
   1.313 +    }
   1.314 +    
   1.315 +    EGLContext egl_context = (EGLContext) context;
   1.316 +    
   1.317 +    /* The android emulator crashes badly if you try to eglMakeCurrent 
   1.318 +     * with a valid context and invalid surface, so we have to check for both here.
   1.319 +     */
   1.320 +    if (!egl_context || !egl_surface) {
   1.321 +         _this->egl_data->eglMakeCurrent(_this->egl_data->egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
   1.322 +    }
   1.323 +    else {
   1.324 +        if (!_this->egl_data->eglMakeCurrent(_this->egl_data->egl_display,
   1.325 +            egl_surface, egl_surface, egl_context)) {
   1.326 +            return SDL_SetError("Unable to make EGL context current");
   1.327 +        }
   1.328 +    }
   1.329 +      
   1.330 +    return 0;
   1.331 +}
   1.332 +
   1.333 +int
   1.334 +SDL_EGL_SetSwapInterval(_THIS, int interval)
   1.335 +{
   1.336 +    if (_this->egl_data) {
   1.337 +        return SDL_SetError("OpenGL ES context not active");
   1.338 +    }
   1.339 +    
   1.340 +    EGLBoolean status;
   1.341 +    status = _this->egl_data->eglSwapInterval(_this->egl_data->egl_display, interval);
   1.342 +    if (status == EGL_TRUE) {
   1.343 +        _this->egl_data->egl_swapinterval = interval;
   1.344 +        return 0;
   1.345 +    }
   1.346 +    
   1.347 +    return SDL_SetError("Unable to set the EGL swap interval");
   1.348 +}
   1.349 +
   1.350 +int
   1.351 +SDL_EGL_GetSwapInterval(_THIS)
   1.352 +{
   1.353 +    if (_this->egl_data) {
   1.354 +        return SDL_SetError("OpenGL ES context not active");
   1.355 +    }
   1.356 +    
   1.357 +    return _this->egl_data->egl_swapinterval;
   1.358 +}
   1.359 +
   1.360 +void
   1.361 +SDL_EGL_SwapBuffers(_THIS, EGLSurface egl_surface)
   1.362 +{
   1.363 +    _this->egl_data->eglSwapBuffers(_this->egl_data->egl_display, egl_surface);
   1.364 +}
   1.365 +
   1.366 +void
   1.367 +SDL_EGL_DeleteContext(_THIS, SDL_GLContext context)
   1.368 +{
   1.369 +    /* Clean up GLES and EGL */
   1.370 +    if (!_this->egl_data) {
   1.371 +        return;
   1.372 +    }
   1.373 +    
   1.374 +    EGLContext egl_context = (EGLContext) context;
   1.375 +    
   1.376 +    if (!egl_context && egl_context != EGL_NO_CONTEXT) {
   1.377 +        SDL_EGL_MakeCurrent(_this, NULL, NULL);
   1.378 +        _this->egl_data->eglDestroyContext(_this->egl_data->egl_display, egl_context);
   1.379 +    }
   1.380 +        
   1.381 +    /* FIXME: This "crappy fix" comes from the X11 code, 
   1.382 +     * it's required so you can create a GLX context, destroy it and create a EGL one */
   1.383 +    SDL_EGL_UnloadLibrary(_this);
   1.384 +}
   1.385 +
   1.386 +EGLSurface *
   1.387 +SDL_EGL_CreateSurface(_THIS, NativeWindowType nw) 
   1.388 +{
   1.389 +    return _this->egl_data->eglCreateWindowSurface(
   1.390 +            _this->egl_data->egl_display,
   1.391 +            _this->egl_data->egl_config,
   1.392 +            nw, NULL);
   1.393 +}
   1.394 +
   1.395 +void
   1.396 +SDL_EGL_DestroySurface(_THIS, EGLSurface egl_surface) 
   1.397 +{
   1.398 +    if (!_this->egl_data) {
   1.399 +        return;
   1.400 +    }
   1.401 +    
   1.402 +    if (egl_surface != EGL_NO_SURFACE) {
   1.403 +        _this->egl_data->eglDestroySurface(_this->egl_data->egl_display, egl_surface);
   1.404 +    }
   1.405 +}
   1.406 +
   1.407 +#endif /* SDL_VIDEO_OPENGL_EGL */
   1.408 +
   1.409 +/* vi: set ts=4 sw=4 expandtab: */
   1.410 +    
   1.411 \ No newline at end of file