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