71bcb04a77b9d8230947bb5858a0c1e646fe034d
[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 unsigned
816 dri2_wl_get_capability(void *loaderPrivate, enum dri_loader_cap cap)
817 {
818 switch (cap) {
819 case DRI_LOADER_CAP_FP16:
820 return 1;
821 default:
822 return 0;
823 }
824 }
825
826 static const __DRIdri2LoaderExtension dri2_loader_extension = {
827 .base = { __DRI_DRI2_LOADER, 4 },
828
829 .getBuffers = dri2_wl_get_buffers,
830 .flushFrontBuffer = dri2_wl_flush_front_buffer,
831 .getBuffersWithFormat = dri2_wl_get_buffers_with_format,
832 .getCapability = dri2_wl_get_capability,
833 };
834
835 static const __DRIimageLoaderExtension image_loader_extension = {
836 .base = { __DRI_IMAGE_LOADER, 2 },
837
838 .getBuffers = image_get_buffers,
839 .flushFrontBuffer = dri2_wl_flush_front_buffer,
840 .getCapability = dri2_wl_get_capability,
841 };
842
843 static void
844 wayland_throttle_callback(void *data,
845 struct wl_callback *callback,
846 uint32_t time)
847 {
848 struct dri2_egl_surface *dri2_surf = data;
849
850 dri2_surf->throttle_callback = NULL;
851 wl_callback_destroy(callback);
852 }
853
854 static const struct wl_callback_listener throttle_listener = {
855 .done = wayland_throttle_callback
856 };
857
858 static EGLBoolean
859 get_fourcc(struct dri2_egl_display *dri2_dpy,
860 __DRIimage *image, int *fourcc)
861 {
862 EGLBoolean query;
863 int dri_format;
864 int visual_idx;
865
866 query = dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_FOURCC,
867 fourcc);
868 if (query)
869 return true;
870
871 query = dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_FORMAT,
872 &dri_format);
873 if (!query)
874 return false;
875
876 visual_idx = dri2_wl_visual_idx_from_dri_image_format(dri_format);
877 if (visual_idx == -1)
878 return false;
879
880 *fourcc = dri2_wl_visuals[visual_idx].wl_drm_format;
881 return true;
882 }
883
884 static struct wl_buffer *
885 create_wl_buffer(struct dri2_egl_display *dri2_dpy,
886 struct dri2_egl_surface *dri2_surf,
887 __DRIimage *image)
888 {
889 struct wl_buffer *ret;
890 EGLBoolean query;
891 int width, height, fourcc, num_planes;
892 uint64_t modifier = DRM_FORMAT_MOD_INVALID;
893
894 query = dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_WIDTH, &width);
895 query &= dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_HEIGHT,
896 &height);
897 query &= get_fourcc(dri2_dpy, image, &fourcc);
898 if (!query)
899 return NULL;
900
901 query = dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_NUM_PLANES,
902 &num_planes);
903 if (!query)
904 num_planes = 1;
905
906 if (dri2_dpy->image->base.version >= 15) {
907 int mod_hi, mod_lo;
908
909 query = dri2_dpy->image->queryImage(image,
910 __DRI_IMAGE_ATTRIB_MODIFIER_UPPER,
911 &mod_hi);
912 query &= dri2_dpy->image->queryImage(image,
913 __DRI_IMAGE_ATTRIB_MODIFIER_LOWER,
914 &mod_lo);
915 if (query) {
916 modifier = combine_u32_into_u64(mod_hi, mod_lo);
917 }
918 }
919
920 if (dri2_dpy->wl_dmabuf && modifier != DRM_FORMAT_MOD_INVALID) {
921 struct zwp_linux_buffer_params_v1 *params;
922 int i;
923
924 /* We don't need a wrapper for wl_dmabuf objects, because we have to
925 * create the intermediate params object; we can set the queue on this,
926 * and the wl_buffer inherits it race-free. */
927 params = zwp_linux_dmabuf_v1_create_params(dri2_dpy->wl_dmabuf);
928 if (dri2_surf)
929 wl_proxy_set_queue((struct wl_proxy *) params, dri2_surf->wl_queue);
930
931 for (i = 0; i < num_planes; i++) {
932 __DRIimage *p_image;
933 int stride, offset;
934 int fd = -1;
935
936 p_image = dri2_dpy->image->fromPlanar(image, i, NULL);
937 if (!p_image) {
938 assert(i == 0);
939 p_image = image;
940 }
941
942 query = dri2_dpy->image->queryImage(p_image,
943 __DRI_IMAGE_ATTRIB_FD,
944 &fd);
945 query &= dri2_dpy->image->queryImage(p_image,
946 __DRI_IMAGE_ATTRIB_STRIDE,
947 &stride);
948 query &= dri2_dpy->image->queryImage(p_image,
949 __DRI_IMAGE_ATTRIB_OFFSET,
950 &offset);
951 if (image != p_image)
952 dri2_dpy->image->destroyImage(p_image);
953
954 if (!query) {
955 if (fd >= 0)
956 close(fd);
957 zwp_linux_buffer_params_v1_destroy(params);
958 return NULL;
959 }
960
961 zwp_linux_buffer_params_v1_add(params, fd, i, offset, stride,
962 modifier >> 32, modifier & 0xffffffff);
963 close(fd);
964 }
965
966 ret = zwp_linux_buffer_params_v1_create_immed(params, width, height,
967 fourcc, 0);
968 zwp_linux_buffer_params_v1_destroy(params);
969 } else if (dri2_dpy->capabilities & WL_DRM_CAPABILITY_PRIME) {
970 struct wl_drm *wl_drm =
971 dri2_surf ? dri2_surf->wl_drm_wrapper : dri2_dpy->wl_drm;
972 int fd, stride;
973
974 if (num_planes > 1)
975 return NULL;
976
977 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_FD, &fd);
978 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_STRIDE, &stride);
979 ret = wl_drm_create_prime_buffer(wl_drm, fd, width, height, fourcc, 0,
980 stride, 0, 0, 0, 0);
981 close(fd);
982 } else {
983 struct wl_drm *wl_drm =
984 dri2_surf ? dri2_surf->wl_drm_wrapper : dri2_dpy->wl_drm;
985 int name, stride;
986
987 if (num_planes > 1)
988 return NULL;
989
990 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_NAME, &name);
991 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_STRIDE, &stride);
992 ret = wl_drm_create_buffer(wl_drm, name, width, height, stride, fourcc);
993 }
994
995 return ret;
996 }
997
998 static EGLBoolean
999 try_damage_buffer(struct dri2_egl_surface *dri2_surf,
1000 const EGLint *rects,
1001 EGLint n_rects)
1002 {
1003 if (wl_proxy_get_version((struct wl_proxy *) dri2_surf->wl_surface_wrapper)
1004 < WL_SURFACE_DAMAGE_BUFFER_SINCE_VERSION)
1005 return EGL_FALSE;
1006
1007 for (int i = 0; i < n_rects; i++) {
1008 const int *rect = &rects[i * 4];
1009
1010 wl_surface_damage_buffer(dri2_surf->wl_surface_wrapper,
1011 rect[0],
1012 dri2_surf->base.Height - rect[1] - rect[3],
1013 rect[2], rect[3]);
1014 }
1015 return EGL_TRUE;
1016 }
1017 /**
1018 * Called via eglSwapBuffers(), drv->API.SwapBuffers().
1019 */
1020 static EGLBoolean
1021 dri2_wl_swap_buffers_with_damage(_EGLDriver *drv,
1022 _EGLDisplay *disp,
1023 _EGLSurface *draw,
1024 const EGLint *rects,
1025 EGLint n_rects)
1026 {
1027 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1028 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
1029
1030 while (dri2_surf->throttle_callback != NULL)
1031 if (wl_display_dispatch_queue(dri2_dpy->wl_dpy,
1032 dri2_surf->wl_queue) == -1)
1033 return -1;
1034
1035 for (int i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++)
1036 if (dri2_surf->color_buffers[i].age > 0)
1037 dri2_surf->color_buffers[i].age++;
1038
1039 /* Make sure we have a back buffer in case we're swapping without ever
1040 * rendering. */
1041 if (update_buffers_if_needed(dri2_surf) < 0)
1042 return _eglError(EGL_BAD_ALLOC, "dri2_swap_buffers");
1043
1044 if (draw->SwapInterval > 0) {
1045 dri2_surf->throttle_callback =
1046 wl_surface_frame(dri2_surf->wl_surface_wrapper);
1047 wl_callback_add_listener(dri2_surf->throttle_callback,
1048 &throttle_listener, dri2_surf);
1049 }
1050
1051 dri2_surf->back->age = 1;
1052 dri2_surf->current = dri2_surf->back;
1053 dri2_surf->back = NULL;
1054
1055 if (!dri2_surf->current->wl_buffer) {
1056 __DRIimage *image;
1057
1058 if (dri2_dpy->is_different_gpu)
1059 image = dri2_surf->current->linear_copy;
1060 else
1061 image = dri2_surf->current->dri_image;
1062
1063 dri2_surf->current->wl_buffer =
1064 create_wl_buffer(dri2_dpy, dri2_surf, image);
1065
1066 dri2_surf->current->wl_release = false;
1067
1068 wl_buffer_add_listener(dri2_surf->current->wl_buffer,
1069 &wl_buffer_listener, dri2_surf);
1070 }
1071
1072 wl_surface_attach(dri2_surf->wl_surface_wrapper,
1073 dri2_surf->current->wl_buffer,
1074 dri2_surf->dx, dri2_surf->dy);
1075
1076 dri2_surf->wl_win->attached_width = dri2_surf->base.Width;
1077 dri2_surf->wl_win->attached_height = dri2_surf->base.Height;
1078 /* reset resize growing parameters */
1079 dri2_surf->dx = 0;
1080 dri2_surf->dy = 0;
1081
1082 /* If the compositor doesn't support damage_buffer, we deliberately
1083 * ignore the damage region and post maximum damage, due to
1084 * https://bugs.freedesktop.org/78190 */
1085 if (!n_rects || !try_damage_buffer(dri2_surf, rects, n_rects))
1086 wl_surface_damage(dri2_surf->wl_surface_wrapper,
1087 0, 0, INT32_MAX, INT32_MAX);
1088
1089 if (dri2_dpy->is_different_gpu) {
1090 _EGLContext *ctx = _eglGetCurrentContext();
1091 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
1092 dri2_dpy->image->blitImage(dri2_ctx->dri_context,
1093 dri2_surf->current->linear_copy,
1094 dri2_surf->current->dri_image,
1095 0, 0, dri2_surf->base.Width,
1096 dri2_surf->base.Height,
1097 0, 0, dri2_surf->base.Width,
1098 dri2_surf->base.Height, 0);
1099 }
1100
1101 dri2_flush_drawable_for_swapbuffers(disp, draw);
1102 dri2_dpy->flush->invalidate(dri2_surf->dri_drawable);
1103
1104 wl_surface_commit(dri2_surf->wl_surface_wrapper);
1105
1106 /* If we're not waiting for a frame callback then we'll at least throttle
1107 * to a sync callback so that we always give a chance for the compositor to
1108 * handle the commit and send a release event before checking for a free
1109 * buffer */
1110 if (dri2_surf->throttle_callback == NULL) {
1111 dri2_surf->throttle_callback = wl_display_sync(dri2_surf->wl_dpy_wrapper);
1112 wl_callback_add_listener(dri2_surf->throttle_callback,
1113 &throttle_listener, dri2_surf);
1114 }
1115
1116 wl_display_flush(dri2_dpy->wl_dpy);
1117
1118 return EGL_TRUE;
1119 }
1120
1121 static EGLint
1122 dri2_wl_query_buffer_age(_EGLDriver *drv,
1123 _EGLDisplay *disp, _EGLSurface *surface)
1124 {
1125 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surface);
1126
1127 if (update_buffers_if_needed(dri2_surf) < 0) {
1128 _eglError(EGL_BAD_ALLOC, "dri2_query_buffer_age");
1129 return -1;
1130 }
1131
1132 return dri2_surf->back->age;
1133 }
1134
1135 static EGLBoolean
1136 dri2_wl_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
1137 {
1138 return dri2_wl_swap_buffers_with_damage(drv, disp, draw, NULL, 0);
1139 }
1140
1141 static struct wl_buffer *
1142 dri2_wl_create_wayland_buffer_from_image(_EGLDriver *drv,
1143 _EGLDisplay *disp,
1144 _EGLImage *img)
1145 {
1146 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1147 struct dri2_egl_image *dri2_img = dri2_egl_image(img);
1148 __DRIimage *image = dri2_img->dri_image;
1149 struct wl_buffer *buffer;
1150 int format, visual_idx;
1151
1152 /* Check the upstream display supports this buffer's format. */
1153 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_FORMAT, &format);
1154 visual_idx = dri2_wl_visual_idx_from_dri_image_format(format);
1155 if (visual_idx == -1)
1156 goto bad_format;
1157
1158 if (!BITSET_TEST(dri2_dpy->formats, visual_idx))
1159 goto bad_format;
1160
1161 buffer = create_wl_buffer(dri2_dpy, NULL, image);
1162
1163 /* The buffer object will have been created with our internal event queue
1164 * because it is using wl_dmabuf/wl_drm as a proxy factory. We want the
1165 * buffer to be used by the application so we'll reset it to the display's
1166 * default event queue. This isn't actually racy, as the only event the
1167 * buffer can get is a buffer release, which doesn't happen with an explicit
1168 * attach. */
1169 if (buffer)
1170 wl_proxy_set_queue((struct wl_proxy *) buffer, NULL);
1171
1172 return buffer;
1173
1174 bad_format:
1175 _eglError(EGL_BAD_MATCH, "unsupported image format");
1176 return NULL;
1177 }
1178
1179 static int
1180 dri2_wl_authenticate(_EGLDisplay *disp, uint32_t id)
1181 {
1182 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1183 int ret = 0;
1184
1185 if (dri2_dpy->is_render_node) {
1186 _eglLog(_EGL_WARNING, "wayland-egl: client asks server to "
1187 "authenticate for render-nodes");
1188 return 0;
1189 }
1190 dri2_dpy->authenticated = false;
1191
1192 wl_drm_authenticate(dri2_dpy->wl_drm, id);
1193 if (roundtrip(dri2_dpy) < 0)
1194 ret = -1;
1195
1196 if (!dri2_dpy->authenticated)
1197 ret = -1;
1198
1199 /* reset authenticated */
1200 dri2_dpy->authenticated = true;
1201
1202 return ret;
1203 }
1204
1205 static void
1206 drm_handle_device(void *data, struct wl_drm *drm, const char *device)
1207 {
1208 struct dri2_egl_display *dri2_dpy = data;
1209 drm_magic_t magic;
1210
1211 dri2_dpy->device_name = strdup(device);
1212 if (!dri2_dpy->device_name)
1213 return;
1214
1215 dri2_dpy->fd = loader_open_device(dri2_dpy->device_name);
1216 if (dri2_dpy->fd == -1) {
1217 _eglLog(_EGL_WARNING, "wayland-egl: could not open %s (%s)",
1218 dri2_dpy->device_name, strerror(errno));
1219 free(dri2_dpy->device_name);
1220 dri2_dpy->device_name = NULL;
1221 return;
1222 }
1223
1224 if (drmGetNodeTypeFromFd(dri2_dpy->fd) == DRM_NODE_RENDER) {
1225 dri2_dpy->authenticated = true;
1226 } else {
1227 if (drmGetMagic(dri2_dpy->fd, &magic)) {
1228 close(dri2_dpy->fd);
1229 dri2_dpy->fd = -1;
1230 free(dri2_dpy->device_name);
1231 dri2_dpy->device_name = NULL;
1232 _eglLog(_EGL_WARNING, "wayland-egl: drmGetMagic failed");
1233 return;
1234 }
1235 wl_drm_authenticate(dri2_dpy->wl_drm, magic);
1236 }
1237 }
1238
1239 static void
1240 drm_handle_format(void *data, struct wl_drm *drm, uint32_t format)
1241 {
1242 struct dri2_egl_display *dri2_dpy = data;
1243 int visual_idx = dri2_wl_visual_idx_from_fourcc(format);
1244
1245 if (visual_idx == -1)
1246 return;
1247
1248 BITSET_SET(dri2_dpy->formats, visual_idx);
1249 }
1250
1251 static void
1252 drm_handle_capabilities(void *data, struct wl_drm *drm, uint32_t value)
1253 {
1254 struct dri2_egl_display *dri2_dpy = data;
1255
1256 dri2_dpy->capabilities = value;
1257 }
1258
1259 static void
1260 drm_handle_authenticated(void *data, struct wl_drm *drm)
1261 {
1262 struct dri2_egl_display *dri2_dpy = data;
1263
1264 dri2_dpy->authenticated = true;
1265 }
1266
1267 static const struct wl_drm_listener drm_listener = {
1268 .device = drm_handle_device,
1269 .format = drm_handle_format,
1270 .authenticated = drm_handle_authenticated,
1271 .capabilities = drm_handle_capabilities
1272 };
1273
1274 static void
1275 dmabuf_ignore_format(void *data, struct zwp_linux_dmabuf_v1 *dmabuf,
1276 uint32_t format)
1277 {
1278 /* formats are implicitly advertised by the 'modifier' event, so ignore */
1279 }
1280
1281 static void
1282 dmabuf_handle_modifier(void *data, struct zwp_linux_dmabuf_v1 *dmabuf,
1283 uint32_t format, uint32_t modifier_hi,
1284 uint32_t modifier_lo)
1285 {
1286 struct dri2_egl_display *dri2_dpy = data;
1287 int visual_idx = dri2_wl_visual_idx_from_fourcc(format);
1288 uint64_t *mod;
1289
1290 if (visual_idx == -1)
1291 return;
1292
1293 if (modifier_hi == (DRM_FORMAT_MOD_INVALID >> 32) &&
1294 modifier_lo == (DRM_FORMAT_MOD_INVALID & 0xffffffff))
1295 return;
1296
1297 BITSET_SET(dri2_dpy->formats, visual_idx);
1298
1299 mod = u_vector_add(&dri2_dpy->wl_modifiers[visual_idx]);
1300 *mod = combine_u32_into_u64(modifier_hi, modifier_lo);
1301 }
1302
1303 static const struct zwp_linux_dmabuf_v1_listener dmabuf_listener = {
1304 .format = dmabuf_ignore_format,
1305 .modifier = dmabuf_handle_modifier,
1306 };
1307
1308 static void
1309 registry_handle_global_drm(void *data, struct wl_registry *registry,
1310 uint32_t name, const char *interface,
1311 uint32_t version)
1312 {
1313 struct dri2_egl_display *dri2_dpy = data;
1314
1315 if (strcmp(interface, "wl_drm") == 0) {
1316 dri2_dpy->wl_drm =
1317 wl_registry_bind(registry, name, &wl_drm_interface, MIN2(version, 2));
1318 wl_drm_add_listener(dri2_dpy->wl_drm, &drm_listener, dri2_dpy);
1319 } else if (strcmp(interface, "zwp_linux_dmabuf_v1") == 0 && version >= 3) {
1320 dri2_dpy->wl_dmabuf =
1321 wl_registry_bind(registry, name, &zwp_linux_dmabuf_v1_interface,
1322 MIN2(version, 3));
1323 zwp_linux_dmabuf_v1_add_listener(dri2_dpy->wl_dmabuf, &dmabuf_listener,
1324 dri2_dpy);
1325 }
1326 }
1327
1328 static void
1329 registry_handle_global_remove(void *data, struct wl_registry *registry,
1330 uint32_t name)
1331 {
1332 }
1333
1334 static const struct wl_registry_listener registry_listener_drm = {
1335 .global = registry_handle_global_drm,
1336 .global_remove = registry_handle_global_remove
1337 };
1338
1339 static void
1340 dri2_wl_setup_swap_interval(_EGLDisplay *disp)
1341 {
1342 /* We can't use values greater than 1 on Wayland because we are using the
1343 * frame callback to synchronise the frame and the only way we be sure to
1344 * get a frame callback is to attach a new buffer. Therefore we can't just
1345 * sit drawing nothing to wait until the next ‘n’ frame callbacks */
1346
1347 dri2_setup_swap_interval(disp, 1);
1348 }
1349
1350 static const struct dri2_egl_display_vtbl dri2_wl_display_vtbl = {
1351 .authenticate = dri2_wl_authenticate,
1352 .create_window_surface = dri2_wl_create_window_surface,
1353 .create_pixmap_surface = dri2_wl_create_pixmap_surface,
1354 .create_pbuffer_surface = dri2_fallback_create_pbuffer_surface,
1355 .destroy_surface = dri2_wl_destroy_surface,
1356 .create_image = dri2_create_image_khr,
1357 .swap_buffers = dri2_wl_swap_buffers,
1358 .swap_buffers_with_damage = dri2_wl_swap_buffers_with_damage,
1359 .swap_buffers_region = dri2_fallback_swap_buffers_region,
1360 .post_sub_buffer = dri2_fallback_post_sub_buffer,
1361 .copy_buffers = dri2_fallback_copy_buffers,
1362 .query_buffer_age = dri2_wl_query_buffer_age,
1363 .create_wayland_buffer_from_image = dri2_wl_create_wayland_buffer_from_image,
1364 .get_sync_values = dri2_fallback_get_sync_values,
1365 .get_dri_drawable = dri2_surface_get_dri_drawable,
1366 };
1367
1368 static const __DRIextension *dri2_loader_extensions[] = {
1369 &dri2_loader_extension.base,
1370 &image_loader_extension.base,
1371 &image_lookup_extension.base,
1372 &use_invalidate.base,
1373 NULL,
1374 };
1375
1376 static const __DRIextension *image_loader_extensions[] = {
1377 &image_loader_extension.base,
1378 &image_lookup_extension.base,
1379 &use_invalidate.base,
1380 NULL,
1381 };
1382
1383 static EGLBoolean
1384 dri2_wl_add_configs_for_visuals(_EGLDriver *drv, _EGLDisplay *disp)
1385 {
1386 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1387 unsigned int format_count[ARRAY_SIZE(dri2_wl_visuals)] = { 0 };
1388 unsigned int count = 0;
1389 bool assigned;
1390
1391 for (unsigned i = 0; dri2_dpy->driver_configs[i]; i++) {
1392 assigned = false;
1393
1394 for (unsigned j = 0; j < ARRAY_SIZE(dri2_wl_visuals); j++) {
1395 struct dri2_egl_config *dri2_conf;
1396
1397 if (!BITSET_TEST(dri2_dpy->formats, j))
1398 continue;
1399
1400 dri2_conf = dri2_add_config(disp, dri2_dpy->driver_configs[i],
1401 count + 1, EGL_WINDOW_BIT, NULL, dri2_wl_visuals[j].rgba_shifts, dri2_wl_visuals[j].rgba_sizes);
1402 if (dri2_conf) {
1403 if (dri2_conf->base.ConfigID == count + 1)
1404 count++;
1405 format_count[j]++;
1406 assigned = true;
1407 }
1408 }
1409
1410 if (!assigned && dri2_dpy->is_different_gpu) {
1411 struct dri2_egl_config *dri2_conf;
1412 int alt_dri_image_format, c, s;
1413
1414 /* No match for config. Try if we can blitImage convert to a visual */
1415 c = dri2_wl_visual_idx_from_config(dri2_dpy,
1416 dri2_dpy->driver_configs[i]);
1417
1418 if (c == -1)
1419 continue;
1420
1421 /* Find optimal target visual for blitImage conversion, if any. */
1422 alt_dri_image_format = dri2_wl_visuals[c].alt_dri_image_format;
1423 s = dri2_wl_visual_idx_from_dri_image_format(alt_dri_image_format);
1424
1425 if (s == -1 || !BITSET_TEST(dri2_dpy->formats, s))
1426 continue;
1427
1428 /* Visual s works for the Wayland server, and c can be converted into s
1429 * by our client gpu during PRIME blitImage conversion to a linear
1430 * wl_buffer, so add visual c as supported by the client renderer.
1431 */
1432 dri2_conf = dri2_add_config(disp, dri2_dpy->driver_configs[i],
1433 count + 1, EGL_WINDOW_BIT, NULL,
1434 dri2_wl_visuals[c].rgba_shifts,
1435 dri2_wl_visuals[c].rgba_sizes);
1436 if (dri2_conf) {
1437 if (dri2_conf->base.ConfigID == count + 1)
1438 count++;
1439 format_count[c]++;
1440 if (format_count[c] == 1)
1441 _eglLog(_EGL_DEBUG, "Client format %s to server format %s via "
1442 "PRIME blitImage.", dri2_wl_visuals[c].format_name,
1443 dri2_wl_visuals[s].format_name);
1444 }
1445 }
1446 }
1447
1448 for (unsigned i = 0; i < ARRAY_SIZE(format_count); i++) {
1449 if (!format_count[i]) {
1450 _eglLog(_EGL_DEBUG, "No DRI config supports native format %s",
1451 dri2_wl_visuals[i].format_name);
1452 }
1453 }
1454
1455 return (count != 0);
1456 }
1457
1458 static EGLBoolean
1459 dri2_initialize_wayland_drm(_EGLDriver *drv, _EGLDisplay *disp)
1460 {
1461 _EGLDevice *dev;
1462 struct dri2_egl_display *dri2_dpy;
1463
1464 dri2_dpy = calloc(1, sizeof *dri2_dpy);
1465 if (!dri2_dpy)
1466 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
1467
1468 dri2_dpy->fd = -1;
1469 disp->DriverData = (void *) dri2_dpy;
1470 if (disp->PlatformDisplay == NULL) {
1471 dri2_dpy->wl_dpy = wl_display_connect(NULL);
1472 if (dri2_dpy->wl_dpy == NULL)
1473 goto cleanup;
1474 dri2_dpy->own_device = true;
1475 } else {
1476 dri2_dpy->wl_dpy = disp->PlatformDisplay;
1477 }
1478
1479 dri2_dpy->wl_modifiers =
1480 calloc(ARRAY_SIZE(dri2_wl_visuals), sizeof(*dri2_dpy->wl_modifiers));
1481 if (!dri2_dpy->wl_modifiers)
1482 goto cleanup;
1483 for (int i = 0; i < ARRAY_SIZE(dri2_wl_visuals); i++) {
1484 if (!u_vector_init(&dri2_dpy->wl_modifiers[i], sizeof(uint64_t), 32))
1485 goto cleanup;
1486 }
1487
1488 dri2_dpy->wl_queue = wl_display_create_queue(dri2_dpy->wl_dpy);
1489
1490 dri2_dpy->wl_dpy_wrapper = wl_proxy_create_wrapper(dri2_dpy->wl_dpy);
1491 if (dri2_dpy->wl_dpy_wrapper == NULL)
1492 goto cleanup;
1493
1494 wl_proxy_set_queue((struct wl_proxy *) dri2_dpy->wl_dpy_wrapper,
1495 dri2_dpy->wl_queue);
1496
1497 if (dri2_dpy->own_device)
1498 wl_display_dispatch_pending(dri2_dpy->wl_dpy);
1499
1500 dri2_dpy->wl_registry = wl_display_get_registry(dri2_dpy->wl_dpy_wrapper);
1501 wl_registry_add_listener(dri2_dpy->wl_registry,
1502 &registry_listener_drm, dri2_dpy);
1503 if (roundtrip(dri2_dpy) < 0 || dri2_dpy->wl_drm == NULL)
1504 goto cleanup;
1505
1506 if (roundtrip(dri2_dpy) < 0 || dri2_dpy->fd == -1)
1507 goto cleanup;
1508
1509 if (roundtrip(dri2_dpy) < 0 || !dri2_dpy->authenticated)
1510 goto cleanup;
1511
1512 dri2_dpy->fd = loader_get_user_preferred_fd(dri2_dpy->fd,
1513 &dri2_dpy->is_different_gpu);
1514 dev = _eglAddDevice(dri2_dpy->fd, false);
1515 if (!dev) {
1516 _eglError(EGL_NOT_INITIALIZED, "DRI2: failed to find EGLDevice");
1517 goto cleanup;
1518 }
1519
1520 disp->Device = dev;
1521
1522 if (dri2_dpy->is_different_gpu) {
1523 free(dri2_dpy->device_name);
1524 dri2_dpy->device_name = loader_get_device_name_for_fd(dri2_dpy->fd);
1525 if (!dri2_dpy->device_name) {
1526 _eglError(EGL_BAD_ALLOC, "wayland-egl: failed to get device name "
1527 "for requested GPU");
1528 goto cleanup;
1529 }
1530 }
1531
1532 /* we have to do the check now, because loader_get_user_preferred_fd
1533 * will return a render-node when the requested gpu is different
1534 * to the server, but also if the client asks for the same gpu than
1535 * the server by requesting its pci-id */
1536 dri2_dpy->is_render_node = drmGetNodeTypeFromFd(dri2_dpy->fd) == DRM_NODE_RENDER;
1537
1538 dri2_dpy->driver_name = loader_get_driver_for_fd(dri2_dpy->fd);
1539 if (dri2_dpy->driver_name == NULL) {
1540 _eglError(EGL_BAD_ALLOC, "DRI2: failed to get driver name");
1541 goto cleanup;
1542 }
1543
1544 /* render nodes cannot use Gem names, and thus do not support
1545 * the __DRI_DRI2_LOADER extension */
1546 if (!dri2_dpy->is_render_node) {
1547 dri2_dpy->loader_extensions = dri2_loader_extensions;
1548 if (!dri2_load_driver(disp)) {
1549 _eglError(EGL_BAD_ALLOC, "DRI2: failed to load driver");
1550 goto cleanup;
1551 }
1552 } else {
1553 dri2_dpy->loader_extensions = image_loader_extensions;
1554 if (!dri2_load_driver_dri3(disp)) {
1555 _eglError(EGL_BAD_ALLOC, "DRI3: failed to load driver");
1556 goto cleanup;
1557 }
1558 }
1559
1560 if (!dri2_create_screen(disp))
1561 goto cleanup;
1562
1563 if (!dri2_setup_extensions(disp))
1564 goto cleanup;
1565
1566 dri2_setup_screen(disp);
1567
1568 dri2_wl_setup_swap_interval(disp);
1569
1570 /* To use Prime, we must have _DRI_IMAGE v7 at least.
1571 * createImageFromFds support indicates that Prime export/import
1572 * is supported by the driver. Fall back to
1573 * gem names if we don't have Prime support. */
1574
1575 if (dri2_dpy->image->base.version < 7 ||
1576 dri2_dpy->image->createImageFromFds == NULL)
1577 dri2_dpy->capabilities &= ~WL_DRM_CAPABILITY_PRIME;
1578
1579 /* We cannot use Gem names with render-nodes, only prime fds (dma-buf).
1580 * The server needs to accept them */
1581 if (dri2_dpy->is_render_node &&
1582 !(dri2_dpy->capabilities & WL_DRM_CAPABILITY_PRIME)) {
1583 _eglLog(_EGL_WARNING, "wayland-egl: display is not render-node capable");
1584 goto cleanup;
1585 }
1586
1587 if (dri2_dpy->is_different_gpu &&
1588 (dri2_dpy->image->base.version < 9 ||
1589 dri2_dpy->image->blitImage == NULL)) {
1590 _eglLog(_EGL_WARNING, "wayland-egl: Different GPU selected, but the "
1591 "Image extension in the driver is not "
1592 "compatible. Version 9 or later and blitImage() "
1593 "are required");
1594 goto cleanup;
1595 }
1596
1597 if (!dri2_wl_add_configs_for_visuals(drv, disp)) {
1598 _eglError(EGL_NOT_INITIALIZED, "DRI2: failed to add configs");
1599 goto cleanup;
1600 }
1601
1602 dri2_set_WL_bind_wayland_display(drv, disp);
1603 /* When cannot convert EGLImage to wl_buffer when on a different gpu,
1604 * because the buffer of the EGLImage has likely a tiling mode the server
1605 * gpu won't support. These is no way to check for now. Thus do not support the
1606 * extension */
1607 if (!dri2_dpy->is_different_gpu)
1608 disp->Extensions.WL_create_wayland_buffer_from_image = EGL_TRUE;
1609
1610 disp->Extensions.EXT_buffer_age = EGL_TRUE;
1611
1612 disp->Extensions.EXT_swap_buffers_with_damage = EGL_TRUE;
1613
1614 /* Fill vtbl last to prevent accidentally calling virtual function during
1615 * initialization.
1616 */
1617 dri2_dpy->vtbl = &dri2_wl_display_vtbl;
1618
1619 return EGL_TRUE;
1620
1621 cleanup:
1622 dri2_display_destroy(disp);
1623 return EGL_FALSE;
1624 }
1625
1626 static int
1627 dri2_wl_swrast_get_stride_for_format(int format, int w)
1628 {
1629 int visual_idx = dri2_wl_visual_idx_from_shm_format(format);
1630
1631 assume(visual_idx != -1);
1632
1633 return w * (dri2_wl_visuals[visual_idx].bpp / 8);
1634 }
1635
1636 static EGLBoolean
1637 dri2_wl_swrast_allocate_buffer(struct dri2_egl_surface *dri2_surf,
1638 int format, int w, int h,
1639 void **data, int *size,
1640 struct wl_buffer **buffer)
1641 {
1642 struct dri2_egl_display *dri2_dpy =
1643 dri2_egl_display(dri2_surf->base.Resource.Display);
1644 struct wl_shm_pool *pool;
1645 int fd, stride, size_map;
1646 void *data_map;
1647
1648 stride = dri2_wl_swrast_get_stride_for_format(format, w);
1649 size_map = h * stride;
1650
1651 /* Create a shareable buffer */
1652 fd = os_create_anonymous_file(size_map, NULL);
1653 if (fd < 0)
1654 return EGL_FALSE;
1655
1656 data_map = mmap(NULL, size_map, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
1657 if (data_map == MAP_FAILED) {
1658 close(fd);
1659 return EGL_FALSE;
1660 }
1661
1662 /* Share it in a wl_buffer */
1663 pool = wl_shm_create_pool(dri2_dpy->wl_shm, fd, size_map);
1664 wl_proxy_set_queue((struct wl_proxy *)pool, dri2_surf->wl_queue);
1665 *buffer = wl_shm_pool_create_buffer(pool, 0, w, h, stride, format);
1666 wl_shm_pool_destroy(pool);
1667 close(fd);
1668
1669 *data = data_map;
1670 *size = size_map;
1671 return EGL_TRUE;
1672 }
1673
1674 static int
1675 swrast_update_buffers(struct dri2_egl_surface *dri2_surf)
1676 {
1677 struct dri2_egl_display *dri2_dpy =
1678 dri2_egl_display(dri2_surf->base.Resource.Display);
1679
1680 /* we need to do the following operations only once per frame */
1681 if (dri2_surf->back)
1682 return 0;
1683
1684 if (dri2_surf->base.Width != dri2_surf->wl_win->width ||
1685 dri2_surf->base.Height != dri2_surf->wl_win->height) {
1686
1687 dri2_wl_release_buffers(dri2_surf);
1688
1689 dri2_surf->base.Width = dri2_surf->wl_win->width;
1690 dri2_surf->base.Height = dri2_surf->wl_win->height;
1691 dri2_surf->dx = dri2_surf->wl_win->dx;
1692 dri2_surf->dy = dri2_surf->wl_win->dy;
1693 dri2_surf->current = NULL;
1694 }
1695
1696 /* find back buffer */
1697
1698 /* There might be a buffer release already queued that wasn't processed */
1699 wl_display_dispatch_queue_pending(dri2_dpy->wl_dpy, dri2_surf->wl_queue);
1700
1701 /* try get free buffer already created */
1702 for (int i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
1703 if (!dri2_surf->color_buffers[i].locked &&
1704 dri2_surf->color_buffers[i].wl_buffer) {
1705 dri2_surf->back = &dri2_surf->color_buffers[i];
1706 break;
1707 }
1708 }
1709
1710 /* else choose any another free location */
1711 if (!dri2_surf->back) {
1712 for (int i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
1713 if (!dri2_surf->color_buffers[i].locked) {
1714 dri2_surf->back = &dri2_surf->color_buffers[i];
1715 if (!dri2_wl_swrast_allocate_buffer(dri2_surf,
1716 dri2_surf->format,
1717 dri2_surf->base.Width,
1718 dri2_surf->base.Height,
1719 &dri2_surf->back->data,
1720 &dri2_surf->back->data_size,
1721 &dri2_surf->back->wl_buffer)) {
1722 _eglError(EGL_BAD_ALLOC, "failed to allocate color buffer");
1723 return -1;
1724 }
1725 wl_buffer_add_listener(dri2_surf->back->wl_buffer,
1726 &wl_buffer_listener, dri2_surf);
1727 break;
1728 }
1729 }
1730 }
1731
1732 if (!dri2_surf->back) {
1733 _eglError(EGL_BAD_ALLOC, "failed to find free buffer");
1734 return -1;
1735 }
1736
1737 dri2_surf->back->locked = true;
1738
1739 /* If we have an extra unlocked buffer at this point, we had to do triple
1740 * buffering for a while, but now can go back to just double buffering.
1741 * That means we can free any unlocked buffer now. */
1742 for (int i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
1743 if (!dri2_surf->color_buffers[i].locked &&
1744 dri2_surf->color_buffers[i].wl_buffer) {
1745 wl_buffer_destroy(dri2_surf->color_buffers[i].wl_buffer);
1746 munmap(dri2_surf->color_buffers[i].data,
1747 dri2_surf->color_buffers[i].data_size);
1748 dri2_surf->color_buffers[i].wl_buffer = NULL;
1749 dri2_surf->color_buffers[i].data = NULL;
1750 }
1751 }
1752
1753 return 0;
1754 }
1755
1756 static void*
1757 dri2_wl_swrast_get_frontbuffer_data(struct dri2_egl_surface *dri2_surf)
1758 {
1759 /* if there has been a resize: */
1760 if (!dri2_surf->current)
1761 return NULL;
1762
1763 return dri2_surf->current->data;
1764 }
1765
1766 static void*
1767 dri2_wl_swrast_get_backbuffer_data(struct dri2_egl_surface *dri2_surf)
1768 {
1769 assert(dri2_surf->back);
1770 return dri2_surf->back->data;
1771 }
1772
1773 static void
1774 dri2_wl_swrast_commit_backbuffer(struct dri2_egl_surface *dri2_surf)
1775 {
1776 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dri2_surf->base.Resource.Display);
1777
1778 while (dri2_surf->throttle_callback != NULL)
1779 if (wl_display_dispatch_queue(dri2_dpy->wl_dpy,
1780 dri2_surf->wl_queue) == -1)
1781 return;
1782
1783 if (dri2_surf->base.SwapInterval > 0) {
1784 dri2_surf->throttle_callback =
1785 wl_surface_frame(dri2_surf->wl_surface_wrapper);
1786 wl_callback_add_listener(dri2_surf->throttle_callback,
1787 &throttle_listener, dri2_surf);
1788 }
1789
1790 dri2_surf->current = dri2_surf->back;
1791 dri2_surf->back = NULL;
1792
1793 wl_surface_attach(dri2_surf->wl_surface_wrapper,
1794 dri2_surf->current->wl_buffer,
1795 dri2_surf->dx, dri2_surf->dy);
1796
1797 dri2_surf->wl_win->attached_width = dri2_surf->base.Width;
1798 dri2_surf->wl_win->attached_height = dri2_surf->base.Height;
1799 /* reset resize growing parameters */
1800 dri2_surf->dx = 0;
1801 dri2_surf->dy = 0;
1802
1803 wl_surface_damage(dri2_surf->wl_surface_wrapper,
1804 0, 0, INT32_MAX, INT32_MAX);
1805 wl_surface_commit(dri2_surf->wl_surface_wrapper);
1806
1807 /* If we're not waiting for a frame callback then we'll at least throttle
1808 * to a sync callback so that we always give a chance for the compositor to
1809 * handle the commit and send a release event before checking for a free
1810 * buffer */
1811 if (dri2_surf->throttle_callback == NULL) {
1812 dri2_surf->throttle_callback = wl_display_sync(dri2_surf->wl_dpy_wrapper);
1813 wl_callback_add_listener(dri2_surf->throttle_callback,
1814 &throttle_listener, dri2_surf);
1815 }
1816
1817 wl_display_flush(dri2_dpy->wl_dpy);
1818 }
1819
1820 static void
1821 dri2_wl_swrast_get_drawable_info(__DRIdrawable * draw,
1822 int *x, int *y, int *w, int *h,
1823 void *loaderPrivate)
1824 {
1825 struct dri2_egl_surface *dri2_surf = loaderPrivate;
1826
1827 (void) swrast_update_buffers(dri2_surf);
1828 *x = 0;
1829 *y = 0;
1830 *w = dri2_surf->base.Width;
1831 *h = dri2_surf->base.Height;
1832 }
1833
1834 static void
1835 dri2_wl_swrast_get_image(__DRIdrawable * read,
1836 int x, int y, int w, int h,
1837 char *data, void *loaderPrivate)
1838 {
1839 struct dri2_egl_surface *dri2_surf = loaderPrivate;
1840 int copy_width = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, w);
1841 int x_offset = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, x);
1842 int src_stride = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, dri2_surf->base.Width);
1843 int dst_stride = copy_width;
1844 char *src, *dst;
1845
1846 src = dri2_wl_swrast_get_frontbuffer_data(dri2_surf);
1847 if (!src) {
1848 memset(data, 0, copy_width * h);
1849 return;
1850 }
1851
1852 assert(data != src);
1853 assert(copy_width <= src_stride);
1854
1855 src += x_offset;
1856 src += y * src_stride;
1857 dst = data;
1858
1859 if (copy_width > src_stride-x_offset)
1860 copy_width = src_stride-x_offset;
1861 if (h > dri2_surf->base.Height-y)
1862 h = dri2_surf->base.Height-y;
1863
1864 for (; h>0; h--) {
1865 memcpy(dst, src, copy_width);
1866 src += src_stride;
1867 dst += dst_stride;
1868 }
1869 }
1870
1871 static void
1872 dri2_wl_swrast_put_image2(__DRIdrawable * draw, int op,
1873 int x, int y, int w, int h, int stride,
1874 char *data, void *loaderPrivate)
1875 {
1876 struct dri2_egl_surface *dri2_surf = loaderPrivate;
1877 int copy_width = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, w);
1878 int dst_stride = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, dri2_surf->base.Width);
1879 int x_offset = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, x);
1880 char *src, *dst;
1881
1882 assert(copy_width <= stride);
1883
1884 (void) swrast_update_buffers(dri2_surf);
1885 dst = dri2_wl_swrast_get_backbuffer_data(dri2_surf);
1886
1887 /* partial copy, copy old content */
1888 if (copy_width < dst_stride)
1889 dri2_wl_swrast_get_image(draw, 0, 0,
1890 dri2_surf->base.Width, dri2_surf->base.Height,
1891 dst, loaderPrivate);
1892
1893 dst += x_offset;
1894 dst += y * dst_stride;
1895
1896 src = data;
1897
1898 /* drivers expect we do these checks (and some rely on it) */
1899 if (copy_width > dst_stride-x_offset)
1900 copy_width = dst_stride-x_offset;
1901 if (h > dri2_surf->base.Height-y)
1902 h = dri2_surf->base.Height-y;
1903
1904 for (; h>0; h--) {
1905 memcpy(dst, src, copy_width);
1906 src += stride;
1907 dst += dst_stride;
1908 }
1909 dri2_wl_swrast_commit_backbuffer(dri2_surf);
1910 }
1911
1912 static void
1913 dri2_wl_swrast_put_image(__DRIdrawable * draw, int op,
1914 int x, int y, int w, int h,
1915 char *data, void *loaderPrivate)
1916 {
1917 struct dri2_egl_surface *dri2_surf = loaderPrivate;
1918 int stride;
1919
1920 stride = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, w);
1921 dri2_wl_swrast_put_image2(draw, op, x, y, w, h,
1922 stride, data, loaderPrivate);
1923 }
1924
1925 static EGLBoolean
1926 dri2_wl_swrast_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
1927 {
1928 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1929 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
1930
1931 dri2_dpy->core->swapBuffers(dri2_surf->dri_drawable);
1932 return EGL_TRUE;
1933 }
1934
1935 static void
1936 shm_handle_format(void *data, struct wl_shm *shm, uint32_t format)
1937 {
1938 struct dri2_egl_display *dri2_dpy = data;
1939 int visual_idx = dri2_wl_visual_idx_from_shm_format(format);
1940
1941 if (visual_idx == -1)
1942 return;
1943
1944 BITSET_SET(dri2_dpy->formats, visual_idx);
1945 }
1946
1947 static const struct wl_shm_listener shm_listener = {
1948 .format = shm_handle_format
1949 };
1950
1951 static void
1952 registry_handle_global_swrast(void *data, struct wl_registry *registry,
1953 uint32_t name, const char *interface,
1954 uint32_t version)
1955 {
1956 struct dri2_egl_display *dri2_dpy = data;
1957
1958 if (strcmp(interface, "wl_shm") == 0) {
1959 dri2_dpy->wl_shm =
1960 wl_registry_bind(registry, name, &wl_shm_interface, 1);
1961 wl_shm_add_listener(dri2_dpy->wl_shm, &shm_listener, dri2_dpy);
1962 }
1963 }
1964
1965 static const struct wl_registry_listener registry_listener_swrast = {
1966 .global = registry_handle_global_swrast,
1967 .global_remove = registry_handle_global_remove
1968 };
1969
1970 static const struct dri2_egl_display_vtbl dri2_wl_swrast_display_vtbl = {
1971 .authenticate = NULL,
1972 .create_window_surface = dri2_wl_create_window_surface,
1973 .create_pixmap_surface = dri2_wl_create_pixmap_surface,
1974 .create_pbuffer_surface = dri2_fallback_create_pbuffer_surface,
1975 .destroy_surface = dri2_wl_destroy_surface,
1976 .create_image = dri2_create_image_khr,
1977 .swap_buffers = dri2_wl_swrast_swap_buffers,
1978 .swap_buffers_with_damage = dri2_fallback_swap_buffers_with_damage,
1979 .swap_buffers_region = dri2_fallback_swap_buffers_region,
1980 .post_sub_buffer = dri2_fallback_post_sub_buffer,
1981 .copy_buffers = dri2_fallback_copy_buffers,
1982 .query_buffer_age = dri2_fallback_query_buffer_age,
1983 .create_wayland_buffer_from_image = dri2_fallback_create_wayland_buffer_from_image,
1984 .get_sync_values = dri2_fallback_get_sync_values,
1985 .get_dri_drawable = dri2_surface_get_dri_drawable,
1986 };
1987
1988 static const __DRIswrastLoaderExtension swrast_loader_extension = {
1989 .base = { __DRI_SWRAST_LOADER, 2 },
1990
1991 .getDrawableInfo = dri2_wl_swrast_get_drawable_info,
1992 .putImage = dri2_wl_swrast_put_image,
1993 .getImage = dri2_wl_swrast_get_image,
1994 .putImage2 = dri2_wl_swrast_put_image2,
1995 };
1996
1997 static const __DRIextension *swrast_loader_extensions[] = {
1998 &swrast_loader_extension.base,
1999 &image_lookup_extension.base,
2000 NULL,
2001 };
2002
2003 static EGLBoolean
2004 dri2_initialize_wayland_swrast(_EGLDriver *drv, _EGLDisplay *disp)
2005 {
2006 _EGLDevice *dev;
2007 struct dri2_egl_display *dri2_dpy;
2008
2009 dri2_dpy = calloc(1, sizeof *dri2_dpy);
2010 if (!dri2_dpy)
2011 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
2012
2013 dri2_dpy->fd = -1;
2014 disp->DriverData = (void *) dri2_dpy;
2015 if (disp->PlatformDisplay == NULL) {
2016 dri2_dpy->wl_dpy = wl_display_connect(NULL);
2017 if (dri2_dpy->wl_dpy == NULL)
2018 goto cleanup;
2019 dri2_dpy->own_device = true;
2020 } else {
2021 dri2_dpy->wl_dpy = disp->PlatformDisplay;
2022 }
2023
2024 dev = _eglAddDevice(dri2_dpy->fd, true);
2025 if (!dev) {
2026 _eglError(EGL_NOT_INITIALIZED, "DRI2: failed to find EGLDevice");
2027 goto cleanup;
2028 }
2029
2030 disp->Device = dev;
2031
2032 dri2_dpy->wl_queue = wl_display_create_queue(dri2_dpy->wl_dpy);
2033
2034 dri2_dpy->wl_dpy_wrapper = wl_proxy_create_wrapper(dri2_dpy->wl_dpy);
2035 if (dri2_dpy->wl_dpy_wrapper == NULL)
2036 goto cleanup;
2037
2038 wl_proxy_set_queue((struct wl_proxy *) dri2_dpy->wl_dpy_wrapper,
2039 dri2_dpy->wl_queue);
2040
2041 if (dri2_dpy->own_device)
2042 wl_display_dispatch_pending(dri2_dpy->wl_dpy);
2043
2044 dri2_dpy->wl_registry = wl_display_get_registry(dri2_dpy->wl_dpy_wrapper);
2045 wl_registry_add_listener(dri2_dpy->wl_registry,
2046 &registry_listener_swrast, dri2_dpy);
2047
2048 if (roundtrip(dri2_dpy) < 0 || dri2_dpy->wl_shm == NULL)
2049 goto cleanup;
2050
2051 if (roundtrip(dri2_dpy) < 0 || !BITSET_TEST_RANGE(dri2_dpy->formats,
2052 0, EGL_DRI2_MAX_FORMATS))
2053 goto cleanup;
2054
2055 dri2_dpy->driver_name = strdup("swrast");
2056 if (!dri2_load_driver_swrast(disp))
2057 goto cleanup;
2058
2059 dri2_dpy->loader_extensions = swrast_loader_extensions;
2060
2061 if (!dri2_create_screen(disp))
2062 goto cleanup;
2063
2064 if (!dri2_setup_extensions(disp))
2065 goto cleanup;
2066
2067 dri2_setup_screen(disp);
2068
2069 dri2_wl_setup_swap_interval(disp);
2070
2071 if (!dri2_wl_add_configs_for_visuals(drv, disp)) {
2072 _eglError(EGL_NOT_INITIALIZED, "DRI2: failed to add configs");
2073 goto cleanup;
2074 }
2075
2076 /* Fill vtbl last to prevent accidentally calling virtual function during
2077 * initialization.
2078 */
2079 dri2_dpy->vtbl = &dri2_wl_swrast_display_vtbl;
2080
2081 return EGL_TRUE;
2082
2083 cleanup:
2084 dri2_display_destroy(disp);
2085 return EGL_FALSE;
2086 }
2087
2088 EGLBoolean
2089 dri2_initialize_wayland(_EGLDriver *drv, _EGLDisplay *disp)
2090 {
2091 EGLBoolean initialized = EGL_FALSE;
2092
2093 if (!disp->Options.ForceSoftware)
2094 initialized = dri2_initialize_wayland_drm(drv, disp);
2095
2096 if (!initialized)
2097 initialized = dri2_initialize_wayland_swrast(drv, disp);
2098
2099 return initialized;
2100
2101 }
2102
2103 void
2104 dri2_teardown_wayland(struct dri2_egl_display *dri2_dpy)
2105 {
2106 if (dri2_dpy->wl_drm)
2107 wl_drm_destroy(dri2_dpy->wl_drm);
2108 if (dri2_dpy->wl_dmabuf)
2109 zwp_linux_dmabuf_v1_destroy(dri2_dpy->wl_dmabuf);
2110 if (dri2_dpy->wl_shm)
2111 wl_shm_destroy(dri2_dpy->wl_shm);
2112 if (dri2_dpy->wl_registry)
2113 wl_registry_destroy(dri2_dpy->wl_registry);
2114 if (dri2_dpy->wl_queue)
2115 wl_event_queue_destroy(dri2_dpy->wl_queue);
2116 if (dri2_dpy->wl_dpy_wrapper)
2117 wl_proxy_wrapper_destroy(dri2_dpy->wl_dpy_wrapper);
2118
2119 for (int i = 0; dri2_dpy->wl_modifiers && i < ARRAY_SIZE(dri2_wl_visuals); i++)
2120 u_vector_finish(&dri2_dpy->wl_modifiers[i]);
2121 free(dri2_dpy->wl_modifiers);
2122
2123 if (dri2_dpy->own_device)
2124 wl_display_disconnect(dri2_dpy->wl_dpy);
2125 }