egl: Store configs in a dynamic array.
[mesa.git] / src / egl / main / egldisplay.c
1 /**
2 * Functions related to EGLDisplay.
3 */
4
5 #include <assert.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include "eglcontext.h"
9 #include "eglsurface.h"
10 #include "egldisplay.h"
11 #include "egldriver.h"
12 #include "eglglobals.h"
13 #include "eglmutex.h"
14 #include "egllog.h"
15
16
17 /**
18 * Return the native platform by parsing EGL_PLATFORM.
19 */
20 static _EGLPlatformType
21 _eglGetNativePlatformFromEnv(void)
22 {
23 /* map --with-egl-platforms names to platform types */
24 static const struct {
25 _EGLPlatformType platform;
26 const char *name;
27 } egl_platforms[_EGL_NUM_PLATFORMS] = {
28 { _EGL_PLATFORM_WINDOWS, "gdi" },
29 { _EGL_PLATFORM_X11, "x11" },
30 { _EGL_PLATFORM_DRM, "kms" },
31 { _EGL_PLATFORM_FBDEV, "fbdev" }
32 };
33 _EGLPlatformType plat = _EGL_INVALID_PLATFORM;
34 const char *plat_name;
35 EGLint i;
36
37 plat_name = getenv("EGL_PLATFORM");
38 /* try deprecated env variable */
39 if (!plat_name || !plat_name[0])
40 plat_name = getenv("EGL_DISPLAY");
41 if (!plat_name || !plat_name[0])
42 return _EGL_INVALID_PLATFORM;
43
44 for (i = 0; i < _EGL_NUM_PLATFORMS; i++) {
45 if (strcmp(egl_platforms[i].name, plat_name) == 0) {
46 plat = egl_platforms[i].platform;
47 break;
48 }
49 }
50
51 return plat;
52 }
53
54
55 /**
56 * Return the native platform. It is the platform of the EGL native types.
57 */
58 _EGLPlatformType
59 _eglGetNativePlatform(void)
60 {
61 static _EGLPlatformType native_platform = _EGL_INVALID_PLATFORM;
62
63 if (native_platform == _EGL_INVALID_PLATFORM) {
64 native_platform = _eglGetNativePlatformFromEnv();
65 if (native_platform == _EGL_INVALID_PLATFORM)
66 native_platform = _EGL_NATIVE_PLATFORM;
67 }
68
69 return native_platform;
70 }
71
72
73 /**
74 * Finish display management.
75 */
76 void
77 _eglFiniDisplay(void)
78 {
79 _EGLDisplay *dpyList, *dpy;
80
81 /* atexit function is called with global mutex locked */
82 dpyList = _eglGlobal.DisplayList;
83 while (dpyList) {
84 EGLint i;
85
86 /* pop list head */
87 dpy = dpyList;
88 dpyList = dpyList->Next;
89
90 for (i = 0; i < _EGL_NUM_RESOURCES; i++) {
91 if (dpy->ResourceLists[i]) {
92 _eglLog(_EGL_DEBUG, "Display %p is destroyed with resources", dpy);
93 break;
94 }
95 }
96
97 free(dpy);
98 }
99 _eglGlobal.DisplayList = NULL;
100 }
101
102
103 /**
104 * Find the display corresponding to the specified native display, or create a
105 * new one.
106 */
107 _EGLDisplay *
108 _eglFindDisplay(_EGLPlatformType plat, void *plat_dpy)
109 {
110 _EGLDisplay *dpy;
111
112 if (plat == _EGL_INVALID_PLATFORM)
113 return NULL;
114
115 _eglLockMutex(_eglGlobal.Mutex);
116
117 /* search the display list first */
118 dpy = _eglGlobal.DisplayList;
119 while (dpy) {
120 if (dpy->Platform == plat && dpy->PlatformDisplay == plat_dpy)
121 break;
122 dpy = dpy->Next;
123 }
124
125 /* create a new display */
126 if (!dpy) {
127 dpy = (_EGLDisplay *) calloc(1, sizeof(_EGLDisplay));
128 if (dpy) {
129 _eglInitMutex(&dpy->Mutex);
130 dpy->Platform = plat;
131 dpy->PlatformDisplay = plat_dpy;
132
133 /* add to the display list */
134 dpy->Next = _eglGlobal.DisplayList;
135 _eglGlobal.DisplayList = dpy;
136 }
137 }
138
139 _eglUnlockMutex(_eglGlobal.Mutex);
140
141 return dpy;
142 }
143
144
145 /**
146 * Destroy the contexts and surfaces that are linked to the display.
147 */
148 void
149 _eglReleaseDisplayResources(_EGLDriver *drv, _EGLDisplay *display)
150 {
151 _EGLResource *list;
152
153 list = display->ResourceLists[_EGL_RESOURCE_CONTEXT];
154 while (list) {
155 _EGLContext *ctx = (_EGLContext *) list;
156 list = list->Next;
157
158 _eglUnlinkContext(ctx);
159 drv->API.DestroyContext(drv, display, ctx);
160 }
161 assert(!display->ResourceLists[_EGL_RESOURCE_CONTEXT]);
162
163 list = display->ResourceLists[_EGL_RESOURCE_SURFACE];
164 while (list) {
165 _EGLSurface *surf = (_EGLSurface *) list;
166 list = list->Next;
167
168 _eglUnlinkSurface(surf);
169 drv->API.DestroySurface(drv, display, surf);
170 }
171 assert(!display->ResourceLists[_EGL_RESOURCE_SURFACE]);
172 }
173
174
175 /**
176 * Free all the data hanging of an _EGLDisplay object, but not
177 * the object itself.
178 */
179 void
180 _eglCleanupDisplay(_EGLDisplay *disp)
181 {
182 if (disp->Configs) {
183 _eglDestroyArray(disp->Configs, free);
184 disp->Configs = NULL;
185 }
186
187 /* XXX incomplete */
188 }
189
190
191 /**
192 * Return EGL_TRUE if the given handle is a valid handle to a display.
193 */
194 EGLBoolean
195 _eglCheckDisplayHandle(EGLDisplay dpy)
196 {
197 _EGLDisplay *cur;
198
199 _eglLockMutex(_eglGlobal.Mutex);
200 cur = _eglGlobal.DisplayList;
201 while (cur) {
202 if (cur == (_EGLDisplay *) dpy)
203 break;
204 cur = cur->Next;
205 }
206 _eglUnlockMutex(_eglGlobal.Mutex);
207 return (cur != NULL);
208 }
209
210
211 /**
212 * Return EGL_TRUE if the given resource is valid. That is, the display does
213 * own the resource.
214 */
215 EGLBoolean
216 _eglCheckResource(void *res, _EGLResourceType type, _EGLDisplay *dpy)
217 {
218 _EGLResource *list = dpy->ResourceLists[type];
219
220 if (!res)
221 return EGL_FALSE;
222
223 while (list) {
224 if (res == (void *) list) {
225 assert(list->Display == dpy);
226 break;
227 }
228 list = list->Next;
229 }
230
231 return (list != NULL);
232 }
233
234
235 /**
236 * Link a resource to a display.
237 */
238 void
239 _eglLinkResource(_EGLResource *res, _EGLResourceType type, _EGLDisplay *dpy)
240 {
241 assert(!res->Display || res->Display == dpy);
242
243 res->Display = dpy;
244 res->IsLinked = EGL_TRUE;
245 res->Next = dpy->ResourceLists[type];
246 dpy->ResourceLists[type] = res;
247 }
248
249
250 /**
251 * Unlink a linked resource from its display.
252 */
253 void
254 _eglUnlinkResource(_EGLResource *res, _EGLResourceType type)
255 {
256 _EGLResource *prev;
257
258 prev = res->Display->ResourceLists[type];
259 if (prev != res) {
260 while (prev) {
261 if (prev->Next == res)
262 break;
263 prev = prev->Next;
264 }
265 assert(prev);
266 prev->Next = res->Next;
267 }
268 else {
269 res->Display->ResourceLists[type] = res->Next;
270 }
271
272 res->Next = NULL;
273 /* do not reset res->Display */
274 res->IsLinked = EGL_FALSE;
275 }