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