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"
40 #include "eglcontext.h"
41 #include "eglcurrent.h"
42 #include "eglsurface.h"
43 #include "egldisplay.h"
44 #include "egldriver.h"
45 #include "eglglobals.h"
48 /* Includes for _eglNativePlatformDetectNativeDisplay */
53 #ifdef HAVE_WAYLAND_PLATFORM
54 #include <wayland-client.h>
56 #ifdef HAVE_DRM_PLATFORM
59 #ifdef HAVE_FBDEV_PLATFORM
61 #include <sys/types.h>
67 * Map --with-egl-platforms names to platform types.
70 _EGLPlatformType platform
;
72 } egl_platforms
[_EGL_NUM_PLATFORMS
] = {
73 { _EGL_PLATFORM_WINDOWS
, "gdi" },
74 { _EGL_PLATFORM_X11
, "x11" },
75 { _EGL_PLATFORM_WAYLAND
, "wayland" },
76 { _EGL_PLATFORM_DRM
, "drm" },
77 { _EGL_PLATFORM_FBDEV
, "fbdev" },
78 { _EGL_PLATFORM_NULL
, "null" },
79 { _EGL_PLATFORM_ANDROID
, "android" },
80 { _EGL_PLATFORM_HAIKU
, "haiku" }
85 * Return the native platform by parsing EGL_PLATFORM.
87 static _EGLPlatformType
88 _eglGetNativePlatformFromEnv(void)
90 _EGLPlatformType plat
= _EGL_INVALID_PLATFORM
;
91 const char *plat_name
;
94 plat_name
= getenv("EGL_PLATFORM");
95 /* try deprecated env variable */
96 if (!plat_name
|| !plat_name
[0])
97 plat_name
= getenv("EGL_DISPLAY");
98 if (!plat_name
|| !plat_name
[0])
99 return _EGL_INVALID_PLATFORM
;
101 for (i
= 0; i
< _EGL_NUM_PLATFORMS
; i
++) {
102 if (strcmp(egl_platforms
[i
].name
, plat_name
) == 0) {
103 plat
= egl_platforms
[i
].platform
;
113 * Perform validity checks on a generic pointer.
116 _eglPointerIsDereferencable(void *p
)
119 uintptr_t addr
= (uintptr_t) p
;
120 unsigned char valid
= 0;
121 const long page_size
= getpagesize();
126 /* align addr to page_size */
127 addr
&= ~(page_size
- 1);
129 if (mincore((void *) addr
, page_size
, &valid
) < 0) {
130 _eglLog(_EGL_DEBUG
, "mincore failed: %m");
134 return (valid
& 0x01) == 0x01;
142 * Try detecting native platform with the help of native display characteristcs.
144 static _EGLPlatformType
145 _eglNativePlatformDetectNativeDisplay(void *nativeDisplay
)
147 #ifdef HAVE_FBDEV_PLATFORM
151 if (nativeDisplay
== EGL_DEFAULT_DISPLAY
)
152 return _EGL_INVALID_PLATFORM
;
154 #ifdef HAVE_FBDEV_PLATFORM
155 /* fbdev is the only platform that can be a file descriptor. */
156 if (fstat((intptr_t) nativeDisplay
, &buf
) == 0 && S_ISCHR(buf
.st_mode
))
157 return _EGL_PLATFORM_FBDEV
;
160 if (_eglPointerIsDereferencable(nativeDisplay
)) {
161 void *first_pointer
= *(void **) nativeDisplay
;
163 (void) first_pointer
; /* silence unused var warning */
165 #ifdef HAVE_WAYLAND_PLATFORM
166 /* wl_display is a wl_proxy, which is a wl_object.
167 * wl_object's first element points to the interfacetype. */
168 if (first_pointer
== &wl_display_interface
)
169 return _EGL_PLATFORM_WAYLAND
;
172 #ifdef HAVE_DRM_PLATFORM
173 /* gbm has a pointer to its constructor as first element. */
174 if (first_pointer
== gbm_create_device
)
175 return _EGL_PLATFORM_DRM
;
178 #ifdef HAVE_X11_PLATFORM
179 /* If not matched to any other platform, fallback to x11. */
180 return _EGL_PLATFORM_X11
;
183 #ifdef HAVE_HAIKU_PLATFORM
184 return _EGL_PLATFORM_HAIKU
;
188 return _EGL_INVALID_PLATFORM
;
193 * Return the native platform. It is the platform of the EGL native types.
196 _eglGetNativePlatform(void *nativeDisplay
)
198 static _EGLPlatformType native_platform
= _EGL_INVALID_PLATFORM
;
199 char *detection_method
= NULL
;
201 if (native_platform
== _EGL_INVALID_PLATFORM
) {
202 native_platform
= _eglGetNativePlatformFromEnv();
203 detection_method
= "environment overwrite";
204 if (native_platform
== _EGL_INVALID_PLATFORM
) {
205 native_platform
= _eglNativePlatformDetectNativeDisplay(nativeDisplay
);
206 detection_method
= "autodetected";
207 if (native_platform
== _EGL_INVALID_PLATFORM
) {
208 native_platform
= _EGL_NATIVE_PLATFORM
;
209 detection_method
= "build-time configuration";
214 if (detection_method
!= NULL
)
215 _eglLog(_EGL_DEBUG
, "Native platform type: %s (%s)",
216 egl_platforms
[native_platform
].name
, detection_method
);
218 return native_platform
;
223 * Finish display management.
226 _eglFiniDisplay(void)
228 _EGLDisplay
*dpyList
, *dpy
;
230 /* atexit function is called with global mutex locked */
231 dpyList
= _eglGlobal
.DisplayList
;
237 dpyList
= dpyList
->Next
;
239 for (i
= 0; i
< _EGL_NUM_RESOURCES
; i
++) {
240 if (dpy
->ResourceLists
[i
]) {
241 _eglLog(_EGL_DEBUG
, "Display %p is destroyed with resources", dpy
);
248 _eglGlobal
.DisplayList
= NULL
;
253 * Find the display corresponding to the specified native display, or create a
257 _eglFindDisplay(_EGLPlatformType plat
, void *plat_dpy
)
261 if (plat
== _EGL_INVALID_PLATFORM
)
264 mtx_lock(_eglGlobal
.Mutex
);
266 /* search the display list first */
267 dpy
= _eglGlobal
.DisplayList
;
269 if (dpy
->Platform
== plat
&& dpy
->PlatformDisplay
== plat_dpy
)
274 /* create a new display */
276 dpy
= calloc(1, sizeof(_EGLDisplay
));
278 mtx_init(&dpy
->Mutex
, mtx_plain
);
279 dpy
->Platform
= plat
;
280 dpy
->PlatformDisplay
= plat_dpy
;
282 /* add to the display list */
283 dpy
->Next
= _eglGlobal
.DisplayList
;
284 _eglGlobal
.DisplayList
= dpy
;
288 mtx_unlock(_eglGlobal
.Mutex
);
295 * Destroy the contexts and surfaces that are linked to the display.
298 _eglReleaseDisplayResources(_EGLDriver
*drv
, _EGLDisplay
*display
)
302 list
= display
->ResourceLists
[_EGL_RESOURCE_CONTEXT
];
304 _EGLContext
*ctx
= (_EGLContext
*) list
;
307 _eglUnlinkContext(ctx
);
308 drv
->API
.DestroyContext(drv
, display
, ctx
);
310 assert(!display
->ResourceLists
[_EGL_RESOURCE_CONTEXT
]);
312 list
= display
->ResourceLists
[_EGL_RESOURCE_SURFACE
];
314 _EGLSurface
*surf
= (_EGLSurface
*) list
;
317 _eglUnlinkSurface(surf
);
318 drv
->API
.DestroySurface(drv
, display
, surf
);
320 assert(!display
->ResourceLists
[_EGL_RESOURCE_SURFACE
]);
325 * Free all the data hanging of an _EGLDisplay object, but not
329 _eglCleanupDisplay(_EGLDisplay
*disp
)
332 _eglDestroyArray(disp
->Configs
, free
);
333 disp
->Configs
= NULL
;
341 * Return EGL_TRUE if the given handle is a valid handle to a display.
344 _eglCheckDisplayHandle(EGLDisplay dpy
)
348 mtx_lock(_eglGlobal
.Mutex
);
349 cur
= _eglGlobal
.DisplayList
;
351 if (cur
== (_EGLDisplay
*) dpy
)
355 mtx_unlock(_eglGlobal
.Mutex
);
356 return (cur
!= NULL
);
361 * Return EGL_TRUE if the given resource is valid. That is, the display does
365 _eglCheckResource(void *res
, _EGLResourceType type
, _EGLDisplay
*dpy
)
367 _EGLResource
*list
= dpy
->ResourceLists
[type
];
373 if (res
== (void *) list
) {
374 assert(list
->Display
== dpy
);
380 return (list
!= NULL
);
385 * Initialize a display resource. The size of the subclass object is
388 * This is supposed to be called from the initializers of subclasses, such as
389 * _eglInitContext or _eglInitSurface.
392 _eglInitResource(_EGLResource
*res
, EGLint size
, _EGLDisplay
*dpy
)
394 memset(res
, 0, size
);
401 * Increment reference count for the resource.
404 _eglGetResource(_EGLResource
*res
)
406 assert(res
&& res
->RefCount
> 0);
407 /* hopefully a resource is always manipulated with its display locked */
413 * Decrement reference count for the resource.
416 _eglPutResource(_EGLResource
*res
)
418 assert(res
&& res
->RefCount
> 0);
420 return (!res
->RefCount
);
425 * Link a resource to its display.
428 _eglLinkResource(_EGLResource
*res
, _EGLResourceType type
)
430 assert(res
->Display
);
432 res
->IsLinked
= EGL_TRUE
;
433 res
->Next
= res
->Display
->ResourceLists
[type
];
434 res
->Display
->ResourceLists
[type
] = res
;
435 _eglGetResource(res
);
440 * Unlink a linked resource from its display.
443 _eglUnlinkResource(_EGLResource
*res
, _EGLResourceType type
)
447 prev
= res
->Display
->ResourceLists
[type
];
450 if (prev
->Next
== res
)
455 prev
->Next
= res
->Next
;
458 res
->Display
->ResourceLists
[type
] = res
->Next
;
462 res
->IsLinked
= EGL_FALSE
;
463 _eglPutResource(res
);
465 /* We always unlink before destroy. The driver still owns a reference */
466 assert(res
->RefCount
);
469 #ifdef HAVE_X11_PLATFORM
471 _eglParseX11DisplayAttribList(const EGLint
*attrib_list
)
475 if (attrib_list
== NULL
) {
479 for (i
= 0; attrib_list
[i
] != EGL_NONE
; i
+= 2) {
480 EGLint attrib
= attrib_list
[i
];
481 EGLint value
= attrib_list
[i
+ 1];
483 /* EGL_EXT_platform_x11 recognizes exactly one attribute,
484 * EGL_PLATFORM_X11_SCREEN_EXT, which is optional.
486 * Mesa supports connecting to only the default screen, so we reject
489 if (attrib
!= EGL_PLATFORM_X11_SCREEN_EXT
|| value
!= 0) {
490 _eglError(EGL_BAD_ATTRIBUTE
, "eglGetPlatformDisplay");
499 _eglGetX11Display(Display
*native_display
,
500 const EGLint
*attrib_list
)
502 if (!_eglParseX11DisplayAttribList(attrib_list
)) {
506 return _eglFindDisplay(_EGL_PLATFORM_X11
, native_display
);
508 #endif /* HAVE_X11_PLATFORM */
510 #ifdef HAVE_DRM_PLATFORM
512 _eglGetGbmDisplay(struct gbm_device
*native_display
,
513 const EGLint
*attrib_list
)
515 /* EGL_MESA_platform_gbm recognizes no attributes. */
516 if (attrib_list
!= NULL
&& attrib_list
[0] != EGL_NONE
) {
517 _eglError(EGL_BAD_ATTRIBUTE
, "eglGetPlatformDisplay");
521 return _eglFindDisplay(_EGL_PLATFORM_DRM
, native_display
);
523 #endif /* HAVE_DRM_PLATFORM */
525 #ifdef HAVE_WAYLAND_PLATFORM
527 _eglGetWaylandDisplay(struct wl_display
*native_display
,
528 const EGLint
*attrib_list
)
530 /* EGL_EXT_platform_wayland recognizes no attributes. */
531 if (attrib_list
!= NULL
&& attrib_list
[0] != EGL_NONE
) {
532 _eglError(EGL_BAD_ATTRIBUTE
, "eglGetPlatformDisplay");
536 return _eglFindDisplay(_EGL_PLATFORM_WAYLAND
, native_display
);
538 #endif /* HAVE_WAYLAND_PLATFORM */