SDL 2.0
SDL_egl.c
Go to the documentation of this file.
1/*
2 * Simple DirectMedia Layer
3 * Copyright (C) 1997-2019 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_log.h"
35#include "SDL_egl_c.h"
36#include "SDL_loadso.h"
37#include "SDL_hints.h"
38
39#ifdef EGL_KHR_create_context
40/* EGL_OPENGL_ES3_BIT_KHR was added in version 13 of the extension. */
41#ifndef EGL_OPENGL_ES3_BIT_KHR
42#define EGL_OPENGL_ES3_BIT_KHR 0x00000040
43#endif
44#endif /* EGL_KHR_create_context */
45
46#if SDL_VIDEO_DRIVER_RPI
47/* Raspbian places the OpenGL ES/EGL binaries in a non standard path */
48#define DEFAULT_EGL ( vc4 ? "libEGL.so.1" : "libbrcmEGL.so" )
49#define DEFAULT_OGL_ES2 ( vc4 ? "libGLESv2.so.2" : "libbrcmGLESv2.so" )
50#define ALT_EGL "libEGL.so"
51#define ALT_OGL_ES2 "libGLESv2.so"
52#define DEFAULT_OGL_ES_PVR ( vc4 ? "libGLES_CM.so.1" : "libbrcmGLESv2.so" )
53#define DEFAULT_OGL_ES ( vc4 ? "libGLESv1_CM.so.1" : "libbrcmGLESv2.so" )
54
55#elif SDL_VIDEO_DRIVER_ANDROID || SDL_VIDEO_DRIVER_VIVANTE
56/* Android */
57#define DEFAULT_EGL "libEGL.so"
58#define DEFAULT_OGL_ES2 "libGLESv2.so"
59#define DEFAULT_OGL_ES_PVR "libGLES_CM.so"
60#define DEFAULT_OGL_ES "libGLESv1_CM.so"
61
62#elif SDL_VIDEO_DRIVER_WINDOWS || SDL_VIDEO_DRIVER_WINRT
63/* EGL AND OpenGL ES support via ANGLE */
64#define DEFAULT_EGL "libEGL.dll"
65#define DEFAULT_OGL_ES2 "libGLESv2.dll"
66#define DEFAULT_OGL_ES_PVR "libGLES_CM.dll"
67#define DEFAULT_OGL_ES "libGLESv1_CM.dll"
68
69#elif SDL_VIDEO_DRIVER_COCOA
70/* EGL AND OpenGL ES support via ANGLE */
71#define DEFAULT_EGL "libEGL.dylib"
72#define DEFAULT_OGL_ES2 "libGLESv2.dylib"
73#define DEFAULT_OGL_ES_PVR "libGLES_CM.dylib" //???
74#define DEFAULT_OGL_ES "libGLESv1_CM.dylib" //???
75
76#else
77/* Desktop Linux */
78#define DEFAULT_OGL "libGL.so.1"
79#define DEFAULT_EGL "libEGL.so.1"
80#define DEFAULT_OGL_ES2 "libGLESv2.so.2"
81#define DEFAULT_OGL_ES_PVR "libGLES_CM.so.1"
82#define DEFAULT_OGL_ES "libGLESv1_CM.so.1"
83#endif /* SDL_VIDEO_DRIVER_RPI */
84
85#ifdef SDL_VIDEO_STATIC_ANGLE
86#define LOAD_FUNC(NAME) \
87_this->egl_data->NAME = (void *)NAME;
88#else
89#define LOAD_FUNC(NAME) \
90_this->egl_data->NAME = SDL_LoadFunction(_this->egl_data->dll_handle, #NAME); \
91if (!_this->egl_data->NAME) \
92{ \
93 return SDL_SetError("Could not retrieve EGL function " #NAME); \
94}
95#endif
96
97static const char * SDL_EGL_GetErrorName(EGLint eglErrorCode)
98{
99#define SDL_EGL_ERROR_TRANSLATE(e) case e: return #e;
100 switch (eglErrorCode) {
101 SDL_EGL_ERROR_TRANSLATE(EGL_SUCCESS);
102 SDL_EGL_ERROR_TRANSLATE(EGL_NOT_INITIALIZED);
103 SDL_EGL_ERROR_TRANSLATE(EGL_BAD_ACCESS);
104 SDL_EGL_ERROR_TRANSLATE(EGL_BAD_ALLOC);
105 SDL_EGL_ERROR_TRANSLATE(EGL_BAD_ATTRIBUTE);
106 SDL_EGL_ERROR_TRANSLATE(EGL_BAD_CONTEXT);
107 SDL_EGL_ERROR_TRANSLATE(EGL_BAD_CONFIG);
108 SDL_EGL_ERROR_TRANSLATE(EGL_BAD_CURRENT_SURFACE);
109 SDL_EGL_ERROR_TRANSLATE(EGL_BAD_DISPLAY);
110 SDL_EGL_ERROR_TRANSLATE(EGL_BAD_SURFACE);
111 SDL_EGL_ERROR_TRANSLATE(EGL_BAD_MATCH);
112 SDL_EGL_ERROR_TRANSLATE(EGL_BAD_PARAMETER);
113 SDL_EGL_ERROR_TRANSLATE(EGL_BAD_NATIVE_PIXMAP);
114 SDL_EGL_ERROR_TRANSLATE(EGL_BAD_NATIVE_WINDOW);
115 SDL_EGL_ERROR_TRANSLATE(EGL_CONTEXT_LOST);
116 }
117 return "";
118}
119
120int SDL_EGL_SetErrorEx(const char * message, const char * eglFunctionName, EGLint eglErrorCode)
121{
122 const char * errorText = SDL_EGL_GetErrorName(eglErrorCode);
123 char altErrorText[32];
124 if (errorText[0] == '\0') {
125 /* An unknown-to-SDL error code was reported. Report its hexadecimal value, instead of its name. */
126 SDL_snprintf(altErrorText, SDL_arraysize(altErrorText), "0x%x", (unsigned int)eglErrorCode);
127 errorText = altErrorText;
128 }
129 return SDL_SetError("%s (call to %s failed, reporting an error of %s)", message, eglFunctionName, errorText);
130}
131
132/* EGL implementation of SDL OpenGL ES support */
133typedef enum {
134 SDL_EGL_DISPLAY_EXTENSION,
135 SDL_EGL_CLIENT_EXTENSION
136} SDL_EGL_ExtensionType;
137
138static SDL_bool SDL_EGL_HasExtension(_THIS, SDL_EGL_ExtensionType type, const char *ext)
139{
140 size_t ext_len;
141 const char *ext_override;
142 const char *egl_extstr;
143 const char *ext_start;
144
145 /* Invalid extensions can be rejected early */
146 if (ext == NULL || *ext == 0 || SDL_strchr(ext, ' ') != NULL) {
147 /* SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "SDL_EGL_HasExtension: Invalid EGL extension"); */
148 return SDL_FALSE;
149 }
150
151 /* Extensions can be masked with an environment variable.
152 * Unlike the OpenGL override, this will use the set bits of an integer
153 * to disable the extension.
154 * Bit Action
155 * 0 If set, the display extension is masked and not present to SDL.
156 * 1 If set, the client extension is masked and not present to SDL.
157 */
158 ext_override = SDL_getenv(ext);
159 if (ext_override != NULL) {
160 int disable_ext = SDL_atoi(ext_override);
161 if (disable_ext & 0x01 && type == SDL_EGL_DISPLAY_EXTENSION) {
162 return SDL_FALSE;
163 } else if (disable_ext & 0x02 && type == SDL_EGL_CLIENT_EXTENSION) {
164 return SDL_FALSE;
165 }
166 }
167
168 ext_len = SDL_strlen(ext);
169 switch (type) {
170 case SDL_EGL_DISPLAY_EXTENSION:
171 egl_extstr = _this->egl_data->eglQueryString(_this->egl_data->egl_display, EGL_EXTENSIONS);
172 break;
173 case SDL_EGL_CLIENT_EXTENSION:
174 /* EGL_EXT_client_extensions modifies eglQueryString to return client extensions
175 * if EGL_NO_DISPLAY is passed. Implementations without it are required to return NULL.
176 * This behavior is included in EGL 1.5.
177 */
178 egl_extstr = _this->egl_data->eglQueryString(EGL_NO_DISPLAY, EGL_EXTENSIONS);
179 break;
180 default:
181 /* SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "SDL_EGL_HasExtension: Invalid extension type"); */
182 return SDL_FALSE;
183 }
184
185 if (egl_extstr != NULL) {
186 ext_start = egl_extstr;
187
188 while (*ext_start) {
189 ext_start = SDL_strstr(ext_start, ext);
190 if (ext_start == NULL) {
191 return SDL_FALSE;
192 }
193 /* Check if the match is not just a substring of one of the extensions */
194 if (ext_start == egl_extstr || *(ext_start - 1) == ' ') {
195 if (ext_start[ext_len] == ' ' || ext_start[ext_len] == 0) {
196 return SDL_TRUE;
197 }
198 }
199 /* If the search stopped in the middle of an extension, skip to the end of it */
200 ext_start += ext_len;
201 while (*ext_start != ' ' && *ext_start != 0) {
202 ext_start++;
203 }
204 }
205 }
206
207 return SDL_FALSE;
208}
209
210void *
211SDL_EGL_GetProcAddress(_THIS, const char *proc)
212{
213 static char procname[1024];
214 void *retval;
215
216 /* eglGetProcAddress is busted on Android http://code.google.com/p/android/issues/detail?id=7681 */
217#if !defined(SDL_VIDEO_DRIVER_ANDROID)
218 if (_this->egl_data->eglGetProcAddress) {
219 retval = _this->egl_data->eglGetProcAddress(proc);
220 if (retval) {
221 return retval;
222 }
223 }
224#endif
225
226 retval = SDL_LoadFunction(_this->egl_data->egl_dll_handle, proc);
227 if (!retval && SDL_strlen(proc) <= 1022) {
228 procname[0] = '_';
229 SDL_strlcpy(procname + 1, proc, 1022);
230 retval = SDL_LoadFunction(_this->egl_data->egl_dll_handle, procname);
231 }
232 return retval;
233}
234
235void
236SDL_EGL_UnloadLibrary(_THIS)
237{
238 if (_this->egl_data) {
239 if (_this->egl_data->egl_display) {
240 _this->egl_data->eglTerminate(_this->egl_data->egl_display);
241 _this->egl_data->egl_display = NULL;
242 }
243
244 if (_this->egl_data->dll_handle) {
246 _this->egl_data->dll_handle = NULL;
247 }
248 if (_this->egl_data->egl_dll_handle) {
249 SDL_UnloadObject(_this->egl_data->egl_dll_handle);
250 _this->egl_data->egl_dll_handle = NULL;
251 }
252
253 SDL_free(_this->egl_data);
254 _this->egl_data = NULL;
255 }
256}
257
258int
259SDL_EGL_LoadLibrary(_THIS, const char *egl_path, NativeDisplayType native_display, EGLenum platform)
260{
261 void *dll_handle = NULL, *egl_dll_handle = NULL; /* The naming is counter intuitive, but hey, I just work here -- Gabriel */
262 const char *path = NULL;
263 int egl_version_major = 0, egl_version_minor = 0;
264#if SDL_VIDEO_DRIVER_WINDOWS || SDL_VIDEO_DRIVER_WINRT
265 const char *d3dcompiler;
266#endif
267#if SDL_VIDEO_DRIVER_RPI
268 SDL_bool vc4 = (0 == access("/sys/module/vc4/", F_OK));
269#endif
270
271 if (_this->egl_data) {
272 return SDL_SetError("EGL context already created");
273 }
274
275 _this->egl_data = (struct SDL_EGL_VideoData *) SDL_calloc(1, sizeof(SDL_EGL_VideoData));
276 if (!_this->egl_data) {
277 return SDL_OutOfMemory();
278 }
279
280#if SDL_VIDEO_DRIVER_WINDOWS || SDL_VIDEO_DRIVER_WINRT
282 if (d3dcompiler) {
283 if (SDL_strcasecmp(d3dcompiler, "none") != 0) {
284 if (SDL_LoadObject(d3dcompiler) == NULL) {
286 }
287 }
288 } else {
290 /* Try the newer d3d compilers first */
291 const char *d3dcompiler_list[] = {
292 "d3dcompiler_47.dll", "d3dcompiler_46.dll",
293 };
294 int i;
295
296 for (i = 0; i < SDL_arraysize(d3dcompiler_list); ++i) {
297 if (SDL_LoadObject(d3dcompiler_list[i]) != NULL) {
298 break;
299 }
301 }
302 } else {
303 if (SDL_LoadObject("d3dcompiler_43.dll") == NULL) {
305 }
306 }
307 }
308#endif
309
310#ifndef SDL_VIDEO_STATIC_ANGLE
311 /* A funny thing, loading EGL.so first does not work on the Raspberry, so we load libGL* first */
312 path = SDL_getenv("SDL_VIDEO_GL_DRIVER");
313 if (path != NULL) {
314 egl_dll_handle = SDL_LoadObject(path);
315 }
316
317 if (egl_dll_handle == NULL) {
319 if (_this->gl_config.major_version > 1) {
320 path = DEFAULT_OGL_ES2;
321 egl_dll_handle = SDL_LoadObject(path);
322#ifdef ALT_OGL_ES2
323 if (egl_dll_handle == NULL && !vc4) {
324 path = ALT_OGL_ES2;
325 egl_dll_handle = SDL_LoadObject(path);
326 }
327#endif
328
329 } else {
330 path = DEFAULT_OGL_ES;
331 egl_dll_handle = SDL_LoadObject(path);
332 if (egl_dll_handle == NULL) {
333 path = DEFAULT_OGL_ES_PVR;
334 egl_dll_handle = SDL_LoadObject(path);
335 }
336#ifdef ALT_OGL_ES2
337 if (egl_dll_handle == NULL && !vc4) {
338 path = ALT_OGL_ES2;
339 egl_dll_handle = SDL_LoadObject(path);
340 }
341#endif
342 }
343 }
344#ifdef DEFAULT_OGL
345 else {
346 path = DEFAULT_OGL;
347 egl_dll_handle = SDL_LoadObject(path);
348 }
349#endif
350 }
351 _this->egl_data->egl_dll_handle = egl_dll_handle;
352
353 if (egl_dll_handle == NULL) {
354 return SDL_SetError("Could not initialize OpenGL / GLES library");
355 }
356
357 /* Loading libGL* in the previous step took care of loading libEGL.so, but we future proof by double checking */
358 if (egl_path != NULL) {
359 dll_handle = SDL_LoadObject(egl_path);
360 }
361 /* Try loading a EGL symbol, if it does not work try the default library paths */
362 if (dll_handle == NULL || SDL_LoadFunction(dll_handle, "eglChooseConfig") == NULL) {
363 if (dll_handle != NULL) {
364 SDL_UnloadObject(dll_handle);
365 }
366 path = SDL_getenv("SDL_VIDEO_EGL_DRIVER");
367 if (path == NULL) {
368 path = DEFAULT_EGL;
369 }
370 dll_handle = SDL_LoadObject(path);
371
372#ifdef ALT_EGL
373 if (dll_handle == NULL && !vc4) {
374 path = ALT_EGL;
375 dll_handle = SDL_LoadObject(path);
376 }
377#endif
378
379 if (dll_handle == NULL || SDL_LoadFunction(dll_handle, "eglChooseConfig") == NULL) {
380 if (dll_handle != NULL) {
381 SDL_UnloadObject(dll_handle);
382 }
383 return SDL_SetError("Could not load EGL library");
384 }
386 }
387#endif
388
389 _this->egl_data->dll_handle = dll_handle;
390
391 /* Load new function pointers */
392 LOAD_FUNC(eglGetDisplay);
393 LOAD_FUNC(eglInitialize);
394 LOAD_FUNC(eglTerminate);
395 LOAD_FUNC(eglGetProcAddress);
396 LOAD_FUNC(eglChooseConfig);
397 LOAD_FUNC(eglGetConfigAttrib);
398 LOAD_FUNC(eglCreateContext);
399 LOAD_FUNC(eglDestroyContext);
400 LOAD_FUNC(eglCreatePbufferSurface);
401 LOAD_FUNC(eglCreateWindowSurface);
402 LOAD_FUNC(eglDestroySurface);
403 LOAD_FUNC(eglMakeCurrent);
404 LOAD_FUNC(eglSwapBuffers);
405 LOAD_FUNC(eglSwapInterval);
406 LOAD_FUNC(eglWaitNative);
407 LOAD_FUNC(eglWaitGL);
408 LOAD_FUNC(eglBindAPI);
409 LOAD_FUNC(eglQueryString);
410 LOAD_FUNC(eglGetError);
411
412 if (_this->egl_data->eglQueryString) {
413 /* EGL 1.5 allows querying for client version */
414 const char *egl_version = _this->egl_data->eglQueryString(EGL_NO_DISPLAY, EGL_VERSION);
415 if (egl_version != NULL) {
416 if (SDL_sscanf(egl_version, "%d.%d", &egl_version_major, &egl_version_minor) != 2) {
417 egl_version_major = 0;
418 egl_version_minor = 0;
419 SDL_LogWarn(SDL_LOG_CATEGORY_VIDEO, "Could not parse EGL version string: %s", egl_version);
420 }
421 }
422 }
423
424 _this->egl_data->egl_version_major = egl_version_major;
425 _this->egl_data->egl_version_minor = egl_version_minor;
426
427 if (egl_version_major == 1 && egl_version_minor == 5) {
428 LOAD_FUNC(eglGetPlatformDisplay);
429 }
430
431 _this->egl_data->egl_display = EGL_NO_DISPLAY;
432#if !defined(__WINRT__)
433 if (platform) {
434 if (egl_version_major == 1 && egl_version_minor == 5) {
435 _this->egl_data->egl_display = _this->egl_data->eglGetPlatformDisplay(platform, (void *)(size_t)native_display, NULL);
436 } else {
437 if (SDL_EGL_HasExtension(_this, SDL_EGL_CLIENT_EXTENSION, "EGL_EXT_platform_base")) {
438 _this->egl_data->eglGetPlatformDisplayEXT = SDL_EGL_GetProcAddress(_this, "eglGetPlatformDisplayEXT");
439 if (_this->egl_data->eglGetPlatformDisplayEXT) {
440 _this->egl_data->egl_display = _this->egl_data->eglGetPlatformDisplayEXT(platform, (void *)(size_t)native_display, NULL);
441 }
442 }
443 }
444 }
445 /* Try the implementation-specific eglGetDisplay even if eglGetPlatformDisplay fails */
446 if (_this->egl_data->egl_display == EGL_NO_DISPLAY) {
447 _this->egl_data->egl_display = _this->egl_data->eglGetDisplay(native_display);
448 }
449 if (_this->egl_data->egl_display == EGL_NO_DISPLAY) {
450 return SDL_SetError("Could not get EGL display");
451 }
452
453 if (_this->egl_data->eglInitialize(_this->egl_data->egl_display, NULL, NULL) != EGL_TRUE) {
454 return SDL_SetError("Could not initialize EGL");
455 }
456#endif
457
458 if (path) {
460 } else {
461 *_this->gl_config.driver_path = '\0';
462 }
463
464 return 0;
465}
466
467void
468SDL_EGL_SetRequiredVisualId(_THIS, int visual_id)
469{
470 _this->egl_data->egl_required_visual_id=visual_id;
471}
472
473#ifdef DUMP_EGL_CONFIG
474
475#define ATTRIBUTE(_attr) { _attr, #_attr }
476
477typedef struct {
479 char const* name;
480} Attribute;
481
482Attribute attributes[] = {
483 ATTRIBUTE( EGL_BUFFER_SIZE ),
484 ATTRIBUTE( EGL_ALPHA_SIZE ),
485 ATTRIBUTE( EGL_BLUE_SIZE ),
486 ATTRIBUTE( EGL_GREEN_SIZE ),
487 ATTRIBUTE( EGL_RED_SIZE ),
488 ATTRIBUTE( EGL_DEPTH_SIZE ),
489 ATTRIBUTE( EGL_STENCIL_SIZE ),
490 ATTRIBUTE( EGL_CONFIG_CAVEAT ),
491 ATTRIBUTE( EGL_CONFIG_ID ),
492 ATTRIBUTE( EGL_LEVEL ),
493 ATTRIBUTE( EGL_MAX_PBUFFER_HEIGHT ),
494 ATTRIBUTE( EGL_MAX_PBUFFER_WIDTH ),
495 ATTRIBUTE( EGL_MAX_PBUFFER_PIXELS ),
496 ATTRIBUTE( EGL_NATIVE_RENDERABLE ),
497 ATTRIBUTE( EGL_NATIVE_VISUAL_ID ),
498 ATTRIBUTE( EGL_NATIVE_VISUAL_TYPE ),
499 ATTRIBUTE( EGL_SAMPLES ),
500 ATTRIBUTE( EGL_SAMPLE_BUFFERS ),
501 ATTRIBUTE( EGL_SURFACE_TYPE ),
502 ATTRIBUTE( EGL_TRANSPARENT_TYPE ),
503 ATTRIBUTE( EGL_TRANSPARENT_BLUE_VALUE ),
504 ATTRIBUTE( EGL_TRANSPARENT_GREEN_VALUE ),
505 ATTRIBUTE( EGL_TRANSPARENT_RED_VALUE ),
506 ATTRIBUTE( EGL_BIND_TO_TEXTURE_RGB ),
507 ATTRIBUTE( EGL_BIND_TO_TEXTURE_RGBA ),
508 ATTRIBUTE( EGL_MIN_SWAP_INTERVAL ),
509 ATTRIBUTE( EGL_MAX_SWAP_INTERVAL ),
510 ATTRIBUTE( EGL_LUMINANCE_SIZE ),
511 ATTRIBUTE( EGL_ALPHA_MASK_SIZE ),
512 ATTRIBUTE( EGL_COLOR_BUFFER_TYPE ),
513 ATTRIBUTE( EGL_RENDERABLE_TYPE ),
514 ATTRIBUTE( EGL_MATCH_NATIVE_PIXMAP ),
515 ATTRIBUTE( EGL_CONFORMANT ),
516};
517
518
519static void dumpconfig(_THIS, EGLConfig config)
520{
521 int attr;
522 for (attr = 0 ; attr<sizeof(attributes)/sizeof(Attribute) ; attr++) {
524 _this->egl_data->eglGetConfigAttrib(_this->egl_data->egl_display, config, attributes[attr].attribute, &value);
525 SDL_Log("\t%-32s: %10d (0x%08x)\n", attributes[attr].name, value, value);
526 }
527}
528
529#endif /* DUMP_EGL_CONFIG */
530
531int
532SDL_EGL_ChooseConfig(_THIS)
533{
534/* 64 seems nice. */
535 EGLint attribs[64];
536 EGLint found_configs = 0, value;
537 /* 128 seems even nicer here */
538 EGLConfig configs[128];
539 int i, j, best_bitdiff = -1, bitdiff;
540
541 if (!_this->egl_data) {
542 /* The EGL library wasn't loaded, SDL_GetError() should have info */
543 return -1;
544 }
545
546 /* Get a valid EGL configuration */
547 i = 0;
554
558 }
559
563 }
564
567
571 }
572
576 }
577
579 attribs[i++] = EGL_SAMPLES;
581 }
582
585#ifdef EGL_KHR_create_context
586 if (_this->gl_config.major_version >= 3 &&
587 SDL_EGL_HasExtension(_this, SDL_EGL_DISPLAY_EXTENSION, "EGL_KHR_create_context")) {
589 } else
590#endif
591 if (_this->gl_config.major_version >= 2) {
593 } else {
595 }
596 _this->egl_data->eglBindAPI(EGL_OPENGL_ES_API);
597 } else {
599 _this->egl_data->eglBindAPI(EGL_OPENGL_API);
600 }
601
602 if (_this->egl_data->egl_surfacetype) {
604 attribs[i++] = _this->egl_data->egl_surfacetype;
605 }
606
607 attribs[i++] = EGL_NONE;
608
609 if (_this->egl_data->eglChooseConfig(_this->egl_data->egl_display,
610 attribs,
611 configs, SDL_arraysize(configs),
612 &found_configs) == EGL_FALSE ||
613 found_configs == 0) {
614 return SDL_EGL_SetError("Couldn't find matching EGL config", "eglChooseConfig");
615 }
616
617 /* eglChooseConfig returns a number of configurations that match or exceed the requested attribs. */
618 /* From those, we select the one that matches our requirements more closely via a makeshift algorithm */
619
620 for (i = 0; i < found_configs; i++ ) {
621 if (_this->egl_data->egl_required_visual_id)
622 {
624 _this->egl_data->eglGetConfigAttrib(_this->egl_data->egl_display,
625 configs[i],
627 if (_this->egl_data->egl_required_visual_id != format)
628 continue;
629 }
630
631 bitdiff = 0;
632 for (j = 0; j < SDL_arraysize(attribs) - 1; j += 2) {
633 if (attribs[j] == EGL_NONE) {
634 break;
635 }
636
637 if ( attribs[j+1] != EGL_DONT_CARE && (
638 attribs[j] == EGL_RED_SIZE ||
640 attribs[j] == EGL_BLUE_SIZE ||
644 _this->egl_data->eglGetConfigAttrib(_this->egl_data->egl_display, configs[i], attribs[j], &value);
645 bitdiff += value - attribs[j + 1]; /* value is always >= attrib */
646 }
647 }
648
649 if (bitdiff < best_bitdiff || best_bitdiff == -1) {
650 _this->egl_data->egl_config = configs[i];
651
652 best_bitdiff = bitdiff;
653 }
654
655 if (bitdiff == 0) {
656 break; /* we found an exact match! */
657 }
658 }
659
660#ifdef DUMP_EGL_CONFIG
661 dumpconfig(_this, _this->egl_data->egl_config);
662#endif
663
664 return 0;
665}
666
668SDL_EGL_CreateContext(_THIS, EGLSurface egl_surface)
669{
670 /* max 14 values plus terminator. */
671 EGLint attribs[15];
672 int attr = 0;
673
674 EGLContext egl_context, share_context = EGL_NO_CONTEXT;
675 EGLint profile_mask = _this->gl_config.profile_mask;
676 EGLint major_version = _this->gl_config.major_version;
677 EGLint minor_version = _this->gl_config.minor_version;
678 SDL_bool profile_es = (profile_mask == SDL_GL_CONTEXT_PROFILE_ES);
679
680 if (!_this->egl_data) {
681 /* The EGL library wasn't loaded, SDL_GetError() should have info */
682 return NULL;
683 }
684
686 share_context = (EGLContext)SDL_GL_GetCurrentContext();
687 }
688
689#if SDL_VIDEO_DRIVER_ANDROID
691 /* If SDL_GL_CONTEXT_DEBUG_FLAG is set but EGL_KHR_debug unsupported, unset.
692 * This is required because some Android devices like to complain about it
693 * by "silently" failing, logging a hint which could be easily overlooked:
694 * E/libEGL (26984): validate_display:255 error 3008 (EGL_BAD_DISPLAY)
695 * The following explicitly checks for EGL_KHR_debug before EGL 1.5
696 */
697 int egl_version_major = _this->egl_data->egl_version_major;
698 int egl_version_minor = _this->egl_data->egl_version_minor;
699 if (((egl_version_major < 1) || (egl_version_major == 1 && egl_version_minor < 5)) &&
700 !SDL_EGL_HasExtension(_this, SDL_EGL_DISPLAY_EXTENSION, "EGL_KHR_debug")) {
701 /* SDL profile bits match EGL profile bits. */
702 _this->gl_config.flags &= ~SDL_GL_CONTEXT_DEBUG_FLAG;
703 }
704 }
705#endif
706
707 /* Set the context version and other attributes. */
708 if ((major_version < 3 || (minor_version == 0 && profile_es)) &&
709 _this->gl_config.flags == 0 &&
710 (profile_mask == 0 || profile_es)) {
711 /* Create a context without using EGL_KHR_create_context attribs.
712 * When creating a GLES context without EGL_KHR_create_context we can
713 * only specify the major version. When creating a desktop GL context
714 * we can't specify any version, so we only try in that case when the
715 * version is less than 3.0 (matches SDL's GLX/WGL behavior.)
716 */
717 if (profile_es) {
719 attribs[attr++] = SDL_max(major_version, 1);
720 }
721 } else {
722#ifdef EGL_KHR_create_context
723 /* The Major/minor version, context profiles, and context flags can
724 * only be specified when this extension is available.
725 */
726 if (SDL_EGL_HasExtension(_this, SDL_EGL_DISPLAY_EXTENSION, "EGL_KHR_create_context")) {
728 attribs[attr++] = major_version;
730 attribs[attr++] = minor_version;
731
732 /* SDL profile bits match EGL profile bits. */
733 if (profile_mask != 0 && profile_mask != SDL_GL_CONTEXT_PROFILE_ES) {
735 attribs[attr++] = profile_mask;
736 }
737
738 /* SDL flags match EGL flags. */
739 if (_this->gl_config.flags != 0) {
741 attribs[attr++] = _this->gl_config.flags;
742 }
743 } else
744#endif /* EGL_KHR_create_context */
745 {
746 SDL_SetError("Could not create EGL context (context attributes are not supported)");
747 return NULL;
748 }
749 }
750
751 if (_this->gl_config.no_error) {
752#ifdef EGL_KHR_create_context_no_error
753 if (SDL_EGL_HasExtension(_this, SDL_EGL_DISPLAY_EXTENSION, "EGL_KHR_create_context_no_error")) {
755 attribs[attr++] = _this->gl_config.no_error;
756 } else
757#endif
758 {
759 SDL_SetError("EGL implementation does not support no_error contexts");
760 return NULL;
761 }
762 }
763
764 attribs[attr++] = EGL_NONE;
765
766 /* Bind the API */
767 if (profile_es) {
768 _this->egl_data->eglBindAPI(EGL_OPENGL_ES_API);
769 } else {
770 _this->egl_data->eglBindAPI(EGL_OPENGL_API);
771 }
772
773 egl_context = _this->egl_data->eglCreateContext(_this->egl_data->egl_display,
774 _this->egl_data->egl_config,
775 share_context, attribs);
776
777 if (egl_context == EGL_NO_CONTEXT) {
778 SDL_EGL_SetError("Could not create EGL context", "eglCreateContext");
779 return NULL;
780 }
781
782 _this->egl_data->egl_swapinterval = 0;
783
784 if (SDL_EGL_MakeCurrent(_this, egl_surface, egl_context) < 0) {
785 /* Save the SDL error set by SDL_EGL_MakeCurrent */
786 char errorText[1024];
787 SDL_strlcpy(errorText, SDL_GetError(), SDL_arraysize(errorText));
788
789 /* Delete the context, which may alter the value returned by SDL_GetError() */
790 SDL_EGL_DeleteContext(_this, egl_context);
791
792 /* Restore the SDL error */
793 SDL_SetError("%s", errorText);
794
795 return NULL;
796 }
797
798 return (SDL_GLContext) egl_context;
799}
800
801int
802SDL_EGL_MakeCurrent(_THIS, EGLSurface egl_surface, SDL_GLContext context)
803{
804 EGLContext egl_context = (EGLContext) context;
805
806 if (!_this->egl_data) {
807 return SDL_SetError("OpenGL not initialized");
808 }
809
810 /* The android emulator crashes badly if you try to eglMakeCurrent
811 * with a valid context and invalid surface, so we have to check for both here.
812 */
813 if (!egl_context || !egl_surface) {
814 _this->egl_data->eglMakeCurrent(_this->egl_data->egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
815 } else {
816 if (!_this->egl_data->eglMakeCurrent(_this->egl_data->egl_display,
817 egl_surface, egl_surface, egl_context)) {
818 return SDL_EGL_SetError("Unable to make EGL context current", "eglMakeCurrent");
819 }
820 }
821
822 return 0;
823}
824
825int
826SDL_EGL_SetSwapInterval(_THIS, int interval)
827{
828 EGLBoolean status;
829
830 if (!_this->egl_data) {
831 return SDL_SetError("EGL not initialized");
832 }
833
834 status = _this->egl_data->eglSwapInterval(_this->egl_data->egl_display, interval);
835 if (status == EGL_TRUE) {
836 _this->egl_data->egl_swapinterval = interval;
837 return 0;
838 }
839
840 return SDL_EGL_SetError("Unable to set the EGL swap interval", "eglSwapInterval");
841}
842
843int
844SDL_EGL_GetSwapInterval(_THIS)
845{
846 if (!_this->egl_data) {
847 SDL_SetError("EGL not initialized");
848 return 0;
849 }
850
851 return _this->egl_data->egl_swapinterval;
852}
853
854int
855SDL_EGL_SwapBuffers(_THIS, EGLSurface egl_surface)
856{
857 if (!_this->egl_data->eglSwapBuffers(_this->egl_data->egl_display, egl_surface)) {
858 return SDL_EGL_SetError("unable to show color buffer in an OS-native window", "eglSwapBuffers");
859 }
860 return 0;
861}
862
863void
864SDL_EGL_DeleteContext(_THIS, SDL_GLContext context)
865{
866 EGLContext egl_context = (EGLContext) context;
867
868 /* Clean up GLES and EGL */
869 if (!_this->egl_data) {
870 return;
871 }
872
873 if (egl_context != NULL && egl_context != EGL_NO_CONTEXT) {
874 _this->egl_data->eglDestroyContext(_this->egl_data->egl_display, egl_context);
875 }
876
877}
878
880SDL_EGL_CreateSurface(_THIS, NativeWindowType nw)
881{
882 /* max 2 values plus terminator. */
883 EGLint attribs[3];
884 int attr = 0;
885
887
888 if (SDL_EGL_ChooseConfig(_this) != 0) {
889 return EGL_NO_SURFACE;
890 }
891
892#if SDL_VIDEO_DRIVER_ANDROID
893 {
894 /* Android docs recommend doing this!
895 * Ref: http://developer.android.com/reference/android/app/NativeActivity.html
896 */
898 _this->egl_data->eglGetConfigAttrib(_this->egl_data->egl_display,
899 _this->egl_data->egl_config,
901
902 ANativeWindow_setBuffersGeometry(nw, 0, 0, format);
903
904 /* Update SurfaceView holder format.
905 * May triggers a sequence surfaceDestroyed(), surfaceCreated(), surfaceChanged(). */
907 }
908#endif
910#ifdef EGL_KHR_gl_colorspace
911 if (SDL_EGL_HasExtension(_this, SDL_EGL_DISPLAY_EXTENSION, "EGL_KHR_gl_colorspace")) {
914 } else
915#endif
916 {
917 SDL_SetError("EGL implementation does not support sRGB system framebuffers");
918 return EGL_NO_SURFACE;
919 }
920 }
921
922 attribs[attr++] = EGL_NONE;
923
924 surface = _this->egl_data->eglCreateWindowSurface(
925 _this->egl_data->egl_display,
926 _this->egl_data->egl_config,
927 nw, &attribs[0]);
928 if (surface == EGL_NO_SURFACE) {
929 SDL_EGL_SetError("unable to create an EGL window surface", "eglCreateWindowSurface");
930 }
931 return surface;
932}
933
934void
935SDL_EGL_DestroySurface(_THIS, EGLSurface egl_surface)
936{
937 if (!_this->egl_data) {
938 return;
939 }
940
941 if (egl_surface != EGL_NO_SURFACE) {
942 _this->egl_data->eglDestroySurface(_this->egl_data->egl_display, egl_surface);
943 }
944}
945
946#endif /* SDL_VIDEO_OPENGL_EGL */
947
948/* vi: set ts=4 sw=4 expandtab: */
949
#define _THIS
void Android_JNI_SetSurfaceViewFormat(int format)
#define SDL_SetError
#define SDL_strchr
#define SDL_GetError
#define SDL_LoadObject
#define SDL_UnloadObject
#define SDL_getenv
#define SDL_strlen
#define SDL_strcasecmp
#define SDL_strlcpy
#define SDL_ClearError
#define SDL_free
#define SDL_strstr
#define SDL_sscanf
#define SDL_atoi
#define SDL_LogWarn
#define SDL_Log
#define SDL_snprintf
#define SDL_calloc
#define SDL_GetHint
#define SDL_GL_GetCurrentContext
#define SDL_OutOfMemory()
Definition: SDL_error.h:52
#define SDL_HINT_VIDEO_WIN_D3DCOMPILER
A variable specifying which shader compiler to preload when using the Chrome ANGLE binaries.
Definition: SDL_hints.h:676
void * SDL_LoadFunction(void *handle, const char *name)
@ SDL_LOG_CATEGORY_VIDEO
Definition: SDL_log.h:71
GLuint GLuint GLsizei GLenum type
Definition: SDL_opengl.h:1571
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: SDL_opengl.h:1572
GLuint GLsizei const GLchar * message
GLuint const GLchar * name
GLuint GLint GLboolean GLint GLenum access
GLsizei const GLchar *const * path
GLsizei const GLfloat * value
const GLint * attribs
SDL_bool
Definition: SDL_stdinc.h:162
@ SDL_TRUE
Definition: SDL_stdinc.h:164
@ SDL_FALSE
Definition: SDL_stdinc.h:163
#define SDL_arraysize(array)
Definition: SDL_stdinc.h:115
#define SDL_max(x, y)
Definition: SDL_stdinc.h:407
static SDL_VideoDevice * _this
Definition: SDL_video.c:118
@ SDL_GL_CONTEXT_DEBUG_FLAG
Definition: SDL_video.h:238
void * SDL_GLContext
An opaque handle to an OpenGL context.
Definition: SDL_video.h:193
@ SDL_GL_CONTEXT_PROFILE_ES
Definition: SDL_video.h:233
BOOL WIN_IsWindowsVistaOrGreater(void)
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int int in j)
Definition: SDL_x11sym.h:50
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
#define NULL
Definition: begin_code.h:167
#define EGL_BAD_NATIVE_PIXMAP
Definition: egl.h:71
#define EGL_MAX_PBUFFER_WIDTH
Definition: egl.h:91
#define EGL_BIND_TO_TEXTURE_RGBA
Definition: egl.h:151
#define EGL_OPENGL_ES_BIT
Definition: egl.h:189
#define EGL_DONT_CARE
Definition: egl.h:81
#define EGL_SAMPLES
Definition: egl.h:105
EGLAPI EGLBoolean EGLAPIENTRY eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
EGLAPI EGLBoolean EGLAPIENTRY eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
#define EGL_NO_CONTEXT
Definition: egl.h:98
#define EGL_NATIVE_VISUAL_ID
Definition: egl.h:93
#define EGL_CONFIG_CAVEAT
Definition: egl.h:77
#define EGL_RENDERABLE_TYPE
Definition: egl.h:195
#define EGL_BAD_CONFIG
Definition: egl.h:66
#define EGL_FALSE
Definition: egl.h:84
#define EGL_BAD_SURFACE
Definition: egl.h:74
#define EGL_ALPHA_SIZE
Definition: egl.h:62
#define EGL_LUMINANCE_SIZE
Definition: egl.h:188
#define EGL_NO_DISPLAY
Definition: egl.h:99
#define EGL_NATIVE_RENDERABLE
Definition: egl.h:92
#define EGL_TRANSPARENT_RED_VALUE
Definition: egl.h:113
#define EGL_TRANSPARENT_BLUE_VALUE
Definition: egl.h:111
unsigned int EGLenum
Definition: egl.h:171
EGLAPI const char *EGLAPIENTRY eglQueryString(EGLDisplay dpy, EGLint name)
#define EGL_BAD_PARAMETER
Definition: egl.h:73
#define EGL_OPENGL_API
Definition: egl.h:232
#define EGL_LEVEL
Definition: egl.h:88
#define EGL_TRANSPARENT_GREEN_VALUE
Definition: egl.h:112
EGLAPI EGLSurface EGLAPIENTRY eglCreatePbufferSurface(EGLDisplay dpy, EGLConfig config, const EGLint *attrib_list)
#define EGL_MAX_SWAP_INTERVAL
Definition: egl.h:154
#define EGL_SAMPLE_BUFFERS
Definition: egl.h:106
void * EGLContext
Definition: egl.h:60
EGLAPI EGLSurface EGLAPIENTRY eglCreateWindowSurface(EGLDisplay dpy, EGLConfig config, EGLNativeWindowType win, const EGLint *attrib_list)
#define EGL_NOT_INITIALIZED
Definition: egl.h:97
EGLAPI EGLBoolean EGLAPIENTRY eglTerminate(EGLDisplay dpy)
#define EGL_MAX_PBUFFER_HEIGHT
Definition: egl.h:89
#define EGL_BAD_CURRENT_SURFACE
Definition: egl.h:68
#define EGL_BAD_CONTEXT
Definition: egl.h:67
#define EGL_OPENGL_BIT
Definition: egl.h:233
#define EGL_CONFIG_ID
Definition: egl.h:78
EGLAPI EGLBoolean EGLAPIENTRY eglChooseConfig(EGLDisplay dpy, const EGLint *attrib_list, EGLConfig *configs, EGLint config_size, EGLint *num_config)
void * EGLConfig
Definition: egl.h:58
#define EGL_OPENGL_ES2_BIT
Definition: egl.h:214
#define EGL_NONE
Definition: egl.h:95
#define EGL_CONTEXT_CLIENT_VERSION
Definition: egl.h:212
#define EGL_NATIVE_VISUAL_TYPE
Definition: egl.h:94
EGLAPI EGLBoolean EGLAPIENTRY eglBindAPI(EGLenum api)
unsigned int EGLBoolean
Definition: egl.h:54
#define EGL_BAD_NATIVE_WINDOW
Definition: egl.h:72
#define EGL_NO_SURFACE
Definition: egl.h:100
#define EGL_GREEN_SIZE
Definition: egl.h:85
#define EGL_ALPHA_MASK_SIZE
Definition: egl.h:176
#define EGL_MATCH_NATIVE_PIXMAP
Definition: egl.h:213
#define EGL_OPENGL_ES_API
Definition: egl.h:191
#define EGL_EXTENSIONS
Definition: egl.h:83
#define EGL_BAD_MATCH
Definition: egl.h:70
#define EGL_COLOR_BUFFER_TYPE
Definition: egl.h:183
#define EGL_BIND_TO_TEXTURE_RGB
Definition: egl.h:150
#define EGL_CONTEXT_LOST
Definition: egl.h:152
#define EGL_BAD_ALLOC
Definition: egl.h:64
EGLAPI EGLBoolean EGLAPIENTRY eglMakeCurrent(EGLDisplay dpy, EGLSurface draw, EGLSurface read, EGLContext ctx)
EGLAPI EGLBoolean EGLAPIENTRY eglWaitNative(EGLint engine)
EGLAPI EGLDisplay EGLAPIENTRY eglGetDisplay(EGLNativeDisplayType display_id)
EGLAPI EGLBoolean EGLAPIENTRY eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
EGLAPI EGLBoolean EGLAPIENTRY eglSwapInterval(EGLDisplay dpy, EGLint interval)
#define EGL_STENCIL_SIZE
Definition: egl.h:108
#define EGL_BUFFER_SIZE
Definition: egl.h:76
EGLAPI __eglMustCastToProperFunctionPointerType EGLAPIENTRY eglGetProcAddress(const char *procname)
EGLAPI EGLBoolean EGLAPIENTRY eglWaitGL(void)
#define EGL_SURFACE_TYPE
Definition: egl.h:110
#define EGL_VERSION
Definition: egl.h:118
#define EGL_TRUE
Definition: egl.h:116
#define EGL_MAX_PBUFFER_PIXELS
Definition: egl.h:90
EGLAPI EGLBoolean EGLAPIENTRY eglSwapBuffers(EGLDisplay dpy, EGLSurface surface)
#define EGL_DEPTH_SIZE
Definition: egl.h:80
#define EGL_BAD_ATTRIBUTE
Definition: egl.h:65
#define EGL_RED_SIZE
Definition: egl.h:104
#define EGL_SUCCESS
Definition: egl.h:109
#define EGL_CONFORMANT
Definition: egl.h:211
EGLAPI EGLBoolean EGLAPIENTRY eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config, EGLint attribute, EGLint *value)
#define EGL_TRANSPARENT_TYPE
Definition: egl.h:115
#define EGL_BAD_ACCESS
Definition: egl.h:63
#define EGL_BLUE_SIZE
Definition: egl.h:75
EGLAPI EGLDisplay EGLAPIENTRY eglGetPlatformDisplay(EGLenum platform, void *native_display, const EGLAttrib *attrib_list)
EGLAPI EGLint EGLAPIENTRY eglGetError(void)
EGLAPI EGLContext EGLAPIENTRY eglCreateContext(EGLDisplay dpy, EGLConfig config, EGLContext share_context, const EGLint *attrib_list)
#define EGL_MIN_SWAP_INTERVAL
Definition: egl.h:153
#define EGL_BAD_DISPLAY
Definition: egl.h:69
void * EGLSurface
Definition: egl.h:59
EGLSurface surface
Definition: eglext.h:248
EGLConfig config
Definition: eglext.h:433
EGLSurface EGLint attribute
Definition: eglext.h:263
#define EGL_GL_COLORSPACE_SRGB_KHR
Definition: eglext.h:172
#define EGL_CONTEXT_MINOR_VERSION_KHR
Definition: eglext.h:90
#define EGL_CONTEXT_MAJOR_VERSION_KHR
Definition: eglext.h:89
void * native_display
Definition: eglext.h:789
#define EGL_OPENGL_ES3_BIT_KHR
Definition: eglext.h:101
#define EGL_CONTEXT_OPENGL_NO_ERROR_KHR
Definition: eglext.h:106
#define EGL_CONTEXT_FLAGS_KHR
Definition: eglext.h:91
#define EGL_GL_COLORSPACE_KHR
Definition: eglext.h:171
#define EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR
Definition: eglext.h:92
khronos_int32_t EGLint
Definition: eglplatform.h:122
EGLNativeDisplayType NativeDisplayType
Definition: eglplatform.h:110
EGLNativeWindowType NativeWindowType
Definition: eglplatform.h:112
int framebuffer_srgb_capable
Definition: SDL_sysvideo.h:350
char driver_path[256]
Definition: SDL_sysvideo.h:354
struct SDL_VideoDevice::@262 gl_config
int share_with_current_context
Definition: SDL_sysvideo.h:347
SDL_bool retval
static screen_context_t context
Definition: video.c:25