1 /**************************************************************************
2 *
3 * Copyright 2008 VMware, Inc.
4 * Copyright 2009-2010 Chia-I Wu <olvaffe@gmail.com>
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_WAYLAND, "wayland" },
74 { _EGL_PLATFORM_DRM, "drm" },
75 { _EGL_PLATFORM_ANDROID, "android" },
76 { _EGL_PLATFORM_HAIKU, "haiku" },
77 { _EGL_PLATFORM_SURFACELESS, "surfaceless" },
78 { _EGL_PLATFORM_DEVICE, "device" },
79 };
80
81
82 /**
83 * Return the native platform by parsing EGL_PLATFORM.
84 */
85 static _EGLPlatformType
_eglGetNativePlatformFromEnv(void)86 _eglGetNativePlatformFromEnv(void)
87 {
88 _EGLPlatformType plat = _EGL_INVALID_PLATFORM;
89 const char *plat_name;
90 EGLint i;
91
92 static_assert(ARRAY_SIZE(egl_platforms) == _EGL_NUM_PLATFORMS,
93 "Missing platform");
94
95 plat_name = getenv("EGL_PLATFORM");
96 /* try deprecated env variable */
97 if (!plat_name || !plat_name[0])
98 plat_name = getenv("EGL_DISPLAY");
99 if (!plat_name || !plat_name[0])
100 return _EGL_INVALID_PLATFORM;
101
102 for (i = 0; i < ARRAY_SIZE(egl_platforms); i++) {
103 if (strcmp(egl_platforms[i].name, plat_name) == 0) {
104 plat = egl_platforms[i].platform;
105 break;
106 }
107 }
108
109 if (plat == _EGL_INVALID_PLATFORM)
110 _eglLog(_EGL_WARNING, "invalid EGL_PLATFORM given");
111
112 return plat;
113 }
114
115
116 /**
117 * Try detecting native platform with the help of native display characteristcs.
118 */
119 static _EGLPlatformType
_eglNativePlatformDetectNativeDisplay(void * nativeDisplay)120 _eglNativePlatformDetectNativeDisplay(void *nativeDisplay)
121 {
122 if (nativeDisplay == EGL_DEFAULT_DISPLAY)
123 return _EGL_INVALID_PLATFORM;
124
125 if (_eglPointerIsDereferencable(nativeDisplay)) {
126 void *first_pointer = *(void **) nativeDisplay;
127
128 (void) first_pointer; /* silence unused var warning */
129
130 #ifdef HAVE_WAYLAND_PLATFORM
131 /* wl_display is a wl_proxy, which is a wl_object.
132 * wl_object's first element points to the interfacetype. */
133 if (first_pointer == &wl_display_interface)
134 return _EGL_PLATFORM_WAYLAND;
135 #endif
136
137 #ifdef HAVE_DRM_PLATFORM
138 /* gbm has a pointer to its constructor as first element. */
139 if (first_pointer == gbm_create_device)
140 return _EGL_PLATFORM_DRM;
141 #endif
142 }
143
144 return _EGL_INVALID_PLATFORM;
145 }
146
147
148 /**
149 * Return the native platform. It is the platform of the EGL native types.
150 */
151 _EGLPlatformType
_eglGetNativePlatform(void * nativeDisplay)152 _eglGetNativePlatform(void *nativeDisplay)
153 {
154 _EGLPlatformType detected_platform = _eglGetNativePlatformFromEnv();
155 const char *detection_method = "environment";
156
157 if (detected_platform == _EGL_INVALID_PLATFORM) {
158 detected_platform = _eglNativePlatformDetectNativeDisplay(nativeDisplay);
159 detection_method = "autodetected";
160 }
161
162 if (detected_platform == _EGL_INVALID_PLATFORM) {
163 detected_platform = _EGL_NATIVE_PLATFORM;
164 detection_method = "build-time configuration";
165 }
166
167 _eglLog(_EGL_DEBUG, "Native platform type: %s (%s)",
168 egl_platforms[detected_platform].name, detection_method);
169
170 return detected_platform;
171 }
172
173
174 /**
175 * Finish display management.
176 */
177 void
_eglFiniDisplay(void)178 _eglFiniDisplay(void)
179 {
180 _EGLDisplay *dispList, *disp;
181
182 /* atexit function is called with global mutex locked */
183 dispList = _eglGlobal.DisplayList;
184 while (dispList) {
185 EGLint i;
186
187 /* pop list head */
188 disp = dispList;
189 dispList = dispList->Next;
190
191 for (i = 0; i < _EGL_NUM_RESOURCES; i++) {
192 if (disp->ResourceLists[i]) {
193 _eglLog(_EGL_DEBUG, "Display %p is destroyed with resources", disp);
194 break;
195 }
196 }
197
198
199 /* The fcntl() code in _eglGetDeviceDisplay() ensures that valid fd >= 3,
200 * and invalid one is 0.
201 */
202 if (disp->Options.fd)
203 close(disp->Options.fd);
204
205 free(disp->Options.Attribs);
206 free(disp);
207 }
208 _eglGlobal.DisplayList = NULL;
209 }
210
211 static EGLBoolean
_eglSameAttribs(const EGLAttrib * a,const EGLAttrib * b)212 _eglSameAttribs(const EGLAttrib *a, const EGLAttrib *b)
213 {
214 size_t na = _eglNumAttribs(a);
215 size_t nb = _eglNumAttribs(b);
216
217 /* different numbers of attributes must be different */
218 if (na != nb)
219 return EGL_FALSE;
220
221 /* both lists NULL are the same */
222 if (!a && !b)
223 return EGL_TRUE;
224
225 /* otherwise, compare the lists */
226 return memcmp(a, b, na * sizeof(a[0])) == 0 ? EGL_TRUE : EGL_FALSE;
227 }
228
229 /**
230 * Find the display corresponding to the specified native display, or create a
231 * new one. EGL 1.5 says:
232 *
233 * Multiple calls made to eglGetPlatformDisplay with the same parameters
234 * will return the same EGLDisplay handle.
235 *
236 * We read this extremely strictly, and treat a call with NULL attribs as
237 * different from a call with attribs only equal to { EGL_NONE }. Similarly
238 * we do not sort the attribute list, so even if all attribute _values_ are
239 * identical, different attribute orders will be considered different
240 * parameters.
241 */
242 _EGLDisplay *
_eglFindDisplay(_EGLPlatformType plat,void * plat_dpy,const EGLAttrib * attrib_list)243 _eglFindDisplay(_EGLPlatformType plat, void *plat_dpy,
244 const EGLAttrib *attrib_list)
245 {
246 _EGLDisplay *disp;
247 size_t num_attribs;
248
249 if (plat == _EGL_INVALID_PLATFORM)
250 return NULL;
251
252 mtx_lock(_eglGlobal.Mutex);
253
254 /* search the display list first */
255 for (disp = _eglGlobal.DisplayList; disp; disp = disp->Next) {
256 if (disp->Platform == plat && disp->PlatformDisplay == plat_dpy &&
257 _eglSameAttribs(disp->Options.Attribs, attrib_list))
258 goto out;
259 }
260
261 /* create a new display */
262 assert(!disp);
263 disp = calloc(1, sizeof(_EGLDisplay));
264 if (!disp)
265 goto out;
266
267 mtx_init(&disp->Mutex, mtx_plain);
268 disp->Platform = plat;
269 disp->PlatformDisplay = plat_dpy;
270 num_attribs = _eglNumAttribs(attrib_list);
271 if (num_attribs) {
272 disp->Options.Attribs = calloc(num_attribs, sizeof(EGLAttrib));
273 if (!disp->Options.Attribs) {
274 free(disp);
275 disp = NULL;
276 goto out;
277 }
278 memcpy(disp->Options.Attribs, attrib_list,
279 num_attribs * sizeof(EGLAttrib));
280 }
281
282 /* add to the display list */
283 disp->Next = _eglGlobal.DisplayList;
284 _eglGlobal.DisplayList = disp;
285
286 out:
287 mtx_unlock(_eglGlobal.Mutex);
288
289 return disp;
290 }
291
292
293 /**
294 * Destroy the contexts and surfaces that are linked to the display.
295 */
296 void
_eglReleaseDisplayResources(_EGLDisplay * display)297 _eglReleaseDisplayResources(_EGLDisplay *display)
298 {
299 _EGLResource *list;
300 const _EGLDriver *drv = display->Driver;
301
302 list = display->ResourceLists[_EGL_RESOURCE_CONTEXT];
303 while (list) {
304 _EGLContext *ctx = (_EGLContext *) list;
305 list = list->Next;
306
307 _eglUnlinkContext(ctx);
308 drv->DestroyContext(display, ctx);
309 }
310 assert(!display->ResourceLists[_EGL_RESOURCE_CONTEXT]);
311
312 list = display->ResourceLists[_EGL_RESOURCE_SURFACE];
313 while (list) {
314 _EGLSurface *surf = (_EGLSurface *) list;
315 list = list->Next;
316
317 _eglUnlinkSurface(surf);
318 drv->DestroySurface(display, surf);
319 }
320 assert(!display->ResourceLists[_EGL_RESOURCE_SURFACE]);
321
322 list = display->ResourceLists[_EGL_RESOURCE_IMAGE];
323 while (list) {
324 _EGLImage *image = (_EGLImage *) list;
325 list = list->Next;
326
327 _eglUnlinkImage(image);
328 drv->DestroyImageKHR(display, image);
329 }
330 assert(!display->ResourceLists[_EGL_RESOURCE_IMAGE]);
331
332 list = display->ResourceLists[_EGL_RESOURCE_SYNC];
333 while (list) {
334 _EGLSync *sync = (_EGLSync *) list;
335 list = list->Next;
336
337 _eglUnlinkSync(sync);
338 drv->DestroySyncKHR(display, sync);
339 }
340 assert(!display->ResourceLists[_EGL_RESOURCE_SYNC]);
341 }
342
343
344 /**
345 * Free all the data hanging of an _EGLDisplay object, but not
346 * the object itself.
347 */
348 void
_eglCleanupDisplay(_EGLDisplay * disp)349 _eglCleanupDisplay(_EGLDisplay *disp)
350 {
351 if (disp->Configs) {
352 _eglDestroyArray(disp->Configs, free);
353 disp->Configs = NULL;
354 }
355
356 /* XXX incomplete */
357 }
358
359
360 /**
361 * Return EGL_TRUE if the given handle is a valid handle to a display.
362 */
363 EGLBoolean
_eglCheckDisplayHandle(EGLDisplay dpy)364 _eglCheckDisplayHandle(EGLDisplay dpy)
365 {
366 _EGLDisplay *cur;
367
368 mtx_lock(_eglGlobal.Mutex);
369 cur = _eglGlobal.DisplayList;
370 while (cur) {
371 if (cur == (_EGLDisplay *) dpy)
372 break;
373 cur = cur->Next;
374 }
375 mtx_unlock(_eglGlobal.Mutex);
376 return (cur != NULL);
377 }
378
379
380 /**
381 * Return EGL_TRUE if the given resource is valid. That is, the display does
382 * own the resource.
383 */
384 EGLBoolean
_eglCheckResource(void * res,_EGLResourceType type,_EGLDisplay * disp)385 _eglCheckResource(void *res, _EGLResourceType type, _EGLDisplay *disp)
386 {
387 _EGLResource *list = disp->ResourceLists[type];
388
389 if (!res)
390 return EGL_FALSE;
391
392 while (list) {
393 if (res == (void *) list) {
394 assert(list->Display == disp);
395 break;
396 }
397 list = list->Next;
398 }
399
400 return (list != NULL);
401 }
402
403
404 /**
405 * Initialize a display resource. The size of the subclass object is
406 * specified.
407 *
408 * This is supposed to be called from the initializers of subclasses, such as
409 * _eglInitContext or _eglInitSurface.
410 */
411 void
_eglInitResource(_EGLResource * res,EGLint size,_EGLDisplay * disp)412 _eglInitResource(_EGLResource *res, EGLint size, _EGLDisplay *disp)
413 {
414 memset(res, 0, size);
415 res->Display = disp;
416 res->RefCount = 1;
417 }
418
419
420 /**
421 * Increment reference count for the resource.
422 */
423 void
_eglGetResource(_EGLResource * res)424 _eglGetResource(_EGLResource *res)
425 {
426 assert(res && res->RefCount > 0);
427 /* hopefully a resource is always manipulated with its display locked */
428 res->RefCount++;
429 }
430
431
432 /**
433 * Decrement reference count for the resource.
434 */
435 EGLBoolean
_eglPutResource(_EGLResource * res)436 _eglPutResource(_EGLResource *res)
437 {
438 assert(res && res->RefCount > 0);
439 res->RefCount--;
440 return (!res->RefCount);
441 }
442
443
444 /**
445 * Link a resource to its display.
446 */
447 void
_eglLinkResource(_EGLResource * res,_EGLResourceType type)448 _eglLinkResource(_EGLResource *res, _EGLResourceType type)
449 {
450 assert(res->Display);
451
452 res->IsLinked = EGL_TRUE;
453 res->Next = res->Display->ResourceLists[type];
454 res->Display->ResourceLists[type] = res;
455 _eglGetResource(res);
456 }
457
458
459 /**
460 * Unlink a linked resource from its display.
461 */
462 void
_eglUnlinkResource(_EGLResource * res,_EGLResourceType type)463 _eglUnlinkResource(_EGLResource *res, _EGLResourceType type)
464 {
465 _EGLResource *prev;
466
467 prev = res->Display->ResourceLists[type];
468 if (prev != res) {
469 while (prev) {
470 if (prev->Next == res)
471 break;
472 prev = prev->Next;
473 }
474 assert(prev);
475 prev->Next = res->Next;
476 }
477 else {
478 res->Display->ResourceLists[type] = res->Next;
479 }
480
481 res->Next = NULL;
482 res->IsLinked = EGL_FALSE;
483 _eglPutResource(res);
484
485 /* We always unlink before destroy. The driver still owns a reference */
486 assert(res->RefCount);
487 }
488
489 #ifdef HAVE_X11_PLATFORM
490 _EGLDisplay*
_eglGetX11Display(Display * native_display,const EGLAttrib * attrib_list)491 _eglGetX11Display(Display *native_display,
492 const EGLAttrib *attrib_list)
493 {
494 /* EGL_EXT_platform_x11 recognizes exactly one attribute,
495 * EGL_PLATFORM_X11_SCREEN_EXT, which is optional.
496 */
497 if (attrib_list != NULL) {
498 for (int i = 0; attrib_list[i] != EGL_NONE; i += 2) {
499 if (attrib_list[i] != EGL_PLATFORM_X11_SCREEN_EXT) {
500 _eglError(EGL_BAD_ATTRIBUTE, "eglGetPlatformDisplay");
501 return NULL;
502 }
503 }
504 }
505 return _eglFindDisplay(_EGL_PLATFORM_X11, native_display, attrib_list);
506 }
507 #endif /* HAVE_X11_PLATFORM */
508
509 #ifdef HAVE_DRM_PLATFORM
510 _EGLDisplay*
_eglGetGbmDisplay(struct gbm_device * native_display,const EGLAttrib * attrib_list)511 _eglGetGbmDisplay(struct gbm_device *native_display,
512 const EGLAttrib *attrib_list)
513 {
514 /* EGL_MESA_platform_gbm recognizes no attributes. */
515 if (attrib_list != NULL && attrib_list[0] != EGL_NONE) {
516 _eglError(EGL_BAD_ATTRIBUTE, "eglGetPlatformDisplay");
517 return NULL;
518 }
519
520 return _eglFindDisplay(_EGL_PLATFORM_DRM, native_display, attrib_list);
521 }
522 #endif /* HAVE_DRM_PLATFORM */
523
524 #ifdef HAVE_WAYLAND_PLATFORM
525 _EGLDisplay*
_eglGetWaylandDisplay(struct wl_display * native_display,const EGLAttrib * attrib_list)526 _eglGetWaylandDisplay(struct wl_display *native_display,
527 const EGLAttrib *attrib_list)
528 {
529 /* EGL_EXT_platform_wayland recognizes no attributes. */
530 if (attrib_list != NULL && attrib_list[0] != EGL_NONE) {
531 _eglError(EGL_BAD_ATTRIBUTE, "eglGetPlatformDisplay");
532 return NULL;
533 }
534
535 return _eglFindDisplay(_EGL_PLATFORM_WAYLAND, native_display, attrib_list);
536 }
537 #endif /* HAVE_WAYLAND_PLATFORM */
538
539 _EGLDisplay*
_eglGetSurfacelessDisplay(void * native_display,const EGLAttrib * attrib_list)540 _eglGetSurfacelessDisplay(void *native_display,
541 const EGLAttrib *attrib_list)
542 {
543 /* This platform has no native display. */
544 if (native_display != NULL) {
545 _eglError(EGL_BAD_PARAMETER, "eglGetPlatformDisplay");
546 return NULL;
547 }
548
549 /* This platform recognizes no display attributes. */
550 if (attrib_list != NULL && attrib_list[0] != EGL_NONE) {
551 _eglError(EGL_BAD_ATTRIBUTE, "eglGetPlatformDisplay");
552 return NULL;
553 }
554
555 return _eglFindDisplay(_EGL_PLATFORM_SURFACELESS, native_display,
556 attrib_list);
557 }
558
559 #ifdef HAVE_ANDROID_PLATFORM
560 _EGLDisplay*
_eglGetAndroidDisplay(void * native_display,const EGLAttrib * attrib_list)561 _eglGetAndroidDisplay(void *native_display,
562 const EGLAttrib *attrib_list)
563 {
564
565 /* This platform recognizes no display attributes. */
566 if (attrib_list != NULL && attrib_list[0] != EGL_NONE) {
567 _eglError(EGL_BAD_ATTRIBUTE, "eglGetPlatformDisplay");
568 return NULL;
569 }
570
571 return _eglFindDisplay(_EGL_PLATFORM_ANDROID, native_display,
572 attrib_list);
573 }
574 #endif /* HAVE_ANDROID_PLATFORM */
575
576 _EGLDisplay*
_eglGetDeviceDisplay(void * native_display,const EGLAttrib * attrib_list)577 _eglGetDeviceDisplay(void *native_display,
578 const EGLAttrib *attrib_list)
579 {
580 _EGLDevice *dev;
581 _EGLDisplay *display;
582 int fd = -1;
583
584 dev = _eglLookupDevice(native_display);
585 if (!dev) {
586 _eglError(EGL_BAD_PARAMETER, "eglGetPlatformDisplay");
587 return NULL;
588 }
589
590 if (attrib_list) {
591 for (int i = 0; attrib_list[i] != EGL_NONE; i += 2) {
592 EGLAttrib attrib = attrib_list[i];
593 EGLAttrib value = attrib_list[i + 1];
594
595 /* EGL_EXT_platform_device does not recognize any attributes,
596 * EGL_EXT_device_drm adds the optional EGL_DRM_MASTER_FD_EXT.
597 */
598
599 if (!_eglDeviceSupports(dev, _EGL_DEVICE_DRM) ||
600 attrib != EGL_DRM_MASTER_FD_EXT) {
601 _eglError(EGL_BAD_ATTRIBUTE, "eglGetPlatformDisplay");
602 return NULL;
603 }
604
605 fd = (int) value;
606 }
607 }
608
609 display = _eglFindDisplay(_EGL_PLATFORM_DEVICE, native_display, attrib_list);
610 if (!display) {
611 _eglError(EGL_BAD_ALLOC, "eglGetPlatformDisplay");
612 return NULL;
613 }
614
615 /* If the fd is explicitly provided and we did not dup() it yet, do so.
616 * The spec mandates that we do so, since we'll need it past the
617 * eglGetPlatformDispay call.
618 *
619 * The new fd is guaranteed to be 3 or greater.
620 */
621 if (fd != -1 && display->Options.fd == 0) {
622 display->Options.fd = os_dupfd_cloexec(fd);
623 if (display->Options.fd == -1) {
624 /* Do not (really) need to teardown the display */
625 _eglError(EGL_BAD_ALLOC, "eglGetPlatformDisplay");
626 return NULL;
627 }
628 }
629
630 return display;
631 }
632