1 | /* |
2 | * Simple DirectMedia Layer |
3 | * Copyright (C) 1997-2025 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 | #ifdef SDL_VIDEO_OPENGL_EGL |
24 | |
25 | #if defined(SDL_VIDEO_DRIVER_WINDOWS) |
26 | #include "../core/windows/SDL_windows.h" |
27 | #endif |
28 | #ifdef SDL_VIDEO_DRIVER_ANDROID |
29 | #include <android/native_window.h> |
30 | #include "../video/android/SDL_androidvideo.h" |
31 | #endif |
32 | #ifdef SDL_VIDEO_DRIVER_RPI |
33 | #include <unistd.h> |
34 | #endif |
35 | #ifdef SDL_VIDEO_VITA_PVR_OGL |
36 | #include <GLES2/gl2.h> |
37 | #endif |
38 | |
39 | #include "SDL_sysvideo.h" |
40 | #include "SDL_egl_c.h" |
41 | |
42 | #ifdef EGL_KHR_create_context |
43 | // EGL_OPENGL_ES3_BIT_KHR was added in version 13 of the extension. |
44 | #ifndef EGL_OPENGL_ES3_BIT_KHR |
45 | #define EGL_OPENGL_ES3_BIT_KHR 0x00000040 |
46 | #endif |
47 | #endif // EGL_KHR_create_context |
48 | |
49 | #ifndef EGL_EXT_pixel_format_float |
50 | #define EGL_EXT_pixel_format_float |
51 | #define EGL_COLOR_COMPONENT_TYPE_EXT 0x3339 |
52 | #define EGL_COLOR_COMPONENT_TYPE_FIXED_EXT 0x333A |
53 | #define EGL_COLOR_COMPONENT_TYPE_FLOAT_EXT 0x333B |
54 | #endif |
55 | |
56 | #ifndef EGL_EXT_present_opaque |
57 | #define EGL_EXT_present_opaque 1 |
58 | #define EGL_PRESENT_OPAQUE_EXT 0x31DF |
59 | #endif // EGL_EXT_present_opaque |
60 | |
61 | #ifdef SDL_VIDEO_DRIVER_RPI |
62 | // Raspbian places the OpenGL ES/EGL binaries in a non standard path |
63 | #define DEFAULT_EGL (vc4 ? "libEGL.so.1" : "libbrcmEGL.so") |
64 | #define DEFAULT_OGL_ES2 (vc4 ? "libGLESv2.so.2" : "libbrcmGLESv2.so") |
65 | #define ALT_EGL "libEGL.so" |
66 | #define ALT_OGL_ES2 "libGLESv2.so" |
67 | #define DEFAULT_OGL_ES_PVR (vc4 ? "libGLES_CM.so.1" : "libbrcmGLESv2.so") |
68 | #define DEFAULT_OGL_ES (vc4 ? "libGLESv1_CM.so.1" : "libbrcmGLESv2.so") |
69 | |
70 | #elif defined(SDL_VIDEO_DRIVER_ANDROID) || defined(SDL_VIDEO_DRIVER_VIVANTE) |
71 | // Android |
72 | #define DEFAULT_EGL "libEGL.so" |
73 | #define DEFAULT_OGL_ES2 "libGLESv2.so" |
74 | #define DEFAULT_OGL_ES_PVR "libGLES_CM.so" |
75 | #define DEFAULT_OGL_ES "libGLESv1_CM.so" |
76 | |
77 | #elif defined(SDL_VIDEO_DRIVER_WINDOWS) |
78 | // EGL AND OpenGL ES support via ANGLE |
79 | #define DEFAULT_EGL "libEGL.dll" |
80 | #define DEFAULT_OGL "opengl32.dll" |
81 | #define DEFAULT_OGL_ES2 "libGLESv2.dll" |
82 | #define DEFAULT_OGL_ES_PVR "libGLES_CM.dll" |
83 | #define DEFAULT_OGL_ES "libGLESv1_CM.dll" |
84 | |
85 | #elif defined(SDL_VIDEO_DRIVER_COCOA) |
86 | // EGL AND OpenGL ES support via ANGLE |
87 | #define DEFAULT_EGL "libEGL.dylib" |
88 | #define DEFAULT_OGL_ES2 "libGLESv2.dylib" |
89 | #define DEFAULT_OGL_ES_PVR "libGLES_CM.dylib" //??? |
90 | #define DEFAULT_OGL_ES "libGLESv1_CM.dylib" //??? |
91 | |
92 | #elif defined(SDL_PLATFORM_OPENBSD) |
93 | // OpenBSD |
94 | #define DEFAULT_OGL "libGL.so" |
95 | #define DEFAULT_EGL "libEGL.so" |
96 | #define DEFAULT_OGL_ES2 "libGLESv2.so" |
97 | #define DEFAULT_OGL_ES_PVR "libGLES_CM.so" |
98 | #define DEFAULT_OGL_ES "libGLESv1_CM.so" |
99 | |
100 | #else |
101 | // Desktop Linux/Unix-like |
102 | #define DEFAULT_OGL "libGL.so.1" |
103 | #define DEFAULT_EGL "libEGL.so.1" |
104 | #define ALT_OGL "libOpenGL.so.0" |
105 | #define DEFAULT_OGL_ES2 "libGLESv2.so.2" |
106 | #define DEFAULT_OGL_ES_PVR "libGLES_CM.so.1" |
107 | #define DEFAULT_OGL_ES "libGLESv1_CM.so.1" |
108 | #endif // SDL_VIDEO_DRIVER_RPI |
109 | |
110 | #if defined(SDL_VIDEO_OPENGL) && !defined(SDL_VIDEO_VITA_PVR_OGL) |
111 | #include <SDL3/SDL_opengl.h> |
112 | #endif |
113 | |
114 | /** If we happen to not have this defined because of an older EGL version, just define it 0x0 |
115 | as eglGetPlatformDisplayEXT will most likely be NULL if this is missing |
116 | */ |
117 | #ifndef EGL_PLATFORM_DEVICE_EXT |
118 | #define EGL_PLATFORM_DEVICE_EXT 0x0 |
119 | #endif |
120 | |
121 | #ifdef SDL_VIDEO_OPENGL |
122 | typedef void (APIENTRY* PFNGLGETINTEGERVPROC) (GLenum pname, GLint * params); |
123 | #endif |
124 | |
125 | #if defined(SDL_VIDEO_STATIC_ANGLE) || defined(SDL_VIDEO_DRIVER_VITA) |
126 | #define LOAD_FUNC(TYPE, NAME) \ |
127 | _this->egl_data->NAME = NAME; |
128 | #else |
129 | #define LOAD_FUNC(TYPE, NAME) \ |
130 | _this->egl_data->NAME = (TYPE)SDL_LoadFunction(_this->egl_data->egl_dll_handle, #NAME); \ |
131 | if (!_this->egl_data->NAME) { \ |
132 | return SDL_SetError("Could not retrieve EGL function " #NAME); \ |
133 | } |
134 | #endif |
135 | |
136 | // it is allowed to not have some of the EGL extensions on start - attempts to use them will fail later. |
137 | #define LOAD_FUNC_EGLEXT(TYPE, NAME) \ |
138 | _this->egl_data->NAME = (TYPE)_this->egl_data->eglGetProcAddress(#NAME); |
139 | |
140 | static const char *SDL_EGL_GetErrorName(EGLint eglErrorCode) |
141 | { |
142 | #define SDL_EGL_ERROR_TRANSLATE(e) \ |
143 | case e: \ |
144 | return #e; |
145 | switch (eglErrorCode) { |
146 | SDL_EGL_ERROR_TRANSLATE(EGL_SUCCESS); |
147 | SDL_EGL_ERROR_TRANSLATE(EGL_NOT_INITIALIZED); |
148 | SDL_EGL_ERROR_TRANSLATE(EGL_BAD_ACCESS); |
149 | SDL_EGL_ERROR_TRANSLATE(EGL_BAD_ALLOC); |
150 | SDL_EGL_ERROR_TRANSLATE(EGL_BAD_ATTRIBUTE); |
151 | SDL_EGL_ERROR_TRANSLATE(EGL_BAD_CONTEXT); |
152 | SDL_EGL_ERROR_TRANSLATE(EGL_BAD_CONFIG); |
153 | SDL_EGL_ERROR_TRANSLATE(EGL_BAD_CURRENT_SURFACE); |
154 | SDL_EGL_ERROR_TRANSLATE(EGL_BAD_DISPLAY); |
155 | SDL_EGL_ERROR_TRANSLATE(EGL_BAD_SURFACE); |
156 | SDL_EGL_ERROR_TRANSLATE(EGL_BAD_MATCH); |
157 | SDL_EGL_ERROR_TRANSLATE(EGL_BAD_PARAMETER); |
158 | SDL_EGL_ERROR_TRANSLATE(EGL_BAD_NATIVE_PIXMAP); |
159 | SDL_EGL_ERROR_TRANSLATE(EGL_BAD_NATIVE_WINDOW); |
160 | SDL_EGL_ERROR_TRANSLATE(EGL_CONTEXT_LOST); |
161 | } |
162 | return "" ; |
163 | } |
164 | |
165 | bool SDL_EGL_SetErrorEx(const char *message, const char *eglFunctionName, EGLint eglErrorCode) |
166 | { |
167 | const char *errorText = SDL_EGL_GetErrorName(eglErrorCode); |
168 | char altErrorText[32]; |
169 | if (errorText[0] == '\0') { |
170 | // An unknown-to-SDL error code was reported. Report its hexadecimal value, instead of its name. |
171 | (void)SDL_snprintf(altErrorText, SDL_arraysize(altErrorText), "0x%x" , (unsigned int)eglErrorCode); |
172 | errorText = altErrorText; |
173 | } |
174 | return SDL_SetError("%s (call to %s failed, reporting an error of %s)" , message, eglFunctionName, errorText); |
175 | } |
176 | |
177 | // EGL implementation of SDL OpenGL ES support |
178 | |
179 | bool SDL_EGL_HasExtension(SDL_VideoDevice *_this, SDL_EGL_ExtensionType type, const char *ext) |
180 | { |
181 | size_t ext_len; |
182 | const char *ext_override; |
183 | const char *egl_extstr; |
184 | const char *ext_start; |
185 | |
186 | // Invalid extensions can be rejected early |
187 | if (!ext || *ext == 0 || SDL_strchr(ext, ' ') != NULL) { |
188 | // SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "SDL_EGL_HasExtension: Invalid EGL extension"); |
189 | return false; |
190 | } |
191 | |
192 | /* Extensions can be masked with a hint or environment variable. |
193 | * Unlike the OpenGL override, this will use the set bits of an integer |
194 | * to disable the extension. |
195 | * Bit Action |
196 | * 0 If set, the display extension is masked and not present to SDL. |
197 | * 1 If set, the client extension is masked and not present to SDL. |
198 | */ |
199 | ext_override = SDL_GetHint(ext); |
200 | if (ext_override) { |
201 | int disable_ext = SDL_atoi(ext_override); |
202 | if (disable_ext & 0x01 && type == SDL_EGL_DISPLAY_EXTENSION) { |
203 | return false; |
204 | } else if (disable_ext & 0x02 && type == SDL_EGL_CLIENT_EXTENSION) { |
205 | return false; |
206 | } |
207 | } |
208 | |
209 | ext_len = SDL_strlen(ext); |
210 | switch (type) { |
211 | case SDL_EGL_DISPLAY_EXTENSION: |
212 | egl_extstr = _this->egl_data->eglQueryString(_this->egl_data->egl_display, EGL_EXTENSIONS); |
213 | break; |
214 | case SDL_EGL_CLIENT_EXTENSION: |
215 | /* EGL_EXT_client_extensions modifies eglQueryString to return client extensions |
216 | * if EGL_NO_DISPLAY is passed. Implementations without it are required to return NULL. |
217 | * This behavior is included in EGL 1.5. |
218 | */ |
219 | egl_extstr = _this->egl_data->eglQueryString(EGL_NO_DISPLAY, EGL_EXTENSIONS); |
220 | break; |
221 | default: |
222 | // SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "SDL_EGL_HasExtension: Invalid extension type"); |
223 | return false; |
224 | } |
225 | |
226 | if (egl_extstr) { |
227 | ext_start = egl_extstr; |
228 | |
229 | while (*ext_start) { |
230 | ext_start = SDL_strstr(ext_start, ext); |
231 | if (!ext_start) { |
232 | return false; |
233 | } |
234 | // Check if the match is not just a substring of one of the extensions |
235 | if (ext_start == egl_extstr || *(ext_start - 1) == ' ') { |
236 | if (ext_start[ext_len] == ' ' || ext_start[ext_len] == 0) { |
237 | return true; |
238 | } |
239 | } |
240 | // If the search stopped in the middle of an extension, skip to the end of it |
241 | ext_start += ext_len; |
242 | while (*ext_start != ' ' && *ext_start != 0) { |
243 | ext_start++; |
244 | } |
245 | } |
246 | } |
247 | |
248 | return false; |
249 | } |
250 | |
251 | SDL_FunctionPointer SDL_EGL_GetProcAddressInternal(SDL_VideoDevice *_this, const char *proc) |
252 | { |
253 | SDL_FunctionPointer result = NULL; |
254 | if (_this->egl_data) { |
255 | const Uint32 eglver = (((Uint32)_this->egl_data->egl_version_major) << 16) | ((Uint32)_this->egl_data->egl_version_minor); |
256 | const bool is_egl_15_or_later = eglver >= ((((Uint32)1) << 16) | 5); |
257 | |
258 | // EGL 1.5 can use eglGetProcAddress() for any symbol. 1.4 and earlier can't use it for core entry points. |
259 | if (!result && is_egl_15_or_later && _this->egl_data->eglGetProcAddress) { |
260 | result = _this->egl_data->eglGetProcAddress(proc); |
261 | } |
262 | |
263 | #if !defined(SDL_PLATFORM_EMSCRIPTEN) && !defined(SDL_VIDEO_DRIVER_VITA) // LoadFunction isn't needed on Emscripten and will call dlsym(), causing other problems. |
264 | // Try SDL_LoadFunction() first for EGL <= 1.4, or as a fallback for >= 1.5. |
265 | if (!result) { |
266 | result = SDL_LoadFunction(_this->egl_data->opengl_dll_handle, proc); |
267 | } |
268 | #endif |
269 | |
270 | // Try eglGetProcAddress if we're on <= 1.4 and still searching... |
271 | if (!result && !is_egl_15_or_later && _this->egl_data->eglGetProcAddress) { |
272 | result = _this->egl_data->eglGetProcAddress(proc); |
273 | } |
274 | } |
275 | return result; |
276 | } |
277 | |
278 | void SDL_EGL_UnloadLibrary(SDL_VideoDevice *_this) |
279 | { |
280 | if (_this->egl_data) { |
281 | if (_this->egl_data->egl_display) { |
282 | _this->egl_data->eglTerminate(_this->egl_data->egl_display); |
283 | _this->egl_data->egl_display = NULL; |
284 | } |
285 | |
286 | if (_this->egl_data->egl_dll_handle) { |
287 | SDL_UnloadObject(_this->egl_data->egl_dll_handle); |
288 | _this->egl_data->egl_dll_handle = NULL; |
289 | } |
290 | if (_this->egl_data->opengl_dll_handle) { |
291 | SDL_UnloadObject(_this->egl_data->opengl_dll_handle); |
292 | _this->egl_data->opengl_dll_handle = NULL; |
293 | } |
294 | |
295 | SDL_free(_this->egl_data); |
296 | _this->egl_data = NULL; |
297 | } |
298 | } |
299 | |
300 | static bool SDL_EGL_LoadLibraryInternal(SDL_VideoDevice *_this, const char *egl_path) |
301 | { |
302 | SDL_SharedObject *egl_dll_handle = NULL; |
303 | #if !defined(SDL_VIDEO_STATIC_ANGLE) && !defined(SDL_VIDEO_DRIVER_VITA) |
304 | SDL_SharedObject *opengl_dll_handle = NULL; |
305 | #endif |
306 | const char *path = NULL; |
307 | #if defined(SDL_VIDEO_DRIVER_WINDOWS) |
308 | const char *d3dcompiler; |
309 | #endif |
310 | #ifdef SDL_VIDEO_DRIVER_RPI |
311 | bool vc4 = (0 == access("/sys/module/vc4/" , F_OK)); |
312 | #endif |
313 | |
314 | #if defined(SDL_VIDEO_DRIVER_WINDOWS) |
315 | d3dcompiler = SDL_GetHint(SDL_HINT_VIDEO_WIN_D3DCOMPILER); |
316 | if (d3dcompiler) { |
317 | if (SDL_strcasecmp(d3dcompiler, "none" ) != 0) { |
318 | if (SDL_LoadObject(d3dcompiler) == NULL) { |
319 | SDL_ClearError(); |
320 | } |
321 | } |
322 | } else { |
323 | if (WIN_IsWindowsVistaOrGreater()) { |
324 | // Try the newer d3d compilers first |
325 | const char *d3dcompiler_list[] = { |
326 | "d3dcompiler_47.dll" , |
327 | "d3dcompiler_46.dll" , |
328 | }; |
329 | int i; |
330 | |
331 | for (i = 0; i < SDL_arraysize(d3dcompiler_list); ++i) { |
332 | if (SDL_LoadObject(d3dcompiler_list[i]) != NULL) { |
333 | break; |
334 | } |
335 | SDL_ClearError(); |
336 | } |
337 | } else { |
338 | if (SDL_LoadObject("d3dcompiler_43.dll" ) == NULL) { |
339 | SDL_ClearError(); |
340 | } |
341 | } |
342 | } |
343 | #endif |
344 | |
345 | #if !defined(SDL_VIDEO_STATIC_ANGLE) && !defined(SDL_VIDEO_DRIVER_VITA) |
346 | /* A funny thing, loading EGL.so first does not work on the Raspberry, so we load libGL* first */ |
347 | path = SDL_GetHint(SDL_HINT_OPENGL_LIBRARY); |
348 | if (path) { |
349 | opengl_dll_handle = SDL_LoadObject(path); |
350 | } |
351 | |
352 | if (!opengl_dll_handle) { |
353 | if (_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES) { |
354 | if (_this->gl_config.major_version > 1) { |
355 | path = DEFAULT_OGL_ES2; |
356 | opengl_dll_handle = SDL_LoadObject(path); |
357 | #ifdef ALT_OGL_ES2 |
358 | if (!opengl_dll_handle && !vc4) { |
359 | path = ALT_OGL_ES2; |
360 | opengl_dll_handle = SDL_LoadObject(path); |
361 | } |
362 | #endif |
363 | |
364 | } else { |
365 | path = DEFAULT_OGL_ES; |
366 | opengl_dll_handle = SDL_LoadObject(path); |
367 | if (!opengl_dll_handle) { |
368 | path = DEFAULT_OGL_ES_PVR; |
369 | opengl_dll_handle = SDL_LoadObject(path); |
370 | } |
371 | #ifdef ALT_OGL_ES2 |
372 | if (!opengl_dll_handle && !vc4) { |
373 | path = ALT_OGL_ES2; |
374 | opengl_dll_handle = SDL_LoadObject(path); |
375 | } |
376 | #endif |
377 | } |
378 | } |
379 | #ifdef DEFAULT_OGL |
380 | else { |
381 | path = DEFAULT_OGL; |
382 | opengl_dll_handle = SDL_LoadObject(path); |
383 | #ifdef ALT_OGL |
384 | if (!opengl_dll_handle) { |
385 | path = ALT_OGL; |
386 | opengl_dll_handle = SDL_LoadObject(path); |
387 | } |
388 | #endif |
389 | } |
390 | #endif |
391 | } |
392 | _this->egl_data->opengl_dll_handle = opengl_dll_handle; |
393 | |
394 | if (!opengl_dll_handle) { |
395 | return SDL_SetError("Could not initialize OpenGL / GLES library" ); |
396 | } |
397 | |
398 | /* Loading libGL* in the previous step took care of loading libEGL.so, but we future proof by double checking */ |
399 | if (egl_path) { |
400 | egl_dll_handle = SDL_LoadObject(egl_path); |
401 | } |
402 | // Try loading a EGL symbol, if it does not work try the default library paths |
403 | if (!egl_dll_handle || SDL_LoadFunction(egl_dll_handle, "eglChooseConfig" ) == NULL) { |
404 | if (egl_dll_handle) { |
405 | SDL_UnloadObject(egl_dll_handle); |
406 | } |
407 | path = SDL_GetHint(SDL_HINT_EGL_LIBRARY); |
408 | if (!path) { |
409 | path = DEFAULT_EGL; |
410 | } |
411 | egl_dll_handle = SDL_LoadObject(path); |
412 | |
413 | #ifdef ALT_EGL |
414 | if (!egl_dll_handle && !vc4) { |
415 | path = ALT_EGL; |
416 | egl_dll_handle = SDL_LoadObject(path); |
417 | } |
418 | #endif |
419 | |
420 | if (!egl_dll_handle || SDL_LoadFunction(egl_dll_handle, "eglChooseConfig" ) == NULL) { |
421 | if (egl_dll_handle) { |
422 | SDL_UnloadObject(egl_dll_handle); |
423 | } |
424 | return SDL_SetError("Could not load EGL library" ); |
425 | } |
426 | SDL_ClearError(); |
427 | } |
428 | #endif |
429 | |
430 | _this->egl_data->egl_dll_handle = egl_dll_handle; |
431 | |
432 | // Load new function pointers |
433 | LOAD_FUNC(PFNEGLGETDISPLAYPROC, eglGetDisplay); |
434 | LOAD_FUNC(PFNEGLINITIALIZEPROC, eglInitialize); |
435 | LOAD_FUNC(PFNEGLTERMINATEPROC, eglTerminate); |
436 | LOAD_FUNC(PFNEGLGETPROCADDRESSPROC, eglGetProcAddress); |
437 | LOAD_FUNC(PFNEGLCHOOSECONFIGPROC, eglChooseConfig); |
438 | LOAD_FUNC(PFNEGLCREATECONTEXTPROC, eglCreateContext); |
439 | LOAD_FUNC(PFNEGLDESTROYCONTEXTPROC, eglDestroyContext); |
440 | LOAD_FUNC(PFNEGLCREATEPBUFFERSURFACEPROC, eglCreatePbufferSurface); |
441 | LOAD_FUNC(PFNEGLCREATEWINDOWSURFACEPROC, eglCreateWindowSurface); |
442 | LOAD_FUNC(PFNEGLDESTROYSURFACEPROC, eglDestroySurface); |
443 | LOAD_FUNC(PFNEGLMAKECURRENTPROC, eglMakeCurrent); |
444 | LOAD_FUNC(PFNEGLSWAPBUFFERSPROC, eglSwapBuffers); |
445 | LOAD_FUNC(PFNEGLSWAPINTERVALPROC, eglSwapInterval); |
446 | LOAD_FUNC(PFNEGLQUERYSTRINGPROC, eglQueryString); |
447 | LOAD_FUNC(PFNEGLGETCONFIGATTRIBPROC, eglGetConfigAttrib); |
448 | LOAD_FUNC(PFNEGLWAITNATIVEPROC, eglWaitNative); |
449 | LOAD_FUNC(PFNEGLWAITGLPROC, eglWaitGL); |
450 | LOAD_FUNC(PFNEGLBINDAPIPROC, eglBindAPI); |
451 | LOAD_FUNC(PFNEGLGETERRORPROC, eglGetError); |
452 | LOAD_FUNC_EGLEXT(PFNEGLQUERYDEVICESEXTPROC, eglQueryDevicesEXT); |
453 | LOAD_FUNC_EGLEXT(PFNEGLGETPLATFORMDISPLAYEXTPROC, eglGetPlatformDisplayEXT); |
454 | // Atomic functions |
455 | LOAD_FUNC_EGLEXT(PFNEGLCREATESYNCKHRPROC, eglCreateSyncKHR); |
456 | LOAD_FUNC_EGLEXT(PFNEGLDESTROYSYNCKHRPROC, eglDestroySyncKHR); |
457 | LOAD_FUNC_EGLEXT(PFNEGLDUPNATIVEFENCEFDANDROIDPROC, eglDupNativeFenceFDANDROID); |
458 | LOAD_FUNC_EGLEXT(PFNEGLWAITSYNCKHRPROC, eglWaitSyncKHR); |
459 | LOAD_FUNC_EGLEXT(PFNEGLCLIENTWAITSYNCKHRPROC, eglClientWaitSyncKHR); |
460 | // Atomic functions end |
461 | |
462 | if (path) { |
463 | SDL_strlcpy(_this->gl_config.driver_path, path, sizeof(_this->gl_config.driver_path) - 1); |
464 | } else { |
465 | *_this->gl_config.driver_path = '\0'; |
466 | } |
467 | |
468 | return true; |
469 | } |
470 | |
471 | bool SDL_EGL_LoadLibraryOnly(SDL_VideoDevice *_this, const char *egl_path) |
472 | { |
473 | if (_this->egl_data) { |
474 | return SDL_SetError("EGL context already created" ); |
475 | } |
476 | |
477 | _this->egl_data = (struct SDL_EGL_VideoData *)SDL_calloc(1, sizeof(SDL_EGL_VideoData)); |
478 | if (!_this->egl_data) { |
479 | return false; |
480 | } |
481 | |
482 | if (!SDL_EGL_LoadLibraryInternal(_this, egl_path)) { |
483 | SDL_free(_this->egl_data); |
484 | _this->egl_data = NULL; |
485 | return false; |
486 | } |
487 | return true; |
488 | } |
489 | |
490 | static void SDL_EGL_GetVersion(SDL_VideoDevice *_this) |
491 | { |
492 | if (_this->egl_data->eglQueryString) { |
493 | const char *egl_version = _this->egl_data->eglQueryString(_this->egl_data->egl_display, EGL_VERSION); |
494 | if (egl_version) { |
495 | int major = 0, minor = 0; |
496 | if (SDL_sscanf(egl_version, "%d.%d" , &major, &minor) == 2) { |
497 | _this->egl_data->egl_version_major = major; |
498 | _this->egl_data->egl_version_minor = minor; |
499 | } else { |
500 | SDL_LogWarn(SDL_LOG_CATEGORY_VIDEO, "Could not parse EGL version string: %s" , egl_version); |
501 | } |
502 | } |
503 | } |
504 | } |
505 | |
506 | bool SDL_EGL_LoadLibrary(SDL_VideoDevice *_this, const char *egl_path, NativeDisplayType native_display, EGLenum platform) |
507 | { |
508 | if (!SDL_EGL_LoadLibraryOnly(_this, egl_path)) { |
509 | return false; |
510 | } |
511 | |
512 | _this->egl_data->egl_display = EGL_NO_DISPLAY; |
513 | |
514 | #ifndef SDL_VIDEO_DRIVER_VITA |
515 | if (platform) { |
516 | /* EGL 1.5 allows querying for client version with EGL_NO_DISPLAY |
517 | * -- |
518 | * Khronos doc: "EGL_BAD_DISPLAY is generated if display is not an EGL display connection, unless display is EGL_NO_DISPLAY and name is EGL_EXTENSIONS." |
519 | * Therefore SDL_EGL_GetVersion() shouldn't work with uninitialized display. |
520 | * - it actually doesn't work on Android that has 1.5 egl client |
521 | * - it works on desktop X11 (using SDL_VIDEO_FORCE_EGL=1) */ |
522 | SDL_EGL_GetVersion(_this); |
523 | |
524 | if (_this->egl_data->egl_version_major == 1 && _this->egl_data->egl_version_minor == 5) { |
525 | LOAD_FUNC(PFNEGLGETPLATFORMDISPLAYPROC, eglGetPlatformDisplay); |
526 | } |
527 | |
528 | if (_this->egl_data->eglGetPlatformDisplay) { |
529 | EGLAttrib *attribs = NULL; |
530 | if (_this->egl_platformattrib_callback) { |
531 | attribs = _this->egl_platformattrib_callback(_this->egl_attrib_callback_userdata); |
532 | if (!attribs) { |
533 | _this->gl_config.driver_loaded = 0; |
534 | *_this->gl_config.driver_path = '\0'; |
535 | return SDL_SetError("EGL platform attribute callback returned NULL pointer" ); |
536 | } |
537 | } |
538 | _this->egl_data->egl_display = _this->egl_data->eglGetPlatformDisplay(platform, (void *)(uintptr_t)native_display, attribs); |
539 | SDL_free(attribs); |
540 | } else { |
541 | if (SDL_EGL_HasExtension(_this, SDL_EGL_CLIENT_EXTENSION, "EGL_EXT_platform_base" )) { |
542 | _this->egl_data->eglGetPlatformDisplayEXT = (PFNEGLGETPLATFORMDISPLAYEXTPROC)SDL_EGL_GetProcAddressInternal(_this, "eglGetPlatformDisplayEXT" ); |
543 | if (_this->egl_data->eglGetPlatformDisplayEXT) { |
544 | _this->egl_data->egl_display = _this->egl_data->eglGetPlatformDisplayEXT(platform, (void *)(uintptr_t)native_display, NULL); |
545 | } |
546 | } |
547 | } |
548 | } |
549 | #endif |
550 | // Try the implementation-specific eglGetDisplay even if eglGetPlatformDisplay fails |
551 | if ((_this->egl_data->egl_display == EGL_NO_DISPLAY) && |
552 | (_this->egl_data->eglGetDisplay) && |
553 | SDL_GetHintBoolean(SDL_HINT_VIDEO_EGL_ALLOW_GETDISPLAY_FALLBACK, true)) { |
554 | _this->egl_data->egl_display = _this->egl_data->eglGetDisplay(native_display); |
555 | } |
556 | if (_this->egl_data->egl_display == EGL_NO_DISPLAY) { |
557 | _this->gl_config.driver_loaded = 0; |
558 | *_this->gl_config.driver_path = '\0'; |
559 | return SDL_SetError("Could not get EGL display" ); |
560 | } |
561 | |
562 | if (_this->egl_data->eglInitialize(_this->egl_data->egl_display, NULL, NULL) != EGL_TRUE) { |
563 | _this->gl_config.driver_loaded = 0; |
564 | *_this->gl_config.driver_path = '\0'; |
565 | return SDL_SetError("Could not initialize EGL" ); |
566 | } |
567 | |
568 | // Get the EGL version with a valid egl_display, for EGL <= 1.4 |
569 | SDL_EGL_GetVersion(_this); |
570 | |
571 | _this->egl_data->is_offscreen = false; |
572 | |
573 | return true; |
574 | } |
575 | |
576 | /** |
577 | On multi GPU machines EGL device 0 is not always the first valid GPU. |
578 | Container environments can restrict access to some GPUs that are still listed in the EGL |
579 | device list. If the requested device is a restricted GPU and cannot be used |
580 | (eglInitialize() will fail) then attempt to automatically and silently select the next |
581 | valid available GPU for EGL to use. |
582 | */ |
583 | |
584 | bool SDL_EGL_InitializeOffscreen(SDL_VideoDevice *_this, int device) |
585 | { |
586 | void *egl_devices[SDL_EGL_MAX_DEVICES]; |
587 | EGLint num_egl_devices = 0; |
588 | const char *egl_device_hint; |
589 | |
590 | if (_this->gl_config.driver_loaded <= 0) { |
591 | return SDL_SetError("SDL_EGL_LoadLibraryOnly() has not been called or has failed." ); |
592 | } |
593 | |
594 | // Check for all extensions that are optional until used and fail if any is missing |
595 | if (!_this->egl_data->eglQueryDevicesEXT) { |
596 | return SDL_SetError("eglQueryDevicesEXT is missing (EXT_device_enumeration not supported by the drivers?)" ); |
597 | } |
598 | |
599 | if (!_this->egl_data->eglGetPlatformDisplayEXT) { |
600 | return SDL_SetError("eglGetPlatformDisplayEXT is missing (EXT_platform_base not supported by the drivers?)" ); |
601 | } |
602 | |
603 | if (_this->egl_data->eglQueryDevicesEXT(SDL_EGL_MAX_DEVICES, egl_devices, &num_egl_devices) != EGL_TRUE) { |
604 | return SDL_SetError("eglQueryDevicesEXT() failed" ); |
605 | } |
606 | |
607 | egl_device_hint = SDL_GetHint("SDL_HINT_EGL_DEVICE" ); |
608 | if (egl_device_hint) { |
609 | device = SDL_atoi(egl_device_hint); |
610 | |
611 | if (device >= num_egl_devices) { |
612 | return SDL_SetError("Invalid EGL device is requested." ); |
613 | } |
614 | |
615 | _this->egl_data->egl_display = _this->egl_data->eglGetPlatformDisplayEXT(EGL_PLATFORM_DEVICE_EXT, egl_devices[device], NULL); |
616 | |
617 | if (_this->egl_data->egl_display == EGL_NO_DISPLAY) { |
618 | return SDL_SetError("eglGetPlatformDisplayEXT() failed." ); |
619 | } |
620 | |
621 | if (_this->egl_data->eglInitialize(_this->egl_data->egl_display, NULL, NULL) != EGL_TRUE) { |
622 | return SDL_SetError("Could not initialize EGL" ); |
623 | } |
624 | } else { |
625 | int i; |
626 | bool found = false; |
627 | EGLDisplay attempted_egl_display; |
628 | |
629 | // If no hint is provided lets look for the first device/display that will allow us to eglInit |
630 | for (i = 0; i < num_egl_devices; i++) { |
631 | attempted_egl_display = _this->egl_data->eglGetPlatformDisplayEXT(EGL_PLATFORM_DEVICE_EXT, egl_devices[i], NULL); |
632 | |
633 | if (attempted_egl_display == EGL_NO_DISPLAY) { |
634 | continue; |
635 | } |
636 | |
637 | if (_this->egl_data->eglInitialize(attempted_egl_display, NULL, NULL) != EGL_TRUE) { |
638 | _this->egl_data->eglTerminate(attempted_egl_display); |
639 | continue; |
640 | } |
641 | |
642 | // We did not fail, we'll pick this one! |
643 | _this->egl_data->egl_display = attempted_egl_display; |
644 | found = true; |
645 | |
646 | break; |
647 | } |
648 | |
649 | if (!found) { |
650 | return SDL_SetError("Could not find a valid EGL device to initialize" ); |
651 | } |
652 | } |
653 | |
654 | // Get the EGL version with a valid egl_display, for EGL <= 1.4 |
655 | SDL_EGL_GetVersion(_this); |
656 | |
657 | _this->egl_data->is_offscreen = true; |
658 | |
659 | return true; |
660 | } |
661 | |
662 | void SDL_EGL_SetRequiredVisualId(SDL_VideoDevice *_this, int visual_id) |
663 | { |
664 | _this->egl_data->egl_required_visual_id = visual_id; |
665 | } |
666 | |
667 | #ifdef DUMP_EGL_CONFIG |
668 | |
669 | #define ATTRIBUTE(_attr) \ |
670 | { \ |
671 | _attr, #_attr \ |
672 | } |
673 | |
674 | typedef struct |
675 | { |
676 | EGLint attribute; |
677 | char const *name; |
678 | } Attribute; |
679 | |
680 | static Attribute all_attributes[] = { |
681 | ATTRIBUTE(EGL_BUFFER_SIZE), |
682 | ATTRIBUTE(EGL_ALPHA_SIZE), |
683 | ATTRIBUTE(EGL_BLUE_SIZE), |
684 | ATTRIBUTE(EGL_GREEN_SIZE), |
685 | ATTRIBUTE(EGL_RED_SIZE), |
686 | ATTRIBUTE(EGL_DEPTH_SIZE), |
687 | ATTRIBUTE(EGL_STENCIL_SIZE), |
688 | ATTRIBUTE(EGL_CONFIG_CAVEAT), |
689 | ATTRIBUTE(EGL_CONFIG_ID), |
690 | ATTRIBUTE(EGL_LEVEL), |
691 | ATTRIBUTE(EGL_MAX_PBUFFER_HEIGHT), |
692 | ATTRIBUTE(EGL_MAX_PBUFFER_WIDTH), |
693 | ATTRIBUTE(EGL_MAX_PBUFFER_PIXELS), |
694 | ATTRIBUTE(EGL_NATIVE_RENDERABLE), |
695 | ATTRIBUTE(EGL_NATIVE_VISUAL_ID), |
696 | ATTRIBUTE(EGL_NATIVE_VISUAL_TYPE), |
697 | ATTRIBUTE(EGL_SAMPLES), |
698 | ATTRIBUTE(EGL_SAMPLE_BUFFERS), |
699 | ATTRIBUTE(EGL_SURFACE_TYPE), |
700 | ATTRIBUTE(EGL_TRANSPARENT_TYPE), |
701 | ATTRIBUTE(EGL_TRANSPARENT_BLUE_VALUE), |
702 | ATTRIBUTE(EGL_TRANSPARENT_GREEN_VALUE), |
703 | ATTRIBUTE(EGL_TRANSPARENT_RED_VALUE), |
704 | ATTRIBUTE(EGL_BIND_TO_TEXTURE_RGB), |
705 | ATTRIBUTE(EGL_BIND_TO_TEXTURE_RGBA), |
706 | ATTRIBUTE(EGL_MIN_SWAP_INTERVAL), |
707 | ATTRIBUTE(EGL_MAX_SWAP_INTERVAL), |
708 | ATTRIBUTE(EGL_LUMINANCE_SIZE), |
709 | ATTRIBUTE(EGL_ALPHA_MASK_SIZE), |
710 | ATTRIBUTE(EGL_COLOR_BUFFER_TYPE), |
711 | ATTRIBUTE(EGL_RENDERABLE_TYPE), |
712 | ATTRIBUTE(EGL_MATCH_NATIVE_PIXMAP), |
713 | ATTRIBUTE(EGL_CONFORMANT), |
714 | }; |
715 | |
716 | static void dumpconfig(SDL_VideoDevice *_this, EGLConfig config) |
717 | { |
718 | int attr; |
719 | for (attr = 0; attr < sizeof(all_attributes) / sizeof(Attribute); attr++) { |
720 | EGLint value; |
721 | _this->egl_data->eglGetConfigAttrib(_this->egl_data->egl_display, config, all_attributes[attr].attribute, &value); |
722 | SDL_Log("\t%-32s: %10d (0x%08x)" , all_attributes[attr].name, value, value); |
723 | } |
724 | } |
725 | |
726 | #endif // DUMP_EGL_CONFIG |
727 | |
728 | static bool SDL_EGL_PrivateChooseConfig(SDL_VideoDevice *_this, bool set_config_caveat_none) |
729 | { |
730 | // 64 seems nice. |
731 | EGLint attribs[64]; |
732 | EGLint found_configs = 0, value; |
733 | // 128 seems even nicer here |
734 | EGLConfig configs[128]; |
735 | bool has_matching_format = false; |
736 | int i, j, best_bitdiff = -1, best_truecolor_bitdiff = -1; |
737 | int truecolor_config_idx = -1; |
738 | |
739 | // Get a valid EGL configuration |
740 | i = 0; |
741 | attribs[i++] = EGL_RED_SIZE; |
742 | attribs[i++] = _this->gl_config.red_size; |
743 | attribs[i++] = EGL_GREEN_SIZE; |
744 | attribs[i++] = _this->gl_config.green_size; |
745 | attribs[i++] = EGL_BLUE_SIZE; |
746 | attribs[i++] = _this->gl_config.blue_size; |
747 | |
748 | if (set_config_caveat_none) { |
749 | attribs[i++] = EGL_CONFIG_CAVEAT; |
750 | attribs[i++] = EGL_NONE; |
751 | } |
752 | |
753 | if (_this->gl_config.alpha_size) { |
754 | attribs[i++] = EGL_ALPHA_SIZE; |
755 | attribs[i++] = _this->gl_config.alpha_size; |
756 | } |
757 | |
758 | if (_this->gl_config.buffer_size) { |
759 | attribs[i++] = EGL_BUFFER_SIZE; |
760 | attribs[i++] = _this->gl_config.buffer_size; |
761 | } |
762 | |
763 | if (_this->gl_config.depth_size) { |
764 | attribs[i++] = EGL_DEPTH_SIZE; |
765 | attribs[i++] = _this->gl_config.depth_size; |
766 | } |
767 | |
768 | if (_this->gl_config.stencil_size) { |
769 | attribs[i++] = EGL_STENCIL_SIZE; |
770 | attribs[i++] = _this->gl_config.stencil_size; |
771 | } |
772 | |
773 | if (_this->gl_config.multisamplebuffers) { |
774 | attribs[i++] = EGL_SAMPLE_BUFFERS; |
775 | attribs[i++] = _this->gl_config.multisamplebuffers; |
776 | } |
777 | |
778 | if (_this->gl_config.multisamplesamples) { |
779 | attribs[i++] = EGL_SAMPLES; |
780 | attribs[i++] = _this->gl_config.multisamplesamples; |
781 | } |
782 | |
783 | if (_this->gl_config.floatbuffers) { |
784 | attribs[i++] = EGL_COLOR_COMPONENT_TYPE_EXT; |
785 | attribs[i++] = EGL_COLOR_COMPONENT_TYPE_FLOAT_EXT; |
786 | } |
787 | |
788 | if (_this->egl_data->is_offscreen) { |
789 | attribs[i++] = EGL_SURFACE_TYPE; |
790 | attribs[i++] = EGL_PBUFFER_BIT; |
791 | } |
792 | |
793 | attribs[i++] = EGL_RENDERABLE_TYPE; |
794 | if (_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES) { |
795 | #ifdef EGL_KHR_create_context |
796 | if (_this->gl_config.major_version >= 3 && |
797 | SDL_EGL_HasExtension(_this, SDL_EGL_DISPLAY_EXTENSION, "EGL_KHR_create_context" )) { |
798 | attribs[i++] = EGL_OPENGL_ES3_BIT_KHR; |
799 | } else |
800 | #endif |
801 | if (_this->gl_config.major_version >= 2) { |
802 | attribs[i++] = EGL_OPENGL_ES2_BIT; |
803 | } else { |
804 | attribs[i++] = EGL_OPENGL_ES_BIT; |
805 | } |
806 | _this->egl_data->eglBindAPI(EGL_OPENGL_ES_API); |
807 | } else { |
808 | attribs[i++] = EGL_OPENGL_BIT; |
809 | _this->egl_data->eglBindAPI(EGL_OPENGL_API); |
810 | } |
811 | |
812 | if (_this->egl_data->egl_surfacetype) { |
813 | attribs[i++] = EGL_SURFACE_TYPE; |
814 | attribs[i++] = _this->egl_data->egl_surfacetype; |
815 | } |
816 | |
817 | attribs[i++] = EGL_NONE; |
818 | |
819 | SDL_assert(i < SDL_arraysize(attribs)); |
820 | |
821 | if (_this->egl_data->eglChooseConfig(_this->egl_data->egl_display, |
822 | attribs, |
823 | configs, SDL_arraysize(configs), |
824 | &found_configs) == EGL_FALSE || |
825 | found_configs == 0) { |
826 | return false; |
827 | } |
828 | |
829 | // first ensure that a found config has a matching format, or the function will fall through. |
830 | if (_this->egl_data->egl_required_visual_id) { |
831 | for (i = 0; i < found_configs; i++) { |
832 | EGLint format; |
833 | _this->egl_data->eglGetConfigAttrib(_this->egl_data->egl_display, |
834 | configs[i], |
835 | EGL_NATIVE_VISUAL_ID, &format); |
836 | if (_this->egl_data->egl_required_visual_id == format) { |
837 | has_matching_format = true; |
838 | break; |
839 | } |
840 | } |
841 | } |
842 | |
843 | // eglChooseConfig returns a number of configurations that match or exceed the requested attribs. |
844 | // From those, we select the one that matches our requirements more closely via a makeshift algorithm |
845 | |
846 | for (i = 0; i < found_configs; i++) { |
847 | bool is_truecolor = false; |
848 | int bitdiff = 0; |
849 | |
850 | if (has_matching_format && _this->egl_data->egl_required_visual_id) { |
851 | EGLint format; |
852 | _this->egl_data->eglGetConfigAttrib(_this->egl_data->egl_display, |
853 | configs[i], |
854 | EGL_NATIVE_VISUAL_ID, &format); |
855 | if (_this->egl_data->egl_required_visual_id != format) { |
856 | continue; |
857 | } |
858 | } |
859 | |
860 | _this->egl_data->eglGetConfigAttrib(_this->egl_data->egl_display, configs[i], EGL_RED_SIZE, &value); |
861 | if (value == 8) { |
862 | _this->egl_data->eglGetConfigAttrib(_this->egl_data->egl_display, configs[i], EGL_GREEN_SIZE, &value); |
863 | if (value == 8) { |
864 | _this->egl_data->eglGetConfigAttrib(_this->egl_data->egl_display, configs[i], EGL_BLUE_SIZE, &value); |
865 | if (value == 8) { |
866 | is_truecolor = true; |
867 | } |
868 | } |
869 | } |
870 | |
871 | for (j = 0; j < SDL_arraysize(attribs) - 1; j += 2) { |
872 | if (attribs[j] == EGL_NONE) { |
873 | break; |
874 | } |
875 | |
876 | if (attribs[j + 1] != EGL_DONT_CARE && (attribs[j] == EGL_RED_SIZE || |
877 | attribs[j] == EGL_GREEN_SIZE || |
878 | attribs[j] == EGL_BLUE_SIZE || |
879 | attribs[j] == EGL_ALPHA_SIZE || |
880 | attribs[j] == EGL_DEPTH_SIZE || |
881 | attribs[j] == EGL_STENCIL_SIZE)) { |
882 | _this->egl_data->eglGetConfigAttrib(_this->egl_data->egl_display, configs[i], attribs[j], &value); |
883 | bitdiff += value - attribs[j + 1]; // value is always >= attrib |
884 | } |
885 | } |
886 | |
887 | if ((bitdiff < best_bitdiff) || (best_bitdiff == -1)) { |
888 | _this->egl_data->egl_config = configs[i]; |
889 | best_bitdiff = bitdiff; |
890 | } |
891 | |
892 | if (is_truecolor && ((bitdiff < best_truecolor_bitdiff) || (best_truecolor_bitdiff == -1))) { |
893 | truecolor_config_idx = i; |
894 | best_truecolor_bitdiff = bitdiff; |
895 | } |
896 | } |
897 | |
898 | #define FAVOR_TRUECOLOR 1 |
899 | #if FAVOR_TRUECOLOR |
900 | /* Some apps request a low color depth, either because they _assume_ |
901 | they'll get a larger one but don't want to fail if only smaller ones |
902 | are available, or they just never called SDL_GL_SetAttribute at all and |
903 | got a tiny default. For these cases, a game that would otherwise run |
904 | at 24-bit color might get dithered down to something smaller, which is |
905 | worth avoiding. If the app requested <= 16 bit color and an exact 24-bit |
906 | match is available, favor that. Otherwise, we look for the closest |
907 | match. Note that while the API promises what you request _or better_, |
908 | it's feasible this can be disastrous for performance for custom software |
909 | on small hardware that all expected to actually get 16-bit color. In this |
910 | case, turn off FAVOR_TRUECOLOR (and maybe send a patch to make this more |
911 | flexible). */ |
912 | if (((_this->gl_config.red_size + _this->gl_config.blue_size + _this->gl_config.green_size) <= 16)) { |
913 | if (truecolor_config_idx != -1) { |
914 | _this->egl_data->egl_config = configs[truecolor_config_idx]; |
915 | } |
916 | } |
917 | #endif |
918 | |
919 | #ifdef DUMP_EGL_CONFIG |
920 | dumpconfig(_this, _this->egl_data->egl_config); |
921 | #endif |
922 | |
923 | return true; |
924 | } |
925 | |
926 | bool SDL_EGL_ChooseConfig(SDL_VideoDevice *_this) |
927 | { |
928 | if (!_this->egl_data) { |
929 | return SDL_SetError("EGL not initialized" ); |
930 | } |
931 | |
932 | // Try with EGL_CONFIG_CAVEAT set to EGL_NONE, to avoid any EGL_SLOW_CONFIG or EGL_NON_CONFORMANT_CONFIG |
933 | if (SDL_EGL_PrivateChooseConfig(_this, true)) { |
934 | return true; |
935 | } |
936 | |
937 | // Fallback with all configs |
938 | if (SDL_EGL_PrivateChooseConfig(_this, false)) { |
939 | SDL_Log("SDL_EGL_ChooseConfig: found a slow EGL config" ); |
940 | return true; |
941 | } |
942 | |
943 | return SDL_EGL_SetError("Couldn't find matching EGL config" , "eglChooseConfig" ); |
944 | } |
945 | |
946 | SDL_GLContext SDL_EGL_CreateContext(SDL_VideoDevice *_this, EGLSurface egl_surface) |
947 | { |
948 | // max 16 key+value pairs plus terminator. |
949 | EGLint attribs[33]; |
950 | int attr = 0; |
951 | |
952 | EGLContext egl_context, share_context = EGL_NO_CONTEXT; |
953 | EGLint profile_mask = _this->gl_config.profile_mask; |
954 | EGLint major_version = _this->gl_config.major_version; |
955 | EGLint minor_version = _this->gl_config.minor_version; |
956 | bool profile_es = (profile_mask == SDL_GL_CONTEXT_PROFILE_ES); |
957 | |
958 | if (!_this->egl_data) { |
959 | SDL_SetError("EGL not initialized" ); |
960 | return NULL; |
961 | } |
962 | |
963 | if (_this->gl_config.share_with_current_context) { |
964 | share_context = (EGLContext)SDL_GL_GetCurrentContext(); |
965 | } |
966 | |
967 | #ifdef SDL_VIDEO_DRIVER_ANDROID |
968 | if (_this->gl_config.flags & SDL_GL_CONTEXT_DEBUG_FLAG) { |
969 | /* If SDL_GL_CONTEXT_DEBUG_FLAG is set but EGL_KHR_debug unsupported, unset. |
970 | * This is required because some Android devices like to complain about it |
971 | * by "silently" failing, logging a hint which could be easily overlooked: |
972 | * E/libEGL (26984): validate_display:255 error 3008 (EGL_BAD_DISPLAY) |
973 | * The following explicitly checks for EGL_KHR_debug before EGL 1.5 |
974 | */ |
975 | int egl_version_major = _this->egl_data->egl_version_major; |
976 | int egl_version_minor = _this->egl_data->egl_version_minor; |
977 | if (((egl_version_major < 1) || (egl_version_major == 1 && egl_version_minor < 5)) && |
978 | !SDL_EGL_HasExtension(_this, SDL_EGL_DISPLAY_EXTENSION, "EGL_KHR_debug" )) { |
979 | // SDL profile bits match EGL profile bits. |
980 | _this->gl_config.flags &= ~SDL_GL_CONTEXT_DEBUG_FLAG; |
981 | } |
982 | } |
983 | #endif |
984 | |
985 | // Set the context version and other attributes. |
986 | if ((major_version < 3 || (minor_version == 0 && profile_es)) && |
987 | _this->gl_config.flags == 0 && |
988 | (profile_mask == 0 || profile_es)) { |
989 | /* Create a context without using EGL_KHR_create_context attribs. |
990 | * When creating a GLES context without EGL_KHR_create_context we can |
991 | * only specify the major version. When creating a desktop GL context |
992 | * we can't specify any version, so we only try in that case when the |
993 | * version is less than 3.0 (matches SDL's GLX/WGL behavior.) |
994 | */ |
995 | if (profile_es) { |
996 | attribs[attr++] = EGL_CONTEXT_CLIENT_VERSION; |
997 | attribs[attr++] = SDL_max(major_version, 1); |
998 | } |
999 | } else { |
1000 | #ifdef EGL_KHR_create_context |
1001 | /* The Major/minor version, context profiles, and context flags can |
1002 | * only be specified when this extension is available. |
1003 | */ |
1004 | if (SDL_EGL_HasExtension(_this, SDL_EGL_DISPLAY_EXTENSION, "EGL_KHR_create_context" )) { |
1005 | attribs[attr++] = EGL_CONTEXT_MAJOR_VERSION_KHR; |
1006 | attribs[attr++] = major_version; |
1007 | attribs[attr++] = EGL_CONTEXT_MINOR_VERSION_KHR; |
1008 | attribs[attr++] = minor_version; |
1009 | |
1010 | // SDL profile bits match EGL profile bits. |
1011 | if (profile_mask != 0 && profile_mask != SDL_GL_CONTEXT_PROFILE_ES) { |
1012 | attribs[attr++] = EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR; |
1013 | attribs[attr++] = profile_mask; |
1014 | } |
1015 | |
1016 | // SDL flags match EGL flags. |
1017 | if (_this->gl_config.flags != 0) { |
1018 | attribs[attr++] = EGL_CONTEXT_FLAGS_KHR; |
1019 | attribs[attr++] = _this->gl_config.flags; |
1020 | } |
1021 | } else |
1022 | #endif // EGL_KHR_create_context |
1023 | { |
1024 | SDL_SetError("Could not create EGL context (context attributes are not supported)" ); |
1025 | return NULL; |
1026 | } |
1027 | } |
1028 | |
1029 | #ifdef EGL_KHR_create_context_no_error |
1030 | if (_this->gl_config.no_error) { |
1031 | if (SDL_EGL_HasExtension(_this, SDL_EGL_DISPLAY_EXTENSION, "EGL_KHR_create_context_no_error" )) { |
1032 | attribs[attr++] = EGL_CONTEXT_OPENGL_NO_ERROR_KHR; |
1033 | attribs[attr++] = _this->gl_config.no_error; |
1034 | } |
1035 | } |
1036 | #endif |
1037 | |
1038 | if (_this->egl_contextattrib_callback) { |
1039 | const int maxAttribs = sizeof(attribs) / sizeof(attribs[0]); |
1040 | EGLint *userAttribs, *userAttribP; |
1041 | userAttribs = _this->egl_contextattrib_callback(_this->egl_attrib_callback_userdata, _this->egl_data->egl_display, _this->egl_data->egl_config); |
1042 | if (!userAttribs) { |
1043 | _this->gl_config.driver_loaded = 0; |
1044 | *_this->gl_config.driver_path = '\0'; |
1045 | SDL_SetError("EGL context attribute callback returned NULL pointer" ); |
1046 | return NULL; |
1047 | } |
1048 | |
1049 | for (userAttribP = userAttribs; *userAttribP != EGL_NONE;) { |
1050 | if (attr + 3 >= maxAttribs) { |
1051 | _this->gl_config.driver_loaded = 0; |
1052 | *_this->gl_config.driver_path = '\0'; |
1053 | SDL_SetError("EGL context attribute callback returned too many attributes" ); |
1054 | return NULL; |
1055 | } |
1056 | attribs[attr++] = *userAttribP++; |
1057 | attribs[attr++] = *userAttribP++; |
1058 | } |
1059 | SDL_free(userAttribs); |
1060 | } |
1061 | |
1062 | attribs[attr++] = EGL_NONE; |
1063 | |
1064 | // Bind the API |
1065 | if (profile_es) { |
1066 | _this->egl_data->apitype = EGL_OPENGL_ES_API; |
1067 | } else { |
1068 | _this->egl_data->apitype = EGL_OPENGL_API; |
1069 | } |
1070 | _this->egl_data->eglBindAPI(_this->egl_data->apitype); |
1071 | |
1072 | egl_context = _this->egl_data->eglCreateContext(_this->egl_data->egl_display, |
1073 | _this->egl_data->egl_config, |
1074 | share_context, attribs); |
1075 | |
1076 | if (egl_context == EGL_NO_CONTEXT) { |
1077 | SDL_EGL_SetError("Could not create EGL context" , "eglCreateContext" ); |
1078 | return NULL; |
1079 | } |
1080 | |
1081 | _this->egl_data->egl_swapinterval = 0; |
1082 | |
1083 | if (!SDL_EGL_MakeCurrent(_this, egl_surface, (SDL_GLContext)egl_context)) { |
1084 | // Delete the context |
1085 | SDL_EGL_DestroyContext(_this, (SDL_GLContext)egl_context); |
1086 | return NULL; |
1087 | } |
1088 | |
1089 | /* Check whether making contexts current without a surface is supported. |
1090 | * First condition: EGL must support it. That's the case for EGL 1.5 |
1091 | * or later, or if the EGL_KHR_surfaceless_context extension is present. */ |
1092 | if ((_this->egl_data->egl_version_major > 1) || |
1093 | ((_this->egl_data->egl_version_major == 1) && (_this->egl_data->egl_version_minor >= 5)) || |
1094 | SDL_EGL_HasExtension(_this, SDL_EGL_DISPLAY_EXTENSION, "EGL_KHR_surfaceless_context" )) { |
1095 | // Secondary condition: The client API must support it. |
1096 | if (profile_es) { |
1097 | /* On OpenGL ES, the GL_OES_surfaceless_context extension must be |
1098 | * present. */ |
1099 | if (SDL_GL_ExtensionSupported("GL_OES_surfaceless_context" )) { |
1100 | _this->gl_allow_no_surface = true; |
1101 | } |
1102 | #if defined(SDL_VIDEO_OPENGL) && !defined(SDL_VIDEO_DRIVER_VITA) |
1103 | } else { |
1104 | // Desktop OpenGL supports it by default from version 3.0 on. |
1105 | PFNGLGETINTEGERVPROC glGetIntegervFunc = (PFNGLGETINTEGERVPROC)SDL_GL_GetProcAddress("glGetIntegerv" ); |
1106 | if (glGetIntegervFunc) { |
1107 | GLint v = 0; |
1108 | glGetIntegervFunc(GL_MAJOR_VERSION, &v); |
1109 | if (v >= 3) { |
1110 | _this->gl_allow_no_surface = true; |
1111 | } |
1112 | } |
1113 | #endif |
1114 | } |
1115 | } |
1116 | |
1117 | return (SDL_GLContext)egl_context; |
1118 | } |
1119 | |
1120 | bool SDL_EGL_MakeCurrent(SDL_VideoDevice *_this, EGLSurface egl_surface, SDL_GLContext context) |
1121 | { |
1122 | EGLContext egl_context = (EGLContext)context; |
1123 | |
1124 | if (!_this->egl_data) { |
1125 | return SDL_SetError("EGL not initialized" ); |
1126 | } |
1127 | |
1128 | if (!_this->egl_data->eglMakeCurrent) { |
1129 | if (!egl_surface && !context) { |
1130 | // Can't do the nothing there is to do? Probably trying to cleanup a failed startup, just return. |
1131 | return true; |
1132 | } else { |
1133 | return SDL_SetError("EGL not initialized" ); // something clearly went wrong somewhere. |
1134 | } |
1135 | } |
1136 | |
1137 | // Make sure current thread has a valid API bound to it. |
1138 | if (_this->egl_data->eglBindAPI) { |
1139 | _this->egl_data->eglBindAPI(_this->egl_data->apitype); |
1140 | } |
1141 | |
1142 | /* The android emulator crashes badly if you try to eglMakeCurrent |
1143 | * with a valid context and invalid surface, so we have to check for both here. |
1144 | */ |
1145 | if (!egl_context || (!egl_surface && !_this->gl_allow_no_surface)) { |
1146 | _this->egl_data->eglMakeCurrent(_this->egl_data->egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); |
1147 | } else { |
1148 | if (!_this->egl_data->eglMakeCurrent(_this->egl_data->egl_display, |
1149 | egl_surface, egl_surface, egl_context)) { |
1150 | return SDL_EGL_SetError("Unable to make EGL context current" , "eglMakeCurrent" ); |
1151 | } |
1152 | } |
1153 | |
1154 | return true; |
1155 | } |
1156 | |
1157 | bool SDL_EGL_SetSwapInterval(SDL_VideoDevice *_this, int interval) |
1158 | { |
1159 | EGLBoolean status; |
1160 | |
1161 | if (!_this->egl_data) { |
1162 | return SDL_SetError("EGL not initialized" ); |
1163 | } |
1164 | |
1165 | /* FIXME: Revisit this check when EGL_EXT_swap_control_tear is published: |
1166 | * https://github.com/KhronosGroup/EGL-Registry/pull/113 |
1167 | */ |
1168 | if (interval < 0) { |
1169 | return SDL_SetError("Late swap tearing currently unsupported" ); |
1170 | } |
1171 | |
1172 | status = _this->egl_data->eglSwapInterval(_this->egl_data->egl_display, interval); |
1173 | if (status == EGL_TRUE) { |
1174 | _this->egl_data->egl_swapinterval = interval; |
1175 | return true; |
1176 | } |
1177 | |
1178 | return SDL_EGL_SetError("Unable to set the EGL swap interval" , "eglSwapInterval" ); |
1179 | } |
1180 | |
1181 | bool SDL_EGL_GetSwapInterval(SDL_VideoDevice *_this, int *interval) |
1182 | { |
1183 | if (!_this->egl_data) { |
1184 | return SDL_SetError("EGL not initialized" ); |
1185 | } |
1186 | |
1187 | *interval = _this->egl_data->egl_swapinterval; |
1188 | return true; |
1189 | } |
1190 | |
1191 | bool SDL_EGL_SwapBuffers(SDL_VideoDevice *_this, EGLSurface egl_surface) |
1192 | { |
1193 | if (!_this->egl_data->eglSwapBuffers(_this->egl_data->egl_display, egl_surface)) { |
1194 | return SDL_EGL_SetError("unable to show color buffer in an OS-native window" , "eglSwapBuffers" ); |
1195 | } |
1196 | return true; |
1197 | } |
1198 | |
1199 | bool SDL_EGL_DestroyContext(SDL_VideoDevice *_this, SDL_GLContext context) |
1200 | { |
1201 | EGLContext egl_context = (EGLContext)context; |
1202 | |
1203 | // Clean up GLES and EGL |
1204 | if (!_this->egl_data) { |
1205 | return true; |
1206 | } |
1207 | |
1208 | if (egl_context != NULL && egl_context != EGL_NO_CONTEXT) { |
1209 | _this->egl_data->eglDestroyContext(_this->egl_data->egl_display, egl_context); |
1210 | } |
1211 | return true; |
1212 | } |
1213 | |
1214 | EGLSurface SDL_EGL_CreateSurface(SDL_VideoDevice *_this, SDL_Window *window, NativeWindowType nw) |
1215 | { |
1216 | #ifdef SDL_VIDEO_DRIVER_ANDROID |
1217 | EGLint format_wanted; |
1218 | EGLint format_got; |
1219 | #endif |
1220 | // max 16 key+value pairs, plus terminator. |
1221 | EGLint attribs[33]; |
1222 | int attr = 0; |
1223 | |
1224 | EGLSurface surface; |
1225 | |
1226 | if (!SDL_EGL_ChooseConfig(_this)) { |
1227 | return EGL_NO_SURFACE; |
1228 | } |
1229 | |
1230 | #ifdef SDL_VIDEO_DRIVER_ANDROID |
1231 | /* On Android, EGL_NATIVE_VISUAL_ID is an attribute of the EGLConfig that is |
1232 | * guaranteed to be accepted by ANativeWindow_setBuffersGeometry(). */ |
1233 | _this->egl_data->eglGetConfigAttrib(_this->egl_data->egl_display, |
1234 | _this->egl_data->egl_config, |
1235 | EGL_NATIVE_VISUAL_ID, &format_wanted); |
1236 | |
1237 | // Format based on selected egl config. |
1238 | ANativeWindow_setBuffersGeometry(nw, 0, 0, format_wanted); |
1239 | #endif |
1240 | |
1241 | if (_this->gl_config.framebuffer_srgb_capable) { |
1242 | #ifdef EGL_KHR_gl_colorspace |
1243 | if (SDL_EGL_HasExtension(_this, SDL_EGL_DISPLAY_EXTENSION, "EGL_KHR_gl_colorspace" )) { |
1244 | attribs[attr++] = EGL_GL_COLORSPACE_KHR; |
1245 | attribs[attr++] = EGL_GL_COLORSPACE_SRGB_KHR; |
1246 | } else |
1247 | #endif |
1248 | { |
1249 | SDL_SetError("EGL implementation does not support sRGB system framebuffers" ); |
1250 | return EGL_NO_SURFACE; |
1251 | } |
1252 | } |
1253 | |
1254 | #ifdef EGL_EXT_present_opaque |
1255 | if (SDL_EGL_HasExtension(_this, SDL_EGL_DISPLAY_EXTENSION, "EGL_EXT_present_opaque" )) { |
1256 | bool allow_transparent = false; |
1257 | if (window && (window->flags & SDL_WINDOW_TRANSPARENT)) { |
1258 | allow_transparent = true; |
1259 | } |
1260 | attribs[attr++] = EGL_PRESENT_OPAQUE_EXT; |
1261 | attribs[attr++] = allow_transparent ? EGL_FALSE : EGL_TRUE; |
1262 | } |
1263 | #endif |
1264 | |
1265 | if (_this->egl_surfaceattrib_callback) { |
1266 | const int maxAttribs = sizeof(attribs) / sizeof(attribs[0]); |
1267 | EGLint *userAttribs, *userAttribP; |
1268 | userAttribs = _this->egl_surfaceattrib_callback(_this->egl_attrib_callback_userdata, _this->egl_data->egl_display, _this->egl_data->egl_config); |
1269 | if (!userAttribs) { |
1270 | _this->gl_config.driver_loaded = 0; |
1271 | *_this->gl_config.driver_path = '\0'; |
1272 | SDL_SetError("EGL surface attribute callback returned NULL pointer" ); |
1273 | return EGL_NO_SURFACE; |
1274 | } |
1275 | |
1276 | for (userAttribP = userAttribs; *userAttribP != EGL_NONE;) { |
1277 | if (attr + 3 >= maxAttribs) { |
1278 | _this->gl_config.driver_loaded = 0; |
1279 | *_this->gl_config.driver_path = '\0'; |
1280 | SDL_SetError("EGL surface attribute callback returned too many attributes" ); |
1281 | return EGL_NO_SURFACE; |
1282 | } |
1283 | attribs[attr++] = *userAttribP++; |
1284 | attribs[attr++] = *userAttribP++; |
1285 | } |
1286 | SDL_free(userAttribs); |
1287 | } |
1288 | |
1289 | attribs[attr++] = EGL_NONE; |
1290 | |
1291 | surface = _this->egl_data->eglCreateWindowSurface( |
1292 | _this->egl_data->egl_display, |
1293 | _this->egl_data->egl_config, |
1294 | nw, &attribs[0]); |
1295 | if (surface == EGL_NO_SURFACE) { |
1296 | SDL_EGL_SetError("unable to create an EGL window surface" , "eglCreateWindowSurface" ); |
1297 | } |
1298 | |
1299 | #ifdef SDL_VIDEO_DRIVER_ANDROID |
1300 | format_got = ANativeWindow_getFormat(nw); |
1301 | Android_SetFormat(format_wanted, format_got); |
1302 | #endif |
1303 | |
1304 | return surface; |
1305 | } |
1306 | |
1307 | EGLSurface |
1308 | SDL_EGL_CreateOffscreenSurface(SDL_VideoDevice *_this, int width, int height) |
1309 | { |
1310 | EGLint attributes[] = { |
1311 | EGL_WIDTH, 0, |
1312 | EGL_HEIGHT, 0, |
1313 | EGL_NONE |
1314 | }; |
1315 | attributes[1] = width; |
1316 | attributes[3] = height; |
1317 | |
1318 | if (!SDL_EGL_ChooseConfig(_this)) { |
1319 | return EGL_NO_SURFACE; |
1320 | } |
1321 | |
1322 | return _this->egl_data->eglCreatePbufferSurface( |
1323 | _this->egl_data->egl_display, |
1324 | _this->egl_data->egl_config, |
1325 | attributes); |
1326 | } |
1327 | |
1328 | void SDL_EGL_DestroySurface(SDL_VideoDevice *_this, EGLSurface egl_surface) |
1329 | { |
1330 | if (!_this->egl_data) { |
1331 | return; |
1332 | } |
1333 | |
1334 | if (egl_surface != EGL_NO_SURFACE) { |
1335 | _this->egl_data->eglDestroySurface(_this->egl_data->egl_display, egl_surface); |
1336 | } |
1337 | } |
1338 | |
1339 | #endif // SDL_VIDEO_OPENGL_EGL |
1340 | |