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