src/video/SDL_egl.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 26 Apr 2014 11:23:55 -0700
changeset 8740 fbe394d76273
parent 8734 cb61954403ca
child 8753 497e72bba21b
permissions -rw-r--r--
Better compiler for Windows XP systems
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 */
gabomdq@8021
    80
#if !defined(SDL_VIDEO_DRIVER_ANDROID) 
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@8105
   192
    if (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@8021
   201
        if (dll_handle == NULL) {
gabomdq@8021
   202
            return SDL_SetError("Could not load EGL library");
gabomdq@8021
   203
        }
gabomdq@7753
   204
    }
slouken@7865
   205
gabomdq@8021
   206
    _this->egl_data->dll_handle = dll_handle;
gabomdq@7659
   207
gabomdq@7659
   208
    /* Load new function pointers */
gabomdq@7659
   209
    LOAD_FUNC(eglGetDisplay);
gabomdq@7659
   210
    LOAD_FUNC(eglInitialize);
gabomdq@7659
   211
    LOAD_FUNC(eglTerminate);
gabomdq@7659
   212
    LOAD_FUNC(eglGetProcAddress);
gabomdq@7659
   213
    LOAD_FUNC(eglChooseConfig);
gabomdq@7659
   214
    LOAD_FUNC(eglGetConfigAttrib);
gabomdq@7659
   215
    LOAD_FUNC(eglCreateContext);
gabomdq@7659
   216
    LOAD_FUNC(eglDestroyContext);
gabomdq@7659
   217
    LOAD_FUNC(eglCreateWindowSurface);
gabomdq@7659
   218
    LOAD_FUNC(eglDestroySurface);
gabomdq@7659
   219
    LOAD_FUNC(eglMakeCurrent);
gabomdq@7659
   220
    LOAD_FUNC(eglSwapBuffers);
gabomdq@7659
   221
    LOAD_FUNC(eglSwapInterval);
gabomdq@7659
   222
    LOAD_FUNC(eglWaitNative);
gabomdq@7659
   223
    LOAD_FUNC(eglWaitGL);
gabomdq@8062
   224
    LOAD_FUNC(eglBindAPI);
gabomdq@7659
   225
    
dludwig@8663
   226
#if !defined(__WINRT__)
gabomdq@7659
   227
    _this->egl_data->egl_display = _this->egl_data->eglGetDisplay(native_display);
gabomdq@7659
   228
    if (!_this->egl_data->egl_display) {
gabomdq@7659
   229
        return SDL_SetError("Could not get EGL display");
gabomdq@7659
   230
    }
gabomdq@7659
   231
    
gabomdq@7659
   232
    if (_this->egl_data->eglInitialize(_this->egl_data->egl_display, NULL, NULL) != EGL_TRUE) {
gabomdq@7659
   233
        return SDL_SetError("Could not initialize EGL");
gabomdq@7659
   234
    }
dludwig@8663
   235
#endif
gabomdq@7659
   236
gabomdq@8021
   237
    _this->egl_data->dll_handle = dll_handle;
gabomdq@7753
   238
    _this->egl_data->egl_dll_handle = egl_dll_handle;
gabomdq@7659
   239
    _this->gl_config.driver_loaded = 1;
gabomdq@7659
   240
gabomdq@7659
   241
    if (path) {
gabomdq@8021
   242
        SDL_strlcpy(_this->gl_config.driver_path, path, sizeof(_this->gl_config.driver_path) - 1);
gabomdq@7659
   243
    } else {
gabomdq@8021
   244
        *_this->gl_config.driver_path = '\0';
gabomdq@7659
   245
    }
gabomdq@7659
   246
    
gabomdq@8041
   247
    return 0;
gabomdq@7659
   248
}
gabomdq@7659
   249
gabomdq@7659
   250
int
gabomdq@7659
   251
