egl: Cast to size_t, in case platform's NativeDisplayType isn't a pointer.
(fixes compiler warnings on QNX.)
2 * Simple DirectMedia Layer
3 * Copyright (C) 1997-2017 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 "/opt/vc/lib/libEGL.so"
48 #define DEFAULT_OGL_ES2 "/opt/vc/lib/libGLESv2.so"
49 #define DEFAULT_OGL_ES_PVR "/opt/vc/lib/libGLES_CM.so"
50 #define DEFAULT_OGL_ES "/opt/vc/lib/libGLESv1_CM.so"
52 #elif SDL_VIDEO_DRIVER_ANDROID || SDL_VIDEO_DRIVER_VIVANTE
54 #define DEFAULT_EGL "libEGL.so"
55 #define DEFAULT_OGL_ES2 "libGLESv2.so"
56 #define DEFAULT_OGL_ES_PVR "libGLES_CM.so"
57 #define DEFAULT_OGL_ES "libGLESv1_CM.so"
59 #elif SDL_VIDEO_DRIVER_WINDOWS || SDL_VIDEO_DRIVER_WINRT
60 /* EGL AND OpenGL ES support via ANGLE */
61 #define DEFAULT_EGL "libEGL.dll"
62 #define DEFAULT_OGL_ES2 "libGLESv2.dll"
63 #define DEFAULT_OGL_ES_PVR "libGLES_CM.dll"
64 #define DEFAULT_OGL_ES "libGLESv1_CM.dll"
68 #define DEFAULT_OGL "libGL.so.1"
69 #define DEFAULT_EGL "libEGL.so.1"
70 #define DEFAULT_OGL_ES2 "libGLESv2.so.2"
71 #define DEFAULT_OGL_ES_PVR "libGLES_CM.so.1"
72 #define DEFAULT_OGL_ES "libGLESv1_CM.so.1"
73 #endif /* SDL_VIDEO_DRIVER_RPI */
75 #ifdef SDL_VIDEO_STATIC_ANGLE
76 #define LOAD_FUNC(NAME) \
77 _this->egl_data->NAME = (void *)NAME;
79 #define LOAD_FUNC(NAME) \
80 _this->egl_data->NAME = SDL_LoadFunction(_this->egl_data->dll_handle, #NAME); \
81 if (!_this->egl_data->NAME) \
83 return SDL_SetError("Could not retrieve EGL function " #NAME); \
87 static const char * SDL_EGL_GetErrorName(EGLint eglErrorCode)
89 #define SDL_EGL_ERROR_TRANSLATE(e) case e: return #e;
90 switch (eglErrorCode) {
91 SDL_EGL_ERROR_TRANSLATE(EGL_SUCCESS);
92 SDL_EGL_ERROR_TRANSLATE(EGL_NOT_INITIALIZED);
93 SDL_EGL_ERROR_TRANSLATE(EGL_BAD_ACCESS);
94 SDL_EGL_ERROR_TRANSLATE(EGL_BAD_ALLOC);
95 SDL_EGL_ERROR_TRANSLATE(EGL_BAD_ATTRIBUTE);
96 SDL_EGL_ERROR_TRANSLATE(EGL_BAD_CONTEXT);
97 SDL_EGL_ERROR_TRANSLATE(EGL_BAD_CONFIG);
98 SDL_EGL_ERROR_TRANSLATE(EGL_BAD_CURRENT_SURFACE);
99 SDL_EGL_ERROR_TRANSLATE(EGL_BAD_DISPLAY);
100 SDL_EGL_ERROR_TRANSLATE(EGL_BAD_SURFACE);
101 SDL_EGL_ERROR_TRANSLATE(EGL_BAD_MATCH);
102 SDL_EGL_ERROR_TRANSLATE(EGL_BAD_PARAMETER);
103 SDL_EGL_ERROR_TRANSLATE(EGL_BAD_NATIVE_PIXMAP);
104 SDL_EGL_ERROR_TRANSLATE(EGL_BAD_NATIVE_WINDOW);
105 SDL_EGL_ERROR_TRANSLATE(EGL_CONTEXT_LOST);
110 int SDL_EGL_SetErrorEx(const char * message, const char * eglFunctionName, EGLint eglErrorCode)
112 const char * errorText = SDL_EGL_GetErrorName(eglErrorCode);
113 char altErrorText[32];
114 if (errorText[0] == '\0') {
115 /* An unknown-to-SDL error code was reported. Report its hexadecimal value, instead of its name. */
116 SDL_snprintf(altErrorText, SDL_arraysize(altErrorText), "0x%x", (unsigned int)eglErrorCode);
117 errorText = altErrorText;
119 return SDL_SetError("%s (call to %s failed, reporting an error of %s)", message, eglFunctionName, errorText);
122 /* EGL implementation of SDL OpenGL ES support */
124 SDL_EGL_DISPLAY_EXTENSION,
125 SDL_EGL_CLIENT_EXTENSION
126 } SDL_EGL_ExtensionType;
128 static SDL_bool SDL_EGL_HasExtension(_THIS, SDL_EGL_ExtensionType type, const char *ext)
131 const char *ext_override;
132 const char *egl_extstr;
133 const char *ext_start;
135 /* Invalid extensions can be rejected early */
136 if (ext == NULL || *ext == 0 || SDL_strchr(ext, ' ') != NULL) {
137 /* SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "SDL_EGL_HasExtension: Invalid EGL extension"); */
141 /* Extensions can be masked with an environment variable.
142 * Unlike the OpenGL override, this will use the set bits of an integer
143 * to disable the extension.
145 * 0 If set, the display extension is masked and not present to SDL.
146 * 1 If set, the client extension is masked and not present to SDL.
148 ext_override = SDL_getenv(ext);
149 if (ext_override != NULL) {
150 int disable_ext = SDL_atoi(ext_override);
151 if (disable_ext & 0x01 && type == SDL_EGL_DISPLAY_EXTENSION) {
153 } else if (disable_ext & 0x02 && type == SDL_EGL_CLIENT_EXTENSION) {
158 ext_len = SDL_strlen(ext);
160 case SDL_EGL_DISPLAY_EXTENSION:
161 egl_extstr = _this->egl_data->eglQueryString(_this->egl_data->egl_display, EGL_EXTENSIONS);
163 case SDL_EGL_CLIENT_EXTENSION:
164 /* EGL_EXT_client_extensions modifies eglQueryString to return client extensions
165 * if EGL_NO_DISPLAY is passed. Implementations without it are required to return NULL.
166 * This behavior is included in EGL 1.5.
168 egl_extstr = _this->egl_data->eglQueryString(EGL_NO_DISPLAY, EGL_EXTENSIONS);
171 /* SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "SDL_EGL_HasExtension: Invalid extension type"); */
175 if (egl_extstr != NULL) {
176 ext_start = egl_extstr;
179 ext_start = SDL_strstr(ext_start, ext);
180 if (ext_start == NULL) {
183 /* Check if the match is not just a substring of one of the extensions */
184 if (ext_start == egl_extstr || *(ext_start - 1) == ' ') {
185 if (ext_start[ext_len] == ' ' || ext_start[ext_len] == 0) {
189 /* If the search stopped in the middle of an extension, skip to the end of it */
190 ext_start += ext_len;
191 while (*ext_start != ' ' && *ext_start != 0) {
201 SDL_EGL_GetProcAddress(_THIS, const char *proc)
203 static char procname[1024];
206 /* eglGetProcAddress is busted on Android http://code.google.com/p/android/issues/detail?id=7681 */
207 #if !defined(SDL_VIDEO_DRIVER_ANDROID) && !defined(SDL_VIDEO_DRIVER_MIR)
208 if (_this->egl_data->eglGetProcAddress) {
209 retval = _this->egl_data->eglGetProcAddress(proc);
216 retval = SDL_LoadFunction(_this->egl_data->egl_dll_handle, proc);
217 if (!retval && SDL_strlen(proc) <= 1022) {
219 SDL_strlcpy(procname + 1, proc, 1022);
220 retval = SDL_LoadFunction(_this->egl_data->egl_dll_handle, procname);
226 SDL_EGL_UnloadLibrary(_THIS)
228 if (_this->egl_data) {
229 if (_this->egl_data->egl_display) {
230 _this->egl_data->eglTerminate(_this->egl_data->egl_display);
231 _this->egl_data->egl_display = NULL;
234 if (_this->egl_data->dll_handle) {
235 SDL_UnloadObject(_this->egl_data->dll_handle);
236 _this->egl_data->dll_handle = NULL;
238 if (_this->egl_data->egl_dll_handle) {
239 SDL_UnloadObject(_this->egl_data->egl_dll_handle);
240 _this->egl_data->egl_dll_handle = NULL;
243 SDL_free(_this->egl_data);
244 _this->egl_data = NULL;
249 SDL_EGL_LoadLibrary(_THIS, const char *egl_path, NativeDisplayType native_display, EGLenum platform)
251 void *dll_handle = NULL, *egl_dll_handle = NULL; /* The naming is counter intuitive, but hey, I just work here -- Gabriel */
252 const char *path = NULL;
253 int egl_version_major = 0, egl_version_minor = 0;
254 #if SDL_VIDEO_DRIVER_WINDOWS || SDL_VIDEO_DRIVER_WINRT
255 const char *d3dcompiler;
258 if (_this->egl_data) {
259 return SDL_SetError("OpenGL ES context already created");
262 _this->egl_data = (struct SDL_EGL_VideoData *) SDL_calloc(1, sizeof(SDL_EGL_VideoData));
263 if (!_this->egl_data) {
264 return SDL_OutOfMemory();
267 #if SDL_VIDEO_DRIVER_WINDOWS || SDL_VIDEO_DRIVER_WINRT
268 d3dcompiler = SDL_GetHint(SDL_HINT_VIDEO_WIN_D3DCOMPILER);
270 if (WIN_IsWindowsVistaOrGreater()) {
271 d3dcompiler = "d3dcompiler_46.dll";
273 d3dcompiler = "d3dcompiler_43.dll";
276 if (SDL_strcasecmp(d3dcompiler, "none") != 0) {
277 if (SDL_LoadObject(d3dcompiler) == NULL) {
283 #ifndef SDL_VIDEO_STATIC_ANGLE
284 /* A funny thing, loading EGL.so first does not work on the Raspberry, so we load libGL* first */
285 path = SDL_getenv("SDL_VIDEO_GL_DRIVER");
287 egl_dll_handle = SDL_LoadObject(path);
290 if (egl_dll_handle == NULL) {
291 if (_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES) {
292 if (_this->gl_config.major_version > 1) {
293 path = DEFAULT_OGL_ES2;
294 egl_dll_handle = SDL_LoadObject(path);
296 path = DEFAULT_OGL_ES;
297 egl_dll_handle = SDL_LoadObject(path);
298 if (egl_dll_handle == NULL) {
299 path = DEFAULT_OGL_ES_PVR;
300 egl_dll_handle = SDL_LoadObject(path);
307 egl_dll_handle = SDL_LoadObject(path);
311 _this->egl_data->egl_dll_handle = egl_dll_handle;
313 if (egl_dll_handle == NULL) {
314 return SDL_SetError("Could not initialize OpenGL / GLES library");
317 /* Loading libGL* in the previous step took care of loading libEGL.so, but we future proof by double checking */
318 if (egl_path != NULL) {
319 dll_handle = SDL_LoadObject(egl_path);
321 /* Try loading a EGL symbol, if it does not work try the default library paths */
322 if (dll_handle == NULL || SDL_LoadFunction(dll_handle, "eglChooseConfig") == NULL) {
323 if (dll_handle != NULL) {
324 SDL_UnloadObject(dll_handle);
326 path = SDL_getenv("SDL_VIDEO_EGL_DRIVER");
330 dll_handle = SDL_LoadObject(path);
331 if (dll_handle == NULL || SDL_LoadFunction(dll_handle, "eglChooseConfig") == NULL) {
332 if (dll_handle != NULL) {
333 SDL_UnloadObject(dll_handle);
335 return SDL_SetError("Could not load EGL library");
341 _this->egl_data->dll_handle = dll_handle;
343 /* Load new function pointers */
344 LOAD_FUNC(eglGetDisplay);
345 LOAD_FUNC(eglInitialize);
346 LOAD_FUNC(eglTerminate);
347 LOAD_FUNC(eglGetProcAddress);
348 LOAD_FUNC(eglChooseConfig);
349 LOAD_FUNC(eglGetConfigAttrib);
350 LOAD_FUNC(eglCreateContext);
351 LOAD_FUNC(eglDestroyContext);
352 LOAD_FUNC(eglCreateWindowSurface);
353 LOAD_FUNC(eglDestroySurface);
354 LOAD_FUNC(eglMakeCurrent);
355 LOAD_FUNC(eglSwapBuffers);
356 LOAD_FUNC(eglSwapInterval);
357 LOAD_FUNC(eglWaitNative);
358 LOAD_FUNC(eglWaitGL);
359 LOAD_FUNC(eglBindAPI);
360 LOAD_FUNC(eglQueryString);
361 LOAD_FUNC(eglGetError);
363 if (_this->egl_data->eglQueryString) {
364 /* EGL 1.5 allows querying for client version */
365 const char *egl_version = _this->egl_data->eglQueryString(EGL_NO_DISPLAY, EGL_VERSION);
366 if (egl_version != NULL) {
367 if (SDL_sscanf(egl_version, "%d.%d", &egl_version_major, &egl_version_minor) != 2) {
368 egl_version_major = 0;
369 egl_version_minor = 0;
370 SDL_LogWarn(SDL_LOG_CATEGORY_VIDEO, "Could not parse EGL version string: %s", egl_version);
375 if (egl_version_major == 1 && egl_version_minor == 5) {
376 LOAD_FUNC(eglGetPlatformDisplay);
379 _this->egl_data->egl_display = EGL_NO_DISPLAY;
380 #if !defined(__WINRT__)
382 if (egl_version_major == 1 && egl_version_minor == 5) {
383 _this->egl_data->egl_display = _this->egl_data->eglGetPlatformDisplay(platform, (void *)(size_t)native_display, NULL);
385 if (SDL_EGL_HasExtension(_this, SDL_EGL_CLIENT_EXTENSION, "EGL_EXT_platform_base")) {
386 _this->egl_data->eglGetPlatformDisplayEXT = SDL_EGL_GetProcAddress(_this, "eglGetPlatformDisplayEXT");
387 if (_this->egl_data->eglGetPlatformDisplayEXT) {
388 _this->egl_data->egl_display = _this->egl_data->eglGetPlatformDisplayEXT(platform, (void *)(size_t)native_display, NULL);
393 /* Try the implementation-specific eglGetDisplay even if eglGetPlatformDisplay fails */
394 if (_this->egl_data->egl_display == EGL_NO_DISPLAY) {
395 _this->egl_data->egl_display = _this->egl_data->eglGetDisplay(native_display);
397 if (_this->egl_data->egl_display == EGL_NO_DISPLAY) {
398 return SDL_SetError("Could not get EGL display");
401 if (_this->egl_data->eglInitialize(_this->egl_data->egl_display, NULL, NULL) != EGL_TRUE) {
402 return SDL_SetError("Could not initialize EGL");
407 SDL_strlcpy(_this->gl_config.driver_path, path, sizeof(_this->gl_config.driver_path) - 1);
409 *_this->gl_config.driver_path = '\0';
416 SDL_EGL_ChooseConfig(_THIS)
420 EGLint found_configs = 0, value;
421 #ifdef SDL_VIDEO_DRIVER_KMSDRM
422 /* Intel EGL on KMS/DRM (al least) returns invalid configs that confuse the bitdiff search used */
423 /* later in this function, so we simply use the first one when using the KMSDRM driver for now. */
424 EGLConfig configs[1];
426 /* 128 seems even nicer here */
427 EGLConfig configs[128];
429 int i, j, best_bitdiff = -1, bitdiff;
431 if (!_this->egl_data) {
432 /* The EGL library wasn't loaded, SDL_GetError() should have info */
436 /* Get a valid EGL configuration */
438 attribs[i++] = EGL_RED_SIZE;
439 attribs[i++] = _this->gl_config.red_size;
440 attribs[i++] = EGL_GREEN_SIZE;
441 attribs[i++] = _this->gl_config.green_size;
442 attribs[i++] = EGL_BLUE_SIZE;
443 attribs[i++] = _this->gl_config.blue_size;
445 if (_this->gl_config.alpha_size) {
446 attribs[i++] = EGL_ALPHA_SIZE;
447 attribs[i++] = _this->gl_config.alpha_size;
450 if (_this->gl_config.buffer_size) {
451 attribs[i++] = EGL_BUFFER_SIZE;
452 attribs[i++] = _this->gl_config.buffer_size;
455 attribs[i++] = EGL_DEPTH_SIZE;
456 attribs[i++] = _this->gl_config.depth_size;
458 if (_this->gl_config.stencil_size) {
459 attribs[i++] = EGL_STENCIL_SIZE;
460 attribs[i++] = _this->gl_config.stencil_size;
463 if (_this->gl_config.multisamplebuffers) {
464 attribs[i++] = EGL_SAMPLE_BUFFERS;
465 attribs[i++] = _this->gl_config.multisamplebuffers;
468 if (_this->gl_config.multisamplesamples) {
469 attribs[i++] = EGL_SAMPLES;
470 attribs[i++] = _this->gl_config.multisamplesamples;
473 if (_this->gl_config.framebuffer_srgb_capable) {
474 #ifdef EGL_KHR_gl_colorspace
475 if (SDL_EGL_HasExtension(_this, SDL_EGL_DISPLAY_EXTENSION, "EGL_KHR_gl_colorspace")) {
476 attribs[i++] = EGL_GL_COLORSPACE_KHR;
477 attribs[i++] = EGL_GL_COLORSPACE_SRGB_KHR;
481 return SDL_SetError("EGL implementation does not support sRGB system framebuffers");
485 attribs[i++] = EGL_RENDERABLE_TYPE;
486 if (_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES) {
487 #ifdef EGL_KHR_create_context
488 if (_this->gl_config.major_version >= 3 &&
489 SDL_EGL_HasExtension(_this, SDL_EGL_DISPLAY_EXTENSION, "EGL_KHR_create_context")) {
490 attribs[i++] = EGL_OPENGL_ES3_BIT_KHR;
493 if (_this->gl_config.major_version >= 2) {
494 attribs[i++] = EGL_OPENGL_ES2_BIT;
496 attribs[i++] = EGL_OPENGL_ES_BIT;
498 _this->egl_data->eglBindAPI(EGL_OPENGL_ES_API);
500 attribs[i++] = EGL_OPENGL_BIT;
501 _this->egl_data->eglBindAPI(EGL_OPENGL_API);
504 attribs[i++] = EGL_NONE;
506 if (_this->egl_data->eglChooseConfig(_this->egl_data->egl_display,
508 configs, SDL_arraysize(configs),
509 &found_configs) == EGL_FALSE ||
510 found_configs == 0) {
511 return SDL_EGL_SetError("Couldn't find matching EGL config", "eglChooseConfig");
514 /* eglChooseConfig returns a number of configurations that match or exceed the requested attribs. */
515 /* From those, we select the one that matches our requirements more closely via a makeshift algorithm */
517 for (i = 0; i < found_configs; i++ ) {
519 for (j = 0; j < SDL_arraysize(attribs) - 1; j += 2) {
520 if (attribs[j] == EGL_NONE) {
524 if ( attribs[j+1] != EGL_DONT_CARE && (
525 attribs[j] == EGL_RED_SIZE ||
526 attribs[j] == EGL_GREEN_SIZE ||
527 attribs[j] == EGL_BLUE_SIZE ||
528 attribs[j] == EGL_ALPHA_SIZE ||
529 attribs[j] == EGL_DEPTH_SIZE ||
530 attribs[j] == EGL_STENCIL_SIZE)) {
531 _this->egl_data->eglGetConfigAttrib(_this->egl_data->egl_display, configs[i], attribs[j], &value);
532 bitdiff += value - attribs[j + 1]; /* value is always >= attrib */
536 if (bitdiff < best_bitdiff || best_bitdiff == -1) {
537 _this->egl_data->egl_config = configs[i];
539 best_bitdiff = bitdiff;
543 break; /* we found an exact match! */
551 SDL_EGL_CreateContext(_THIS, EGLSurface egl_surface)
553 /* max 14 values plus terminator. */
557 EGLContext egl_context, share_context = EGL_NO_CONTEXT;
558 EGLint profile_mask = _this->gl_config.profile_mask;
559 EGLint major_version = _this->gl_config.major_version;
560 EGLint minor_version = _this->gl_config.minor_version;
561 SDL_bool profile_es = (profile_mask == SDL_GL_CONTEXT_PROFILE_ES);
563 if (!_this->egl_data) {
564 /* The EGL library wasn't loaded, SDL_GetError() should have info */
568 if (_this->gl_config.share_with_current_context) {
569 share_context = (EGLContext)SDL_GL_GetCurrentContext();
572 /* Set the context version and other attributes. */
573 if ((major_version < 3 || (minor_version == 0 && profile_es)) &&
574 _this->gl_config.flags == 0 &&
575 (profile_mask == 0 || profile_es)) {
576 /* Create a context without using EGL_KHR_create_context attribs.
577 * When creating a GLES context without EGL_KHR_create_context we can
578 * only specify the major version. When creating a desktop GL context
579 * we can't specify any version, so we only try in that case when the
580 * version is less than 3.0 (matches SDL's GLX/WGL behavior.)
583 attribs[attr++] = EGL_CONTEXT_CLIENT_VERSION;
584 attribs[attr++] = SDL_max(major_version, 1);
587 #ifdef EGL_KHR_create_context
588 /* The Major/minor version, context profiles, and context flags can
589 * only be specified when this extension is available.
591 if (SDL_EGL_HasExtension(_this, SDL_EGL_DISPLAY_EXTENSION, "EGL_KHR_create_context")) {
592 attribs[attr++] = EGL_CONTEXT_MAJOR_VERSION_KHR;
593 attribs[attr++] = major_version;
594 attribs[attr++] = EGL_CONTEXT_MINOR_VERSION_KHR;
595 attribs[attr++] = minor_version;
597 /* SDL profile bits match EGL profile bits. */
598 if (profile_mask != 0 && profile_mask != SDL_GL_CONTEXT_PROFILE_ES) {
599 attribs[attr++] = EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR;
600 attribs[attr++] = profile_mask;
603 /* SDL flags match EGL flags. */
604 if (_this->gl_config.flags != 0) {
605 attribs[attr++] = EGL_CONTEXT_FLAGS_KHR;
606 attribs[attr++] = _this->gl_config.flags;
609 #endif /* EGL_KHR_create_context */
611 SDL_SetError("Could not create EGL context (context attributes are not supported)");
616 if (_this->gl_config.no_error) {
617 #ifdef EGL_KHR_create_context_no_error
618 if (SDL_EGL_HasExtension(_this, SDL_EGL_DISPLAY_EXTENSION, "EGL_KHR_create_context_no_error")) {
619 attribs[attr++] = EGL_CONTEXT_OPENGL_NO_ERROR_KHR;
620 attribs[attr++] = _this->gl_config.no_error;
624 SDL_SetError("EGL implementation does not support no_error contexts");
629 attribs[attr++] = EGL_NONE;
633 _this->egl_data->eglBindAPI(EGL_OPENGL_ES_API);
635 _this->egl_data->eglBindAPI(EGL_OPENGL_API);
638 egl_context = _this->egl_data->eglCreateContext(_this->egl_data->egl_display,
639 _this->egl_data->egl_config,
640 share_context, attribs);
642 if (egl_context == EGL_NO_CONTEXT) {
643 SDL_EGL_SetError("Could not create EGL context", "eglCreateContext");
647 _this->egl_data->egl_swapinterval = 0;
649 if (SDL_EGL_MakeCurrent(_this, egl_surface, egl_context) < 0) {
650 /* Save the SDL error set by SDL_EGL_MakeCurrent */
651 char errorText[1024];
652 SDL_strlcpy(errorText, SDL_GetError(), SDL_arraysize(errorText));
654 /* Delete the context, which may alter the value returned by SDL_GetError() */
655 SDL_EGL_DeleteContext(_this, egl_context);
657 /* Restore the SDL error */
658 SDL_SetError("%s", errorText);
663 return (SDL_GLContext) egl_context;
667 SDL_EGL_MakeCurrent(_THIS, EGLSurface egl_surface, SDL_GLContext context)
669 EGLContext egl_context = (EGLContext) context;
671 if (!_this->egl_data) {
672 return SDL_SetError("OpenGL not initialized");
675 /* The android emulator crashes badly if you try to eglMakeCurrent
676 * with a valid context and invalid surface, so we have to check for both here.
678 if (!egl_context || !egl_surface) {
679 _this->egl_data->eglMakeCurrent(_this->egl_data->egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
681 if (!_this->egl_data->eglMakeCurrent(_this->egl_data->egl_display,
682 egl_surface, egl_surface, egl_context)) {
683 return SDL_EGL_SetError("Unable to make EGL context current", "eglMakeCurrent");
691 SDL_EGL_SetSwapInterval(_THIS, int interval)
695 if (!_this->egl_data) {
696 return SDL_SetError("EGL not initialized");
699 status = _this->egl_data->eglSwapInterval(_this->egl_data->egl_display, interval);
700 if (status == EGL_TRUE) {
701 _this->egl_data->egl_swapinterval = interval;
705 return SDL_EGL_SetError("Unable to set the EGL swap interval", "eglSwapInterval");
709 SDL_EGL_GetSwapInterval(_THIS)
711 if (!_this->egl_data) {
712 SDL_SetError("EGL not initialized");
716 return _this->egl_data->egl_swapinterval;
720 SDL_EGL_SwapBuffers(_THIS, EGLSurface egl_surface)
722 if (!_this->egl_data->eglSwapBuffers(_this->egl_data->egl_display, egl_surface)) {
723 return SDL_EGL_SetError("unable to show color buffer in an OS-native window", "eglSwapBuffers");
729 SDL_EGL_DeleteContext(_THIS, SDL_GLContext context)
731 EGLContext egl_context = (EGLContext) context;
733 /* Clean up GLES and EGL */
734 if (!_this->egl_data) {
738 if (egl_context != NULL && egl_context != EGL_NO_CONTEXT) {
739 SDL_EGL_MakeCurrent(_this, NULL, NULL);
740 _this->egl_data->eglDestroyContext(_this->egl_data->egl_display, egl_context);
746 SDL_EGL_CreateSurface(_THIS, NativeWindowType nw)
748 EGLSurface * surface;
750 if (SDL_EGL_ChooseConfig(_this) != 0) {
751 return EGL_NO_SURFACE;
754 #if SDL_VIDEO_DRIVER_ANDROID
756 /* Android docs recommend doing this!
757 * Ref: http://developer.android.com/reference/android/app/NativeActivity.html
760 _this->egl_data->eglGetConfigAttrib(_this->egl_data->egl_display,
761 _this->egl_data->egl_config,
762 EGL_NATIVE_VISUAL_ID, &format);
764 ANativeWindow_setBuffersGeometry(nw, 0, 0, format);
768 surface = _this->egl_data->eglCreateWindowSurface(
769 _this->egl_data->egl_display,
770 _this->egl_data->egl_config,
772 if (surface == EGL_NO_SURFACE) {
773 SDL_EGL_SetError("unable to create an EGL window surface", "eglCreateWindowSurface");
779 SDL_EGL_DestroySurface(_THIS, EGLSurface egl_surface)
781 if (!_this->egl_data) {
785 if (egl_surface != EGL_NO_SURFACE) {
786 _this->egl_data->eglDestroySurface(_this->egl_data->egl_display, egl_surface);
790 #endif /* SDL_VIDEO_OPENGL_EGL */
792 /* vi: set ts=4 sw=4 expandtab: */