egl: Move loader_set_logger() up to egl_dri2.c.
[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 dri2_dpy = calloc(1, sizeof *dri2_dpy);
1327 if (!dri2_dpy)
1328 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
1329
1330 dri2_dpy->fd = -1;
1331 disp->DriverData = (void *) dri2_dpy;
1332 if (disp->PlatformDisplay == NULL) {
1333 dri2_dpy->wl_dpy = wl_display_connect(NULL);
1334 if (dri2_dpy->wl_dpy == NULL)
1335 goto cleanup;
1336 dri2_dpy->own_device = true;
1337 } else {
1338 dri2_dpy->wl_dpy = disp->PlatformDisplay;
1339 }
1340
1341 dri2_dpy->wl_modifiers =
1342 calloc(ARRAY_SIZE(dri2_wl_visuals), sizeof(*dri2_dpy->wl_modifiers));
1343 if (!dri2_dpy->wl_modifiers)
1344 goto cleanup;
1345 for (int i = 0; i < ARRAY_SIZE(dri2_wl_visuals); i++) {
1346 if (!u_vector_init(&dri2_dpy->wl_modifiers[i], sizeof(uint64_t), 32))
1347 goto cleanup;
1348 }
1349
1350 dri2_dpy->wl_queue = wl_display_create_queue(dri2_dpy->wl_dpy);
1351
1352 dri2_dpy->wl_dpy_wrapper = wl_proxy_create_wrapper(dri2_dpy->wl_dpy);
1353 if (dri2_dpy->wl_dpy_wrapper == NULL)
1354 goto cleanup;
1355
1356 wl_proxy_set_queue((struct wl_proxy *) dri2_dpy->wl_dpy_wrapper,
1357 dri2_dpy->wl_queue);
1358
1359 if (dri2_dpy->own_device)
1360 wl_display_dispatch_pending(dri2_dpy->wl_dpy);
1361
1362 dri2_dpy->wl_registry = wl_display_get_registry(dri2_dpy->wl_dpy_wrapper);
1363 wl_registry_add_listener(dri2_dpy->wl_registry,
1364 &registry_listener_drm, dri2_dpy);
1365 if (roundtrip(dri2_dpy) < 0 || dri2_dpy->wl_drm == NULL)
1366 goto cleanup;
1367
1368 if (roundtrip(dri2_dpy) < 0 || dri2_dpy->fd == -1)
1369 goto cleanup;
1370
1371 if (roundtrip(dri2_dpy) < 0 || !dri2_dpy->authenticated)
1372 goto cleanup;
1373
1374 dri2_dpy->fd = loader_get_user_preferred_fd(dri2_dpy->fd,
1375 &dri2_dpy->is_different_gpu);
1376 dev = _eglAddDevice(dri2_dpy->fd, false);
1377 if (!dev) {
1378 _eglError(EGL_NOT_INITIALIZED, "DRI2: failed to find EGLDevice");
1379 goto cleanup;
1380 }
1381
1382 disp->Device = dev;
1383
1384 if (dri2_dpy->is_different_gpu) {
1385 free(dri2_dpy->device_name);
1386 dri2_dpy->device_name = loader_get_device_name_for_fd(dri2_dpy->fd);
1387 if (!dri2_dpy->device_name) {
1388 _eglError(EGL_BAD_ALLOC, "wayland-egl: failed to get device name "
1389 "for requested GPU");
1390 goto cleanup;
1391 }
1392 }
1393
1394 /* we have to do the check now, because loader_get_user_preferred_fd
1395 * will return a render-node when the requested gpu is different
1396 * to the server, but also if the client asks for the same gpu than
1397 * the server by requesting its pci-id */
1398 dri2_dpy->is_render_node = drmGetNodeTypeFromFd(dri2_dpy->fd) == DRM_NODE_RENDER;
1399
1400 dri2_dpy->driver_name = loader_get_driver_for_fd(dri2_dpy->fd);
1401 if (dri2_dpy->driver_name == NULL) {
1402 _eglError(EGL_BAD_ALLOC, "DRI2: failed to get driver name");
1403 goto cleanup;
1404 }
1405
1406 /* render nodes cannot use Gem names, and thus do not support
1407 * the __DRI_DRI2_LOADER extension */
1408 if (!dri2_dpy->is_render_node) {
1409 dri2_dpy->loader_extensions = dri2_loader_extensions;
1410 if (!dri2_load_driver(disp)) {
1411 _eglError(EGL_BAD_ALLOC, "DRI2: failed to load driver");
1412 goto cleanup;
1413 }
1414 } else {
1415 dri2_dpy->loader_extensions = image_loader_extensions;
1416 if (!dri2_load_driver_dri3(disp)) {
1417 _eglError(EGL_BAD_ALLOC, "DRI3: failed to load driver");
1418 goto cleanup;
1419 }
1420 }
1421
1422 if (!dri2_create_screen(disp))
1423 goto cleanup;
1424
1425 if (!dri2_setup_extensions(disp))
1426 goto cleanup;
1427
1428 dri2_setup_screen(disp);
1429
1430 dri2_wl_setup_swap_interval(disp);
1431
1432 /* To use Prime, we must have _DRI_IMAGE v7 at least.
1433 * createImageFromFds support indicates that Prime export/import
1434 * is supported by the driver. Fall back to
1435 * gem names if we don't have Prime support. */
1436
1437 if (dri2_dpy->image->base.version < 7 ||
1438 dri2_dpy->image->createImageFromFds == NULL)
1439 dri2_dpy->capabilities &= ~WL_DRM_CAPABILITY_PRIME;
1440
1441 /* We cannot use Gem names with render-nodes, only prime fds (dma-buf).
1442 * The server needs to accept them */
1443 if (dri2_dpy->is_render_node &&
1444 !(dri2_dpy->capabilities & WL_DRM_CAPABILITY_PRIME)) {
1445 _eglLog(_EGL_WARNING, "wayland-egl: display is not render-node capable");
1446 goto cleanup;
1447 }
1448
1449 if (dri2_dpy->is_different_gpu &&
1450 (dri2_dpy->image->base.version < 9 ||
1451 dri2_dpy->image->blitImage == NULL)) {
1452 _eglLog(_EGL_WARNING, "wayland-egl: Different GPU selected, but the "
1453 "Image extension in the driver is not "
1454 "compatible. Version 9 or later and blitImage() "
1455 "are required");
1456 goto cleanup;
1457 }
1458
1459 if (!dri2_wl_add_configs_for_visuals(drv, disp)) {
1460 _eglError(EGL_NOT_INITIALIZED, "DRI2: failed to add configs");
1461 goto cleanup;
1462 }
1463
1464 dri2_set_WL_bind_wayland_display(drv, disp);
1465 /* When cannot convert EGLImage to wl_buffer when on a different gpu,
1466 * because the buffer of the EGLImage has likely a tiling mode the server
1467 * gpu won't support. These is no way to check for now. Thus do not support the
1468 * extension */
1469 if (!dri2_dpy->is_different_gpu)
1470 disp->Extensions.WL_create_wayland_buffer_from_image = EGL_TRUE;
1471
1472 disp->Extensions.EXT_buffer_age = EGL_TRUE;
1473
1474 disp->Extensions.EXT_swap_buffers_with_damage = EGL_TRUE;
1475
1476 /* Fill vtbl last to prevent accidentally calling virtual function during
1477 * initialization.
1478 */
1479 dri2_dpy->vtbl = &dri2_wl_display_vtbl;
1480
1481 return EGL_TRUE;
1482
1483 cleanup:
1484 dri2_display_destroy(disp);
1485 return EGL_FALSE;
1486 }
1487
1488 static int
1489 dri2_wl_swrast_get_stride_for_format(int format, int w)
1490 {
1491 int visual_idx = dri2_wl_visual_idx_from_shm_format(format);
1492
1493 assume(visual_idx != -1);
1494
1495 return w * (dri2_wl_visuals[visual_idx].bpp / 8);
1496 }
1497
1498 /*
1499 * Taken from weston shared/os-compatibility.c
1500 */
1501
1502 #ifndef HAVE_MKOSTEMP
1503
1504 static int
1505 set_cloexec_or_close(int fd)
1506 {
1507 long flags;
1508
1509 if (fd == -1)
1510 return -1;
1511
1512 flags = fcntl(fd, F_GETFD);
1513 if (flags == -1)
1514 goto err;
1515
1516 if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1)
1517 goto err;
1518
1519 return fd;
1520
1521 err:
1522 close(fd);
1523 return -1;
1524 }
1525
1526 #endif
1527
1528 /*
1529 * Taken from weston shared/os-compatibility.c
1530 */
1531
1532 static int
1533 create_tmpfile_cloexec(char *tmpname)
1534 {
1535 int fd;
1536
1537 #ifdef HAVE_MKOSTEMP
1538 fd = mkostemp(tmpname, O_CLOEXEC);
1539 if (fd >= 0)
1540 unlink(tmpname);
1541 #else
1542 fd = mkstemp(tmpname);
1543 if (fd >= 0) {
1544 fd = set_cloexec_or_close(fd);
1545 unlink(tmpname);
1546 }
1547 #endif
1548
1549 return fd;
1550 }
1551
1552 /*
1553 * Taken from weston shared/os-compatibility.c
1554 *
1555 * Create a new, unique, anonymous file of the given size, and
1556 * return the file descriptor for it. The file descriptor is set
1557 * CLOEXEC. The file is immediately suitable for mmap()'ing
1558 * the given size at offset zero.
1559 *
1560 * The file should not have a permanent backing store like a disk,
1561 * but may have if XDG_RUNTIME_DIR is not properly implemented in OS.
1562 *
1563 * The file name is deleted from the file system.
1564 *
1565 * The file is suitable for buffer sharing between processes by
1566 * transmitting the file descriptor over Unix sockets using the
1567 * SCM_RIGHTS methods.
1568 *
1569 * If the C library implements posix_fallocate(), it is used to
1570 * guarantee that disk space is available for the file at the
1571 * given size. If disk space is insufficient, errno is set to ENOSPC.
1572 * If posix_fallocate() is not supported, program may receive
1573 * SIGBUS on accessing mmap()'ed file contents instead.
1574 */
1575 static int
1576 os_create_anonymous_file(off_t size)
1577 {
1578 static const char templ[] = "/mesa-shared-XXXXXX";
1579 const char *path;
1580 char *name;
1581 int fd;
1582 int ret;
1583
1584 path = getenv("XDG_RUNTIME_DIR");
1585 if (!path) {
1586 errno = ENOENT;
1587 return -1;
1588 }
1589
1590 name = malloc(strlen(path) + sizeof(templ));
1591 if (!name)
1592 return -1;
1593
1594 strcpy(name, path);
1595 strcat(name, templ);
1596
1597 fd = create_tmpfile_cloexec(name);
1598
1599 free(name);
1600
1601 if (fd < 0)
1602 return -1;
1603
1604 ret = ftruncate(fd, size);
1605 if (ret < 0) {
1606 close(fd);
1607 return -1;
1608 }
1609
1610 return fd;
1611 }
1612
1613
1614 static EGLBoolean
1615 dri2_wl_swrast_allocate_buffer(struct dri2_egl_surface *dri2_surf,
1616 int format, int w, int h,
1617 void **data, int *size,
1618 struct wl_buffer **buffer)
1619 {
1620 struct dri2_egl_display *dri2_dpy =
1621 dri2_egl_display(dri2_surf->base.Resource.Display);
1622 struct wl_shm_pool *pool;
1623 int fd, stride, size_map;
1624 void *data_map;
1625
1626 stride = dri2_wl_swrast_get_stride_for_format(format, w);
1627 size_map = h * stride;
1628
1629 /* Create a shareable buffer */
1630 fd = os_create_anonymous_file(size_map);
1631 if (fd < 0)
1632 return EGL_FALSE;
1633
1634 data_map = mmap(NULL, size_map, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
1635 if (data_map == MAP_FAILED) {
1636 close(fd);
1637 return EGL_FALSE;
1638 }
1639
1640 /* Share it in a wl_buffer */
1641 pool = wl_shm_create_pool(dri2_dpy->wl_shm, fd, size_map);
1642 wl_proxy_set_queue((struct wl_proxy *)pool, dri2_surf->wl_queue);
1643 *buffer = wl_shm_pool_create_buffer(pool, 0, w, h, stride, format);
1644 wl_shm_pool_destroy(pool);
1645 close(fd);
1646
1647 *data = data_map;
1648 *size = size_map;
1649 return EGL_TRUE;
1650 }
1651
1652 static int
1653 swrast_update_buffers(struct dri2_egl_surface *dri2_surf)
1654 {
1655 struct dri2_egl_display *dri2_dpy =
1656 dri2_egl_display(dri2_surf->base.Resource.Display);
1657
1658 /* we need to do the following operations only once per frame */
1659 if (dri2_surf->back)
1660 return 0;
1661
1662 if (dri2_surf->base.Width != dri2_surf->wl_win->width ||
1663 dri2_surf->base.Height != dri2_surf->wl_win->height) {
1664
1665 dri2_wl_release_buffers(dri2_surf);
1666
1667 dri2_surf->base.Width = dri2_surf->wl_win->width;
1668 dri2_surf->base.Height = dri2_surf->wl_win->height;
1669 dri2_surf->dx = dri2_surf->wl_win->dx;
1670 dri2_surf->dy = dri2_surf->wl_win->dy;
1671 dri2_surf->current = NULL;
1672 }
1673
1674 /* find back buffer */
1675
1676 /* There might be a buffer release already queued that wasn't processed */
1677 wl_display_dispatch_queue_pending(dri2_dpy->wl_dpy, dri2_surf->wl_queue);
1678
1679 /* try get free buffer already created */
1680 for (int i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
1681 if (!dri2_surf->color_buffers[i].locked &&
1682 dri2_surf->color_buffers[i].wl_buffer) {
1683 dri2_surf->back = &dri2_surf->color_buffers[i];
1684 break;
1685 }
1686 }
1687
1688 /* else choose any another free location */
1689 if (!dri2_surf->back) {
1690 for (int i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
1691 if (!dri2_surf->color_buffers[i].locked) {
1692 dri2_surf->back = &dri2_surf->color_buffers[i];
1693 if (!dri2_wl_swrast_allocate_buffer(dri2_surf,
1694 dri2_surf->format,
1695 dri2_surf->base.Width,
1696 dri2_surf->base.Height,
1697 &dri2_surf->back->data,
1698 &dri2_surf->back->data_size,
1699 &dri2_surf->back->wl_buffer)) {
1700 _eglError(EGL_BAD_ALLOC, "failed to allocate color buffer");
1701 return -1;
1702 }
1703 wl_buffer_add_listener(dri2_surf->back->wl_buffer,
1704 &wl_buffer_listener, dri2_surf);
1705 break;
1706 }
1707 }
1708 }
1709
1710 if (!dri2_surf->back) {
1711 _eglError(EGL_BAD_ALLOC, "failed to find free buffer");
1712 return -1;
1713 }
1714
1715 dri2_surf->back->locked = true;
1716
1717 /* If we have an extra unlocked buffer at this point, we had to do triple
1718 * buffering for a while, but now can go back to just double buffering.
1719 * That means we can free any unlocked buffer now. */
1720 for (int i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
1721 if (!dri2_surf->color_buffers[i].locked &&
1722 dri2_surf->color_buffers[i].wl_buffer) {
1723 wl_buffer_destroy(dri2_surf->color_buffers[i].wl_buffer);
1724 munmap(dri2_surf->color_buffers[i].data,
1725 dri2_surf->color_buffers[i].data_size);
1726 dri2_surf->color_buffers[i].wl_buffer = NULL;
1727 dri2_surf->color_buffers[i].data = NULL;
1728 }
1729 }
1730
1731 return 0;
1732 }
1733
1734 static void*
1735 dri2_wl_swrast_get_frontbuffer_data(struct dri2_egl_surface *dri2_surf)
1736 {
1737 /* if there has been a resize: */
1738 if (!dri2_surf->current)
1739 return NULL;
1740
1741 return dri2_surf->current->data;
1742 }
1743
1744 static void*
1745 dri2_wl_swrast_get_backbuffer_data(struct dri2_egl_surface *dri2_surf)
1746 {
1747 assert(dri2_surf->back);
1748 return dri2_surf->back->data;
1749 }
1750
1751 static void
1752 dri2_wl_swrast_commit_backbuffer(struct dri2_egl_surface *dri2_surf)
1753 {
1754 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dri2_surf->base.Resource.Display);
1755
1756 while (dri2_surf->throttle_callback != NULL)
1757 if (wl_display_dispatch_queue(dri2_dpy->wl_dpy,
1758 dri2_surf->wl_queue) == -1)
1759 return;
1760
1761 if (dri2_surf->base.SwapInterval > 0) {
1762 dri2_surf->throttle_callback =
1763 wl_surface_frame(dri2_surf->wl_surface_wrapper);
1764 wl_callback_add_listener(dri2_surf->throttle_callback,
1765 &throttle_listener, dri2_surf);
1766 }
1767
1768 dri2_surf->current = dri2_surf->back;
1769 dri2_surf->back = NULL;
1770
1771 wl_surface_attach(dri2_surf->wl_surface_wrapper,
1772 dri2_surf->current->wl_buffer,
1773 dri2_surf->dx, dri2_surf->dy);
1774
1775 dri2_surf->wl_win->attached_width = dri2_surf->base.Width;
1776 dri2_surf->wl_win->attached_height = dri2_surf->base.Height;
1777 /* reset resize growing parameters */
1778 dri2_surf->dx = 0;
1779 dri2_surf->dy = 0;
1780
1781 wl_surface_damage(dri2_surf->wl_surface_wrapper,
1782 0, 0, INT32_MAX, INT32_MAX);
1783 wl_surface_commit(dri2_surf->wl_surface_wrapper);
1784
1785 /* If we're not waiting for a frame callback then we'll at least throttle
1786 * to a sync callback so that we always give a chance for the compositor to
1787 * handle the commit and send a release event before checking for a free
1788 * buffer */
1789 if (dri2_surf->throttle_callback == NULL) {
1790 dri2_surf->throttle_callback = wl_display_sync(dri2_surf->wl_dpy_wrapper);
1791 wl_callback_add_listener(dri2_surf->throttle_callback,
1792 &throttle_listener, dri2_surf);
1793 }
1794
1795 wl_display_flush(dri2_dpy->wl_dpy);
1796 }
1797
1798 static void
1799 dri2_wl_swrast_get_drawable_info(__DRIdrawable * draw,
1800 int *x, int *y, int *w, int *h,
1801 void *loaderPrivate)
1802 {
1803 struct dri2_egl_surface *dri2_surf = loaderPrivate;
1804
1805 (void) swrast_update_buffers(dri2_surf);
1806 *x = 0;
1807 *y = 0;
1808 *w = dri2_surf->base.Width;
1809 *h = dri2_surf->base.Height;
1810 }
1811
1812 static void
1813 dri2_wl_swrast_get_image(__DRIdrawable * read,
1814 int x, int y, int w, int h,
1815 char *data, void *loaderPrivate)
1816 {
1817 struct dri2_egl_surface *dri2_surf = loaderPrivate;
1818 int copy_width = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, w);
1819 int x_offset = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, x);
1820 int src_stride = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, dri2_surf->base.Width);
1821 int dst_stride = copy_width;
1822 char *src, *dst;
1823
1824 src = dri2_wl_swrast_get_frontbuffer_data(dri2_surf);
1825 if (!src) {
1826 memset(data, 0, copy_width * h);
1827 return;
1828 }
1829
1830 assert(data != src);
1831 assert(copy_width <= src_stride);
1832
1833 src += x_offset;
1834 src += y * src_stride;
1835 dst = data;
1836
1837 if (copy_width > src_stride-x_offset)
1838 copy_width = src_stride-x_offset;
1839 if (h > dri2_surf->base.Height-y)
1840 h = dri2_surf->base.Height-y;
1841
1842 for (; h>0; h--) {
1843 memcpy(dst, src, copy_width);
1844 src += src_stride;
1845 dst += dst_stride;
1846 }
1847 }
1848
1849 static void
1850 dri2_wl_swrast_put_image2(__DRIdrawable * draw, int op,
1851 int x, int y, int w, int h, int stride,
1852 char *data, void *loaderPrivate)
1853 {
1854 struct dri2_egl_surface *dri2_surf = loaderPrivate;
1855 int copy_width = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, w);
1856 int dst_stride = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, dri2_surf->base.Width);
1857 int x_offset = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, x);
1858 char *src, *dst;
1859
1860 assert(copy_width <= stride);
1861
1862 (void) swrast_update_buffers(dri2_surf);
1863 dst = dri2_wl_swrast_get_backbuffer_data(dri2_surf);
1864
1865 /* partial copy, copy old content */
1866 if (copy_width < dst_stride)
1867 dri2_wl_swrast_get_image(draw, 0, 0,
1868 dri2_surf->base.Width, dri2_surf->base.Height,
1869 dst, loaderPrivate);
1870
1871 dst += x_offset;
1872 dst += y * dst_stride;
1873
1874 src = data;
1875
1876 /* drivers expect we do these checks (and some rely on it) */
1877 if (copy_width > dst_stride-x_offset)
1878 copy_width = dst_stride-x_offset;
1879 if (h > dri2_surf->base.Height-y)
1880 h = dri2_surf->base.Height-y;
1881
1882 for (; h>0; h--) {
1883 memcpy(dst, src, copy_width);
1884 src += stride;
1885 dst += dst_stride;
1886 }
1887 dri2_wl_swrast_commit_backbuffer(dri2_surf);
1888 }
1889
1890 static void
1891 dri2_wl_swrast_put_image(__DRIdrawable * draw, int op,
1892 int x, int y, int w, int h,
1893 char *data, void *loaderPrivate)
1894 {
1895 struct dri2_egl_surface *dri2_surf = loaderPrivate;
1896 int stride;
1897
1898 stride = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, w);
1899 dri2_wl_swrast_put_image2(draw, op, x, y, w, h,
1900 stride, data, loaderPrivate);
1901 }
1902
1903 static EGLBoolean
1904 dri2_wl_swrast_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
1905 {
1906 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1907 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
1908
1909 dri2_dpy->core->swapBuffers(dri2_surf->dri_drawable);
1910 return EGL_TRUE;
1911 }
1912
1913 static void
1914 shm_handle_format(void *data, struct wl_shm *shm, uint32_t format)
1915 {
1916 struct dri2_egl_display *dri2_dpy = data;
1917 int visual_idx = dri2_wl_visual_idx_from_shm_format(format);
1918
1919 if (visual_idx == -1)
1920 return;
1921
1922 dri2_dpy->formats |= (1u << visual_idx);
1923 }
1924
1925 static const struct wl_shm_listener shm_listener = {
1926 .format = shm_handle_format
1927 };
1928
1929 static void
1930 registry_handle_global_swrast(void *data, struct wl_registry *registry,
1931 uint32_t name, const char *interface,
1932 uint32_t version)
1933 {
1934 struct dri2_egl_display *dri2_dpy = data;
1935
1936 if (strcmp(interface, "wl_shm") == 0) {
1937 dri2_dpy->wl_shm =
1938 wl_registry_bind(registry, name, &wl_shm_interface, 1);
1939 wl_shm_add_listener(dri2_dpy->wl_shm, &shm_listener, dri2_dpy);
1940 }
1941 }
1942
1943 static const struct wl_registry_listener registry_listener_swrast = {
1944 .global = registry_handle_global_swrast,
1945 .global_remove = registry_handle_global_remove
1946 };
1947
1948 static const struct dri2_egl_display_vtbl dri2_wl_swrast_display_vtbl = {
1949 .authenticate = NULL,
1950 .create_window_surface = dri2_wl_create_window_surface,
1951 .create_pixmap_surface = dri2_wl_create_pixmap_surface,
1952 .create_pbuffer_surface = dri2_fallback_create_pbuffer_surface,
1953 .destroy_surface = dri2_wl_destroy_surface,
1954 .create_image = dri2_create_image_khr,
1955 .swap_buffers = dri2_wl_swrast_swap_buffers,
1956 .swap_buffers_with_damage = dri2_fallback_swap_buffers_with_damage,
1957 .swap_buffers_region = dri2_fallback_swap_buffers_region,
1958 .post_sub_buffer = dri2_fallback_post_sub_buffer,
1959 .copy_buffers = dri2_fallback_copy_buffers,
1960 .query_buffer_age = dri2_fallback_query_buffer_age,
1961 .create_wayland_buffer_from_image = dri2_fallback_create_wayland_buffer_from_image,
1962 .get_sync_values = dri2_fallback_get_sync_values,
1963 .get_dri_drawable = dri2_surface_get_dri_drawable,
1964 };
1965
1966 static const __DRIswrastLoaderExtension swrast_loader_extension = {
1967 .base = { __DRI_SWRAST_LOADER, 2 },
1968
1969 .getDrawableInfo = dri2_wl_swrast_get_drawable_info,
1970 .putImage = dri2_wl_swrast_put_image,
1971 .getImage = dri2_wl_swrast_get_image,
1972 .putImage2 = dri2_wl_swrast_put_image2,
1973 };
1974
1975 static const __DRIextension *swrast_loader_extensions[] = {
1976 &swrast_loader_extension.base,
1977 &image_lookup_extension.base,
1978 NULL,
1979 };
1980
1981 static EGLBoolean
1982 dri2_initialize_wayland_swrast(_EGLDriver *drv, _EGLDisplay *disp)
1983 {
1984 _EGLDevice *dev;
1985 struct dri2_egl_display *dri2_dpy;
1986
1987 dri2_dpy = calloc(1, sizeof *dri2_dpy);
1988 if (!dri2_dpy)
1989 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
1990
1991 dri2_dpy->fd = -1;
1992 disp->DriverData = (void *) dri2_dpy;
1993 if (disp->PlatformDisplay == NULL) {
1994 dri2_dpy->wl_dpy = wl_display_connect(NULL);
1995 if (dri2_dpy->wl_dpy == NULL)
1996 goto cleanup;
1997 dri2_dpy->own_device = true;
1998 } else {
1999 dri2_dpy->wl_dpy = disp->PlatformDisplay;
2000 }
2001
2002 dev = _eglAddDevice(dri2_dpy->fd, true);
2003 if (!dev) {
2004 _eglError(EGL_NOT_INITIALIZED, "DRI2: failed to find EGLDevice");
2005 goto cleanup;
2006 }
2007
2008 disp->Device = dev;
2009
2010 dri2_dpy->wl_queue = wl_display_create_queue(dri2_dpy->wl_dpy);
2011
2012 dri2_dpy->wl_dpy_wrapper = wl_proxy_create_wrapper(dri2_dpy->wl_dpy);
2013 if (dri2_dpy->wl_dpy_wrapper == NULL)
2014 goto cleanup;
2015
2016 wl_proxy_set_queue((struct wl_proxy *) dri2_dpy->wl_dpy_wrapper,
2017 dri2_dpy->wl_queue);
2018
2019 if (dri2_dpy->own_device)
2020 wl_display_dispatch_pending(dri2_dpy->wl_dpy);
2021
2022 dri2_dpy->wl_registry = wl_display_get_registry(dri2_dpy->wl_dpy_wrapper);
2023 wl_registry_add_listener(dri2_dpy->wl_registry,
2024 &registry_listener_swrast, dri2_dpy);
2025
2026 if (roundtrip(dri2_dpy) < 0 || dri2_dpy->wl_shm == NULL)
2027 goto cleanup;
2028
2029 if (roundtrip(dri2_dpy) < 0 || dri2_dpy->formats == 0)
2030 goto cleanup;
2031
2032 dri2_dpy->driver_name = strdup("swrast");
2033 if (!dri2_load_driver_swrast(disp))
2034 goto cleanup;
2035
2036 dri2_dpy->loader_extensions = swrast_loader_extensions;
2037
2038 if (!dri2_create_screen(disp))
2039 goto cleanup;
2040
2041 if (!dri2_setup_extensions(disp))
2042 goto cleanup;
2043
2044 dri2_setup_screen(disp);
2045
2046 dri2_wl_setup_swap_interval(disp);
2047
2048 if (!dri2_wl_add_configs_for_visuals(drv, disp)) {
2049 _eglError(EGL_NOT_INITIALIZED, "DRI2: failed to add configs");
2050 goto cleanup;
2051 }
2052
2053 /* Fill vtbl last to prevent accidentally calling virtual function during
2054 * initialization.
2055 */
2056 dri2_dpy->vtbl = &dri2_wl_swrast_display_vtbl;
2057
2058 return EGL_TRUE;
2059
2060 cleanup:
2061 dri2_display_destroy(disp);
2062 return EGL_FALSE;
2063 }
2064
2065 EGLBoolean
2066 dri2_initialize_wayland(_EGLDriver *drv, _EGLDisplay *disp)
2067 {
2068 EGLBoolean initialized = EGL_FALSE;
2069
2070 if (!disp->Options.ForceSoftware)
2071 initialized = dri2_initialize_wayland_drm(drv, disp);
2072
2073 if (!initialized)
2074 initialized = dri2_initialize_wayland_swrast(drv, disp);
2075
2076 return initialized;
2077
2078 }
2079
2080 void
2081 dri2_teardown_wayland(struct dri2_egl_display *dri2_dpy)
2082 {
2083 if (dri2_dpy->wl_drm)
2084 wl_drm_destroy(dri2_dpy->wl_drm);
2085 if (dri2_dpy->wl_dmabuf)
2086 zwp_linux_dmabuf_v1_destroy(dri2_dpy->wl_dmabuf);
2087 if (dri2_dpy->wl_shm)
2088 wl_shm_destroy(dri2_dpy->wl_shm);
2089 if (dri2_dpy->wl_registry)
2090 wl_registry_destroy(dri2_dpy->wl_registry);
2091 if (dri2_dpy->wl_queue)
2092 wl_event_queue_destroy(dri2_dpy->wl_queue);
2093 if (dri2_dpy->wl_dpy_wrapper)
2094 wl_proxy_wrapper_destroy(dri2_dpy->wl_dpy_wrapper);
2095
2096 for (int i = 0; dri2_dpy->wl_modifiers && i < ARRAY_SIZE(dri2_wl_visuals); i++)
2097 u_vector_finish(&dri2_dpy->wl_modifiers[i]);
2098 free(dri2_dpy->wl_modifiers);
2099
2100 if (dri2_dpy->own_device)
2101 wl_display_disconnect(dri2_dpy->wl_dpy);
2102 }