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