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" },
76 { _EGL_PLATFORM_ANDROID
, "android" }
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(EGLNativeDisplayType nativeDisplay
)
143 #ifdef HAVE_FBDEV_PLATFORM
147 if (nativeDisplay
== EGL_DEFAULT_DISPLAY
)
148 return _EGL_INVALID_PLATFORM
;
150 #ifdef HAVE_FBDEV_PLATFORM
151 /* fbdev is the only platform that can be a file descriptor. */
152 if (fstat((intptr_t) nativeDisplay
, &buf
) == 0 && S_ISCHR(buf
.st_mode
))
153 return _EGL_PLATFORM_FBDEV
;
156 if (_eglPointerIsDereferencable(nativeDisplay
)) {
157 void *first_pointer
= *(void **) nativeDisplay
;
159 #ifdef HAVE_WAYLAND_PLATFORM
160 /* wl_display is a wl_proxy, which is a wl_object.
161 * wl_object's first element points to the interfacetype. */
162 if (first_pointer
== &wl_display_interface
)
163 return _EGL_PLATFORM_WAYLAND
;
166 #ifdef HAVE_DRM_PLATFORM
167 /* gbm has a pointer to its constructor as first element. */
168 if (first_pointer
== gbm_create_device
)
169 return _EGL_PLATFORM_DRM
;
172 #ifdef HAVE_X11_PLATFORM
173 /* If not matched to any other platform, fallback to x11. */
174 return _EGL_PLATFORM_X11
;
178 return _EGL_INVALID_PLATFORM
;
183 * Return the native platform. It is the platform of the EGL native types.
186 _eglGetNativePlatform(EGLNativeDisplayType nativeDisplay
)
188 static _EGLPlatformType native_platform
= _EGL_INVALID_PLATFORM
;
189 char *detection_method
= NULL
;
191 if (native_platform
== _EGL_INVALID_PLATFORM
) {
192 native_platform
= _eglGetNativePlatformFromEnv();
193 detection_method
= "environment overwrite";
194 if (native_platform
== _EGL_INVALID_PLATFORM
) {
195 native_platform
= _eglNativePlatformDetectNativeDisplay(nativeDisplay
);
196 detection_method
= "autodetected";
197 if (native_platform
== _EGL_INVALID_PLATFORM
) {
198 native_platform
= _EGL_NATIVE_PLATFORM
;
199 detection_method
= "build-time configuration";
204 if (detection_method
!= NULL
)
205 _eglLog(_EGL_DEBUG
, "Native platform type: %s (%s)",
206 egl_platforms
[native_platform
].name
, detection_method
);
208 return native_platform
;
213 * Finish display management.
216 _eglFiniDisplay(void)
218 _EGLDisplay
*dpyList
, *dpy
;
220 /* atexit function is called with global mutex locked */
221 dpyList
= _eglGlobal
.DisplayList
;
227 dpyList
= dpyList
->Next
;
229 for (i
= 0; i
< _EGL_NUM_RESOURCES
; i
++) {
230 if (dpy
->ResourceLists
[i
]) {
231 _eglLog(_EGL_DEBUG
, "Display %p is destroyed with resources", dpy
);
238 _eglGlobal
.DisplayList
= NULL
;
243 * Find the display corresponding to the specified native display, or create a
247 _eglFindDisplay(_EGLPlatformType plat
, void *plat_dpy
)
251 if (plat
== _EGL_INVALID_PLATFORM
)
254 _eglLockMutex(_eglGlobal
.Mutex
);
256 /* search the display list first */
257 dpy
= _eglGlobal
.DisplayList
;
259 if (dpy
->Platform
== plat
&& dpy
->PlatformDisplay
== plat_dpy
)
264 /* create a new display */
266 dpy
= (_EGLDisplay
*) calloc(1, sizeof(_EGLDisplay
));
268 _eglInitMutex(&dpy
->Mutex
);
269 dpy
->Platform
= plat
;
270 dpy
->PlatformDisplay
= plat_dpy
;
272 /* add to the display list */
273 dpy
->Next
= _eglGlobal
.DisplayList
;
274 _eglGlobal
.DisplayList
= dpy
;
278 _eglUnlockMutex(_eglGlobal
.Mutex
);
285 * Destroy the contexts and surfaces that are linked to the display.
288 _eglReleaseDisplayResources(_EGLDriver
*drv
, _EGLDisplay
*display
)
292 list
= display
->ResourceLists
[_EGL_RESOURCE_CONTEXT
];
294 _EGLContext
*ctx
= (_EGLContext
*) list
;
297 _eglUnlinkContext(ctx
);
298 drv
->API
.DestroyContext(drv
, display
, ctx
);
300 assert(!display
->ResourceLists
[_EGL_RESOURCE_CONTEXT
]);
302 list
= display
->ResourceLists
[_EGL_RESOURCE_SURFACE
];
304 _EGLSurface
*surf
= (_EGLSurface
*) list
;
307 _eglUnlinkSurface(surf
);
308 drv
->API
.DestroySurface(drv
, display
, surf
);
310 assert(!display
->ResourceLists
[_EGL_RESOURCE_SURFACE
]);
315 * Free all the data hanging of an _EGLDisplay object, but not
319 _eglCleanupDisplay(_EGLDisplay
*disp
)
322 _eglDestroyArray(disp
->Configs
, free
);
323 disp
->Configs
= NULL
;
331 * Return EGL_TRUE if the given handle is a valid handle to a display.
334 _eglCheckDisplayHandle(EGLDisplay dpy
)
338 _eglLockMutex(_eglGlobal
.Mutex
);
339 cur
= _eglGlobal
.DisplayList
;
341 if (cur
== (_EGLDisplay
*) dpy
)
345 _eglUnlockMutex(_eglGlobal
.Mutex
);
346 return (cur
!= NULL
);
351 * Return EGL_TRUE if the given resource is valid. That is, the display does
355 _eglCheckResource(void *res
, _EGLResourceType type
, _EGLDisplay
*dpy
)
357 _EGLResource
*list
= dpy
->ResourceLists
[type
];
363 if (res
== (void *) list
) {
364 assert(list
->Display
== dpy
);
370 return (list
!= NULL
);
375 * Initialize a display resource.
378 _eglInitResource(_EGLResource
*res
, EGLint size
, _EGLDisplay
*dpy
)
380 memset(res
, 0, size
);
387 * Increment reference count for the resource.
390 _eglGetResource(_EGLResource
*res
)
392 assert(res
&& res
->RefCount
> 0);
393 /* hopefully a resource is always manipulated with its display locked */
399 * Decrement reference count for the resource.
402 _eglPutResource(_EGLResource
*res
)
404 assert(res
&& res
->RefCount
> 0);
406 return (!res
->RefCount
);
411 * Link a resource to its display.
414 _eglLinkResource(_EGLResource
*res
, _EGLResourceType type
)
416 assert(res
->Display
);
418 res
->IsLinked
= EGL_TRUE
;
419 res
->Next
= res
->Display
->ResourceLists
[type
];
420 res
->Display
->ResourceLists
[type
] = res
;
421 _eglGetResource(res
);
426 * Unlink a linked resource from its display.
429 _eglUnlinkResource(_EGLResource
*res
, _EGLResourceType type
)
433 prev
= res
->Display
->ResourceLists
[type
];
436 if (prev
->Next
== res
)
441 prev
->Next
= res
->Next
;
444 res
->Display
->ResourceLists
[type
] = res
->Next
;
448 res
->IsLinked
= EGL_FALSE
;
449 _eglPutResource(res
);
451 /* We always unlink before destroy. The driver still owns a reference */
452 assert(res
->RefCount
);