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