SDL_egl.c 51 KB

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