summarylogtreecommitdiffstats
path: root/0013-libobs-opengl-Introduce-an-EGL-Wayland-renderer.patch
blob: 26ac4909d1961aa7b1680ca9ab3bbb2c9f36b982 (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
470
471
472
473
474
From 7fd9f95fee70ec8d604797db4a0de77e58c6e32d Mon Sep 17 00:00:00 2001
From: Georges Basile Stavracas Neto <georges.stavracas@gmail.com>
Date: Mon, 9 Mar 2020 21:09:11 -0300
Subject: [PATCH 13/15] libobs-opengl: Introduce an EGL/Wayland renderer

Introduce a new Wayland/EGL renderer.
---
 libobs-opengl/CMakeLists.txt   |  23 +++
 libobs-opengl/gl-nix.c         |   7 +-
 libobs-opengl/gl-nix.h         |   1 +
 libobs-opengl/gl-wayland-egl.c | 350 +++++++++++++++++++++++++++++++++
 libobs-opengl/gl-wayland-egl.h |  22 +++
 5 files changed, 402 insertions(+), 1 deletion(-)
 create mode 100644 libobs-opengl/gl-wayland-egl.c
 create mode 100644 libobs-opengl/gl-wayland-egl.h

diff --git a/libobs-opengl/CMakeLists.txt b/libobs-opengl/CMakeLists.txt
index f84636cf..0f694b6c 100644
--- a/libobs-opengl/CMakeLists.txt
+++ b/libobs-opengl/CMakeLists.txt
@@ -52,6 +52,29 @@ else()
 		gl-nix.c
 		gl-x11-egl.c
 		gl-x11-glx.c)
+
+	if(ENABLE_WAYLAND)
+		find_package(EGL REQUIRED)
+		find_package(Wayland REQUIRED)
+
+		include_directories(
+			${WAYLAND_CLIENT_INCLUDE_DIRS}
+			${WAYLAND_EGL_INCLUDE_DIRS}
+			${EGL_INCLUDE_DIRS})
+
+		add_definitions(
+			${WAYLAND_DEFINITIONS})
+
+		set(libobs-opengl_PLATFORM_DEPS
+			${libobs-opengl_PLATFORM_DEPS}
+			${WAYLAND_CLIENT_LIBRARIES}
+			${WAYLAND_EGL_LIBRARIES}
+			${EGL_LIBRARIES})
+
+		set(libobs-opengl_PLATFORM_SOURCES
+			${libobs-opengl_PLATFORM_SOURCES}
+			gl-wayland-egl.c)
+	endif()
 endif()
 
 set(libobs-opengl_SOURCES
diff --git a/libobs-opengl/gl-nix.c b/libobs-opengl/gl-nix.c
index 581e16a4..6c272c3d 100644
--- a/libobs-opengl/gl-nix.c
+++ b/libobs-opengl/gl-nix.c
@@ -19,6 +19,10 @@
 #include "gl-x11-glx.h"
 #include "gl-x11-egl.h"
 
+#ifdef ENABLE_WAYLAND
+#include "gl-wayland-egl.h"
+#endif
+
 static const struct gl_winsys_vtable *gl_vtable = NULL;
 
 static void init_winsys(void)
@@ -34,7 +38,8 @@ static void init_winsys(void)
 		break;
 #ifdef ENABLE_WAYLAND
 	case OBS_NIX_PLATFORM_WAYLAND:
-		blog(LOG_ERROR, "EGL/Wayland not implemented yet");
+		gl_vtable = gl_wayland_egl_get_winsys_vtable();
+		blog(LOG_INFO, "Using EGL/Wayland");
 		break;
 #endif
 	}
diff --git a/libobs-opengl/gl-nix.h b/libobs-opengl/gl-nix.h
index f5532719..741154da 100644
--- a/libobs-opengl/gl-nix.h
+++ b/libobs-opengl/gl-nix.h
@@ -17,6 +17,7 @@
 
 #pragma once
 
+#include <obs.h>
 #include <obs-nix-platform.h>
 
 #include "gl-subsystem.h"
