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