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"
40 #include "eglcontext.h"
41 #include "eglcurrent.h"
42 #include "eglsurface.h"
43 #include "egldisplay.h"
44 #include "egldriver.h"
45 #include "eglglobals.h"
50 /* Includes for _eglNativePlatformDetectNativeDisplay */
55 #ifdef HAVE_WAYLAND_PLATFORM
56 #include <wayland-client.h>
58 #ifdef HAVE_DRM_PLATFORM
64 * Map --with-egl-platforms names to platform types.
67 _EGLPlatformType platform
;
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" },
80 * Return the native platform by parsing EGL_PLATFORM.
82 static _EGLPlatformType
83 _eglGetNativePlatformFromEnv(void)
85 _EGLPlatformType plat
= _EGL_INVALID_PLATFORM
;
86 const char *plat_name
;
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
;
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
;
108 * Perform validity checks on a generic pointer.
111 _eglPointerIsDereferencable(void *p
)
114 uintptr_t addr
= (uintptr_t) p
;
115 unsigned char valid
= 0;
116 const long page_size
= getpagesize();
121 /* align addr to page_size */
122 addr
&= ~(page_size
- 1);
124 if (mincore((void *) addr
, page_size
, &valid
) < 0) {
125 _eglLog(_EGL_DEBUG
, "mincore failed: %m");
129 return (valid
& 0x01) == 0x01;
137 * Try detecting native platform with the help of native display characteristcs.
139 static _EGLPlatformType
140 _eglNativePlatformDetectNativeDisplay(void *nativeDisplay
)
142 if (nativeDisplay
== EGL_DEFAULT_DISPLAY
)
143 return _EGL_INVALID_PLATFORM
;
145 if (_eglPointerIsDereferencable(nativeDisplay
)) {
146 void *first_pointer
= *(void **) nativeDisplay
;
148 (void) first_pointer
; /* silence unused var warning */
150 #ifdef HAVE_WAYLAND_PLATFORM
151 /* wl_display is a wl_proxy, which is a wl_object.
152 * wl_object's first element points to the interfacetype. */
153 if (first_pointer
== &wl_display_interface
)
154 return _EGL_PLATFORM_WAYLAND
;
157 #ifdef HAVE_DRM_PLATFORM
158 /* gbm has a pointer to its constructor as first element. */
159 if (first_pointer
== gbm_create_device
)
160 return _EGL_PLATFORM_DRM
;
163 #ifdef HAVE_X11_PLATFORM
164 /* If not matched to any other platform, fallback to x11. */
165 return _EGL_PLATFORM_X11
;
168 #ifdef HAVE_HAIKU_PLATFORM
169 return _EGL_PLATFORM_HAIKU
;
173 return _EGL_INVALID_PLATFORM
;
178 * Return the native platform. It is the platform of the EGL native types.
181 _eglGetNativePlatform(void *nativeDisplay
)
183 static _EGLPlatformType native_platform
;
184 char *detection_method
;
186 native_platform
= _eglGetNativePlatformFromEnv();
187 detection_method
= "environment overwrite";
189 if (native_platform
== _EGL_INVALID_PLATFORM
) {
190 native_platform
= _eglNativePlatformDetectNativeDisplay(nativeDisplay
);
191 detection_method
= "autodetected";
194 if (native_platform
== _EGL_INVALID_PLATFORM
) {
195 native_platform
= _EGL_NATIVE_PLATFORM
;
196 detection_method
= "build-time configuration";
199 _eglLog(_EGL_DEBUG
, "Native platform type: %s (%s)",
200 egl_platforms
[native_platform
].name
, detection_method
);
202 return native_platform
;
207 * Finish display management.
210 _eglFiniDisplay(void)
212 _EGLDisplay
*dpyList
, *dpy
;
214 /* atexit function is called with global mutex locked */
215 dpyList
= _eglGlobal
.DisplayList
;
221 dpyList
= dpyList
->Next
;
223 for (i
= 0; i
< _EGL_NUM_RESOURCES
; i
++) {
224 if (dpy
->ResourceLists
[i
]) {
225 _eglLog(_EGL_DEBUG
, "Display %p is destroyed with resources", dpy
);
232 _eglGlobal
.DisplayList
= NULL
;
237 * Find the display corresponding to the specified native display, or create a
241 _eglFindDisplay(_EGLPlatformType plat
, void *plat_dpy
)
245 if (plat
== _EGL_INVALID_PLATFORM
)
248 mtx_lock(_eglGlobal
.Mutex
);
250 /* search the display list first */
251 dpy
= _eglGlobal
.DisplayList
;
253 if (dpy
->Platform
== plat
&& dpy
->PlatformDisplay
== plat_dpy
)
258 /* create a new display */
260 dpy
= calloc(1, sizeof(_EGLDisplay
));
262 mtx_init(&dpy
->Mutex
, mtx_plain
);
263 dpy
->Platform
= plat
;
264 dpy
->PlatformDisplay
= plat_dpy
;
266 /* add to the display list */
267 dpy
->Next
= _eglGlobal
.DisplayList
;
268 _eglGlobal
.DisplayList
= dpy
;
272 mtx_unlock(_eglGlobal
.Mutex
);
279 * Destroy the contexts and surfaces that are linked to the display.
282 _eglReleaseDisplayResources(_EGLDriver
*drv
, _EGLDisplay
*display
)
286 list
= display
->ResourceLists
[_EGL_RESOURCE_CONTEXT
];
288 _EGLContext
*ctx
= (_EGLContext
*) list
;
291 _eglUnlinkContext(ctx
);
292 drv
->API
.DestroyContext(drv
, display
, ctx
);
294 assert(!display
->ResourceLists
[_EGL_RESOURCE_CONTEXT
]);
296 list
= display
->ResourceLists
[_EGL_RESOURCE_SURFACE
];
298 _EGLSurface
*surf
= (_EGLSurface
*) list
;
301 _eglUnlinkSurface(surf
);
302 drv
->API
.DestroySurface(drv
, display
, surf
);
304 assert(!display
->ResourceLists
[_EGL_RESOURCE_SURFACE
]);
306 list
= display
->ResourceLists
[_EGL_RESOURCE_IMAGE
];
308 _EGLImage
*image
= (_EGLImage
*) list
;
311 _eglUnlinkImage(image
);
312 drv
->API
.DestroyImageKHR(drv
, display
, image
);
314 assert(!display
->ResourceLists
[_EGL_RESOURCE_IMAGE
]);
316 list
= display
->ResourceLists
[_EGL_RESOURCE_SYNC
];
318 _EGLSync
*sync
= (_EGLSync
*) list
;
321 _eglUnlinkSync(sync
);
322 drv
->API
.DestroySyncKHR(drv
, display
, sync
);
324 assert(!display
->ResourceLists
[_EGL_RESOURCE_SYNC
]);
329 * Free all the data hanging of an _EGLDisplay object, but not
333 _eglCleanupDisplay(_EGLDisplay
*disp
)
336 _eglDestroyArray(disp
->Configs
, free
);
337 disp
->Configs
= NULL
;
345 * Return EGL_TRUE if the given handle is a valid handle to a display.
348 _eglCheckDisplayHandle(EGLDisplay dpy
)
352 mtx_lock(_eglGlobal
.Mutex
);
353 cur
= _eglGlobal
.DisplayList
;
355 if (cur
== (_EGLDisplay
*) dpy
)
359 mtx_unlock(_eglGlobal
.Mutex
);
360 return (cur
!= NULL
);
365 * Return EGL_TRUE if the given resource is valid. That is, the display does
369 _eglCheckResource(void *res
, _EGLResourceType type
, _EGLDisplay
*dpy
)
371 _EGLResource
*list
= dpy
->ResourceLists
[type
];
377 if (res
== (void *) list
) {
378 assert(list
->Display
== dpy
);
384 return (list
!= NULL
);
389 * Initialize a display resource. The size of the subclass object is
392 * This is supposed to be called from the initializers of subclasses, such as
393 * _eglInitContext or _eglInitSurface.
396 _eglInitResource(_EGLResource
*res
, EGLint size
, _EGLDisplay
*dpy
)
398 memset(res
, 0, size
);
405 * Increment reference count for the resource.
408 _eglGetResource(_EGLResource
*res
)
410 assert(res
&& res
->RefCount
> 0);
411 /* hopefully a resource is always manipulated with its display locked */
417 * Decrement reference count for the resource.
420 _eglPutResource(_EGLResource
*res
)
422 assert(res
&& res
->RefCount
> 0);
424 return (!res
->RefCount
);
429 * Link a resource to its display.
432 _eglLinkResource(_EGLResource
*res
, _EGLResourceType type
)
434 assert(res
->Display
);
436 res
->IsLinked
= EGL_TRUE
;
437 res
->Next
= res
->Display
->ResourceLists
[type
];
438 res
->Display
->ResourceLists
[type
] = res
;
439 _eglGetResource(res
);
444 * Unlink a linked resource from its display.
447 _eglUnlinkResource(_EGLResource
*res
, _EGLResourceType type
)
451 prev
= res
->Display
->ResourceLists
[type
];
454 if (prev
->Next
== res
)
459 prev
->Next
= res
->Next
;
462 res
->Display
->ResourceLists
[type
] = res
->Next
;
466 res
->IsLinked
= EGL_FALSE
;
467 _eglPutResource(res
);
469 /* We always unlink before destroy. The driver still owns a reference */
470 assert(res
->RefCount
);
473 #ifdef HAVE_X11_PLATFORM
475 _eglParseX11DisplayAttribList(_EGLDisplay
*display
, const EGLint
*attrib_list
)
479 if (attrib_list
== NULL
) {
483 for (i
= 0; attrib_list
[i
] != EGL_NONE
; i
+= 2) {
484 EGLint attrib
= attrib_list
[i
];
485 EGLint value
= attrib_list
[i
+ 1];
487 /* EGL_EXT_platform_x11 recognizes exactly one attribute,
488 * EGL_PLATFORM_X11_SCREEN_EXT, which is optional.
490 if (attrib
== EGL_PLATFORM_X11_SCREEN_EXT
) {
491 display
->Options
.Platform
= (void *)value
;
493 _eglError(EGL_BAD_ATTRIBUTE
, "eglGetPlatformDisplay");
502 _eglGetX11Display(Display
*native_display
,
503 const EGLint
*attrib_list
)
505 _EGLDisplay
*display
= _eglFindDisplay(_EGL_PLATFORM_X11
,
509 _eglError(EGL_BAD_ALLOC
, "eglGetPlatformDisplay");
513 if (!_eglParseX11DisplayAttribList(display
, attrib_list
)) {
519 #endif /* HAVE_X11_PLATFORM */
521 #ifdef HAVE_DRM_PLATFORM
523 _eglGetGbmDisplay(struct gbm_device
*native_display
,
524 const EGLint
*attrib_list
)
526 /* EGL_MESA_platform_gbm recognizes no attributes. */
527 if (attrib_list
!= NULL
&& attrib_list
[0] != EGL_NONE
) {
528 _eglError(EGL_BAD_ATTRIBUTE
, "eglGetPlatformDisplay");
532 return _eglFindDisplay(_EGL_PLATFORM_DRM
, native_display
);
534 #endif /* HAVE_DRM_PLATFORM */
536 #ifdef HAVE_WAYLAND_PLATFORM
538 _eglGetWaylandDisplay(struct wl_display
*native_display
,
539 const EGLint
*attrib_list
)
541 /* EGL_EXT_platform_wayland recognizes no attributes. */
542 if (attrib_list
!= NULL
&& attrib_list
[0] != EGL_NONE
) {
543 _eglError(EGL_BAD_ATTRIBUTE
, "eglGetPlatformDisplay");
547 return _eglFindDisplay(_EGL_PLATFORM_WAYLAND
, native_display
);
549 #endif /* HAVE_WAYLAND_PLATFORM */
551 #ifdef HAVE_SURFACELESS_PLATFORM
553 _eglGetSurfacelessDisplay(void *native_display
,
554 const EGLint
*attrib_list
)
556 /* This platform has no native display. */
557 if (native_display
!= NULL
) {
558 _eglError(EGL_BAD_PARAMETER
, "eglGetPlatformDisplay");
562 /* This platform recognizes no display attributes. */
563 if (attrib_list
!= NULL
&& attrib_list
[0] != EGL_NONE
) {
564 _eglError(EGL_BAD_ATTRIBUTE
, "eglGetPlatformDisplay");
568 return _eglFindDisplay(_EGL_PLATFORM_SURFACELESS
, native_display
);
570 #endif /* HAVE_SURFACELESS_PLATFORM */