src/video/SDL_egl.c
author Conn O'Griofa <connogriofa@gmail.com>
Sat, 04 Nov 2017 09:04:19 -0700
changeset 11676 591e138c5a04
parent 11553 b89649d1a7a5
child 11677 aed6a9ff3c99
permissions -rw-r--r--
Raspberry Pi: fix ES 1/PVR support & autodetect Mesa driver
* The brcmGLESv2 vendor library also supports ES PVR/1 profiles
* Fallback to standard Mesa libraries if the VC4 driver is loaded
     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 ( vc4 ? "libEGL.so.1" : "/opt/vc/lib/libbrcmEGL.so" )
    48 #define DEFAULT_OGL_ES2 ( vc4 ? "libGLESv2.so.2" : "/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 ( vc4 ? "libGLES_CM.so.1" : "/opt/vc/lib/libbrcmGLESv2.so" )
    52 #define DEFAULT_OGL_ES ( vc4 ? "libGLESv1_CM.so.1" : "/opt/vc/lib/libbrcmGLESv2.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         while (*ext_start) {
   181             ext_start = SDL_strstr(ext_start, ext);
   182             if (ext_start == NULL) {
   183                 return SDL_FALSE;
   184             }
   185             /* Check if the match is not just a substring of one of the extensions */
   186             if (ext_start == egl_extstr || *(ext_start - 1) == ' ') {
   187                 if (ext_start[ext_len] == ' ' || ext_start[ext_len] == 0) {
   188                     return SDL_TRUE;
   189                 }
   190             }
   191             /* If the search stopped in the middle of an extension, skip to the end of it */
   192             ext_start += ext_len;
   193             while (*ext_start != ' ' && *ext_start != 0) {
   194                 ext_start++;
   195             }
   196         }
   197     }
   198 
   199     return SDL_FALSE;
   200 }
   201 
   202 void *
   203 SDL_EGL_GetProcAddress(_THIS, const char *proc)
   204 {
   205     static char procname[1024];
   206     void *retval;
   207     
   208     /* eglGetProcAddress is busted on Android http://code.google.com/p/android/issues/detail?id=7681 */
   209 #if !defined(SDL_VIDEO_DRIVER_ANDROID)
   210     if (_this->egl_data->eglGetProcAddress) {
   211         retval = _this->egl_data->eglGetProcAddress(proc);
   212         if (retval) {
   213             return retval;
   214         }
   215     }
   216 #endif
   217     
   218     retval = SDL_LoadFunction(_this->egl_data->egl_dll_handle, proc);
   219     if (!retval && SDL_strlen(proc) <= 1022) {
   220         procname[0] = '_';
   221         SDL_strlcpy(procname + 1, proc, 1022);
   222         retval = SDL_LoadFunction(_this->egl_data->egl_dll_handle, procname);
   223     }
   224     return retval;
   225 }
   226 
   227 void
   228 SDL_EGL_UnloadLibrary(_THIS)
   229 {
   230     if (_this->egl_data) {
   231         if (_this->egl_data->egl_display) {
   232             _this->egl_data->eglTerminate(_this->egl_data->egl_display);
   233             _this->egl_data->egl_display = NULL;
   234         }
   235 
   236         if (_this->egl_data->dll_handle) {
   237             SDL_UnloadObject(_this->egl_data->dll_handle);
   238             _this->egl_data->dll_handle = NULL;
   239         }
   240         if (_this->egl_data->egl_dll_handle) {
   241             SDL_UnloadObject(_this->egl_data->egl_dll_handle);
   242             _this->egl_data->egl_dll_handle = NULL;
   243         }
   244         
   245         SDL_free(_this->egl_data);
   246         _this->egl_data = NULL;
   247     }
   248 }
   249 
   250 int
   251 SDL_EGL_LoadLibrary(_THIS, const char *egl_path, NativeDisplayType native_display, EGLenum platform)
   252 {
   253     void *dll_handle = NULL, *egl_dll_handle = NULL; /* The naming is counter intuitive, but hey, I just work here -- Gabriel */
   254     const char *path = NULL;
   255     int egl_version_major = 0, egl_version_minor = 0;
   256 #if SDL_VIDEO_DRIVER_WINDOWS || SDL_VIDEO_DRIVER_WINRT
   257     const char *d3dcompiler;
   258 #endif
   259 #if SDL_VIDEO_DRIVER_RPI
   260     SDL_bool vc4 = (0 == access("/sys/module/vc4/", F_OK));
   261 #endif
   262 
   263     if (_this->egl_data) {
   264         return SDL_SetError("OpenGL ES context already created");
   265     }
   266 
   267     _this->egl_data = (struct SDL_EGL_VideoData *) SDL_calloc(1, sizeof(SDL_EGL_VideoData));
   268     if (!_this->egl_data) {
   269         return SDL_OutOfMemory();
   270     }
   271 
   272 #if SDL_VIDEO_DRIVER_WINDOWS || SDL_VIDEO_DRIVER_WINRT
   273     d3dcompiler = SDL_GetHint(SDL_HINT_VIDEO_WIN_D3DCOMPILER);
   274     if (!d3dcompiler) {
   275         if (WIN_IsWindowsVistaOrGreater()) {
   276             d3dcompiler = "d3dcompiler_46.dll";
   277         } else {
   278             d3dcompiler = "d3dcompiler_43.dll";
   279         }
   280     }
   281     if (SDL_strcasecmp(d3dcompiler, "none") != 0) {
   282         if (SDL_LoadObject(d3dcompiler) == NULL) {
   283             SDL_ClearError();
   284         }
   285     }
   286 #endif
   287 
   288 #ifndef SDL_VIDEO_STATIC_ANGLE
   289     /* A funny thing, loading EGL.so first does not work on the Raspberry, so we load libGL* first */
   290     path = SDL_getenv("SDL_VIDEO_GL_DRIVER");
   291     if (path != NULL) {
   292         egl_dll_handle = SDL_LoadObject(path);
   293     }
   294 
   295     if (egl_dll_handle == NULL) {
   296         if (_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES) {
   297             if (_this->gl_config.major_version > 1) {
   298                 path = DEFAULT_OGL_ES2;
   299                 egl_dll_handle = SDL_LoadObject(path);
   300 #ifdef ALT_OGL_ES2
   301                 if (egl_dll_handle == NULL && !vc4) {
   302                     path = ALT_OGL_ES2;
   303                     egl_dll_handle = SDL_LoadObject(path);
   304                 }
   305 #endif
   306 
   307             } else {
   308                 path = DEFAULT_OGL_ES;
   309                 egl_dll_handle = SDL_LoadObject(path);
   310                 if (egl_dll_handle == NULL) {
   311                     path = DEFAULT_OGL_ES_PVR;
   312                     egl_dll_handle = SDL_LoadObject(path);
   313                 }
   314 #ifdef ALT_OGL_ES2
   315                 if (egl_dll_handle == NULL && !vc4) {
   316                     path = ALT_OGL_ES2;
   317                     egl_dll_handle = SDL_LoadObject(path);
   318                 }
   319 #endif
   320             }
   321         }
   322 #ifdef DEFAULT_OGL         
   323         else {
   324             path = DEFAULT_OGL;
   325             egl_dll_handle = SDL_LoadObject(path);
   326         }
   327 #endif        
   328     }
   329     _this->egl_data->egl_dll_handle = egl_dll_handle;
   330 
   331     if (egl_dll_handle == NULL) {
   332         return SDL_SetError("Could not initialize OpenGL / GLES library");
   333     }
   334 
   335     /* Loading libGL* in the previous step took care of loading libEGL.so, but we future proof by double checking */
   336     if (egl_path != NULL) {
   337         dll_handle = SDL_LoadObject(egl_path);
   338     }   
   339     /* Try loading a EGL symbol, if it does not work try the default library paths */
   340     if (dll_handle == NULL || SDL_LoadFunction(dll_handle, "eglChooseConfig") == NULL) {
   341         if (dll_handle != NULL) {
   342             SDL_UnloadObject(dll_handle);
   343         }
   344         path = SDL_getenv("SDL_VIDEO_EGL_DRIVER");
   345         if (path == NULL) {
   346             path = DEFAULT_EGL;
   347         }
   348         dll_handle = SDL_LoadObject(path);
   349 
   350 #ifdef ALT_EGL
   351         if (dll_handle == NULL && !vc4) {
   352             path = ALT_EGL;
   353             dll_handle = SDL_LoadObject(path);
   354         }
   355 #endif
   356 
   357         if (dll_handle == NULL || SDL_LoadFunction(dll_handle, "eglChooseConfig") == NULL) {
   358             if (dll_handle != NULL) {
   359                 SDL_UnloadObject(dll_handle);
   360             }
   361             return SDL_SetError("Could not load EGL library");
   362         }
   363         SDL_ClearError();
   364     }
   365 #endif
   366 
   367     _this->egl_data->dll_handle = dll_handle;
   368 
   369     /* Load new function pointers */
   370     LOAD_FUNC(eglGetDisplay);
   371     LOAD_FUNC(eglInitialize);
   372     LOAD_FUNC(eglTerminate);
   373     LOAD_FUNC(eglGetProcAddress);
   374     LOAD_FUNC(eglChooseConfig);
   375     LOAD_FUNC(eglGetConfigAttrib);
   376     LOAD_FUNC(eglCreateContext);
   377     LOAD_FUNC(eglDestroyContext);
   378     LOAD_FUNC(eglCreateWindowSurface);
   379     LOAD_FUNC(eglDestroySurface);
   380     LOAD_FUNC(eglMakeCurrent);
   381     LOAD_FUNC(eglSwapBuffers);
   382     LOAD_FUNC(eglSwapInterval);
   383     LOAD_FUNC(eglWaitNative);
   384     LOAD_FUNC(eglWaitGL);
   385     LOAD_FUNC(eglBindAPI);
   386     LOAD_FUNC(eglQueryString);
   387     LOAD_FUNC(eglGetError);
   388 
   389     if (_this->egl_data->eglQueryString) {
   390         /* EGL 1.5 allows querying for client version */
   391         const char *egl_version = _this->egl_data->eglQueryString(EGL_NO_DISPLAY, EGL_VERSION);
   392         if (egl_version != NULL) {
   393             if (SDL_sscanf(egl_version, "%d.%d", &egl_version_major, &egl_version_minor) != 2) {
   394                 egl_version_major = 0;
   395                 egl_version_minor = 0;
   396                 SDL_LogWarn(SDL_LOG_CATEGORY_VIDEO, "Could not parse EGL version string: %s", egl_version);
   397             }
   398         }
   399     }
   400 
   401     if (egl_version_major == 1 && egl_version_minor == 5) {
   402         LOAD_FUNC(eglGetPlatformDisplay);
   403     }
   404 
   405     _this->egl_data->egl_display = EGL_NO_DISPLAY;
   406 #if !defined(__WINRT__)
   407     if (platform) {
   408         if (egl_version_major == 1 && egl_version_minor == 5) {
   409             _this->egl_data->egl_display = _this->egl_data->eglGetPlatformDisplay(platform, (void *)(size_t)native_display, NULL);
   410         } else {
   411             if (SDL_EGL_HasExtension(_this, SDL_EGL_CLIENT_EXTENSION, "EGL_EXT_platform_base")) {
   412                 _this->egl_data->eglGetPlatformDisplayEXT = SDL_EGL_GetProcAddress(_this, "eglGetPlatformDisplayEXT");
   413                 if (_this->egl_data->eglGetPlatformDisplayEXT) {
   414                     _this->egl_data->egl_display = _this->egl_data->eglGetPlatformDisplayEXT(platform, (void *)(size_t)native_display, NULL);
   415                 }
   416             }
   417         }
   418     }
   419     /* Try the implementation-specific eglGetDisplay even if eglGetPlatformDisplay fails */
   420     if (_this->egl_data->egl_display == EGL_NO_DISPLAY) {
   421         _this->egl_data->egl_display = _this->egl_data->eglGetDisplay(native_display);
   422     }
   423     if (_this->egl_data->egl_display == EGL_NO_DISPLAY) {
   424         return SDL_SetError("Could not get EGL display");
   425     }
   426     
   427     if (_this->egl_data->eglInitialize(_this->egl_data->egl_display, NULL, NULL) != EGL_TRUE) {
   428         return SDL_SetError("Could not initialize EGL");
   429     }
   430 #endif
   431 
   432     if (path) {
   433         SDL_strlcpy(_this->gl_config.driver_path, path, sizeof(_this->gl_config.driver_path) - 1);
   434     } else {
   435         *_this->gl_config.driver_path = '\0';
   436     }
   437     
   438     return 0;
   439 }
   440 
   441 int
   442 SDL_EGL_ChooseConfig(_THIS) 
   443 {
   444 /* 64 seems nice. */
   445     EGLint attribs[64];
   446     EGLint found_configs = 0, value;
   447 #ifdef SDL_VIDEO_DRIVER_KMSDRM
   448     /* Intel EGL on KMS/DRM (al least) returns invalid configs that confuse the bitdiff search used */
   449     /* later in this function, so we simply use the first one when using the KMSDRM driver for now. */
   450     EGLConfig configs[1];
   451 #else
   452     /* 128 seems even nicer here */
   453     EGLConfig configs[128];
   454 #endif
   455     int i, j, best_bitdiff = -1, bitdiff;
   456    
   457     if (!_this->egl_data) {
   458         /* The EGL library wasn't loaded, SDL_GetError() should have info */
   459         return -1;
   460     }
   461   
   462     /* Get a valid EGL configuration */
   463     i = 0;
   464     attribs[i++] = EGL_RED_SIZE;
   465     attribs[i++] = _this->gl_config.red_size;
   466     attribs[i++] = EGL_GREEN_SIZE;
   467     attribs[i++] = _this->gl_config.green_size;
   468     attribs[i++] = EGL_BLUE_SIZE;
   469     attribs[i++] = _this->gl_config.blue_size;
   470     
   471     if (_this->gl_config.alpha_size) {
   472         attribs[i++] = EGL_ALPHA_SIZE;
   473         attribs[i++] = _this->gl_config.alpha_size;
   474     }
   475     
   476     if (_this->gl_config.buffer_size) {
   477         attribs[i++] = EGL_BUFFER_SIZE;
   478         attribs[i++] = _this->gl_config.buffer_size;
   479     }
   480     
   481     attribs[i++] = EGL_DEPTH_SIZE;
   482     attribs[i++] = _this->gl_config.depth_size;
   483     
   484     if (_this->gl_config.stencil_size) {
   485         attribs[i++] = EGL_STENCIL_SIZE;
   486         attribs[i++] = _this->gl_config.stencil_size;
   487     }
   488     
   489     if (_this->gl_config.multisamplebuffers) {
   490         attribs[i++] = EGL_SAMPLE_BUFFERS;
   491         attribs[i++] = _this->gl_config.multisamplebuffers;
   492     }
   493     
   494     if (_this->gl_config.multisamplesamples) {
   495         attribs[i++] = EGL_SAMPLES;
   496         attribs[i++] = _this->gl_config.multisamplesamples;
   497     }
   498 
   499     if (_this->gl_config.framebuffer_srgb_capable) {
   500 #ifdef EGL_KHR_gl_colorspace
   501         if (SDL_EGL_HasExtension(_this, SDL_EGL_DISPLAY_EXTENSION, "EGL_KHR_gl_colorspace")) {
   502             attribs[i++] = EGL_GL_COLORSPACE_KHR;
   503             attribs[i++] = EGL_GL_COLORSPACE_SRGB_KHR;
   504         } else
   505 #endif
   506         {
   507             return SDL_SetError("EGL implementation does not support sRGB system framebuffers");
   508         }
   509     }
   510 
   511     attribs[i++] = EGL_RENDERABLE_TYPE;
   512     if (_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES) {
   513 #ifdef EGL_KHR_create_context
   514         if (_this->gl_config.major_version >= 3 &&
   515             SDL_EGL_HasExtension(_this, SDL_EGL_DISPLAY_EXTENSION, "EGL_KHR_create_context")) {
   516             attribs[i++] = EGL_OPENGL_ES3_BIT_KHR;
   517         } else
   518 #endif
   519         if (_this->gl_config.major_version >= 2) {
   520             attribs[i++] = EGL_OPENGL_ES2_BIT;
   521         } else {
   522             attribs[i++] = EGL_OPENGL_ES_BIT;
   523         }
   524         _this->egl_data->eglBindAPI(EGL_OPENGL_ES_API);
   525     } else {
   526         attribs[i++] = EGL_OPENGL_BIT;
   527         _this->egl_data->eglBindAPI(EGL_OPENGL_API);
   528     }
   529 
   530     attribs[i++] = EGL_NONE;
   531 
   532     if (_this->egl_data->eglChooseConfig(_this->egl_data->egl_display,
   533         attribs,
   534         configs, SDL_arraysize(configs),
   535         &found_configs) == EGL_FALSE ||
   536         found_configs == 0) {
   537         return SDL_EGL_SetError("Couldn't find matching EGL config", "eglChooseConfig");
   538     }
   539 
   540     /* eglChooseConfig returns a number of configurations that match or exceed the requested attribs. */
   541     /* From those, we select the one that matches our requirements more closely via a makeshift algorithm */
   542 
   543     for (i = 0; i < found_configs; i++ ) {
   544         bitdiff = 0;
   545         for (j = 0; j < SDL_arraysize(attribs) - 1; j += 2) {
   546             if (attribs[j] == EGL_NONE) {
   547                break;
   548             }
   549             
   550             if ( attribs[j+1] != EGL_DONT_CARE && (
   551                 attribs[j] == EGL_RED_SIZE ||
   552                 attribs[j] == EGL_GREEN_SIZE ||
   553                 attribs[j] == EGL_BLUE_SIZE ||
   554                 attribs[j] == EGL_ALPHA_SIZE ||
   555                 attribs[j] == EGL_DEPTH_SIZE ||
   556                 attribs[j] == EGL_STENCIL_SIZE)) {
   557                 _this->egl_data->eglGetConfigAttrib(_this->egl_data->egl_display, configs[i], attribs[j], &value);
   558                 bitdiff += value - attribs[j + 1]; /* value is always >= attrib */
   559             }
   560         }
   561 
   562         if (bitdiff < best_bitdiff || best_bitdiff == -1) {
   563             _this->egl_data->egl_config = configs[i];
   564             
   565             best_bitdiff = bitdiff;
   566         }
   567 
   568         if (bitdiff == 0) {
   569             break; /* we found an exact match! */
   570         }
   571     }
   572     
   573     return 0;
   574 }
   575 
   576 SDL_GLContext
   577 SDL_EGL_CreateContext(_THIS, EGLSurface egl_surface)
   578 {
   579     /* max 14 values plus terminator. */
   580     EGLint attribs[15];
   581     int attr = 0;
   582 
   583     EGLContext egl_context, share_context = EGL_NO_CONTEXT;
   584     EGLint profile_mask = _this->gl_config.profile_mask;
   585     EGLint major_version = _this->gl_config.major_version;
   586     EGLint minor_version = _this->gl_config.minor_version;
   587     SDL_bool profile_es = (profile_mask == SDL_GL_CONTEXT_PROFILE_ES);
   588 
   589     if (!_this->egl_data) {
   590         /* The EGL library wasn't loaded, SDL_GetError() should have info */
   591         return NULL;
   592     }
   593 
   594     if (_this->gl_config.share_with_current_context) {
   595         share_context = (EGLContext)SDL_GL_GetCurrentContext();
   596     }
   597 
   598     /* Set the context version and other attributes. */
   599     if ((major_version < 3 || (minor_version == 0 && profile_es)) &&
   600         _this->gl_config.flags == 0 &&
   601         (profile_mask == 0 || profile_es)) {
   602         /* Create a context without using EGL_KHR_create_context attribs.
   603          * When creating a GLES context without EGL_KHR_create_context we can
   604          * only specify the major version. When creating a desktop GL context
   605          * we can't specify any version, so we only try in that case when the
   606          * version is less than 3.0 (matches SDL's GLX/WGL behavior.)
   607          */
   608         if (profile_es) {
   609             attribs[attr++] = EGL_CONTEXT_CLIENT_VERSION;
   610             attribs[attr++] = SDL_max(major_version, 1);
   611         }
   612     } else {
   613 #ifdef EGL_KHR_create_context
   614         /* The Major/minor version, context profiles, and context flags can
   615          * only be specified when this extension is available.
   616          */
   617         if (SDL_EGL_HasExtension(_this, SDL_EGL_DISPLAY_EXTENSION, "EGL_KHR_create_context")) {
   618             attribs[attr++] = EGL_CONTEXT_MAJOR_VERSION_KHR;
   619             attribs[attr++] = major_version;
   620             attribs[attr++] = EGL_CONTEXT_MINOR_VERSION_KHR;
   621             attribs[attr++] = minor_version;
   622 
   623             /* SDL profile bits match EGL profile bits. */
   624             if (profile_mask != 0 && profile_mask != SDL_GL_CONTEXT_PROFILE_ES) {
   625                 attribs[attr++] = EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR;
   626                 attribs[attr++] = profile_mask;
   627             }
   628 
   629             /* SDL flags match EGL flags. */
   630             if (_this->gl_config.flags != 0) {
   631                 attribs[attr++] = EGL_CONTEXT_FLAGS_KHR;
   632                 attribs[attr++] = _this->gl_config.flags;
   633             }
   634         } else
   635 #endif /* EGL_KHR_create_context */
   636         {
   637             SDL_SetError("Could not create EGL context (context attributes are not supported)");
   638             return NULL;
   639         }
   640     }
   641 
   642     if (_this->gl_config.no_error) {
   643 #ifdef EGL_KHR_create_context_no_error
   644         if (SDL_EGL_HasExtension(_this, SDL_EGL_DISPLAY_EXTENSION, "EGL_KHR_create_context_no_error")) {
   645             attribs[attr++] = EGL_CONTEXT_OPENGL_NO_ERROR_KHR;
   646             attribs[attr++] = _this->gl_config.no_error;
   647         } else
   648 #endif
   649         {
   650             SDL_SetError("EGL implementation does not support no_error contexts");
   651             return NULL;
   652         }
   653     }
   654 
   655     attribs[attr++] = EGL_NONE;
   656 
   657     /* Bind the API */
   658     if (profile_es) {
   659         _this->egl_data->eglBindAPI(EGL_OPENGL_ES_API);
   660     } else {
   661         _this->egl_data->eglBindAPI(EGL_OPENGL_API);
   662     }
   663 
   664     egl_context = _this->egl_data->eglCreateContext(_this->egl_data->egl_display,
   665                                       _this->egl_data->egl_config,
   666                                       share_context, attribs);
   667 
   668     if (egl_context == EGL_NO_CONTEXT) {
   669         SDL_EGL_SetError("Could not create EGL context", "eglCreateContext");
   670         return NULL;
   671     }
   672 
   673     _this->egl_data->egl_swapinterval = 0;
   674 
   675     if (SDL_EGL_MakeCurrent(_this, egl_surface, egl_context) < 0) {
   676         /* Save the SDL error set by SDL_EGL_MakeCurrent */
   677         char errorText[1024];
   678         SDL_strlcpy(errorText, SDL_GetError(), SDL_arraysize(errorText));
   679 
   680         /* Delete the context, which may alter the value returned by SDL_GetError() */
   681         SDL_EGL_DeleteContext(_this, egl_context);
   682 
   683         /* Restore the SDL error */
   684         SDL_SetError("%s", errorText);
   685 
   686         return NULL;
   687     }
   688 
   689     return (SDL_GLContext) egl_context;
   690 }
   691 
   692 int
   693 SDL_EGL_MakeCurrent(_THIS, EGLSurface egl_surface, SDL_GLContext context)
   694 {
   695     EGLContext egl_context = (EGLContext) context;
   696 
   697     if (!_this->egl_data) {
   698         return SDL_SetError("OpenGL not initialized");
   699     }
   700     
   701     /* The android emulator crashes badly if you try to eglMakeCurrent 
   702      * with a valid context and invalid surface, so we have to check for both here.
   703      */
   704     if (!egl_context || !egl_surface) {
   705          _this->egl_data->eglMakeCurrent(_this->egl_data->egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
   706     } else {
   707         if (!_this->egl_data->eglMakeCurrent(_this->egl_data->egl_display,
   708             egl_surface, egl_surface, egl_context)) {
   709             return SDL_EGL_SetError("Unable to make EGL context current", "eglMakeCurrent");
   710         }
   711     }
   712       
   713     return 0;
   714 }
   715 
   716 int
   717 SDL_EGL_SetSwapInterval(_THIS, int interval)
   718 {
   719     EGLBoolean status;
   720     
   721     if (!_this->egl_data) {
   722         return SDL_SetError("EGL not initialized");
   723     }
   724     
   725     status = _this->egl_data->eglSwapInterval(_this->egl_data->egl_display, interval);
   726     if (status == EGL_TRUE) {
   727         _this->egl_data->egl_swapinterval = interval;
   728         return 0;
   729     }
   730     
   731     return SDL_EGL_SetError("Unable to set the EGL swap interval", "eglSwapInterval");
   732 }
   733 
   734 int
   735 SDL_EGL_GetSwapInterval(_THIS)
   736 {
   737     if (!_this->egl_data) {
   738         SDL_SetError("EGL not initialized");
   739         return 0;
   740     }
   741     
   742     return _this->egl_data->egl_swapinterval;
   743 }
   744 
   745 int
   746 SDL_EGL_SwapBuffers(_THIS, EGLSurface egl_surface)
   747 {
   748     if (!_this->egl_data->eglSwapBuffers(_this->egl_data->egl_display, egl_surface)) {
   749         return SDL_EGL_SetError("unable to show color buffer in an OS-native window", "eglSwapBuffers");
   750     }
   751     return 0;
   752 }
   753 
   754 void
   755 SDL_EGL_DeleteContext(_THIS, SDL_GLContext context)
   756 {
   757     EGLContext egl_context = (EGLContext) context;
   758 
   759     /* Clean up GLES and EGL */
   760     if (!_this->egl_data) {
   761         return;
   762     }
   763     
   764     if (egl_context != NULL && egl_context != EGL_NO_CONTEXT) {
   765         SDL_EGL_MakeCurrent(_this, NULL, NULL);
   766         _this->egl_data->eglDestroyContext(_this->egl_data->egl_display, egl_context);
   767     }
   768         
   769 }
   770 
   771 EGLSurface *
   772 SDL_EGL_CreateSurface(_THIS, NativeWindowType nw) 
   773 {
   774     EGLSurface * surface;
   775 
   776     if (SDL_EGL_ChooseConfig(_this) != 0) {
   777         return EGL_NO_SURFACE;
   778     }
   779     
   780 #if SDL_VIDEO_DRIVER_ANDROID
   781     {
   782         /* Android docs recommend doing this!
   783          * Ref: http://developer.android.com/reference/android/app/NativeActivity.html 
   784          */
   785         EGLint format;
   786         _this->egl_data->eglGetConfigAttrib(_this->egl_data->egl_display,
   787                                             _this->egl_data->egl_config, 
   788                                             EGL_NATIVE_VISUAL_ID, &format);
   789 
   790         ANativeWindow_setBuffersGeometry(nw, 0, 0, format);
   791     }
   792 #endif    
   793     
   794     surface = _this->egl_data->eglCreateWindowSurface(
   795             _this->egl_data->egl_display,
   796             _this->egl_data->egl_config,
   797             nw, NULL);
   798     if (surface == EGL_NO_SURFACE) {
   799         SDL_EGL_SetError("unable to create an EGL window surface", "eglCreateWindowSurface");
   800     }
   801     return surface;
   802 }
   803 
   804 void
   805 SDL_EGL_DestroySurface(_THIS, EGLSurface egl_surface) 
   806 {
   807     if (!_this->egl_data) {
   808         return;
   809     }
   810     
   811     if (egl_surface != EGL_NO_SURFACE) {
   812         _this->egl_data->eglDestroySurface(_this->egl_data->egl_display, egl_surface);
   813     }
   814 }
   815 
   816 #endif /* SDL_VIDEO_OPENGL_EGL */
   817 
   818 /* vi: set ts=4 sw=4 expandtab: */
   819