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