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/macros.h"
42 #include "util/os_file.h"
43 #include "util/u_atomic.h"
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"
56 /* Includes for _eglNativePlatformDetectNativeDisplay */
57 #ifdef HAVE_WAYLAND_PLATFORM
58 #include <wayland-client.h>
60 #ifdef HAVE_DRM_PLATFORM
66 * Map build-system platform names to platform types.
69 _EGLPlatformType platform
;
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" },
83 * Return the native platform by parsing EGL_PLATFORM.
85 static _EGLPlatformType
86 _eglGetNativePlatformFromEnv(void)
88 _EGLPlatformType plat
= _EGL_INVALID_PLATFORM
;
89 const char *plat_name
;
92 static_assert(ARRAY_SIZE(egl_platforms
) == _EGL_NUM_PLATFORMS
,
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
;
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
;
109 if (plat
== _EGL_INVALID_PLATFORM
)
110 _eglLog(_EGL_WARNING
, "invalid EGL_PLATFORM given");
117 * Try detecting native platform with the help of native display characteristcs.
119 static _EGLPlatformType
120 _eglNativePlatformDetectNativeDisplay(void *nativeDisplay
)
122 if (nativeDisplay
== EGL_DEFAULT_DISPLAY
)
123 return _EGL_INVALID_PLATFORM
;
125 if (_eglPointerIsDereferencable(nativeDisplay
)) {
126 void *first_pointer
= *(void **) nativeDisplay
;
128 (void) first_pointer
; /* silence unused var warning */
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
;
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
;
144 return _EGL_INVALID_PLATFORM
;
149 * Return the native platform. It is the platform of the EGL native types.
152 _eglGetNativePlatform(void *nativeDisplay
)
154 _EGLPlatformType detected_platform
= _eglGetNativePlatformFromEnv();
155 const char *detection_method
= "environment";
157 if (detected_platform
== _EGL_INVALID_PLATFORM
) {
158 detected_platform
= _eglNativePlatformDetectNativeDisplay(nativeDisplay
);
159 detection_method
= "autodetected";
162 if (detected_platform
== _EGL_INVALID_PLATFORM
) {
163 detected_platform
= _EGL_NATIVE_PLATFORM
;
164 detection_method
= "build-time configuration";
167 _eglLog(_EGL_DEBUG
, "Native platform type: %s (%s)",
168 egl_platforms
[detected_platform
].name
, detection_method
);
170 return detected_platform
;
175 * Finish display management.
178 _eglFiniDisplay(void)
180 _EGLDisplay
*dispList
, *disp
;
182 /* atexit function is called with global mutex locked */
183 dispList
= _eglGlobal
.DisplayList
;
189 dispList
= dispList
->Next
;
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
);
199 /* The fcntl() code in _eglGetDeviceDisplay() ensures that valid fd >= 3,
200 * and invalid one is 0.
202 if (disp
->Options
.fd
)
203 close(disp
->Options
.fd
);
205 free(disp
->Options
.Attribs
);
208 _eglGlobal
.DisplayList
= NULL
;
212 _eglSameAttribs(const EGLAttrib
*a
, const EGLAttrib
*b
)
214 size_t na
= _eglNumAttribs(a
);
215 size_t nb
= _eglNumAttribs(b
);
217 /* different numbers of attributes must be different */
221 /* both lists NULL are the same */
225 /* otherwise, compare the lists */
226 return memcmp(a
, b
, na
* sizeof(a
[0])) == 0 ? EGL_TRUE
: EGL_FALSE
;
230 * Find the display corresponding to the specified native display, or create a
231 * new one. EGL 1.5 says:
233 * Multiple calls made to eglGetPlatformDisplay with the same parameters
234 * will return the same EGLDisplay handle.
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
243 _eglFindDisplay(_EGLPlatformType plat
, void *plat_dpy
,
244 const EGLAttrib
*attrib_list
)
249 if (plat
== _EGL_INVALID_PLATFORM
)
252 mtx_lock(_eglGlobal
.Mutex
);
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
))
261 /* create a new display */
263 disp
= calloc(1, sizeof(_EGLDisplay
));
265 mtx_init(&disp
->Mutex
, mtx_plain
);
266 disp
->Platform
= plat
;
267 disp
->PlatformDisplay
= plat_dpy
;
268 num_attribs
= _eglNumAttribs(attrib_list
);
270 disp
->Options
.Attribs
= calloc(num_attribs
, sizeof(EGLAttrib
));
271 if (!disp
->Options
.Attribs
) {
276 memcpy(disp
->Options
.Attribs
, attrib_list
,
277 num_attribs
* sizeof(EGLAttrib
));
279 /* add to the display list */
280 disp
->Next
= _eglGlobal
.DisplayList
;
281 _eglGlobal
.DisplayList
= disp
;
286 mtx_unlock(_eglGlobal
.Mutex
);
293 * Destroy the contexts and surfaces that are linked to the display.
296 _eglReleaseDisplayResources(_EGLDisplay
*display
)
299 const _EGLDriver
*drv
= display
->Driver
;
301 list
= display
->ResourceLists
[_EGL_RESOURCE_CONTEXT
];
303 _EGLContext
*ctx
= (_EGLContext
*) list
;
306 _eglUnlinkContext(ctx
);
307 drv
->DestroyContext(display
, ctx
);
309 assert(!display
->ResourceLists
[_EGL_RESOURCE_CONTEXT
]);
311 list
= display
->ResourceLists
[_EGL_RESOURCE_SURFACE
];
313 _EGLSurface
*surf
= (_EGLSurface
*) list
;
316 _eglUnlinkSurface(surf
);
317 drv
->DestroySurface(display
, surf
);
319 assert(!display
->ResourceLists
[_EGL_RESOURCE_SURFACE
]);
321 list
= display
->ResourceLists
[_EGL_RESOURCE_IMAGE
];
323 _EGLImage
*image
= (_EGLImage
*) list
;
326 _eglUnlinkImage(image
);
327 drv
->DestroyImageKHR(display
, image
);
329 assert(!display
->ResourceLists
[_EGL_RESOURCE_IMAGE
]);
331 list
= display
->ResourceLists
[_EGL_RESOURCE_SYNC
];
333 _EGLSync
*sync
= (_EGLSync
*) list
;
336 _eglUnlinkSync(sync
);
337 drv
->DestroySyncKHR(display
, sync
);
339 assert(!display
->ResourceLists
[_EGL_RESOURCE_SYNC
]);
344 * Free all the data hanging of an _EGLDisplay object, but not
348 _eglCleanupDisplay(_EGLDisplay
*disp
)
351 _eglDestroyArray(disp
->Configs
, free
);
352 disp
->Configs
= NULL
;
360 * Return EGL_TRUE if the given handle is a valid handle to a display.
363 _eglCheckDisplayHandle(EGLDisplay dpy
)
367 mtx_lock(_eglGlobal
.Mutex
);
368 cur
= _eglGlobal
.DisplayList
;
370 if (cur
== (_EGLDisplay
*) dpy
)
374 mtx_unlock(_eglGlobal
.Mutex
);
375 return (cur
!= NULL
);
380 * Return EGL_TRUE if the given resource is valid. That is, the display does
384 _eglCheckResource(void *res
, _EGLResourceType type
, _EGLDisplay
*disp
)
386 _EGLResource
*list
= disp
->ResourceLists
[type
];
392 if (res
== (void *) list
) {
393 assert(list
->Display
== disp
);
399 return (list
!= NULL
);
404 * Initialize a display resource. The size of the subclass object is
407 * This is supposed to be called from the initializers of subclasses, such as
408 * _eglInitContext or _eglInitSurface.
411 _eglInitResource(_EGLResource
*res
, EGLint size
, _EGLDisplay
*disp
)
413 memset(res
, 0, size
);
420 * Increment reference count for the resource.
423 _eglGetResource(_EGLResource
*res
)
425 assert(res
&& res
->RefCount
> 0);
426 /* hopefully a resource is always manipulated with its display locked */
432 * Decrement reference count for the resource.
435 _eglPutResource(_EGLResource
*res
)
437 assert(res
&& res
->RefCount
> 0);
439 return (!res
->RefCount
);
444 * Link a resource to its display.
447 _eglLinkResource(_EGLResource
*res
, _EGLResourceType type
)
449 assert(res
->Display
);
451 res
->IsLinked
= EGL_TRUE
;
452 res
->Next
= res
->Display
->ResourceLists
[type
];
453 res
->Display
->ResourceLists
[type
] = res
;
454 _eglGetResource(res
);
459 * Unlink a linked resource from its display.
462 _eglUnlinkResource(_EGLResource
*res
, _EGLResourceType type
)
466 prev
= res
->Display
->ResourceLists
[type
];
469 if (prev
->Next
== res
)
474 prev
->Next
= res
->Next
;
477 res
->Display
->ResourceLists
[type
] = res
->Next
;
481 res
->IsLinked
= EGL_FALSE
;
482 _eglPutResource(res
);
484 /* We always unlink before destroy. The driver still owns a reference */
485 assert(res
->RefCount
);
488 #ifdef HAVE_X11_PLATFORM
490 _eglGetX11Display(Display
*native_display
,
491 const EGLAttrib
*attrib_list
)
493 /* EGL_EXT_platform_x11 recognizes exactly one attribute,
494 * EGL_PLATFORM_X11_SCREEN_EXT, which is optional.
496 if (attrib_list
!= NULL
) {
497 for (int i
= 0; attrib_list
[i
] != EGL_NONE
; i
+= 2) {
498 if (attrib_list
[i
] != EGL_PLATFORM_X11_SCREEN_EXT
) {
499 _eglError(EGL_BAD_ATTRIBUTE
, "eglGetPlatformDisplay");
504 return _eglFindDisplay(_EGL_PLATFORM_X11
, native_display
, attrib_list
);
506 #endif /* HAVE_X11_PLATFORM */
508 #ifdef HAVE_DRM_PLATFORM
510 _eglGetGbmDisplay(struct gbm_device
*native_display
,
511 const EGLAttrib
*attrib_list
)
513 /* EGL_MESA_platform_gbm recognizes no attributes. */
514 if (attrib_list
!= NULL
&& attrib_list
[0] != EGL_NONE
) {
515 _eglError(EGL_BAD_ATTRIBUTE
, "eglGetPlatformDisplay");
519 return _eglFindDisplay(_EGL_PLATFORM_DRM
, native_display
, attrib_list
);
521 #endif /* HAVE_DRM_PLATFORM */
523 #ifdef HAVE_WAYLAND_PLATFORM
525 _eglGetWaylandDisplay(struct wl_display
*native_display
,
526 const EGLAttrib
*attrib_list
)
528 /* EGL_EXT_platform_wayland recognizes no attributes. */
529 if (attrib_list
!= NULL
&& attrib_list
[0] != EGL_NONE
) {
530 _eglError(EGL_BAD_ATTRIBUTE
, "eglGetPlatformDisplay");
534 return _eglFindDisplay(_EGL_PLATFORM_WAYLAND
, native_display
, attrib_list
);
536 #endif /* HAVE_WAYLAND_PLATFORM */
539 _eglGetSurfacelessDisplay(void *native_display
,
540 const EGLAttrib
*attrib_list
)
542 /* This platform has no native display. */
543 if (native_display
!= NULL
) {
544 _eglError(EGL_BAD_PARAMETER
, "eglGetPlatformDisplay");
548 /* This platform recognizes no display attributes. */
549 if (attrib_list
!= NULL
&& attrib_list
[0] != EGL_NONE
) {
550 _eglError(EGL_BAD_ATTRIBUTE
, "eglGetPlatformDisplay");
554 return _eglFindDisplay(_EGL_PLATFORM_SURFACELESS
, native_display
,
558 #ifdef HAVE_ANDROID_PLATFORM
560 _eglGetAndroidDisplay(void *native_display
,
561 const EGLAttrib
*attrib_list
)
564 /* This platform recognizes no display attributes. */
565 if (attrib_list
!= NULL
&& attrib_list
[0] != EGL_NONE
) {
566 _eglError(EGL_BAD_ATTRIBUTE
, "eglGetPlatformDisplay");
570 return _eglFindDisplay(_EGL_PLATFORM_ANDROID
, native_display
,
573 #endif /* HAVE_ANDROID_PLATFORM */
576 _eglGetDeviceDisplay(void *native_display
,
577 const EGLAttrib
*attrib_list
)
580 _EGLDisplay
*display
;
583 dev
= _eglLookupDevice(native_display
);
585 _eglError(EGL_BAD_PARAMETER
, "eglGetPlatformDisplay");
590 for (int i
= 0; attrib_list
[i
] != EGL_NONE
; i
+= 2) {
591 EGLAttrib attrib
= attrib_list
[i
];
592 EGLAttrib value
= attrib_list
[i
+ 1];
594 /* EGL_EXT_platform_device does not recognize any attributes,
595 * EGL_EXT_device_drm adds the optional EGL_DRM_MASTER_FD_EXT.
598 if (!_eglDeviceSupports(dev
, _EGL_DEVICE_DRM
) ||
599 attrib
!= EGL_DRM_MASTER_FD_EXT
) {
600 _eglError(EGL_BAD_ATTRIBUTE
, "eglGetPlatformDisplay");
608 display
= _eglFindDisplay(_EGL_PLATFORM_DEVICE
, native_display
, attrib_list
);
610 _eglError(EGL_BAD_ALLOC
, "eglGetPlatformDisplay");
614 /* If the fd is explicitly provided and we did not dup() it yet, do so.
615 * The spec mandates that we do so, since we'll need it past the
616 * eglGetPlatformDispay call.
618 * The new fd is guaranteed to be 3 or greater.
620 if (fd
!= -1 && display
->Options
.fd
== 0) {
621 display
->Options
.fd
= os_dupfd_cloexec(fd
);
622 if (display
->Options
.fd
== -1) {
623 /* Do not (really) need to teardown the display */
624 _eglError(EGL_BAD_ALLOC
, "eglGetPlatformDisplay");