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