1 /**************************************************************************
3 * Copyright 2008 VMware, Inc.
4 * Copyright 2009-2010 Chia-I Wu <olvaffe@gmail.com>
5 * Copyright 2010-2011 LunarG, Inc.
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:
16 * The above copyright notice and this permission notice (including the
17 * next paragraph) shall be included in all copies or substantial portions
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.
28 **************************************************************************/
32 * Functions related to EGLDisplay.
38 #include "c11/threads.h"
39 #include "util/u_atomic.h"
41 #include "eglcontext.h"
42 #include "eglcurrent.h"
43 #include "eglsurface.h"
44 #include "egldisplay.h"
45 #include "egldriver.h"
46 #include "eglglobals.h"
51 /* Includes for _eglNativePlatformDetectNativeDisplay */
52 #ifdef HAVE_WAYLAND_PLATFORM
53 #include <wayland-client.h>
55 #ifdef HAVE_DRM_PLATFORM
61 * Map build-system platform names to platform types.
64 _EGLPlatformType platform
;
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" },
77 * Return the native platform by parsing EGL_PLATFORM.
79 static _EGLPlatformType
80 _eglGetNativePlatformFromEnv(void)
82 _EGLPlatformType plat
= _EGL_INVALID_PLATFORM
;
83 const char *plat_name
;
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
;
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
;
105 * Try detecting native platform with the help of native display characteristcs.
107 static _EGLPlatformType
108 _eglNativePlatformDetectNativeDisplay(void *nativeDisplay
)
110 if (nativeDisplay
== EGL_DEFAULT_DISPLAY
)
111 return _EGL_INVALID_PLATFORM
;
113 if (_eglPointerIsDereferencable(nativeDisplay
)) {
114 void *first_pointer
= *(void **) nativeDisplay
;
116 (void) first_pointer
; /* silence unused var warning */
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
;
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
;
131 #ifdef HAVE_X11_PLATFORM
132 /* If not matched to any other platform, fallback to x11. */
133 return _EGL_PLATFORM_X11
;
136 #ifdef HAVE_HAIKU_PLATFORM
137 return _EGL_PLATFORM_HAIKU
;
141 return _EGL_INVALID_PLATFORM
;
146 * Return the native platform. It is the platform of the EGL native types.
149 _eglGetNativePlatform(void *nativeDisplay
)
151 static _EGLPlatformType native_platform
= _EGL_INVALID_PLATFORM
;
152 _EGLPlatformType detected_platform
= native_platform
;
154 if (detected_platform
== _EGL_INVALID_PLATFORM
) {
155 const char *detection_method
;
157 detected_platform
= _eglGetNativePlatformFromEnv();
158 detection_method
= "environment overwrite";
160 if (detected_platform
== _EGL_INVALID_PLATFORM
) {
161 detected_platform
= _eglNativePlatformDetectNativeDisplay(nativeDisplay
);
162 detection_method
= "autodetected";
165 if (detected_platform
== _EGL_INVALID_PLATFORM
) {
166 detected_platform
= _EGL_NATIVE_PLATFORM
;
167 detection_method
= "build-time configuration";
170 _eglLog(_EGL_DEBUG
, "Native platform type: %s (%s)",
171 egl_platforms
[detected_platform
].name
, detection_method
);
173 p_atomic_cmpxchg(&native_platform
, _EGL_INVALID_PLATFORM
,
177 return native_platform
;
182 * Finish display management.
185 _eglFiniDisplay(void)
187 _EGLDisplay
*dispList
, *disp
;
189 /* atexit function is called with global mutex locked */
190 dispList
= _eglGlobal
.DisplayList
;
196 dispList
= dispList
->Next
;
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
);
205 free(disp
->Options
.Attribs
);
208 _eglGlobal
.DisplayList
= NULL
;
212 _eglSameAttribs(const EGLAttrib
*a
, const EGLAttrib
*b
)
214 size_t na
= _eglNumAttribs(a
);
215 size_t nb
= _eglNumAttribs(b
);
217 /* different numbers of attributes must be different */
221 /* both lists NULL are the same */
225 /* otherwise, compare the lists */
226 return memcmp(a
, b
, na
) == 0 ? EGL_TRUE
: EGL_FALSE
;
230 * Find the display corresponding to the specified native display, or create a
231 * new one. EGL 1.5 says:
233 * Multiple calls made to eglGetPlatformDisplay with the same parameters
234 * will return the same EGLDisplay handle.
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
243 _eglFindDisplay(_EGLPlatformType plat
, void *plat_dpy
,
244 const EGLAttrib
*attrib_list
)
249 if (plat
== _EGL_INVALID_PLATFORM
)
252 mtx_lock(_eglGlobal
.Mutex
);
254 /* search the display list first */
255 disp
= _eglGlobal
.DisplayList
;
257 if (disp
->Platform
== plat
&& disp
->PlatformDisplay
== plat_dpy
&&
258 _eglSameAttribs(disp
->Options
.Attribs
, attrib_list
))
263 /* create a new display */
265 disp
= calloc(1, sizeof(_EGLDisplay
));
267 mtx_init(&disp
->Mutex
, mtx_plain
);
268 disp
->Platform
= plat
;
269 disp
->PlatformDisplay
= plat_dpy
;
270 num_attribs
= _eglNumAttribs(attrib_list
);
272 disp
->Options
.Attribs
= calloc(num_attribs
, sizeof(EGLAttrib
));
273 if (!disp
->Options
.Attribs
) {
278 memcpy(disp
->Options
.Attribs
, attrib_list
,
279 num_attribs
* sizeof(EGLAttrib
));
281 /* add to the display list */
282 disp
->Next
= _eglGlobal
.DisplayList
;
283 _eglGlobal
.DisplayList
= disp
;
288 mtx_unlock(_eglGlobal
.Mutex
);
295 * Destroy the contexts and surfaces that are linked to the display.
298 _eglReleaseDisplayResources(_EGLDriver
*drv
, _EGLDisplay
*display
)
302 list
= display
->ResourceLists
[_EGL_RESOURCE_CONTEXT
];
304 _EGLContext
*ctx
= (_EGLContext
*) list
;
307 _eglUnlinkContext(ctx
);
308 drv
->API
.DestroyContext(drv
, display
, ctx
);
310 assert(!display
->ResourceLists
[_EGL_RESOURCE_CONTEXT
]);
312 list
= display
->ResourceLists
[_EGL_RESOURCE_SURFACE
];
314 _EGLSurface
*surf
= (_EGLSurface
*) list
;
317 _eglUnlinkSurface(surf
);
318 drv
->API
.DestroySurface(drv
, display
, surf
);
320 assert(!display
->ResourceLists
[_EGL_RESOURCE_SURFACE
]);
322 list
= display
->ResourceLists
[_EGL_RESOURCE_IMAGE
];
324 _EGLImage
*image
= (_EGLImage
*) list
;
327 _eglUnlinkImage(image
);
328 drv
->API
.DestroyImageKHR(drv
, display
, image
);
330 assert(!display
->ResourceLists
[_EGL_RESOURCE_IMAGE
]);
332 list
= display
->ResourceLists
[_EGL_RESOURCE_SYNC
];
334 _EGLSync
*sync
= (_EGLSync
*) list
;
337 _eglUnlinkSync(sync
);
338 drv
->API
.DestroySyncKHR(drv
, display
, sync
);
340 assert(!display
->ResourceLists
[_EGL_RESOURCE_SYNC
]);
345 * Free all the data hanging of an _EGLDisplay object, but not
349 _eglCleanupDisplay(_EGLDisplay
*disp
)
352 _eglDestroyArray(disp
->Configs
, free
);
353 disp
->Configs
= NULL
;
361 * Return EGL_TRUE if the given handle is a valid handle to a display.
364 _eglCheckDisplayHandle(EGLDisplay dpy
)
368 mtx_lock(_eglGlobal
.Mutex
);
369 cur
= _eglGlobal
.DisplayList
;
371 if (cur
== (_EGLDisplay
*) dpy
)
375 mtx_unlock(_eglGlobal
.Mutex
);
376 return (cur
!= NULL
);
381 * Return EGL_TRUE if the given resource is valid. That is, the display does
385 _eglCheckResource(void *res
, _EGLResourceType type
, _EGLDisplay
*disp
)
387 _EGLResource
*list
= disp
->ResourceLists
[type
];
393 if (res
== (void *) list
) {
394 assert(list
->Display
== disp
);
400 return (list
!= NULL
);
405 * Initialize a display resource. The size of the subclass object is
408 * This is supposed to be called from the initializers of subclasses, such as
409 * _eglInitContext or _eglInitSurface.
412 _eglInitResource(_EGLResource
*res
, EGLint size
, _EGLDisplay
*disp
)
414 memset(res
, 0, size
);
421 * Increment reference count for the resource.
424 _eglGetResource(_EGLResource
*res
)
426 assert(res
&& res
->RefCount
> 0);
427 /* hopefully a resource is always manipulated with its display locked */
433 * Decrement reference count for the resource.
436 _eglPutResource(_EGLResource
*res
)
438 assert(res
&& res
->RefCount
> 0);
440 return (!res
->RefCount
);
445 * Link a resource to its display.
448 _eglLinkResource(_EGLResource
*res
, _EGLResourceType type
)
450 assert(res
->Display
);
452 res
->IsLinked
= EGL_TRUE
;
453 res
->Next
= res
->Display
->ResourceLists
[type
];
454 res
->Display
->ResourceLists
[type
] = res
;
455 _eglGetResource(res
);
460 * Unlink a linked resource from its display.
463 _eglUnlinkResource(_EGLResource
*res
, _EGLResourceType type
)
467 prev
= res
->Display
->ResourceLists
[type
];
470 if (prev
->Next
== res
)
475 prev
->Next
= res
->Next
;
478 res
->Display
->ResourceLists
[type
] = res
->Next
;
482 res
->IsLinked
= EGL_FALSE
;
483 _eglPutResource(res
);
485 /* We always unlink before destroy. The driver still owns a reference */
486 assert(res
->RefCount
);
489 #ifdef HAVE_X11_PLATFORM
491 _eglParseX11DisplayAttribList(_EGLDisplay
*display
,
492 const EGLAttrib
*attrib_list
)
496 if (attrib_list
== NULL
) {
500 for (i
= 0; attrib_list
[i
] != EGL_NONE
; i
+= 2) {
501 EGLAttrib attrib
= attrib_list
[i
];
502 EGLAttrib value
= attrib_list
[i
+ 1];
504 /* EGL_EXT_platform_x11 recognizes exactly one attribute,
505 * EGL_PLATFORM_X11_SCREEN_EXT, which is optional.
507 if (attrib
!= EGL_PLATFORM_X11_SCREEN_EXT
)
508 return _eglError(EGL_BAD_ATTRIBUTE
, "eglGetPlatformDisplay");
510 display
->Options
.Platform
= (void *)(uintptr_t)value
;
517 _eglGetX11Display(Display
*native_display
,
518 const EGLAttrib
*attrib_list
)
520 _EGLDisplay
*display
= _eglFindDisplay(_EGL_PLATFORM_X11
,
525 _eglError(EGL_BAD_ALLOC
, "eglGetPlatformDisplay");
529 if (!_eglParseX11DisplayAttribList(display
, attrib_list
)) {
535 #endif /* HAVE_X11_PLATFORM */
537 #ifdef HAVE_DRM_PLATFORM
539 _eglGetGbmDisplay(struct gbm_device
*native_display
,
540 const EGLAttrib
*attrib_list
)
542 /* EGL_MESA_platform_gbm recognizes no attributes. */
543 if (attrib_list
!= NULL
&& attrib_list
[0] != EGL_NONE
) {
544 _eglError(EGL_BAD_ATTRIBUTE
, "eglGetPlatformDisplay");
548 return _eglFindDisplay(_EGL_PLATFORM_DRM
, native_display
, attrib_list
);
550 #endif /* HAVE_DRM_PLATFORM */
552 #ifdef HAVE_WAYLAND_PLATFORM
554 _eglGetWaylandDisplay(struct wl_display
*native_display
,
555 const EGLAttrib
*attrib_list
)
557 /* EGL_EXT_platform_wayland recognizes no attributes. */
558 if (attrib_list
!= NULL
&& attrib_list
[0] != EGL_NONE
) {
559 _eglError(EGL_BAD_ATTRIBUTE
, "eglGetPlatformDisplay");
563 return _eglFindDisplay(_EGL_PLATFORM_WAYLAND
, native_display
, attrib_list
);
565 #endif /* HAVE_WAYLAND_PLATFORM */
567 #ifdef HAVE_SURFACELESS_PLATFORM
569 _eglGetSurfacelessDisplay(void *native_display
,
570 const EGLAttrib
*attrib_list
)
572 /* This platform has no native display. */
573 if (native_display
!= NULL
) {
574 _eglError(EGL_BAD_PARAMETER
, "eglGetPlatformDisplay");
578 /* This platform recognizes no display attributes. */
579 if (attrib_list
!= NULL
&& attrib_list
[0] != EGL_NONE
) {
580 _eglError(EGL_BAD_ATTRIBUTE
, "eglGetPlatformDisplay");
584 return _eglFindDisplay(_EGL_PLATFORM_SURFACELESS
, native_display
,
587 #endif /* HAVE_SURFACELESS_PLATFORM */