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"
48 * Return the native platform by parsing EGL_PLATFORM.
50 static _EGLPlatformType
51 _eglGetNativePlatformFromEnv(void)
53 /* map --with-egl-platforms names to platform types */
55 _EGLPlatformType platform;
57 } egl_platforms[_EGL_NUM_PLATFORMS] = {
58 { _EGL_PLATFORM_WINDOWS, "gdi" },
59 { _EGL_PLATFORM_X11, "x11" },
60 { _EGL_PLATFORM_WAYLAND, "wayland" },
61 { _EGL_PLATFORM_DRM, "drm" },
62 { _EGL_PLATFORM_FBDEV, "fbdev" }
64 _EGLPlatformType plat = _EGL_INVALID_PLATFORM;
65 const char *plat_name;
68 plat_name = getenv("EGL_PLATFORM");
69 /* try deprecated env variable */
70 if (!plat_name || !plat_name[0])
71 plat_name = getenv("EGL_DISPLAY");
72 if (!plat_name || !plat_name[0])
73 return _EGL_INVALID_PLATFORM;
75 for (i = 0; i < _EGL_NUM_PLATFORMS; i++) {
76 if (strcmp(egl_platforms[i].name, plat_name) == 0) {
77 plat = egl_platforms[i].platform;
87 * Return the native platform. It is the platform of the EGL native types.
90 _eglGetNativePlatform(void)
92 static _EGLPlatformType native_platform = _EGL_INVALID_PLATFORM;
94 if (native_platform == _EGL_INVALID_PLATFORM) {
95 native_platform = _eglGetNativePlatformFromEnv();
96 if (native_platform == _EGL_INVALID_PLATFORM)
97 native_platform = _EGL_NATIVE_PLATFORM;
100 return native_platform;
105 * Finish display management.
108 _eglFiniDisplay(void)
110 _EGLDisplay *dpyList, *dpy;
112 /* atexit function is called with global mutex locked */
113 dpyList = _eglGlobal.DisplayList;
119 dpyList = dpyList->Next;
121 for (i = 0; i < _EGL_NUM_RESOURCES; i++) {
122 if (dpy->ResourceLists[i]) {
123 _eglLog(_EGL_DEBUG, "Display %p is destroyed with resources", dpy);
130 _eglGlobal.DisplayList = NULL;
135 * Find the display corresponding to the specified native display, or create a
139 _eglFindDisplay(_EGLPlatformType plat, void *plat_dpy)
143 if (plat == _EGL_INVALID_PLATFORM)
146 _eglLockMutex(_eglGlobal.Mutex);
148 /* search the display list first */
149 dpy = _eglGlobal.DisplayList;
151 if (dpy->Platform == plat && dpy->PlatformDisplay == plat_dpy)
156 /* create a new display */
158 dpy = (_EGLDisplay *) calloc(1, sizeof(_EGLDisplay));
160 _eglInitMutex(&dpy->Mutex);
161 dpy->Platform = plat;
162 dpy->PlatformDisplay = plat_dpy;
164 /* add to the display list */
165 dpy->Next = _eglGlobal.DisplayList;
166 _eglGlobal.DisplayList = dpy;
170 _eglUnlockMutex(_eglGlobal.Mutex);
177 * Destroy the contexts and surfaces that are linked to the display.
180 _eglReleaseDisplayResources(_EGLDriver *drv, _EGLDisplay *display)
184 list = display->ResourceLists[_EGL_RESOURCE_CONTEXT];
186 _EGLContext *ctx = (_EGLContext *) list;
189 _eglUnlinkContext(ctx);
190 drv->API.DestroyContext(drv, display, ctx);
192 assert(!display->ResourceLists[_EGL_RESOURCE_CONTEXT]);
194 list = display->ResourceLists[_EGL_RESOURCE_SURFACE];
196 _EGLSurface *surf = (_EGLSurface *) list;
199 _eglUnlinkSurface(surf);
200 drv->API.DestroySurface(drv, display, surf);
202 assert(!display->ResourceLists[_EGL_RESOURCE_SURFACE]);
207 * Free all the data hanging of an _EGLDisplay object, but not
211 _eglCleanupDisplay(_EGLDisplay *disp)
214 _eglDestroyArray(disp->Configs, free);
215 disp->Configs = NULL;
223 * Return EGL_TRUE if the given handle is a valid handle to a display.
226 _eglCheckDisplayHandle(EGLDisplay dpy)
230 _eglLockMutex(_eglGlobal.Mutex);
231 cur = _eglGlobal.DisplayList;
233 if (cur == (_EGLDisplay *) dpy)
237 _eglUnlockMutex(_eglGlobal.Mutex);
238 return (cur != NULL);
243 * Return EGL_TRUE if the given resource is valid. That is, the display does
247 _eglCheckResource(void *res, _EGLResourceType type, _EGLDisplay *dpy)
249 _EGLResource *list = dpy->ResourceLists[type];
255 if (res == (void *) list) {
256 assert(list->Display == dpy);
262 return (list != NULL);
267 * Initialize a display resource.
270 _eglInitResource(_EGLResource *res, EGLint size, _EGLDisplay *dpy)
272 memset(res, 0, size);
279 * Increment reference count for the resource.
282 _eglGetResource(_EGLResource *res)
284 assert(res && res->RefCount > 0);
285 /* hopefully a resource is always manipulated with its display locked */
291 * Decrement reference count for the resource.
294 _eglPutResource(_EGLResource *res)
296 assert(res && res->RefCount > 0);
298 return (!res->RefCount);
303 * Link a resource to its display.
306 _eglLinkResource(_EGLResource *res, _EGLResourceType type)
308 assert(res->Display);
310 res->IsLinked = EGL_TRUE;
311 res->Next = res->Display->ResourceLists[type];
312 res->Display->ResourceLists[type] = res;
313 _eglGetResource(res);
318 * Unlink a linked resource from its display.
321 _eglUnlinkResource(_EGLResource *res, _EGLResourceType type)
325 prev = res->Display->ResourceLists[type];
328 if (prev->Next == res)
333 prev->Next = res->Next;
336 res->Display->ResourceLists[type] = res->Next;
340 res->IsLinked = EGL_FALSE;
341 _eglPutResource(res);
343 /* We always unlink before destroy. The driver still owns a reference */
344 assert(res->RefCount);