gl-egl-common.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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 <stdio.h>
  16. #include <stdlib.h>
  17. #include <glad/glad_egl.h>
  18. #if defined(__linux__)
  19. #include <linux/types.h>
  20. #include <asm/ioctl.h>
  21. typedef unsigned int drm_handle_t;
  22. #else
  23. #include <stdint.h>
  24. #include <sys/ioccom.h>
  25. #include <sys/types.h>
  26. typedef int8_t __s8;
  27. typedef uint8_t __u8;
  28. typedef int16_t __s16;
  29. typedef uint16_t __u16;
  30. typedef int32_t __s32;
  31. typedef uint32_t __u32;
  32. typedef int64_t __s64;
  33. typedef uint64_t __u64;
  34. typedef size_t __kernel_size_t;
  35. typedef unsigned long drm_handle_t;
  36. #endif
  37. typedef void(APIENTRYP PFNGLEGLIMAGETARGETTEXTURE2DOESPROC)(GLenum target, GLeglImageOES image);
  38. static PFNGLEGLIMAGETARGETTEXTURE2DOESPROC glEGLImageTargetTexture2DOES;
  39. static bool find_gl_extension(const char *extension)
  40. {
  41. GLint n, i;
  42. glGetIntegerv(GL_NUM_EXTENSIONS, &n);
  43. for (i = 0; i < n; i++) {
  44. const char *e = (char *)glGetStringi(GL_EXTENSIONS, i);
  45. if (extension && strcmp(e, extension) == 0)
  46. return true;
  47. }
  48. return false;
  49. }
  50. static bool init_egl_image_target_texture_2d_ext(void)
  51. {
  52. static bool initialized = false;
  53. if (!initialized) {
  54. initialized = true;
  55. if (!find_gl_extension("GL_OES_EGL_image")) {
  56. blog(LOG_ERROR, "No GL_OES_EGL_image");
  57. return false;
  58. }
  59. glEGLImageTargetTexture2DOES =
  60. (PFNGLEGLIMAGETARGETTEXTURE2DOESPROC)eglGetProcAddress("glEGLImageTargetTexture2DOES");
  61. }
  62. if (!glEGLImageTargetTexture2DOES)
  63. return false;
  64. return true;
  65. }
  66. static EGLImageKHR create_dmabuf_egl_image(EGLDisplay egl_display, unsigned int width, unsigned int height,
  67. uint32_t drm_format, uint32_t n_planes, const int *fds,
  68. const uint32_t *strides, const uint32_t *offsets, const uint64_t *modifiers)
  69. {
  70. EGLAttrib attribs[47];
  71. int atti = 0;
  72. /* This requires the Mesa commit in
  73. * Mesa 10.3 (08264e5dad4df448e7718e782ad9077902089a07) or
  74. * Mesa 10.2.7 (55d28925e6109a4afd61f109e845a8a51bd17652).
  75. * Otherwise Mesa closes the fd behind our back and re-importing
  76. * will fail.
  77. * https://bugs.freedesktop.org/show_bug.cgi?id=76188
  78. * */
  79. attribs[atti++] = EGL_WIDTH;
  80. attribs[atti++] = width;
  81. attribs[atti++] = EGL_HEIGHT;
  82. attribs[atti++] = height;
  83. attribs[atti++] = EGL_LINUX_DRM_FOURCC_EXT;
  84. attribs[atti++] = drm_format;
  85. if (n_planes > 0) {
  86. attribs[atti++] = EGL_DMA_BUF_PLANE0_FD_EXT;
  87. attribs[atti++] = fds[0];
  88. attribs[atti++] = EGL_DMA_BUF_PLANE0_OFFSET_EXT;
  89. attribs[atti++] = offsets[0];
  90. attribs[atti++] = EGL_DMA_BUF_PLANE0_PITCH_EXT;
  91. attribs[atti++] = strides[0];
  92. if (modifiers) {
  93. attribs[atti++] = EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT;
  94. attribs[atti++] = modifiers[0] & 0xFFFFFFFF;
  95. attribs[atti++] = EGL_DMA_BUF_PLANE0_MODIFIER_HI_EXT;
  96. attribs[atti++] = modifiers[0] >> 32;
  97. }
  98. }
  99. if (n_planes > 1) {
  100. attribs[atti++] = EGL_DMA_BUF_PLANE1_FD_EXT;
  101. attribs[atti++] = fds[1];
  102. attribs[atti++] = EGL_DMA_BUF_PLANE1_OFFSET_EXT;
  103. attribs[atti++] = offsets[1];
  104. attribs[atti++] = EGL_DMA_BUF_PLANE1_PITCH_EXT;
  105. attribs[atti++] = strides[1];
  106. if (modifiers) {
  107. attribs[atti++] = EGL_DMA_BUF_PLANE1_MODIFIER_LO_EXT;
  108. attribs[atti++] = modifiers[1] & 0xFFFFFFFF;
  109. attribs[atti++] = EGL_DMA_BUF_PLANE1_MODIFIER_HI_EXT;
  110. attribs[atti++] = modifiers[1] >> 32;
  111. }
  112. }
  113. if (n_planes > 2) {
  114. attribs[atti++] = EGL_DMA_BUF_PLANE2_FD_EXT;
  115. attribs[atti++] = fds[2];
  116. attribs[atti++] = EGL_DMA_BUF_PLANE2_OFFSET_EXT;
  117. attribs[atti++] = offsets[2];
  118. attribs[atti++] = EGL_DMA_BUF_PLANE2_PITCH_EXT;
  119. attribs[atti++] = strides[2];
  120. if (modifiers) {
  121. attribs[atti++] = EGL_DMA_BUF_PLANE2_MODIFIER_LO_EXT;
  122. attribs[atti++] = modifiers[2] & 0xFFFFFFFF;
  123. attribs[atti++] = EGL_DMA_BUF_PLANE2_MODIFIER_HI_EXT;
  124. attribs[atti++] = modifiers[2] >> 32;
  125. }
  126. }
  127. if (n_planes > 3) {
  128. attribs[atti++] = EGL_DMA_BUF_PLANE3_FD_EXT;
  129. attribs[atti++] = fds[3];
  130. attribs[atti++] = EGL_DMA_BUF_PLANE3_OFFSET_EXT;
  131. attribs[atti++] = offsets[3];
  132. attribs[atti++] = EGL_DMA_BUF_PLANE3_PITCH_EXT;
  133. attribs[atti++] = strides[3];
  134. if (modifiers) {
  135. attribs[atti++] = EGL_DMA_BUF_PLANE3_MODIFIER_LO_EXT;
  136. attribs[atti++] = modifiers[3] & 0xFFFFFFFF;
  137. attribs[atti++] = EGL_DMA_BUF_PLANE3_MODIFIER_HI_EXT;
  138. attribs[atti++] = modifiers[3] >> 32;
  139. }
  140. }
  141. attribs[atti++] = EGL_NONE;
  142. return eglCreateImage(egl_display, EGL_NO_CONTEXT, EGL_LINUX_DMA_BUF_EXT, 0, attribs);
  143. }
  144. struct gs_texture *gl_egl_create_texture_from_eglimage(EGLDisplay egl_display, uint32_t width, uint32_t height,
  145. enum gs_color_format color_format, EGLint target, EGLImage image)
  146. {
  147. UNUSED_PARAMETER(egl_display);
  148. UNUSED_PARAMETER(target);
  149. struct gs_texture *texture = NULL;
  150. texture = gs_texture_create(width, height, color_format, 1, NULL, GS_GL_DUMMYTEX | GS_RENDER_TARGET);
  151. const GLuint gltex = *(GLuint *)gs_texture_get_obj(texture);
  152. gl_bind_texture(GL_TEXTURE_2D, gltex);
  153. gl_tex_param_i(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  154. gl_tex_param_i(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  155. glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, image);
  156. if (!gl_success("glEGLImageTargetTexture2DOES")) {
  157. gs_texture_destroy(texture);
  158. texture = NULL;
  159. }
  160. gl_bind_texture(GL_TEXTURE_2D, 0);
  161. return texture;
  162. }
  163. bool gl_egl_enum_adapters(EGLDisplay display, bool (*callback)(void *param, const char *name, uint32_t id), void *param)
  164. {
  165. EGLDeviceEXT display_dev;
  166. if (eglQueryDisplayAttribEXT(display, EGL_DEVICE_EXT, (EGLAttrib *)&display_dev) &&
  167. eglGetError() == EGL_SUCCESS) {
  168. const char *display_node = eglQueryDeviceStringEXT(display_dev, EGL_DRM_RENDER_NODE_FILE_EXT);
  169. if (eglGetError() != EGL_SUCCESS || display_node == NULL) {
  170. display_node = "/Software";
  171. }
  172. if (!callback(param, display_node, 0)) {
  173. return true;
  174. }
  175. }
  176. EGLint num_devices = 0;
  177. EGLDeviceEXT devices[32];
  178. if (!eglQueryDevicesEXT(32, devices, &num_devices)) {
  179. eglGetError();
  180. return true;
  181. }
  182. for (int i = 0; i < num_devices; i++) {
  183. const char *node = eglQueryDeviceStringEXT(devices[i], EGL_DRM_RENDER_NODE_FILE_EXT);
  184. if (node == NULL || eglGetError() != EGL_SUCCESS) {
  185. // Do not enumerate additional software renderers.
  186. continue;
  187. }
  188. if (!callback(param, node, i + 1)) {
  189. return true;
  190. }
  191. }
  192. return true;
  193. }
  194. uint32_t gs_get_adapter_count()
  195. {
  196. EGLint num_devices = 0;
  197. eglQueryDevicesEXT(0, NULL, &num_devices);
  198. return 1 + num_devices; // Display + devices.
  199. }
  200. struct gs_texture *gl_egl_create_dmabuf_image(EGLDisplay egl_display, unsigned int width, unsigned int height,
  201. uint32_t drm_format, enum gs_color_format color_format, uint32_t n_planes,
  202. const int *fds, const uint32_t *strides, const uint32_t *offsets,
  203. const uint64_t *modifiers)
  204. {
  205. struct gs_texture *texture = NULL;
  206. EGLImage egl_image;
  207. if (!init_egl_image_target_texture_2d_ext())
  208. return NULL;
  209. egl_image = create_dmabuf_egl_image(egl_display, width, height, drm_format, n_planes, fds, strides, offsets,
  210. modifiers);
  211. if (egl_image == EGL_NO_IMAGE) {
  212. blog(LOG_ERROR, "Cannot create EGLImage: %s", gl_egl_error_to_string(eglGetError()));
  213. return NULL;
  214. }
  215. texture =
  216. gl_egl_create_texture_from_eglimage(egl_display, width, height, color_format, GL_TEXTURE_2D, egl_image);
  217. if (texture)
  218. eglDestroyImage(egl_display, egl_image);
  219. return texture;
  220. }
  221. struct gs_texture *gl_egl_create_texture_from_pixmap(EGLDisplay egl_display, uint32_t width, uint32_t height,
  222. enum gs_color_format color_format, EGLint target,
  223. EGLClientBuffer pixmap)
  224. {
  225. if (!init_egl_image_target_texture_2d_ext())
  226. return NULL;
  227. const EGLAttrib pixmap_attrs[] = {
  228. EGL_IMAGE_PRESERVED_KHR,
  229. EGL_TRUE,
  230. EGL_NONE,
  231. };
  232. EGLImage image = eglCreateImage(egl_display, EGL_NO_CONTEXT, EGL_NATIVE_PIXMAP_KHR, pixmap, pixmap_attrs);
  233. if (image == EGL_NO_IMAGE) {
  234. blog(LOG_DEBUG, "Cannot create EGLImage: %s", gl_egl_error_to_string(eglGetError()));
  235. return NULL;
  236. }
  237. struct gs_texture *texture =
  238. gl_egl_create_texture_from_eglimage(egl_display, width, height, color_format, target, image);
  239. eglDestroyImage(egl_display, image);
  240. return texture;
  241. }
  242. static inline bool is_implicit_dmabuf_modifiers_supported(void)
  243. {
  244. return EGL_EXT_image_dma_buf_import > 0;
  245. }
  246. static inline bool query_dmabuf_formats(EGLDisplay egl_display, EGLint **formats, EGLint *num_formats)
  247. {
  248. EGLint max_formats = 0;
  249. if (!glad_eglQueryDmaBufFormatsEXT(egl_display, 0, NULL, &max_formats)) {
  250. blog(LOG_ERROR, "Cannot query the number of formats: %s", gl_egl_error_to_string(eglGetError()));
  251. return false;
  252. }
  253. if (max_formats != 0) {
  254. EGLint *format_list = bzalloc(max_formats * sizeof(EGLint));
  255. if (!format_list) {
  256. blog(LOG_ERROR, "Unable to allocate memory");
  257. return false;
  258. }
  259. if (!glad_eglQueryDmaBufFormatsEXT(egl_display, max_formats, format_list, &max_formats)) {
  260. blog(LOG_ERROR, "Cannot query a list of formats: %s", gl_egl_error_to_string(eglGetError()));
  261. bfree(format_list);
  262. return false;
  263. }
  264. *formats = format_list;
  265. }
  266. *num_formats = max_formats;
  267. return true;
  268. }
  269. bool gl_egl_query_dmabuf_capabilities(EGLDisplay egl_display, enum gs_dmabuf_flags *dmabuf_flags, uint32_t **formats,
  270. size_t *n_formats)
  271. {
  272. bool ret = false;
  273. if (is_implicit_dmabuf_modifiers_supported()) {
  274. *dmabuf_flags = GS_DMABUF_FLAG_IMPLICIT_MODIFIERS_SUPPORTED;
  275. ret = true;
  276. }
  277. if (!glad_eglQueryDmaBufFormatsEXT) {
  278. blog(LOG_ERROR, "Unable to load eglQueryDmaBufFormatsEXT");
  279. return ret;
  280. }
  281. if (!query_dmabuf_formats(egl_display, (EGLint **)formats, (EGLint *)n_formats)) {
  282. *n_formats = 0;
  283. *formats = NULL;
  284. }
  285. return ret;
  286. }
  287. static inline bool query_dmabuf_modifiers(EGLDisplay egl_display, EGLint drm_format, EGLuint64KHR **modifiers,
  288. EGLuint64KHR *n_modifiers)
  289. {
  290. EGLint max_modifiers;
  291. if (!glad_eglQueryDmaBufModifiersEXT(egl_display, drm_format, 0, NULL, NULL, &max_modifiers)) {
  292. blog(LOG_ERROR, "Cannot query the number of modifiers: %s", gl_egl_error_to_string(eglGetError()));
  293. return false;
  294. }
  295. if (max_modifiers != 0) {
  296. EGLuint64KHR *modifier_list = bzalloc(max_modifiers * sizeof(EGLuint64KHR));
  297. EGLBoolean *external_only = NULL;
  298. if (!modifier_list) {
  299. blog(LOG_ERROR, "Unable to allocate memory");
  300. return false;
  301. }
  302. if (!glad_eglQueryDmaBufModifiersEXT(egl_display, drm_format, max_modifiers, modifier_list,
  303. external_only, &max_modifiers)) {
  304. blog(LOG_ERROR, "Cannot query a list of modifiers: %s", gl_egl_error_to_string(eglGetError()));
  305. bfree(modifier_list);
  306. return false;
  307. }
  308. *modifiers = modifier_list;
  309. }
  310. *n_modifiers = (EGLuint64KHR)max_modifiers;
  311. return true;
  312. }
  313. bool gl_egl_query_dmabuf_modifiers_for_format(EGLDisplay egl_display, uint32_t drm_format, uint64_t **modifiers,
  314. size_t *n_modifiers)
  315. {
  316. if (!glad_eglQueryDmaBufModifiersEXT) {
  317. blog(LOG_ERROR, "Unable to load eglQueryDmaBufModifiersEXT");
  318. return false;
  319. }
  320. if (!query_dmabuf_modifiers(egl_display, drm_format, modifiers, n_modifiers)) {
  321. *n_modifiers = 0;
  322. *modifiers = NULL;
  323. return false;
  324. }
  325. return true;
  326. }
  327. const char *gl_egl_error_to_string(EGLint error_number)
  328. {
  329. switch (error_number) {
  330. case EGL_SUCCESS:
  331. return "The last function succeeded without error.";
  332. break;
  333. case EGL_NOT_INITIALIZED:
  334. return "EGL is not initialized, or could not be initialized, for the specified EGL display connection.";
  335. break;
  336. case EGL_BAD_ACCESS:
  337. return "EGL cannot access a requested resource (for example a context is bound in another thread).";
  338. break;
  339. case EGL_BAD_ALLOC:
  340. return "EGL failed to allocate resources for the requested operation.";
  341. break;
  342. case EGL_BAD_ATTRIBUTE:
  343. return "An unrecognized attribute or attribute value was passed in the attribute list.";
  344. break;
  345. case EGL_BAD_CONTEXT:
  346. return "An EGLContext argument does not name a valid EGL rendering context.";
  347. break;
  348. case EGL_BAD_CONFIG:
  349. return "An EGLConfig argument does not name a valid EGL frame buffer configuration.";
  350. break;
  351. case EGL_BAD_CURRENT_SURFACE:
  352. return "The current surface of the calling thread is a window, pixel buffer or pixmap that is no longer valid.";
  353. break;
  354. case EGL_BAD_DISPLAY:
  355. return "An EGLDisplay argument does not name a valid EGL display connection.";
  356. break;
  357. case EGL_BAD_SURFACE:
  358. return "An EGLSurface argument does not name a valid surface (window, pixel buffer or pixmap) configured for GL rendering.";
  359. break;
  360. case EGL_BAD_MATCH:
  361. return "Arguments are inconsistent (for example, a valid context requires buffers not supplied by a valid surface).";
  362. break;
  363. case EGL_BAD_PARAMETER:
  364. return "One or more argument values are invalid.";
  365. break;
  366. case EGL_BAD_NATIVE_PIXMAP:
  367. return "A NativePixmapType argument does not refer to a valid native pixmap.";
  368. break;
  369. case EGL_BAD_NATIVE_WINDOW:
  370. return "A NativeWindowType argument does not refer to a valid native window.";
  371. break;
  372. case EGL_CONTEXT_LOST:
  373. return "A power management event has occurred. The application must destroy all contexts and reinitialise OpenGL ES state and objects to continue rendering. ";
  374. break;
  375. default:
  376. return "Unknown error";
  377. break;
  378. }
  379. }