st/egl: One driver per hardware.
[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 EGLint i;
183
184 if (disp->Configs) {
185 for (i = 0; i < disp->NumConfigs; i++)
186 free(disp->Configs[i]);
187 free(disp->Configs);
188 disp->Configs = NULL;
189 disp->NumConfigs = 0;
190 disp->MaxConfigs = 0;
191 }
192
193 /* XXX incomplete */
194 }
195
196
197 /**
198 * Return EGL_TRUE if the given handle is a valid handle to a display.
199 */
200 EGLBoolean
201 _eglCheckDisplayHandle(EGLDisplay dpy)
202 {
203 _EGLDisplay *cur;
204
205 _eglLockMutex(_eglGlobal.Mutex);
206 cur = _eglGlobal.DisplayList;
207 while (cur) {
208 if (cur == (_EGLDisplay *) dpy)
209 break;
210 cur = cur->Next;
211 }
212 _eglUnlockMutex(_eglGlobal.Mutex);
213 return (cur != NULL);
214 }
215
216
217 /**
218 * Return EGL_TRUE if the given resource is valid. That is, the display does
219 * own the resource.
220 */
221 EGLBoolean
222 _eglCheckResource(void *res, _EGLResourceType type, _EGLDisplay *dpy)
223 {
224 _EGLResource *list = dpy->ResourceLists[type];
225
226 if (!res)
227 return EGL_FALSE;
228
229 while (list) {
230 if (res == (void *) list) {
231 assert(list->Display == dpy);
232 break;
233 }
234 list = list->Next;
235 }
236
237 return (list != NULL);
238 }
239
240
241 /**
242 * Link a resource to a display.
243 */
244 void
245 _eglLinkResource(_EGLResource *res, _EGLResourceType type, _EGLDisplay *dpy)
246 {
247 assert(!res->Display || res->Display == dpy);
248
249 res->Display = dpy;
250 res->IsLinked = EGL_TRUE;
251 res->Next = dpy->ResourceLists[type];
252 dpy->ResourceLists[type] = res;
253 }
254
255
256 /**
257 * Unlink a linked resource from its display.
258 */
259 void
260 _eglUnlinkResource(_EGLResource *res, _EGLResourceType type)
261 {
262 _EGLResource *prev;
263
264 prev = res->Display->ResourceLists[type];
265 if (prev != res) {
266 while (prev) {
267 if (prev->Next == res)
268 break;
269 prev = prev->Next;
270 }
271 assert(prev);
272 prev->Next = res->Next;
273 }
274 else {
275 res->Display->ResourceLists[type] = res->Next;
276 }
277
278 res->Next = NULL;
279 /* do not reset res->Display */
280 res->IsLinked = EGL_FALSE;
281 }