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