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