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