SDL_egl.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. /*
  2. * Simple DirectMedia Layer
  3. * Copyright (C) 1997-2014 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. #include "SDL_sysvideo.h"
  24. #include "SDL_egl_c.h"
  25. #include "SDL_loadso.h"
  26. #include "SDL_hints.h"
  27. #if SDL_VIDEO_DRIVER_RPI
  28. /* Raspbian places the OpenGL ES/EGL binaries in a non standard path */
  29. #define DEFAULT_EGL "/opt/vc/lib/libEGL.so"
  30. #define DEFAULT_OGL_ES2 "/opt/vc/lib/libGLESv2.so"
  31. #define DEFAULT_OGL_ES_PVR "/opt/vc/lib/libGLES_CM.so"
  32. #define DEFAULT_OGL_ES "/opt/vc/lib/libGLESv1_CM.so"
  33. #elif SDL_VIDEO_DRIVER_ANDROID
  34. /* Android */
  35. #define DEFAULT_EGL "libEGL.so"
  36. #define DEFAULT_OGL_ES2 "libGLESv2.so"
  37. #define DEFAULT_OGL_ES_PVR "libGLES_CM.so"
  38. #define DEFAULT_OGL_ES "libGLESv1_CM.so"
  39. #elif SDL_VIDEO_DRIVER_WINDOWS || SDL_VIDEO_DRIVER_WINRT
  40. /* EGL AND OpenGL ES support via ANGLE */
  41. #define DEFAULT_EGL "libEGL.dll"
  42. #define DEFAULT_OGL_ES2 "libGLESv2.dll"
  43. #define DEFAULT_OGL_ES_PVR "libGLES_CM.dll"
  44. #define DEFAULT_OGL_ES "libGLESv1_CM.dll"
  45. #else
  46. /* Desktop Linux */
  47. #define DEFAULT_OGL "libGL.so.1"
  48. #define DEFAULT_EGL "libEGL.so.1"
  49. #define DEFAULT_OGL_ES2 "libGLESv2.so.2"
  50. #define DEFAULT_OGL_ES_PVR "libGLES_CM.so.1"
  51. #define DEFAULT_OGL_ES "libGLESv1_CM.so.1"
  52. #endif /* SDL_VIDEO_DRIVER_RPI */
  53. #define LOAD_FUNC(NAME) \
  54. *((void**)&_this->egl_data->NAME) = SDL_LoadFunction(_this->egl_data->dll_handle, #NAME); \
  55. if (!_this->egl_data->NAME) \
  56. { \
  57. return SDL_SetError("Could not retrieve EGL function " #NAME); \
  58. }
  59. /* EGL implementation of SDL OpenGL ES support */
  60. void *
  61. SDL_EGL_GetProcAddress(_THIS, const char *proc)
  62. {
  63. static char procname[1024];
  64. void *retval;
  65. /* eglGetProcAddress is busted on Android http://code.google.com/p/android/issues/detail?id=7681 */
  66. #if !defined(SDL_VIDEO_DRIVER_ANDROID)
  67. if (_this->egl_data->eglGetProcAddress) {
  68. retval = _this->egl_data->eglGetProcAddress(proc);
  69. if (retval) {
  70. return retval;
  71. }
  72. }
  73. #endif
  74. retval = SDL_LoadFunction(_this->egl_data->egl_dll_handle, proc);
  75. if (!retval && SDL_strlen(proc) <= 1022) {
  76. procname[0] = '_';
  77. SDL_strlcpy(procname + 1, proc, 1022);
  78. retval = SDL_LoadFunction(_this->egl_data->egl_dll_handle, procname);
  79. }
  80. return retval;
  81. }
  82. void
  83. SDL_EGL_UnloadLibrary(_THIS)
  84. {
  85. if (_this->egl_data) {
  86. if (_this->egl_data->egl_display) {
  87. _this->egl_data->eglTerminate(_this->egl_data->egl_display);
  88. _this->egl_data->egl_display = NULL;
  89. }
  90. if (_this->egl_data->dll_handle) {
  91. SDL_UnloadObject(_this->egl_data->dll_handle);
  92. _this->egl_data->dll_handle = NULL;
  93. }
  94. if (_this->egl_data->egl_dll_handle) {
  95. SDL_UnloadObject(_this->egl_data->egl_dll_handle);
  96. _this->egl_data->egl_dll_handle = NULL;
  97. }
  98. SDL_free(_this->egl_data);
  99. _this->egl_data = NULL;
  100. }
  101. }
  102. int
  103. SDL_EGL_LoadLibrary(_THIS, const char *egl_path, NativeDisplayType native_display)
  104. {
  105. void *dll_handle = NULL, *egl_dll_handle = NULL; /* The naming is counter intuitive, but hey, I just work here -- Gabriel */
  106. char *path = NULL;
  107. #if SDL_VIDEO_DRIVER_WINDOWS || SDL_VIDEO_DRIVER_WINRT
  108. const char *d3dcompiler;
  109. #endif
  110. if (_this->egl_data) {
  111. return SDL_SetError("OpenGL ES context already created");
  112. }
  113. _this->egl_data = (struct SDL_EGL_VideoData *) SDL_calloc(1, sizeof(SDL_EGL_VideoData));
  114. if (!_this->egl_data) {
  115. return SDL_OutOfMemory();
  116. }
  117. #if SDL_VIDEO_DRIVER_WINDOWS || SDL_VIDEO_DRIVER_WINRT
  118. d3dcompiler = SDL_GetHint(SDL_HINT_VIDEO_WIN_D3DCOMPILER);
  119. if (!d3dcompiler) {
  120. /* By default we load the Vista+ compatible compiler */
  121. d3dcompiler = "d3dcompiler_46.dll";
  122. }
  123. if (SDL_strcasecmp(d3dcompiler, "none") != 0) {
  124. SDL_LoadObject(d3dcompiler);
  125. }
  126. #endif
  127. /* A funny thing, loading EGL.so first does not work on the Raspberry, so we load libGL* first */
  128. path = SDL_getenv("SDL_VIDEO_GL_DRIVER");
  129. if (path != NULL) {
  130. egl_dll_handle = SDL_LoadObject(path);
  131. }
  132. if (egl_dll_handle == NULL) {
  133. if(_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES) {
  134. if (_this->gl_config.major_version > 1) {
  135. path = DEFAULT_OGL_ES2;
  136. egl_dll_handle = SDL_LoadObject(path);
  137. }
  138. else {
  139. path = DEFAULT_OGL_ES;
  140. egl_dll_handle = SDL_LoadObject(path);
  141. if (egl_dll_handle == NULL) {
  142. path = DEFAULT_OGL_ES_PVR;
  143. egl_dll_handle = SDL_LoadObject(path);
  144. }
  145. }
  146. }
  147. #ifdef DEFAULT_OGL
  148. else {
  149. path = DEFAULT_OGL;
  150. egl_dll_handle = SDL_LoadObject(path);
  151. }
  152. #endif
  153. }
  154. _this->egl_data->egl_dll_handle = egl_dll_handle;
  155. if (egl_dll_handle == NULL) {
  156. return SDL_SetError("Could not initialize OpenGL / GLES library");
  157. }
  158. /* Loading libGL* in the previous step took care of loading libEGL.so, but we future proof by double checking */
  159. if (egl_path != NULL) {
  160. dll_handle = SDL_LoadObject(egl_path);
  161. }
  162. /* Try loading a EGL symbol, if it does not work try the default library paths */
  163. if (SDL_LoadFunction(dll_handle, "eglChooseConfig") == NULL) {
  164. if (dll_handle != NULL) {
  165. SDL_UnloadObject(dll_handle);
  166. }
  167. path = SDL_getenv("SDL_VIDEO_EGL_DRIVER");
  168. if (path == NULL) {
  169. path = DEFAULT_EGL;
  170. }
  171. dll_handle = SDL_LoadObject(path);
  172. if (dll_handle == NULL) {
  173. return SDL_SetError("Could not load EGL library");
  174. }
  175. }
  176. _this->egl_data->dll_handle = dll_handle;
  177. /* Load new function pointers */
  178. LOAD_FUNC(eglGetDisplay);
  179. LOAD_FUNC(eglInitialize);
  180. LOAD_FUNC(eglTerminate);
  181. LOAD_FUNC(eglGetProcAddress);
  182. LOAD_FUNC(eglChooseConfig);
  183. LOAD_FUNC(eglGetConfigAttrib);
  184. LOAD_FUNC(eglCreateContext);
  185. LOAD_FUNC(eglDestroyContext);
  186. LOAD_FUNC(eglCreateWindowSurface);
  187. LOAD_FUNC(eglDestroySurface);
  188. LOAD_FUNC(eglMakeCurrent);
  189. LOAD_FUNC(eglSwapBuffers);
  190. LOAD_FUNC(eglSwapInterval);
  191. LOAD_FUNC(eglWaitNative);
  192. LOAD_FUNC(eglWaitGL);
  193. LOAD_FUNC(eglBindAPI);
  194. _this->egl_data->egl_display = _this->egl_data->eglGetDisplay(native_display);
  195. if (!_this->egl_data->egl_display) {
  196. return SDL_SetError("Could not get EGL display");
  197. }
  198. if (_this->egl_data->eglInitialize(_this->egl_data->egl_display, NULL, NULL) != EGL_TRUE) {
  199. return SDL_SetError("Could not initialize EGL");
  200. }
  201. _this->egl_data->dll_handle = dll_handle;
  202. _this->egl_data->egl_dll_handle = egl_dll_handle;
  203. _this->gl_config.driver_loaded = 1;
  204. if (path) {
  205. SDL_strlcpy(_this->gl_config.driver_path, path, sizeof(_this->gl_config.driver_path) - 1);
  206. } else {
  207. *_this->gl_config.driver_path = '\0';
  208. }
  209. return 0;
  210. }
  211. int
  212. SDL_EGL_ChooseConfig(_THIS)
  213. {
  214. /* 64 seems nice. */
  215. EGLint attribs[64];
  216. EGLint found_configs = 0, value;
  217. /* 128 seems even nicer here */
  218. EGLConfig configs[128];
  219. int i, j, best_bitdiff = -1, bitdiff;
  220. if (!_this->egl_data) {
  221. /* The EGL library wasn't loaded, SDL_GetError() should have info */
  222. return -1;
  223. }
  224. /* Get a valid EGL configuration */
  225. i = 0;
  226. attribs[i++] = EGL_RED_SIZE;
  227. attribs[i++] = _this->gl_config.red_size;
  228. attribs[i++] = EGL_GREEN_SIZE;
  229. attribs[i++] = _this->gl_config.green_size;
  230. attribs[i++] = EGL_BLUE_SIZE;
  231. attribs[i++] = _this->gl_config.blue_size;
  232. if (_this->gl_config.alpha_size) {
  233. attribs[i++] = EGL_ALPHA_SIZE;
  234. attribs[i++] = _this->gl_config.alpha_size;
  235. }
  236. if (_this->gl_config.buffer_size) {
  237. attribs[i++] = EGL_BUFFER_SIZE;
  238. attribs[i++] = _this->gl_config.buffer_size;
  239. }
  240. attribs[i++] = EGL_DEPTH_SIZE;
  241. attribs[i++] = _this->gl_config.depth_size;
  242. if (_this->gl_config.stencil_size) {
  243. attribs[i++] = EGL_STENCIL_SIZE;
  244. attribs[i++] = _this->gl_config.stencil_size;
  245. }
  246. if (_this->gl_config.multisamplebuffers) {
  247. attribs[i++] = EGL_SAMPLE_BUFFERS;
  248. attribs[i++] = _this->gl_config.multisamplebuffers;
  249. }
  250. if (_this->gl_config.multisamplesamples) {
  251. attribs[i++] = EGL_SAMPLES;
  252. attribs[i++] = _this->gl_config.multisamplesamples;
  253. }
  254. attribs[i++] = EGL_RENDERABLE_TYPE;
  255. if(_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES) {
  256. if (_this->gl_config.major_version == 2) {
  257. attribs[i++] = EGL_OPENGL_ES2_BIT;
  258. } else {
  259. attribs[i++] = EGL_OPENGL_ES_BIT;
  260. }
  261. _this->egl_data->eglBindAPI(EGL_OPENGL_ES_API);
  262. }
  263. else {
  264. attribs[i++] = EGL_OPENGL_BIT;
  265. _this->egl_data->eglBindAPI(EGL_OPENGL_API);
  266. }
  267. attribs[i++] = EGL_NONE;
  268. if (_this->egl_data->eglChooseConfig(_this->egl_data->egl_display,
  269. attribs,
  270. configs, SDL_arraysize(configs),
  271. &found_configs) == EGL_FALSE ||
  272. found_configs == 0) {
  273. return SDL_SetError("Couldn't find matching EGL config");
  274. }
  275. /* eglChooseConfig returns a number of configurations that match or exceed the requested attribs. */
  276. /* From those, we select the one that matches our requirements more closely via a makeshift algorithm */
  277. for ( i=0; i<found_configs; i++ ) {
  278. bitdiff = 0;
  279. for (j = 0; j < SDL_arraysize(attribs) - 1; j += 2) {
  280. if (attribs[j] == EGL_NONE) {
  281. break;
  282. }
  283. if ( attribs[j+1] != EGL_DONT_CARE && (
  284. attribs[j] == EGL_RED_SIZE ||
  285. attribs[j] == EGL_GREEN_SIZE ||
  286. attribs[j] == EGL_BLUE_SIZE ||
  287. attribs[j] == EGL_ALPHA_SIZE ||
  288. attribs[j] == EGL_DEPTH_SIZE ||
  289. attribs[j] == EGL_STENCIL_SIZE)) {
  290. _this->egl_data->eglGetConfigAttrib(_this->egl_data->egl_display, configs[i], attribs[j], &value);
  291. bitdiff += value - attribs[j + 1]; /* value is always >= attrib */
  292. }
  293. }
  294. if (bitdiff < best_bitdiff || best_bitdiff == -1) {
  295. _this->egl_data->egl_config = configs[i];
  296. best_bitdiff = bitdiff;
  297. }
  298. if (bitdiff == 0) break; /* we found an exact match! */
  299. }
  300. return 0;
  301. }
  302. SDL_GLContext
  303. SDL_EGL_CreateContext(_THIS, EGLSurface egl_surface)
  304. {
  305. EGLint context_attrib_list[] = {
  306. EGL_CONTEXT_CLIENT_VERSION,
  307. 1,
  308. EGL_NONE
  309. };
  310. EGLContext egl_context, share_context = EGL_NO_CONTEXT;
  311. if (!_this->egl_data) {
  312. /* The EGL library wasn't loaded, SDL_GetError() should have info */
  313. return NULL;
  314. }
  315. if (_this->gl_config.share_with_current_context) {
  316. share_context = (EGLContext)SDL_GL_GetCurrentContext();
  317. }
  318. /* Bind the API */
  319. if(_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES) {
  320. _this->egl_data->eglBindAPI(EGL_OPENGL_ES_API);
  321. if (_this->gl_config.major_version) {
  322. context_attrib_list[1] = _this->gl_config.major_version;
  323. }
  324. egl_context = _this->egl_data->eglCreateContext(_this->egl_data->egl_display,
  325. _this->egl_data->egl_config,
  326. share_context, context_attrib_list);
  327. }
  328. else {
  329. _this->egl_data->eglBindAPI(EGL_OPENGL_API);
  330. egl_context = _this->egl_data->eglCreateContext(_this->egl_data->egl_display,
  331. _this->egl_data->egl_config,
  332. share_context, NULL);
  333. }
  334. if (egl_context == EGL_NO_CONTEXT) {
  335. SDL_SetError("Could not create EGL context");
  336. return NULL;
  337. }
  338. _this->egl_data->egl_swapinterval = 0;
  339. if (SDL_EGL_MakeCurrent(_this, egl_surface, egl_context) < 0) {
  340. SDL_EGL_DeleteContext(_this, egl_context);
  341. SDL_SetError("Could not make EGL context current");
  342. return NULL;
  343. }
  344. return (SDL_GLContext) egl_context;
  345. }
  346. int
  347. SDL_EGL_MakeCurrent(_THIS, EGLSurface egl_surface, SDL_GLContext context)
  348. {
  349. EGLContext egl_context = (EGLContext) context;
  350. if (!_this->egl_data) {
  351. return SDL_SetError("OpenGL not initialized");
  352. }
  353. /* The android emulator crashes badly if you try to eglMakeCurrent
  354. * with a valid context and invalid surface, so we have to check for both here.
  355. */
  356. if (!egl_context || !egl_surface) {
  357. _this->egl_data->eglMakeCurrent(_this->egl_data->egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
  358. }
  359. else {
  360. if (!_this->egl_data->eglMakeCurrent(_this->egl_data->egl_display,
  361. egl_surface, egl_surface, egl_context)) {
  362. return SDL_SetError("Unable to make EGL context current");
  363. }
  364. }
  365. return 0;
  366. }
  367. int
  368. SDL_EGL_SetSwapInterval(_THIS, int interval)
  369. {
  370. EGLBoolean status;
  371. if (!_this->egl_data) {
  372. return SDL_SetError("EGL not initialized");
  373. }
  374. status = _this->egl_data->eglSwapInterval(_this->egl_data->egl_display, interval);
  375. if (status == EGL_TRUE) {
  376. _this->egl_data->egl_swapinterval = interval;
  377. return 0;
  378. }
  379. return SDL_SetError("Unable to set the EGL swap interval");
  380. }
  381. int
  382. SDL_EGL_GetSwapInterval(_THIS)
  383. {
  384. if (!_this->egl_data) {
  385. return SDL_SetError("EGL not initialized");
  386. }
  387. return _this->egl_data->egl_swapinterval;
  388. }
  389. void
  390. SDL_EGL_SwapBuffers(_THIS, EGLSurface egl_surface)
  391. {
  392. _this->egl_data->eglSwapBuffers(_this->egl_data->egl_display, egl_surface);
  393. }
  394. void
  395. SDL_EGL_DeleteContext(_THIS, SDL_GLContext context)
  396. {
  397. EGLContext egl_context = (EGLContext) context;
  398. /* Clean up GLES and EGL */
  399. if (!_this->egl_data) {
  400. return;
  401. }
  402. if (egl_context != NULL && egl_context != EGL_NO_CONTEXT) {
  403. SDL_EGL_MakeCurrent(_this, NULL, NULL);
  404. _this->egl_data->eglDestroyContext(_this->egl_data->egl_display, egl_context);
  405. }
  406. }
  407. EGLSurface *
  408. SDL_EGL_CreateSurface(_THIS, NativeWindowType nw)
  409. {
  410. if (SDL_EGL_ChooseConfig(_this) != 0) {
  411. return EGL_NO_SURFACE;
  412. }
  413. return _this->egl_data->eglCreateWindowSurface(
  414. _this->egl_data->egl_display,
  415. _this->egl_data->egl_config,
  416. nw, NULL);
  417. }
  418. void
  419. SDL_EGL_DestroySurface(_THIS, EGLSurface egl_surface)
  420. {
  421. if (!_this->egl_data) {
  422. return;
  423. }
  424. if (egl_surface != EGL_NO_SURFACE) {
  425. _this->egl_data->eglDestroySurface(_this->egl_data->egl_display, egl_surface);
  426. }
  427. }
  428. #endif /* SDL_VIDEO_OPENGL_EGL */
  429. /* vi: set ts=4 sw=4 expandtab: */