Check for SYS/GL during library init. Reason is that
[AROS.git] / workbench / libs / mesa / src / egl / main / egldisplay.c
blob61cc089acb339cabf64ac0b3ae65de7149335612
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.
6 * All Rights Reserved.
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
18 * of the Software.
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 **************************************************************************/
31 /**
32 * Functions related to EGLDisplay.
35 #include <assert.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include "eglcontext.h"
39 #include "eglsurface.h"
40 #include "egldisplay.h"
41 #include "egldriver.h"
42 #include "eglglobals.h"
43 #include "eglmutex.h"
44 #include "egllog.h"
47 /**
48 * Return the native platform by parsing EGL_PLATFORM.
50 static _EGLPlatformType
51 _eglGetNativePlatformFromEnv(void)
53 /* map --with-egl-platforms names to platform types */
54 static const struct {
55 _EGLPlatformType platform;
56 const char *name;
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" },
63 { _EGL_PLATFORM_AROS, "aros" }
65 _EGLPlatformType plat = _EGL_INVALID_PLATFORM;
66 const char *plat_name;
67 EGLint i;
69 plat_name = getenv("EGL_PLATFORM");
70 /* try deprecated env variable */
71 if (!plat_name || !plat_name[0])
72 plat_name = getenv("EGL_DISPLAY");
73 if (!plat_name || !plat_name[0])
74 return _EGL_INVALID_PLATFORM;
76 for (i = 0; i < _EGL_NUM_PLATFORMS; i++) {
77 if (strcmp(egl_platforms[i].name, plat_name) == 0) {
78 plat = egl_platforms[i].platform;
79 break;
83 return plat;
87 /**
88 * Return the native platform. It is the platform of the EGL native types.
90 _EGLPlatformType
91 _eglGetNativePlatform(void)
93 static _EGLPlatformType native_platform = _EGL_INVALID_PLATFORM;
95 if (native_platform == _EGL_INVALID_PLATFORM) {
96 native_platform = _eglGetNativePlatformFromEnv();
97 if (native_platform == _EGL_INVALID_PLATFORM)
98 native_platform = _EGL_NATIVE_PLATFORM;
101 return native_platform;
106 * Finish display management.
108 void
109 _eglFiniDisplay(void)
111 _EGLDisplay *dpyList, *dpy;
113 /* atexit function is called with global mutex locked */
114 dpyList = _eglGlobal.DisplayList;
115 while (dpyList) {
116 EGLint i;
118 /* pop list head */
119 dpy = dpyList;
120 dpyList = dpyList->Next;
122 for (i = 0; i < _EGL_NUM_RESOURCES; i++) {
123 if (dpy->ResourceLists[i]) {
124 _eglLog(_EGL_DEBUG, "Display %p is destroyed with resources", dpy);
125 break;
129 free(dpy);
131 _eglGlobal.DisplayList = NULL;
136 * Find the display corresponding to the specified native display, or create a
137 * new one.
139 _EGLDisplay *
140 _eglFindDisplay(_EGLPlatformType plat, void *plat_dpy)
142 _EGLDisplay *dpy;
144 if (plat == _EGL_INVALID_PLATFORM)
145 return NULL;
147 _eglLockMutex(_eglGlobal.Mutex);
149 /* search the display list first */
150 dpy = _eglGlobal.DisplayList;
151 while (dpy) {
152 #if !defined(_EGL_OS_AROS)
153 /* It seems the _eglGlobal.DisplayList should unique per opener (not per task).
154 This is not true on AROS (_eglGlobal.DisplayList is global for all
155 openers). The workaround is to always create a new display object. This
156 might fail with multithreaded applications as they might expect to have
157 the same display object returned by GetDisplay */
158 if (dpy->Platform == plat && dpy->PlatformDisplay == plat_dpy)
159 break;
160 #endif
161 dpy = dpy->Next;
164 /* create a new display */
165 if (!dpy) {
166 dpy = (_EGLDisplay *) calloc(1, sizeof(_EGLDisplay));
167 if (dpy) {
168 _eglInitMutex(&dpy->Mutex);
169 dpy->Platform = plat;
170 dpy->PlatformDisplay = plat_dpy;
172 /* add to the display list */
173 dpy->Next = _eglGlobal.DisplayList;
174 _eglGlobal.DisplayList = dpy;
178 _eglUnlockMutex(_eglGlobal.Mutex);
180 return dpy;
185 * Destroy the contexts and surfaces that are linked to the display.
187 void
188 _eglReleaseDisplayResources(_EGLDriver *drv, _EGLDisplay *display)
190 _EGLResource *list;
192 list = display->ResourceLists[_EGL_RESOURCE_CONTEXT];
193 while (list) {
194 _EGLContext *ctx = (_EGLContext *) list;
195 list = list->Next;
197 _eglUnlinkContext(ctx);
198 drv->API.DestroyContext(drv, display, ctx);
200 assert(!display->ResourceLists[_EGL_RESOURCE_CONTEXT]);
202 list = display->ResourceLists[_EGL_RESOURCE_SURFACE];
203 while (list) {
204 _EGLSurface *surf = (_EGLSurface *) list;
205 list = list->Next;
207 _eglUnlinkSurface(surf);
208 drv->API.DestroySurface(drv, display, surf);
210 assert(!display->ResourceLists[_EGL_RESOURCE_SURFACE]);
215 * Free all the data hanging of an _EGLDisplay object, but not
216 * the object itself.
218 void
219 _eglCleanupDisplay(_EGLDisplay *disp)
221 if (disp->Configs) {
222 _eglDestroyArray(disp->Configs, free);
223 disp->Configs = NULL;
226 /* XXX incomplete */
231 * Return EGL_TRUE if the given handle is a valid handle to a display.
233 EGLBoolean
234 _eglCheckDisplayHandle(EGLDisplay dpy)
236 _EGLDisplay *cur;
238 _eglLockMutex(_eglGlobal.Mutex);
239 cur = _eglGlobal.DisplayList;
240 while (cur) {
241 if (cur == (_EGLDisplay *) dpy)
242 break;
243 cur = cur->Next;
245 _eglUnlockMutex(_eglGlobal.Mutex);
246 return (cur != NULL);
251 * Return EGL_TRUE if the given resource is valid. That is, the display does
252 * own the resource.
254 EGLBoolean
255 _eglCheckResource(void *res, _EGLResourceType type, _EGLDisplay *dpy)
257 _EGLResource *list = dpy->ResourceLists[type];
259 if (!res)
260 return EGL_FALSE;
262 while (list) {
263 if (res == (void *) list) {
264 assert(list->Display == dpy);
265 break;
267 list = list->Next;
270 return (list != NULL);
275 * Initialize a display resource.
277 void
278 _eglInitResource(_EGLResource *res, EGLint size, _EGLDisplay *dpy)
280 memset(res, 0, size);
281 res->Display = dpy;
282 res->RefCount = 1;
287 * Increment reference count for the resource.
289 void
290 _eglGetResource(_EGLResource *res)
292 assert(res && res->RefCount > 0);
293 /* hopefully a resource is always manipulated with its display locked */
294 res->RefCount++;
299 * Decrement reference count for the resource.
301 EGLBoolean
302 _eglPutResource(_EGLResource *res)
304 assert(res && res->RefCount > 0);
305 res->RefCount--;
306 return (!res->RefCount);
311 * Link a resource to its display.
313 void
314 _eglLinkResource(_EGLResource *res, _EGLResourceType type)
316 assert(res->Display);
318 res->IsLinked = EGL_TRUE;
319 res->Next = res->Display->ResourceLists[type];
320 res->Display->ResourceLists[type] = res;
321 _eglGetResource(res);
326 * Unlink a linked resource from its display.
328 void
329 _eglUnlinkResource(_EGLResource *res, _EGLResourceType type)
331 _EGLResource *prev;
333 prev = res->Display->ResourceLists[type];
334 if (prev != res) {
335 while (prev) {
336 if (prev->Next == res)
337 break;
338 prev = prev->Next;
340 assert(prev);
341 prev->Next = res->Next;
343 else {
344 res->Display->ResourceLists[type] = res->Next;
347 res->Next = NULL;
348 res->IsLinked = EGL_FALSE;
349 _eglPutResource(res);
351 /* We always unlink before destroy. The driver still owns a reference */
352 assert(res->RefCount);