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 #ifndef EGLDISPLAY_INCLUDED
31 #define EGLDISPLAY_INCLUDED
32
33 #include "util/rwlock.h"
34 #include "util/simple_mtx.h"
35
36 #include "eglarray.h"
37 #include "egldefines.h"
38 #include "egltypedefs.h"
39
40 #ifdef HAVE_X11_PLATFORM
41 #include <X11/Xlib.h>
42 #endif
43
44 #ifdef __cplusplus
45 extern "C" {
46 #endif
47
48 enum _egl_platform_type {
49 _EGL_PLATFORM_X11,
50 _EGL_PLATFORM_XCB,
51 _EGL_PLATFORM_WAYLAND,
52 _EGL_PLATFORM_DRM,
53 _EGL_PLATFORM_ANDROID,
54 _EGL_PLATFORM_HAIKU,
55 _EGL_PLATFORM_SURFACELESS,
56 _EGL_PLATFORM_DEVICE,
57 _EGL_PLATFORM_WINDOWS,
58 _EGL_PLATFORM_OHOS,
59
60 _EGL_NUM_PLATFORMS,
61 _EGL_INVALID_PLATFORM = -1
62 };
63 typedef enum _egl_platform_type _EGLPlatformType;
64
65 enum _egl_resource_type {
66 _EGL_RESOURCE_CONTEXT,
67 _EGL_RESOURCE_SURFACE,
68 _EGL_RESOURCE_IMAGE,
69 _EGL_RESOURCE_SYNC,
70
71 _EGL_NUM_RESOURCES
72 };
73 /* this cannot and need not go into egltypedefs.h */
74 typedef enum _egl_resource_type _EGLResourceType;
75
76 /**
77 * A resource of a display.
78 */
79 struct _egl_resource {
80 /* which display the resource belongs to */
81 _EGLDisplay *Display;
82 EGLBoolean IsLinked;
83 EGLint RefCount;
84
85 EGLLabelKHR Label;
86
87 /* used to link resources of the same type */
88 _EGLResource *Next;
89 };
90
91 /**
92 * Optional EGL extensions info.
93 */
94 struct _egl_extensions {
95 /* Please keep these sorted alphabetically. */
96 EGLBoolean ANDROID_blob_cache;
97 EGLBoolean ANDROID_framebuffer_target;
98 EGLBoolean ANDROID_image_native_buffer;
99 EGLBoolean ANDROID_native_fence_sync;
100 EGLBoolean ANDROID_recordable;
101
102 EGLBoolean ANGLE_sync_control_rate;
103 EGLBoolean CHROMIUM_sync_control;
104
105 EGLBoolean EXT_buffer_age;
106 EGLBoolean EXT_config_select_group;
107 EGLBoolean EXT_create_context_robustness;
108 EGLBoolean EXT_image_dma_buf_import;
109 EGLBoolean EXT_image_dma_buf_import_modifiers;
110 EGLBoolean EXT_pixel_format_float;
111 EGLBoolean EXT_present_opaque;
112 EGLBoolean EXT_protected_content;
113 EGLBoolean EXT_protected_surface;
114 EGLBoolean EXT_query_reset_notification_strategy;
115 EGLBoolean EXT_surface_compression;
116 EGLBoolean EXT_surface_CTA861_3_metadata;
117 EGLBoolean EXT_surface_SMPTE2086_metadata;
118 EGLBoolean EXT_swap_buffers_with_damage;
119
120 unsigned int IMG_context_priority;
121 #define __EGL_CONTEXT_PRIORITY_LOW_BIT 0
122 #define __EGL_CONTEXT_PRIORITY_MEDIUM_BIT 1
123 #define __EGL_CONTEXT_PRIORITY_HIGH_BIT 2
124 #define __EGL_CONTEXT_PRIORITY_REALTIME_BIT 3
125
126 EGLBoolean KHR_cl_event2;
127 EGLBoolean KHR_config_attribs;
128 EGLBoolean KHR_context_flush_control;
129 EGLBoolean KHR_create_context;
130 EGLBoolean KHR_create_context_no_error;
131 EGLBoolean KHR_fence_sync;
132 EGLBoolean KHR_get_all_proc_addresses;
133 EGLBoolean KHR_gl_colorspace;
134 EGLBoolean KHR_gl_renderbuffer_image;
135 EGLBoolean KHR_gl_texture_2D_image;
136 EGLBoolean KHR_gl_texture_3D_image;
137 EGLBoolean KHR_gl_texture_cubemap_image;
138 EGLBoolean KHR_image;
139 EGLBoolean KHR_image_base;
140 EGLBoolean KHR_image_pixmap;
141 EGLBoolean KHR_mutable_render_buffer;
142 EGLBoolean KHR_no_config_context;
143 EGLBoolean KHR_partial_update;
144 EGLBoolean KHR_reusable_sync;
145 EGLBoolean KHR_surfaceless_context;
146 EGLBoolean KHR_wait_sync;
147
148 EGLBoolean MESA_drm_image;
149 EGLBoolean MESA_gl_interop;
150 EGLBoolean MESA_image_dma_buf_export;
151 EGLBoolean MESA_query_driver;
152 EGLBoolean MESA_x11_native_visual_id;
153
154 EGLBoolean NOK_swap_region;
155 EGLBoolean NOK_texture_from_pixmap;
156
157 EGLBoolean NV_post_sub_buffer;
158 EGLBoolean NV_context_priority_realtime;
159
160 EGLBoolean WL_bind_wayland_display;
161 EGLBoolean WL_create_wayland_buffer_from_image;
162 };
163
164 struct _egl_display {
165 /* used to link displays */
166 _EGLDisplay *Next;
167
168 /**
169 * The big-display-lock (BDL) which protects our internal state. EGL
170 * drivers should use their own locking, as needed, to protect their
171 * own state, rather than relying on this.
172 */
173 simple_mtx_t Mutex;
174
175 /**
176 * The spec appears to allow eglTerminate() to race with more or less
177 * any other egl call. To allow for this, while relaxing the BDL to
178 * allow other egl calls to happen in parallel, a rwlock is used. All
179 * points where the BDL lock is acquired also acquire TerminateLock
180 * for reading, while eglTerminate() itself acquires the TerminateLock
181 * for writing.
182 *
183 * Note, we could conceivably just replace the BDL with a single
184 * rwlock. But there are a couple shortcomings of u_rwlock:
185 *
186 * 1) The WIN32 implementation does not allow promoting a read-
187 * lock to write-lock, nor recursive locking, whereas the
188 * pthread based implementation does. Because of this, it
189 * would be difficult to keep the eglapi layer portable if
190 * we depended on any less-than-trivial rwlock usage.
191 *
192 * 2) We'd lose simple_mtx_assert_locked().
193 */
194 struct u_rwlock TerminateLock;
195
196 _EGLPlatformType Platform; /**< The type of the platform display */
197 void *PlatformDisplay; /**< A pointer to the platform display */
198
199 _EGLDevice *Device; /**< Device backing the display */
200 const _EGLDriver *Driver; /**< Matched driver of the display */
201 EGLBoolean Initialized; /**< True if the display is initialized */
202
203 /* options that affect how the driver initializes the display */
204 struct {
205 EGLBoolean Zink; /**< Use kopper only */
206 EGLBoolean FallbackZink; /**< True if zink is tried as fallback */
207 EGLBoolean ForceSoftware; /**< Use software path only */
208 EGLBoolean GalliumHudWarn; /**< Using hud, warn when querying buffer age */
209 EGLAttrib *Attribs; /**< Platform-specific options */
210 int fd; /**< Platform device specific, local fd */
211 } Options;
212
213 /* these fields are set by the driver during init */
214 void *DriverData; /**< Driver private data */
215 EGLint Version; /**< EGL version major*10+minor */
216 EGLint ClientAPIs; /**< Bitmask of APIs supported (EGL_xxx_BIT) */
217 _EGLExtensions Extensions; /**< Extensions supported */
218 EGLBoolean RobustBufferAccess; /**< Supports robust buffer access behavior */
219
220 /* these fields are derived from above */
221 char VersionString[100]; /**< EGL_VERSION */
222 char ClientAPIsString[100]; /**< EGL_CLIENT_APIS */
223 char ExtensionsString[_EGL_MAX_EXTENSIONS_LEN]; /**< EGL_EXTENSIONS */
224
225 _EGLArray *Configs;
226
227 /* lists of resources */
228 _EGLResource *ResourceLists[_EGL_NUM_RESOURCES];
229
230 EGLLabelKHR Label;
231
232 EGLSetBlobFuncANDROID BlobCacheSet;
233 EGLGetBlobFuncANDROID BlobCacheGet;
234 };
235
236 extern _EGLDisplay *
237 _eglLockDisplay(EGLDisplay dpy);
238
239 extern void
240 _eglUnlockDisplay(_EGLDisplay *disp);
241
242 extern _EGLPlatformType
243 _eglGetNativePlatform(void *nativeDisplay);
244
245 extern void
246 _eglFiniDisplay(void);
247
248 extern _EGLDisplay *
249 _eglFindDisplay(_EGLPlatformType plat, void *plat_dpy, const EGLAttrib *attr);
250
251 extern void
252 _eglReleaseDisplayResources(_EGLDisplay *disp);
253
254 extern void
255 _eglCleanupDisplay(_EGLDisplay *disp);
256
257 extern EGLBoolean
258 _eglCheckResource(void *res, _EGLResourceType type, _EGLDisplay *disp);
259
260 /**
261 * Return the handle of a linked display, or EGL_NO_DISPLAY.
262 */
263 static inline EGLDisplay
_eglGetDisplayHandle(_EGLDisplay * disp)264 _eglGetDisplayHandle(_EGLDisplay *disp)
265 {
266 return (EGLDisplay)((disp) ? disp : EGL_NO_DISPLAY);
267 }
268
269 static inline EGLBoolean
_eglHasAttrib(_EGLDisplay * disp,EGLAttrib attrib)270 _eglHasAttrib(_EGLDisplay *disp, EGLAttrib attrib)
271 {
272 EGLAttrib *attribs = disp->Options.Attribs;
273
274 if (!attribs) {
275 return EGL_FALSE;
276 }
277
278 for (int i = 0; attribs[i] != EGL_NONE; i += 2) {
279 if (attrib == attribs[i]) {
280 return EGL_TRUE;
281 }
282 }
283 return EGL_FALSE;
284 }
285
286 extern void
287 _eglInitResource(_EGLResource *res, EGLint size, _EGLDisplay *disp);
288
289 extern void
290 _eglGetResource(_EGLResource *res);
291
292 extern EGLBoolean
293 _eglPutResource(_EGLResource *res);
294
295 extern void
296 _eglLinkResource(_EGLResource *res, _EGLResourceType type);
297
298 extern void
299 _eglUnlinkResource(_EGLResource *res, _EGLResourceType type);
300
301 /**
302 * Return true if the resource is linked.
303 */
304 static inline EGLBoolean
_eglIsResourceLinked(_EGLResource * res)305 _eglIsResourceLinked(_EGLResource *res)
306 {
307 return res->IsLinked;
308 }
309
310 static inline size_t
_eglNumAttribs(const EGLAttrib * attribs)311 _eglNumAttribs(const EGLAttrib *attribs)
312 {
313 size_t len = 0;
314
315 if (attribs) {
316 while (attribs[len] != EGL_NONE)
317 len += 2;
318 len++;
319 }
320 return len;
321 }
322
323 #ifdef HAVE_X11_PLATFORM
324 _EGLDisplay *
325 _eglGetX11Display(Display *native_display, const EGLAttrib *attrib_list);
326 #endif
327
328 #ifdef HAVE_XCB_PLATFORM
329 typedef struct xcb_connection_t xcb_connection_t;
330 _EGLDisplay *
331 _eglGetXcbDisplay(xcb_connection_t *native_display,
332 const EGLAttrib *attrib_list);
333 #endif
334
335 #ifdef HAVE_DRM_PLATFORM
336 struct gbm_device;
337
338 _EGLDisplay *
339 _eglGetGbmDisplay(struct gbm_device *native_display,
340 const EGLAttrib *attrib_list);
341 #endif
342
343 #ifdef HAVE_WAYLAND_PLATFORM
344 struct wl_display;
345
346 _EGLDisplay *
347 _eglGetWaylandDisplay(struct wl_display *native_display,
348 const EGLAttrib *attrib_list);
349 #endif
350
351 _EGLDisplay *
352 _eglGetSurfacelessDisplay(void *native_display, const EGLAttrib *attrib_list);
353
354 #ifdef HAVE_ANDROID_PLATFORM
355 _EGLDisplay *
356 _eglGetAndroidDisplay(void *native_display, const EGLAttrib *attrib_list);
357 #endif
358
359 _EGLDisplay *
360 _eglGetDeviceDisplay(void *native_display, const EGLAttrib *attrib_list);
361
362 #ifdef __cplusplus
363 }
364 #endif
365
366 #endif /* EGLDISPLAY_INCLUDED */
367