1 /*
2  *  Simple DirectMedia Layer
3  *  Copyright (C) 1997-2020 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 #include "../core/android/SDL_android.h"
31 #endif
32 
33 #include "SDL_sysvideo.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" : "libbrcmEGL.so" )
48 #define DEFAULT_OGL_ES2 ( vc4 ? "libGLESv2.so.2" : "libbrcmGLESv2.so" )
49 #define ALT_EGL "libEGL.so"
50 #define ALT_OGL_ES2 "libGLESv2.so"
51 #define DEFAULT_OGL_ES_PVR ( vc4 ? "libGLES_CM.so.1" : "libbrcmGLESv2.so" )
52 #define DEFAULT_OGL_ES ( vc4 ? "libGLESv1_CM.so.1" : "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 #elif SDL_VIDEO_DRIVER_COCOA
69 /* EGL AND OpenGL ES support via ANGLE */
70 #define DEFAULT_EGL "libEGL.dylib"
71 #define DEFAULT_OGL_ES2 "libGLESv2.dylib"
72 #define DEFAULT_OGL_ES_PVR "libGLES_CM.dylib"   //???
73 #define DEFAULT_OGL_ES "libGLESv1_CM.dylib"     //???
74 
75 #else
76 /* Desktop Linux */
77 #define DEFAULT_OGL "libGL.so.1"
78 #define DEFAULT_EGL "libEGL.so.1"
79 #define DEFAULT_OGL_ES2 "libGLESv2.so.2"
80 #define DEFAULT_OGL_ES_PVR "libGLES_CM.so.1"
81 #define DEFAULT_OGL_ES "libGLESv1_CM.so.1"
82 #endif /* SDL_VIDEO_DRIVER_RPI */
83 
84 #if SDL_VIDEO_OPENGL
85 #include "SDL_opengl.h"
86 #endif
87 
88 /** If we happen to not have this defined because of an older EGL version, just define it 0x0
89     as eglGetPlatformDisplayEXT will most likely be NULL if this is missing
90 */
91 #ifndef EGL_PLATFORM_DEVICE_EXT
92 #define EGL_PLATFORM_DEVICE_EXT 0x0
93 #endif
94 
95 #ifdef SDL_VIDEO_STATIC_ANGLE
96 #define LOAD_FUNC(NAME) \
97 _this->egl_data->NAME = (void *)NAME;
98 #else
99 #define LOAD_FUNC(NAME) \
100 _this->egl_data->NAME = SDL_LoadFunction(_this->egl_data->dll_handle, #NAME); \
101 if (!_this->egl_data->NAME) \
102 { \
103     return SDL_SetError("Could not retrieve EGL function " #NAME); \
104 }
105 #endif
106 
107 /* it is allowed to not have some of the EGL extensions on start - attempts to use them will fail later. */
108 #define LOAD_FUNC_EGLEXT(NAME) \
109     _this->egl_data->NAME = _this->egl_data->eglGetProcAddress(#NAME);
110 
111 
SDL_EGL_GetErrorName(EGLint eglErrorCode)112 static const char * SDL_EGL_GetErrorName(EGLint eglErrorCode)
113 {
114 #define SDL_EGL_ERROR_TRANSLATE(e) case e: return #e;
115     switch (eglErrorCode) {
116         SDL_EGL_ERROR_TRANSLATE(EGL_SUCCESS);
117         SDL_EGL_ERROR_TRANSLATE(EGL_NOT_INITIALIZED);
118         SDL_EGL_ERROR_TRANSLATE(EGL_BAD_ACCESS);
119         SDL_EGL_ERROR_TRANSLATE(EGL_BAD_ALLOC);
120         SDL_EGL_ERROR_TRANSLATE(EGL_BAD_ATTRIBUTE);
121         SDL_EGL_ERROR_TRANSLATE(EGL_BAD_CONTEXT);
122         SDL_EGL_ERROR_TRANSLATE(EGL_BAD_CONFIG);
123         SDL_EGL_ERROR_TRANSLATE(EGL_BAD_CURRENT_SURFACE);
124         SDL_EGL_ERROR_TRANSLATE(EGL_BAD_DISPLAY);
125         SDL_EGL_ERROR_TRANSLATE(EGL_BAD_SURFACE);
126         SDL_EGL_ERROR_TRANSLATE(EGL_BAD_MATCH);
127         SDL_EGL_ERROR_TRANSLATE(EGL_BAD_PARAMETER);
128         SDL_EGL_ERROR_TRANSLATE(EGL_BAD_NATIVE_PIXMAP);
129         SDL_EGL_ERROR_TRANSLATE(EGL_BAD_NATIVE_WINDOW);
130         SDL_EGL_ERROR_TRANSLATE(EGL_CONTEXT_LOST);
131     }
132     return "";
133 }
134 
SDL_EGL_SetErrorEx(const char * message,const char * eglFunctionName,EGLint eglErrorCode)135 int SDL_EGL_SetErrorEx(const char * message, const char * eglFunctionName, EGLint eglErrorCode)
136 {
137     const char * errorText = SDL_EGL_GetErrorName(eglErrorCode);
138     char altErrorText[32];
139     if (errorText[0] == '\0') {
140         /* An unknown-to-SDL error code was reported.  Report its hexadecimal value, instead of its name. */
141         SDL_snprintf(altErrorText, SDL_arraysize(altErrorText), "0x%x", (unsigned int)eglErrorCode);
142         errorText = altErrorText;
143     }
144     return SDL_SetError("%s (call to %s failed, reporting an error of %s)", message, eglFunctionName, errorText);
145 }
146 
147 /* EGL implementation of SDL OpenGL ES support */
148 typedef enum {
149     SDL_EGL_DISPLAY_EXTENSION,
150     SDL_EGL_CLIENT_EXTENSION
151 } SDL_EGL_ExtensionType;
152 
SDL_EGL_HasExtension(_THIS,SDL_EGL_ExtensionType type,const char * ext)153 static SDL_bool SDL_EGL_HasExtension(_THIS, SDL_EGL_ExtensionType type, const char *ext)
154 {
155     size_t ext_len;
156     const char *ext_override;
157     const char *egl_extstr;
158     const char *ext_start;
159 
160     /* Invalid extensions can be rejected early */
161     if (ext == NULL || *ext == 0 || SDL_strchr(ext, ' ') != NULL) {
162         /* SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "SDL_EGL_HasExtension: Invalid EGL extension"); */
163         return SDL_FALSE;
164     }
165 
166     /* Extensions can be masked with an environment variable.
167      * Unlike the OpenGL override, this will use the set bits of an integer
168      * to disable the extension.
169      *  Bit   Action
170      *  0     If set, the display extension is masked and not present to SDL.
171      *  1     If set, the client extension is masked and not present to SDL.
172      */
173     ext_override = SDL_getenv(ext);
174     if (ext_override != NULL) {
175         int disable_ext = SDL_atoi(ext_override);
176         if (disable_ext & 0x01 && type == SDL_EGL_DISPLAY_EXTENSION) {
177             return SDL_FALSE;
178         } else if (disable_ext & 0x02 && type == SDL_EGL_CLIENT_EXTENSION) {
179             return SDL_FALSE;
180         }
181     }
182 
183     ext_len = SDL_strlen(ext);
184     switch (type) {
185     case SDL_EGL_DISPLAY_EXTENSION:
186         egl_extstr = _this->egl_data->eglQueryString(_this->egl_data->egl_display, EGL_EXTENSIONS);
187         break;
188     case SDL_EGL_CLIENT_EXTENSION:
189         /* EGL_EXT_client_extensions modifies eglQueryString to return client extensions
190          * if EGL_NO_DISPLAY is passed. Implementations without it are required to return NULL.
191          * This behavior is included in EGL 1.5.
192          */
193         egl_extstr = _this->egl_data->eglQueryString(EGL_NO_DISPLAY, EGL_EXTENSIONS);
194         break;
195     default:
196         /* SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "SDL_EGL_HasExtension: Invalid extension type"); */
197         return SDL_FALSE;
198     }
199 
200     if (egl_extstr != NULL) {
201         ext_start = egl_extstr;
202 
203         while (*ext_start) {
204             ext_start = SDL_strstr(ext_start, ext);
205             if (ext_start == NULL) {
206                 return SDL_FALSE;
207             }
208             /* Check if the match is not just a substring of one of the extensions */
209             if (ext_start == egl_extstr || *(ext_start - 1) == ' ') {
210                 if (ext_start[ext_len] == ' ' || ext_start[ext_len] == 0) {
211                     return SDL_TRUE;
212                 }
213             }
214             /* If the search stopped in the middle of an extension, skip to the end of it */
215             ext_start += ext_len;
216             while (*ext_start != ' ' && *ext_start != 0) {
217                 ext_start++;
218             }
219         }
220     }
221 
222     return SDL_FALSE;
223 }
224 
225 void *
SDL_EGL_GetProcAddress(_THIS,const char * proc)226 SDL_EGL_GetProcAddress(_THIS, const char *proc)
227 {
228     const Uint32 eglver = (((Uint32) _this->egl_data->egl_version_major) << 16) | ((Uint32) _this->egl_data->egl_version_minor);
229     const SDL_bool is_egl_15_or_later = eglver >= ((((Uint32) 1) << 16) | 5);
230     void *retval = NULL;
231 
232     /* EGL 1.5 can use eglGetProcAddress() for any symbol. 1.4 and earlier can't use it for core entry points. */
233     if (!retval && is_egl_15_or_later && _this->egl_data->eglGetProcAddress) {
234         retval = _this->egl_data->eglGetProcAddress(proc);
235     }
236 
237     #ifndef __EMSCRIPTEN__  /* LoadFunction isn't needed on Emscripten and will call dlsym(), causing other problems. */
238     /* Try SDL_LoadFunction() first for EGL <= 1.4, or as a fallback for >= 1.5. */
239     if (!retval) {
240         static char procname[64];
241         retval = SDL_LoadFunction(_this->egl_data->egl_dll_handle, proc);
242         /* just in case you need an underscore prepended... */
243         if (!retval && (SDL_strlen(proc) < (sizeof (procname) - 1))) {
244             procname[0] = '_';
245             SDL_strlcpy(procname + 1, proc, sizeof (procname) - 1);
246             retval = SDL_LoadFunction(_this->egl_data->egl_dll_handle, procname);
247         }
248     }
249     #endif
250 
251     /* Try eglGetProcAddress if we're on <= 1.4 and still searching... */
252     if (!retval && !is_egl_15_or_later && _this->egl_data->eglGetProcAddress) {
253         retval = _this->egl_data->eglGetProcAddress(proc);
254         if (retval) {
255             return retval;
256         }
257     }
258 
259     return retval;
260 }
261 
262 void
SDL_EGL_UnloadLibrary(_THIS)263 SDL_EGL_UnloadLibrary(_THIS)
264 {
265     if (_this->egl_data) {
266         if (_this->egl_data->egl_display) {
267             _this->egl_data->eglTerminate(_this->egl_data->egl_display);
268             _this->egl_data->egl_display = NULL;
269         }
270 
271         if (_this->egl_data->dll_handle) {
272             SDL_UnloadObject(_this->egl_data->dll_handle);
273             _this->egl_data->dll_handle = NULL;
274         }
275         if (_this->egl_data->egl_dll_handle) {
276             SDL_UnloadObject(_this->egl_data->egl_dll_handle);
277             _this->egl_data->egl_dll_handle = NULL;
278         }
279 
280         SDL_free(_this->egl_data);
281         _this->egl_data = NULL;
282     }
283 }
284 
285 int
SDL_EGL_LoadLibraryOnly(_THIS,const char * egl_path)286 SDL_EGL_LoadLibraryOnly(_THIS, const char *egl_path)
287 {
288     void *dll_handle = NULL, *egl_dll_handle = NULL; /* The naming is counter intuitive, but hey, I just work here -- Gabriel */
289     const char *path = NULL;
290 #if SDL_VIDEO_DRIVER_WINDOWS || SDL_VIDEO_DRIVER_WINRT
291     const char *d3dcompiler;
292 #endif
293 #if SDL_VIDEO_DRIVER_RPI
294     SDL_bool vc4 = (0 == access("/sys/module/vc4/", F_OK));
295 #endif
296 
297     if (_this->egl_data) {
298         return SDL_SetError("EGL context already created");
299     }
300 
301     _this->egl_data = (struct SDL_EGL_VideoData *) SDL_calloc(1, sizeof(SDL_EGL_VideoData));
302     if (!_this->egl_data) {
303         return SDL_OutOfMemory();
304     }
305 
306 #if SDL_VIDEO_DRIVER_WINDOWS || SDL_VIDEO_DRIVER_WINRT
307     d3dcompiler = SDL_GetHint(SDL_HINT_VIDEO_WIN_D3DCOMPILER);
308     if (d3dcompiler) {
309         if (SDL_strcasecmp(d3dcompiler, "none") != 0) {
310             if (SDL_LoadObject(d3dcompiler) == NULL) {
311                 SDL_ClearError();
312             }
313         }
314     } else {
315         if (WIN_IsWindowsVistaOrGreater()) {
316             /* Try the newer d3d compilers first */
317             const char *d3dcompiler_list[] = {
318                 "d3dcompiler_47.dll", "d3dcompiler_46.dll",
319             };
320             int i;
321 
322             for (i = 0; i < SDL_arraysize(d3dcompiler_list); ++i) {
323                 if (SDL_LoadObject(d3dcompiler_list[i]) != NULL) {
324                     break;
325                 }
326                 SDL_ClearError();
327             }
328         } else {
329             if (SDL_LoadObject("d3dcompiler_43.dll") == NULL) {
330                 SDL_ClearError();
331             }
332         }
333     }
334 #endif
335 
336 #ifndef SDL_VIDEO_STATIC_ANGLE
337     /* A funny thing, loading EGL.so first does not work on the Raspberry, so we load libGL* first */
338     path = SDL_getenv("SDL_VIDEO_GL_DRIVER");
339     if (path != NULL) {
340         egl_dll_handle = SDL_LoadObject(path);
341     }
342 
343     if (egl_dll_handle == NULL) {
344         if (_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES) {
345             if (_this->gl_config.major_version > 1) {
346                 path = DEFAULT_OGL_ES2;
347                 egl_dll_handle = SDL_LoadObject(path);
348 #ifdef ALT_OGL_ES2
349                 if (egl_dll_handle == NULL && !vc4) {
350                     path = ALT_OGL_ES2;
351                     egl_dll_handle = SDL_LoadObject(path);
352                 }
353 #endif
354 
355             } else {
356                 path = DEFAULT_OGL_ES;
357                 egl_dll_handle = SDL_LoadObject(path);
358                 if (egl_dll_handle == NULL) {
359                     path = DEFAULT_OGL_ES_PVR;
360                     egl_dll_handle = SDL_LoadObject(path);
361                 }
362 #ifdef ALT_OGL_ES2
363                 if (egl_dll_handle == NULL && !vc4) {
364                     path = ALT_OGL_ES2;
365                     egl_dll_handle = SDL_LoadObject(path);
366                 }
367 #endif
368             }
369         }
370 #ifdef DEFAULT_OGL
371         else {
372             path = DEFAULT_OGL;
373             egl_dll_handle = SDL_LoadObject(path);
374         }
375 #endif
376     }
377     _this->egl_data->egl_dll_handle = egl_dll_handle;
378 
379     if (egl_dll_handle == NULL) {
380         return SDL_SetError("Could not initialize OpenGL / GLES library");
381     }
382 
383     /* Loading libGL* in the previous step took care of loading libEGL.so, but we future proof by double checking */
384     if (egl_path != NULL) {
385         dll_handle = SDL_LoadObject(egl_path);
386     }
387     /* Try loading a EGL symbol, if it does not work try the default library paths */
388     if (dll_handle == NULL || SDL_LoadFunction(dll_handle, "eglChooseConfig") == NULL) {
389         if (dll_handle != NULL) {
390             SDL_UnloadObject(dll_handle);
391         }
392         path = SDL_getenv("SDL_VIDEO_EGL_DRIVER");
393         if (path == NULL) {
394             path = DEFAULT_EGL;
395         }
396         dll_handle = SDL_LoadObject(path);
397 
398 #ifdef ALT_EGL
399         if (dll_handle == NULL && !vc4) {
400             path = ALT_EGL;
401             dll_handle = SDL_LoadObject(path);
402         }
403 #endif
404 
405         if (dll_handle == NULL || SDL_LoadFunction(dll_handle, "eglChooseConfig") == NULL) {
406             if (dll_handle != NULL) {
407                 SDL_UnloadObject(dll_handle);
408             }
409             return SDL_SetError("Could not load EGL library");
410         }
411         SDL_ClearError();
412     }
413 #endif
414 
415     _this->egl_data->dll_handle = dll_handle;
416 
417     /* Load new function pointers */
418     LOAD_FUNC(eglGetDisplay);
419     LOAD_FUNC(eglInitialize);
420     LOAD_FUNC(eglTerminate);
421     LOAD_FUNC(eglGetProcAddress);
422     LOAD_FUNC(eglChooseConfig);
423     LOAD_FUNC(eglGetConfigAttrib);
424     LOAD_FUNC(eglCreateContext);
425     LOAD_FUNC(eglDestroyContext);
426     LOAD_FUNC(eglCreatePbufferSurface);
427     LOAD_FUNC(eglCreateWindowSurface);
428     LOAD_FUNC(eglDestroySurface);
429     LOAD_FUNC(eglMakeCurrent);
430     LOAD_FUNC(eglSwapBuffers);
431     LOAD_FUNC(eglSwapInterval);
432     LOAD_FUNC(eglWaitNative);
433     LOAD_FUNC(eglWaitGL);
434     LOAD_FUNC(eglBindAPI);
435     LOAD_FUNC(eglQueryAPI);
436     LOAD_FUNC(eglQueryString);
437     LOAD_FUNC(eglGetError);
438     LOAD_FUNC_EGLEXT(eglQueryDevicesEXT);
439     LOAD_FUNC_EGLEXT(eglGetPlatformDisplayEXT);
440 
441     _this->gl_config.driver_loaded = 1;
442 
443     if (path) {
444         SDL_strlcpy(_this->gl_config.driver_path, path, sizeof(_this->gl_config.driver_path) - 1);
445     } else {
446         *_this->gl_config.driver_path = '\0';
447     }
448 
449     return 0;
450 }
451 
452 static void
SDL_EGL_GetVersion(_THIS)453 SDL_EGL_GetVersion(_THIS) {
454     if (_this->egl_data->eglQueryString) {
455         const char *egl_version = _this->egl_data->eglQueryString(_this->egl_data->egl_display, EGL_VERSION);
456         if (egl_version) {
457             int major = 0, minor = 0;
458             if (SDL_sscanf(egl_version, "%d.%d", &major, &minor) == 2) {
459                 _this->egl_data->egl_version_major = major;
460                 _this->egl_data->egl_version_minor = minor;
461             } else {
462                 SDL_LogWarn(SDL_LOG_CATEGORY_VIDEO, "Could not parse EGL version string: %s", egl_version);
463             }
464         }
465     }
466 }
467 
468 int
SDL_EGL_LoadLibrary(_THIS,const char * egl_path,NativeDisplayType native_display,EGLenum platform)469 SDL_EGL_LoadLibrary(_THIS, const char *egl_path, NativeDisplayType native_display, EGLenum platform)
470 {
471     int egl_version_major, egl_version_minor;
472     int library_load_retcode = SDL_EGL_LoadLibraryOnly(_this, egl_path);
473     if (library_load_retcode != 0) {
474         return library_load_retcode;
475     }
476 
477     /* EGL 1.5 allows querying for client version with EGL_NO_DISPLAY */
478     SDL_EGL_GetVersion(_this);
479 
480     egl_version_major = _this->egl_data->egl_version_major;
481     egl_version_minor = _this->egl_data->egl_version_minor;
482 
483     if (egl_version_major == 1 && egl_version_minor == 5) {
484         LOAD_FUNC(eglGetPlatformDisplay);
485     }
486 
487     _this->egl_data->egl_display = EGL_NO_DISPLAY;
488 #if !defined(__WINRT__)
489     if (platform) {
490         if (egl_version_major == 1 && egl_version_minor == 5) {
491             _this->egl_data->egl_display = _this->egl_data->eglGetPlatformDisplay(platform, (void *)(size_t)native_display, NULL);
492         } else {
493             if (SDL_EGL_HasExtension(_this, SDL_EGL_CLIENT_EXTENSION, "EGL_EXT_platform_base")) {
494                 _this->egl_data->eglGetPlatformDisplayEXT = SDL_EGL_GetProcAddress(_this, "eglGetPlatformDisplayEXT");
495                 if (_this->egl_data->eglGetPlatformDisplayEXT) {
496                     _this->egl_data->egl_display = _this->egl_data->eglGetPlatformDisplayEXT(platform, (void *)(size_t)native_display, NULL);
497                 }
498             }
499         }
500     }
501     /* Try the implementation-specific eglGetDisplay even if eglGetPlatformDisplay fails */
502     if (_this->egl_data->egl_display == EGL_NO_DISPLAY) {
503         _this->egl_data->egl_display = _this->egl_data->eglGetDisplay(native_display);
504     }
505     if (_this->egl_data->egl_display == EGL_NO_DISPLAY) {
506         _this->gl_config.driver_loaded = 0;
507         *_this->gl_config.driver_path = '\0';
508         return SDL_SetError("Could not get EGL display");
509     }
510 
511     if (_this->egl_data->eglInitialize(_this->egl_data->egl_display, NULL, NULL) != EGL_TRUE) {
512         _this->gl_config.driver_loaded = 0;
513         *_this->gl_config.driver_path = '\0';
514         return SDL_SetError("Could not initialize EGL");
515     }
516 #endif
517 
518     /* Get the EGL version with a valid egl_display, for EGL <= 1.4 */
519     SDL_EGL_GetVersion(_this);
520 
521     _this->egl_data->is_offscreen = 0;
522 
523     return 0;
524 }
525 
526 /**
527    On multi GPU machines EGL device 0 is not always the first valid GPU.
528    Container environments can restrict access to some GPUs that are still listed in the EGL
529    device list. If the requested device is a restricted GPU and cannot be used
530    (eglInitialize() will fail) then attempt to automatically and silently select the next
531    valid available GPU for EGL to use.
532 */
533 
534 int
SDL_EGL_InitializeOffscreen(_THIS,int device)535 SDL_EGL_InitializeOffscreen(_THIS, int device)
536 {
537     void *egl_devices[SDL_EGL_MAX_DEVICES];
538     EGLint num_egl_devices = 0;
539     const char *egl_device_hint;
540 
541     if (_this->gl_config.driver_loaded != 1) {
542         return SDL_SetError("SDL_EGL_LoadLibraryOnly() has not been called or has failed.");
543     }
544 
545     /* Check for all extensions that are optional until used and fail if any is missing */
546     if (_this->egl_data->eglQueryDevicesEXT == NULL) {
547         return SDL_SetError("eglQueryDevicesEXT is missing (EXT_device_enumeration not supported by the drivers?)");
548     }
549 
550     if (_this->egl_data->eglGetPlatformDisplayEXT == NULL) {
551         return SDL_SetError("eglGetPlatformDisplayEXT is missing (EXT_platform_base not supported by the drivers?)");
552     }
553 
554     if (_this->egl_data->eglQueryDevicesEXT(SDL_EGL_MAX_DEVICES, egl_devices, &num_egl_devices) != EGL_TRUE) {
555         return SDL_SetError("eglQueryDevicesEXT() failed");
556     }
557 
558     egl_device_hint = SDL_GetHint("SDL_HINT_EGL_DEVICE");
559     if (egl_device_hint) {
560         device = SDL_atoi(egl_device_hint);
561 
562         if (device >= num_egl_devices) {
563             return SDL_SetError("Invalid EGL device is requested.");
564         }
565 
566         _this->egl_data->egl_display = _this->egl_data->eglGetPlatformDisplayEXT(EGL_PLATFORM_DEVICE_EXT, egl_devices[device], NULL);
567 
568         if (_this->egl_data->egl_display == EGL_NO_DISPLAY) {
569             return SDL_SetError("eglGetPlatformDisplayEXT() failed.");
570         }
571 
572         if (_this->egl_data->eglInitialize(_this->egl_data->egl_display, NULL, NULL) != EGL_TRUE) {
573             return SDL_SetError("Could not initialize EGL");
574         }
575     }
576     else {
577         int i;
578         SDL_bool found = SDL_FALSE;
579         EGLDisplay attempted_egl_display;
580 
581         /* If no hint is provided lets look for the first device/display that will allow us to eglInit */
582         for (i = 0; i < num_egl_devices; i++) {
583             attempted_egl_display = _this->egl_data->eglGetPlatformDisplayEXT(EGL_PLATFORM_DEVICE_EXT, egl_devices[i], NULL);
584 
585             if (attempted_egl_display == EGL_NO_DISPLAY) {
586                 continue;
587             }
588 
589             if (_this->egl_data->eglInitialize(attempted_egl_display, NULL, NULL) != EGL_TRUE) {
590                 _this->egl_data->eglTerminate(attempted_egl_display);
591                 continue;
592             }
593 
594             /* We did not fail, we'll pick this one! */
595             _this->egl_data->egl_display = attempted_egl_display;
596             found = SDL_TRUE;
597 
598             break;
599         }
600 
601         if (!found) {
602             return SDL_SetError("Could not find a valid EGL device to initialize");
603         }
604     }
605 
606     /* Get the EGL version with a valid egl_display, for EGL <= 1.4 */
607     SDL_EGL_GetVersion(_this);
608 
609     _this->egl_data->is_offscreen = 1;
610 
611     return 0;
612 }
613 
614 void
SDL_EGL_SetRequiredVisualId(_THIS,int visual_id)615 SDL_EGL_SetRequiredVisualId(_THIS, int visual_id)
616 {
617     _this->egl_data->egl_required_visual_id=visual_id;
618 }
619 
620 #ifdef DUMP_EGL_CONFIG
621 
622 #define ATTRIBUTE(_attr) { _attr, #_attr }
623 
624 typedef struct {
625     EGLint attribute;
626     char const* name;
627 } Attribute;
628 
629 Attribute attributes[] = {
630         ATTRIBUTE( EGL_BUFFER_SIZE ),
631         ATTRIBUTE( EGL_ALPHA_SIZE ),
632         ATTRIBUTE( EGL_BLUE_SIZE ),
633         ATTRIBUTE( EGL_GREEN_SIZE ),
634         ATTRIBUTE( EGL_RED_SIZE ),
635         ATTRIBUTE( EGL_DEPTH_SIZE ),
636         ATTRIBUTE( EGL_STENCIL_SIZE ),
637         ATTRIBUTE( EGL_CONFIG_CAVEAT ),
638         ATTRIBUTE( EGL_CONFIG_ID ),
639         ATTRIBUTE( EGL_LEVEL ),
640         ATTRIBUTE( EGL_MAX_PBUFFER_HEIGHT ),
641         ATTRIBUTE( EGL_MAX_PBUFFER_WIDTH ),
642         ATTRIBUTE( EGL_MAX_PBUFFER_PIXELS ),
643         ATTRIBUTE( EGL_NATIVE_RENDERABLE ),
644         ATTRIBUTE( EGL_NATIVE_VISUAL_ID ),
645         ATTRIBUTE( EGL_NATIVE_VISUAL_TYPE ),
646         ATTRIBUTE( EGL_SAMPLES ),
647         ATTRIBUTE( EGL_SAMPLE_BUFFERS ),
648         ATTRIBUTE( EGL_SURFACE_TYPE ),
649         ATTRIBUTE( EGL_TRANSPARENT_TYPE ),
650         ATTRIBUTE( EGL_TRANSPARENT_BLUE_VALUE ),
651         ATTRIBUTE( EGL_TRANSPARENT_GREEN_VALUE ),
652         ATTRIBUTE( EGL_TRANSPARENT_RED_VALUE ),
653         ATTRIBUTE( EGL_BIND_TO_TEXTURE_RGB ),
654         ATTRIBUTE( EGL_BIND_TO_TEXTURE_RGBA ),
655         ATTRIBUTE( EGL_MIN_SWAP_INTERVAL ),
656         ATTRIBUTE( EGL_MAX_SWAP_INTERVAL ),
657         ATTRIBUTE( EGL_LUMINANCE_SIZE ),
658         ATTRIBUTE( EGL_ALPHA_MASK_SIZE ),
659         ATTRIBUTE( EGL_COLOR_BUFFER_TYPE ),
660         ATTRIBUTE( EGL_RENDERABLE_TYPE ),
661         ATTRIBUTE( EGL_MATCH_NATIVE_PIXMAP ),
662         ATTRIBUTE( EGL_CONFORMANT ),
663 };
664 
665 
dumpconfig(_THIS,EGLConfig config)666 static void dumpconfig(_THIS, EGLConfig config)
667 {
668     int attr;
669     for (attr = 0 ; attr<sizeof(attributes)/sizeof(Attribute) ; attr++) {
670         EGLint value;
671         _this->egl_data->eglGetConfigAttrib(_this->egl_data->egl_display, config, attributes[attr].attribute, &value);
672         SDL_Log("\t%-32s: %10d (0x%08x)\n", attributes[attr].name, value, value);
673     }
674 }
675 
676 #endif /* DUMP_EGL_CONFIG */
677 
678 int
SDL_EGL_ChooseConfig(_THIS)679 SDL_EGL_ChooseConfig(_THIS)
680 {
681 /* 64 seems nice. */
682     EGLint attribs[64];
683     EGLint found_configs = 0, value;
684     /* 128 seems even nicer here */
685     EGLConfig configs[128];
686     SDL_bool has_matching_format = SDL_FALSE;
687     int i, j, best_bitdiff = -1, bitdiff;
688 
689     if (!_this->egl_data) {
690         /* The EGL library wasn't loaded, SDL_GetError() should have info */
691         return -1;
692     }
693 
694     /* Get a valid EGL configuration */
695     i = 0;
696     attribs[i++] = EGL_RED_SIZE;
697     attribs[i++] = _this->gl_config.red_size;
698     attribs[i++] = EGL_GREEN_SIZE;
699     attribs[i++] = _this->gl_config.green_size;
700     attribs[i++] = EGL_BLUE_SIZE;
701     attribs[i++] = _this->gl_config.blue_size;
702 
703     if (_this->gl_config.alpha_size) {
704         attribs[i++] = EGL_ALPHA_SIZE;
705         attribs[i++] = _this->gl_config.alpha_size;
706     }
707 
708     if (_this->gl_config.buffer_size) {
709         attribs[i++] = EGL_BUFFER_SIZE;
710         attribs[i++] = _this->gl_config.buffer_size;
711     }
712 
713     attribs[i++] = EGL_DEPTH_SIZE;
714     attribs[i++] = _this->gl_config.depth_size;
715 
716     if (_this->gl_config.stencil_size) {
717         attribs[i++] = EGL_STENCIL_SIZE;
718         attribs[i++] = _this->gl_config.stencil_size;
719     }
720 
721     if (_this->gl_config.multisamplebuffers) {
722         attribs[i++] = EGL_SAMPLE_BUFFERS;
723         attribs[i++] = _this->gl_config.multisamplebuffers;
724     }
725 
726     if (_this->gl_config.multisamplesamples) {
727         attribs[i++] = EGL_SAMPLES;
728         attribs[i++] = _this->gl_config.multisamplesamples;
729     }
730 
731     if (_this->egl_data->is_offscreen) {
732         attribs[i++] = EGL_SURFACE_TYPE;
733         attribs[i++] = EGL_PBUFFER_BIT;
734     }
735 
736     attribs[i++] = EGL_RENDERABLE_TYPE;
737     if (_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES) {
738 #ifdef EGL_KHR_create_context
739         if (_this->gl_config.major_version >= 3 &&
740             SDL_EGL_HasExtension(_this, SDL_EGL_DISPLAY_EXTENSION, "EGL_KHR_create_context")) {
741             attribs[i++] = EGL_OPENGL_ES3_BIT_KHR;
742         } else
743 #endif
744         if (_this->gl_config.major_version >= 2) {
745             attribs[i++] = EGL_OPENGL_ES2_BIT;
746         } else {
747             attribs[i++] = EGL_OPENGL_ES_BIT;
748         }
749         _this->egl_data->eglBindAPI(EGL_OPENGL_ES_API);
750     } else {
751         attribs[i++] = EGL_OPENGL_BIT;
752         _this->egl_data->eglBindAPI(EGL_OPENGL_API);
753     }
754 
755     if (_this->egl_data->egl_surfacetype) {
756         attribs[i++] = EGL_SURFACE_TYPE;
757         attribs[i++] = _this->egl_data->egl_surfacetype;
758     }
759 
760     attribs[i++] = EGL_NONE;
761 
762     if (_this->egl_data->eglChooseConfig(_this->egl_data->egl_display,
763         attribs,
764         configs, SDL_arraysize(configs),
765         &found_configs) == EGL_FALSE ||
766         found_configs == 0) {
767         return SDL_EGL_SetError("Couldn't find matching EGL config", "eglChooseConfig");
768     }
769 
770     /* first ensure that a found config has a matching format, or the function will fall through. */
771     for (i = 0; i < found_configs; i++ ) {
772         if (_this->egl_data->egl_required_visual_id)
773         {
774             EGLint format;
775             _this->egl_data->eglGetConfigAttrib(_this->egl_data->egl_display,
776                                             configs[i],
777                                             EGL_NATIVE_VISUAL_ID, &format);
778             if (_this->egl_data->egl_required_visual_id == format)
779                 has_matching_format = SDL_TRUE;
780         }
781     }
782 
783     /* eglChooseConfig returns a number of configurations that match or exceed the requested attribs. */
784     /* From those, we select the one that matches our requirements more closely via a makeshift algorithm */
785 
786     for (i = 0; i < found_configs; i++ ) {
787         if (has_matching_format && _this->egl_data->egl_required_visual_id)
788         {
789             EGLint format;
790             _this->egl_data->eglGetConfigAttrib(_this->egl_data->egl_display,
791                                             configs[i],
792                                             EGL_NATIVE_VISUAL_ID, &format);
793             if (_this->egl_data->egl_required_visual_id != format)
794                 continue;
795         }
796 
797         bitdiff = 0;
798         for (j = 0; j < SDL_arraysize(attribs) - 1; j += 2) {
799             if (attribs[j] == EGL_NONE) {
800                break;
801             }
802 
803             if ( attribs[j+1] != EGL_DONT_CARE && (
804                 attribs[j] == EGL_RED_SIZE ||
805                 attribs[j] == EGL_GREEN_SIZE ||
806                 attribs[j] == EGL_BLUE_SIZE ||
807                 attribs[j] == EGL_ALPHA_SIZE ||
808                 attribs[j] == EGL_DEPTH_SIZE ||
809                 attribs[j] == EGL_STENCIL_SIZE)) {
810                 _this->egl_data->eglGetConfigAttrib(_this->egl_data->egl_display, configs[i], attribs[j], &value);
811                 bitdiff += value - attribs[j + 1]; /* value is always >= attrib */
812             }
813         }
814 
815         if (bitdiff < best_bitdiff || best_bitdiff == -1) {
816             _this->egl_data->egl_config = configs[i];
817 
818             best_bitdiff = bitdiff;
819         }
820 
821         if (bitdiff == 0) {
822             break; /* we found an exact match! */
823         }
824     }
825 
826 #ifdef DUMP_EGL_CONFIG
827     dumpconfig(_this, _this->egl_data->egl_config);
828 #endif
829 
830     return 0;
831 }
832 
833 SDL_GLContext
SDL_EGL_CreateContext(_THIS,EGLSurface egl_surface)834 SDL_EGL_CreateContext(_THIS, EGLSurface egl_surface)
835 {
836     /* max 14 values plus terminator. */
837     EGLint attribs[15];
838     int attr = 0;
839 
840     EGLContext egl_context, share_context = EGL_NO_CONTEXT;
841     EGLint profile_mask = _this->gl_config.profile_mask;
842     EGLint major_version = _this->gl_config.major_version;
843     EGLint minor_version = _this->gl_config.minor_version;
844     SDL_bool profile_es = (profile_mask == SDL_GL_CONTEXT_PROFILE_ES);
845 
846     if (!_this->egl_data) {
847         /* The EGL library wasn't loaded, SDL_GetError() should have info */
848         return NULL;
849     }
850 
851     if (_this->gl_config.share_with_current_context) {
852         share_context = (EGLContext)SDL_GL_GetCurrentContext();
853     }
854 
855 #if SDL_VIDEO_DRIVER_ANDROID
856     if ((_this->gl_config.flags & SDL_GL_CONTEXT_DEBUG_FLAG) != 0) {
857         /* If SDL_GL_CONTEXT_DEBUG_FLAG is set but EGL_KHR_debug unsupported, unset.
858          * This is required because some Android devices like to complain about it
859          * by "silently" failing, logging a hint which could be easily overlooked:
860          * E/libEGL  (26984): validate_display:255 error 3008 (EGL_BAD_DISPLAY)
861          * The following explicitly checks for EGL_KHR_debug before EGL 1.5
862          */
863         int egl_version_major = _this->egl_data->egl_version_major;
864         int egl_version_minor = _this->egl_data->egl_version_minor;
865         if (((egl_version_major < 1) || (egl_version_major == 1 && egl_version_minor < 5)) &&
866             !SDL_EGL_HasExtension(_this, SDL_EGL_DISPLAY_EXTENSION, "EGL_KHR_debug")) {
867             /* SDL profile bits match EGL profile bits. */
868             _this->gl_config.flags &= ~SDL_GL_CONTEXT_DEBUG_FLAG;
869         }
870     }
871 #endif
872 
873     /* Set the context version and other attributes. */
874     if ((major_version < 3 || (minor_version == 0 && profile_es)) &&
875         _this->gl_config.flags == 0 &&
876         (profile_mask == 0 || profile_es)) {
877         /* Create a context without using EGL_KHR_create_context attribs.
878          * When creating a GLES context without EGL_KHR_create_context we can
879          * only specify the major version. When creating a desktop GL context
880          * we can't specify any version, so we only try in that case when the
881          * version is less than 3.0 (matches SDL's GLX/WGL behavior.)
882          */
883         if (profile_es) {
884             attribs[attr++] = EGL_CONTEXT_CLIENT_VERSION;
885             attribs[attr++] = SDL_max(major_version, 1);
886         }
887     } else {
888 #ifdef EGL_KHR_create_context
889         /* The Major/minor version, context profiles, and context flags can
890          * only be specified when this extension is available.
891          */
892         if (SDL_EGL_HasExtension(_this, SDL_EGL_DISPLAY_EXTENSION, "EGL_KHR_create_context")) {
893             attribs[attr++] = EGL_CONTEXT_MAJOR_VERSION_KHR;
894             attribs[attr++] = major_version;
895             attribs[attr++] = EGL_CONTEXT_MINOR_VERSION_KHR;
896             attribs[attr++] = minor_version;
897 
898             /* SDL profile bits match EGL profile bits. */
899             if (profile_mask != 0 && profile_mask != SDL_GL_CONTEXT_PROFILE_ES) {
900                 attribs[attr++] = EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR;
901                 attribs[attr++] = profile_mask;
902             }
903 
904             /* SDL flags match EGL flags. */
905             if (_this->gl_config.flags != 0) {
906                 attribs[attr++] = EGL_CONTEXT_FLAGS_KHR;
907                 attribs[attr++] = _this->gl_config.flags;
908             }
909         } else
910 #endif /* EGL_KHR_create_context */
911         {
912             SDL_SetError("Could not create EGL context (context attributes are not supported)");
913             return NULL;
914         }
915     }
916 
917     if (_this->gl_config.no_error) {
918 #ifdef EGL_KHR_create_context_no_error
919         if (SDL_EGL_HasExtension(_this, SDL_EGL_DISPLAY_EXTENSION, "EGL_KHR_create_context_no_error")) {
920             attribs[attr++] = EGL_CONTEXT_OPENGL_NO_ERROR_KHR;
921             attribs[attr++] = _this->gl_config.no_error;
922         } else
923 #endif
924         {
925             SDL_SetError("EGL implementation does not support no_error contexts");
926             return NULL;
927         }
928     }
929 
930     attribs[attr++] = EGL_NONE;
931 
932     /* Bind the API */
933     if (profile_es) {
934         _this->egl_data->eglBindAPI(EGL_OPENGL_ES_API);
935     } else {
936         _this->egl_data->eglBindAPI(EGL_OPENGL_API);
937     }
938 
939     egl_context = _this->egl_data->eglCreateContext(_this->egl_data->egl_display,
940                                       _this->egl_data->egl_config,
941                                       share_context, attribs);
942 
943     if (egl_context == EGL_NO_CONTEXT) {
944         SDL_EGL_SetError("Could not create EGL context", "eglCreateContext");
945         return NULL;
946     }
947 
948     _this->egl_data->egl_swapinterval = 0;
949 
950     if (SDL_EGL_MakeCurrent(_this, egl_surface, egl_context) < 0) {
951         /* Save the SDL error set by SDL_EGL_MakeCurrent */
952         char errorText[1024];
953         SDL_strlcpy(errorText, SDL_GetError(), SDL_arraysize(errorText));
954 
955         /* Delete the context, which may alter the value returned by SDL_GetError() */
956         SDL_EGL_DeleteContext(_this, egl_context);
957 
958         /* Restore the SDL error */
959         SDL_SetError("%s", errorText);
960 
961         return NULL;
962     }
963 
964     /* Check whether making contexts current without a surface is supported.
965      * First condition: EGL must support it. That's the case for EGL 1.5
966      * or later, or if the EGL_KHR_surfaceless_context extension is present. */
967     if ((_this->egl_data->egl_version_major > 1) ||
968         ((_this->egl_data->egl_version_major == 1) && (_this->egl_data->egl_version_minor >= 5)) ||
969         SDL_EGL_HasExtension(_this, SDL_EGL_DISPLAY_EXTENSION, "EGL_KHR_surfaceless_context"))
970     {
971         /* Secondary condition: The client API must support it. */
972         if (profile_es) {
973             /* On OpenGL ES, the GL_OES_surfaceless_context extension must be
974              * present. */
975             if (SDL_GL_ExtensionSupported("GL_OES_surfaceless_context")) {
976                 _this->gl_allow_no_surface = SDL_TRUE;
977             }
978 #if SDL_VIDEO_OPENGL
979         } else {
980             /* Desktop OpenGL supports it by default from version 3.0 on. */
981             void (APIENTRY * glGetIntegervFunc) (GLenum pname, GLint * params);
982             glGetIntegervFunc = SDL_GL_GetProcAddress("glGetIntegerv");
983             if (glGetIntegervFunc) {
984                 GLint v = 0;
985                 glGetIntegervFunc(GL_MAJOR_VERSION, &v);
986                 if (v >= 3) {
987                     _this->gl_allow_no_surface = SDL_TRUE;
988                 }
989             }
990 #endif
991         }
992     }
993 
994     return (SDL_GLContext) egl_context;
995 }
996 
997 int
SDL_EGL_MakeCurrent(_THIS,EGLSurface egl_surface,SDL_GLContext context)998 SDL_EGL_MakeCurrent(_THIS, EGLSurface egl_surface, SDL_GLContext context)
999 {
1000     EGLContext egl_context = (EGLContext) context;
1001 
1002     if (!_this->egl_data) {
1003         return SDL_SetError("OpenGL not initialized");
1004     }
1005 
1006     /* The android emulator crashes badly if you try to eglMakeCurrent
1007      * with a valid context and invalid surface, so we have to check for both here.
1008      */
1009     if (!egl_context || (!egl_surface && !_this->gl_allow_no_surface)) {
1010          _this->egl_data->eglMakeCurrent(_this->egl_data->egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
1011     } else {
1012         if (!_this->egl_data->eglMakeCurrent(_this->egl_data->egl_display,
1013             egl_surface, egl_surface, egl_context)) {
1014             return SDL_EGL_SetError("Unable to make EGL context current", "eglMakeCurrent");
1015         }
1016     }
1017 
1018     return 0;
1019 }
1020 
1021 int
SDL_EGL_SetSwapInterval(_THIS,int interval)1022 SDL_EGL_SetSwapInterval(_THIS, int interval)
1023 {
1024     EGLBoolean status;
1025 
1026     if (!_this->egl_data) {
1027         return SDL_SetError("EGL not initialized");
1028     }
1029 
1030     status = _this->egl_data->eglSwapInterval(_this->egl_data->egl_display, interval);
1031     if (status == EGL_TRUE) {
1032         _this->egl_data->egl_swapinterval = interval;
1033         return 0;
1034     }
1035 
1036     return SDL_EGL_SetError("Unable to set the EGL swap interval", "eglSwapInterval");
1037 }
1038 
1039 int
SDL_EGL_GetSwapInterval(_THIS)1040 SDL_EGL_GetSwapInterval(_THIS)
1041 {
1042     if (!_this->egl_data) {
1043         SDL_SetError("EGL not initialized");
1044         return 0;
1045     }
1046 
1047     return _this->egl_data->egl_swapinterval;
1048 }
1049 
1050 int
SDL_EGL_SwapBuffers(_THIS,EGLSurface egl_surface)1051 SDL_EGL_SwapBuffers(_THIS, EGLSurface egl_surface)
1052 {
1053     if (!_this->egl_data->eglSwapBuffers(_this->egl_data->egl_display, egl_surface)) {
1054         return SDL_EGL_SetError("unable to show color buffer in an OS-native window", "eglSwapBuffers");
1055     }
1056     return 0;
1057 }
1058 
1059 void
SDL_EGL_DeleteContext(_THIS,SDL_GLContext context)1060 SDL_EGL_DeleteContext(_THIS, SDL_GLContext context)
1061 {
1062     EGLContext egl_context = (EGLContext) context;
1063 
1064     /* Clean up GLES and EGL */
1065     if (!_this->egl_data) {
1066         return;
1067     }
1068 
1069     if (egl_context != NULL && egl_context != EGL_NO_CONTEXT) {
1070         _this->egl_data->eglDestroyContext(_this->egl_data->egl_display, egl_context);
1071     }
1072 
1073 }
1074 
1075 EGLSurface *
SDL_EGL_CreateSurface(_THIS,NativeWindowType nw)1076 SDL_EGL_CreateSurface(_THIS, NativeWindowType nw)
1077 {
1078     /* max 2 values plus terminator. */
1079     EGLint attribs[3];
1080     int attr = 0;
1081 
1082     EGLSurface * surface;
1083 
1084     if (SDL_EGL_ChooseConfig(_this) != 0) {
1085         return EGL_NO_SURFACE;
1086     }
1087 
1088 #if SDL_VIDEO_DRIVER_ANDROID
1089     {
1090         /* Android docs recommend doing this!
1091          * Ref: http://developer.android.com/reference/android/app/NativeActivity.html
1092          */
1093         EGLint format;
1094         _this->egl_data->eglGetConfigAttrib(_this->egl_data->egl_display,
1095                                             _this->egl_data->egl_config,
1096                                             EGL_NATIVE_VISUAL_ID, &format);
1097 
1098         ANativeWindow_setBuffersGeometry(nw, 0, 0, format);
1099 
1100         /* Update SurfaceView holder format.
1101          * May triggers a sequence surfaceDestroyed(), surfaceCreated(), surfaceChanged(). */
1102         Android_JNI_SetSurfaceViewFormat(format);
1103     }
1104 #endif
1105     if (_this->gl_config.framebuffer_srgb_capable) {
1106 #ifdef EGL_KHR_gl_colorspace
1107         if (SDL_EGL_HasExtension(_this, SDL_EGL_DISPLAY_EXTENSION, "EGL_KHR_gl_colorspace")) {
1108             attribs[attr++] = EGL_GL_COLORSPACE_KHR;
1109             attribs[attr++] = EGL_GL_COLORSPACE_SRGB_KHR;
1110         } else
1111 #endif
1112         {
1113             SDL_SetError("EGL implementation does not support sRGB system framebuffers");
1114             return EGL_NO_SURFACE;
1115         }
1116     }
1117 
1118     attribs[attr++] = EGL_NONE;
1119 
1120     surface = _this->egl_data->eglCreateWindowSurface(
1121             _this->egl_data->egl_display,
1122             _this->egl_data->egl_config,
1123             nw, &attribs[0]);
1124     if (surface == EGL_NO_SURFACE) {
1125         SDL_EGL_SetError("unable to create an EGL window surface", "eglCreateWindowSurface");
1126     }
1127     return surface;
1128 }
1129 
1130 EGLSurface
SDL_EGL_CreateOffscreenSurface(_THIS,int width,int height)1131 SDL_EGL_CreateOffscreenSurface(_THIS, int width, int height)
1132 {
1133     EGLint attributes[] = {
1134         EGL_WIDTH, width,
1135         EGL_HEIGHT, height,
1136         EGL_NONE
1137     };
1138 
1139     if (SDL_EGL_ChooseConfig(_this) != 0) {
1140         return EGL_NO_SURFACE;
1141     }
1142 
1143     return _this->egl_data->eglCreatePbufferSurface(
1144         _this->egl_data->egl_display,
1145         _this->egl_data->egl_config,
1146         attributes);
1147 }
1148 
1149 void
SDL_EGL_DestroySurface(_THIS,EGLSurface egl_surface)1150 SDL_EGL_DestroySurface(_THIS, EGLSurface egl_surface)
1151 {
1152     if (!_this->egl_data) {
1153         return;
1154     }
1155 
1156     if (egl_surface != EGL_NO_SURFACE) {
1157         _this->egl_data->eglDestroySurface(_this->egl_data->egl_display, egl_surface);
1158     }
1159 }
1160 
1161 #endif /* SDL_VIDEO_OPENGL_EGL */
1162 
1163 /* vi: set ts=4 sw=4 expandtab: */
1164 
1165