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