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