src/video/SDL_egl.c
author Ryan C. Gordon <icculus@icculus.org>
Wed, 06 Sep 2017 19:34:23 -0400
changeset 11462 dc5698982bac
parent 11418 7749a3b2b343
child 11516 68a767ae3a88
permissions -rw-r--r--
raspberrypi: The latest Raspbian moved its EGL and GLES2 libs elsewhere.

Now we try the new (hardware-specific) pathnames first, and if those fail to
load, we'll try the more generic names that earlier versions of Raspbian used.

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