2 * Simple DirectMedia Layer
3 * Copyright (C) 1997-2015 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 */
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 attribs[i++] = EGL_RENDERABLE_TYPE;
344 if (_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES) {
345 #ifdef EGL_KHR_create_context
346 if (_this->gl_config.major_version >= 3 &&
347 SDL_EGL_HasExtension(_this, "EGL_KHR_create_context")) {
348 attribs[i++] = EGL_OPENGL_ES3_BIT_KHR;
351 if (_this->gl_config.major_version >= 2) {
352 attribs[i++] = EGL_OPENGL_ES2_BIT;
354 attribs[i++] = EGL_OPENGL_ES_BIT;
356 _this->egl_data->eglBindAPI(EGL_OPENGL_ES_API);
358 attribs[i++] = EGL_OPENGL_BIT;
359 _this->egl_data->eglBindAPI(EGL_OPENGL_API);
362 attribs[i++] = EGL_NONE;
364 if (_this->egl_data->eglChooseConfig(_this->egl_data->egl_display,
366 configs, SDL_arraysize(configs),
367 &found_configs) == EGL_FALSE ||
368 found_configs == 0) {
369 return SDL_SetError("Couldn't find matching EGL config");
372 /* eglChooseConfig returns a number of configurations that match or exceed the requested attribs. */
373 /* From those, we select the one that matches our requirements more closely via a makeshift algorithm */
375 for (i = 0; i < found_configs; i++ ) {
377 for (j = 0; j < SDL_arraysize(attribs) - 1; j += 2) {
378 if (attribs[j] == EGL_NONE) {
382 if ( attribs[j+1] != EGL_DONT_CARE && (
383 attribs[j] == EGL_RED_SIZE ||
384 attribs[j] == EGL_GREEN_SIZE ||
385 attribs[j] == EGL_BLUE_SIZE ||
386 attribs[j] == EGL_ALPHA_SIZE ||
387 attribs[j] == EGL_DEPTH_SIZE ||
388 attribs[j] == EGL_STENCIL_SIZE)) {
389 _this->egl_data->eglGetConfigAttrib(_this->egl_data->egl_display, configs[i], attribs[j], &value);
390 bitdiff += value - attribs[j + 1]; /* value is always >= attrib */
394 if (bitdiff < best_bitdiff || best_bitdiff == -1) {
395 _this->egl_data->egl_config = configs[i];
397 best_bitdiff = bitdiff;
401 break; /* we found an exact match! */
409 SDL_EGL_CreateContext(_THIS, EGLSurface egl_surface)
411 /* max 14 values plus terminator. */
415 EGLContext egl_context, share_context = EGL_NO_CONTEXT;
416 EGLint profile_mask = _this->gl_config.profile_mask;
418 if (!_this->egl_data) {
419 /* The EGL library wasn't loaded, SDL_GetError() should have info */
423 if (_this->gl_config.share_with_current_context) {
424 share_context = (EGLContext)SDL_GL_GetCurrentContext();
427 /* Set the context version and other attributes. */
428 if (_this->gl_config.major_version < 3 && _this->gl_config.flags == 0 &&
429 (profile_mask == 0 || profile_mask == SDL_GL_CONTEXT_PROFILE_ES)) {
430 /* Create a context without using EGL_KHR_create_context attribs. */
431 if (profile_mask == SDL_GL_CONTEXT_PROFILE_ES) {
432 attribs[attr++] = EGL_CONTEXT_CLIENT_VERSION;
433 attribs[attr++] = SDL_max(_this->gl_config.major_version, 1);
436 #ifdef EGL_KHR_create_context
437 /* The Major/minor version, context profiles, and context flags can
438 * only be specified when this extension is available.
440 if (SDL_EGL_HasExtension(_this, "EGL_KHR_create_context")) {
441 attribs[attr++] = EGL_CONTEXT_MAJOR_VERSION_KHR;
442 attribs[attr++] = _this->gl_config.major_version;
443 attribs[attr++] = EGL_CONTEXT_MINOR_VERSION_KHR;
444 attribs[attr++] = _this->gl_config.minor_version;
446 /* SDL profile bits match EGL profile bits. */
447 if (profile_mask != 0 && profile_mask != SDL_GL_CONTEXT_PROFILE_ES) {
448 attribs[attr++] = EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR;
449 attribs[attr++] = profile_mask;
452 /* SDL flags match EGL flags. */
453 if (_this->gl_config.flags != 0) {
454 attribs[attr++] = EGL_CONTEXT_FLAGS_KHR;
455 attribs[attr++] = _this->gl_config.flags;
458 #endif /* EGL_KHR_create_context */
460 SDL_SetError("Could not create EGL context (context attributes are not supported)");
465 attribs[attr++] = EGL_NONE;
468 if (_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES) {
469 _this->egl_data->eglBindAPI(EGL_OPENGL_ES_API);
471 _this->egl_data->eglBindAPI(EGL_OPENGL_API);
474 egl_context = _this->egl_data->eglCreateContext(_this->egl_data->egl_display,
475 _this->egl_data->egl_config,
476 share_context, attribs);
478 if (egl_context == EGL_NO_CONTEXT) {
479 SDL_SetError("Could not create EGL context");
483 _this->egl_data->egl_swapinterval = 0;
485 if (SDL_EGL_MakeCurrent(_this, egl_surface, egl_context) < 0) {
486 SDL_EGL_DeleteContext(_this, egl_context);
487 SDL_SetError("Could not make EGL context current");
491 return (SDL_GLContext) egl_context;
495 SDL_EGL_MakeCurrent(_THIS, EGLSurface egl_surface, SDL_GLContext context)
497 EGLContext egl_context = (EGLContext) context;
499 if (!_this->egl_data) {
500 return SDL_SetError("OpenGL not initialized");
503 /* The android emulator crashes badly if you try to eglMakeCurrent
504 * with a valid context and invalid surface, so we have to check for both here.
506 if (!egl_context || !egl_surface) {
507 _this->egl_data->eglMakeCurrent(_this->egl_data->egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
509 if (!_this->egl_data->eglMakeCurrent(_this->egl_data->egl_display,
510 egl_surface, egl_surface, egl_context)) {
511 return SDL_SetError("Unable to make EGL context current");
519 SDL_EGL_SetSwapInterval(_THIS, int interval)
523 if (!_this->egl_data) {
524 return SDL_SetError("EGL not initialized");
527 status = _this->egl_data->eglSwapInterval(_this->egl_data->egl_display, interval);
528 if (status == EGL_TRUE) {
529 _this->egl_data->egl_swapinterval = interval;
533 return SDL_SetError("Unable to set the EGL swap interval");
537 SDL_EGL_GetSwapInterval(_THIS)
539 if (!_this->egl_data) {
540 return SDL_SetError("EGL not initialized");
543 return _this->egl_data->egl_swapinterval;
547 SDL_EGL_SwapBuffers(_THIS, EGLSurface egl_surface)
549 _this->egl_data->eglSwapBuffers(_this->egl_data->egl_display, egl_surface);
553 SDL_EGL_DeleteContext(_THIS, SDL_GLContext context)
555 EGLContext egl_context = (EGLContext) context;
557 /* Clean up GLES and EGL */
558 if (!_this->egl_data) {
562 if (egl_context != NULL && egl_context != EGL_NO_CONTEXT) {
563 SDL_EGL_MakeCurrent(_this, NULL, NULL);
564 _this->egl_data->eglDestroyContext(_this->egl_data->egl_display, egl_context);
570 SDL_EGL_CreateSurface(_THIS, NativeWindowType nw)
572 if (SDL_EGL_ChooseConfig(_this) != 0) {
573 return EGL_NO_SURFACE;
578 /* Android docs recommend doing this!
579 * Ref: http://developer.android.com/reference/android/app/NativeActivity.html
582 _this->egl_data->eglGetConfigAttrib(_this->egl_data->egl_display,
583 _this->egl_data->egl_config,
584 EGL_NATIVE_VISUAL_ID, &format);
586 ANativeWindow_setBuffersGeometry(nw, 0, 0, format);
590 return _this->egl_data->eglCreateWindowSurface(
591 _this->egl_data->egl_display,
592 _this->egl_data->egl_config,
597 SDL_EGL_DestroySurface(_THIS, EGLSurface egl_surface)
599 if (!_this->egl_data) {
603 if (egl_surface != EGL_NO_SURFACE) {
604 _this->egl_data->eglDestroySurface(_this->egl_data->egl_display, egl_surface);
608 #endif /* SDL_VIDEO_OPENGL_EGL */
610 /* vi: set ts=4 sw=4 expandtab: */