summarylogtreecommitdiffstats
path: root/0004-libobs-opengl-Introduce-the-X11-EGL-winsys.patch
blob: 564813fe3bcf7ee03524071d3bf2cbdd28bd37df (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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
From 25ae9766e63d059a10bdaa1ce267367e24e455d5 Mon Sep 17 00:00:00 2001
From: Georges Basile Stavracas Neto <georges.stavracas@gmail.com>
Date: Mon, 9 Mar 2020 17:35:49 -0300
Subject: [PATCH 04/24] libobs-opengl: Introduce the X11/EGL winsys

Introduce the EGL/X11 winsys, and use it when the OBS_USE_EGL environment
variable is defined. This variable is only temporary, for future commits
will add a proper concept of platform.

All the EGL/X11 code is authored by Ivan Avdeev <me@w23.ru>.
---
 libobs-opengl/CMakeLists.txt |   1 +
 libobs-opengl/gl-nix.c       |   8 +-
 libobs-opengl/gl-x11-egl.c   | 651 +++++++++++++++++++++++++++++++++++
 libobs-opengl/gl-x11-egl.h   |  22 ++
 4 files changed, 681 insertions(+), 1 deletion(-)
 create mode 100644 libobs-opengl/gl-x11-egl.c
 create mode 100644 libobs-opengl/gl-x11-egl.h

diff --git a/libobs-opengl/CMakeLists.txt b/libobs-opengl/CMakeLists.txt
index 50afb923..9c645c3d 100644
--- a/libobs-opengl/CMakeLists.txt
+++ b/libobs-opengl/CMakeLists.txt
@@ -50,6 +50,7 @@ else() #This needs to change to be more specific to get ready for Wayland
 
 	set(libobs-opengl_PLATFORM_SOURCES
 		gl-nix.c
+		gl-x11-egl.c
 		gl-x11-glx.c)
 endif()
 
diff --git a/libobs-opengl/gl-nix.c b/libobs-opengl/gl-nix.c
index 574d4f77..4b616ef1 100644
--- a/libobs-opengl/gl-nix.c
+++ b/libobs-opengl/gl-nix.c
@@ -17,6 +17,7 @@
 
 #include "gl-nix.h"
 #include "gl-x11-glx.h"
+#include "gl-x11-egl.h"
 
 static const struct gl_winsys_vtable *gl_vtable = NULL;
 
@@ -24,7 +25,12 @@ static void init_winsys(void)
 {
 	assert(gl_vtable == NULL);
 
-	gl_vtable = gl_x11_glx_get_winsys_vtable();
+	if (getenv("OBS_USE_EGL")) {
+		gl_vtable = gl_x11_egl_get_winsys_vtable();
+		blog(LOG_INFO, "Using EGL/X11");
+	} else {
+		gl_vtable = gl_x11_glx_get_winsys_vtable();
+	}
 
 	assert(gl_vtable != NULL);
 }
diff --git a/libobs-opengl/gl-x11-egl.c b/libobs-opengl/gl-x11-egl.c
new file mode 100644
index 00000000..5b413995
--- /dev/null
+++ b/libobs-opengl/gl-x11-egl.c
@@ -0,0 +1,651 @@
+/******************************************************************************
+    Copyright (C) 2019 by Ivan Avdeev <me@w23.ru>
+
+    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/>.
+******************************************************************************/
+
+/* GL context initialization using EGL instead of GLX
+ * Which is essential for improved and more performant screen grabbing and
+ * VA-API feeding techniques.
+ *
+ * Note: most of x11-related functionality was taken from gl-x11.c
+ */
+
+#include <X11/Xlib.h>
+#include <X11/Xlib-xcb.h>
+
+#include <xcb/xcb.h>
+
+#include <stdio.h>
+
+#include "gl-x11-egl.h"
+
+#include <glad/glad_egl.h>
+
+typedef EGLDisplay(EGLAPIENTRYP PFNEGLGETPLATFORMDISPLAYEXTPROC)(
+	EGLenum platform, void *native_display, const EGLint *attrib_list);
+
+static const int 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,
+};
+
+static int ctx_pbuffer_attribs[] = {EGL_WIDTH, 2, EGL_HEIGHT, 2, EGL_NONE};
+
+static const EGLint ctx_config_attribs[] = {EGL_STENCIL_SIZE,
+					    0,
+					    EGL_DEPTH_SIZE,
+					    0,
+					    EGL_BUFFER_SIZE,
+					    32,
+					    EGL_ALPHA_SIZE,
+					    8,
+					    EGL_RENDERABLE_TYPE,
+					    EGL_OPENGL_BIT,
+					    EGL_SURFACE_TYPE,
+					    EGL_WINDOW_BIT | EGL_PBUFFER_BIT,
+					    EGL_NONE};
+
+struct gl_windowinfo {
+	EGLConfig config;
+
+	/* Windows in X11 are defined with integers (XID).
+	 * xcb_window_t is a define for this... they are
+	 * compatible with Xlib as well.
+	 */
+	xcb_window_t window;
+	EGLSurface surface;
+
+	/* We can't fetch screen without a request so we cache it. */
+	int screen;
+};
+
+struct gl_platform {
+	Display *xdisplay;
+	EGLDisplay edisplay;
+	EGLConfig config;
+	EGLContext context;
+	EGLSurface pbuffer;
+};
+
+/* The following utility functions are copied verbatim from GLX code. */
+
+/*
+ * Since we cannot take advantage of the asynchronous nature of xcb,
+ * all of the helper functions are synchronous but thread-safe.
+ *
+ * They check for errors and will return 0 on problems
+ * with the exception of when 0 is a valid return value... in which case
+ * read the specific function comments.
+ */
+
+/* Returns -1 on invalid screen. */
+static int get_screen_num_from_xcb_screen(xcb_connection_t *xcb_conn,
+					  xcb_screen_t *screen)
+{
+	xcb_screen_iterator_t iter =
+		xcb_setup_roots_iterator(xcb_get_setup(xcb_conn));
+	int screen_num = 0;
+
+	for (; iter.rem; xcb_screen_next(&iter), ++screen_num)
+		if (iter.data == screen)
+			return screen_num;
+
+	return -1;
+}
+
+static xcb_screen_t *get_screen_from_root(xcb_connection_t *xcb_conn,
+					  xcb_window_t root)
+{
+	xcb_screen_iterator_t iter =
+		xcb_setup_roots_iterator(xcb_get_setup(xcb_conn));
+
+	while (iter.rem) {
+		if (iter.data->root == root)
+			return iter.data;
+
+		xcb_screen_next(&iter);
+	}
+
+	return 0;
+}
+
+static inline int get_screen_num_from_root(xcb_connection_t *xcb_conn,
+					   xcb_window_t root)
+{
+	xcb_screen_t *screen = get_screen_from_root(xcb_conn, root);
+
+	if (!screen)
+		return -1;
+
+	return get_screen_num_from_xcb_screen(xcb_conn, screen);
+}
+
+static xcb_get_geometry_reply_t *get_window_geometry(xcb_connection_t *xcb_conn,
+						     xcb_drawable_t drawable)
+{
+	xcb_get_geometry_cookie_t cookie;
+	xcb_generic_error_t *error;
+	xcb_get_geometry_reply_t *reply;
+
+	cookie = xcb_get_geometry(xcb_conn, drawable);
+	reply = xcb_get_geometry_reply(xcb_conn, cookie, &error);
+
+	if (error) {
+		blog(LOG_ERROR, "Failed to fetch parent window geometry!");
+		free(error);
+		free(reply);
+		return 0;
+	}
+
+	free(error);
+	return reply;
+}
+
+static const char *get_egl_error_string2(const EGLint error)
+{
+	switch (error) {
+#define OBS_EGL_CASE_ERROR(e) \
+	case e:               \
+		return #e;
+		OBS_EGL_CASE_ERROR(EGL_SUCCESS)
+		OBS_EGL_CASE_ERROR(EGL_NOT_INITIALIZED)
+		OBS_EGL_CASE_ERROR(EGL_BAD_ACCESS)
+		OBS_EGL_CASE_ERROR(EGL_BAD_ALLOC)
+		OBS_EGL_CASE_ERROR(EGL_BAD_ATTRIBUTE)
+		OBS_EGL_CASE_ERROR(EGL_BAD_CONTEXT)
+		OBS_EGL_CASE_ERROR(EGL_BAD_CONFIG)
+		OBS_EGL_CASE_ERROR(EGL_BAD_CURRENT_SURFACE)
+		OBS_EGL_CASE_ERROR(EGL_BAD_DISPLAY)
+		OBS_EGL_CASE_ERROR(EGL_BAD_SURFACE)
+		OBS_EGL_CASE_ERROR(EGL_BAD_MATCH)
+		OBS_EGL_CASE_ERROR(EGL_BAD_PARAMETER)
+		OBS_EGL_CASE_ERROR(EGL_BAD_NATIVE_PIXMAP)
+		OBS_EGL_CASE_ERROR(EGL_BAD_NATIVE_WINDOW)
+		OBS_EGL_CASE_ERROR(EGL_CONTEXT_LOST)
+#undef OBS_EGL_CASE_ERROR
+	default:
+		return "Unknown";
+	}
+}
+static const char *get_egl_error_string()
+{
+	return get_egl_error_string2(eglGetError());
+}
+
+static EGLDisplay get_egl_display(struct gl_platform *plat)
+{
+	Display *display = plat->xdisplay;
+	EGLDisplay edisplay = EGL_NO_DISPLAY;
+	const char *egl_client_extensions = NULL;
+
+	egl_client_extensions = eglQueryString(EGL_NO_DISPLAY, EGL_EXTENSIONS);
+
+	PFNEGLGETPLATFORMDISPLAYEXTPROC eglGetPlatformDisplayEXT =
+		(PFNEGLGETPLATFORMDISPLAYEXTPROC)(
+			strstr(egl_client_extensions, "EGL_EXT_platform_base")
+				? eglGetProcAddress("eglGetPlatformDisplayEXT")
+				: NULL);
+
+	if (eglGetPlatformDisplayEXT) {
+		edisplay = eglGetPlatformDisplayEXT(EGL_PLATFORM_X11_EXT,
+						    display, NULL);
+		if (EGL_NO_DISPLAY == edisplay)
+			blog(LOG_ERROR, "Failed to get EGL/X11 display");
+	}
+
+	if (EGL_NO_DISPLAY == edisplay)
+		edisplay = eglGetDisplay(display);
+
+	return edisplay;
+}
+
+static bool gl_context_create(struct gl_platform *plat)
+{
+	Display *display = plat->xdisplay;
+	int frame_buf_config_count = 0;
+	EGLDisplay edisplay = EGL_NO_DISPLAY;
+	EGLConfig config = NULL;
+	EGLContext context = EGL_NO_CONTEXT;
+	int egl_min = 0, egl_maj = 0;
+	bool success = false;
+
+	eglBindAPI(EGL_OPENGL_API);
+
+	edisplay = get_egl_display(plat);
+
+	if (EGL_NO_DISPLAY == edisplay) {
+		blog(LOG_ERROR,
+		     "Failed to get EGL display using eglGetDisplay");
+		return false;
+	}
+
+	if (!eglInitialize(edisplay, &egl_maj, &egl_min)) {
+		blog(LOG_ERROR, "Failed to initialize EGL: %s",
+		     get_egl_error_string());
+		return false;
+	}
+
+	if (!eglChooseConfig(edisplay, ctx_config_attribs, &config, 1,
+			     &frame_buf_config_count)) {
+		blog(LOG_ERROR, "Unable to find suitable EGL config: %s",
+		     get_egl_error_string());
+		goto error;
+	}
+
+	context =
+		eglCreateContext(edisplay, config, EGL_NO_CONTEXT, ctx_attribs);
+#ifdef _DEBUG
+	if (EGL_NO_CONTEXT == context) {
+		const EGLint error = eglGetError();
+		if (error == EGL_BAD_ATTRIBUTE) {
+			/* Sometimes creation fails because debug gl is not supported */
+			blog(LOG_ERROR,
+			     "Unable to create EGL context with DEBUG attrib, trying without");
+			context = eglCreateContext(edisplay, config,
+						   EGL_NO_CONTEXT,
+						   ctx_attribs + 2);
+		} else {
+			blog(LOG_ERROR, "Unable to create EGL context: %s",
+			     get_egl_error_string2(error));
+			goto error;
+		}
+	}
+#endif
+	if (EGL_NO_CONTEXT == context) {
+		blog(LOG_ERROR, "Unable to create EGL context: %s",
+		     get_egl_error_string());
+		goto error;
+	}
+
+	plat->pbuffer =
+		eglCreatePbufferSurface(edisplay, config, ctx_pbuffer_attribs);
+	if (EGL_NO_SURFACE == plat->pbuffer) {
+		blog(LOG_ERROR, "Failed to create OpenGL pbuffer: %s",
+		     get_egl_error_string());
+		goto error;
+	}
+
+	plat->edisplay = edisplay;
+	plat->config = config;
+	plat->context = context;
+
+	success = true;
+	blog(LOG_DEBUG, "Created EGLDisplay %p", plat->edisplay);
+
+error:
+	if (!success) {
+		if (EGL_NO_CONTEXT != context)
+			eglDestroyContext(edisplay, context);
+		eglTerminate(edisplay);
+	}
+
+	XSync(display, false);
+	return success;
+}
+
+static void gl_context_destroy(struct gl_platform *plat)
+{
+	eglMakeCurrent(plat->edisplay, EGL_NO_SURFACE, EGL_NO_SURFACE,
+		       EGL_NO_CONTEXT);
+	eglDestroyContext(plat->edisplay, plat->context);
+}
+
+static struct gl_windowinfo *
+gl_x11_egl_windowinfo_create(const struct gs_init_data *info)
+{
+	UNUSED_PARAMETER(info);
+	return bmalloc(sizeof(struct gl_windowinfo));
+}
+
+static void gl_x11_egl_windowinfo_destroy(struct gl_windowinfo *info)
+{
+	UNUSED_PARAMETER(info);
+	bfree(info);
+}
+
+static Display *open_windowless_display(void)
+{
+	Display *display = XOpenDisplay(NULL);
+	xcb_connection_t *xcb_conn;
+	xcb_screen_iterator_t screen_iterator;
+	xcb_screen_t *screen;
+	int screen_num;
+
+	if (!display) {
+		blog(LOG_ERROR, "Unable to open new X connection!");
+		return NULL;
+	}
+
+	xcb_conn = XGetXCBConnection(display);
+	if (!xcb_conn) {
+		blog(LOG_ERROR, "Unable to get XCB connection to main display");
+		goto error;
+	}
+
+	screen_iterator = xcb_setup_roots_iterator(xcb_get_setup(xcb_conn));
+	screen = screen_iterator.data;
+	if (!screen) {
+		blog(LOG_ERROR, "Unable to get screen root");
+		goto error;
+	}
+
+	screen_num = get_screen_num_from_root(xcb_conn, screen->root);
+	if (screen_num == -1) {
+		blog(LOG_ERROR, "Unable to get screen number from root");
+		goto error;
+	}
+
+	if (!gladLoadEGL()) {
+		blog(LOG_ERROR, "Unable to load EGL entry functions.");
+		goto error;
+	}
+
+	return display;
+
+error:
+	if (display)
+		XCloseDisplay(display);
+	return NULL;
+}
+
+static int x_error_handler(Display *display, XErrorEvent *error)
+{
+	char str1[512];
+	char str2[512];
+	char str3[512];
+	XGetErrorText(display, error->error_code, str1, sizeof(str1));
+	XGetErrorText(display, error->request_code, str2, sizeof(str2));
+	XGetErrorText(display, error->minor_code, str3, sizeof(str3));
+
+	blog(LOG_ERROR,
+	     "X Error: %s, Major opcode: %s, "
+	     "Minor opcode: %s, Serial: %lu",
+	     str1, str2, str3, error->serial);
+	return 0;
+}
+
+static struct gl_platform *gl_x11_egl_platform_create(gs_device_t *device,
+						      uint32_t adapter)
+{
+	/* There's some trickery here... we're mixing libX11, xcb, and EGL
+	   For an explanation see here: http://xcb.freedesktop.org/MixingCalls/
+	   Essentially, EGL requires Xlib. Everything else we use xcb. */
+	struct gl_platform *plat = bmalloc(sizeof(struct gl_platform));
+	Display *display = open_windowless_display();
+
+	if (!display) {
+		goto fail_display_open;
+	}
+
+	XSetEventQueueOwner(display, XCBOwnsEventQueue);
+	XSetErrorHandler(x_error_handler);
+
+	/* We assume later that cur_swap is already set. */
+	device->plat = plat;
+
+	plat->xdisplay = display;
+
+	if (!gl_context_create(plat)) {
+		blog(LOG_ERROR, "Failed to create context!");
+		goto fail_context_create;
+	}
+
+	if (!eglMakeCurrent(plat->edisplay, plat->pbuffer, plat->pbuffer,
+			    plat->context)) {
+		blog(LOG_ERROR, "Failed to make context current: %s",
+		     get_egl_error_string());
+		goto fail_make_current;
+	}
+
+	if (!gladLoadGL()) {
+		blog(LOG_ERROR, "Failed to load OpenGL entry functions.");
+		goto fail_load_gl;
+	}
+
+	goto success;
+
+fail_make_current:
+	gl_context_destroy(plat);
+fail_context_create:
+fail_load_gl:
+	XCloseDisplay(display);
+fail_display_open:
+	bfree(plat);
+	plat = NULL;
+success:
+	UNUSED_PARAMETER(adapter);
+	return plat;
+}
+
+static void gl_x11_egl_platform_destroy(struct gl_platform *plat)
+{
+	if (!plat)
+		return;
+
+	gl_context_destroy(plat);
+	eglTerminate(plat->edisplay);
+	bfree(plat);
+}
+
+static bool gl_x11_egl_platform_init_swapchain(struct gs_swap_chain *swap)
+{
+	const struct gl_platform *plat = swap->device->plat;
+	Display *display = plat->xdisplay;
+	xcb_connection_t *xcb_conn = XGetXCBConnection(display);
+	xcb_window_t wid = xcb_generate_id(xcb_conn);
+	xcb_window_t parent = swap->info.window.id;
+	xcb_get_geometry_reply_t *geometry =
+		get_window_geometry(xcb_conn, parent);
+	bool status = false;
+
+	int screen_num;
+	int visual;
+
+	if (!geometry)
+		goto fail_geometry_request;
+
+	screen_num = get_screen_num_from_root(xcb_conn, geometry->root);
+	if (screen_num == -1) {
+		goto fail_screen;
+	}
+
+	{
+		if (!eglGetConfigAttrib(plat->edisplay, plat->config,
+					EGL_NATIVE_VISUAL_ID,
+					(EGLint *)&visual)) {
+			blog(LOG_ERROR,
+			     "Cannot get visual id for EGL context: %s",
+			     get_egl_error_string());
+			goto fail_visual_id;
+		}
+	}
+
+	xcb_colormap_t colormap = xcb_generate_id(xcb_conn);
+	uint32_t mask = XCB_CW_BORDER_PIXEL | XCB_CW_COLORMAP;
+	uint32_t mask_values[] = {0, colormap, 0};
+
+	xcb_create_colormap(xcb_conn, XCB_COLORMAP_ALLOC_NONE, colormap, parent,
+			    visual);
+
+	xcb_create_window(xcb_conn, 24 /* Hardcoded? */, wid, parent, 0, 0,
+			  geometry->width, geometry->height, 0, 0, visual, mask,
+			  mask_values);
+
+	const EGLSurface surface =
+		eglCreateWindowSurface(plat->edisplay, plat->config, wid, 0);
+	if (EGL_NO_SURFACE == surface) {
+		blog(LOG_ERROR, "Cannot get window EGL surface: %s",
+		     get_egl_error_string());
+		goto fail_window_surface;
+	}
+
+	swap->wi->config = plat->config;
+	swap->wi->window = wid;
+	swap->wi->surface = surface;
+	swap->wi->screen = screen_num;
+
+	xcb_map_window(xcb_conn, wid);
+
+	status = true;
+	goto success;
+
+fail_window_surface:
+fail_visual_id:
+fail_screen:
+fail_geometry_request:
+success:
+	free(geometry);
+	return status;
+}
+
+static void gl_x11_egl_platform_cleanup_swapchain(struct gs_swap_chain *swap)
+{
+	UNUSED_PARAMETER(swap);
+	/* Really nothing to clean up? */
+}
+
+static void gl_x11_egl_device_enter_context(gs_device_t *device)
+{
+	const EGLContext context = device->plat->context;
+	const EGLDisplay display = device->plat->edisplay;
+	const EGLSurface surface = (device->cur_swap)
+					   ? device->cur_swap->wi->surface
+					   : device->plat->pbuffer;
+
+	if (!eglMakeCurrent(display, surface, surface, context))
+		blog(LOG_ERROR, "Failed to make context current: %s",
+		     get_egl_error_string());
+}
+
+static void gl_x11_egl_device_leave_context(gs_device_t *device)
+{
+	const EGLDisplay display = device->plat->edisplay;
+
+	if (!eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE,
+			    EGL_NO_CONTEXT)) {
+		blog(LOG_ERROR, "Failed to reset current context: %s",
+		     get_egl_error_string());
+	}
+}
+
+static void *gl_x11_egl_device_get_device_obj(gs_device_t *device)
+{
+	return device->plat->context;
+}
+
+static void gl_x11_egl_getclientsize(const struct gs_swap_chain *swap,
+				     uint32_t *width, uint32_t *height)
+{
+	xcb_connection_t *xcb_conn =
+		XGetXCBConnection(swap->device->plat->xdisplay);
+	xcb_window_t window = swap->wi->window;
+
+	xcb_get_geometry_reply_t *geometry =
+		get_window_geometry(xcb_conn, window);
+	if (geometry) {
+		*width = geometry->width;
+		*height = geometry->height;
+	}
+
+	free(geometry);
+}
+
+static void gl_x11_egl_update(gs_device_t *device)
+{
+	Display *display = device->plat->xdisplay;
+	xcb_window_t window = device->cur_swap->wi->window;
+
+	uint32_t values[] = {device->cur_swap->info.cx,
+			     device->cur_swap->info.cy};
+
+	xcb_configure_window(XGetXCBConnection(display), window,
+			     XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
+			     values);
+}
+
+static void gl_x11_egl_clear_context(gs_device_t *device)
+{
+	Display *display = device->plat->edisplay;
+
+	if (!eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE,
+			    EGL_NO_CONTEXT)) {
+		blog(LOG_ERROR, "Failed to reset current context.");
+	}
+}
+
+static void gl_x11_egl_device_load_swapchain(gs_device_t *device,
+					     gs_swapchain_t *swap)
+{
+	if (device->cur_swap == swap)
+		return;
+
+	device->cur_swap = swap;
+
+	device_enter_context(device);
+}
+
+enum swap_type {
+	SWAP_TYPE_NORMAL,
+	SWAP_TYPE_EXT,
+	SWAP_TYPE_MESA,
+	SWAP_TYPE_SGI,
+};
+
+static void gl_x11_egl_device_present(gs_device_t *device)
+{
+	Display *display = device->plat->xdisplay;
+
+	xcb_connection_t *xcb_conn = XGetXCBConnection(display);
+	xcb_generic_event_t *xcb_event;
+	while ((xcb_event = xcb_poll_for_event(xcb_conn))) {
+		free(xcb_event);
+	}
+
+	if (!eglSwapBuffers(device->plat->edisplay,
+			    device->cur_swap->wi->surface))
+		blog(LOG_ERROR, "Cannot swap EGL buffers: %s",
+		     get_egl_error_string());
+}
+
+static const struct gl_winsys_vtable egl_x11_winsys_vtable = {
+	.windowinfo_create = gl_x11_egl_windowinfo_create,
+	.windowinfo_destroy = gl_x11_egl_windowinfo_destroy,
+	.platform_create = gl_x11_egl_platform_create,
+	.platform_destroy = gl_x11_egl_platform_destroy,
+	.platform_init_swapchain = gl_x11_egl_platform_init_swapchain,
+	.platform_cleanup_swapchain = gl_x11_egl_platform_cleanup_swapchain,
+	.device_enter_context = gl_x11_egl_device_enter_context,
+	.device_leave_context = gl_x11_egl_device_leave_context,
+	.device_get_device_obj = gl_x11_egl_device_get_device_obj,
+	.getclientsize = gl_x11_egl_getclientsize,
+	.clear_context = gl_x11_egl_clear_context,
+	.update = gl_x11_egl_update,
+	.device_load_swapchain = gl_x11_egl_device_load_swapchain,
+	.device_present = gl_x11_egl_device_present,
+};
+
+const struct gl_winsys_vtable *gl_x11_egl_get_winsys_vtable(void)
+{
+	return &egl_x11_winsys_vtable;
+}
diff --git a/libobs-opengl/gl-x11-egl.h b/libobs-opengl/gl-x11-egl.h
new file mode 100644
index 00000000..44ab3111
--- /dev/null
+++ b/libobs-opengl/gl-x11-egl.h
@@ -0,0 +1,22 @@
+/******************************************************************************
+    Copyright (C) 2019 by Ivan Avdeev <me@w23.ru>
+
+    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_x11_egl_get_winsys_vtable(void);
-- 
2.28.0