egl: inline fallback for post_sub_buffer
[mesa.git] / src / egl / drivers / dri2 / platform_wayland.c
1 /*
2 * Copyright © 2011-2012 Intel Corporation
3 * Copyright © 2012 Collabora, Ltd.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
24 *
25 * Authors:
26 * Kristian Høgsberg <krh@bitplanet.net>
27 * Benjamin Franzke <benjaminfranzke@googlemail.com>
28 */
29
30 #include <stdint.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <limits.h>
34 #include <dlfcn.h>
35 #include <errno.h>
36 #include <unistd.h>
37 #include <fcntl.h>
38 #include <xf86drm.h>
39 #include "drm-uapi/drm_fourcc.h"
40 #include <sys/mman.h>
41
42 #include "egl_dri2.h"
43 #include "egl_dri2_fallbacks.h"
44 #include "loader.h"
45 #include "util/u_vector.h"
46 #include "util/anon_file.h"
47 #include "eglglobals.h"
48
49 #include <wayland-egl-backend.h>
50 #include <wayland-client.h>
51 #include "wayland-drm-client-protocol.h"
52 #include "linux-dmabuf-unstable-v1-client-protocol.h"
53
54 /* cheesy workaround until wayland 1.18 is released */
55 #if WAYLAND_VERSION_MAJOR > 1 || \
56 (WAYLAND_VERSION_MAJOR == 1 && WAYLAND_VERSION_MINOR < 18)
57 #define WL_SHM_FORMAT_ABGR16161616F 0x48344241
58 #define WL_SHM_FORMAT_XBGR16161616F 0x48344258
59 #endif
60
61 /*
62 * The index of entries in this table is used as a bitmask in
63 * dri2_dpy->formats, which tracks the formats supported by our server.
64 */
65 static const struct dri2_wl_visual {
66 const char *format_name;
67 uint32_t wl_drm_format;
68 uint32_t wl_shm_format;
69 int dri_image_format;
70 /* alt_dri_image_format is a substitute wl_buffer format to use for a
71 * wl-server unsupported dri_image_format, ie. some other dri_image_format in
72 * the table, of the same precision but with different channel ordering, or
73 * __DRI_IMAGE_FORMAT_NONE if an alternate format is not needed or supported.
74 * The code checks if alt_dri_image_format can be used as a fallback for a
75 * dri_image_format for a given wl-server implementation.
76 */
77 int alt_dri_image_format;
78 int bpp;
79 int rgba_shifts[4];
80 unsigned int rgba_sizes[4];
81 } dri2_wl_visuals[] = {
82 {
83 "ABGR16F",
84 WL_DRM_FORMAT_ABGR16F, WL_SHM_FORMAT_ABGR16161616F,
85 __DRI_IMAGE_FORMAT_ABGR16161616F, 0, 64,
86 { 0, 16, 32, 48 },
87 { 16, 16, 16, 16 },
88 },
89 {
90 "XBGR16F",
91 WL_DRM_FORMAT_XBGR16F, WL_SHM_FORMAT_XBGR16161616F,
92 __DRI_IMAGE_FORMAT_XBGR16161616F, 0, 64,
93 { 0, 16, 32, -1 },
94 { 16, 16, 16, 0 },
95 },
96 {
97 "XRGB2101010",
98 WL_DRM_FORMAT_XRGB2101010, WL_SHM_FORMAT_XRGB2101010,
99 __DRI_IMAGE_FORMAT_XRGB2101010, __DRI_IMAGE_FORMAT_XBGR2101010, 32,
100 { 20, 10, 0, -1 },
101 { 10, 10, 10, 0 },
102 },
103 {
104 "ARGB2101010",
105 WL_DRM_FORMAT_ARGB2101010, WL_SHM_FORMAT_ARGB2101010,
106 __DRI_IMAGE_FORMAT_ARGB2101010, __DRI_IMAGE_FORMAT_ABGR2101010, 32,
107 { 20, 10, 0, 30 },
108 { 10, 10, 10, 2 },
109 },
110 {
111 "XBGR2101010",
112 WL_DRM_FORMAT_XBGR2101010, WL_SHM_FORMAT_XBGR2101010,
113 __DRI_IMAGE_FORMAT_XBGR2101010, __DRI_IMAGE_FORMAT_XRGB2101010, 32,
114 { 0, 10, 20, -1 },
115 { 10, 10, 10, 0 },
116 },
117 {
118 "ABGR2101010",
119 WL_DRM_FORMAT_ABGR2101010, WL_SHM_FORMAT_ABGR2101010,
120 __DRI_IMAGE_FORMAT_ABGR2101010, __DRI_IMAGE_FORMAT_ARGB2101010, 32,
121 { 0, 10, 20, 30 },
122 { 10, 10, 10, 2 },
123 },
124 {
125 "XRGB8888",
126 WL_DRM_FORMAT_XRGB8888, WL_SHM_FORMAT_XRGB8888,
127 __DRI_IMAGE_FORMAT_XRGB8888, __DRI_IMAGE_FORMAT_NONE, 32,
128 { 16, 8, 0, -1 },
129 { 8, 8, 8, 0 },
130 },
131 {
132 "ARGB8888",
133 WL_DRM_FORMAT_ARGB8888, WL_SHM_FORMAT_ARGB8888,
134 __DRI_IMAGE_FORMAT_ARGB8888, __DRI_IMAGE_FORMAT_NONE, 32,
135 { 16, 8, 0, 24 },
136 { 8, 8, 8, 8 },
137 },
138 {
139 "RGB565",
140 WL_DRM_FORMAT_RGB565, WL_SHM_FORMAT_RGB565,
141 __DRI_IMAGE_FORMAT_RGB565, __DRI_IMAGE_FORMAT_NONE, 16,
142 { 11, 5, 0, -1 },
143 { 5, 6, 5, 0 },
144 },
145 };
146
147 static_assert(ARRAY_SIZE(dri2_wl_visuals) <= EGL_DRI2_MAX_FORMATS,
148 "dri2_egl_display::formats is not large enough for "
149 "the formats in dri2_wl_visuals");
150
151 static int
152 dri2_wl_visual_idx_from_config(struct dri2_egl_display *dri2_dpy,
153 const __DRIconfig *config)
154 {
155 int shifts[4];
156 unsigned int sizes[4];
157
158 dri2_get_shifts_and_sizes(dri2_dpy->core, config, shifts, sizes);
159
160 for (unsigned int i = 0; i < ARRAY_SIZE(dri2_wl_visuals); i++) {
161 const struct dri2_wl_visual *wl_visual = &dri2_wl_visuals[i];
162
163 if (shifts[0] == wl_visual->rgba_shifts[0] &&
164 shifts[1] == wl_visual->rgba_shifts[1] &&
165 shifts[2] == wl_visual->rgba_shifts[2] &&
166 shifts[3] == wl_visual->rgba_shifts[3] &&
167 sizes[0] == wl_visual->rgba_sizes[0] &&
168 sizes[1] == wl_visual->rgba_sizes[1] &&
169 sizes[2] == wl_visual->rgba_sizes[2] &&
170 sizes[3] == wl_visual->rgba_sizes[3]) {
171 return i;
172 }
173 }
174
175 return -1;
176 }
177
178 static int
179 dri2_wl_visual_idx_from_fourcc(uint32_t fourcc)
180 {
181 for (int i = 0; i < ARRAY_SIZE(dri2_wl_visuals); i++) {
182 /* wl_drm format codes overlap with DRIImage FourCC codes for all formats
183 * we support. */
184 if (dri2_wl_visuals[i].wl_drm_format == fourcc)
185 return i;
186 }
187
188 return -1;
189 }
190
191 static int
192 dri2_wl_visual_idx_from_dri_image_format(uint32_t dri_image_format)
193 {
194 for (int i = 0; i < ARRAY_SIZE(dri2_wl_visuals); i++) {
195 if (dri2_wl_visuals[i].dri_image_format == dri_image_format)
196 return i;
197 }
198
199 return -1;
200 }
201
202 static int
203 dri2_wl_visual_idx_from_shm_format(uint32_t shm_format)
204 {
205 for (int i = 0; i < ARRAY_SIZE(dri2_wl_visuals); i++) {
206 if (dri2_wl_visuals[i].wl_shm_format == shm_format)
207 return i;
208 }
209
210 return -1;
211 }
212
213 bool
214 dri2_wl_is_format_supported(void* user_data, uint32_t format)
215 {
216 _EGLDisplay *disp = (_EGLDisplay *) user_data;
217 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
218 int j = dri2_wl_visual_idx_from_fourcc(format);
219
220 if (j == -1)
221 return false;
222
223 for (int i = 0; dri2_dpy->driver_configs[i]; i++)
224 if (j == dri2_wl_visual_idx_from_config(dri2_dpy,
225 dri2_dpy->driver_configs[i]))
226 return true;
227
228 return false;
229 }
230
231 static int
232 roundtrip(struct dri2_egl_display *dri2_dpy)
233 {
234 return wl_display_roundtrip_queue(dri2_dpy->wl_dpy, dri2_dpy->wl_queue);
235 }
236
237 static void
238 wl_buffer_release(void *data, struct wl_buffer *buffer)
239 {
240 struct dri2_egl_surface *dri2_surf = data;
241 int i;
242
243 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); ++i)
244 if (dri2_surf->color_buffers[i].wl_buffer == buffer)
245 break;
246
247 assert (i < ARRAY_SIZE(dri2_surf->color_buffers));
248
249 if (dri2_surf->color_buffers[i].wl_release) {
250 wl_buffer_destroy(buffer);
251 dri2_surf->color_buffers[i].wl_release = false;
252 dri2_surf->color_buffers[i].wl_buffer = NULL;
253 }
254
255 dri2_surf->color_buffers[i].locked = false;
256 }
257
258 static const struct wl_buffer_listener wl_buffer_listener = {
259 .release = wl_buffer_release
260 };
261
262 static void
263 resize_callback(struct wl_egl_window *wl_win, void *data)
264 {
265 struct dri2_egl_surface *dri2_surf = data;
266 struct dri2_egl_display *dri2_dpy =
267 dri2_egl_display(dri2_surf->base.Resource.Display);
268
269 if (dri2_surf->base.Width == wl_win->width &&
270 dri2_surf->base.Height == wl_win->height)
271 return;
272
273 /* Update the surface size as soon as native window is resized; from user
274 * pov, this makes the effect that resize is done immediately after native
275 * window resize, without requiring to wait until the first draw.
276 *
277 * A more detailed and lengthy explanation can be found at
278 * https://lists.freedesktop.org/archives/mesa-dev/2018-June/196474.html
279 */
280 if (!dri2_surf->back) {
281 dri2_surf->base.Width = wl_win->width;
282 dri2_surf->base.Height = wl_win->height;
283 }
284 dri2_dpy->flush->invalidate(dri2_surf->dri_drawable);
285 }
286
287 static void
288 destroy_window_callback(void *data)
289 {
290 struct dri2_egl_surface *dri2_surf = data;
291 dri2_surf->wl_win = NULL;
292 }
293
294 static struct wl_surface *
295 get_wl_surface_proxy(struct wl_egl_window *window)
296 {
297 /* Version 3 of wl_egl_window introduced a version field at the same
298 * location where a pointer to wl_surface was stored. Thus, if
299 * window->version is dereferenceable, we've been given an older version of
300 * wl_egl_window, and window->version points to wl_surface */
301 if (_eglPointerIsDereferencable((void *)(window->version))) {
302 return wl_proxy_create_wrapper((void *)(window->version));
303 }
304 return wl_proxy_create_wrapper(window->surface);
305 }
306
307 /**
308 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
309 */
310 static _EGLSurface *
311 dri2_wl_create_window_surface(_EGLDriver *drv, _EGLDisplay *disp,
312 _EGLConfig *conf, void *native_window,
313 const EGLint *attrib_list)
314 {
315 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
316 struct dri2_egl_config *dri2_conf = dri2_egl_config(conf);
317 struct wl_egl_window *window = native_window;
318 struct dri2_egl_surface *dri2_surf;
319 int visual_idx;
320 const __DRIconfig *config;
321
322 dri2_surf = calloc(1, sizeof *dri2_surf);
323 if (!dri2_surf) {
324 _eglError(EGL_BAD_ALLOC, "dri2_create_surface");
325 return NULL;
326 }
327
328 if (!dri2_init_surface(&dri2_surf->base, disp, EGL_WINDOW_BIT, conf,
329 attrib_list, false, native_window))
330 goto cleanup_surf;
331
332 config = dri2_get_dri_config(dri2_conf, EGL_WINDOW_BIT,
333 dri2_surf->base.GLColorspace);
334
335 if (!config) {
336 _eglError(EGL_BAD_MATCH, "Unsupported surfacetype/colorspace configuration");
337 goto cleanup_surf;
338 }
339
340 dri2_surf->base.Width = window->width;
341 dri2_surf->base.Height = window->height;
342
343 visual_idx = dri2_wl_visual_idx_from_config(dri2_dpy, config);
344 assert(visual_idx != -1);
345
346 if (dri2_dpy->wl_dmabuf || dri2_dpy->wl_drm) {
347 dri2_surf->format = dri2_wl_visuals[visual_idx].wl_drm_format;
348 } else {
349 assert(dri2_dpy->wl_shm);
350 dri2_surf->format = dri2_wl_visuals[visual_idx].wl_shm_format;
351 }
352
353 dri2_surf->wl_queue = wl_display_create_queue(dri2_dpy->wl_dpy);
354 if (!dri2_surf->wl_queue) {
355 _eglError(EGL_BAD_ALLOC, "dri2_create_surface");
356 goto cleanup_surf;
357 }
358
359 if (dri2_dpy->wl_drm) {
360 dri2_surf->wl_drm_wrapper = wl_proxy_create_wrapper(dri2_dpy->wl_drm);
361 if (!dri2_surf->wl_drm_wrapper) {
362 _eglError(EGL_BAD_ALLOC, "dri2_create_surface");
363 goto cleanup_queue;
364 }
365 wl_proxy_set_queue((struct wl_proxy *)dri2_surf->wl_drm_wrapper,
366 dri2_surf->wl_queue);
367 }
368
369 dri2_surf->wl_dpy_wrapper = wl_proxy_create_wrapper(dri2_dpy->wl_dpy);
370 if (!dri2_surf->wl_dpy_wrapper) {
371 _eglError(EGL_BAD_ALLOC, "dri2_create_surface");
372 goto cleanup_drm;
373 }
374 wl_proxy_set_queue((struct wl_proxy *)dri2_surf->wl_dpy_wrapper,
375 dri2_surf->wl_queue);
376
377 dri2_surf->wl_surface_wrapper = get_wl_surface_proxy(window);
378 if (!dri2_surf->wl_surface_wrapper) {
379 _eglError(EGL_BAD_ALLOC, "dri2_create_surface");
380 goto cleanup_dpy_wrapper;
381 }
382 wl_proxy_set_queue((struct wl_proxy *)dri2_surf->wl_surface_wrapper,
383 dri2_surf->wl_queue);
384
385 dri2_surf->wl_win = window;
386 dri2_surf->wl_win->driver_private = dri2_surf;
387 dri2_surf->wl_win->destroy_window_callback = destroy_window_callback;
388 if (dri2_dpy->flush)
389 dri2_surf->wl_win->resize_callback = resize_callback;
390
391 if (!dri2_create_drawable(dri2_dpy, config, dri2_surf, dri2_surf))
392 goto cleanup_surf_wrapper;
393
394 dri2_surf->base.SwapInterval = dri2_dpy->default_swap_interval;
395
396 return &dri2_surf->base;
397
398 cleanup_surf_wrapper:
399 wl_proxy_wrapper_destroy(dri2_surf->wl_surface_wrapper);
400 cleanup_dpy_wrapper:
401 wl_proxy_wrapper_destroy(dri2_surf->wl_dpy_wrapper);
402 cleanup_drm:
403 if (dri2_surf->wl_drm_wrapper)
404 wl_proxy_wrapper_destroy(dri2_surf->wl_drm_wrapper);
405 cleanup_queue:
406 wl_event_queue_destroy(dri2_surf->wl_queue);
407 cleanup_surf:
408 free(dri2_surf);
409
410 return NULL;
411 }
412
413 static _EGLSurface *
414 dri2_wl_create_pixmap_surface(_EGLDriver *drv, _EGLDisplay *disp,
415 _EGLConfig *conf, void *native_window,
416 const EGLint *attrib_list)
417 {
418 /* From the EGL_EXT_platform_wayland spec, version 3:
419 *
420 * It is not valid to call eglCreatePlatformPixmapSurfaceEXT with a <dpy>
421 * that belongs to Wayland. Any such call fails and generates
422 * EGL_BAD_PARAMETER.
423 */
424 _eglError(EGL_BAD_PARAMETER, "cannot create EGL pixmap surfaces on "
425 "Wayland");
426 return NULL;
427 }
428
429 /**
430 * Called via eglDestroySurface(), drv->API.DestroySurface().
431 */
432 static EGLBoolean
433 dri2_wl_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
434 {
435 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
436 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
437
438 (void) drv;
439
440 dri2_dpy->core->destroyDrawable(dri2_surf->dri_drawable);
441
442 for (int i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
443 if (dri2_surf->color_buffers[i].wl_buffer)
444 wl_buffer_destroy(dri2_surf->color_buffers[i].wl_buffer);
445 if (dri2_surf->color_buffers[i].dri_image)
446 dri2_dpy->image->destroyImage(dri2_surf->color_buffers[i].dri_image);
447 if (dri2_surf->color_buffers[i].linear_copy)
448 dri2_dpy->image->destroyImage(dri2_surf->color_buffers[i].linear_copy);
449 if (dri2_surf->color_buffers[i].data)
450 munmap(dri2_surf->color_buffers[i].data,
451 dri2_surf->color_buffers[i].data_size);
452 }
453
454 if (dri2_dpy->dri2)
455 dri2_egl_surface_free_local_buffers(dri2_surf);
456
457 if (dri2_surf->throttle_callback)
458 wl_callback_destroy(dri2_surf->throttle_callback);
459
460 if (dri2_surf->wl_win) {
461 dri2_surf->wl_win->driver_private = NULL;
462 dri2_surf->wl_win->resize_callback = NULL;
463 dri2_surf->wl_win->destroy_window_callback = NULL;
464 }
465
466 wl_proxy_wrapper_destroy(dri2_surf->wl_surface_wrapper);
467 wl_proxy_wrapper_destroy(dri2_surf->wl_dpy_wrapper);
468 if (dri2_surf->wl_drm_wrapper)
469 wl_proxy_wrapper_destroy(dri2_surf->wl_drm_wrapper);
470 wl_event_queue_destroy(dri2_surf->wl_queue);
471
472 dri2_fini_surface(surf);
473 free(surf);
474
475 return EGL_TRUE;
476 }
477
478 static void
479 dri2_wl_release_buffers(struct dri2_egl_surface *dri2_surf)
480 {
481 struct dri2_egl_display *dri2_dpy =
482 dri2_egl_display(dri2_surf->base.Resource.Display);
483
484 for (int i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
485 if (dri2_surf->color_buffers[i].wl_buffer) {
486 if (dri2_surf->color_buffers[i].locked) {
487 dri2_surf->color_buffers[i].wl_release = true;
488 } else {
489 wl_buffer_destroy(dri2_surf->color_buffers[i].wl_buffer);
490 dri2_surf->color_buffers[i].wl_buffer = NULL;
491 }
492 }
493 if (dri2_surf->color_buffers[i].dri_image)
494 dri2_dpy->image->destroyImage(dri2_surf->color_buffers[i].dri_image);
495 if (dri2_surf->color_buffers[i].linear_copy)
496 dri2_dpy->image->destroyImage(dri2_surf->color_buffers[i].linear_copy);
497 if (dri2_surf->color_buffers[i].data)
498 munmap(dri2_surf->color_buffers[i].data,
499 dri2_surf->color_buffers[i].data_size);
500
501 dri2_surf->color_buffers[i].dri_image = NULL;
502 dri2_surf->color_buffers[i].linear_copy = NULL;
503 dri2_surf->color_buffers[i].data = NULL;
504 }
505
506 if (dri2_dpy->dri2)
507 dri2_egl_surface_free_local_buffers(dri2_surf);
508 }
509
510 static int
511 get_back_bo(struct dri2_egl_surface *dri2_surf)
512 {
513 struct dri2_egl_display *dri2_dpy =
514 dri2_egl_display(dri2_surf->base.Resource.Display);
515 int use_flags;
516 int visual_idx;
517 unsigned int dri_image_format;
518 unsigned int linear_dri_image_format;
519 uint64_t *modifiers;
520 int num_modifiers;
521
522 visual_idx = dri2_wl_visual_idx_from_fourcc(dri2_surf->format);
523 assert(visual_idx != -1);
524 dri_image_format = dri2_wl_visuals[visual_idx].dri_image_format;
525 linear_dri_image_format = dri_image_format;
526 modifiers = u_vector_tail(&dri2_dpy->wl_modifiers[visual_idx]);
527 num_modifiers = u_vector_length(&dri2_dpy->wl_modifiers[visual_idx]);
528
529 if (num_modifiers == 1 && modifiers[0] == DRM_FORMAT_MOD_INVALID) {
530 /* For the purposes of this function, an INVALID modifier on its own
531 * means the modifiers aren't supported.
532 */
533 num_modifiers = 0;
534 }
535
536 /* Substitute dri image format if server does not support original format */
537 if (!BITSET_TEST(dri2_dpy->formats, visual_idx))
538 linear_dri_image_format = dri2_wl_visuals[visual_idx].alt_dri_image_format;
539
540 /* These asserts hold, as long as dri2_wl_visuals[] is self-consistent and
541 * the PRIME substitution logic in dri2_wl_add_configs_for_visuals() is free
542 * of bugs.
543 */
544 assert(linear_dri_image_format != __DRI_IMAGE_FORMAT_NONE);
545 assert(BITSET_TEST(dri2_dpy->formats,
546 dri2_wl_visual_idx_from_dri_image_format(linear_dri_image_format)));
547
548 /* There might be a buffer release already queued that wasn't processed */
549 wl_display_dispatch_queue_pending(dri2_dpy->wl_dpy, dri2_surf->wl_queue);
550
551 while (dri2_surf->back == NULL) {
552 for (int i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
553 /* Get an unlocked buffer, preferably one with a dri_buffer
554 * already allocated. */
555 if (dri2_surf->color_buffers[i].locked)
556 continue;
557 if (dri2_surf->back == NULL)
558 dri2_surf->back = &dri2_surf->color_buffers[i];
559 else if (dri2_surf->back->dri_image == NULL)
560 dri2_surf->back = &dri2_surf->color_buffers[i];
561 }
562
563 if (dri2_surf->back)
564 break;
565
566 /* If we don't have a buffer, then block on the server to release one for
567 * us, and try again. wl_display_dispatch_queue will process any pending
568 * events, however not all servers flush on issuing a buffer release
569 * event. So, we spam the server with roundtrips as they always cause a
570 * client flush.
571 */
572 if (wl_display_roundtrip_queue(dri2_dpy->wl_dpy,
573 dri2_surf->wl_queue) < 0)
574 return -1;
575 }
576
577 if (dri2_surf->back == NULL)
578 return -1;
579
580 use_flags = __DRI_IMAGE_USE_SHARE | __DRI_IMAGE_USE_BACKBUFFER;
581
582 if (dri2_dpy->is_different_gpu &&
583 dri2_surf->back->linear_copy == NULL) {
584 /* The LINEAR modifier should be a perfect alias of the LINEAR use
585 * flag; try the new interface first before the old, then fall back. */
586 if (dri2_dpy->image->base.version >= 15 &&
587 dri2_dpy->image->createImageWithModifiers) {
588 uint64_t linear_mod = DRM_FORMAT_MOD_LINEAR;
589
590 dri2_surf->back->linear_copy =
591 dri2_dpy->image->createImageWithModifiers(dri2_dpy->dri_screen,
592 dri2_surf->base.Width,
593 dri2_surf->base.Height,
594 linear_dri_image_format,
595 &linear_mod,
596 1,
597 NULL);
598 } else {
599 dri2_surf->back->linear_copy =
600 dri2_dpy->image->createImage(dri2_dpy->dri_screen,
601 dri2_surf->base.Width,
602 dri2_surf->base.Height,
603 linear_dri_image_format,
604 use_flags |
605 __DRI_IMAGE_USE_LINEAR,
606 NULL);
607 }
608 if (dri2_surf->back->linear_copy == NULL)
609 return -1;
610 }
611
612 if (dri2_surf->back->dri_image == NULL) {
613 /* If our DRIImage implementation does not support
614 * createImageWithModifiers, then fall back to the old createImage,
615 * and hope it allocates an image which is acceptable to the winsys.
616 */
617 if (num_modifiers && dri2_dpy->image->base.version >= 15 &&
618 dri2_dpy->image->createImageWithModifiers) {
619 dri2_surf->back->dri_image =
620 dri2_dpy->image->createImageWithModifiers(dri2_dpy->dri_screen,
621 dri2_surf->base.Width,
622 dri2_surf->base.Height,
623 dri_image_format,
624 modifiers,
625 num_modifiers,
626 NULL);
627 } else {
628 dri2_surf->back->dri_image =
629 dri2_dpy->image->createImage(dri2_dpy->dri_screen,
630 dri2_surf->base.Width,
631 dri2_surf->base.Height,
632 dri_image_format,
633 dri2_dpy->is_different_gpu ?
634 0 : use_flags,
635 NULL);
636 }
637
638 dri2_surf->back->age = 0;
639 }
640 if (dri2_surf->back->dri_image == NULL)
641 return -1;
642
643 dri2_surf->back->locked = true;
644
645 return 0;
646 }
647
648
649 static void
650 back_bo_to_dri_buffer(struct dri2_egl_surface *dri2_surf, __DRIbuffer *buffer)
651 {
652 struct dri2_egl_display *dri2_dpy =
653 dri2_egl_display(dri2_surf->base.Resource.Display);
654 __DRIimage *image;
655 int name, pitch;
656
657 image = dri2_surf->back->dri_image;
658
659 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_NAME, &name);
660 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_STRIDE, &pitch);
661
662 buffer->attachment = __DRI_BUFFER_BACK_LEFT;
663 buffer->name = name;
664 buffer->pitch = pitch;
665 buffer->cpp = 4;
666 buffer->flags = 0;
667 }
668
669 static int
670 update_buffers(struct dri2_egl_surface *dri2_surf)
671 {
672 struct dri2_egl_display *dri2_dpy =
673 dri2_egl_display(dri2_surf->base.Resource.Display);
674
675 if (dri2_surf->base.Width != dri2_surf->wl_win->width ||
676 dri2_surf->base.Height != dri2_surf->wl_win->height) {
677
678 dri2_surf->base.Width = dri2_surf->wl_win->width;
679 dri2_surf->base.Height = dri2_surf->wl_win->height;
680 dri2_surf->dx = dri2_surf->wl_win->dx;
681 dri2_surf->dy = dri2_surf->wl_win->dy;
682 }
683
684 if (dri2_surf->base.Width != dri2_surf->wl_win->attached_width ||
685 dri2_surf->base.Height != dri2_surf->wl_win->attached_height) {
686 dri2_wl_release_buffers(dri2_surf);
687 }
688
689 if (get_back_bo(dri2_surf) < 0) {
690 _eglError(EGL_BAD_ALLOC, "failed to allocate color buffer");
691 return -1;
692 }
693
694 /* If we have an extra unlocked buffer at this point, we had to do triple
695 * buffering for a while, but now can go back to just double buffering.
696 * That means we can free any unlocked buffer now. */
697 for (int i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
698 if (!dri2_surf->color_buffers[i].locked &&
699 dri2_surf->color_buffers[i].wl_buffer) {
700 wl_buffer_destroy(dri2_surf->color_buffers[i].wl_buffer);
701 dri2_dpy->image->destroyImage(dri2_surf->color_buffers[i].dri_image);
702 if (dri2_dpy->is_different_gpu)
703 dri2_dpy->image->destroyImage(dri2_surf->color_buffers[i].linear_copy);
704 dri2_surf->color_buffers[i].wl_buffer = NULL;
705 dri2_surf->color_buffers[i].dri_image = NULL;
706 dri2_surf->color_buffers[i].linear_copy = NULL;
707 }
708 }
709
710 return 0;
711 }
712
713 static int
714 update_buffers_if_needed(struct dri2_egl_surface *dri2_surf)
715 {
716 if (dri2_surf->back != NULL)
717 return 0;
718
719 return update_buffers(dri2_surf);
720 }
721
722 static __DRIbuffer *
723 dri2_wl_get_buffers_with_format(__DRIdrawable * driDrawable,
724 int *width, int *height,
725 unsigned int *attachments, int count,
726 int *out_count, void *loaderPrivate)
727 {
728 struct dri2_egl_surface *dri2_surf = loaderPrivate;
729 int i, j;
730
731 if (update_buffers(dri2_surf) < 0)
732 return NULL;
733
734 for (i = 0, j = 0; i < 2 * count; i += 2, j++) {
735 __DRIbuffer *local;
736
737 switch (attachments[i]) {
738 case __DRI_BUFFER_BACK_LEFT:
739 back_bo_to_dri_buffer(dri2_surf, &dri2_surf->buffers[j]);
740 break;
741 default:
742 local = dri2_egl_surface_alloc_local_buffer(dri2_surf, attachments[i],
743 attachments[i + 1]);
744
745 if (!local) {
746 _eglError(EGL_BAD_ALLOC, "failed to allocate local buffer");
747 return NULL;
748 }
749 dri2_surf->buffers[j] = *local;
750 break;
751 }
752 }
753
754 *out_count = j;
755 if (j == 0)
756 return NULL;
757
758 *width = dri2_surf->base.Width;
759 *height = dri2_surf->base.Height;
760
761 return dri2_surf->buffers;
762 }
763
764 static __DRIbuffer *
765 dri2_wl_get_buffers(__DRIdrawable * driDrawable,
766 int *width, int *height,
767 unsigned int *attachments, int count,
768 int *out_count, void *loaderPrivate)
769 {
770 struct dri2_egl_surface *dri2_surf = loaderPrivate;
771 unsigned int *attachments_with_format;
772 __DRIbuffer *buffer;
773 int visual_idx = dri2_wl_visual_idx_from_fourcc(dri2_surf->format);
774
775 if (visual_idx == -1)
776 return NULL;
777
778 attachments_with_format = calloc(count, 2 * sizeof(unsigned int));
779 if (!attachments_with_format) {
780 *out_count = 0;
781 return NULL;
782 }
783
784 for (int i = 0; i < count; ++i) {
785 attachments_with_format[2*i] = attachments[i];
786 attachments_with_format[2*i + 1] = dri2_wl_visuals[visual_idx].bpp;
787 }
788
789 buffer =
790 dri2_wl_get_buffers_with_format(driDrawable,
791 width, height,
792 attachments_with_format, count,
793 out_count, loaderPrivate);
794
795 free(attachments_with_format);
796
797 return buffer;
798 }
799
800 static int
801 image_get_buffers(__DRIdrawable *driDrawable,
802 unsigned int format,
803 uint32_t *stamp,
804 void *loaderPrivate,
805 uint32_t buffer_mask,
806 struct __DRIimageList *buffers)
807 {
808 struct dri2_egl_surface *dri2_surf = loaderPrivate;
809
810 if (update_buffers(dri2_surf) < 0)
811 return 0;
812
813 buffers->image_mask = __DRI_IMAGE_BUFFER_BACK;
814 buffers->back = dri2_surf->back->dri_image;
815
816 return 1;
817 }
818
819 static void
820 dri2_wl_flush_front_buffer(__DRIdrawable * driDrawable, void *loaderPrivate)
821 {
822 (void) driDrawable;
823 (void) loaderPrivate;
824 }
825
826 static unsigned
827 dri2_wl_get_capability(void *loaderPrivate, enum dri_loader_cap cap)
828 {
829 switch (cap) {
830 case DRI_LOADER_CAP_FP16:
831 return 1;
832 default:
833 return 0;
834 }
835 }
836
837 static const __DRIdri2LoaderExtension dri2_loader_extension = {
838 .base = { __DRI_DRI2_LOADER, 4 },
839
840 .getBuffers = dri2_wl_get_buffers,
841 .flushFrontBuffer = dri2_wl_flush_front_buffer,
842 .getBuffersWithFormat = dri2_wl_get_buffers_with_format,
843 .getCapability = dri2_wl_get_capability,
844 };
845
846 static const __DRIimageLoaderExtension image_loader_extension = {
847 .base = { __DRI_IMAGE_LOADER, 2 },
848
849 .getBuffers = image_get_buffers,
850 .flushFrontBuffer = dri2_wl_flush_front_buffer,
851 .getCapability = dri2_wl_get_capability,
852 };
853
854 static void
855 wayland_throttle_callback(void *data,
856 struct wl_callback *callback,
857 uint32_t time)
858 {
859 struct dri2_egl_surface *dri2_surf = data;
860
861 dri2_surf->throttle_callback = NULL;
862 wl_callback_destroy(callback);
863 }
864
865 static const struct wl_callback_listener throttle_listener = {
866 .done = wayland_throttle_callback
867 };
868
869 static EGLBoolean
870 get_fourcc(struct dri2_egl_display *dri2_dpy,
871 __DRIimage *image, int *fourcc)
872 {
873 EGLBoolean query;
874 int dri_format;
875 int visual_idx;
876
877 query = dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_FOURCC,
878 fourcc);
879 if (query)
880 return true;
881
882 query = dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_FORMAT,
883 &dri_format);
884 if (!query)
885 return false;
886
887 visual_idx = dri2_wl_visual_idx_from_dri_image_format(dri_format);
888 if (visual_idx == -1)
889 return false;
890
891 *fourcc = dri2_wl_visuals[visual_idx].wl_drm_format;
892 return true;
893 }
894
895 static struct wl_buffer *
896 create_wl_buffer(struct dri2_egl_display *dri2_dpy,
897 struct dri2_egl_surface *dri2_surf,
898 __DRIimage *image)
899 {
900 struct wl_buffer *ret;
901 EGLBoolean query;
902 int width, height, fourcc, num_planes;
903 uint64_t modifier = DRM_FORMAT_MOD_INVALID;
904
905 query = dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_WIDTH, &width);
906 query &= dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_HEIGHT,
907 &height);
908 query &= get_fourcc(dri2_dpy, image, &fourcc);
909 if (!query)
910 return NULL;
911
912 query = dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_NUM_PLANES,
913 &num_planes);
914 if (!query)
915 num_planes = 1;
916
917 if (dri2_dpy->image->base.version >= 15) {
918 int mod_hi, mod_lo;
919
920 query = dri2_dpy->image->queryImage(image,
921 __DRI_IMAGE_ATTRIB_MODIFIER_UPPER,
922 &mod_hi);
923 query &= dri2_dpy->image->queryImage(image,
924 __DRI_IMAGE_ATTRIB_MODIFIER_LOWER,
925 &mod_lo);
926 if (query) {
927 modifier = combine_u32_into_u64(mod_hi, mod_lo);
928 }
929 }
930
931 bool supported_modifier = false;
932 bool mod_invalid_supported = false;
933 int visual_idx = dri2_wl_visual_idx_from_fourcc(fourcc);
934 assert(visual_idx != -1);
935
936 uint64_t *mod;
937 u_vector_foreach(mod, &dri2_dpy->wl_modifiers[visual_idx]) {
938 if (*mod == DRM_FORMAT_MOD_INVALID) {
939 mod_invalid_supported = true;
940 }
941 if (*mod == modifier) {
942 supported_modifier = true;
943 break;
944 }
945 }
946 if (!supported_modifier && mod_invalid_supported) {
947 /* If the server has advertised DRM_FORMAT_MOD_INVALID then we trust
948 * that the client has allocated the buffer with the right implicit
949 * modifier for the format, even though it's allocated a buffer the
950 * server hasn't explicitly claimed to support. */
951 modifier = DRM_FORMAT_MOD_INVALID;
952 supported_modifier = true;
953 }
954
955 if (dri2_dpy->wl_dmabuf && supported_modifier) {
956 struct zwp_linux_buffer_params_v1 *params;
957 int i;
958
959 /* We don't need a wrapper for wl_dmabuf objects, because we have to
960 * create the intermediate params object; we can set the queue on this,
961 * and the wl_buffer inherits it race-free. */
962 params = zwp_linux_dmabuf_v1_create_params(dri2_dpy->wl_dmabuf);
963 if (dri2_surf)
964 wl_proxy_set_queue((struct wl_proxy *) params, dri2_surf->wl_queue);
965
966 for (i = 0; i < num_planes; i++) {
967 __DRIimage *p_image;
968 int stride, offset;
969 int fd = -1;
970
971 p_image = dri2_dpy->image->fromPlanar(image, i, NULL);
972 if (!p_image) {
973 assert(i == 0);
974 p_image = image;
975 }
976
977 query = dri2_dpy->image->queryImage(p_image,
978 __DRI_IMAGE_ATTRIB_FD,
979 &fd);
980 query &= dri2_dpy->image->queryImage(p_image,
981 __DRI_IMAGE_ATTRIB_STRIDE,
982 &stride);
983 query &= dri2_dpy->image->queryImage(p_image,
984 __DRI_IMAGE_ATTRIB_OFFSET,
985 &offset);
986 if (image != p_image)
987 dri2_dpy->image->destroyImage(p_image);
988
989 if (!query) {
990 if (fd >= 0)
991 close(fd);
992 zwp_linux_buffer_params_v1_destroy(params);
993 return NULL;
994 }
995
996 zwp_linux_buffer_params_v1_add(params, fd, i, offset, stride,
997 modifier >> 32, modifier & 0xffffffff);
998 close(fd);
999 }
1000
1001 ret = zwp_linux_buffer_params_v1_create_immed(params, width, height,
1002 fourcc, 0);
1003 zwp_linux_buffer_params_v1_destroy(params);
1004 } else if (dri2_dpy->capabilities & WL_DRM_CAPABILITY_PRIME) {
1005 struct wl_drm *wl_drm =
1006 dri2_surf ? dri2_surf->wl_drm_wrapper : dri2_dpy->wl_drm;
1007 int fd, stride;
1008
1009 if (num_planes > 1)
1010 return NULL;
1011
1012 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_FD, &fd);
1013 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_STRIDE, &stride);
1014 ret = wl_drm_create_prime_buffer(wl_drm, fd, width, height, fourcc, 0,
1015 stride, 0, 0, 0, 0);
1016 close(fd);
1017 } else {
1018 struct wl_drm *wl_drm =
1019 dri2_surf ? dri2_surf->wl_drm_wrapper : dri2_dpy->wl_drm;
1020 int name, stride;
1021
1022 if (num_planes > 1)
1023 return NULL;
1024
1025 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_NAME, &name);
1026 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_STRIDE, &stride);
1027 ret = wl_drm_create_buffer(wl_drm, name, width, height, stride, fourcc);
1028 }
1029
1030 return ret;
1031 }
1032
1033 static EGLBoolean
1034 try_damage_buffer(struct dri2_egl_surface *dri2_surf,
1035 const EGLint *rects,
1036 EGLint n_rects)
1037 {
1038 if (wl_proxy_get_version((struct wl_proxy *) dri2_surf->wl_surface_wrapper)
1039 < WL_SURFACE_DAMAGE_BUFFER_SINCE_VERSION)
1040 return EGL_FALSE;
1041
1042 for (int i = 0; i < n_rects; i++) {
1043 const int *rect = &rects[i * 4];
1044
1045 wl_surface_damage_buffer(dri2_surf->wl_surface_wrapper,
1046 rect[0],
1047 dri2_surf->base.Height - rect[1] - rect[3],
1048 rect[2], rect[3]);
1049 }
1050 return EGL_TRUE;
1051 }
1052
1053 /**
1054 * Called via eglSwapBuffers(), drv->API.SwapBuffers().
1055 */
1056 static EGLBoolean
1057 dri2_wl_swap_buffers_with_damage(_EGLDriver *drv,
1058 _EGLDisplay *disp,
1059 _EGLSurface *draw,
1060 const EGLint *rects,
1061 EGLint n_rects)
1062 {
1063 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1064 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
1065
1066 while (dri2_surf->throttle_callback != NULL)
1067 if (wl_display_dispatch_queue(dri2_dpy->wl_dpy,
1068 dri2_surf->wl_queue) == -1)
1069 return -1;
1070
1071 for (int i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++)
1072 if (dri2_surf->color_buffers[i].age > 0)
1073 dri2_surf->color_buffers[i].age++;
1074
1075 /* Make sure we have a back buffer in case we're swapping without ever
1076 * rendering. */
1077 if (update_buffers_if_needed(dri2_surf) < 0)
1078 return _eglError(EGL_BAD_ALLOC, "dri2_swap_buffers");
1079
1080 if (draw->SwapInterval > 0) {
1081 dri2_surf->throttle_callback =
1082 wl_surface_frame(dri2_surf->wl_surface_wrapper);
1083 wl_callback_add_listener(dri2_surf->throttle_callback,
1084 &throttle_listener, dri2_surf);
1085 }
1086
1087 dri2_surf->back->age = 1;
1088 dri2_surf->current = dri2_surf->back;
1089 dri2_surf->back = NULL;
1090
1091 if (!dri2_surf->current->wl_buffer) {
1092 __DRIimage *image;
1093
1094 if (dri2_dpy->is_different_gpu)
1095 image = dri2_surf->current->linear_copy;
1096 else
1097 image = dri2_surf->current->dri_image;
1098
1099 dri2_surf->current->wl_buffer =
1100 create_wl_buffer(dri2_dpy, dri2_surf, image);
1101
1102 dri2_surf->current->wl_release = false;
1103
1104 wl_buffer_add_listener(dri2_surf->current->wl_buffer,
1105 &wl_buffer_listener, dri2_surf);
1106 }
1107
1108 wl_surface_attach(dri2_surf->wl_surface_wrapper,
1109 dri2_surf->current->wl_buffer,
1110 dri2_surf->dx, dri2_surf->dy);
1111
1112 dri2_surf->wl_win->attached_width = dri2_surf->base.Width;
1113 dri2_surf->wl_win->attached_height = dri2_surf->base.Height;
1114 /* reset resize growing parameters */
1115 dri2_surf->dx = 0;
1116 dri2_surf->dy = 0;
1117
1118 /* If the compositor doesn't support damage_buffer, we deliberately
1119 * ignore the damage region and post maximum damage, due to
1120 * https://bugs.freedesktop.org/78190 */
1121 if (!n_rects || !try_damage_buffer(dri2_surf, rects, n_rects))
1122 wl_surface_damage(dri2_surf->wl_surface_wrapper,
1123 0, 0, INT32_MAX, INT32_MAX);
1124
1125 if (dri2_dpy->is_different_gpu) {
1126 _EGLContext *ctx = _eglGetCurrentContext();
1127 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
1128 dri2_dpy->image->blitImage(dri2_ctx->dri_context,
1129 dri2_surf->current->linear_copy,
1130 dri2_surf->current->dri_image,
1131 0, 0, dri2_surf->base.Width,
1132 dri2_surf->base.Height,
1133 0, 0, dri2_surf->base.Width,
1134 dri2_surf->base.Height, 0);
1135 }
1136
1137 dri2_flush_drawable_for_swapbuffers(disp, draw);
1138 dri2_dpy->flush->invalidate(dri2_surf->dri_drawable);
1139
1140 wl_surface_commit(dri2_surf->wl_surface_wrapper);
1141
1142 /* If we're not waiting for a frame callback then we'll at least throttle
1143 * to a sync callback so that we always give a chance for the compositor to
1144 * handle the commit and send a release event before checking for a free
1145 * buffer */
1146 if (dri2_surf->throttle_callback == NULL) {
1147 dri2_surf->throttle_callback = wl_display_sync(dri2_surf->wl_dpy_wrapper);
1148 wl_callback_add_listener(dri2_surf->throttle_callback,
1149 &throttle_listener, dri2_surf);
1150 }
1151
1152 wl_display_flush(dri2_dpy->wl_dpy);
1153
1154 return EGL_TRUE;
1155 }
1156
1157 static EGLint
1158 dri2_wl_query_buffer_age(_EGLDriver *drv,
1159 _EGLDisplay *disp, _EGLSurface *surface)
1160 {
1161 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surface);
1162
1163 if (update_buffers_if_needed(dri2_surf) < 0) {
1164 _eglError(EGL_BAD_ALLOC, "dri2_query_buffer_age");
1165 return -1;
1166 }
1167
1168 return dri2_surf->back->age;
1169 }
1170
1171 static EGLBoolean
1172 dri2_wl_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
1173 {
1174 return dri2_wl_swap_buffers_with_damage(drv, disp, draw, NULL, 0);
1175 }
1176
1177 static struct wl_buffer *
1178 dri2_wl_create_wayland_buffer_from_image(_EGLDriver *drv,
1179 _EGLDisplay *disp,
1180 _EGLImage *img)
1181 {
1182 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1183 struct dri2_egl_image *dri2_img = dri2_egl_image(img);
1184 __DRIimage *image = dri2_img->dri_image;
1185 struct wl_buffer *buffer;
1186 int format, visual_idx;
1187
1188 /* Check the upstream display supports this buffer's format. */
1189 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_FORMAT, &format);
1190 visual_idx = dri2_wl_visual_idx_from_dri_image_format(format);
1191 if (visual_idx == -1)
1192 goto bad_format;
1193
1194 if (!BITSET_TEST(dri2_dpy->formats, visual_idx))
1195 goto bad_format;
1196
1197 buffer = create_wl_buffer(dri2_dpy, NULL, image);
1198
1199 /* The buffer object will have been created with our internal event queue
1200 * because it is using wl_dmabuf/wl_drm as a proxy factory. We want the
1201 * buffer to be used by the application so we'll reset it to the display's
1202 * default event queue. This isn't actually racy, as the only event the
1203 * buffer can get is a buffer release, which doesn't happen with an explicit
1204 * attach. */
1205 if (buffer)
1206 wl_proxy_set_queue((struct wl_proxy *) buffer, NULL);
1207
1208 return buffer;
1209
1210 bad_format:
1211 _eglError(EGL_BAD_MATCH, "unsupported image format");
1212 return NULL;
1213 }
1214
1215 static int
1216 dri2_wl_authenticate(_EGLDisplay *disp, uint32_t id)
1217 {
1218 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1219 int ret = 0;
1220
1221 if (dri2_dpy->is_render_node) {
1222 _eglLog(_EGL_WARNING, "wayland-egl: client asks server to "
1223 "authenticate for render-nodes");
1224 return 0;
1225 }
1226 dri2_dpy->authenticated = false;
1227
1228 wl_drm_authenticate(dri2_dpy->wl_drm, id);
1229 if (roundtrip(dri2_dpy) < 0)
1230 ret = -1;
1231
1232 if (!dri2_dpy->authenticated)
1233 ret = -1;
1234
1235 /* reset authenticated */
1236 dri2_dpy->authenticated = true;
1237
1238 return ret;
1239 }
1240
1241 static void
1242 drm_handle_device(void *data, struct wl_drm *drm, const char *device)
1243 {
1244 struct dri2_egl_display *dri2_dpy = data;
1245 drm_magic_t magic;
1246
1247 dri2_dpy->device_name = strdup(device);
1248 if (!dri2_dpy->device_name)
1249 return;
1250
1251 dri2_dpy->fd = loader_open_device(dri2_dpy->device_name);
1252 if (dri2_dpy->fd == -1) {
1253 _eglLog(_EGL_WARNING, "wayland-egl: could not open %s (%s)",
1254 dri2_dpy->device_name, strerror(errno));
1255 free(dri2_dpy->device_name);
1256 dri2_dpy->device_name = NULL;
1257 return;
1258 }
1259
1260 if (drmGetNodeTypeFromFd(dri2_dpy->fd) == DRM_NODE_RENDER) {
1261 dri2_dpy->authenticated = true;
1262 } else {
1263 if (drmGetMagic(dri2_dpy->fd, &magic)) {
1264 close(dri2_dpy->fd);
1265 dri2_dpy->fd = -1;
1266 free(dri2_dpy->device_name);
1267 dri2_dpy->device_name = NULL;
1268 _eglLog(_EGL_WARNING, "wayland-egl: drmGetMagic failed");
1269 return;
1270 }
1271 wl_drm_authenticate(dri2_dpy->wl_drm, magic);
1272 }
1273 }
1274
1275 static void
1276 drm_handle_format(void *data, struct wl_drm *drm, uint32_t format)
1277 {
1278 struct dri2_egl_display *dri2_dpy = data;
1279 int visual_idx = dri2_wl_visual_idx_from_fourcc(format);
1280
1281 if (visual_idx == -1)
1282 return;
1283
1284 BITSET_SET(dri2_dpy->formats, visual_idx);
1285 }
1286
1287 static void
1288 drm_handle_capabilities(void *data, struct wl_drm *drm, uint32_t value)
1289 {
1290 struct dri2_egl_display *dri2_dpy = data;
1291
1292 dri2_dpy->capabilities = value;
1293 }
1294
1295 static void
1296 drm_handle_authenticated(void *data, struct wl_drm *drm)
1297 {
1298 struct dri2_egl_display *dri2_dpy = data;
1299
1300 dri2_dpy->authenticated = true;
1301 }
1302
1303 static const struct wl_drm_listener drm_listener = {
1304 .device = drm_handle_device,
1305 .format = drm_handle_format,
1306 .authenticated = drm_handle_authenticated,
1307 .capabilities = drm_handle_capabilities
1308 };
1309
1310 static void
1311 dmabuf_ignore_format(void *data, struct zwp_linux_dmabuf_v1 *dmabuf,
1312 uint32_t format)
1313 {
1314 /* formats are implicitly advertised by the 'modifier' event, so ignore */
1315 }
1316
1317 static void
1318 dmabuf_handle_modifier(void *data, struct zwp_linux_dmabuf_v1 *dmabuf,
1319 uint32_t format, uint32_t modifier_hi,
1320 uint32_t modifier_lo)
1321 {
1322 struct dri2_egl_display *dri2_dpy = data;
1323 int visual_idx = dri2_wl_visual_idx_from_fourcc(format);
1324 uint64_t *mod;
1325
1326 if (visual_idx == -1)
1327 return;
1328
1329 BITSET_SET(dri2_dpy->formats, visual_idx);
1330
1331 mod = u_vector_add(&dri2_dpy->wl_modifiers[visual_idx]);
1332 *mod = combine_u32_into_u64(modifier_hi, modifier_lo);
1333 }
1334
1335 static const struct zwp_linux_dmabuf_v1_listener dmabuf_listener = {
1336 .format = dmabuf_ignore_format,
1337 .modifier = dmabuf_handle_modifier,
1338 };
1339
1340 static void
1341 registry_handle_global_drm(void *data, struct wl_registry *registry,
1342 uint32_t name, const char *interface,
1343 uint32_t version)
1344 {
1345 struct dri2_egl_display *dri2_dpy = data;
1346
1347 if (strcmp(interface, "wl_drm") == 0) {
1348 dri2_dpy->wl_drm =
1349 wl_registry_bind(registry, name, &wl_drm_interface, MIN2(version, 2));
1350 wl_drm_add_listener(dri2_dpy->wl_drm, &drm_listener, dri2_dpy);
1351 } else if (strcmp(interface, "zwp_linux_dmabuf_v1") == 0 && version >= 3) {
1352 dri2_dpy->wl_dmabuf =
1353 wl_registry_bind(registry, name, &zwp_linux_dmabuf_v1_interface,
1354 MIN2(version, 3));
1355 zwp_linux_dmabuf_v1_add_listener(dri2_dpy->wl_dmabuf, &dmabuf_listener,
1356 dri2_dpy);
1357 }
1358 }
1359
1360 static void
1361 registry_handle_global_remove(void *data, struct wl_registry *registry,
1362 uint32_t name)
1363 {
1364 }
1365
1366 static const struct wl_registry_listener registry_listener_drm = {
1367 .global = registry_handle_global_drm,
1368 .global_remove = registry_handle_global_remove
1369 };
1370
1371 static void
1372 dri2_wl_setup_swap_interval(_EGLDisplay *disp)
1373 {
1374 /* We can't use values greater than 1 on Wayland because we are using the
1375 * frame callback to synchronise the frame and the only way we be sure to
1376 * get a frame callback is to attach a new buffer. Therefore we can't just
1377 * sit drawing nothing to wait until the next ‘n’ frame callbacks */
1378
1379 dri2_setup_swap_interval(disp, 1);
1380 }
1381
1382 static const struct dri2_egl_display_vtbl dri2_wl_display_vtbl = {
1383 .authenticate = dri2_wl_authenticate,
1384 .create_window_surface = dri2_wl_create_window_surface,
1385 .create_pixmap_surface = dri2_wl_create_pixmap_surface,
1386 .destroy_surface = dri2_wl_destroy_surface,
1387 .create_image = dri2_create_image_khr,
1388 .swap_buffers = dri2_wl_swap_buffers,
1389 .swap_buffers_with_damage = dri2_wl_swap_buffers_with_damage,
1390 .copy_buffers = dri2_fallback_copy_buffers,
1391 .query_buffer_age = dri2_wl_query_buffer_age,
1392 .create_wayland_buffer_from_image = dri2_wl_create_wayland_buffer_from_image,
1393 .get_sync_values = dri2_fallback_get_sync_values,
1394 .get_dri_drawable = dri2_surface_get_dri_drawable,
1395 };
1396
1397 static const __DRIextension *dri2_loader_extensions[] = {
1398 &dri2_loader_extension.base,
1399 &image_loader_extension.base,
1400 &image_lookup_extension.base,
1401 &use_invalidate.base,
1402 NULL,
1403 };
1404
1405 static const __DRIextension *image_loader_extensions[] = {
1406 &image_loader_extension.base,
1407 &image_lookup_extension.base,
1408 &use_invalidate.base,
1409 NULL,
1410 };
1411
1412 static EGLBoolean
1413 dri2_wl_add_configs_for_visuals(_EGLDriver *drv, _EGLDisplay *disp)
1414 {
1415 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1416 unsigned int format_count[ARRAY_SIZE(dri2_wl_visuals)] = { 0 };
1417 unsigned int count = 0;
1418 bool assigned;
1419
1420 for (unsigned i = 0; dri2_dpy->driver_configs[i]; i++) {
1421 assigned = false;
1422
1423 for (unsigned j = 0; j < ARRAY_SIZE(dri2_wl_visuals); j++) {
1424 struct dri2_egl_config *dri2_conf;
1425
1426 if (!BITSET_TEST(dri2_dpy->formats, j))
1427 continue;
1428
1429 dri2_conf = dri2_add_config(disp, dri2_dpy->driver_configs[i],
1430 count + 1, EGL_WINDOW_BIT, NULL, dri2_wl_visuals[j].rgba_shifts, dri2_wl_visuals[j].rgba_sizes);
1431 if (dri2_conf) {
1432 if (dri2_conf->base.ConfigID == count + 1)
1433 count++;
1434 format_count[j]++;
1435 assigned = true;
1436 }
1437 }
1438
1439 if (!assigned && dri2_dpy->is_different_gpu) {
1440 struct dri2_egl_config *dri2_conf;
1441 int alt_dri_image_format, c, s;
1442
1443 /* No match for config. Try if we can blitImage convert to a visual */
1444 c = dri2_wl_visual_idx_from_config(dri2_dpy,
1445 dri2_dpy->driver_configs[i]);
1446
1447 if (c == -1)
1448 continue;
1449
1450 /* Find optimal target visual for blitImage conversion, if any. */
1451 alt_dri_image_format = dri2_wl_visuals[c].alt_dri_image_format;
1452 s = dri2_wl_visual_idx_from_dri_image_format(alt_dri_image_format);
1453
1454 if (s == -1 || !BITSET_TEST(dri2_dpy->formats, s))
1455 continue;
1456
1457 /* Visual s works for the Wayland server, and c can be converted into s
1458 * by our client gpu during PRIME blitImage conversion to a linear
1459 * wl_buffer, so add visual c as supported by the client renderer.
1460 */
1461 dri2_conf = dri2_add_config(disp, dri2_dpy->driver_configs[i],
1462 count + 1, EGL_WINDOW_BIT, NULL,
1463 dri2_wl_visuals[c].rgba_shifts,
1464 dri2_wl_visuals[c].rgba_sizes);
1465 if (dri2_conf) {
1466 if (dri2_conf->base.ConfigID == count + 1)
1467 count++;
1468 format_count[c]++;
1469 if (format_count[c] == 1)
1470 _eglLog(_EGL_DEBUG, "Client format %s to server format %s via "
1471 "PRIME blitImage.", dri2_wl_visuals[c].format_name,
1472 dri2_wl_visuals[s].format_name);
1473 }
1474 }
1475 }
1476
1477 for (unsigned i = 0; i < ARRAY_SIZE(format_count); i++) {
1478 if (!format_count[i]) {
1479 _eglLog(_EGL_DEBUG, "No DRI config supports native format %s",
1480 dri2_wl_visuals[i].format_name);
1481 }
1482 }
1483
1484 return (count != 0);
1485 }
1486
1487 static EGLBoolean
1488 dri2_initialize_wayland_drm(_EGLDriver *drv, _EGLDisplay *disp)
1489 {
1490 _EGLDevice *dev;
1491 struct dri2_egl_display *dri2_dpy;
1492
1493 dri2_dpy = calloc(1, sizeof *dri2_dpy);
1494 if (!dri2_dpy)
1495 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
1496
1497 dri2_dpy->fd = -1;
1498 disp->DriverData = (void *) dri2_dpy;
1499 if (disp->PlatformDisplay == NULL) {
1500 dri2_dpy->wl_dpy = wl_display_connect(NULL);
1501 if (dri2_dpy->wl_dpy == NULL)
1502 goto cleanup;
1503 dri2_dpy->own_device = true;
1504 } else {
1505 dri2_dpy->wl_dpy = disp->PlatformDisplay;
1506 }
1507
1508 dri2_dpy->wl_modifiers =
1509 calloc(ARRAY_SIZE(dri2_wl_visuals), sizeof(*dri2_dpy->wl_modifiers));
1510 if (!dri2_dpy->wl_modifiers)
1511 goto cleanup;
1512 for (int i = 0; i < ARRAY_SIZE(dri2_wl_visuals); i++) {
1513 if (!u_vector_init(&dri2_dpy->wl_modifiers[i], sizeof(uint64_t), 32))
1514 goto cleanup;
1515 }
1516
1517 dri2_dpy->wl_queue = wl_display_create_queue(dri2_dpy->wl_dpy);
1518
1519 dri2_dpy->wl_dpy_wrapper = wl_proxy_create_wrapper(dri2_dpy->wl_dpy);
1520 if (dri2_dpy->wl_dpy_wrapper == NULL)
1521 goto cleanup;
1522
1523 wl_proxy_set_queue((struct wl_proxy *) dri2_dpy->wl_dpy_wrapper,
1524 dri2_dpy->wl_queue);
1525
1526 if (dri2_dpy->own_device)
1527 wl_display_dispatch_pending(dri2_dpy->wl_dpy);
1528
1529 dri2_dpy->wl_registry = wl_display_get_registry(dri2_dpy->wl_dpy_wrapper);
1530 wl_registry_add_listener(dri2_dpy->wl_registry,
1531 &registry_listener_drm, dri2_dpy);
1532 if (roundtrip(dri2_dpy) < 0 || dri2_dpy->wl_drm == NULL)
1533 goto cleanup;
1534
1535 if (roundtrip(dri2_dpy) < 0 || dri2_dpy->fd == -1)
1536 goto cleanup;
1537
1538 if (roundtrip(dri2_dpy) < 0 || !dri2_dpy->authenticated)
1539 goto cleanup;
1540
1541 dri2_dpy->fd = loader_get_user_preferred_fd(dri2_dpy->fd,
1542 &dri2_dpy->is_different_gpu);
1543 dev = _eglAddDevice(dri2_dpy->fd, false);
1544 if (!dev) {
1545 _eglError(EGL_NOT_INITIALIZED, "DRI2: failed to find EGLDevice");
1546 goto cleanup;
1547 }
1548
1549 disp->Device = dev;
1550
1551 if (dri2_dpy->is_different_gpu) {
1552 free(dri2_dpy->device_name);
1553 dri2_dpy->device_name = loader_get_device_name_for_fd(dri2_dpy->fd);
1554 if (!dri2_dpy->device_name) {
1555 _eglError(EGL_BAD_ALLOC, "wayland-egl: failed to get device name "
1556 "for requested GPU");
1557 goto cleanup;
1558 }
1559 }
1560
1561 /* we have to do the check now, because loader_get_user_preferred_fd
1562 * will return a render-node when the requested gpu is different
1563 * to the server, but also if the client asks for the same gpu than
1564 * the server by requesting its pci-id */
1565 dri2_dpy->is_render_node = drmGetNodeTypeFromFd(dri2_dpy->fd) == DRM_NODE_RENDER;
1566
1567 dri2_dpy->driver_name = loader_get_driver_for_fd(dri2_dpy->fd);
1568 if (dri2_dpy->driver_name == NULL) {
1569 _eglError(EGL_BAD_ALLOC, "DRI2: failed to get driver name");
1570 goto cleanup;
1571 }
1572
1573 /* render nodes cannot use Gem names, and thus do not support
1574 * the __DRI_DRI2_LOADER extension */
1575 if (!dri2_dpy->is_render_node) {
1576 dri2_dpy->loader_extensions = dri2_loader_extensions;
1577 if (!dri2_load_driver(disp)) {
1578 _eglError(EGL_BAD_ALLOC, "DRI2: failed to load driver");
1579 goto cleanup;
1580 }
1581 } else {
1582 dri2_dpy->loader_extensions = image_loader_extensions;
1583 if (!dri2_load_driver_dri3(disp)) {
1584 _eglError(EGL_BAD_ALLOC, "DRI3: failed to load driver");
1585 goto cleanup;
1586 }
1587 }
1588
1589 if (!dri2_create_screen(disp))
1590 goto cleanup;
1591
1592 if (!dri2_setup_extensions(disp))
1593 goto cleanup;
1594
1595 dri2_setup_screen(disp);
1596
1597 dri2_wl_setup_swap_interval(disp);
1598
1599 /* To use Prime, we must have _DRI_IMAGE v7 at least.
1600 * createImageFromFds support indicates that Prime export/import
1601 * is supported by the driver. Fall back to
1602 * gem names if we don't have Prime support. */
1603
1604 if (dri2_dpy->image->base.version < 7 ||
1605 dri2_dpy->image->createImageFromFds == NULL)
1606 dri2_dpy->capabilities &= ~WL_DRM_CAPABILITY_PRIME;
1607
1608 /* We cannot use Gem names with render-nodes, only prime fds (dma-buf).
1609 * The server needs to accept them */
1610 if (dri2_dpy->is_render_node &&
1611 !(dri2_dpy->capabilities & WL_DRM_CAPABILITY_PRIME)) {
1612 _eglLog(_EGL_WARNING, "wayland-egl: display is not render-node capable");
1613 goto cleanup;
1614 }
1615
1616 if (dri2_dpy->is_different_gpu &&
1617 (dri2_dpy->image->base.version < 9 ||
1618 dri2_dpy->image->blitImage == NULL)) {
1619 _eglLog(_EGL_WARNING, "wayland-egl: Different GPU selected, but the "
1620 "Image extension in the driver is not "
1621 "compatible. Version 9 or later and blitImage() "
1622 "are required");
1623 goto cleanup;
1624 }
1625
1626 if (!dri2_wl_add_configs_for_visuals(drv, disp)) {
1627 _eglError(EGL_NOT_INITIALIZED, "DRI2: failed to add configs");
1628 goto cleanup;
1629 }
1630
1631 dri2_set_WL_bind_wayland_display(drv, disp);
1632 /* When cannot convert EGLImage to wl_buffer when on a different gpu,
1633 * because the buffer of the EGLImage has likely a tiling mode the server
1634 * gpu won't support. These is no way to check for now. Thus do not support the
1635 * extension */
1636 if (!dri2_dpy->is_different_gpu)
1637 disp->Extensions.WL_create_wayland_buffer_from_image = EGL_TRUE;
1638
1639 disp->Extensions.EXT_buffer_age = EGL_TRUE;
1640
1641 disp->Extensions.EXT_swap_buffers_with_damage = EGL_TRUE;
1642
1643 /* Fill vtbl last to prevent accidentally calling virtual function during
1644 * initialization.
1645 */
1646 dri2_dpy->vtbl = &dri2_wl_display_vtbl;
1647
1648 return EGL_TRUE;
1649
1650 cleanup:
1651 dri2_display_destroy(disp);
1652 return EGL_FALSE;
1653 }
1654
1655 static int
1656 dri2_wl_swrast_get_stride_for_format(int format, int w)
1657 {
1658 int visual_idx = dri2_wl_visual_idx_from_shm_format(format);
1659
1660 assume(visual_idx != -1);
1661
1662 return w * (dri2_wl_visuals[visual_idx].bpp / 8);
1663 }
1664
1665 static EGLBoolean
1666 dri2_wl_swrast_allocate_buffer(struct dri2_egl_surface *dri2_surf,
1667 int format, int w, int h,
1668 void **data, int *size,
1669 struct wl_buffer **buffer)
1670 {
1671 struct dri2_egl_display *dri2_dpy =
1672 dri2_egl_display(dri2_surf->base.Resource.Display);
1673 struct wl_shm_pool *pool;
1674 int fd, stride, size_map;
1675 void *data_map;
1676
1677 stride = dri2_wl_swrast_get_stride_for_format(format, w);
1678 size_map = h * stride;
1679
1680 /* Create a shareable buffer */
1681 fd = os_create_anonymous_file(size_map, NULL);
1682 if (fd < 0)
1683 return EGL_FALSE;
1684
1685 data_map = mmap(NULL, size_map, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
1686 if (data_map == MAP_FAILED) {
1687 close(fd);
1688 return EGL_FALSE;
1689 }
1690
1691 /* Share it in a wl_buffer */
1692 pool = wl_shm_create_pool(dri2_dpy->wl_shm, fd, size_map);
1693 wl_proxy_set_queue((struct wl_proxy *)pool, dri2_surf->wl_queue);
1694 *buffer = wl_shm_pool_create_buffer(pool, 0, w, h, stride, format);
1695 wl_shm_pool_destroy(pool);
1696 close(fd);
1697
1698 *data = data_map;
1699 *size = size_map;
1700 return EGL_TRUE;
1701 }
1702
1703 static int
1704 swrast_update_buffers(struct dri2_egl_surface *dri2_surf)
1705 {
1706 struct dri2_egl_display *dri2_dpy =
1707 dri2_egl_display(dri2_surf->base.Resource.Display);
1708
1709 /* we need to do the following operations only once per frame */
1710 if (dri2_surf->back)
1711 return 0;
1712
1713 if (dri2_surf->base.Width != dri2_surf->wl_win->width ||
1714 dri2_surf->base.Height != dri2_surf->wl_win->height) {
1715
1716 dri2_wl_release_buffers(dri2_surf);
1717
1718 dri2_surf->base.Width = dri2_surf->wl_win->width;
1719 dri2_surf->base.Height = dri2_surf->wl_win->height;
1720 dri2_surf->dx = dri2_surf->wl_win->dx;
1721 dri2_surf->dy = dri2_surf->wl_win->dy;
1722 dri2_surf->current = NULL;
1723 }
1724
1725 /* find back buffer */
1726
1727 /* There might be a buffer release already queued that wasn't processed */
1728 wl_display_dispatch_queue_pending(dri2_dpy->wl_dpy, dri2_surf->wl_queue);
1729
1730 /* try get free buffer already created */
1731 for (int i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
1732 if (!dri2_surf->color_buffers[i].locked &&
1733 dri2_surf->color_buffers[i].wl_buffer) {
1734 dri2_surf->back = &dri2_surf->color_buffers[i];
1735 break;
1736 }
1737 }
1738
1739 /* else choose any another free location */
1740 if (!dri2_surf->back) {
1741 for (int i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
1742 if (!dri2_surf->color_buffers[i].locked) {
1743 dri2_surf->back = &dri2_surf->color_buffers[i];
1744 if (!dri2_wl_swrast_allocate_buffer(dri2_surf,
1745 dri2_surf->format,
1746 dri2_surf->base.Width,
1747 dri2_surf->base.Height,
1748 &dri2_surf->back->data,
1749 &dri2_surf->back->data_size,
1750 &dri2_surf->back->wl_buffer)) {
1751 _eglError(EGL_BAD_ALLOC, "failed to allocate color buffer");
1752 return -1;
1753 }
1754 wl_buffer_add_listener(dri2_surf->back->wl_buffer,
1755 &wl_buffer_listener, dri2_surf);
1756 break;
1757 }
1758 }
1759 }
1760
1761 if (!dri2_surf->back) {
1762 _eglError(EGL_BAD_ALLOC, "failed to find free buffer");
1763 return -1;
1764 }
1765
1766 dri2_surf->back->locked = true;
1767
1768 /* If we have an extra unlocked buffer at this point, we had to do triple
1769 * buffering for a while, but now can go back to just double buffering.
1770 * That means we can free any unlocked buffer now. */
1771 for (int i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
1772 if (!dri2_surf->color_buffers[i].locked &&
1773 dri2_surf->color_buffers[i].wl_buffer) {
1774 wl_buffer_destroy(dri2_surf->color_buffers[i].wl_buffer);
1775 munmap(dri2_surf->color_buffers[i].data,
1776 dri2_surf->color_buffers[i].data_size);
1777 dri2_surf->color_buffers[i].wl_buffer = NULL;
1778 dri2_surf->color_buffers[i].data = NULL;
1779 }
1780 }
1781
1782 return 0;
1783 }
1784
1785 static void*
1786 dri2_wl_swrast_get_frontbuffer_data(struct dri2_egl_surface *dri2_surf)
1787 {
1788 /* if there has been a resize: */
1789 if (!dri2_surf->current)
1790 return NULL;
1791
1792 return dri2_surf->current->data;
1793 }
1794
1795 static void*
1796 dri2_wl_swrast_get_backbuffer_data(struct dri2_egl_surface *dri2_surf)
1797 {
1798 assert(dri2_surf->back);
1799 return dri2_surf->back->data;
1800 }
1801
1802 static void
1803 dri2_wl_swrast_commit_backbuffer(struct dri2_egl_surface *dri2_surf)
1804 {
1805 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dri2_surf->base.Resource.Display);
1806
1807 while (dri2_surf->throttle_callback != NULL)
1808 if (wl_display_dispatch_queue(dri2_dpy->wl_dpy,
1809 dri2_surf->wl_queue) == -1)
1810 return;
1811
1812 if (dri2_surf->base.SwapInterval > 0) {
1813 dri2_surf->throttle_callback =
1814 wl_surface_frame(dri2_surf->wl_surface_wrapper);
1815 wl_callback_add_listener(dri2_surf->throttle_callback,
1816 &throttle_listener, dri2_surf);
1817 }
1818
1819 dri2_surf->current = dri2_surf->back;
1820 dri2_surf->back = NULL;
1821
1822 wl_surface_attach(dri2_surf->wl_surface_wrapper,
1823 dri2_surf->current->wl_buffer,
1824 dri2_surf->dx, dri2_surf->dy);
1825
1826 dri2_surf->wl_win->attached_width = dri2_surf->base.Width;
1827 dri2_surf->wl_win->attached_height = dri2_surf->base.Height;
1828 /* reset resize growing parameters */
1829 dri2_surf->dx = 0;
1830 dri2_surf->dy = 0;
1831
1832 wl_surface_damage(dri2_surf->wl_surface_wrapper,
1833 0, 0, INT32_MAX, INT32_MAX);
1834 wl_surface_commit(dri2_surf->wl_surface_wrapper);
1835
1836 /* If we're not waiting for a frame callback then we'll at least throttle
1837 * to a sync callback so that we always give a chance for the compositor to
1838 * handle the commit and send a release event before checking for a free
1839 * buffer */
1840 if (dri2_surf->throttle_callback == NULL) {
1841 dri2_surf->throttle_callback = wl_display_sync(dri2_surf->wl_dpy_wrapper);
1842 wl_callback_add_listener(dri2_surf->throttle_callback,
1843 &throttle_listener, dri2_surf);
1844 }
1845
1846 wl_display_flush(dri2_dpy->wl_dpy);
1847 }
1848
1849 static void
1850 dri2_wl_swrast_get_drawable_info(__DRIdrawable * draw,
1851 int *x, int *y, int *w, int *h,
1852 void *loaderPrivate)
1853 {
1854 struct dri2_egl_surface *dri2_surf = loaderPrivate;
1855
1856 (void) swrast_update_buffers(dri2_surf);
1857 *x = 0;
1858 *y = 0;
1859 *w = dri2_surf->base.Width;
1860 *h = dri2_surf->base.Height;
1861 }
1862
1863 static void
1864 dri2_wl_swrast_get_image(__DRIdrawable * read,
1865 int x, int y, int w, int h,
1866 char *data, void *loaderPrivate)
1867 {
1868 struct dri2_egl_surface *dri2_surf = loaderPrivate;
1869 int copy_width = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, w);
1870 int x_offset = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, x);
1871 int src_stride = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, dri2_surf->base.Width);
1872 int dst_stride = copy_width;
1873 char *src, *dst;
1874
1875 src = dri2_wl_swrast_get_frontbuffer_data(dri2_surf);
1876 if (!src) {
1877 memset(data, 0, copy_width * h);
1878 return;
1879 }
1880
1881 assert(data != src);
1882 assert(copy_width <= src_stride);
1883
1884 src += x_offset;
1885 src += y * src_stride;
1886 dst = data;
1887
1888 if (copy_width > src_stride-x_offset)
1889 copy_width = src_stride-x_offset;
1890 if (h > dri2_surf->base.Height-y)
1891 h = dri2_surf->base.Height-y;
1892
1893 for (; h>0; h--) {
1894 memcpy(dst, src, copy_width);
1895 src += src_stride;
1896 dst += dst_stride;
1897 }
1898 }
1899
1900 static void
1901 dri2_wl_swrast_put_image2(__DRIdrawable * draw, int op,
1902 int x, int y, int w, int h, int stride,
1903 char *data, void *loaderPrivate)
1904 {
1905 struct dri2_egl_surface *dri2_surf = loaderPrivate;
1906 int copy_width = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, w);
1907 int dst_stride = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, dri2_surf->base.Width);
1908 int x_offset = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, x);
1909 char *src, *dst;
1910
1911 assert(copy_width <= stride);
1912
1913 (void) swrast_update_buffers(dri2_surf);
1914 dst = dri2_wl_swrast_get_backbuffer_data(dri2_surf);
1915
1916 /* partial copy, copy old content */
1917 if (copy_width < dst_stride)
1918 dri2_wl_swrast_get_image(draw, 0, 0,
1919 dri2_surf->base.Width, dri2_surf->base.Height,
1920 dst, loaderPrivate);
1921
1922 dst += x_offset;
1923 dst += y * dst_stride;
1924
1925 src = data;
1926
1927 /* drivers expect we do these checks (and some rely on it) */
1928 if (copy_width > dst_stride-x_offset)
1929 copy_width = dst_stride-x_offset;
1930 if (h > dri2_surf->base.Height-y)
1931 h = dri2_surf->base.Height-y;
1932
1933 for (; h>0; h--) {
1934 memcpy(dst, src, copy_width);
1935 src += stride;
1936 dst += dst_stride;
1937 }
1938 dri2_wl_swrast_commit_backbuffer(dri2_surf);
1939 }
1940
1941 static void
1942 dri2_wl_swrast_put_image(__DRIdrawable * draw, int op,
1943 int x, int y, int w, int h,
1944 char *data, void *loaderPrivate)
1945 {
1946 struct dri2_egl_surface *dri2_surf = loaderPrivate;
1947 int stride;
1948
1949 stride = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, w);
1950 dri2_wl_swrast_put_image2(draw, op, x, y, w, h,
1951 stride, data, loaderPrivate);
1952 }
1953
1954 static EGLBoolean
1955 dri2_wl_swrast_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
1956 {
1957 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1958 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
1959
1960 dri2_dpy->core->swapBuffers(dri2_surf->dri_drawable);
1961 return EGL_TRUE;
1962 }
1963
1964 static void
1965 shm_handle_format(void *data, struct wl_shm *shm, uint32_t format)
1966 {
1967 struct dri2_egl_display *dri2_dpy = data;
1968 int visual_idx = dri2_wl_visual_idx_from_shm_format(format);
1969
1970 if (visual_idx == -1)
1971 return;
1972
1973 BITSET_SET(dri2_dpy->formats, visual_idx);
1974 }
1975
1976 static const struct wl_shm_listener shm_listener = {
1977 .format = shm_handle_format
1978 };
1979
1980 static void
1981 registry_handle_global_swrast(void *data, struct wl_registry *registry,
1982 uint32_t name, const char *interface,
1983 uint32_t version)
1984 {
1985 struct dri2_egl_display *dri2_dpy = data;
1986
1987 if (strcmp(interface, "wl_shm") == 0) {
1988 dri2_dpy->wl_shm =
1989 wl_registry_bind(registry, name, &wl_shm_interface, 1);
1990 wl_shm_add_listener(dri2_dpy->wl_shm, &shm_listener, dri2_dpy);
1991 }
1992 }
1993
1994 static const struct wl_registry_listener registry_listener_swrast = {
1995 .global = registry_handle_global_swrast,
1996 .global_remove = registry_handle_global_remove
1997 };
1998
1999 static const struct dri2_egl_display_vtbl dri2_wl_swrast_display_vtbl = {
2000 .authenticate = NULL,
2001 .create_window_surface = dri2_wl_create_window_surface,
2002 .create_pixmap_surface = dri2_wl_create_pixmap_surface,
2003 .destroy_surface = dri2_wl_destroy_surface,
2004 .create_image = dri2_create_image_khr,
2005 .swap_buffers = dri2_wl_swrast_swap_buffers,
2006 .copy_buffers = dri2_fallback_copy_buffers,
2007 .query_buffer_age = dri2_fallback_query_buffer_age,
2008 .create_wayland_buffer_from_image = dri2_fallback_create_wayland_buffer_from_image,
2009 .get_sync_values = dri2_fallback_get_sync_values,
2010 .get_dri_drawable = dri2_surface_get_dri_drawable,
2011 };
2012
2013 static const __DRIswrastLoaderExtension swrast_loader_extension = {
2014 .base = { __DRI_SWRAST_LOADER, 2 },
2015
2016 .getDrawableInfo = dri2_wl_swrast_get_drawable_info,
2017 .putImage = dri2_wl_swrast_put_image,
2018 .getImage = dri2_wl_swrast_get_image,
2019 .putImage2 = dri2_wl_swrast_put_image2,
2020 };
2021
2022 static const __DRIextension *swrast_loader_extensions[] = {
2023 &swrast_loader_extension.base,
2024 &image_lookup_extension.base,
2025 NULL,
2026 };
2027
2028 static EGLBoolean
2029 dri2_initialize_wayland_swrast(_EGLDriver *drv, _EGLDisplay *disp)
2030 {
2031 _EGLDevice *dev;
2032 struct dri2_egl_display *dri2_dpy;
2033
2034 dri2_dpy = calloc(1, sizeof *dri2_dpy);
2035 if (!dri2_dpy)
2036 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
2037
2038 dri2_dpy->fd = -1;
2039 disp->DriverData = (void *) dri2_dpy;
2040 if (disp->PlatformDisplay == NULL) {
2041 dri2_dpy->wl_dpy = wl_display_connect(NULL);
2042 if (dri2_dpy->wl_dpy == NULL)
2043 goto cleanup;
2044 dri2_dpy->own_device = true;
2045 } else {
2046 dri2_dpy->wl_dpy = disp->PlatformDisplay;
2047 }
2048
2049 dev = _eglAddDevice(dri2_dpy->fd, true);
2050 if (!dev) {
2051 _eglError(EGL_NOT_INITIALIZED, "DRI2: failed to find EGLDevice");
2052 goto cleanup;
2053 }
2054
2055 disp->Device = dev;
2056
2057 dri2_dpy->wl_queue = wl_display_create_queue(dri2_dpy->wl_dpy);
2058
2059 dri2_dpy->wl_dpy_wrapper = wl_proxy_create_wrapper(dri2_dpy->wl_dpy);
2060 if (dri2_dpy->wl_dpy_wrapper == NULL)
2061 goto cleanup;
2062
2063 wl_proxy_set_queue((struct wl_proxy *) dri2_dpy->wl_dpy_wrapper,
2064 dri2_dpy->wl_queue);
2065
2066 if (dri2_dpy->own_device)
2067 wl_display_dispatch_pending(dri2_dpy->wl_dpy);
2068
2069 dri2_dpy->wl_registry = wl_display_get_registry(dri2_dpy->wl_dpy_wrapper);
2070 wl_registry_add_listener(dri2_dpy->wl_registry,
2071 &registry_listener_swrast, dri2_dpy);
2072
2073 if (roundtrip(dri2_dpy) < 0 || dri2_dpy->wl_shm == NULL)
2074 goto cleanup;
2075
2076 if (roundtrip(dri2_dpy) < 0 || !BITSET_TEST_RANGE(dri2_dpy->formats,
2077 0, EGL_DRI2_MAX_FORMATS))
2078 goto cleanup;
2079
2080 dri2_dpy->driver_name = strdup("swrast");
2081 if (!dri2_load_driver_swrast(disp))
2082 goto cleanup;
2083
2084 dri2_dpy->loader_extensions = swrast_loader_extensions;
2085
2086 if (!dri2_create_screen(disp))
2087 goto cleanup;
2088
2089 if (!dri2_setup_extensions(disp))
2090 goto cleanup;
2091
2092 dri2_setup_screen(disp);
2093
2094 dri2_wl_setup_swap_interval(disp);
2095
2096 if (!dri2_wl_add_configs_for_visuals(drv, disp)) {
2097 _eglError(EGL_NOT_INITIALIZED, "DRI2: failed to add configs");
2098 goto cleanup;
2099 }
2100
2101 /* Fill vtbl last to prevent accidentally calling virtual function during
2102 * initialization.
2103 */
2104 dri2_dpy->vtbl = &dri2_wl_swrast_display_vtbl;
2105
2106 return EGL_TRUE;
2107
2108 cleanup:
2109 dri2_display_destroy(disp);
2110 return EGL_FALSE;
2111 }
2112
2113 EGLBoolean
2114 dri2_initialize_wayland(_EGLDriver *drv, _EGLDisplay *disp)
2115 {
2116 EGLBoolean initialized = EGL_FALSE;
2117
2118 if (!disp->Options.ForceSoftware)
2119 initialized = dri2_initialize_wayland_drm(drv, disp);
2120
2121 if (!initialized)
2122 initialized = dri2_initialize_wayland_swrast(drv, disp);
2123
2124 return initialized;
2125
2126 }
2127
2128 void
2129 dri2_teardown_wayland(struct dri2_egl_display *dri2_dpy)
2130 {
2131 if (dri2_dpy->wl_drm)
2132 wl_drm_destroy(dri2_dpy->wl_drm);
2133 if (dri2_dpy->wl_dmabuf)
2134 zwp_linux_dmabuf_v1_destroy(dri2_dpy->wl_dmabuf);
2135 if (dri2_dpy->wl_shm)
2136 wl_shm_destroy(dri2_dpy->wl_shm);
2137 if (dri2_dpy->wl_registry)
2138 wl_registry_destroy(dri2_dpy->wl_registry);
2139 if (dri2_dpy->wl_queue)
2140 wl_event_queue_destroy(dri2_dpy->wl_queue);
2141 if (dri2_dpy->wl_dpy_wrapper)
2142 wl_proxy_wrapper_destroy(dri2_dpy->wl_dpy_wrapper);
2143
2144 for (int i = 0; dri2_dpy->wl_modifiers && i < ARRAY_SIZE(dri2_wl_visuals); i++)
2145 u_vector_finish(&dri2_dpy->wl_modifiers[i]);
2146 free(dri2_dpy->wl_modifiers);
2147
2148 if (dri2_dpy->own_device)
2149 wl_display_disconnect(dri2_dpy->wl_dpy);
2150 }