upgrade SDL to version 2.0.8
[platform/upstream/SDL.git] / src / video / SDL_egl.c
1 /*
2  *  Simple DirectMedia Layer
3  *  Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
4  * 
5  *  This software is provided 'as-is', without any express or implied
6  *  warranty.  In no event will the authors be held liable for any damages
7  *  arising from the use of this software.
8  * 
9  *  Permission is granted to anyone to use this software for any purpose,
10  *  including commercial applications, and to alter it and redistribute it
11  *  freely, subject to the following restrictions:
12  * 
13  *  1. The origin of this software must not be misrepresented; you must not
14  *     claim that you wrote the original software. If you use this software
15  *     in a product, an acknowledgment in the product documentation would be
16  *     appreciated but is not required.
17  *  2. Altered source versions must be plainly marked as such, and must not be
18  *     misrepresented as being the original software.
19  *  3. This notice may not be removed or altered from any source distribution.
20  */
21 #include "../SDL_internal.h"
22
23 #if SDL_VIDEO_OPENGL_EGL
24
25 #if SDL_VIDEO_DRIVER_WINDOWS || SDL_VIDEO_DRIVER_WINRT
26 #include "../core/windows/SDL_windows.h"
27 #endif
28 #if SDL_VIDEO_DRIVER_ANDROID
29 #include <android/native_window.h>
30 #endif
31
32 #include "SDL_sysvideo.h"
33 #include "SDL_log.h"
34 #include "SDL_egl_c.h"
35 #include "SDL_loadso.h"
36 #include "SDL_hints.h"
37
38 #ifdef EGL_KHR_create_context
39 /* EGL_OPENGL_ES3_BIT_KHR was added in version 13 of the extension. */
40 #ifndef EGL_OPENGL_ES3_BIT_KHR
41 #define EGL_OPENGL_ES3_BIT_KHR 0x00000040
42 #endif
43 #endif /* EGL_KHR_create_context */
44
45 #if SDL_VIDEO_DRIVER_RPI
46 /* Raspbian places the OpenGL ES/EGL binaries in a non standard path */
47 #define DEFAULT_EGL ( vc4 ? "libEGL.so.1" : "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 #ifdef SDL_VIDEO_STATIC_ANGLE
85 #define LOAD_FUNC(NAME) \
86 _this->egl_data->NAME = (void *)NAME;
87 #else
88 #define LOAD_FUNC(NAME) \
89 _this->egl_data->NAME = SDL_LoadFunction(_this->egl_data->dll_handle, #NAME); \
90 if (!_this->egl_data->NAME) \
91 { \
92     return SDL_SetError("Could not retrieve EGL function " #NAME); \
93 }
94 #endif
95
96 static const char * SDL_EGL_GetErrorName(EGLint eglErrorCode)
97 {
98 #define SDL_EGL_ERROR_TRANSLATE(e) case e: return #e;
99     switch (eglErrorCode) {
100         SDL_EGL_ERROR_TRANSLATE(EGL_SUCCESS);
101         SDL_EGL_ERROR_TRANSLATE(EGL_NOT_INITIALIZED);
102         SDL_EGL_ERROR_TRANSLATE(EGL_BAD_ACCESS);
103         SDL_EGL_ERROR_TRANSLATE(EGL_BAD_ALLOC);
104         SDL_EGL_ERROR_TRANSLATE(EGL_BAD_ATTRIBUTE);
105         SDL_EGL_ERROR_TRANSLATE(EGL_BAD_CONTEXT);
106         SDL_EGL_ERROR_TRANSLATE(EGL_BAD_CONFIG);
107         SDL_EGL_ERROR_TRANSLATE(EGL_BAD_CURRENT_SURFACE);
108         SDL_EGL_ERROR_TRANSLATE(EGL_BAD_DISPLAY);
109         SDL_EGL_ERROR_TRANSLATE(EGL_BAD_SURFACE);
110         SDL_EGL_ERROR_TRANSLATE(EGL_BAD_MATCH);
111         SDL_EGL_ERROR_TRANSLATE(EGL_BAD_PARAMETER);
112         SDL_EGL_ERROR_TRANSLATE(EGL_BAD_NATIVE_PIXMAP);
113         SDL_EGL_ERROR_TRANSLATE(EGL_BAD_NATIVE_WINDOW);
114         SDL_EGL_ERROR_TRANSLATE(EGL_CONTEXT_LOST);
115     }
116     return "";
117 }
118
119 int SDL_EGL_SetErrorEx(const char * message, const char * eglFunctionName, EGLint eglErrorCode)
120 {
121     const char * errorText = SDL_EGL_GetErrorName(eglErrorCode);
122     char altErrorText[32];
123     if (errorText[0] == '\0') {
124         /* An unknown-to-SDL error code was reported.  Report its hexadecimal value, instead of its name. */
125         SDL_snprintf(altErrorText, SDL_arraysize(altErrorText), "0x%x", (unsigned int)eglErrorCode);
126         errorText = altErrorText;
127     }
128     return SDL_SetError("%s (call to %s failed, reporting an error of %s)", message, eglFunctionName, errorText);
129 }
130
131 /* EGL implementation of SDL OpenGL ES support */
132 typedef enum {
133     SDL_EGL_DISPLAY_EXTENSION,
134     SDL_EGL_CLIENT_EXTENSION
135 } SDL_EGL_ExtensionType;
136
137 static SDL_bool SDL_EGL_HasExtension(_THIS, SDL_EGL_ExtensionType type, const char *ext)
138 {
139     size_t ext_len;
140     const char *ext_override;
141     const char *egl_extstr;
142     const char *ext_start;
143
144     /* Invalid extensions can be rejected early */
145     if (ext == NULL || *ext == 0 || SDL_strchr(ext, ' ') != NULL) {
146         /* SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "SDL_EGL_HasExtension: Invalid EGL extension"); */
147         return SDL_FALSE;
148     }
149
150     /* Extensions can be masked with an environment variable.
151      * Unlike the OpenGL override, this will use the set bits of an integer
152      * to disable the extension.
153      *  Bit   Action
154      *  0     If set, the display extension is masked and not present to SDL.
155      *  1     If set, the client extension is masked and not present to SDL.
156      */
157     ext_override = SDL_getenv(ext);
158     if (ext_override != NULL) {
159         int disable_ext = SDL_atoi(ext_override);
160         if (disable_ext & 0x01 && type == SDL_EGL_DISPLAY_EXTENSION) {
161             return SDL_FALSE;
162         } else if (disable_ext & 0x02 && type == SDL_EGL_CLIENT_EXTENSION) {
163             return SDL_FALSE;
164         }
165     }
166
167     ext_len = SDL_strlen(ext);
168     switch (type) {
169     case SDL_EGL_DISPLAY_EXTENSION:
170         egl_extstr = _this->egl_data->eglQueryString(_this->egl_data->egl_display, EGL_EXTENSIONS);
171         break;
172     case SDL_EGL_CLIENT_EXTENSION:
173         /* EGL_EXT_client_extensions modifies eglQueryString to return client extensions
174          * if EGL_NO_DISPLAY is passed. Implementations without it are required to return NULL.
175          * This behavior is included in EGL 1.5.
176          */
177         egl_extstr = _this->egl_data->eglQueryString(EGL_NO_DISPLAY, EGL_EXTENSIONS);
178         break;
179     default:
180         /* SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "SDL_EGL_HasExtension: Invalid extension type"); */
181         return SDL_FALSE;
182     }
183
184     if (egl_extstr != NULL) {
185         ext_start = egl_extstr;
186
187         while (*ext_start) {
188             ext_start = SDL_strstr(ext_start, ext);
189             if (ext_start == NULL) {
190                 return SDL_FALSE;
191             }
192             /* Check if the match is not just a substring of one of the extensions */
193             if (ext_start == egl_extstr || *(ext_start - 1) == ' ') {
194                 if (ext_start[ext_len] == ' ' || ext_start[ext_len] == 0) {
195                     return SDL_TRUE;
196                 }
197             }
198             /* If the search stopped in the middle of an extension, skip to the end of it */
199             ext_start += ext_len;
200             while (*ext_start != ' ' && *ext_start != 0) {
201                 ext_start++;
202             }
203         }
204     }
205
206     return SDL_FALSE;
207 }
208
209 void *
210 SDL_EGL_GetProcAddress(_THIS, const char *proc)
211 {
212     static char procname[1024];
213     void *retval;
214     
215     /* eglGetProcAddress is busted on Android http://code.google.com/p/android/issues/detail?id=7681 */
216 #if !defined(SDL_VIDEO_DRIVER_ANDROID)
217     if (_this->egl_data->eglGetProcAddress) {
218         retval = _this->egl_data->eglGetProcAddress(proc);
219         if (retval) {
220             return retval;
221         }
222     }
223 #endif
224
225 #ifdef __TIZEN__
226     char *Tizen_Not_Support_API[4] = {
227         "eglSetDamageRegionKHR",
228         "eglSwapBuffersWithDamageEXT",
229         "eglSwapBuffersWithDamageINTEL",
230         "eglSwapBuffersWithDamage"
231     };
232
233     int i;
234     for(i=0; i<4; i++)
235     {
236         if(!SDL_strcmp(proc, Tizen_Not_Support_API[i]))
237             return NULL;
238     }
239 #endif
240
241     retval = SDL_LoadFunction(_this->egl_data->egl_dll_handle, proc);
242     if (!retval && SDL_strlen(proc) <= 1022) {
243         procname[0] = '_';
244         SDL_strlcpy(procname + 1, proc, 1022);
245         retval = SDL_LoadFunction(_this->egl_data->egl_dll_handle, procname);
246     }
247     return retval;
248 }
249
250 void
251 SDL_EGL_UnloadLibrary(_THIS)
252 {
253     if (_this->egl_data) {
254         if (_this->egl_data->egl_display) {
255             _this->egl_data->eglTerminate(_this->egl_data->egl_display);
256             _this->egl_data->egl_display = NULL;
257         }
258
259         if (_this->egl_data->dll_handle) {
260             SDL_UnloadObject(_this->egl_data->dll_handle);
261             _this->egl_data->dll_handle = NULL;
262         }
263         if (_this->egl_data->egl_dll_handle) {
264             SDL_UnloadObject(_this->egl_data->egl_dll_handle);
265             _this->egl_data->egl_dll_handle = NULL;
266         }
267         
268         SDL_free(_this->egl_data);
269         _this->egl_data = NULL;
270     }
271 }
272
273 int
274 SDL_EGL_LoadLibrary(_THIS, const char *egl_path, NativeDisplayType native_display, EGLenum platform)
275 {
276     void *dll_handle = NULL, *egl_dll_handle = NULL; /* The naming is counter intuitive, but hey, I just work here -- Gabriel */
277     const char *path = NULL;
278     int egl_version_major = 0, egl_version_minor = 0;
279 #if SDL_VIDEO_DRIVER_WINDOWS || SDL_VIDEO_DRIVER_WINRT
280     const char *d3dcompiler;
281 #endif
282 #if SDL_VIDEO_DRIVER_RPI
283     SDL_bool vc4 = (0 == access("/sys/module/vc4/", F_OK));
284 #endif
285
286     if (_this->egl_data) {
287         return SDL_SetError("EGL context already created");
288     }
289
290     _this->egl_data = (struct SDL_EGL_VideoData *) SDL_calloc(1, sizeof(SDL_EGL_VideoData));
291     if (!_this->egl_data) {
292         return SDL_OutOfMemory();
293     }
294
295 #if SDL_VIDEO_DRIVER_WINDOWS || SDL_VIDEO_DRIVER_WINRT
296     d3dcompiler = SDL_GetHint(SDL_HINT_VIDEO_WIN_D3DCOMPILER);
297     if (!d3dcompiler) {
298         if (WIN_IsWindowsVistaOrGreater()) {
299             d3dcompiler = "d3dcompiler_46.dll";
300         } else {
301             d3dcompiler = "d3dcompiler_43.dll";
302         }
303     }
304     if (SDL_strcasecmp(d3dcompiler, "none") != 0) {
305         if (SDL_LoadObject(d3dcompiler) == NULL) {
306             SDL_ClearError();
307         }
308     }
309 #endif
310
311 #ifndef SDL_VIDEO_STATIC_ANGLE
312     /* A funny thing, loading EGL.so first does not work on the Raspberry, so we load libGL* first */
313     path = SDL_getenv("SDL_VIDEO_GL_DRIVER");
314     if (path != NULL) {
315         egl_dll_handle = SDL_LoadObject(path);
316     }
317
318     if (egl_dll_handle == NULL) {
319         if (_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES) {
320             if (_this->gl_config.major_version > 1) {
321                 path = DEFAULT_OGL_ES2;
322                 egl_dll_handle = SDL_LoadObject(path);
323 #ifdef ALT_OGL_ES2
324                 if (egl_dll_handle == NULL && !vc4) {
325                     path = ALT_OGL_ES2;
326                     egl_dll_handle = SDL_LoadObject(path);
327                 }
328 #endif
329
330             } else {
331                 path = DEFAULT_OGL_ES;
332                 egl_dll_handle = SDL_LoadObject(path);
333                 if (egl_dll_handle == NULL) {
334                     path = DEFAULT_OGL_ES_PVR;
335                     egl_dll_handle = SDL_LoadObject(path);
336                 }
337 #ifdef ALT_OGL_ES2
338                 if (egl_dll_handle == NULL && !vc4) {
339                     path = ALT_OGL_ES2;
340                     egl_dll_handle = SDL_LoadObject(path);
341                 }
342 #endif
343             }
344         }
345 #ifdef DEFAULT_OGL         
346         else {
347             path = DEFAULT_OGL;
348             egl_dll_handle = SDL_LoadObject(path);
349         }
350 #endif        
351     }
352     _this->egl_data->egl_dll_handle = egl_dll_handle;
353
354     if (egl_dll_handle == NULL) {
355         return SDL_SetError("Could not initialize OpenGL / GLES library");
356     }
357
358     /* Loading libGL* in the previous step took care of loading libEGL.so, but we future proof by double checking */
359     if (egl_path != NULL) {
360         dll_handle = SDL_LoadObject(egl_path);
361     }   
362     /* Try loading a EGL symbol, if it does not work try the default library paths */
363     if (dll_handle == NULL || SDL_LoadFunction(dll_handle, "eglChooseConfig") == NULL) {
364         if (dll_handle != NULL) {
365             SDL_UnloadObject(dll_handle);
366         }
367         path = SDL_getenv("SDL_VIDEO_EGL_DRIVER");
368         if (path == NULL) {
369             path = DEFAULT_EGL;
370         }
371         dll_handle = SDL_LoadObject(path);
372
373 #ifdef ALT_EGL
374         if (dll_handle == NULL && !vc4) {
375             path = ALT_EGL;
376             dll_handle = SDL_LoadObject(path);
377         }
378 #endif
379
380         if (dll_handle == NULL || SDL_LoadFunction(dll_handle, "eglChooseConfig") == NULL) {
381             if (dll_handle != NULL) {
382                 SDL_UnloadObject(dll_handle);
383             }
384             return SDL_SetError("Could not load EGL library");
385         }
386         SDL_ClearError();
387     }
388 #endif
389
390     _this->egl_data->dll_handle = dll_handle;
391
392     /* Load new function pointers */
393     LOAD_FUNC(eglGetDisplay);
394     LOAD_FUNC(eglInitialize);
395     LOAD_FUNC(eglTerminate);
396     LOAD_FUNC(eglGetProcAddress);
397     LOAD_FUNC(eglChooseConfig);
398     LOAD_FUNC(eglGetConfigAttrib);
399     LOAD_FUNC(eglCreateContext);
400     LOAD_FUNC(eglDestroyContext);
401     LOAD_FUNC(eglCreatePbufferSurface);
402     LOAD_FUNC(eglCreateWindowSurface);
403     LOAD_FUNC(eglDestroySurface);
404     LOAD_FUNC(eglMakeCurrent);
405     LOAD_FUNC(eglSwapBuffers);
406     LOAD_FUNC(eglSwapInterval);
407     LOAD_FUNC(eglWaitNative);
408     LOAD_FUNC(eglWaitGL);
409     LOAD_FUNC(eglBindAPI);
410     LOAD_FUNC(eglQueryString);
411     LOAD_FUNC(eglGetError);
412
413     if (_this->egl_data->eglQueryString) {
414         /* EGL 1.5 allows querying for client version */
415         const char *egl_version = _this->egl_data->eglQueryString(EGL_NO_DISPLAY, EGL_VERSION);
416         if (egl_version != NULL) {
417             if (SDL_sscanf(egl_version, "%d.%d", &egl_version_major, &egl_version_minor) != 2) {
418                 egl_version_major = 0;
419                 egl_version_minor = 0;
420                 SDL_LogWarn(SDL_LOG_CATEGORY_VIDEO, "Could not parse EGL version string: %s", egl_version);
421             }
422         }
423     }
424
425     if (egl_version_major == 1 && egl_version_minor == 5) {
426         LOAD_FUNC(eglGetPlatformDisplay);
427     }
428
429     _this->egl_data->egl_display = EGL_NO_DISPLAY;
430 #if !defined(__WINRT__)
431     if (platform) {
432         if (egl_version_major == 1 && egl_version_minor == 5) {
433             _this->egl_data->egl_display = _this->egl_data->eglGetPlatformDisplay(platform, (void *)(size_t)native_display, NULL);
434         } else {
435             if (SDL_EGL_HasExtension(_this, SDL_EGL_CLIENT_EXTENSION, "EGL_EXT_platform_base")) {
436                 _this->egl_data->eglGetPlatformDisplayEXT = SDL_EGL_GetProcAddress(_this, "eglGetPlatformDisplayEXT");
437                 if (_this->egl_data->eglGetPlatformDisplayEXT) {
438                     _this->egl_data->egl_display = _this->egl_data->eglGetPlatformDisplayEXT(platform, (void *)(size_t)native_display, NULL);
439                 }
440             }
441         }
442     }
443     /* Try the implementation-specific eglGetDisplay even if eglGetPlatformDisplay fails */
444     if (_this->egl_data->egl_display == EGL_NO_DISPLAY) {
445         _this->egl_data->egl_display = _this->egl_data->eglGetDisplay(native_display);
446     }
447     if (_this->egl_data->egl_display == EGL_NO_DISPLAY) {
448         return SDL_SetError("Could not get EGL display");
449     }
450     
451     if (_this->egl_data->eglInitialize(_this->egl_data->egl_display, NULL, NULL) != EGL_TRUE) {
452         return SDL_SetError("Could not initialize EGL");
453     }
454 #endif
455
456     if (path) {
457         SDL_strlcpy(_this->gl_config.driver_path, path, sizeof(_this->gl_config.driver_path) - 1);
458     } else {
459         *_this->gl_config.driver_path = '\0';
460     }
461     
462     return 0;
463 }
464
465 int
466 SDL_EGL_ChooseConfig(_THIS) 
467 {
468 /* 64 seems nice. */
469     EGLint attribs[64];
470     EGLint found_configs = 0, value;
471 #ifdef SDL_VIDEO_DRIVER_KMSDRM
472     /* Intel EGL on KMS/DRM (al least) returns invalid configs that confuse the bitdiff search used */
473     /* later in this function, so we simply use the first one when using the KMSDRM driver for now. */
474     EGLConfig configs[1];
475 #else
476     /* 128 seems even nicer here */
477     EGLConfig configs[128];
478 #endif
479     int i, j, best_bitdiff = -1, bitdiff;
480    
481     if (!_this->egl_data) {
482         /* The EGL library wasn't loaded, SDL_GetError() should have info */
483         return -1;
484     }
485   
486     /* Get a valid EGL configuration */
487     i = 0;
488     attribs[i++] = EGL_RED_SIZE;
489     attribs[i++] = _this->gl_config.red_size;
490     attribs[i++] = EGL_GREEN_SIZE;
491     attribs[i++] = _this->gl_config.green_size;
492     attribs[i++] = EGL_BLUE_SIZE;
493     attribs[i++] = _this->gl_config.blue_size;
494     
495     if (_this->gl_config.alpha_size) {
496         attribs[i++] = EGL_ALPHA_SIZE;
497         attribs[i++] = _this->gl_config.alpha_size;
498     }
499     
500     if (_this->gl_config.buffer_size) {
501         attribs[i++] = EGL_BUFFER_SIZE;
502         attribs[i++] = _this->gl_config.buffer_size;
503     }
504     
505     attribs[i++] = EGL_DEPTH_SIZE;
506     attribs[i++] = _this->gl_config.depth_size;
507     
508     if (_this->gl_config.stencil_size) {
509         attribs[i++] = EGL_STENCIL_SIZE;
510         attribs[i++] = _this->gl_config.stencil_size;
511     }
512     
513     if (_this->gl_config.multisamplebuffers) {
514         attribs[i++] = EGL_SAMPLE_BUFFERS;
515         attribs[i++] = _this->gl_config.multisamplebuffers;
516     }
517     
518     if (_this->gl_config.multisamplesamples) {
519         attribs[i++] = EGL_SAMPLES;
520         attribs[i++] = _this->gl_config.multisamplesamples;
521     }
522
523     attribs[i++] = EGL_RENDERABLE_TYPE;
524     if (_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES) {
525 #ifdef EGL_KHR_create_context
526         if (_this->gl_config.major_version >= 3 &&
527             SDL_EGL_HasExtension(_this, SDL_EGL_DISPLAY_EXTENSION, "EGL_KHR_create_context")) {
528             attribs[i++] = EGL_OPENGL_ES3_BIT_KHR;
529         } else
530 #endif
531         if (_this->gl_config.major_version >= 2) {
532             attribs[i++] = EGL_OPENGL_ES2_BIT;
533         } else {
534             attribs[i++] = EGL_OPENGL_ES_BIT;
535         }
536         _this->egl_data->eglBindAPI(EGL_OPENGL_ES_API);
537     } else {
538         attribs[i++] = EGL_OPENGL_BIT;
539         _this->egl_data->eglBindAPI(EGL_OPENGL_API);
540     }
541
542     if (_this->egl_data->egl_surfacetype) {
543         attribs[i++] = EGL_SURFACE_TYPE;
544         attribs[i++] = _this->egl_data->egl_surfacetype;
545     }
546
547     attribs[i++] = EGL_NONE;
548
549     if (_this->egl_data->eglChooseConfig(_this->egl_data->egl_display,
550         attribs,
551         configs, SDL_arraysize(configs),
552         &found_configs) == EGL_FALSE ||
553         found_configs == 0) {
554         return SDL_EGL_SetError("Couldn't find matching EGL config", "eglChooseConfig");
555     }
556
557     /* eglChooseConfig returns a number of configurations that match or exceed the requested attribs. */
558     /* From those, we select the one that matches our requirements more closely via a makeshift algorithm */
559
560     for (i = 0; i < found_configs; i++ ) {
561         bitdiff = 0;
562         for (j = 0; j < SDL_arraysize(attribs) - 1; j += 2) {
563             if (attribs[j] == EGL_NONE) {
564                break;
565             }
566             
567             if ( attribs[j+1] != EGL_DONT_CARE && (
568                 attribs[j] == EGL_RED_SIZE ||
569                 attribs[j] == EGL_GREEN_SIZE ||
570                 attribs[j] == EGL_BLUE_SIZE ||
571                 attribs[j] == EGL_ALPHA_SIZE ||
572                 attribs[j] == EGL_DEPTH_SIZE ||
573                 attribs[j] == EGL_STENCIL_SIZE)) {
574                 _this->egl_data->eglGetConfigAttrib(_this->egl_data->egl_display, configs[i], attribs[j], &value);
575                 bitdiff += value - attribs[j + 1]; /* value is always >= attrib */
576             }
577         }
578
579         if (bitdiff < best_bitdiff || best_bitdiff == -1) {
580             _this->egl_data->egl_config = configs[i];
581             
582             best_bitdiff = bitdiff;
583         }
584
585         if (bitdiff == 0) {
586             break; /* we found an exact match! */
587         }
588     }
589     
590     return 0;
591 }
592
593 SDL_GLContext
594 SDL_EGL_CreateContext(_THIS, EGLSurface egl_surface)
595 {
596     /* max 14 values plus terminator. */
597     EGLint attribs[40];
598     int attr = 0;
599
600     EGLContext egl_context, share_context = EGL_NO_CONTEXT;
601     EGLint profile_mask = _this->gl_config.profile_mask;
602     EGLint major_version = _this->gl_config.major_version;
603     EGLint minor_version = _this->gl_config.minor_version;
604     SDL_bool profile_es = (profile_mask == SDL_GL_CONTEXT_PROFILE_ES);
605
606     if (!_this->egl_data) {
607         /* The EGL library wasn't loaded, SDL_GetError() should have info */
608         return NULL;
609     }
610
611     if (_this->gl_config.share_with_current_context) {
612         share_context = (EGLContext)SDL_GL_GetCurrentContext();
613     }
614
615     /* Set the context version and other attributes. */
616     if ((major_version < 3 || (minor_version == 0 && profile_es)) &&
617         _this->gl_config.flags == 0 &&
618         (profile_mask == 0 || profile_es)) {
619         /* Create a context without using EGL_KHR_create_context attribs.
620          * When creating a GLES context without EGL_KHR_create_context we can
621          * only specify the major version. When creating a desktop GL context
622          * we can't specify any version, so we only try in that case when the
623          * version is less than 3.0 (matches SDL's GLX/WGL behavior.)
624          */
625         if (profile_es) {
626             attribs[attr++] = EGL_CONTEXT_CLIENT_VERSION;
627             attribs[attr++] = SDL_max(major_version, 1);
628         }
629     } else {
630 #ifdef EGL_KHR_create_context
631         /* The Major/minor version, context profiles, and context flags can
632          * only be specified when this extension is available.
633          */
634         if (SDL_EGL_HasExtension(_this, SDL_EGL_DISPLAY_EXTENSION, "EGL_KHR_create_context")) {
635             attribs[attr++] = EGL_CONTEXT_MAJOR_VERSION_KHR;
636             attribs[attr++] = major_version;
637             attribs[attr++] = EGL_CONTEXT_MINOR_VERSION_KHR;
638             attribs[attr++] = minor_version;
639
640             /* SDL profile bits match EGL profile bits. */
641             if (profile_mask != 0 && profile_mask != SDL_GL_CONTEXT_PROFILE_ES) {
642                 attribs[attr++] = EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR;
643                 attribs[attr++] = profile_mask;
644             }
645
646             /* SDL flags match EGL flags. */
647             if (_this->gl_config.flags != 0) {
648                 attribs[attr++] = EGL_CONTEXT_FLAGS_KHR;
649                 attribs[attr++] = _this->gl_config.flags;
650             }
651         } else
652 #endif /* EGL_KHR_create_context */
653         {
654             SDL_SetError("Could not create EGL context (context attributes are not supported)");
655             return NULL;
656         }
657     }
658
659 #ifdef __TIZEN__
660     if ((SDL_EGL_HasExtension(_this, SDL_EGL_DISPLAY_EXTENSION, "EGL_IMG_context_priority"))
661          && (_this->gl_config.context_priority_level > SDL_GL_CONTEXT_PRIORITY_NONE)) {
662         if (_this->gl_config.flags == SDL_GL_CONTEXT_PRIORITY_HIGH) {
663            attribs[attr++] = EGL_CONTEXT_PRIORITY_HIGH_IMG;
664         }
665         else if (_this->gl_config.flags == SDL_GL_CONTEXT_PRIORITY_MEDIUM) {
666            attribs[attr++] = EGL_CONTEXT_PRIORITY_MEDIUM_IMG;
667         }
668         else if (_this->gl_config.flags == SDL_GL_CONTEXT_PRIORITY_LOW) {
669            attribs[attr++] = EGL_CONTEXT_PRIORITY_LOW_IMG;
670         }
671     }
672 #endif
673
674     if (_this->gl_config.no_error) {
675 #ifdef EGL_KHR_create_context_no_error
676         if (SDL_EGL_HasExtension(_this, SDL_EGL_DISPLAY_EXTENSION, "EGL_KHR_create_context_no_error")) {
677             attribs[attr++] = EGL_CONTEXT_OPENGL_NO_ERROR_KHR;
678             attribs[attr++] = _this->gl_config.no_error;
679         } else
680 #endif
681         {
682             SDL_SetError("EGL implementation does not support no_error contexts");
683             return NULL;
684         }
685     }
686
687     if (_this->gl_config.no_error) {
688 #ifdef EGL_KHR_create_context_no_error
689         if (SDL_EGL_HasExtension(_this, SDL_EGL_DISPLAY_EXTENSION, "EGL_KHR_create_context_no_error")) {
690             attribs[attr++] = EGL_CONTEXT_OPENGL_NO_ERROR_KHR;
691             attribs[attr++] = _this->gl_config.no_error;
692         } else
693 #endif
694         {
695             SDL_SetError("EGL implementation does not support no_error contexts");
696             return NULL;
697         }
698     }
699
700     attribs[attr++] = EGL_NONE;
701
702     /* Bind the API */
703     if (profile_es) {
704         _this->egl_data->eglBindAPI(EGL_OPENGL_ES_API);
705     } else {
706         _this->egl_data->eglBindAPI(EGL_OPENGL_API);
707     }
708
709     egl_context = _this->egl_data->eglCreateContext(_this->egl_data->egl_display,
710                                       _this->egl_data->egl_config,
711                                       share_context, attribs);
712
713     if (egl_context == EGL_NO_CONTEXT) {
714         SDL_EGL_SetError("Could not create EGL context", "eglCreateContext");
715         return NULL;
716     }
717
718     _this->egl_data->egl_swapinterval = 0;
719
720     if (SDL_EGL_MakeCurrent(_this, egl_surface, egl_context) < 0) {
721         /* Save the SDL error set by SDL_EGL_MakeCurrent */
722         char errorText[1024];
723         SDL_strlcpy(errorText, SDL_GetError(), SDL_arraysize(errorText));
724
725         /* Delete the context, which may alter the value returned by SDL_GetError() */
726         SDL_EGL_DeleteContext(_this, egl_context);
727
728         /* Restore the SDL error */
729         SDL_SetError("%s", errorText);
730
731         return NULL;
732     }
733
734     return (SDL_GLContext) egl_context;
735 }
736
737 int
738 SDL_EGL_MakeCurrent(_THIS, EGLSurface egl_surface, SDL_GLContext context)
739 {
740     EGLContext egl_context = (EGLContext) context;
741
742     if (!_this->egl_data) {
743         return SDL_SetError("OpenGL not initialized");
744     }
745 #ifdef __TIZEN__
746     if ((SDL_EGL_HasExtension(_this, SDL_EGL_DISPLAY_EXTENSION, "EGL_KHR_surfaceless_context")) && (!egl_surface)) {
747         if (!_this->egl_data->eglMakeCurrent(_this->egl_data->egl_display,
748                 EGL_NO_SURFACE , EGL_NO_SURFACE , egl_context)) {
749                 return SDL_SetError("Unable to make EGL context current");
750             }
751     }
752     else
753 #endif
754     {
755         /* The android emulator crashes badly if you try to eglMakeCurrent
756         * with a valid context and invalid surface, so we have to check for both here.
757         */
758         if (!egl_context || !egl_surface) {
759             _this->egl_data->eglMakeCurrent(_this->egl_data->egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
760         } else {
761             if (!_this->egl_data->eglMakeCurrent(_this->egl_data->egl_display,
762                 egl_surface, egl_surface, egl_context)) {
763                 return SDL_EGL_SetError("Unable to make EGL context current", "eglMakeCurrent");
764             }
765         }
766     }
767     return 0;
768 }
769
770 int
771 SDL_EGL_SetSwapInterval(_THIS, int interval)
772 {
773     EGLBoolean status;
774     
775     if (!_this->egl_data) {
776         return SDL_SetError("EGL not initialized");
777     }
778     
779     status = _this->egl_data->eglSwapInterval(_this->egl_data->egl_display, interval);
780     if (status == EGL_TRUE) {
781         _this->egl_data->egl_swapinterval = interval;
782         return 0;
783     }
784     
785     return SDL_EGL_SetError("Unable to set the EGL swap interval", "eglSwapInterval");
786 }
787
788 int
789 SDL_EGL_GetSwapInterval(_THIS)
790 {
791     if (!_this->egl_data) {
792         SDL_SetError("EGL not initialized");
793         return 0;
794     }
795     
796     return _this->egl_data->egl_swapinterval;
797 }
798
799 int
800 SDL_EGL_SwapBuffers(_THIS, EGLSurface egl_surface)
801 {
802     if (!_this->egl_data->eglSwapBuffers(_this->egl_data->egl_display, egl_surface)) {
803         return SDL_EGL_SetError("unable to show color buffer in an OS-native window", "eglSwapBuffers");
804     }
805     return 0;
806 }
807
808 void
809 SDL_EGL_DeleteContext(_THIS, SDL_GLContext context)
810 {
811     EGLContext egl_context = (EGLContext) context;
812
813     /* Clean up GLES and EGL */
814     if (!_this->egl_data) {
815         return;
816     }
817     
818     if (egl_context != NULL && egl_context != EGL_NO_CONTEXT) {
819         SDL_EGL_MakeCurrent(_this, NULL, NULL);
820         _this->egl_data->eglDestroyContext(_this->egl_data->egl_display, egl_context);
821     }
822         
823 }
824
825 EGLSurface *
826 SDL_EGL_CreateSurface(_THIS, NativeWindowType nw) 
827 {
828     /* max 2 values plus terminator. */
829     EGLint attribs[3];
830     int attr = 0;
831         
832     EGLSurface * surface;
833
834     if (SDL_EGL_ChooseConfig(_this) != 0) {
835         return EGL_NO_SURFACE;
836     }
837     
838 #if SDL_VIDEO_DRIVER_ANDROID
839     {
840         /* Android docs recommend doing this!
841          * Ref: http://developer.android.com/reference/android/app/NativeActivity.html 
842          */
843         EGLint format;
844         _this->egl_data->eglGetConfigAttrib(_this->egl_data->egl_display,
845                                             _this->egl_data->egl_config, 
846                                             EGL_NATIVE_VISUAL_ID, &format);
847
848         ANativeWindow_setBuffersGeometry(nw, 0, 0, format);
849     }
850 #endif    
851     if (_this->gl_config.framebuffer_srgb_capable) {
852 #ifdef EGL_KHR_gl_colorspace
853         if (SDL_EGL_HasExtension(_this, SDL_EGL_DISPLAY_EXTENSION, "EGL_KHR_gl_colorspace")) {
854             attribs[attr++] = EGL_GL_COLORSPACE_KHR;
855             attribs[attr++] = EGL_GL_COLORSPACE_SRGB_KHR;
856         } else
857 #endif
858         {
859             SDL_SetError("EGL implementation does not support sRGB system framebuffers");
860             return EGL_NO_SURFACE;
861         }
862     }
863         
864     attribs[attr++] = EGL_NONE;
865     
866     surface = _this->egl_data->eglCreateWindowSurface(
867             _this->egl_data->egl_display,
868             _this->egl_data->egl_config,
869             nw, &attribs[0]);
870     if (surface == EGL_NO_SURFACE) {
871         SDL_EGL_SetError("unable to create an EGL window surface", "eglCreateWindowSurface");
872     }
873     return surface;
874 }
875
876 void
877 SDL_EGL_DestroySurface(_THIS, EGLSurface egl_surface) 
878 {
879     if (!_this->egl_data) {
880         return;
881     }
882     
883     if (egl_surface != EGL_NO_SURFACE) {
884         _this->egl_data->eglDestroySurface(_this->egl_data->egl_display, egl_surface);
885     }
886 }
887
888 #endif /* SDL_VIDEO_OPENGL_EGL */
889
890 /* vi: set ts=4 sw=4 expandtab: */
891