gl-egl-common.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. texture = gs_texture_create(width, height, color_format, 1, NULL,
  170. GS_DYNAMIC);
  171. const GLuint gltex = *(GLuint *)gs_texture_get_obj(texture);
  172. glBindTexture(GL_TEXTURE_2D, gltex);
  173. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  174. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  175. glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, egl_image);
  176. glBindTexture(GL_TEXTURE_2D, 0);
  177. eglDestroyImage(egl_display, egl_image);
  178. return texture;
  179. }
  180. const char *gl_egl_error_to_string(EGLint error_number)
  181. {
  182. switch (error_number) {
  183. case EGL_SUCCESS:
  184. return "The last function succeeded without error.";
  185. break;
  186. case EGL_NOT_INITIALIZED:
  187. return "EGL is not initialized, or could not be initialized, for the specified EGL display connection.";
  188. break;
  189. case EGL_BAD_ACCESS:
  190. return "EGL cannot access a requested resource (for example a context is bound in another thread).";
  191. break;
  192. case EGL_BAD_ALLOC:
  193. return "EGL failed to allocate resources for the requested operation.";
  194. break;
  195. case EGL_BAD_ATTRIBUTE:
  196. return "An unrecognized attribute or attribute value was passed in the attribute list.";
  197. break;
  198. case EGL_BAD_CONTEXT:
  199. return "An EGLContext argument does not name a valid EGL rendering context.";
  200. break;
  201. case EGL_BAD_CONFIG:
  202. return "An EGLConfig argument does not name a valid EGL frame buffer configuration.";
  203. break;
  204. case EGL_BAD_CURRENT_SURFACE:
  205. return "The current surface of the calling thread is a window, pixel buffer or pixmap that is no longer valid.";
  206. break;
  207. case EGL_BAD_DISPLAY:
  208. return "An EGLDisplay argument does not name a valid EGL display connection.";
  209. break;
  210. case EGL_BAD_SURFACE:
  211. return "An EGLSurface argument does not name a valid surface (window, pixel buffer or pixmap) configured for GL rendering.";
  212. break;
  213. case EGL_BAD_MATCH:
  214. return "Arguments are inconsistent (for example, a valid context requires buffers not supplied by a valid surface).";
  215. break;
  216. case EGL_BAD_PARAMETER:
  217. return "One or more argument values are invalid.";
  218. break;
  219. case EGL_BAD_NATIVE_PIXMAP:
  220. return "A NativePixmapType argument does not refer to a valid native pixmap.";
  221. break;
  222. case EGL_BAD_NATIVE_WINDOW:
  223. return "A NativeWindowType argument does not refer to a valid native window.";
  224. break;
  225. case EGL_CONTEXT_LOST:
  226. return "A power management event has occurred. The application must destroy all contexts and reinitialise OpenGL ES state and objects to continue rendering. ";
  227. break;
  228. default:
  229. return "Unknown error";
  230. break;
  231. }
  232. }