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.
39 #include "c11/threads.h"
40 #include "util/u_atomic.h"
42 #include "eglcontext.h"
43 #include "eglcurrent.h"
44 #include "eglsurface.h"
45 #include "egldevice.h"
46 #include "egldisplay.h"
47 #include "egldriver.h"
48 #include "eglglobals.h"
53 /* Includes for _eglNativePlatformDetectNativeDisplay */
54 #ifdef HAVE_WAYLAND_PLATFORM
55 #include <wayland-client.h>
57 #ifdef HAVE_DRM_PLATFORM
63 * Map build-system platform names to platform types.
66 _EGLPlatformType platform
;
68 } egl_platforms
[_EGL_NUM_PLATFORMS
] = {
69 { _EGL_PLATFORM_X11
, "x11" },
70 { _EGL_PLATFORM_WAYLAND
, "wayland" },
71 { _EGL_PLATFORM_DRM
, "drm" },
72 { _EGL_PLATFORM_ANDROID
, "android" },
73 { _EGL_PLATFORM_HAIKU
, "haiku" },
74 { _EGL_PLATFORM_SURFACELESS
, "surfaceless" },
79 * Return the native platform by parsing EGL_PLATFORM.
81 static _EGLPlatformType
82 _eglGetNativePlatformFromEnv(void)
84 _EGLPlatformType plat
= _EGL_INVALID_PLATFORM
;
85 const char *plat_name
;
88 plat_name
= getenv("EGL_PLATFORM");
89 /* try deprecated env variable */
90 if (!plat_name
|| !plat_name
[0])
91 plat_name
= getenv("EGL_DISPLAY");
92 if (!plat_name
|| !plat_name
[0])
93 return _EGL_INVALID_PLATFORM
;
95 for (i
= 0; i
< _EGL_NUM_PLATFORMS
; i
++) {
96 if (strcmp(egl_platforms
[i
].name
, plat_name
) == 0) {
97 plat
= egl_platforms
[i
].platform
;
107 * Try detecting native platform with the help of native display characteristcs.
109 static _EGLPlatformType
110 _eglNativePlatformDetectNativeDisplay(void *nativeDisplay
)
112 if (nativeDisplay
== EGL_DEFAULT_DISPLAY
)
113 return _EGL_INVALID_PLATFORM
;
115 if (_eglPointerIsDereferencable(nativeDisplay
)) {
116 void *first_pointer
= *(void **) nativeDisplay
;
118 (void) first_pointer
; /* silence unused var warning */
120 #ifdef HAVE_WAYLAND_PLATFORM
121 /* wl_display is a wl_proxy, which is a wl_object.
122 * wl_object's first element points to the interfacetype. */
123 if (first_pointer
== &wl_display_interface
)
124 return _EGL_PLATFORM_WAYLAND
;
127 #ifdef HAVE_DRM_PLATFORM
128 /* gbm has a pointer to its constructor as first element. */
129 if (first_pointer
== gbm_create_device
)
130 return _EGL_PLATFORM_DRM
;
133 #ifdef HAVE_X11_PLATFORM
134 /* If not matched to any other platform, fallback to x11. */
135 return _EGL_PLATFORM_X11
;
138 #ifdef HAVE_HAIKU_PLATFORM
139 return _EGL_PLATFORM_HAIKU
;
143 return _EGL_INVALID_PLATFORM
;
148 * Return the native platform. It is the platform of the EGL native types.
151 _eglGetNativePlatform(void *nativeDisplay
)
153 static _EGLPlatformType native_platform
= _EGL_INVALID_PLATFORM
;
154 _EGLPlatformType detected_platform
= native_platform
;
156 if (detected_platform
== _EGL_INVALID_PLATFORM
) {
157 const char *detection_method
;
159 detected_platform
= _eglGetNativePlatformFromEnv();
160 detection_method
= "environment overwrite";
162 if (detected_platform
== _EGL_INVALID_PLATFORM
) {
163 detected_platform
= _eglNativePlatformDetectNativeDisplay(nativeDisplay
);
164 detection_method
= "autodetected";
167 if (detected_platform
== _EGL_INVALID_PLATFORM
) {
168 detected_platform
= _EGL_NATIVE_PLATFORM
;
169 detection_method
= "build-time configuration";
172 _eglLog(_EGL_DEBUG
, "Native platform type: %s (%s)",
173 egl_platforms
[detected_platform
].name
, detection_method
);
175 p_atomic_cmpxchg(&native_platform
, _EGL_INVALID_PLATFORM
,
179 return native_platform
;
184 * Finish display management.
187 _eglFiniDisplay(void)
189 _EGLDisplay
*dispList
, *disp
;
191 /* atexit function is called with global mutex locked */
192 dispList
= _eglGlobal
.DisplayList
;
198 dispList
= dispList
->Next
;
200 for (i
= 0; i
< _EGL_NUM_RESOURCES
; i
++) {
201 if (disp
->ResourceLists
[i
]) {
202 _eglLog(_EGL_DEBUG
, "Display %p is destroyed with resources", disp
);
208 /* The fcntl() code in _eglGetDeviceDisplay() ensures that valid fd >= 3,
209 * and invalid one is 0.
211 if (disp
->Options
.fd
)
212 close(disp
->Options
.fd
);
214 free(disp
->Options
.Attribs
);
217 _eglGlobal
.DisplayList
= NULL
;
221 _eglSameAttribs(const EGLAttrib
*a
, const EGLAttrib
*b
)
223 size_t na
= _eglNumAttribs(a
);
224 size_t nb
= _eglNumAttribs(b
);
226 /* different numbers of attributes must be different */
230 /* both lists NULL are the same */
234 /* otherwise, compare the lists */
235 return memcmp(a
, b
, na
* sizeof(a
[0])) == 0 ? EGL_TRUE
: EGL_FALSE
;
239 * Find the display corresponding to the specified native display, or create a
240 * new one. EGL 1.5 says:
242 * Multiple calls made to eglGetPlatformDisplay with the same parameters
243 * will return the same EGLDisplay handle.
245 * We read this extremely strictly, and treat a call with NULL attribs as
246 * different from a call with attribs only equal to { EGL_NONE }. Similarly
247 * we do not sort the attribute list, so even if all attribute _values_ are
248 * identical, different attribute orders will be considered different
252 _eglFindDisplay(_EGLPlatformType plat
, void *plat_dpy
,
253 const EGLAttrib
*attrib_list
)
258 if (plat
== _EGL_INVALID_PLATFORM
)
261 mtx_lock(_eglGlobal
.Mutex
);
263 /* search the display list first */
264 disp
= _eglGlobal
.DisplayList
;
266 if (disp
->Platform
== plat
&& disp
->PlatformDisplay
== plat_dpy
&&
267 _eglSameAttribs(disp
->Options
.Attribs
, attrib_list
))
272 /* create a new display */
274 disp
= calloc(1, sizeof(_EGLDisplay
));
276 mtx_init(&disp
->Mutex
, mtx_plain
);
277 disp
->Platform
= plat
;
278 disp
->PlatformDisplay
= plat_dpy
;
279 num_attribs
= _eglNumAttribs(attrib_list
);
281 disp
->Options
.Attribs
= calloc(num_attribs
, sizeof(EGLAttrib
));
282 if (!disp
->Options
.Attribs
) {
287 memcpy(disp
->Options
.Attribs
, attrib_list
,
288 num_attribs
* sizeof(EGLAttrib
));
290 /* add to the display list */
291 disp
->Next
= _eglGlobal
.DisplayList
;
292 _eglGlobal
.DisplayList
= disp
;
297 mtx_unlock(_eglGlobal
.Mutex
);
304 * Destroy the contexts and surfaces that are linked to the display.
307 _eglReleaseDisplayResources(_EGLDriver
*drv
, _EGLDisplay
*display
)
311 list
= display
->ResourceLists
[_EGL_RESOURCE_CONTEXT
];
313 _EGLContext
*ctx
= (_EGLContext
*) list
;
316 _eglUnlinkContext(ctx
);
317 drv
->API
.DestroyContext(drv
, display
, ctx
);
319 assert(!display
->ResourceLists
[_EGL_RESOURCE_CONTEXT
]);
321 list
= display
->ResourceLists
[_EGL_RESOURCE_SURFACE
];
323 _EGLSurface
*surf
= (_EGLSurface
*) list
;
326 _eglUnlinkSurface(surf
);
327 drv
->API
.DestroySurface(drv
, display
, surf
);
329 assert(!display
->ResourceLists
[_EGL_RESOURCE_SURFACE
]);
331 list
= display
->ResourceLists
[_EGL_RESOURCE_IMAGE
];
333 _EGLImage
*image
= (_EGLImage
*) list
;
336 _eglUnlinkImage(image
);
337 drv
->API
.DestroyImageKHR(drv
, display
, image
);
339 assert(!display
->ResourceLists
[_EGL_RESOURCE_IMAGE
]);
341 list
= display
->ResourceLists
[_EGL_RESOURCE_SYNC
];
343 _EGLSync
*sync
= (_EGLSync
*) list
;
346 _eglUnlinkSync(sync
);
347 drv
->API
.DestroySyncKHR(drv
, display
, sync
);
349 assert(!display
->ResourceLists
[_EGL_RESOURCE_SYNC
]);
354 * Free all the data hanging of an _EGLDisplay object, but not
358 _eglCleanupDisplay(_EGLDisplay
*disp
)
361 _eglDestroyArray(disp
->Configs
, free
);
362 disp
->Configs
= NULL
;
370 * Return EGL_TRUE if the given handle is a valid handle to a display.
373 _eglCheckDisplayHandle(EGLDisplay dpy
)
377 mtx_lock(_eglGlobal
.Mutex
);
378 cur
= _eglGlobal
.DisplayList
;
380 if (cur
== (_EGLDisplay
*) dpy
)
384 mtx_unlock(_eglGlobal
.Mutex
);
385 return (cur
!= NULL
);
390 * Return EGL_TRUE if the given resource is valid. That is, the display does
394 _eglCheckResource(void *res
, _EGLResourceType type
, _EGLDisplay
*disp
)
396 _EGLResource
*list
= disp
->ResourceLists
[type
];
402 if (res
== (void *) list
) {
403 assert(list
->Display
== disp
);
409 return (list
!= NULL
);
414 * Initialize a display resource. The size of the subclass object is
417 * This is supposed to be called from the initializers of subclasses, such as
418 * _eglInitContext or _eglInitSurface.
421 _eglInitResource(_EGLResource
*res
, EGLint size
, _EGLDisplay
*disp
)
423 memset(res
, 0, size
);
430 * Increment reference count for the resource.
433 _eglGetResource(_EGLResource
*res
)
435 assert(res
&& res
->RefCount
> 0);
436 /* hopefully a resource is always manipulated with its display locked */
442 * Decrement reference count for the resource.
445 _eglPutResource(_EGLResource
*res
)
447 assert(res
&& res
->RefCount
> 0);
449 return (!res
->RefCount
);
454 * Link a resource to its display.
457 _eglLinkResource(_EGLResource
*res
, _EGLResourceType type
)
459 assert(res
->Display
);
461 res
->IsLinked
= EGL_TRUE
;
462 res
->Next
= res
->Display
->ResourceLists
[type
];
463 res
->Display
->ResourceLists
[type
] = res
;
464 _eglGetResource(res
);
469 * Unlink a linked resource from its display.
472 _eglUnlinkResource(_EGLResource
*res
, _EGLResourceType type
)
476 prev
= res
->Display
->ResourceLists
[type
];
479 if (prev
->Next
== res
)
484 prev
->Next
= res
->Next
;
487 res
->Display
->ResourceLists
[type
] = res
->Next
;
491 res
->IsLinked
= EGL_FALSE
;
492 _eglPutResource(res
);
494 /* We always unlink before destroy. The driver still owns a reference */
495 assert(res
->RefCount
);
498 #ifdef HAVE_X11_PLATFORM
500 _eglGetX11Display(Display
*native_display
,
501 const EGLAttrib
*attrib_list
)
503 /* EGL_EXT_platform_x11 recognizes exactly one attribute,
504 * EGL_PLATFORM_X11_SCREEN_EXT, which is optional.
506 if (attrib_list
!= NULL
) {
507 for (int i
= 0; attrib_list
[i
] != EGL_NONE
; i
+= 2) {
508 if (attrib_list
[i
] != EGL_PLATFORM_X11_SCREEN_EXT
) {
509 _eglError(EGL_BAD_ATTRIBUTE
, "eglGetPlatformDisplay");
514 return _eglFindDisplay(_EGL_PLATFORM_X11
, native_display
, attrib_list
);
516 #endif /* HAVE_X11_PLATFORM */
518 #ifdef HAVE_DRM_PLATFORM
520 _eglGetGbmDisplay(struct gbm_device
*native_display
,
521 const EGLAttrib
*attrib_list
)
523 /* EGL_MESA_platform_gbm recognizes no attributes. */
524 if (attrib_list
!= NULL
&& attrib_list
[0] != EGL_NONE
) {
525 _eglError(EGL_BAD_ATTRIBUTE
, "eglGetPlatformDisplay");
529 return _eglFindDisplay(_EGL_PLATFORM_DRM
, native_display
, attrib_list
);
531 #endif /* HAVE_DRM_PLATFORM */
533 #ifdef HAVE_WAYLAND_PLATFORM
535 _eglGetWaylandDisplay(struct wl_display
*native_display
,
536 const EGLAttrib
*attrib_list
)
538 /* EGL_EXT_platform_wayland recognizes no attributes. */
539 if (attrib_list
!= NULL
&& attrib_list
[0] != EGL_NONE
) {
540 _eglError(EGL_BAD_ATTRIBUTE
, "eglGetPlatformDisplay");
544 return _eglFindDisplay(_EGL_PLATFORM_WAYLAND
, native_display
, attrib_list
);
546 #endif /* HAVE_WAYLAND_PLATFORM */
548 #ifdef HAVE_SURFACELESS_PLATFORM
550 _eglGetSurfacelessDisplay(void *native_display
,
551 const EGLAttrib
*attrib_list
)
553 /* This platform has no native display. */
554 if (native_display
!= NULL
) {
555 _eglError(EGL_BAD_PARAMETER
, "eglGetPlatformDisplay");
559 /* This platform recognizes no display attributes. */
560 if (attrib_list
!= NULL
&& attrib_list
[0] != EGL_NONE
) {
561 _eglError(EGL_BAD_ATTRIBUTE
, "eglGetPlatformDisplay");
565 return _eglFindDisplay(_EGL_PLATFORM_SURFACELESS
, native_display
,
568 #endif /* HAVE_SURFACELESS_PLATFORM */
572 _eglGetDeviceDisplay(void *native_display
,
573 const EGLAttrib
*attrib_list
)
576 _EGLDisplay
*display
;
579 dev
= _eglLookupDevice(native_display
);
581 _eglError(EGL_BAD_PARAMETER
, "eglGetPlatformDisplay");
586 for (int i
= 0; attrib_list
[i
] != EGL_NONE
; i
+= 2) {
587 EGLAttrib attrib
= attrib_list
[i
];
588 EGLAttrib value
= attrib_list
[i
+ 1];
590 /* EGL_EXT_platform_device does not recognize any attributes,
591 * EGL_EXT_device_drm adds the optional EGL_DRM_MASTER_FD_EXT.
594 if (!_eglDeviceSupports(dev
, _EGL_DEVICE_DRM
) ||
595 attrib
!= EGL_DRM_MASTER_FD_EXT
) {
596 _eglError(EGL_BAD_ATTRIBUTE
, "eglGetPlatformDisplay");
604 display
= _eglFindDisplay(_EGL_PLATFORM_DEVICE
, native_display
, attrib_list
);
606 _eglError(EGL_BAD_ALLOC
, "eglGetPlatformDisplay");
610 /* If the fd is explicitly provided and we did not dup() it yet, do so.
611 * The spec mandates that we do so, since we'll need it past the
612 * eglGetPlatformDispay call.
614 * The new fd is guaranteed to be 3 or greater.
616 if (fd
!= -1 && display
->Options
.fd
== 0) {
617 display
->Options
.fd
= fcntl(fd
, F_DUPFD_CLOEXEC
, 3);
618 if (display
->Options
.fd
== -1) {
619 /* Do not (really) need to teardown the display */
620 _eglError(EGL_BAD_ALLOC
, "eglGetPlatformDisplay");