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