SDL  2.0
SDL_egl.c
Go to the documentation of this file.
1 /*
2  * Simple DirectMedia Layer
3  * Copyright (C) 1997-2018 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 #endif
31 
32 #include "SDL_sysvideo.h"
33 #include "SDL_log.h"
34 #include "SDL_egl_c.h"
35 #include "SDL_loadso.h"
36 #include "SDL_hints.h"
37 
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
42 #endif
43 #endif /* EGL_KHR_create_context */
44 
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" )
53 
54 #elif SDL_VIDEO_DRIVER_ANDROID || SDL_VIDEO_DRIVER_VIVANTE
55 /* Android */
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"
60 
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"
67 
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" //???
74 
75 #else
76 /* Desktop Linux */
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 */
83 
84 #ifdef SDL_VIDEO_STATIC_ANGLE
85 #define LOAD_FUNC(NAME) \
86 _this->egl_data->NAME = (void *)NAME;
87 #else
88 #define LOAD_FUNC(NAME) \
89 _this->egl_data->NAME = SDL_LoadFunction(_this->egl_data->dll_handle, #NAME); \
90 if (!_this->egl_data->NAME) \
91 { \
92  return SDL_SetError("Could not retrieve EGL function " #NAME); \
93 }
94 #endif
95 
96 static const char * SDL_EGL_GetErrorName(EGLint eglErrorCode)
97 {
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);
115  }
116  return "";
117 }
118 
119 int SDL_EGL_SetErrorEx(const char * message, const char * eglFunctionName, EGLint eglErrorCode)
120 {
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;
127  }
128  return SDL_SetError("%s (call to %s failed, reporting an error of %s)", message, eglFunctionName, errorText);
129 }
130 
131 /* EGL implementation of SDL OpenGL ES support */
132 typedef enum {
133  SDL_EGL_DISPLAY_EXTENSION,
134  SDL_EGL_CLIENT_EXTENSION
135 } SDL_EGL_ExtensionType;
136 
137 static SDL_bool SDL_EGL_HasExtension(_THIS, SDL_EGL_ExtensionType type, const char *ext)
138 {
139  size_t ext_len;
140  const char *ext_override;
141  const char *egl_extstr;
142  const char *ext_start;
143 
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"); */
147  return SDL_FALSE;
148  }
149 
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.
153  * Bit Action
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.
156  */
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) {
161  return SDL_FALSE;
162  } else if (disable_ext & 0x02 && type == SDL_EGL_CLIENT_EXTENSION) {
163  return SDL_FALSE;
164  }
165  }
166 
167  ext_len = SDL_strlen(ext);
168  switch (type) {
169  case SDL_EGL_DISPLAY_EXTENSION:
170  egl_extstr = _this->egl_data->eglQueryString(_this->egl_data->egl_display, EGL_EXTENSIONS);
171  break;
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.
176  */
177  egl_extstr = _this->egl_data->eglQueryString(EGL_NO_DISPLAY, EGL_EXTENSIONS);
178  break;
179  default:
180  /* SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "SDL_EGL_HasExtension: Invalid extension type"); */
181  return SDL_FALSE;
182  }
183 
184  if (egl_extstr != NULL) {
185  ext_start = egl_extstr;
186 
187  while (*ext_start) {
188  ext_start = SDL_strstr(ext_start, ext);
189  if (ext_start == NULL) {
190  return SDL_FALSE;
191  }
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) {
195  return SDL_TRUE;
196  }
197  }
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) {
201  ext_start++;
202  }
203  }
204  }
205 
206  return SDL_FALSE;
207 }
208 
209 void *
210 SDL_EGL_GetProcAddress(_THIS, const char *proc)
211 {
212  static char procname[1024];
213  void *retval;
214 
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);
219  if (retval) {
220  return retval;
221  }
222  }
223 #endif
224 
225  retval = SDL_LoadFunction(_this->egl_data->egl_dll_handle, proc);
226  if (!retval && SDL_strlen(proc) <= 1022) {
227  procname[0] = '_';
228  SDL_strlcpy(procname + 1, proc, 1022);
229  retval = SDL_LoadFunction(_this->egl_data->egl_dll_handle, procname);
230  }
231  return retval;
232 }
233 
234 void
235 SDL_EGL_UnloadLibrary(_THIS)
236 {
237  if (_this->egl_data) {
238  if (_this->egl_data->egl_display) {
239  _this->egl_data->eglTerminate(_this->egl_data->egl_display);
240  _this->egl_data->egl_display = NULL;
241  }
242 
243  if (_this->egl_data->dll_handle) {
244  SDL_UnloadObject(_this->egl_data->dll_handle);
245  _this->egl_data->dll_handle = NULL;
246  }
247  if (_this->egl_data->egl_dll_handle) {
248  SDL_UnloadObject(_this->egl_data->egl_dll_handle);
249  _this->egl_data->egl_dll_handle = NULL;
250  }
251 
252  SDL_free(_this->egl_data);
253  _this->egl_data = NULL;
254  }
255 }
256 
257 int
258 SDL_EGL_LoadLibrary(_THIS, const char *egl_path, NativeDisplayType native_display, EGLenum platform)
259 {
260  void *dll_handle = NULL, *egl_dll_handle = NULL; /* The naming is counter intuitive, but hey, I just work here -- Gabriel */
261  const char *path = NULL;
262  int egl_version_major = 0, egl_version_minor = 0;
263 #if SDL_VIDEO_DRIVER_WINDOWS || SDL_VIDEO_DRIVER_WINRT
264  const char *d3dcompiler;
265 #endif
266 #if SDL_VIDEO_DRIVER_RPI
267  SDL_bool vc4 = (0 == access("/sys/module/vc4/", F_OK));
268 #endif
269 
270  if (_this->egl_data) {
271  return SDL_SetError("EGL context already created");
272  }
273 
274  _this->egl_data = (struct SDL_EGL_VideoData *) SDL_calloc(1, sizeof(SDL_EGL_VideoData));
275  if (!_this->egl_data) {
276  return SDL_OutOfMemory();
277  }
278 
279 #if SDL_VIDEO_DRIVER_WINDOWS || SDL_VIDEO_DRIVER_WINRT
281  if (!d3dcompiler) {
283  d3dcompiler = "d3dcompiler_46.dll";
284  } else {
285  d3dcompiler = "d3dcompiler_43.dll";
286  }
287  }
288  if (SDL_strcasecmp(d3dcompiler, "none") != 0) {
289  if (SDL_LoadObject(d3dcompiler) == NULL) {
290  SDL_ClearError();
291  }
292  }
293 #endif
294 
295 #ifndef SDL_VIDEO_STATIC_ANGLE
296  /* A funny thing, loading EGL.so first does not work on the Raspberry, so we load libGL* first */
297  path = SDL_getenv("SDL_VIDEO_GL_DRIVER");
298  if (path != NULL) {
299  egl_dll_handle = SDL_LoadObject(path);
300  }
301 
302  if (egl_dll_handle == NULL) {
304  if (_this->gl_config.major_version > 1) {
305  path = DEFAULT_OGL_ES2;
306  egl_dll_handle = SDL_LoadObject(path);
307 #ifdef ALT_OGL_ES2
308  if (egl_dll_handle == NULL && !vc4) {
309  path = ALT_OGL_ES2;
310  egl_dll_handle = SDL_LoadObject(path);
311  }
312 #endif
313 
314  } else {
315  path = DEFAULT_OGL_ES;
316  egl_dll_handle = SDL_LoadObject(path);
317  if (egl_dll_handle == NULL) {
318  path = DEFAULT_OGL_ES_PVR;
319  egl_dll_handle = SDL_LoadObject(path);
320  }
321 #ifdef ALT_OGL_ES2
322  if (egl_dll_handle == NULL && !vc4) {
323  path = ALT_OGL_ES2;
324  egl_dll_handle = SDL_LoadObject(path);
325  }
326 #endif
327  }
328  }
329 #ifdef DEFAULT_OGL
330  else {
331  path = DEFAULT_OGL;
332  egl_dll_handle = SDL_LoadObject(path);
333  }
334 #endif
335  }
336  _this->egl_data->egl_dll_handle = egl_dll_handle;
337 
338  if (egl_dll_handle == NULL) {
339  return SDL_SetError("Could not initialize OpenGL / GLES library");
340  }
341 
342  /* Loading libGL* in the previous step took care of loading libEGL.so, but we future proof by double checking */
343  if (egl_path != NULL) {
344  dll_handle = SDL_LoadObject(egl_path);
345  }
346  /* Try loading a EGL symbol, if it does not work try the default library paths */
347  if (dll_handle == NULL || SDL_LoadFunction(dll_handle, "eglChooseConfig") == NULL) {
348  if (dll_handle != NULL) {
349  SDL_UnloadObject(dll_handle);
350  }
351  path = SDL_getenv("SDL_VIDEO_EGL_DRIVER");
352  if (path == NULL) {
353  path = DEFAULT_EGL;
354  }
355  dll_handle = SDL_LoadObject(path);
356 
357 #ifdef ALT_EGL
358  if (dll_handle == NULL && !vc4) {
359  path = ALT_EGL;
360  dll_handle = SDL_LoadObject(path);
361  }
362 #endif
363 
364  if (dll_handle == NULL || SDL_LoadFunction(dll_handle, "eglChooseConfig") == NULL) {
365  if (dll_handle != NULL) {
366  SDL_UnloadObject(dll_handle);
367  }
368  return SDL_SetError("Could not load EGL library");
369  }
370  SDL_ClearError();
371  }
372 #endif
373 
374  _this->egl_data->dll_handle = dll_handle;
375 
376  /* Load new function pointers */
377  LOAD_FUNC(eglGetDisplay);
378  LOAD_FUNC(eglInitialize);
379  LOAD_FUNC(eglTerminate);
380  LOAD_FUNC(eglGetProcAddress);
381  LOAD_FUNC(eglChooseConfig);
382  LOAD_FUNC(eglGetConfigAttrib);
383  LOAD_FUNC(eglCreateContext);
384  LOAD_FUNC(eglDestroyContext);
385  LOAD_FUNC(eglCreatePbufferSurface);
386  LOAD_FUNC(eglCreateWindowSurface);
387  LOAD_FUNC(eglDestroySurface);
388  LOAD_FUNC(eglMakeCurrent);
389  LOAD_FUNC(eglSwapBuffers);
390  LOAD_FUNC(eglSwapInterval);
391  LOAD_FUNC(eglWaitNative);
392  LOAD_FUNC(eglWaitGL);
393  LOAD_FUNC(eglBindAPI);
394  LOAD_FUNC(eglQueryString);
395  LOAD_FUNC(eglGetError);
396 
397  if (_this->egl_data->eglQueryString) {
398  /* EGL 1.5 allows querying for client version */
399  const char *egl_version = _this->egl_data->eglQueryString(EGL_NO_DISPLAY, EGL_VERSION);
400  if (egl_version != NULL) {
401  if (SDL_sscanf(egl_version, "%d.%d", &egl_version_major, &egl_version_minor) != 2) {
402  egl_version_major = 0;
403  egl_version_minor = 0;
404  SDL_LogWarn(SDL_LOG_CATEGORY_VIDEO, "Could not parse EGL version string: %s", egl_version);
405  }
406  }
407  }
408 
409  if (egl_version_major == 1 && egl_version_minor == 5) {
410  LOAD_FUNC(eglGetPlatformDisplay);
411  }
412 
413  _this->egl_data->egl_display = EGL_NO_DISPLAY;
414 #if !defined(__WINRT__)
415  if (platform) {
416  if (egl_version_major == 1 && egl_version_minor == 5) {
417  _this->egl_data->egl_display = _this->egl_data->eglGetPlatformDisplay(platform, (void *)(size_t)native_display, NULL);
418  } else {
419  if (SDL_EGL_HasExtension(_this, SDL_EGL_CLIENT_EXTENSION, "EGL_EXT_platform_base")) {
420  _this->egl_data->eglGetPlatformDisplayEXT = SDL_EGL_GetProcAddress(_this, "eglGetPlatformDisplayEXT");
421  if (_this->egl_data->eglGetPlatformDisplayEXT) {
422  _this->egl_data->egl_display = _this->egl_data->eglGetPlatformDisplayEXT(platform, (void *)(size_t)native_display, NULL);
423  }
424  }
425  }
426  }
427  /* Try the implementation-specific eglGetDisplay even if eglGetPlatformDisplay fails */
428  if (_this->egl_data->egl_display == EGL_NO_DISPLAY) {
429  _this->egl_data->egl_display = _this->egl_data->eglGetDisplay(native_display);
430  }
431  if (_this->egl_data->egl_display == EGL_NO_DISPLAY) {
432  return SDL_SetError("Could not get EGL display");
433  }
434 
435  if (_this->egl_data->eglInitialize(_this->egl_data->egl_display, NULL, NULL) != EGL_TRUE) {
436  return SDL_SetError("Could not initialize EGL");
437  }
438 #endif
439 
440  if (path) {
442  } else {
443  *_this->gl_config.driver_path = '\0';
444  }
445 
446  return 0;
447 }
448 
449 int
450 SDL_EGL_ChooseConfig(_THIS)
451 {
452 /* 64 seems nice. */
453  EGLint attribs[64];
454  EGLint found_configs = 0, value;
455 #ifdef SDL_VIDEO_DRIVER_KMSDRM
456  /* Intel EGL on KMS/DRM (al least) returns invalid configs that confuse the bitdiff search used */
457  /* later in this function, so we simply use the first one when using the KMSDRM driver for now. */
458  EGLConfig configs[1];
459 #else
460  /* 128 seems even nicer here */
461  EGLConfig configs[128];
462 #endif
463  int i, j, best_bitdiff = -1, bitdiff;
464 
465  if (!_this->egl_data) {
466  /* The EGL library wasn't loaded, SDL_GetError() should have info */
467  return -1;
468  }
469 
470  /* Get a valid EGL configuration */
471  i = 0;
472  attribs[i++] = EGL_RED_SIZE;
473  attribs[i++] = _this->gl_config.red_size;
474  attribs[i++] = EGL_GREEN_SIZE;
475  attribs[i++] = _this->gl_config.green_size;
476  attribs[i++] = EGL_BLUE_SIZE;
477  attribs[i++] = _this->gl_config.blue_size;
478 
479  if (_this->gl_config.alpha_size) {
480  attribs[i++] = EGL_ALPHA_SIZE;
481  attribs[i++] = _this->gl_config.alpha_size;
482  }
483 
484  if (_this->gl_config.buffer_size) {
485  attribs[i++] = EGL_BUFFER_SIZE;
486  attribs[i++] = _this->gl_config.buffer_size;
487  }
488 
489  attribs[i++] = EGL_DEPTH_SIZE;
490  attribs[i++] = _this->gl_config.depth_size;
491 
493  attribs[i++] = EGL_STENCIL_SIZE;
494  attribs[i++] = _this->gl_config.stencil_size;
495  }
496 
498  attribs[i++] = EGL_SAMPLE_BUFFERS;
499  attribs[i++] = _this->gl_config.multisamplebuffers;
500  }
501 
503  attribs[i++] = EGL_SAMPLES;
504  attribs[i++] = _this->gl_config.multisamplesamples;
505  }
506 
507  attribs[i++] = EGL_RENDERABLE_TYPE;
509 #ifdef EGL_KHR_create_context
510  if (_this->gl_config.major_version >= 3 &&
511  SDL_EGL_HasExtension(_this, SDL_EGL_DISPLAY_EXTENSION, "EGL_KHR_create_context")) {
512  attribs[i++] = EGL_OPENGL_ES3_BIT_KHR;
513  } else
514 #endif
515  if (_this->gl_config.major_version >= 2) {
516  attribs[i++] = EGL_OPENGL_ES2_BIT;
517  } else {
518  attribs[i++] = EGL_OPENGL_ES_BIT;
519  }
520  _this->egl_data->eglBindAPI(EGL_OPENGL_ES_API);
521  } else {
522  attribs[i++] = EGL_OPENGL_BIT;
523  _this->egl_data->eglBindAPI(EGL_OPENGL_API);
524  }
525 
526  if (_this->egl_data->egl_surfacetype) {
527  attribs[i++] = EGL_SURFACE_TYPE;
528  attribs[i++] = _this->egl_data->egl_surfacetype;
529  }
530 
531  attribs[i++] = EGL_NONE;
532 
533  if (_this->egl_data->eglChooseConfig(_this->egl_data->egl_display,
534  attribs,
535  configs, SDL_arraysize(configs),
536  &found_configs) == EGL_FALSE ||
537  found_configs == 0) {
538  return SDL_EGL_SetError("Couldn't find matching EGL config", "eglChooseConfig");
539  }
540 
541  /* eglChooseConfig returns a number of configurations that match or exceed the requested attribs. */
542  /* From those, we select the one that matches our requirements more closely via a makeshift algorithm */
543 
544  for (i = 0; i < found_configs; i++ ) {
545  bitdiff = 0;
546  for (j = 0; j < SDL_arraysize(attribs) - 1; j += 2) {
547  if (attribs[j] == EGL_NONE) {
548  break;
549  }
550 
551  if ( attribs[j+1] != EGL_DONT_CARE && (
552  attribs[j] == EGL_RED_SIZE ||
553  attribs[j] == EGL_GREEN_SIZE ||
554  attribs[j] == EGL_BLUE_SIZE ||
555  attribs[j] == EGL_ALPHA_SIZE ||
556  attribs[j] == EGL_DEPTH_SIZE ||
557  attribs[j] == EGL_STENCIL_SIZE)) {
558  _this->egl_data->eglGetConfigAttrib(_this->egl_data->egl_display, configs[i], attribs[j], &value);
559  bitdiff += value - attribs[j + 1]; /* value is always >= attrib */
560  }
561  }
562 
563  if (bitdiff < best_bitdiff || best_bitdiff == -1) {
564  _this->egl_data->egl_config = configs[i];
565 
566  best_bitdiff = bitdiff;
567  }
568 
569  if (bitdiff == 0) {
570  break; /* we found an exact match! */
571  }
572  }
573 
574  return 0;
575 }
576 
578 SDL_EGL_CreateContext(_THIS, EGLSurface egl_surface)
579 {
580  /* max 14 values plus terminator. */
581  EGLint attribs[15];
582  int attr = 0;
583 
584  EGLContext egl_context, share_context = EGL_NO_CONTEXT;
585  EGLint profile_mask = _this->gl_config.profile_mask;
586  EGLint major_version = _this->gl_config.major_version;
587  EGLint minor_version = _this->gl_config.minor_version;
588  SDL_bool profile_es = (profile_mask == SDL_GL_CONTEXT_PROFILE_ES);
589 
590  if (!_this->egl_data) {
591  /* The EGL library wasn't loaded, SDL_GetError() should have info */
592  return NULL;
593  }
594 
596  share_context = (EGLContext)SDL_GL_GetCurrentContext();
597  }
598 
599  /* Set the context version and other attributes. */
600  if ((major_version < 3 || (minor_version == 0 && profile_es)) &&
601  _this->gl_config.flags == 0 &&
602  (profile_mask == 0 || profile_es)) {
603  /* Create a context without using EGL_KHR_create_context attribs.
604  * When creating a GLES context without EGL_KHR_create_context we can
605  * only specify the major version. When creating a desktop GL context
606  * we can't specify any version, so we only try in that case when the
607  * version is less than 3.0 (matches SDL's GLX/WGL behavior.)
608  */
609  if (profile_es) {
610  attribs[attr++] = EGL_CONTEXT_CLIENT_VERSION;
611  attribs[attr++] = SDL_max(major_version, 1);
612  }
613  } else {
614 #ifdef EGL_KHR_create_context
615  /* The Major/minor version, context profiles, and context flags can
616  * only be specified when this extension is available.
617  */
618  if (SDL_EGL_HasExtension(_this, SDL_EGL_DISPLAY_EXTENSION, "EGL_KHR_create_context")) {
619  attribs[attr++] = EGL_CONTEXT_MAJOR_VERSION_KHR;
620  attribs[attr++] = major_version;
621  attribs[attr++] = EGL_CONTEXT_MINOR_VERSION_KHR;
622  attribs[attr++] = minor_version;
623 
624  /* SDL profile bits match EGL profile bits. */
625  if (profile_mask != 0 && profile_mask != SDL_GL_CONTEXT_PROFILE_ES) {
626  attribs[attr++] = EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR;
627  attribs[attr++] = profile_mask;
628  }
629 
630  /* SDL flags match EGL flags. */
631  if (_this->gl_config.flags != 0) {
632  attribs[attr++] = EGL_CONTEXT_FLAGS_KHR;
633  attribs[attr++] = _this->gl_config.flags;
634  }
635  } else
636 #endif /* EGL_KHR_create_context */
637  {
638  SDL_SetError("Could not create EGL context (context attributes are not supported)");
639  return NULL;
640  }
641  }
642 
643  if (_this->gl_config.no_error) {
644 #ifdef EGL_KHR_create_context_no_error
645  if (SDL_EGL_HasExtension(_this, SDL_EGL_DISPLAY_EXTENSION, "EGL_KHR_create_context_no_error")) {
646  attribs[attr++] = EGL_CONTEXT_OPENGL_NO_ERROR_KHR;
647  attribs[attr++] = _this->gl_config.no_error;
648  } else
649 #endif
650  {
651  SDL_SetError("EGL implementation does not support no_error contexts");
652  return NULL;
653  }
654  }
655 
656  attribs[attr++] = EGL_NONE;
657 
658  /* Bind the API */
659  if (profile_es) {
660  _this->egl_data->eglBindAPI(EGL_OPENGL_ES_API);
661  } else {
662  _this->egl_data->eglBindAPI(EGL_OPENGL_API);
663  }
664 
665  egl_context = _this->egl_data->eglCreateContext(_this->egl_data->egl_display,
666  _this->egl_data->egl_config,
667  share_context, attribs);
668 
669  if (egl_context == EGL_NO_CONTEXT) {
670  SDL_EGL_SetError("Could not create EGL context", "eglCreateContext");
671  return NULL;
672  }
673 
674  _this->egl_data->egl_swapinterval = 0;
675 
676  if (SDL_EGL_MakeCurrent(_this, egl_surface, egl_context) < 0) {
677  /* Save the SDL error set by SDL_EGL_MakeCurrent */
678  char errorText[1024];
679  SDL_strlcpy(errorText, SDL_GetError(), SDL_arraysize(errorText));
680 
681  /* Delete the context, which may alter the value returned by SDL_GetError() */
682  SDL_EGL_DeleteContext(_this, egl_context);
683 
684  /* Restore the SDL error */
685  SDL_SetError("%s", errorText);
686 
687  return NULL;
688  }
689 
690  return (SDL_GLContext) egl_context;
691 }
692 
693 int
694 SDL_EGL_MakeCurrent(_THIS, EGLSurface egl_surface, SDL_GLContext context)
695 {
696  EGLContext egl_context = (EGLContext) context;
697 
698  if (!_this->egl_data) {
699  return SDL_SetError("OpenGL not initialized");
700  }
701 
702  /* The android emulator crashes badly if you try to eglMakeCurrent
703  * with a valid context and invalid surface, so we have to check for both here.
704  */
705  if (!egl_context || !egl_surface) {
706  _this->egl_data->eglMakeCurrent(_this->egl_data->egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
707  } else {
708  if (!_this->egl_data->eglMakeCurrent(_this->egl_data->egl_display,
709  egl_surface, egl_surface, egl_context)) {
710  return SDL_EGL_SetError("Unable to make EGL context current", "eglMakeCurrent");
711  }
712  }
713 
714  return 0;
715 }
716 
717 int
718 SDL_EGL_SetSwapInterval(_THIS, int interval)
719 {
720  EGLBoolean status;
721 
722  if (!_this->egl_data) {
723  return SDL_SetError("EGL not initialized");
724  }
725 
726  status = _this->egl_data->eglSwapInterval(_this->egl_data->egl_display, interval);
727  if (status == EGL_TRUE) {
728  _this->egl_data->egl_swapinterval = interval;
729  return 0;
730  }
731 
732  return SDL_EGL_SetError("Unable to set the EGL swap interval", "eglSwapInterval");
733 }
734 
735 int
736 SDL_EGL_GetSwapInterval(_THIS)
737 {
738  if (!_this->egl_data) {
739  SDL_SetError("EGL not initialized");
740  return 0;
741  }
742 
743  return _this->egl_data->egl_swapinterval;
744 }
745 
746 int
747 SDL_EGL_SwapBuffers(_THIS, EGLSurface egl_surface)
748 {
749  if (!_this->egl_data->eglSwapBuffers(_this->egl_data->egl_display, egl_surface)) {
750  return SDL_EGL_SetError("unable to show color buffer in an OS-native window", "eglSwapBuffers");
751  }
752  return 0;
753 }
754 
755 void
756 SDL_EGL_DeleteContext(_THIS, SDL_GLContext context)
757 {
758  EGLContext egl_context = (EGLContext) context;
759 
760  /* Clean up GLES and EGL */
761  if (!_this->egl_data) {
762  return;
763  }
764 
765  if (egl_context != NULL && egl_context != EGL_NO_CONTEXT) {
766  SDL_EGL_MakeCurrent(_this, NULL, NULL);
767  _this->egl_data->eglDestroyContext(_this->egl_data->egl_display, egl_context);
768  }
769 
770 }
771 
772 EGLSurface *
773 SDL_EGL_CreateSurface(_THIS, NativeWindowType nw)
774 {
775  /* max 2 values plus terminator. */
776  EGLint attribs[3];
777  int attr = 0;
778 
780 
781  if (SDL_EGL_ChooseConfig(_this) != 0) {
782  return EGL_NO_SURFACE;
783  }
784 
785 #if SDL_VIDEO_DRIVER_ANDROID
786  {
787  /* Android docs recommend doing this!
788  * Ref: http://developer.android.com/reference/android/app/NativeActivity.html
789  */
790  EGLint format;
791  _this->egl_data->eglGetConfigAttrib(_this->egl_data->egl_display,
792  _this->egl_data->egl_config,
793  EGL_NATIVE_VISUAL_ID, &format);
794 
795  ANativeWindow_setBuffersGeometry(nw, 0, 0, format);
796  }
797 #endif
799 #ifdef EGL_KHR_gl_colorspace
800  if (SDL_EGL_HasExtension(_this, SDL_EGL_DISPLAY_EXTENSION, "EGL_KHR_gl_colorspace")) {
801  attribs[attr++] = EGL_GL_COLORSPACE_KHR;
802  attribs[attr++] = EGL_GL_COLORSPACE_SRGB_KHR;
803  } else
804 #endif
805  {
806  SDL_SetError("EGL implementation does not support sRGB system framebuffers");
807  return EGL_NO_SURFACE;
808  }
809  }
810 
811  attribs[attr++] = EGL_NONE;
812 
813  surface = _this->egl_data->eglCreateWindowSurface(
814  _this->egl_data->egl_display,
815  _this->egl_data->egl_config,
816  nw, &attribs[0]);
817  if (surface == EGL_NO_SURFACE) {
818  SDL_EGL_SetError("unable to create an EGL window surface", "eglCreateWindowSurface");
819  }
820  return surface;
821 }
822 
823 void
824 SDL_EGL_DestroySurface(_THIS, EGLSurface egl_surface)
825 {
826  if (!_this->egl_data) {
827  return;
828  }
829 
830  if (egl_surface != EGL_NO_SURFACE) {
831  _this->egl_data->eglDestroySurface(_this->egl_data->egl_display, egl_surface);
832  }
833 }
834 
835 #endif /* SDL_VIDEO_OPENGL_EGL */
836 
837 /* vi: set ts=4 sw=4 expandtab: */
838 
#define EGL_BAD_PARAMETER
Definition: egl.h:73
#define SDL_strlcpy
#define EGL_CONTEXT_FLAGS_KHR
Definition: eglext.h:91
const GLint * attribs
#define SDL_ClearError
#define EGL_BAD_MATCH
Definition: egl.h:70
EGLAPI EGLBoolean EGLAPIENTRY eglTerminate(EGLDisplay dpy)
#define EGL_CONTEXT_LOST
Definition: egl.h:152
BOOL WIN_IsWindowsVistaOrGreater(void)
#define SDL_GetError
EGLAPI const char *EGLAPIENTRY eglQueryString(EGLDisplay dpy, EGLint name)
#define EGL_RED_SIZE
Definition: egl.h:104
void * EGLSurface
Definition: egl.h:59
EGLAPI EGLBoolean EGLAPIENTRY eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
EGLAPI EGLSurface EGLAPIENTRY eglCreateWindowSurface(EGLDisplay dpy, EGLConfig config, EGLNativeWindowType win, const EGLint *attrib_list)
void * EGLConfig
Definition: egl.h:58
EGLAPI EGLBoolean EGLAPIENTRY eglMakeCurrent(EGLDisplay dpy, EGLSurface draw, EGLSurface read, EGLContext ctx)
#define EGL_NO_SURFACE
Definition: egl.h:100
GLuint GLsizei const GLchar * message
#define EGL_SURFACE_TYPE
Definition: egl.h:110
EGLSurface surface
Definition: eglext.h:248
#define EGL_SAMPLE_BUFFERS
Definition: egl.h:106
#define EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR
Definition: eglext.h:92
#define EGL_BAD_CONTEXT
Definition: egl.h:67
#define EGL_ALPHA_SIZE
Definition: egl.h:62
static screen_context_t context
Definition: video.c:25
#define SDL_GetHint
#define EGL_NONE
Definition: egl.h:95
EGLAPI EGLint EGLAPIENTRY eglGetError(void)
EGLAPI EGLSurface EGLAPIENTRY eglCreatePbufferSurface(EGLDisplay dpy, EGLConfig config, const EGLint *attrib_list)
EGLAPI __eglMustCastToProperFunctionPointerType EGLAPIENTRY eglGetProcAddress(const char *procname)
#define SDL_strcasecmp
#define SDL_LoadObject
EGLAPI EGLBoolean EGLAPIENTRY eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config, EGLint attribute, EGLint *value)
EGLAPI EGLBoolean EGLAPIENTRY eglBindAPI(EGLenum api)
#define SDL_UnloadObject
#define EGL_OPENGL_ES_API
Definition: egl.h:191
unsigned int EGLenum
Definition: egl.h:171
#define SDL_max(x, y)
Definition: SDL_stdinc.h:407
#define EGL_TRUE
Definition: egl.h:116
#define EGL_NO_DISPLAY
Definition: egl.h:99
GLuint GLint GLboolean GLint GLenum access
#define EGL_BAD_CURRENT_SURFACE
Definition: egl.h:68
#define EGL_BLUE_SIZE
Definition: egl.h:75
unsigned int EGLBoolean
Definition: egl.h:54
#define EGL_NATIVE_VISUAL_ID
Definition: egl.h:93
#define SDL_strchr
EGLAPI EGLBoolean EGLAPIENTRY eglChooseConfig(EGLDisplay dpy, const EGLint *attrib_list, EGLConfig *configs, EGLint config_size, EGLint *num_config)
static SDL_VideoDevice * _this
Definition: SDL_video.c:121
#define SDL_HINT_VIDEO_WIN_D3DCOMPILER
A variable specifying which shader compiler to preload when using the Chrome ANGLE binaries...
Definition: SDL_hints.h:560
EGLNativeWindowType NativeWindowType
Definition: eglplatform.h:112
EGLAPI EGLBoolean EGLAPIENTRY eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
#define EGL_SUCCESS
Definition: egl.h:109
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: SDL_opengl.h:1572
void * EGLContext
Definition: egl.h:60
#define EGL_GL_COLORSPACE_SRGB_KHR
Definition: eglext.h:172
SDL_bool retval
#define EGL_OPENGL_API
Definition: egl.h:232
#define EGL_BUFFER_SIZE
Definition: egl.h:76
void * SDL_GLContext
An opaque handle to an OpenGL context.
Definition: SDL_video.h:175
#define EGL_NO_CONTEXT
Definition: egl.h:98
#define EGL_OPENGL_ES_BIT
Definition: egl.h:189
#define EGL_BAD_SURFACE
Definition: egl.h:74
void * native_display
Definition: eglext.h:789
#define EGL_CONTEXT_CLIENT_VERSION
Definition: egl.h:212
#define _THIS
#define SDL_free
#define EGL_OPENGL_ES3_BIT_KHR
Definition: eglext.h:101
#define EGL_CONTEXT_MAJOR_VERSION_KHR
Definition: eglext.h:89
#define EGL_CONTEXT_OPENGL_NO_ERROR_KHR
Definition: eglext.h:106
khronos_int32_t EGLint
Definition: eglplatform.h:122
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
char driver_path[256]
Definition: SDL_sysvideo.h:350
#define EGL_CONTEXT_MINOR_VERSION_KHR
Definition: eglext.h:90
#define EGL_BAD_ACCESS
Definition: egl.h:63
GLsizei const GLfloat * value
#define EGL_DONT_CARE
Definition: egl.h:81
#define SDL_sscanf
int framebuffer_srgb_capable
Definition: SDL_sysvideo.h:346
#define EGL_DEPTH_SIZE
Definition: egl.h:80
#define SDL_atoi
#define EGL_FALSE
Definition: egl.h:84
#define SDL_getenv
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
int share_with_current_context
Definition: SDL_sysvideo.h:343
#define EGL_SAMPLES
Definition: egl.h:105
#define NULL
Definition: begin_code.h:164
#define SDL_OutOfMemory()
Definition: SDL_error.h:52
SDL_bool
Definition: SDL_stdinc.h:139
#define SDL_GL_GetCurrentContext
EGLAPI EGLDisplay EGLAPIENTRY eglGetDisplay(EGLNativeDisplayType display_id)
#define SDL_SetError
EGLAPI EGLBoolean EGLAPIENTRY eglSwapBuffers(EGLDisplay dpy, EGLSurface surface)
#define SDL_calloc
#define SDL_strlen
EGLAPI EGLBoolean EGLAPIENTRY eglWaitGL(void)
#define EGL_BAD_ALLOC
Definition: egl.h:64
#define EGL_GREEN_SIZE
Definition: egl.h:85
EGLAPI EGLBoolean EGLAPIENTRY eglSwapInterval(EGLDisplay dpy, EGLint interval)
#define EGL_STENCIL_SIZE
Definition: egl.h:108
#define EGL_BAD_ATTRIBUTE
Definition: egl.h:65
GLuint GLuint GLsizei GLenum type
Definition: SDL_opengl.h:1571
#define EGL_OPENGL_BIT
Definition: egl.h:233
#define SDL_snprintf
#define EGL_VERSION
Definition: egl.h:118
#define EGL_BAD_DISPLAY
Definition: egl.h:69
#define SDL_arraysize(array)
Definition: SDL_stdinc.h:93
EGLNativeDisplayType NativeDisplayType
Definition: eglplatform.h:110
EGLAPI EGLBoolean EGLAPIENTRY eglWaitNative(EGLint engine)
GLsizei const GLchar *const * path
struct SDL_VideoDevice::@34 gl_config
void * SDL_LoadFunction(void *handle, const char *name)
#define EGL_BAD_NATIVE_WINDOW
Definition: egl.h:72
EGLAPI EGLDisplay EGLAPIENTRY eglGetPlatformDisplay(EGLenum platform, void *native_display, const EGLAttrib *attrib_list)
#define SDL_LogWarn
EGLAPI EGLContext EGLAPIENTRY eglCreateContext(EGLDisplay dpy, EGLConfig config, EGLContext share_context, const EGLint *attrib_list)
#define EGL_BAD_NATIVE_PIXMAP
Definition: egl.h:71
#define EGL_NOT_INITIALIZED
Definition: egl.h:97
#define EGL_GL_COLORSPACE_KHR
Definition: eglext.h:171
#define EGL_RENDERABLE_TYPE
Definition: egl.h:195
#define EGL_BAD_CONFIG
Definition: egl.h:66
#define SDL_strstr
#define EGL_OPENGL_ES2_BIT
Definition: egl.h:214
EGLAPI EGLBoolean EGLAPIENTRY eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
#define EGL_EXTENSIONS
Definition: egl.h:83