Debian Codegraph @

src/egl/drivers/dri2/platform_wayland.c

raw
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 <[email protected]>
27 * Benjamin Franzke <[email protected]>
28 */
29
30 #include <dlfcn.h>
31 #include <errno.h>
32 #include <fcntl.h>
33 #include <limits.h>
34 #include <stdint.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <unistd.h>
38 #include "util/libdrm.h"
39 #include "drm-uapi/drm_fourcc.h"
40 #include <sys/mman.h>
41 #include <vulkan/vulkan_core.h>
42 #include <vulkan/vulkan_wayland.h>
43
44 #include "util/anon_file.h"
45 #include "util/perf/cpu_trace.h"
46 #include "util/u_vector.h"
47 #include "util/format/u_formats.h"
48 #include "main/glconfig.h"
49 #include "pipe/p_screen.h"
50 #include "egl_dri2.h"
51 #include "eglglobals.h"
52 #include "kopper_interface.h"
53 #include "loader.h"
54 #include "loader_dri_helper.h"
55 #include "dri_screen.h"
56 #include "dri_util.h"
57 #include <loader_wayland_helper.h>
58
59 #include "linux-dmabuf-unstable-v1-client-protocol.h"
60 #ifdef HAVE_BIND_WL_DISPLAY
61 #include "wayland-drm-client-protocol.h"
62 #endif
63 #include <wayland-client.h>
64 #include <wayland-egl-backend.h>
65
66 /*
67 * The index of entries in this table is used as a bitmask in
68 * dri2_dpy->formats.formats_bitmap, which tracks the formats supported
69 * by our server.
70 */
71 static const struct dri2_wl_visual {
72 uint32_t wl_drm_format;
73 int pipe_format;
74 /* alt_pipe_format is a substitute wl_buffer format to use for a
75 * wl-server unsupported pipe_format, ie. some other pipe_format in
76 * the table, of the same precision but with different channel ordering, or
77 * PIPE_FORMAT_NONE if an alternate format is not needed or supported.
78 * The code checks if alt_pipe_format can be used as a fallback for a
79 * pipe_format for a given wl-server implementation.
80 */
81 int alt_pipe_format;
82 int opaque_wl_drm_format;
83 } dri2_wl_visuals[] = {
84 {
85 DRM_FORMAT_ABGR16161616F,
86 PIPE_FORMAT_R16G16B16A16_FLOAT,
87 PIPE_FORMAT_NONE,
88 DRM_FORMAT_XBGR16161616F,
89 },
90 {
91 DRM_FORMAT_XBGR16161616F,
92 PIPE_FORMAT_R16G16B16X16_FLOAT,
93 PIPE_FORMAT_NONE,
94 DRM_FORMAT_XBGR16161616F,
95 },
96 {
97 DRM_FORMAT_ABGR16161616,
98 PIPE_FORMAT_R16G16B16A16_UNORM,
99 PIPE_FORMAT_NONE,
100 DRM_FORMAT_XBGR16161616,
101 },
102 {
103 DRM_FORMAT_XBGR16161616,
104 PIPE_FORMAT_R16G16B16X16_UNORM,
105 PIPE_FORMAT_NONE,
106 DRM_FORMAT_XBGR16161616,
107 },
108 {
109 DRM_FORMAT_XRGB2101010,
110 PIPE_FORMAT_B10G10R10X2_UNORM,
111 PIPE_FORMAT_R10G10B10X2_UNORM,
112 DRM_FORMAT_XRGB2101010,
113 },
114 {
115 DRM_FORMAT_ARGB2101010,
116 PIPE_FORMAT_B10G10R10A2_UNORM,
117 PIPE_FORMAT_R10G10B10A2_UNORM,
118 DRM_FORMAT_XRGB2101010,
119 },
120 {
121 DRM_FORMAT_XBGR2101010,
122 PIPE_FORMAT_R10G10B10X2_UNORM,
123 PIPE_FORMAT_B10G10R10X2_UNORM,
124 DRM_FORMAT_XBGR2101010,
125 },
126 {
127 DRM_FORMAT_ABGR2101010,
128 PIPE_FORMAT_R10G10B10A2_UNORM,
129 PIPE_FORMAT_B10G10R10A2_UNORM,
130 DRM_FORMAT_XBGR2101010,
131 },
132 {
133 DRM_FORMAT_XRGB8888,
134 PIPE_FORMAT_BGRX8888_UNORM,
135 PIPE_FORMAT_NONE,
136 DRM_FORMAT_XRGB8888,
137 },
138 {
139 DRM_FORMAT_ARGB8888,
140 PIPE_FORMAT_BGRA8888_UNORM,
141 PIPE_FORMAT_NONE,
142 DRM_FORMAT_XRGB8888,
143 },
144 {
145 DRM_FORMAT_RGB888,
146 PIPE_FORMAT_B8G8R8_UNORM,
147 PIPE_FORMAT_NONE,
148 DRM_FORMAT_RGB888,
149 },
150 {
151 DRM_FORMAT_ABGR8888,
152 PIPE_FORMAT_RGBA8888_UNORM,
153 PIPE_FORMAT_NONE,
154 DRM_FORMAT_XBGR8888,
155 },
156 {
157 DRM_FORMAT_XBGR8888,
158 PIPE_FORMAT_RGBX8888_UNORM,
159 PIPE_FORMAT_NONE,
160 DRM_FORMAT_XBGR8888,
161 },
162 {
163 DRM_FORMAT_BGR888,
164 PIPE_FORMAT_R8G8B8_UNORM,
165 PIPE_FORMAT_NONE,
166 DRM_FORMAT_BGR888,
167 },
168 {
169 DRM_FORMAT_RGB565,
170 PIPE_FORMAT_B5G6R5_UNORM,
171 PIPE_FORMAT_NONE,
172 DRM_FORMAT_RGB565,
173 },
174 {
175 DRM_FORMAT_ARGB1555,
176 PIPE_FORMAT_B5G5R5A1_UNORM,
177 PIPE_FORMAT_R5G5B5A1_UNORM,
178 DRM_FORMAT_XRGB1555,
179 },
180 {
181 DRM_FORMAT_XRGB1555,
182 PIPE_FORMAT_B5G5R5X1_UNORM,
183 PIPE_FORMAT_R5G5B5X1_UNORM,
184 DRM_FORMAT_XRGB1555,
185 },
186 {
187 DRM_FORMAT_ARGB4444,
188 PIPE_FORMAT_B4G4R4A4_UNORM,
189 PIPE_FORMAT_R4G4B4A4_UNORM,
190 DRM_FORMAT_XRGB4444,
191 },
192 {
193 DRM_FORMAT_XRGB4444,
194 PIPE_FORMAT_B4G4R4X4_UNORM,
195 PIPE_FORMAT_R4G4B4X4_UNORM,
196 DRM_FORMAT_XRGB4444,
197 },
198 };
199
200 static int
201 dri2_wl_visual_idx_from_pipe_format(enum pipe_format pipe_format)
202 {
203 if (util_format_is_srgb(pipe_format))
204 pipe_format = util_format_linear(pipe_format);
205
206 for (int i = 0; i < ARRAY_SIZE(dri2_wl_visuals); i++) {
207 if (dri2_wl_visuals[i].pipe_format == pipe_format)
208 return i;
209 }
210
211 return -1;
212 }
213
214 static int
215 dri2_wl_visual_idx_from_config(const struct dri_config *config)
216 {
217 struct gl_config *gl_config = (struct gl_config *) config;
218
219 return dri2_wl_visual_idx_from_pipe_format(gl_config->color_format);
220 }
221
222 static int
223 dri2_wl_visual_idx_from_fourcc(uint32_t fourcc)
224 {
225 for (int i = 0; i < ARRAY_SIZE(dri2_wl_visuals); i++) {
226 /* wl_drm format codes overlap with DRIImage FourCC codes for all formats
227 * we support. */
228 if (dri2_wl_visuals[i].wl_drm_format == fourcc)
229 return i;
230 }
231
232 return -1;
233 }
234
235 static int
236 dri2_wl_shm_format_from_visual_idx(int idx)
237 {
238 uint32_t fourcc = dri2_wl_visuals[idx].wl_drm_format;
239
240 if (fourcc == DRM_FORMAT_ARGB8888)
241 return WL_SHM_FORMAT_ARGB8888;
242 else if (fourcc == DRM_FORMAT_XRGB8888)
243 return WL_SHM_FORMAT_XRGB8888;
244 else
245 return fourcc;
246 }
247
248 static int
249 dri2_wl_visual_idx_from_shm_format(uint32_t shm_format)
250 {
251 uint32_t fourcc;
252
253 if (shm_format == WL_SHM_FORMAT_ARGB8888)
254 fourcc = DRM_FORMAT_ARGB8888;
255 else if (shm_format == WL_SHM_FORMAT_XRGB8888)
256 fourcc = DRM_FORMAT_XRGB8888;
257 else
258 fourcc = shm_format;
259
260 return dri2_wl_visual_idx_from_fourcc(fourcc);
261 }
262
263 bool
264 dri2_wl_is_format_supported(void *user_data, uint32_t format)
265 {
266 _EGLDisplay *disp = (_EGLDisplay *)user_data;
267 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
268 int j = dri2_wl_visual_idx_from_fourcc(format);
269
270 if (j == -1)
271 return false;
272
273 for (int i = 0; dri2_dpy->driver_configs[i]; i++)
274 if (j == dri2_wl_visual_idx_from_config(dri2_dpy->driver_configs[i]))
275 return true;
276
277 return false;
278 }
279
280 static bool
281 server_supports_format(struct dri2_wl_formats *formats, int idx)
282 {
283 return idx >= 0 && BITSET_TEST(formats->formats_bitmap, idx);
284 }
285
286 static bool
287 server_supports_pipe_format(struct dri2_wl_formats *formats,
288 enum pipe_format format)
289 {
290 return server_supports_format(formats,
291 dri2_wl_visual_idx_from_pipe_format(format));
292 }
293
294 static bool
295 server_supports_fourcc(struct dri2_wl_formats *formats, uint32_t fourcc)
296 {
297 return server_supports_format(formats, dri2_wl_visual_idx_from_fourcc(fourcc));
298 }
299
300 static int
301 roundtrip(struct dri2_egl_display *dri2_dpy)
302 {
303 return wl_display_roundtrip_queue(dri2_dpy->wl_dpy, dri2_dpy->wl_queue);
304 }
305
306 static void
307 wl_buffer_release(void *data, struct wl_buffer *buffer)
308 {
309 struct dri2_egl_surface *dri2_surf = data;
310 int i;
311
312 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); ++i)
313 if (dri2_surf->color_buffers[i].wayland_buffer.buffer == buffer)
314 break;
315
316 assert(i < ARRAY_SIZE(dri2_surf->color_buffers));
317
318 if (dri2_surf->color_buffers[i].wl_release) {
319 loader_wayland_buffer_destroy(&dri2_surf->color_buffers[i].wayland_buffer);
320 dri2_surf->color_buffers[i].wl_release = false;
321 dri2_surf->color_buffers[i].wayland_buffer.buffer = NULL;
322 dri2_surf->color_buffers[i].age = 0;
323 }
324
325 dri2_surf->color_buffers[i].locked = false;
326 }
327
328 static const struct wl_buffer_listener wl_buffer_listener = {
329 .release = wl_buffer_release,
330 };
331
332 static void
333 dri2_wl_formats_fini(struct dri2_wl_formats *formats)
334 {
335 unsigned int i;
336
337 for (i = 0; i < formats->num_formats; i++)
338 u_vector_finish(&formats->modifiers[i]);
339
340 free(formats->modifiers);
341 free(formats->formats_bitmap);
342 }
343
344 static int
345 dri2_wl_formats_init(struct dri2_wl_formats *formats)
346 {
347 unsigned int i, j;
348
349 /* formats->formats_bitmap tells us if a format in dri2_wl_visuals is present
350 * or not. So we must compute the amount of unsigned int's needed to
351 * represent all the formats of dri2_wl_visuals. We use BITSET_WORDS for
352 * this task. */
353 formats->num_formats = ARRAY_SIZE(dri2_wl_visuals);
354 formats->formats_bitmap = BITSET_CALLOC(formats->num_formats);
355 if (!formats->formats_bitmap)
356 goto err;
357
358 /* Here we have an array of u_vector's to store the modifiers supported by
359 * each format in the bitmask. */
360 formats->modifiers =
361 calloc(formats->num_formats, sizeof(*formats->modifiers));
362 if (!formats->modifiers)
363 goto err_modifier;
364
365 for (i = 0; i < formats->num_formats; i++)
366 if (!u_vector_init_pow2(&formats->modifiers[i], 4, sizeof(uint64_t))) {
367 j = i;
368 goto err_vector_init;
369 }
370
371 return 0;
372
373 err_vector_init:
374 for (i = 0; i < j; i++)
375 u_vector_finish(&formats->modifiers[i]);
376 free(formats->modifiers);
377 err_modifier:
378 free(formats->formats_bitmap);
379 err:
380 _eglError(EGL_BAD_ALLOC, "dri2_wl_formats_init");
381 return -1;
382 }
383
384 static void
385 dmabuf_feedback_format_table_fini(
386 struct dmabuf_feedback_format_table *format_table)
387 {
388 if (format_table->data && format_table->data != MAP_FAILED)
389 munmap(format_table->data, format_table->size);
390 }
391
392 static void
393 dmabuf_feedback_format_table_init(
394 struct dmabuf_feedback_format_table *format_table)
395 {
396 memset(format_table, 0, sizeof(*format_table));
397 }
398
399 static void
400 dmabuf_feedback_tranche_fini(struct dmabuf_feedback_tranche *tranche)
401 {
402 dri2_wl_formats_fini(&tranche->formats);
403 }
404
405 static int
406 dmabuf_feedback_tranche_init(struct dmabuf_feedback_tranche *tranche)
407 {
408 memset(tranche, 0, sizeof(*tranche));
409
410 if (dri2_wl_formats_init(&tranche->formats) < 0)
411 return -1;
412
413 return 0;
414 }
415
416 static void
417 dmabuf_feedback_fini(struct dmabuf_feedback *dmabuf_feedback)
418 {
419 dmabuf_feedback_tranche_fini(&dmabuf_feedback->pending_tranche);
420
421 util_dynarray_foreach (&dmabuf_feedback->tranches,
422 struct dmabuf_feedback_tranche, tranche)
423 dmabuf_feedback_tranche_fini(tranche);
424 util_dynarray_fini(&dmabuf_feedback->tranches);
425
426 dmabuf_feedback_format_table_fini(&dmabuf_feedback->format_table);
427 }
428
429 static int
430 dmabuf_feedback_init(struct dmabuf_feedback *dmabuf_feedback)
431 {
432 memset(dmabuf_feedback, 0, sizeof(*dmabuf_feedback));
433
434 if (dmabuf_feedback_tranche_init(&dmabuf_feedback->pending_tranche) < 0)
435 return -1;
436
437 dmabuf_feedback->tranches = UTIL_DYNARRAY_INIT;
438
439 dmabuf_feedback_format_table_init(&dmabuf_feedback->format_table);
440
441 return 0;
442 }
443
444 static void
445 resize_callback(struct wl_egl_window *wl_win, void *data)
446 {
447 struct dri2_egl_surface *dri2_surf = data;
448
449 if (dri2_surf->base.Width == wl_win->width &&
450 dri2_surf->base.Height == wl_win->height)
451 return;
452
453 dri2_surf->resized = true;
454
455 /* Update the surface size as soon as native window is resized; from user
456 * pov, this makes the effect that resize is done immediately after native
457 * window resize, without requiring to wait until the first draw.
458 *
459 * A more detailed and lengthy explanation can be found at
460 * https://lists.freedesktop.org/archives/mesa-dev/2018-June/196474.html
461 */
462 if (!dri2_surf->back) {
463 dri2_surf->base.Width = wl_win->width;
464 dri2_surf->base.Height = wl_win->height;
465 }
466 dri_invalidate_drawable(dri2_surf->dri_drawable);
467 }
468
469 static void
470 destroy_window_callback(void *data)
471 {
472 struct dri2_egl_surface *dri2_surf = data;
473 dri2_surf->wl_win = NULL;
474 }
475
476 static bool
477 get_wayland_surface(struct dri2_egl_surface *dri2_surf,
478 struct wl_egl_window *window)
479 {
480 struct wl_surface *base_surface;
481
482 /* Version 3 of wl_egl_window introduced a version field at the same
483 * location where a pointer to wl_surface was stored. Thus, if
484 * window->version is dereferenceable, we've been given an older version of
485 * wl_egl_window, and window->version points to wl_surface */
486 if (_eglPointerIsDereferenceable((void *)(window->version)))
487 base_surface = (struct wl_surface *)window->version;
488 else
489 base_surface = window->surface;
490
491 return loader_wayland_wrap_surface(&dri2_surf->wayland_surface,
492 base_surface,
493 dri2_surf->wl_queue);
494 }
495
496 static void
497 surface_dmabuf_feedback_format_table(
498 void *data,
499 struct zwp_linux_dmabuf_feedback_v1 *zwp_linux_dmabuf_feedback_v1,
500 int32_t fd, uint32_t size)
501 {
502 struct dri2_egl_surface *dri2_surf = data;
503 struct dmabuf_feedback *feedback = &dri2_surf->pending_dmabuf_feedback;
504
505 feedback->format_table.size = size;
506 feedback->format_table.data =
507 mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
508
509 close(fd);
510 }
511
512 static void
513 surface_dmabuf_feedback_main_device(
514 void *data, struct zwp_linux_dmabuf_feedback_v1 *dmabuf_feedback,
515 struct wl_array *device)
516 {
517 struct dri2_egl_surface *dri2_surf = data;
518 struct dmabuf_feedback *feedback = &dri2_surf->pending_dmabuf_feedback;
519
520 memcpy(&feedback->main_device, device->data, sizeof(feedback->main_device));
521
522 /* Compositors may support switching render devices and change the main
523 * device of the dma-buf feedback. In this case, when we reallocate the
524 * buffers of the surface we must ensure that it is not allocated in memory
525 * that is only visible to the GPU that EGL is using, as the compositor will
526 * have to import them to the render device it is using.
527 *
528 * TODO: we still don't know how to allocate such buffers.
529 */
530 if (dri2_surf->dmabuf_feedback.main_device != 0 &&
531 (feedback->main_device != dri2_surf->dmabuf_feedback.main_device))
532 dri2_surf->compositor_using_another_device = true;
533 else
534 dri2_surf->compositor_using_another_device = false;
535 }
536
537 static void
538 surface_dmabuf_feedback_tranche_target_device(
539 void *data, struct zwp_linux_dmabuf_feedback_v1 *dmabuf_feedback,
540 struct wl_array *device)
541 {
542 struct dri2_egl_surface *dri2_surf = data;
543 struct dmabuf_feedback *feedback = &dri2_surf->pending_dmabuf_feedback;
544
545 memcpy(&feedback->pending_tranche.target_device, device->data,
546 sizeof(feedback->pending_tranche.target_device));
547 }
548
549 static void
550 surface_dmabuf_feedback_tranche_flags(
551 void *data, struct zwp_linux_dmabuf_feedback_v1 *dmabuf_feedback,
552 uint32_t flags)
553 {
554 struct dri2_egl_surface *dri2_surf = data;
555 struct dmabuf_feedback *feedback = &dri2_surf->pending_dmabuf_feedback;
556
557 feedback->pending_tranche.flags = flags;
558 }
559
560 static void
561 surface_dmabuf_feedback_tranche_formats(
562 void *data, struct zwp_linux_dmabuf_feedback_v1 *dmabuf_feedback,
563 struct wl_array *indices)
564 {
565 struct dri2_egl_surface *dri2_surf = data;
566 struct dmabuf_feedback *feedback = &dri2_surf->pending_dmabuf_feedback;
567 uint32_t present_format = dri2_surf->format;
568 uint64_t *modifier_ptr, modifier;
569 uint32_t format;
570 uint16_t *index;
571 int visual_idx;
572
573 if (dri2_surf->base.PresentOpaque) {
574 visual_idx = dri2_wl_visual_idx_from_fourcc(present_format);
575 if (visual_idx != -1)
576 present_format = dri2_wl_visuals[visual_idx].opaque_wl_drm_format;
577 }
578
579 /* Compositor may advertise or not a format table. If it does, we use it.
580 * Otherwise, we steal the most recent advertised format table. If we don't
581 * have a most recent advertised format table, compositor did something
582 * wrong. */
583 if (feedback->format_table.data == NULL) {
584 feedback->format_table = dri2_surf->dmabuf_feedback.format_table;
585 dmabuf_feedback_format_table_init(
586 &dri2_surf->dmabuf_feedback.format_table);
587 }
588 if (feedback->format_table.data == MAP_FAILED) {
589 _eglLog(_EGL_WARNING, "wayland-egl: we could not map the format table "
590 "so we won't be able to use this batch of dma-buf "
591 "feedback events.");
592 return;
593 }
594 if (feedback->format_table.data == NULL) {
595 _eglLog(_EGL_WARNING,
596 "wayland-egl: compositor didn't advertise a format "
597 "table, so we won't be able to use this batch of dma-buf "
598 "feedback events.");
599 return;
600 }
601
602 wl_array_for_each (index, indices) {
603 format = feedback->format_table.data[*index].format;
604 modifier = feedback->format_table.data[*index].modifier;
605
606 /* Skip formats that are not the one the surface is already using. We
607 * can't switch to another format. */
608 if (format != present_format)
609 continue;
610
611 /* We are sure that the format is supported because of the check above. */
612 visual_idx = dri2_wl_visual_idx_from_fourcc(format);
613 assert(visual_idx != -1);
614
615 BITSET_SET(feedback->pending_tranche.formats.formats_bitmap, visual_idx);
616 modifier_ptr =
617 u_vector_add(&feedback->pending_tranche.formats.modifiers[visual_idx]);
618 if (modifier_ptr)
619 *modifier_ptr = modifier;
620 }
621 }
622
623 static void
624 surface_dmabuf_feedback_tranche_done(
625 void *data, struct zwp_linux_dmabuf_feedback_v1 *dmabuf_feedback)
626 {
627 struct dri2_egl_surface *dri2_surf = data;
628 struct dmabuf_feedback *feedback = &dri2_surf->pending_dmabuf_feedback;
629
630 /* Add tranche to array of tranches. */
631 util_dynarray_append(&feedback->tranches, feedback->pending_tranche);
632
633 dmabuf_feedback_tranche_init(&feedback->pending_tranche);
634 }
635
636 static void
637 surface_dmabuf_feedback_done(
638 void *data, struct zwp_linux_dmabuf_feedback_v1 *dmabuf_feedback)
639 {
640 struct dri2_egl_surface *dri2_surf = data;
641
642 /* The dma-buf feedback protocol states that surface dma-buf feedback should
643 * be sent by the compositor only if its buffers are using a suboptimal pair
644 * of format and modifier. We can't change the buffer format, but we can
645 * reallocate with another modifier. So we raise this flag in order to force
646 * buffer reallocation based on the dma-buf feedback sent. */
647 dri2_surf->received_dmabuf_feedback = true;
648
649 dmabuf_feedback_fini(&dri2_surf->dmabuf_feedback);
650 dri2_surf->dmabuf_feedback = dri2_surf->pending_dmabuf_feedback;
651 dmabuf_feedback_init(&dri2_surf->pending_dmabuf_feedback);
652 }
653
654 static const struct zwp_linux_dmabuf_feedback_v1_listener
655 surface_dmabuf_feedback_listener = {
656 .format_table = surface_dmabuf_feedback_format_table,
657 .main_device = surface_dmabuf_feedback_main_device,
658 .tranche_target_device = surface_dmabuf_feedback_tranche_target_device,
659 .tranche_flags = surface_dmabuf_feedback_tranche_flags,
660 .tranche_formats = surface_dmabuf_feedback_tranche_formats,
661 .tranche_done = surface_dmabuf_feedback_tranche_done,
662 .done = surface_dmabuf_feedback_done,
663 };
664
665 static bool
666 dri2_wl_modifiers_have_common(struct u_vector *modifiers1,
667 struct u_vector *modifiers2)
668 {
669 uint64_t *mod1, *mod2;
670
671 /* If both modifier vectors are empty, assume there is a compatible
672 * implicit modifier. */
673 if (u_vector_length(modifiers1) == 0 && u_vector_length(modifiers2) == 0)
674 return true;
675
676 u_vector_foreach(mod1, modifiers1)
677 {
678 u_vector_foreach(mod2, modifiers2)
679 {
680 if (*mod1 == *mod2)
681 return true;
682 }
683 }
684
685 return false;
686 }
687
688 /**
689 * Called via eglCreateWindowSurface(), drv->CreateWindowSurface().
690 */
691 static _EGLSurface *
692 dri2_wl_create_window_surface(_EGLDisplay *disp, _EGLConfig *conf,
693 void *native_window, const EGLint *attrib_list)
694 {
695 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
696 struct dri2_egl_config *dri2_conf = dri2_egl_config(conf);
697 struct wl_egl_window *window = native_window;
698 struct dri2_egl_surface *dri2_surf;
699 struct zwp_linux_dmabuf_v1 *dmabuf_wrapper;
700 int visual_idx;
701 const struct dri_config *config;
702
703 if (!window) {
704 _eglError(EGL_BAD_NATIVE_WINDOW, "dri2_create_surface");
705 return NULL;
706 }
707
708 if (window->driver_private) {
709 _eglError(EGL_BAD_ALLOC, "dri2_create_surface");
710 return NULL;
711 }
712
713 dri2_surf = calloc(1, sizeof *dri2_surf);
714 if (!dri2_surf) {
715 _eglError(EGL_BAD_ALLOC, "dri2_create_surface");
716 return NULL;
717 }
718
719 if (!dri2_init_surface(&dri2_surf->base, disp, EGL_WINDOW_BIT, conf,
720 attrib_list, false, native_window))
721 goto cleanup_surf;
722
723 config = dri2_get_dri_config(dri2_conf, EGL_WINDOW_BIT,
724 dri2_surf->base.GLColorspace);
725
726 if (!config) {
727 _eglError(EGL_BAD_MATCH,
728 "Unsupported surfacetype/colorspace configuration");
729 goto cleanup_surf;
730 }
731
732 dri2_surf->base.Width = window->width;
733 dri2_surf->base.Height = window->height;
734
735 visual_idx = dri2_wl_visual_idx_from_config(config);
736 assert(visual_idx != -1);
737 assert(dri2_wl_visuals[visual_idx].pipe_format != PIPE_FORMAT_NONE);
738
739 if (dri2_dpy->wl_dmabuf
740 #ifdef HAVE_BIND_WL_DISPLAY
741 || dri2_dpy->wl_drm
742 #endif
743 ) {
744 dri2_surf->format = dri2_wl_visuals[visual_idx].wl_drm_format;
745 } else {
746 assert(dri2_dpy->wl_shm);
747 dri2_surf->format = dri2_wl_shm_format_from_visual_idx(visual_idx);
748 }
749
750 if (dri2_surf->base.PresentOpaque) {
751 uint32_t opaque_fourcc =
752 dri2_wl_visuals[visual_idx].opaque_wl_drm_format;
753 int opaque_visual_idx = dri2_wl_visual_idx_from_fourcc(opaque_fourcc);
754
755 if (!server_supports_format(&dri2_dpy->formats, opaque_visual_idx) ||
756 !dri2_wl_modifiers_have_common(
757 &dri2_dpy->formats.modifiers[visual_idx],
758 &dri2_dpy->formats.modifiers[opaque_visual_idx])) {
759 _eglError(EGL_BAD_MATCH, "Unsupported opaque format");
760 goto cleanup_surf;
761 }
762 }
763
764 dri2_surf->wl_queue = wl_display_create_queue_with_name(dri2_dpy->wl_dpy,
765 "mesa egl surface queue");
766 if (!dri2_surf->wl_queue) {
767 _eglError(EGL_BAD_ALLOC, "dri2_create_surface");
768 goto cleanup_surf;
769 }
770
771 #ifdef HAVE_BIND_WL_DISPLAY
772 if (dri2_dpy->wl_drm) {
773 dri2_surf->wl_drm_wrapper = wl_proxy_create_wrapper(dri2_dpy->wl_drm);
774 if (!dri2_surf->wl_drm_wrapper) {
775 _eglError(EGL_BAD_ALLOC, "dri2_create_surface");
776 goto cleanup_queue;
777 }
778 wl_proxy_set_queue((struct wl_proxy *)dri2_surf->wl_drm_wrapper,
779 dri2_surf->wl_queue);
780 }
781 #endif
782
783 dri2_surf->wl_dpy_wrapper = wl_proxy_create_wrapper(dri2_dpy->wl_dpy);
784 if (!dri2_surf->wl_dpy_wrapper) {
785 _eglError(EGL_BAD_ALLOC, "dri2_create_surface");
786 goto cleanup_drm;
787 }
788 wl_proxy_set_queue((struct wl_proxy *)dri2_surf->wl_dpy_wrapper,
789 dri2_surf->wl_queue);
790
791 if (!get_wayland_surface(dri2_surf, window)) {
792 _eglError(EGL_BAD_ALLOC, "dri2_create_surface");
793 goto cleanup_dpy_wrapper;
794 }
795
796 if (dri2_dpy->wp_presentation) {
797 loader_wayland_wrap_presentation(&dri2_surf->wayland_presentation,
798 dri2_dpy->wp_presentation,
799 dri2_surf->wl_queue,
800 dri2_dpy->presentation_clock_id,
801 &dri2_surf->wayland_surface,
802 NULL, NULL, NULL);
803 }
804
805 if (dri2_dpy->wl_dmabuf &&
806 zwp_linux_dmabuf_v1_get_version(dri2_dpy->wl_dmabuf) >=
807 ZWP_LINUX_DMABUF_V1_GET_SURFACE_FEEDBACK_SINCE_VERSION) {
808 dmabuf_wrapper = wl_proxy_create_wrapper(dri2_dpy->wl_dmabuf);
809 if (!dmabuf_wrapper) {
810 _eglError(EGL_BAD_ALLOC, "dri2_create_surface");
811 goto cleanup_surf_wrapper;
812 }
813 wl_proxy_set_queue((struct wl_proxy *)dmabuf_wrapper,
814 dri2_surf->wl_queue);
815 dri2_surf->wl_dmabuf_feedback = zwp_linux_dmabuf_v1_get_surface_feedback(
816 dmabuf_wrapper, dri2_surf->wayland_surface.wrapper);
817 wl_proxy_wrapper_destroy(dmabuf_wrapper);
818
819 zwp_linux_dmabuf_feedback_v1_add_listener(
820 dri2_surf->wl_dmabuf_feedback, &surface_dmabuf_feedback_listener,
821 dri2_surf);
822
823 if (dmabuf_feedback_init(&dri2_surf->pending_dmabuf_feedback) < 0) {
824 zwp_linux_dmabuf_feedback_v1_destroy(dri2_surf->wl_dmabuf_feedback);
825 goto cleanup_surf_wrapper;
826 }
827 if (dmabuf_feedback_init(&dri2_surf->dmabuf_feedback) < 0) {
828 dmabuf_feedback_fini(&dri2_surf->pending_dmabuf_feedback);
829 zwp_linux_dmabuf_feedback_v1_destroy(dri2_surf->wl_dmabuf_feedback);
830 goto cleanup_surf_wrapper;
831 }
832
833 if (roundtrip(dri2_dpy) < 0)
834 goto cleanup_dmabuf_feedback;
835 }
836
837 dri2_surf->wl_win = window;
838 dri2_surf->wl_win->driver_private = dri2_surf;
839 dri2_surf->wl_win->destroy_window_callback = destroy_window_callback;
840 if (!dri2_dpy->swrast_not_kms)
841 dri2_surf->wl_win->resize_callback = resize_callback;
842
843 if (!dri2_create_drawable(dri2_dpy, config, dri2_surf, dri2_surf))
844 goto cleanup_dmabuf_feedback;
845
846 dri2_surf->base.SwapInterval = dri2_dpy->default_swap_interval;
847
848 return &dri2_surf->base;
849
850 cleanup_dmabuf_feedback:
851 if (dri2_surf->wl_dmabuf_feedback) {
852 zwp_linux_dmabuf_feedback_v1_destroy(dri2_surf->wl_dmabuf_feedback);
853 dmabuf_feedback_fini(&dri2_surf->dmabuf_feedback);
854 dmabuf_feedback_fini(&dri2_surf->pending_dmabuf_feedback);
855 }
856 cleanup_surf_wrapper:
857 loader_wayland_surface_destroy(&dri2_surf->wayland_surface);
858 cleanup_dpy_wrapper:
859 wl_proxy_wrapper_destroy(dri2_surf->wl_dpy_wrapper);
860 cleanup_drm:
861 #ifdef HAVE_BIND_WL_DISPLAY
862 if (dri2_surf->wl_drm_wrapper)
863 wl_proxy_wrapper_destroy(dri2_surf->wl_drm_wrapper);
864 cleanup_queue:
865 #endif
866 wl_event_queue_destroy(dri2_surf->wl_queue);
867 cleanup_surf:
868 free(dri2_surf);
869
870 return NULL;
871 }
872
873 static _EGLSurface *
874 dri2_wl_create_pbuffer_surface(_EGLDisplay *disp, _EGLConfig *conf,
875 const EGLint *attrib_list)
876 {
877 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
878 struct dri2_egl_config *dri2_conf = dri2_egl_config(conf);
879 struct dri2_egl_surface *dri2_surf;
880 const struct dri_config *config;
881 int visual_idx;
882 enum pipe_format pipe_format;
883
884 dri2_surf = calloc(1, sizeof *dri2_surf);
885 if (!dri2_surf) {
886 _eglError(EGL_BAD_ALLOC, "eglCreatePbufferSurface");
887 return NULL;
888 }
889
890 if (!dri2_init_surface(&dri2_surf->base, disp, EGL_PBUFFER_BIT, conf,
891 attrib_list, false, NULL))
892 goto cleanup_surface;
893
894 config = dri2_get_dri_config(dri2_conf, EGL_PBUFFER_BIT,
895 dri2_surf->base.GLColorspace);
896 if (!config) {
897 _eglError(EGL_BAD_MATCH,
898 "Unsupported surfacetype/colorspace configuration");
899 goto cleanup_surface;
900 }
901
902 visual_idx = dri2_wl_visual_idx_from_config(config);
903 assert(visual_idx != -1);
904 pipe_format = dri2_wl_visuals[visual_idx].pipe_format;
905 dri2_surf->format = dri2_wl_visuals[visual_idx].wl_drm_format;
906
907 if (!dri2_create_drawable(dri2_dpy, config, dri2_surf, dri2_surf))
908 goto cleanup_surface;
909
910 dri2_surf->front = dri_create_image(
911 dri2_dpy->dri_screen_render_gpu, dri2_surf->base.Width,
912 dri2_surf->base.Height, pipe_format, NULL, 0, 0, NULL);
913 if (!dri2_surf->front)
914 goto cleanup_surface;
915
916 return &dri2_surf->base;
917
918 cleanup_surface:
919 driDestroyDrawable(dri2_surf->dri_drawable);
920 free(dri2_surf);
921 return NULL;
922 }
923
924 static _EGLSurface *
925 dri2_wl_create_pixmap_surface(_EGLDisplay *disp, _EGLConfig *conf,
926 void *native_window, const EGLint *attrib_list)
927 {
928 /* From the EGL_EXT_platform_wayland spec, version 3:
929 *
930 * It is not valid to call eglCreatePlatformPixmapSurfaceEXT with a <dpy>
931 * that belongs to Wayland. Any such call fails and generates
932 * EGL_BAD_PARAMETER.
933 */
934 _eglError(EGL_BAD_PARAMETER, "cannot create EGL pixmap surfaces on "
935 "Wayland");
936 return NULL;
937 }
938
939 /**
940 * Called via eglDestroySurface(), drv->DestroySurface().
941 */
942 static EGLBoolean
943 dri2_wl_destroy_surface(_EGLDisplay *disp, _EGLSurface *surf)
944 {
945 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
946
947 driDestroyDrawable(dri2_surf->dri_drawable);
948
949 if (dri2_surf->base.Type == EGL_PBUFFER_BIT) {
950 dri2_destroy_image(dri2_surf->front);
951 dri2_fini_surface(surf);
952 free(surf);
953 return EGL_TRUE;
954 }
955
956 for (int i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
957 if (dri2_surf->color_buffers[i].wayland_buffer.buffer)
958 loader_wayland_buffer_destroy(&dri2_surf->color_buffers[i].wayland_buffer);
959 if (dri2_surf->color_buffers[i].dri_image)
960 dri2_destroy_image(dri2_surf->color_buffers[i].dri_image);
961 if (dri2_surf->color_buffers[i].linear_copy)
962 dri2_destroy_image(dri2_surf->color_buffers[i].linear_copy);
963 if (dri2_surf->color_buffers[i].data)
964 munmap(dri2_surf->color_buffers[i].data,
965 dri2_surf->color_buffers[i].data_size);
966 }
967
968 if (dri2_surf->throttle_callback)
969 wl_callback_destroy(dri2_surf->throttle_callback);
970
971 if (dri2_surf->wl_win) {
972 dri2_surf->wl_win->driver_private = NULL;
973 dri2_surf->wl_win->resize_callback = NULL;
974 dri2_surf->wl_win->destroy_window_callback = NULL;
975 }
976
977 loader_wayland_presentation_destroy(&dri2_surf->wayland_presentation);
978
979 loader_wayland_surface_destroy(&dri2_surf->wayland_surface);
980 wl_proxy_wrapper_destroy(dri2_surf->wl_dpy_wrapper);
981 #ifdef HAVE_BIND_WL_DISPLAY
982 if (dri2_surf->wl_drm_wrapper)
983 wl_proxy_wrapper_destroy(dri2_surf->wl_drm_wrapper);
984 #endif
985 if (dri2_surf->wl_dmabuf_feedback) {
986 zwp_linux_dmabuf_feedback_v1_destroy(dri2_surf->wl_dmabuf_feedback);
987 dmabuf_feedback_fini(&dri2_surf->dmabuf_feedback);
988 dmabuf_feedback_fini(&dri2_surf->pending_dmabuf_feedback);
989 }
990 wl_event_queue_destroy(dri2_surf->wl_queue);
991
992 dri2_fini_surface(surf);
993 free(surf);
994
995 return EGL_TRUE;
996 }
997
998 static EGLBoolean
999 dri2_wl_swap_interval(_EGLDisplay *disp, _EGLSurface *surf, EGLint interval)
1000 {
1001 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
1002
1003 kopperSetSwapInterval(dri2_surf->dri_drawable, interval);
1004
1005 return EGL_TRUE;
1006 }
1007
1008 static void
1009 dri2_wl_release_buffers(struct dri2_egl_surface *dri2_surf)
1010 {
1011 for (int i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
1012 if (dri2_surf->color_buffers[i].wayland_buffer.buffer) {
1013 if (dri2_surf->color_buffers[i].locked) {
1014 dri2_surf->color_buffers[i].wl_release = true;
1015 } else {
1016 loader_wayland_buffer_destroy(&dri2_surf->color_buffers[i].wayland_buffer);
1017 }
1018 }
1019 if (dri2_surf->color_buffers[i].dri_image)
1020 dri2_destroy_image(dri2_surf->color_buffers[i].dri_image);
1021 if (dri2_surf->color_buffers[i].linear_copy)
1022 dri2_destroy_image(dri2_surf->color_buffers[i].linear_copy);
1023 if (dri2_surf->color_buffers[i].data)
1024 munmap(dri2_surf->color_buffers[i].data,
1025 dri2_surf->color_buffers[i].data_size);
1026
1027 dri2_surf->color_buffers[i].dri_image = NULL;
1028 dri2_surf->color_buffers[i].linear_copy = NULL;
1029 dri2_surf->color_buffers[i].data = NULL;
1030 dri2_surf->color_buffers[i].age = 0;
1031 }
1032 }
1033
1034 /* Return list of modifiers that should be used to restrict the list of
1035 * modifiers actually supported by the surface. As of now, it is only used
1036 * to get the set of modifiers used for fixed-rate compression. */
1037 static uint64_t *
1038 get_surface_specific_modifiers(struct dri2_egl_surface *dri2_surf,
1039 int *modifiers_count)
1040 {
1041 struct dri2_egl_display *dri2_dpy =
1042 dri2_egl_display(dri2_surf->base.Resource.Display);
1043 int rate = dri2_surf->base.CompressionRate;
1044 uint64_t *modifiers;
1045
1046 if (rate == EGL_SURFACE_COMPRESSION_FIXED_RATE_NONE_EXT ||
1047 !dri2_surf->wl_win)
1048 return NULL;
1049
1050 if (!dri2_query_compression_modifiers(
1051 dri2_dpy->dri_screen_render_gpu, dri2_surf->format, rate,
1052 0, NULL, modifiers_count))
1053 return NULL;
1054
1055 modifiers = malloc(*modifiers_count * sizeof(uint64_t));
1056 if (!modifiers)
1057 return NULL;
1058
1059 if (!dri2_query_compression_modifiers(
1060 dri2_dpy->dri_screen_render_gpu, dri2_surf->format, rate,
1061 *modifiers_count, modifiers, modifiers_count)) {
1062 free(modifiers);
1063 return NULL;
1064 }
1065
1066 return modifiers;
1067 }
1068
1069 static void
1070 update_surface(struct dri2_egl_surface *dri2_surf, struct dri_image *dri_img)
1071 {
1072 int compression_rate;
1073
1074 if (!dri_img)
1075 return;
1076
1077 /* Update the surface with the actual compression rate */
1078 dri2_query_image(dri_img, __DRI_IMAGE_ATTRIB_COMPRESSION_RATE,
1079 &compression_rate);
1080 dri2_surf->base.CompressionRate = compression_rate;
1081 }
1082
1083 static bool
1084 intersect_modifiers(struct u_vector *subset, struct u_vector *set,
1085 uint64_t *other_modifiers, int other_modifiers_count)
1086 {
1087 if (!u_vector_init_pow2(subset, 4, sizeof(uint64_t)))
1088 return false;
1089
1090 uint64_t *modifier_ptr, *mod;
1091 u_vector_foreach(mod, set) {
1092 for (int i = 0; i < other_modifiers_count; ++i) {
1093 if (other_modifiers[i] != *mod)
1094 continue;
1095 modifier_ptr = u_vector_add(subset);
1096 if (modifier_ptr)
1097 *modifier_ptr = *mod;
1098 }
1099 }
1100
1101 return true;
1102 }
1103
1104 static void
1105 create_dri_image(struct dri2_egl_surface *dri2_surf,
1106 enum pipe_format pipe_format, uint32_t use_flags,
1107 uint64_t *surf_modifiers, int surf_modifiers_count,
1108 struct dri2_wl_formats *formats)
1109 {
1110 struct dri2_egl_display *dri2_dpy =
1111 dri2_egl_display(dri2_surf->base.Resource.Display);
1112 int visual_idx = dri2_wl_visual_idx_from_fourcc(dri2_surf->format);
1113 struct u_vector modifiers_subset;
1114 struct u_vector modifiers_subset_opaque;
1115 uint64_t *modifiers;
1116 unsigned int num_modifiers;
1117 struct u_vector *modifiers_present;
1118 bool implicit_mod_supported;
1119
1120 assert(visual_idx != -1);
1121
1122 if (dri2_surf->base.PresentOpaque) {
1123 uint32_t opaque_fourcc =
1124 dri2_wl_visuals[visual_idx].opaque_wl_drm_format;
1125 int opaque_visual_idx = dri2_wl_visual_idx_from_fourcc(opaque_fourcc);
1126 struct u_vector *modifiers_dpy = &dri2_dpy->formats.modifiers[visual_idx];
1127 /* Surface creation would have failed if we didn't support the matching
1128 * opaque format. */
1129 assert(opaque_visual_idx != -1);
1130
1131 if (!BITSET_TEST(formats->formats_bitmap, opaque_visual_idx))
1132 return;
1133
1134 if (!intersect_modifiers(&modifiers_subset_opaque,
1135 &formats->modifiers[opaque_visual_idx],
1136 u_vector_tail(modifiers_dpy),
1137 u_vector_length(modifiers_dpy)))
1138 return;
1139
1140 modifiers_present = &modifiers_subset_opaque;
1141 } else {
1142 if (!BITSET_TEST(formats->formats_bitmap, visual_idx))
1143 return;
1144 modifiers_present = &formats->modifiers[visual_idx];
1145 }
1146
1147 if (surf_modifiers_count > 0) {
1148 if (!intersect_modifiers(&modifiers_subset, modifiers_present,
1149 surf_modifiers, surf_modifiers_count))
1150 goto cleanup_present;
1151 modifiers = u_vector_tail(&modifiers_subset);
1152 num_modifiers = u_vector_length(&modifiers_subset);
1153 } else {
1154 modifiers = u_vector_tail(modifiers_present);
1155 num_modifiers = u_vector_length(modifiers_present);
1156 }
1157
1158 if (!dri2_dpy->dri_screen_render_gpu->base.screen->resource_create_with_modifiers &&
1159 dri2_dpy->wl_dmabuf) {
1160 /* We don't support explicit modifiers, check if the compositor supports
1161 * implicit modifiers. */
1162 implicit_mod_supported = false;
1163 for (unsigned int i = 0; i < num_modifiers; i++) {
1164 if (modifiers[i] == DRM_FORMAT_MOD_INVALID) {
1165 implicit_mod_supported = true;
1166 break;
1167 }
1168 }
1169
1170 if (!implicit_mod_supported) {
1171 if (surf_modifiers_count > 0) {
1172 u_vector_finish(&modifiers_subset);
1173 }
1174
1175 goto cleanup_present;
1176 }
1177
1178 num_modifiers = 0;
1179 modifiers = NULL;
1180 }
1181
1182 /* For the purposes of this function, an INVALID modifier on
1183 * its own means the modifiers aren't supported. */
1184 if (num_modifiers == 0 ||
1185 (num_modifiers == 1 && modifiers[0] == DRM_FORMAT_MOD_INVALID)) {
1186 num_modifiers = 0;
1187 modifiers = NULL;
1188 }
1189
1190 if (dri2_dpy->fd_render_gpu != dri2_dpy->fd_display_gpu) {
1191 use_flags = 0;
1192 modifiers = NULL;
1193 num_modifiers = 0;
1194 }
1195
1196 dri2_surf->back->dri_image = dri_create_image_with_modifiers(
1197 dri2_dpy->dri_screen_render_gpu, dri2_surf->base.Width,
1198 dri2_surf->base.Height, pipe_format, use_flags,
1199 modifiers, num_modifiers, NULL);
1200
1201 if (surf_modifiers_count > 0) {
1202 u_vector_finish(&modifiers_subset);
1203 update_surface(dri2_surf, dri2_surf->back->dri_image);
1204 }
1205
1206 cleanup_present:
1207 if (modifiers_present == &modifiers_subset_opaque)
1208 u_vector_finish(&modifiers_subset_opaque);
1209 }
1210
1211 static void
1212 create_dri_image_from_dmabuf_feedback(struct dri2_egl_surface *dri2_surf,
1213 enum pipe_format pipe_format,
1214 uint32_t use_flags,
1215 uint64_t *surf_modifiers,
1216 int surf_modifiers_count)
1217 {
1218 uint32_t flags;
1219
1220 /* We don't have valid dma-buf feedback, so return */
1221 if (dri2_surf->dmabuf_feedback.main_device == 0)
1222 return;
1223
1224 /* Iterates through the dma-buf feedback to pick a new set of modifiers. The
1225 * tranches are sent in descending order of preference by the compositor, so
1226 * the first set that we can pick is the best one. For now we still can't
1227 * specify the target device in order to make the render device try its best
1228 * to allocate memory that can be directly scanned out by the KMS device. But
1229 * in the future this may change (newer versions of
1230 * createImageWithModifiers). Also, we are safe to pick modifiers from
1231 * tranches whose target device differs from the main device, as compositors
1232 * do not expose (in dma-buf feedback tranches) formats/modifiers that are
1233 * incompatible with the main device. */
1234 util_dynarray_foreach (&dri2_surf->dmabuf_feedback.tranches,
1235 struct dmabuf_feedback_tranche, tranche) {
1236 flags = use_flags;
1237 if (tranche->flags & ZWP_LINUX_DMABUF_FEEDBACK_V1_TRANCHE_FLAGS_SCANOUT)
1238 flags |= __DRI_IMAGE_USE_SCANOUT;
1239
1240 create_dri_image(dri2_surf, pipe_format, flags, surf_modifiers,
1241 surf_modifiers_count, &tranche->formats);
1242
1243 if (dri2_surf->back->dri_image)
1244 return;
1245 }
1246 }
1247
1248 static void
1249 create_dri_image_from_formats(struct dri2_egl_surface *dri2_surf,
1250 enum pipe_format pipe_format, uint32_t use_flags,
1251 uint64_t *surf_modifiers,
1252 int surf_modifiers_count)
1253 {
1254 struct dri2_egl_display *dri2_dpy =
1255 dri2_egl_display(dri2_surf->base.Resource.Display);
1256 create_dri_image(dri2_surf, pipe_format, use_flags, surf_modifiers,
1257 surf_modifiers_count, &dri2_dpy->formats);
1258 }
1259
1260 static void
1261 wait_for_free_buffer(struct dri2_egl_display *dri2_dpy,
1262 struct dri2_egl_surface *dri2_surf)
1263 {
1264 MESA_TRACE_FUNC();
1265
1266 /* There might be a buffer release already queued that wasn't processed */
1267 wl_display_dispatch_queue_pending(dri2_dpy->wl_dpy, dri2_surf->wl_queue);
1268
1269 while (dri2_surf->back == NULL) {
1270 for (int i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
1271 /* Get an unlocked buffer, preferably one with a dri_buffer
1272 * already allocated and with minimum age.
1273 */
1274 if (dri2_surf->color_buffers[i].locked)
1275 continue;
1276
1277 if (!dri2_surf->back || !dri2_surf->back->dri_image ||
1278 (dri2_surf->color_buffers[i].age > 0 &&
1279 dri2_surf->color_buffers[i].age < dri2_surf->back->age))
1280 dri2_surf->back = &dri2_surf->color_buffers[i];
1281 }
1282
1283 if (dri2_surf->back)
1284 break;
1285
1286 /* If we don't have a buffer, then block on the server to release one for
1287 * us, and try again. wl_display_dispatch_queue will process any pending
1288 * events, however not all servers flush on issuing a buffer release
1289 * event. So, we spam the server with roundtrips as they always cause a
1290 * client flush.
1291 */
1292 if (wl_display_roundtrip_queue(dri2_dpy->wl_dpy, dri2_surf->wl_queue) < 0)
1293 return;
1294 }
1295 }
1296
1297 static int
1298 get_back_bo(struct dri2_egl_surface *dri2_surf,
1299 struct mesa_trace_flow *flow)
1300 {
1301 struct dri2_egl_display *dri2_dpy =
1302 dri2_egl_display(dri2_surf->base.Resource.Display);
1303 int use_flags;
1304 int visual_idx;
1305 unsigned int pipe_format;
1306 unsigned int linear_pipe_format;
1307
1308 MESA_TRACE_FUNC_FLOW(flow);
1309
1310 visual_idx = dri2_wl_visual_idx_from_fourcc(dri2_surf->format);
1311 assert(visual_idx != -1);
1312 pipe_format = dri2_wl_visuals[visual_idx].pipe_format;
1313 linear_pipe_format = pipe_format;
1314
1315 /* Substitute dri image format if server does not support original format */
1316 if (!BITSET_TEST(dri2_dpy->formats.formats_bitmap, visual_idx))
1317 linear_pipe_format = dri2_wl_visuals[visual_idx].alt_pipe_format;
1318
1319 /* These asserts hold, as long as dri2_wl_visuals[] is self-consistent and
1320 * the PRIME substitution logic in dri2_wl_add_configs_for_visuals() is free
1321 * of bugs.
1322 */
1323 assert(linear_pipe_format != PIPE_FORMAT_NONE);
1324 assert(BITSET_TEST(
1325 dri2_dpy->formats.formats_bitmap,
1326 dri2_wl_visual_idx_from_pipe_format(linear_pipe_format)));
1327
1328 wait_for_free_buffer(dri2_dpy, dri2_surf);
1329 if (dri2_surf->back == NULL)
1330 return -1;
1331
1332 use_flags = __DRI_IMAGE_USE_SHARE | __DRI_IMAGE_USE_BACKBUFFER;
1333
1334 if (dri2_surf->base.ProtectedContent) {
1335 /* Protected buffers can't be read from another GPU */
1336 if (dri2_dpy->fd_render_gpu != dri2_dpy->fd_display_gpu)
1337 return -1;
1338 use_flags |= __DRI_IMAGE_USE_PROTECTED;
1339 }
1340
1341 if (dri2_dpy->fd_render_gpu != dri2_dpy->fd_display_gpu &&
1342 dri2_surf->back->linear_copy == NULL) {
1343 uint64_t linear_mod = DRM_FORMAT_MOD_LINEAR;
1344 const uint64_t *render_modifiers = NULL, *display_modifiers = NULL;
1345 unsigned int render_num_modifiers = 0, display_num_modifiers = 0;
1346 struct dri_image *linear_copy_display_gpu_image = NULL;
1347
1348 if (dri2_dpy->dri_screen_render_gpu->base.screen->resource_create_with_modifiers) {
1349 render_modifiers = &linear_mod;
1350 render_num_modifiers = 1;
1351 }
1352
1353 if (dri2_dpy->dri_screen_display_gpu) {
1354 if (dri2_dpy->dri_screen_display_gpu->base.screen->resource_create_with_modifiers) {
1355 display_modifiers = &linear_mod;
1356 display_num_modifiers = 1;
1357 }
1358
1359 linear_copy_display_gpu_image = dri_create_image_with_modifiers(
1360 dri2_dpy->dri_screen_display_gpu,
1361 dri2_surf->base.Width, dri2_surf->base.Height,
1362 linear_pipe_format, use_flags | __DRI_IMAGE_USE_LINEAR,
1363 display_modifiers, display_num_modifiers, NULL);
1364
1365 if (linear_copy_display_gpu_image) {
1366 int i, ret = 1;
1367 int fourcc;
1368 int num_planes = 0;
1369 int buffer_fds[4];
1370 int strides[4];
1371 int offsets[4];
1372 unsigned error;
1373
1374 if (!dri2_query_image(linear_copy_display_gpu_image,
1375 __DRI_IMAGE_ATTRIB_NUM_PLANES,
1376 &num_planes))
1377 num_planes = 1;
1378
1379 for (i = 0; i < num_planes; i++) {
1380 struct dri_image *image = dri2_from_planar(
1381 linear_copy_display_gpu_image, i, NULL);
1382
1383 if (!image) {
1384 assert(i == 0);
1385 image = linear_copy_display_gpu_image;
1386 }
1387
1388 buffer_fds[i] = -1;
1389 ret &= dri2_query_image(image, __DRI_IMAGE_ATTRIB_FD,
1390 &buffer_fds[i]);
1391 ret &= dri2_query_image(
1392 image, __DRI_IMAGE_ATTRIB_STRIDE, &strides[i]);
1393 ret &= dri2_query_image(
1394 image, __DRI_IMAGE_ATTRIB_OFFSET, &offsets[i]);
1395
1396 if (image != linear_copy_display_gpu_image)
1397 dri2_destroy_image(image);
1398
1399 if (!ret) {
1400 do {
1401 if (buffer_fds[i] != -1)
1402 close(buffer_fds[i]);
1403 } while (--i >= 0);
1404 dri2_destroy_image(linear_copy_display_gpu_image);
1405 return -1;
1406 }
1407 }
1408
1409 ret &= dri2_query_image(linear_copy_display_gpu_image,
1410 __DRI_IMAGE_ATTRIB_FOURCC,
1411 &fourcc);
1412 if (!ret) {
1413 do {
1414 if (buffer_fds[i] != -1)
1415 close(buffer_fds[i]);
1416 } while (--i >= 0);
1417 dri2_destroy_image(linear_copy_display_gpu_image);
1418 return -1;
1419 }
1420
1421 /* The linear buffer was created in the display GPU's vram, so we
1422 * need to make it visible to render GPU
1423 */
1424 dri2_surf->back->linear_copy =
1425 dri2_from_dma_bufs(
1426 dri2_dpy->dri_screen_render_gpu,
1427 dri2_surf->base.Width, dri2_surf->base.Height,
1428 fourcc, linear_mod,
1429 &buffer_fds[0], num_planes, &strides[0], &offsets[0],
1430 __DRI_YUV_COLOR_SPACE_UNDEFINED,
1431 __DRI_YUV_RANGE_UNDEFINED, __DRI_YUV_CHROMA_SITING_UNDEFINED,
1432 __DRI_YUV_CHROMA_SITING_UNDEFINED, __DRI_IMAGE_PRIME_LINEAR_BUFFER,
1433 &error, dri2_surf->back);
1434
1435 for (i = 0; i < num_planes; ++i) {
1436 if (buffer_fds[i] != -1)
1437 close(buffer_fds[i]);
1438 }
1439 dri2_destroy_image(linear_copy_display_gpu_image);
1440 }
1441 }
1442
1443 if (!dri2_surf->back->linear_copy) {
1444 dri2_surf->back->linear_copy = dri_create_image_with_modifiers(
1445 dri2_dpy->dri_screen_render_gpu,
1446 dri2_surf->base.Width, dri2_surf->base.Height,
1447 linear_pipe_format, use_flags | __DRI_IMAGE_USE_LINEAR,
1448 render_modifiers, render_num_modifiers, NULL);
1449 }
1450
1451 if (dri2_surf->back->linear_copy == NULL)
1452 return -1;
1453 }
1454
1455 if (dri2_surf->back->dri_image == NULL) {
1456 int modifiers_count = 0;
1457 uint64_t *modifiers =
1458 get_surface_specific_modifiers(dri2_surf, &modifiers_count);
1459
1460 if (dri2_surf->wl_dmabuf_feedback)
1461 create_dri_image_from_dmabuf_feedback(
1462 dri2_surf, pipe_format, use_flags, modifiers, modifiers_count);
1463 if (dri2_surf->back->dri_image == NULL)
1464 create_dri_image_from_formats(dri2_surf, pipe_format, use_flags,
1465 modifiers, modifiers_count);
1466
1467 free(modifiers);
1468 dri2_surf->back->age = 0;
1469 }
1470
1471 if (dri2_surf->back->dri_image == NULL)
1472 return -1;
1473
1474 loader_wayland_buffer_set_flow(&dri2_surf->back->wayland_buffer, flow);
1475 dri2_surf->back->locked = true;
1476
1477 return 0;
1478 }
1479
1480 static void
1481 back_bo_to_dri_buffer(struct dri2_egl_surface *dri2_surf, __DRIbuffer *buffer)
1482 {
1483 struct dri_image *image;
1484 int name, pitch;
1485
1486 image = dri2_surf->back->dri_image;
1487
1488 dri2_query_image(image, __DRI_IMAGE_ATTRIB_NAME, &name);
1489 dri2_query_image(image, __DRI_IMAGE_ATTRIB_STRIDE, &pitch);
1490
1491 buffer->attachment = __DRI_BUFFER_BACK_LEFT;
1492 buffer->name = name;
1493 buffer->pitch = pitch;
1494 buffer->cpp = 4;
1495 buffer->flags = 0;
1496 }
1497
1498 /* Value chosen empirically as a compromise between avoiding frequent
1499 * reallocations and extended time of increased memory consumption due to
1500 * unused buffers being kept.
1501 */
1502 #define BUFFER_TRIM_AGE_HYSTERESIS 20
1503
1504 static int
1505 update_buffers(struct dri2_egl_surface *dri2_surf,
1506 struct mesa_trace_flow *flow)
1507 {
1508 struct dri2_egl_display *dri2_dpy =
1509 dri2_egl_display(dri2_surf->base.Resource.Display);
1510
1511 MESA_TRACE_FUNC_FLOW(flow);
1512
1513 if (dri2_surf->wl_win &&
1514 (dri2_surf->base.Width != dri2_surf->wl_win->width ||
1515 dri2_surf->base.Height != dri2_surf->wl_win->height)) {
1516
1517 dri2_surf->base.Width = dri2_surf->wl_win->width;
1518 dri2_surf->base.Height = dri2_surf->wl_win->height;
1519 dri2_surf->dx = dri2_surf->wl_win->dx;
1520 dri2_surf->dy = dri2_surf->wl_win->dy;
1521 }
1522
1523 if (dri2_surf->resized || dri2_surf->received_dmabuf_feedback) {
1524 dri2_wl_release_buffers(dri2_surf);
1525 dri2_surf->resized = false;
1526 dri2_surf->received_dmabuf_feedback = false;
1527 }
1528
1529 if (get_back_bo(dri2_surf, flow) < 0) {
1530 _eglError(EGL_BAD_ALLOC, "failed to allocate color buffer");
1531 return -1;
1532 }
1533
1534 /* If we have an extra unlocked buffer at this point, we had to do triple
1535 * buffering for a while, but now can go back to just double buffering.
1536 * That means we can free any unlocked buffer now. To avoid toggling between
1537 * going back to double buffering and needing to allocate another buffer too
1538 * fast we let the unneeded buffer sit around for a short while. */
1539 for (int i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
1540 if (!dri2_surf->color_buffers[i].locked &&
1541 dri2_surf->color_buffers[i].wayland_buffer.buffer &&
1542 dri2_surf->color_buffers[i].age > BUFFER_TRIM_AGE_HYSTERESIS) {
1543 loader_wayland_buffer_destroy(&dri2_surf->color_buffers[i].wayland_buffer);
1544 dri2_destroy_image(dri2_surf->color_buffers[i].dri_image);
1545 if (dri2_dpy->fd_render_gpu != dri2_dpy->fd_display_gpu)
1546 dri2_destroy_image(
1547 dri2_surf->color_buffers[i].linear_copy);
1548 dri2_surf->color_buffers[i].wayland_buffer.buffer = NULL;
1549 dri2_surf->color_buffers[i].dri_image = NULL;
1550 dri2_surf->color_buffers[i].linear_copy = NULL;
1551 dri2_surf->color_buffers[i].age = 0;
1552 }
1553 }
1554
1555 return 0;
1556 }
1557
1558 static int
1559 update_buffers_if_needed(struct dri2_egl_surface *dri2_surf,
1560 struct mesa_trace_flow *flow)
1561 {
1562 MESA_TRACE_FUNC_FLOW(flow);
1563
1564 if (dri2_surf->back != NULL)
1565 return 0;
1566
1567 return update_buffers(dri2_surf, flow);
1568 }
1569
1570 static int
1571 image_get_buffers(struct dri_drawable *driDrawable, unsigned int format,
1572 uint32_t *stamp, void *loaderPrivate, uint32_t buffer_mask,
1573 struct __DRIimageList *buffers)
1574 {
1575 struct dri2_egl_surface *dri2_surf = loaderPrivate;
1576 struct mesa_trace_flow flow = {0};
1577
1578 MESA_TRACE_FUNC_FLOW(&flow);
1579
1580 if (dri2_surf->base.Type == EGL_PBUFFER_BIT) {
1581 buffers->back = NULL;
1582 if (buffer_mask & __DRI_IMAGE_BUFFER_FRONT) {
1583 buffers->image_mask = __DRI_IMAGE_BUFFER_FRONT;
1584 buffers->front = dri2_surf->front;
1585 } else {
1586 buffers->image_mask = 0;
1587 buffers->front = NULL;
1588 }
1589 return 1;
1590 }
1591
1592 if (update_buffers_if_needed(dri2_surf, &flow) < 0)
1593 return 0;
1594
1595 buffers->image_mask = __DRI_IMAGE_BUFFER_BACK;
1596 buffers->back = dri2_surf->back->dri_image;
1597
1598 return 1;
1599 }
1600
1601 static void
1602 dri2_wl_flush_front_buffer(struct dri_drawable *driDrawable, void *loaderPrivate)
1603 {
1604 (void)driDrawable;
1605 (void)loaderPrivate;
1606 }
1607
1608 static unsigned
1609 dri2_wl_get_capability(void *loaderPrivate, enum dri_loader_cap cap)
1610 {
1611 switch (cap) {
1612 case DRI_LOADER_CAP_FP16:
1613 return 1;
1614 case DRI_LOADER_CAP_RGBA_ORDERING:
1615 return 1;
1616 default:
1617 return 0;
1618 }
1619 }
1620
1621 static const __DRIimageLoaderExtension image_loader_extension = {
1622 .base = {__DRI_IMAGE_LOADER, 2},
1623
1624 .getBuffers = image_get_buffers,
1625 .flushFrontBuffer = dri2_wl_flush_front_buffer,
1626 .getCapability = dri2_wl_get_capability,
1627 };
1628
1629 static void
1630 wayland_throttle_callback(void *data, struct wl_callback *callback,
1631 uint32_t time)
1632 {
1633 struct dri2_egl_surface *dri2_surf = data;
1634
1635 dri2_surf->throttle_callback = NULL;
1636 wl_callback_destroy(callback);
1637 }
1638
1639 static const struct wl_callback_listener throttle_listener = {
1640 .done = wayland_throttle_callback,
1641 };
1642
1643 static struct wl_buffer *
1644 create_wl_buffer(struct dri2_egl_display *dri2_dpy,
1645 struct dri2_egl_surface *dri2_surf, struct dri_image *image)
1646 {
1647 struct wl_buffer *ret = NULL;
1648 EGLBoolean query;
1649 int width, height, fourcc, num_planes;
1650 uint64_t modifier = DRM_FORMAT_MOD_INVALID;
1651 int mod_hi, mod_lo;
1652
1653 MESA_TRACE_FUNC();
1654
1655 query = dri2_query_image(image, __DRI_IMAGE_ATTRIB_WIDTH, &width);
1656 query &=
1657 dri2_query_image(image, __DRI_IMAGE_ATTRIB_HEIGHT, &height);
1658 query &=
1659 dri2_query_image(image, __DRI_IMAGE_ATTRIB_FOURCC, &fourcc);
1660 if (!query)
1661 return NULL;
1662
1663 query = dri2_query_image(image, __DRI_IMAGE_ATTRIB_NUM_PLANES,
1664 &num_planes);
1665 if (!query)
1666 num_planes = 1;
1667
1668 query = dri2_query_image(image, __DRI_IMAGE_ATTRIB_MODIFIER_UPPER,
1669 &mod_hi);
1670 query &= dri2_query_image(
1671 image, __DRI_IMAGE_ATTRIB_MODIFIER_LOWER, &mod_lo);
1672 if (query) {
1673 modifier = combine_u32_into_u64(mod_hi, mod_lo);
1674 }
1675
1676 bool supported_modifier = false;
1677 bool mod_invalid_supported = false;
1678 int visual_idx = dri2_wl_visual_idx_from_fourcc(fourcc);
1679 assert(visual_idx != -1);
1680
1681 uint64_t *mod;
1682 u_vector_foreach(mod, &dri2_dpy->formats.modifiers[visual_idx])
1683 {
1684 if (*mod == DRM_FORMAT_MOD_INVALID) {
1685 mod_invalid_supported = true;
1686 }
1687 if (*mod == modifier) {
1688 supported_modifier = true;
1689 break;
1690 }
1691 }
1692 if (!supported_modifier && mod_invalid_supported) {
1693 /* If the server has advertised DRM_FORMAT_MOD_INVALID then we trust
1694 * that the client has allocated the buffer with the right implicit
1695 * modifier for the format, even though it's allocated a buffer the
1696 * server hasn't explicitly claimed to support. */
1697 modifier = DRM_FORMAT_MOD_INVALID;
1698 supported_modifier = true;
1699 }
1700
1701 if (dri2_dpy->wl_dmabuf && supported_modifier) {
1702 struct zwp_linux_buffer_params_v1 *params;
1703 int i;
1704
1705 /* We don't need a wrapper for wl_dmabuf objects, because we have to
1706 * create the intermediate params object; we can set the queue on this,
1707 * and the wl_buffer inherits it race-free. */
1708 params = zwp_linux_dmabuf_v1_create_params(dri2_dpy->wl_dmabuf);
1709 if (dri2_surf)
1710 wl_proxy_set_queue((struct wl_proxy *)params, dri2_surf->wl_queue);
1711
1712 for (i = 0; i < num_planes; i++) {
1713 struct dri_image *p_image;
1714 int stride, offset;
1715 int fd = -1;
1716
1717 p_image = dri2_from_planar(image, i, NULL);
1718 if (!p_image) {
1719 assert(i == 0);
1720 p_image = image;
1721 }
1722
1723 query =
1724 dri2_query_image(p_image, __DRI_IMAGE_ATTRIB_FD, &fd);
1725 query &= dri2_query_image(
1726 p_image, __DRI_IMAGE_ATTRIB_STRIDE, &stride);
1727 query &= dri2_query_image(
1728 p_image, __DRI_IMAGE_ATTRIB_OFFSET, &offset);
1729 if (image != p_image)
1730 dri2_destroy_image(p_image);
1731
1732 if (!query) {
1733 if (fd >= 0)
1734 close(fd);
1735 zwp_linux_buffer_params_v1_destroy(params);
1736 return NULL;
1737 }
1738
1739 zwp_linux_buffer_params_v1_add(params, fd, i, offset, stride,
1740 modifier >> 32, modifier & 0xffffffff);
1741 close(fd);
1742 }
1743
1744 if (dri2_surf && dri2_surf->base.PresentOpaque)
1745 fourcc = dri2_wl_visuals[visual_idx].opaque_wl_drm_format;
1746
1747 ret = zwp_linux_buffer_params_v1_create_immed(params, width, height,
1748 fourcc, 0);
1749 zwp_linux_buffer_params_v1_destroy(params);
1750 }
1751 #ifdef HAVE_BIND_WL_DISPLAY
1752 else if (dri2_dpy->wl_drm) {
1753 struct wl_drm *wl_drm =
1754 dri2_surf ? dri2_surf->wl_drm_wrapper : dri2_dpy->wl_drm;
1755 int fd = -1, stride;
1756
1757 /* wl_drm doesn't support explicit modifiers, so ideally we should bail
1758 * out if modifier != DRM_FORMAT_MOD_INVALID. However many drivers will
1759 * return a valid modifier when querying the DRIImage even if a buffer
1760 * was allocated without explicit modifiers.
1761 * XXX: bail out if the buffer was allocated without explicit modifiers
1762 */
1763 if (num_planes > 1)
1764 return NULL;
1765
1766 query = dri2_query_image(image, __DRI_IMAGE_ATTRIB_FD, &fd);
1767 query &=
1768 dri2_query_image(image, __DRI_IMAGE_ATTRIB_STRIDE, &stride);
1769 if (!query) {
1770 if (fd >= 0)
1771 close(fd);
1772 return NULL;
1773 }
1774
1775 ret = wl_drm_create_prime_buffer(wl_drm, fd, width, height, fourcc, 0,
1776 stride, 0, 0, 0, 0);
1777 close(fd);
1778 }
1779 #endif
1780
1781 return ret;
1782 }
1783
1784 static EGLBoolean
1785 try_damage_buffer(struct dri2_egl_surface *dri2_surf, const EGLint *rects,
1786 EGLint n_rects)
1787 {
1788 if (wl_proxy_get_version((struct wl_proxy *)dri2_surf->wayland_surface.wrapper) <
1789 WL_SURFACE_DAMAGE_BUFFER_SINCE_VERSION)
1790 return EGL_FALSE;
1791
1792 if (n_rects == 0) {
1793 wl_surface_damage_buffer(dri2_surf->wayland_surface.wrapper, 0, 0,
1794 INT32_MAX, INT32_MAX);
1795 } else {
1796 for (int i = 0; i < n_rects; i++) {
1797 const int *rect = &rects[i * 4];
1798
1799 wl_surface_damage_buffer(dri2_surf->wayland_surface.wrapper, rect[0],
1800 dri2_surf->base.Height - rect[1] - rect[3],
1801 rect[2], rect[3]);
1802 }
1803 }
1804
1805 return EGL_TRUE;
1806 }
1807
1808 static int
1809 throttle(struct dri2_egl_display *dri2_dpy,
1810 struct dri2_egl_surface *dri2_surf)
1811 {
1812 MESA_TRACE_FUNC();
1813
1814 while (dri2_surf->throttle_callback != NULL)
1815 if (loader_wayland_dispatch(dri2_dpy->wl_dpy, dri2_surf->wl_queue, NULL) ==
1816 -1)
1817 return -1;
1818
1819 return 0;
1820 }
1821
1822 /**
1823 * Called via eglSwapBuffers(), drv->SwapBuffers().
1824 */
1825 static EGLBoolean
1826 dri2_wl_swap_buffers_with_damage(_EGLDisplay *disp, _EGLSurface *draw,
1827 const EGLint *rects, EGLint n_rects)
1828 {
1829 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1830 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
1831 struct mesa_trace_flow flow = { 0 };
1832
1833 if (!dri2_surf->wl_win)
1834 return _eglError(EGL_BAD_NATIVE_WINDOW, "dri2_swap_buffers");
1835
1836 if (dri2_surf->back)
1837 flow = dri2_surf->back->wayland_buffer.flow;
1838
1839 MESA_TRACE_FUNC_FLOW(&flow);
1840
1841 /* Flush (and finish glthread) before:
1842 * - update_buffers_if_needed because the unmarshalling thread
1843 * may be running currently, and we would concurrently alloc/free
1844 * the back bo.
1845 * - swapping current/back because flushing may free the buffer and
1846 * dri_image and reallocate them using get_back_bo (which causes a
1847 * a crash because 'current' becomes NULL).
1848 * - using any wl_* function because accessing them from this thread
1849 * and glthread causes troubles (see #7624 and #8136)
1850 */
1851 dri2_flush_drawable_for_swapbuffers(disp, draw);
1852 dri_invalidate_drawable(dri2_surf->dri_drawable);
1853
1854 if (dri2_surf->throttle_callback && throttle(dri2_dpy, dri2_surf) == -1)
1855 return -1;
1856
1857 for (int i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++)
1858 if (dri2_surf->color_buffers[i].age > 0)
1859 dri2_surf->color_buffers[i].age++;
1860
1861 /* Make sure we have a back buffer in case we're swapping without ever
1862 * rendering. */
1863 if (update_buffers_if_needed(dri2_surf, &flow) < 0)
1864 return _eglError(EGL_BAD_ALLOC, "dri2_swap_buffers");
1865
1866 if (draw->SwapInterval > 0) {
1867 dri2_surf->throttle_callback =
1868 wl_surface_frame(dri2_surf->wayland_surface.wrapper);
1869 wl_callback_add_listener(dri2_surf->throttle_callback, &throttle_listener,
1870 dri2_surf);
1871 }
1872
1873 dri2_surf->back->age = 1;
1874 dri2_surf->current = dri2_surf->back;
1875 dri2_surf->back = NULL;
1876
1877 if (!dri2_surf->current->wayland_buffer.buffer) {
1878 struct dri_image *image;
1879 struct wl_buffer *buffer;
1880
1881 if (dri2_dpy->fd_render_gpu != dri2_dpy->fd_display_gpu)
1882 image = dri2_surf->current->linear_copy;
1883 else
1884 image = dri2_surf->current->dri_image;
1885
1886 buffer = create_wl_buffer(dri2_dpy, dri2_surf, image);
1887 if (buffer == NULL)
1888 return _eglError(EGL_BAD_ALLOC, "dri2_swap_buffers");
1889 loader_wayland_wrap_buffer(&dri2_surf->current->wayland_buffer, buffer);
1890
1891 dri2_surf->current->wl_release = false;
1892
1893 wl_buffer_add_listener(dri2_surf->current->wayland_buffer.buffer,
1894 &wl_buffer_listener,
1895 dri2_surf);
1896
1897 loader_wayland_buffer_set_flow(&dri2_surf->current->wayland_buffer, &flow);
1898 }
1899
1900 wl_surface_attach(dri2_surf->wayland_surface.wrapper,
1901 dri2_surf->current->wayland_buffer.buffer,
1902 dri2_surf->dx,
1903 dri2_surf->dy);
1904
1905 dri2_surf->wl_win->attached_width = dri2_surf->base.Width;
1906 dri2_surf->wl_win->attached_height = dri2_surf->base.Height;
1907 /* reset resize growing parameters */
1908 dri2_surf->dx = 0;
1909 dri2_surf->dy = 0;
1910
1911 /* If the compositor doesn't support damage_buffer, we deliberately
1912 * ignore the damage region and post maximum damage, due to
1913 * https://bugs.freedesktop.org/78190 */
1914 if (!try_damage_buffer(dri2_surf, rects, n_rects))
1915 wl_surface_damage(dri2_surf->wayland_surface.wrapper, 0, 0, INT32_MAX,
1916 INT32_MAX);
1917
1918 if (dri2_dpy->fd_render_gpu != dri2_dpy->fd_display_gpu) {
1919 _EGLContext *ctx = _eglGetCurrentContext();
1920 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
1921 struct dri_drawable *dri_drawable = dri2_dpy->vtbl->get_dri_drawable(draw);
1922 dri2_blit_image(
1923 dri2_ctx->dri_context, dri2_surf->current->linear_copy,
1924 dri2_surf->current->dri_image, 0, 0, dri2_surf->base.Width,
1925 dri2_surf->base.Height, 0, 0, dri2_surf->base.Width,
1926 dri2_surf->base.Height, 0);
1927 dri_flush_drawable(dri_drawable);
1928 }
1929
1930 loader_wayland_presentation_feedback(&dri2_surf->wayland_presentation,
1931 &dri2_surf->current->wayland_buffer,
1932 NULL);
1933
1934 wl_surface_commit(dri2_surf->wayland_surface.wrapper);
1935
1936 /* If we're not waiting for a frame callback then we'll at least throttle
1937 * to a sync callback so that we always give a chance for the compositor to
1938 * handle the commit and send a release event before checking for a free
1939 * buffer */
1940 if (dri2_surf->throttle_callback == NULL) {
1941 dri2_surf->throttle_callback = wl_display_sync(dri2_surf->wl_dpy_wrapper);
1942 wl_callback_add_listener(dri2_surf->throttle_callback, &throttle_listener,
1943 dri2_surf);
1944 }
1945
1946 wl_display_flush(dri2_dpy->wl_dpy);
1947
1948 return EGL_TRUE;
1949 }
1950
1951 static EGLint
1952 dri2_wl_query_buffer_age(_EGLDisplay *disp, _EGLSurface *surface)
1953 {
1954 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surface);
1955 struct mesa_trace_flow flow = { 0 };
1956
1957 MESA_TRACE_FUNC_FLOW(&flow);
1958
1959 if (dri2_surf->base.Type == EGL_PBUFFER_BIT)
1960 return 0;
1961
1962 if (update_buffers_if_needed(dri2_surf, &flow) < 0) {
1963 _eglError(EGL_BAD_ALLOC, "dri2_query_buffer_age");
1964 return -1;
1965 }
1966
1967 return dri2_surf->back->age;
1968 }
1969
1970 static EGLBoolean
1971 dri2_wl_swap_buffers(_EGLDisplay *disp, _EGLSurface *draw)
1972 {
1973 return dri2_wl_swap_buffers_with_damage(disp, draw, NULL, 0);
1974 }
1975
1976 #ifdef HAVE_BIND_WL_DISPLAY
1977 static struct wl_buffer *
1978 dri2_wl_create_wayland_buffer_from_image(_EGLDisplay *disp, _EGLImage *img)
1979 {
1980 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1981 struct dri2_egl_image *dri2_img = dri2_egl_image(img);
1982 struct dri_image *image = dri2_img->dri_image;
1983 struct wl_buffer *buffer;
1984 int fourcc;
1985
1986 /* Check the upstream display supports this buffer's format. */
1987 dri2_query_image(image, __DRI_IMAGE_ATTRIB_FOURCC, &fourcc);
1988 if (!server_supports_fourcc(&dri2_dpy->formats, fourcc))
1989 goto bad_format;
1990
1991 buffer = create_wl_buffer(dri2_dpy, NULL, image);
1992
1993 /* The buffer object will have been created with our internal event queue
1994 * because it is using wl_dmabuf/wl_drm as a proxy factory. We want the
1995 * buffer to be used by the application so we'll reset it to the display's
1996 * default event queue. This isn't actually racy, as the only event the
1997 * buffer can get is a buffer release, which doesn't happen with an explicit
1998 * attach. */
1999 if (buffer)
2000 wl_proxy_set_queue((struct wl_proxy *)buffer, NULL);
2001
2002 return buffer;
2003
2004 bad_format:
2005 _eglError(EGL_BAD_MATCH, "unsupported image format");
2006 return NULL;
2007 }
2008
2009 static int
2010 dri2_wl_authenticate(_EGLDisplay *disp, uint32_t id)
2011 {
2012 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2013 int ret = 0;
2014
2015 if (dri2_dpy->is_render_node) {
2016 _eglLog(_EGL_WARNING, "wayland-egl: client asks server to "
2017 "authenticate for render-nodes");
2018 return 0;
2019 }
2020 dri2_dpy->authenticated = false;
2021
2022 wl_drm_authenticate(dri2_dpy->wl_drm, id);
2023 if (roundtrip(dri2_dpy) < 0)
2024 ret = -1;
2025
2026 if (!dri2_dpy->authenticated)
2027 ret = -1;
2028
2029 /* reset authenticated */
2030 dri2_dpy->authenticated = true;
2031
2032 return ret;
2033 }
2034
2035 static void
2036 drm_handle_device(void *data, struct wl_drm *drm, const char *device)
2037 {
2038 struct dri2_egl_display *dri2_dpy = data;
2039 drm_magic_t magic;
2040
2041 dri2_dpy->device_name = strdup(device);
2042 if (!dri2_dpy->device_name)
2043 return;
2044
2045 dri2_dpy->fd_render_gpu = loader_open_device(dri2_dpy->device_name);
2046 if (dri2_dpy->fd_render_gpu == -1) {
2047 _eglLog(_EGL_WARNING, "wayland-egl: could not open %s (%s)",
2048 dri2_dpy->device_name, strerror(errno));
2049 free(dri2_dpy->device_name);
2050 dri2_dpy->device_name = NULL;
2051 return;
2052 }
2053
2054 if (drmGetNodeTypeFromFd(dri2_dpy->fd_render_gpu) == DRM_NODE_RENDER) {
2055 dri2_dpy->authenticated = true;
2056 } else {
2057 if (drmGetMagic(dri2_dpy->fd_render_gpu, &magic)) {
2058 close(dri2_dpy->fd_render_gpu);
2059 dri2_dpy->fd_render_gpu = -1;
2060 free(dri2_dpy->device_name);
2061 dri2_dpy->device_name = NULL;
2062 _eglLog(_EGL_WARNING, "wayland-egl: drmGetMagic failed");
2063 return;
2064 }
2065 wl_drm_authenticate(dri2_dpy->wl_drm, magic);
2066 }
2067 }
2068
2069 static void
2070 drm_handle_format(void *data, struct wl_drm *drm, uint32_t format)
2071 {
2072 struct dri2_egl_display *dri2_dpy = data;
2073 int visual_idx = dri2_wl_visual_idx_from_fourcc(format);
2074
2075 if (visual_idx == -1)
2076 return;
2077
2078 BITSET_SET(dri2_dpy->formats.formats_bitmap, visual_idx);
2079 }
2080
2081 static void
2082 drm_handle_capabilities(void *data, struct wl_drm *drm, uint32_t value)
2083 {
2084 struct dri2_egl_display *dri2_dpy = data;
2085
2086 dri2_dpy->capabilities = value;
2087 }
2088
2089 static void
2090 drm_handle_authenticated(void *data, struct wl_drm *drm)
2091 {
2092 struct dri2_egl_display *dri2_dpy = data;
2093
2094 dri2_dpy->authenticated = true;
2095 }
2096
2097 static const struct wl_drm_listener drm_listener = {
2098 .device = drm_handle_device,
2099 .format = drm_handle_format,
2100 .authenticated = drm_handle_authenticated,
2101 .capabilities = drm_handle_capabilities,
2102 };
2103 #endif
2104
2105 static void
2106 dmabuf_ignore_format(void *data, struct zwp_linux_dmabuf_v1 *dmabuf,
2107 uint32_t format)
2108 {
2109 /* formats are implicitly advertised by the 'modifier' event, so ignore */
2110 }
2111
2112 static void
2113 dmabuf_handle_modifier(void *data, struct zwp_linux_dmabuf_v1 *dmabuf,
2114 uint32_t format, uint32_t modifier_hi,
2115 uint32_t modifier_lo)
2116 {
2117 struct dri2_egl_display *dri2_dpy = data;
2118 int visual_idx = dri2_wl_visual_idx_from_fourcc(format);
2119 uint64_t *mod;
2120
2121 /* Ignore this if the compositor advertised dma-buf feedback. From version 4
2122 * onwards (when dma-buf feedback was introduced), the compositor should not
2123 * advertise this event anymore, but let's keep this for safety. */
2124 if (dri2_dpy->wl_dmabuf_feedback)
2125 return;
2126
2127 if (visual_idx == -1)
2128 return;
2129
2130 BITSET_SET(dri2_dpy->formats.formats_bitmap, visual_idx);
2131
2132 mod = u_vector_add(&dri2_dpy->formats.modifiers[visual_idx]);
2133 if (mod)
2134 *mod = combine_u32_into_u64(modifier_hi, modifier_lo);
2135 }
2136
2137 static const struct zwp_linux_dmabuf_v1_listener dmabuf_listener = {
2138 .format = dmabuf_ignore_format,
2139 .modifier = dmabuf_handle_modifier,
2140 };
2141
2142 #ifdef HAVE_BIND_WL_DISPLAY
2143 static void
2144 wl_drm_bind(struct dri2_egl_display *dri2_dpy)
2145 {
2146 dri2_dpy->wl_drm =
2147 wl_registry_bind(dri2_dpy->wl_registry, dri2_dpy->wl_drm_name,
2148 &wl_drm_interface, dri2_dpy->wl_drm_version);
2149 wl_drm_add_listener(dri2_dpy->wl_drm, &drm_listener, dri2_dpy);
2150 }
2151 #endif
2152
2153 static void
2154 default_dmabuf_feedback_format_table(
2155 void *data,
2156 struct zwp_linux_dmabuf_feedback_v1 *zwp_linux_dmabuf_feedback_v1,
2157 int32_t fd, uint32_t size)
2158 {
2159 struct dri2_egl_display *dri2_dpy = data;
2160
2161 dri2_dpy->format_table.size = size;
2162 dri2_dpy->format_table.data =
2163 mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
2164
2165 close(fd);
2166 }
2167
2168 static void
2169 default_dmabuf_feedback_main_device(
2170 void *data, struct zwp_linux_dmabuf_feedback_v1 *dmabuf_feedback,
2171 struct wl_array *device)
2172 {
2173 struct dri2_egl_display *dri2_dpy = data;
2174 char *node;
2175 int fd;
2176 dev_t dev;
2177
2178 /* Given the device, look for a render node and try to open it. */
2179 memcpy(&dev, device->data, sizeof(dev));
2180 node = loader_get_render_node(dev);
2181 if (!node)
2182 return;
2183 fd = loader_open_device(node);
2184 if (fd == -1) {
2185 free(node);
2186 return;
2187 }
2188
2189 dri2_dpy->device_name = node;
2190 dri2_dpy->fd_render_gpu = fd;
2191 #ifdef HAVE_BIND_WL_DISPLAY
2192 dri2_dpy->authenticated = true;
2193 #endif
2194 }
2195
2196 static void
2197 default_dmabuf_feedback_tranche_target_device(
2198 void *data, struct zwp_linux_dmabuf_feedback_v1 *dmabuf_feedback,
2199 struct wl_array *device)
2200 {
2201 /* ignore this event */
2202 }
2203
2204 static void
2205 default_dmabuf_feedback_tranche_flags(
2206 void *data, struct zwp_linux_dmabuf_feedback_v1 *dmabuf_feedback,
2207 uint32_t flags)
2208 {
2209 /* ignore this event */
2210 }
2211
2212 static void
2213 default_dmabuf_feedback_tranche_formats(
2214 void *data, struct zwp_linux_dmabuf_feedback_v1 *dmabuf_feedback,
2215 struct wl_array *indices)
2216 {
2217 struct dri2_egl_display *dri2_dpy = data;
2218 uint64_t *modifier_ptr, modifier;
2219 uint32_t format;
2220 uint16_t *index;
2221 int visual_idx;
2222
2223 if (dri2_dpy->format_table.data == MAP_FAILED) {
2224 _eglLog(_EGL_WARNING, "wayland-egl: we could not map the format table "
2225 "so we won't be able to use this batch of dma-buf "
2226 "feedback events.");
2227 return;
2228 }
2229 if (dri2_dpy->format_table.data == NULL) {
2230 _eglLog(_EGL_WARNING,
2231 "wayland-egl: compositor didn't advertise a format "
2232 "table, so we won't be able to use this batch of dma-buf "
2233 "feedback events.");
2234 return;
2235 }
2236
2237 wl_array_for_each (index, indices) {
2238 format = dri2_dpy->format_table.data[*index].format;
2239 modifier = dri2_dpy->format_table.data[*index].modifier;
2240
2241 /* skip formats that we don't support */
2242 visual_idx = dri2_wl_visual_idx_from_fourcc(format);
2243 if (visual_idx == -1)
2244 continue;
2245
2246 BITSET_SET(dri2_dpy->formats.formats_bitmap, visual_idx);
2247 modifier_ptr = u_vector_add(&dri2_dpy->formats.modifiers[visual_idx]);
2248 if (modifier_ptr)
2249 *modifier_ptr = modifier;
2250 }
2251 }
2252
2253 static void
2254 default_dmabuf_feedback_tranche_done(
2255 void *data, struct zwp_linux_dmabuf_feedback_v1 *dmabuf_feedback)
2256 {
2257 /* ignore this event */
2258 }
2259
2260 static void
2261 default_dmabuf_feedback_done(
2262 void *data, struct zwp_linux_dmabuf_feedback_v1 *dmabuf_feedback)
2263 {
2264 /* ignore this event */
2265 }
2266
2267 static const struct zwp_linux_dmabuf_feedback_v1_listener
2268 dmabuf_feedback_listener = {
2269 .format_table = default_dmabuf_feedback_format_table,
2270 .main_device = default_dmabuf_feedback_main_device,
2271 .tranche_target_device = default_dmabuf_feedback_tranche_target_device,
2272 .tranche_flags = default_dmabuf_feedback_tranche_flags,
2273 .tranche_formats = default_dmabuf_feedback_tranche_formats,
2274 .tranche_done = default_dmabuf_feedback_tranche_done,
2275 .done = default_dmabuf_feedback_done,
2276 };
2277
2278 static void
2279 presentation_handle_clock_id(void* data, struct wp_presentation *wp_presentation, uint32_t clk_id)
2280 {
2281 struct dri2_egl_display *dri2_dpy = data;
2282
2283 dri2_dpy->presentation_clock_id = clk_id;
2284 }
2285
2286 static const struct wp_presentation_listener presentation_listener = {
2287 presentation_handle_clock_id,
2288 };
2289
2290 static void
2291 registry_handle_global_drm(void *data, struct wl_registry *registry,
2292 uint32_t name, const char *interface,
2293 uint32_t version)
2294 {
2295 struct dri2_egl_display *dri2_dpy = data;
2296
2297 #ifdef HAVE_BIND_WL_DISPLAY
2298 if (strcmp(interface, wl_drm_interface.name) == 0) {
2299 dri2_dpy->wl_drm_version = MIN2(version, 2);
2300 dri2_dpy->wl_drm_name = name;
2301 } else
2302 #endif
2303 if (strcmp(interface, zwp_linux_dmabuf_v1_interface.name) == 0 &&
2304 version >= 3) {
2305 dri2_dpy->wl_dmabuf = wl_registry_bind(
2306 registry, name, &zwp_linux_dmabuf_v1_interface,
2307 MIN2(version, ZWP_LINUX_DMABUF_V1_GET_DEFAULT_FEEDBACK_SINCE_VERSION));
2308 zwp_linux_dmabuf_v1_add_listener(dri2_dpy->wl_dmabuf, &dmabuf_listener,
2309 dri2_dpy);
2310 } else if (strcmp(interface, wp_presentation_interface.name) == 0) {
2311 dri2_dpy->wp_presentation =
2312 wl_registry_bind(registry, name, &wp_presentation_interface, 1);
2313 wp_presentation_add_listener(dri2_dpy->wp_presentation,
2314 &presentation_listener, dri2_dpy);
2315 #ifdef WL_FIXES_INTERFACE
2316 } else if (strcmp(interface, wl_fixes_interface.name) == 0) {
2317 dri2_dpy->wl_fixes = wl_registry_bind(registry, name, &wl_fixes_interface, 1);
2318 #endif
2319 }
2320 }
2321
2322 static void
2323 registry_handle_global_remove(void *data, struct wl_registry *registry,
2324 uint32_t name)
2325 {
2326 }
2327
2328 static const struct wl_registry_listener registry_listener_drm = {
2329 .global = registry_handle_global_drm,
2330 .global_remove = registry_handle_global_remove,
2331 };
2332
2333 static void
2334 dri2_wl_setup_swap_interval(_EGLDisplay *disp)
2335 {
2336 /* We can't use values greater than 1 on Wayland because we are using the
2337 * frame callback to synchronise the frame and the only way we be sure to
2338 * get a frame callback is to attach a new buffer. Therefore we can't just
2339 * sit drawing nothing to wait until the next ‘n’ frame callbacks */
2340
2341 dri2_setup_swap_interval(disp, 1);
2342 }
2343
2344 static const struct dri2_egl_display_vtbl dri2_wl_display_vtbl = {
2345 #ifdef HAVE_BIND_WL_DISPLAY
2346 .authenticate = dri2_wl_authenticate,
2347 .create_wayland_buffer_from_image = dri2_wl_create_wayland_buffer_from_image,
2348 #endif
2349 .create_window_surface = dri2_wl_create_window_surface,
2350 .create_pbuffer_surface = dri2_wl_create_pbuffer_surface,
2351 .create_pixmap_surface = dri2_wl_create_pixmap_surface,
2352 .destroy_surface = dri2_wl_destroy_surface,
2353 .swap_interval = dri2_wl_swap_interval,
2354 .create_image = dri2_create_image_khr,
2355 .swap_buffers = dri2_wl_swap_buffers,
2356 .swap_buffers_with_damage = dri2_wl_swap_buffers_with_damage,
2357 .query_buffer_age = dri2_wl_query_buffer_age,
2358 .get_dri_drawable = dri2_surface_get_dri_drawable,
2359 };
2360
2361 static const __DRIextension *dri2_loader_extensions[] = {
2362 &image_loader_extension.base,
2363 &image_lookup_extension.base,
2364 NULL,
2365 };
2366
2367 static EGLBoolean
2368 dri2_wl_surface_throttle(struct dri2_egl_surface *dri2_surf)
2369 {
2370 struct dri2_egl_display *dri2_dpy =
2371 dri2_egl_display(dri2_surf->base.Resource.Display);
2372
2373 while (dri2_surf->throttle_callback != NULL)
2374 if (loader_wayland_dispatch(dri2_dpy->wl_dpy, dri2_surf->wl_queue, NULL) ==
2375 -1)
2376 return EGL_FALSE;
2377
2378 if (dri2_surf->base.SwapInterval > 0) {
2379 dri2_surf->throttle_callback =
2380 wl_surface_frame(dri2_surf->wayland_surface.wrapper);
2381 wl_callback_add_listener(dri2_surf->throttle_callback, &throttle_listener,
2382 dri2_surf);
2383 }
2384
2385 return EGL_TRUE;
2386 }
2387
2388 static EGLBoolean
2389 dri2_wl_kopper_swap_buffers_with_damage(_EGLDisplay *disp, _EGLSurface *draw,
2390 const EGLint *rects, EGLint n_rects)
2391 {
2392 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
2393
2394 if (!dri2_surf->wl_win)
2395 return _eglError(EGL_BAD_NATIVE_WINDOW, "dri2_swap_buffers");
2396
2397 if (!dri2_wl_surface_throttle(dri2_surf))
2398 return EGL_FALSE;
2399
2400 if (n_rects) {
2401 kopperSwapBuffersWithDamage(dri2_surf->dri_drawable, __DRI2_FLUSH_CONTEXT | __DRI2_FLUSH_INVALIDATE_ANCILLARY, n_rects, rects);
2402 } else {
2403 kopperSwapBuffers(dri2_surf->dri_drawable, __DRI2_FLUSH_CONTEXT | __DRI2_FLUSH_INVALIDATE_ANCILLARY);
2404 }
2405
2406 dri2_surf->current = dri2_surf->back;
2407 dri2_surf->back = NULL;
2408
2409 return EGL_TRUE;
2410 }
2411
2412 static EGLBoolean
2413 dri2_wl_kopper_swap_buffers(_EGLDisplay *disp, _EGLSurface *draw)
2414 {
2415 dri2_wl_kopper_swap_buffers_with_damage(disp, draw, NULL, 0);
2416 return EGL_TRUE;
2417 }
2418
2419 static EGLint
2420 dri2_wl_kopper_query_buffer_age(_EGLDisplay *disp, _EGLSurface *surface)
2421 {
2422 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surface);
2423
2424 return kopperQueryBufferAge(dri2_surf->dri_drawable);
2425 }
2426
2427 static const struct dri2_egl_display_vtbl dri2_wl_kopper_display_vtbl = {
2428 .authenticate = NULL,
2429 .create_window_surface = dri2_wl_create_window_surface,
2430 .create_pixmap_surface = dri2_wl_create_pixmap_surface,
2431 .create_pbuffer_surface = dri2_wl_create_pbuffer_surface,
2432 .destroy_surface = dri2_wl_destroy_surface,
2433 .create_image = dri2_create_image_khr,
2434 .swap_buffers = dri2_wl_kopper_swap_buffers,
2435 .swap_buffers_with_damage = dri2_wl_kopper_swap_buffers_with_damage,
2436 .get_dri_drawable = dri2_surface_get_dri_drawable,
2437 .query_buffer_age = dri2_wl_kopper_query_buffer_age,
2438 };
2439
2440 static_assert(sizeof(struct kopper_vk_surface_create_storage) >=
2441 sizeof(VkWaylandSurfaceCreateInfoKHR),
2442 "");
2443
2444 static void
2445 kopperSetSurfaceCreateInfo(void *_draw, struct kopper_loader_info *out)
2446 {
2447 struct dri2_egl_surface *dri2_surf = _draw;
2448 struct dri2_egl_display *dri2_dpy =
2449 dri2_egl_display(dri2_surf->base.Resource.Display);
2450 VkWaylandSurfaceCreateInfoKHR *wlsci =
2451 (VkWaylandSurfaceCreateInfoKHR *)&out->bos;
2452
2453 wlsci->sType = VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR;
2454 wlsci->pNext = NULL;
2455 wlsci->flags = 0;
2456 wlsci->display = dri2_dpy->wl_dpy;
2457 /* Pass the original wl_surface through to Vulkan WSI. If we pass the
2458 * proxy wrapper, kopper won't be able to properly de-duplicate surfaces
2459 * and we may end up creating two VkSurfaceKHRs for the same underlying
2460 * wl_surface. Vulkan WSI (which kopper calls into) will make its own
2461 * queues and proxy wrappers.
2462 */
2463 wlsci->surface = dri2_surf->wayland_surface.surface;
2464 out->present_opaque = dri2_surf->base.PresentOpaque;
2465 /* convert to vulkan constants */
2466 switch (dri2_surf->base.CompressionRate) {
2467 case EGL_SURFACE_COMPRESSION_FIXED_RATE_NONE_EXT:
2468 out->compression = 0;
2469 break;
2470 case EGL_SURFACE_COMPRESSION_FIXED_RATE_DEFAULT_EXT:
2471 out->compression = UINT32_MAX;
2472 break;
2473 #define EGL_VK_COMP(NUM) \
2474 case EGL_SURFACE_COMPRESSION_FIXED_RATE_##NUM##BPC_EXT: \
2475 out->compression = VK_IMAGE_COMPRESSION_FIXED_RATE_##NUM##BPC_BIT_EXT; \
2476 break
2477 EGL_VK_COMP(1);
2478 EGL_VK_COMP(2);
2479 EGL_VK_COMP(3);
2480 EGL_VK_COMP(4);
2481 EGL_VK_COMP(5);
2482 EGL_VK_COMP(6);
2483 EGL_VK_COMP(7);
2484 EGL_VK_COMP(8);
2485 EGL_VK_COMP(9);
2486 EGL_VK_COMP(10);
2487 EGL_VK_COMP(11);
2488 EGL_VK_COMP(12);
2489 #undef EGL_VK_COMP
2490 default:
2491 UNREACHABLE("unknown compression rate");
2492 }
2493 }
2494
2495 static void
2496 kopper_update_buffers(struct dri2_egl_surface *dri2_surf)
2497 {
2498 /* we need to do the following operations only once per frame */
2499 if (dri2_surf->back)
2500 return;
2501
2502 if (dri2_surf->wl_win &&
2503 (dri2_surf->base.Width != dri2_surf->wl_win->width ||
2504 dri2_surf->base.Height != dri2_surf->wl_win->height)) {
2505
2506 dri2_surf->base.Width = dri2_surf->wl_win->width;
2507 dri2_surf->base.Height = dri2_surf->wl_win->height;
2508 dri2_surf->dx = dri2_surf->wl_win->dx;
2509 dri2_surf->dy = dri2_surf->wl_win->dy;
2510 dri2_surf->current = NULL;
2511 }
2512 }
2513
2514 static void
2515 dri2_wl_kopper_get_drawable_info(struct dri_drawable *draw, int *w,
2516 int *h, void *loaderPrivate)
2517 {
2518 struct dri2_egl_surface *dri2_surf = loaderPrivate;
2519
2520 if (dri2_surf->base.Type != EGL_PBUFFER_BIT)
2521 kopper_update_buffers(dri2_surf);
2522 *w = dri2_surf->base.Width;
2523 *h = dri2_surf->base.Height;
2524 }
2525
2526 static const __DRIkopperLoaderExtension kopper_loader_extension = {
2527 .base = {__DRI_KOPPER_LOADER, 1},
2528
2529 .SetSurfaceCreateInfo = kopperSetSurfaceCreateInfo,
2530 .GetDrawableInfo = dri2_wl_kopper_get_drawable_info,
2531 };
2532
2533 static const __DRIextension *kopper_loader_extensions[] = {
2534 &kopper_loader_extension.base,
2535 &image_lookup_extension.base,
2536 NULL,
2537 };
2538
2539 static void
2540 dri2_wl_add_configs_for_visuals(_EGLDisplay *disp)
2541 {
2542 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2543 unsigned int format_count[ARRAY_SIZE(dri2_wl_visuals)] = {0};
2544
2545 /* Try to create an EGLConfig for every config the driver declares */
2546 for (unsigned i = 0; dri2_dpy->driver_configs[i]; i++) {
2547 struct dri2_egl_config *dri2_conf;
2548 enum pipe_format format = PIPE_FORMAT_NONE;
2549 EGLint config_group = 0;
2550 bool conversion = false;
2551 bool server_supported = true;
2552 int idx = dri2_wl_visual_idx_from_config(dri2_dpy->driver_configs[i]);
2553
2554 if (idx < 0)
2555 continue;
2556
2557 /* Check if the server natively supports the colour buffer format */
2558 if (!server_supports_format(&dri2_dpy->formats, idx)) {
2559 if (dri2_dpy->fd_render_gpu == dri2_dpy->fd_display_gpu) {
2560 /* Not supported by the server, and we aren't doing a blit, so we
2561 * can't use it. */
2562 server_supported = false;
2563 } else if (server_supports_pipe_format(
2564 &dri2_dpy->formats,
2565 dri2_wl_visuals[idx].alt_pipe_format)) {
2566 /* The alternate format is supported by the server, so we can
2567 * convert whilst we do a blit. */
2568 conversion = true;
2569 format = dri2_wl_visuals[idx].alt_pipe_format;
2570 } else {
2571 /* Not supported at all by the server. */
2572 server_supported = false;
2573 }
2574 } else {
2575 format = dri2_wl_visuals[idx].pipe_format;
2576 }
2577
2578 /* Put the 16 bpc rgb[a] unorm formats into a lower priority EGL config
2579 * group 1, so they don't get preferably chosen by eglChooseConfig().
2580 */
2581 if (server_supported && util_format_is_unorm16(util_format_description(format)))
2582 config_group = 1;
2583
2584 EGLint attr_list[] = {
2585 EGL_NATIVE_VISUAL_ID, dri2_wl_visuals[idx].wl_drm_format,
2586 EGL_CONFIG_SELECT_GROUP_EXT, config_group,
2587 EGL_NONE,
2588 };
2589
2590 /* The format is supported one way or another; add the EGLConfig */
2591 dri2_conf = dri2_add_config(disp, dri2_dpy->driver_configs[i],
2592 EGL_PBUFFER_BIT |
2593 (server_supported ? EGL_WINDOW_BIT : 0),
2594 attr_list);
2595 if (!dri2_conf)
2596 continue;
2597
2598 format_count[idx]++;
2599
2600 if (conversion && format_count[idx] == 1) {
2601 _eglLog(_EGL_DEBUG, "Client format %s converted via PRIME blitImage.",
2602 util_format_name(dri2_wl_visuals[idx].pipe_format));
2603 }
2604 }
2605
2606 for (unsigned i = 0; i < ARRAY_SIZE(format_count); i++) {
2607 if (!format_count[i]) {
2608 _eglLog(_EGL_DEBUG, "No DRI config supports native format %s",
2609 util_format_name(dri2_wl_visuals[i].pipe_format));
2610 }
2611 }
2612 }
2613
2614 static bool
2615 dri2_initialize_wayland_drm_extensions(struct dri2_egl_display *dri2_dpy)
2616 {
2617 /* Get default dma-buf feedback */
2618 if (dri2_dpy->wl_dmabuf &&
2619 zwp_linux_dmabuf_v1_get_version(dri2_dpy->wl_dmabuf) >=
2620 ZWP_LINUX_DMABUF_V1_GET_DEFAULT_FEEDBACK_SINCE_VERSION) {
2621 dmabuf_feedback_format_table_init(&dri2_dpy->format_table);
2622 dri2_dpy->wl_dmabuf_feedback =
2623 zwp_linux_dmabuf_v1_get_default_feedback(dri2_dpy->wl_dmabuf);
2624 zwp_linux_dmabuf_feedback_v1_add_listener(
2625 dri2_dpy->wl_dmabuf_feedback, &dmabuf_feedback_listener, dri2_dpy);
2626 }
2627
2628 if (roundtrip(dri2_dpy) < 0)
2629 return false;
2630
2631 /* Destroy the default dma-buf feedback and the format table. */
2632 if (dri2_dpy->wl_dmabuf_feedback) {
2633 zwp_linux_dmabuf_feedback_v1_destroy(dri2_dpy->wl_dmabuf_feedback);
2634 dri2_dpy->wl_dmabuf_feedback = NULL;
2635 dmabuf_feedback_format_table_fini(&dri2_dpy->format_table);
2636 }
2637
2638 #ifdef HAVE_BIND_WL_DISPLAY
2639 /* We couldn't retrieve a render node from the dma-buf feedback (or the
2640 * feedback was not advertised at all), so we must fallback to wl_drm. */
2641 if (dri2_dpy->fd_render_gpu == -1) {
2642 /* wl_drm not advertised by compositor, so can't continue */
2643 if (dri2_dpy->wl_drm_name == 0)
2644 return false;
2645 wl_drm_bind(dri2_dpy);
2646
2647 if (dri2_dpy->wl_drm == NULL)
2648 return false;
2649 if (roundtrip(dri2_dpy) < 0 || dri2_dpy->fd_render_gpu == -1)
2650 return false;
2651
2652 if (!dri2_dpy->authenticated &&
2653 (roundtrip(dri2_dpy) < 0 || !dri2_dpy->authenticated))
2654 return false;
2655 }
2656 #endif
2657 return true;
2658 }
2659
2660 static EGLBoolean
2661 dri2_initialize_wayland_drm(_EGLDisplay *disp)
2662 {
2663 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
2664
2665 if (dri2_wl_formats_init(&dri2_dpy->formats) < 0)
2666 goto cleanup;
2667
2668 if (disp->PlatformDisplay == NULL) {
2669 dri2_dpy->wl_dpy = wl_display_connect(NULL);
2670 if (dri2_dpy->wl_dpy == NULL)
2671 goto cleanup;
2672 dri2_dpy->own_device = true;
2673 } else {
2674 dri2_dpy->wl_dpy = disp->PlatformDisplay;
2675 }
2676
2677 dri2_dpy->wl_queue = wl_display_create_queue_with_name(dri2_dpy->wl_dpy,
2678 "mesa egl display queue");
2679
2680 dri2_dpy->wl_dpy_wrapper = wl_proxy_create_wrapper(dri2_dpy->wl_dpy);
2681 if (dri2_dpy->wl_dpy_wrapper == NULL)
2682 goto cleanup;
2683
2684 wl_proxy_set_queue((struct wl_proxy *)dri2_dpy->wl_dpy_wrapper,
2685 dri2_dpy->wl_queue);
2686
2687 if (dri2_dpy->own_device)
2688 wl_display_dispatch_pending(dri2_dpy->wl_dpy);
2689
2690 dri2_dpy->wl_registry = wl_display_get_registry(dri2_dpy->wl_dpy_wrapper);
2691 wl_registry_add_listener(dri2_dpy->wl_registry, &registry_listener_drm,
2692 dri2_dpy);
2693
2694 if (roundtrip(dri2_dpy) < 0)
2695 goto cleanup;
2696
2697 if (!dri2_initialize_wayland_drm_extensions(dri2_dpy))
2698 goto cleanup;
2699
2700 loader_get_user_preferred_fd(&dri2_dpy->fd_render_gpu,
2701 &dri2_dpy->fd_display_gpu);
2702
2703 if (dri2_dpy->fd_render_gpu != dri2_dpy->fd_display_gpu) {
2704 free(dri2_dpy->device_name);
2705 dri2_dpy->device_name =
2706 loader_get_device_name_for_fd(dri2_dpy->fd_render_gpu);
2707 if (!dri2_dpy->device_name) {
2708 _eglError(EGL_BAD_ALLOC, "wayland-egl: failed to get device name "
2709 "for requested GPU");
2710 goto cleanup;
2711 }
2712 }
2713
2714 /* we have to do the check now, because loader_get_user_preferred_fd
2715 * will return a render-node when the requested gpu is different
2716 * to the server, but also if the client asks for the same gpu than
2717 * the server by requesting its pci-id */
2718 dri2_dpy->is_render_node =
2719 drmGetNodeTypeFromFd(dri2_dpy->fd_render_gpu) == DRM_NODE_RENDER;
2720
2721 if (disp->Options.Zink)
2722 dri2_dpy->driver_name = strdup("zink");
2723 else
2724 dri2_dpy->driver_name = loader_get_driver_for_fd(dri2_dpy->fd_render_gpu);
2725 if (dri2_dpy->driver_name == NULL) {
2726 _eglError(EGL_BAD_ALLOC, "DRI2: failed to get driver name");
2727 goto cleanup;
2728 }
2729
2730 dri2_detect_swrast_kopper(disp);
2731
2732 dri2_dpy->loader_extensions = dri2_dpy->kopper ? kopper_loader_extensions
2733 : dri2_loader_extensions;
2734
2735 if (!dri2_create_screen(disp))
2736 goto cleanup;
2737
2738 if (!dri2_setup_device(disp, false)) {
2739 _eglError(EGL_NOT_INITIALIZED, "DRI2: failed to setup EGLDevice");
2740 goto cleanup;
2741 }
2742
2743 dri2_setup_screen(disp);
2744
2745 dri2_wl_setup_swap_interval(disp);
2746
2747 #ifdef HAVE_BIND_WL_DISPLAY
2748 if (dri2_dpy->wl_drm) {
2749 /* To use Prime, we must have _DRI_IMAGE v7 at least.
2750 * createImageFromDmaBufs support indicates that Prime export/import is
2751 * supported by the driver. We deprecated the support to GEM names API, so
2752 * we bail out if the driver does not support Prime. */
2753 if (!(dri2_dpy->capabilities & WL_DRM_CAPABILITY_PRIME) ||
2754 !dri2_dpy->has_dmabuf_import) {
2755 _eglLog(_EGL_WARNING, "wayland-egl: display does not support prime");
2756 goto cleanup;
2757 }
2758 }
2759
2760 dri2_set_WL_bind_wayland_display(disp);
2761 /* When cannot convert EGLImage to wl_buffer when on a different gpu,
2762 * because the buffer of the EGLImage has likely a tiling mode the server
2763 * gpu won't support. These is no way to check for now. Thus do not support
2764 * the extension */
2765 if (dri2_dpy->fd_render_gpu == dri2_dpy->fd_display_gpu)
2766 disp->Extensions.WL_create_wayland_buffer_from_image = EGL_TRUE;
2767 #endif
2768
2769 dri2_wl_add_configs_for_visuals(disp);
2770
2771 disp->Extensions.EXT_buffer_age = EGL_TRUE;
2772 disp->Extensions.EXT_swap_buffers_with_damage = EGL_TRUE;
2773 disp->Extensions.EXT_present_opaque = EGL_TRUE;
2774
2775 /* Fill vtbl last to prevent accidentally calling virtual function during
2776 * initialization.
2777 */
2778 dri2_dpy->vtbl = dri2_dpy->kopper ? &dri2_wl_kopper_display_vtbl
2779 : &dri2_wl_display_vtbl;
2780
2781 return EGL_TRUE;
2782
2783 cleanup:
2784 return EGL_FALSE;
2785 }
2786
2787 static int
2788 dri2_wl_swrast_get_stride_for_format(int format, int w)
2789 {
2790 int visual_idx = dri2_wl_visual_idx_from_shm_format(format);
2791
2792 assume(visual_idx != -1);
2793
2794 return w * util_format_get_blocksize(dri2_wl_visuals[visual_idx].pipe_format);
2795 }
2796
2797 static EGLBoolean
2798 dri2_wl_swrast_allocate_buffer(struct dri2_egl_surface *dri2_surf, int format,
2799 int w, int h, void **data, int *size,
2800 struct wl_buffer **buffer)
2801 {
2802 struct dri2_egl_display *dri2_dpy =
2803 dri2_egl_display(dri2_surf->base.Resource.Display);
2804 struct wl_shm_pool *pool;
2805 int fd, stride, size_map;
2806 void *data_map;
2807
2808 assert(!*buffer);
2809
2810 stride = dri2_wl_swrast_get_stride_for_format(format, w);
2811 size_map = h * stride;
2812
2813 /* Create a shareable buffer */
2814 fd = os_create_anonymous_file(size_map, NULL);
2815 if (fd < 0)
2816 return EGL_FALSE;
2817
2818 data_map = mmap(NULL, size_map, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
2819 if (data_map == MAP_FAILED) {
2820 close(fd);
2821 return EGL_FALSE;
2822 }
2823
2824 /* Share it in a wl_buffer */
2825 pool = wl_shm_create_pool(dri2_dpy->wl_shm, fd, size_map);
2826 wl_proxy_set_queue((struct wl_proxy *)pool, dri2_surf->wl_queue);
2827 *buffer = wl_shm_pool_create_buffer(pool, 0, w, h, stride, format);
2828 wl_shm_pool_destroy(pool);
2829 close(fd);
2830
2831 *data = data_map;
2832 *size = size_map;
2833 return EGL_TRUE;
2834 }
2835
2836 static int
2837 swrast_update_buffers(struct dri2_egl_surface *dri2_surf)
2838 {
2839 struct dri2_egl_display *dri2_dpy =
2840 dri2_egl_display(dri2_surf->base.Resource.Display);
2841
2842 /* we need to do the following operations only once per frame */
2843 if (dri2_surf->back)
2844 return 0;
2845
2846 if (dri2_surf->wl_win &&
2847 (dri2_surf->base.Width != dri2_surf->wl_win->width ||
2848 dri2_surf->base.Height != dri2_surf->wl_win->height)) {
2849
2850 dri2_wl_release_buffers(dri2_surf);
2851
2852 dri2_surf->base.Width = dri2_surf->wl_win->width;
2853 dri2_surf->base.Height = dri2_surf->wl_win->height;
2854 dri2_surf->dx = dri2_surf->wl_win->dx;
2855 dri2_surf->dy = dri2_surf->wl_win->dy;
2856 dri2_surf->current = NULL;
2857 }
2858
2859 /* find back buffer */
2860 /* There might be a buffer release already queued that wasn't processed */
2861 wl_display_dispatch_queue_pending(dri2_dpy->wl_dpy, dri2_surf->wl_queue);
2862
2863 /* else choose any another free location */
2864 while (!dri2_surf->back) {
2865 for (int i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
2866 if (!dri2_surf->color_buffers[i].locked) {
2867 dri2_surf->back = &dri2_surf->color_buffers[i];
2868 if (dri2_surf->back->wayland_buffer.buffer)
2869 break;
2870
2871 if (!dri2_wl_swrast_allocate_buffer(
2872 dri2_surf, dri2_surf->format, dri2_surf->base.Width,
2873 dri2_surf->base.Height, &dri2_surf->back->data,
2874 &dri2_surf->back->data_size,
2875 &dri2_surf->back->wayland_buffer.buffer)) {
2876 _eglError(EGL_BAD_ALLOC, "failed to allocate color buffer");
2877 return -1;
2878 }
2879 wl_buffer_add_listener(dri2_surf->back->wayland_buffer.buffer,
2880 &wl_buffer_listener, dri2_surf);
2881 break;
2882 }
2883 }
2884
2885 /* wait for the compositor to release a buffer */
2886 if (!dri2_surf->back) {
2887 if (loader_wayland_dispatch(dri2_dpy->wl_dpy, dri2_surf->wl_queue, NULL) ==
2888 -1) {
2889 _eglError(EGL_BAD_ALLOC, "waiting for a free buffer failed");
2890 return -1;
2891 }
2892 }
2893 }
2894
2895 dri2_surf->back->locked = true;
2896
2897 /* If we have an extra unlocked buffer at this point, we had to do triple
2898 * buffering for a while, but now can go back to just double buffering.
2899 * That means we can free any unlocked buffer now. To avoid toggling between
2900 * going back to double buffering and needing to allocate another buffer too
2901 * fast we let the unneeded buffer sit around for a short while. */
2902 for (int i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
2903 if (!dri2_surf->color_buffers[i].locked &&
2904 dri2_surf->color_buffers[i].wayland_buffer.buffer &&
2905 dri2_surf->color_buffers[i].age > BUFFER_TRIM_AGE_HYSTERESIS) {
2906 loader_wayland_buffer_destroy(&dri2_surf->color_buffers[i].wayland_buffer);
2907 munmap(dri2_surf->color_buffers[i].data,
2908 dri2_surf->color_buffers[i].data_size);
2909 dri2_surf->color_buffers[i].data = NULL;
2910 dri2_surf->color_buffers[i].age = 0;
2911 }
2912 }
2913
2914 return 0;
2915 }
2916
2917 static void *
2918 dri2_wl_swrast_get_frontbuffer_data(struct dri2_egl_surface *dri2_surf)
2919 {
2920 /* if there has been a resize: */
2921 if (!dri2_surf->current)
2922 return NULL;
2923
2924 return dri2_surf->current->data;
2925 }
2926
2927 static void *
2928 dri2_wl_swrast_get_backbuffer_data(struct dri2_egl_surface *dri2_surf)
2929 {
2930 assert(dri2_surf->back);
2931 return dri2_surf->back->data;
2932 }
2933
2934 static void
2935 dri2_wl_swrast_commit_backbuffer(struct dri2_egl_surface *dri2_surf)
2936 {
2937 struct dri2_egl_display *dri2_dpy =
2938 dri2_egl_display(dri2_surf->base.Resource.Display);
2939
2940 dri2_surf->wl_win->attached_width = dri2_surf->base.Width;
2941 dri2_surf->wl_win->attached_height = dri2_surf->base.Height;
2942 /* reset resize growing parameters */
2943 dri2_surf->dx = 0;
2944 dri2_surf->dy = 0;
2945
2946 wl_surface_commit(dri2_surf->wayland_surface.wrapper);
2947
2948 /* If we're not waiting for a frame callback then we'll at least throttle
2949 * to a sync callback so that we always give a chance for the compositor to
2950 * handle the commit and send a release event before checking for a free
2951 * buffer */
2952 if (dri2_surf->throttle_callback == NULL) {
2953 dri2_surf->throttle_callback = wl_display_sync(dri2_surf->wl_dpy_wrapper);
2954 wl_callback_add_listener(dri2_surf->throttle_callback, &throttle_listener,
2955 dri2_surf);
2956 }
2957
2958 wl_display_flush(dri2_dpy->wl_dpy);
2959 }
2960
2961 static void
2962 dri2_wl_swrast_get_drawable_info(struct dri_drawable *draw, int *x, int *y, int *w,
2963 int *h, void *loaderPrivate)
2964 {
2965 struct dri2_egl_surface *dri2_surf = loaderPrivate;
2966
2967 if (dri2_surf->base.Type != EGL_PBUFFER_BIT)
2968 (void)swrast_update_buffers(dri2_surf);
2969 *x = 0;
2970 *y = 0;
2971 *w = dri2_surf->base.Width;
2972 *h = dri2_surf->base.Height;
2973 }
2974
2975 static void
2976 dri2_wl_swrast_get_image(struct dri_drawable *read, int x, int y, int w, int h,
2977 char *data, void *loaderPrivate)
2978 {
2979 struct dri2_egl_surface *dri2_surf = loaderPrivate;
2980 int copy_width = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, w);
2981 int x_offset = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, x);
2982 int src_stride = dri2_wl_swrast_get_stride_for_format(dri2_surf->format,
2983 dri2_surf->base.Width);
2984 int dst_stride = copy_width;
2985 char *src, *dst;
2986
2987 src = dri2_wl_swrast_get_frontbuffer_data(dri2_surf);
2988 /* this is already the most up-to-date buffer */
2989 if (src == data)
2990 return;
2991 if (!src) {
2992 memset(data, 0, copy_width * h);
2993 return;
2994 }
2995
2996 assert(copy_width <= src_stride);
2997
2998 src += x_offset;
2999 src += y * src_stride;
3000 dst = data;
3001
3002 if (copy_width > src_stride - x_offset)
3003 copy_width = src_stride - x_offset;
3004 if (h > dri2_surf->base.Height - y)
3005 h = dri2_surf->base.Height - y;
3006
3007 for (; h > 0; h--) {
3008 memcpy(dst, src, copy_width);
3009 src += src_stride;
3010 dst += dst_stride;
3011 }
3012 }
3013
3014 static void
3015 dri2_wl_swrast_put_image2(struct dri_drawable *draw, int op, int x, int y, int w,
3016 int h, int stride, char *data, void *loaderPrivate)
3017 {
3018 struct dri2_egl_surface *dri2_surf = loaderPrivate;
3019 /* clamp to surface size */
3020 w = MIN2(w, dri2_surf->base.Width);
3021 h = MIN2(h, dri2_surf->base.Height);
3022 int copy_width = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, w);
3023 int dst_stride = dri2_wl_swrast_get_stride_for_format(dri2_surf->format,
3024 dri2_surf->base.Width);
3025 int x_offset = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, x);
3026 char *src, *dst;
3027
3028 assert(copy_width <= stride);
3029
3030 dst = dri2_wl_swrast_get_backbuffer_data(dri2_surf);
3031
3032 dst += x_offset;
3033 dst += y * dst_stride;
3034
3035 src = data;
3036
3037 /* drivers expect we do these checks (and some rely on it) */
3038 if (copy_width > dst_stride - x_offset)
3039 copy_width = dst_stride - x_offset;
3040 if (h > dri2_surf->base.Height - y)
3041 h = dri2_surf->base.Height - y;
3042
3043 for (; h > 0; h--) {
3044 memcpy(dst, src, copy_width);
3045 src += stride;
3046 dst += dst_stride;
3047 }
3048 }
3049
3050 static void
3051 dri2_wl_swrast_put_image(struct dri_drawable *draw, int op, int x, int y, int w,
3052 int h, char *data, void *loaderPrivate)
3053 {
3054 struct dri2_egl_surface *dri2_surf = loaderPrivate;
3055 int stride;
3056
3057 stride = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, w);
3058 dri2_wl_swrast_put_image2(draw, op, x, y, w, h, stride, data, loaderPrivate);
3059 }
3060
3061 static EGLBoolean
3062 dri2_wl_swrast_swap_buffers_with_damage(_EGLDisplay *disp, _EGLSurface *draw,
3063 const EGLint *rects, EGLint n_rects)
3064 {
3065 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
3066
3067 if (!dri2_surf->wl_win)
3068 return _eglError(EGL_BAD_NATIVE_WINDOW, "dri2_swap_buffers");
3069
3070 (void)swrast_update_buffers(dri2_surf);
3071
3072 if (dri2_wl_surface_throttle(dri2_surf))
3073 wl_surface_attach(dri2_surf->wayland_surface.wrapper,
3074 /* 'back' here will be promoted to 'current' */
3075 dri2_surf->back->wayland_buffer.buffer, dri2_surf->dx,
3076 dri2_surf->dy);
3077
3078 /* If the compositor doesn't support damage_buffer, we deliberately
3079 * ignore the damage region and post maximum damage, due to
3080 * https://bugs.freedesktop.org/78190 */
3081 if (!try_damage_buffer(dri2_surf, rects, n_rects))
3082 wl_surface_damage(dri2_surf->wayland_surface.wrapper, 0, 0, INT32_MAX,
3083 INT32_MAX);
3084
3085 /* guarantee full copy for partial update */
3086 int w = n_rects == 1 ? (rects[2] - rects[0]) : 0;
3087 int copy_width = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, w);
3088 int dst_stride = dri2_wl_swrast_get_stride_for_format(dri2_surf->format,
3089 dri2_surf->base.Width);
3090 char *dst = dri2_wl_swrast_get_backbuffer_data(dri2_surf);
3091
3092 /* partial copy, copy old content */
3093 if (copy_width < dst_stride)
3094 dri2_wl_swrast_get_image(NULL, 0, 0, dri2_surf->base.Width,
3095 dri2_surf->base.Height, dst, dri2_surf);
3096
3097 if (n_rects)
3098 driSwapBuffersWithDamage(dri2_surf->dri_drawable, n_rects, rects);
3099 else
3100 driSwapBuffers(dri2_surf->dri_drawable);
3101
3102 dri2_surf->current = dri2_surf->back;
3103 dri2_surf->back = NULL;
3104
3105 dri2_wl_swrast_commit_backbuffer(dri2_surf);
3106 return EGL_TRUE;
3107 }
3108
3109 static EGLBoolean
3110 dri2_wl_swrast_swap_buffers(_EGLDisplay *disp, _EGLSurface *draw)
3111 {
3112 dri2_wl_swrast_swap_buffers_with_damage(disp, draw, NULL, 0);
3113 return EGL_TRUE;
3114 }
3115
3116 static EGLint
3117 dri2_wl_swrast_query_buffer_age(_EGLDisplay *disp, _EGLSurface *surface)
3118 {
3119 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3120 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surface);
3121
3122 assert(dri2_dpy->swrast);
3123 return driSWRastQueryBufferAge(dri2_surf->dri_drawable);
3124 }
3125
3126 static void
3127 shm_handle_format(void *data, struct wl_shm *shm, uint32_t format)
3128 {
3129 struct dri2_egl_display *dri2_dpy = data;
3130 int visual_idx = dri2_wl_visual_idx_from_shm_format(format);
3131
3132 if (visual_idx == -1)
3133 return;
3134
3135 BITSET_SET(dri2_dpy->formats.formats_bitmap, visual_idx);
3136 }
3137
3138 static const struct wl_shm_listener shm_listener = {
3139 .format = shm_handle_format,
3140 };
3141
3142 static void
3143 registry_handle_global_swrast(void *data, struct wl_registry *registry,
3144 uint32_t name, const char *interface,
3145 uint32_t version)
3146 {
3147 struct dri2_egl_display *dri2_dpy = data;
3148
3149 if (strcmp(interface, wl_shm_interface.name) == 0) {
3150 dri2_dpy->wl_shm = wl_registry_bind(registry, name, &wl_shm_interface, 1);
3151 wl_shm_add_listener(dri2_dpy->wl_shm, &shm_listener, dri2_dpy);
3152 } else if (strcmp(interface, wp_presentation_interface.name) == 0) {
3153 dri2_dpy->wp_presentation =
3154 wl_registry_bind(registry, name, &wp_presentation_interface, 1);
3155 wp_presentation_add_listener(dri2_dpy->wp_presentation,
3156 &presentation_listener, dri2_dpy);
3157 #ifdef WL_FIXES_INTERFACE
3158 } else if (strcmp(interface, wl_fixes_interface.name) == 0) {
3159 dri2_dpy->wl_fixes = wl_registry_bind(registry, name, &wl_fixes_interface, 1);
3160 #endif
3161 }
3162
3163 }
3164
3165 static const struct wl_registry_listener registry_listener_swrast = {
3166 .global = registry_handle_global_swrast,
3167 .global_remove = registry_handle_global_remove,
3168 };
3169
3170 static const struct dri2_egl_display_vtbl dri2_wl_swrast_display_vtbl = {
3171 .authenticate = NULL,
3172 .create_window_surface = dri2_wl_create_window_surface,
3173 .create_pixmap_surface = dri2_wl_create_pixmap_surface,
3174 .create_pbuffer_surface = dri2_wl_create_pbuffer_surface,
3175 .destroy_surface = dri2_wl_destroy_surface,
3176 .swap_interval = dri2_wl_swap_interval,
3177 .create_image = dri2_create_image_khr,
3178 .swap_buffers = dri2_wl_swrast_swap_buffers,
3179 .swap_buffers_with_damage = dri2_wl_swrast_swap_buffers_with_damage,
3180 .get_dri_drawable = dri2_surface_get_dri_drawable,
3181 .query_buffer_age = dri2_wl_swrast_query_buffer_age,
3182 };
3183
3184 static const __DRIswrastLoaderExtension swrast_loader_extension = {
3185 .base = {__DRI_SWRAST_LOADER, 2},
3186
3187 .getDrawableInfo = dri2_wl_swrast_get_drawable_info,
3188 .putImage = dri2_wl_swrast_put_image,
3189 .getImage = dri2_wl_swrast_get_image,
3190 .putImage2 = dri2_wl_swrast_put_image2,
3191 };
3192
3193 static const __DRIextension *swrast_loader_extensions[] = {
3194 &swrast_loader_extension.base,
3195 &image_lookup_extension.base,
3196 NULL,
3197 };
3198
3199 static EGLBoolean
3200 dri2_initialize_wayland_swrast(_EGLDisplay *disp)
3201 {
3202 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
3203
3204 if (dri2_wl_formats_init(&dri2_dpy->formats) < 0)
3205 goto cleanup;
3206
3207 if (disp->PlatformDisplay == NULL) {
3208 dri2_dpy->wl_dpy = wl_display_connect(NULL);
3209 if (dri2_dpy->wl_dpy == NULL)
3210 goto cleanup;
3211 dri2_dpy->own_device = true;
3212 } else {
3213 dri2_dpy->wl_dpy = disp->PlatformDisplay;
3214 }
3215
3216 dri2_dpy->wl_queue = wl_display_create_queue_with_name(dri2_dpy->wl_dpy,
3217 "mesa egl swrast display queue");
3218
3219 dri2_dpy->wl_dpy_wrapper = wl_proxy_create_wrapper(dri2_dpy->wl_dpy);
3220 if (dri2_dpy->wl_dpy_wrapper == NULL)
3221 goto cleanup;
3222
3223 wl_proxy_set_queue((struct wl_proxy *)dri2_dpy->wl_dpy_wrapper,
3224 dri2_dpy->wl_queue);
3225
3226 if (dri2_dpy->own_device)
3227 wl_display_dispatch_pending(dri2_dpy->wl_dpy);
3228
3229 dri2_dpy->wl_registry = wl_display_get_registry(dri2_dpy->wl_dpy_wrapper);
3230 wl_registry_add_listener(dri2_dpy->wl_registry, &registry_listener_swrast,
3231 dri2_dpy);
3232
3233 if (roundtrip(dri2_dpy) < 0 || dri2_dpy->wl_shm == NULL)
3234 goto cleanup;
3235
3236 if (roundtrip(dri2_dpy) < 0 ||
3237 !BITSET_TEST_RANGE(dri2_dpy->formats.formats_bitmap, 0,
3238 dri2_dpy->formats.num_formats))
3239 goto cleanup;
3240
3241 dri2_dpy->driver_name = strdup(disp->Options.Zink ? "zink" : "swrast");
3242 dri2_detect_swrast_kopper(disp);
3243
3244 dri2_dpy->loader_extensions = dri2_dpy->kopper ? kopper_loader_extensions
3245 : swrast_loader_extensions;
3246
3247 if (!dri2_create_screen(disp))
3248 goto cleanup;
3249
3250 if (!dri2_setup_device(disp, disp->Options.ForceSoftware)) {
3251 _eglError(EGL_NOT_INITIALIZED, "DRI2: failed to setup EGLDevice");
3252 goto cleanup;
3253 }
3254
3255 dri2_setup_screen(disp);
3256
3257 dri2_wl_setup_swap_interval(disp);
3258
3259 dri2_wl_add_configs_for_visuals(disp);
3260
3261 #ifdef HAVE_BIND_WL_DISPLAY
3262 if (disp->Options.Zink && dri2_dpy->fd_render_gpu >= 0 &&
3263 (dri2_dpy->wl_dmabuf || dri2_dpy->wl_drm))
3264 dri2_set_WL_bind_wayland_display(disp);
3265 #endif
3266 disp->Extensions.EXT_buffer_age = EGL_TRUE;
3267 disp->Extensions.EXT_swap_buffers_with_damage = EGL_TRUE;
3268 disp->Extensions.EXT_present_opaque = EGL_TRUE;
3269
3270 /* Fill vtbl last to prevent accidentally calling virtual function during
3271 * initialization.
3272 */
3273 dri2_dpy->vtbl = dri2_dpy->kopper ? &dri2_wl_kopper_display_vtbl
3274 : &dri2_wl_swrast_display_vtbl;
3275
3276 return EGL_TRUE;
3277
3278 cleanup:
3279 return EGL_FALSE;
3280 }
3281
3282 EGLBoolean
3283 dri2_initialize_wayland(_EGLDisplay *disp)
3284 {
3285 if (disp->Options.ForceSoftware)
3286 return dri2_initialize_wayland_swrast(disp);
3287 else
3288 return dri2_initialize_wayland_drm(disp);
3289 }
3290
3291 void
3292 dri2_teardown_wayland(struct dri2_egl_display *dri2_dpy)
3293 {
3294 dri2_wl_formats_fini(&dri2_dpy->formats);
3295 if (dri2_dpy->wp_presentation)
3296 wp_presentation_destroy(dri2_dpy->wp_presentation);
3297 #ifdef HAVE_BIND_WL_DISPLAY
3298 if (dri2_dpy->wl_drm)
3299 wl_drm_destroy(dri2_dpy->wl_drm);
3300 #endif
3301 if (dri2_dpy->wl_dmabuf)
3302 zwp_linux_dmabuf_v1_destroy(dri2_dpy->wl_dmabuf);
3303 if (dri2_dpy->wl_shm)
3304 wl_shm_destroy(dri2_dpy->wl_shm);
3305 if (dri2_dpy->wl_registry) {
3306 #ifdef WL_FIXES_INTERFACE
3307 if (dri2_dpy->wl_fixes)
3308 wl_fixes_destroy_registry(dri2_dpy->wl_fixes, dri2_dpy->wl_registry);
3309 #endif
3310 wl_registry_destroy(dri2_dpy->wl_registry);
3311 }
3312 #ifdef WL_FIXES_INTERFACE
3313 if (dri2_dpy->wl_fixes)
3314 wl_fixes_destroy(dri2_dpy->wl_fixes);
3315 #endif
3316 if (dri2_dpy->wl_dpy_wrapper)
3317 wl_proxy_wrapper_destroy(dri2_dpy->wl_dpy_wrapper);
3318 if (dri2_dpy->wl_queue)
3319 wl_event_queue_destroy(dri2_dpy->wl_queue);
3320
3321 if (dri2_dpy->own_device)
3322 wl_display_disconnect(dri2_dpy->wl_dpy);
3323 }