src/video/SDL_egl.c
author Gabriel Jacobo <gabomdq@gmail.com>
Fri, 22 Nov 2013 13:24:53 -0300
changeset 8021 f96e12e0ade9
parent 8015 ba26f042e36d
child 8041 afd62b3fda31
child 8543 b9dd3cf38585
permissions -rw-r--r--
OpenGL ES support for Windows
gabomdq@7659
     1
/*
gabomdq@7659
     2
 *  Simple DirectMedia Layer
gabomdq@7659
     3
 *  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
gabomdq@7659
     4
 * 
gabomdq@7659
     5
 *  This software is provided 'as-is', without any express or implied
gabomdq@7659
     6
 *  warranty.  In no event will the authors be held liable for any damages
gabomdq@7659
     7
 *  arising from the use of this software.
gabomdq@7659
     8
 * 
gabomdq@7659
     9
 *  Permission is granted to anyone to use this software for any purpose,
gabomdq@7659
    10
 *  including commercial applications, and to alter it and redistribute it
gabomdq@7659
    11
 *  freely, subject to the following restrictions:
gabomdq@7659
    12
 * 
gabomdq@7659
    13
 *  1. The origin of this software must not be misrepresented; you must not
gabomdq@7659
    14
 *     claim that you wrote the original software. If you use this software
gabomdq@7659
    15
 *     in a product, an acknowledgment in the product documentation would be
gabomdq@7659
    16
 *     appreciated but is not required.
gabomdq@7659
    17
 *  2. Altered source versions must be plainly marked as such, and must not be
gabomdq@7659
    18
 *     misrepresented as being the original software.
gabomdq@7659
    19
 *  3. This notice may not be removed or altered from any source distribution.
gabomdq@7659
    20
 */
gabomdq@7659
    21
#include "SDL_config.h"
gabomdq@7659
    22
gabomdq@7659
    23
#if SDL_VIDEO_OPENGL_EGL
gabomdq@7659
    24
gabomdq@7659
    25
#include "SDL_sysvideo.h"
gabomdq@8021
    26
#include "SDL_egl_c.h"
gabomdq@8021
    27
#include "SDL_loadso.h"
gabomdq@8021
    28
#include "SDL_hints.h"
gabomdq@7782
    29
gabomdq@7753
    30
#if SDL_VIDEO_DRIVER_RPI
gabomdq@7782
    31
/* Raspbian places the OpenGL ES/EGL binaries in a non standard path */
gabomdq@7753
    32
#define DEFAULT_EGL "/opt/vc/lib/libEGL.so"
gabomdq@7753
    33
#define DEFAULT_OGL_ES2 "/opt/vc/lib/libGLESv2.so"
gabomdq@7753
    34
#define DEFAULT_OGL_ES_PVR "/opt/vc/lib/libGLES_CM.so"
gabomdq@7753
    35
#define DEFAULT_OGL_ES "/opt/vc/lib/libGLESv1_CM.so"
gabomdq@7782
    36
gabomdq@7782
    37
#elif SDL_VIDEO_DRIVER_ANDROID
gabomdq@7782
    38
/* Android */
gabomdq@7782
    39
#define DEFAULT_EGL "libEGL.so"
gabomdq@7782
    40
#define DEFAULT_OGL_ES2 "libGLESv2.so"
gabomdq@7782
    41
#define DEFAULT_OGL_ES_PVR "libGLES_CM.so"
gabomdq@7782
    42
#define DEFAULT_OGL_ES "libGLESv1_CM.so"
gabomdq@7782
    43
gabomdq@8021
    44
#elif SDL_VIDEO_DRIVER_WINDOWS
gabomdq@8021
    45
/* EGL AND OpenGL ES support via ANGLE */
gabomdq@8021
    46
#define DEFAULT_EGL "libEGL.dll"
gabomdq@8021
    47
#define DEFAULT_OGL_ES2 "libGLESv2.dll"
gabomdq@8021
    48
#define DEFAULT_OGL_ES_PVR "libGLES_CM.dll"
gabomdq@8021
    49
