egl: add helper to combine two u32 into one u64
[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 immediately 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 dereferenceable, 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, preferably 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 = combine_u32_into_u64(mod_hi, mod_lo);
822 }
823 }
824
825 if (dri2_dpy->wl_dmabuf && modifier != DRM_FORMAT_MOD_INVALID) {
826 struct zwp_linux_buffer_params_v1 *params;
827 int i;
828
829 /* We don't need a wrapper for wl_dmabuf objects, because we have to
830 * create the intermediate params object; we can set the queue on this,
831 * and the wl_buffer inherits it race-free. */
832 params = zwp_linux_dmabuf_v1_create_params(dri2_dpy->wl_dmabuf);
833 if (dri2_surf)
834 wl_proxy_set_queue((struct wl_proxy *) params, dri2_surf->wl_queue);
835
836 for (i = 0; i < num_planes; i++) {
837 __DRIimage *p_image;
838 int stride, offset;
839 int fd = -1;
840
841 p_image = dri2_dpy->image->fromPlanar(image, i, NULL);
842 if (!p_image) {
843 assert(i == 0);
844 p_image = image;
845 }
846
847 query = dri2_dpy->image->queryImage(p_image,
848 __DRI_IMAGE_ATTRIB_FD,
849 &fd);
850 query &= dri2_dpy->image->queryImage(p_image,
851 __DRI_IMAGE_ATTRIB_STRIDE,
852 &stride);
853 query &= dri2_dpy->image->queryImage(p_image,
854 __DRI_IMAGE_ATTRIB_OFFSET,
855 &offset);
856 if (image != p_image)
857 dri2_dpy->image->destroyImage(p_image);
858
859 if (!query) {
860 if (fd >= 0)
861 close(fd);
862 zwp_linux_buffer_params_v1_destroy(params);
863 return NULL;
864 }
865
866 zwp_linux_buffer_params_v1_add(params, fd, i, offset, stride,
867 modifier >> 32, modifier & 0xffffffff);
868 close(fd);
869 }
870
871 ret = zwp_linux_buffer_params_v1_create_immed(params, width, height,
872 fourcc, 0);
873 zwp_linux_buffer_params_v1_destroy(params);
874 } else if (dri2_dpy->capabilities & WL_DRM_CAPABILITY_PRIME) {
875 struct wl_drm *wl_drm =
876 dri2_surf ? dri2_surf->wl_drm_wrapper : dri2_dpy->wl_drm;
877 int fd, stride;
878
879 if (num_planes > 1)
880 return NULL;
881
882 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_FD, &fd);
883 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_STRIDE, &stride);
884 ret = wl_drm_create_prime_buffer(wl_drm, fd, width, height, fourcc, 0,
885 stride, 0, 0, 0, 0);
886 close(fd);
887 } else {
888 struct wl_drm *wl_drm =
889 dri2_surf ? dri2_surf->wl_drm_wrapper : dri2_dpy->wl_drm;
890 int name, stride;
891
892 if (num_planes > 1)
893 return NULL;
894
895 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_NAME, &name);
896 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_STRIDE, &stride);
897 ret = wl_drm_create_buffer(wl_drm, name, width, height, stride, fourcc);
898 }
899
900 return ret;
901 }
902
903 static EGLBoolean
904 try_damage_buffer(struct dri2_egl_surface *dri2_surf,
905 const EGLint *rects,
906 EGLint n_rects)
907 {
908 if (wl_proxy_get_version((struct wl_proxy *) dri2_surf->wl_surface_wrapper)
909 < WL_SURFACE_DAMAGE_BUFFER_SINCE_VERSION)
910 return EGL_FALSE;
911
912 for (int i = 0; i < n_rects; i++) {
913 const int *rect = &rects[i * 4];
914
915 wl_surface_damage_buffer(dri2_surf->wl_surface_wrapper,
916 rect[0],
917 dri2_surf->base.Height - rect[1] - rect[3],
918 rect[2], rect[3]);
919 }
920 return EGL_TRUE;
921 }
922 /**
923 * Called via eglSwapBuffers(), drv->API.SwapBuffers().
924 */
925 static EGLBoolean
926 dri2_wl_swap_buffers_with_damage(_EGLDriver *drv,
927 _EGLDisplay *disp,
928 _EGLSurface *draw,
929 const EGLint *rects,
930 EGLint n_rects)
931 {
932 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
933 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
934
935 while (dri2_surf->throttle_callback != NULL)
936 if (wl_display_dispatch_queue(dri2_dpy->wl_dpy,
937 dri2_surf->wl_queue) == -1)
938 return -1;
939
940 for (int i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++)
941 if (dri2_surf->color_buffers[i].age > 0)
942 dri2_surf->color_buffers[i].age++;
943
944 /* Make sure we have a back buffer in case we're swapping without ever
945 * rendering. */
946 if (get_back_bo(dri2_surf) < 0)
947 return _eglError(EGL_BAD_ALLOC, "dri2_swap_buffers");
948
949 if (draw->SwapInterval > 0) {
950 dri2_surf->throttle_callback =
951 wl_surface_frame(dri2_surf->wl_surface_wrapper);
952 wl_callback_add_listener(dri2_surf->throttle_callback,
953 &throttle_listener, dri2_surf);
954 }
955
956 dri2_surf->back->age = 1;
957 dri2_surf->current = dri2_surf->back;
958 dri2_surf->back = NULL;
959
960 if (!dri2_surf->current->wl_buffer) {
961 __DRIimage *image;
962
963 if (dri2_dpy->is_different_gpu)
964 image = dri2_surf->current->linear_copy;
965 else
966 image = dri2_surf->current->dri_image;
967
968 dri2_surf->current->wl_buffer =
969 create_wl_buffer(dri2_dpy, dri2_surf, image);
970
971 wl_buffer_add_listener(dri2_surf->current->wl_buffer,
972 &wl_buffer_listener, dri2_surf);
973 }
974
975 wl_surface_attach(dri2_surf->wl_surface_wrapper,
976 dri2_surf->current->wl_buffer,
977 dri2_surf->dx, dri2_surf->dy);
978
979 dri2_surf->wl_win->attached_width = dri2_surf->base.Width;
980 dri2_surf->wl_win->attached_height = dri2_surf->base.Height;
981 /* reset resize growing parameters */
982 dri2_surf->dx = 0;
983 dri2_surf->dy = 0;
984
985 /* If the compositor doesn't support damage_buffer, we deliberately
986 * ignore the damage region and post maximum damage, due to
987 * https://bugs.freedesktop.org/78190 */
988 if (!n_rects || !try_damage_buffer(dri2_surf, rects, n_rects))
989 wl_surface_damage(dri2_surf->wl_surface_wrapper,
990 0, 0, INT32_MAX, INT32_MAX);
991
992 if (dri2_dpy->is_different_gpu) {
993 _EGLContext *ctx = _eglGetCurrentContext();
994 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
995 dri2_dpy->image->blitImage(dri2_ctx->dri_context,
996 dri2_surf->current->linear_copy,
997 dri2_surf->current->dri_image,
998 0, 0, dri2_surf->base.Width,
999 dri2_surf->base.Height,
1000 0, 0, dri2_surf->base.Width,
1001 dri2_surf->base.Height, 0);
1002 }
1003
1004 dri2_flush_drawable_for_swapbuffers(disp, draw);
1005 dri2_dpy->flush->invalidate(dri2_surf->dri_drawable);
1006
1007 wl_surface_commit(dri2_surf->wl_surface_wrapper);
1008
1009 /* If we're not waiting for a frame callback then we'll at least throttle
1010 * to a sync callback so that we always give a chance for the compositor to
1011 * handle the commit and send a release event before checking for a free
1012 * buffer */
1013 if (dri2_surf->throttle_callback == NULL) {
1014 dri2_surf->throttle_callback = wl_display_sync(dri2_surf->wl_dpy_wrapper);
1015 wl_callback_add_listener(dri2_surf->throttle_callback,
1016 &throttle_listener, dri2_surf);
1017 }
1018
1019 wl_display_flush(dri2_dpy->wl_dpy);
1020
1021 return EGL_TRUE;
1022 }
1023
1024 static EGLint
1025 dri2_wl_query_buffer_age(_EGLDriver *drv,
1026 _EGLDisplay *disp, _EGLSurface *surface)
1027 {
1028 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surface);
1029
1030 if (get_back_bo(dri2_surf) < 0) {
1031 _eglError(EGL_BAD_ALLOC, "dri2_query_buffer_age");
1032 return -1;
1033 }
1034
1035 return dri2_surf->back->age;
1036 }
1037
1038 static EGLBoolean
1039 dri2_wl_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
1040 {
1041 return dri2_wl_swap_buffers_with_damage(drv, disp, draw, NULL, 0);
1042 }
1043
1044 static struct wl_buffer *
1045 dri2_wl_create_wayland_buffer_from_image(_EGLDriver *drv,
1046 _EGLDisplay *disp,
1047 _EGLImage *img)
1048 {
1049 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1050 struct dri2_egl_image *dri2_img = dri2_egl_image(img);
1051 __DRIimage *image = dri2_img->dri_image;
1052 struct wl_buffer *buffer;
1053 int format, visual_idx;
1054
1055 /* Check the upstream display supports this buffer's format. */
1056 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_FORMAT, &format);
1057 visual_idx = dri2_wl_visual_idx_from_dri_image_format(format);
1058 if (visual_idx == -1)
1059 goto bad_format;
1060
1061 if (!(dri2_dpy->formats & (1u << visual_idx)))
1062 goto bad_format;
1063
1064 buffer = create_wl_buffer(dri2_dpy, NULL, image);
1065
1066 /* The buffer object will have been created with our internal event queue
1067 * because it is using wl_dmabuf/wl_drm as a proxy factory. We want the
1068 * buffer to be used by the application so we'll reset it to the display's
1069 * default event queue. This isn't actually racy, as the only event the
1070 * buffer can get is a buffer release, which doesn't happen with an explicit
1071 * attach. */
1072 if (buffer)
1073 wl_proxy_set_queue((struct wl_proxy *) buffer, NULL);
1074
1075 return buffer;
1076
1077 bad_format:
1078 _eglError(EGL_BAD_MATCH, "unsupported image format");
1079 return NULL;
1080 }
1081
1082 static int
1083 dri2_wl_authenticate(_EGLDisplay *disp, uint32_t id)
1084 {
1085 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1086 int ret = 0;
1087
1088 if (dri2_dpy->is_render_node) {
1089 _eglLog(_EGL_WARNING, "wayland-egl: client asks server to "
1090 "authenticate for render-nodes");
1091 return 0;
1092 }
1093 dri2_dpy->authenticated = false;
1094
1095 wl_drm_authenticate(dri2_dpy->wl_drm, id);
1096 if (roundtrip(dri2_dpy) < 0)
1097 ret = -1;
1098
1099 if (!dri2_dpy->authenticated)
1100 ret = -1;
1101
1102 /* reset authenticated */
1103 dri2_dpy->authenticated = true;
1104
1105 return ret;
1106 }
1107
1108 static void
1109 drm_handle_device(void *data, struct wl_drm *drm, const char *device)
1110 {
1111 struct dri2_egl_display *dri2_dpy = data;
1112 drm_magic_t magic;
1113
1114 dri2_dpy->device_name = strdup(device);
1115 if (!dri2_dpy->device_name)
1116 return;
1117
1118 dri2_dpy->fd = loader_open_device(dri2_dpy->device_name);
1119 if (dri2_dpy->fd == -1) {
1120 _eglLog(_EGL_WARNING, "wayland-egl: could not open %s (%s)",
1121 dri2_dpy->device_name, strerror(errno));
1122 return;
1123 }
1124
1125 if (drmGetNodeTypeFromFd(dri2_dpy->fd) == DRM_NODE_RENDER) {
1126 dri2_dpy->authenticated = true;
1127 } else {
1128 drmGetMagic(dri2_dpy->fd, &magic);
1129 wl_drm_authenticate(dri2_dpy->wl_drm, magic);
1130 }
1131 }
1132
1133 static void
1134 drm_handle_format(void *data, struct wl_drm *drm, uint32_t format)
1135 {
1136 struct dri2_egl_display *dri2_dpy = data;
1137 int visual_idx = dri2_wl_visual_idx_from_fourcc(format);
1138
1139 if (visual_idx == -1)
1140 return;
1141
1142 dri2_dpy->formats |= (1u << visual_idx);
1143 }
1144
1145 static void
1146 drm_handle_capabilities(void *data, struct wl_drm *drm, uint32_t value)
1147 {
1148 struct dri2_egl_display *dri2_dpy = data;
1149
1150 dri2_dpy->capabilities = value;
1151 }
1152
1153 static void
1154 drm_handle_authenticated(void *data, struct wl_drm *drm)
1155 {
1156 struct dri2_egl_display *dri2_dpy = data;
1157
1158 dri2_dpy->authenticated = true;
1159 }
1160
1161 static const struct wl_drm_listener drm_listener = {
1162 .device = drm_handle_device,
1163 .format = drm_handle_format,
1164 .authenticated = drm_handle_authenticated,
1165 .capabilities = drm_handle_capabilities
1166 };
1167
1168 static void
1169 dmabuf_ignore_format(void *data, struct zwp_linux_dmabuf_v1 *dmabuf,
1170 uint32_t format)
1171 {
1172 /* formats are implicitly advertised by the 'modifier' event, so ignore */
1173 }
1174
1175 static void
1176 dmabuf_handle_modifier(void *data, struct zwp_linux_dmabuf_v1 *dmabuf,
1177 uint32_t format, uint32_t modifier_hi,
1178 uint32_t modifier_lo)
1179 {
1180 struct dri2_egl_display *dri2_dpy = data;
1181 int visual_idx = dri2_wl_visual_idx_from_fourcc(format);
1182 uint64_t *mod;
1183
1184 if (visual_idx == -1)
1185 return;
1186
1187 if (modifier_hi == (DRM_FORMAT_MOD_INVALID >> 32) &&
1188 modifier_lo == (DRM_FORMAT_MOD_INVALID & 0xffffffff))
1189 return;
1190
1191 dri2_dpy->formats |= (1u << visual_idx);
1192
1193 mod = u_vector_add(&dri2_dpy->wl_modifiers[visual_idx]);
1194 *mod = combine_u32_into_u64(modifier_hi, modifier_lo);
1195 }
1196
1197 static const struct zwp_linux_dmabuf_v1_listener dmabuf_listener = {
1198 .format = dmabuf_ignore_format,
1199 .modifier = dmabuf_handle_modifier,
1200 };
1201
1202 static void
1203 registry_handle_global_drm(void *data, struct wl_registry *registry,
1204 uint32_t name, const char *interface,
1205 uint32_t version)
1206 {
1207 struct dri2_egl_display *dri2_dpy = data;
1208
1209 if (strcmp(interface, "wl_drm") == 0) {
1210 dri2_dpy->wl_drm =
1211 wl_registry_bind(registry, name, &wl_drm_interface, MIN2(version, 2));
1212 wl_drm_add_listener(dri2_dpy->wl_drm, &drm_listener, dri2_dpy);
1213 } else if (strcmp(interface, "zwp_linux_dmabuf_v1") == 0 && version >= 3) {
1214 dri2_dpy->wl_dmabuf =
1215 wl_registry_bind(registry, name, &zwp_linux_dmabuf_v1_interface,
1216 MIN2(version, 3));
1217 zwp_linux_dmabuf_v1_add_listener(dri2_dpy->wl_dmabuf, &dmabuf_listener,
1218 dri2_dpy);
1219 }
1220 }
1221
1222 static void
1223 registry_handle_global_remove(void *data, struct wl_registry *registry,
1224 uint32_t name)
1225 {
1226 }
1227
1228 static const struct wl_registry_listener registry_listener_drm = {
1229 .global = registry_handle_global_drm,
1230 .global_remove = registry_handle_global_remove
1231 };
1232
1233 static void
1234 dri2_wl_setup_swap_interval(_EGLDisplay *disp)
1235 {
1236 /* We can't use values greater than 1 on Wayland because we are using the
1237 * frame callback to synchronise the frame and the only way we be sure to
1238 * get a frame callback is to attach a new buffer. Therefore we can't just
1239 * sit drawing nothing to wait until the next ‘n’ frame callbacks */
1240
1241 dri2_setup_swap_interval(disp, 1);
1242 }
1243
1244 static const struct dri2_egl_display_vtbl dri2_wl_display_vtbl = {
1245 .authenticate = dri2_wl_authenticate,
1246 .create_window_surface = dri2_wl_create_window_surface,
1247 .create_pixmap_surface = dri2_wl_create_pixmap_surface,
1248 .create_pbuffer_surface = dri2_fallback_create_pbuffer_surface,
1249 .destroy_surface = dri2_wl_destroy_surface,
1250 .create_image = dri2_create_image_khr,
1251 .swap_buffers = dri2_wl_swap_buffers,
1252 .swap_buffers_with_damage = dri2_wl_swap_buffers_with_damage,
1253 .swap_buffers_region = dri2_fallback_swap_buffers_region,
1254 .set_damage_region = dri2_fallback_set_damage_region,
1255 .post_sub_buffer = dri2_fallback_post_sub_buffer,
1256 .copy_buffers = dri2_fallback_copy_buffers,
1257 .query_buffer_age = dri2_wl_query_buffer_age,
1258 .create_wayland_buffer_from_image = dri2_wl_create_wayland_buffer_from_image,
1259 .get_sync_values = dri2_fallback_get_sync_values,
1260 .get_dri_drawable = dri2_surface_get_dri_drawable,
1261 };
1262
1263 static const __DRIextension *dri2_loader_extensions[] = {
1264 &dri2_loader_extension.base,
1265 &image_loader_extension.base,
1266 &image_lookup_extension.base,
1267 &use_invalidate.base,
1268 NULL,
1269 };
1270
1271 static const __DRIextension *image_loader_extensions[] = {
1272 &image_loader_extension.base,
1273 &image_lookup_extension.base,
1274 &use_invalidate.base,
1275 NULL,
1276 };
1277
1278 static EGLBoolean
1279 dri2_wl_add_configs_for_visuals(_EGLDriver *drv, _EGLDisplay *disp)
1280 {
1281 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1282 unsigned int format_count[ARRAY_SIZE(dri2_wl_visuals)] = { 0 };
1283 unsigned int count = 0;
1284
1285 for (unsigned i = 0; dri2_dpy->driver_configs[i]; i++) {
1286 for (unsigned j = 0; j < ARRAY_SIZE(dri2_wl_visuals); j++) {
1287 struct dri2_egl_config *dri2_conf;
1288
1289 if (!(dri2_dpy->formats & (1u << j)))
1290 continue;
1291
1292 dri2_conf = dri2_add_config(disp, dri2_dpy->driver_configs[i],
1293 count + 1, EGL_WINDOW_BIT, NULL, dri2_wl_visuals[j].rgba_masks);
1294 if (dri2_conf) {
1295 if (dri2_conf->base.ConfigID == count + 1)
1296 count++;
1297 format_count[j]++;
1298 }
1299 }
1300 }
1301
1302 for (unsigned i = 0; i < ARRAY_SIZE(format_count); i++) {
1303 if (!format_count[i]) {
1304 _eglLog(_EGL_DEBUG, "No DRI config supports native format %s",
1305 dri2_wl_visuals[i].format_name);
1306 }
1307 }
1308
1309 return (count != 0);
1310 }
1311
1312 static EGLBoolean
1313 dri2_initialize_wayland_drm(_EGLDriver *drv, _EGLDisplay *disp)
1314 {
1315 struct dri2_egl_display *dri2_dpy;
1316
1317 loader_set_logger(_eglLog);
1318
1319 dri2_dpy = calloc(1, sizeof *dri2_dpy);
1320 if (!dri2_dpy)
1321 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
1322
1323 dri2_dpy->fd = -1;
1324 disp->DriverData = (void *) dri2_dpy;
1325 if (disp->PlatformDisplay == NULL) {
1326 dri2_dpy->wl_dpy = wl_display_connect(NULL);
1327 if (dri2_dpy->wl_dpy == NULL)
1328 goto cleanup;
1329 dri2_dpy->own_device = true;
1330 } else {
1331 dri2_dpy->wl_dpy = disp->PlatformDisplay;
1332 }
1333
1334 dri2_dpy->wl_modifiers =
1335 calloc(ARRAY_SIZE(dri2_wl_visuals), sizeof(*dri2_dpy->wl_modifiers));
1336 if (!dri2_dpy->wl_modifiers)
1337 goto cleanup;
1338 for (int i = 0; i < ARRAY_SIZE(dri2_wl_visuals); i++) {
1339 if (!u_vector_init(&dri2_dpy->wl_modifiers[i], sizeof(uint64_t), 32))
1340 goto cleanup;
1341 }
1342
1343 dri2_dpy->wl_queue = wl_display_create_queue(dri2_dpy->wl_dpy);
1344
1345 dri2_dpy->wl_dpy_wrapper = wl_proxy_create_wrapper(dri2_dpy->wl_dpy);
1346 if (dri2_dpy->wl_dpy_wrapper == NULL)
1347 goto cleanup;
1348
1349 wl_proxy_set_queue((struct wl_proxy *) dri2_dpy->wl_dpy_wrapper,
1350 dri2_dpy->wl_queue);
1351
1352 if (dri2_dpy->own_device)
1353 wl_display_dispatch_pending(dri2_dpy->wl_dpy);
1354
1355 dri2_dpy->wl_registry = wl_display_get_registry(dri2_dpy->wl_dpy_wrapper);
1356 wl_registry_add_listener(dri2_dpy->wl_registry,
1357 &registry_listener_drm, dri2_dpy);
1358 if (roundtrip(dri2_dpy) < 0 || dri2_dpy->wl_drm == NULL)
1359 goto cleanup;
1360
1361 if (roundtrip(dri2_dpy) < 0 || dri2_dpy->fd == -1)
1362 goto cleanup;
1363
1364 if (roundtrip(dri2_dpy) < 0 || !dri2_dpy->authenticated)
1365 goto cleanup;
1366
1367 dri2_dpy->fd = loader_get_user_preferred_fd(dri2_dpy->fd,
1368 &dri2_dpy->is_different_gpu);
1369 if (dri2_dpy->is_different_gpu) {
1370 free(dri2_dpy->device_name);
1371 dri2_dpy->device_name = loader_get_device_name_for_fd(dri2_dpy->fd);
1372 if (!dri2_dpy->device_name) {
1373 _eglError(EGL_BAD_ALLOC, "wayland-egl: failed to get device name "
1374 "for requested GPU");
1375 goto cleanup;
1376 }
1377 }
1378
1379 /* we have to do the check now, because loader_get_user_preferred_fd
1380 * will return a render-node when the requested gpu is different
1381 * to the server, but also if the client asks for the same gpu than
1382 * the server by requesting its pci-id */
1383 dri2_dpy->is_render_node = drmGetNodeTypeFromFd(dri2_dpy->fd) == DRM_NODE_RENDER;
1384
1385 dri2_dpy->driver_name = loader_get_driver_for_fd(dri2_dpy->fd);
1386 if (dri2_dpy->driver_name == NULL) {
1387 _eglError(EGL_BAD_ALLOC, "DRI2: failed to get driver name");
1388 goto cleanup;
1389 }
1390
1391 /* render nodes cannot use Gem names, and thus do not support
1392 * the __DRI_DRI2_LOADER extension */
1393 if (!dri2_dpy->is_render_node) {
1394 dri2_dpy->loader_extensions = dri2_loader_extensions;
1395 if (!dri2_load_driver(disp)) {
1396 _eglError(EGL_BAD_ALLOC, "DRI2: failed to load driver");
1397 goto cleanup;
1398 }
1399 } else {
1400 dri2_dpy->loader_extensions = image_loader_extensions;
1401 if (!dri2_load_driver_dri3(disp)) {
1402 _eglError(EGL_BAD_ALLOC, "DRI3: failed to load driver");
1403 goto cleanup;
1404 }
1405 }
1406
1407 if (!dri2_create_screen(disp))
1408 goto cleanup;
1409
1410 if (!dri2_setup_extensions(disp))
1411 goto cleanup;
1412
1413 dri2_setup_screen(disp);
1414
1415 dri2_wl_setup_swap_interval(disp);
1416
1417 /* To use Prime, we must have _DRI_IMAGE v7 at least.
1418 * createImageFromFds support indicates that Prime export/import
1419 * is supported by the driver. Fall back to
1420 * gem names if we don't have Prime support. */
1421
1422 if (dri2_dpy->image->base.version < 7 ||
1423 dri2_dpy->image->createImageFromFds == NULL)
1424 dri2_dpy->capabilities &= ~WL_DRM_CAPABILITY_PRIME;
1425
1426 /* We cannot use Gem names with render-nodes, only prime fds (dma-buf).
1427 * The server needs to accept them */
1428 if (dri2_dpy->is_render_node &&
1429 !(dri2_dpy->capabilities & WL_DRM_CAPABILITY_PRIME)) {
1430 _eglLog(_EGL_WARNING, "wayland-egl: display is not render-node capable");
1431 goto cleanup;
1432 }
1433
1434 if (dri2_dpy->is_different_gpu &&
1435 (dri2_dpy->image->base.version < 9 ||
1436 dri2_dpy->image->blitImage == NULL)) {
1437 _eglLog(_EGL_WARNING, "wayland-egl: Different GPU selected, but the "
1438 "Image extension in the driver is not "
1439 "compatible. Version 9 or later and blitImage() "
1440 "are required");
1441 goto cleanup;
1442 }
1443
1444 if (!dri2_wl_add_configs_for_visuals(drv, disp)) {
1445 _eglError(EGL_NOT_INITIALIZED, "DRI2: failed to add configs");
1446 goto cleanup;
1447 }
1448
1449 dri2_set_WL_bind_wayland_display(drv, disp);
1450 /* When cannot convert EGLImage to wl_buffer when on a different gpu,
1451 * because the buffer of the EGLImage has likely a tiling mode the server
1452 * gpu won't support. These is no way to check for now. Thus do not support the
1453 * extension */
1454 if (!dri2_dpy->is_different_gpu)
1455 disp->Extensions.WL_create_wayland_buffer_from_image = EGL_TRUE;
1456
1457 disp->Extensions.EXT_buffer_age = EGL_TRUE;
1458
1459 disp->Extensions.EXT_swap_buffers_with_damage = EGL_TRUE;
1460
1461 /* Fill vtbl last to prevent accidentally calling virtual function during
1462 * initialization.
1463 */
1464 dri2_dpy->vtbl = &dri2_wl_display_vtbl;
1465
1466 return EGL_TRUE;
1467
1468 cleanup:
1469 dri2_display_destroy(disp);
1470 return EGL_FALSE;
1471 }
1472
1473 static int
1474 dri2_wl_swrast_get_stride_for_format(int format, int w)
1475 {
1476 int visual_idx = dri2_wl_visual_idx_from_shm_format(format);
1477
1478 assume(visual_idx != -1);
1479
1480 return w * (dri2_wl_visuals[visual_idx].bpp / 8);
1481 }
1482
1483 /*
1484 * Taken from weston shared/os-compatibility.c
1485 */
1486
1487 #ifndef HAVE_MKOSTEMP
1488
1489 static int
1490 set_cloexec_or_close(int fd)
1491 {
1492 long flags;
1493
1494 if (fd == -1)
1495 return -1;
1496
1497 flags = fcntl(fd, F_GETFD);
1498 if (flags == -1)
1499 goto err;
1500
1501 if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1)
1502 goto err;
1503
1504 return fd;
1505
1506 err:
1507 close(fd);
1508 return -1;
1509 }
1510
1511 #endif
1512
1513 /*
1514 * Taken from weston shared/os-compatibility.c
1515 */
1516
1517 static int
1518 create_tmpfile_cloexec(char *tmpname)
1519 {
1520 int fd;
1521
1522 #ifdef HAVE_MKOSTEMP
1523 fd = mkostemp(tmpname, O_CLOEXEC);
1524 if (fd >= 0)
1525 unlink(tmpname);
1526 #else
1527 fd = mkstemp(tmpname);
1528 if (fd >= 0) {
1529 fd = set_cloexec_or_close(fd);
1530 unlink(tmpname);
1531 }
1532 #endif
1533
1534 return fd;
1535 }
1536
1537 /*
1538 * Taken from weston shared/os-compatibility.c
1539 *
1540 * Create a new, unique, anonymous file of the given size, and
1541 * return the file descriptor for it. The file descriptor is set
1542 * CLOEXEC. The file is immediately suitable for mmap()'ing
1543 * the given size at offset zero.
1544 *
1545 * The file should not have a permanent backing store like a disk,
1546 * but may have if XDG_RUNTIME_DIR is not properly implemented in OS.
1547 *
1548 * The file name is deleted from the file system.
1549 *
1550 * The file is suitable for buffer sharing between processes by
1551 * transmitting the file descriptor over Unix sockets using the
1552 * SCM_RIGHTS methods.
1553 *
1554 * If the C library implements posix_fallocate(), it is used to
1555 * guarantee that disk space is available for the file at the
1556 * given size. If disk space is insufficient, errno is set to ENOSPC.
1557 * If posix_fallocate() is not supported, program may receive
1558 * SIGBUS on accessing mmap()'ed file contents instead.
1559 */
1560 static int
1561 os_create_anonymous_file(off_t size)
1562 {
1563 static const char templ[] = "/mesa-shared-XXXXXX";
1564 const char *path;
1565 char *name;
1566 int fd;
1567 int ret;
1568
1569 path = getenv("XDG_RUNTIME_DIR");
1570 if (!path) {
1571 errno = ENOENT;
1572 return -1;
1573 }
1574
1575 name = malloc(strlen(path) + sizeof(templ));
1576 if (!name)
1577 return -1;
1578
1579 strcpy(name, path);
1580 strcat(name, templ);
1581
1582 fd = create_tmpfile_cloexec(name);
1583
1584 free(name);
1585
1586 if (fd < 0)
1587 return -1;
1588
1589 ret = ftruncate(fd, size);
1590 if (ret < 0) {
1591 close(fd);
1592 return -1;
1593 }
1594
1595 return fd;
1596 }
1597
1598
1599 static EGLBoolean
1600 dri2_wl_swrast_allocate_buffer(struct dri2_egl_surface *dri2_surf,
1601 int format, int w, int h,
1602 void **data, int *size,
1603 struct wl_buffer **buffer)
1604 {
1605 struct dri2_egl_display *dri2_dpy =
1606 dri2_egl_display(dri2_surf->base.Resource.Display);
1607 struct wl_shm_pool *pool;
1608 int fd, stride, size_map;
1609 void *data_map;
1610
1611 stride = dri2_wl_swrast_get_stride_for_format(format, w);
1612 size_map = h * stride;
1613
1614 /* Create a shareable buffer */
1615 fd = os_create_anonymous_file(size_map);
1616 if (fd < 0)
1617 return EGL_FALSE;
1618
1619 data_map = mmap(NULL, size_map, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
1620 if (data_map == MAP_FAILED) {
1621 close(fd);
1622 return EGL_FALSE;
1623 }
1624
1625 /* Share it in a wl_buffer */
1626 pool = wl_shm_create_pool(dri2_dpy->wl_shm, fd, size_map);
1627 wl_proxy_set_queue((struct wl_proxy *)pool, dri2_surf->wl_queue);
1628 *buffer = wl_shm_pool_create_buffer(pool, 0, w, h, stride, format);
1629 wl_shm_pool_destroy(pool);
1630 close(fd);
1631
1632 *data = data_map;
1633 *size = size_map;
1634 return EGL_TRUE;
1635 }
1636
1637 static int
1638 swrast_update_buffers(struct dri2_egl_surface *dri2_surf)
1639 {
1640 struct dri2_egl_display *dri2_dpy =
1641 dri2_egl_display(dri2_surf->base.Resource.Display);
1642
1643 /* we need to do the following operations only once per frame */
1644 if (dri2_surf->back)
1645 return 0;
1646
1647 if (dri2_surf->base.Width != dri2_surf->wl_win->attached_width ||
1648 dri2_surf->base.Height != dri2_surf->wl_win->attached_height) {
1649
1650 dri2_wl_release_buffers(dri2_surf);
1651
1652 dri2_surf->base.Width = dri2_surf->wl_win->width;
1653 dri2_surf->base.Height = dri2_surf->wl_win->height;
1654 dri2_surf->dx = dri2_surf->wl_win->dx;
1655 dri2_surf->dy = dri2_surf->wl_win->dy;
1656 dri2_surf->current = NULL;
1657 }
1658
1659 /* find back buffer */
1660
1661 /* There might be a buffer release already queued that wasn't processed */
1662 wl_display_dispatch_queue_pending(dri2_dpy->wl_dpy, dri2_surf->wl_queue);
1663
1664 /* try get free buffer already created */
1665 for (int i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
1666 if (!dri2_surf->color_buffers[i].locked &&
1667 dri2_surf->color_buffers[i].wl_buffer) {
1668 dri2_surf->back = &dri2_surf->color_buffers[i];
1669 break;
1670 }
1671 }
1672
1673 /* else choose any another free location */
1674 if (!dri2_surf->back) {
1675 for (int i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
1676 if (!dri2_surf->color_buffers[i].locked) {
1677 dri2_surf->back = &dri2_surf->color_buffers[i];
1678 if (!dri2_wl_swrast_allocate_buffer(dri2_surf,
1679 dri2_surf->format,
1680 dri2_surf->base.Width,
1681 dri2_surf->base.Height,
1682 &dri2_surf->back->data,
1683 &dri2_surf->back->data_size,
1684 &dri2_surf->back->wl_buffer)) {
1685 _eglError(EGL_BAD_ALLOC, "failed to allocate color buffer");
1686 return -1;
1687 }
1688 wl_buffer_add_listener(dri2_surf->back->wl_buffer,
1689 &wl_buffer_listener, dri2_surf);
1690 break;
1691 }
1692 }
1693 }
1694
1695 if (!dri2_surf->back) {
1696 _eglError(EGL_BAD_ALLOC, "failed to find free buffer");
1697 return -1;
1698 }
1699
1700 dri2_surf->back->locked = true;
1701
1702 /* If we have an extra unlocked buffer at this point, we had to do triple
1703 * buffering for a while, but now can go back to just double buffering.
1704 * That means we can free any unlocked buffer now. */
1705 for (int i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
1706 if (!dri2_surf->color_buffers[i].locked &&
1707 dri2_surf->color_buffers[i].wl_buffer) {
1708 wl_buffer_destroy(dri2_surf->color_buffers[i].wl_buffer);
1709 munmap(dri2_surf->color_buffers[i].data,
1710 dri2_surf->color_buffers[i].data_size);
1711 dri2_surf->color_buffers[i].wl_buffer = NULL;
1712 dri2_surf->color_buffers[i].data = NULL;
1713 }
1714 }
1715
1716 return 0;
1717 }
1718
1719 static void*
1720 dri2_wl_swrast_get_frontbuffer_data(struct dri2_egl_surface *dri2_surf)
1721 {
1722 /* if there has been a resize: */
1723 if (!dri2_surf->current)
1724 return NULL;
1725
1726 return dri2_surf->current->data;
1727 }
1728
1729 static void*
1730 dri2_wl_swrast_get_backbuffer_data(struct dri2_egl_surface *dri2_surf)
1731 {
1732 assert(dri2_surf->back);
1733 return dri2_surf->back->data;
1734 }
1735
1736 static void
1737 dri2_wl_swrast_commit_backbuffer(struct dri2_egl_surface *dri2_surf)
1738 {
1739 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dri2_surf->base.Resource.Display);
1740
1741 while (dri2_surf->throttle_callback != NULL)
1742 if (wl_display_dispatch_queue(dri2_dpy->wl_dpy,
1743 dri2_surf->wl_queue) == -1)
1744 return;
1745
1746 if (dri2_surf->base.SwapInterval > 0) {
1747 dri2_surf->throttle_callback =
1748 wl_surface_frame(dri2_surf->wl_surface_wrapper);
1749 wl_callback_add_listener(dri2_surf->throttle_callback,
1750 &throttle_listener, dri2_surf);
1751 }
1752
1753 dri2_surf->current = dri2_surf->back;
1754 dri2_surf->back = NULL;
1755
1756 wl_surface_attach(dri2_surf->wl_surface_wrapper,
1757 dri2_surf->current->wl_buffer,
1758 dri2_surf->dx, dri2_surf->dy);
1759
1760 dri2_surf->wl_win->attached_width = dri2_surf->base.Width;
1761 dri2_surf->wl_win->attached_height = dri2_surf->base.Height;
1762 /* reset resize growing parameters */
1763 dri2_surf->dx = 0;
1764 dri2_surf->dy = 0;
1765
1766 wl_surface_damage(dri2_surf->wl_surface_wrapper,
1767 0, 0, INT32_MAX, INT32_MAX);
1768 wl_surface_commit(dri2_surf->wl_surface_wrapper);
1769
1770 /* If we're not waiting for a frame callback then we'll at least throttle
1771 * to a sync callback so that we always give a chance for the compositor to
1772 * handle the commit and send a release event before checking for a free
1773 * buffer */
1774 if (dri2_surf->throttle_callback == NULL) {
1775 dri2_surf->throttle_callback = wl_display_sync(dri2_surf->wl_dpy_wrapper);
1776 wl_callback_add_listener(dri2_surf->throttle_callback,
1777 &throttle_listener, dri2_surf);
1778 }
1779
1780 wl_display_flush(dri2_dpy->wl_dpy);
1781 }
1782
1783 static void
1784 dri2_wl_swrast_get_drawable_info(__DRIdrawable * draw,
1785 int *x, int *y, int *w, int *h,
1786 void *loaderPrivate)
1787 {
1788 struct dri2_egl_surface *dri2_surf = loaderPrivate;
1789
1790 (void) swrast_update_buffers(dri2_surf);
1791 *x = 0;
1792 *y = 0;
1793 *w = dri2_surf->base.Width;
1794 *h = dri2_surf->base.Height;
1795 }
1796
1797 static void
1798 dri2_wl_swrast_get_image(__DRIdrawable * read,
1799 int x, int y, int w, int h,
1800 char *data, void *loaderPrivate)
1801 {
1802 struct dri2_egl_surface *dri2_surf = loaderPrivate;
1803 int copy_width = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, w);
1804 int x_offset = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, x);
1805 int src_stride = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, dri2_surf->base.Width);
1806 int dst_stride = copy_width;
1807 char *src, *dst;
1808
1809 src = dri2_wl_swrast_get_frontbuffer_data(dri2_surf);
1810 if (!src) {
1811 memset(data, 0, copy_width * h);
1812 return;
1813 }
1814
1815 assert(data != src);
1816 assert(copy_width <= src_stride);
1817
1818 src += x_offset;
1819 src += y * src_stride;
1820 dst = data;
1821
1822 if (copy_width > src_stride-x_offset)
1823 copy_width = src_stride-x_offset;
1824 if (h > dri2_surf->base.Height-y)
1825 h = dri2_surf->base.Height-y;
1826
1827 for (; h>0; h--) {
1828 memcpy(dst, src, copy_width);
1829 src += src_stride;
1830 dst += dst_stride;
1831 }
1832 }
1833
1834 static void
1835 dri2_wl_swrast_put_image2(__DRIdrawable * draw, int op,
1836 int x, int y, int w, int h, int stride,
1837 char *data, void *loaderPrivate)
1838 {
1839 struct dri2_egl_surface *dri2_surf = loaderPrivate;
1840 int copy_width = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, w);
1841 int dst_stride = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, dri2_surf->base.Width);
1842 int x_offset = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, x);
1843 char *src, *dst;
1844
1845 assert(copy_width <= stride);
1846
1847 (void) swrast_update_buffers(dri2_surf);
1848 dst = dri2_wl_swrast_get_backbuffer_data(dri2_surf);
1849
1850 /* partial copy, copy old content */
1851 if (copy_width < dst_stride)
1852 dri2_wl_swrast_get_image(draw, 0, 0,
1853 dri2_surf->base.Width, dri2_surf->base.Height,
1854 dst, loaderPrivate);
1855
1856 dst += x_offset;
1857 dst += y * dst_stride;
1858
1859 src = data;
1860
1861 /* drivers expect we do these checks (and some rely on it) */
1862 if (copy_width > dst_stride-x_offset)
1863 copy_width = dst_stride-x_offset;
1864 if (h > dri2_surf->base.Height-y)
1865 h = dri2_surf->base.Height-y;
1866
1867 for (; h>0; h--) {
1868 memcpy(dst, src, copy_width);
1869 src += stride;
1870 dst += dst_stride;
1871 }
1872 dri2_wl_swrast_commit_backbuffer(dri2_surf);
1873 }
1874
1875 static void
1876 dri2_wl_swrast_put_image(__DRIdrawable * draw, int op,
1877 int x, int y, int w, int h,
1878 char *data, void *loaderPrivate)
1879 {
1880 struct dri2_egl_surface *dri2_surf = loaderPrivate;
1881 int stride;
1882
1883 stride = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, w);
1884 dri2_wl_swrast_put_image2(draw, op, x, y, w, h,
1885 stride, data, loaderPrivate);
1886 }
1887
1888 static EGLBoolean
1889 dri2_wl_swrast_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
1890 {
1891 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1892 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
1893
1894 dri2_dpy->core->swapBuffers(dri2_surf->dri_drawable);
1895 return EGL_TRUE;
1896 }
1897
1898 static void
1899 shm_handle_format(void *data, struct wl_shm *shm, uint32_t format)
1900 {
1901 struct dri2_egl_display *dri2_dpy = data;
1902 int visual_idx = dri2_wl_visual_idx_from_shm_format(format);
1903
1904 if (visual_idx == -1)
1905 return;
1906
1907 dri2_dpy->formats |= (1u << visual_idx);
1908 }
1909
1910 static const struct wl_shm_listener shm_listener = {
1911 .format = shm_handle_format
1912 };
1913
1914 static void
1915 registry_handle_global_swrast(void *data, struct wl_registry *registry,
1916 uint32_t name, const char *interface,
1917 uint32_t version)
1918 {
1919 struct dri2_egl_display *dri2_dpy = data;
1920
1921 if (strcmp(interface, "wl_shm") == 0) {
1922 dri2_dpy->wl_shm =
1923 wl_registry_bind(registry, name, &wl_shm_interface, 1);
1924 wl_shm_add_listener(dri2_dpy->wl_shm, &shm_listener, dri2_dpy);
1925 }
1926 }
1927
1928 static const struct wl_registry_listener registry_listener_swrast = {
1929 .global = registry_handle_global_swrast,
1930 .global_remove = registry_handle_global_remove
1931 };
1932
1933 static const struct dri2_egl_display_vtbl dri2_wl_swrast_display_vtbl = {
1934 .authenticate = NULL,
1935 .create_window_surface = dri2_wl_create_window_surface,
1936 .create_pixmap_surface = dri2_wl_create_pixmap_surface,
1937 .create_pbuffer_surface = dri2_fallback_create_pbuffer_surface,
1938 .destroy_surface = dri2_wl_destroy_surface,
1939 .create_image = dri2_create_image_khr,
1940 .swap_buffers = dri2_wl_swrast_swap_buffers,
1941 .swap_buffers_with_damage = dri2_fallback_swap_buffers_with_damage,
1942 .swap_buffers_region = dri2_fallback_swap_buffers_region,
1943 .post_sub_buffer = dri2_fallback_post_sub_buffer,
1944 .copy_buffers = dri2_fallback_copy_buffers,
1945 .query_buffer_age = dri2_fallback_query_buffer_age,
1946 .create_wayland_buffer_from_image = dri2_fallback_create_wayland_buffer_from_image,
1947 .get_sync_values = dri2_fallback_get_sync_values,
1948 .get_dri_drawable = dri2_surface_get_dri_drawable,
1949 };
1950
1951 static const __DRIswrastLoaderExtension swrast_loader_extension = {
1952 .base = { __DRI_SWRAST_LOADER, 2 },
1953
1954 .getDrawableInfo = dri2_wl_swrast_get_drawable_info,
1955 .putImage = dri2_wl_swrast_put_image,
1956 .getImage = dri2_wl_swrast_get_image,
1957 .putImage2 = dri2_wl_swrast_put_image2,
1958 };
1959
1960 static const __DRIextension *swrast_loader_extensions[] = {
1961 &swrast_loader_extension.base,
1962 &image_lookup_extension.base,
1963 NULL,
1964 };
1965
1966 static EGLBoolean
1967 dri2_initialize_wayland_swrast(_EGLDriver *drv, _EGLDisplay *disp)
1968 {
1969 struct dri2_egl_display *dri2_dpy;
1970
1971 loader_set_logger(_eglLog);
1972
1973 dri2_dpy = calloc(1, sizeof *dri2_dpy);
1974 if (!dri2_dpy)
1975 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
1976
1977 dri2_dpy->fd = -1;
1978 disp->DriverData = (void *) dri2_dpy;
1979 if (disp->PlatformDisplay == NULL) {
1980 dri2_dpy->wl_dpy = wl_display_connect(NULL);
1981 if (dri2_dpy->wl_dpy == NULL)
1982 goto cleanup;
1983 dri2_dpy->own_device = true;
1984 } else {
1985 dri2_dpy->wl_dpy = disp->PlatformDisplay;
1986 }
1987
1988 dri2_dpy->wl_queue = wl_display_create_queue(dri2_dpy->wl_dpy);
1989
1990 dri2_dpy->wl_dpy_wrapper = wl_proxy_create_wrapper(dri2_dpy->wl_dpy);
1991 if (dri2_dpy->wl_dpy_wrapper == NULL)
1992 goto cleanup;
1993
1994 wl_proxy_set_queue((struct wl_proxy *) dri2_dpy->wl_dpy_wrapper,
1995 dri2_dpy->wl_queue);
1996
1997 if (dri2_dpy->own_device)
1998 wl_display_dispatch_pending(dri2_dpy->wl_dpy);
1999
2000 dri2_dpy->wl_registry = wl_display_get_registry(dri2_dpy->wl_dpy_wrapper);
2001 wl_registry_add_listener(dri2_dpy->wl_registry,
2002 &registry_listener_swrast, dri2_dpy);
2003
2004 if (roundtrip(dri2_dpy) < 0 || dri2_dpy->wl_shm == NULL)
2005 goto cleanup;
2006
2007 if (roundtrip(dri2_dpy) < 0 || dri2_dpy->formats == 0)
2008 goto cleanup;
2009
2010 dri2_dpy->driver_name = strdup("swrast");
2011 if (!dri2_load_driver_swrast(disp))
2012 goto cleanup;
2013
2014 dri2_dpy->loader_extensions = swrast_loader_extensions;
2015
2016 if (!dri2_create_screen(disp))
2017 goto cleanup;
2018
2019 if (!dri2_setup_extensions(disp))
2020 goto cleanup;
2021
2022 dri2_setup_screen(disp);
2023
2024 dri2_wl_setup_swap_interval(disp);
2025
2026 if (!dri2_wl_add_configs_for_visuals(drv, disp)) {
2027 _eglError(EGL_NOT_INITIALIZED, "DRI2: failed to add configs");
2028 goto cleanup;
2029 }
2030
2031 /* Fill vtbl last to prevent accidentally calling virtual function during
2032 * initialization.
2033 */
2034 dri2_dpy->vtbl = &dri2_wl_swrast_display_vtbl;
2035
2036 return EGL_TRUE;
2037
2038 cleanup:
2039 dri2_display_destroy(disp);
2040 return EGL_FALSE;
2041 }
2042
2043 EGLBoolean
2044 dri2_initialize_wayland(_EGLDriver *drv, _EGLDisplay *disp)
2045 {
2046 EGLBoolean initialized = EGL_FALSE;
2047
2048 if (!disp->Options.ForceSoftware)
2049 initialized = dri2_initialize_wayland_drm(drv, disp);
2050
2051 if (!initialized)
2052 initialized = dri2_initialize_wayland_swrast(drv, disp);
2053
2054 return initialized;
2055
2056 }
2057
2058 void
2059 dri2_teardown_wayland(struct dri2_egl_display *dri2_dpy)
2060 {
2061 if (dri2_dpy->wl_drm)
2062 wl_drm_destroy(dri2_dpy->wl_drm);
2063 if (dri2_dpy->wl_dmabuf)
2064 zwp_linux_dmabuf_v1_destroy(dri2_dpy->wl_dmabuf);
2065 if (dri2_dpy->wl_shm)
2066 wl_shm_destroy(dri2_dpy->wl_shm);
2067 if (dri2_dpy->wl_registry)
2068 wl_registry_destroy(dri2_dpy->wl_registry);
2069 if (dri2_dpy->wl_queue)
2070 wl_event_queue_destroy(dri2_dpy->wl_queue);
2071 if (dri2_dpy->wl_dpy_wrapper)
2072 wl_proxy_wrapper_destroy(dri2_dpy->wl_dpy_wrapper);
2073
2074 for (int i = 0; dri2_dpy->wl_modifiers && i < ARRAY_SIZE(dri2_wl_visuals); i++)
2075 u_vector_finish(&dri2_dpy->wl_modifiers[i]);
2076 free(dri2_dpy->wl_modifiers);
2077
2078 if (dri2_dpy->own_device)
2079 wl_display_disconnect(dri2_dpy->wl_dpy);
2080 }