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.
38 #include "c11/threads.h"
39 #include "util/u_atomic.h"
41 #include "eglcontext.h"
42 #include "eglcurrent.h"
43 #include "eglsurface.h"
44 #include "egldisplay.h"
45 #include "egldriver.h"
46 #include "eglglobals.h"
51 /* Includes for _eglNativePlatformDetectNativeDisplay */
56 #ifdef HAVE_WAYLAND_PLATFORM
57 #include <wayland-client.h>
59 #ifdef HAVE_DRM_PLATFORM
65 * Map --with-platforms names to platform types.
68 _EGLPlatformType platform
;
70 } egl_platforms
[_EGL_NUM_PLATFORMS
] = {
71 { _EGL_PLATFORM_X11
, "x11" },
72 { _EGL_PLATFORM_WAYLAND
, "wayland" },
73 { _EGL_PLATFORM_DRM
, "drm" },
74 { _EGL_PLATFORM_ANDROID
, "android" },
75 { _EGL_PLATFORM_HAIKU
, "haiku" },
76 { _EGL_PLATFORM_SURFACELESS
, "surfaceless" },
81 * Return the native platform by parsing EGL_PLATFORM.
83 static _EGLPlatformType
84 _eglGetNativePlatformFromEnv(void)
86 _EGLPlatformType plat
= _EGL_INVALID_PLATFORM
;
87 const char *plat_name
;
90 plat_name
= getenv("EGL_PLATFORM");
91 /* try deprecated env variable */
92 if (!plat_name
|| !plat_name
[0])
93 plat_name
= getenv("EGL_DISPLAY");
94 if (!plat_name
|| !plat_name
[0])
95 return _EGL_INVALID_PLATFORM
;
97 for (i
= 0; i
< _EGL_NUM_PLATFORMS
; i
++) {
98 if (strcmp(egl_platforms
[i
].name
, plat_name
) == 0) {
99 plat
= egl_platforms
[i
].platform
;
109 * Perform validity checks on a generic pointer.
112 _eglPointerIsDereferencable(void *p
)
115 uintptr_t addr
= (uintptr_t) p
;
116 unsigned char valid
= 0;
117 const long page_size
= getpagesize();
122 /* align addr to page_size */
123 addr
&= ~(page_size
- 1);
125 if (mincore((void *) addr
, page_size
, &valid
) < 0) {
126 _eglLog(_EGL_DEBUG
, "mincore failed: %m");
130 return (valid
& 0x01) == 0x01;
138 * Try detecting native platform with the help of native display characteristcs.
140 static _EGLPlatformType
141 _eglNativePlatformDetectNativeDisplay(void *nativeDisplay
)
143 if (nativeDisplay
== EGL_DEFAULT_DISPLAY
)
144 return _EGL_INVALID_PLATFORM
;
146 if (_eglPointerIsDereferencable(nativeDisplay
)) {
147 void *first_pointer
= *(void **) nativeDisplay
;
149 (void) first_pointer
; /* silence unused var warning */
151 #ifdef HAVE_WAYLAND_PLATFORM
152 /* wl_display is a wl_proxy, which is a wl_object.
153 * wl_object's first element points to the interfacetype. */
154 if (first_pointer
== &wl_display_interface
)
155 return _EGL_PLATFORM_WAYLAND
;
158 #ifdef HAVE_DRM_PLATFORM
159 /* gbm has a pointer to its constructor as first element. */
160 if (first_pointer
== gbm_create_device
)
161 return _EGL_PLATFORM_DRM
;
164 #ifdef HAVE_X11_PLATFORM
165 /* If not matched to any other platform, fallback to x11. */
166 return _EGL_PLATFORM_X11
;
169 #ifdef HAVE_HAIKU_PLATFORM
170 return _EGL_PLATFORM_HAIKU
;
174 return _EGL_INVALID_PLATFORM
;
179 * Return the native platform. It is the platform of the EGL native types.
182 _eglGetNativePlatform(void *nativeDisplay
)
184 static _EGLPlatformType native_platform
= _EGL_INVALID_PLATFORM
;
185 _EGLPlatformType detected_platform
= native_platform
;
187 if (detected_platform
== _EGL_INVALID_PLATFORM
) {
188 const char *detection_method
;
190 detected_platform
= _eglGetNativePlatformFromEnv();
191 detection_method
= "environment overwrite";
193 if (detected_platform
== _EGL_INVALID_PLATFORM
) {
194 detected_platform
= _eglNativePlatformDetectNativeDisplay(nativeDisplay
);
195 detection_method
= "autodetected";
198 if (detected_platform
== _EGL_INVALID_PLATFORM
) {
199 detected_platform
= _EGL_NATIVE_PLATFORM
;
200 detection_method
= "build-time configuration";
203 _eglLog(_EGL_DEBUG
, "Native platform type: %s (%s)",
204 egl_platforms
[detected_platform
].name
, detection_method
);
206 p_atomic_cmpxchg(&native_platform
, _EGL_INVALID_PLATFORM
,
210 return native_platform
;
215 * Finish display management.
218 _eglFiniDisplay(void)
220 _EGLDisplay
*dpyList
, *dpy
;
222 /* atexit function is called with global mutex locked */
223 dpyList
= _eglGlobal
.DisplayList
;
229 dpyList
= dpyList
->Next
;
231 for (i
= 0; i
< _EGL_NUM_RESOURCES
; i
++) {
232 if (dpy
->ResourceLists
[i
]) {
233 _eglLog(_EGL_DEBUG
, "Display %p is destroyed with resources", dpy
);
240 _eglGlobal
.DisplayList
= NULL
;
245 * Find the display corresponding to the specified native display, or create a
249 _eglFindDisplay(_EGLPlatformType plat
, void *plat_dpy
)
253 if (plat
== _EGL_INVALID_PLATFORM
)
256 mtx_lock(_eglGlobal
.Mutex
);
258 /* search the display list first */
259 dpy
= _eglGlobal
.DisplayList
;
261 if (dpy
->Platform
== plat
&& dpy
->PlatformDisplay
== plat_dpy
)
266 /* create a new display */
268 dpy
= calloc(1, sizeof(_EGLDisplay
));
270 mtx_init(&dpy
->Mutex
, mtx_plain
);
271 dpy
->Platform
= plat
;
272 dpy
->PlatformDisplay
= plat_dpy
;
274 /* add to the display list */
275 dpy
->Next
= _eglGlobal
.DisplayList
;
276 _eglGlobal
.DisplayList
= dpy
;
280 mtx_unlock(_eglGlobal
.Mutex
);
287 * Destroy the contexts and surfaces that are linked to the display.
290 _eglReleaseDisplayResources(_EGLDriver
*drv
, _EGLDisplay
*display
)
294 list
= display
->ResourceLists
[_EGL_RESOURCE_CONTEXT
];
296 _EGLContext
*ctx
= (_EGLContext
*) list
;
299 _eglUnlinkContext(ctx
);
300 drv
->API
.DestroyContext(drv
, display
, ctx
);
302 assert(!display
->ResourceLists
[_EGL_RESOURCE_CONTEXT
]);
304 list
= display
->ResourceLists
[_EGL_RESOURCE_SURFACE
];
306 _EGLSurface
*surf
= (_EGLSurface
*) list
;
309 _eglUnlinkSurface(surf
);
310 drv
->API
.DestroySurface(drv
, display
, surf
);
312 assert(!display
->ResourceLists
[_EGL_RESOURCE_SURFACE
]);
314 list
= display
->ResourceLists
[_EGL_RESOURCE_IMAGE
];
316 _EGLImage
*image
= (_EGLImage
*) list
;
319 _eglUnlinkImage(image
);
320 drv
->API
.DestroyImageKHR(drv
, display
, image
);
322 assert(!display
->ResourceLists
[_EGL_RESOURCE_IMAGE
]);
324 list
= display
->ResourceLists
[_EGL_RESOURCE_SYNC
];
326 _EGLSync
*sync
= (_EGLSync
*) list
;
329 _eglUnlinkSync(sync
);
330 drv
->API
.DestroySyncKHR(drv
, display
, sync
);
332 assert(!display
->ResourceLists
[_EGL_RESOURCE_SYNC
]);
337 * Free all the data hanging of an _EGLDisplay object, but not
341 _eglCleanupDisplay(_EGLDisplay
*disp
)
344 _eglDestroyArray(disp
->Configs
, free
);
345 disp
->Configs
= NULL
;
353 * Return EGL_TRUE if the given handle is a valid handle to a display.
356 _eglCheckDisplayHandle(EGLDisplay dpy
)
360 mtx_lock(_eglGlobal
.Mutex
);
361 cur
= _eglGlobal
.DisplayList
;
363 if (cur
== (_EGLDisplay
*) dpy
)
367 mtx_unlock(_eglGlobal
.Mutex
);
368 return (cur
!= NULL
);
373 * Return EGL_TRUE if the given resource is valid. That is, the display does
377 _eglCheckResource(void *res
, _EGLResourceType type
, _EGLDisplay
*dpy
)
379 _EGLResource
*list
= dpy
->ResourceLists
[type
];
385 if (res
== (void *) list
) {
386 assert(list
->Display
== dpy
);
392 return (list
!= NULL
);
397 * Initialize a display resource. The size of the subclass object is
400 * This is supposed to be called from the initializers of subclasses, such as
401 * _eglInitContext or _eglInitSurface.
404 _eglInitResource(_EGLResource
*res
, EGLint size
, _EGLDisplay
*dpy
)
406 memset(res
, 0, size
);
413 * Increment reference count for the resource.
416 _eglGetResource(_EGLResource
*res
)
418 assert(res
&& res
->RefCount
> 0);
419 /* hopefully a resource is always manipulated with its display locked */
425 * Decrement reference count for the resource.
428 _eglPutResource(_EGLResource
*res
)
430 assert(res
&& res
->RefCount
> 0);
432 return (!res
->RefCount
);
437 * Link a resource to its display.
440 _eglLinkResource(_EGLResource
*res
, _EGLResourceType type
)
442 assert(res
->Display
);
444 res
->IsLinked
= EGL_TRUE
;
445 res
->Next
= res
->Display
->ResourceLists
[type
];
446 res
->Display
->ResourceLists
[type
] = res
;
447 _eglGetResource(res
);
452 * Unlink a linked resource from its display.
455 _eglUnlinkResource(_EGLResource
*res
, _EGLResourceType type
)
459 prev
= res
->Display
->ResourceLists
[type
];
462 if (prev
->Next
== res
)
467 prev
->Next
= res
->Next
;
470 res
->Display
->ResourceLists
[type
] = res
->Next
;
474 res
->IsLinked
= EGL_FALSE
;
475 _eglPutResource(res
);
477 /* We always unlink before destroy. The driver still owns a reference */
478 assert(res
->RefCount
);
481 #ifdef HAVE_X11_PLATFORM
483 _eglParseX11DisplayAttribList(_EGLDisplay
*display
, const EGLint
*attrib_list
)
487 if (attrib_list
== NULL
) {
491 for (i
= 0; attrib_list
[i
] != EGL_NONE
; i
+= 2) {
492 EGLint attrib
= attrib_list
[i
];
493 EGLint value
= attrib_list
[i
+ 1];
495 /* EGL_EXT_platform_x11 recognizes exactly one attribute,
496 * EGL_PLATFORM_X11_SCREEN_EXT, which is optional.
498 if (attrib
== EGL_PLATFORM_X11_SCREEN_EXT
) {
499 display
->Options
.Platform
= (void *)(uintptr_t)value
;
501 _eglError(EGL_BAD_ATTRIBUTE
, "eglGetPlatformDisplay");
510 _eglGetX11Display(Display
*native_display
,
511 const EGLint
*attrib_list
)
513 _EGLDisplay
*display
= _eglFindDisplay(_EGL_PLATFORM_X11
,
517 _eglError(EGL_BAD_ALLOC
, "eglGetPlatformDisplay");
521 if (!_eglParseX11DisplayAttribList(display
, attrib_list
)) {
527 #endif /* HAVE_X11_PLATFORM */
529 #ifdef HAVE_DRM_PLATFORM
531 _eglGetGbmDisplay(struct gbm_device
*native_display
,
532 const EGLint
*attrib_list
)
534 /* EGL_MESA_platform_gbm recognizes no attributes. */
535 if (attrib_list
!= NULL
&& attrib_list
[0] != EGL_NONE
) {
536 _eglError(EGL_BAD_ATTRIBUTE
, "eglGetPlatformDisplay");
540 return _eglFindDisplay(_EGL_PLATFORM_DRM
, native_display
);
542 #endif /* HAVE_DRM_PLATFORM */
544 #ifdef HAVE_WAYLAND_PLATFORM
546 _eglGetWaylandDisplay(struct wl_display
*native_display
,
547 const EGLint
*attrib_list
)
549 /* EGL_EXT_platform_wayland recognizes no attributes. */
550 if (attrib_list
!= NULL
&& attrib_list
[0] != EGL_NONE
) {
551 _eglError(EGL_BAD_ATTRIBUTE
, "eglGetPlatformDisplay");
555 return _eglFindDisplay(_EGL_PLATFORM_WAYLAND
, native_display
);
557 #endif /* HAVE_WAYLAND_PLATFORM */
559 #ifdef HAVE_SURFACELESS_PLATFORM
561 _eglGetSurfacelessDisplay(void *native_display
,
562 const EGLint
*attrib_list
)
564 /* This platform has no native display. */
565 if (native_display
!= NULL
) {
566 _eglError(EGL_BAD_PARAMETER
, "eglGetPlatformDisplay");
570 /* This platform recognizes no display attributes. */
571 if (attrib_list
!= NULL
&& attrib_list
[0] != EGL_NONE
) {
572 _eglError(EGL_BAD_ATTRIBUTE
, "eglGetPlatformDisplay");
576 return _eglFindDisplay(_EGL_PLATFORM_SURFACELESS
, native_display
);
578 #endif /* HAVE_SURFACELESS_PLATFORM */