#define DEFAULT_OGL_ES "libGLESv1_CM.dll"
gabomdq@8021
    50
gabomdq@7753
    51
#else
gabomdq@7782
    52
/* Desktop Linux */
gabomdq@7753
    53
#define DEFAULT_EGL "libEGL.so.1"
gabomdq@7753
    54
#define DEFAULT_OGL_ES2 "libGLESv2.so.2"
gabomdq@7753
    55
#define DEFAULT_OGL_ES_PVR "libGLES_CM.so.1"
gabomdq@7753
    56
#define DEFAULT_OGL_ES "libGLESv1_CM.so.1"
gabomdq@7753
    57
#endif /* SDL_VIDEO_DRIVER_RPI */
gabomdq@7659
    58
gabomdq@7659
    59
#define LOAD_FUNC(NAME) \
gabomdq@8021
    60
*((void**)&_this->egl_data->NAME) = SDL_LoadFunction(_this->egl_data->dll_handle, #NAME); \
gabomdq@7659
    61
if (!_this->egl_data->NAME) \
gabomdq@7659
    62
{ \
gabomdq@7659
    63
    return SDL_SetError("Could not retrieve EGL function " #NAME); \
gabomdq@7659
    64
}
gabomdq@7659
    65
    
gabomdq@7659
    66
/* EGL implementation of SDL OpenGL ES support */
gabomdq@7659
    67
gabomdq@7659
    68
void *
gabomdq@7659
    69
SDL_EGL_GetProcAddress(_THIS, const char *proc)
gabomdq@7659
    70
{
gabomdq@7659
    71
    static char procname[1024];
gabomdq@7659
    72
    void *retval;
gabomdq@7659
    73
    
gabomdq@7659
    74
    /* eglGetProcAddress is busted on Android http://code.google.com/p/android/issues/detail?id=7681 */
gabomdq@8021
    75
#if !defined(SDL_VIDEO_DRIVER_ANDROID) 
gabomdq@7659
    76
    if (_this->egl_data->eglGetProcAddress) {
gabomdq@7659
    77
        retval = _this->egl_data->eglGetProcAddress(proc);
gabomdq@7659
    78
        if (retval) {
gabomdq@7659
    79
            return retval;
gabomdq@7659
    80
        }
gabomdq@7659
    81
    }
gabomdq@7659
    82
#endif
gabomdq@7659
    83
    
gabomdq@8021
    84
    retval = SDL_LoadFunction(_this->egl_data->egl_dll_handle, proc);
gabomdq@8021
    85
    if (!retval && SDL_strlen(proc) <= 1022) {
gabomdq@7659
    86
        procname[0] = '_';
gabomdq@8021
    87
        SDL_strlcpy(procname + 1, proc, 1022);
gabomdq@8021
    88
        retval = SDL_LoadFunction(_this->egl_data->egl_dll_handle, procname);
gabomdq@7659
    89
    }
gabomdq@7659
    90
    return retval;
gabomdq@7659
    91
}
gabomdq@7659
    92
gabomdq@7659
    93
void
gabomdq@7659
    94
SDL_EGL_UnloadLibrary(_THIS)
gabomdq@7659
    95
{
slouken@7865
    96
    if (_this->egl_data) {
slouken@7865
    97
        if (_this->egl_data->egl_display) {
slouken@7865
    98
            _this->egl_data->eglTerminate(_this->egl_data->egl_display);
slouken@7865
    99
            _this->egl_data->egl_display = NULL;
slouken@7865
   100
        }
slouken@7865
   101
gabomdq@8021
   102
        if (_this->egl_data->dll_handle) {
gabomdq@8021
   103
            SDL_UnloadObject(_this->egl_data->dll_handle);
gabomdq@8021
   104
            _this->egl_data->dll_handle = NULL;
slouken@7865
   105
        }
slouken@7865
   106
        if (_this->egl_data->egl_dll_handle) {
gabomdq@8021
   107
            SDL_UnloadObject(_this->egl_data->egl_dll_handle);
slouken@7865
   108
            _this->egl_data->egl_dll_handle = NULL;
slouken@7865
   109
        }
gabomdq@7659
   110
        
gabomdq@7659
   111
        SDL_free(_this->egl_data);
gabomdq@7659
   112
        _this->egl_data = NULL;
gabomdq@7659
   113
    }
gabomdq@7659
   114
}
gabomdq@7659
   115
gabomdq@7659
   116
int
gabomdq@7753
   117
SDL_EGL_LoadLibrary(_THIS, const char *egl_path, NativeDisplayType native_display)
gabomdq@7659
   118
{
gabomdq@8021
   119
    void *dll_handle = NULL, *egl_dll_handle = NULL; /* The naming is counter intuitive, but hey, I just work here -- Gabriel */
gabomdq@8021
   120
    char *path = NULL;
gabomdq@8021
   121
#if SDL_VIDEO_DRIVER_WINDOWS
gabomdq@8021
   122
    const char *d3dcompiler;
gabomdq@8021
   123
#endif
gabomdq@8021
   124
gabomdq@7659
   125
    if (_this->egl_data) {
gabomdq@7659
   126
        return SDL_SetError("OpenGL ES context already created");
gabomdq@7659
   127
    }
slouken@7865
   128
slouken@7865
   129
    _this->egl_data = (struct SDL_EGL_VideoData *) SDL_calloc(1, sizeof(SDL_EGL_VideoData));
slouken@7865
   130
    if (!_this->egl_data) {
slouken@7865
   131
        return SDL_OutOfMemory();
slouken@7865
   132
    }
slouken@7865
   133
gabomdq@8021
   134
#if SDL_VIDEO_DRIVER_WINDOWS
gabomdq@8021
   135
    d3dcompiler = SDL_GetHint(SDL_HINT_VIDEO_WIN_D3DCOMPILER);
gabomdq@8021
   136
    if (!d3dcompiler) {
gabomdq@8021
   137
        /* By default we load the Vista+ compatible compiler */
gabomdq@8021
   138
        d3dcompiler = "d3dcompiler_46.dll";
gabomdq@8021
   139
    }
gabomdq@8021
   140
    if (SDL_strcasecmp(d3dcompiler, "none") != 0) {
gabomdq@8021
   141
        SDL_LoadObject(d3dcompiler);
gabomdq@8021
   142
    }
slouken@7865
   143
#endif
slouken@7865
   144
gabomdq@7753
   145
    /* A funny thing, loading EGL.so first does not work on the Raspberry, so we load libGL* first */
gabomdq@8021
   146
    path = SDL_getenv("SDL_VIDEO_GL_DRIVER");
gabomdq@8021
   147
    if (path != NULL) {
gabomdq@8021
   148
        egl_dll_handle = SDL_LoadObject(path);
gabomdq@8021
   149
    }
gabomdq@8021
   150
gabomdq@8021
   151
    if (egl_dll_handle == NULL) {
gabomdq@7753
   152
        if (_this->gl_config.major_version > 1) {
gabomdq@7753
   153
            path = DEFAULT_OGL_ES2;
gabomdq@8021
   154
            egl_dll_handle = SDL_LoadObject(path);
gabomdq@8021
   155
        }
gabomdq@8021
   156
        else {
gabomdq@7753
   157
            path = DEFAULT_OGL_ES;
gabomdq@8021
   158
            egl_dll_handle = SDL_LoadObject(path);
gabomdq@7753
   159
            if (egl_dll_handle == NULL) {
gabomdq@7753
   160
                path = DEFAULT_OGL_ES_PVR;
gabomdq@8021
   161
                egl_dll_handle = SDL_LoadObject(path);
gabomdq@7753
   162
            }
gabomdq@7753
   163
        }
gabomdq@7753
   164
    }
slouken@7865
   165
    _this->egl_data->egl_dll_handle = egl_dll_handle;
gabomdq@7753
   166
gabomdq@7753
   167
    if (egl_dll_handle == NULL) {
gabomdq@8021
   168
        return SDL_SetError("Could not initialize OpenGL ES library");
gabomdq@7753
   169
    }
gabomdq@8021
   170
gabomdq@7753
   171
    /* Loading libGL* in the previous step took care of loading libEGL.so, but we future proof by double checking */
gabomdq@8021
   172
    if (egl_path != NULL) {
gabomdq@8021
   173
        dll_handle = SDL_LoadObject(egl_path);
gabomdq@8021
   174
    }   
gabomdq@7659
   175
    /* Catch the case where the application isn't linked with EGL */
gabomdq@8021
   176
    if ((SDL_LoadFunction(dll_handle, "eglChooseConfig") == NULL) && (egl_path == NULL)) {
gabomdq@8021
   177
        if (dll_handle != NULL) {
gabomdq@8021
   178
            SDL_UnloadObject(dll_handle);
gabomdq@8021
   179
        }
gabomdq@8021
   180
        path = SDL_getenv("SDL_VIDEO_EGL_DRIVER");
gabomdq@7659
   181
        if (path == NULL) {
gabomdq@7659
   182
            path = DEFAULT_EGL;
gabomdq@7659
   183
        }
gabomdq@8021
   184
        dll_handle = SDL_LoadObject(path);
gabomdq@8021
   185
        if (dll_handle == NULL) {
gabomdq@8021
   186
            return SDL_SetError("Could not load EGL library");
gabomdq@8021
   187
        }
gabomdq@7753
   188
    }
slouken@7865
   189
gabomdq@8021
   190
    _this->egl_data->dll_handle = dll_handle;
gabomdq@7659
   191
gabomdq@7659
   192
    /* Load new function pointers */
gabomdq@7659
   193
    LOAD_FUNC(eglGetDisplay);
gabomdq@7659
   194
    LOAD_FUNC(eglInitialize);
gabomdq@7659
   195
    LOAD_FUNC(eglTerminate);
gabomdq@7659
   196
    LOAD_FUNC(eglGetProcAddress);
gabomdq@7659
   197
    LOAD_FUNC(eglChooseConfig);
gabomdq@7659
   198
    LOAD_FUNC(eglGetConfigAttrib);
gabomdq@7659
   199
    LOAD_FUNC(eglCreateContext);
gabomdq@7659
   200
    LOAD_FUNC(eglDestroyContext);
gabomdq@7659
   201
    LOAD_FUNC(eglCreateWindowSurface);
gabomdq@7659
   202
    LOAD_FUNC(eglDestroySurface);
gabomdq@7659
   203
    LOAD_FUNC(eglMakeCurrent);
gabomdq@7659
   204
    LOAD_FUNC(eglSwapBuffers);
gabomdq@7659
   205
    LOAD_FUNC(eglSwapInterval);
gabomdq@7659
   206
    LOAD_FUNC(eglWaitNative);
gabomdq@7659
   207
    LOAD_FUNC(eglWaitGL);
gabomdq@7659
   208
    
gabomdq@7659
   209
    _this->egl_data->egl_display = _this->egl_data->eglGetDisplay(native_display);
gabomdq@7659
   210
    if (!_this->egl_data->egl_display) {
gabomdq@7659
   211
        return SDL_SetError("Could not get EGL display");
gabomdq@7659
   212
    }
gabomdq@7659
   213
    
gabomdq@7659
   214
    if (_this->egl_data->eglInitialize(_this->egl_data->egl_display, NULL, NULL) != EGL_TRUE) {
gabomdq@7659
   215
        return SDL_SetError("Could not initialize EGL");
gabomdq@7659
   216
    }
gabomdq@7659
   217
gabomdq@8021
   218
    _this->egl_data->dll_handle = dll_handle;
gabomdq@7753
   219
    _this->egl_data->egl_dll_handle = egl_dll_handle;
gabomdq@7659
   220
    _this->gl_config.driver_loaded = 1;
gabomdq@7659
   221
gabomdq@7659
   222
    if (path) {
gabomdq@8021
   223
        SDL_strlcpy(_this->gl_config.driver_path, path, sizeof(_this->gl_config.driver_path) - 1);
gabomdq@7659
   224
    } else {
gabomdq@8021
   225
        *_this->gl_config.driver_path = '\0';
gabomdq@7659
   226
    }
gabomdq@7659
   227
    
gabomdq@7659
   228
    /* We need to select a config here to satisfy some video backends such as X11 */
gabomdq@7986
   229
    return SDL_EGL_ChooseConfig(_this);
gabomdq@7659
   230
}
gabomdq@7659
   231
gabomdq@7659
   232
int
gabomdq@7659
   233
SDL_EGL_ChooseConfig(_THIS) 
gabomdq@7659
   234
{
gabomdq@7659
   235
    /* 64 seems nice. */
gabomdq@7659
   236
    EGLint attribs[64];
gabomdq@8021
   237
    EGLint found_configs = 0, value;
gabomdq@8015
   238
    /* 128 seems even nicer here */
gabomdq@8015
   239
    EGLConfig configs[128];
gabomdq@8021
   240
    int i, j, best_bitdiff = -1, bitdiff;
gabomdq@7659
   241
    
gabomdq@7659
   242
    if (!_this->egl_data) {
gabomdq@7659
   243
        /* The EGL library wasn't loaded, SDL_GetError() should have info */
gabomdq@7659
   244
        return -1;
gabomdq@7659
   245
    }
gabomdq@7659
   246
  
gabomdq@7659
   247
    /* Get a valid EGL configuration */
gabomdq@7659
   248
    i = 0;
gabomdq@7659
   249
    attribs[i++] = EGL_RED_SIZE;
gabomdq@7659
   250
    attribs[i++] = _this->gl_config.red_size;
gabomdq@7659
   251
    attribs[i++] = EGL_GREEN_SIZE;
gabomdq@7659
   252
    attribs[i++] = _this->gl_config.green_size;
gabomdq@7659
   253
    attribs[i++] = EGL_BLUE_SIZE;
gabomdq@7659
   254
    attribs[i++] = _this->gl_config.blue_size;
gabomdq@7659
   255
    
gabomdq@7659
   256
    if (_this->gl_config.alpha_size) {
gabomdq@7659
   257
        attribs[i++] = EGL_ALPHA_SIZE;
gabomdq@7659
   258
        attribs[i++] = _this->gl_config.alpha_size;
gabomdq@7659
   259
    }
gabomdq@7659
   260
    
gabomdq@7659
   261
    if (_this->gl_config.buffer_size) {
gabomdq@7659
   262
        attribs[i++] = EGL_BUFFER_SIZE;
gabomdq@7659
   263
        attribs[i++] = _this->gl_config.buffer_size;
gabomdq@7659
   264
    }
gabomdq@7659
   265
    
gabomdq@7659
   266
    attribs[i++] = EGL_DEPTH_SIZE;
gabomdq@7659
   267
    attribs[i++] = _this->gl_config.depth_size;
gabomdq@7659
   268
    
gabomdq@7659
   269
    if (_this->gl_config.stencil_size) {
gabomdq@7659
   270
        attribs[i++] = EGL_STENCIL_SIZE;
gabomdq@7659
   271
        attribs[i++] = _this->gl_config.stencil_size;
gabomdq@7659
   272
    }
gabomdq@7659
   273
    
gabomdq@7659
   274
    if (_this->gl_config.multisamplebuffers) {
gabomdq@7659
   275
        attribs[i++] = EGL_SAMPLE_BUFFERS;
gabomdq@7659
   276
        attribs[i++] = _this->gl_config.multisamplebuffers;
gabomdq@7659
   277
    }
gabomdq@7659
   278
    
gabomdq@7659
   279
    if (_this->gl_config.multisamplesamples) {
gabomdq@7659
   280
        attribs[i++] = EGL_SAMPLES;
gabomdq@7659
   281
        attribs[i++] = _this->gl_config.multisamplesamples;
gabomdq@7659
   282
    }
gabomdq@7659
   283
    
gabomdq@7659
   284
    attribs[i++] = EGL_RENDERABLE_TYPE;
gabomdq@7659
   285
    if (_this->gl_config.major_version == 2) {
gabomdq@7659
   286
        attribs[i++] = EGL_OPENGL_ES2_BIT;
gabomdq@7659
   287
    } else {
gabomdq@7659
   288
        attribs[i++] = EGL_OPENGL_ES_BIT;
gabomdq@7659
   289
    }
gabomdq@7659
   290
    
gabomdq@7659
   291
    attribs[i++] = EGL_NONE;
gabomdq@7659
   292
    
gabomdq@7659
   293
    if (_this->egl_data->eglChooseConfig(_this->egl_data->egl_display,
gabomdq@7659
   294
        attribs,
gabomdq@8015
   295
        configs, SDL_arraysize(configs),
gabomdq@7659
   296
        &found_configs) == EGL_FALSE ||
gabomdq@7659
   297
        found_configs == 0) {
gabomdq@7659
   298
        return SDL_SetError("Couldn't find matching EGL config");
gabomdq@7659
   299
    }
gabomdq@7659
   300
    
gabomdq@8015
   301
    /* eglChooseConfig returns a number of configurations that match or exceed the requested attribs. */
gabomdq@8015
   302
    /* From those, we select the one that matches our requirements more closely via a makeshift algorithm */
gabomdq@8021
   303
gabomdq@8015
   304
    for ( i=0; i<found_configs; i++ ) {
gabomdq@8015
   305
        bitdiff = 0;
gabomdq@8021
   306
        for (j = 0; j < SDL_arraysize(attribs) - 1; j += 2) {
gabomdq@8015
   307
            if (attribs[j] == EGL_NONE) {
gabomdq@8015
   308
               break;
gabomdq@8015
   309
            }
gabomdq@8015
   310
            
gabomdq@8015
   311
            if ( attribs[j+1] != EGL_DONT_CARE && (
gabomdq@8015
   312
                attribs[j] == EGL_RED_SIZE ||
gabomdq@8015
   313
                attribs[j] == EGL_GREEN_SIZE ||
gabomdq@8015
   314
                attribs[j] == EGL_BLUE_SIZE ||
gabomdq@8015
   315
                attribs[j] == EGL_ALPHA_SIZE ||
gabomdq@8015
   316
                attribs[j] == EGL_DEPTH_SIZE ||
gabomdq@8015
   317
                attribs[j] == EGL_STENCIL_SIZE)) {
gabomdq@8015
   318
                _this->egl_data->eglGetConfigAttrib(_this->egl_data->egl_display, configs[i], attribs[j], &value);
gabomdq@8015
   319
                bitdiff += value - attribs[j + 1]; /* value is always >= attrib */
gabomdq@8015
   320
            }
gabomdq@8015
   321
        }
gabomdq@8015
   322
gabomdq@8015
   323
        if (bitdiff < best_bitdiff || best_bitdiff == -1) {
gabomdq@8015
   324
            _this->egl_data->egl_config = configs[i];
gabomdq@8015
   325
            
gabomdq@8015
   326
            best_bitdiff = bitdiff;
gabomdq@8015
   327
        }
gabomdq@8015
   328
           
gabomdq@8015
   329
        if (bitdiff == 0) break; /* we found an exact match! */
gabomdq@8015
   330
    }
gabomdq@8015
   331
    
gabomdq@7659
   332
    return 0;
gabomdq@7659
   333
}
gabomdq@7659
   334
gabomdq@7659
   335
SDL_GLContext
gabomdq@7659
   336
SDL_EGL_CreateContext(_THIS, EGLSurface egl_surface)
gabomdq@7659
   337
{
gabomdq@7659
   338
    EGLint context_attrib_list[] = {
gabomdq@7659
   339
        EGL_CONTEXT_CLIENT_VERSION,
gabomdq@7659
   340
        1,
gabomdq@7659
   341
        EGL_NONE
gabomdq@7659
   342
    };
gabomdq@7659
   343
    
gabomdq@7659
   344
    EGLContext egl_context;
gabomdq@7659
   345
    
gabomdq@7659
   346
    if (!_this->egl_data) {
gabomdq@7659
   347
        /* The EGL library wasn't loaded, SDL_GetError() should have info */
gabomdq@7659
   348
        return NULL;
gabomdq@7659
   349
    }
gabomdq@7659
   350
    
gabomdq@7659
   351
    if (_this->gl_config.major_version) {
gabomdq@7659
   352
        context_attrib_list[1] = _this->gl_config.major_version;
gabomdq@7659
   353
    }
gabomdq@7659
   354
gabomdq@7659
   355
    egl_context =
gabomdq@7659
   356
    _this->egl_data->eglCreateContext(_this->egl_data->egl_display,
gabomdq@7659
   357
                                      _this->egl_data->egl_config,
gabomdq@7659
   358
                                      EGL_NO_CONTEXT, context_attrib_list);
gabomdq@7659
   359
    
gabomdq@7659
   360
    if (egl_context == EGL_NO_CONTEXT) {
gabomdq@7659
   361
        SDL_SetError("Could not create EGL context");
gabomdq@7659
   362
        return NULL;
gabomdq@7659
   363
    }
gabomdq@7659
   364
    
gabomdq@7659
   365
    _this->egl_data->egl_swapinterval = 0;
gabomdq@7659
   366
    
gabomdq@7659
   367
    if (SDL_EGL_MakeCurrent(_this, egl_surface, egl_context) < 0) {
gabomdq@7659
   368
        SDL_EGL_DeleteContext(_this, egl_context);
gabomdq@7659
   369
        SDL_SetError("Could not make EGL context current");
gabomdq@7659
   370
        return NULL;
gabomdq@7659
   371
    }
gabomdq@7659
   372
  
gabomdq@7659
   373
    return (SDL_GLContext) egl_context;
gabomdq@7659
   374
}
gabomdq@7659
   375
gabomdq@7659
   376
int
gabomdq@7659
   377
SDL_EGL_MakeCurrent(_THIS, EGLSurface egl_surface, SDL_GLContext context)
gabomdq@7659
   378
{
gabomdq@7679
   379
    EGLContext egl_context = (EGLContext) context;
gabomdq@7679
   380
gabomdq@7659
   381
    if (!_this->egl_data) {
gabomdq@7659
   382
        return SDL_SetError("OpenGL not initialized");
gabomdq@7659
   383
    }
gabomdq@7659
   384
    
gabomdq@7659
   385
    /* The android emulator crashes badly if you try to eglMakeCurrent 
gabomdq@7659
   386
     * with a valid context and invalid surface, so we have to check for both here.
gabomdq@7659
   387
     */
gabomdq@7659
   388
    if (!egl_context || !egl_surface) {
gabomdq@7659
   389
         _this->egl_data->eglMakeCurrent(_this->egl_data->egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
gabomdq@7659
   390
    }
gabomdq@7659
   391
    else {
gabomdq@7659
   392
        if (!_this->egl_data->eglMakeCurrent(_this->egl_data->egl_display,
gabomdq@7659
   393
            egl_surface, egl_surface, egl_context)) {
gabomdq@7659
   394
            return SDL_SetError("Unable to make EGL context current");
gabomdq@7659
   395
        }
gabomdq@7659
   396
    }
gabomdq@7659
   397
      
gabomdq@7659
   398
    return 0;
gabomdq@7659
   399
}
gabomdq@7659
   400
gabomdq@7659
   401
int
gabomdq@7659
   402
SDL_EGL_SetSwapInterval(_THIS, int interval)
gabomdq@7659
   403
{
gabomdq@7679
   404
    EGLBoolean status;
gabomdq@7679
   405
    
gabomdq@7702
   406
    if (!_this->egl_data) {
gabomdq@7702
   407
        return SDL_SetError("EGL not initialized");
gabomdq@7659
   408
    }
gabomdq@7659
   409
    
gabomdq@7659
   410
    status = _this->egl_data->eglSwapInterval(_this->egl_data->egl_display, interval);
gabomdq@7659
   411
    if (status == EGL_TRUE) {
gabomdq@7659
   412
        _this->egl_data->egl_swapinterval = interval;
gabomdq@7659
   413
        return 0;
gabomdq@7659
   414
    }
gabomdq@7659
   415
    
gabomdq@7659
   416
    return SDL_SetError("Unable to set the EGL swap interval");
gabomdq@7659
   417
}
gabomdq@7659
   418
gabomdq@7659
   419
int
gabomdq@7659
   420
SDL_EGL_GetSwapInterval(_THIS)
gabomdq@7659
   421
{
gabomdq@7702
   422
    if (!_this->egl_data) {
gabomdq@7702
   423
        return SDL_SetError("EGL not initialized");
gabomdq@7659
   424
    }
gabomdq@7659
   425
    
gabomdq@7659
   426
    return _this->egl_data->egl_swapinterval;
gabomdq@7659
   427
}
gabomdq@7659
   428
gabomdq@7659
   429
void
gabomdq@7659
   430
SDL_EGL_SwapBuffers(_THIS, EGLSurface egl_surface)
gabomdq@7659
   431
{
gabomdq@7659
   432
    _this->egl_data->eglSwapBuffers(_this->egl_data->egl_display, egl_surface);
gabomdq@7659
   433
}
gabomdq@7659
   434
gabomdq@7659
   435
void
gabomdq@7659
   436
SDL_EGL_DeleteContext(_THIS, SDL_GLContext context)
gabomdq@7659
   437
{
gabomdq@7679
   438
    EGLContext egl_context = (EGLContext) context;
gabomdq@7679
   439
gabomdq@7659
   440
    /* Clean up GLES and EGL */
gabomdq@7659
   441
    if (!_this->egl_data) {
gabomdq@7659
   442
        return;
gabomdq@7659
   443
    }
gabomdq@7659
   444
    
gabomdq@7896
   445
    if (egl_context != NULL && egl_context != EGL_NO_CONTEXT) {
gabomdq@7659
   446
        SDL_EGL_MakeCurrent(_this, NULL, NULL);
gabomdq@7659
   447
        _this->egl_data->eglDestroyContext(_this->egl_data->egl_display, egl_context);
gabomdq@7659
   448
    }
gabomdq@7659
   449
        
gabomdq@7659
   450
}
gabomdq@7659
   451
gabomdq@7659
   452
EGLSurface *
gabomdq@7659
   453
SDL_EGL_CreateSurface(_THIS, NativeWindowType nw) 
gabomdq@7659
   454
{
gabomdq@7659
   455
    return _this->egl_data->eglCreateWindowSurface(
gabomdq@7659
   456
            _this->egl_data->egl_display,
gabomdq@7659
   457
            _this->egl_data->egl_config,
gabomdq@7659
   458
            nw, NULL);
gabomdq@7659
   459
}
gabomdq@7659
   460
gabomdq@7659
   461
void
gabomdq@7659
   462
SDL_EGL_DestroySurface(_THIS, EGLSurface egl_surface) 
gabomdq@7659
   463
{
gabomdq@7659
   464
    if (!_this->egl_data) {
gabomdq@7659
   465
        return;
gabomdq@7659
   466
    }
gabomdq@7659
   467
    
gabomdq@7659
   468
    if (egl_surface != EGL_NO_SURFACE) {
gabomdq@7659
   469
        _this->egl_data->eglDestroySurface(_this->egl_data->egl_display, egl_surface);
gabomdq@7659
   470
    }
gabomdq@7659
   471
}
gabomdq@7659
   472
gabomdq@7659
   473
#endif /* SDL_VIDEO_OPENGL_EGL */
gabomdq@7659
   474
gabomdq@7659
   475
/* vi: set ts=4 sw=4 expandtab: */
slouken@7865
   476