gl-egl-common.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. /******************************************************************************
  2. Copyright (C) 2020 by Georges Basile Stavracas Neto <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include "gl-egl-common.h"
  15. #include <fcntl.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <unistd.h>
  19. #include <xf86drm.h>
  20. #include <glad/glad_egl.h>
  21. #if defined(__linux__)
  22. #include <linux/types.h>
  23. #include <asm/ioctl.h>
  24. typedef unsigned int drm_handle_t;
  25. #else
  26. #include <stdint.h>
  27. #include <sys/ioccom.h>
  28. #include <sys/types.h>
  29. typedef int8_t __s8;
  30. typedef uint8_t __u8;
  31. typedef int16_t __s16;
  32. typedef uint16_t __u16;
  33. typedef int32_t __s32;
  34. typedef uint32_t __u32;
  35. typedef int64_t __s64;
  36. typedef uint64_t __u64;
  37. typedef size_t __kernel_size_t;
  38. typedef unsigned long drm_handle_t;
  39. #endif
  40. typedef void(APIENTRYP PFNGLEGLIMAGETARGETTEXTURE2DOESPROC)(GLenum target, GLeglImageOES image);
  41. static PFNGLEGLIMAGETARGETTEXTURE2DOESPROC glEGLImageTargetTexture2DOES;
  42. static bool find_gl_extension(const char *extension)
  43. {
  44. GLint n, i;
  45. glGetIntegerv(GL_NUM_EXTENSIONS, &n);
  46. for (i = 0; i < n; i++) {
  47. const char *e = (char *)glGetStringi(GL_EXTENSIONS, i);
  48. if (extension && strcmp(e, extension) == 0)
  49. return true;
  50. }
  51. return false;
  52. }
  53. static bool init_egl_image_target_texture_2d_ext(void)
  54. {
  55. static bool initialized = false;
  56. if (!initialized) {
  57. initialized = true;
  58. if (!find_gl_extension("GL_OES_EGL_image")) {
  59. blog(LOG_ERROR, "No GL_OES_EGL_image");
  60. return false;
  61. }
  62. glEGLImageTargetTexture2DOES =
  63. (PFNGLEGLIMAGETARGETTEXTURE2DOESPROC)eglGetProcAddress("glEGLImageTargetTexture2DOES");
  64. }
  65. if (!glEGLImageTargetTexture2DOES)
  66. return false;
  67. return true;
  68. }
  69. static EGLImageKHR create_dmabuf_egl_image(EGLDisplay egl_display, unsigned int width, unsigned int height,
  70. uint32_t drm_format, uint32_t n_planes, const int *fds,
  71. const uint32_t *strides, const uint32_t *offsets, const uint64_t *modifiers)
  72. {
  73. EGLAttrib attribs[47];
  74. int atti = 0;
  75. /* This requires the Mesa commit in
  76. * Mesa 10.3 (08264e5dad4df448e7718e782ad9077902089a07) or
  77. * Mesa 10.2.7 (55d28925e6109a4afd61f109e845a8a51bd17652).
  78. * Otherwise Mesa closes the fd behind our back and re-importing
  79. * will fail.
  80. * https://bugs.freedesktop.org/show_bug.cgi?id=76188
  81. * */
  82. attribs[atti++] = EGL_WIDTH;
  83. attribs[atti++] = width;
  84. attribs[atti++] = EGL_HEIGHT;
  85. attribs[atti++] = height;
  86. attribs[atti++] = EGL_LINUX_DRM_FOURCC_EXT;
  87. attribs[atti++] = drm_format;
  88. if (n_planes > 0) {
  89. attribs[atti++] = EGL_DMA_BUF_PLANE0_FD_EXT;
  90. attribs[atti++] = fds[0];
  91. attribs[atti++] = EGL_DMA_BUF_PLANE0_OFFSET_EXT;
  92. attribs[atti++] = offsets[0];
  93. attribs[atti++] = EGL_DMA_BUF_PLANE0_PITCH_EXT;
  94. attribs[atti++] = strides[0];
  95. if (modifiers) {
  96. attribs[atti++] = EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT;
  97. attribs[atti++] = modifiers[0] & 0xFFFFFFFF;
  98. attribs[atti++] = EGL_DMA_BUF_PLANE0_MODIFIER_HI_EXT;
  99. attribs[atti++] = modifiers[0] >> 32;
  100. }
  101. }
  102. if (n_planes > 1) {
  103. attribs[atti++] = EGL_DMA_BUF_PLANE1_FD_EXT;
  104. attribs[atti++] = fds[1];
  105. attribs[atti++] = EGL_DMA_BUF_PLANE1_OFFSET_EXT;
  106. attribs[atti++] = offsets[1];
  107. attribs[atti++] = EGL_DMA_BUF_PLANE1_PITCH_EXT;
  108. attribs[atti++] = strides[1];
  109. if (modifiers) {
  110. attribs[atti++] = EGL_DMA_BUF_PLANE1_MODIFIER_LO_EXT;
  111. attribs[atti++] = modifiers[1] & 0xFFFFFFFF;
  112. attribs[atti++] = EGL_DMA_BUF_PLANE1_MODIFIER_HI_EXT;
  113. attribs[atti++] = modifiers[1] >> 32;
  114. }
  115. }
  116. if (n_planes > 2) {
  117. attribs[atti++] = EGL_DMA_BUF_PLANE2_FD_EXT;
  118. attribs[atti++] = fds[2];
  119. attribs[atti++] = EGL_DMA_BUF_PLANE2_OFFSET_EXT;
  120. attribs[atti++] = offsets[2];
  121. attribs[atti++] = EGL_DMA_BUF_PLANE2_PITCH_EXT;
  122. attribs[atti++] = strides[2];
  123. if (modifiers) {
  124. attribs[atti++] = EGL_DMA_BUF_PLANE2_MODIFIER_LO_EXT;
  125. attribs[atti++] = modifiers[2] & 0xFFFFFFFF;
  126. attribs[atti++] = EGL_DMA_BUF_PLANE2_MODIFIER_HI_EXT;
  127. attribs[atti++] = modifiers[2] >> 32;
  128. }
  129. }
  130. if (n_planes > 3) {
  131. attribs[atti++] = EGL_DMA_BUF_PLANE3_FD_EXT;
  132. attribs[atti++] = fds[3];
  133. attribs[atti++] = EGL_DMA_BUF_PLANE3_OFFSET_EXT;
  134. attribs[atti++] = offsets[3];
  135. attribs[atti++] = EGL_DMA_BUF_PLANE3_PITCH_EXT;
  136. attribs[atti++] = strides[3];
  137. if (modifiers) {
  138. attribs[atti++] = EGL_DMA_BUF_PLANE3_MODIFIER_LO_EXT;
  139. attribs[atti++] = modifiers[3] & 0xFFFFFFFF;
  140. attribs[atti++] = EGL_DMA_BUF_PLANE3_MODIFIER_HI_EXT;
  141. attribs[atti++] = modifiers[3] >> 32;
  142. }
  143. }
  144. attribs[atti++] = EGL_NONE;
  145. return eglCreateImage(egl_display, EGL_NO_CONTEXT, EGL_LINUX_DMA_BUF_EXT, 0, attribs);
  146. }
  147. struct gs_texture *gl_egl_create_texture_from_eglimage(EGLDisplay egl_display, uint32_t width, uint32_t height,
  148. enum gs_color_format color_format, EGLint target, EGLImage image)
  149. {
  150. UNUSED_PARAMETER(egl_display);
  151. UNUSED_PARAMETER(target);
  152. struct gs_texture *texture = NULL;
  153. texture = gs_texture_create(width, height, color_format, 1, NULL, GS_GL_DUMMYTEX | GS_RENDER_TARGET);
  154. const GLuint gltex = *(GLuint *)gs_texture_get_obj(texture);
  155. gl_bind_texture(GL_TEXTURE_2D, gltex);
  156. gl_tex_param_i(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  157. gl_tex_param_i(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  158. glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, image);
  159. if (!gl_success("glEGLImageTargetTexture2DOES")) {
  160. gs_texture_destroy(texture);
  161. texture = NULL;
  162. }
  163. gl_bind_texture(GL_TEXTURE_2D, 0);
  164. return texture;
  165. }
  166. bool gl_egl_enum_adapters(EGLDisplay display, bool (*callback)(void *param, const char *name, uint32_t id), void *param)
  167. {
  168. EGLDeviceEXT display_dev;
  169. if (eglQueryDisplayAttribEXT(display, EGL_DEVICE_EXT, (EGLAttrib *)&display_dev) &&
  170. eglGetError() == EGL_SUCCESS) {
  171. const char *display_node = eglQueryDeviceStringEXT(display_dev, EGL_DRM_RENDER_NODE_FILE_EXT);
  172. if (eglGetError() != EGL_SUCCESS || display_node == NULL) {
  173. display_node = "/Software";
  174. }
  175. if (!callback(param, display_node, 0)) {
  176. return true;
  177. }
  178. }
  179. EGLint num_devices = 0;
  180. EGLDeviceEXT devices[32];
  181. if (!eglQueryDevicesEXT(32, devices, &num_devices)) {
  182. eglGetError();
  183. return true;
  184. }
  185. for (int i = 0; i < num_devices; i++) {
  186. const char *node = eglQueryDeviceStringEXT(devices[i], EGL_DRM_RENDER_NODE_FILE_EXT);
  187. if (node == NULL || eglGetError() != EGL_SUCCESS) {
  188. // Do not enumerate additional software renderers.
  189. continue;
  190. }
  191. if (!callback(param, node, i + 1)) {
  192. return true;
  193. }
  194. }
  195. return true;
  196. }
  197. uint32_t gs_get_adapter_count()
  198. {
  199. EGLint num_devices = 0;
  200. eglQueryDevicesEXT(0, NULL, &num_devices);
  201. return 1 + num_devices; // Display + devices.
  202. }
  203. struct gs_texture *gl_egl_create_dmabuf_image(EGLDisplay egl_display, unsigned int width, unsigned int height,
  204. uint32_t drm_format, enum gs_color_format color_format, uint32_t n_planes,
  205. const int *fds, const uint32_t *strides, const uint32_t *offsets,
  206. const uint64_t *modifiers)
  207. {
  208. struct gs_texture *texture = NULL;
  209. EGLImage egl_image;
  210. if (!init_egl_image_target_texture_2d_ext())
  211. return NULL;
  212. egl_image = create_dmabuf_egl_image(egl_display, width, height, drm_format, n_planes, fds, strides, offsets,
  213. modifiers);
  214. if (egl_image == EGL_NO_IMAGE) {
  215. blog(LOG_ERROR, "Cannot create EGLImage: %s", gl_egl_error_to_string(eglGetError()));
  216. return NULL;
  217. }
  218. texture =
  219. gl_egl_create_texture_from_eglimage(egl_display, width, height, color_format, GL_TEXTURE_2D, egl_image);
  220. if (texture)
  221. eglDestroyImage(egl_display, egl_image);
  222. return texture;
  223. }
  224. struct gs_texture *gl_egl_create_texture_from_pixmap(EGLDisplay egl_display, uint32_t width, uint32_t height,
  225. enum gs_color_format color_format, EGLint target,
  226. EGLClientBuffer pixmap)
  227. {
  228. if (!init_egl_image_target_texture_2d_ext())
  229. return NULL;
  230. const EGLAttrib pixmap_attrs[] = {
  231. EGL_IMAGE_PRESERVED_KHR,
  232. EGL_TRUE,
  233. EGL_NONE,
  234. };
  235. EGLImage image = eglCreateImage(egl_display, EGL_NO_CONTEXT, EGL_NATIVE_PIXMAP_KHR, pixmap, pixmap_attrs);
  236. if (image == EGL_NO_IMAGE) {
  237. blog(LOG_ERROR, "Cannot create EGLImage: %s", gl_egl_error_to_string(eglGetError()));
  238. return NULL;
  239. }
  240. struct gs_texture *texture =
  241. gl_egl_create_texture_from_eglimage(egl_display, width, height, color_format, target, image);
  242. eglDestroyImage(egl_display, image);
  243. return texture;
  244. }
  245. static inline bool is_implicit_dmabuf_modifiers_supported(void)
  246. {
  247. return EGL_EXT_image_dma_buf_import > 0;
  248. }
  249. static inline bool query_dmabuf_formats(EGLDisplay egl_display, EGLint **formats, EGLint *num_formats)
  250. {
  251. EGLint max_formats = 0;
  252. if (!glad_eglQueryDmaBufFormatsEXT(egl_display, 0, NULL, &max_formats)) {
  253. blog(LOG_ERROR, "Cannot query the number of formats: %s", gl_egl_error_to_string(eglGetError()));
  254. return false;
  255. }
  256. if (max_formats != 0) {
  257. EGLint *format_list = bzalloc(max_formats * sizeof(EGLint));
  258. if (!format_list) {
  259. blog(LOG_ERROR, "Unable to allocate memory");
  260. return false;
  261. }
  262. if (!glad_eglQueryDmaBufFormatsEXT(egl_display, max_formats, format_list, &max_formats)) {
  263. blog(LOG_ERROR, "Cannot query a list of formats: %s", gl_egl_error_to_string(eglGetError()));
  264. bfree(format_list);
  265. return false;
  266. }
  267. *formats = format_list;
  268. }
  269. *num_formats = max_formats;
  270. return true;
  271. }
  272. bool gl_egl_query_dmabuf_capabilities(EGLDisplay egl_display, enum gs_dmabuf_flags *dmabuf_flags, uint32_t **formats,
  273. size_t *n_formats)
  274. {
  275. bool ret = false;
  276. if (is_implicit_dmabuf_modifiers_supported()) {
  277. *dmabuf_flags = GS_DMABUF_FLAG_IMPLICIT_MODIFIERS_SUPPORTED;
  278. ret = true;
  279. }
  280. if (!glad_eglQueryDmaBufFormatsEXT) {
  281. blog(LOG_ERROR, "Unable to load eglQueryDmaBufFormatsEXT");
  282. return ret;
  283. }
  284. if (!query_dmabuf_formats(egl_display, (EGLint **)formats, (EGLint *)n_formats)) {
  285. *n_formats = 0;
  286. *formats = NULL;
  287. }
  288. return ret;
  289. }
  290. static inline bool query_dmabuf_modifiers(EGLDisplay egl_display, EGLint drm_format, EGLuint64KHR **modifiers,
  291. EGLuint64KHR *n_modifiers)
  292. {
  293. EGLint max_modifiers;
  294. if (!glad_eglQueryDmaBufModifiersEXT(egl_display, drm_format, 0, NULL, NULL, &max_modifiers)) {
  295. blog(LOG_ERROR, "Cannot query the number of modifiers: %s", gl_egl_error_to_string(eglGetError()));
  296. return false;
  297. }
  298. if (max_modifiers != 0) {
  299. EGLuint64KHR *modifier_list = bzalloc(max_modifiers * sizeof(EGLuint64KHR));
  300. EGLBoolean *external_only = NULL;
  301. if (!modifier_list) {
  302. blog(LOG_ERROR, "Unable to allocate memory");
  303. return false;
  304. }
  305. if (!glad_eglQueryDmaBufModifiersEXT(egl_display, drm_format, max_modifiers, modifier_list,
  306. external_only, &max_modifiers)) {
  307. blog(LOG_ERROR, "Cannot query a list of modifiers: %s", gl_egl_error_to_string(eglGetError()));
  308. bfree(modifier_list);
  309. return false;
  310. }
  311. *modifiers = modifier_list;
  312. }
  313. *n_modifiers = (EGLuint64KHR)max_modifiers;
  314. return true;
  315. }
  316. bool gl_egl_query_dmabuf_modifiers_for_format(EGLDisplay egl_display, uint32_t drm_format, uint64_t **modifiers,
  317. size_t *n_modifiers)
  318. {
  319. if (!glad_eglQueryDmaBufModifiersEXT) {
  320. blog(LOG_ERROR, "Unable to load eglQueryDmaBufModifiersEXT");
  321. return false;
  322. }
  323. if (!query_dmabuf_modifiers(egl_display, drm_format, modifiers, n_modifiers)) {
  324. *n_modifiers = 0;
  325. *modifiers = NULL;
  326. return false;
  327. }
  328. return true;
  329. }
  330. bool gl_egl_query_sync_capabilities(int drm_fd)
  331. {
  332. uint64_t syncobjCap = 0;
  333. uint64_t syncobjTimelineCap = 0;
  334. drmGetCap(drm_fd, DRM_CAP_SYNCOBJ, &syncobjCap);
  335. drmGetCap(drm_fd, DRM_CAP_SYNCOBJ_TIMELINE, &syncobjTimelineCap);
  336. return syncobjCap && syncobjTimelineCap && (EGL_ANDROID_native_fence_sync > 0);
  337. }
  338. gs_sync_t *gl_egl_create_sync(EGLDisplay egl_display)
  339. {
  340. gs_sync_t *sync = eglCreateSync(egl_display, EGL_SYNC_NATIVE_FENCE_ANDROID, NULL);
  341. glFlush();
  342. return sync;
  343. }
  344. gs_sync_t *gl_egl_create_sync_from_syncobj_timeline_point(EGLDisplay egl_display, int drm_fd, int syncobj_fd,
  345. uint64_t timeline_point)
  346. {
  347. EGLSync sync;
  348. int syncfile_fd = -1;
  349. uint32_t syncobj_handle = 0;
  350. uint32_t temp_handle = 0;
  351. drmSyncobjFDToHandle(drm_fd, syncobj_fd, &syncobj_handle);
  352. drmSyncobjTimelineWait(drm_fd, &syncobj_handle, &timeline_point, 1, INT64_MAX,
  353. DRM_SYNCOBJ_WAIT_FLAGS_WAIT_AVAILABLE, NULL);
  354. drmSyncobjCreate(drm_fd, 0, &temp_handle);
  355. drmSyncobjTransfer(drm_fd, temp_handle, 0, syncobj_handle, timeline_point, 0);
  356. drmSyncobjExportSyncFile(drm_fd, temp_handle, &syncfile_fd);
  357. drmSyncobjDestroy(drm_fd, temp_handle);
  358. drmSyncobjDestroy(drm_fd, syncobj_handle);
  359. const EGLAttrib attributes[] = {EGL_SYNC_NATIVE_FENCE_FD_ANDROID, syncfile_fd, EGL_NONE};
  360. sync = eglCreateSync(egl_display, EGL_SYNC_NATIVE_FENCE_ANDROID, attributes);
  361. if (sync == EGL_NO_SYNC) {
  362. blog(LOG_ERROR, "Unable to create an EGLSync object from a syncfile fd");
  363. close(syncfile_fd);
  364. }
  365. return sync;
  366. }
  367. bool gl_egl_sync_export_syncobj_timeline_point(EGLDisplay egl_display, gs_sync_t *sync, int drm_fd, int syncobj_fd,
  368. uint64_t timeline_point)
  369. {
  370. uint32_t syncobj_handle = 0;
  371. uint32_t temp_handle = 0;
  372. int gs_sync_fd = eglDupNativeFenceFDANDROID(egl_display, sync);
  373. if (gs_sync_fd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
  374. return false;
  375. }
  376. drmSyncobjFDToHandle(drm_fd, syncobj_fd, &syncobj_handle);
  377. drmSyncobjCreate(drm_fd, 0, &temp_handle);
  378. drmSyncobjImportSyncFile(drm_fd, temp_handle, gs_sync_fd);
  379. drmSyncobjTransfer(drm_fd, syncobj_handle, timeline_point, temp_handle, 0, 0);
  380. drmSyncobjDestroy(drm_fd, temp_handle);
  381. drmSyncobjDestroy(drm_fd, syncobj_handle);
  382. close(gs_sync_fd);
  383. return true;
  384. }
  385. bool gl_egl_sync_signal_syncobj_timeline_point(int drm_fd, int syncobj_fd, uint64_t timeline_point)
  386. {
  387. uint32_t syncobj_handle = 0;
  388. bool result;
  389. drmSyncobjFDToHandle(drm_fd, syncobj_fd, &syncobj_handle);
  390. result = drmSyncobjTimelineSignal(drm_fd, &syncobj_handle, &timeline_point, 1) == 0;
  391. drmSyncobjDestroy(drm_fd, syncobj_handle);
  392. return result;
  393. }
  394. void gl_egl_device_sync_destroy(EGLDisplay egl_display, gs_sync_t *sync)
  395. {
  396. eglDestroySync(egl_display, sync);
  397. }
  398. bool gl_egl_sync_wait(EGLDisplay egl_display, gs_sync_t *sync)
  399. {
  400. return eglWaitSync(egl_display, sync, 0);
  401. }
  402. const char *gl_egl_error_to_string(EGLint error_number)
  403. {
  404. switch (error_number) {
  405. case EGL_SUCCESS:
  406. return "The last function succeeded without error.";
  407. break;
  408. case EGL_NOT_INITIALIZED:
  409. return "EGL is not initialized, or could not be initialized, for the specified EGL display connection.";
  410. break;
  411. case EGL_BAD_ACCESS:
  412. return "EGL cannot access a requested resource (for example a context is bound in another thread).";
  413. break;
  414. case EGL_BAD_ALLOC:
  415. return "EGL failed to allocate resources for the requested operation.";
  416. break;
  417. case EGL_BAD_ATTRIBUTE:
  418. return "An unrecognized attribute or attribute value was passed in the attribute list.";
  419. break;
  420. case EGL_BAD_CONTEXT:
  421. return "An EGLContext argument does not name a valid EGL rendering context.";
  422. break;
  423. case EGL_BAD_CONFIG:
  424. return "An EGLConfig argument does not name a valid EGL frame buffer configuration.";
  425. break;
  426. case EGL_BAD_CURRENT_SURFACE:
  427. return "The current surface of the calling thread is a window, pixel buffer or pixmap that is no longer valid.";
  428. break;
  429. case EGL_BAD_DISPLAY:
  430. return "An EGLDisplay argument does not name a valid EGL display connection.";
  431. break;
  432. case EGL_BAD_SURFACE:
  433. return "An EGLSurface argument does not name a valid surface (window, pixel buffer or pixmap) configured for GL rendering.";
  434. break;
  435. case EGL_BAD_MATCH:
  436. return "Arguments are inconsistent (for example, a valid context requires buffers not supplied by a valid surface).";
  437. break;
  438. case EGL_BAD_PARAMETER:
  439. return "One or more argument values are invalid.";
  440. break;
  441. case EGL_BAD_NATIVE_PIXMAP:
  442. return "A NativePixmapType argument does not refer to a valid native pixmap.";
  443. break;
  444. case EGL_BAD_NATIVE_WINDOW:
  445. return "A NativeWindowType argument does not refer to a valid native window.";
  446. break;
  447. case EGL_CONTEXT_LOST:
  448. return "A power management event has occurred. The application must destroy all contexts and reinitialise OpenGL ES state and objects to continue rendering. ";
  449. break;
  450. default:
  451. return "Unknown error";
  452. break;
  453. }
  454. }
  455. int get_drm_render_node_fd(EGLDisplay egl_display)
  456. {
  457. EGLAttrib attrib;
  458. EGLDeviceEXT device;
  459. const char *drm_render_node_path;
  460. if (eglQueryDisplayAttribEXT(egl_display, EGL_DEVICE_EXT, &attrib) != EGL_TRUE) {
  461. return -1;
  462. }
  463. device = (EGLDeviceEXT)attrib;
  464. drm_render_node_path = eglQueryDeviceStringEXT(device, EGL_DRM_RENDER_NODE_FILE_EXT);
  465. if (drm_render_node_path == NULL) {
  466. return -1;
  467. }
  468. return open(drm_render_node_path, O_RDWR | O_CLOEXEC);
  469. }
  470. void close_drm_render_node_fd(int fd)
  471. {
  472. close(fd);
  473. }