gl-egl-common.c 13 KB

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