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