egl: Log (debug) native platform type
[mesa.git] / src / egl / main / egldisplay.c
1 /**************************************************************************
2 *
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.
7 *
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:
15 *
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.
19 *
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.
27 *
28 **************************************************************************/
29
30
31 /**
32 * Functions related to EGLDisplay.
33 */
34
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"
45
46 /* Includes for _eglNativePlatformDetectNativeDisplay */
47 #ifdef HAVE_MINCORE
48 #include <unistd.h>
49 #include <sys/mman.h>
50 #endif
51 #ifdef HAVE_WAYLAND_PLATFORM
52 #include <wayland-client.h>
53 #endif
54 #ifdef HAVE_DRM_PLATFORM
55 #include <gbm.h>
56 #endif
57 #ifdef HAVE_FBDEV_PLATFORM
58 #include <stdint.h>
59 #include <sys/types.h>
60 #include <sys/stat.h>
61 #endif
62
63
64 /**
65 * Map --with-egl-platforms names to platform types.
66 */
67 static const struct {
68 _EGLPlatformType platform;
69 const char *name;
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 };
77
78
79 /**
80 * Return the native platform by parsing EGL_PLATFORM.
81 */
82 static _EGLPlatformType
83 _eglGetNativePlatformFromEnv(void)
84 {
85 _EGLPlatformType plat = _EGL_INVALID_PLATFORM;
86 const char *plat_name;
87 EGLint i;
88
89 plat_name = getenv("EGL_PLATFORM");
90 /* try deprecated env variable */
91 if (!plat_name || !plat_name[0])
92 plat_name = getenv("EGL_DISPLAY");
93 if (!plat_name || !plat_name[0])
94 return _EGL_INVALID_PLATFORM;
95
96 for (i = 0; i < _EGL_NUM_PLATFORMS; i++) {
97 if (strcmp(egl_platforms[i].name, plat_name) == 0) {
98 plat = egl_platforms[i].platform;
99 break;
100 }
101 }
102
103 return plat;
104 }
105
106
107 /**
108 * Perform validity checks on a generic pointer.
109 */
110 static EGLBoolean
111 _eglPointerIsDereferencable(void *p)
112 {
113 #ifdef HAVE_MINCORE
114 uintptr_t addr = (uintptr_t) p;
115 unsigned char valid = 0;
116 const long page_size = getpagesize();
117
118 if (p == NULL)
119 return EGL_FALSE;
120
121 /* align addr to page_size */
122 addr &= ~(page_size - 1);
123
124 if (mincore((void *) addr, page_size, &valid) < 0) {
125 _eglLog(_EGL_DEBUG, "mincore failed: %m");
126 return EGL_FALSE;
127 }
128
129 return (valid & 0x01) == 0x01;
130 #else
131 return p != NULL;
132 #endif
133 }
134
135
136 /**
137 * Try detecting native platform with the help of native display characteristcs.
138 */
139 static _EGLPlatformType
140 _eglNativePlatformDetectNativeDisplay(EGLNativeDisplayType nativeDisplay)
141 {
142 #ifdef HAVE_FBDEV_PLATFORM
143 struct stat buf;
144 #endif
145
146 if (nativeDisplay == EGL_DEFAULT_DISPLAY)
147 return _EGL_INVALID_PLATFORM;
148
149 #ifdef HAVE_FBDEV_PLATFORM
150 /* fbdev is the only platform that can be a file descriptor. */
151 if (fstat((intptr_t) nativeDisplay, &buf) == 0 && S_ISCHR(buf.st_mode))
152 return _EGL_PLATFORM_FBDEV;
153 #endif
154
155 if (_eglPointerIsDereferencable(nativeDisplay)) {
156 void *first_pointer = *(void **) nativeDisplay;
157
158 #ifdef HAVE_WAYLAND_PLATFORM
159 /* wl_display is a wl_proxy, which is a wl_object.
160 * wl_object's first element points to the interfacetype. */
161 if (first_pointer == &wl_display_interface)
162 return _EGL_PLATFORM_WAYLAND;
163 #endif
164
165 #ifdef HAVE_DRM_PLATFORM
166 /* gbm has a pointer to its constructor as first element. */
167 if (first_pointer == gbm_create_device)
168 return _EGL_PLATFORM_DRM;
169 #endif
170
171 #ifdef HAVE_X11_PLATFORM
172 /* If not matched to any other platform, fallback to x11. */
173 return _EGL_PLATFORM_X11;
174 #endif
175 }
176
177 return _EGL_INVALID_PLATFORM;
178 }
179
180
181 /**
182 * Return the native platform. It is the platform of the EGL native types.
183 */
184 _EGLPlatformType
185 _eglGetNativePlatform(EGLNativeDisplayType nativeDisplay)
186 {
187 static _EGLPlatformType native_platform = _EGL_INVALID_PLATFORM;
188 char *detection_method = NULL;
189
190 if (native_platform == _EGL_INVALID_PLATFORM) {
191 native_platform = _eglGetNativePlatformFromEnv();
192 detection_method = "environment overwrite";
193 if (native_platform == _EGL_INVALID_PLATFORM) {
194 native_platform = _eglNativePlatformDetectNativeDisplay(nativeDisplay);
195 detection_method = "autodetected";
196 if (native_platform == _EGL_INVALID_PLATFORM) {
197 native_platform = _EGL_NATIVE_PLATFORM;
198 detection_method = "build-time configuration";
199 }
200 }
201 }
202
203 if (detection_method != NULL)
204 _eglLog(_EGL_DEBUG, "Native platform type: %s (%s)",
205 egl_platforms[native_platform].name, detection_method);
206
207 return native_platform;
208 }
209
210
211 /**
212 * Finish display management.
213 */
214 void
215 _eglFiniDisplay(void)
216 {
217 _EGLDisplay *dpyList, *dpy;
218
219 /* atexit function is called with global mutex locked */
220 dpyList = _eglGlobal.DisplayList;
221 while (dpyList) {
222 EGLint i;
223
224 /* pop list head */
225 dpy = dpyList;
226 dpyList = dpyList->Next;
227
228 for (i = 0; i < _EGL_NUM_RESOURCES; i++) {
229 if (dpy->ResourceLists[i]) {
230 _eglLog(_EGL_DEBUG, "Display %p is destroyed with resources", dpy);
231 break;
232 }
233 }
234
235 free(dpy);
236 }
237 _eglGlobal.DisplayList = NULL;
238 }
239
240
241 /**
242 * Find the display corresponding to the specified native display, or create a
243 * new one.
244 */
245 _EGLDisplay *
246 _eglFindDisplay(_EGLPlatformType plat, void *plat_dpy)
247 {
248 _EGLDisplay *dpy;
249
250 if (plat == _EGL_INVALID_PLATFORM)
251 return NULL;
252
253 _eglLockMutex(_eglGlobal.Mutex);
254
255 /* search the display list first */
256 dpy = _eglGlobal.DisplayList;
257 while (dpy) {
258 if (dpy->Platform == plat && dpy->PlatformDisplay == plat_dpy)
259 break;
260 dpy = dpy->Next;
261 }
262
263 /* create a new display */
264 if (!dpy) {
265 dpy = (_EGLDisplay *) calloc(1, sizeof(_EGLDisplay));
266 if (dpy) {
267 _eglInitMutex(&dpy->Mutex);
268 dpy->Platform = plat;
269 dpy->PlatformDisplay = plat_dpy;
270
271 /* add to the display list */
272 dpy->Next = _eglGlobal.DisplayList;
273 _eglGlobal.DisplayList = dpy;
274 }
275 }
276
277 _eglUnlockMutex(_eglGlobal.Mutex);
278
279 return dpy;
280 }
281
282
283 /**
284 * Destroy the contexts and surfaces that are linked to the display.
285 */
286 void
287 _eglReleaseDisplayResources(_EGLDriver *drv, _EGLDisplay *display)
288 {
289 _EGLResource *list;
290
291 list = display->ResourceLists[_EGL_RESOURCE_CONTEXT];
292 while (list) {
293 _EGLContext *ctx = (_EGLContext *) list;
294 list = list->Next;
295
296 _eglUnlinkContext(ctx);
297 drv->API.DestroyContext(drv, display, ctx);
298 }
299 assert(!display->ResourceLists[_EGL_RESOURCE_CONTEXT]);
300
301 list = display->ResourceLists[_EGL_RESOURCE_SURFACE];
302 while (list) {
303 _EGLSurface *surf = (_EGLSurface *) list;
304 list = list->Next;
305
306 _eglUnlinkSurface(surf);
307 drv->API.DestroySurface(drv, display, surf);
308 }
309 assert(!display->ResourceLists[_EGL_RESOURCE_SURFACE]);
310 }
311
312
313 /**
314 * Free all the data hanging of an _EGLDisplay object, but not
315 * the object itself.
316 */
317 void
318 _eglCleanupDisplay(_EGLDisplay *disp)
319 {
320 if (disp->Configs) {
321 _eglDestroyArray(disp->Configs, free);
322 disp->Configs = NULL;
323 }
324
325 /* XXX incomplete */
326 }
327
328
329 /**
330 * Return EGL_TRUE if the given handle is a valid handle to a display.
331 */
332 EGLBoolean
333 _eglCheckDisplayHandle(EGLDisplay dpy)
334 {
335 _EGLDisplay *cur;
336
337 _eglLockMutex(_eglGlobal.Mutex);
338 cur = _eglGlobal.DisplayList;
339 while (cur) {
340 if (cur == (_EGLDisplay *) dpy)
341 break;
342 cur = cur->Next;
343 }
344 _eglUnlockMutex(_eglGlobal.Mutex);
345 return (cur != NULL);
346 }
347
348
349 /**
350 * Return EGL_TRUE if the given resource is valid. That is, the display does
351 * own the resource.
352 */
353 EGLBoolean
354 _eglCheckResource(void *res, _EGLResourceType type, _EGLDisplay *dpy)
355 {
356 _EGLResource *list = dpy->ResourceLists[type];
357
358 if (!res)
359 return EGL_FALSE;
360
361 while (list) {
362 if (res == (void *) list) {
363 assert(list->Display == dpy);
364 break;
365 }
366 list = list->Next;
367 }
368
369 return (list != NULL);
370 }
371
372
373 /**
374 * Initialize a display resource.
375 */
376 void
377 _eglInitResource(_EGLResource *res, EGLint size, _EGLDisplay *dpy)
378 {
379 memset(res, 0, size);
380 res->Display = dpy;
381 res->RefCount = 1;
382 }
383
384
385 /**
386 * Increment reference count for the resource.
387 */
388 void
389 _eglGetResource(_EGLResource *res)
390 {
391 assert(res && res->RefCount > 0);
392 /* hopefully a resource is always manipulated with its display locked */
393 res->RefCount++;
394 }
395
396
397 /**
398 * Decrement reference count for the resource.
399 */
400 EGLBoolean
401 _eglPutResource(_EGLResource *res)
402 {
403 assert(res && res->RefCount > 0);
404 res->RefCount--;
405 return (!res->RefCount);
406 }
407
408
409 /**
410 * Link a resource to its display.
411 */
412 void
413 _eglLinkResource(_EGLResource *res, _EGLResourceType type)
414 {
415 assert(res->Display);
416
417 res->IsLinked = EGL_TRUE;
418 res->Next = res->Display->ResourceLists[type];
419 res->Display->ResourceLists[type] = res;
420 _eglGetResource(res);
421 }
422
423
424 /**
425 * Unlink a linked resource from its display.
426 */
427 void
428 _eglUnlinkResource(_EGLResource *res, _EGLResourceType type)
429 {
430 _EGLResource *prev;
431
432 prev = res->Display->ResourceLists[type];
433 if (prev != res) {
434 while (prev) {
435 if (prev->Next == res)
436 break;
437 prev = prev->Next;
438 }
439 assert(prev);
440 prev->Next = res->Next;
441 }
442 else {
443 res->Display->ResourceLists[type] = res->Next;
444 }
445
446 res->Next = NULL;
447 res->IsLinked = EGL_FALSE;
448 _eglPutResource(res);
449
450 /* We always unlink before destroy. The driver still owns a reference */
451 assert(res->RefCount);
452 }