SDL_egl.c 45 KB

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