src/video/SDL_egl.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 03 Dec 2017 20:25:55 -0800
changeset 11718 3006dd42e4db
parent 11677 aed6a9ff3c99
child 11719 a4477243eb20
permissions -rw-r--r--
Fixed bug 3945 - Add eglCreatePbufferSurface function

tomwardio

Proposed patch loads eglCreatePbufferSurface in same manner as other 1.1 functors. This allows custom video drivers to create pbuffer surfaces.
     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("EGL 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(eglCreatePBufferSurface);
   379     LOAD_FUNC(eglCreateWindowSurface);
   380     LOAD_FUNC(eglDestroySurface);
   381     LOAD_FUNC(eglMakeCurrent);
   382     LOAD_FUNC(eglSwapBuffers);
   383     LOAD_FUNC(eglSwapInterval);
   384     LOAD_FUNC(eglWaitNative);
   385     LOAD_FUNC(eglWaitGL);
   386     LOAD_FUNC(eglBindAPI);
   387     LOAD_FUNC(eglQueryString);
   388     LOAD_FUNC(eglGetError);
   389 
   390     if (_this->egl_data->eglQueryString) {
   391         /* EGL 1.5 allows querying for client version */
   392         const char *egl_version = _this->egl_data->eglQueryString(EGL_NO_DISPLAY, EGL_VERSION);
   393         if (egl_version != NULL) {
   394             if (SDL_sscanf(egl_version, "%d.%d", &egl_version_major, &egl_version_minor) != 2) {
   395                 egl_version_major = 0;
   396                 egl_version_minor = 0;
   397                 SDL_LogWarn(SDL_LOG_CATEGORY_VIDEO, "Could not parse EGL version string: %s", egl_version);
   398             }
   399         }
   400     }
   401 
   402     if (egl_version_major == 1 && egl_version_minor == 5) {
   403         LOAD_FUNC(eglGetPlatformDisplay);
   404     }
   405 
   406     _this->egl_data->egl_display = EGL_NO_DISPLAY;
   407 #if !defined(__WINRT__)
   408     if (platform) {
   409         if (egl_version_major == 1 && egl_version_minor == 5) {
   410             _this->egl_data->egl_display = _this->egl_data->eglGetPlatformDisplay(platform, (void *)(size_t)native_display, NULL);
   411         } else {
   412             if (SDL_EGL_HasExtension(_this, SDL_EGL_CLIENT_EXTENSION, "EGL_EXT_platform_base")) {
   413                 _this->egl_data->eglGetPlatformDisplayEXT = SDL_EGL_GetProcAddress(_this, "eglGetPlatformDisplayEXT");
   414                 if (_this->egl_data->eglGetPlatformDisplayEXT) {
   415                     _this->egl_data->egl_display = _this->egl_data->eglGetPlatformDisplayEXT(platform, (void *)(size_t)native_display, NULL);
   416                 }
   417             }
   418         }
   419     }
   420     /* Try the implementation-specific eglGetDisplay even if eglGetPlatformDisplay fails */
   421     if (_this->egl_data->egl_display == EGL_NO_DISPLAY) {
   422         _this->egl_data->egl_display = _this->egl_data->eglGetDisplay(native_display);
   423     }
   424     if (_this->egl_data->egl_display == EGL_NO_DISPLAY) {
   425         return SDL_SetError("Could not get EGL display");
   426     }
   427     
   428     if (_this->egl_data->eglInitialize(_this->egl_data->egl_display, NULL, NULL) != EGL_TRUE) {
   429         return SDL_SetError("Could not initialize EGL");
   430     }
   431 #endif
   432 
   433     if (path) {
   434         SDL_strlcpy(_this->gl_config.driver_path, path, sizeof(_this->gl_config.driver_path) - 1);
   435     } else {
   436         *_this->gl_config.driver_path = '\0';
   437     }
   438     
   439     return 0;
   440 }
   441 
   442 int
   443 SDL_EGL_ChooseConfig(_THIS) 
   444 {
   445 /* 64 seems nice. */
   446     EGLint attribs[64];
   447     EGLint found_configs = 0, value;
   448 #ifdef SDL_VIDEO_DRIVER_KMSDRM
   449     /* Intel EGL on KMS/DRM (al least) returns invalid configs that confuse the bitdiff search used */
   450     /* later in this function, so we simply use the first one when using the KMSDRM driver for now. */
   451     EGLConfig configs[1];
   452 #else
   453     /* 128 seems even nicer here */
   454     EGLConfig configs[128];
   455 #endif
   456     int i, j, best_bitdiff = -1, bitdiff;
   457    
   458     if (!_this->egl_data) {
   459         /* The EGL library wasn't loaded, SDL_GetError() should have info */
   460         return -1;
   461     }
   462   
   463     /* Get a valid EGL configuration */
   464     i = 0;
   465     attribs[i++] = EGL_RED_SIZE;
   466     attribs[i++] = _this->gl_config.red_size;
   467     attribs[i++] = EGL_GREEN_SIZE;
   468     attribs[i++] = _this->gl_config.green_size;
   469     attribs[i++] = EGL_BLUE_SIZE;
   470     attribs[i++] = _this->gl_config.blue_size;
   471     
   472     if (_this->gl_config.alpha_size) {
   473         attribs[i++] = EGL_ALPHA_SIZE;
   474         attribs[i++] = _this->gl_config.alpha_size;
   475     }
   476     
   477     if (_this->gl_config.buffer_size) {
   478         attribs[i++] = EGL_BUFFER_SIZE;
   479         attribs[i++] = _this->gl_config.buffer_size;
   480     }
   481     
   482     attribs[i++] = EGL_DEPTH_SIZE;
   483     attribs[i++] = _this->gl_config.depth_size;
   484     
   485     if (_this->gl_config.stencil_size) {
   486         attribs[i++] = EGL_STENCIL_SIZE;
   487         attribs[i++] = _this->gl_config.stencil_size;
   488     }
   489     
   490     if (_this->gl_config.multisamplebuffers) {
   491         attribs[i++] = EGL_SAMPLE_BUFFERS;
   492         attribs[i++] = _this->gl_config.multisamplebuffers;
   493     }
   494     
   495     if (_this->gl_config.multisamplesamples) {
   496         attribs[i++] = EGL_SAMPLES;
   497         attribs[i++] = _this->gl_config.multisamplesamples;
   498     }
   499 
   500     if (_this->gl_config.framebuffer_srgb_capable) {
   501 #ifdef EGL_KHR_gl_colorspace
   502         if (SDL_EGL_HasExtension(_this, SDL_EGL_DISPLAY_EXTENSION, "EGL_KHR_gl_colorspace")) {
   503             attribs[i++] = EGL_GL_COLORSPACE_KHR;
   504             attribs[i++] = EGL_GL_COLORSPACE_SRGB_KHR;
   505         } else
   506 #endif
   507         {
   508             return SDL_SetError("EGL implementation does not support sRGB system framebuffers");
   509         }
   510     }
   511 
   512     attribs[i++] = EGL_RENDERABLE_TYPE;
   513     if (_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES) {
   514 #ifdef EGL_KHR_create_context
   515         if (_this->gl_config.major_version >= 3 &&
   516             SDL_EGL_HasExtension(_this, SDL_EGL_DISPLAY_EXTENSION, "EGL_KHR_create_context")) {
   517             attribs[i++] = EGL_OPENGL_ES3_BIT_KHR;
   518         } else
   519 #endif
   520         if (_this->gl_config.major_version >= 2) {
   521             attribs[i++] = EGL_OPENGL_ES2_BIT;
   522         } else {
   523             attribs[i++] = EGL_OPENGL_ES_BIT;
   524         }
   525         _this->egl_data->eglBindAPI(EGL_OPENGL_ES_API);
   526     } else {
   527         attribs[i++] = EGL_OPENGL_BIT;
   528         _this->egl_data->eglBindAPI(EGL_OPENGL_API);
   529     }
   530 
   531     if (_this->egl_data->egl_surfacetype) {
   532         attribs[i++] = EGL_SURFACE_TYPE;
   533         attribs[i++] = _this->egl_data->egl_surfacetype;
   534     }
   535 
   536     attribs[i++] = EGL_NONE;
   537 
   538     if (_this->egl_data->eglChooseConfig(_this->egl_data->egl_display,
   539         attribs,
   540         configs, SDL_arraysize(configs),
   541         &found_configs) == EGL_FALSE ||
   542         found_configs == 0) {
   543         return SDL_EGL_SetError("Couldn't find matching EGL config", "eglChooseConfig");
   544     }
   545 
   546     /* eglChooseConfig returns a number of configurations that match or exceed the requested attribs. */
   547     /* From those, we select the one that matches our requirements more closely via a makeshift algorithm */
   548 
   549     for (i = 0; i < found_configs; i++ ) {
   550         bitdiff = 0;
   551         for (j = 0; j < SDL_arraysize(attribs) - 1; j += 2) {
   552             if (attribs[j] == EGL_NONE) {
   553                break;
   554             }
   555             
   556             if ( attribs[j+1] != EGL_DONT_CARE && (
   557                 attribs[j] == EGL_RED_SIZE ||
   558                 attribs[j] == EGL_GREEN_SIZE ||
   559                 attribs[j] == EGL_BLUE_SIZE ||
   560                 attribs[j] == EGL_ALPHA_SIZE ||
   561                 attribs[j] == EGL_DEPTH_SIZE ||
   562                 attribs[j] == EGL_STENCIL_SIZE)) {
   563                 _this->egl_data->eglGetConfigAttrib(_this->egl_data->egl_display, configs[i], attribs[j], &value);
   564                 bitdiff += value - attribs[j + 1]; /* value is always >= attrib */
   565             }
   566         }
   567 
   568         if (bitdiff < best_bitdiff || best_bitdiff == -1) {
   569             _this->egl_data->egl_config = configs[i];
   570             
   571             best_bitdiff = bitdiff;
   572         }
   573 
   574         if (bitdiff == 0) {
   575             break; /* we found an exact match! */
   576         }
   577     }
   578     
   579     return 0;
   580 }
   581 
   582 SDL_GLContext
   583 SDL_EGL_CreateContext(_THIS, EGLSurface egl_surface)
   584 {
   585     /* max 14 values plus terminator. */
   586     EGLint attribs[15];
   587     int attr = 0;
   588 
   589     EGLContext egl_context, share_context = EGL_NO_CONTEXT;
   590     EGLint profile_mask = _this->gl_config.profile_mask;
   591     EGLint major_version = _this->gl_config.major_version;
   592     EGLint minor_version = _this->gl_config.minor_version;
   593     SDL_bool profile_es = (profile_mask == SDL_GL_CONTEXT_PROFILE_ES);
   594 
   595     if (!_this->egl_data) {
   596         /* The EGL library wasn't loaded, SDL_GetError() should have info */
   597         return NULL;
   598     }
   599 
   600     if (_this->gl_config.share_with_current_context) {
   601         share_context = (EGLContext)SDL_GL_GetCurrentContext();
   602     }
   603 
   604     /* Set the context version and other attributes. */
   605     if ((major_version < 3 || (minor_version == 0 && profile_es)) &&
   606         _this->gl_config.flags == 0 &&
   607         (profile_mask == 0 || profile_es)) {
   608         /* Create a context without using EGL_KHR_create_context attribs.
   609          * When creating a GLES context without EGL_KHR_create_context we can
   610          * only specify the major version. When creating a desktop GL context
   611          * we can't specify any version, so we only try in that case when the
   612          * version is less than 3.0 (matches SDL's GLX/WGL behavior.)
   613          */
   614         if (profile_es) {
   615             attribs[attr++] = EGL_CONTEXT_CLIENT_VERSION;
   616             attribs[attr++] = SDL_max(major_version, 1);
   617         }
   618     } else {
   619 #ifdef EGL_KHR_create_context
   620         /* The Major/minor version, context profiles, and context flags can
   621          * only be specified when this extension is available.
   622          */
   623         if (SDL_EGL_HasExtension(_this, SDL_EGL_DISPLAY_EXTENSION, "EGL_KHR_create_context")) {
   624             attribs[attr++] = EGL_CONTEXT_MAJOR_VERSION_KHR;
   625             attribs[attr++] = major_version;
   626             attribs[attr++] = EGL_CONTEXT_MINOR_VERSION_KHR;
   627             attribs[attr++] = minor_version;
   628 
   629             /* SDL profile bits match EGL profile bits. */
   630             if (profile_mask != 0 && profile_mask != SDL_GL_CONTEXT_PROFILE_ES) {
   631                 attribs[attr++] = EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR;
   632                 attribs[attr++] = profile_mask;
   633             }
   634 
   635             /* SDL flags match EGL flags. */
   636             if (_this->gl_config.flags != 0) {
   637                 attribs[attr++] = EGL_CONTEXT_FLAGS_KHR;
   638                 attribs[attr++] = _this->gl_config.flags;
   639             }
   640         } else
   641 #endif /* EGL_KHR_create_context */
   642         {
   643             SDL_SetError("Could not create EGL context (context attributes are not supported)");
   644             return NULL;
   645         }
   646     }
   647 
   648     if (_this->gl_config.no_error) {
   649 #ifdef EGL_KHR_create_context_no_error
   650         if (SDL_EGL_HasExtension(_this, SDL_EGL_DISPLAY_EXTENSION, "EGL_KHR_create_context_no_error")) {
   651             attribs[attr++] = EGL_CONTEXT_OPENGL_NO_ERROR_KHR;
   652             attribs[attr++] = _this->gl_config.no_error;
   653         } else
   654 #endif
   655         {
   656             SDL_SetError("EGL implementation does not support no_error contexts");
   657             return NULL;
   658         }
   659     }
   660 
   661     attribs[attr++] = EGL_NONE;
   662 
   663     /* Bind the API */
   664     if (profile_es) {
   665         _this->egl_data->eglBindAPI(EGL_OPENGL_ES_API);
   666     } else {
   667         _this->egl_data->eglBindAPI(EGL_OPENGL_API);
   668     }
   669 
   670     egl_context = _this->egl_data->eglCreateContext(_this->egl_data->egl_display,
   671                                       _this->egl_data->egl_config,
   672                                       share_context, attribs);
   673 
   674     if (egl_context == EGL_NO_CONTEXT) {
   675         SDL_EGL_SetError("Could not create EGL context", "eglCreateContext");
   676         return NULL;
   677     }
   678 
   679     _this->egl_data->egl_swapinterval = 0;
   680 
   681     if (SDL_EGL_MakeCurrent(_this, egl_surface, egl_context) < 0) {
   682         /* Save the SDL error set by SDL_EGL_MakeCurrent */
   683         char errorText[1024];
   684         SDL_strlcpy(errorText, SDL_GetError(), SDL_arraysize(errorText));
   685 
   686         /* Delete the context, which may alter the value returned by SDL_GetError() */
   687         SDL_EGL_DeleteContext(_this, egl_context);
   688 
   689         /* Restore the SDL error */
   690         SDL_SetError("%s", errorText);
   691 
   692         return NULL;
   693     }
   694 
   695     return (SDL_GLContext) egl_context;
   696 }
   697 
   698 int
   699 SDL_EGL_MakeCurrent(_THIS, EGLSurface egl_surface, SDL_GLContext context)
   700 {
   701     EGLContext egl_context = (EGLContext) context;
   702 
   703     if (!_this->egl_data) {
   704         return SDL_SetError("OpenGL not initialized");
   705     }
   706     
   707     /* The android emulator crashes badly if you try to eglMakeCurrent 
   708      * with a valid context and invalid surface, so we have to check for both here.
   709      */
   710     if (!egl_context || !egl_surface) {
   711          _this->egl_data->eglMakeCurrent(_this->egl_data->egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
   712     } else {
   713         if (!_this->egl_data->eglMakeCurrent(_this->egl_data->egl_display,
   714             egl_surface, egl_surface, egl_context)) {
   715             return SDL_EGL_SetError("Unable to make EGL context current", "eglMakeCurrent");
   716         }
   717     }
   718       
   719     return 0;
   720 }
   721 
   722 int
   723 SDL_EGL_SetSwapInterval(_THIS, int interval)
   724 {
   725     EGLBoolean status;
   726     
   727     if (!_this->egl_data) {
   728         return SDL_SetError("EGL not initialized");
   729     }
   730     
   731     status = _this->egl_data->eglSwapInterval(_this->egl_data->egl_display, interval);
   732     if (status == EGL_TRUE) {
   733         _this->egl_data->egl_swapinterval = interval;
   734         return 0;
   735     }
   736     
   737     return SDL_EGL_SetError("Unable to set the EGL swap interval", "eglSwapInterval");
   738 }
   739 
   740 int
   741 SDL_EGL_GetSwapInterval(_THIS)
   742 {
   743     if (!_this->egl_data) {
   744         SDL_SetError("EGL not initialized");
   745         return 0;
   746     }
   747     
   748     return _this->egl_data->egl_swapinterval;
   749 }
   750 
   751 int
   752 SDL_EGL_SwapBuffers(_THIS, EGLSurface egl_surface)
   753 {
   754     if (!_this->egl_data->eglSwapBuffers(_this->egl_data->egl_display, egl_surface)) {
   755         return SDL_EGL_SetError("unable to show color buffer in an OS-native window", "eglSwapBuffers");
   756     }
   757     return 0;
   758 }
   759 
   760 void
   761 SDL_EGL_DeleteContext(_THIS, SDL_GLContext context)
   762 {
   763     EGLContext egl_context = (EGLContext) context;
   764 
   765     /* Clean up GLES and EGL */
   766     if (!_this->egl_data) {
   767         return;
   768     }
   769     
   770     if (egl_context != NULL && egl_context != EGL_NO_CONTEXT) {
   771         SDL_EGL_MakeCurrent(_this, NULL, NULL);
   772         _this->egl_data->eglDestroyContext(_this->egl_data->egl_display, egl_context);
   773     }
   774         
   775 }
   776 
   777 EGLSurface *
   778 SDL_EGL_CreateSurface(_THIS, NativeWindowType nw) 
   779 {
   780     EGLSurface * surface;
   781 
   782     if (SDL_EGL_ChooseConfig(_this) != 0) {
   783         return EGL_NO_SURFACE;
   784     }
   785     
   786 #if SDL_VIDEO_DRIVER_ANDROID
   787     {
   788         /* Android docs recommend doing this!
   789          * Ref: http://developer.android.com/reference/android/app/NativeActivity.html 
   790          */
   791         EGLint format;
   792         _this->egl_data->eglGetConfigAttrib(_this->egl_data->egl_display,
   793                                             _this->egl_data->egl_config, 
   794                                             EGL_NATIVE_VISUAL_ID, &format);
   795 
   796         ANativeWindow_setBuffersGeometry(nw, 0, 0, format);
   797     }
   798 #endif    
   799     
   800     surface = _this->egl_data->eglCreateWindowSurface(
   801             _this->egl_data->egl_display,
   802             _this->egl_data->egl_config,
   803             nw, NULL);
   804     if (surface == EGL_NO_SURFACE) {
   805         SDL_EGL_SetError("unable to create an EGL window surface", "eglCreateWindowSurface");
   806     }
   807     return surface;
   808 }
   809 
   810 void
   811 SDL_EGL_DestroySurface(_THIS, EGLSurface egl_surface) 
   812 {
   813     if (!_this->egl_data) {
   814         return;
   815     }
   816     
   817     if (egl_surface != EGL_NO_SURFACE) {
   818         _this->egl_data->eglDestroySurface(_this->egl_data->egl_display, egl_surface);
   819     }
   820 }
   821 
   822 #endif /* SDL_VIDEO_OPENGL_EGL */
   823 
   824 /* vi: set ts=4 sw=4 expandtab: */
   825