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