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 "eglcontext.h"
39 #include "eglcurrent.h"
40 #include "eglsurface.h"
41 #include "egldisplay.h"
42 #include "egldriver.h"
43 #include "eglglobals.h"
47 /* Includes for _eglNativePlatformDetectNativeDisplay */
52 #ifdef HAVE_WAYLAND_PLATFORM
53 #include <wayland-client.h>
55 #ifdef HAVE_DRM_PLATFORM
58 #ifdef HAVE_FBDEV_PLATFORM
60 #include <sys/types.h>
66 * Map --with-egl-platforms names to platform types.
69 _EGLPlatformType platform
;
71 } egl_platforms
[_EGL_NUM_PLATFORMS
] = {
72 { _EGL_PLATFORM_WINDOWS
, "gdi" },
73 { _EGL_PLATFORM_X11
, "x11" },
74 { _EGL_PLATFORM_WAYLAND
, "wayland" },
75 { _EGL_PLATFORM_DRM
, "drm" },
76 { _EGL_PLATFORM_FBDEV
, "fbdev" },
77 { _EGL_PLATFORM_NULL
, "null" },
78 { _EGL_PLATFORM_ANDROID
, "android" }
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 plat_name
= getenv("EGL_PLATFORM");
93 /* try deprecated env variable */
94 if (!plat_name
|| !plat_name
[0])
95 plat_name
= getenv("EGL_DISPLAY");
96 if (!plat_name
|| !plat_name
[0])
97 return _EGL_INVALID_PLATFORM
;
99 for (i
= 0; i
< _EGL_NUM_PLATFORMS
; i
++) {
100 if (strcmp(egl_platforms
[i
].name
, plat_name
) == 0) {
101 plat
= egl_platforms
[i
].platform
;
111 * Perform validity checks on a generic pointer.
114 _eglPointerIsDereferencable(void *p
)
117 uintptr_t addr
= (uintptr_t) p
;
118 unsigned char valid
= 0;
119 const long page_size
= getpagesize();
124 /* align addr to page_size */
125 addr
&= ~(page_size
- 1);
127 if (mincore((void *) addr
, page_size
, &valid
) < 0) {
128 _eglLog(_EGL_DEBUG
, "mincore failed: %m");
132 return (valid
& 0x01) == 0x01;
140 * Try detecting native platform with the help of native display characteristcs.
142 static _EGLPlatformType
143 _eglNativePlatformDetectNativeDisplay(void *nativeDisplay
)
145 #ifdef HAVE_FBDEV_PLATFORM
149 if (nativeDisplay
== EGL_DEFAULT_DISPLAY
)
150 return _EGL_INVALID_PLATFORM
;
152 #ifdef HAVE_FBDEV_PLATFORM
153 /* fbdev is the only platform that can be a file descriptor. */
154 if (fstat((intptr_t) nativeDisplay
, &buf
) == 0 && S_ISCHR(buf
.st_mode
))
155 return _EGL_PLATFORM_FBDEV
;
158 if (_eglPointerIsDereferencable(nativeDisplay
)) {
159 void *first_pointer
= *(void **) nativeDisplay
;
161 (void) first_pointer
; /* silence unused var warning */
163 #ifdef HAVE_WAYLAND_PLATFORM
164 /* wl_display is a wl_proxy, which is a wl_object.
165 * wl_object's first element points to the interfacetype. */
166 if (first_pointer
== &wl_display_interface
)
167 return _EGL_PLATFORM_WAYLAND
;
170 #ifdef HAVE_DRM_PLATFORM
171 /* gbm has a pointer to its constructor as first element. */
172 if (first_pointer
== gbm_create_device
)
173 return _EGL_PLATFORM_DRM
;
176 #ifdef HAVE_X11_PLATFORM
177 /* If not matched to any other platform, fallback to x11. */
178 return _EGL_PLATFORM_X11
;
182 return _EGL_INVALID_PLATFORM
;
187 * Return the native platform. It is the platform of the EGL native types.
190 _eglGetNativePlatform(void *nativeDisplay
)
192 static _EGLPlatformType native_platform
= _EGL_INVALID_PLATFORM
;
193 char *detection_method
= NULL
;
195 if (native_platform
== _EGL_INVALID_PLATFORM
) {
196 native_platform
= _eglGetNativePlatformFromEnv();
197 detection_method
= "environment overwrite";
198 if (native_platform
== _EGL_INVALID_PLATFORM
) {
199 native_platform
= _eglNativePlatformDetectNativeDisplay(nativeDisplay
);
200 detection_method
= "autodetected";
201 if (native_platform
== _EGL_INVALID_PLATFORM
) {
202 native_platform
= _EGL_NATIVE_PLATFORM
;
203 detection_method
= "build-time configuration";
208 if (detection_method
!= NULL
)
209 _eglLog(_EGL_DEBUG
, "Native platform type: %s (%s)",
210 egl_platforms
[native_platform
].name
, detection_method
);
212 return native_platform
;
217 * Finish display management.
220 _eglFiniDisplay(void)
222 _EGLDisplay
*dpyList
, *dpy
;
224 /* atexit function is called with global mutex locked */
225 dpyList
= _eglGlobal
.DisplayList
;
231 dpyList
= dpyList
->Next
;
233 for (i
= 0; i
< _EGL_NUM_RESOURCES
; i
++) {
234 if (dpy
->ResourceLists
[i
]) {
235 _eglLog(_EGL_DEBUG
, "Display %p is destroyed with resources", dpy
);
242 _eglGlobal
.DisplayList
= NULL
;
247 * Find the display corresponding to the specified native display, or create a
251 _eglFindDisplay(_EGLPlatformType plat
, void *plat_dpy
)
255 if (plat
== _EGL_INVALID_PLATFORM
)
258 _eglLockMutex(_eglGlobal
.Mutex
);
260 /* search the display list first */
261 dpy
= _eglGlobal
.DisplayList
;
263 if (dpy
->Platform
== plat
&& dpy
->PlatformDisplay
== plat_dpy
)
268 /* create a new display */
270 dpy
= calloc(1, sizeof(_EGLDisplay
));
272 _eglInitMutex(&dpy
->Mutex
);
273 dpy
->Platform
= plat
;
274 dpy
->PlatformDisplay
= plat_dpy
;
276 /* add to the display list */
277 dpy
->Next
= _eglGlobal
.DisplayList
;
278 _eglGlobal
.DisplayList
= dpy
;
282 _eglUnlockMutex(_eglGlobal
.Mutex
);
289 * Destroy the contexts and surfaces that are linked to the display.
292 _eglReleaseDisplayResources(_EGLDriver
*drv
, _EGLDisplay
*display
)
296 list
= display
->ResourceLists
[_EGL_RESOURCE_CONTEXT
];
298 _EGLContext
*ctx
= (_EGLContext
*) list
;
301 _eglUnlinkContext(ctx
);
302 drv
->API
.DestroyContext(drv
, display
, ctx
);
304 assert(!display
->ResourceLists
[_EGL_RESOURCE_CONTEXT
]);
306 list
= display
->ResourceLists
[_EGL_RESOURCE_SURFACE
];
308 _EGLSurface
*surf
= (_EGLSurface
*) list
;
311 _eglUnlinkSurface(surf
);
312 drv
->API
.DestroySurface(drv
, display
, surf
);
314 assert(!display
->ResourceLists
[_EGL_RESOURCE_SURFACE
]);
319 * Free all the data hanging of an _EGLDisplay object, but not
323 _eglCleanupDisplay(_EGLDisplay
*disp
)
326 _eglDestroyArray(disp
->Configs
, free
);
327 disp
->Configs
= NULL
;
335 * Return EGL_TRUE if the given handle is a valid handle to a display.
338 _eglCheckDisplayHandle(EGLDisplay dpy
)
342 _eglLockMutex(_eglGlobal
.Mutex
);
343 cur
= _eglGlobal
.DisplayList
;
345 if (cur
== (_EGLDisplay
*) dpy
)
349 _eglUnlockMutex(_eglGlobal
.Mutex
);
350 return (cur
!= NULL
);
355 * Return EGL_TRUE if the given resource is valid. That is, the display does
359 _eglCheckResource(void *res
, _EGLResourceType type
, _EGLDisplay
*dpy
)
361 _EGLResource
*list
= dpy
->ResourceLists
[type
];
367 if (res
== (void *) list
) {
368 assert(list
->Display
== dpy
);
374 return (list
!= NULL
);
379 * Initialize a display resource. The size of the subclass object is
382 * This is supposed to be called from the initializers of subclasses, such as
383 * _eglInitContext or _eglInitSurface.
386 _eglInitResource(_EGLResource
*res
, EGLint size
, _EGLDisplay
*dpy
)
388 memset(res
, 0, size
);
395 * Increment reference count for the resource.
398 _eglGetResource(_EGLResource
*res
)
400 assert(res
&& res
->RefCount
> 0);
401 /* hopefully a resource is always manipulated with its display locked */
407 * Decrement reference count for the resource.
410 _eglPutResource(_EGLResource
*res
)
412 assert(res
&& res
->RefCount
> 0);
414 return (!res
->RefCount
);
419 * Link a resource to its display.
422 _eglLinkResource(_EGLResource
*res
, _EGLResourceType type
)
424 assert(res
->Display
);
426 res
->IsLinked
= EGL_TRUE
;
427 res
->Next
= res
->Display
->ResourceLists
[type
];
428 res
->Display
->ResourceLists
[type
] = res
;
429 _eglGetResource(res
);
434 * Unlink a linked resource from its display.
437 _eglUnlinkResource(_EGLResource
*res
, _EGLResourceType type
)
441 prev
= res
->Display
->ResourceLists
[type
];
444 if (prev
->Next
== res
)
449 prev
->Next
= res
->Next
;
452 res
->Display
->ResourceLists
[type
] = res
->Next
;
456 res
->IsLinked
= EGL_FALSE
;
457 _eglPutResource(res
);
459 /* We always unlink before destroy. The driver still owns a reference */
460 assert(res
->RefCount
);
463 #ifdef HAVE_X11_PLATFORM
465 _eglParseX11DisplayAttribList(const EGLint
*attrib_list
)
469 if (attrib_list
== NULL
) {
473 for (i
= 0; attrib_list
[i
] != EGL_NONE
; i
+= 2) {
474 EGLint attrib
= attrib_list
[i
];
475 EGLint value
= attrib_list
[i
+ 1];
477 /* EGL_EXT_platform_x11 recognizes exactly one attribute,
478 * EGL_PLATFORM_X11_SCREEN_EXT, which is optional.
480 * Mesa supports connecting to only the default screen, so we reject
483 if (attrib
!= EGL_PLATFORM_X11_SCREEN_EXT
|| value
!= 0) {
484 _eglError(EGL_BAD_ATTRIBUTE
, "eglGetPlatformDisplay");
493 _eglGetX11Display(Display
*native_display
,
494 const EGLint
*attrib_list
)
496 if (!_eglParseX11DisplayAttribList(attrib_list
)) {
500 return _eglFindDisplay(_EGL_PLATFORM_X11
, native_display
);
502 #endif /* HAVE_X11_PLATFORM */
504 #ifdef HAVE_DRM_PLATFORM
506 _eglGetGbmDisplay(struct gbm_device
*native_display
,
507 const EGLint
*attrib_list
)
509 /* EGL_MESA_platform_gbm recognizes no attributes. */
510 if (attrib_list
!= NULL
&& attrib_list
[0] != EGL_NONE
) {
511 _eglError(EGL_BAD_ATTRIBUTE
, "eglGetPlatformDisplay");
515 return _eglFindDisplay(_EGL_PLATFORM_DRM
, native_display
);
517 #endif /* HAVE_DRM_PLATFORM */
519 #ifdef HAVE_WAYLAND_PLATFORM
521 _eglGetWaylandDisplay(struct wl_display
*native_display
,
522 const EGLint
*attrib_list
)
524 /* EGL_EXT_platform_wayland recognizes no attributes. */
525 if (attrib_list
!= NULL
&& attrib_list
[0] != EGL_NONE
) {
526 _eglError(EGL_BAD_ATTRIBUTE
, "eglGetPlatformDisplay");
530 return _eglFindDisplay(_EGL_PLATFORM_WAYLAND
, native_display
);
532 #endif /* HAVE_WAYLAND_PLATFORM */