1 /**************************************************************************
3 * Copyright 2008 VMware, Inc.
4 * Copyright 2009-2010 Chia-I Wu <olvaffe@gmail.com>
5 * Copyright 2010-2011 LunarG, Inc.
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:
16 * The above copyright notice and this permission notice (including the
17 * next paragraph) shall be included in all copies or substantial portions
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.
28 **************************************************************************/
32 * Functions related to EGLDisplay.
40 #include "c11/threads.h"
41 #include "util/u_atomic.h"
43 #include "eglcontext.h"
44 #include "eglcurrent.h"
45 #include "eglsurface.h"
46 #include "egldevice.h"
47 #include "egldisplay.h"
48 #include "egldriver.h"
49 #include "eglglobals.h"
54 /* Includes for _eglNativePlatformDetectNativeDisplay */
55 #ifdef HAVE_WAYLAND_PLATFORM
56 #include <wayland-client.h>
58 #ifdef HAVE_DRM_PLATFORM
64 * Map build-system platform names to platform types.
67 _EGLPlatformType platform
;
69 } egl_platforms
[_EGL_NUM_PLATFORMS
] = {
70 { _EGL_PLATFORM_X11
, "x11" },
71 { _EGL_PLATFORM_WAYLAND
, "wayland" },
72 { _EGL_PLATFORM_DRM
, "drm" },
73 { _EGL_PLATFORM_ANDROID
, "android" },
74 { _EGL_PLATFORM_HAIKU
, "haiku" },
75 { _EGL_PLATFORM_SURFACELESS
, "surfaceless" },
80 * Return the native platform by parsing EGL_PLATFORM.
82 static _EGLPlatformType
83 _eglGetNativePlatformFromEnv(void)
85 _EGLPlatformType plat
= _EGL_INVALID_PLATFORM
;
86 const char *plat_name
;
89 plat_name
= getenv("EGL_PLATFORM");
90 /* try deprecated env variable */
91 if (!plat_name
|| !plat_name
[0])
92 plat_name
= getenv("EGL_DISPLAY");
93 if (!plat_name
|| !plat_name
[0])
94 return _EGL_INVALID_PLATFORM
;
96 for (i
= 0; i
< _EGL_NUM_PLATFORMS
; i
++) {
97 if (strcmp(egl_platforms
[i
].name
, plat_name
) == 0) {
98 plat
= egl_platforms
[i
].platform
;
108 * Try detecting native platform with the help of native display characteristcs.
110 static _EGLPlatformType
111 _eglNativePlatformDetectNativeDisplay(void *nativeDisplay
)
113 if (nativeDisplay
== EGL_DEFAULT_DISPLAY
)
114 return _EGL_INVALID_PLATFORM
;
116 if (_eglPointerIsDereferencable(nativeDisplay
)) {
117 void *first_pointer
= *(void **) nativeDisplay
;
119 (void) first_pointer
; /* silence unused var warning */
121 #ifdef HAVE_WAYLAND_PLATFORM
122 /* wl_display is a wl_proxy, which is a wl_object.
123 * wl_object's first element points to the interfacetype. */
124 if (first_pointer
== &wl_display_interface
)
125 return _EGL_PLATFORM_WAYLAND
;
128 #ifdef HAVE_DRM_PLATFORM
129 /* gbm has a pointer to its constructor as first element. */
130 if (first_pointer
== gbm_create_device
)
131 return _EGL_PLATFORM_DRM
;
134 #ifdef HAVE_X11_PLATFORM
135 /* If not matched to any other platform, fallback to x11. */
136 return _EGL_PLATFORM_X11
;
139 #ifdef HAVE_HAIKU_PLATFORM
140 return _EGL_PLATFORM_HAIKU
;
144 return _EGL_INVALID_PLATFORM
;
149 * Return the native platform. It is the platform of the EGL native types.
152 _eglGetNativePlatform(void *nativeDisplay
)
154 static _EGLPlatformType native_platform
= _EGL_INVALID_PLATFORM
;
155 _EGLPlatformType detected_platform
= native_platform
;
157 if (detected_platform
== _EGL_INVALID_PLATFORM
) {
158 const char *detection_method
;
160 detected_platform
= _eglGetNativePlatformFromEnv();
161 detection_method
= "environment overwrite";
163 if (detected_platform
== _EGL_INVALID_PLATFORM
) {
164 detected_platform
= _eglNativePlatformDetectNativeDisplay(nativeDisplay
);
165 detection_method
= "autodetected";
168 if (detected_platform
== _EGL_INVALID_PLATFORM
) {
169 detected_platform
= _EGL_NATIVE_PLATFORM
;
170 detection_method
= "build-time configuration";
173 _eglLog(_EGL_DEBUG
, "Native platform type: %s (%s)",
174 egl_platforms
[detected_platform
].name
, detection_method
);
176 p_atomic_cmpxchg(&native_platform
, _EGL_INVALID_PLATFORM
,
180 return native_platform
;
185 * Finish display management.
188 _eglFiniDisplay(void)
190 _EGLDisplay
*dispList
, *disp
;
192 /* atexit function is called with global mutex locked */
193 dispList
= _eglGlobal
.DisplayList
;
199 dispList
= dispList
->Next
;
201 for (i
= 0; i
< _EGL_NUM_RESOURCES
; i
++) {
202 if (disp
->ResourceLists
[i
]) {
203 _eglLog(_EGL_DEBUG
, "Display %p is destroyed with resources", disp
);
209 /* The fcntl() code in _eglGetDeviceDisplay() ensures that valid fd >= 3,
210 * and invalid one is 0.
212 if (disp
->Options
.fd
)
213 close(disp
->Options
.fd
);
215 free(disp
->Options
.Attribs
);
218 _eglGlobal
.DisplayList
= NULL
;
222 _eglSameAttribs(const EGLAttrib
*a
, const EGLAttrib
*b
)
224 size_t na
= _eglNumAttribs(a
);
225 size_t nb
= _eglNumAttribs(b
);
227 /* different numbers of attributes must be different */
231 /* both lists NULL are the same */
235 /* otherwise, compare the lists */
236 return memcmp(a
, b
, na
* sizeof(a
[0])) == 0 ? EGL_TRUE
: EGL_FALSE
;
240 * Find the display corresponding to the specified native display, or create a
241 * new one. EGL 1.5 says:
243 * Multiple calls made to eglGetPlatformDisplay with the same parameters
244 * will return the same EGLDisplay handle.
246 * We read this extremely strictly, and treat a call with NULL attribs as
247 * different from a call with attribs only equal to { EGL_NONE }. Similarly
248 * we do not sort the attribute list, so even if all attribute _values_ are
249 * identical, different attribute orders will be considered different
253 _eglFindDisplay(_EGLPlatformType plat
, void *plat_dpy
,
254 const EGLAttrib
*attrib_list
)
259 if (plat
== _EGL_INVALID_PLATFORM
)
262 mtx_lock(_eglGlobal
.Mutex
);
264 /* search the display list first */
265 for (disp
= _eglGlobal
.DisplayList
; disp
; disp
= disp
->Next
) {
266 if (disp
->Platform
== plat
&& disp
->PlatformDisplay
== plat_dpy
&&
267 _eglSameAttribs(disp
->Options
.Attribs
, attrib_list
))
271 /* create a new display */
273 disp
= calloc(1, sizeof(_EGLDisplay
));
275 mtx_init(&disp
->Mutex
, mtx_plain
);
276 disp
->Platform
= plat
;
277 disp
->PlatformDisplay
= plat_dpy
;
278 num_attribs
= _eglNumAttribs(attrib_list
);
280 disp
->Options
.Attribs
= calloc(num_attribs
, sizeof(EGLAttrib
));
281 if (!disp
->Options
.Attribs
) {
286 memcpy(disp
->Options
.Attribs
, attrib_list
,
287 num_attribs
* sizeof(EGLAttrib
));
289 /* add to the display list */
290 disp
->Next
= _eglGlobal
.DisplayList
;
291 _eglGlobal
.DisplayList
= disp
;
296 mtx_unlock(_eglGlobal
.Mutex
);
303 * Destroy the contexts and surfaces that are linked to the display.
306 _eglReleaseDisplayResources(_EGLDriver
*drv
, _EGLDisplay
*display
)
310 list
= display
->ResourceLists
[_EGL_RESOURCE_CONTEXT
];
312 _EGLContext
*ctx
= (_EGLContext
*) list
;
315 _eglUnlinkContext(ctx
);
316 drv
->API
.DestroyContext(drv
, display
, ctx
);
318 assert(!display
->ResourceLists
[_EGL_RESOURCE_CONTEXT
]);
320 list
= display
->ResourceLists
[_EGL_RESOURCE_SURFACE
];
322 _EGLSurface
*surf
= (_EGLSurface
*) list
;
325 _eglUnlinkSurface(surf
);
326 drv
->API
.DestroySurface(drv
, display
, surf
);
328 assert(!display
->ResourceLists
[_EGL_RESOURCE_SURFACE
]);
330 list
= display
->ResourceLists
[_EGL_RESOURCE_IMAGE
];
332 _EGLImage
*image
= (_EGLImage
*) list
;
335 _eglUnlinkImage(image
);
336 drv
->API
.DestroyImageKHR(drv
, display
, image
);
338 assert(!display
->ResourceLists
[_EGL_RESOURCE_IMAGE
]);
340 list
= display
->ResourceLists
[_EGL_RESOURCE_SYNC
];
342 _EGLSync
*sync
= (_EGLSync
*) list
;
345 _eglUnlinkSync(sync
);
346 drv
->API
.DestroySyncKHR(drv
, display
, sync
);
348 assert(!display
->ResourceLists
[_EGL_RESOURCE_SYNC
]);
353 * Free all the data hanging of an _EGLDisplay object, but not
357 _eglCleanupDisplay(_EGLDisplay
*disp
)
360 _eglDestroyArray(disp
->Configs
, free
);
361 disp
->Configs
= NULL
;
369 * Return EGL_TRUE if the given handle is a valid handle to a display.
372 _eglCheckDisplayHandle(EGLDisplay dpy
)
376 mtx_lock(_eglGlobal
.Mutex
);
377 cur
= _eglGlobal
.DisplayList
;
379 if (cur
== (_EGLDisplay
*) dpy
)
383 mtx_unlock(_eglGlobal
.Mutex
);
384 return (cur
!= NULL
);
389 * Return EGL_TRUE if the given resource is valid. That is, the display does
393 _eglCheckResource(void *res
, _EGLResourceType type
, _EGLDisplay
*disp
)
395 _EGLResource
*list
= disp
->ResourceLists
[type
];
401 if (res
== (void *) list
) {
402 assert(list
->Display
== disp
);
408 return (list
!= NULL
);
413 * Initialize a display resource. The size of the subclass object is
416 * This is supposed to be called from the initializers of subclasses, such as
417 * _eglInitContext or _eglInitSurface.
420 _eglInitResource(_EGLResource
*res
, EGLint size
, _EGLDisplay
*disp
)
422 memset(res
, 0, size
);
429 * Increment reference count for the resource.
432 _eglGetResource(_EGLResource
*res
)
434 assert(res
&& res
->RefCount
> 0);
435 /* hopefully a resource is always manipulated with its display locked */
441 * Decrement reference count for the resource.
444 _eglPutResource(_EGLResource
*res
)
446 assert(res
&& res
->RefCount
> 0);
448 return (!res
->RefCount
);
453 * Link a resource to its display.
456 _eglLinkResource(_EGLResource
*res
, _EGLResourceType type
)
458 assert(res
->Display
);
460 res
->IsLinked
= EGL_TRUE
;
461 res
->Next
= res
->Display
->ResourceLists
[type
];
462 res
->Display
->ResourceLists
[type
] = res
;
463 _eglGetResource(res
);
468 * Unlink a linked resource from its display.
471 _eglUnlinkResource(_EGLResource
*res
, _EGLResourceType type
)
475 prev
= res
->Display
->ResourceLists
[type
];
478 if (prev
->Next
== res
)
483 prev
->Next
= res
->Next
;
486 res
->Display
->ResourceLists
[type
] = res
->Next
;
490 res
->IsLinked
= EGL_FALSE
;
491 _eglPutResource(res
);
493 /* We always unlink before destroy. The driver still owns a reference */
494 assert(res
->RefCount
);
497 #ifdef HAVE_X11_PLATFORM
499 _eglGetX11Display(Display
*native_display
,
500 const EGLAttrib
*attrib_list
)
502 /* EGL_EXT_platform_x11 recognizes exactly one attribute,
503 * EGL_PLATFORM_X11_SCREEN_EXT, which is optional.
505 if (attrib_list
!= NULL
) {
506 for (int i
= 0; attrib_list
[i
] != EGL_NONE
; i
+= 2) {
507 if (attrib_list
[i
] != EGL_PLATFORM_X11_SCREEN_EXT
) {
508 _eglError(EGL_BAD_ATTRIBUTE
, "eglGetPlatformDisplay");
513 return _eglFindDisplay(_EGL_PLATFORM_X11
, native_display
, attrib_list
);
515 #endif /* HAVE_X11_PLATFORM */
517 #ifdef HAVE_DRM_PLATFORM
519 _eglGetGbmDisplay(struct gbm_device
*native_display
,
520 const EGLAttrib
*attrib_list
)
522 /* EGL_MESA_platform_gbm recognizes no attributes. */
523 if (attrib_list
!= NULL
&& attrib_list
[0] != EGL_NONE
) {
524 _eglError(EGL_BAD_ATTRIBUTE
, "eglGetPlatformDisplay");
528 return _eglFindDisplay(_EGL_PLATFORM_DRM
, native_display
, attrib_list
);
530 #endif /* HAVE_DRM_PLATFORM */
532 #ifdef HAVE_WAYLAND_PLATFORM
534 _eglGetWaylandDisplay(struct wl_display
*native_display
,
535 const EGLAttrib
*attrib_list
)
537 /* EGL_EXT_platform_wayland recognizes no attributes. */
538 if (attrib_list
!= NULL
&& attrib_list
[0] != EGL_NONE
) {
539 _eglError(EGL_BAD_ATTRIBUTE
, "eglGetPlatformDisplay");
543 return _eglFindDisplay(_EGL_PLATFORM_WAYLAND
, native_display
, attrib_list
);
545 #endif /* HAVE_WAYLAND_PLATFORM */
547 #ifdef HAVE_SURFACELESS_PLATFORM
549 _eglGetSurfacelessDisplay(void *native_display
,
550 const EGLAttrib
*attrib_list
)
552 /* This platform has no native display. */
553 if (native_display
!= NULL
) {
554 _eglError(EGL_BAD_PARAMETER
, "eglGetPlatformDisplay");
558 /* This platform recognizes no display attributes. */
559 if (attrib_list
!= NULL
&& attrib_list
[0] != EGL_NONE
) {
560 _eglError(EGL_BAD_ATTRIBUTE
, "eglGetPlatformDisplay");
564 return _eglFindDisplay(_EGL_PLATFORM_SURFACELESS
, native_display
,
567 #endif /* HAVE_SURFACELESS_PLATFORM */
569 #ifdef HAVE_ANDROID_PLATFORM
571 _eglGetAndroidDisplay(void *native_display
,
572 const EGLAttrib
*attrib_list
)
575 /* This platform recognizes no display attributes. */
576 if (attrib_list
!= NULL
&& attrib_list
[0] != EGL_NONE
) {
577 _eglError(EGL_BAD_ATTRIBUTE
, "eglGetPlatformDisplay");
581 return _eglFindDisplay(_EGL_PLATFORM_ANDROID
, native_display
,
584 #endif /* HAVE_ANDROID_PLATFORM */
587 _eglGetDeviceDisplay(void *native_display
,
588 const EGLAttrib
*attrib_list
)
591 _EGLDisplay
*display
;
594 dev
= _eglLookupDevice(native_display
);
596 _eglError(EGL_BAD_PARAMETER
, "eglGetPlatformDisplay");
601 for (int i
= 0; attrib_list
[i
] != EGL_NONE
; i
+= 2) {
602 EGLAttrib attrib
= attrib_list
[i
];
603 EGLAttrib value
= attrib_list
[i
+ 1];
605 /* EGL_EXT_platform_device does not recognize any attributes,
606 * EGL_EXT_device_drm adds the optional EGL_DRM_MASTER_FD_EXT.
609 if (!_eglDeviceSupports(dev
, _EGL_DEVICE_DRM
) ||
610 attrib
!= EGL_DRM_MASTER_FD_EXT
) {
611 _eglError(EGL_BAD_ATTRIBUTE
, "eglGetPlatformDisplay");
619 display
= _eglFindDisplay(_EGL_PLATFORM_DEVICE
, native_display
, attrib_list
);
621 _eglError(EGL_BAD_ALLOC
, "eglGetPlatformDisplay");
625 /* If the fd is explicitly provided and we did not dup() it yet, do so.
626 * The spec mandates that we do so, since we'll need it past the
627 * eglGetPlatformDispay call.
629 * The new fd is guaranteed to be 3 or greater.
631 if (fd
!= -1 && display
->Options
.fd
== 0) {
632 display
->Options
.fd
= fcntl(fd
, F_DUPFD_CLOEXEC
, 3);
633 if (display
->Options
.fd
== -1) {
634 /* Do not (really) need to teardown the display */
635 _eglError(EGL_BAD_ALLOC
, "eglGetPlatformDisplay");