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