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