2 * Simple DirectMedia Layer
3 * Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
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.
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:
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.
21 #include "../SDL_internal.h"
23 #if SDL_VIDEO_OPENGL_EGL
25 #if SDL_VIDEO_DRIVER_WINDOWS || SDL_VIDEO_DRIVER_WINRT
26 #include "../core/windows/SDL_windows.h"
28 #if SDL_VIDEO_DRIVER_ANDROID
29 #include <android/native_window.h>
32 #include "SDL_sysvideo.h"
34 #include "SDL_egl_c.h"
35 #include "SDL_loadso.h"
36 #include "SDL_hints.h"
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
43 #endif /* EGL_KHR_create_context */
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" )
54 #elif SDL_VIDEO_DRIVER_ANDROID || SDL_VIDEO_DRIVER_VIVANTE
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"
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"
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" //???
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 */
84 #ifdef SDL_VIDEO_STATIC_ANGLE
85 #define LOAD_FUNC(NAME) \
86 _this->egl_data->NAME = (void *)NAME;
88 #define LOAD_FUNC(NAME) \
89 _this->egl_data->NAME = SDL_LoadFunction(_this->egl_data->dll_handle, #NAME); \
90 if (!_this->egl_data->NAME) \
92 return SDL_SetError("Could not retrieve EGL function " #NAME); \
96 static const char * SDL_EGL_GetErrorName(EGLint eglErrorCode)
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);
119 int SDL_EGL_SetErrorEx(const char * message, const char * eglFunctionName, EGLint eglErrorCode)
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;
128 return SDL_SetError("%s (call to %s failed, reporting an error of %s)", message, eglFunctionName, errorText);
131 /* EGL implementation of SDL OpenGL ES support */
133 SDL_EGL_DISPLAY_EXTENSION,
134 SDL_EGL_CLIENT_EXTENSION
135 } SDL_EGL_ExtensionType;
137 static SDL_bool SDL_EGL_HasExtension(_THIS, SDL_EGL_ExtensionType type, const char *ext)
140 const char *ext_override;
141 const char *egl_extstr;
142 const char *ext_start;
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"); */
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.
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.
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) {
162 } else if (disable_ext & 0x02 && type == SDL_EGL_CLIENT_EXTENSION) {
167 ext_len = SDL_strlen(ext);
169 case SDL_EGL_DISPLAY_EXTENSION:
170 egl_extstr = _this->egl_data->eglQueryString(_this->egl_data->egl_display, EGL_EXTENSIONS);
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.
177 egl_extstr = _this->egl_data->eglQueryString(EGL_NO_DISPLAY, EGL_EXTENSIONS);
180 /* SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "SDL_EGL_HasExtension: Invalid extension type"); */
184 if (egl_extstr != NULL) {
185 ext_start = egl_extstr;
188 ext_start = SDL_strstr(ext_start, ext);
189 if (ext_start == NULL) {
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) {
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) {
210 SDL_EGL_GetProcAddress(_THIS, const char *proc)
212 static char procname[1024];
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);
226 char *Tizen_Not_Support_API[4] = {
227 "eglSetDamageRegionKHR",
228 "eglSwapBuffersWithDamageEXT",
229 "eglSwapBuffersWithDamageINTEL",
230 "eglSwapBuffersWithDamage"
236 if(!SDL_strcmp(proc, Tizen_Not_Support_API[i]))
241 retval = SDL_LoadFunction(_this->egl_data->egl_dll_handle, proc);
242 if (!retval && SDL_strlen(proc) <= 1022) {
244 SDL_strlcpy(procname + 1, proc, 1022);
245 retval = SDL_LoadFunction(_this->egl_data->egl_dll_handle, procname);
251 SDL_EGL_UnloadLibrary(_THIS)
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;
259 if (_this->egl_data->dll_handle) {
260 SDL_UnloadObject(_this->egl_data->dll_handle);
261 _this->egl_data->dll_handle = NULL;
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;
268 SDL_free(_this->egl_data);
269 _this->egl_data = NULL;
274 SDL_EGL_LoadLibrary(_THIS, const char *egl_path, NativeDisplayType native_display, EGLenum platform)
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;
282 #if SDL_VIDEO_DRIVER_RPI
283 SDL_bool vc4 = (0 == access("/sys/module/vc4/", F_OK));
286 if (_this->egl_data) {
287 return SDL_SetError("EGL context already created");
290 _this->egl_data = (struct SDL_EGL_VideoData *) SDL_calloc(1, sizeof(SDL_EGL_VideoData));
291 if (!_this->egl_data) {
292 return SDL_OutOfMemory();
295 #if SDL_VIDEO_DRIVER_WINDOWS || SDL_VIDEO_DRIVER_WINRT
296 d3dcompiler = SDL_GetHint(SDL_HINT_VIDEO_WIN_D3DCOMPILER);
298 if (WIN_IsWindowsVistaOrGreater()) {
299 d3dcompiler = "d3dcompiler_46.dll";
301 d3dcompiler = "d3dcompiler_43.dll";
304 if (SDL_strcasecmp(d3dcompiler, "none") != 0) {
305 if (SDL_LoadObject(d3dcompiler) == NULL) {
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");
315 egl_dll_handle = SDL_LoadObject(path);
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);
324 if (egl_dll_handle == NULL && !vc4) {
326 egl_dll_handle = SDL_LoadObject(path);
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);
338 if (egl_dll_handle == NULL && !vc4) {
340 egl_dll_handle = SDL_LoadObject(path);
348 egl_dll_handle = SDL_LoadObject(path);
352 _this->egl_data->egl_dll_handle = egl_dll_handle;
354 if (egl_dll_handle == NULL) {
355 return SDL_SetError("Could not initialize OpenGL / GLES library");
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);
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);
367 path = SDL_getenv("SDL_VIDEO_EGL_DRIVER");
371 dll_handle = SDL_LoadObject(path);
374 if (dll_handle == NULL && !vc4) {
376 dll_handle = SDL_LoadObject(path);
380 if (dll_handle == NULL || SDL_LoadFunction(dll_handle, "eglChooseConfig") == NULL) {
381 if (dll_handle != NULL) {
382 SDL_UnloadObject(dll_handle);
384 return SDL_SetError("Could not load EGL library");
390 _this->egl_data->dll_handle = dll_handle;
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);
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);
425 if (egl_version_major == 1 && egl_version_minor == 5) {
426 LOAD_FUNC(eglGetPlatformDisplay);
429 _this->egl_data->egl_display = EGL_NO_DISPLAY;
430 #if !defined(__WINRT__)
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);
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);
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);
447 if (_this->egl_data->egl_display == EGL_NO_DISPLAY) {
448 return SDL_SetError("Could not get EGL display");
451 if (_this->egl_data->eglInitialize(_this->egl_data->egl_display, NULL, NULL) != EGL_TRUE) {
452 return SDL_SetError("Could not initialize EGL");
457 SDL_strlcpy(_this->gl_config.driver_path, path, sizeof(_this->gl_config.driver_path) - 1);
459 *_this->gl_config.driver_path = '\0';
466 SDL_EGL_ChooseConfig(_THIS)
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];
476 /* 128 seems even nicer here */
477 EGLConfig configs[128];
479 int i, j, best_bitdiff = -1, bitdiff;
481 if (!_this->egl_data) {
482 /* The EGL library wasn't loaded, SDL_GetError() should have info */
486 /* Get a valid EGL configuration */
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;
495 if (_this->gl_config.alpha_size) {
496 attribs[i++] = EGL_ALPHA_SIZE;
497 attribs[i++] = _this->gl_config.alpha_size;
500 if (_this->gl_config.buffer_size) {
501 attribs[i++] = EGL_BUFFER_SIZE;
502 attribs[i++] = _this->gl_config.buffer_size;
505 attribs[i++] = EGL_DEPTH_SIZE;
506 attribs[i++] = _this->gl_config.depth_size;
508 if (_this->gl_config.stencil_size) {
509 attribs[i++] = EGL_STENCIL_SIZE;
510 attribs[i++] = _this->gl_config.stencil_size;
513 if (_this->gl_config.multisamplebuffers) {
514 attribs[i++] = EGL_SAMPLE_BUFFERS;
515 attribs[i++] = _this->gl_config.multisamplebuffers;
518 if (_this->gl_config.multisamplesamples) {
519 attribs[i++] = EGL_SAMPLES;
520 attribs[i++] = _this->gl_config.multisamplesamples;
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;
531 if (_this->gl_config.major_version >= 2) {
532 attribs[i++] = EGL_OPENGL_ES2_BIT;
534 attribs[i++] = EGL_OPENGL_ES_BIT;
536 _this->egl_data->eglBindAPI(EGL_OPENGL_ES_API);
538 attribs[i++] = EGL_OPENGL_BIT;
539 _this->egl_data->eglBindAPI(EGL_OPENGL_API);
542 if (_this->egl_data->egl_surfacetype) {
543 attribs[i++] = EGL_SURFACE_TYPE;
544 attribs[i++] = _this->egl_data->egl_surfacetype;
547 attribs[i++] = EGL_NONE;
549 if (_this->egl_data->eglChooseConfig(_this->egl_data->egl_display,
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");
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 */
560 for (i = 0; i < found_configs; i++ ) {
562 for (j = 0; j < SDL_arraysize(attribs) - 1; j += 2) {
563 if (attribs[j] == EGL_NONE) {
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 */
579 if (bitdiff < best_bitdiff || best_bitdiff == -1) {
580 _this->egl_data->egl_config = configs[i];
582 best_bitdiff = bitdiff;
586 break; /* we found an exact match! */
594 SDL_EGL_CreateContext(_THIS, EGLSurface egl_surface)
596 /* max 14 values plus terminator. */
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);
606 if (!_this->egl_data) {
607 /* The EGL library wasn't loaded, SDL_GetError() should have info */
611 if (_this->gl_config.share_with_current_context) {
612 share_context = (EGLContext)SDL_GL_GetCurrentContext();
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.)
626 attribs[attr++] = EGL_CONTEXT_CLIENT_VERSION;
627 attribs[attr++] = SDL_max(major_version, 1);
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.
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;
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;
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;
652 #endif /* EGL_KHR_create_context */
654 SDL_SetError("Could not create EGL context (context attributes are not supported)");
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;
665 else if (_this->gl_config.flags == SDL_GL_CONTEXT_PRIORITY_MEDIUM) {
666 attribs[attr++] = EGL_CONTEXT_PRIORITY_MEDIUM_IMG;
668 else if (_this->gl_config.flags == SDL_GL_CONTEXT_PRIORITY_LOW) {
669 attribs[attr++] = EGL_CONTEXT_PRIORITY_LOW_IMG;
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;
682 SDL_SetError("EGL implementation does not support no_error contexts");
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;
695 SDL_SetError("EGL implementation does not support no_error contexts");
700 attribs[attr++] = EGL_NONE;
704 _this->egl_data->eglBindAPI(EGL_OPENGL_ES_API);
706 _this->egl_data->eglBindAPI(EGL_OPENGL_API);
709 egl_context = _this->egl_data->eglCreateContext(_this->egl_data->egl_display,
710 _this->egl_data->egl_config,
711 share_context, attribs);
713 if (egl_context == EGL_NO_CONTEXT) {
714 SDL_EGL_SetError("Could not create EGL context", "eglCreateContext");
718 _this->egl_data->egl_swapinterval = 0;
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));
725 /* Delete the context, which may alter the value returned by SDL_GetError() */
726 SDL_EGL_DeleteContext(_this, egl_context);
728 /* Restore the SDL error */
729 SDL_SetError("%s", errorText);
734 return (SDL_GLContext) egl_context;
738 SDL_EGL_MakeCurrent(_THIS, EGLSurface egl_surface, SDL_GLContext context)
740 EGLContext egl_context = (EGLContext) context;
742 if (!_this->egl_data) {
743 return SDL_SetError("OpenGL not initialized");
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");
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.
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);
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");
771 SDL_EGL_SetSwapInterval(_THIS, int interval)
775 if (!_this->egl_data) {
776 return SDL_SetError("EGL not initialized");
779 status = _this->egl_data->eglSwapInterval(_this->egl_data->egl_display, interval);
780 if (status == EGL_TRUE) {
781 _this->egl_data->egl_swapinterval = interval;
785 return SDL_EGL_SetError("Unable to set the EGL swap interval", "eglSwapInterval");
789 SDL_EGL_GetSwapInterval(_THIS)
791 if (!_this->egl_data) {
792 SDL_SetError("EGL not initialized");
796 return _this->egl_data->egl_swapinterval;
800 SDL_EGL_SwapBuffers(_THIS, EGLSurface egl_surface)
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");
809 SDL_EGL_DeleteContext(_THIS, SDL_GLContext context)
811 EGLContext egl_context = (EGLContext) context;
813 /* Clean up GLES and EGL */
814 if (!_this->egl_data) {
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);
826 SDL_EGL_CreateSurface(_THIS, NativeWindowType nw)
828 /* max 2 values plus terminator. */
832 EGLSurface * surface;
834 if (SDL_EGL_ChooseConfig(_this) != 0) {
835 return EGL_NO_SURFACE;
838 #if SDL_VIDEO_DRIVER_ANDROID
840 /* Android docs recommend doing this!
841 * Ref: http://developer.android.com/reference/android/app/NativeActivity.html
844 _this->egl_data->eglGetConfigAttrib(_this->egl_data->egl_display,
845 _this->egl_data->egl_config,
846 EGL_NATIVE_VISUAL_ID, &format);
848 ANativeWindow_setBuffersGeometry(nw, 0, 0, format);
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;
859 SDL_SetError("EGL implementation does not support sRGB system framebuffers");
860 return EGL_NO_SURFACE;
864 attribs[attr++] = EGL_NONE;
866 surface = _this->egl_data->eglCreateWindowSurface(
867 _this->egl_data->egl_display,
868 _this->egl_data->egl_config,
870 if (surface == EGL_NO_SURFACE) {
871 SDL_EGL_SetError("unable to create an EGL window surface", "eglCreateWindowSurface");
877 SDL_EGL_DestroySurface(_THIS, EGLSurface egl_surface)
879 if (!_this->egl_data) {
883 if (egl_surface != EGL_NO_SURFACE) {
884 _this->egl_data->eglDestroySurface(_this->egl_data->egl_display, egl_surface);
888 #endif /* SDL_VIDEO_OPENGL_EGL */
890 /* vi: set ts=4 sw=4 expandtab: */