src/video/SDL_egl.c
author Dawid Gan <deveee@gmail.com>
Tue, 16 Jan 2018 21:29:32 +0100
changeset 11830 a01af1a724b3
parent 11829 fcbafc9e443c
child 11839 971881e55d61
permissions -rw-r--r--
EGL: Request sRGB framebuffer in correct place.

The EGL_GL_COLORSPACE_KHR is an attribute for eglCreate*Surface.

As written in EGL_KHR_gl_colorspace documentation:

Accepted as an attribute name by eglCreateWindowSurface,
eglCreatePbufferSurface and eglCreatePixmapSurface

EGL_GL_COLORSPACE_KHR 0x309D
(...)
gabomdq@7659
     1
/*
gabomdq@7659
     2
 *  Simple DirectMedia Layer
slouken@11811
     3
 *  Copyright (C) 1997-2018 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"
slouken@11175
    33
#include "SDL_log.h"
gabomdq@8021
    34
#include "SDL_egl_c.h"
gabomdq@8021
    35
#include "SDL_loadso.h"
gabomdq@8021
    36
#include "SDL_hints.h"
gabomdq@7782
    37
slime73@9595
    38
#ifdef EGL_KHR_create_context
slime73@9595
    39
/* EGL_OPENGL_ES3_BIT_KHR was added in version 13 of the extension. */
slime73@9595
    40
#ifndef EGL_OPENGL_ES3_BIT_KHR
slime73@9595
    41
#define EGL_OPENGL_ES3_BIT_KHR 0x00000040
slime73@9595
    42
#endif
slime73@9595
    43
#endif /* EGL_KHR_create_context */
slime73@9595
    44
gabomdq@7753
    45
#if SDL_VIDEO_DRIVER_RPI
gabomdq@7782
    46
/* Raspbian places the OpenGL ES/EGL binaries in a non standard path */
g@11829
    47
static const char g_rpi_opt_path[] = "/opt/vc/lib";
g@11829
    48
#define DEFAULT_EGL ( vc4 ? "libEGL.so.1" : "libbrcmEGL.so" )
g@11829
    49
#define DEFAULT_OGL_ES2 ( vc4 ? "libGLESv2.so.2" : "libbrcmGLESv2.so" )
g@11829
    50
#define ALT_EGL "libEGL.so"
g@11829
    51
#define ALT_OGL_ES2 "libGLESv2.so"
g@11829
    52
#define DEFAULT_OGL_ES_PVR ( vc4 ? "libGLES_CM.so.1" : "libbrcmGLESv2.so" )
g@11829
    53
#define DEFAULT_OGL_ES ( vc4 ? "libGLESv1_CM.so.1" : "libbrcmGLESv2.so" )
gabomdq@7782
    54
slouken@9164
    55
#elif SDL_VIDEO_DRIVER_ANDROID || SDL_VIDEO_DRIVER_VIVANTE
gabomdq@7782
    56
/* Android */
gabomdq@7782
    57
#define DEFAULT_EGL "libEGL.so"
gabomdq@7782
    58
#define DEFAULT_OGL_ES2 "libGLESv2.so"
gabomdq@7782
    59
#define DEFAULT_OGL_ES_PVR "libGLES_CM.so"
gabomdq@7782
    60
#define DEFAULT_OGL_ES "libGLESv1_CM.so"
gabomdq@7782
    61
dludwig@8545
    62
#elif SDL_VIDEO_DRIVER_WINDOWS || SDL_VIDEO_DRIVER_WINRT
gabomdq@8021
    63
/* EGL AND OpenGL ES support via ANGLE */
gabomdq@8021
    64
#define DEFAULT_EGL "libEGL.dll"
gabomdq@8021
    65
#define DEFAULT_OGL_ES2 "libGLESv2.dll"
gabomdq@8021
    66
#define DEFAULT_OGL_ES_PVR "libGLES_CM.dll"
gabomdq@8021
    67
#define DEFAULT_OGL_ES "libGLESv1_CM.dll"
gabomdq@8021
    68
slouken@11723
    69
#elif SDL_VIDEO_DRIVER_COCOA
slouken@11723
    70
/* EGL AND OpenGL ES support via ANGLE */
slouken@11723
    71
#define DEFAULT_EGL "libEGL.dylib"
slouken@11723
    72
#define DEFAULT_OGL_ES2 "libGLESv2.dylib"
slouken@11723
    73
#define DEFAULT_OGL_ES_PVR "libGLES_CM.dylib"   //???
slouken@11723
    74
#define DEFAULT_OGL_ES "libGLESv1_CM.dylib"     //???
slouken@11723
    75
gabomdq@7753
    76
#else
gabomdq@7782
    77
/* Desktop Linux */
gabomdq@8062
    78
#define DEFAULT_OGL "libGL.so.1"
gabomdq@7753
    79
#define DEFAULT_EGL "libEGL.so.1"
gabomdq@7753
    80
#define DEFAULT_OGL_ES2 "libGLESv2.so.2"
gabomdq@7753
    81
#define DEFAULT_OGL_ES_PVR "libGLES_CM.so.1"
gabomdq@7753
    82
#define DEFAULT_OGL_ES "libGLESv1_CM.so.1"
gabomdq@7753
    83
#endif /* SDL_VIDEO_DRIVER_RPI */
gabomdq@7659
    84
slouken@11363
    85
#ifdef SDL_VIDEO_STATIC_ANGLE
slouken@11363
    86
#define LOAD_FUNC(NAME) \
slouken@11363
    87
_this->egl_data->NAME = (void *)NAME;
slouken@11363
    88
#else
gabomdq@7659
    89
#define LOAD_FUNC(NAME) \
slouken@9162
    90