SDL_EGL_ChooseConfig(_THIS) 
gabomdq@7659
   252
{
gabomdq@7659
   253
    /* 64 seems nice. */
gabomdq@7659
   254
    EGLint attribs[64];
gabomdq@8021
   255
    EGLint found_configs = 0, value;
gabomdq@8015
   256
    /* 128 seems even nicer here */
gabomdq@8015
   257
    EGLConfig configs[128];
gabomdq@8021
   258
    int i, j, best_bitdiff = -1, bitdiff;
gabomdq@7659
   259
    
gabomdq@7659
   260
    if (!_this->egl_data) {
gabomdq@7659
   261
        /* The EGL library wasn't loaded, SDL_GetError() should have info */
gabomdq@7659
   262
        return -1;
gabomdq@7659
   263
    }
gabomdq@7659
   264
  
gabomdq@7659
   265
    /* Get a valid EGL configuration */
gabomdq@7659
   266
    i = 0;
gabomdq@7659
   267
    attribs[i++] = EGL_RED_SIZE;
gabomdq@7659
   268
    attribs[i++] = _this->gl_config.red_size;
gabomdq@7659
   269
    attribs[i++] = EGL_GREEN_SIZE;
gabomdq@7659
   270
    attribs[i++] = _this->gl_config.green_size;
gabomdq@7659
   271
    attribs[i++] = EGL_BLUE_SIZE;
gabomdq@7659
   272
    attribs[i++] = _this->gl_config.blue_size;
gabomdq@7659
   273
    
gabomdq@7659
   274
    if (_this->gl_config.alpha_size) {
gabomdq@7659
   275
        attribs[i++] = EGL_ALPHA_SIZE;
gabomdq@7659
   276
        attribs[i++] = _this->gl_config.alpha_size;
gabomdq@7659
   277
    }
gabomdq@7659
   278
    
gabomdq@7659
   279
    if (_this->gl_config.buffer_size) {
gabomdq@7659
   280
        attribs[i++] = EGL_BUFFER_SIZE;
gabomdq@7659
   281
        attribs[i++] = _this->gl_config.buffer_size;
gabomdq@7659
   282
    }
gabomdq@7659
   283
    
gabomdq@7659
   284
    attribs[i++] = EGL_DEPTH_SIZE;
gabomdq@7659
   285
    attribs[i++] = _this->gl_config.depth_size;
gabomdq@7659
   286
    
gabomdq@7659
   287
    if (_this->gl_config.stencil_size) {
gabomdq@7659
   288
        attribs[i++] = EGL_STENCIL_SIZE;
gabomdq@7659
   289
        attribs[i++] = _this->gl_config.stencil_size;
gabomdq@7659
   290
    }
gabomdq@7659
   291
    
gabomdq@7659
   292
    if (_this->gl_config.multisamplebuffers) {
gabomdq@7659
   293
        attribs[i++] = EGL_SAMPLE_BUFFERS;
gabomdq@7659
   294
        attribs[i++] = _this->gl_config.multisamplebuffers;
gabomdq@7659
   295
    }
gabomdq@7659
   296
    
gabomdq@7659
   297
    if (_this->gl_config.multisamplesamples) {
gabomdq@7659
   298
        attribs[i++] = EGL_SAMPLES;
gabomdq@7659
   299
        attribs[i++] = _this->gl_config.multisamplesamples;
gabomdq@7659
   300
    }
gabomdq@7659
   301
    
gabomdq@7659
   302
    attribs[i++] = EGL_RENDERABLE_TYPE;
gabomdq@8062
   303
    if(_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES) {
gabomdq@8062
   304
        if (_this->gl_config.major_version == 2) {
gabomdq@8062
   305
            attribs[i++] = EGL_OPENGL_ES2_BIT;
gabomdq@8062
   306
        } else {
gabomdq@8062
   307
            attribs[i++] = EGL_OPENGL_ES_BIT;
gabomdq@8062
   308
        }
gabomdq@8062
   309
        _this->egl_data->eglBindAPI(EGL_OPENGL_ES_API);
gabomdq@8062
   310
    }
gabomdq@8062
   311
    else {
gabomdq@8062
   312
        attribs[i++] = EGL_OPENGL_BIT;
gabomdq@8062
   313
        _this->egl_data->eglBindAPI(EGL_OPENGL_API);
gabomdq@7659
   314
    }
gabomdq@7659
   315
    
gabomdq@7659
   316
    attribs[i++] = EGL_NONE;
gabomdq@8062
   317
   
gabomdq@7659
   318
    if (_this->egl_data->eglChooseConfig(_this->egl_data->egl_display,
gabomdq@7659
   319
        attribs,
gabomdq@8015
   320
        configs, SDL_arraysize(configs),
gabomdq@7659
   321
        &found_configs) == EGL_FALSE ||
gabomdq@7659
   322
        found_configs == 0) {
gabomdq@7659
   323
        return SDL_SetError("Couldn't find matching EGL config");
gabomdq@7659
   324
    }
gabomdq@7659
   325
    
gabomdq@8015
   326
    /* eglChooseConfig returns a number of configurations that match or exceed the requested attribs. */
gabomdq@8015
   327
    /* From those, we select the one that matches our requirements more closely via a makeshift algorithm */
gabomdq@8021
   328
gabomdq@8015
   329
    for ( i=0; i<found_configs; i++ ) {
gabomdq@8015
   330
        bitdiff = 0;
gabomdq@8021
   331
        for (j = 0; j < SDL_arraysize(attribs) - 1; j += 2) {
gabomdq@8015
   332
            if (attribs[j] == EGL_NONE) {
gabomdq@8015
   333
               break;
gabomdq@8015
   334
            }
gabomdq@8015
   335
            
gabomdq@8015
   336
            if ( attribs[j+1] != EGL_DONT_CARE && (
gabomdq@8015
   337
                attribs[j] == EGL_RED_SIZE ||
gabomdq@8015
   338
                attribs[j] == EGL_GREEN_SIZE ||
gabomdq@8015
   339
                attribs[j] == EGL_BLUE_SIZE ||
gabomdq@8015
   340
                attribs[j] == EGL_ALPHA_SIZE ||
gabomdq@8015
   341
                attribs[j] == EGL_DEPTH_SIZE ||
gabomdq@8015
   342
                attribs[j] == EGL_STENCIL_SIZE)) {
gabomdq@8015
   343
                _this->egl_data->eglGetConfigAttrib(_this->egl_data->egl_display, configs[i], attribs[j], &value);
gabomdq@8015
   344
                bitdiff += value - attribs[j + 1]; /* value is always >= attrib */
gabomdq@8015
   345
            }
gabomdq@8015
   346
        }
gabomdq@8015
   347
gabomdq@8015
   348
        if (bitdiff < best_bitdiff || best_bitdiff == -1) {
gabomdq@8015
   349
            _this->egl_data->egl_config = configs[i];
gabomdq@8015
   350
            
gabomdq@8015
   351
            best_bitdiff = bitdiff;
gabomdq@8015
   352
        }
gabomdq@8015
   353
           
gabomdq@8015
   354
        if (bitdiff == 0) break; /* we found an exact match! */
gabomdq@8015
   355
    }
gabomdq@8015
   356
    
gabomdq@7659
   357
    return 0;
gabomdq@7659
   358
}
gabomdq@7659
   359