diff --git a/libobs-opengl/gl-wayland-egl.c b/libobs-opengl/gl-wayland-egl.c
new file mode 100644
index 00000000..98bb483f
--- /dev/null
+++ b/libobs-opengl/gl-wayland-egl.c
@@ -0,0 +1,350 @@
+/******************************************************************************
+    Copyright (C) 2019 by Jason Francis <cycl0ps@tuta.io>
+
+    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-wayland-egl.h"
+
+#include <wayland-client.h>
+#include <wayland-egl.h>
+
+#include <EGL/egl.h>
+#include <EGL/eglplatform.h>
+#include <EGL/eglext.h>
+
+static const EGLint config_attribs[] = {EGL_SURFACE_TYPE,
+					EGL_WINDOW_BIT,
+					EGL_RENDERABLE_TYPE,
+					EGL_OPENGL_BIT,
+					EGL_STENCIL_SIZE,
+					0,
+					EGL_DEPTH_SIZE,
+					0,
+					EGL_BUFFER_SIZE,
+					32,
+					EGL_ALPHA_SIZE,
+					8,
+					EGL_NATIVE_RENDERABLE,
+					EGL_TRUE,
+					EGL_NONE};
+
+static const EGLint ctx_attribs[] = {
+#ifdef _DEBUG
+	EGL_CONTEXT_OPENGL_DEBUG,
+	EGL_TRUE,
+#endif
+	EGL_CONTEXT_OPENGL_PROFILE_MASK,
+	EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT,
+	EGL_CONTEXT_MAJOR_VERSION,
+	3,
+	EGL_CONTEXT_MINOR_VERSION,
+	3,
+	EGL_NONE};
+
+#ifdef EGL_KHR_create_context
+static const EGLint khr_ctx_attribs[] = {
+#ifdef _DEBUG
+	EGL_CONTEXT_FLAGS_KHR,
+	EGL_CONTEXT_OPENGL_DEBUG_BIT_KHR,
+#endif
+	EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR,
+	EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR,
+	EGL_CONTEXT_MAJOR_VERSION_KHR,
+	3,
+	EGL_CONTEXT_MINOR_VERSION_KHR,
+	3,
+	EGL_NONE};
+#endif
+
+struct gl_windowinfo {
+	struct wl_egl_window *window;
+	EGLSurface egl_surface;
+};
+
+struct gl_platform {
+	struct wl_display *wl_display;
+	EGLDisplay display;
+	EGLConfig config;
+	EGLContext context;
+};
+
+struct gl_windowinfo *
+gl_wayland_egl_windowinfo_create(const struct gs_init_data *info)
+{
+	struct wl_egl_window *window =
+		wl_egl_window_create(info->window.display, info->cx, info->cy);
+	if (window == NULL) {
+		blog(LOG_ERROR, "wl_egl_window_create failed");
+		return NULL;
+	}
+
+	struct gl_windowinfo *wi = bmalloc(sizeof(struct gl_windowinfo));
+	wi->window = window;
+	return wi;
+}
+
+static void gl_wayland_egl_windowinfo_destroy(struct gl_windowinfo *info)
+{
+	wl_egl_window_destroy(info->window);
+	bfree(info);
+}
+
+static bool egl_make_current(EGLDisplay display, EGLSurface surface,
+			     EGLContext context)
+{
+	if (eglBindAPI(EGL_OPENGL_API) == EGL_FALSE) {
+		blog(LOG_ERROR, "eglBindAPI failed");
+	}
+
+	if (!eglMakeCurrent(display, surface, surface, context)) {
+		blog(LOG_ERROR, "eglMakeCurrent failed");
+		return false;
+	}
+	return true;
+}
+
+static bool egl_context_create(struct gl_platform *plat, const EGLint *attribs)
+{
+	bool success = false;
+	EGLint num_config;
+
+	if (eglBindAPI(EGL_OPENGL_API) == EGL_FALSE) {
+		blog(LOG_ERROR, "eglBindAPI failed");
+	}
+
+	EGLBoolean result = eglChooseConfig(plat->display, config_attribs,
+					    &plat->config, 1, &num_config);
+	if (result != EGL_TRUE || num_config == 0) {
+		blog(LOG_ERROR, "eglChooseConfig failed");
+		goto error;
+	}
+
+	plat->context = eglCreateContext(plat->display, plat->config,
+					 EGL_NO_CONTEXT, attribs);
+	if (plat->context == EGL_NO_CONTEXT) {
+		blog(LOG_ERROR, "eglCreateContext failed");
+		goto error;
+	}
+
+	success =
+		egl_make_current(plat->display, EGL_NO_SURFACE, plat->context);
+
+error:
+	return success;
+}
+
+static void egl_context_destroy(struct gl_platform *plat)
+{
+	egl_make_current(plat->display, EGL_NO_SURFACE, EGL_NO_CONTEXT);
+	eglDestroyContext(plat->display, plat->context);
+}
+
+static bool extension_supported(const char *extensions, const char *search)
+{
+	const char *result = strstr(extensions, search);
+	unsigned long len = strlen(search);
+	return result != NULL &&
+	       (result == extensions || *(result - 1) == ' ') &&
+	       (result[len] == ' ' || result[len] == '\0');
+}
+
+static struct gl_platform *gl_wayland_egl_platform_create(gs_device_t *device,
+							  uint32_t adapter)
+{
+	struct gl_platform *plat = bmalloc(sizeof(struct gl_platform));
+
+	plat->wl_display = obs_get_nix_platform_display();
+
+	device->plat = plat;
+
+	plat->display = eglGetDisplay(plat->wl_display);
+	if (plat->display == EGL_NO_DISPLAY) {
+		blog(LOG_ERROR, "eglGetDisplay failed");
+		goto fail_display_init;
+	}
+
+	EGLint major;
+	EGLint minor;
+
+	if (eglInitialize(plat->display, &major, &minor) == EGL_FALSE) {
+		blog(LOG_ERROR, "eglInitialize failed");
+		goto fail_display_init;
+	}
+
+	blog(LOG_INFO, "Initialized EGL %d.%d", major, minor);
+
+	const char *extensions = eglQueryString(plat->display, EGL_EXTENSIONS);
+	blog(LOG_DEBUG, "Supported EGL Extensions: %s", extensions);
+
+	const EGLint *attribs = ctx_attribs;
+	if (major == 1 && minor == 4) {
+#ifdef EGL_KHR_create_context
+		if (extension_supported(extensions, "EGL_KHR_create_context")) {
+			attribs = khr_ctx_attribs;
+		} else {
+#endif
+			blog(LOG_ERROR,
+			     "EGL_KHR_create_context extension is required to use EGL 1.4.");
+			goto fail_context_create;
+#ifdef EGL_KHR_create_context
+		}
+#endif
+	} else if (major < 1 || (major == 1 && minor < 4)) {
+		blog(LOG_ERROR, "EGL 1.4 or higher is required.");
+		goto fail_context_create;
+	}
+
+	if (!egl_context_create(plat, attribs)) {
+		goto fail_context_create;
+	}
+
+	if (!gladLoadGL()) {
+		blog(LOG_ERROR, "Failed to load OpenGL entry functions.");
+		goto fail_load_gl;
+	}
+
+	goto success;
+
+fail_load_gl:
+	egl_context_destroy(plat);
+fail_context_create:
+	eglTerminate(plat->display);
+fail_display_init:
+	bfree(plat);
+	plat = NULL;
+success:
+	UNUSED_PARAMETER(adapter);
+	return plat;
+}
+
+static void gl_wayland_egl_platform_destroy(struct gl_platform *plat)
+{
+	if (plat) {
+		egl_context_destroy(plat);
+		eglTerminate(plat->display);
+		bfree(plat);
+	}
+}
+
+static bool gl_wayland_egl_platform_init_swapchain(struct gs_swap_chain *swap)
+{
+	struct gl_platform *plat = swap->device->plat;
+	EGLSurface egl_surface = eglCreateWindowSurface(
+		plat->display, plat->config, swap->wi->window, NULL);
+	if (egl_surface == EGL_NO_SURFACE) {
+		blog(LOG_ERROR, "eglCreateWindowSurface failed");
+		return false;
+	}
+	swap->wi->egl_surface = egl_surface;
+	return true;
+}
+
+static void
+gl_wayland_egl_platform_cleanup_swapchain(struct gs_swap_chain *swap)
+{
+	struct gl_platform *plat = swap->device->plat;
+	eglDestroySurface(plat->display, swap->wi->egl_surface);
+}
+
+static void gl_wayland_egl_device_enter_context(gs_device_t *device)
+{
+	struct gl_platform *plat = device->plat;
+	EGLSurface surface = EGL_NO_SURFACE;
+	if (device->cur_swap != NULL)
+		surface = device->cur_swap->wi->egl_surface;
+	egl_make_current(plat->display, surface, plat->context);
+}
+
+static void gl_wayland_egl_device_leave_context(gs_device_t *device)
+{
+	struct gl_platform *plat = device->plat;
+	egl_make_current(plat->display, EGL_NO_SURFACE, EGL_NO_CONTEXT);
+}
+
+static void *gl_wayland_egl_device_get_device_obj(gs_device_t *device)
+{
+	return device->plat->context;
+}
+
+static void gl_wayland_egl_getclientsize(const struct gs_swap_chain *swap,
+					 uint32_t *width, uint32_t *height)
+{
+	wl_egl_window_get_attached_size(swap->wi->window, (void *)width,
+					(void *)height);
+}
+
+static void gl_wayland_egl_clear_context(gs_device_t *device)
+{
+	struct gl_platform *plat = device->plat;
+	egl_make_current(plat->display, EGL_NO_SURFACE, EGL_NO_CONTEXT);
+}
+
+static void gl_wayland_egl_update(gs_device_t *device)
+{
+	wl_egl_window_resize(device->cur_swap->wi->window,
+			     device->cur_swap->info.cx,
+			     device->cur_swap->info.cy, 0, 0);
+}
+
+static void gl_wayland_egl_device_load_swapchain(gs_device_t *device,
+						 gs_swapchain_t *swap)
+{
+	if (device->cur_swap == swap)
+		return;
+
+	device->cur_swap = swap;
+
+	struct gl_platform *plat = device->plat;
+	if (swap == NULL) {
+		egl_make_current(plat->display, EGL_NO_SURFACE, EGL_NO_CONTEXT);
+	} else {
+		egl_make_current(plat->display, swap->wi->egl_surface,
+				 plat->context);
+	}
+}
+
+static void gl_wayland_egl_device_present(gs_device_t *device)
+{
+	struct gl_platform *plat = device->plat;
+	struct gl_windowinfo *wi = device->cur_swap->wi;
+	if (eglSwapInterval(plat->display, 0) == EGL_FALSE) {
+		blog(LOG_ERROR, "eglSwapInterval failed");
+	}
+	if (eglSwapBuffers(plat->display, wi->egl_surface) == EGL_FALSE) {
+		blog(LOG_ERROR, "eglSwapBuffers failed");
+	}
+}
+
+static const struct gl_winsys_vtable egl_wayland_winsys_vtable = {
+	.windowinfo_create = gl_wayland_egl_windowinfo_create,
+	.windowinfo_destroy = gl_wayland_egl_windowinfo_destroy,
+	.platform_create = gl_wayland_egl_platform_create,
+	.platform_destroy = gl_wayland_egl_platform_destroy,
+	.platform_init_swapchain = gl_wayland_egl_platform_init_swapchain,
+	.platform_cleanup_swapchain = gl_wayland_egl_platform_cleanup_swapchain,
+	.device_enter_context = gl_wayland_egl_device_enter_context,
+	.device_leave_context = gl_wayland_egl_device_leave_context,
+	.device_get_device_obj = gl_wayland_egl_device_get_device_obj,
+	.getclientsize = gl_wayland_egl_getclientsize,
+	.clear_context = gl_wayland_egl_clear_context,
+	.update = gl_wayland_egl_update,
+	.device_load_swapchain = gl_wayland_egl_device_load_swapchain,
+	.device_present = gl_wayland_egl_device_present,
+};
+
+const struct gl_winsys_vtable *gl_wayland_egl_get_winsys_vtable(void)
+{
+	return &egl_wayland_winsys_vtable;
+}
diff --git a/libobs-opengl/gl-wayland-egl.h b/libobs-opengl/gl-wayland-egl.h
new file mode 100644
index 00000000..3384576f
--- /dev/null
+++ b/libobs-opengl/gl-wayland-egl.h
@@ -0,0 +1,22 @@
+/******************************************************************************
+    Copyright (C) 2019 by Jason Francis <cycl0ps@tuta.io>
+
+    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/>.
+******************************************************************************/
+
+#pragma once
+
+#include "gl-nix.h"
+
+const struct gl_winsys_vtable *gl_wayland_egl_get_winsys_vtable(void);
-- 
2.28.0