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