_this->egl_data->NAME = SDL_LoadFunction(_this->egl_data->dll_handle, #NAME); \
gabomdq@7659
    91
if (!_this->egl_data->NAME) \
gabomdq@7659
    92
{ \
gabomdq@7659
    93
    return SDL_SetError("Could not retrieve EGL function " #NAME); \
gabomdq@7659
    94
}
slouken@11363
    95
#endif
dludwig@10740
    96
dludwig@10740
    97
static const char * SDL_EGL_GetErrorName(EGLint eglErrorCode)
dludwig@10740
    98
{
dludwig@10740
    99
#define SDL_EGL_ERROR_TRANSLATE(e) case e: return #e;
dludwig@10740
   100
    switch (eglErrorCode) {
dludwig@10740
   101
        SDL_EGL_ERROR_TRANSLATE(EGL_SUCCESS);
dludwig@10740
   102
        SDL_EGL_ERROR_TRANSLATE(EGL_NOT_INITIALIZED);
dludwig@10740
   103
        SDL_EGL_ERROR_TRANSLATE(EGL_BAD_ACCESS);
dludwig@10740
   104
        SDL_EGL_ERROR_TRANSLATE(EGL_BAD_ALLOC);
dludwig@10740
   105
        SDL_EGL_ERROR_TRANSLATE(EGL_BAD_ATTRIBUTE);
dludwig@10740
   106
        SDL_EGL_ERROR_TRANSLATE(EGL_BAD_CONTEXT);
dludwig@10740
   107
        SDL_EGL_ERROR_TRANSLATE(EGL_BAD_CONFIG);
dludwig@10740
   108
        SDL_EGL_ERROR_TRANSLATE(EGL_BAD_CURRENT_SURFACE);
dludwig@10740
   109
        SDL_EGL_ERROR_TRANSLATE(EGL_BAD_DISPLAY);
dludwig@10740
   110
        SDL_EGL_ERROR_TRANSLATE(EGL_BAD_SURFACE);
dludwig@10740
   111
        SDL_EGL_ERROR_TRANSLATE(EGL_BAD_MATCH);
dludwig@10740
   112
        SDL_EGL_ERROR_TRANSLATE(EGL_BAD_PARAMETER);
dludwig@10740
   113
        SDL_EGL_ERROR_TRANSLATE(EGL_BAD_NATIVE_PIXMAP);
dludwig@10740
   114
        SDL_EGL_ERROR_TRANSLATE(EGL_BAD_NATIVE_WINDOW);
dludwig@10740
   115
        SDL_EGL_ERROR_TRANSLATE(EGL_CONTEXT_LOST);
dludwig@10740
   116
    }
dludwig@10740
   117
    return "";
dludwig@10740
   118
}
dludwig@10740
   119
dludwig@10740
   120
int SDL_EGL_SetErrorEx(const char * message, const char * eglFunctionName, EGLint eglErrorCode)
dludwig@10740
   121
{
dludwig@10740
   122
    const char * errorText = SDL_EGL_GetErrorName(eglErrorCode);
dludwig@10740
   123
    char altErrorText[32];
dludwig@10740
   124
    if (errorText[0] == '\0') {
dludwig@10740
   125
        /* An unknown-to-SDL error code was reported.  Report its hexadecimal value, instead of its name. */
dludwig@10740
   126
        SDL_snprintf(altErrorText, SDL_arraysize(altErrorText), "0x%x", (unsigned int)eglErrorCode);
dludwig@10740
   127
        errorText = altErrorText;
dludwig@10740
   128
    }
dludwig@10740
   129
    return SDL_SetError("%s (call to %s failed, reporting an error of %s)", message, eglFunctionName, errorText);
dludwig@10740
   130
}
dludwig@10740
   131
gabomdq@7659
   132
/* EGL implementation of SDL OpenGL ES support */
slouken@11175
   133
typedef enum {
slouken@11175
   134
    SDL_EGL_DISPLAY_EXTENSION,
slouken@11175
   135
    SDL_EGL_CLIENT_EXTENSION
slouken@11175
   136
} SDL_EGL_ExtensionType;
slouken@11175
   137
slouken@11175
   138
static SDL_bool SDL_EGL_HasExtension(_THIS, SDL_EGL_ExtensionType type, const char *ext)
knut@8962
   139
{
slouken@9821
   140
    size_t ext_len;
slouken@11175
   141
    const char *ext_override;
slouken@11175
   142
    const char *egl_extstr;
slouken@11175
   143
    const char *ext_start;
slouken@11175
   144
slouken@11175
   145
    /* Invalid extensions can be rejected early */
slouken@11175
   146
    if (ext == NULL || *ext == 0 || SDL_strchr(ext, ' ') != NULL) {
slouken@11175
   147
        /* SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "SDL_EGL_HasExtension: Invalid EGL extension"); */
slouken@11175
   148
        return SDL_FALSE;
slouken@11175
   149
    }
slouken@11175
   150
slouken@11175
   151
    /* Extensions can be masked with an environment variable.
slouken@11175
   152
     * Unlike the OpenGL override, this will use the set bits of an integer
slouken@11175
   153
     * to disable the extension.
slouken@11175
   154
     *  Bit   Action
slouken@11175
   155
     *  0     If set, the display extension is masked and not present to SDL.
slouken@11175
   156
     *  1     If set, the client extension is masked and not present to SDL.
slouken@11175
   157
     */
slouken@11175
   158
    ext_override = SDL_getenv(ext);
slouken@11175
   159
    if (ext_override != NULL) {
slouken@11175
   160
        int disable_ext = SDL_atoi(ext_override);
slouken@11175
   161
        if (disable_ext & 0x01 && type == SDL_EGL_DISPLAY_EXTENSION) {
slouken@11175
   162
            return SDL_FALSE;
slouken@11175
   163
        } else if (disable_ext & 0x02 && type == SDL_EGL_CLIENT_EXTENSION) {
slouken@11175
   164
            return SDL_FALSE;
slouken@11175
   165
        }
slouken@11175
   166
    }
knut@8962
   167
knut@8962
   168
    ext_len = SDL_strlen(ext);
slouken@11175
   169
    switch (type) {
slouken@11175
   170
    case SDL_EGL_DISPLAY_EXTENSION:
slouken@11175
   171
        egl_extstr = _this->egl_data->eglQueryString(_this->egl_data->egl_display, EGL_EXTENSIONS);
slouken@11175
   172
        break;
slouken@11175
   173
    case SDL_EGL_CLIENT_EXTENSION:
slouken@11175
   174
        /* EGL_EXT_client_extensions modifies eglQueryString to return client extensions
slouken@11175
   175
         * if EGL_NO_DISPLAY is passed. Implementations without it are required to return NULL.
slouken@11175
   176
         * This behavior is included in EGL 1.5.
slouken@11175
   177
         */
slouken@11175
   178
        egl_extstr = _this->egl_data->eglQueryString(EGL_NO_DISPLAY, EGL_EXTENSIONS);
slouken@11175
   179
        break;
slouken@11175
   180
    default:
slouken@11175
   181
        /* SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "SDL_EGL_HasExtension: Invalid extension type"); */
slouken@11175
   182
        return SDL_FALSE;
slouken@11175
   183
    }
knut@8962
   184
slouken@11175
   185
    if (egl_extstr != NULL) {
slouken@11175
   186
        ext_start = egl_extstr;
knut@8962
   187
slouken@11175
   188
        while (*ext_start) {
slouken@11175
   189
            ext_start = SDL_strstr(ext_start, ext);
slouken@11175
   190
            if (ext_start == NULL) {
slouken@11175
   191
                return SDL_FALSE;
slouken@11175
   192
            }
slouken@11175
   193
            /* Check if the match is not just a substring of one of the extensions */
slouken@11175
   194
            if (ext_start == egl_extstr || *(ext_start - 1) == ' ') {
slouken@11175
   195
                if (ext_start[ext_len] == ' ' || ext_start[ext_len] == 0) {
slouken@11175
   196
                    return SDL_TRUE;
knut@8962
   197
                }
slouken@11175
   198
            }
slouken@11175
   199
            /* If the search stopped in the middle of an extension, skip to the end of it */
slouken@11175
   200
            ext_start += ext_len;
slouken@11175
   201
            while (*ext_start != ' ' && *ext_start != 0) {
slouken@11175
   202
                ext_start++;
knut@8962
   203
            }
knut@8962
   204
        }
knut@8962
   205
    }
knut@8962
   206
slouken@11175
   207
    return SDL_FALSE;
knut@8962
   208
}
gabomdq@7659
   209
gabomdq@7659
   210
void *
gabomdq@7659
   211
SDL_EGL_GetProcAddress(_THIS, const char *proc)
gabomdq@7659
   212
{
gabomdq@7659
   213
    static char procname[1024];
gabomdq@7659
   214
    void *retval;
gabomdq@7659
   215
    
gabomdq@7659
   216
    /* eglGetProcAddress is busted on Android http://code.google.com/p/android/issues/detail?id=7681 */
brandon@11516
   217
#if !defined(SDL_VIDEO_DRIVER_ANDROID)
gabomdq@7659
   218
    if (_this->egl_data->eglGetProcAddress) {
gabomdq@7659
   219
        retval = _this->egl_data->eglGetProcAddress(proc);
gabomdq@7659
   220
        if (retval) {
gabomdq@7659
   221
            return retval;
gabomdq@7659
   222
        }
gabomdq@7659
   223
    }
gabomdq@7659
   224
#endif
gabomdq@7659
   225
    
gabomdq@8021
   226
    retval = SDL_LoadFunction(_this->egl_data->egl_dll_handle, proc);
gabomdq@8021
   227
    if (!retval && SDL_strlen(proc) <= 1022) {
gabomdq@7659
   228
        procname[0] = '_';
gabomdq@8021
   229
        SDL_strlcpy(procname + 1, proc, 1022);
gabomdq@8021
   230
        retval = SDL_LoadFunction(_this->egl_data->egl_dll_handle, procname);
gabomdq@7659
   231
    }
gabomdq@7659
   232
    return retval;
gabomdq@7659
   233
}
gabomdq@7659
   234
gabomdq@7659
   235
void
gabomdq@7659
   236
SDL_EGL_UnloadLibrary(_THIS)
gabomdq@7659
   237
{
slouken@7865
   238
    if (_this->egl_data) {
slouken@7865
   239
        if (_this->egl_data->egl_display) {
slouken@7865
   240
            _this->egl_data->eglTerminate(_this->egl_data->egl_display);
slouken@7865
   241
            _this->egl_data->egl_display = NULL;
slouken@7865
   242
        }
slouken@7865
   243
gabomdq@8021
   244
        if (_this->egl_data->dll_handle) {
gabomdq@8021
   245
            SDL_UnloadObject(_this->egl_data->dll_handle);
gabomdq@8021
   246
            _this->egl_data->dll_handle = NULL;
slouken@7865
   247
        }
slouken@7865
   248
        if (_this->egl_data->egl_dll_handle) {
gabomdq@8021
   249
            SDL_UnloadObject(_this->egl_data->egl_dll_handle);
slouken@7865
   250
            _this->egl_data->egl_dll_handle = NULL;
slouken@7865
   251
        }
gabomdq@7659
   252
        
gabomdq@7659
   253
        SDL_free(_this->egl_data);
gabomdq@7659
   254
        _this->egl_data = NULL;
gabomdq@7659
   255
    }
gabomdq@7659
   256
}
gabomdq@7659
   257
gabomdq@7659
   258
int
slouken@11175
   259
SDL_EGL_LoadLibrary(_THIS, const char *egl_path, NativeDisplayType native_display, EGLenum platform)
gabomdq@7659
   260
{
gabomdq@8021
   261
    void *dll_handle = NULL, *egl_dll_handle = NULL; /* The naming is counter intuitive, but hey, I just work here -- Gabriel */
slouken@10461
   262
    const char *path = NULL;
slouken@11175
   263
    int egl_version_major = 0, egl_version_minor = 0;
dludwig@8545
   264
#if SDL_VIDEO_DRIVER_WINDOWS || SDL_VIDEO_DRIVER_WINRT
gabomdq@8021
   265
    const char *d3dcompiler;
gabomdq@8021
   266
#endif
connogriofa@11676
   267
#if SDL_VIDEO_DRIVER_RPI
connogriofa@11676
   268
    SDL_bool vc4 = (0 == access("/sys/module/vc4/", F_OK));
g@11829
   269
g@11829
   270
    path = SDL_getenv("LD_LIBRARY_PATH");
g@11829
   271
    if (path) {
g@11829
   272
        const int path_size = SDL_strlen(path) + 1 + sizeof(g_rpi_opt_path);
g@11829
   273
        char *new_path = SDL_calloc(1, path_size);
g@11829
   274
        strcat(new_path, path);
g@11829
   275
        strcat(new_path, ":");
g@11829
   276
        strcat(new_path, g_rpi_opt_path);
g@11829
   277
        SDL_setenv("LD_LIBRARY_PATH", new_path, 1);
g@11829
   278
        SDL_free(new_path);
g@11829
   279
    } else {
g@11829
   280
        SDL_setenv("LD_LIBRARY_PATH", g_rpi_opt_path, 1);
g@11829
   281
    }
g@11829
   282
    path = NULL;
connogriofa@11676
   283
#endif
gabomdq@8021
   284
gabomdq@7659
   285
    if (_this->egl_data) {
slouken@11718
   286
        return SDL_SetError("EGL context already created");
gabomdq@7659
   287
    }
slouken@7865
   288
slouken@7865
   289
    _this->egl_data = (struct SDL_EGL_VideoData *) SDL_calloc(1, sizeof(SDL_EGL_VideoData));
slouken@7865
   290
    if (!_this->egl_data) {
slouken@7865
   291
        return SDL_OutOfMemory();
slouken@7865
   292
    }
slouken@7865
   293
dludwig@8545
   294
#if SDL_VIDEO_DRIVER_WINDOWS || SDL_VIDEO_DRIVER_WINRT
gabomdq@8021
   295
    d3dcompiler = SDL_GetHint(SDL_HINT_VIDEO_WIN_D3DCOMPILER);
gabomdq@8021
   296
    if (!d3dcompiler) {
slouken@8733
   297
        if (WIN_IsWindowsVistaOrGreater()) {
slouken@8733
   298
            d3dcompiler = "d3dcompiler_46.dll";
slouken@8733
   299
        } else {
slouken@8740
   300
            d3dcompiler = "d3dcompiler_43.dll";
slouken@8733
   301
        }
gabomdq@8021
   302
    }
gabomdq@8021
   303
    if (SDL_strcasecmp(d3dcompiler, "none") != 0) {
slouken@11219
   304
        if (SDL_LoadObject(d3dcompiler) == NULL) {
slouken@11219
   305
            SDL_ClearError();
slouken@11219
   306
        }
gabomdq@8021
   307
    }
slouken@7865
   308
#endif
slouken@7865
   309
slouken@11363
   310
#ifndef SDL_VIDEO_STATIC_ANGLE
gabomdq@7753
   311
    /* A funny thing, loading EGL.so first does not work on the Raspberry, so we load libGL* first */
gabomdq@8021
   312
    path = SDL_getenv("SDL_VIDEO_GL_DRIVER");
gabomdq@8021
   313
    if (path != NULL) {
gabomdq@8021
   314
        egl_dll_handle = SDL_LoadObject(path);
gabomdq@8021
   315
    }
gabomdq@8021
   316
gabomdq@8021
   317
    if (egl_dll_handle == NULL) {
slime73@9595
   318
        if (_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES) {
gabomdq@8062
   319
            if (_this->gl_config.major_version > 1) {
gabomdq@8062
   320
                path = DEFAULT_OGL_ES2;
gabomdq@8062
   321
                egl_dll_handle = SDL_LoadObject(path);
icculus@11462
   322
#ifdef ALT_OGL_ES2
connogriofa@11676
   323
                if (egl_dll_handle == NULL && !vc4) {
icculus@11462
   324
                    path = ALT_OGL_ES2;
icculus@11462
   325
                    egl_dll_handle = SDL_LoadObject(path);
icculus@11462
   326
                }
icculus@11462
   327
#endif
icculus@11462
   328
slime73@9595
   329
            } else {
gabomdq@8062
   330
                path = DEFAULT_OGL_ES;
gabomdq@8062
   331
                egl_dll_handle = SDL_LoadObject(path);
gabomdq@8062
   332
                if (egl_dll_handle == NULL) {
gabomdq@8062
   333
                    path = DEFAULT_OGL_ES_PVR;
gabomdq@8062
   334
                    egl_dll_handle = SDL_LoadObject(path);
gabomdq@8062
   335
                }
connogriofa@11676
   336
#ifdef ALT_OGL_ES2
connogriofa@11676
   337
                if (egl_dll_handle == NULL && !vc4) {
connogriofa@11676
   338
                    path = ALT_OGL_ES2;
connogriofa@11676
   339
                    egl_dll_handle = SDL_LoadObject(path);
connogriofa@11676
   340
                }
connogriofa@11676
   341
#endif
gabomdq@8062
   342
            }
gabomdq@8062
   343
        }
gabomdq@8062
   344
#ifdef DEFAULT_OGL         
gabomdq@8062
   345
        else {
gabomdq@8062
   346
            path = DEFAULT_OGL;
gabomdq@8021
   347
            egl_dll_handle = SDL_LoadObject(path);
gabomdq@8021
   348
        }
gabomdq@8062
   349
#endif        
gabomdq@7753
   350
    }
slouken@7865
   351
    _this->egl_data->egl_dll_handle = egl_dll_handle;
gabomdq@7753
   352
gabomdq@7753
   353
    if (egl_dll_handle == NULL) {
gabomdq@8062
   354
        return SDL_SetError("Could not initialize OpenGL / GLES library");
gabomdq@7753
   355
    }
gabomdq@8021
   356
gabomdq@7753
   357
    /* Loading libGL* in the previous step took care of loading libEGL.so, but we future proof by double checking */
gabomdq@8021
   358
    if (egl_path != NULL) {
gabomdq@8021
   359
        dll_handle = SDL_LoadObject(egl_path);
gabomdq@8021
   360
    }   
gabomdq@8105
   361
    /* Try loading a EGL symbol, if it does not work try the default library paths */
gabomdq@8872
   362
    if (dll_handle == NULL || SDL_LoadFunction(dll_handle, "eglChooseConfig") == NULL) {
gabomdq@8021
   363
        if (dll_handle != NULL) {
gabomdq@8021
   364
            SDL_UnloadObject(dll_handle);
gabomdq@8021
   365
        }
gabomdq@8021
   366
        path = SDL_getenv("SDL_VIDEO_EGL_DRIVER");
gabomdq@7659
   367
        if (path == NULL) {
gabomdq@7659
   368
            path = DEFAULT_EGL;
gabomdq@7659
   369
        }
gabomdq@8021
   370
        dll_handle = SDL_LoadObject(path);
icculus@11462
   371
icculus@11462
   372
#ifdef ALT_EGL
connogriofa@11676
   373
        if (dll_handle == NULL && !vc4) {
icculus@11462
   374
            path = ALT_EGL;
icculus@11462
   375
            dll_handle = SDL_LoadObject(path);
icculus@11462
   376
        }
icculus@11462
   377
#endif
icculus@11462
   378
gabomdq@8872
   379
        if (dll_handle == NULL || SDL_LoadFunction(dll_handle, "eglChooseConfig") == NULL) {
gabomdq@8872
   380
            if (dll_handle != NULL) {
gabomdq@8872
   381
                SDL_UnloadObject(dll_handle);
gabomdq@8872
   382
            }
gabomdq@8021
   383
            return SDL_SetError("Could not load EGL library");
gabomdq@8021
   384
        }
gabomdq@8763
   385
        SDL_ClearError();
gabomdq@7753
   386
    }
slouken@11363
   387
#endif
slouken@7865
   388
gabomdq@8021
   389
    _this->egl_data->dll_handle = dll_handle;
gabomdq@7659
   390
gabomdq@7659
   391
    /* Load new function pointers */
gabomdq@7659
   392
    LOAD_FUNC(eglGetDisplay);
gabomdq@7659
   393
    LOAD_FUNC(eglInitialize);
gabomdq@7659
   394
    LOAD_FUNC(eglTerminate);
gabomdq@7659
   395
    LOAD_FUNC(eglGetProcAddress);
gabomdq@7659
   396
    LOAD_FUNC(eglChooseConfig);
gabomdq@7659
   397
    LOAD_FUNC(eglGetConfigAttrib);
gabomdq@7659
   398
    LOAD_FUNC(eglCreateContext);
gabomdq@7659
   399
    LOAD_FUNC(eglDestroyContext);
slouken@11719
   400
    LOAD_FUNC(eglCreatePbufferSurface);
gabomdq@7659
   401
    LOAD_FUNC(eglCreateWindowSurface);
gabomdq@7659
   402
    LOAD_FUNC(eglDestroySurface);
gabomdq@7659
   403
    LOAD_FUNC(eglMakeCurrent);
gabomdq@7659
   404
    LOAD_FUNC(eglSwapBuffers);
gabomdq@7659
   405
    LOAD_FUNC(eglSwapInterval);
gabomdq@7659
   406
    LOAD_FUNC(eglWaitNative);
gabomdq@7659
   407
    LOAD_FUNC(eglWaitGL);
gabomdq@8062
   408
    LOAD_FUNC(eglBindAPI);
knut@8962
   409
    LOAD_FUNC(eglQueryString);
dludwig@10740
   410
    LOAD_FUNC(eglGetError);
dludwig@10740
   411
slouken@11175
   412
    if (_this->egl_data->eglQueryString) {
slouken@11175
   413
        /* EGL 1.5 allows querying for client version */
slouken@11175
   414
        const char *egl_version = _this->egl_data->eglQueryString(EGL_NO_DISPLAY, EGL_VERSION);
slouken@11175
   415
        if (egl_version != NULL) {
slouken@11175
   416
            if (SDL_sscanf(egl_version, "%d.%d", &egl_version_major, &egl_version_minor) != 2) {
slouken@11175
   417
                egl_version_major = 0;
slouken@11175
   418
                egl_version_minor = 0;
slouken@11175
   419
                SDL_LogWarn(SDL_LOG_CATEGORY_VIDEO, "Could not parse EGL version string: %s", egl_version);
slouken@11175
   420
            }
slouken@11175
   421
        }
slouken@11175
   422
    }
slouken@11175
   423
slouken@11175
   424
    if (egl_version_major == 1 && egl_version_minor == 5) {
slouken@11175
   425
        LOAD_FUNC(eglGetPlatformDisplay);
slouken@11175
   426
    }
slouken@11175
   427
slouken@11175
   428
    _this->egl_data->egl_display = EGL_NO_DISPLAY;
dludwig@8663
   429
#if !defined(__WINRT__)
slouken@11175
   430
    if (platform) {
slouken@11175
   431
        if (egl_version_major == 1 && egl_version_minor == 5) {
icculus@11418
   432
            _this->egl_data->egl_display = _this->egl_data->eglGetPlatformDisplay(platform, (void *)(size_t)native_display, NULL);
slouken@11175
   433
        } else {
slouken@11175
   434
            if (SDL_EGL_HasExtension(_this, SDL_EGL_CLIENT_EXTENSION, "EGL_EXT_platform_base")) {
slouken@11175
   435
                _this->egl_data->eglGetPlatformDisplayEXT = SDL_EGL_GetProcAddress(_this, "eglGetPlatformDisplayEXT");
slouken@11175
   436
                if (_this->egl_data->eglGetPlatformDisplayEXT) {
icculus@11418
   437
                    _this->egl_data->egl_display = _this->egl_data->eglGetPlatformDisplayEXT(platform, (void *)(size_t)native_display, NULL);
slouken@11175
   438
                }
slouken@11175
   439
            }
slouken@11175
   440
        }
slouken@11175
   441
    }
slouken@11175
   442
    /* Try the implementation-specific eglGetDisplay even if eglGetPlatformDisplay fails */
slouken@11175
   443
    if (_this->egl_data->egl_display == EGL_NO_DISPLAY) {
slouken@11175
   444
        _this->egl_data->egl_display = _this->egl_data->eglGetDisplay(native_display);
slouken@11175
   445
    }
slouken@11175
   446
    if (_this->egl_data->egl_display == EGL_NO_DISPLAY) {
gabomdq@7659
   447
        return SDL_SetError("Could not get EGL display");
gabomdq@7659
   448
    }
gabomdq@7659
   449
    
gabomdq@7659
   450
    if (_this->egl_data->eglInitialize(_this->egl_data->egl_display, NULL, NULL) != EGL_TRUE) {
gabomdq@7659
   451
        return SDL_SetError("Could not initialize EGL");
gabomdq@7659
   452
    }
dludwig@8663
   453
#endif
gabomdq@7659
   454
gabomdq@7659
   455
    if (path) {
gabomdq@8021
   456
        SDL_strlcpy(_this->gl_config.driver_path, path, sizeof(_this->gl_config.driver_path) - 1);
gabomdq@7659
   457
    } else {
gabomdq@8021
   458
        *_this->gl_config.driver_path = '\0';
gabomdq@7659
   459
    }
gabomdq@7659
   460
    
gabomdq@8041
   461
    return 0;
gabomdq@7659
   462
}
gabomdq@7659
   463
gabomdq@7659
   464
int
gabomdq@7659
   465
SDL_EGL_ChooseConfig(_THIS) 
gabomdq@7659
   466
{
slouken@11175
   467
/* 64 seems nice. */
gabomdq@7659
   468
    EGLint attribs[64];
gabomdq@8021
   469
    EGLint found_configs = 0, value;
slouken@11175
   470
#ifdef SDL_VIDEO_DRIVER_KMSDRM
slouken@11175
   471
    /* Intel EGL on KMS/DRM (al least) returns invalid configs that confuse the bitdiff search used */
slouken@11175
   472
    /* later in this function, so we simply use the first one when using the KMSDRM driver for now. */
slouken@11175
   473
    EGLConfig configs[1];
slouken@11175
   474
#else
gabomdq@8015
   475
    /* 128 seems even nicer here */
gabomdq@8015
   476
    EGLConfig configs[128];
slouken@11175
   477
#endif
gabomdq@8021
   478
    int i, j, best_bitdiff = -1, bitdiff;
slouken@11175
   479
   
gabomdq@7659
   480
    if (!_this->egl_data) {
gabomdq@7659
   481
        /* The EGL library wasn't loaded, SDL_GetError() should have info */
gabomdq@7659
   482
        return -1;
gabomdq@7659
   483
    }
gabomdq@7659
   484
  
gabomdq@7659
   485
    /* Get a valid EGL configuration */
gabomdq@7659
   486
    i = 0;
gabomdq@7659
   487
    attribs[i++] = EGL_RED_SIZE;
gabomdq@7659
   488
    attribs[i++] = _this->gl_config.red_size;
gabomdq@7659
   489
    attribs[i++] = EGL_GREEN_SIZE;
gabomdq@7659
   490
    attribs[i++] = _this->gl_config.green_size;
gabomdq@7659
   491
    attribs[i++] = EGL_BLUE_SIZE;
gabomdq@7659
   492
    attribs[i++] = _this->gl_config.blue_size;
gabomdq@7659
   493
    
gabomdq@7659
   494
    if (_this->gl_config.alpha_size) {
gabomdq@7659
   495
        attribs[i++] = EGL_ALPHA_SIZE;
gabomdq@7659
   496
        attribs[i++] = _this->gl_config.alpha_size;
gabomdq@7659
   497
    }
gabomdq@7659
   498
    
gabomdq@7659
   499
    if (_this->gl_config.buffer_size) {
gabomdq@7659
   500
        attribs[i++] = EGL_BUFFER_SIZE;
gabomdq@7659
   501
        attribs[i++] = _this->gl_config.buffer_size;
gabomdq@7659
   502
    }
gabomdq@7659
   503
    
gabomdq@7659
   504
    attribs[i++] = EGL_DEPTH_SIZE;
gabomdq@7659
   505
    attribs[i++] = _this->gl_config.depth_size;
gabomdq@7659
   506
    
gabomdq@7659
   507
    if (_this->gl_config.stencil_size) {
gabomdq@7659
   508
        attribs[i++] = EGL_STENCIL_SIZE;
gabomdq@7659
   509
        attribs[i++] = _this->gl_config.stencil_size;
gabomdq@7659
   510
    }
gabomdq@7659
   511
    
gabomdq@7659
   512
    if (_this->gl_config.multisamplebuffers) {
gabomdq@7659
   513
        attribs[i++] = EGL_SAMPLE_BUFFERS;
gabomdq@7659
   514
        attribs[i++] = _this->gl_config.multisamplebuffers;
gabomdq@7659
   515
    }
gabomdq@7659
   516
    
gabomdq@7659
   517
    if (_this->gl_config.multisamplesamples) {
gabomdq@7659
   518
        attribs[i++] = EGL_SAMPLES;
gabomdq@7659
   519
        attribs[i++] = _this->gl_config.multisamplesamples;
gabomdq@7659
   520
    }
slime73@9595
   521
gabomdq@7659
   522
    attribs[i++] = EGL_RENDERABLE_TYPE;
slime73@9595
   523
    if (_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES) {
slime73@9595
   524
#ifdef EGL_KHR_create_context
slime73@9595
   525
        if (_this->gl_config.major_version >= 3 &&
slouken@11175
   526
            SDL_EGL_HasExtension(_this, SDL_EGL_DISPLAY_EXTENSION, "EGL_KHR_create_context")) {
slime73@9595
   527
            attribs[i++] = EGL_OPENGL_ES3_BIT_KHR;
slime73@9595
   528
        } else
slime73@9595
   529
#endif
slime73@9595
   530
        if (_this->gl_config.major_version >= 2) {
gabomdq@8062
   531
            attribs[i++] = EGL_OPENGL_ES2_BIT;
gabomdq@8062
   532
        } else {
gabomdq@8062
   533
            attribs[i++] = EGL_OPENGL_ES_BIT;
gabomdq@8062
   534
        }
gabomdq@8062
   535
        _this->egl_data->eglBindAPI(EGL_OPENGL_ES_API);
slime73@9595
   536
    } else {
gabomdq@8062
   537
        attribs[i++] = EGL_OPENGL_BIT;
gabomdq@8062
   538
        _this->egl_data->eglBindAPI(EGL_OPENGL_API);
gabomdq@7659
   539
    }
slime73@9827
   540
slouken@11677
   541
    if (_this->egl_data->egl_surfacetype) {
slouken@11677
   542
        attribs[i++] = EGL_SURFACE_TYPE;
slouken@11677
   543
        attribs[i++] = _this->egl_data->egl_surfacetype;
slouken@11677
   544
    }
slouken@11677
   545
gabomdq@7659
   546
    attribs[i++] = EGL_NONE;
slime73@9827
   547
gabomdq@7659
   548
    if (_this->egl_data->eglChooseConfig(_this->egl_data->egl_display,
gabomdq@7659
   549
        attribs,
gabomdq@8015
   550
        configs, SDL_arraysize(configs),
gabomdq@7659
   551
        &found_configs) == EGL_FALSE ||
gabomdq@7659
   552
        found_configs == 0) {
dludwig@10740
   553
        return SDL_EGL_SetError("Couldn't find matching EGL config", "eglChooseConfig");
gabomdq@7659
   554
    }
slime73@9827
   555
gabomdq@8015
   556
    /* eglChooseConfig returns a number of configurations that match or exceed the requested attribs. */
gabomdq@8015
   557
    /* From those, we select the one that matches our requirements more closely via a makeshift algorithm */
gabomdq@8021
   558
slime73@9595
   559
    for (i = 0; i < found_configs; i++ ) {
gabomdq@8015
   560
        bitdiff = 0;
gabomdq@8021
   561
        for (j = 0; j < SDL_arraysize(attribs) - 1; j += 2) {
gabomdq@8015
   562
            if (attribs[j] == EGL_NONE) {
gabomdq@8015
   563
               break;
gabomdq@8015
   564
            }
gabomdq@8015
   565
            
gabomdq@8015
   566
            if ( attribs[j+1] != EGL_DONT_CARE && (
gabomdq@8015
   567
                attribs[j] == EGL_RED_SIZE ||
gabomdq@8015
   568
                attribs[j] == EGL_GREEN_SIZE ||
gabomdq@8015
   569
                attribs[j] == EGL_BLUE_SIZE ||
gabomdq@8015
   570
                attribs[j] == EGL_ALPHA_SIZE ||
gabomdq@8015
   571
                attribs[j] == EGL_DEPTH_SIZE ||
gabomdq@8015
   572
                attribs[j] == EGL_STENCIL_SIZE)) {
gabomdq@8015
   573
                _this->egl_data->eglGetConfigAttrib(_this->egl_data->egl_display, configs[i], attribs[j], &value);
gabomdq@8015
   574
                bitdiff += value - attribs[j + 1]; /* value is always >= attrib */
gabomdq@8015
   575
            }
gabomdq@8015
   576
        }
gabomdq@8015
   577
gabomdq@8015
   578
        if (bitdiff < best_bitdiff || best_bitdiff == -1) {
gabomdq@8015
   579
            _this->egl_data->egl_config = configs[i];
gabomdq@8015
   580
            
gabomdq@8015
   581
            best_bitdiff = bitdiff;
gabomdq@8015
   582
        }
slime73@9595
   583
slime73@9595
   584
        if (bitdiff == 0) {
slime73@9595
   585
            break; /* we found an exact match! */
slime73@9595
   586
        }
gabomdq@8015
   587
    }
gabomdq@8015
   588
    
gabomdq@7659
   589
    return 0;
gabomdq@7659
   590
}
gabomdq@7659
   591
gabomdq@7659
   592
SDL_GLContext
gabomdq@7659
   593
SDL_EGL_CreateContext(_THIS, EGLSurface egl_surface)
gabomdq@7659
   594
{
slime73@9595
   595
    /* max 14 values plus terminator. */
slime73@9595
   596
    EGLint attribs[15];
slime73@9595
   597
    int attr = 0;
slime73@9595
   598
gabomdq@8061
   599
    EGLContext egl_context, share_context = EGL_NO_CONTEXT;
slime73@9595
   600
    EGLint profile_mask = _this->gl_config.profile_mask;
slime73@9637
   601
    EGLint major_version = _this->gl_config.major_version;
slime73@9637
   602
    EGLint minor_version = _this->gl_config.minor_version;
slime73@9637
   603
    SDL_bool profile_es = (profile_mask == SDL_GL_CONTEXT_PROFILE_ES);
slime73@9595
   604
gabomdq@7659
   605
    if (!_this->egl_data) {
gabomdq@7659
   606
        /* The EGL library wasn't loaded, SDL_GetError() should have info */
gabomdq@7659
   607
        return NULL;
gabomdq@7659
   608
    }
slime73@9595
   609
gabomdq@8061
   610
    if (_this->gl_config.share_with_current_context) {
gabomdq@8061
   611
        share_context = (EGLContext)SDL_GL_GetCurrentContext();
gabomdq@7659
   612
    }
slime73@9595
   613
slime73@9595
   614
    /* Set the context version and other attributes. */
slime73@9637
   615
    if ((major_version < 3 || (minor_version == 0 && profile_es)) &&
slime73@9637
   616
        _this->gl_config.flags == 0 &&
slime73@9637
   617
        (profile_mask == 0 || profile_es)) {
slime73@9637
   618
        /* Create a context without using EGL_KHR_create_context attribs.
slime73@9637
   619
         * When creating a GLES context without EGL_KHR_create_context we can
slime73@9637
   620
         * only specify the major version. When creating a desktop GL context
slime73@9637
   621
         * we can't specify any version, so we only try in that case when the
slime73@9637
   622
         * version is less than 3.0 (matches SDL's GLX/WGL behavior.)
slime73@9637
   623
         */
slime73@9637
   624
        if (profile_es) {
slime73@9595
   625
            attribs[attr++] = EGL_CONTEXT_CLIENT_VERSION;
slime73@9637
   626
            attribs[attr++] = SDL_max(major_version, 1);
slime73@9595
   627
        }
slime73@9595
   628
    } else {
slime73@9595
   629
#ifdef EGL_KHR_create_context
slime73@9595
   630
        /* The Major/minor version, context profiles, and context flags can
slime73@9595
   631
         * only be specified when this extension is available.
slime73@9595
   632
         */
slouken@11175
   633
        if (SDL_EGL_HasExtension(_this, SDL_EGL_DISPLAY_EXTENSION, "EGL_KHR_create_context")) {
slime73@9595
   634
            attribs[attr++] = EGL_CONTEXT_MAJOR_VERSION_KHR;
slime73@9637
   635
            attribs[attr++] = major_version;
slime73@9595
   636
            attribs[attr++] = EGL_CONTEXT_MINOR_VERSION_KHR;
slime73@9637
   637
            attribs[attr++] = minor_version;
slime73@9595
   638
slime73@9595
   639
            /* SDL profile bits match EGL profile bits. */
slime73@9595
   640
            if (profile_mask != 0 && profile_mask != SDL_GL_CONTEXT_PROFILE_ES) {
slime73@9595
   641
                attribs[attr++] = EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR;
slime73@9595
   642
                attribs[attr++] = profile_mask;
slime73@9595
   643
            }
slime73@9595
   644
slime73@9595
   645
            /* SDL flags match EGL flags. */
slime73@9595
   646
            if (_this->gl_config.flags != 0) {
slime73@9595
   647
                attribs[attr++] = EGL_CONTEXT_FLAGS_KHR;
slime73@9595
   648
                attribs[attr++] = _this->gl_config.flags;
slime73@9595
   649
            }
slime73@9595
   650
        } else
slime73@9595
   651
#endif /* EGL_KHR_create_context */
slime73@9595
   652
        {
slime73@9595
   653
            SDL_SetError("Could not create EGL context (context attributes are not supported)");
slime73@9595
   654
            return NULL;
slime73@9595
   655
        }
slime73@9595
   656
    }
slime73@9595
   657
flibitijibibo@11347
   658
    if (_this->gl_config.no_error) {
flibitijibibo@11347
   659
#ifdef EGL_KHR_create_context_no_error
flibitijibibo@11347
   660
        if (SDL_EGL_HasExtension(_this, SDL_EGL_DISPLAY_EXTENSION, "EGL_KHR_create_context_no_error")) {
flibitijibibo@11347
   661
            attribs[attr++] = EGL_CONTEXT_OPENGL_NO_ERROR_KHR;
flibitijibibo@11347
   662
            attribs[attr++] = _this->gl_config.no_error;
flibitijibibo@11347
   663
        } else
flibitijibibo@11347
   664
#endif
flibitijibibo@11347
   665
        {
flibitijibibo@11347
   666
            SDL_SetError("EGL implementation does not support no_error contexts");
flibitijibibo@11347
   667
            return NULL;
flibitijibibo@11347
   668
        }
flibitijibibo@11347
   669
    }
flibitijibibo@11347
   670
slime73@9595
   671
    attribs[attr++] = EGL_NONE;
slime73@9595
   672
gabomdq@8062
   673
    /* Bind the API */
slime73@9637
   674
    if (profile_es) {
gabomdq@8062
   675
        _this->egl_data->eglBindAPI(EGL_OPENGL_ES_API);
slime73@9595
   676
    } else {
slime73@9595
   677
        _this->egl_data->eglBindAPI(EGL_OPENGL_API);
slime73@9595
   678
    }
gabomdq@7659
   679
slime73@9595
   680
    egl_context = _this->egl_data->eglCreateContext(_this->egl_data->egl_display,
slime73@9595
   681
                                      _this->egl_data->egl_config,
slime73@9595
   682
                                      share_context, attribs);
knut@8962
   683
gabomdq@7659
   684
    if (egl_context == EGL_NO_CONTEXT) {
dludwig@10740
   685
        SDL_EGL_SetError("Could not create EGL context", "eglCreateContext");
gabomdq@7659
   686
        return NULL;
gabomdq@7659
   687
    }
slime73@9595
   688
gabomdq@7659
   689
    _this->egl_data->egl_swapinterval = 0;
slime73@9595
   690
gabomdq@7659
   691
    if (SDL_EGL_MakeCurrent(_this, egl_surface, egl_context) < 0) {
dludwig@10740
   692
        /* Save the SDL error set by SDL_EGL_MakeCurrent */
dludwig@10740
   693
        char errorText[1024];
dludwig@10740
   694
        SDL_strlcpy(errorText, SDL_GetError(), SDL_arraysize(errorText));
dludwig@10740
   695
dludwig@10740
   696
        /* Delete the context, which may alter the value returned by SDL_GetError() */
gabomdq@7659
   697
        SDL_EGL_DeleteContext(_this, egl_context);
dludwig@10740
   698
dludwig@10740
   699
        /* Restore the SDL error */
dludwig@10740
   700
        SDL_SetError("%s", errorText);
dludwig@10740
   701
gabomdq@7659
   702
        return NULL;
gabomdq@7659
   703
    }
slime73@9595
   704
gabomdq@7659
   705
    return (SDL_GLContext) egl_context;
gabomdq@7659
   706
}
gabomdq@7659
   707
gabomdq@7659
   708
int
gabomdq@7659
   709
SDL_EGL_MakeCurrent(_THIS, EGLSurface egl_surface, SDL_GLContext context)
gabomdq@7659
   710
{
gabomdq@7679
   711
    EGLContext egl_context = (EGLContext) context;
gabomdq@7679
   712
gabomdq@7659
   713
    if (!_this->egl_data) {
gabomdq@7659
   714
        return SDL_SetError("OpenGL not initialized");
gabomdq@7659
   715
    }
gabomdq@7659
   716
    
gabomdq@7659
   717
    /* The android emulator crashes badly if you try to eglMakeCurrent 
gabomdq@7659
   718
     * with a valid context and invalid surface, so we have to check for both here.
gabomdq@7659
   719
     */
gabomdq@7659
   720
    if (!egl_context || !egl_surface) {
gabomdq@7659
   721
         _this->egl_data->eglMakeCurrent(_this->egl_data->egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
slime73@9595
   722
    } else {
gabomdq@7659
   723
        if (!_this->egl_data->eglMakeCurrent(_this->egl_data->egl_display,
gabomdq@7659
   724
            egl_surface, egl_surface, egl_context)) {
dludwig@10740
   725
            return SDL_EGL_SetError("Unable to make EGL context current", "eglMakeCurrent");
gabomdq@7659
   726
        }
gabomdq@7659
   727
    }
gabomdq@7659
   728
      
gabomdq@7659
   729
    return 0;
gabomdq@7659
   730
}
gabomdq@7659
   731
gabomdq@7659
   732
int
gabomdq@7659
   733
SDL_EGL_SetSwapInterval(_THIS, int interval)
gabomdq@7659
   734
{
gabomdq@7679
   735
    EGLBoolean status;
gabomdq@7679
   736
    
gabomdq@7702
   737
    if (!_this->egl_data) {
gabomdq@7702
   738
        return SDL_SetError("EGL not initialized");
gabomdq@7659
   739
    }
gabomdq@7659
   740
    
gabomdq@7659
   741
    status = _this->egl_data->eglSwapInterval(_this->egl_data->egl_display, interval);
gabomdq@7659
   742
    if (status == EGL_TRUE) {
gabomdq@7659
   743
        _this->egl_data->egl_swapinterval = interval;
gabomdq@7659
   744
        return 0;
gabomdq@7659
   745
    }
gabomdq@7659
   746
    
dludwig@10740
   747
    return SDL_EGL_SetError("Unable to set the EGL swap interval", "eglSwapInterval");
gabomdq@7659
   748
}
gabomdq@7659
   749
gabomdq@7659
   750
int
gabomdq@7659
   751
SDL_EGL_GetSwapInterval(_THIS)
gabomdq@7659
   752
{
gabomdq@7702
   753
    if (!_this->egl_data) {
philipp@10169
   754
        SDL_SetError("EGL not initialized");
philipp@10169
   755
        return 0;
gabomdq@7659
   756
    }
gabomdq@7659
   757
    
gabomdq@7659
   758
    return _this->egl_data->egl_swapinterval;
gabomdq@7659
   759
}
gabomdq@7659
   760
slouken@10693
   761
int
gabomdq@7659
   762
SDL_EGL_SwapBuffers(_THIS, EGLSurface egl_surface)
gabomdq@7659
   763
{
slouken@10693
   764
    if (!_this->egl_data->eglSwapBuffers(_this->egl_data->egl_display, egl_surface)) {
dludwig@10740
   765
        return SDL_EGL_SetError("unable to show color buffer in an OS-native window", "eglSwapBuffers");
slouken@10693
   766
    }
slouken@10693
   767
    return 0;
gabomdq@7659
   768
}
gabomdq@7659
   769
gabomdq@7659
   770
void
gabomdq@7659
   771
SDL_EGL_DeleteContext(_THIS, SDL_GLContext context)
gabomdq@7659
   772
{
gabomdq@7679
   773
    EGLContext egl_context = (EGLContext) context;
gabomdq@7679
   774
gabomdq@7659
   775
    /* Clean up GLES and EGL */
gabomdq@7659
   776
    if (!_this->egl_data) {
gabomdq@7659
   777
        return;
gabomdq@7659
   778
    }
gabomdq@7659
   779
    
gabomdq@7896
   780
    if (egl_context != NULL && egl_context != EGL_NO_CONTEXT) {
gabomdq@7659
   781
        SDL_EGL_MakeCurrent(_this, NULL, NULL);
gabomdq@7659
   782
        _this->egl_data->eglDestroyContext(_this->egl_data->egl_display, egl_context);
gabomdq@7659
   783
    }
gabomdq@7659
   784
        
gabomdq@7659
   785
}
gabomdq@7659
   786
gabomdq@7659
   787
EGLSurface *
gabomdq@7659
   788
SDL_EGL_CreateSurface(_THIS, NativeWindowType nw) 
gabomdq@7659
   789
{
deveee@11830
   790
    /* max 2 values plus terminator. */
deveee@11830
   791
    EGLint attribs[3];
deveee@11830
   792
    int attr = 0;
deveee@11830
   793
	
dludwig@10740
   794
    EGLSurface * surface;
dludwig@10740
   795
gabomdq@8041
   796
    if (SDL_EGL_ChooseConfig(_this) != 0) {
slouken@8045
   797
        return EGL_NO_SURFACE;
gabomdq@8041
   798
    }
gabomdq@8041
   799
    
slouken@10878
   800
#if SDL_VIDEO_DRIVER_ANDROID
slouken@8976
   801
    {
slouken@8976
   802
        /* Android docs recommend doing this!
slouken@8976
   803
         * Ref: http://developer.android.com/reference/android/app/NativeActivity.html 
slouken@8976
   804
         */
slouken@8976
   805
        EGLint format;
slouken@8976
   806
        _this->egl_data->eglGetConfigAttrib(_this->egl_data->egl_display,
slouken@8976
   807
                                            _this->egl_data->egl_config, 
slouken@8976
   808
                                            EGL_NATIVE_VISUAL_ID, &format);
gabomdq@8971
   809
slouken@8976
   810
        ANativeWindow_setBuffersGeometry(nw, 0, 0, format);
slouken@8976
   811
    }
gabomdq@8971
   812
#endif    
deveee@11830
   813
    if (_this->gl_config.framebuffer_srgb_capable) {
deveee@11830
   814
#ifdef EGL_KHR_gl_colorspace
deveee@11830
   815
        if (SDL_EGL_HasExtension(_this, SDL_EGL_DISPLAY_EXTENSION, "EGL_KHR_gl_colorspace")) {
deveee@11830
   816
            attribs[attr++] = EGL_GL_COLORSPACE_KHR;
deveee@11830
   817
            attribs[attr++] = EGL_GL_COLORSPACE_SRGB_KHR;
deveee@11830
   818
        } else
deveee@11830
   819
#endif
deveee@11830
   820
        {
deveee@11830
   821
            SDL_SetError("EGL implementation does not support sRGB system framebuffers");
deveee@11830
   822
            return EGL_NO_SURFACE;
deveee@11830
   823
        }
deveee@11830
   824
    }
deveee@11830
   825
	
deveee@11830
   826
    attribs[attr++] = EGL_NONE;
gabomdq@8971
   827
    
dludwig@10740
   828
    surface = _this->egl_data->eglCreateWindowSurface(
gabomdq@7659
   829
            _this->egl_data->egl_display,
gabomdq@7659
   830
            _this->egl_data->egl_config,
deveee@11830
   831
            nw, &attribs[0]);
dludwig@10740
   832
    if (surface == EGL_NO_SURFACE) {
dludwig@10740
   833
        SDL_EGL_SetError("unable to create an EGL window surface", "eglCreateWindowSurface");
dludwig@10740
   834
    }
dludwig@10740
   835
    return surface;
gabomdq@7659
   836
}
gabomdq@7659
   837
gabomdq@7659
   838
void
gabomdq@7659
   839
SDL_EGL_DestroySurface(_THIS, EGLSurface egl_surface) 
gabomdq@7659
   840
{
gabomdq@7659
   841
    if (!_this->egl_data) {
gabomdq@7659
   842
        return;
gabomdq@7659
   843
    }
gabomdq@7659
   844
    
gabomdq@7659
   845
    if (egl_surface != EGL_NO_SURFACE) {
gabomdq@7659
   846
        _this->egl_data->eglDestroySurface(_this->egl_data->egl_display, egl_surface);
gabomdq@7659
   847
    }
gabomdq@7659
   848
}
gabomdq@7659
   849
gabomdq@7659
   850
#endif /* SDL_VIDEO_OPENGL_EGL */
gabomdq@7659
   851
gabomdq@7659
   852
/* vi: set ts=4 sw=4 expandtab: */
philipp@11192
   853