gl-egl-common.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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 *
  151. gl_egl_create_dmabuf_image(EGLDisplay egl_display, unsigned int width,
  152. unsigned int height, uint32_t drm_format,
  153. enum gs_color_format color_format, uint32_t n_planes,
  154. const int *fds, const uint32_t *strides,
  155. const uint32_t *offsets, const uint64_t *modifiers)
  156. {
  157. struct gs_texture *texture = NULL;
  158. EGLImage egl_image;
  159. if (!init_egl_image_target_texture_2d_ext())
  160. return NULL;
  161. egl_image = create_dmabuf_egl_image(egl_display, width, height,
  162. drm_format, n_planes, fds, strides,
  163. offsets, modifiers);
  164. if (egl_image == EGL_NO_IMAGE) {
  165. blog(LOG_ERROR, "Cannot create EGLImage: %s",
  166. gl_egl_error_to_string(eglGetError()));
  167. return NULL;
  168. }
  169. if ((texture = gs_texture_create(width, height, color_format, 1, NULL,
  170. GS_DYNAMIC)) == NULL) {
  171. return NULL;
  172. }
  173. const GLuint gltex = *(GLuint *)gs_texture_get_obj(texture);
  174. gl_bind_texture(GL_TEXTURE_2D, gltex);
  175. gl_tex_param_i(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  176. gl_tex_param_i(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  177. glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, egl_image);
  178. if (!gl_success("glEGLImageTargetTexture2DOES")) {
  179. gs_texture_destroy(texture);
  180. texture = NULL;
  181. }
  182. gl_bind_texture(GL_TEXTURE_2D, 0);
  183. eglDestroyImage(egl_display, egl_image);
  184. return texture;
  185. }
  186. static inline bool is_implicit_dmabuf_modifiers_supported(void)
  187. {
  188. return EGL_EXT_image_dma_buf_import > 0;
  189. }
  190. static inline bool query_dmabuf_formats(EGLDisplay egl_display,
  191. EGLint **formats, EGLint *num_formats)
  192. {
  193. EGLint max_formats = 0;
  194. EGLint *format_list = NULL;
  195. if (!glad_eglQueryDmaBufFormatsEXT(egl_display, 0, NULL,
  196. &max_formats)) {
  197. blog(LOG_ERROR, "Cannot query the number of formats: %s",
  198. gl_egl_error_to_string(eglGetError()));
  199. return false;
  200. }
  201. format_list = bzalloc(max_formats * sizeof(EGLint));
  202. if (!format_list) {
  203. blog(LOG_ERROR, "Unable to allocate memory");
  204. return false;
  205. }
  206. if (!glad_eglQueryDmaBufFormatsEXT(egl_display, max_formats,
  207. format_list, &max_formats)) {
  208. blog(LOG_ERROR, "Cannot query a list of formats: %s",
  209. gl_egl_error_to_string(eglGetError()));
  210. bfree(format_list);
  211. return false;
  212. }
  213. *formats = format_list;
  214. *num_formats = max_formats;
  215. return true;
  216. }
  217. bool gl_egl_query_dmabuf_capabilities(EGLDisplay egl_display,
  218. enum gs_dmabuf_flags *dmabuf_flags,
  219. uint32_t **formats, size_t *n_formats)
  220. {
  221. bool ret = false;
  222. if (is_implicit_dmabuf_modifiers_supported()) {
  223. *dmabuf_flags = GS_DMABUF_FLAG_IMPLICIT_MODIFIERS_SUPPORTED;
  224. ret = true;
  225. }
  226. if (!glad_eglQueryDmaBufFormatsEXT) {
  227. blog(LOG_ERROR, "Unable to load eglQueryDmaBufFormatsEXT");
  228. return ret;
  229. }
  230. if (!query_dmabuf_formats(egl_display, (EGLint **)formats,
  231. (EGLint *)n_formats)) {
  232. *n_formats = 0;
  233. *formats = NULL;
  234. }
  235. return ret;
  236. }
  237. static inline bool query_dmabuf_modifiers(EGLDisplay egl_display,
  238. EGLint drm_format,
  239. EGLuint64KHR **modifiers,
  240. EGLuint64KHR *n_modifiers)
  241. {
  242. EGLint max_modifiers;
  243. if (!glad_eglQueryDmaBufModifiersEXT(egl_display, drm_format, 0, NULL,
  244. NULL, &max_modifiers)) {
  245. blog(LOG_ERROR, "Cannot query the number of modifiers: %s",
  246. gl_egl_error_to_string(eglGetError()));
  247. return false;
  248. }
  249. EGLuint64KHR *modifier_list =
  250. bzalloc(max_modifiers * sizeof(EGLuint64KHR));
  251. EGLBoolean *external_only = NULL;
  252. if (!modifier_list) {
  253. blog(LOG_ERROR, "Unable to allocate memory");
  254. return false;
  255. }
  256. if (!glad_eglQueryDmaBufModifiersEXT(egl_display, drm_format,
  257. max_modifiers, modifier_list,
  258. external_only, &max_modifiers)) {
  259. blog(LOG_ERROR, "Cannot query a list of modifiers: %s",
  260. gl_egl_error_to_string(eglGetError()));
  261. bfree(modifier_list);
  262. return false;
  263. }
  264. *modifiers = modifier_list;
  265. *n_modifiers = (EGLuint64KHR)max_modifiers;
  266. return true;
  267. }
  268. bool gl_egl_query_dmabuf_modifiers_for_format(EGLDisplay egl_display,
  269. uint32_t drm_format,
  270. uint64_t **modifiers,
  271. size_t *n_modifiers)
  272. {
  273. if (!glad_eglQueryDmaBufModifiersEXT) {
  274. blog(LOG_ERROR, "Unable to load eglQueryDmaBufModifiersEXT");
  275. return false;
  276. }
  277. if (!query_dmabuf_modifiers(egl_display, drm_format, modifiers,
  278. n_modifiers)) {
  279. *n_modifiers = 0;
  280. *modifiers = NULL;
  281. return false;
  282. }
  283. return true;
  284. }
  285. const char *gl_egl_error_to_string(EGLint error_number)
  286. {
  287. switch (error_number) {
  288. case EGL_SUCCESS:
  289. return "The last function succeeded without error.";
  290. break;
  291. case EGL_NOT_INITIALIZED:
  292. return "EGL is not initialized, or could not be initialized, for the specified EGL display connection.";
  293. break;
  294. case EGL_BAD_ACCESS:
  295. return "EGL cannot access a requested resource (for example a context is bound in another thread).";
  296. break;
  297. case EGL_BAD_ALLOC:
  298. return "EGL failed to allocate resources for the requested operation.";
  299. break;
  300. case EGL_BAD_ATTRIBUTE:
  301. return "An unrecognized attribute or attribute value was passed in the attribute list.";
  302. break;
  303. case EGL_BAD_CONTEXT:
  304. return "An EGLContext argument does not name a valid EGL rendering context.";
  305. break;
  306. case EGL_BAD_CONFIG:
  307. return "An EGLConfig argument does not name a valid EGL frame buffer configuration.";
  308. break;
  309. case EGL_BAD_CURRENT_SURFACE:
  310. return "The current surface of the calling thread is a window, pixel buffer or pixmap that is no longer valid.";
  311. break;
  312. case EGL_BAD_DISPLAY:
  313. return "An EGLDisplay argument does not name a valid EGL display connection.";
  314. break;
  315. case EGL_BAD_SURFACE:
  316. return "An EGLSurface argument does not name a valid surface (window, pixel buffer or pixmap) configured for GL rendering.";
  317. break;
  318. case EGL_BAD_MATCH:
  319. return "Arguments are inconsistent (for example, a valid context requires buffers not supplied by a valid surface).";
  320. break;
  321. case EGL_BAD_PARAMETER:
  322. return "One or more argument values are invalid.";
  323. break;
  324. case EGL_BAD_NATIVE_PIXMAP:
  325. return "A NativePixmapType argument does not refer to a valid native pixmap.";
  326. break;
  327. case EGL_BAD_NATIVE_WINDOW:
  328. return "A NativeWindowType argument does not refer to a valid native window.";
  329. break;
  330. case EGL_CONTEXT_LOST:
  331. return "A power management event has occurred. The application must destroy all contexts and reinitialise OpenGL ES state and objects to continue rendering. ";
  332. break;
  333. default:
  334. return "Unknown error";
  335. break;
  336. }
  337. }