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