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