gabomdq@7659
   360
SDL_GLContext
gabomdq@7659
   361
SDL_EGL_CreateContext(_THIS, EGLSurface egl_surface)
gabomdq@7659
   362
{
gabomdq@7659
   363
    EGLint context_attrib_list[] = {
gabomdq@7659
   364
        EGL_CONTEXT_CLIENT_VERSION,
gabomdq@7659
   365
        1,
gabomdq@7659
   366
        EGL_NONE
gabomdq@7659
   367
    };
gabomdq@7659
   368
    
gabomdq@8061
   369
    EGLContext egl_context, share_context = EGL_NO_CONTEXT;
gabomdq@7659
   370
    
gabomdq@7659
   371
    if (!_this->egl_data) {
gabomdq@7659
   372
        /* The EGL library wasn't loaded, SDL_GetError() should have info */
gabomdq@7659
   373
        return NULL;
gabomdq@7659
   374
    }
gabomdq@7659
   375
    
gabomdq@8061
   376
    if (_this->gl_config.share_with_current_context) {
gabomdq@8061
   377
        share_context = (EGLContext)SDL_GL_GetCurrentContext();
gabomdq@7659
   378
    }
gabomdq@8062
   379
    
gabomdq@8062
   380
    /* Bind the API */
gabomdq@8062
   381
    if(_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES) {
gabomdq@8062
   382
        _this->egl_data->eglBindAPI(EGL_OPENGL_ES_API);
gabomdq@8062
   383
        if (_this->gl_config.major_version) {
gabomdq@8062
   384
            context_attrib_list[1] = _this->gl_config.major_version;
gabomdq@8062
   385
        }
gabomdq@7659
   386
gabomdq@8062
   387
        egl_context = _this->egl_data->eglCreateContext(_this->egl_data->egl_display,
gabomdq@8062
   388
                                          _this->egl_data->egl_config,
gabomdq@8062
   389
                                          share_context, context_attrib_list);
gabomdq@8062
   390
    }
gabomdq@8062
   391
    else {
gabomdq@8062
   392
        _this->egl_data->eglBindAPI(EGL_OPENGL_API);
gabomdq@8062
   393
        egl_context = _this->egl_data->eglCreateContext(_this->egl_data->egl_display,
gabomdq@8062
   394
                                          _this->egl_data->egl_config,
gabomdq@8062
   395
                                          share_context, NULL);
gabomdq@8062
   396
    }
gabomdq@7659
   397
    
gabomdq@7659
   398
    if (egl_context == EGL_NO_CONTEXT) {
gabomdq@7659
   399
        SDL_SetError("Could not create EGL context");
gabomdq@7659
   400
        return NULL;
gabomdq@7659
   401
    }
gabomdq@7659
   402
    
gabomdq@7659
   403
    _this->egl_data->egl_swapinterval = 0;
gabomdq@7659
   404
    
gabomdq@7659
   405
    if (SDL_EGL_MakeCurrent(_this, egl_surface, egl_context) < 0) {
gabomdq@7659
   406
        SDL_EGL_DeleteContext(_this, egl_context);
gabomdq@7659
   407
        SDL_SetError("Could not make EGL context current");
gabomdq@7659
   408
        return NULL;
gabomdq@7659
   409
    }
gabomdq@7659
   410
  
gabomdq@7659
   411
    return (SDL_GLContext) egl_context;
gabomdq@7659
   412
}
gabomdq@7659
   413
