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