summarylogtreecommitdiffstats
path: root/0025-libobs-opengl-Implement-DMA-BUF-importing-on-EGL-ren.patch
blob: cecc49cf9e22a440976a69b45675e81712bef14d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
From d47bea7c915ea2c9d9b2f81724509760bd769df4 Mon Sep 17 00:00:00 2001
From: Georges Basile Stavracas Neto <georges.stavracas@gmail.com>
Date: Sat, 14 Mar 2020 14:26:02 -0300
Subject: [PATCH 25/25] libobs-opengl: Implement DMA-BUF importing on EGL
 renderers

This is currently extremely limited. It only supports XRGB8888
DMA-BUFs for now, which seems to be a sensible default.
---
 libobs-opengl/CMakeLists.txt   |   1 +
 libobs-opengl/gl-egl-common.c  | 340 +++++++++++++++++++++++++++++++++
 libobs-opengl/gl-egl-common.h  |  14 ++
 libobs-opengl/gl-wayland-egl.c |  18 +-
 libobs-opengl/gl-x11-egl.c     |  15 +-
 5 files changed, 367 insertions(+), 21 deletions(-)
 create mode 100644 libobs-opengl/gl-egl-common.c
 create mode 100644 libobs-opengl/gl-egl-common.h

diff --git a/libobs-opengl/CMakeLists.txt b/libobs-opengl/CMakeLists.txt
index 6476e0be..d4179806 100644
--- a/libobs-opengl/CMakeLists.txt
+++ b/libobs-opengl/CMakeLists.txt
@@ -49,6 +49,7 @@ else()
 		${X11_XCB_LIBRARIES})
 
 	set(libobs-opengl_PLATFORM_SOURCES
+		gl-egl-common.c
 		gl-nix.c
 		gl-x11-egl.c
 		gl-x11-glx.c)
