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