forked from obsproject/obs-studio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgl-x11-egl.c
More file actions
635 lines (529 loc) · 17 KB
/
gl-x11-egl.c
File metadata and controls
635 lines (529 loc) · 17 KB
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
/******************************************************************************
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-egl-common.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,
24,
EGL_ALPHA_SIZE,
0,
EGL_RENDERABLE_TYPE,
EGL_OPENGL_BIT,
EGL_SURFACE_TYPE,
EGL_WINDOW_BIT | EGL_PBUFFER_BIT,
EGL_NONE};
struct gl_windowinfo {
/* 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;
};
struct gl_platform {
Display *xdisplay;
EGLDisplay edisplay;
EGLConfig config;
EGLContext context;
EGLSurface pbuffer;
};
/* The following utility function is copied verbatim from GLX code. */
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;
}
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)
{
bfree(info);
}
static Display *open_windowless_display(Display *platform_display)
{
Display *display;
xcb_connection_t *xcb_conn;
if (platform_display)
display = platform_display;
else
display = XOpenDisplay(NULL);
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;
}
if (!gladLoadEGL()) {
blog(LOG_ERROR, "Unable to load EGL entry functions.");
goto error;
}
return display;
error:
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 *platform_display = obs_get_nix_platform_display();
Display *display = open_windowless_display(platform_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 visual;
if (!geometry)
goto fail_geometry_request;
{
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_void_cookie_t window_cookie = xcb_create_window_checked(
xcb_conn, 24 /* Hardcoded? */, wid, parent, 0, 0,
geometry->width, geometry->height, 0, 0, visual, mask,
mask_values);
xcb_generic_error_t *err = xcb_request_check(xcb_conn, window_cookie);
if (err != NULL) {
char text[512];
XGetErrorText(display, err->error_code, text, sizeof(text));
blog(LOG_ERROR,
"Swapchain window creation failed: %s"
", Major opcode: %d, Minor opcode: %d",
text, err->major_code, err->minor_code);
goto fail_window_surface;
}
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->window = wid;
swap->wi->surface = surface;
xcb_map_window(xcb_conn, wid);
status = true;
goto success;
fail_window_surface:
fail_visual_id:
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);
}
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 (eglSwapInterval(device->plat->edisplay, 0) == EGL_FALSE) {
blog(LOG_ERROR, "eglSwapInterval failed");
}
if (!eglSwapBuffers(device->plat->edisplay,
device->cur_swap->wi->surface))
blog(LOG_ERROR, "Cannot swap EGL buffers: %s",
get_egl_error_string());
}
static struct gs_texture *gl_x11_egl_device_texture_create_from_dmabuf(
gs_device_t *device, unsigned int width, unsigned int height,
uint32_t drm_format, 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 gl_platform *plat = device->plat;
return gl_egl_create_dmabuf_image(plat->edisplay, width, height,
drm_format, color_format, n_planes,
fds, strides, offsets, modifiers);
}
static struct gs_texture *gl_x11_egl_device_texture_create_from_pixmap(
gs_device_t *device, uint32_t width, uint32_t height,
enum gs_color_format color_format, uint32_t target, void *pixmap)
{
struct gl_platform *plat = device->plat;
return gl_egl_create_texture_from_pixmap(plat->edisplay, width, height,
color_format, target,
(EGLClientBuffer)pixmap);
}
static bool gl_x11_egl_device_query_dmabuf_capabilities(
gs_device_t *device, enum gs_dmabuf_flags *dmabuf_flags,
uint32_t **drm_formats, size_t *n_formats)
{
struct gl_platform *plat = device->plat;
return gl_egl_query_dmabuf_capabilities(plat->xdisplay, dmabuf_flags,
drm_formats, n_formats);
}
static bool gl_x11_egl_device_query_dmabuf_modifiers_for_format(
gs_device_t *device, uint32_t drm_format, uint64_t **modifiers,
size_t *n_modifiers)
{
struct gl_platform *plat = device->plat;
return gl_egl_query_dmabuf_modifiers_for_format(
plat->xdisplay, drm_format, modifiers, n_modifiers);
}
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,
.device_texture_create_from_dmabuf =
gl_x11_egl_device_texture_create_from_dmabuf,
.device_query_dmabuf_capabilities =
gl_x11_egl_device_query_dmabuf_capabilities,
.device_query_dmabuf_modifiers_for_format =
gl_x11_egl_device_query_dmabuf_modifiers_for_format,
.device_texture_create_from_pixmap =
gl_x11_egl_device_texture_create_from_pixmap,
};
const struct gl_winsys_vtable *gl_x11_egl_get_winsys_vtable(void)
{
return &egl_x11_winsys_vtable;
}