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