9a2ac48e8bc16d8b63371486a610435d65c3a9b5
[mesa.git] / src / egl / main / egldisplay.c
1 /**************************************************************************
2 *
3 * Copyright 2008 VMware, Inc.
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 "c11/threads.h"
39 #include "util/u_atomic.h"
40
41 #include "eglcontext.h"
42 #include "eglcurrent.h"
43 #include "eglsurface.h"
44 #include "egldisplay.h"
45 #include "egldriver.h"
46 #include "eglglobals.h"
47 #include "egllog.h"
48 #include "eglimage.h"
49 #include "eglsync.h"
50
51 /* Includes for _eglNativePlatformDetectNativeDisplay */
52 #ifdef HAVE_WAYLAND_PLATFORM
53 #include <wayland-client.h>
54 #endif
55 #ifdef HAVE_DRM_PLATFORM
56 #include <gbm.h>
57 #endif
58
59
60 /**
61 * Map build-system platform names to platform types.
62 */
63 static const struct {
64 _EGLPlatformType platform;
65 const char *name;
66 } egl_platforms[_EGL_NUM_PLATFORMS] = {
67 { _EGL_PLATFORM_X11, "x11" },
68 { _EGL_PLATFORM_WAYLAND, "wayland" },
69 { _EGL_PLATFORM_DRM, "drm" },
70 { _EGL_PLATFORM_ANDROID, "android" },
71 { _EGL_PLATFORM_HAIKU, "haiku" },
72 { _EGL_PLATFORM_SURFACELESS, "surfaceless" },
73 };
74
75
76 /**
77 * Return the native platform by parsing EGL_PLATFORM.
78 */
79 static _EGLPlatformType
80 _eglGetNativePlatformFromEnv(void)
81 {
82 _EGLPlatformType plat = _EGL_INVALID_PLATFORM;
83 const char *plat_name;
84 EGLint i;
85
86 plat_name = getenv("EGL_PLATFORM");
87 /* try deprecated env variable */
88 if (!plat_name || !plat_name[0])
89 plat_name = getenv("EGL_DISPLAY");
90 if (!plat_name || !plat_name[0])
91 return _EGL_INVALID_PLATFORM;
92
93 for (i = 0; i < _EGL_NUM_PLATFORMS; i++) {
94 if (strcmp(egl_platforms[i].name, plat_name) == 0) {
95 plat = egl_platforms[i].platform;
96 break;
97 }
98 }
99
100 return plat;
101 }
102
103
104 /**
105 * Try detecting native platform with the help of native display characteristcs.
106 */
107 static _EGLPlatformType
108 _eglNativePlatformDetectNativeDisplay(void *nativeDisplay)
109 {
110 if (nativeDisplay == EGL_DEFAULT_DISPLAY)
111 return _EGL_INVALID_PLATFORM;
112
113 if (_eglPointerIsDereferencable(nativeDisplay)) {
114 void *first_pointer = *(void **) nativeDisplay;
115
116 (void) first_pointer; /* silence unused var warning */
117
118 #ifdef HAVE_WAYLAND_PLATFORM
119 /* wl_display is a wl_proxy, which is a wl_object.
120 * wl_object's first element points to the interfacetype. */
121 if (first_pointer == &wl_display_interface)
122 return _EGL_PLATFORM_WAYLAND;
123 #endif
124
125 #ifdef HAVE_DRM_PLATFORM
126 /* gbm has a pointer to its constructor as first element. */
127 if (first_pointer == gbm_create_device)
128 return _EGL_PLATFORM_DRM;
129 #endif
130
131 #ifdef HAVE_X11_PLATFORM
132 /* If not matched to any other platform, fallback to x11. */
133 return _EGL_PLATFORM_X11;
134 #endif
135
136 #ifdef HAVE_HAIKU_PLATFORM
137 return _EGL_PLATFORM_HAIKU;
138 #endif
139 }
140
141 return _EGL_INVALID_PLATFORM;
142 }
143
144
145 /**
146 * Return the native platform. It is the platform of the EGL native types.
147 */
148 _EGLPlatformType
149 _eglGetNativePlatform(void *nativeDisplay)
150 {
151 static _EGLPlatformType native_platform = _EGL_INVALID_PLATFORM;
152 _EGLPlatformType detected_platform = native_platform;
153
154 if (detected_platform == _EGL_INVALID_PLATFORM) {
155 const char *detection_method;
156
157 detected_platform = _eglGetNativePlatformFromEnv();
158 detection_method = "environment overwrite";
159
160 if (detected_platform == _EGL_INVALID_PLATFORM) {
161 detected_platform = _eglNativePlatformDetectNativeDisplay(nativeDisplay);
162 detection_method = "autodetected";
163 }
164
165 if (detected_platform == _EGL_INVALID_PLATFORM) {
166 detected_platform = _EGL_NATIVE_PLATFORM;
167 detection_method = "build-time configuration";
168 }
169
170 _eglLog(_EGL_DEBUG, "Native platform type: %s (%s)",
171 egl_platforms[detected_platform].name, detection_method);
172
173 p_atomic_cmpxchg(&native_platform, _EGL_INVALID_PLATFORM,
174 detected_platform);
175 }
176
177 return native_platform;
178 }
179
180
181 /**
182 * Finish display management.
183 */
184 void
185 _eglFiniDisplay(void)
186 {
187 _EGLDisplay *dispList, *disp;
188
189 /* atexit function is called with global mutex locked */
190 dispList = _eglGlobal.DisplayList;
191 while (dispList) {
192 EGLint i;
193
194 /* pop list head */
195 disp = dispList;
196 dispList = dispList->Next;
197
198 for (i = 0; i < _EGL_NUM_RESOURCES; i++) {
199 if (disp->ResourceLists[i]) {
200 _eglLog(_EGL_DEBUG, "Display %p is destroyed with resources", disp);
201 break;
202 }
203 }
204
205 free(disp->Options.Attribs);
206 free(disp);
207 }
208 _eglGlobal.DisplayList = NULL;
209 }
210
211 static EGLBoolean
212 _eglSameAttribs(const EGLAttrib *a, const EGLAttrib *b)
213 {
214 size_t na = _eglNumAttribs(a);
215 size_t nb = _eglNumAttribs(b);
216
217 /* different numbers of attributes must be different */
218 if (na != nb)
219 return EGL_FALSE;
220
221 /* both lists NULL are the same */
222 if (!a && !b)
223 return EGL_TRUE;
224
225 /* otherwise, compare the lists */
226 return memcmp(a, b, na) == 0 ? EGL_TRUE : EGL_FALSE;
227 }
228
229 /**
230 * Find the display corresponding to the specified native display, or create a
231 * new one. EGL 1.5 says:
232 *
233 * Multiple calls made to eglGetPlatformDisplay with the same parameters
234 * will return the same EGLDisplay handle.
235 *
236 * We read this extremely strictly, and treat a call with NULL attribs as
237 * different from a call with attribs only equal to { EGL_NONE }. Similarly
238 * we do not sort the attribute list, so even if all attribute _values_ are
239 * identical, different attribute orders will be considered different
240 * parameters.
241 */
242 _EGLDisplay *
243 _eglFindDisplay(_EGLPlatformType plat, void *plat_dpy,
244 const EGLAttrib *attrib_list)
245 {
246 _EGLDisplay *disp;
247 size_t num_attribs;
248
249 if (plat == _EGL_INVALID_PLATFORM)
250 return NULL;
251
252 mtx_lock(_eglGlobal.Mutex);
253
254 /* search the display list first */
255 disp = _eglGlobal.DisplayList;
256 while (disp) {
257 if (disp->Platform == plat && disp->PlatformDisplay == plat_dpy &&
258 _eglSameAttribs(disp->Options.Attribs, attrib_list))
259 break;
260 disp = disp->Next;
261 }
262
263 /* create a new display */
264 if (!disp) {
265 disp = calloc(1, sizeof(_EGLDisplay));
266 if (disp) {
267 mtx_init(&disp->Mutex, mtx_plain);
268 disp->Platform = plat;
269 disp->PlatformDisplay = plat_dpy;
270 num_attribs = _eglNumAttribs(attrib_list);
271 if (num_attribs) {
272 disp->Options.Attribs = calloc(num_attribs, sizeof(EGLAttrib));
273 if (!disp->Options.Attribs) {
274 free(disp);
275 disp = NULL;
276 goto out;
277 }
278 memcpy(disp->Options.Attribs, attrib_list,
279 num_attribs * sizeof(EGLAttrib));
280 }
281 /* add to the display list */
282 disp->Next = _eglGlobal.DisplayList;
283 _eglGlobal.DisplayList = disp;
284 }
285 }
286
287 out:
288 mtx_unlock(_eglGlobal.Mutex);
289
290 return disp;
291 }
292
293
294 /**
295 * Destroy the contexts and surfaces that are linked to the display.
296 */
297 void
298 _eglReleaseDisplayResources(_EGLDriver *drv, _EGLDisplay *display)
299 {
300 _EGLResource *list;
301
302 list = display->ResourceLists[_EGL_RESOURCE_CONTEXT];
303 while (list) {
304 _EGLContext *ctx = (_EGLContext *) list;
305 list = list->Next;
306
307 _eglUnlinkContext(ctx);
308 drv->API.DestroyContext(drv, display, ctx);
309 }
310 assert(!display->ResourceLists[_EGL_RESOURCE_CONTEXT]);
311
312 list = display->ResourceLists[_EGL_RESOURCE_SURFACE];
313 while (list) {
314 _EGLSurface *surf = (_EGLSurface *) list;
315 list = list->Next;
316
317 _eglUnlinkSurface(surf);
318 drv->API.DestroySurface(drv, display, surf);
319 }
320 assert(!display->ResourceLists[_EGL_RESOURCE_SURFACE]);
321
322 list = display->ResourceLists[_EGL_RESOURCE_IMAGE];
323 while (list) {
324 _EGLImage *image = (_EGLImage *) list;
325 list = list->Next;
326
327 _eglUnlinkImage(image);
328 drv->API.DestroyImageKHR(drv, display, image);
329 }
330 assert(!display->ResourceLists[_EGL_RESOURCE_IMAGE]);
331
332 list = display->ResourceLists[_EGL_RESOURCE_SYNC];
333 while (list) {
334 _EGLSync *sync = (_EGLSync *) list;
335 list = list->Next;
336
337 _eglUnlinkSync(sync);
338 drv->API.DestroySyncKHR(drv, display, sync);
339 }
340 assert(!display->ResourceLists[_EGL_RESOURCE_SYNC]);
341 }
342
343
344 /**
345 * Free all the data hanging of an _EGLDisplay object, but not
346 * the object itself.
347 */
348 void
349 _eglCleanupDisplay(_EGLDisplay *disp)
350 {
351 if (disp->Configs) {
352 _eglDestroyArray(disp->Configs, free);
353 disp->Configs = NULL;
354 }
355
356 /* XXX incomplete */
357 }
358
359
360 /**
361 * Return EGL_TRUE if the given handle is a valid handle to a display.
362 */
363 EGLBoolean
364 _eglCheckDisplayHandle(EGLDisplay dpy)
365 {
366 _EGLDisplay *cur;
367
368 mtx_lock(_eglGlobal.Mutex);
369 cur = _eglGlobal.DisplayList;
370 while (cur) {
371 if (cur == (_EGLDisplay *) dpy)
372 break;
373 cur = cur->Next;
374 }
375 mtx_unlock(_eglGlobal.Mutex);
376 return (cur != NULL);
377 }
378
379
380 /**
381 * Return EGL_TRUE if the given resource is valid. That is, the display does
382 * own the resource.
383 */
384 EGLBoolean
385 _eglCheckResource(void *res, _EGLResourceType type, _EGLDisplay *disp)
386 {
387 _EGLResource *list = disp->ResourceLists[type];
388
389 if (!res)
390 return EGL_FALSE;
391
392 while (list) {
393 if (res == (void *) list) {
394 assert(list->Display == disp);
395 break;
396 }
397 list = list->Next;
398 }
399
400 return (list != NULL);
401 }
402
403
404 /**
405 * Initialize a display resource. The size of the subclass object is
406 * specified.
407 *
408 * This is supposed to be called from the initializers of subclasses, such as
409 * _eglInitContext or _eglInitSurface.
410 */
411 void
412 _eglInitResource(_EGLResource *res, EGLint size, _EGLDisplay *disp)
413 {
414 memset(res, 0, size);
415 res->Display = disp;
416 res->RefCount = 1;
417 }
418
419
420 /**
421 * Increment reference count for the resource.
422 */
423 void
424 _eglGetResource(_EGLResource *res)
425 {
426 assert(res && res->RefCount > 0);
427 /* hopefully a resource is always manipulated with its display locked */
428 res->RefCount++;
429 }
430
431
432 /**
433 * Decrement reference count for the resource.
434 */
435 EGLBoolean
436 _eglPutResource(_EGLResource *res)
437 {
438 assert(res && res->RefCount > 0);
439 res->RefCount--;
440 return (!res->RefCount);
441 }
442
443
444 /**
445 * Link a resource to its display.
446 */
447 void
448 _eglLinkResource(_EGLResource *res, _EGLResourceType type)
449 {
450 assert(res->Display);
451
452 res->IsLinked = EGL_TRUE;
453 res->Next = res->Display->ResourceLists[type];
454 res->Display->ResourceLists[type] = res;
455 _eglGetResource(res);
456 }
457
458
459 /**
460 * Unlink a linked resource from its display.
461 */
462 void
463 _eglUnlinkResource(_EGLResource *res, _EGLResourceType type)
464 {
465 _EGLResource *prev;
466
467 prev = res->Display->ResourceLists[type];
468 if (prev != res) {
469 while (prev) {
470 if (prev->Next == res)
471 break;
472 prev = prev->Next;
473 }
474 assert(prev);
475 prev->Next = res->Next;
476 }
477 else {
478 res->Display->ResourceLists[type] = res->Next;
479 }
480
481 res->Next = NULL;
482 res->IsLinked = EGL_FALSE;
483 _eglPutResource(res);
484
485 /* We always unlink before destroy. The driver still owns a reference */
486 assert(res->RefCount);
487 }
488
489 #ifdef HAVE_X11_PLATFORM
490 static EGLBoolean
491 _eglParseX11DisplayAttribList(_EGLDisplay *display,
492 const EGLAttrib *attrib_list)
493 {
494 int i;
495
496 if (attrib_list == NULL) {
497 return EGL_TRUE;
498 }
499
500 for (i = 0; attrib_list[i] != EGL_NONE; i += 2) {
501 EGLAttrib attrib = attrib_list[i];
502 EGLAttrib value = attrib_list[i + 1];
503
504 /* EGL_EXT_platform_x11 recognizes exactly one attribute,
505 * EGL_PLATFORM_X11_SCREEN_EXT, which is optional.
506 */
507 if (attrib != EGL_PLATFORM_X11_SCREEN_EXT)
508 return _eglError(EGL_BAD_ATTRIBUTE, "eglGetPlatformDisplay");
509
510 display->Options.Platform = (void *)(uintptr_t)value;
511 }
512
513 return EGL_TRUE;
514 }
515
516 _EGLDisplay*
517 _eglGetX11Display(Display *native_display,
518 const EGLAttrib *attrib_list)
519 {
520 _EGLDisplay *display = _eglFindDisplay(_EGL_PLATFORM_X11,
521 native_display,
522 attrib_list);
523
524 if (!display) {
525 _eglError(EGL_BAD_ALLOC, "eglGetPlatformDisplay");
526 return NULL;
527 }
528
529 if (!_eglParseX11DisplayAttribList(display, attrib_list)) {
530 return NULL;
531 }
532
533 return display;
534 }
535 #endif /* HAVE_X11_PLATFORM */
536
537 #ifdef HAVE_DRM_PLATFORM
538 _EGLDisplay*
539 _eglGetGbmDisplay(struct gbm_device *native_display,
540 const EGLAttrib *attrib_list)
541 {
542 /* EGL_MESA_platform_gbm recognizes no attributes. */
543 if (attrib_list != NULL && attrib_list[0] != EGL_NONE) {
544 _eglError(EGL_BAD_ATTRIBUTE, "eglGetPlatformDisplay");
545 return NULL;
546 }
547
548 return _eglFindDisplay(_EGL_PLATFORM_DRM, native_display, attrib_list);
549 }
550 #endif /* HAVE_DRM_PLATFORM */
551
552 #ifdef HAVE_WAYLAND_PLATFORM
553 _EGLDisplay*
554 _eglGetWaylandDisplay(struct wl_display *native_display,
555 const EGLAttrib *attrib_list)
556 {
557 /* EGL_EXT_platform_wayland recognizes no attributes. */
558 if (attrib_list != NULL && attrib_list[0] != EGL_NONE) {
559 _eglError(EGL_BAD_ATTRIBUTE, "eglGetPlatformDisplay");
560 return NULL;
561 }
562
563 return _eglFindDisplay(_EGL_PLATFORM_WAYLAND, native_display, attrib_list);
564 }
565 #endif /* HAVE_WAYLAND_PLATFORM */
566
567 #ifdef HAVE_SURFACELESS_PLATFORM
568 _EGLDisplay*
569 _eglGetSurfacelessDisplay(void *native_display,
570 const EGLAttrib *attrib_list)
571 {
572 /* This platform has no native display. */
573 if (native_display != NULL) {
574 _eglError(EGL_BAD_PARAMETER, "eglGetPlatformDisplay");
575 return NULL;
576 }
577
578 /* This platform recognizes no display attributes. */
579 if (attrib_list != NULL && attrib_list[0] != EGL_NONE) {
580 _eglError(EGL_BAD_ATTRIBUTE, "eglGetPlatformDisplay");
581 return NULL;
582 }
583
584 return _eglFindDisplay(_EGL_PLATFORM_SURFACELESS, native_display,
585 attrib_list);
586 }
587 #endif /* HAVE_SURFACELESS_PLATFORM */