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