egl/dri2: move wayland header inclusion where applicable
[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 /* forward declarations to avoid pulling wayland headers everywhere */
47 struct wl_egl_window;
48 struct wl_event_queue;
49 struct wl_callback;
50 struct wl_display;
51 struct wl_drm;
52 struct wl_registry;
53 struct wl_shm;
54 struct wl_surface;
55 struct zwp_linux_dmabuf_v1;
56 #endif
57
58 #include <GL/gl.h>
59 #include <GL/internal/dri_interface.h>
60
61 #ifdef HAVE_DRM_PLATFORM
62 #include <gbm_driint.h>
63 #endif
64
65 #ifdef HAVE_ANDROID_PLATFORM
66 #define LOG_TAG "EGL-DRI2"
67
68 #include <system/window.h>
69 #include <hardware/gralloc.h>
70 #include <gralloc_drm_handle.h>
71
72 #endif /* HAVE_ANDROID_PLATFORM */
73
74 #include "eglconfig.h"
75 #include "eglcontext.h"
76 #include "egldisplay.h"
77 #include "egldriver.h"
78 #include "eglcurrent.h"
79 #include "egllog.h"
80 #include "eglsurface.h"
81 #include "eglimage.h"
82 #include "eglsync.h"
83
84 #include "util/u_vector.h"
85
86 struct wl_buffer;
87
88 struct dri2_egl_display_vtbl {
89 int (*authenticate)(_EGLDisplay *disp, uint32_t id);
90
91 _EGLSurface* (*create_window_surface)(_EGLDriver *drv, _EGLDisplay *dpy,
92 _EGLConfig *config,
93 void *native_window,
94 const EGLint *attrib_list);
95
96 _EGLSurface* (*create_pixmap_surface)(_EGLDriver *drv, _EGLDisplay *dpy,
97 _EGLConfig *config,
98 void *native_pixmap,
99 const EGLint *attrib_list);
100
101 _EGLSurface* (*create_pbuffer_surface)(_EGLDriver *drv, _EGLDisplay *dpy,
102 _EGLConfig *config,
103 const EGLint *attrib_list);
104
105 EGLBoolean (*destroy_surface)(_EGLDriver *drv, _EGLDisplay *dpy,
106 _EGLSurface *surface);
107
108 EGLBoolean (*swap_interval)(_EGLDriver *drv, _EGLDisplay *dpy,
109 _EGLSurface *surf, EGLint interval);
110
111 _EGLImage* (*create_image)(_EGLDriver *drv, _EGLDisplay *dpy,
112 _EGLContext *ctx, EGLenum target,
113 EGLClientBuffer buffer,
114 const EGLint *attr_list);
115
116 EGLBoolean (*swap_buffers)(_EGLDriver *drv, _EGLDisplay *dpy,
117 _EGLSurface *surf);
118
119 EGLBoolean (*swap_buffers_with_damage)(_EGLDriver *drv, _EGLDisplay *dpy,
120 _EGLSurface *surface,
121 const EGLint *rects, EGLint n_rects);
122
123 EGLBoolean (*set_damage_region)(_EGLDriver *drv, _EGLDisplay *dpy,
124 _EGLSurface *surface,
125 const EGLint *rects, EGLint n_rects);
126
127 EGLBoolean (*swap_buffers_region)(_EGLDriver *drv, _EGLDisplay *dpy,
128 _EGLSurface *surf, EGLint numRects,
129 const EGLint *rects);
130
131 EGLBoolean (*post_sub_buffer)(_EGLDriver *drv, _EGLDisplay *dpy,
132 _EGLSurface *surf,
133 EGLint x, EGLint y,
134 EGLint width, EGLint height);
135
136 EGLBoolean (*copy_buffers)(_EGLDriver *drv, _EGLDisplay *dpy,
137 _EGLSurface *surf, void *native_pixmap_target);
138
139 EGLint (*query_buffer_age)(_EGLDriver *drv, _EGLDisplay *dpy,
140 _EGLSurface *surf);
141
142 EGLBoolean (*query_surface)(_EGLDriver *drv, _EGLDisplay *dpy,
143 _EGLSurface *surf, EGLint attribute,
144 EGLint *value);
145
146 struct wl_buffer* (*create_wayland_buffer_from_image)(
147 _EGLDriver *drv, _EGLDisplay *dpy, _EGLImage *img);
148
149 EGLBoolean (*get_sync_values)(_EGLDisplay *display, _EGLSurface *surface,
150 EGLuint64KHR *ust, EGLuint64KHR *msc,
151 EGLuint64KHR *sbc);
152
153 __DRIdrawable *(*get_dri_drawable)(_EGLSurface *surf);
154
155 void (*close_screen_notify)(_EGLDisplay *dpy);
156 };
157
158 struct dri2_egl_display
159 {
160 const struct dri2_egl_display_vtbl *vtbl;
161
162 int dri2_major;
163 int dri2_minor;
164 __DRIscreen *dri_screen;
165 bool own_dri_screen;
166 const __DRIconfig **driver_configs;
167 void *driver;
168 const __DRIcoreExtension *core;
169 const __DRIimageDriverExtension *image_driver;
170 const __DRIdri2Extension *dri2;
171 const __DRIswrastExtension *swrast;
172 const __DRI2flushExtension *flush;
173 const __DRI2flushControlExtension *flush_control;
174 const __DRItexBufferExtension *tex_buffer;
175 const __DRIimageExtension *image;
176 const __DRIrobustnessExtension *robustness;
177 const __DRInoErrorExtension *no_error;
178 const __DRI2configQueryExtension *config;
179 const __DRI2fenceExtension *fence;
180 const __DRI2blobExtension *blob;
181 const __DRI2rendererQueryExtension *rendererQuery;
182 const __DRI2interopExtension *interop;
183 int fd;
184
185 /* dri2_initialize/dri2_terminate increment/decrement this count, so does
186 * dri2_make_current (tracks if there are active contexts/surfaces). */
187 int ref_count;
188
189 bool own_device;
190 bool invalidate_available;
191 int min_swap_interval;
192 int max_swap_interval;
193 int default_swap_interval;
194 #ifdef HAVE_DRM_PLATFORM
195 struct gbm_dri_device *gbm_dri;
196 #endif
197
198 char *driver_name;
199
200 const __DRIextension **loader_extensions;
201 const __DRIextension **driver_extensions;
202
203 #ifdef HAVE_X11_PLATFORM
204 xcb_connection_t *conn;
205 xcb_screen_t *screen;
206 bool swap_available;
207 #ifdef HAVE_DRI3
208 bool multibuffers_available;
209 int dri3_major_version;
210 int dri3_minor_version;
211 int present_major_version;
212 int present_minor_version;
213 struct loader_dri3_extensions loader_dri3_ext;
214 #endif
215 #endif
216
217 #ifdef HAVE_WAYLAND_PLATFORM
218 struct wl_display *wl_dpy;
219 struct wl_display *wl_dpy_wrapper;
220 struct wl_registry *wl_registry;
221 struct wl_drm *wl_server_drm;
222 struct wl_drm *wl_drm;
223 struct wl_shm *wl_shm;
224 struct wl_event_queue *wl_queue;
225 struct zwp_linux_dmabuf_v1 *wl_dmabuf;
226 struct u_vector *wl_modifiers;
227 bool authenticated;
228 int formats;
229 uint32_t capabilities;
230 char *device_name;
231 #endif
232
233 #ifdef HAVE_ANDROID_PLATFORM
234 const gralloc_module_t *gralloc;
235 #endif
236
237 bool is_render_node;
238 bool is_different_gpu;
239 };
240
241 struct dri2_egl_context
242 {
243 _EGLContext base;
244 __DRIcontext *dri_context;
245 };
246
247 #ifdef HAVE_WAYLAND_PLATFORM
248 enum wayland_buffer_type {
249 WL_BUFFER_FRONT,
250 WL_BUFFER_BACK,
251 WL_BUFFER_THIRD,
252 WL_BUFFER_COUNT
253 };
254 #endif
255
256 struct dri2_egl_surface
257 {
258 _EGLSurface base;
259 __DRIdrawable *dri_drawable;
260 __DRIbuffer buffers[5];
261 bool have_fake_front;
262
263 #ifdef HAVE_X11_PLATFORM
264 xcb_drawable_t drawable;
265 xcb_xfixes_region_t region;
266 int depth;
267 int bytes_per_pixel;
268 xcb_gcontext_t gc;
269 xcb_gcontext_t swapgc;
270 #endif
271
272 #ifdef HAVE_WAYLAND_PLATFORM
273 struct wl_egl_window *wl_win;
274 int dx;
275 int dy;
276 struct wl_event_queue *wl_queue;
277 struct wl_surface *wl_surface_wrapper;
278 struct wl_display *wl_dpy_wrapper;
279 struct wl_drm *wl_drm_wrapper;
280 struct wl_callback *throttle_callback;
281 int format;
282 #endif
283
284 #ifdef HAVE_DRM_PLATFORM
285 struct gbm_dri_surface *gbm_surf;
286 #endif
287
288 /* EGL-owned buffers */
289 __DRIbuffer *local_buffers[__DRI_BUFFER_COUNT];
290
291 #if defined(HAVE_WAYLAND_PLATFORM) || defined(HAVE_DRM_PLATFORM)
292 struct {
293 #ifdef HAVE_WAYLAND_PLATFORM
294 struct wl_buffer *wl_buffer;
295 __DRIimage *dri_image;
296 /* for is_different_gpu case. NULL else */
297 __DRIimage *linear_copy;
298 /* for swrast */
299 void *data;
300 int data_size;
301 #endif
302 #ifdef HAVE_DRM_PLATFORM
303 struct gbm_bo *bo;
304 #endif
305 bool locked;
306 int age;
307 } color_buffers[4], *back, *current;
308 #endif
309
310 #ifdef HAVE_ANDROID_PLATFORM
311 struct ANativeWindow *window;
312 struct ANativeWindowBuffer *buffer;
313 __DRIimage *dri_image_back;
314 __DRIimage *dri_image_front;
315
316 /* Used to record all the buffers created by ANativeWindow and their ages.
317 * Usually Android uses at most triple buffers in ANativeWindow
318 * so hardcode the number of color_buffers to 3.
319 */
320 struct {
321 struct ANativeWindowBuffer *buffer;
322 int age;
323 } color_buffers[3], *back;
324 #endif
325
326 #if defined(HAVE_SURFACELESS_PLATFORM)
327 __DRIimage *front;
328 unsigned int visual;
329 #endif
330 int out_fence_fd;
331 EGLBoolean enable_out_fence;
332 };
333
334 struct dri2_egl_config
335 {
336 _EGLConfig base;
337 const __DRIconfig *dri_config[2][2];
338 };
339
340 struct dri2_egl_image
341 {
342 _EGLImage base;
343 __DRIimage *dri_image;
344 };
345
346 struct dri2_egl_sync {
347 _EGLSync base;
348 mtx_t mutex;
349 cnd_t cond;
350 int refcount;
351 void *fence;
352 };
353
354 /* From xmlpool/options.h, user exposed so should be stable */
355 #define DRI_CONF_VBLANK_NEVER 0
356 #define DRI_CONF_VBLANK_DEF_INTERVAL_0 1
357 #define DRI_CONF_VBLANK_DEF_INTERVAL_1 2
358 #define DRI_CONF_VBLANK_ALWAYS_SYNC 3
359
360 /* standard typecasts */
361 _EGL_DRIVER_STANDARD_TYPECASTS(dri2_egl)
362 _EGL_DRIVER_TYPECAST(dri2_egl_image, _EGLImage, obj)
363 _EGL_DRIVER_TYPECAST(dri2_egl_sync, _EGLSync, obj)
364
365 extern const __DRIimageLookupExtension image_lookup_extension;
366 extern const __DRIuseInvalidateExtension use_invalidate;
367 extern const __DRIbackgroundCallableExtension background_callable_extension;
368
369 EGLBoolean
370 dri2_load_driver(_EGLDisplay *disp);
371
372 /* Helper for platforms not using dri2_create_screen */
373 void
374 dri2_setup_screen(_EGLDisplay *disp);
375
376 void
377 dri2_setup_swap_interval(_EGLDisplay *disp, int max_swap_interval);
378
379 EGLBoolean
380 dri2_load_driver_swrast(_EGLDisplay *disp);
381
382 EGLBoolean
383 dri2_load_driver_dri3(_EGLDisplay *disp);
384
385 EGLBoolean
386 dri2_create_screen(_EGLDisplay *disp);
387
388 EGLBoolean
389 dri2_setup_extensions(_EGLDisplay *disp);
390
391 __DRIdrawable *
392 dri2_surface_get_dri_drawable(_EGLSurface *surf);
393
394 __DRIimage *
395 dri2_lookup_egl_image(__DRIscreen *screen, void *image, void *data);
396
397 struct dri2_egl_config *
398 dri2_add_config(_EGLDisplay *disp, const __DRIconfig *dri_config, int id,
399 EGLint surface_type, const EGLint *attr_list,
400 const unsigned int *rgba_masks);
401
402 _EGLImage *
403 dri2_create_image_khr(_EGLDriver *drv, _EGLDisplay *disp,
404 _EGLContext *ctx, EGLenum target,
405 EGLClientBuffer buffer, const EGLint *attr_list);
406
407 _EGLImage *
408 dri2_create_image_dma_buf(_EGLDisplay *disp, _EGLContext *ctx,
409 EGLClientBuffer buffer, const EGLint *attr_list);
410
411 #ifdef HAVE_X11_PLATFORM
412 EGLBoolean
413 dri2_initialize_x11(_EGLDriver *drv, _EGLDisplay *disp);
414 void
415 dri2_teardown_x11(struct dri2_egl_display *dri2_dpy);
416 #else
417 static inline EGLBoolean
418 dri2_initialize_x11(_EGLDriver *drv, _EGLDisplay *disp)
419 {
420 return _eglError(EGL_NOT_INITIALIZED, "X11 platform not built");
421 }
422 static inline void
423 dri2_teardown_x11(struct dri2_egl_display *dri2_dpy) {}
424 #endif
425
426 #ifdef HAVE_DRM_PLATFORM
427 EGLBoolean
428 dri2_initialize_drm(_EGLDriver *drv, _EGLDisplay *disp);
429 void
430 dri2_teardown_drm(struct dri2_egl_display *dri2_dpy);
431 #else
432 static inline EGLBoolean
433 dri2_initialize_drm(_EGLDriver *drv, _EGLDisplay *disp)
434 {
435 return _eglError(EGL_NOT_INITIALIZED, "GBM/DRM platform not built");
436 }
437 static inline void
438 dri2_teardown_drm(struct dri2_egl_display *dri2_dpy) {}
439 #endif
440
441 #ifdef HAVE_WAYLAND_PLATFORM
442 EGLBoolean
443 dri2_initialize_wayland(_EGLDriver *drv, _EGLDisplay *disp);
444 void
445 dri2_teardown_wayland(struct dri2_egl_display *dri2_dpy);
446 #else
447 static inline EGLBoolean
448 dri2_initialize_wayland(_EGLDriver *drv, _EGLDisplay *disp)
449 {
450 return _eglError(EGL_NOT_INITIALIZED, "Wayland platform not built");
451 }
452 static inline void
453 dri2_teardown_wayland(struct dri2_egl_display *dri2_dpy) {}
454 #endif
455
456 #ifdef HAVE_ANDROID_PLATFORM
457 EGLBoolean
458 dri2_initialize_android(_EGLDriver *drv, _EGLDisplay *disp);
459 #else
460 static inline EGLBoolean
461 dri2_initialize_android(_EGLDriver *drv, _EGLDisplay *disp)
462 {
463 return _eglError(EGL_NOT_INITIALIZED, "Android platform not built");
464 }
465 #endif
466
467 #ifdef HAVE_SURFACELESS_PLATFORM
468 EGLBoolean
469 dri2_initialize_surfaceless(_EGLDriver *drv, _EGLDisplay *disp);
470 #else
471 static inline EGLBoolean
472 dri2_initialize_surfaceless(_EGLDriver *drv, _EGLDisplay *disp)
473 {
474 return _eglError(EGL_NOT_INITIALIZED, "Surfaceless platform not built");
475 }
476 #endif
477
478 void
479 dri2_flush_drawable_for_swapbuffers(_EGLDisplay *disp, _EGLSurface *draw);
480
481 const __DRIconfig *
482 dri2_get_dri_config(struct dri2_egl_config *conf, EGLint surface_type,
483 EGLenum colorspace);
484
485 static inline void
486 dri2_set_WL_bind_wayland_display(_EGLDriver *drv, _EGLDisplay *disp)
487 {
488 #ifdef HAVE_WAYLAND_PLATFORM
489 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
490
491 (void) drv;
492
493 if (dri2_dpy->device_name && dri2_dpy->image) {
494 if (dri2_dpy->image->base.version >= 10 &&
495 dri2_dpy->image->getCapabilities != NULL) {
496 int capabilities;
497
498 capabilities =
499 dri2_dpy->image->getCapabilities(dri2_dpy->dri_screen);
500 disp->Extensions.WL_bind_wayland_display =
501 (capabilities & __DRI_IMAGE_CAP_GLOBAL_NAMES) != 0;
502 } else {
503 disp->Extensions.WL_bind_wayland_display = EGL_TRUE;
504 }
505 }
506 #endif
507 }
508
509 void
510 dri2_display_destroy(_EGLDisplay *disp);
511
512 __DRIbuffer *
513 dri2_egl_surface_alloc_local_buffer(struct dri2_egl_surface *dri2_surf,
514 unsigned int att, unsigned int format);
515
516 void
517 dri2_egl_surface_free_local_buffers(struct dri2_egl_surface *dri2_surf);
518
519 EGLBoolean
520 dri2_init_surface(_EGLSurface *surf, _EGLDisplay *dpy, EGLint type,
521 _EGLConfig *conf, const EGLint *attrib_list, EGLBoolean enable_out_fence);
522
523 void
524 dri2_fini_surface(_EGLSurface *surf);
525
526 #endif /* EGL_DRI2_INCLUDED */