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