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