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