diff --git a/libobs-opengl/gl-egl-common.c b/libobs-opengl/gl-egl-common.c
new file mode 100644
index 00000000..0696839e
--- /dev/null
+++ b/libobs-opengl/gl-egl-common.c
@@ -0,0 +1,340 @@
+/******************************************************************************
+    Copyright (C) 2020 by Georges Basile Stavracas Neto <georges.stavracas@gmail.com>
+
+    This program is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 2 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+******************************************************************************/
+
+#include "gl-egl-common.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <glad/glad_egl.h>
+
+#if defined(__linux__)
+
+#include <linux/types.h>
+#include <asm/ioctl.h>
+typedef unsigned int drm_handle_t;
+
+#else
+
+#include <stdint.h>
+#include <sys/ioccom.h>
+#include <sys/types.h>
+typedef int8_t __s8;
+typedef uint8_t __u8;
+typedef int16_t __s16;
+typedef uint16_t __u16;
+typedef int32_t __s32;
+typedef uint32_t __u32;
+typedef int64_t __s64;
+typedef uint64_t __u64;
+typedef size_t __kernel_size_t;
+typedef unsigned long drm_handle_t;
+
+#endif
+
+typedef void(APIENTRYP PFNGLEGLIMAGETARGETTEXTURE2DOESPROC)(
+	GLenum target, GLeglImageOES image);
+static PFNGLEGLIMAGETARGETTEXTURE2DOESPROC glEGLImageTargetTexture2DOES;
+
+/* copied from drm_fourcc.h */
+
+#define fourcc_code(a, b, c, d)                                \
+	((__u32)(a) | ((__u32)(b) << 8) | ((__u32)(c) << 16) | \
+	 ((__u32)(d) << 24))
+#define DRM_FORMAT_INVALID 0
+#define DRM_FORMAT_R8 fourcc_code('R', '8', ' ', ' ') /* [7:0] R */
+#define DRM_FORMAT_R16 \
+	fourcc_code('R', '1', '6', ' ') /* [15:0] R little endian */
+#define DRM_FORMAT_RG88 \
+	fourcc_code('R', 'G', '8', '8') /* [15:0] R:G 8:8 little endian */
+#define DRM_FORMAT_ABGR8888        \
+	fourcc_code('A', 'B', '2', \
+		    '4') /* [31:0] A:B:G:R 8:8:8:8 little endian */
+#define DRM_FORMAT_ABGR2101010     \
+	fourcc_code('A', 'B', '3', \
+		    '0') /* [31:0] A:B:G:R 2:10:10:10 little endian */
+#define DRM_FORMAT_ABGR16161616F   \
+	fourcc_code('A', 'B', '4', \
+		    'H') /* [63:0] A:B:G:R 16:16:16:16 little endian */
+#define DRM_FORMAT_ARGB8888        \
+	fourcc_code('A', 'R', '2', \
+		    '4') /* [31:0] A:R:G:B 8:8:8:8 little endian */
+#define DRM_FORMAT_XRGB8888        \
+	fourcc_code('X', 'R', '2', \
+		    '4') /* [31:0] x:R:G:B 8:8:8:8 little endian */
+
+static bool find_gl_extension(const char *extension)
+{
+	GLint n, i;
+
+	glGetIntegerv(GL_NUM_EXTENSIONS, &n);
+	for (i = 0; i < n; i++) {
+		const char *e = (char *)glGetStringi(GL_EXTENSIONS, i);
+		if (extension && strcmp(e, extension) == 0)
+			return true;
+	}
+	return false;
+}
+
+static bool init_egl_image_target_texture_2d_ext(void)
+{
+	static bool initialized = false;
+
+	if (!initialized) {
+		initialized = true;
+
+		if (!find_gl_extension("GL_OES_EGL_image")) {
+			blog(LOG_ERROR, "No GL_OES_EGL_image");
+			return false;
+		}
+
+		glEGLImageTargetTexture2DOES =
+			(PFNGLEGLIMAGETARGETTEXTURE2DOESPROC)eglGetProcAddress(
+				"glEGLImageTargetTexture2DOES");
+	}
+
+	if (!glEGLImageTargetTexture2DOES)
+		return false;
+
+	return true;
+}
+
+static inline enum gs_color_format gs_format_to_drm_format(uint32_t drm_format)
+{
+	switch (drm_format) {
+	case GS_R8:
+		return DRM_FORMAT_R8;
+	case GS_RGBA:
+		return DRM_FORMAT_ABGR8888;
+	case GS_BGRX:
+		return DRM_FORMAT_XRGB8888;
+	case GS_BGRA:
+		return DRM_FORMAT_ARGB8888;
+	case GS_R10G10B10A2:
+		return DRM_FORMAT_ABGR2101010;
+	case GS_R16:
+		return DRM_FORMAT_R16;
+	case GS_RGBA16F:
+		return DRM_FORMAT_ABGR16161616F;
+	case GS_R8G8:
+		return DRM_FORMAT_RG88;
+	case GS_A8:
+	case GS_R16F:
+	case GS_RGBA16:
+	case GS_RG16F:
+	case GS_R32F:
+	case GS_RG32F:
+	case GS_RGBA32F:
+	case GS_DXT1:
+	case GS_DXT3:
+	case GS_DXT5:
+	case GS_UNKNOWN:
+		return DRM_FORMAT_INVALID;
+	}
+
+	return DRM_FORMAT_INVALID;
+}
+
+static EGLImageKHR
+create_dmabuf_egl_image(EGLDisplay egl_display, unsigned int width,
+			unsigned int height, uint32_t drm_format,
+			uint32_t n_planes, const int *fds,
+			const uint32_t *strides, const uint32_t *offsets,
+			const uint64_t *modifiers)
+{
+	EGLAttrib attribs[47];
+	int atti = 0;
+
+	/* This requires the Mesa commit in
+	 * Mesa 10.3 (08264e5dad4df448e7718e782ad9077902089a07) or
+	 * Mesa 10.2.7 (55d28925e6109a4afd61f109e845a8a51bd17652).
+	 * Otherwise Mesa closes the fd behind our back and re-importing
+	 * will fail.
+	 * https://bugs.freedesktop.org/show_bug.cgi?id=76188
+	 * */
+
+	attribs[atti++] = EGL_WIDTH;
+	attribs[atti++] = width;
+	attribs[atti++] = EGL_HEIGHT;
+	attribs[atti++] = height;
+	attribs[atti++] = EGL_LINUX_DRM_FOURCC_EXT;
+	attribs[atti++] = drm_format;
+
+	if (n_planes > 0) {
+		attribs[atti++] = EGL_DMA_BUF_PLANE0_FD_EXT;
+		attribs[atti++] = fds[0];
+		attribs[atti++] = EGL_DMA_BUF_PLANE0_OFFSET_EXT;
+		attribs[atti++] = offsets[0];
+		attribs[atti++] = EGL_DMA_BUF_PLANE0_PITCH_EXT;
+		attribs[atti++] = strides[0];
+		if (modifiers) {
+			attribs[atti++] = EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT;
+			attribs[atti++] = modifiers[0] & 0xFFFFFFFF;
+			attribs[atti++] = EGL_DMA_BUF_PLANE0_MODIFIER_HI_EXT;
+			attribs[atti++] = modifiers[0] >> 32;
+		}
+	}
+
+	if (n_planes > 1) {
+		attribs[atti++] = EGL_DMA_BUF_PLANE1_FD_EXT;
+		attribs[atti++] = fds[1];
+		attribs[atti++] = EGL_DMA_BUF_PLANE1_OFFSET_EXT;
+		attribs[atti++] = offsets[1];
+		attribs[atti++] = EGL_DMA_BUF_PLANE1_PITCH_EXT;
+		attribs[atti++] = strides[1];
+		if (modifiers) {
+			attribs[atti++] = EGL_DMA_BUF_PLANE1_MODIFIER_LO_EXT;
+			attribs[atti++] = modifiers[1] & 0xFFFFFFFF;
+			attribs[atti++] = EGL_DMA_BUF_PLANE1_MODIFIER_HI_EXT;
+			attribs[atti++] = modifiers[1] >> 32;
+		}
+	}
+
+	if (n_planes > 2) {
+		attribs[atti++] = EGL_DMA_BUF_PLANE2_FD_EXT;
+		attribs[atti++] = fds[2];
+		attribs[atti++] = EGL_DMA_BUF_PLANE2_OFFSET_EXT;
+		attribs[atti++] = offsets[2];
+		attribs[atti++] = EGL_DMA_BUF_PLANE2_PITCH_EXT;
+		attribs[atti++] = strides[2];
+		if (modifiers) {
+			attribs[atti++] = EGL_DMA_BUF_PLANE2_MODIFIER_LO_EXT;
+			attribs[atti++] = modifiers[2] & 0xFFFFFFFF;
+			attribs[atti++] = EGL_DMA_BUF_PLANE2_MODIFIER_HI_EXT;
+			attribs[atti++] = modifiers[2] >> 32;
+		}
+	}
+
+	if (n_planes > 3) {
+		attribs[atti++] = EGL_DMA_BUF_PLANE3_FD_EXT;
+		attribs[atti++] = fds[3];
+		attribs[atti++] = EGL_DMA_BUF_PLANE3_OFFSET_EXT;
+		attribs[atti++] = offsets[3];
+		attribs[atti++] = EGL_DMA_BUF_PLANE3_PITCH_EXT;
+		attribs[atti++] = strides[3];
+		if (modifiers) {
+			attribs[atti++] = EGL_DMA_BUF_PLANE3_MODIFIER_LO_EXT;
+			attribs[atti++] = modifiers[3] & 0xFFFFFFFF;
+			attribs[atti++] = EGL_DMA_BUF_PLANE3_MODIFIER_HI_EXT;
+			attribs[atti++] = modifiers[3] >> 32;
+		}
+	}
+
+	attribs[atti++] = EGL_NONE;
+
+	return eglCreateImage(egl_display, EGL_NO_CONTEXT,
+			      EGL_LINUX_DMA_BUF_EXT, 0, attribs);
+}
+
+struct gs_texture *
+gl_egl_create_dmabuf_image(EGLDisplay egl_display, unsigned int width,
+			   unsigned int height,
+			   enum gs_color_format color_format, uint32_t n_planes,
+			   const int *fds, const uint32_t *strides,
+			   const uint32_t *offsets, const uint64_t *modifiers)
+{
+	struct gs_texture *texture = NULL;
+	EGLImage egl_image;
+	uint32_t drm_format;
+
+	if (!init_egl_image_target_texture_2d_ext())
+		return NULL;
+
+	drm_format = gs_format_to_drm_format(color_format);
+	if (drm_format == DRM_FORMAT_INVALID) {
+		blog(LOG_ERROR, "Invalid or unsupported image format");
+		return NULL;
+	}
+
+	egl_image = create_dmabuf_egl_image(egl_display, width, height,
+					    drm_format, n_planes, fds, strides,
+					    offsets, modifiers);
+	if (egl_image == EGL_NO_IMAGE) {
+		blog(LOG_ERROR, "Cannot create EGLImage: %s",
+		     gl_egl_error_to_string(eglGetError()));
+		return NULL;
+	}
+
+	texture = gs_texture_create(width, height, color_format, 1, NULL,
+				    GS_DYNAMIC);
+	const GLuint gltex = *(GLuint *)gs_texture_get_obj(texture);
+
+	glBindTexture(GL_TEXTURE_2D, gltex);
+	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+
+	glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, egl_image);
+
+	glBindTexture(GL_TEXTURE_2D, 0);
+	eglDestroyImage(egl_display, egl_image);
+
+	return texture;
+}
+
+const char *gl_egl_error_to_string(EGLint error_number)
+{
+	switch (error_number) {
+	case EGL_SUCCESS:
+		return "The last function succeeded without error.";
+		break;
+	case EGL_NOT_INITIALIZED:
+		return "EGL is not initialized, or could not be initialized, for the specified EGL display connection.";
+		break;
+	case EGL_BAD_ACCESS:
+		return "EGL cannot access a requested resource (for example a context is bound in another thread).";
+		break;
+	case EGL_BAD_ALLOC:
+		return "EGL failed to allocate resources for the requested operation.";
+		break;
+	case EGL_BAD_ATTRIBUTE:
+		return "An unrecognized attribute or attribute value was passed in the attribute list.";
+		break;
+	case EGL_BAD_CONTEXT:
+		return "An EGLContext argument does not name a valid EGL rendering context.";
+		break;
+	case EGL_BAD_CONFIG:
+		return "An EGLConfig argument does not name a valid EGL frame buffer configuration.";
+		break;
+	case EGL_BAD_CURRENT_SURFACE:
+		return "The current surface of the calling thread is a window, pixel buffer or pixmap that is no longer valid.";
+		break;
+	case EGL_BAD_DISPLAY:
+		return "An EGLDisplay argument does not name a valid EGL display connection.";
+		break;
+	case EGL_BAD_SURFACE:
+		return "An EGLSurface argument does not name a valid surface (window, pixel buffer or pixmap) configured for GL rendering.";
+		break;
+	case EGL_BAD_MATCH:
+		return "Arguments are inconsistent (for example, a valid context requires buffers not supplied by a valid surface).";
+		break;
+	case EGL_BAD_PARAMETER:
+		return "One or more argument values are invalid.";
+		break;
+	case EGL_BAD_NATIVE_PIXMAP:
+		return "A NativePixmapType argument does not refer to a valid native pixmap.";
+		break;
+	case EGL_BAD_NATIVE_WINDOW:
+		return "A NativeWindowType argument does not refer to a valid native window.";
+		break;
+	case EGL_CONTEXT_LOST:
+		return "A power management event has occurred. The application must destroy all contexts and reinitialise OpenGL ES state and objects to continue rendering. ";
+		break;
+	default:
+		return "Unknown error";
+		break;
+	}
+}
diff --git a/libobs-opengl/gl-egl-common.h b/libobs-opengl/gl-egl-common.h
new file mode 100644
index 00000000..35811387
--- /dev/null
+++ b/libobs-opengl/gl-egl-common.h
@@ -0,0 +1,14 @@
+#pragma once
+
+#include "gl-nix.h"
+
+#include <glad/glad_egl.h>
+
+const char *gl_egl_error_to_string(EGLint error_number);
+
+struct gs_texture *
+gl_egl_create_dmabuf_image(EGLDisplay egl_display, unsigned int width,
+			   unsigned int height,
+			   enum gs_color_format color_format, uint32_t n_planes,
+			   const int *fds, const uint32_t *strides,
+			   const uint32_t *offsets, const uint64_t *modifiers);
diff --git a/libobs-opengl/gl-wayland-egl.c b/libobs-opengl/gl-wayland-egl.c
index 3968736d..299fe7ea 100644
--- a/libobs-opengl/gl-wayland-egl.c
+++ b/libobs-opengl/gl-wayland-egl.c
@@ -17,6 +17,8 @@
 
 #include "gl-wayland-egl.h"
 
