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