1 /**************************************************************************
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
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 "eglsurface.h"
40 #include "egldisplay.h"
41 #include "egldriver.h"
42 #include "eglglobals.h"
46 /* Includes for _eglNativePlatformDetectNativeDisplay */
51 #ifdef HAVE_WAYLAND_PLATFORM
52 #include <wayland-client.h>
54 #ifdef HAVE_DRM_PLATFORM
57 #ifdef HAVE_FBDEV_PLATFORM
59 #include <sys/types.h>
65 * Map --with-egl-platforms names to platform types.
68 _EGLPlatformType platform
;
70 } egl_platforms
[_EGL_NUM_PLATFORMS
] = {
71 { _EGL_PLATFORM_WINDOWS
, "gdi" },
72 { _EGL_PLATFORM_X11
, "x11" },
73 { _EGL_PLATFORM_WAYLAND
, "wayland" },
74 { _EGL_PLATFORM_DRM
, "drm" },
75 { _EGL_PLATFORM_FBDEV
, "fbdev" }
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 * Perform validity checks on a generic pointer.
111 _eglPointerIsDereferencable(void *p
)
114 uintptr_t addr
= (uintptr_t) p
;
115 unsigned char valid
= 0;
116 const long page_size
= getpagesize();
121 /* align addr to page_size */
122 addr
&= ~(page_size
- 1);
124 if (mincore((void *) addr
, page_size
, &valid
) < 0) {
125 _eglLog(_EGL_DEBUG
, "mincore failed: %m");
129 return (valid
& 0x01) == 0x01;
137 * Try detecting native platform with the help of native display characteristcs.
139 static _EGLPlatformType
140 _eglNativePlatformDetectNativeDisplay(EGLNativeDisplayType nativeDisplay
)
142 #ifdef HAVE_FBDEV_PLATFORM
146 if (nativeDisplay
== EGL_DEFAULT_DISPLAY
)
147 return _EGL_INVALID_PLATFORM
;
149 #ifdef HAVE_FBDEV_PLATFORM
150 /* fbdev is the only platform that can be a file descriptor. */
151 if (fstat((intptr_t) nativeDisplay
, &buf
) == 0 && S_ISCHR(buf
.st_mode
))
152 return _EGL_PLATFORM_FBDEV
;
155 if (_eglPointerIsDereferencable(nativeDisplay
)) {
156 void *first_pointer
= *(void **) nativeDisplay
;
158 #ifdef HAVE_WAYLAND_PLATFORM
159 /* wl_display is a wl_proxy, which is a wl_object.
160 * wl_object's first element points to the interfacetype. */
161 if (first_pointer
== &wl_display_interface
)
162 return _EGL_PLATFORM_WAYLAND
;
165 #ifdef HAVE_DRM_PLATFORM
166 /* gbm has a pointer to its constructor as first element. */
167 if (first_pointer
== gbm_create_device
)
168 return _EGL_PLATFORM_DRM
;
171 #ifdef HAVE_X11_PLATFORM
172 /* If not matched to any other platform, fallback to x11. */
173 return _EGL_PLATFORM_X11
;
177 return _EGL_INVALID_PLATFORM
;
182 * Return the native platform. It is the platform of the EGL native types.
185 _eglGetNativePlatform(EGLNativeDisplayType nativeDisplay
)
187 static _EGLPlatformType native_platform
= _EGL_INVALID_PLATFORM
;
188 char *detection_method
= NULL
;
190 if (native_platform
== _EGL_INVALID_PLATFORM
) {
191 native_platform
= _eglGetNativePlatformFromEnv();
192 detection_method
= "environment overwrite";
193 if (native_platform
== _EGL_INVALID_PLATFORM
) {
194 native_platform
= _eglNativePlatformDetectNativeDisplay(nativeDisplay
);
195 detection_method
= "autodetected";
196 if (native_platform
== _EGL_INVALID_PLATFORM
) {
197 native_platform
= _EGL_NATIVE_PLATFORM
;
198 detection_method
= "build-time configuration";
203 if (detection_method
!= NULL
)
204 _eglLog(_EGL_DEBUG
, "Native platform type: %s (%s)",
205 egl_platforms
[native_platform
].name
, detection_method
);
207 return native_platform
;
212 * Finish display management.
215 _eglFiniDisplay(void)
217 _EGLDisplay
*dpyList
, *dpy
;
219 /* atexit function is called with global mutex locked */
220 dpyList
= _eglGlobal
.DisplayList
;
226 dpyList
= dpyList
->Next
;
228 for (i
= 0; i
< _EGL_NUM_RESOURCES
; i
++) {
229 if (dpy
->ResourceLists
[i
]) {
230 _eglLog(_EGL_DEBUG
, "Display %p is destroyed with resources", dpy
);
237 _eglGlobal
.DisplayList
= NULL
;
242 * Find the display corresponding to the specified native display, or create a
246 _eglFindDisplay(_EGLPlatformType plat
, void *plat_dpy
)
250 if (plat
== _EGL_INVALID_PLATFORM
)
253 _eglLockMutex(_eglGlobal
.Mutex
);
255 /* search the display list first */
256 dpy
= _eglGlobal
.DisplayList
;
258 if (dpy
->Platform
== plat
&& dpy
->PlatformDisplay
== plat_dpy
)
263 /* create a new display */
265 dpy
= (_EGLDisplay
*) calloc(1, sizeof(_EGLDisplay
));
267 _eglInitMutex(&dpy
->Mutex
);
268 dpy
->Platform
= plat
;
269 dpy
->PlatformDisplay
= plat_dpy
;
271 /* add to the display list */
272 dpy
->Next
= _eglGlobal
.DisplayList
;
273 _eglGlobal
.DisplayList
= dpy
;
277 _eglUnlockMutex(_eglGlobal
.Mutex
);
284 * Destroy the contexts and surfaces that are linked to the display.
287 _eglReleaseDisplayResources(_EGLDriver
*drv
, _EGLDisplay
*display
)
291 list
= display
->ResourceLists
[_EGL_RESOURCE_CONTEXT
];
293 _EGLContext
*ctx
= (_EGLContext
*) list
;
296 _eglUnlinkContext(ctx
);
297 drv
->API
.DestroyContext(drv
, display
, ctx
);
299 assert(!display
->ResourceLists
[_EGL_RESOURCE_CONTEXT
]);
301 list
= display
->ResourceLists
[_EGL_RESOURCE_SURFACE
];
303 _EGLSurface
*surf
= (_EGLSurface
*) list
;
306 _eglUnlinkSurface(surf
);
307 drv
->API
.DestroySurface(drv
, display
, surf
);
309 assert(!display
->ResourceLists
[_EGL_RESOURCE_SURFACE
]);
314 * Free all the data hanging of an _EGLDisplay object, but not
318 _eglCleanupDisplay(_EGLDisplay
*disp
)
321 _eglDestroyArray(disp
->Configs
, free
);
322 disp
->Configs
= NULL
;
330 * Return EGL_TRUE if the given handle is a valid handle to a display.
333 _eglCheckDisplayHandle(EGLDisplay dpy
)
337 _eglLockMutex(_eglGlobal
.Mutex
);
338 cur
= _eglGlobal
.DisplayList
;
340 if (cur
== (_EGLDisplay
*) dpy
)
344 _eglUnlockMutex(_eglGlobal
.Mutex
);
345 return (cur
!= NULL
);
350 * Return EGL_TRUE if the given resource is valid. That is, the display does
354 _eglCheckResource(void *res
, _EGLResourceType type
, _EGLDisplay
*dpy
)
356 _EGLResource
*list
= dpy
->ResourceLists
[type
];
362 if (res
== (void *) list
) {
363 assert(list
->Display
== dpy
);
369 return (list
!= NULL
);
374 * Initialize a display resource.
377 _eglInitResource(_EGLResource
*res
, EGLint size
, _EGLDisplay
*dpy
)
379 memset(res
, 0, size
);
386 * Increment reference count for the resource.
389 _eglGetResource(_EGLResource
*res
)
391 assert(res
&& res
->RefCount
> 0);
392 /* hopefully a resource is always manipulated with its display locked */
398 * Decrement reference count for the resource.
401 _eglPutResource(_EGLResource
*res
)
403 assert(res
&& res
->RefCount
> 0);
405 return (!res
->RefCount
);
410 * Link a resource to its display.
413 _eglLinkResource(_EGLResource
*res
, _EGLResourceType type
)
415 assert(res
->Display
);
417 res
->IsLinked
= EGL_TRUE
;
418 res
->Next
= res
->Display
->ResourceLists
[type
];
419 res
->Display
->ResourceLists
[type
] = res
;
420 _eglGetResource(res
);
425 * Unlink a linked resource from its display.
428 _eglUnlinkResource(_EGLResource
*res
, _EGLResourceType type
)
432 prev
= res
->Display
->ResourceLists
[type
];
435 if (prev
->Next
== res
)
440 prev
->Next
= res
->Next
;
443 res
->Display
->ResourceLists
[type
] = res
->Next
;
447 res
->IsLinked
= EGL_FALSE
;
448 _eglPutResource(res
);
450 /* We always unlink before destroy. The driver still owns a reference */
451 assert(res
->RefCount
);