egl: inline fallback for swap_buffers_with_damage
[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(_EGLDriver *drv, _EGLDisplay *display)
297 {
298 _EGLResource *list;
299
300 list = display->ResourceLists[_EGL_RESOURCE_CONTEXT];
301 while (list) {
302 _EGLContext *ctx = (_EGLContext *) list;
303 list = list->Next;
304
305 _eglUnlinkContext(ctx);
306 drv->API.DestroyContext(drv, display, ctx);
307 }
308 assert(!display->ResourceLists[_EGL_RESOURCE_CONTEXT]);
309
310 list = display->ResourceLists[_EGL_RESOURCE_SURFACE];
311 while (list) {
312 _EGLSurface *surf = (_EGLSurface *) list;
313 list = list->Next;
314
315 _eglUnlinkSurface(surf);
316 drv->API.DestroySurface(drv, display, surf);
317 }
318 assert(!display->ResourceLists[_EGL_RESOURCE_SURFACE]);
319
320 list = display->ResourceLists[_EGL_RESOURCE_IMAGE];
321 while (list) {
322 _EGLImage *image = (_EGLImage *) list;
323 list = list->Next;
324
325 _eglUnlinkImage(image);
326 drv->API.DestroyImageKHR(drv, display, image);
327 }
328 assert(!display->ResourceLists[_EGL_RESOURCE_IMAGE]);
329
330 list = display->ResourceLists[_EGL_RESOURCE_SYNC];
331 while (list) {
332 _EGLSync *sync = (_EGLSync *) list;
333 list = list->Next;
334
335 _eglUnlinkSync(sync);
336 drv->API.DestroySyncKHR(drv, display, sync);
337 }
338 assert(!display->ResourceLists[_EGL_RESOURCE_SYNC]);
339 }
340
341
342 /**
343 * Free all the data hanging of an _EGLDisplay object, but not
344 * the object itself.
345 */
346 void
347 _eglCleanupDisplay(_EGLDisplay *disp)
348 {
349 if (disp->Configs) {
350 _eglDestroyArray(disp->Configs, free);
351 disp->Configs = NULL;
352 }
353
354 /* XXX incomplete */
355 }
356
357
358 /**
359 * Return EGL_TRUE if the given handle is a valid handle to a display.
360 */
361 EGLBoolean
362 _eglCheckDisplayHandle(EGLDisplay dpy)
363 {
364 _EGLDisplay *cur;
365
366 mtx_lock(_eglGlobal.Mutex);
367 cur = _eglGlobal.DisplayList;
368 while (cur) {
369 if (cur == (_EGLDisplay *) dpy)
370 break;
371 cur = cur->Next;
372 }
373 mtx_unlock(_eglGlobal.Mutex);
374 return (cur != NULL);
375 }
376
377
378 /**
379 * Return EGL_TRUE if the given resource is valid. That is, the display does
380 * own the resource.
381 */
382 EGLBoolean
383 _eglCheckResource(void *res, _EGLResourceType type, _EGLDisplay *disp)
384 {
385 _EGLResource *list = disp->ResourceLists[type];
386
387 if (!res)
388 return EGL_FALSE;
389
390 while (list) {
391 if (res == (void *) list) {
392 assert(list->Display == disp);
393 break;
394 }
395 list = list->Next;
396 }
397
398 return (list != NULL);
399 }
400
401
402 /**
403 * Initialize a display resource. The size of the subclass object is
404 * specified.
405 *
406 * This is supposed to be called from the initializers of subclasses, such as
407 * _eglInitContext or _eglInitSurface.
408 */
409 void
410 _eglInitResource(_EGLResource *res, EGLint size, _EGLDisplay *disp)
411 {
412 memset(res, 0, size);
413 res->Display = disp;
414 res->RefCount = 1;
415 }
416
417
418 /**
419 * Increment reference count for the resource.
420 */
421 void
422 _eglGetResource(_EGLResource *res)
423 {
424 assert(res && res->RefCount > 0);
425 /* hopefully a resource is always manipulated with its display locked */
426 res->RefCount++;
427 }
428
429
430 /**
431 * Decrement reference count for the resource.
432 */
433 EGLBoolean
434 _eglPutResource(_EGLResource *res)
435 {
436 assert(res && res->RefCount > 0);
437 res->RefCount--;
438 return (!res->RefCount);
439 }
440
441
442 /**
443 * Link a resource to its display.
444 */
445 void
446 _eglLinkResource(_EGLResource *res, _EGLResourceType type)
447 {
448 assert(res->Display);
449
450 res->IsLinked = EGL_TRUE;
451 res->Next = res->Display->ResourceLists[type];
452 res->Display->ResourceLists[type] = res;
453 _eglGetResource(res);
454 }
455
456
457 /**
458 * Unlink a linked resource from its display.
459 */
460 void
461 _eglUnlinkResource(_EGLResource *res, _EGLResourceType type)
462 {
463 _EGLResource *prev;
464
465 prev = res->Display->ResourceLists[type];
466 if (prev != res) {
467 while (prev) {
468 if (prev->Next == res)
469 break;
470 prev = prev->Next;
471 }
472 assert(prev);
473 prev->Next = res->Next;
474 }
475 else {
476 res->Display->ResourceLists[type] = res->Next;
477 }
478
479 res->Next = NULL;
480 res->IsLinked = EGL_FALSE;
481 _eglPutResource(res);
482
483 /* We always unlink before destroy. The driver still owns a reference */
484 assert(res->RefCount);
485 }
486
487 #ifdef HAVE_X11_PLATFORM
488 _EGLDisplay*
489 _eglGetX11Display(Display *native_display,
490 const EGLAttrib *attrib_list)
491 {
492 /* EGL_EXT_platform_x11 recognizes exactly one attribute,
493 * EGL_PLATFORM_X11_SCREEN_EXT, which is optional.
494 */
495 if (attrib_list != NULL) {
496 for (int i = 0; attrib_list[i] != EGL_NONE; i += 2) {
497 if (attrib_list[i] != EGL_PLATFORM_X11_SCREEN_EXT) {
498 _eglError(EGL_BAD_ATTRIBUTE, "eglGetPlatformDisplay");
499 return NULL;
500 }
501 }
502 }
503 return _eglFindDisplay(_EGL_PLATFORM_X11, native_display, attrib_list);
504 }
505 #endif /* HAVE_X11_PLATFORM */
506
507 #ifdef HAVE_DRM_PLATFORM
508 _EGLDisplay*
509 _eglGetGbmDisplay(struct gbm_device *native_display,
510 const EGLAttrib *attrib_list)
511 {
512 /* EGL_MESA_platform_gbm recognizes no attributes. */
513 if (attrib_list != NULL && attrib_list[0] != EGL_NONE) {
514 _eglError(EGL_BAD_ATTRIBUTE, "eglGetPlatformDisplay");
515 return NULL;
516 }
517
518 return _eglFindDisplay(_EGL_PLATFORM_DRM, native_display, attrib_list);
519 }
520 #endif /* HAVE_DRM_PLATFORM */
521
522 #ifdef HAVE_WAYLAND_PLATFORM
523 _EGLDisplay*
524 _eglGetWaylandDisplay(struct wl_display *native_display,
525 const EGLAttrib *attrib_list)
526 {
527 /* EGL_EXT_platform_wayland 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_WAYLAND, native_display, attrib_list);
534 }
535 #endif /* HAVE_WAYLAND_PLATFORM */
536
537 _EGLDisplay*
538 _eglGetSurfacelessDisplay(void *native_display,
539 const EGLAttrib *attrib_list)
540 {
541 /* This platform has no native display. */
542 if (native_display != NULL) {
543 _eglError(EGL_BAD_PARAMETER, "eglGetPlatformDisplay");
544 return NULL;
545 }
546
547 /* This platform recognizes no display attributes. */
548 if (attrib_list != NULL && attrib_list[0] != EGL_NONE) {
549 _eglError(EGL_BAD_ATTRIBUTE, "eglGetPlatformDisplay");
550 return NULL;
551 }
552
553 return _eglFindDisplay(_EGL_PLATFORM_SURFACELESS, native_display,
554 attrib_list);
555 }
556
557 #ifdef HAVE_ANDROID_PLATFORM
558 _EGLDisplay*
559 _eglGetAndroidDisplay(void *native_display,
560 const EGLAttrib *attrib_list)
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_ANDROID, native_display,
570 attrib_list);
571 }
572 #endif /* HAVE_ANDROID_PLATFORM */
573
574 _EGLDisplay*
575 _eglGetDeviceDisplay(void *native_display,
576 const EGLAttrib *attrib_list)
577 {
578 _EGLDevice *dev;
579 _EGLDisplay *display;
580 int fd = -1;
581
582 dev = _eglLookupDevice(native_display);
583 if (!dev) {
584 _eglError(EGL_BAD_PARAMETER, "eglGetPlatformDisplay");
585 return NULL;
586 }
587
588 if (attrib_list) {
589 for (int i = 0; attrib_list[i] != EGL_NONE; i += 2) {
590 EGLAttrib attrib = attrib_list[i];
591 EGLAttrib value = attrib_list[i + 1];
592
593 /* EGL_EXT_platform_device does not recognize any attributes,
594 * EGL_EXT_device_drm adds the optional EGL_DRM_MASTER_FD_EXT.
595 */
596
597 if (!_eglDeviceSupports(dev, _EGL_DEVICE_DRM) ||
598 attrib != EGL_DRM_MASTER_FD_EXT) {
599 _eglError(EGL_BAD_ATTRIBUTE, "eglGetPlatformDisplay");
600 return NULL;
601 }
602
603 fd = (int) value;
604 }
605 }
606
607 display = _eglFindDisplay(_EGL_PLATFORM_DEVICE, native_display, attrib_list);
608 if (!display) {
609 _eglError(EGL_BAD_ALLOC, "eglGetPlatformDisplay");
610 return NULL;
611 }
612
613 /* If the fd is explicitly provided and we did not dup() it yet, do so.
614 * The spec mandates that we do so, since we'll need it past the
615 * eglGetPlatformDispay call.
616 *
617 * The new fd is guaranteed to be 3 or greater.
618 */
619 if (fd != -1 && display->Options.fd == 0) {
620 display->Options.fd = os_dupfd_cloexec(fd);
621 if (display->Options.fd == -1) {
622 /* Do not (really) need to teardown the display */
623 _eglError(EGL_BAD_ALLOC, "eglGetPlatformDisplay");
624 return NULL;
625 }
626 }
627
628 return display;
629 }