src/video/SDL_egl.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 10 Feb 2017 09:41:16 -0800
changeset 10878 7fa08228d9e3
parent 10741 a32fd6b7412b
child 11175 cbc6a8a5b701
permissions -rw-r--r--
Fixed warning in Android build
     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_egl_c.h"
    34 #include "SDL_loadso.h"
    35 #include "SDL_hints.h"
    36 
    37 #ifdef EGL_KHR_create_context
    38 /* EGL_OPENGL_ES3_BIT_KHR was added in version 13 of the extension. */
    39 #ifndef EGL_OPENGL_ES3_BIT_KHR
    40 #define EGL_OPENGL_ES3_BIT_KHR 0x00000040
    41 #endif
    42 #endif /* EGL_KHR_create_context */
    43 
    44 #if SDL_VIDEO_DRIVER_RPI
    45 /* Raspbian places the OpenGL ES/EGL binaries in a non standard path */
    46 #define DEFAULT_EGL "/opt/vc/lib/libEGL.so"
    47 #define DEFAULT_OGL_ES2 "/opt/vc/lib/libGLESv2.so"
    48 #define DEFAULT_OGL_ES_PVR "/opt/vc/lib/libGLES_CM.so"
    49 #define DEFAULT_OGL_ES "/opt/vc/lib/libGLESv1_CM.so"
    50 
    51 #elif SDL_VIDEO_DRIVER_ANDROID || SDL_VIDEO_DRIVER_VIVANTE
    52 /* Android */
    53 #define DEFAULT_EGL "libEGL.so"
    54 #define DEFAULT_OGL_ES2 "libGLESv2.so"
    55 #define DEFAULT_OGL_ES_PVR "libGLES_CM.so"
    56 #define DEFAULT_OGL_ES "libGLESv1_CM.so"
    57 
    58 #elif SDL_VIDEO_DRIVER_WINDOWS || SDL_VIDEO_DRIVER_WINRT
    59 /* EGL AND OpenGL ES support via ANGLE */
    60 #define DEFAULT_EGL "libEGL.dll"
    61 #define DEFAULT_OGL_ES2 "libGLESv2.dll"
    62 #define DEFAULT_OGL_ES_PVR "libGLES_CM.dll"
    63 #define DEFAULT_OGL_ES "libGLESv1_CM.dll"
    64 
    65 #else
    66 /* Desktop Linux */
    67 #define DEFAULT_OGL "libGL.so.1"
    68 #define DEFAULT_EGL "libEGL.so.1"
    69 #define DEFAULT_OGL_ES2 "libGLESv2.so.2"
    70 #define DEFAULT_OGL_ES_PVR "libGLES_CM.so.1"
    71 #define DEFAULT_OGL_ES "libGLESv1_CM.so.1"
    72 #endif /* SDL_VIDEO_DRIVER_RPI */
    73 
    74 #define LOAD_FUNC(NAME) \
    75 _this->egl_data->NAME = SDL_LoadFunction(_this->egl_data->dll_handle, #NAME); \
    76 if (!_this->egl_data->NAME) \
    77 { \
    78     return SDL_SetError("Could not retrieve EGL function " #NAME); \
    79 }
    80 
    81 static const char * SDL_EGL_GetErrorName(EGLint eglErrorCode)
    82 {
    83 #define SDL_EGL_ERROR_TRANSLATE(e) case e: return #e;
    84     switch (eglErrorCode) {
    85         SDL_EGL_ERROR_TRANSLATE(EGL_SUCCESS);
    86         SDL_EGL_ERROR_TRANSLATE(EGL_NOT_INITIALIZED);
    87         SDL_EGL_ERROR_TRANSLATE(EGL_BAD_ACCESS);
    88         SDL_EGL_ERROR_TRANSLATE(EGL_BAD_ALLOC);
    89         SDL_EGL_ERROR_TRANSLATE(EGL_BAD_ATTRIBUTE);
    90         SDL_EGL_ERROR_TRANSLATE(EGL_BAD_CONTEXT);
    91         SDL_EGL_ERROR_TRANSLATE(EGL_BAD_CONFIG);
    92         SDL_EGL_ERROR_TRANSLATE(EGL_BAD_CURRENT_SURFACE);
    93         SDL_EGL_ERROR_TRANSLATE(EGL_BAD_DISPLAY);
    94         SDL_EGL_ERROR_TRANSLATE(EGL_BAD_SURFACE);
    95         SDL_EGL_ERROR_TRANSLATE(EGL_BAD_MATCH);
    96         SDL_EGL_ERROR_TRANSLATE(EGL_BAD_PARAMETER);
    97         SDL_EGL_ERROR_TRANSLATE(EGL_BAD_NATIVE_PIXMAP);
    98         SDL_EGL_ERROR_TRANSLATE(EGL_BAD_NATIVE_WINDOW);
    99         SDL_EGL_ERROR_TRANSLATE(EGL_CONTEXT_LOST);
   100     }
   101     return "";
   102 }
   103 
   104 int SDL_EGL_SetErrorEx(const char * message, const char * eglFunctionName, EGLint eglErrorCode)
   105 {
   106     const char * errorText = SDL_EGL_GetErrorName(eglErrorCode);
   107     char altErrorText[32];
   108     if (errorText[0] == '\0') {
   109         /* An unknown-to-SDL error code was reported.  Report its hexadecimal value, instead of its name. */
   110         SDL_snprintf(altErrorText, SDL_arraysize(altErrorText), "0x%x", (unsigned int)eglErrorCode);
   111         errorText = altErrorText;
   112     }
   113     return SDL_SetError("%s (call to %s failed, reporting an error of %s)", message, eglFunctionName, errorText);
   114 }
   115 
   116 /* EGL implementation of SDL OpenGL ES support */
   117 #ifdef EGL_KHR_create_context        
   118 static int SDL_EGL_HasExtension(_THIS, const char *ext)
   119 {
   120     int i;
   121     int len = 0;
   122     size_t ext_len;
   123     const char *exts;
   124     const char *ext_word;
   125 
   126     ext_len = SDL_strlen(ext);
   127     exts = _this->egl_data->eglQueryString(_this->egl_data->egl_display, EGL_EXTENSIONS);
   128 
   129     if (exts) {
   130         ext_word = exts;
   131 
   132         for (i = 0; exts[i] != 0; i++) {
   133             if (exts[i] == ' ') {
   134                 if (ext_len == len && !SDL_strncmp(ext_word, ext, len)) {
   135                     return 1;
   136                 }
   137 
   138                 len = 0;
   139                 ext_word = &exts[i + 1];
   140             } else {
   141                 len++;
   142             }
   143         }
   144     }
   145 
   146     return 0;
   147 }
   148 #endif /* EGL_KHR_create_context */
   149 
   150 void *
   151 SDL_EGL_GetProcAddress(_THIS, const char *proc)
   152 {
   153     static char procname[1024];
   154     void *retval;
   155     
   156     /* eglGetProcAddress is busted on Android http://code.google.com/p/android/issues/detail?id=7681 */
   157 #if !defined(SDL_VIDEO_DRIVER_ANDROID) && !defined(SDL_VIDEO_DRIVER_MIR) 
   158     if (_this->egl_data->eglGetProcAddress) {
   159         retval = _this->egl_data->eglGetProcAddress(proc);
   160         if (retval) {
   161             return retval;
   162         }
   163     }
   164 #endif
   165     
   166     retval = SDL_LoadFunction(_this->egl_data->egl_dll_handle, proc);
   167     if (!retval && SDL_strlen(proc) <= 1022) {
   168         procname[0] = '_';
   169         SDL_strlcpy(procname + 1, proc, 1022);
   170         retval = SDL_LoadFunction(_this->egl_data->egl_dll_handle, procname);
   171     }
   172     return retval;
   173 }
   174 
   175 void
   176 SDL_EGL_UnloadLibrary(_THIS)
   177 {
   178     if (_this->egl_data) {
   179         if (_this->egl_data->egl_display) {
   180             _this->egl_data->eglTerminate(_this->egl_data->egl_display);
   181             _this->egl_data->egl_display = NULL;
   182         }
   183 
   184         if (_this->egl_data->dll_handle) {
   185             SDL_UnloadObject(_this->egl_data->dll_handle);
   186             _this->egl_data->dll_handle = NULL;
   187         }
   188         if (_this->egl_data->egl_dll_handle) {
   189             SDL_UnloadObject(_this->egl_data->egl_dll_handle);
   190             _this->egl_data->egl_dll_handle = NULL;
   191         }
   192         
   193         SDL_free(_this->egl_data);
   194         _this->egl_data = NULL;
   195     }
   196 }
   197 
   198 int
   199 SDL_EGL_LoadLibrary(_THIS, const char *egl_path, NativeDisplayType native_display)
   200 {
   201     void *dll_handle = NULL, *egl_dll_handle = NULL; /* The naming is counter intuitive, but hey, I just work here -- Gabriel */
   202     const char *path = NULL;
   203 #if SDL_VIDEO_DRIVER_WINDOWS || SDL_VIDEO_DRIVER_WINRT
   204     const char *d3dcompiler;
   205 #endif
   206 
   207     if (_this->egl_data) {
   208         return SDL_SetError("OpenGL ES context already created");
   209     }
   210 
   211     _this->egl_data = (struct SDL_EGL_VideoData *) SDL_calloc(1, sizeof(SDL_EGL_VideoData));
   212     if (!_this->egl_data) {
   213         return SDL_OutOfMemory();
   214     }
   215 
   216 #if SDL_VIDEO_DRIVER_WINDOWS || SDL_VIDEO_DRIVER_WINRT
   217     d3dcompiler = SDL_GetHint(SDL_HINT_VIDEO_WIN_D3DCOMPILER);
   218     if (!d3dcompiler) {
   219         if (WIN_IsWindowsVistaOrGreater()) {
   220             d3dcompiler = "d3dcompiler_46.dll";
   221         } else {
   222             d3dcompiler = "d3dcompiler_43.dll";
   223         }
   224     }
   225     if (SDL_strcasecmp(d3dcompiler, "none") != 0) {
   226         SDL_LoadObject(d3dcompiler);
   227     }
   228 #endif
   229 
   230     /* A funny thing, loading EGL.so first does not work on the Raspberry, so we load libGL* first */
   231     path = SDL_getenv("SDL_VIDEO_GL_DRIVER");
   232     if (path != NULL) {
   233         egl_dll_handle = SDL_LoadObject(path);
   234     }
   235 
   236     if (egl_dll_handle == NULL) {
   237         if (_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES) {
   238             if (_this->gl_config.major_version > 1) {
   239                 path = DEFAULT_OGL_ES2;
   240                 egl_dll_handle = SDL_LoadObject(path);
   241             } else {
   242                 path = DEFAULT_OGL_ES;
   243                 egl_dll_handle = SDL_LoadObject(path);
   244                 if (egl_dll_handle == NULL) {
   245                     path = DEFAULT_OGL_ES_PVR;
   246                     egl_dll_handle = SDL_LoadObject(path);
   247                 }
   248             }
   249         }
   250 #ifdef DEFAULT_OGL         
   251         else {
   252             path = DEFAULT_OGL;
   253             egl_dll_handle = SDL_LoadObject(path);
   254         }
   255 #endif        
   256     }
   257     _this->egl_data->egl_dll_handle = egl_dll_handle;
   258 
   259     if (egl_dll_handle == NULL) {
   260         return SDL_SetError("Could not initialize OpenGL / GLES library");
   261     }
   262 
   263     /* Loading libGL* in the previous step took care of loading libEGL.so, but we future proof by double checking */
   264     if (egl_path != NULL) {
   265         dll_handle = SDL_LoadObject(egl_path);
   266     }   
   267     /* Try loading a EGL symbol, if it does not work try the default library paths */
   268     if (dll_handle == NULL || SDL_LoadFunction(dll_handle, "eglChooseConfig") == NULL) {
   269         if (dll_handle != NULL) {
   270             SDL_UnloadObject(dll_handle);
   271         }
   272         path = SDL_getenv("SDL_VIDEO_EGL_DRIVER");
   273         if (path == NULL) {
   274             path = DEFAULT_EGL;
   275         }
   276         dll_handle = SDL_LoadObject(path);
   277         if (dll_handle == NULL || SDL_LoadFunction(dll_handle, "eglChooseConfig") == NULL) {
   278             if (dll_handle != NULL) {
   279                 SDL_UnloadObject(dll_handle);
   280             }
   281             return SDL_SetError("Could not load EGL library");
   282         }
   283         SDL_ClearError();
   284     }
   285 
   286     _this->egl_data->dll_handle = dll_handle;
   287 
   288     /* Load new function pointers */
   289     LOAD_FUNC(eglGetDisplay);
   290     LOAD_FUNC(eglInitialize);
   291     LOAD_FUNC(eglTerminate);
   292     LOAD_FUNC(eglGetProcAddress);
   293     LOAD_FUNC(eglChooseConfig);
   294     LOAD_FUNC(eglGetConfigAttrib);
   295     LOAD_FUNC(eglCreateContext);
   296     LOAD_FUNC(eglDestroyContext);
   297     LOAD_FUNC(eglCreateWindowSurface);
   298     LOAD_FUNC(eglDestroySurface);
   299     LOAD_FUNC(eglMakeCurrent);
   300     LOAD_FUNC(eglSwapBuffers);
   301     LOAD_FUNC(eglSwapInterval);
   302     LOAD_FUNC(eglWaitNative);
   303     LOAD_FUNC(eglWaitGL);
   304     LOAD_FUNC(eglBindAPI);
   305     LOAD_FUNC(eglQueryString);
   306     LOAD_FUNC(eglGetError);
   307 
   308 #if !defined(__WINRT__)
   309     _this->egl_data->egl_display = _this->egl_data->eglGetDisplay(native_display);
   310     if (!_this->egl_data->egl_display) {
   311         return SDL_SetError("Could not get EGL display");
   312     }
   313     
   314     if (_this->egl_data->eglInitialize(_this->egl_data->egl_display, NULL, NULL) != EGL_TRUE) {
   315         return SDL_SetError("Could not initialize EGL");
   316     }
   317 #endif
   318 
   319     if (path) {
   320         SDL_strlcpy(_this->gl_config.driver_path, path, sizeof(_this->gl_config.driver_path) - 1);
   321     } else {
   322         *_this->gl_config.driver_path = '\0';
   323     }
   324     
   325     return 0;
   326 }
   327 
   328 int
   329 SDL_EGL_ChooseConfig(_THIS) 
   330 {
   331     /* 64 seems nice. */
   332     EGLint attribs[64];
   333     EGLint found_configs = 0, value;
   334     /* 128 seems even nicer here */
   335     EGLConfig configs[128];
   336     int i, j, best_bitdiff = -1, bitdiff;
   337     
   338     if (!_this->egl_data) {
   339         /* The EGL library wasn't loaded, SDL_GetError() should have info */
   340         return -1;
   341     }
   342   
   343     /* Get a valid EGL configuration */
   344     i = 0;
   345     attribs[i++] = EGL_RED_SIZE;
   346     attribs[i++] = _this->gl_config.red_size;
   347     attribs[i++] = EGL_GREEN_SIZE;
   348     attribs[i++] = _this->gl_config.green_size;
   349     attribs[i++] = EGL_BLUE_SIZE;
   350     attribs[i++] = _this->gl_config.blue_size;
   351     
   352     if (_this->gl_config.alpha_size) {
   353         attribs[i++] = EGL_ALPHA_SIZE;
   354         attribs[i++] = _this->gl_config.alpha_size;
   355     }
   356     
   357     if (_this->gl_config.buffer_size) {
   358         attribs[i++] = EGL_BUFFER_SIZE;
   359         attribs[i++] = _this->gl_config.buffer_size;
   360     }
   361     
   362     attribs[i++] = EGL_DEPTH_SIZE;
   363     attribs[i++] = _this->gl_config.depth_size;
   364     
   365     if (_this->gl_config.stencil_size) {
   366         attribs[i++] = EGL_STENCIL_SIZE;
   367         attribs[i++] = _this->gl_config.stencil_size;
   368     }
   369     
   370     if (_this->gl_config.multisamplebuffers) {
   371         attribs[i++] = EGL_SAMPLE_BUFFERS;
   372         attribs[i++] = _this->gl_config.multisamplebuffers;
   373     }
   374     
   375     if (_this->gl_config.multisamplesamples) {
   376         attribs[i++] = EGL_SAMPLES;
   377         attribs[i++] = _this->gl_config.multisamplesamples;
   378     }
   379 
   380     if (_this->gl_config.framebuffer_srgb_capable) {
   381 #ifdef EGL_KHR_gl_colorspace
   382         if (SDL_EGL_HasExtension(_this, "EGL_KHR_gl_colorspace")) {
   383             attribs[i++] = EGL_GL_COLORSPACE_KHR;
   384             attribs[i++] = EGL_GL_COLORSPACE_SRGB_KHR;
   385         } else
   386 #endif
   387         {
   388             return SDL_SetError("EGL implementation does not support sRGB system framebuffers");
   389         }
   390     }
   391 
   392     attribs[i++] = EGL_RENDERABLE_TYPE;
   393     if (_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES) {
   394 #ifdef EGL_KHR_create_context
   395         if (_this->gl_config.major_version >= 3 &&
   396             SDL_EGL_HasExtension(_this, "EGL_KHR_create_context")) {
   397             attribs[i++] = EGL_OPENGL_ES3_BIT_KHR;
   398         } else
   399 #endif
   400         if (_this->gl_config.major_version >= 2) {
   401             attribs[i++] = EGL_OPENGL_ES2_BIT;
   402         } else {
   403             attribs[i++] = EGL_OPENGL_ES_BIT;
   404         }
   405         _this->egl_data->eglBindAPI(EGL_OPENGL_ES_API);
   406     } else {
   407         attribs[i++] = EGL_OPENGL_BIT;
   408         _this->egl_data->eglBindAPI(EGL_OPENGL_API);
   409     }
   410 
   411     attribs[i++] = EGL_NONE;
   412 
   413     if (_this->egl_data->eglChooseConfig(_this->egl_data->egl_display,
   414         attribs,
   415         configs, SDL_arraysize(configs),
   416         &found_configs) == EGL_FALSE ||
   417         found_configs == 0) {
   418         return SDL_EGL_SetError("Couldn't find matching EGL config", "eglChooseConfig");
   419     }
   420 
   421     /* eglChooseConfig returns a number of configurations that match or exceed the requested attribs. */
   422     /* From those, we select the one that matches our requirements more closely via a makeshift algorithm */
   423 
   424     for (i = 0; i < found_configs; i++ ) {
   425         bitdiff = 0;
   426         for (j = 0; j < SDL_arraysize(attribs) - 1; j += 2) {
   427             if (attribs[j] == EGL_NONE) {
   428                break;
   429             }
   430             
   431             if ( attribs[j+1] != EGL_DONT_CARE && (
   432                 attribs[j] == EGL_RED_SIZE ||
   433                 attribs[j] == EGL_GREEN_SIZE ||
   434                 attribs[j] == EGL_BLUE_SIZE ||
   435                 attribs[j] == EGL_ALPHA_SIZE ||
   436                 attribs[j] == EGL_DEPTH_SIZE ||
   437                 attribs[j] == EGL_STENCIL_SIZE)) {
   438                 _this->egl_data->eglGetConfigAttrib(_this->egl_data->egl_display, configs[i], attribs[j], &value);
   439                 bitdiff += value - attribs[j + 1]; /* value is always >= attrib */
   440             }
   441         }
   442 
   443         if (bitdiff < best_bitdiff || best_bitdiff == -1) {
   444             _this->egl_data->egl_config = configs[i];
   445             
   446             best_bitdiff = bitdiff;
   447         }
   448 
   449         if (bitdiff == 0) {
   450             break; /* we found an exact match! */
   451         }
   452     }
   453     
   454     return 0;
   455 }
   456 
   457 SDL_GLContext
   458 SDL_EGL_CreateContext(_THIS, EGLSurface egl_surface)
   459 {
   460     /* max 14 values plus terminator. */
   461     EGLint attribs[15];
   462     int attr = 0;
   463 
   464     EGLContext egl_context, share_context = EGL_NO_CONTEXT;
   465     EGLint profile_mask = _this->gl_config.profile_mask;
   466     EGLint major_version = _this->gl_config.major_version;
   467     EGLint minor_version = _this->gl_config.minor_version;
   468     SDL_bool profile_es = (profile_mask == SDL_GL_CONTEXT_PROFILE_ES);
   469 
   470     if (!_this->egl_data) {
   471         /* The EGL library wasn't loaded, SDL_GetError() should have info */
   472         return NULL;
   473     }
   474 
   475     if (_this->gl_config.share_with_current_context) {
   476         share_context = (EGLContext)SDL_GL_GetCurrentContext();
   477     }
   478 
   479     /* Set the context version and other attributes. */
   480     if ((major_version < 3 || (minor_version == 0 && profile_es)) &&
   481         _this->gl_config.flags == 0 &&
   482         (profile_mask == 0 || profile_es)) {
   483         /* Create a context without using EGL_KHR_create_context attribs.
   484          * When creating a GLES context without EGL_KHR_create_context we can
   485          * only specify the major version. When creating a desktop GL context
   486          * we can't specify any version, so we only try in that case when the
   487          * version is less than 3.0 (matches SDL's GLX/WGL behavior.)
   488          */
   489         if (profile_es) {
   490             attribs[attr++] = EGL_CONTEXT_CLIENT_VERSION;
   491             attribs[attr++] = SDL_max(major_version, 1);
   492         }
   493     } else {
   494 #ifdef EGL_KHR_create_context
   495         /* The Major/minor version, context profiles, and context flags can
   496          * only be specified when this extension is available.
   497          */
   498         if (SDL_EGL_HasExtension(_this, "EGL_KHR_create_context")) {
   499             attribs[attr++] = EGL_CONTEXT_MAJOR_VERSION_KHR;
   500             attribs[attr++] = major_version;
   501             attribs[attr++] = EGL_CONTEXT_MINOR_VERSION_KHR;
   502             attribs[attr++] = minor_version;
   503 
   504             /* SDL profile bits match EGL profile bits. */
   505             if (profile_mask != 0 && profile_mask != SDL_GL_CONTEXT_PROFILE_ES) {
   506                 attribs[attr++] = EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR;
   507                 attribs[attr++] = profile_mask;
   508             }
   509 
   510             /* SDL flags match EGL flags. */
   511             if (_this->gl_config.flags != 0) {
   512                 attribs[attr++] = EGL_CONTEXT_FLAGS_KHR;
   513                 attribs[attr++] = _this->gl_config.flags;
   514             }
   515         } else
   516 #endif /* EGL_KHR_create_context */
   517         {
   518             SDL_SetError("Could not create EGL context (context attributes are not supported)");
   519             return NULL;
   520         }
   521     }
   522 
   523     attribs[attr++] = EGL_NONE;
   524 
   525     /* Bind the API */
   526     if (profile_es) {
   527         _this->egl_data->eglBindAPI(EGL_OPENGL_ES_API);
   528     } else {
   529         _this->egl_data->eglBindAPI(EGL_OPENGL_API);
   530     }
   531 
   532     egl_context = _this->egl_data->eglCreateContext(_this->egl_data->egl_display,
   533                                       _this->egl_data->egl_config,
   534                                       share_context, attribs);
   535 
   536     if (egl_context == EGL_NO_CONTEXT) {
   537         SDL_EGL_SetError("Could not create EGL context", "eglCreateContext");
   538         return NULL;
   539     }
   540 
   541     _this->egl_data->egl_swapinterval = 0;
   542 
   543     if (SDL_EGL_MakeCurrent(_this, egl_surface, egl_context) < 0) {
   544         /* Save the SDL error set by SDL_EGL_MakeCurrent */
   545         char errorText[1024];
   546         SDL_strlcpy(errorText, SDL_GetError(), SDL_arraysize(errorText));
   547 
   548         /* Delete the context, which may alter the value returned by SDL_GetError() */
   549         SDL_EGL_DeleteContext(_this, egl_context);
   550 
   551         /* Restore the SDL error */
   552         SDL_SetError("%s", errorText);
   553 
   554         return NULL;
   555     }
   556 
   557     return (SDL_GLContext) egl_context;
   558 }
   559 
   560 int
   561 SDL_EGL_MakeCurrent(_THIS, EGLSurface egl_surface, SDL_GLContext context)
   562 {
   563     EGLContext egl_context = (EGLContext) context;
   564 
   565     if (!_this->egl_data) {
   566         return SDL_SetError("OpenGL not initialized");
   567     }
   568     
   569     /* The android emulator crashes badly if you try to eglMakeCurrent 
   570      * with a valid context and invalid surface, so we have to check for both here.
   571      */
   572     if (!egl_context || !egl_surface) {
   573          _this->egl_data->eglMakeCurrent(_this->egl_data->egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
   574     } else {
   575         if (!_this->egl_data->eglMakeCurrent(_this->egl_data->egl_display,
   576             egl_surface, egl_surface, egl_context)) {
   577             return SDL_EGL_SetError("Unable to make EGL context current", "eglMakeCurrent");
   578         }
   579     }
   580       
   581     return 0;
   582 }
   583 
   584 int
   585 SDL_EGL_SetSwapInterval(_THIS, int interval)
   586 {
   587     EGLBoolean status;
   588     
   589     if (!_this->egl_data) {
   590         return SDL_SetError("EGL not initialized");
   591     }
   592     
   593     status = _this->egl_data->eglSwapInterval(_this->egl_data->egl_display, interval);
   594     if (status == EGL_TRUE) {
   595         _this->egl_data->egl_swapinterval = interval;
   596         return 0;
   597     }
   598     
   599     return SDL_EGL_SetError("Unable to set the EGL swap interval", "eglSwapInterval");
   600 }
   601 
   602 int
   603 SDL_EGL_GetSwapInterval(_THIS)
   604 {
   605     if (!_this->egl_data) {
   606         SDL_SetError("EGL not initialized");
   607         return 0;
   608     }
   609     
   610     return _this->egl_data->egl_swapinterval;
   611 }
   612 
   613 int
   614 SDL_EGL_SwapBuffers(_THIS, EGLSurface egl_surface)
   615 {
   616     if (!_this->egl_data->eglSwapBuffers(_this->egl_data->egl_display, egl_surface)) {
   617         return SDL_EGL_SetError("unable to show color buffer in an OS-native window", "eglSwapBuffers");
   618     }
   619     return 0;
   620 }
   621 
   622 void
   623 SDL_EGL_DeleteContext(_THIS, SDL_GLContext context)
   624 {
   625     EGLContext egl_context = (EGLContext) context;
   626 
   627     /* Clean up GLES and EGL */
   628     if (!_this->egl_data) {
   629         return;
   630     }
   631     
   632     if (egl_context != NULL && egl_context != EGL_NO_CONTEXT) {
   633         SDL_EGL_MakeCurrent(_this, NULL, NULL);
   634         _this->egl_data->eglDestroyContext(_this->egl_data->egl_display, egl_context);
   635     }
   636         
   637 }
   638 
   639 EGLSurface *
   640 SDL_EGL_CreateSurface(_THIS, NativeWindowType nw) 
   641 {
   642     EGLSurface * surface;
   643 
   644     if (SDL_EGL_ChooseConfig(_this) != 0) {
   645         return EGL_NO_SURFACE;
   646     }
   647     
   648 #if SDL_VIDEO_DRIVER_ANDROID
   649     {
   650         /* Android docs recommend doing this!
   651          * Ref: http://developer.android.com/reference/android/app/NativeActivity.html 
   652          */
   653         EGLint format;
   654         _this->egl_data->eglGetConfigAttrib(_this->egl_data->egl_display,
   655                                             _this->egl_data->egl_config, 
   656                                             EGL_NATIVE_VISUAL_ID, &format);
   657 
   658         ANativeWindow_setBuffersGeometry(nw, 0, 0, format);
   659     }
   660 #endif    
   661     
   662     surface = _this->egl_data->eglCreateWindowSurface(
   663             _this->egl_data->egl_display,
   664             _this->egl_data->egl_config,
   665             nw, NULL);
   666     if (surface == EGL_NO_SURFACE) {
   667         SDL_EGL_SetError("unable to create an EGL window surface", "eglCreateWindowSurface");
   668     }
   669     return surface;
   670 }
   671 
   672 void
   673 SDL_EGL_DestroySurface(_THIS, EGLSurface egl_surface) 
   674 {
   675     if (!_this->egl_data) {
   676         return;
   677     }
   678     
   679     if (egl_surface != EGL_NO_SURFACE) {
   680         _this->egl_data->eglDestroySurface(_this->egl_data->egl_display, egl_surface);
   681     }
   682 }
   683 
   684 #endif /* SDL_VIDEO_OPENGL_EGL */
   685 
   686 /* vi: set ts=4 sw=4 expandtab: */
   687