1
0

gl-egl-common.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. /* copied from drm_fourcc.h */
  41. #define fourcc_code(a, b, c, d) \
  42. ((__u32)(a) | ((__u32)(b) << 8) | ((__u32)(c) << 16) | \
  43. ((__u32)(d) << 24))
  44. #define DRM_FORMAT_INVALID 0
  45. #define DRM_FORMAT_R8 fourcc_code('R', '8', ' ', ' ') /* [7:0] R */
  46. #define DRM_FORMAT_R16 \
  47. fourcc_code('R', '1', '6', ' ') /* [15:0] R little endian */
  48. #define DRM_FORMAT_RG88 \
  49. fourcc_code('R', 'G', '8', '8') /* [15:0] R:G 8:8 little endian */
  50. #define DRM_FORMAT_ABGR8888 \
  51. fourcc_code('A', 'B', '2', \
  52. '4') /* [31:0] A:B:G:R 8:8:8:8 little endian */
  53. #define DRM_FORMAT_ABGR2101010 \
  54. fourcc_code('A', 'B', '3', \
  55. '0') /* [31:0] A:B:G:R 2:10:10:10 little endian */
  56. #define DRM_FORMAT_ABGR16161616F \
  57. fourcc_code('A', 'B', '4', \
  58. 'H') /* [63:0] A:B:G:R 16:16:16:16 little endian */
  59. #define DRM_FORMAT_ARGB8888 \
  60. fourcc_code('A', 'R', '2', \
  61. '4') /* [31:0] A:R:G:B 8:8:8:8 little endian */
  62. #define DRM_FORMAT_XRGB8888 \
  63. fourcc_code('X', 'R', '2', \
  64. '4') /* [31:0] x:R:G:B 8:8:8:8 little endian */
  65. static bool find_gl_extension(const char *extension)
  66. {
  67. GLint n, i;
  68. glGetIntegerv(GL_NUM_EXTENSIONS, &n);
  69. for (i = 0; i < n; i++) {
  70. const char *e = (char *)glGetStringi(GL_EXTENSIONS, i);
  71. if (extension && strcmp(e, extension) == 0)
  72. return true;
  73. }
  74. return false;
  75. }
  76. static bool init_egl_image_target_texture_2d_ext(void)
  77. {
  78. static bool initialized = false;
  79. if (!initialized) {
  80. initialized = true;
  81. if (!find_gl_extension("GL_OES_EGL_image")) {
  82. blog(LOG_ERROR, "No GL_OES_EGL_image");
  83. return false;
  84. }
  85. glEGLImageTargetTexture2DOES =
  86. (PFNGLEGLIMAGETARGETTEXTURE2DOESPROC)eglGetProcAddress(
  87. "glEGLImageTargetTexture2DOES");
  88. }
  89. if (!glEGLImageTargetTexture2DOES)
  90. return false;
  91. return true;
  92. }
  93. static inline enum gs_color_format gs_format_to_drm_format(uint32_t drm_format)
  94. {
  95. switch (drm_format) {
  96. case GS_R8:
  97. return DRM_FORMAT_R8;
  98. case GS_RGBA:
  99. return DRM_FORMAT_ABGR8888;
  100. case GS_BGRX:
  101. return DRM_FORMAT_XRGB8888;
  102. case GS_BGRA:
  103. return DRM_FORMAT_ARGB8888;
  104. case GS_R10G10B10A2:
  105. return DRM_FORMAT_ABGR2101010;
  106. case GS_R16:
  107. return DRM_FORMAT_R16;
  108. case GS_RGBA16F:
  109. return DRM_FORMAT_ABGR16161616F;
  110. case GS_R8G8:
  111. return DRM_FORMAT_RG88;
  112. case GS_A8:
  113. case GS_R16F:
  114. case GS_RGBA16:
  115. case GS_RG16F:
  116. case GS_R32F:
  117. case GS_RG32F:
  118. case GS_RGBA32F:
  119. case GS_DXT1:
  120. case GS_DXT3:
  121. case GS_DXT5:
  122. case GS_UNKNOWN:
  123. return DRM_FORMAT_INVALID;
  124. }
  125. return DRM_FORMAT_INVALID;
  126. }
  127. static EGLImageKHR
  128. create_dmabuf_egl_image(EGLDisplay egl_display, unsigned int width,
  129. unsigned int height, uint32_t drm_format,
  130. uint32_t n_planes, const int *fds,
  131. const uint32_t *strides, const uint32_t *offsets,
  132. const uint64_t *modifiers)
  133. {
  134. EGLAttrib attribs[47];
  135. int atti = 0;
  136. /* This requires the Mesa commit in
  137. * Mesa 10.3 (08264e5dad4df448e7718e782ad9077902089a07) or
  138. * Mesa 10.2.7 (55d28925e6109a4afd61f109e845a8a51bd17652).
  139. * Otherwise Mesa closes the fd behind our back and re-importing
  140. * will fail.
  141. * https://bugs.freedesktop.org/show_bug.cgi?id=76188
  142. * */
  143. attribs[atti++] = EGL_WIDTH;
  144. attribs[atti++] = width;
  145. attribs[atti++] = EGL_HEIGHT;
  146. attribs[atti++] = height;
  147. attribs[atti++] = EGL_LINUX_DRM_FOURCC_EXT;
  148. attribs[atti++] = drm_format;
  149. if (n_planes > 0) {
  150. attribs[atti++] = EGL_DMA_BUF_PLANE0_FD_EXT;
  151. attribs[atti++] = fds[0];
  152. attribs[atti++] = EGL_DMA_BUF_PLANE0_OFFSET_EXT;
  153. attribs[atti++] = offsets[0];
  154. attribs[atti++] = EGL_DMA_BUF_PLANE0_PITCH_EXT;
  155. attribs[atti++] = strides[0];
  156. if (modifiers) {
  157. attribs[atti++] = EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT;
  158. attribs[atti++] = modifiers[0] & 0xFFFFFFFF;
  159. attribs[atti++] = EGL_DMA_BUF_PLANE0_MODIFIER_HI_EXT;
  160. attribs[atti++] = modifiers[0] >> 32;
  161. }
  162. }
  163. if (n_planes > 1) {
  164. attribs[atti++] = EGL_DMA_BUF_PLANE1_FD_EXT;
  165. attribs[atti++] = fds[1];
  166. attribs[atti++] = EGL_DMA_BUF_PLANE1_OFFSET_EXT;
  167. attribs[atti++] = offsets[1];
  168. attribs[atti++] = EGL_DMA_BUF_PLANE1_PITCH_EXT;
  169. attribs[atti++] = strides[1];
  170. if (modifiers) {
  171. attribs[atti++] = EGL_DMA_BUF_PLANE1_MODIFIER_LO_EXT;
  172. attribs[atti++] = modifiers[1] & 0xFFFFFFFF;
  173. attribs[atti++] = EGL_DMA_BUF_PLANE1_MODIFIER_HI_EXT;
  174. attribs[atti++] = modifiers[1] >> 32;
  175. }
  176. }
  177. if (n_planes > 2) {
  178. attribs[atti++] = EGL_DMA_BUF_PLANE2_FD_EXT;
  179. attribs[atti++] = fds[2];
  180. attribs[atti++] = EGL_DMA_BUF_PLANE2_OFFSET_EXT;
  181. attribs[atti++] = offsets[2];
  182. attribs[atti++] = EGL_DMA_BUF_PLANE2_PITCH_EXT;
  183. attribs[atti++] = strides[2];
  184. if (modifiers) {
  185. attribs[atti++] = EGL_DMA_BUF_PLANE2_MODIFIER_LO_EXT;
  186. attribs[atti++] = modifiers[2] & 0xFFFFFFFF;
  187. attribs[atti++] = EGL_DMA_BUF_PLANE2_MODIFIER_HI_EXT;
  188. attribs[atti++] = modifiers[2] >> 32;
  189. }
  190. }
  191. if (n_planes > 3) {
  192. attribs[atti++] = EGL_DMA_BUF_PLANE3_FD_EXT;
  193. attribs[atti++] = fds[3];
  194. attribs[atti++] = EGL_DMA_BUF_PLANE3_OFFSET_EXT;
  195. attribs[atti++] = offsets[3];
  196. attribs[atti++] = EGL_DMA_BUF_PLANE3_PITCH_EXT;
  197. attribs[atti++] = strides[3];
  198. if (modifiers) {
  199. attribs[atti++] = EGL_DMA_BUF_PLANE3_MODIFIER_LO_EXT;
  200. attribs[atti++] = modifiers[3] & 0xFFFFFFFF;
  201. attribs[atti++] = EGL_DMA_BUF_PLANE3_MODIFIER_HI_EXT;
  202. attribs[atti++] = modifiers[3] >> 32;
  203. }
  204. }
  205. attribs[atti++] = EGL_NONE;
  206. return eglCreateImage(egl_display, EGL_NO_CONTEXT,
  207. EGL_LINUX_DMA_BUF_EXT, 0, attribs);
  208. }
  209. struct gs_texture *
  210. gl_egl_create_dmabuf_image(EGLDisplay egl_display, unsigned int width,
  211. unsigned int height,
  212. enum gs_color_format color_format, uint32_t n_planes,
  213. const int *fds, const uint32_t *strides,
  214. const uint32_t *offsets, const uint64_t *modifiers)
  215. {
  216. struct gs_texture *texture = NULL;
  217. EGLImage egl_image;
  218. uint32_t drm_format;
  219. if (!init_egl_image_target_texture_2d_ext())
  220. return NULL;
  221. drm_format = gs_format_to_drm_format(color_format);
  222. if (drm_format == DRM_FORMAT_INVALID) {
  223. blog(LOG_ERROR, "Invalid or unsupported image format");
  224. return NULL;
  225. }
  226. egl_image = create_dmabuf_egl_image(egl_display, width, height,
  227. drm_format, n_planes, fds, strides,
  228. offsets, modifiers);
  229. if (egl_image == EGL_NO_IMAGE) {
  230. blog(LOG_ERROR, "Cannot create EGLImage: %s",
  231. gl_egl_error_to_string(eglGetError()));
  232. return NULL;
  233. }
  234. texture = gs_texture_create(width, height, color_format, 1, NULL,
  235. GS_DYNAMIC);
  236. const GLuint gltex = *(GLuint *)gs_texture_get_obj(texture);
  237. glBindTexture(GL_TEXTURE_2D, gltex);
  238. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  239. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  240. glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, egl_image);
  241. glBindTexture(GL_TEXTURE_2D, 0);
  242. eglDestroyImage(egl_display, egl_image);
  243. return texture;
  244. }
  245. const char *gl_egl_error_to_string(EGLint error_number)
  246. {
  247. switch (error_number) {
  248. case EGL_SUCCESS:
  249. return "The last function succeeded without error.";
  250. break;
  251. case EGL_NOT_INITIALIZED:
  252. return "EGL is not initialized, or could not be initialized, for the specified EGL display connection.";
  253. break;
  254. case EGL_BAD_ACCESS:
  255. return "EGL cannot access a requested resource (for example a context is bound in another thread).";
  256. break;
  257. case EGL_BAD_ALLOC:
  258. return "EGL failed to allocate resources for the requested operation.";
  259. break;
  260. case EGL_BAD_ATTRIBUTE:
  261. return "An unrecognized attribute or attribute value was passed in the attribute list.";
  262. break;
  263. case EGL_BAD_CONTEXT:
  264. return "An EGLContext argument does not name a valid EGL rendering context.";
  265. break;
  266. case EGL_BAD_CONFIG:
  267. return "An EGLConfig argument does not name a valid EGL frame buffer configuration.";
  268. break;
  269. case EGL_BAD_CURRENT_SURFACE:
  270. return "The current surface of the calling thread is a window, pixel buffer or pixmap that is no longer valid.";
  271. break;
  272. case EGL_BAD_DISPLAY:
  273. return "An EGLDisplay argument does not name a valid EGL display connection.";
  274. break;
  275. case EGL_BAD_SURFACE:
  276. return "An EGLSurface argument does not name a valid surface (window, pixel buffer or pixmap) configured for GL rendering.";
  277. break;
  278. case EGL_BAD_MATCH:
  279. return "Arguments are inconsistent (for example, a valid context requires buffers not supplied by a valid surface).";
  280. break;
  281. case EGL_BAD_PARAMETER:
  282. return "One or more argument values are invalid.";
  283. break;
  284. case EGL_BAD_NATIVE_PIXMAP:
  285. return "A NativePixmapType argument does not refer to a valid native pixmap.";
  286. break;
  287. case EGL_BAD_NATIVE_WINDOW:
  288. return "A NativeWindowType argument does not refer to a valid native window.";
  289. break;
  290. case EGL_CONTEXT_LOST:
  291. return "A power management event has occurred. The application must destroy all contexts and reinitialise OpenGL ES state and objects to continue rendering. ";
  292. break;
  293. default:
  294. return "Unknown error";
  295. break;
  296. }
  297. }