gabomdq@7659
   414
int
gabomdq@7659
   415
SDL_EGL_MakeCurrent(_THIS, EGLSurface egl_surface, SDL_GLContext context)
gabomdq@7659
   416
{
gabomdq@7679
   417
    EGLContext egl_context = (EGLContext) context;
gabomdq@7679
   418
gabomdq@7659
   419
    if (!_this->egl_data) {
gabomdq@7659
   420
        return SDL_SetError("OpenGL not initialized");
gabomdq@7659
   421
    }
gabomdq@7659
   422
    
gabomdq@7659
   423
    /* The android emulator crashes badly if you try to eglMakeCurrent 
gabomdq@7659
   424
     * with a valid context and invalid surface, so we have to check for both here.
gabomdq@7659
   425
     */
gabomdq@7659
   426
    if (!egl_context || !egl_surface) {
gabomdq@7659
   427
         _this->egl_data->eglMakeCurrent(_this->egl_data->egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
gabomdq@7659
   428
    }
gabomdq@7659
   429
    else {
gabomdq@7659
   430
        if (!_this->egl_data->eglMakeCurrent(_this->egl_data->egl_display,
gabomdq@7659
   431
            egl_surface, egl_surface, egl_context)) {
gabomdq@7659
   432
            return SDL_SetError("Unable to make EGL context current");
gabomdq@7659
   433
        }
gabomdq@7659
   434
    }
gabomdq@7659
   435
      
gabomdq@7659
   436
    return 0;
gabomdq@7659
   437
}
gabomdq@7659
   438
gabomdq@7659
   439
int
gabomdq@7659
   440
SDL_EGL_SetSwapInterval(_THIS, int interval)
gabomdq@7659
   441
{
gabomdq@7679
   442
    EGLBoolean status;
gabomdq@7679
   443
    
gabomdq@7702
   444
    if (!_this->egl_data) {
gabomdq@7702
   445
        return SDL_SetError("EGL not initialized");
gabomdq@7659
   446
    }
gabomdq@7659
   447
    
gabomdq@7659
   448
    status = _this->egl_data->eglSwapInterval(_this->egl_data->egl_display, interval);
gabomdq@7659
   449
    if (status == EGL_TRUE) {
gabomdq@7659
   450
        _this->egl_data->egl_swapinterval = interval;
gabomdq@7659
   451
        return 0;
gabomdq@7659
   452
    }
gabomdq@7659
   453
    
gabomdq@7659
   454
    return SDL_SetError("Unable to set the EGL swap interval");
gabomdq@7659
   455
}
gabomdq@7659
   456
gabomdq@7659
   457
int
gabomdq@7659
   458
SDL_EGL_GetSwapInterval(_THIS)
gabomdq@7659
   459
{
gabomdq@7702
   460
    if (!_this->egl_data) {
gabomdq@7702
   461
        return SDL_SetError("EGL not initialized");
gabomdq@7659
   462
    }
gabomdq@7659
   463
    
gabomdq@7659
   464
    return _this->egl_data->egl_swapinterval;
gabomdq@7659
   465
}
gabomdq@7659
   466
gabomdq@7659
   467
void
gabomdq@7659
   468
SDL_EGL_SwapBuffers(_THIS, EGLSurface egl_surface)
gabomdq@7659
   469
{
gabomdq@7659
   470
    _this->egl_data->eglSwapBuffers(_this->egl_data->egl_display, egl_surface);
gabomdq@7659
   471
}
gabomdq@7659
   472
gabomdq@7659
   473
void
gabomdq@7659
   474
SDL_EGL_DeleteContext(_THIS, SDL_GLContext context)
gabomdq@7659
   475
{
gabomdq@7679
   476
    EGLContext egl_context = (EGLContext) context;
gabomdq@7679
   477
gabomdq@7659
   478
    /* Clean up GLES and EGL */
gabomdq@7659
   479
    if (!_this->egl_data) {
gabomdq@7659
   480
        return;
gabomdq@7659
   481
    }
gabomdq@7659
   482
    
gabomdq@7896
   483
    if (egl_context != NULL && egl_context != EGL_NO_CONTEXT) {
gabomdq@7659
   484
        SDL_EGL_MakeCurrent(_this, NULL, NULL);
gabomdq@7659
   485
        _this->egl_data->eglDestroyContext(_this->egl_data->egl_display, egl_context);
gabomdq@7659
   486
    }
gabomdq@7659
   487
        
gabomdq@7659
   488
}
gabomdq@7659
   489
gabomdq@7659
   490
EGLSurface *
gabomdq@7659
   491
SDL_EGL_CreateSurface(_THIS, NativeWindowType nw) 
gabomdq@7659
   492
{
gabomdq@8041
   493
    if (SDL_EGL_ChooseConfig(_this) != 0) {
slouken@8045
   494
        return EGL_NO_SURFACE;
gabomdq@8041
   495
    }
gabomdq@8041
   496
    
gabomdq@7659
   497
    return _this->egl_data->eglCreateWindowSurface(
gabomdq@7659
   498
            _this->egl_data->egl_display,
gabomdq@7659
   499
            _this->egl_data->egl_config,
gabomdq@7659
   500
            nw, NULL);
gabomdq@7659
   501
}
gabomdq@7659
   502
gabomdq@7659
   503
void
gabomdq@7659
   504
SDL_EGL_DestroySurface(_THIS, EGLSurface egl_surface) 
gabomdq@7659
   505
{
gabomdq@7659
   506
    if (!_this->egl_data) {
gabomdq@7659
   507
        return;
gabomdq@7659
   508
    }
gabomdq@7659
   509
    
gabomdq@7659
   510
    if (egl_surface != EGL_NO_SURFACE) {
gabomdq@7659
   511
        _this->egl_data->eglDestroySurface(_this->egl_data->egl_display, egl_surface);
gabomdq@7659
   512
    }
gabomdq@7659
   513
}
gabomdq@7659
   514
gabomdq@7659
   515
#endif /* SDL_VIDEO_OPENGL_EGL */
gabomdq@7659
   516
gabomdq@7659
   517
/* vi: set ts=4 sw=4 expandtab: */
slouken@7865
   518