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