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