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