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