SDL_egl.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  1. /*
  2. * Simple DirectMedia Layer
  3. * Copyright (C) 1997-2017 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. #endif
  29. #include "SDL_sysvideo.h"
  30. #include "SDL_egl_c.h"
  31. #include "SDL_loadso.h"
  32. #include "SDL_hints.h"
  33. #ifdef EGL_KHR_create_context
  34. /* EGL_OPENGL_ES3_BIT_KHR was added in version 13 of the extension. */
  35. #ifndef EGL_OPENGL_ES3_BIT_KHR
  36. #define EGL_OPENGL_ES3_BIT_KHR 0x00000040
  37. #endif
  38. #endif /* EGL_KHR_create_context */
  39. #if SDL_VIDEO_DRIVER_RPI
  40. /* Raspbian places the OpenGL ES/EGL binaries in a non standard path */
  41. #define DEFAULT_EGL "/opt/vc/lib/libEGL.so"
  42. #define DEFAULT_OGL_ES2 "/opt/vc/lib/libGLESv2.so"
  43. #define DEFAULT_OGL_ES_PVR "/opt/vc/lib/libGLES_CM.so"
  44. #define DEFAULT_OGL_ES "/opt/vc/lib/libGLESv1_CM.so"
  45. #elif SDL_VIDEO_DRIVER_ANDROID || SDL_VIDEO_DRIVER_VIVANTE
  46. /* Android */
  47. #define DEFAULT_EGL "libEGL.so"
  48. #define DEFAULT_OGL_ES2 "libGLESv2.so"
  49. #define DEFAULT_OGL_ES_PVR "libGLES_CM.so"
  50. #define DEFAULT_OGL_ES "libGLESv1_CM.so"
  51. #elif SDL_VIDEO_DRIVER_WINDOWS || SDL_VIDEO_DRIVER_WINRT
  52. /* EGL AND OpenGL ES support via ANGLE */
  53. #define DEFAULT_EGL "libEGL.dll"
  54. #define DEFAULT_OGL_ES2 "libGLESv2.dll"
  55. #define DEFAULT_OGL_ES_PVR "libGLES_CM.dll"
  56. #define DEFAULT_OGL_ES "libGLESv1_CM.dll"
  57. #else
  58. /* Desktop Linux */
  59. #define DEFAULT_OGL "libGL.so.1"
  60. #define DEFAULT_EGL "libEGL.so.1"
  61. #define DEFAULT_OGL_ES2 "libGLESv2.so.2"
  62. #define DEFAULT_OGL_ES_PVR "libGLES_CM.so.1"
  63. #define DEFAULT_OGL_ES "libGLESv1_CM.so.1"
  64. #endif /* SDL_VIDEO_DRIVER_RPI */
  65. #define LOAD_FUNC(NAME) \
  66. _this->egl_data->NAME = SDL_LoadFunction(_this->egl_data->dll_handle, #NAME); \
  67. if (!_this->egl_data->NAME) \
  68. { \
  69. return SDL_SetError("Could not retrieve EGL function " #NAME); \
  70. }
  71. static const char * SDL_EGL_GetErrorName(EGLint eglErrorCode)
  72. {
  73. #define SDL_EGL_ERROR_TRANSLATE(e) case e: return #e;
  74. switch (eglErrorCode) {
  75. SDL_EGL_ERROR_TRANSLATE(EGL_SUCCESS);
  76. SDL_EGL_ERROR_TRANSLATE(EGL_NOT_INITIALIZED);
  77. SDL_EGL_ERROR_TRANSLATE(EGL_BAD_ACCESS);
  78. SDL_EGL_ERROR_TRANSLATE(EGL_BAD_ALLOC);
  79. SDL_EGL_ERROR_TRANSLATE(EGL_BAD_ATTRIBUTE);
  80. SDL_EGL_ERROR_TRANSLATE(EGL_BAD_CONTEXT);
  81. SDL_EGL_ERROR_TRANSLATE(EGL_BAD_CONFIG);
  82. SDL_EGL_ERROR_TRANSLATE(EGL_BAD_CURRENT_SURFACE);
  83. SDL_EGL_ERROR_TRANSLATE(EGL_BAD_DISPLAY);
  84. SDL_EGL_ERROR_TRANSLATE(EGL_BAD_SURFACE);
  85. SDL_EGL_ERROR_TRANSLATE(EGL_BAD_MATCH);
  86. SDL_EGL_ERROR_TRANSLATE(EGL_BAD_PARAMETER);
  87. SDL_EGL_ERROR_TRANSLATE(EGL_BAD_NATIVE_PIXMAP);
  88. SDL_EGL_ERROR_TRANSLATE(EGL_BAD_NATIVE_WINDOW);
  89. SDL_EGL_ERROR_TRANSLATE(EGL_CONTEXT_LOST);
  90. }
  91. return "";
  92. }
  93. int SDL_EGL_SetErrorEx(const char * message, const char * eglFunctionName, EGLint eglErrorCode)
  94. {
  95. const char * errorText = SDL_EGL_GetErrorName(eglErrorCode);
  96. char altErrorText[32];
  97. if (errorText[0] == '\0') {
  98. /* An unknown-to-SDL error code was reported. Report its hexadecimal value, instead of its name. */
  99. SDL_snprintf(altErrorText, SDL_arraysize(altErrorText), "0x%x", (unsigned int)eglErrorCode);
  100. errorText = altErrorText;
  101. }
  102. return SDL_SetError("%s (call to %s failed, reporting an error of %s)", message, eglFunctionName, errorText);
  103. }
  104. /* EGL implementation of SDL OpenGL ES support */
  105. #ifdef EGL_KHR_create_context
  106. static int SDL_EGL_HasExtension(_THIS, const char *ext)
  107. {
  108. int i;
  109. int len = 0;
  110. size_t ext_len;
  111. const char *exts;
  112. const char *ext_word;
  113. ext_len = SDL_strlen(ext);
  114. exts = _this->egl_data->eglQueryString(_this->egl_data->egl_display, EGL_EXTENSIONS);
  115. if (exts) {
  116. ext_word = exts;
  117. for (i = 0; exts[i] != 0; i++) {
  118. if (exts[i] == ' ') {
  119. if (ext_len == len && !SDL_strncmp(ext_word, ext, len)) {
  120. return 1;
  121. }
  122. len = 0;
  123. ext_word = &exts[i + 1];
  124. } else {
  125. len++;
  126. }
  127. }
  128. }
  129. return 0;
  130. }
  131. #endif /* EGL_KHR_create_context */
  132. void *
  133. SDL_EGL_GetProcAddress(_THIS, const char *proc)
  134. {
  135. static char procname[1024];
  136. void *retval;
  137. /* eglGetProcAddress is busted on Android http://code.google.com/p/android/issues/detail?id=7681 */
  138. #if !defined(SDL_VIDEO_DRIVER_ANDROID) && !defined(SDL_VIDEO_DRIVER_MIR)
  139. if (_this->egl_data->eglGetProcAddress) {
  140. retval = _this->egl_data->eglGetProcAddress(proc);
  141. if (retval) {
  142. return retval;
  143. }
  144. }
  145. #endif
  146. retval = SDL_LoadFunction(_this->egl_data->egl_dll_handle, proc);
  147. if (!retval && SDL_strlen(proc) <= 1022) {
  148. procname[0] = '_';
  149. SDL_strlcpy(procname + 1, proc, 1022);
  150. retval = SDL_LoadFunction(_this->egl_data->egl_dll_handle, procname);
  151. }
  152. return retval;
  153. }
  154. void
  155. SDL_EGL_UnloadLibrary(_THIS)
  156. {
  157. if (_this->egl_data) {
  158. if (_this->egl_data->egl_display) {
  159. _this->egl_data->eglTerminate(_this->egl_data->egl_display);
  160. _this->egl_data->egl_display = NULL;
  161. }
  162. if (_this->egl_data->dll_handle) {
  163. SDL_UnloadObject(_this->egl_data->dll_handle);
  164. _this->egl_data->dll_handle = NULL;
  165. }
  166. if (_this->egl_data->egl_dll_handle) {
  167. SDL_UnloadObject(_this->egl_data->egl_dll_handle);
  168. _this->egl_data->egl_dll_handle = NULL;
  169. }
  170. SDL_free(_this->egl_data);
  171. _this->egl_data = NULL;
  172. }
  173. }
  174. int
  175. SDL_EGL_LoadLibrary(_THIS, const char *egl_path, NativeDisplayType native_display)
  176. {
  177. void *dll_handle = NULL, *egl_dll_handle = NULL; /* The naming is counter intuitive, but hey, I just work here -- Gabriel */
  178. const char *path = NULL;
  179. #if SDL_VIDEO_DRIVER_WINDOWS || SDL_VIDEO_DRIVER_WINRT
  180. const char *d3dcompiler;
  181. #endif
  182. if (_this->egl_data) {
  183. return SDL_SetError("OpenGL ES context already created");
  184. }
  185. _this->egl_data = (struct SDL_EGL_VideoData *) SDL_calloc(1, sizeof(SDL_EGL_VideoData));
  186. if (!_this->egl_data) {
  187. return SDL_OutOfMemory();
  188. }
  189. #if SDL_VIDEO_DRIVER_WINDOWS || SDL_VIDEO_DRIVER_WINRT
  190. d3dcompiler = SDL_GetHint(SDL_HINT_VIDEO_WIN_D3DCOMPILER);
  191. if (!d3dcompiler) {
  192. if (WIN_IsWindowsVistaOrGreater()) {
  193. d3dcompiler = "d3dcompiler_46.dll";
  194. } else {
  195. d3dcompiler = "d3dcompiler_43.dll";
  196. }
  197. }
  198. if (SDL_strcasecmp(d3dcompiler, "none") != 0) {
  199. SDL_LoadObject(d3dcompiler);
  200. }
  201. #endif
  202. /* A funny thing, loading EGL.so first does not work on the Raspberry, so we load libGL* first */
  203. path = SDL_getenv("SDL_VIDEO_GL_DRIVER");
  204. if (path != NULL) {
  205. egl_dll_handle = SDL_LoadObject(path);
  206. }
  207. if (egl_dll_handle == NULL) {
  208. if (_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES) {
  209. if (_this->gl_config.major_version > 1) {
  210. path = DEFAULT_OGL_ES2;
  211. egl_dll_handle = SDL_LoadObject(path);
  212. } else {
  213. path = DEFAULT_OGL_ES;
  214. egl_dll_handle = SDL_LoadObject(path);
  215. if (egl_dll_handle == NULL) {
  216. path = DEFAULT_OGL_ES_PVR;
  217. egl_dll_handle = SDL_LoadObject(path);
  218. }
  219. }
  220. }
  221. #ifdef DEFAULT_OGL
  222. else {
  223. path = DEFAULT_OGL;
  224. egl_dll_handle = SDL_LoadObject(path);
  225. }
  226. #endif
  227. }
  228. _this->egl_data->egl_dll_handle = egl_dll_handle;
  229. if (egl_dll_handle == NULL) {
  230. return SDL_SetError("Could not initialize OpenGL / GLES library");
  231. }
  232. /* Loading libGL* in the previous step took care of loading libEGL.so, but we future proof by double checking */
  233. if (egl_path != NULL) {
  234. dll_handle = SDL_LoadObject(egl_path);
  235. }
  236. /* Try loading a EGL symbol, if it does not work try the default library paths */
  237. if (dll_handle == NULL || SDL_LoadFunction(dll_handle, "eglChooseConfig") == NULL) {
  238. if (dll_handle != NULL) {
  239. SDL_UnloadObject(dll_handle);
  240. }
  241. path = SDL_getenv("SDL_VIDEO_EGL_DRIVER");
  242. if (path == NULL) {
  243. path = DEFAULT_EGL;
  244. }
  245. dll_handle = SDL_LoadObject(path);
  246. if (dll_handle == NULL || SDL_LoadFunction(dll_handle, "eglChooseConfig") == NULL) {
  247. if (dll_handle != NULL) {
  248. SDL_UnloadObject(dll_handle);
  249. }
  250. return SDL_SetError("Could not load EGL library");
  251. }
  252. SDL_ClearError();
  253. }
  254. _this->egl_data->dll_handle = dll_handle;
  255. /* Load new function pointers */
  256. LOAD_FUNC(eglGetDisplay);
  257. LOAD_FUNC(eglInitialize);
  258. LOAD_FUNC(eglTerminate);
  259. LOAD_FUNC(eglGetProcAddress);
  260. LOAD_FUNC(eglChooseConfig);
  261. LOAD_FUNC(eglGetConfigAttrib);
  262. LOAD_FUNC(eglCreateContext);
  263. LOAD_FUNC(eglDestroyContext);
  264. LOAD_FUNC(eglCreateWindowSurface);
  265. LOAD_FUNC(eglDestroySurface);
  266. LOAD_FUNC(eglMakeCurrent);
  267. LOAD_FUNC(eglSwapBuffers);
  268. LOAD_FUNC(eglSwapInterval);
  269. LOAD_FUNC(eglWaitNative);
  270. LOAD_FUNC(eglWaitGL);
  271. LOAD_FUNC(eglBindAPI);
  272. LOAD_FUNC(eglQueryString);
  273. LOAD_FUNC(eglGetError);
  274. #if !defined(__WINRT__)
  275. _this->egl_data->egl_display = _this->egl_data->eglGetDisplay(native_display);
  276. if (!_this->egl_data->egl_display) {
  277. return SDL_SetError("Could not get EGL display");
  278. }
  279. if (_this->egl_data->eglInitialize(_this->egl_data->egl_display, NULL, NULL) != EGL_TRUE) {
  280. return SDL_SetError("Could not initialize EGL");
  281. }
  282. #endif
  283. if (path) {
  284. SDL_strlcpy(_this->gl_config.driver_path, path, sizeof(_this->gl_config.driver_path) - 1);
  285. } else {
  286. *_this->gl_config.driver_path = '\0';
  287. }
  288. return 0;
  289. }
  290. int
  291. SDL_EGL_ChooseConfig(_THIS)
  292. {
  293. /* 64 seems nice. */
  294. EGLint attribs[64];
  295. EGLint found_configs = 0, value;
  296. /* 128 seems even nicer here */
  297. EGLConfig configs[128];
  298. int i, j, best_bitdiff = -1, bitdiff;
  299. if (!_this->egl_data) {
  300. /* The EGL library wasn't loaded, SDL_GetError() should have info */
  301. return -1;
  302. }
  303. /* Get a valid EGL configuration */
  304. i = 0;
  305. attribs[i++] = EGL_RED_SIZE;
  306. attribs[i++] = _this->gl_config.red_size;
  307. attribs[i++] = EGL_GREEN_SIZE;
  308. attribs[i++] = _this->gl_config.green_size;
  309. attribs[i++] = EGL_BLUE_SIZE;
  310. attribs[i++] = _this->gl_config.blue_size;
  311. if (_this->gl_config.alpha_size) {
  312. attribs[i++] = EGL_ALPHA_SIZE;
  313. attribs[i++] = _this->gl_config.alpha_size;
  314. }
  315. if (_this->gl_config.buffer_size) {
  316. attribs[i++] = EGL_BUFFER_SIZE;
  317. attribs[i++] = _this->gl_config.buffer_size;
  318. }
  319. attribs[i++] = EGL_DEPTH_SIZE;
  320. attribs[i++] = _this->gl_config.depth_size;
  321. if (_this->gl_config.stencil_size) {
  322. attribs[i++] = EGL_STENCIL_SIZE;
  323. attribs[i++] = _this->gl_config.stencil_size;
  324. }
  325. if (_this->gl_config.multisamplebuffers) {
  326. attribs[i++] = EGL_SAMPLE_BUFFERS;
  327. attribs[i++] = _this->gl_config.multisamplebuffers;
  328. }
  329. if (_this->gl_config.multisamplesamples) {
  330. attribs[i++] = EGL_SAMPLES;
  331. attribs[i++] = _this->gl_config.multisamplesamples;
  332. }
  333. if (_this->gl_config.framebuffer_srgb_capable) {
  334. #ifdef EGL_KHR_gl_colorspace
  335. if (SDL_EGL_HasExtension(_this, "EGL_KHR_gl_colorspace")) {
  336. attribs[i++] = EGL_GL_COLORSPACE_KHR;
  337. attribs[i++] = EGL_GL_COLORSPACE_SRGB_KHR;
  338. } else
  339. #endif
  340. {
  341. return SDL_SetError("EGL implementation does not support sRGB system framebuffers");
  342. }
  343. }
  344. attribs[i++] = EGL_RENDERABLE_TYPE;
  345. if (_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES) {
  346. #ifdef EGL_KHR_create_context
  347. if (_this->gl_config.major_version >= 3 &&
  348. SDL_EGL_HasExtension(_this, "EGL_KHR_create_context")) {
  349. attribs[i++] = EGL_OPENGL_ES3_BIT_KHR;
  350. } else
  351. #endif
  352. if (_this->gl_config.major_version >= 2) {
  353. attribs[i++] = EGL_OPENGL_ES2_BIT;
  354. } else {
  355. attribs[i++] = EGL_OPENGL_ES_BIT;
  356. }
  357. _this->egl_data->eglBindAPI(EGL_OPENGL_ES_API);
  358. } else {
  359. attribs[i++] = EGL_OPENGL_BIT;
  360. _this->egl_data->eglBindAPI(EGL_OPENGL_API);
  361. }
  362. attribs[i++] = EGL_NONE;
  363. if (_this->egl_data->eglChooseConfig(_this->egl_data->egl_display,
  364. attribs,
  365. configs, SDL_arraysize(configs),
  366. &found_configs) == EGL_FALSE ||
  367. found_configs == 0) {
  368. return SDL_EGL_SetError("Couldn't find matching EGL config", "eglChooseConfig");
  369. }
  370. /* eglChooseConfig returns a number of configurations that match or exceed the requested attribs. */
  371. /* From those, we select the one that matches our requirements more closely via a makeshift algorithm */
  372. for (i = 0; i < found_configs; i++ ) {
  373. bitdiff = 0;
  374. for (j = 0; j < SDL_arraysize(attribs) - 1; j += 2) {
  375. if (attribs[j] == EGL_NONE) {
  376. break;
  377. }
  378. if ( attribs[j+1] != EGL_DONT_CARE && (
  379. attribs[j] == EGL_RED_SIZE ||
  380. attribs[j] == EGL_GREEN_SIZE ||
  381. attribs[j] == EGL_BLUE_SIZE ||
  382. attribs[j] == EGL_ALPHA_SIZE ||
  383. attribs[j] == EGL_DEPTH_SIZE ||
  384. attribs[j] == EGL_STENCIL_SIZE)) {
  385. _this->egl_data->eglGetConfigAttrib(_this->egl_data->egl_display, configs[i], attribs[j], &value);
  386. bitdiff += value - attribs[j + 1]; /* value is always >= attrib */
  387. }
  388. }
  389. if (bitdiff < best_bitdiff || best_bitdiff == -1) {
  390. _this->egl_data->egl_config = configs[i];
  391. best_bitdiff = bitdiff;
  392. }
  393. if (bitdiff == 0) {
  394. break; /* we found an exact match! */
  395. }
  396. }
  397. return 0;
  398. }
  399. SDL_GLContext
  400. SDL_EGL_CreateContext(_THIS, EGLSurface egl_surface)
  401. {
  402. /* max 14 values plus terminator. */
  403. EGLint attribs[15];
  404. int attr = 0;
  405. EGLContext egl_context, share_context = EGL_NO_CONTEXT;
  406. EGLint profile_mask = _this->gl_config.profile_mask;
  407. EGLint major_version = _this->gl_config.major_version;
  408. EGLint minor_version = _this->gl_config.minor_version;
  409. SDL_bool profile_es = (profile_mask == SDL_GL_CONTEXT_PROFILE_ES);
  410. if (!_this->egl_data) {
  411. /* The EGL library wasn't loaded, SDL_GetError() should have info */
  412. return NULL;
  413. }
  414. if (_this->gl_config.share_with_current_context) {
  415. share_context = (EGLContext)SDL_GL_GetCurrentContext();
  416. }
  417. /* Set the context version and other attributes. */
  418. if ((major_version < 3 || (minor_version == 0 && profile_es)) &&
  419. _this->gl_config.flags == 0 &&
  420. (profile_mask == 0 || profile_es)) {
  421. /* Create a context without using EGL_KHR_create_context attribs.
  422. * When creating a GLES context without EGL_KHR_create_context we can
  423. * only specify the major version. When creating a desktop GL context
  424. * we can't specify any version, so we only try in that case when the
  425. * version is less than 3.0 (matches SDL's GLX/WGL behavior.)
  426. */
  427. if (profile_es) {
  428. attribs[attr++] = EGL_CONTEXT_CLIENT_VERSION;
  429. attribs[attr++] = SDL_max(major_version, 1);
  430. }
  431. } else {
  432. #ifdef EGL_KHR_create_context
  433. /* The Major/minor version, context profiles, and context flags can
  434. * only be specified when this extension is available.
  435. */
  436. if (SDL_EGL_HasExtension(_this, "EGL_KHR_create_context")) {
  437. attribs[attr++] = EGL_CONTEXT_MAJOR_VERSION_KHR;
  438. attribs[attr++] = major_version;
  439. attribs[attr++] = EGL_CONTEXT_MINOR_VERSION_KHR;
  440. attribs[attr++] = minor_version;
  441. /* SDL profile bits match EGL profile bits. */
  442. if (profile_mask != 0 && profile_mask != SDL_GL_CONTEXT_PROFILE_ES) {
  443. attribs[attr++] = EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR;
  444. attribs[attr++] = profile_mask;
  445. }
  446. /* SDL flags match EGL flags. */
  447. if (_this->gl_config.flags != 0) {
  448. attribs[attr++] = EGL_CONTEXT_FLAGS_KHR;
  449. attribs[attr++] = _this->gl_config.flags;
  450. }
  451. } else
  452. #endif /* EGL_KHR_create_context */
  453. {
  454. SDL_SetError("Could not create EGL context (context attributes are not supported)");
  455. return NULL;
  456. }
  457. }
  458. attribs[attr++] = EGL_NONE;
  459. /* Bind the API */
  460. if (profile_es) {
  461. _this->egl_data->eglBindAPI(EGL_OPENGL_ES_API);
  462. } else {
  463. _this->egl_data->eglBindAPI(EGL_OPENGL_API);
  464. }
  465. egl_context = _this->egl_data->eglCreateContext(_this->egl_data->egl_display,
  466. _this->egl_data->egl_config,
  467. share_context, attribs);
  468. if (egl_context == EGL_NO_CONTEXT) {
  469. SDL_EGL_SetError("Could not create EGL context", "eglCreateContext");
  470. return NULL;
  471. }
  472. _this->egl_data->egl_swapinterval = 0;
  473. if (SDL_EGL_MakeCurrent(_this, egl_surface, egl_context) < 0) {
  474. /* Save the SDL error set by SDL_EGL_MakeCurrent */
  475. char errorText[1024];
  476. SDL_strlcpy(errorText, SDL_GetError(), SDL_arraysize(errorText));
  477. /* Delete the context, which may alter the value returned by SDL_GetError() */
  478. SDL_EGL_DeleteContext(_this, egl_context);
  479. /* Restore the SDL error */
  480. SDL_SetError("%s", errorText);
  481. return NULL;
  482. }
  483. return (SDL_GLContext) egl_context;
  484. }
  485. int
  486. SDL_EGL_MakeCurrent(_THIS, EGLSurface egl_surface, SDL_GLContext context)
  487. {
  488. EGLContext egl_context = (EGLContext) context;
  489. if (!_this->egl_data) {
  490. return SDL_SetError("OpenGL not initialized");
  491. }
  492. /* The android emulator crashes badly if you try to eglMakeCurrent
  493. * with a valid context and invalid surface, so we have to check for both here.
  494. */
  495. if (!egl_context || !egl_surface) {
  496. _this->egl_data->eglMakeCurrent(_this->egl_data->egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
  497. } else {
  498. if (!_this->egl_data->eglMakeCurrent(_this->egl_data->egl_display,
  499. egl_surface, egl_surface, egl_context)) {
  500. return SDL_EGL_SetError("Unable to make EGL context current", "eglMakeCurrent");
  501. }
  502. }
  503. return 0;
  504. }
  505. int
  506. SDL_EGL_SetSwapInterval(_THIS, int interval)
  507. {
  508. EGLBoolean status;
  509. if (!_this->egl_data) {
  510. return SDL_SetError("EGL not initialized");
  511. }
  512. status = _this->egl_data->eglSwapInterval(_this->egl_data->egl_display, interval);
  513. if (status == EGL_TRUE) {
  514. _this->egl_data->egl_swapinterval = interval;
  515. return 0;
  516. }
  517. return SDL_EGL_SetError("Unable to set the EGL swap interval", "eglSwapInterval");
  518. }
  519. int
  520. SDL_EGL_GetSwapInterval(_THIS)
  521. {
  522. if (!_this->egl_data) {
  523. SDL_SetError("EGL not initialized");
  524. return 0;
  525. }
  526. return _this->egl_data->egl_swapinterval;
  527. }
  528. int
  529. SDL_EGL_SwapBuffers(_THIS, EGLSurface egl_surface)
  530. {
  531. if (!_this->egl_data->eglSwapBuffers(_this->egl_data->egl_display, egl_surface)) {
  532. return SDL_EGL_SetError("unable to show color buffer in an OS-native window", "eglSwapBuffers");
  533. }
  534. return 0;
  535. }
  536. void
  537. SDL_EGL_DeleteContext(_THIS, SDL_GLContext context)
  538. {
  539. EGLContext egl_context = (EGLContext) context;
  540. /* Clean up GLES and EGL */
  541. if (!_this->egl_data) {
  542. return;
  543. }
  544. if (egl_context != NULL && egl_context != EGL_NO_CONTEXT) {
  545. SDL_EGL_MakeCurrent(_this, NULL, NULL);
  546. _this->egl_data->eglDestroyContext(_this->egl_data->egl_display, egl_context);
  547. }
  548. }
  549. EGLSurface *
  550. SDL_EGL_CreateSurface(_THIS, NativeWindowType nw)
  551. {
  552. EGLSurface * surface;
  553. if (SDL_EGL_ChooseConfig(_this) != 0) {
  554. return EGL_NO_SURFACE;
  555. }
  556. #if SDL_VIDEO_DRIVER_ANDROID
  557. {
  558. /* Android docs recommend doing this!
  559. * Ref: http://developer.android.com/reference/android/app/NativeActivity.html
  560. */
  561. EGLint format;
  562. _this->egl_data->eglGetConfigAttrib(_this->egl_data->egl_display,
  563. _this->egl_data->egl_config,
  564. EGL_NATIVE_VISUAL_ID, &format);
  565. ANativeWindow_setBuffersGeometry(nw, 0, 0, format);
  566. }
  567. #endif
  568. surface = _this->egl_data->eglCreateWindowSurface(
  569. _this->egl_data->egl_display,
  570. _this->egl_data->egl_config,
  571. nw, NULL);
  572. if (surface == EGL_NO_SURFACE) {
  573. SDL_EGL_SetError("unable to create an EGL window surface", "eglCreateWindowSurface");
  574. }
  575. return surface;
  576. }
  577. void
  578. SDL_EGL_DestroySurface(_THIS, EGLSurface egl_surface)
  579. {
  580. if (!_this->egl_data) {
  581. return;
  582. }
  583. if (egl_surface != EGL_NO_SURFACE) {
  584. _this->egl_data->eglDestroySurface(_this->egl_data->egl_display, egl_surface);
  585. }
  586. }
  587. #endif /* SDL_VIDEO_OPENGL_EGL */
  588. /* vi: set ts=4 sw=4 expandtab: */