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