2 * Functions related to EGLDisplay.
8 #include "eglcontext.h"
9 #include "eglsurface.h"
10 #include "egldisplay.h"
11 #include "egldriver.h"
12 #include "eglglobals.h"
18 * Return the native platform by parsing EGL_PLATFORM.
20 static _EGLPlatformType
21 _eglGetNativePlatformFromEnv(void)
23 /* map --with-egl-platforms names to platform types */
25 _EGLPlatformType platform
;
27 } egl_platforms
[_EGL_NUM_PLATFORMS
] = {
28 { _EGL_PLATFORM_WINDOWS
, "gdi" },
29 { _EGL_PLATFORM_X11
, "x11" },
30 { _EGL_PLATFORM_WAYLAND
, "wayland" },
31 { _EGL_PLATFORM_DRM
, "drm" },
32 { _EGL_PLATFORM_FBDEV
, "fbdev" }
34 _EGLPlatformType plat
= _EGL_INVALID_PLATFORM
;
35 const char *plat_name
;
38 plat_name
= getenv("EGL_PLATFORM");
39 /* try deprecated env variable */
40 if (!plat_name
|| !plat_name
[0])
41 plat_name
= getenv("EGL_DISPLAY");
42 if (!plat_name
|| !plat_name
[0])
43 return _EGL_INVALID_PLATFORM
;
45 for (i
= 0; i
< _EGL_NUM_PLATFORMS
; i
++) {
46 if (strcmp(egl_platforms
[i
].name
, plat_name
) == 0) {
47 plat
= egl_platforms
[i
].platform
;
57 * Return the native platform. It is the platform of the EGL native types.
60 _eglGetNativePlatform(void)
62 static _EGLPlatformType native_platform
= _EGL_INVALID_PLATFORM
;
64 if (native_platform
== _EGL_INVALID_PLATFORM
) {
65 native_platform
= _eglGetNativePlatformFromEnv();
66 if (native_platform
== _EGL_INVALID_PLATFORM
)
67 native_platform
= _EGL_NATIVE_PLATFORM
;
70 return native_platform
;
75 * Finish display management.
80 _EGLDisplay
*dpyList
, *dpy
;
82 /* atexit function is called with global mutex locked */
83 dpyList
= _eglGlobal
.DisplayList
;
89 dpyList
= dpyList
->Next
;
91 for (i
= 0; i
< _EGL_NUM_RESOURCES
; i
++) {
92 if (dpy
->ResourceLists
[i
]) {
93 _eglLog(_EGL_DEBUG
, "Display %p is destroyed with resources", dpy
);
100 _eglGlobal
.DisplayList
= NULL
;
105 * Find the display corresponding to the specified native display, or create a
109 _eglFindDisplay(_EGLPlatformType plat
, void *plat_dpy
)
113 if (plat
== _EGL_INVALID_PLATFORM
)
116 _eglLockMutex(_eglGlobal
.Mutex
);
118 /* search the display list first */
119 dpy
= _eglGlobal
.DisplayList
;
121 if (dpy
->Platform
== plat
&& dpy
->PlatformDisplay
== plat_dpy
)
126 /* create a new display */
128 dpy
= (_EGLDisplay
*) calloc(1, sizeof(_EGLDisplay
));
130 _eglInitMutex(&dpy
->Mutex
);
131 dpy
->Platform
= plat
;
132 dpy
->PlatformDisplay
= plat_dpy
;
134 /* add to the display list */
135 dpy
->Next
= _eglGlobal
.DisplayList
;
136 _eglGlobal
.DisplayList
= dpy
;
140 _eglUnlockMutex(_eglGlobal
.Mutex
);
147 * Destroy the contexts and surfaces that are linked to the display.
150 _eglReleaseDisplayResources(_EGLDriver
*drv
, _EGLDisplay
*display
)
154 list
= display
->ResourceLists
[_EGL_RESOURCE_CONTEXT
];
156 _EGLContext
*ctx
= (_EGLContext
*) list
;
159 _eglUnlinkContext(ctx
);
160 drv
->API
.DestroyContext(drv
, display
, ctx
);
162 assert(!display
->ResourceLists
[_EGL_RESOURCE_CONTEXT
]);
164 list
= display
->ResourceLists
[_EGL_RESOURCE_SURFACE
];
166 _EGLSurface
*surf
= (_EGLSurface
*) list
;
169 _eglUnlinkSurface(surf
);
170 drv
->API
.DestroySurface(drv
, display
, surf
);
172 assert(!display
->ResourceLists
[_EGL_RESOURCE_SURFACE
]);
177 * Free all the data hanging of an _EGLDisplay object, but not
181 _eglCleanupDisplay(_EGLDisplay
*disp
)
184 _eglDestroyArray(disp
->Configs
, free
);
185 disp
->Configs
= NULL
;
193 * Return EGL_TRUE if the given handle is a valid handle to a display.
196 _eglCheckDisplayHandle(EGLDisplay dpy
)
200 _eglLockMutex(_eglGlobal
.Mutex
);
201 cur
= _eglGlobal
.DisplayList
;
203 if (cur
== (_EGLDisplay
*) dpy
)
207 _eglUnlockMutex(_eglGlobal
.Mutex
);
208 return (cur
!= NULL
);
213 * Return EGL_TRUE if the given resource is valid. That is, the display does
217 _eglCheckResource(void *res
, _EGLResourceType type
, _EGLDisplay
*dpy
)
219 _EGLResource
*list
= dpy
->ResourceLists
[type
];
225 if (res
== (void *) list
) {
226 assert(list
->Display
== dpy
);
232 return (list
!= NULL
);
237 * Initialize a display resource.
240 _eglInitResource(_EGLResource
*res
, EGLint size
, _EGLDisplay
*dpy
)
242 memset(res
, 0, size
);
249 * Increment reference count for the resource.
252 _eglGetResource(_EGLResource
*res
)
254 assert(res
&& res
->RefCount
> 0);
255 /* hopefully a resource is always manipulated with its display locked */
261 * Decrement reference count for the resource.
264 _eglPutResource(_EGLResource
*res
)
266 assert(res
&& res
->RefCount
> 0);
268 return (!res
->RefCount
);
273 * Link a resource to its display.
276 _eglLinkResource(_EGLResource
*res
, _EGLResourceType type
)
278 assert(res
->Display
);
280 res
->IsLinked
= EGL_TRUE
;
281 res
->Next
= res
->Display
->ResourceLists
[type
];
282 res
->Display
->ResourceLists
[type
] = res
;
283 _eglGetResource(res
);
288 * Unlink a linked resource from its display.
291 _eglUnlinkResource(_EGLResource
*res
, _EGLResourceType type
)
295 prev
= res
->Display
->ResourceLists
[type
];
298 if (prev
->Next
== res
)
303 prev
->Next
= res
->Next
;
306 res
->Display
->ResourceLists
[type
] = res
->Next
;
310 res
->IsLinked
= EGL_FALSE
;
311 _eglPutResource(res
);
313 /* We always unlink before destroy. The driver still owns a reference */
314 assert(res
->RefCount
);