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