egl_dri2: Add a function to let platform code return dri drawable from _EGLSurface
[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 /* We deliberately ignore the damage region and post maximum damage, due to
707 * https://bugs.freedesktop.org/78190 */
708 wl_surface_damage(dri2_surf->wl_win->surface,
709 0, 0, INT32_MAX, INT32_MAX);
710
711 if (dri2_dpy->is_different_gpu) {
712 _EGLContext *ctx = _eglGetCurrentContext();
713 struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
714 dri2_dpy->image->blitImage(dri2_ctx->dri_context,
715 dri2_surf->current->linear_copy,
716 dri2_surf->current->dri_image,
717 0, 0, dri2_surf->base.Width,
718 dri2_surf->base.Height,
719 0, 0, dri2_surf->base.Width,
720 dri2_surf->base.Height, 0);
721 }
722
723 dri2_flush_drawable_for_swapbuffers(disp, draw);
724 (*dri2_dpy->flush->invalidate)(dri2_surf->dri_drawable);
725
726 wl_surface_commit(dri2_surf->wl_win->surface);
727
728 /* If we're not waiting for a frame callback then we'll at least throttle
729 * to a sync callback so that we always give a chance for the compositor to
730 * handle the commit and send a release event before checking for a free
731 * buffer */
732 if (dri2_surf->throttle_callback == NULL) {
733 dri2_surf->throttle_callback = wl_display_sync(dri2_dpy->wl_dpy);
734 wl_callback_add_listener(dri2_surf->throttle_callback,
735 &throttle_listener, dri2_surf);
736 wl_proxy_set_queue((struct wl_proxy *) dri2_surf->throttle_callback,
737 dri2_dpy->wl_queue);
738 }
739
740 wl_display_flush(dri2_dpy->wl_dpy);
741
742 return EGL_TRUE;
743 }
744
745 static EGLint
746 dri2_wl_query_buffer_age(_EGLDriver *drv,
747 _EGLDisplay *disp, _EGLSurface *surface)
748 {
749 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surface);
750
751 if (get_back_bo(dri2_surf) < 0) {
752 _eglError(EGL_BAD_ALLOC, "dri2_query_buffer_age");
753 return 0;
754 }
755
756 return dri2_surf->back->age;
757 }
758
759 static EGLBoolean
760 dri2_wl_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
761 {
762 return dri2_wl_swap_buffers_with_damage (drv, disp, draw, NULL, 0);
763 }
764
765 static struct wl_buffer *
766 dri2_wl_create_wayland_buffer_from_image(_EGLDriver *drv,
767 _EGLDisplay *disp,
768 _EGLImage *img)
769 {
770 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
771 struct dri2_egl_image *dri2_img = dri2_egl_image(img);
772 __DRIimage *image = dri2_img->dri_image;
773 struct wl_buffer *buffer;
774 int width, height, format, pitch;
775 enum wl_drm_format wl_format;
776
777 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_FORMAT, &format);
778
779 switch (format) {
780 case __DRI_IMAGE_FORMAT_ARGB8888:
781 if (!(dri2_dpy->formats & HAS_ARGB8888))
782 goto bad_format;
783 wl_format = WL_DRM_FORMAT_ARGB8888;
784 break;
785 case __DRI_IMAGE_FORMAT_XRGB8888:
786 if (!(dri2_dpy->formats & HAS_XRGB8888))
787 goto bad_format;
788 wl_format = WL_DRM_FORMAT_XRGB8888;
789 break;
790 default:
791 goto bad_format;
792 }
793
794 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_WIDTH, &width);
795 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_HEIGHT, &height);
796 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_STRIDE, &pitch);
797
798 if (dri2_dpy->capabilities & WL_DRM_CAPABILITY_PRIME) {
799 int fd;
800
801 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_FD, &fd);
802
803 buffer =
804 wl_drm_create_prime_buffer(dri2_dpy->wl_drm,
805 fd,
806 width, height,
807 wl_format,
808 0, pitch,
809 0, 0,
810 0, 0);
811
812 close(fd);
813 } else {
814 int name;
815
816 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_NAME, &name);
817
818 buffer =
819 wl_drm_create_buffer(dri2_dpy->wl_drm,
820 name,
821 width, height,
822 pitch,
823 wl_format);
824 }
825
826 /* The buffer object will have been created with our internal event queue
827 * because it is using the wl_drm object as a proxy factory. We want the
828 * buffer to be used by the application so we'll reset it to the display's
829 * default event queue */
830 if (buffer)
831 wl_proxy_set_queue((struct wl_proxy *) buffer, NULL);
832
833 return buffer;
834
835 bad_format:
836 _eglError(EGL_BAD_MATCH, "unsupported image format");
837 return NULL;
838 }
839
840 static int
841 dri2_wl_authenticate(_EGLDisplay *disp, uint32_t id)
842 {
843 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
844 int ret = 0;
845
846 if (dri2_dpy->is_render_node) {
847 _eglLog(_EGL_WARNING, "wayland-egl: client asks server to "
848 "authenticate for render-nodes");
849 return 0;
850 }
851 dri2_dpy->authenticated = 0;
852
853 wl_drm_authenticate(dri2_dpy->wl_drm, id);
854 if (roundtrip(dri2_dpy) < 0)
855 ret = -1;
856
857 if (!dri2_dpy->authenticated)
858 ret = -1;
859
860 /* reset authenticated */
861 dri2_dpy->authenticated = 1;
862
863 return ret;
864 }
865
866 static void
867 drm_handle_device(void *data, struct wl_drm *drm, const char *device)
868 {
869 struct dri2_egl_display *dri2_dpy = data;
870 drm_magic_t magic;
871
872 dri2_dpy->device_name = strdup(device);
873 if (!dri2_dpy->device_name)
874 return;
875
876 dri2_dpy->fd = loader_open_device(dri2_dpy->device_name);
877 if (dri2_dpy->fd == -1) {
878 _eglLog(_EGL_WARNING, "wayland-egl: could not open %s (%s)",
879 dri2_dpy->device_name, strerror(errno));
880 return;
881 }
882
883 if (drmGetNodeTypeFromFd(dri2_dpy->fd) == DRM_NODE_RENDER) {
884 dri2_dpy->authenticated = 1;
885 } else {
886 drmGetMagic(dri2_dpy->fd, &magic);
887 wl_drm_authenticate(dri2_dpy->wl_drm, magic);
888 }
889 }
890
891 static void
892 drm_handle_format(void *data, struct wl_drm *drm, uint32_t format)
893 {
894 struct dri2_egl_display *dri2_dpy = data;
895
896 switch (format) {
897 case WL_DRM_FORMAT_ARGB8888:
898 dri2_dpy->formats |= HAS_ARGB8888;
899 break;
900 case WL_DRM_FORMAT_XRGB8888:
901 dri2_dpy->formats |= HAS_XRGB8888;
902 break;
903 case WL_DRM_FORMAT_RGB565:
904 dri2_dpy->formats |= HAS_RGB565;
905 break;
906 }
907 }
908
909 static void
910 drm_handle_capabilities(void *data, struct wl_drm *drm, uint32_t value)
911 {
912 struct dri2_egl_display *dri2_dpy = data;
913
914 dri2_dpy->capabilities = value;
915 }
916
917 static void
918 drm_handle_authenticated(void *data, struct wl_drm *drm)
919 {
920 struct dri2_egl_display *dri2_dpy = data;
921
922 dri2_dpy->authenticated = 1;
923 }
924
925 static const struct wl_drm_listener drm_listener = {
926 .device = drm_handle_device,
927 .format = drm_handle_format,
928 .authenticated = drm_handle_authenticated,
929 .capabilities = drm_handle_capabilities
930 };
931
932 static void
933 registry_handle_global_drm(void *data, struct wl_registry *registry, uint32_t name,
934 const char *interface, uint32_t version)
935 {
936 struct dri2_egl_display *dri2_dpy = data;
937
938 if (version > 1)
939 version = 2;
940 if (strcmp(interface, "wl_drm") == 0) {
941 dri2_dpy->wl_drm =
942 wl_registry_bind(registry, name, &wl_drm_interface, version);
943 wl_drm_add_listener(dri2_dpy->wl_drm, &drm_listener, dri2_dpy);
944 }
945 }
946
947 static void
948 registry_handle_global_remove(void *data, struct wl_registry *registry,
949 uint32_t name)
950 {
951 }
952
953 static const struct wl_registry_listener registry_listener_drm = {
954 .global = registry_handle_global_drm,
955 .global_remove = registry_handle_global_remove
956 };
957
958 static EGLBoolean
959 dri2_wl_swap_interval(_EGLDriver *drv,
960 _EGLDisplay *disp,
961 _EGLSurface *surf,
962 EGLint interval)
963 {
964 if (interval > surf->Config->MaxSwapInterval)
965 interval = surf->Config->MaxSwapInterval;
966 else if (interval < surf->Config->MinSwapInterval)
967 interval = surf->Config->MinSwapInterval;
968
969 surf->SwapInterval = interval;
970
971 return EGL_TRUE;
972 }
973
974 static void
975 dri2_wl_setup_swap_interval(struct dri2_egl_display *dri2_dpy)
976 {
977 GLint vblank_mode = DRI_CONF_VBLANK_DEF_INTERVAL_1;
978
979 /* We can't use values greater than 1 on Wayland because we are using the
980 * frame callback to synchronise the frame and the only way we be sure to
981 * get a frame callback is to attach a new buffer. Therefore we can't just
982 * sit drawing nothing to wait until the next ‘n’ frame callbacks */
983
984 if (dri2_dpy->config)
985 dri2_dpy->config->configQueryi(dri2_dpy->dri_screen,
986 "vblank_mode", &vblank_mode);
987 switch (vblank_mode) {
988 case DRI_CONF_VBLANK_NEVER:
989 dri2_dpy->min_swap_interval = 0;
990 dri2_dpy->max_swap_interval = 0;
991 dri2_dpy->default_swap_interval = 0;
992 break;
993 case DRI_CONF_VBLANK_ALWAYS_SYNC:
994 dri2_dpy->min_swap_interval = 1;
995 dri2_dpy->max_swap_interval = 1;
996 dri2_dpy->default_swap_interval = 1;
997 break;
998 case DRI_CONF_VBLANK_DEF_INTERVAL_0:
999 dri2_dpy->min_swap_interval = 0;
1000 dri2_dpy->max_swap_interval = 1;
1001 dri2_dpy->default_swap_interval = 0;
1002 break;
1003 default:
1004 case DRI_CONF_VBLANK_DEF_INTERVAL_1:
1005 dri2_dpy->min_swap_interval = 0;
1006 dri2_dpy->max_swap_interval = 1;
1007 dri2_dpy->default_swap_interval = 1;
1008 break;
1009 }
1010 }
1011
1012 static struct dri2_egl_display_vtbl dri2_wl_display_vtbl = {
1013 .authenticate = dri2_wl_authenticate,
1014 .create_window_surface = dri2_wl_create_window_surface,
1015 .create_pixmap_surface = dri2_wl_create_pixmap_surface,
1016 .create_pbuffer_surface = dri2_fallback_create_pbuffer_surface,
1017 .destroy_surface = dri2_wl_destroy_surface,
1018 .create_image = dri2_create_image_khr,
1019 .swap_interval = dri2_wl_swap_interval,
1020 .swap_buffers = dri2_wl_swap_buffers,
1021 .swap_buffers_with_damage = dri2_wl_swap_buffers_with_damage,
1022 .swap_buffers_region = dri2_fallback_swap_buffers_region,
1023 .post_sub_buffer = dri2_fallback_post_sub_buffer,
1024 .copy_buffers = dri2_fallback_copy_buffers,
1025 .query_buffer_age = dri2_wl_query_buffer_age,
1026 .create_wayland_buffer_from_image = dri2_wl_create_wayland_buffer_from_image,
1027 .get_sync_values = dri2_fallback_get_sync_values,
1028 .get_dri_drawable = dri2_surface_get_dri_drawable,
1029 };
1030
1031 static EGLBoolean
1032 dri2_initialize_wayland_drm(_EGLDriver *drv, _EGLDisplay *disp)
1033 {
1034 struct dri2_egl_display *dri2_dpy;
1035 const __DRIconfig *config;
1036 uint32_t types;
1037 int i;
1038 static const unsigned int argb_masks[4] =
1039 { 0xff0000, 0xff00, 0xff, 0xff000000 };
1040 static const unsigned int rgb_masks[4] = { 0xff0000, 0xff00, 0xff, 0 };
1041 static const unsigned int rgb565_masks[4] = { 0xf800, 0x07e0, 0x001f, 0 };
1042
1043 loader_set_logger(_eglLog);
1044
1045 dri2_dpy = calloc(1, sizeof *dri2_dpy);
1046 if (!dri2_dpy)
1047 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
1048
1049 disp->DriverData = (void *) dri2_dpy;
1050 if (disp->PlatformDisplay == NULL) {
1051 dri2_dpy->wl_dpy = wl_display_connect(NULL);
1052 if (dri2_dpy->wl_dpy == NULL)
1053 goto cleanup_dpy;
1054 dri2_dpy->own_device = 1;
1055 } else {
1056 dri2_dpy->wl_dpy = disp->PlatformDisplay;
1057 }
1058
1059 dri2_dpy->wl_queue = wl_display_create_queue(dri2_dpy->wl_dpy);
1060
1061 if (dri2_dpy->own_device)
1062 wl_display_dispatch_pending(dri2_dpy->wl_dpy);
1063
1064 dri2_dpy->wl_registry = wl_display_get_registry(dri2_dpy->wl_dpy);
1065 wl_proxy_set_queue((struct wl_proxy *) dri2_dpy->wl_registry,
1066 dri2_dpy->wl_queue);
1067 wl_registry_add_listener(dri2_dpy->wl_registry,
1068 &registry_listener_drm, dri2_dpy);
1069 if (roundtrip(dri2_dpy) < 0 || dri2_dpy->wl_drm == NULL)
1070 goto cleanup_registry;
1071
1072 if (roundtrip(dri2_dpy) < 0 || dri2_dpy->fd == -1)
1073 goto cleanup_drm;
1074
1075 if (roundtrip(dri2_dpy) < 0 || !dri2_dpy->authenticated)
1076 goto cleanup_fd;
1077
1078 dri2_dpy->fd = loader_get_user_preferred_fd(dri2_dpy->fd,
1079 &dri2_dpy->is_different_gpu);
1080 if (dri2_dpy->is_different_gpu) {
1081 free(dri2_dpy->device_name);
1082 dri2_dpy->device_name = loader_get_device_name_for_fd(dri2_dpy->fd);
1083 if (!dri2_dpy->device_name) {
1084 _eglError(EGL_BAD_ALLOC, "wayland-egl: failed to get device name "
1085 "for requested GPU");
1086 goto cleanup_fd;
1087 }
1088 }
1089
1090 /* we have to do the check now, because loader_get_user_preferred_fd
1091 * will return a render-node when the requested gpu is different
1092 * to the server, but also if the client asks for the same gpu than
1093 * the server by requesting its pci-id */
1094 dri2_dpy->is_render_node = drmGetNodeTypeFromFd(dri2_dpy->fd) == DRM_NODE_RENDER;
1095
1096 dri2_dpy->driver_name = loader_get_driver_for_fd(dri2_dpy->fd, 0);
1097 if (dri2_dpy->driver_name == NULL) {
1098 _eglError(EGL_BAD_ALLOC, "DRI2: failed to get driver name");
1099 goto cleanup_fd;
1100 }
1101
1102 if (!dri2_load_driver(disp))
1103 goto cleanup_driver_name;
1104
1105 dri2_dpy->extensions[0] = &image_loader_extension.base;
1106 dri2_dpy->extensions[1] = &image_lookup_extension.base;
1107 dri2_dpy->extensions[2] = &use_invalidate.base;
1108
1109 /* render nodes cannot use Gem names, and thus do not support
1110 * the __DRI_DRI2_LOADER extension */
1111 if (!dri2_dpy->is_render_node) {
1112 dri2_dpy->dri2_loader_extension.base.name = __DRI_DRI2_LOADER;
1113 dri2_dpy->dri2_loader_extension.base.version = 3;
1114 dri2_dpy->dri2_loader_extension.getBuffers = dri2_wl_get_buffers;
1115 dri2_dpy->dri2_loader_extension.flushFrontBuffer = dri2_wl_flush_front_buffer;
1116 dri2_dpy->dri2_loader_extension.getBuffersWithFormat =
1117 dri2_wl_get_buffers_with_format;
1118 dri2_dpy->extensions[3] = &dri2_dpy->dri2_loader_extension.base;
1119 dri2_dpy->extensions[4] = NULL;
1120 } else
1121 dri2_dpy->extensions[3] = NULL;
1122
1123 dri2_dpy->swap_available = EGL_TRUE;
1124
1125 if (!dri2_create_screen(disp))
1126 goto cleanup_driver;
1127
1128 dri2_wl_setup_swap_interval(dri2_dpy);
1129
1130 /* To use Prime, we must have _DRI_IMAGE v7 at least.
1131 * createImageFromFds support indicates that Prime export/import
1132 * is supported by the driver. Fall back to
1133 * gem names if we don't have Prime support. */
1134
1135 if (dri2_dpy->image->base.version < 7 ||
1136 dri2_dpy->image->createImageFromFds == NULL)
1137 dri2_dpy->capabilities &= ~WL_DRM_CAPABILITY_PRIME;
1138
1139 /* We cannot use Gem names with render-nodes, only prime fds (dma-buf).
1140 * The server needs to accept them */
1141 if (dri2_dpy->is_render_node &&
1142 !(dri2_dpy->capabilities & WL_DRM_CAPABILITY_PRIME)) {
1143 _eglLog(_EGL_WARNING, "wayland-egl: display is not render-node capable");
1144 goto cleanup_screen;
1145 }
1146
1147 if (dri2_dpy->is_different_gpu &&
1148 (dri2_dpy->image->base.version < 9 ||
1149 dri2_dpy->image->blitImage == NULL)) {
1150 _eglLog(_EGL_WARNING, "wayland-egl: Different GPU selected, but the "
1151 "Image extension in the driver is not "
1152 "compatible. Version 9 or later and blitImage() "
1153 "are required");
1154 goto cleanup_screen;
1155 }
1156
1157 types = EGL_WINDOW_BIT;
1158 for (i = 0; dri2_dpy->driver_configs[i]; i++) {
1159 config = dri2_dpy->driver_configs[i];
1160 if (dri2_dpy->formats & HAS_XRGB8888)
1161 dri2_add_config(disp, config, i + 1, types, NULL, rgb_masks);
1162 if (dri2_dpy->formats & HAS_ARGB8888)
1163 dri2_add_config(disp, config, i + 1, types, NULL, argb_masks);
1164 if (dri2_dpy->formats & HAS_RGB565)
1165 dri2_add_config(disp, config, i + 1, types, NULL, rgb565_masks);
1166 }
1167
1168 disp->Extensions.WL_bind_wayland_display = EGL_TRUE;
1169 /* When cannot convert EGLImage to wl_buffer when on a different gpu,
1170 * because the buffer of the EGLImage has likely a tiling mode the server
1171 * gpu won't support. These is no way to check for now. Thus do not support the
1172 * extension */
1173 if (!dri2_dpy->is_different_gpu) {
1174 disp->Extensions.WL_create_wayland_buffer_from_image = EGL_TRUE;
1175 } else {
1176 dri2_wl_display_vtbl.create_wayland_buffer_from_image =
1177 dri2_fallback_create_wayland_buffer_from_image;
1178 }
1179 disp->Extensions.EXT_buffer_age = EGL_TRUE;
1180
1181 disp->Extensions.EXT_swap_buffers_with_damage = EGL_TRUE;
1182
1183 /* Fill vtbl last to prevent accidentally calling virtual function during
1184 * initialization.
1185 */
1186 dri2_dpy->vtbl = &dri2_wl_display_vtbl;
1187
1188 return EGL_TRUE;
1189
1190 cleanup_screen:
1191 dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen);
1192 cleanup_driver:
1193 dlclose(dri2_dpy->driver);
1194 cleanup_driver_name:
1195 free(dri2_dpy->driver_name);
1196 cleanup_fd:
1197 close(dri2_dpy->fd);
1198 cleanup_drm:
1199 free(dri2_dpy->device_name);
1200 wl_drm_destroy(dri2_dpy->wl_drm);
1201 cleanup_registry:
1202 wl_registry_destroy(dri2_dpy->wl_registry);
1203 wl_event_queue_destroy(dri2_dpy->wl_queue);
1204 cleanup_dpy:
1205 free(dri2_dpy);
1206
1207 return EGL_FALSE;
1208 }
1209
1210 static int
1211 dri2_wl_swrast_get_stride_for_format(int format, int w)
1212 {
1213 if (format == WL_SHM_FORMAT_RGB565)
1214 return 2 * w;
1215 else /* ARGB8888 || XRGB8888 */
1216 return 4 * w;
1217 }
1218
1219 /*
1220 * Taken from weston shared/os-compatibility.c
1221 */
1222
1223 #ifndef HAVE_MKOSTEMP
1224
1225 static int
1226 set_cloexec_or_close(int fd)
1227 {
1228 long flags;
1229
1230 if (fd == -1)
1231 return -1;
1232
1233 flags = fcntl(fd, F_GETFD);
1234 if (flags == -1)
1235 goto err;
1236
1237 if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1)
1238 goto err;
1239
1240 return fd;
1241
1242 err:
1243 close(fd);
1244 return -1;
1245 }
1246
1247 #endif
1248
1249 /*
1250 * Taken from weston shared/os-compatibility.c
1251 */
1252
1253 static int
1254 create_tmpfile_cloexec(char *tmpname)
1255 {
1256 int fd;
1257
1258 #ifdef HAVE_MKOSTEMP
1259 fd = mkostemp(tmpname, O_CLOEXEC);
1260 if (fd >= 0)
1261 unlink(tmpname);
1262 #else
1263 fd = mkstemp(tmpname);
1264 if (fd >= 0) {
1265 fd = set_cloexec_or_close(fd);
1266 unlink(tmpname);
1267 }
1268 #endif
1269
1270 return fd;
1271 }
1272
1273 /*
1274 * Taken from weston shared/os-compatibility.c
1275 *
1276 * Create a new, unique, anonymous file of the given size, and
1277 * return the file descriptor for it. The file descriptor is set
1278 * CLOEXEC. The file is immediately suitable for mmap()'ing
1279 * the given size at offset zero.
1280 *
1281 * The file should not have a permanent backing store like a disk,
1282 * but may have if XDG_RUNTIME_DIR is not properly implemented in OS.
1283 *
1284 * The file name is deleted from the file system.
1285 *
1286 * The file is suitable for buffer sharing between processes by
1287 * transmitting the file descriptor over Unix sockets using the
1288 * SCM_RIGHTS methods.
1289 *
1290 * If the C library implements posix_fallocate(), it is used to
1291 * guarantee that disk space is available for the file at the
1292 * given size. If disk space is insufficent, errno is set to ENOSPC.
1293 * If posix_fallocate() is not supported, program may receive
1294 * SIGBUS on accessing mmap()'ed file contents instead.
1295 */
1296 static int
1297 os_create_anonymous_file(off_t size)
1298 {
1299 static const char template[] = "/mesa-shared-XXXXXX";
1300 const char *path;
1301 char *name;
1302 int fd;
1303 int ret;
1304
1305 path = getenv("XDG_RUNTIME_DIR");
1306 if (!path) {
1307 errno = ENOENT;
1308 return -1;
1309 }
1310
1311 name = malloc(strlen(path) + sizeof(template));
1312 if (!name)
1313 return -1;
1314
1315 strcpy(name, path);
1316 strcat(name, template);
1317
1318 fd = create_tmpfile_cloexec(name);
1319
1320 free(name);
1321
1322 if (fd < 0)
1323 return -1;
1324
1325 ret = ftruncate(fd, size);
1326 if (ret < 0) {
1327 close(fd);
1328 return -1;
1329 }
1330
1331 return fd;
1332 }
1333
1334
1335 static EGLBoolean
1336 dri2_wl_swrast_allocate_buffer(struct dri2_egl_display *dri2_dpy,
1337 int format, int w, int h,
1338 void **data, int *size,
1339 struct wl_buffer **buffer)
1340 {
1341 struct wl_shm_pool *pool;
1342 int fd, stride, size_map;
1343 void *data_map;
1344
1345 stride = dri2_wl_swrast_get_stride_for_format(format, w);
1346 size_map = h * stride;
1347
1348 /* Create a sharable buffer */
1349 fd = os_create_anonymous_file(size_map);
1350 if (fd < 0)
1351 return EGL_FALSE;
1352
1353 data_map = mmap(NULL, size_map, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
1354 if (data_map == MAP_FAILED) {
1355 close(fd);
1356 return EGL_FALSE;
1357 }
1358
1359 /* Share it in a wl_buffer */
1360 pool = wl_shm_create_pool(dri2_dpy->wl_shm, fd, size_map);
1361 *buffer = wl_shm_pool_create_buffer(pool, 0, w, h, stride, format);
1362 wl_shm_pool_destroy(pool);
1363 close(fd);
1364
1365 *data = data_map;
1366 *size = size_map;
1367 return EGL_TRUE;
1368 }
1369
1370 static int
1371 swrast_update_buffers(struct dri2_egl_surface *dri2_surf)
1372 {
1373 struct dri2_egl_display *dri2_dpy =
1374 dri2_egl_display(dri2_surf->base.Resource.Display);
1375 int i;
1376
1377 /* we need to do the following operations only once per frame */
1378 if (dri2_surf->back)
1379 return 0;
1380
1381 if (dri2_surf->base.Width != dri2_surf->wl_win->width ||
1382 dri2_surf->base.Height != dri2_surf->wl_win->height) {
1383
1384 dri2_wl_release_buffers(dri2_surf);
1385
1386 dri2_surf->base.Width = dri2_surf->wl_win->width;
1387 dri2_surf->base.Height = dri2_surf->wl_win->height;
1388 dri2_surf->dx = dri2_surf->wl_win->dx;
1389 dri2_surf->dy = dri2_surf->wl_win->dy;
1390 dri2_surf->current = NULL;
1391 }
1392
1393 /* find back buffer */
1394
1395 /* We always want to throttle to some event (either a frame callback or
1396 * a sync request) after the commit so that we can be sure the
1397 * compositor has had a chance to handle it and send us a release event
1398 * before we look for a free buffer */
1399 while (dri2_surf->throttle_callback != NULL)
1400 if (wl_display_dispatch_queue(dri2_dpy->wl_dpy,
1401 dri2_dpy->wl_queue) == -1)
1402 return -1;
1403
1404 /* try get free buffer already created */
1405 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
1406 if (!dri2_surf->color_buffers[i].locked &&
1407 dri2_surf->color_buffers[i].wl_buffer) {
1408 dri2_surf->back = &dri2_surf->color_buffers[i];
1409 break;
1410 }
1411 }
1412
1413 /* else choose any another free location */
1414 if (!dri2_surf->back) {
1415 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
1416 if (!dri2_surf->color_buffers[i].locked) {
1417 dri2_surf->back = &dri2_surf->color_buffers[i];
1418 if (!dri2_wl_swrast_allocate_buffer(dri2_dpy,
1419 dri2_surf->format,
1420 dri2_surf->base.Width,
1421 dri2_surf->base.Height,
1422 &dri2_surf->back->data,
1423 &dri2_surf->back->data_size,
1424 &dri2_surf->back->wl_buffer)) {
1425 _eglError(EGL_BAD_ALLOC, "failed to allocate color buffer");
1426 return -1;
1427 }
1428 wl_proxy_set_queue((struct wl_proxy *) dri2_surf->back->wl_buffer,
1429 dri2_dpy->wl_queue);
1430 wl_buffer_add_listener(dri2_surf->back->wl_buffer,
1431 &wl_buffer_listener, dri2_surf);
1432 break;
1433 }
1434 }
1435 }
1436
1437 if (!dri2_surf->back) {
1438 _eglError(EGL_BAD_ALLOC, "failed to find free buffer");
1439 return -1;
1440 }
1441
1442 dri2_surf->back->locked = 1;
1443
1444 /* If we have an extra unlocked buffer at this point, we had to do triple
1445 * buffering for a while, but now can go back to just double buffering.
1446 * That means we can free any unlocked buffer now. */
1447 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
1448 if (!dri2_surf->color_buffers[i].locked &&
1449 dri2_surf->color_buffers[i].wl_buffer) {
1450 wl_buffer_destroy(dri2_surf->color_buffers[i].wl_buffer);
1451 munmap(dri2_surf->color_buffers[i].data,
1452 dri2_surf->color_buffers[i].data_size);
1453 dri2_surf->color_buffers[i].wl_buffer = NULL;
1454 dri2_surf->color_buffers[i].data = NULL;
1455 }
1456 }
1457
1458 return 0;
1459 }
1460
1461 static void*
1462 dri2_wl_swrast_get_frontbuffer_data(struct dri2_egl_surface *dri2_surf)
1463 {
1464 /* if there has been a resize: */
1465 if (!dri2_surf->current)
1466 return NULL;
1467
1468 return dri2_surf->current->data;
1469 }
1470
1471 static void*
1472 dri2_wl_swrast_get_backbuffer_data(struct dri2_egl_surface *dri2_surf)
1473 {
1474 assert(dri2_surf->back);
1475 return dri2_surf->back->data;
1476 }
1477
1478 static void
1479 dri2_wl_swrast_commit_backbuffer(struct dri2_egl_surface *dri2_surf)
1480 {
1481 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dri2_surf->base.Resource.Display);
1482
1483 if (dri2_surf->base.SwapInterval > 0) {
1484 dri2_surf->throttle_callback =
1485 wl_surface_frame(dri2_surf->wl_win->surface);
1486 wl_callback_add_listener(dri2_surf->throttle_callback,
1487 &throttle_listener, dri2_surf);
1488 wl_proxy_set_queue((struct wl_proxy *) dri2_surf->throttle_callback,
1489 dri2_dpy->wl_queue);
1490 }
1491
1492 dri2_surf->current = dri2_surf->back;
1493 dri2_surf->back = NULL;
1494
1495 wl_surface_attach(dri2_surf->wl_win->surface,
1496 dri2_surf->current->wl_buffer,
1497 dri2_surf->dx, dri2_surf->dy);
1498
1499 dri2_surf->wl_win->attached_width = dri2_surf->base.Width;
1500 dri2_surf->wl_win->attached_height = dri2_surf->base.Height;
1501 /* reset resize growing parameters */
1502 dri2_surf->dx = 0;
1503 dri2_surf->dy = 0;
1504
1505 wl_surface_damage(dri2_surf->wl_win->surface,
1506 0, 0, INT32_MAX, INT32_MAX);
1507 wl_surface_commit(dri2_surf->wl_win->surface);
1508
1509 /* If we're not waiting for a frame callback then we'll at least throttle
1510 * to a sync callback so that we always give a chance for the compositor to
1511 * handle the commit and send a release event before checking for a free
1512 * buffer */
1513 if (dri2_surf->throttle_callback == NULL) {
1514 dri2_surf->throttle_callback = wl_display_sync(dri2_dpy->wl_dpy);
1515 wl_callback_add_listener(dri2_surf->throttle_callback,
1516 &throttle_listener, dri2_surf);
1517 wl_proxy_set_queue((struct wl_proxy *) dri2_surf->throttle_callback,
1518 dri2_dpy->wl_queue);
1519 }
1520
1521 wl_display_flush(dri2_dpy->wl_dpy);
1522 }
1523
1524 static void
1525 dri2_wl_swrast_get_drawable_info(__DRIdrawable * draw,
1526 int *x, int *y, int *w, int *h,
1527 void *loaderPrivate)
1528 {
1529 struct dri2_egl_surface *dri2_surf = loaderPrivate;
1530
1531 (void) swrast_update_buffers(dri2_surf);
1532 *x = 0;
1533 *y = 0;
1534 *w = dri2_surf->base.Width;
1535 *h = dri2_surf->base.Height;
1536 }
1537
1538 static void
1539 dri2_wl_swrast_get_image(__DRIdrawable * read,
1540 int x, int y, int w, int h,
1541 char *data, void *loaderPrivate)
1542 {
1543 struct dri2_egl_surface *dri2_surf = loaderPrivate;
1544 int copy_width = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, w);
1545 int x_offset = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, x);
1546 int src_stride = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, dri2_surf->base.Width);
1547 int dst_stride = copy_width;
1548 char *src, *dst;
1549
1550 src = dri2_wl_swrast_get_frontbuffer_data(dri2_surf);
1551 if (!src) {
1552 memset(data, 0, copy_width * h);
1553 return;
1554 }
1555
1556 assert(data != src);
1557 assert(copy_width <= src_stride);
1558
1559 src += x_offset;
1560 src += y * src_stride;
1561 dst = data;
1562
1563 if (copy_width > src_stride-x_offset)
1564 copy_width = src_stride-x_offset;
1565 if (h > dri2_surf->base.Height-y)
1566 h = dri2_surf->base.Height-y;
1567
1568 for (; h>0; h--) {
1569 memcpy(dst, src, copy_width);
1570 src += src_stride;
1571 dst += dst_stride;
1572 }
1573 }
1574
1575 static void
1576 dri2_wl_swrast_put_image2(__DRIdrawable * draw, int op,
1577 int x, int y, int w, int h, int stride,
1578 char *data, void *loaderPrivate)
1579 {
1580 struct dri2_egl_surface *dri2_surf = loaderPrivate;
1581 int copy_width = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, w);
1582 int dst_stride = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, dri2_surf->base.Width);
1583 int x_offset = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, x);
1584 char *src, *dst;
1585
1586 assert(copy_width <= stride);
1587
1588 (void) swrast_update_buffers(dri2_surf);
1589 dst = dri2_wl_swrast_get_backbuffer_data(dri2_surf);
1590
1591 /* partial copy, copy old content */
1592 if (copy_width < dst_stride)
1593 dri2_wl_swrast_get_image(draw, 0, 0,
1594 dri2_surf->base.Width, dri2_surf->base.Height,
1595 dst, loaderPrivate);
1596
1597 dst += x_offset;
1598 dst += y * dst_stride;
1599
1600 src = data;
1601
1602 /* drivers expect we do these checks (and some rely on it) */
1603 if (copy_width > dst_stride-x_offset)
1604 copy_width = dst_stride-x_offset;
1605 if (h > dri2_surf->base.Height-y)
1606 h = dri2_surf->base.Height-y;
1607
1608 for (; h>0; h--) {
1609 memcpy(dst, src, copy_width);
1610 src += stride;
1611 dst += dst_stride;
1612 }
1613 dri2_wl_swrast_commit_backbuffer(dri2_surf);
1614 }
1615
1616 static void
1617 dri2_wl_swrast_put_image(__DRIdrawable * draw, int op,
1618 int x, int y, int w, int h,
1619 char *data, void *loaderPrivate)
1620 {
1621 struct dri2_egl_surface *dri2_surf = loaderPrivate;
1622 int stride;
1623
1624 stride = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, w);
1625 dri2_wl_swrast_put_image2(draw, op, x, y, w, h,
1626 stride, data, loaderPrivate);
1627 }
1628
1629 /**
1630 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
1631 */
1632 static _EGLSurface *
1633 dri2_wl_swrast_create_window_surface(_EGLDriver *drv, _EGLDisplay *disp,
1634 _EGLConfig *conf, void *native_window,
1635 const EGLint *attrib_list)
1636 {
1637 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1638 struct dri2_egl_config *dri2_conf = dri2_egl_config(conf);
1639 struct wl_egl_window *window = native_window;
1640 struct dri2_egl_surface *dri2_surf;
1641 const __DRIconfig *config;
1642
1643 (void) drv;
1644
1645 dri2_surf = calloc(1, sizeof *dri2_surf);
1646 if (!dri2_surf) {
1647 _eglError(EGL_BAD_ALLOC, "dri2_create_surface");
1648 return NULL;
1649 }
1650
1651 if (!_eglInitSurface(&dri2_surf->base, disp, EGL_WINDOW_BIT, conf, attrib_list))
1652 goto cleanup_surf;
1653
1654 if (conf->RedSize == 5)
1655 dri2_surf->format = WL_SHM_FORMAT_RGB565;
1656 else if (conf->AlphaSize == 0)
1657 dri2_surf->format = WL_SHM_FORMAT_XRGB8888;
1658 else
1659 dri2_surf->format = WL_SHM_FORMAT_ARGB8888;
1660
1661 dri2_surf->wl_win = window;
1662
1663 dri2_surf->base.Width = -1;
1664 dri2_surf->base.Height = -1;
1665
1666 config = dri2_get_dri_config(dri2_conf, EGL_WINDOW_BIT,
1667 dri2_surf->base.GLColorspace);
1668
1669 dri2_surf->dri_drawable =
1670 (*dri2_dpy->swrast->createNewDrawable)(dri2_dpy->dri_screen,
1671 config, dri2_surf);
1672 if (dri2_surf->dri_drawable == NULL) {
1673 _eglError(EGL_BAD_ALLOC, "swrast->createNewDrawable");
1674 goto cleanup_dri_drawable;
1675 }
1676
1677 dri2_wl_swap_interval(drv, disp, &dri2_surf->base,
1678 dri2_dpy->default_swap_interval);
1679
1680 return &dri2_surf->base;
1681
1682 cleanup_dri_drawable:
1683 dri2_dpy->core->destroyDrawable(dri2_surf->dri_drawable);
1684 cleanup_surf:
1685 free(dri2_surf);
1686
1687 return NULL;
1688 }
1689
1690 static EGLBoolean
1691 dri2_wl_swrast_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
1692 {
1693 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1694 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
1695
1696 dri2_dpy->core->swapBuffers(dri2_surf->dri_drawable);
1697 return EGL_TRUE;
1698 }
1699
1700 static void
1701 shm_handle_format(void *data, struct wl_shm *shm, uint32_t format)
1702 {
1703 struct dri2_egl_display *dri2_dpy = data;
1704
1705 switch (format) {
1706 case WL_SHM_FORMAT_ARGB8888:
1707 dri2_dpy->formats |= HAS_ARGB8888;
1708 break;
1709 case WL_SHM_FORMAT_XRGB8888:
1710 dri2_dpy->formats |= HAS_XRGB8888;
1711 break;
1712 case WL_SHM_FORMAT_RGB565:
1713 dri2_dpy->formats |= HAS_RGB565;
1714 break;
1715 }
1716 }
1717
1718 static const struct wl_shm_listener shm_listener = {
1719 .format = shm_handle_format
1720 };
1721
1722 static void
1723 registry_handle_global_swrast(void *data, struct wl_registry *registry, uint32_t name,
1724 const char *interface, uint32_t version)
1725 {
1726 struct dri2_egl_display *dri2_dpy = data;
1727
1728 if (strcmp(interface, "wl_shm") == 0) {
1729 dri2_dpy->wl_shm =
1730 wl_registry_bind(registry, name, &wl_shm_interface, 1);
1731 wl_shm_add_listener(dri2_dpy->wl_shm, &shm_listener, dri2_dpy);
1732 }
1733 }
1734
1735 static const struct wl_registry_listener registry_listener_swrast = {
1736 .global = registry_handle_global_swrast,
1737 .global_remove = registry_handle_global_remove
1738 };
1739
1740 static struct dri2_egl_display_vtbl dri2_wl_swrast_display_vtbl = {
1741 .authenticate = NULL,
1742 .create_window_surface = dri2_wl_swrast_create_window_surface,
1743 .create_pixmap_surface = dri2_wl_create_pixmap_surface,
1744 .create_pbuffer_surface = dri2_fallback_create_pbuffer_surface,
1745 .destroy_surface = dri2_wl_destroy_surface,
1746 .create_image = dri2_fallback_create_image_khr,
1747 .swap_interval = dri2_wl_swap_interval,
1748 .swap_buffers = dri2_wl_swrast_swap_buffers,
1749 .swap_buffers_with_damage = dri2_fallback_swap_buffers_with_damage,
1750 .swap_buffers_region = dri2_fallback_swap_buffers_region,
1751 .post_sub_buffer = dri2_fallback_post_sub_buffer,
1752 .copy_buffers = dri2_fallback_copy_buffers,
1753 .query_buffer_age = dri2_fallback_query_buffer_age,
1754 .create_wayland_buffer_from_image = dri2_fallback_create_wayland_buffer_from_image,
1755 .get_sync_values = dri2_fallback_get_sync_values,
1756 .get_dri_drawable = dri2_surface_get_dri_drawable,
1757 };
1758
1759 static EGLBoolean
1760 dri2_initialize_wayland_swrast(_EGLDriver *drv, _EGLDisplay *disp)
1761 {
1762 struct dri2_egl_display *dri2_dpy;
1763 const __DRIconfig *config;
1764 uint32_t types;
1765 int i;
1766 static const unsigned int argb_masks[4] =
1767 { 0xff0000, 0xff00, 0xff, 0xff000000 };
1768 static const unsigned int rgb_masks[4] = { 0xff0000, 0xff00, 0xff, 0 };
1769 static const unsigned int rgb565_masks[4] = { 0xf800, 0x07e0, 0x001f, 0 };
1770
1771 loader_set_logger(_eglLog);
1772
1773 dri2_dpy = calloc(1, sizeof *dri2_dpy);
1774 if (!dri2_dpy)
1775 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
1776
1777 disp->DriverData = (void *) dri2_dpy;
1778 if (disp->PlatformDisplay == NULL) {
1779 dri2_dpy->wl_dpy = wl_display_connect(NULL);
1780 if (dri2_dpy->wl_dpy == NULL)
1781 goto cleanup_dpy;
1782 dri2_dpy->own_device = 1;
1783 } else {
1784 dri2_dpy->wl_dpy = disp->PlatformDisplay;
1785 }
1786
1787 dri2_dpy->wl_queue = wl_display_create_queue(dri2_dpy->wl_dpy);
1788
1789 if (dri2_dpy->own_device)
1790 wl_display_dispatch_pending(dri2_dpy->wl_dpy);
1791
1792 dri2_dpy->wl_registry = wl_display_get_registry(dri2_dpy->wl_dpy);
1793 wl_proxy_set_queue((struct wl_proxy *) dri2_dpy->wl_registry,
1794 dri2_dpy->wl_queue);
1795 wl_registry_add_listener(dri2_dpy->wl_registry,
1796 &registry_listener_swrast, dri2_dpy);
1797
1798 if (roundtrip(dri2_dpy) < 0 || dri2_dpy->wl_shm == NULL)
1799 goto cleanup_registry;
1800
1801 if (roundtrip(dri2_dpy) < 0 || dri2_dpy->formats == 0)
1802 goto cleanup_shm;
1803
1804 dri2_dpy->fd = -1;
1805 dri2_dpy->driver_name = strdup("swrast");
1806 if (!dri2_load_driver_swrast(disp))
1807 goto cleanup_shm;
1808
1809 dri2_dpy->swrast_loader_extension.base.name = __DRI_SWRAST_LOADER;
1810 dri2_dpy->swrast_loader_extension.base.version = 2;
1811 dri2_dpy->swrast_loader_extension.getDrawableInfo = dri2_wl_swrast_get_drawable_info;
1812 dri2_dpy->swrast_loader_extension.putImage = dri2_wl_swrast_put_image;
1813 dri2_dpy->swrast_loader_extension.getImage = dri2_wl_swrast_get_image;
1814 dri2_dpy->swrast_loader_extension.putImage2 = dri2_wl_swrast_put_image2;
1815
1816 dri2_dpy->extensions[0] = &dri2_dpy->swrast_loader_extension.base;
1817 dri2_dpy->extensions[1] = NULL;
1818
1819 if (!dri2_create_screen(disp))
1820 goto cleanup_driver;
1821
1822 dri2_wl_setup_swap_interval(dri2_dpy);
1823
1824 types = EGL_WINDOW_BIT;
1825 for (i = 0; dri2_dpy->driver_configs[i]; i++) {
1826 config = dri2_dpy->driver_configs[i];
1827 if (dri2_dpy->formats & HAS_XRGB8888)
1828 dri2_add_config(disp, config, i + 1, types, NULL, rgb_masks);
1829 if (dri2_dpy->formats & HAS_ARGB8888)
1830 dri2_add_config(disp, config, i + 1, types, NULL, argb_masks);
1831 if (dri2_dpy->formats & HAS_RGB565)
1832 dri2_add_config(disp, config, i + 1, types, NULL, rgb565_masks);
1833 }
1834
1835 /* Fill vtbl last to prevent accidentally calling virtual function during
1836 * initialization.
1837 */
1838 dri2_dpy->vtbl = &dri2_wl_swrast_display_vtbl;
1839
1840 return EGL_TRUE;
1841
1842 cleanup_driver:
1843 dlclose(dri2_dpy->driver);
1844 cleanup_shm:
1845 wl_shm_destroy(dri2_dpy->wl_shm);
1846 cleanup_registry:
1847 wl_registry_destroy(dri2_dpy->wl_registry);
1848 wl_event_queue_destroy(dri2_dpy->wl_queue);
1849 cleanup_dpy:
1850 free(dri2_dpy);
1851
1852 return EGL_FALSE;
1853 }
1854
1855 EGLBoolean
1856 dri2_initialize_wayland(_EGLDriver *drv, _EGLDisplay *disp)
1857 {
1858 EGLBoolean initialized = EGL_TRUE;
1859
1860 int hw_accel = (getenv("LIBGL_ALWAYS_SOFTWARE") == NULL);
1861
1862 if (hw_accel) {
1863 if (!dri2_initialize_wayland_drm(drv, disp)) {
1864 initialized = dri2_initialize_wayland_swrast(drv, disp);
1865 }
1866 } else {
1867 initialized = dri2_initialize_wayland_swrast(drv, disp);
1868 }
1869
1870 return initialized;
1871
1872 }