2 * Simple DirectMedia Layer
3 * Copyright (C) 1997-2016 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"
29 #include "SDL_sysvideo.h"
30 #include "SDL_egl_c.h"
31 #include "SDL_loadso.h"
32 #include "SDL_hints.h"
34 #ifdef EGL_KHR_create_context
35 /* EGL_OPENGL_ES3_BIT_KHR was added in version 13 of the extension. */
36 #ifndef EGL_OPENGL_ES3_BIT_KHR
37 #define EGL_OPENGL_ES3_BIT_KHR 0x00000040
39 #endif /* EGL_KHR_create_context */
41 #if SDL_VIDEO_DRIVER_RPI
42 /* Raspbian places the OpenGL ES/EGL binaries in a non standard path */
43 #define DEFAULT_EGL "/opt/vc/lib/libEGL.so"
44 #define DEFAULT_OGL_ES2 "/opt/vc/lib/libGLESv2.so"
45 #define DEFAULT_OGL_ES_PVR "/opt/vc/lib/libGLES_CM.so"
46 #define DEFAULT_OGL_ES "/opt/vc/lib/libGLESv1_CM.so"
48 #elif SDL_VIDEO_DRIVER_ANDROID || SDL_VIDEO_DRIVER_VIVANTE
50 #define DEFAULT_EGL "libEGL.so"
51 #define DEFAULT_OGL_ES2 "libGLESv2.so"
52 #define DEFAULT_OGL_ES_PVR "libGLES_CM.so"
53 #define DEFAULT_OGL_ES "libGLESv1_CM.so"
55 #elif SDL_VIDEO_DRIVER_WINDOWS || SDL_VIDEO_DRIVER_WINRT
56 /* EGL AND OpenGL ES support via ANGLE */
57 #define DEFAULT_EGL "libEGL.dll"
58 #define DEFAULT_OGL_ES2 "libGLESv2.dll"
59 #define DEFAULT_OGL_ES_PVR "libGLES_CM.dll"
60 #define DEFAULT_OGL_ES "libGLESv1_CM.dll"
64 #define DEFAULT_OGL "libGL.so.1"
65 #define DEFAULT_EGL "libEGL.so.1"
66 #define DEFAULT_OGL_ES2 "libGLESv2.so.2"
67 #define DEFAULT_OGL_ES_PVR "libGLES_CM.so.1"
68 #define DEFAULT_OGL_ES "libGLESv1_CM.so.1"
69 #endif /* SDL_VIDEO_DRIVER_RPI */
71 #define LOAD_FUNC(NAME) \
72 _this->egl_data->NAME = SDL_LoadFunction(_this->egl_data->dll_handle, #NAME); \
73 if (!_this->egl_data->NAME) \
75 return SDL_SetError("Could not retrieve EGL function " #NAME); \
78 /* EGL implementation of SDL OpenGL ES support */
79 #ifdef EGL_KHR_create_context
80 static int SDL_EGL_HasExtension(_THIS, const char *ext)
88 ext_len = SDL_strlen(ext);
89 exts = _this->egl_data->eglQueryString(_this->egl_data->egl_display, EGL_EXTENSIONS);
94 for (i = 0; exts[i] != 0; i++) {
96 if (ext_len == len && !SDL_strncmp(ext_word, ext, len)) {
101 ext_word = &exts[i + 1];
110 #endif /* EGL_KHR_create_context */
113 SDL_EGL_GetProcAddress(_THIS, const char *proc)
115 static char procname[1024];
118 /* eglGetProcAddress is busted on Android http://code.google.com/p/android/issues/detail?id=7681 */
119 #if !defined(SDL_VIDEO_DRIVER_ANDROID) && !defined(SDL_VIDEO_DRIVER_MIR)
120 if (_this->egl_data->eglGetProcAddress) {
121 retval = _this->egl_data->eglGetProcAddress(proc);
128 retval = SDL_LoadFunction(_this->egl_data->egl_dll_handle, proc);
129 if (!retval && SDL_strlen(proc) <= 1022) {
131 SDL_strlcpy(procname + 1, proc, 1022);
132 retval = SDL_LoadFunction(_this->egl_data->egl_dll_handle, procname);
138 SDL_EGL_UnloadLibrary(_THIS)
140 if (_this->egl_data) {
141 if (_this->egl_data->egl_display) {
142 _this->egl_data->eglTerminate(_this->egl_data->egl_display);
143 _this->egl_data->egl_display = NULL;
146 if (_this->egl_data->dll_handle) {
147 SDL_UnloadObject(_this->egl_data->dll_handle);
148 _this->egl_data->dll_handle = NULL;
150 if (_this->egl_data->egl_dll_handle) {
151 SDL_UnloadObject(_this->egl_data->egl_dll_handle);
152 _this->egl_data->egl_dll_handle = NULL;
155 SDL_free(_this->egl_data);
156 _this->egl_data = NULL;
161 SDL_EGL_LoadLibrary(_THIS, const char *egl_path, NativeDisplayType native_display)
163 void *dll_handle = NULL, *egl_dll_handle = NULL; /* The naming is counter intuitive, but hey, I just work here -- Gabriel */
164 const char *path = NULL;
165 #if SDL_VIDEO_DRIVER_WINDOWS || SDL_VIDEO_DRIVER_WINRT
166 const char *d3dcompiler;
169 if (_this->egl_data) {
170 return SDL_SetError("OpenGL ES context already created");
173 _this->egl_data = (struct SDL_EGL_VideoData *) SDL_calloc(1, sizeof(SDL_EGL_VideoData));
174 if (!_this->egl_data) {
175 return SDL_OutOfMemory();
178 #if SDL_VIDEO_DRIVER_WINDOWS || SDL_VIDEO_DRIVER_WINRT
179 d3dcompiler = SDL_GetHint(SDL_HINT_VIDEO_WIN_D3DCOMPILER);
181 if (WIN_IsWindowsVistaOrGreater()) {
182 d3dcompiler = "d3dcompiler_46.dll";
184 d3dcompiler = "d3dcompiler_43.dll";
187 if (SDL_strcasecmp(d3dcompiler, "none") != 0) {
188 SDL_LoadObject(d3dcompiler);
192 /* A funny thing, loading EGL.so first does not work on the Raspberry, so we load libGL* first */
193 path = SDL_getenv("SDL_VIDEO_GL_DRIVER");
195 egl_dll_handle = SDL_LoadObject(path);
198 if (egl_dll_handle == NULL) {
199 if (_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES) {
200 if (_this->gl_config.major_version > 1) {
201 path = DEFAULT_OGL_ES2;
202 egl_dll_handle = SDL_LoadObject(path);
204 path = DEFAULT_OGL_ES;
205 egl_dll_handle = SDL_LoadObject(path);
206 if (egl_dll_handle == NULL) {
207 path = DEFAULT_OGL_ES_PVR;
208 egl_dll_handle = SDL_LoadObject(path);
215 egl_dll_handle = SDL_LoadObject(path);
219 _this->egl_data->egl_dll_handle = egl_dll_handle;
221 if (egl_dll_handle == NULL) {
222 return SDL_SetError("Could not initialize OpenGL / GLES library");
225 /* Loading libGL* in the previous step took care of loading libEGL.so, but we future proof by double checking */
226 if (egl_path != NULL) {
227 dll_handle = SDL_LoadObject(egl_path);
229 /* Try loading a EGL symbol, if it does not work try the default library paths */
230 if (dll_handle == NULL || SDL_LoadFunction(dll_handle, "eglChooseConfig") == NULL) {
231 if (dll_handle != NULL) {
232 SDL_UnloadObject(dll_handle);
234 path = SDL_getenv("SDL_VIDEO_EGL_DRIVER");
238 dll_handle = SDL_LoadObject(path);
239 if (dll_handle == NULL || SDL_LoadFunction(dll_handle, "eglChooseConfig") == NULL) {
240 if (dll_handle != NULL) {
241 SDL_UnloadObject(dll_handle);
243 return SDL_SetError("Could not load EGL library");
248 _this->egl_data->dll_handle = dll_handle;
250 /* Load new function pointers */
251 LOAD_FUNC(eglGetDisplay);
252 LOAD_FUNC(eglInitialize);
253 LOAD_FUNC(eglTerminate);
254 LOAD_FUNC(eglGetProcAddress);
255 LOAD_FUNC(eglChooseConfig);
256 LOAD_FUNC(eglGetConfigAttrib);
257 LOAD_FUNC(eglCreateContext);
258 LOAD_FUNC(eglDestroyContext);
259 LOAD_FUNC(eglCreateWindowSurface);
260 LOAD_FUNC(eglDestroySurface);
261 LOAD_FUNC(eglMakeCurrent);
262 LOAD_FUNC(eglSwapBuffers);
263 LOAD_FUNC(eglSwapInterval);
264 LOAD_FUNC(eglWaitNative);
265 LOAD_FUNC(eglWaitGL);
266 LOAD_FUNC(eglBindAPI);
267 LOAD_FUNC(eglQueryString);
269 #if !defined(__WINRT__)
270 _this->egl_data->egl_display = _this->egl_data->eglGetDisplay(native_display);
271 if (!_this->egl_data->egl_display) {
272 return SDL_SetError("Could not get EGL display");
275 if (_this->egl_data->eglInitialize(_this->egl_data->egl_display, NULL, NULL) != EGL_TRUE) {
276 return SDL_SetError("Could not initialize EGL");
280 _this->gl_config.driver_loaded = 1;
283 SDL_strlcpy(_this->gl_config.driver_path, path, sizeof(_this->gl_config.driver_path) - 1);
285 *_this->gl_config.driver_path = '\0';
292 SDL_EGL_ChooseConfig(_THIS)
296 EGLint found_configs = 0, value;
297 /* 128 seems even nicer here */
298 EGLConfig configs[128];
299 int i, j, best_bitdiff = -1, bitdiff;
301 if (!_this->egl_data) {
302 /* The EGL library wasn't loaded, SDL_GetError() should have info */
306 /* Get a valid EGL configuration */
308 attribs[i++] = EGL_RED_SIZE;
309 attribs[i++] = _this->gl_config.red_size;
310 attribs[i++] = EGL_GREEN_SIZE;
311 attribs[i++] = _this->gl_config.green_size;
312 attribs[i++] = EGL_BLUE_SIZE;
313 attribs[i++] = _this->gl_config.blue_size;
315 if (_this->gl_config.alpha_size) {
316 attribs[i++] = EGL_ALPHA_SIZE;
317 attribs[i++] = _this->gl_config.alpha_size;
320 if (_this->gl_config.buffer_size) {
321 attribs[i++] = EGL_BUFFER_SIZE;
322 attribs[i++] = _this->gl_config.buffer_size;
325 attribs[i++] = EGL_DEPTH_SIZE;
326 attribs[i++] = _this->gl_config.depth_size;
328 if (_this->gl_config.stencil_size) {
329 attribs[i++] = EGL_STENCIL_SIZE;
330 attribs[i++] = _this->gl_config.stencil_size;
333 if (_this->gl_config.multisamplebuffers) {
334 attribs[i++] = EGL_SAMPLE_BUFFERS;
335 attribs[i++] = _this->gl_config.multisamplebuffers;
338 if (_this->gl_config.multisamplesamples) {
339 attribs[i++] = EGL_SAMPLES;
340 attribs[i++] = _this->gl_config.multisamplesamples;
343 if (_this->gl_config.framebuffer_srgb_capable) {
344 #ifdef EGL_KHR_gl_colorspace
345 if (SDL_EGL_HasExtension(_this, "EGL_KHR_gl_colorspace")) {
346 attribs[i++] = EGL_GL_COLORSPACE_KHR;
347 attribs[i++] = EGL_GL_COLORSPACE_SRGB_KHR;
351 return SDL_SetError("EGL implementation does not support sRGB system framebuffers");
355 attribs[i++] = EGL_RENDERABLE_TYPE;
356 if (_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES) {
357 #ifdef EGL_KHR_create_context
358 if (_this->gl_config.major_version >= 3 &&
359 SDL_EGL_HasExtension(_this, "EGL_KHR_create_context")) {
360 attribs[i++] = EGL_OPENGL_ES3_BIT_KHR;
363 if (_this->gl_config.major_version >= 2) {
364 attribs[i++] = EGL_OPENGL_ES2_BIT;
366 attribs[i++] = EGL_OPENGL_ES_BIT;
368 _this->egl_data->eglBindAPI(EGL_OPENGL_ES_API);
370 attribs[i++] = EGL_OPENGL_BIT;
371 _this->egl_data->eglBindAPI(EGL_OPENGL_API);
374 attribs[i++] = EGL_NONE;
376 if (_this->egl_data->eglChooseConfig(_this->egl_data->egl_display,
378 configs, SDL_arraysize(configs),
379 &found_configs) == EGL_FALSE ||
380 found_configs == 0) {
381 return SDL_SetError("Couldn't find matching EGL config");
384 /* eglChooseConfig returns a number of configurations that match or exceed the requested attribs. */
385 /* From those, we select the one that matches our requirements more closely via a makeshift algorithm */
387 for (i = 0; i < found_configs; i++ ) {
389 for (j = 0; j < SDL_arraysize(attribs) - 1; j += 2) {
390 if (attribs[j] == EGL_NONE) {
394 if ( attribs[j+1] != EGL_DONT_CARE && (
395 attribs[j] == EGL_RED_SIZE ||
396 attribs[j] == EGL_GREEN_SIZE ||
397 attribs[j] == EGL_BLUE_SIZE ||
398 attribs[j] == EGL_ALPHA_SIZE ||
399 attribs[j] == EGL_DEPTH_SIZE ||
400 attribs[j] == EGL_STENCIL_SIZE)) {
401 _this->egl_data->eglGetConfigAttrib(_this->egl_data->egl_display, configs[i], attribs[j], &value);
402 bitdiff += value - attribs[j + 1]; /* value is always >= attrib */
406 if (bitdiff < best_bitdiff || best_bitdiff == -1) {
407 _this->egl_data->egl_config = configs[i];
409 best_bitdiff = bitdiff;
413 break; /* we found an exact match! */
421 SDL_EGL_CreateContext(_THIS, EGLSurface egl_surface)
423 /* max 14 values plus terminator. */
427 EGLContext egl_context, share_context = EGL_NO_CONTEXT;
428 EGLint profile_mask = _this->gl_config.profile_mask;
429 EGLint major_version = _this->gl_config.major_version;
430 EGLint minor_version = _this->gl_config.minor_version;
431 SDL_bool profile_es = (profile_mask == SDL_GL_CONTEXT_PROFILE_ES);
433 if (!_this->egl_data) {
434 /* The EGL library wasn't loaded, SDL_GetError() should have info */
438 if (_this->gl_config.share_with_current_context) {
439 share_context = (EGLContext)SDL_GL_GetCurrentContext();
442 /* Set the context version and other attributes. */
443 if ((major_version < 3 || (minor_version == 0 && profile_es)) &&
444 _this->gl_config.flags == 0 &&
445 (profile_mask == 0 || profile_es)) {
446 /* Create a context without using EGL_KHR_create_context attribs.
447 * When creating a GLES context without EGL_KHR_create_context we can
448 * only specify the major version. When creating a desktop GL context
449 * we can't specify any version, so we only try in that case when the
450 * version is less than 3.0 (matches SDL's GLX/WGL behavior.)
453 attribs[attr++] = EGL_CONTEXT_CLIENT_VERSION;
454 attribs[attr++] = SDL_max(major_version, 1);
457 #ifdef EGL_KHR_create_context
458 /* The Major/minor version, context profiles, and context flags can
459 * only be specified when this extension is available.
461 if (SDL_EGL_HasExtension(_this, "EGL_KHR_create_context")) {
462 attribs[attr++] = EGL_CONTEXT_MAJOR_VERSION_KHR;
463 attribs[attr++] = major_version;
464 attribs[attr++] = EGL_CONTEXT_MINOR_VERSION_KHR;
465 attribs[attr++] = minor_version;
467 /* SDL profile bits match EGL profile bits. */
468 if (profile_mask != 0 && profile_mask != SDL_GL_CONTEXT_PROFILE_ES) {
469 attribs[attr++] = EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR;
470 attribs[attr++] = profile_mask;
473 /* SDL flags match EGL flags. */
474 if (_this->gl_config.flags != 0) {
475 attribs[attr++] = EGL_CONTEXT_FLAGS_KHR;
476 attribs[attr++] = _this->gl_config.flags;
479 #endif /* EGL_KHR_create_context */
481 SDL_SetError("Could not create EGL context (context attributes are not supported)");
486 attribs[attr++] = EGL_NONE;
490 _this->egl_data->eglBindAPI(EGL_OPENGL_ES_API);
492 _this->egl_data->eglBindAPI(EGL_OPENGL_API);
495 egl_context = _this->egl_data->eglCreateContext(_this->egl_data->egl_display,
496 _this->egl_data->egl_config,
497 share_context, attribs);
499 if (egl_context == EGL_NO_CONTEXT) {
500 SDL_SetError("Could not create EGL context");
504 _this->egl_data->egl_swapinterval = 0;
506 if (SDL_EGL_MakeCurrent(_this, egl_surface, egl_context) < 0) {
507 SDL_EGL_DeleteContext(_this, egl_context);
508 SDL_SetError("Could not make EGL context current");
512 return (SDL_GLContext) egl_context;
516 SDL_EGL_MakeCurrent(_THIS, EGLSurface egl_surface, SDL_GLContext context)
518 EGLContext egl_context = (EGLContext) context;
520 if (!_this->egl_data) {
521 return SDL_SetError("OpenGL not initialized");
524 /* The android emulator crashes badly if you try to eglMakeCurrent
525 * with a valid context and invalid surface, so we have to check for both here.
527 if (!egl_context || !egl_surface) {
528 _this->egl_data->eglMakeCurrent(_this->egl_data->egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
530 if (!_this->egl_data->eglMakeCurrent(_this->egl_data->egl_display,
531 egl_surface, egl_surface, egl_context)) {
532 return SDL_SetError("Unable to make EGL context current");
540 SDL_EGL_SetSwapInterval(_THIS, int interval)
544 if (!_this->egl_data) {
545 return SDL_SetError("EGL not initialized");
548 status = _this->egl_data->eglSwapInterval(_this->egl_data->egl_display, interval);
549 if (status == EGL_TRUE) {
550 _this->egl_data->egl_swapinterval = interval;
554 return SDL_SetError("Unable to set the EGL swap interval");
558 SDL_EGL_GetSwapInterval(_THIS)
560 if (!_this->egl_data) {
561 SDL_SetError("EGL not initialized");
565 return _this->egl_data->egl_swapinterval;
569 SDL_EGL_SwapBuffers(_THIS, EGLSurface egl_surface)
571 _this->egl_data->eglSwapBuffers(_this->egl_data->egl_display, egl_surface);
575 SDL_EGL_DeleteContext(_THIS, SDL_GLContext context)
577 EGLContext egl_context = (EGLContext) context;
579 /* Clean up GLES and EGL */
580 if (!_this->egl_data) {
584 if (egl_context != NULL && egl_context != EGL_NO_CONTEXT) {
585 SDL_EGL_MakeCurrent(_this, NULL, NULL);
586 _this->egl_data->eglDestroyContext(_this->egl_data->egl_display, egl_context);
592 SDL_EGL_CreateSurface(_THIS, NativeWindowType nw)
594 if (SDL_EGL_ChooseConfig(_this) != 0) {
595 return EGL_NO_SURFACE;
600 /* Android docs recommend doing this!
601 * Ref: http://developer.android.com/reference/android/app/NativeActivity.html
604 _this->egl_data->eglGetConfigAttrib(_this->egl_data->egl_display,
605 _this->egl_data->egl_config,
606 EGL_NATIVE_VISUAL_ID, &format);
608 ANativeWindow_setBuffersGeometry(nw, 0, 0, format);
612 return _this->egl_data->eglCreateWindowSurface(
613 _this->egl_data->egl_display,
614 _this->egl_data->egl_config,
619 SDL_EGL_DestroySurface(_THIS, EGLSurface egl_surface)
621 if (!_this->egl_data) {
625 if (egl_surface != EGL_NO_SURFACE) {
626 _this->egl_data->eglDestroySurface(_this->egl_data->egl_display, egl_surface);
630 #endif /* SDL_VIDEO_OPENGL_EGL */
632 /* vi: set ts=4 sw=4 expandtab: */