bffc63e1e94d21c9783875bd91f1aa4293c50264
[mesa.git] / src / egl / drivers / dri2 / egl_dri2.h
1 /*
2 * Copyright © 2011 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Kristian Høgsberg <krh@bitplanet.net>
26 */
27
28 #ifndef EGL_DRI2_INCLUDED
29 #define EGL_DRI2_INCLUDED
30
31 #include <stdbool.h>
32 #include <stdint.h>
33
34 #ifdef HAVE_X11_PLATFORM
35 #include <xcb/xcb.h>
36 #include <xcb/dri2.h>
37 #include <xcb/xfixes.h>
38 #include <X11/Xlib-xcb.h>
39
40 #ifdef HAVE_DRI3
41 #include "loader_dri3_helper.h"
42 #endif
43 #endif
44
45 #ifdef HAVE_WAYLAND_PLATFORM
46 #include <wayland-client.h>
47 #include "wayland-egl-priv.h"
48 #endif
49
50 #include <GL/gl.h>
51 #include <GL/internal/dri_interface.h>
52
53 #ifdef HAVE_DRM_PLATFORM
54 #include <gbm_driint.h>
55 #endif
56
57 #ifdef HAVE_ANDROID_PLATFORM
58 #define LOG_TAG "EGL-DRI2"
59
60 #include <system/window.h>
61 #include <hardware/gralloc.h>
62 #include <gralloc_drm_handle.h>
63
64 #endif /* HAVE_ANDROID_PLATFORM */
65
66 #include "eglconfig.h"
67 #include "eglcontext.h"
68 #include "egldisplay.h"
69 #include "egldriver.h"
70 #include "eglcurrent.h"
71 #include "egllog.h"
72 #include "eglsurface.h"
73 #include "eglimage.h"
74 #include "eglsync.h"
75
76 struct wl_buffer;
77
78 struct dri2_egl_driver
79 {
80 _EGLDriver base;
81
82 void *handle;
83 _EGLProc (*get_proc_address)(const char *procname);
84 void (*glFlush)(void);
85 };
86
87 struct dri2_egl_display_vtbl {
88 int (*authenticate)(_EGLDisplay *disp, uint32_t id);
89
90 _EGLSurface* (*create_window_surface)(_EGLDriver *drv, _EGLDisplay *dpy,
91 _EGLConfig *config,
92 void *native_window,
93 const EGLint *attrib_list);
94
95 _EGLSurface* (*create_pixmap_surface)(_EGLDriver *drv, _EGLDisplay *dpy,
96 _EGLConfig *config,
97 void *native_pixmap,
98 const EGLint *attrib_list);
99
100 _EGLSurface* (*create_pbuffer_surface)(_EGLDriver *drv, _EGLDisplay *dpy,
101 _EGLConfig *config,
102 const EGLint *attrib_list);
103
104 EGLBoolean (*destroy_surface)(_EGLDriver *drv, _EGLDisplay *dpy,
105 _EGLSurface *surface);
106
107 EGLBoolean (*swap_interval)(_EGLDriver *drv, _EGLDisplay *dpy,
108 _EGLSurface *surf, EGLint interval);
109
110 _EGLImage* (*create_image)(_EGLDriver *drv, _EGLDisplay *dpy,
111 _EGLContext *ctx, EGLenum target,
112 EGLClientBuffer buffer,
113 const EGLint *attr_list);
114
115 EGLBoolean (*swap_buffers)(_EGLDriver *drv, _EGLDisplay *dpy,
116 _EGLSurface *surf);
117
118 EGLBoolean (*swap_buffers_with_damage)(_EGLDriver *drv, _EGLDisplay *dpy,
119 _EGLSurface *surface,
120 const EGLint *rects, EGLint n_rects);
121
122 EGLBoolean (*set_damage_region)(_EGLDriver *drv, _EGLDisplay *dpy,
123 _EGLSurface *surface,
124 const EGLint *rects, EGLint n_rects);
125
126 EGLBoolean (*swap_buffers_region)(_EGLDriver *drv, _EGLDisplay *dpy,
127 _EGLSurface *surf, EGLint numRects,
128 const EGLint *rects);
129
130 EGLBoolean (*post_sub_buffer)(_EGLDriver *drv, _EGLDisplay *dpy,
131 _EGLSurface *surf,
132 EGLint x, EGLint y,
133 EGLint width, EGLint height);
134
135 EGLBoolean (*copy_buffers)(_EGLDriver *drv, _EGLDisplay *dpy,
136 _EGLSurface *surf, void *native_pixmap_target);
137
138 EGLint (*query_buffer_age)(_EGLDriver *drv, _EGLDisplay *dpy,
139 _EGLSurface *surf);
140
141 EGLBoolean (*query_surface)(_EGLDriver *drv, _EGLDisplay *dpy,
142 _EGLSurface *surf, EGLint attribute,
143 EGLint *value);
144
145 struct wl_buffer* (*create_wayland_buffer_from_image)(
146 _EGLDriver *drv, _EGLDisplay *dpy, _EGLImage *img);
147
148 EGLBoolean (*get_sync_values)(_EGLDisplay *display, _EGLSurface *surface,
149 EGLuint64KHR *ust, EGLuint64KHR *msc,
150 EGLuint64KHR *sbc);
151
152 __DRIdrawable *(*get_dri_drawable)(_EGLSurface *surf);
153 };
154
155 struct dri2_egl_display
156 {
157 const struct dri2_egl_display_vtbl *vtbl;
158
159 int dri2_major;
160 int dri2_minor;
161 __DRIscreen *dri_screen;
162 bool own_dri_screen;
163 const __DRIconfig **driver_configs;
164 void *driver;
165 const __DRIcoreExtension *core;
166 const __DRIimageDriverExtension *image_driver;
167 const __DRIdri2Extension *dri2;
168 const __DRIswrastExtension *swrast;
169 const __DRI2flushExtension *flush;
170 const __DRItexBufferExtension *tex_buffer;
171 const __DRIimageExtension *image;
172 const __DRIrobustnessExtension *robustness;
173 const __DRI2configQueryExtension *config;
174 const __DRI2fenceExtension *fence;
175 const __DRI2rendererQueryExtension *rendererQuery;
176 const __DRI2interopExtension *interop;
177 int fd;
178
179 /* dri2_initialize/dri2_terminate increment/decrement this count, so does
180 * dri2_make_current (tracks if there are active contexts/surfaces). */
181 int ref_count;
182
183 bool own_device;
184 bool invalidate_available;
185 int min_swap_interval;
186 int max_swap_interval;
187 int default_swap_interval;
188 #ifdef HAVE_DRM_PLATFORM
189 struct gbm_dri_device *gbm_dri;
190 #endif
191
192 char *driver_name;
193
194 const __DRIextension **loader_extensions;
195 const __DRIextension **driver_extensions;
196
197 #ifdef HAVE_X11_PLATFORM
198 xcb_connection_t *conn;
199 xcb_screen_t *screen;
200 bool swap_available;
201 #ifdef HAVE_DRI3
202 struct loader_dri3_extensions loader_dri3_ext;
203 #endif
204 #endif
205
206 #ifdef HAVE_WAYLAND_PLATFORM
207 struct wl_display *wl_dpy;
208 struct wl_display *wl_dpy_wrapper;
209 struct wl_registry *wl_registry;
210 struct wl_drm *wl_server_drm;
211 struct wl_drm *wl_drm;
212 struct wl_shm *wl_shm;
213 struct wl_event_queue *wl_queue;
214 bool authenticated;
215 int formats;
216 uint32_t capabilities;
217 char *device_name;
218 #endif
219
220 #ifdef HAVE_ANDROID_PLATFORM
221 const gralloc_module_t *gralloc;
222 #endif
223
224 bool is_render_node;
225 int is_different_gpu;
226 };
227
228 struct dri2_egl_context
229 {
230 _EGLContext base;
231 __DRIcontext *dri_context;
232 };
233
234 #ifdef HAVE_WAYLAND_PLATFORM
235 enum wayland_buffer_type {
236 WL_BUFFER_FRONT,
237 WL_BUFFER_BACK,
238 WL_BUFFER_THIRD,
239 WL_BUFFER_COUNT
240 };
241 #endif
242
243 struct dri2_egl_surface
244 {
245 _EGLSurface base;
246 __DRIdrawable *dri_drawable;
247 __DRIbuffer buffers[5];
248 int buffer_count;
249 bool have_fake_front;
250
251 #ifdef HAVE_X11_PLATFORM
252 xcb_drawable_t drawable;
253 xcb_xfixes_region_t region;
254 int depth;
255 int bytes_per_pixel;
256 xcb_gcontext_t gc;
257 xcb_gcontext_t swapgc;
258 #endif
259
260 #ifdef HAVE_WAYLAND_PLATFORM
261 struct wl_egl_window *wl_win;
262 int dx;
263 int dy;
264 struct wl_event_queue *wl_queue;
265 struct wl_surface *wl_surface_wrapper;
266 struct wl_display *wl_dpy_wrapper;
267 struct wl_drm *wl_drm_wrapper;
268 struct wl_callback *throttle_callback;
269 int format;
270 #endif
271
272 #ifdef HAVE_DRM_PLATFORM
273 struct gbm_dri_surface *gbm_surf;
274 #endif
275
276 #if defined(HAVE_WAYLAND_PLATFORM) || defined(HAVE_DRM_PLATFORM)
277 __DRIbuffer *dri_buffers[__DRI_BUFFER_COUNT];
278 struct {
279 #ifdef HAVE_WAYLAND_PLATFORM
280 struct wl_buffer *wl_buffer;
281 __DRIimage *dri_image;
282 /* for is_different_gpu case. NULL else */
283 __DRIimage *linear_copy;
284 /* for swrast */
285 void *data;
286 int data_size;
287 #endif
288 #ifdef HAVE_DRM_PLATFORM
289 struct gbm_bo *bo;
290 #endif
291 bool locked;
292 int age;
293 } color_buffers[4], *back, *current;
294 #endif
295
296 #ifdef HAVE_ANDROID_PLATFORM
297 struct ANativeWindow *window;
298 struct ANativeWindowBuffer *buffer;
299 __DRIimage *dri_image_back;
300 __DRIimage *dri_image_front;
301
302 /* EGL-owned buffers */
303 __DRIbuffer *local_buffers[__DRI_BUFFER_COUNT];
304
305 /* Used to record all the buffers created by ANativeWindow and their ages.
306 * Usually Android uses at most triple buffers in ANativeWindow
307 * so hardcode the number of color_buffers to 3.
308 */
309 struct {
310 struct ANativeWindowBuffer *buffer;
311 int age;
312 } color_buffers[3], *back;
313 #endif
314
315 #if defined(HAVE_SURFACELESS_PLATFORM)
316 __DRIimage *front;
317 unsigned int visual;
318 #endif
319 };
320
321 struct dri2_egl_config
322 {
323 _EGLConfig base;
324 const __DRIconfig *dri_single_config[2];
325 const __DRIconfig *dri_double_config[2];
326 };
327
328 struct dri2_egl_image
329 {
330 _EGLImage base;
331 __DRIimage *dri_image;
332 };
333
334 struct dri2_egl_sync {
335 _EGLSync base;
336 mtx_t mutex;
337 cnd_t cond;
338 int refcount;
339 void *fence;
340 };
341
342 /* From xmlpool/options.h, user exposed so should be stable */
343 #define DRI_CONF_VBLANK_NEVER 0
344 #define DRI_CONF_VBLANK_DEF_INTERVAL_0 1
345 #define DRI_CONF_VBLANK_DEF_INTERVAL_1 2
346 #define DRI_CONF_VBLANK_ALWAYS_SYNC 3
347
348 /* standard typecasts */
349 _EGL_DRIVER_STANDARD_TYPECASTS(dri2_egl)
350 _EGL_DRIVER_TYPECAST(dri2_egl_image, _EGLImage, obj)
351 _EGL_DRIVER_TYPECAST(dri2_egl_sync, _EGLSync, obj)
352
353 extern const __DRIimageLookupExtension image_lookup_extension;
354 extern const __DRIuseInvalidateExtension use_invalidate;
355 extern const __DRIbackgroundCallableExtension background_callable_extension;
356
357 EGLBoolean
358 dri2_load_driver(_EGLDisplay *disp);
359
360 /* Helper for platforms not using dri2_create_screen */
361 void
362 dri2_setup_screen(_EGLDisplay *disp);
363
364 EGLBoolean
365 dri2_load_driver_swrast(_EGLDisplay *disp);
366
367 EGLBoolean
368 dri2_load_driver_dri3(_EGLDisplay *disp);
369
370 EGLBoolean
371 dri2_create_screen(_EGLDisplay *disp);
372
373 EGLBoolean
374 dri2_setup_extensions(_EGLDisplay *disp);
375
376 __DRIdrawable *
377 dri2_surface_get_dri_drawable(_EGLSurface *surf);
378
379 __DRIimage *
380 dri2_lookup_egl_image(__DRIscreen *screen, void *image, void *data);
381
382 struct dri2_egl_config *
383 dri2_add_config(_EGLDisplay *disp, const __DRIconfig *dri_config, int id,
384 EGLint surface_type, const EGLint *attr_list,
385 const unsigned int *rgba_masks);
386
387 _EGLImage *
388 dri2_create_image_khr(_EGLDriver *drv, _EGLDisplay *disp,
389 _EGLContext *ctx, EGLenum target,
390 EGLClientBuffer buffer, const EGLint *attr_list);
391
392 _EGLImage *
393 dri2_create_image_dma_buf(_EGLDisplay *disp, _EGLContext *ctx,
394 EGLClientBuffer buffer, const EGLint *attr_list);
395
396 EGLBoolean
397 dri2_initialize_x11(_EGLDriver *drv, _EGLDisplay *disp);
398
399 EGLBoolean
400 dri2_initialize_drm(_EGLDriver *drv, _EGLDisplay *disp);
401
402 EGLBoolean
403 dri2_initialize_wayland(_EGLDriver *drv, _EGLDisplay *disp);
404
405 EGLBoolean
406 dri2_initialize_android(_EGLDriver *drv, _EGLDisplay *disp);
407
408 EGLBoolean
409 dri2_initialize_surfaceless(_EGLDriver *drv, _EGLDisplay *disp);
410
411 void
412 dri2_flush_drawable_for_swapbuffers(_EGLDisplay *disp, _EGLSurface *draw);
413
414 const __DRIconfig *
415 dri2_get_dri_config(struct dri2_egl_config *conf, EGLint surface_type,
416 EGLenum colorspace);
417
418 static inline void
419 dri2_set_WL_bind_wayland_display(_EGLDriver *drv, _EGLDisplay *disp)
420 {
421 #ifdef HAVE_WAYLAND_PLATFORM
422 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
423
424 (void) drv;
425
426 if (dri2_dpy->device_name && dri2_dpy->image) {
427 if (dri2_dpy->image->base.version >= 10 &&
428 dri2_dpy->image->getCapabilities != NULL) {
429 int capabilities;
430
431 capabilities =
432 dri2_dpy->image->getCapabilities(dri2_dpy->dri_screen);
433 disp->Extensions.WL_bind_wayland_display =
434 (capabilities & __DRI_IMAGE_CAP_GLOBAL_NAMES) != 0;
435 } else {
436 disp->Extensions.WL_bind_wayland_display = EGL_TRUE;
437 }
438 }
439 #endif
440 }
441
442 void
443 dri2_display_destroy(_EGLDisplay *disp);
444
445 #endif /* EGL_DRI2_INCLUDED */