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