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