src/video/SDL_egl.c
author David Ludwig <dludwig@pobox.com>
Sat, 22 Mar 2014 20:48:18 -0400
changeset 8663 308e74493106
parent 8583 fb2933ca805f
child 8733 02b7b0448811
permissions -rw-r--r--
WinRT: Got OpenGL ES 2 working with the latest version of ANGLE/WinRT.

SDL/WinRT did have support for OpenGL ES 2 via an older version of ANGLE/WinRT,
however its API changed a few months ago, and SDL/WinRT would crash when trying
to use it. It would also occasionally crash when using the older version.

This changeset should make SDL/WinRT work with the latest version, as
available via MS Open Tech's git repository of it at
https://github.com/msopentech/angle

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