src/video/SDL_egl.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 04 Nov 2017 09:07:33 -0700
changeset 11677 aed6a9ff3c99
parent 11676 591e138c5a04
child 11718 3006dd42e4db
permissions -rw-r--r--
Fixed bug 3940 - Add support for EGL_SURFACE_TYPE

tomwardio

Add support to be able to set EGL_SURFACE_TYPE bits when creating an EGL config. This is usefule when wanting to create pixel buffer surfaces in custom video drivers.
     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     if (_this->egl_data->egl_surfacetype) {
   531         attribs[i++] = EGL_SURFACE_TYPE;
   532         attribs[i++] = _this->egl_data->egl_surfacetype;
   533     }
   534 
   535     attribs[i++] = EGL_NONE;
   536 
   537     if (_this->egl_data->eglChooseConfig(_this->egl_data->egl_display,
   538         attribs,
   539         configs, SDL_arraysize(configs),
   540         &found_configs) == EGL_FALSE ||
   541         found_configs == 0) {
   542         return SDL_EGL_SetError("Couldn't find matching EGL config", "eglChooseConfig");
   543     }
   544 
   545     /* eglChooseConfig returns a number of configurations that match or exceed the requested attribs. */
   546     /* From those, we select the one that matches our requirements more closely via a makeshift algorithm */
   547 
   548     for (i = 0; i < found_configs; i++ ) {
   549         bitdiff = 0;
   550         for (j = 0; j < SDL_arraysize(attribs) - 1; j += 2) {
   551             if (attribs[j] == EGL_NONE) {
   552                break;
   553             }
   554             
   555             if ( attribs[j+1] != EGL_DONT_CARE && (
   556                 attribs[j] == EGL_RED_SIZE ||
   557                 attribs[j] == EGL_GREEN_SIZE ||
   558                 attribs[j] == EGL_BLUE_SIZE ||
   559                 attribs[j] == EGL_ALPHA_SIZE ||
   560                 attribs[j] == EGL_DEPTH_SIZE ||
   561                 attribs[j] == EGL_STENCIL_SIZE)) {
   562                 _this->egl_data->eglGetConfigAttrib(_this->egl_data->egl_display, configs[i], attribs[j], &value);
   563                 bitdiff += value - attribs[j + 1]; /* value is always >= attrib */
   564             }
   565         }
   566 
   567         if (bitdiff < best_bitdiff || best_bitdiff == -1) {
   568             _this->egl_data->egl_config = configs[i];
   569             
   570             best_bitdiff = bitdiff;
   571         }
   572 
   573         if (bitdiff == 0) {
   574             break; /* we found an exact match! */
   575         }
   576     }
   577     
   578     return 0;
   579 }
   580 
   581 SDL_GLContext
   582 SDL_EGL_CreateContext(_THIS, EGLSurface egl_surface)
   583 {
   584     /* max 14 values plus terminator. */
   585     EGLint attribs[15];
   586     int attr = 0;
   587 
   588     EGLContext egl_context, share_context = EGL_NO_CONTEXT;
   589     EGLint profile_mask = _this->gl_config.profile_mask;
   590     EGLint major_version = _this->gl_config.major_version;
   591     EGLint minor_version = _this->gl_config.minor_version;
   592     SDL_bool profile_es = (profile_mask == SDL_GL_CONTEXT_PROFILE_ES);
   593 
   594     if (!_this->egl_data) {
   595         /* The EGL library wasn't loaded, SDL_GetError() should have info */
   596         return NULL;
   597     }
   598 
   599     if (_this->gl_config.share_with_current_context) {
   600         share_context = (EGLContext)SDL_GL_GetCurrentContext();
   601     }
   602 
   603     /* Set the context version and other attributes. */
   604     if ((major_version < 3 || (minor_version == 0 && profile_es)) &&
   605         _this->gl_config.flags == 0 &&
   606         (profile_mask == 0 || profile_es)) {
   607         /* Create a context without using EGL_KHR_create_context attribs.
   608          * When creating a GLES context without EGL_KHR_create_context we can
   609          * only specify the major version. When creating a desktop GL context
   610          * we can't specify any version, so we only try in that case when the
   611          * version is less than 3.0 (matches SDL's GLX/WGL behavior.)
   612          */
   613         if (profile_es) {
   614             attribs[attr++] = EGL_CONTEXT_CLIENT_VERSION;
   615             attribs[attr++] = SDL_max(major_version, 1);
   616         }
   617     } else {
   618 #ifdef EGL_KHR_create_context
   619         /* The Major/minor version, context profiles, and context flags can
   620          * only be specified when this extension is available.
   621          */
   622         if (SDL_EGL_HasExtension(_this, SDL_EGL_DISPLAY_EXTENSION, "EGL_KHR_create_context")) {
   623             attribs[attr++] = EGL_CONTEXT_MAJOR_VERSION_KHR;
   624             attribs[attr++] = major_version;
   625             attribs[attr++] = EGL_CONTEXT_MINOR_VERSION_KHR;
   626             attribs[attr++] = minor_version;
   627 
   628             /* SDL profile bits match EGL profile bits. */
   629             if (profile_mask != 0 && profile_mask != SDL_GL_CONTEXT_PROFILE_ES) {
   630                 attribs[attr++] = EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR;
   631                 attribs[attr++] = profile_mask;
   632             }
   633 
   634             /* SDL flags match EGL flags. */
   635             if (_this->gl_config.flags != 0) {
   636                 attribs[attr++] = EGL_CONTEXT_FLAGS_KHR;
   637                 attribs[attr++] = _this->gl_config.flags;
   638             }
   639         } else
   640 #endif /* EGL_KHR_create_context */
   641         {
   642             SDL_SetError("Could not create EGL context (context attributes are not supported)");
   643             return NULL;
   644         }
   645     }
   646 
   647     if (_this->gl_config.no_error) {
   648 #ifdef EGL_KHR_create_context_no_error
   649         if (SDL_EGL_HasExtension(_this, SDL_EGL_DISPLAY_EXTENSION, "EGL_KHR_create_context_no_error")) {
   650             attribs[attr++] = EGL_CONTEXT_OPENGL_NO_ERROR_KHR;
   651             attribs[attr++] = _this->gl_config.no_error;
   652         } else
   653 #endif
   654         {
   655             SDL_SetError("EGL implementation does not support no_error contexts");
   656             return NULL;
   657         }
   658     }
   659 
   660     attribs[attr++] = EGL_NONE;
   661 
   662     /* Bind the API */
   663     if (profile_es) {
   664         _this->egl_data->eglBindAPI(EGL_OPENGL_ES_API);
   665     } else {
   666         _this->egl_data->eglBindAPI(EGL_OPENGL_API);
   667     }
   668 
   669     egl_context = _this->egl_data->eglCreateContext(_this->egl_data->egl_display,
   670                                       _this->egl_data->egl_config,
   671                                       share_context, attribs);
   672 
   673     if (egl_context == EGL_NO_CONTEXT) {
   674         SDL_EGL_SetError("Could not create EGL context", "eglCreateContext");
   675         return NULL;
   676     }
   677 
   678     _this->egl_data->egl_swapinterval = 0;
   679 
   680     if (SDL_EGL_MakeCurrent(_this, egl_surface, egl_context) < 0) {
   681         /* Save the SDL error set by SDL_EGL_MakeCurrent */
   682         char errorText[1024];
   683         SDL_strlcpy(errorText, SDL_GetError(), SDL_arraysize(errorText));
   684 
   685         /* Delete the context, which may alter the value returned by SDL_GetError() */
   686         SDL_EGL_DeleteContext(_this, egl_context);
   687 
   688         /* Restore the SDL error */
   689         SDL_SetError("%s", errorText);
   690 
   691         return NULL;
   692     }
   693 
   694     return (SDL_GLContext) egl_context;
   695 }
   696 
   697 int
   698 SDL_EGL_MakeCurrent(_THIS, EGLSurface egl_surface, SDL_GLContext context)
   699 {
   700     EGLContext egl_context = (EGLContext) context;
   701 
   702     if (!_this->egl_data) {
   703         return SDL_SetError("OpenGL not initialized");
   704     }
   705     
   706     /* The android emulator crashes badly if you try to eglMakeCurrent 
   707      * with a valid context and invalid surface, so we have to check for both here.
   708      */
   709     if (!egl_context || !egl_surface) {
   710          _this->egl_data->eglMakeCurrent(_this->egl_data->egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
   711     } else {
   712         if (!_this->egl_data->eglMakeCurrent(_this->egl_data->egl_display,
   713             egl_surface, egl_surface, egl_context)) {
   714             return SDL_EGL_SetError("Unable to make EGL context current", "eglMakeCurrent");
   715         }
   716     }
   717       
   718     return 0;
   719 }
   720 
   721 int
   722 SDL_EGL_SetSwapInterval(_THIS, int interval)
   723 {
   724     EGLBoolean status;
   725     
   726     if (!_this->egl_data) {
   727         return SDL_SetError("EGL not initialized");
   728     }
   729     
   730     status = _this->egl_data->eglSwapInterval(_this->egl_data->egl_display, interval);
   731     if (status == EGL_TRUE) {
   732         _this->egl_data->egl_swapinterval = interval;
   733         return 0;
   734     }
   735     
   736     return SDL_EGL_SetError("Unable to set the EGL swap interval", "eglSwapInterval");
   737 }
   738 
   739 int
   740 SDL_EGL_GetSwapInterval(_THIS)
   741 {
   742     if (!_this->egl_data) {
   743         SDL_SetError("EGL not initialized");
   744         return 0;
   745     }
   746     
   747     return _this->egl_data->egl_swapinterval;
   748 }
   749 
   750 int
   751 SDL_EGL_SwapBuffers(_THIS, EGLSurface egl_surface)
   752 {
   753     if (!_this->egl_data->eglSwapBuffers(_this->egl_data->egl_display, egl_surface)) {
   754         return SDL_EGL_SetError("unable to show color buffer in an OS-native window", "eglSwapBuffers");
   755     }
   756     return 0;
   757 }
   758 
   759 void
   760 SDL_EGL_DeleteContext(_THIS, SDL_GLContext context)
   761 {
   762     EGLContext egl_context = (EGLContext) context;
   763 
   764     /* Clean up GLES and EGL */
   765     if (!_this->egl_data) {
   766         return;
   767     }
   768     
   769     if (egl_context != NULL && egl_context != EGL_NO_CONTEXT) {
   770         SDL_EGL_MakeCurrent(_this, NULL, NULL);
   771         _this->egl_data->eglDestroyContext(_this->egl_data->egl_display, egl_context);
   772     }
   773         
   774 }
   775 
   776 EGLSurface *
   777 SDL_EGL_CreateSurface(_THIS, NativeWindowType nw) 
   778 {
   779     EGLSurface * surface;
   780 
   781     if (SDL_EGL_ChooseConfig(_this) != 0) {
   782         return EGL_NO_SURFACE;
   783     }
   784     
   785 #if SDL_VIDEO_DRIVER_ANDROID
   786     {
   787         /* Android docs recommend doing this!
   788          * Ref: http://developer.android.com/reference/android/app/NativeActivity.html 
   789          */
   790         EGLint format;
   791         _this->egl_data->eglGetConfigAttrib(_this->egl_data->egl_display,
   792                                             _this->egl_data->egl_config, 
   793                                             EGL_NATIVE_VISUAL_ID, &format);
   794 
   795         ANativeWindow_setBuffersGeometry(nw, 0, 0, format);
   796     }
   797 #endif    
   798     
   799     surface = _this->egl_data->eglCreateWindowSurface(
   800             _this->egl_data->egl_display,
   801             _this->egl_data->egl_config,
   802             nw, NULL);
   803     if (surface == EGL_NO_SURFACE) {
   804         SDL_EGL_SetError("unable to create an EGL window surface", "eglCreateWindowSurface");
   805     }
   806     return surface;
   807 }
   808 
   809 void
   810 SDL_EGL_DestroySurface(_THIS, EGLSurface egl_surface) 
   811 {
   812     if (!_this->egl_data) {
   813         return;
   814     }
   815     
   816     if (egl_surface != EGL_NO_SURFACE) {
   817         _this->egl_data->eglDestroySurface(_this->egl_data->egl_display, egl_surface);
   818     }
   819 }
   820 
   821 #endif /* SDL_VIDEO_OPENGL_EGL */
   822 
   823 /* vi: set ts=4 sw=4 expandtab: */
   824