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 disp
= _eglGlobal
.DisplayList
;
267 if (disp
->Platform
== plat
&& disp
->PlatformDisplay
== plat_dpy
&&
268 _eglSameAttribs(disp
->Options
.Attribs
, attrib_list
))
273 /* create a new display */
275 disp
= calloc(1, sizeof(_EGLDisplay
));
277 mtx_init(&disp
->Mutex
, mtx_plain
);
278 disp
->Platform
= plat
;
279 disp
->PlatformDisplay
= plat_dpy
;
280 num_attribs
= _eglNumAttribs(attrib_list
);
282 disp
->Options
.Attribs
= calloc(num_attribs
, sizeof(EGLAttrib
));
283 if (!disp
->Options
.Attribs
) {
288 memcpy(disp
->Options
.Attribs
, attrib_list
,
289 num_attribs
* sizeof(EGLAttrib
));
291 /* add to the display list */
292 disp
->Next
= _eglGlobal
.DisplayList
;
293 _eglGlobal
.DisplayList
= disp
;
298 mtx_unlock(_eglGlobal
.Mutex
);
305 * Destroy the contexts and surfaces that are linked to the display.
308 _eglReleaseDisplayResources(_EGLDriver
*drv
, _EGLDisplay
*display
)
312 list
= display
->ResourceLists
[_EGL_RESOURCE_CONTEXT
];
314 _EGLContext
*ctx
= (_EGLContext
*) list
;
317 _eglUnlinkContext(ctx
);
318 drv
->API
.DestroyContext(drv
, display
, ctx
);
320 assert(!display
->ResourceLists
[_EGL_RESOURCE_CONTEXT
]);
322 list
= display
->ResourceLists
[_EGL_RESOURCE_SURFACE
];
324 _EGLSurface
*surf
= (_EGLSurface
*) list
;
327 _eglUnlinkSurface(surf
);
328 drv
->API
.DestroySurface(drv
, display
, surf
);
330 assert(!display
->ResourceLists
[_EGL_RESOURCE_SURFACE
]);
332 list
= display
->ResourceLists
[_EGL_RESOURCE_IMAGE
];
334 _EGLImage
*image
= (_EGLImage
*) list
;
337 _eglUnlinkImage(image
);
338 drv
->API
.DestroyImageKHR(drv
, display
, image
);
340 assert(!display
->ResourceLists
[_EGL_RESOURCE_IMAGE
]);
342 list
= display
->ResourceLists
[_EGL_RESOURCE_SYNC
];
344 _EGLSync
*sync
= (_EGLSync
*) list
;
347 _eglUnlinkSync(sync
);
348 drv
->API
.DestroySyncKHR(drv
, display
, sync
);
350 assert(!display
->ResourceLists
[_EGL_RESOURCE_SYNC
]);
355 * Free all the data hanging of an _EGLDisplay object, but not
359 _eglCleanupDisplay(_EGLDisplay
*disp
)
362 _eglDestroyArray(disp
->Configs
, free
);
363 disp
->Configs
= NULL
;
371 * Return EGL_TRUE if the given handle is a valid handle to a display.
374 _eglCheckDisplayHandle(EGLDisplay dpy
)
378 mtx_lock(_eglGlobal
.Mutex
);
379 cur
= _eglGlobal
.DisplayList
;
381 if (cur
== (_EGLDisplay
*) dpy
)
385 mtx_unlock(_eglGlobal
.Mutex
);
386 return (cur
!= NULL
);
391 * Return EGL_TRUE if the given resource is valid. That is, the display does
395 _eglCheckResource(void *res
, _EGLResourceType type
, _EGLDisplay
*disp
)
397 _EGLResource
*list
= disp
->ResourceLists
[type
];
403 if (res
== (void *) list
) {
404 assert(list
->Display
== disp
);
410 return (list
!= NULL
);
415 * Initialize a display resource. The size of the subclass object is
418 * This is supposed to be called from the initializers of subclasses, such as
419 * _eglInitContext or _eglInitSurface.
422 _eglInitResource(_EGLResource
*res
, EGLint size
, _EGLDisplay
*disp
)
424 memset(res
, 0, size
);
431 * Increment reference count for the resource.
434 _eglGetResource(_EGLResource
*res
)
436 assert(res
&& res
->RefCount
> 0);
437 /* hopefully a resource is always manipulated with its display locked */
443 * Decrement reference count for the resource.
446 _eglPutResource(_EGLResource
*res
)
448 assert(res
&& res
->RefCount
> 0);
450 return (!res
->RefCount
);
455 * Link a resource to its display.
458 _eglLinkResource(_EGLResource
*res
, _EGLResourceType type
)
460 assert(res
->Display
);
462 res
->IsLinked
= EGL_TRUE
;
463 res
->Next
= res
->Display
->ResourceLists
[type
];
464 res
->Display
->ResourceLists
[type
] = res
;
465 _eglGetResource(res
);
470 * Unlink a linked resource from its display.
473 _eglUnlinkResource(_EGLResource
*res
, _EGLResourceType type
)
477 prev
= res
->Display
->ResourceLists
[type
];
480 if (prev
->Next
== res
)
485 prev
->Next
= res
->Next
;
488 res
->Display
->ResourceLists
[type
] = res
->Next
;
492 res
->IsLinked
= EGL_FALSE
;
493 _eglPutResource(res
);
495 /* We always unlink before destroy. The driver still owns a reference */
496 assert(res
->RefCount
);
499 #ifdef HAVE_X11_PLATFORM
501 _eglGetX11Display(Display
*native_display
,
502 const EGLAttrib
*attrib_list
)
504 /* EGL_EXT_platform_x11 recognizes exactly one attribute,
505 * EGL_PLATFORM_X11_SCREEN_EXT, which is optional.
507 if (attrib_list
!= NULL
) {
508 for (int i
= 0; attrib_list
[i
] != EGL_NONE
; i
+= 2) {
509 if (attrib_list
[i
] != EGL_PLATFORM_X11_SCREEN_EXT
) {
510 _eglError(EGL_BAD_ATTRIBUTE
, "eglGetPlatformDisplay");
515 return _eglFindDisplay(_EGL_PLATFORM_X11
, native_display
, attrib_list
);
517 #endif /* HAVE_X11_PLATFORM */
519 #ifdef HAVE_DRM_PLATFORM
521 _eglGetGbmDisplay(struct gbm_device
*native_display
,
522 const EGLAttrib
*attrib_list
)
524 /* EGL_MESA_platform_gbm recognizes no attributes. */
525 if (attrib_list
!= NULL
&& attrib_list
[0] != EGL_NONE
) {
526 _eglError(EGL_BAD_ATTRIBUTE
, "eglGetPlatformDisplay");
530 return _eglFindDisplay(_EGL_PLATFORM_DRM
, native_display
, attrib_list
);
532 #endif /* HAVE_DRM_PLATFORM */
534 #ifdef HAVE_WAYLAND_PLATFORM
536 _eglGetWaylandDisplay(struct wl_display
*native_display
,
537 const EGLAttrib
*attrib_list
)
539 /* EGL_EXT_platform_wayland recognizes no attributes. */
540 if (attrib_list
!= NULL
&& attrib_list
[0] != EGL_NONE
) {
541 _eglError(EGL_BAD_ATTRIBUTE
, "eglGetPlatformDisplay");
545 return _eglFindDisplay(_EGL_PLATFORM_WAYLAND
, native_display
, attrib_list
);
547 #endif /* HAVE_WAYLAND_PLATFORM */
549 #ifdef HAVE_SURFACELESS_PLATFORM
551 _eglGetSurfacelessDisplay(void *native_display
,
552 const EGLAttrib
*attrib_list
)
554 /* This platform has no native display. */
555 if (native_display
!= NULL
) {
556 _eglError(EGL_BAD_PARAMETER
, "eglGetPlatformDisplay");
560 /* This platform recognizes no display attributes. */
561 if (attrib_list
!= NULL
&& attrib_list
[0] != EGL_NONE
) {
562 _eglError(EGL_BAD_ATTRIBUTE
, "eglGetPlatformDisplay");
566 return _eglFindDisplay(_EGL_PLATFORM_SURFACELESS
, native_display
,
569 #endif /* HAVE_SURFACELESS_PLATFORM */
573 _eglGetDeviceDisplay(void *native_display
,
574 const EGLAttrib
*attrib_list
)
577 _EGLDisplay
*display
;
580 dev
= _eglLookupDevice(native_display
);
582 _eglError(EGL_BAD_PARAMETER
, "eglGetPlatformDisplay");
587 for (int i
= 0; attrib_list
[i
] != EGL_NONE
; i
+= 2) {
588 EGLAttrib attrib
= attrib_list
[i
];
589 EGLAttrib value
= attrib_list
[i
+ 1];
591 /* EGL_EXT_platform_device does not recognize any attributes,
592 * EGL_EXT_device_drm adds the optional EGL_DRM_MASTER_FD_EXT.
595 if (!_eglDeviceSupports(dev
, _EGL_DEVICE_DRM
) ||
596 attrib
!= EGL_DRM_MASTER_FD_EXT
) {
597 _eglError(EGL_BAD_ATTRIBUTE
, "eglGetPlatformDisplay");
605 display
= _eglFindDisplay(_EGL_PLATFORM_DEVICE
, native_display
, attrib_list
);
607 _eglError(EGL_BAD_ALLOC
, "eglGetPlatformDisplay");
611 /* If the fd is explicitly provided and we did not dup() it yet, do so.
612 * The spec mandates that we do so, since we'll need it past the
613 * eglGetPlatformDispay call.
615 * The new fd is guaranteed to be 3 or greater.
617 if (fd
!= -1 && display
->Options
.fd
== 0) {
618 display
->Options
.fd
= fcntl(fd
, F_DUPFD_CLOEXEC
, 3);
619 if (display
->Options
.fd
== -1) {
620 /* Do not (really) need to teardown the display */
621 _eglError(EGL_BAD_ALLOC
, "eglGetPlatformDisplay");