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