• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
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 #define __EGL_CONTEXT_PRIORITY_REALTIME_BIT 3
124 
125    EGLBoolean KHR_cl_event2;
126    EGLBoolean KHR_config_attribs;
127    EGLBoolean KHR_context_flush_control;
128    EGLBoolean KHR_create_context;
129    EGLBoolean KHR_create_context_no_error;
130    EGLBoolean KHR_fence_sync;
131    EGLBoolean KHR_get_all_proc_addresses;
132    EGLBoolean KHR_gl_colorspace;
133    EGLBoolean KHR_gl_renderbuffer_image;
134    EGLBoolean KHR_gl_texture_2D_image;
135    EGLBoolean KHR_gl_texture_3D_image;
136    EGLBoolean KHR_gl_texture_cubemap_image;
137    EGLBoolean KHR_image;
138    EGLBoolean KHR_image_base;
139    EGLBoolean KHR_image_pixmap;
140    EGLBoolean KHR_mutable_render_buffer;
141    EGLBoolean KHR_no_config_context;
142    EGLBoolean KHR_partial_update;
143    EGLBoolean KHR_reusable_sync;
144    EGLBoolean KHR_surfaceless_context;
145    EGLBoolean KHR_wait_sync;
146 
147    EGLBoolean MESA_drm_image;
148    EGLBoolean MESA_gl_interop;
149    EGLBoolean MESA_image_dma_buf_export;
150    EGLBoolean MESA_query_driver;
151    EGLBoolean MESA_x11_native_visual_id;
152 
153    EGLBoolean NOK_swap_region;
154    EGLBoolean NOK_texture_from_pixmap;
155 
156    EGLBoolean NV_post_sub_buffer;
157    EGLBoolean NV_context_priority_realtime;
158 
159    EGLBoolean WL_bind_wayland_display;
160    EGLBoolean WL_create_wayland_buffer_from_image;
161 };
162 
163 struct _egl_display {
164    /* used to link displays */
165    _EGLDisplay *Next;
166 
167    /**
168     * The big-display-lock (BDL) which protects our internal state.  EGL
169     * drivers should use their own locking, as needed, to protect their
170     * own state, rather than relying on this.
171     */
172    simple_mtx_t Mutex;
173 
174    /**
175     * The spec appears to allow eglTerminate() to race with more or less
176     * any other egl call.  To allow for this, while relaxing the BDL to
177     * allow other egl calls to happen in parallel, a rwlock is used.  All
178     * points where the BDL lock is acquired also acquire TerminateLock
179     * for reading, while eglTerminate() itself acquires the TerminateLock
180     * for writing.
181     *
182     * Note, we could conceivably just replace the BDL with a single
183     * rwlock.  But there are a couple shortcomings of u_rwlock:
184     *
185     *   1) The WIN32 implementation does not allow promoting a read-
186     *      lock to write-lock, nor recursive locking, whereas the
187     *      pthread based implementation does.  Because of this, it
188     *      would be difficult to keep the eglapi layer portable if
189     *      we depended on any less-than-trivial rwlock usage.
190     *
191     *   2) We'd lose simple_mtx_assert_locked().
192     */
193    struct u_rwlock TerminateLock;
194 
195    _EGLPlatformType Platform; /**< The type of the platform display */
196    void *PlatformDisplay;     /**< A pointer to the platform display */
197 
198    _EGLDevice *Device;       /**< Device backing the display */
199    const _EGLDriver *Driver; /**< Matched driver of the display */
200    EGLBoolean Initialized;   /**< True if the display is initialized */
201 
202    /* options that affect how the driver initializes the display */
203    struct {
204       EGLBoolean Zink;           /**< Use kopper only */
205       EGLBoolean FallbackZink;   /**< True if zink is tried as fallback */
206       EGLBoolean ForceSoftware;  /**< Use software path only */
207       EGLBoolean GalliumHudWarn; /**< Using hud, warn when querying buffer age */
208       EGLAttrib *Attribs;        /**< Platform-specific options */
209       int fd;                    /**< Platform device specific, local fd */
210    } Options;
211 
212    /* these fields are set by the driver during init */
213    void *DriverData;          /**< Driver private data */
214    EGLint Version;            /**< EGL version major*10+minor */
215    EGLint ClientAPIs;         /**< Bitmask of APIs supported (EGL_xxx_BIT) */
216    _EGLExtensions Extensions; /**< Extensions supported */
217    EGLBoolean RobustBufferAccess; /**< Supports robust buffer access behavior */
218 
219    /* these fields are derived from above */
220    char VersionString[100];                        /**< EGL_VERSION */
221    char ClientAPIsString[100];                     /**< EGL_CLIENT_APIS */
222    char ExtensionsString[_EGL_MAX_EXTENSIONS_LEN]; /**< EGL_EXTENSIONS */
223 
224    _EGLArray *Configs;
225 
226    /* lists of resources */
227    _EGLResource *ResourceLists[_EGL_NUM_RESOURCES];
228 
229    EGLLabelKHR Label;
230 
231    EGLSetBlobFuncANDROID BlobCacheSet;
232    EGLGetBlobFuncANDROID BlobCacheGet;
233 };
234 
235 extern _EGLDisplay *
236 _eglLockDisplay(EGLDisplay dpy);
237 
238 extern void
239 _eglUnlockDisplay(_EGLDisplay *disp);
240 
241 extern _EGLPlatformType
242 _eglGetNativePlatform(void *nativeDisplay);
243 
244 extern void
245 _eglFiniDisplay(void);
246 
247 extern _EGLDisplay *
248 _eglFindDisplay(_EGLPlatformType plat, void *plat_dpy, const EGLAttrib *attr);
249 
250 extern void
251 _eglReleaseDisplayResources(_EGLDisplay *disp);
252 
253 extern void
254 _eglCleanupDisplay(_EGLDisplay *disp);
255 
256 extern EGLBoolean
257 _eglCheckResource(void *res, _EGLResourceType type, _EGLDisplay *disp);
258 
259 /**
260  * Return the handle of a linked display, or EGL_NO_DISPLAY.
261  */
262 static inline EGLDisplay
_eglGetDisplayHandle(_EGLDisplay * disp)263 _eglGetDisplayHandle(_EGLDisplay *disp)
264 {
265    return (EGLDisplay)((disp) ? disp : EGL_NO_DISPLAY);
266 }
267 
268 static inline EGLBoolean
_eglHasAttrib(_EGLDisplay * disp,EGLAttrib attrib)269 _eglHasAttrib(_EGLDisplay *disp, EGLAttrib attrib)
270 {
271    EGLAttrib *attribs = disp->Options.Attribs;
272 
273    if (!attribs) {
274       return EGL_FALSE;
275    }
276 
277    for (int i = 0; attribs[i] != EGL_NONE; i += 2) {
278       if (attrib == attribs[i]) {
279          return EGL_TRUE;
280       }
281    }
282    return EGL_FALSE;
283 }
284 
285 extern void
286 _eglInitResource(_EGLResource *res, EGLint size, _EGLDisplay *disp);
287 
288 extern void
289 _eglGetResource(_EGLResource *res);
290 
291 extern EGLBoolean
292 _eglPutResource(_EGLResource *res);
293 
294 extern void
295 _eglLinkResource(_EGLResource *res, _EGLResourceType type);
296 
297 extern void
298 _eglUnlinkResource(_EGLResource *res, _EGLResourceType type);
299 
300 /**
301  * Return true if the resource is linked.
302  */
303 static inline EGLBoolean
_eglIsResourceLinked(_EGLResource * res)304 _eglIsResourceLinked(_EGLResource *res)
305 {
306    return res->IsLinked;
307 }
308 
309 static inline size_t
_eglNumAttribs(const EGLAttrib * attribs)310 _eglNumAttribs(const EGLAttrib *attribs)
311 {
312    size_t len = 0;
313 
314    if (attribs) {
315       while (attribs[len] != EGL_NONE)
316          len += 2;
317       len++;
318    }
319    return len;
320 }
321 
322 #ifdef HAVE_X11_PLATFORM
323 _EGLDisplay *
324 _eglGetX11Display(Display *native_display, const EGLAttrib *attrib_list);
325 #endif
326 
327 #ifdef HAVE_XCB_PLATFORM
328 typedef struct xcb_connection_t xcb_connection_t;
329 _EGLDisplay *
330 _eglGetXcbDisplay(xcb_connection_t *native_display,
331                   const EGLAttrib *attrib_list);
332 #endif
333 
334 #ifdef HAVE_DRM_PLATFORM
335 struct gbm_device;
336 
337 _EGLDisplay *
338 _eglGetGbmDisplay(struct gbm_device *native_display,
339                   const EGLAttrib *attrib_list);
340 #endif
341 
342 #ifdef HAVE_WAYLAND_PLATFORM
343 struct wl_display;
344 
345 _EGLDisplay *
346 _eglGetWaylandDisplay(struct wl_display *native_display,
347                       const EGLAttrib *attrib_list);
348 #endif
349 
350 _EGLDisplay *
351 _eglGetSurfacelessDisplay(void *native_display, const EGLAttrib *attrib_list);
352 
353 #ifdef HAVE_ANDROID_PLATFORM
354 _EGLDisplay *
355 _eglGetAndroidDisplay(void *native_display, const EGLAttrib *attrib_list);
356 #endif
357 
358 _EGLDisplay *
359 _eglGetDeviceDisplay(void *native_display, const EGLAttrib *attrib_list);
360 
361 #ifdef __cplusplus
362 }
363 #endif
364 
365 #endif /* EGLDISPLAY_INCLUDED */
366