+#include "gl-egl-common.h"
+
 #include <wayland-client.h>
 #include <wayland-egl.h>
 
@@ -325,17 +327,11 @@ static struct gs_texture *gl_wayland_egl_device_texture_create_from_dmabuf(
 	const uint32_t *strides, const uint32_t *offsets,
 	const uint64_t *modifiers)
 {
-	UNUSED_PARAMETER(device);
-	UNUSED_PARAMETER(width);
-	UNUSED_PARAMETER(height);
-	UNUSED_PARAMETER(color_format);
-	UNUSED_PARAMETER(n_planes);
-	UNUSED_PARAMETER(fds);
-	UNUSED_PARAMETER(strides);
-	UNUSED_PARAMETER(offsets);
-	UNUSED_PARAMETER(modifiers);
-
-	return NULL;
+	struct gl_platform *plat = device->plat;
+
+	return gl_egl_create_dmabuf_image(plat->display, width, height,
+					  color_format, n_planes, fds, strides,
+					  offsets, modifiers);
 }
 
 static const struct gl_winsys_vtable egl_wayland_winsys_vtable = {
diff --git a/libobs-opengl/gl-x11-egl.c b/libobs-opengl/gl-x11-egl.c
index 7a9c5975..d56f6b71 100644
--- a/libobs-opengl/gl-x11-egl.c
+++ b/libobs-opengl/gl-x11-egl.c
@@ -29,6 +29,7 @@
 
 #include <stdio.h>
 
+#include "gl-egl-common.h"
 #include "gl-x11-egl.h"
 
 #include <glad/glad_egl.h>
@@ -640,17 +641,11 @@ static struct gs_texture *gl_x11_egl_device_texture_create_from_dmabuf(
 	const uint32_t *strides, const uint32_t *offsets,
 	const uint64_t *modifiers)
 {
-	UNUSED_PARAMETER(device);
-	UNUSED_PARAMETER(width);
-	UNUSED_PARAMETER(height);
-	UNUSED_PARAMETER(color_format);
-	UNUSED_PARAMETER(n_planes);
-	UNUSED_PARAMETER(fds);
-	UNUSED_PARAMETER(strides);
-	UNUSED_PARAMETER(offsets);
-	UNUSED_PARAMETER(modifiers);
+	struct gl_platform *plat = device->plat;
 
-	return NULL;
+	return gl_egl_create_dmabuf_image(plat->edisplay, width, height,
+					  color_format, n_planes, fds, strides,
+					  offsets, modifiers);
 }
 
 static const struct gl_winsys_vtable egl_x11_winsys_vtable = {
-- 
2.28.0