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