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" },
79 { _EGL_PLATFORM_HAIKU
, "haiku" }
84 * Return the native platform by parsing EGL_PLATFORM.
86 static _EGLPlatformType
87 _eglGetNativePlatformFromEnv(void)
89 _EGLPlatformType plat
= _EGL_INVALID_PLATFORM
;
90 const char *plat_name
;
93 plat_name
= getenv("EGL_PLATFORM");
94 /* try deprecated env variable */
95 if (!plat_name
|| !plat_name
[0])
96 plat_name
= getenv("EGL_DISPLAY");
97 if (!plat_name
|| !plat_name
[0])
98 return _EGL_INVALID_PLATFORM
;
100 for (i
= 0; i
< _EGL_NUM_PLATFORMS
; i
++) {
101 if (strcmp(egl_platforms
[i
].name
, plat_name
) == 0) {
102 plat
= egl_platforms
[i
].platform
;
112 * Perform validity checks on a generic pointer.
115 _eglPointerIsDereferencable(void *p
)
118 uintptr_t addr
= (uintptr_t) p
;
119 unsigned char valid
= 0;
120 const long page_size
= getpagesize();
125 /* align addr to page_size */
126 addr
&= ~(page_size
- 1);
128 if (mincore((void *) addr
, page_size
, &valid
) < 0) {
129 _eglLog(_EGL_DEBUG
, "mincore failed: %m");
133 return (valid
& 0x01) == 0x01;
141 * Try detecting native platform with the help of native display characteristcs.
143 static _EGLPlatformType
144 _eglNativePlatformDetectNativeDisplay(void *nativeDisplay
)
146 #ifdef HAVE_FBDEV_PLATFORM
150 if (nativeDisplay
== EGL_DEFAULT_DISPLAY
)
151 return _EGL_INVALID_PLATFORM
;
153 #ifdef HAVE_FBDEV_PLATFORM
154 /* fbdev is the only platform that can be a file descriptor. */
155 if (fstat((intptr_t) nativeDisplay
, &buf
) == 0 && S_ISCHR(buf
.st_mode
))
156 return _EGL_PLATFORM_FBDEV
;
159 if (_eglPointerIsDereferencable(nativeDisplay
)) {
160 void *first_pointer
= *(void **) nativeDisplay
;
162 (void) first_pointer
; /* silence unused var warning */
164 #ifdef HAVE_WAYLAND_PLATFORM
165 /* wl_display is a wl_proxy, which is a wl_object.
166 * wl_object's first element points to the interfacetype. */
167 if (first_pointer
== &wl_display_interface
)
168 return _EGL_PLATFORM_WAYLAND
;
171 #ifdef HAVE_DRM_PLATFORM
172 /* gbm has a pointer to its constructor as first element. */
173 if (first_pointer
== gbm_create_device
)
174 return _EGL_PLATFORM_DRM
;
177 #ifdef HAVE_X11_PLATFORM
178 /* If not matched to any other platform, fallback to x11. */
179 return _EGL_PLATFORM_X11
;
182 #ifdef HAVE_HAIKU_PLATFORM
183 return _EGL_PLATFORM_HAIKU
;
187 return _EGL_INVALID_PLATFORM
;
192 * Return the native platform. It is the platform of the EGL native types.
195 _eglGetNativePlatform(void *nativeDisplay
)
197 static _EGLPlatformType native_platform
= _EGL_INVALID_PLATFORM
;
198 char *detection_method
= NULL
;
200 if (native_platform
== _EGL_INVALID_PLATFORM
) {
201 native_platform
= _eglGetNativePlatformFromEnv();
202 detection_method
= "environment overwrite";
203 if (native_platform
== _EGL_INVALID_PLATFORM
) {
204 native_platform
= _eglNativePlatformDetectNativeDisplay(nativeDisplay
);
205 detection_method
= "autodetected";
206 if (native_platform
== _EGL_INVALID_PLATFORM
) {
207 native_platform
= _EGL_NATIVE_PLATFORM
;
208 detection_method
= "build-time configuration";
213 if (detection_method
!= NULL
)
214 _eglLog(_EGL_DEBUG
, "Native platform type: %s (%s)",
215 egl_platforms
[native_platform
].name
, detection_method
);
217 return native_platform
;
222 * Finish display management.
225 _eglFiniDisplay(void)
227 _EGLDisplay
*dpyList
, *dpy
;
229 /* atexit function is called with global mutex locked */
230 dpyList
= _eglGlobal
.DisplayList
;
236 dpyList
= dpyList
->Next
;
238 for (i
= 0; i
< _EGL_NUM_RESOURCES
; i
++) {
239 if (dpy
->ResourceLists
[i
]) {
240 _eglLog(_EGL_DEBUG
, "Display %p is destroyed with resources", dpy
);
247 _eglGlobal
.DisplayList
= NULL
;
252 * Find the display corresponding to the specified native display, or create a
256 _eglFindDisplay(_EGLPlatformType plat
, void *plat_dpy
)
260 if (plat
== _EGL_INVALID_PLATFORM
)
263 _eglLockMutex(_eglGlobal
.Mutex
);
265 /* search the display list first */
266 dpy
= _eglGlobal
.DisplayList
;
268 if (dpy
->Platform
== plat
&& dpy
->PlatformDisplay
== plat_dpy
)
273 /* create a new display */
275 dpy
= calloc(1, sizeof(_EGLDisplay
));
277 _eglInitMutex(&dpy
->Mutex
);
278 dpy
->Platform
= plat
;
279 dpy
->PlatformDisplay
= plat_dpy
;
281 /* add to the display list */
282 dpy
->Next
= _eglGlobal
.DisplayList
;
283 _eglGlobal
.DisplayList
= dpy
;
287 _eglUnlockMutex(_eglGlobal
.Mutex
);
294 * Destroy the contexts and surfaces that are linked to the display.
297 _eglReleaseDisplayResources(_EGLDriver
*drv
, _EGLDisplay
*display
)
301 list
= display
->ResourceLists
[_EGL_RESOURCE_CONTEXT
];
303 _EGLContext
*ctx
= (_EGLContext
*) list
;
306 _eglUnlinkContext(ctx
);
307 drv
->API
.DestroyContext(drv
, 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
->API
.DestroySurface(drv
, display
, surf
);
319 assert(!display
->ResourceLists
[_EGL_RESOURCE_SURFACE
]);
324 * Free all the data hanging of an _EGLDisplay object, but not
328 _eglCleanupDisplay(_EGLDisplay
*disp
)
331 _eglDestroyArray(disp
->Configs
, free
);
332 disp
->Configs
= NULL
;
340 * Return EGL_TRUE if the given handle is a valid handle to a display.
343 _eglCheckDisplayHandle(EGLDisplay dpy
)
347 _eglLockMutex(_eglGlobal
.Mutex
);
348 cur
= _eglGlobal
.DisplayList
;
350 if (cur
== (_EGLDisplay
*) dpy
)
354 _eglUnlockMutex(_eglGlobal
.Mutex
);
355 return (cur
!= NULL
);
360 * Return EGL_TRUE if the given resource is valid. That is, the display does
364 _eglCheckResource(void *res
, _EGLResourceType type
, _EGLDisplay
*dpy
)
366 _EGLResource
*list
= dpy
->ResourceLists
[type
];
372 if (res
== (void *) list
) {
373 assert(list
->Display
== dpy
);
379 return (list
!= NULL
);
384 * Initialize a display resource. The size of the subclass object is
387 * This is supposed to be called from the initializers of subclasses, such as
388 * _eglInitContext or _eglInitSurface.
391 _eglInitResource(_EGLResource
*res
, EGLint size
, _EGLDisplay
*dpy
)
393 memset(res
, 0, size
);
400 * Increment reference count for the resource.
403 _eglGetResource(_EGLResource
*res
)
405 assert(res
&& res
->RefCount
> 0);
406 /* hopefully a resource is always manipulated with its display locked */
412 * Decrement reference count for the resource.
415 _eglPutResource(_EGLResource
*res
)
417 assert(res
&& res
->RefCount
> 0);
419 return (!res
->RefCount
);
424 * Link a resource to its display.
427 _eglLinkResource(_EGLResource
*res
, _EGLResourceType type
)
429 assert(res
->Display
);
431 res
->IsLinked
= EGL_TRUE
;
432 res
->Next
= res
->Display
->ResourceLists
[type
];
433 res
->Display
->ResourceLists
[type
] = res
;
434 _eglGetResource(res
);
439 * Unlink a linked resource from its display.
442 _eglUnlinkResource(_EGLResource
*res
, _EGLResourceType type
)
446 prev
= res
->Display
->ResourceLists
[type
];
449 if (prev
->Next
== res
)
454 prev
->Next
= res
->Next
;
457 res
->Display
->ResourceLists
[type
] = res
->Next
;
461 res
->IsLinked
= EGL_FALSE
;
462 _eglPutResource(res
);
464 /* We always unlink before destroy. The driver still owns a reference */
465 assert(res
->RefCount
);
468 #ifdef HAVE_X11_PLATFORM
470 _eglParseX11DisplayAttribList(const EGLint
*attrib_list
)
474 if (attrib_list
== NULL
) {
478 for (i
= 0; attrib_list
[i
] != EGL_NONE
; i
+= 2) {
479 EGLint attrib
= attrib_list
[i
];
480 EGLint value
= attrib_list
[i
+ 1];
482 /* EGL_EXT_platform_x11 recognizes exactly one attribute,
483 * EGL_PLATFORM_X11_SCREEN_EXT, which is optional.
485 * Mesa supports connecting to only the default screen, so we reject
488 if (attrib
!= EGL_PLATFORM_X11_SCREEN_EXT
|| value
!= 0) {
489 _eglError(EGL_BAD_ATTRIBUTE
, "eglGetPlatformDisplay");
498 _eglGetX11Display(Display
*native_display
,
499 const EGLint
*attrib_list
)
501 if (!_eglParseX11DisplayAttribList(attrib_list
)) {
505 return _eglFindDisplay(_EGL_PLATFORM_X11
, native_display
);
507 #endif /* HAVE_X11_PLATFORM */
509 #ifdef HAVE_DRM_PLATFORM
511 _eglGetGbmDisplay(struct gbm_device
*native_display
,
512 const EGLint
*attrib_list
)
514 /* EGL_MESA_platform_gbm recognizes no attributes. */
515 if (attrib_list
!= NULL
&& attrib_list
[0] != EGL_NONE
) {
516 _eglError(EGL_BAD_ATTRIBUTE
, "eglGetPlatformDisplay");
520 return _eglFindDisplay(_EGL_PLATFORM_DRM
, native_display
);
522 #endif /* HAVE_DRM_PLATFORM */
524 #ifdef HAVE_WAYLAND_PLATFORM
526 _eglGetWaylandDisplay(struct wl_display
*native_display
,
527 const EGLint
*attrib_list
)
529 /* EGL_EXT_platform_wayland recognizes no attributes. */
530 if (attrib_list
!= NULL
&& attrib_list
[0] != EGL_NONE
) {
531 _eglError(EGL_BAD_ATTRIBUTE
, "eglGetPlatformDisplay");
535 return _eglFindDisplay(_EGL_PLATFORM_WAYLAND
, native_display
);
537 #endif /* HAVE_WAYLAND_PLATFORM */