src/video/SDL_egl.c
author Knut Andre Tidemann <knut.tidemann@gmail.com>
Sat, 28 Jun 2014 12:17:29 -0300
changeset 8962 c593cbef6d7d
parent 8872 5a6375d96834
child 8964 d9ac759bb62a
permissions -rw-r--r--
SDL_egl: allow creation of versioned OpenGL contexts with EGL_KHR_create_context

If the EGL extension EGL_KHR_create_context is available, we can use it to
set the core/compatability profile and the minimum OpenGL version.

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