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