egl.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /**
  2. * SPDX-License-Identifier: (WTFPL OR CC0-1.0) AND Apache-2.0
  3. */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <glad/egl.h>
  8. #ifndef GLAD_IMPL_UTIL_C_
  9. #define GLAD_IMPL_UTIL_C_
  10. #ifdef _MSC_VER
  11. #define GLAD_IMPL_UTIL_SSCANF sscanf_s
  12. #else
  13. #define GLAD_IMPL_UTIL_SSCANF sscanf
  14. #endif
  15. #endif /* GLAD_IMPL_UTIL_C_ */
  16. #ifdef __cplusplus
  17. extern "C" {
  18. #endif
  19. int GLAD_EGL_VERSION_1_0 = 0;
  20. int GLAD_EGL_VERSION_1_1 = 0;
  21. int GLAD_EGL_VERSION_1_2 = 0;
  22. int GLAD_EGL_VERSION_1_3 = 0;
  23. int GLAD_EGL_VERSION_1_4 = 0;
  24. int GLAD_EGL_VERSION_1_5 = 0;
  25. int GLAD_EGL_ANDROID_blob_cache = 0;
  26. int GLAD_EGL_KHR_platform_wayland = 0;
  27. int GLAD_EGL_KHR_platform_x11 = 0;
  28. PFNEGLBINDAPIPROC glad_eglBindAPI = NULL;
  29. PFNEGLBINDTEXIMAGEPROC glad_eglBindTexImage = NULL;
  30. PFNEGLCHOOSECONFIGPROC glad_eglChooseConfig = NULL;
  31. PFNEGLCLIENTWAITSYNCPROC glad_eglClientWaitSync = NULL;
  32. PFNEGLCOPYBUFFERSPROC glad_eglCopyBuffers = NULL;
  33. PFNEGLCREATECONTEXTPROC glad_eglCreateContext = NULL;
  34. PFNEGLCREATEIMAGEPROC glad_eglCreateImage = NULL;
  35. PFNEGLCREATEPBUFFERFROMCLIENTBUFFERPROC glad_eglCreatePbufferFromClientBuffer = NULL;
  36. PFNEGLCREATEPBUFFERSURFACEPROC glad_eglCreatePbufferSurface = NULL;
  37. PFNEGLCREATEPIXMAPSURFACEPROC glad_eglCreatePixmapSurface = NULL;
  38. PFNEGLCREATEPLATFORMPIXMAPSURFACEPROC glad_eglCreatePlatformPixmapSurface = NULL;
  39. PFNEGLCREATEPLATFORMWINDOWSURFACEPROC glad_eglCreatePlatformWindowSurface = NULL;
  40. PFNEGLCREATESYNCPROC glad_eglCreateSync = NULL;
  41. PFNEGLCREATEWINDOWSURFACEPROC glad_eglCreateWindowSurface = NULL;
  42. PFNEGLDESTROYCONTEXTPROC glad_eglDestroyContext = NULL;
  43. PFNEGLDESTROYIMAGEPROC glad_eglDestroyImage = NULL;
  44. PFNEGLDESTROYSURFACEPROC glad_eglDestroySurface = NULL;
  45. PFNEGLDESTROYSYNCPROC glad_eglDestroySync = NULL;
  46. PFNEGLGETCONFIGATTRIBPROC glad_eglGetConfigAttrib = NULL;
  47. PFNEGLGETCONFIGSPROC glad_eglGetConfigs = NULL;
  48. PFNEGLGETCURRENTCONTEXTPROC glad_eglGetCurrentContext = NULL;
  49. PFNEGLGETCURRENTDISPLAYPROC glad_eglGetCurrentDisplay = NULL;
  50. PFNEGLGETCURRENTSURFACEPROC glad_eglGetCurrentSurface = NULL;
  51. PFNEGLGETDISPLAYPROC glad_eglGetDisplay = NULL;
  52. PFNEGLGETERRORPROC glad_eglGetError = NULL;
  53. PFNEGLGETPLATFORMDISPLAYPROC glad_eglGetPlatformDisplay = NULL;
  54. PFNEGLGETPROCADDRESSPROC glad_eglGetProcAddress = NULL;
  55. PFNEGLGETSYNCATTRIBPROC glad_eglGetSyncAttrib = NULL;
  56. PFNEGLINITIALIZEPROC glad_eglInitialize = NULL;
  57. PFNEGLMAKECURRENTPROC glad_eglMakeCurrent = NULL;
  58. PFNEGLQUERYAPIPROC glad_eglQueryAPI = NULL;
  59. PFNEGLQUERYCONTEXTPROC glad_eglQueryContext = NULL;
  60. PFNEGLQUERYSTRINGPROC glad_eglQueryString = NULL;
  61. PFNEGLQUERYSURFACEPROC glad_eglQuerySurface = NULL;
  62. PFNEGLRELEASETEXIMAGEPROC glad_eglReleaseTexImage = NULL;
  63. PFNEGLRELEASETHREADPROC glad_eglReleaseThread = NULL;
  64. PFNEGLSETBLOBCACHEFUNCSANDROIDPROC glad_eglSetBlobCacheFuncsANDROID = NULL;
  65. PFNEGLSURFACEATTRIBPROC glad_eglSurfaceAttrib = NULL;
  66. PFNEGLSWAPBUFFERSPROC glad_eglSwapBuffers = NULL;
  67. PFNEGLSWAPINTERVALPROC glad_eglSwapInterval = NULL;
  68. PFNEGLTERMINATEPROC glad_eglTerminate = NULL;
  69. PFNEGLWAITCLIENTPROC glad_eglWaitClient = NULL;
  70. PFNEGLWAITGLPROC glad_eglWaitGL = NULL;
  71. PFNEGLWAITNATIVEPROC glad_eglWaitNative = NULL;
  72. PFNEGLWAITSYNCPROC glad_eglWaitSync = NULL;
  73. static void glad_egl_load_EGL_VERSION_1_0( GLADuserptrloadfunc load, void* userptr) {
  74. if(!GLAD_EGL_VERSION_1_0) return;
  75. glad_eglChooseConfig = (PFNEGLCHOOSECONFIGPROC) load(userptr, "eglChooseConfig");
  76. glad_eglCopyBuffers = (PFNEGLCOPYBUFFERSPROC) load(userptr, "eglCopyBuffers");
  77. glad_eglCreateContext = (PFNEGLCREATECONTEXTPROC) load(userptr, "eglCreateContext");
  78. glad_eglCreatePbufferSurface = (PFNEGLCREATEPBUFFERSURFACEPROC) load(userptr, "eglCreatePbufferSurface");
  79. glad_eglCreatePixmapSurface = (PFNEGLCREATEPIXMAPSURFACEPROC) load(userptr, "eglCreatePixmapSurface");
  80. glad_eglCreateWindowSurface = (PFNEGLCREATEWINDOWSURFACEPROC) load(userptr, "eglCreateWindowSurface");
  81. glad_eglDestroyContext = (PFNEGLDESTROYCONTEXTPROC) load(userptr, "eglDestroyContext");
  82. glad_eglDestroySurface = (PFNEGLDESTROYSURFACEPROC) load(userptr, "eglDestroySurface");
  83. glad_eglGetConfigAttrib = (PFNEGLGETCONFIGATTRIBPROC) load(userptr, "eglGetConfigAttrib");
  84. glad_eglGetConfigs = (PFNEGLGETCONFIGSPROC) load(userptr, "eglGetConfigs");
  85. glad_eglGetCurrentDisplay = (PFNEGLGETCURRENTDISPLAYPROC) load(userptr, "eglGetCurrentDisplay");
  86. glad_eglGetCurrentSurface = (PFNEGLGETCURRENTSURFACEPROC) load(userptr, "eglGetCurrentSurface");
  87. glad_eglGetDisplay = (PFNEGLGETDISPLAYPROC) load(userptr, "eglGetDisplay");
  88. glad_eglGetError = (PFNEGLGETERRORPROC) load(userptr, "eglGetError");
  89. glad_eglGetProcAddress = (PFNEGLGETPROCADDRESSPROC) load(userptr, "eglGetProcAddress");
  90. glad_eglInitialize = (PFNEGLINITIALIZEPROC) load(userptr, "eglInitialize");
  91. glad_eglMakeCurrent = (PFNEGLMAKECURRENTPROC) load(userptr, "eglMakeCurrent");
  92. glad_eglQueryContext = (PFNEGLQUERYCONTEXTPROC) load(userptr, "eglQueryContext");
  93. glad_eglQueryString = (PFNEGLQUERYSTRINGPROC) load(userptr, "eglQueryString");
  94. glad_eglQuerySurface = (PFNEGLQUERYSURFACEPROC) load(userptr, "eglQuerySurface");
  95. glad_eglSwapBuffers = (PFNEGLSWAPBUFFERSPROC) load(userptr, "eglSwapBuffers");
  96. glad_eglTerminate = (PFNEGLTERMINATEPROC) load(userptr, "eglTerminate");
  97. glad_eglWaitGL = (PFNEGLWAITGLPROC) load(userptr, "eglWaitGL");
  98. glad_eglWaitNative = (PFNEGLWAITNATIVEPROC) load(userptr, "eglWaitNative");
  99. }
  100. static void glad_egl_load_EGL_VERSION_1_1( GLADuserptrloadfunc load, void* userptr) {
  101. if(!GLAD_EGL_VERSION_1_1) return;
  102. glad_eglBindTexImage = (PFNEGLBINDTEXIMAGEPROC) load(userptr, "eglBindTexImage");
  103. glad_eglReleaseTexImage = (PFNEGLRELEASETEXIMAGEPROC) load(userptr, "eglReleaseTexImage");
  104. glad_eglSurfaceAttrib = (PFNEGLSURFACEATTRIBPROC) load(userptr, "eglSurfaceAttrib");
  105. glad_eglSwapInterval = (PFNEGLSWAPINTERVALPROC) load(userptr, "eglSwapInterval");
  106. }
  107. static void glad_egl_load_EGL_VERSION_1_2( GLADuserptrloadfunc load, void* userptr) {
  108. if(!GLAD_EGL_VERSION_1_2) return;
  109. glad_eglBindAPI = (PFNEGLBINDAPIPROC) load(userptr, "eglBindAPI");
  110. glad_eglCreatePbufferFromClientBuffer = (PFNEGLCREATEPBUFFERFROMCLIENTBUFFERPROC) load(userptr, "eglCreatePbufferFromClientBuffer");
  111. glad_eglQueryAPI = (PFNEGLQUERYAPIPROC) load(userptr, "eglQueryAPI");
  112. glad_eglReleaseThread = (PFNEGLRELEASETHREADPROC) load(userptr, "eglReleaseThread");
  113. glad_eglWaitClient = (PFNEGLWAITCLIENTPROC) load(userptr, "eglWaitClient");
  114. }
  115. static void glad_egl_load_EGL_VERSION_1_4( GLADuserptrloadfunc load, void* userptr) {
  116. if(!GLAD_EGL_VERSION_1_4) return;
  117. glad_eglGetCurrentContext = (PFNEGLGETCURRENTCONTEXTPROC) load(userptr, "eglGetCurrentContext");
  118. }
  119. static void glad_egl_load_EGL_VERSION_1_5( GLADuserptrloadfunc load, void* userptr) {
  120. if(!GLAD_EGL_VERSION_1_5) return;
  121. glad_eglClientWaitSync = (PFNEGLCLIENTWAITSYNCPROC) load(userptr, "eglClientWaitSync");
  122. glad_eglCreateImage = (PFNEGLCREATEIMAGEPROC) load(userptr, "eglCreateImage");
  123. glad_eglCreatePlatformPixmapSurface = (PFNEGLCREATEPLATFORMPIXMAPSURFACEPROC) load(userptr, "eglCreatePlatformPixmapSurface");
  124. glad_eglCreatePlatformWindowSurface = (PFNEGLCREATEPLATFORMWINDOWSURFACEPROC) load(userptr, "eglCreatePlatformWindowSurface");
  125. glad_eglCreateSync = (PFNEGLCREATESYNCPROC) load(userptr, "eglCreateSync");
  126. glad_eglDestroyImage = (PFNEGLDESTROYIMAGEPROC) load(userptr, "eglDestroyImage");
  127. glad_eglDestroySync = (PFNEGLDESTROYSYNCPROC) load(userptr, "eglDestroySync");
  128. glad_eglGetPlatformDisplay = (PFNEGLGETPLATFORMDISPLAYPROC) load(userptr, "eglGetPlatformDisplay");
  129. glad_eglGetSyncAttrib = (PFNEGLGETSYNCATTRIBPROC) load(userptr, "eglGetSyncAttrib");
  130. glad_eglWaitSync = (PFNEGLWAITSYNCPROC) load(userptr, "eglWaitSync");
  131. }
  132. static void glad_egl_load_EGL_ANDROID_blob_cache( GLADuserptrloadfunc load, void* userptr) {
  133. if(!GLAD_EGL_ANDROID_blob_cache) return;
  134. glad_eglSetBlobCacheFuncsANDROID = (PFNEGLSETBLOBCACHEFUNCSANDROIDPROC) load(userptr, "eglSetBlobCacheFuncsANDROID");
  135. }
  136. static int glad_egl_get_extensions(EGLDisplay display, const char **extensions) {
  137. *extensions = eglQueryString(display, EGL_EXTENSIONS);
  138. return extensions != NULL;
  139. }
  140. static int glad_egl_has_extension(const char *extensions, const char *ext) {
  141. const char *loc;
  142. const char *terminator;
  143. if(extensions == NULL) {
  144. return 0;
  145. }
  146. while(1) {
  147. loc = strstr(extensions, ext);
  148. if(loc == NULL) {
  149. return 0;
  150. }
  151. terminator = loc + strlen(ext);
  152. if((loc == extensions || *(loc - 1) == ' ') &&
  153. (*terminator == ' ' || *terminator == '\0')) {
  154. return 1;
  155. }
  156. extensions = terminator;
  157. }
  158. }
  159. static GLADapiproc glad_egl_get_proc_from_userptr(void *userptr, const char *name) {
  160. return (GLAD_GNUC_EXTENSION (GLADapiproc (*)(const char *name)) userptr)(name);
  161. }
  162. static int glad_egl_find_extensions_egl(EGLDisplay display) {
  163. const char *extensions;
  164. if (!glad_egl_get_extensions(display, &extensions)) return 0;
  165. GLAD_EGL_ANDROID_blob_cache = glad_egl_has_extension(extensions, "EGL_ANDROID_blob_cache");
  166. GLAD_EGL_KHR_platform_wayland = glad_egl_has_extension(extensions, "EGL_KHR_platform_wayland");
  167. GLAD_EGL_KHR_platform_x11 = glad_egl_has_extension(extensions, "EGL_KHR_platform_x11");
  168. return 1;
  169. }
  170. static int glad_egl_find_core_egl(EGLDisplay display) {
  171. int major, minor;
  172. const char *version;
  173. if (display == NULL) {
  174. display = EGL_NO_DISPLAY; /* this is usually NULL, better safe than sorry */
  175. }
  176. if (display == EGL_NO_DISPLAY) {
  177. display = eglGetCurrentDisplay();
  178. }
  179. #ifdef EGL_VERSION_1_4
  180. if (display == EGL_NO_DISPLAY) {
  181. display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
  182. }
  183. #endif
  184. #ifndef EGL_VERSION_1_5
  185. if (display == EGL_NO_DISPLAY) {
  186. return 0;
  187. }
  188. #endif
  189. version = eglQueryString(display, EGL_VERSION);
  190. (void) eglGetError();
  191. if (version == NULL) {
  192. major = 1;
  193. minor = 0;
  194. } else {
  195. GLAD_IMPL_UTIL_SSCANF(version, "%d.%d", &major, &minor);
  196. }
  197. GLAD_EGL_VERSION_1_0 = (major == 1 && minor >= 0) || major > 1;
  198. GLAD_EGL_VERSION_1_1 = (major == 1 && minor >= 1) || major > 1;
  199. GLAD_EGL_VERSION_1_2 = (major == 1 && minor >= 2) || major > 1;
  200. GLAD_EGL_VERSION_1_3 = (major == 1 && minor >= 3) || major > 1;
  201. GLAD_EGL_VERSION_1_4 = (major == 1 && minor >= 4) || major > 1;
  202. GLAD_EGL_VERSION_1_5 = (major == 1 && minor >= 5) || major > 1;
  203. return GLAD_MAKE_VERSION(major, minor);
  204. }
  205. int gladLoadEGLUserPtr(EGLDisplay display, GLADuserptrloadfunc load, void* userptr) {
  206. int version;
  207. eglGetDisplay = (PFNEGLGETDISPLAYPROC) load(userptr, "eglGetDisplay");
  208. eglGetCurrentDisplay = (PFNEGLGETCURRENTDISPLAYPROC) load(userptr, "eglGetCurrentDisplay");
  209. eglQueryString = (PFNEGLQUERYSTRINGPROC) load(userptr, "eglQueryString");
  210. eglGetError = (PFNEGLGETERRORPROC) load(userptr, "eglGetError");
  211. if (eglGetDisplay == NULL || eglGetCurrentDisplay == NULL || eglQueryString == NULL || eglGetError == NULL) return 0;
  212. version = glad_egl_find_core_egl(display);
  213. if (!version) return 0;
  214. glad_egl_load_EGL_VERSION_1_0(load, userptr);
  215. glad_egl_load_EGL_VERSION_1_1(load, userptr);
  216. glad_egl_load_EGL_VERSION_1_2(load, userptr);
  217. glad_egl_load_EGL_VERSION_1_4(load, userptr);
  218. glad_egl_load_EGL_VERSION_1_5(load, userptr);
  219. if (!glad_egl_find_extensions_egl(display)) return 0;
  220. glad_egl_load_EGL_ANDROID_blob_cache(load, userptr);
  221. return version;
  222. }
  223. int gladLoadEGL(EGLDisplay display, GLADloadfunc load) {
  224. return gladLoadEGLUserPtr(display, glad_egl_get_proc_from_userptr, GLAD_GNUC_EXTENSION (void*) load);
  225. }
  226. #ifdef GLAD_EGL
  227. #ifndef GLAD_LOADER_LIBRARY_C_
  228. #define GLAD_LOADER_LIBRARY_C_
  229. #include <stddef.h>
  230. #include <stdlib.h>
  231. #if GLAD_PLATFORM_WIN32
  232. #include <windows.h>
  233. #else
  234. #include <dlfcn.h>
  235. #endif
  236. static void* glad_get_dlopen_handle(const char *lib_names[], int length) {
  237. void *handle = NULL;
  238. int i;
  239. for (i = 0; i < length; ++i) {
  240. #if GLAD_PLATFORM_WIN32
  241. #if GLAD_PLATFORM_UWP
  242. size_t buffer_size = (strlen(lib_names[i]) + 1) * sizeof(WCHAR);
  243. LPWSTR buffer = (LPWSTR) malloc(buffer_size);
  244. if (buffer != NULL) {
  245. int ret = MultiByteToWideChar(CP_ACP, 0, lib_names[i], -1, buffer, buffer_size);
  246. if (ret != 0) {
  247. handle = (void*) LoadPackagedLibrary(buffer, 0);
  248. }
  249. free((void*) buffer);
  250. }
  251. #else
  252. handle = (void*) LoadLibraryA(lib_names[i]);
  253. #endif
  254. #else
  255. handle = dlopen(lib_names[i], RTLD_LAZY | RTLD_LOCAL);
  256. #endif
  257. if (handle != NULL) {
  258. return handle;
  259. }
  260. }
  261. return NULL;
  262. }
  263. static void glad_close_dlopen_handle(void* handle) {
  264. if (handle != NULL) {
  265. #if GLAD_PLATFORM_WIN32
  266. FreeLibrary((HMODULE) handle);
  267. #else
  268. dlclose(handle);
  269. #endif
  270. }
  271. }
  272. static GLADapiproc glad_dlsym_handle(void* handle, const char *name) {
  273. if (handle == NULL) {
  274. return NULL;
  275. }
  276. #if GLAD_PLATFORM_WIN32
  277. return (GLADapiproc) GetProcAddress((HMODULE) handle, name);
  278. #else
  279. return GLAD_GNUC_EXTENSION (GLADapiproc) dlsym(handle, name);
  280. #endif
  281. }
  282. #endif /* GLAD_LOADER_LIBRARY_C_ */
  283. struct _glad_egl_userptr {
  284. void *handle;
  285. PFNEGLGETPROCADDRESSPROC get_proc_address_ptr;
  286. };
  287. static GLADapiproc glad_egl_get_proc(void *vuserptr, const char* name) {
  288. struct _glad_egl_userptr userptr = *(struct _glad_egl_userptr*) vuserptr;
  289. GLADapiproc result = NULL;
  290. result = glad_dlsym_handle(userptr.handle, name);
  291. if (result == NULL) {
  292. result = GLAD_GNUC_EXTENSION (GLADapiproc) userptr.get_proc_address_ptr(name);
  293. }
  294. return result;
  295. }
  296. static void* _egl_handle = NULL;
  297. static void* glad_egl_dlopen_handle(void) {
  298. #if GLAD_PLATFORM_APPLE
  299. static const char *NAMES[] = {"libEGL.dylib"};
  300. #elif GLAD_PLATFORM_WIN32
  301. static const char *NAMES[] = {"libEGL.dll", "EGL.dll"};
  302. #else
  303. static const char *NAMES[] = {"libEGL.so.1", "libEGL.so"};
  304. #endif
  305. if (_egl_handle == NULL) {
  306. _egl_handle = glad_get_dlopen_handle(NAMES, sizeof(NAMES) / sizeof(NAMES[0]));
  307. }
  308. return _egl_handle;
  309. }
  310. static struct _glad_egl_userptr glad_egl_build_userptr(void *handle) {
  311. struct _glad_egl_userptr userptr;
  312. userptr.handle = handle;
  313. userptr.get_proc_address_ptr = (PFNEGLGETPROCADDRESSPROC) glad_dlsym_handle(handle, "eglGetProcAddress");
  314. return userptr;
  315. }
  316. int gladLoaderLoadEGL(EGLDisplay display) {
  317. int version = 0;
  318. void *handle = NULL;
  319. int did_load = 0;
  320. struct _glad_egl_userptr userptr;
  321. did_load = _egl_handle == NULL;
  322. handle = glad_egl_dlopen_handle();
  323. if (handle != NULL) {
  324. userptr = glad_egl_build_userptr(handle);
  325. if (userptr.get_proc_address_ptr != NULL) {
  326. version = gladLoadEGLUserPtr(display, glad_egl_get_proc, &userptr);
  327. }
  328. if (!version && did_load) {
  329. gladLoaderUnloadEGL();
  330. }
  331. }
  332. return version;
  333. }
  334. void gladLoaderUnloadEGL() {
  335. if (_egl_handle != NULL) {
  336. glad_close_dlopen_handle(_egl_handle);
  337. _egl_handle = NULL;
  338. }
  339. }
  340. #endif /* GLAD_EGL */
  341. #ifdef __cplusplus
  342. }
  343. #endif