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