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