Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/egl/main/egldisplay.c
4560 views
1
/**************************************************************************
2
*
3
* Copyright 2008 VMware, Inc.
4
* Copyright 2009-2010 Chia-I Wu <[email protected]>
5
* Copyright 2010-2011 LunarG, Inc.
6
* All Rights Reserved.
7
*
8
* Permission is hereby granted, free of charge, to any person obtaining a
9
* copy of this software and associated documentation files (the
10
* "Software"), to deal in the Software without restriction, including
11
* without limitation the rights to use, copy, modify, merge, publish,
12
* distribute, sub license, and/or sell copies of the Software, and to
13
* permit persons to whom the Software is furnished to do so, subject to
14
* the following conditions:
15
*
16
* The above copyright notice and this permission notice (including the
17
* next paragraph) shall be included in all copies or substantial portions
18
* of the Software.
19
*
20
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26
* DEALINGS IN THE SOFTWARE.
27
*
28
**************************************************************************/
29
30
31
/**
32
* Functions related to EGLDisplay.
33
*/
34
35
#include <assert.h>
36
#include <stdlib.h>
37
#include <string.h>
38
#include <unistd.h>
39
#include <fcntl.h>
40
#include "c11/threads.h"
41
#include "util/macros.h"
42
#include "util/os_file.h"
43
#include "util/u_atomic.h"
44
45
#include "eglcontext.h"
46
#include "eglcurrent.h"
47
#include "eglsurface.h"
48
#include "egldevice.h"
49
#include "egldisplay.h"
50
#include "egldriver.h"
51
#include "eglglobals.h"
52
#include "egllog.h"
53
#include "eglimage.h"
54
#include "eglsync.h"
55
56
/* Includes for _eglNativePlatformDetectNativeDisplay */
57
#ifdef HAVE_WAYLAND_PLATFORM
58
#include <wayland-client.h>
59
#endif
60
#ifdef HAVE_DRM_PLATFORM
61
#include <gbm.h>
62
#endif
63
64
65
/**
66
* Map build-system platform names to platform types.
67
*/
68
static const struct {
69
_EGLPlatformType platform;
70
const char *name;
71
} egl_platforms[] = {
72
{ _EGL_PLATFORM_X11, "x11" },
73
{ _EGL_PLATFORM_XCB, "xcb" },
74
{ _EGL_PLATFORM_WAYLAND, "wayland" },
75
{ _EGL_PLATFORM_DRM, "drm" },
76
{ _EGL_PLATFORM_ANDROID, "android" },
77
{ _EGL_PLATFORM_HAIKU, "haiku" },
78
{ _EGL_PLATFORM_SURFACELESS, "surfaceless" },
79
{ _EGL_PLATFORM_DEVICE, "device" },
80
};
81
82
83
/**
84
* Return the native platform by parsing EGL_PLATFORM.
85
*/
86
static _EGLPlatformType
87
_eglGetNativePlatformFromEnv(void)
88
{
89
_EGLPlatformType plat = _EGL_INVALID_PLATFORM;
90
const char *plat_name;
91
EGLint i;
92
93
static_assert(ARRAY_SIZE(egl_platforms) == _EGL_NUM_PLATFORMS,
94
"Missing platform");
95
96
plat_name = getenv("EGL_PLATFORM");
97
/* try deprecated env variable */
98
if (!plat_name || !plat_name[0])
99
plat_name = getenv("EGL_DISPLAY");
100
if (!plat_name || !plat_name[0])
101
return _EGL_INVALID_PLATFORM;
102
103
for (i = 0; i < ARRAY_SIZE(egl_platforms); i++) {
104
if (strcmp(egl_platforms[i].name, plat_name) == 0) {
105
plat = egl_platforms[i].platform;
106
break;
107
}
108
}
109
110
if (plat == _EGL_INVALID_PLATFORM)
111
_eglLog(_EGL_WARNING, "invalid EGL_PLATFORM given");
112
113
return plat;
114
}
115
116
117
/**
118
* Try detecting native platform with the help of native display characteristcs.
119
*/
120
static _EGLPlatformType
121
_eglNativePlatformDetectNativeDisplay(void *nativeDisplay)
122
{
123
if (nativeDisplay == EGL_DEFAULT_DISPLAY)
124
return _EGL_INVALID_PLATFORM;
125
126
if (_eglPointerIsDereferencable(nativeDisplay)) {
127
void *first_pointer = *(void **) nativeDisplay;
128
129
(void) first_pointer; /* silence unused var warning */
130
131
#ifdef HAVE_WAYLAND_PLATFORM
132
/* wl_display is a wl_proxy, which is a wl_object.
133
* wl_object's first element points to the interfacetype. */
134
if (first_pointer == &wl_display_interface)
135
return _EGL_PLATFORM_WAYLAND;
136
#endif
137
138
#ifdef HAVE_DRM_PLATFORM
139
/* gbm has a pointer to its constructor as first element. */
140
if (first_pointer == gbm_create_device)
141
return _EGL_PLATFORM_DRM;
142
#endif
143
}
144
145
return _EGL_INVALID_PLATFORM;
146
}
147
148
149
/**
150
* Return the native platform. It is the platform of the EGL native types.
151
*/
152
_EGLPlatformType
153
_eglGetNativePlatform(void *nativeDisplay)
154
{
155
_EGLPlatformType detected_platform = _eglGetNativePlatformFromEnv();
156
const char *detection_method = "environment";
157
158
if (detected_platform == _EGL_INVALID_PLATFORM) {
159
detected_platform = _eglNativePlatformDetectNativeDisplay(nativeDisplay);
160
detection_method = "autodetected";
161
}
162
163
if (detected_platform == _EGL_INVALID_PLATFORM) {
164
detected_platform = _EGL_NATIVE_PLATFORM;
165
detection_method = "build-time configuration";
166
}
167
168
_eglLog(_EGL_DEBUG, "Native platform type: %s (%s)",
169
egl_platforms[detected_platform].name, detection_method);
170
171
return detected_platform;
172
}
173
174
175
/**
176
* Finish display management.
177
*/
178
void
179
_eglFiniDisplay(void)
180
{
181
_EGLDisplay *dispList, *disp;
182
183
/* atexit function is called with global mutex locked */
184
dispList = _eglGlobal.DisplayList;
185
while (dispList) {
186
EGLint i;
187
188
/* pop list head */
189
disp = dispList;
190
dispList = dispList->Next;
191
192
for (i = 0; i < _EGL_NUM_RESOURCES; i++) {
193
if (disp->ResourceLists[i]) {
194
_eglLog(_EGL_DEBUG, "Display %p is destroyed with resources", disp);
195
break;
196
}
197
}
198
199
200
/* The fcntl() code in _eglGetDeviceDisplay() ensures that valid fd >= 3,
201
* and invalid one is 0.
202
*/
203
if (disp->Options.fd)
204
close(disp->Options.fd);
205
206
free(disp->Options.Attribs);
207
free(disp);
208
}
209
_eglGlobal.DisplayList = NULL;
210
}
211
212
static EGLBoolean
213
_eglSameAttribs(const EGLAttrib *a, const EGLAttrib *b)
214
{
215
size_t na = _eglNumAttribs(a);
216
size_t nb = _eglNumAttribs(b);
217
218
/* different numbers of attributes must be different */
219
if (na != nb)
220
return EGL_FALSE;
221
222
/* both lists NULL are the same */
223
if (!a && !b)
224
return EGL_TRUE;
225
226
/* otherwise, compare the lists */
227
return memcmp(a, b, na * sizeof(a[0])) == 0 ? EGL_TRUE : EGL_FALSE;
228
}
229
230
/**
231
* Find the display corresponding to the specified native display, or create a
232
* new one. EGL 1.5 says:
233
*
234
* Multiple calls made to eglGetPlatformDisplay with the same parameters
235
* will return the same EGLDisplay handle.
236
*
237
* We read this extremely strictly, and treat a call with NULL attribs as
238
* different from a call with attribs only equal to { EGL_NONE }. Similarly
239
* we do not sort the attribute list, so even if all attribute _values_ are
240
* identical, different attribute orders will be considered different
241
* parameters.
242
*/
243
_EGLDisplay *
244
_eglFindDisplay(_EGLPlatformType plat, void *plat_dpy,
245
const EGLAttrib *attrib_list)
246
{
247
_EGLDisplay *disp;
248
size_t num_attribs;
249
250
if (plat == _EGL_INVALID_PLATFORM)
251
return NULL;
252
253
mtx_lock(_eglGlobal.Mutex);
254
255
/* search the display list first */
256
for (disp = _eglGlobal.DisplayList; disp; disp = disp->Next) {
257
if (disp->Platform == plat && disp->PlatformDisplay == plat_dpy &&
258
_eglSameAttribs(disp->Options.Attribs, attrib_list))
259
goto out;
260
}
261
262
/* create a new display */
263
assert(!disp);
264
disp = calloc(1, sizeof(_EGLDisplay));
265
if (!disp)
266
goto out;
267
268
mtx_init(&disp->Mutex, mtx_plain);
269
disp->Platform = plat;
270
disp->PlatformDisplay = plat_dpy;
271
num_attribs = _eglNumAttribs(attrib_list);
272
if (num_attribs) {
273
disp->Options.Attribs = calloc(num_attribs, sizeof(EGLAttrib));
274
if (!disp->Options.Attribs) {
275
free(disp);
276
disp = NULL;
277
goto out;
278
}
279
memcpy(disp->Options.Attribs, attrib_list,
280
num_attribs * sizeof(EGLAttrib));
281
}
282
283
/* add to the display list */
284
disp->Next = _eglGlobal.DisplayList;
285
_eglGlobal.DisplayList = disp;
286
287
out:
288
mtx_unlock(_eglGlobal.Mutex);
289
290
return disp;
291
}
292
293
294
/**
295
* Destroy the contexts and surfaces that are linked to the display.
296
*/
297
void
298
_eglReleaseDisplayResources(_EGLDisplay *display)
299
{
300
_EGLResource *list;
301
const _EGLDriver *drv = display->Driver;
302
303
list = display->ResourceLists[_EGL_RESOURCE_CONTEXT];
304
while (list) {
305
_EGLContext *ctx = (_EGLContext *) list;
306
list = list->Next;
307
308
_eglUnlinkContext(ctx);
309
drv->DestroyContext(display, ctx);
310
}
311
assert(!display->ResourceLists[_EGL_RESOURCE_CONTEXT]);
312
313
list = display->ResourceLists[_EGL_RESOURCE_SURFACE];
314
while (list) {
315
_EGLSurface *surf = (_EGLSurface *) list;
316
list = list->Next;
317
318
_eglUnlinkSurface(surf);
319
drv->DestroySurface(display, surf);
320
}
321
assert(!display->ResourceLists[_EGL_RESOURCE_SURFACE]);
322
323
list = display->ResourceLists[_EGL_RESOURCE_IMAGE];
324
while (list) {
325
_EGLImage *image = (_EGLImage *) list;
326
list = list->Next;
327
328
_eglUnlinkImage(image);
329
drv->DestroyImageKHR(display, image);
330
}
331
assert(!display->ResourceLists[_EGL_RESOURCE_IMAGE]);
332
333
list = display->ResourceLists[_EGL_RESOURCE_SYNC];
334
while (list) {
335
_EGLSync *sync = (_EGLSync *) list;
336
list = list->Next;
337
338
_eglUnlinkSync(sync);
339
drv->DestroySyncKHR(display, sync);
340
}
341
assert(!display->ResourceLists[_EGL_RESOURCE_SYNC]);
342
}
343
344
345
/**
346
* Free all the data hanging of an _EGLDisplay object, but not
347
* the object itself.
348
*/
349
void
350
_eglCleanupDisplay(_EGLDisplay *disp)
351
{
352
if (disp->Configs) {
353
_eglDestroyArray(disp->Configs, free);
354
disp->Configs = NULL;
355
}
356
357
/* XXX incomplete */
358
}
359
360
361
/**
362
* Return EGL_TRUE if the given handle is a valid handle to a display.
363
*/
364
EGLBoolean
365
_eglCheckDisplayHandle(EGLDisplay dpy)
366
{
367
_EGLDisplay *cur;
368
369
mtx_lock(_eglGlobal.Mutex);
370
cur = _eglGlobal.DisplayList;
371
while (cur) {
372
if (cur == (_EGLDisplay *) dpy)
373
break;
374
cur = cur->Next;
375
}
376
mtx_unlock(_eglGlobal.Mutex);
377
return (cur != NULL);
378
}
379
380
381
/**
382
* Return EGL_TRUE if the given resource is valid. That is, the display does
383
* own the resource.
384
*/
385
EGLBoolean
386
_eglCheckResource(void *res, _EGLResourceType type, _EGLDisplay *disp)
387
{
388
_EGLResource *list = disp->ResourceLists[type];
389
390
if (!res)
391
return EGL_FALSE;
392
393
while (list) {
394
if (res == (void *) list) {
395
assert(list->Display == disp);
396
break;
397
}
398
list = list->Next;
399
}
400
401
return (list != NULL);
402
}
403
404
405
/**
406
* Initialize a display resource. The size of the subclass object is
407
* specified.
408
*
409
* This is supposed to be called from the initializers of subclasses, such as
410
* _eglInitContext or _eglInitSurface.
411
*/
412
void
413
_eglInitResource(_EGLResource *res, EGLint size, _EGLDisplay *disp)
414
{
415
memset(res, 0, size);
416
res->Display = disp;
417
res->RefCount = 1;
418
}
419
420
421
/**
422
* Increment reference count for the resource.
423
*/
424
void
425
_eglGetResource(_EGLResource *res)
426
{
427
assert(res && res->RefCount > 0);
428
/* hopefully a resource is always manipulated with its display locked */
429
res->RefCount++;
430
}
431
432
433
/**
434
* Decrement reference count for the resource.
435
*/
436
EGLBoolean
437
_eglPutResource(_EGLResource *res)
438
{
439
assert(res && res->RefCount > 0);
440
res->RefCount--;
441
return (!res->RefCount);
442
}
443
444
445
/**
446
* Link a resource to its display.
447
*/
448
void
449
_eglLinkResource(_EGLResource *res, _EGLResourceType type)
450
{
451
assert(res->Display);
452
453
res->IsLinked = EGL_TRUE;
454
res->Next = res->Display->ResourceLists[type];
455
res->Display->ResourceLists[type] = res;
456
_eglGetResource(res);
457
}
458
459
460
/**
461
* Unlink a linked resource from its display.
462
*/
463
void
464
_eglUnlinkResource(_EGLResource *res, _EGLResourceType type)
465
{
466
_EGLResource *prev;
467
468
prev = res->Display->ResourceLists[type];
469
if (prev != res) {
470
while (prev) {
471
if (prev->Next == res)
472
break;
473
prev = prev->Next;
474
}
475
assert(prev);
476
prev->Next = res->Next;
477
}
478
else {
479
res->Display->ResourceLists[type] = res->Next;
480
}
481
482
res->Next = NULL;
483
res->IsLinked = EGL_FALSE;
484
_eglPutResource(res);
485
486
/* We always unlink before destroy. The driver still owns a reference */
487
assert(res->RefCount);
488
}
489
490
#ifdef HAVE_X11_PLATFORM
491
_EGLDisplay*
492
_eglGetX11Display(Display *native_display,
493
const EGLAttrib *attrib_list)
494
{
495
/* EGL_EXT_platform_x11 recognizes exactly one attribute,
496
* EGL_PLATFORM_X11_SCREEN_EXT, which is optional.
497
*/
498
if (attrib_list != NULL) {
499
for (int i = 0; attrib_list[i] != EGL_NONE; i += 2) {
500
if (attrib_list[i] != EGL_PLATFORM_X11_SCREEN_EXT) {
501
_eglError(EGL_BAD_ATTRIBUTE, "eglGetPlatformDisplay");
502
return NULL;
503
}
504
}
505
}
506
return _eglFindDisplay(_EGL_PLATFORM_X11, native_display, attrib_list);
507
}
508
#endif /* HAVE_X11_PLATFORM */
509
510
#ifdef HAVE_XCB_PLATFORM
511
_EGLDisplay*
512
_eglGetXcbDisplay(xcb_connection_t *native_display,
513
const EGLAttrib *attrib_list)
514
{
515
/* EGL_EXT_platform_xcb recognizes exactly one attribute,
516
* EGL_PLATFORM_XCB_SCREEN_EXT, which is optional.
517
*/
518
if (attrib_list != NULL) {
519
for (int i = 0; attrib_list[i] != EGL_NONE; i += 2) {
520
if (attrib_list[i] != EGL_PLATFORM_XCB_SCREEN_EXT) {
521
_eglError(EGL_BAD_ATTRIBUTE, "eglGetPlatformDisplay");
522
return NULL;
523
}
524
}
525
}
526
527
return _eglFindDisplay(_EGL_PLATFORM_XCB, native_display, attrib_list);
528
}
529
#endif /* HAVE_XCB_PLATFORM */
530
531
#ifdef HAVE_DRM_PLATFORM
532
_EGLDisplay*
533
_eglGetGbmDisplay(struct gbm_device *native_display,
534
const EGLAttrib *attrib_list)
535
{
536
/* EGL_MESA_platform_gbm recognizes no attributes. */
537
if (attrib_list != NULL && attrib_list[0] != EGL_NONE) {
538
_eglError(EGL_BAD_ATTRIBUTE, "eglGetPlatformDisplay");
539
return NULL;
540
}
541
542
return _eglFindDisplay(_EGL_PLATFORM_DRM, native_display, attrib_list);
543
}
544
#endif /* HAVE_DRM_PLATFORM */
545
546
#ifdef HAVE_WAYLAND_PLATFORM
547
_EGLDisplay*
548
_eglGetWaylandDisplay(struct wl_display *native_display,
549
const EGLAttrib *attrib_list)
550
{
551
/* EGL_EXT_platform_wayland recognizes no attributes. */
552
if (attrib_list != NULL && attrib_list[0] != EGL_NONE) {
553
_eglError(EGL_BAD_ATTRIBUTE, "eglGetPlatformDisplay");
554
return NULL;
555
}
556
557
return _eglFindDisplay(_EGL_PLATFORM_WAYLAND, native_display, attrib_list);
558
}
559
#endif /* HAVE_WAYLAND_PLATFORM */
560
561
_EGLDisplay*
562
_eglGetSurfacelessDisplay(void *native_display,
563
const EGLAttrib *attrib_list)
564
{
565
/* This platform has no native display. */
566
if (native_display != NULL) {
567
_eglError(EGL_BAD_PARAMETER, "eglGetPlatformDisplay");
568
return NULL;
569
}
570
571
/* This platform recognizes no display attributes. */
572
if (attrib_list != NULL && attrib_list[0] != EGL_NONE) {
573
_eglError(EGL_BAD_ATTRIBUTE, "eglGetPlatformDisplay");
574
return NULL;
575
}
576
577
return _eglFindDisplay(_EGL_PLATFORM_SURFACELESS, native_display,
578
attrib_list);
579
}
580
581
#ifdef HAVE_ANDROID_PLATFORM
582
_EGLDisplay*
583
_eglGetAndroidDisplay(void *native_display,
584
const EGLAttrib *attrib_list)
585
{
586
587
/* This platform recognizes no display attributes. */
588
if (attrib_list != NULL && attrib_list[0] != EGL_NONE) {
589
_eglError(EGL_BAD_ATTRIBUTE, "eglGetPlatformDisplay");
590
return NULL;
591
}
592
593
return _eglFindDisplay(_EGL_PLATFORM_ANDROID, native_display,
594
attrib_list);
595
}
596
#endif /* HAVE_ANDROID_PLATFORM */
597
598
_EGLDisplay*
599
_eglGetDeviceDisplay(void *native_display,
600
const EGLAttrib *attrib_list)
601
{
602
_EGLDevice *dev;
603
_EGLDisplay *display;
604
int fd = -1;
605
606
dev = _eglLookupDevice(native_display);
607
if (!dev) {
608
_eglError(EGL_BAD_PARAMETER, "eglGetPlatformDisplay");
609
return NULL;
610
}
611
612
if (attrib_list) {
613
for (int i = 0; attrib_list[i] != EGL_NONE; i += 2) {
614
EGLAttrib attrib = attrib_list[i];
615
EGLAttrib value = attrib_list[i + 1];
616
617
/* EGL_EXT_platform_device does not recognize any attributes,
618
* EGL_EXT_device_drm adds the optional EGL_DRM_MASTER_FD_EXT.
619
*/
620
621
if (!_eglDeviceSupports(dev, _EGL_DEVICE_DRM) ||
622
attrib != EGL_DRM_MASTER_FD_EXT) {
623
_eglError(EGL_BAD_ATTRIBUTE, "eglGetPlatformDisplay");
624
return NULL;
625
}
626
627
fd = (int) value;
628
}
629
}
630
631
display = _eglFindDisplay(_EGL_PLATFORM_DEVICE, native_display, attrib_list);
632
if (!display) {
633
_eglError(EGL_BAD_ALLOC, "eglGetPlatformDisplay");
634
return NULL;
635
}
636
637
/* If the fd is explicitly provided and we did not dup() it yet, do so.
638
* The spec mandates that we do so, since we'll need it past the
639
* eglGetPlatformDispay call.
640
*
641
* The new fd is guaranteed to be 3 or greater.
642
*/
643
if (fd != -1 && display->Options.fd == 0) {
644
display->Options.fd = os_dupfd_cloexec(fd);
645
if (display->Options.fd == -1) {
646
/* Do not (really) need to teardown the display */
647
_eglError(EGL_BAD_ALLOC, "eglGetPlatformDisplay");
648
return NULL;
649
}
650
}
651
652
return display;
653
}
654
655