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