egl: Inform the client API when ancillary buffers may become undefined.
[mesa.git] / src / egl / drivers / dri2 / platform_wayland.c
1 /*
2 * Copyright © 2011-2012 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Kristian Høgsberg <krh@bitplanet.net>
26 * Benjamin Franzke <benjaminfranzke@googlemail.com>
27 */
28
29 #include <stdlib.h>
30 #include <string.h>
31 #include <limits.h>
32 #include <dlfcn.h>
33 #include <errno.h>
34 #include <unistd.h>
35 #include <fcntl.h>
36 #include <xf86drm.h>
37
38 #include "egl_dri2.h"
39 #include "egl_dri2_fallbacks.h"
40 #include "loader.h"
41
42 #include <wayland-client.h>
43 #include "wayland-drm-client-protocol.h"
44
45 enum wl_drm_format_flags {
46 HAS_ARGB8888 = 1,
47 HAS_XRGB8888 = 2,
48 HAS_RGB565 = 4,
49 };
50
51 static EGLBoolean
52 dri2_wl_swap_interval(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf,
53 EGLint interval);
54
55 static void
56 sync_callback(void *data, struct wl_callback *callback, uint32_t serial)
57 {
58 int *done = data;
59
60 *done = 1;
61 wl_callback_destroy(callback);
62 }
63
64 static const struct wl_callback_listener sync_listener = {
65 sync_callback
66 };
67
68 static int
69 roundtrip(struct dri2_egl_display *dri2_dpy)
70 {
71 struct wl_callback *callback;
72 int done = 0, ret = 0;
73
74 callback = wl_display_sync(dri2_dpy->wl_dpy);
75 wl_callback_add_listener(callback, &sync_listener, &done);
76 wl_proxy_set_queue((struct wl_proxy *) callback, dri2_dpy->wl_queue);
77 while (ret != -1 && !done)
78 ret = wl_display_dispatch_queue(dri2_dpy->wl_dpy, dri2_dpy->wl_queue);
79
80 if (!done)
81 wl_callback_destroy(callback);
82
83 return ret;
84 }
85
86 static void
87 wl_buffer_release(void *data, struct wl_buffer *buffer)
88 {
89 struct dri2_egl_surface *dri2_surf = data;
90 int i;
91
92 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); ++i)
93 if (dri2_surf->color_buffers[i].wl_buffer == buffer)
94 break;
95
96 if (i == ARRAY_SIZE(dri2_surf->color_buffers)) {
97 wl_buffer_destroy(buffer);
98 return;
99 }
100
101 dri2_surf->color_buffers[i].locked = 0;
102 }
103
104 static struct wl_buffer_listener wl_buffer_listener = {
105 wl_buffer_release
106 };
107
108 static void
109 resize_callback(struct wl_egl_window *wl_win, void *data)
110 {
111 struct dri2_egl_surface *dri2_surf = data;
112 struct dri2_egl_display *dri2_dpy =
113 dri2_egl_display(dri2_surf->base.Resource.Display);
114
115 (*dri2_dpy->flush->invalidate)(dri2_surf->dri_drawable);
116 }
117
118 /**
119 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
120 */
121 static _EGLSurface *
122 dri2_wl_create_surface(_EGLDriver *drv, _EGLDisplay *disp, EGLint type,
123 _EGLConfig *conf, void *native_window,
124 const EGLint *attrib_list)
125 {
126 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
127 struct dri2_egl_config *dri2_conf = dri2_egl_config(conf);
128 struct wl_egl_window *window = native_window;
129 struct dri2_egl_surface *dri2_surf;
130
131 (void) drv;
132
133 dri2_surf = calloc(1, sizeof *dri2_surf);
134 if (!dri2_surf) {
135 _eglError(EGL_BAD_ALLOC, "dri2_create_surface");
136 return NULL;
137 }
138
139 if (!_eglInitSurface(&dri2_surf->base, disp, type, conf, attrib_list))
140 goto cleanup_surf;
141
142 if (conf->RedSize == 5)
143 dri2_surf->format = WL_DRM_FORMAT_RGB565;
144 else if (conf->AlphaSize == 0)
145 dri2_surf->format = WL_DRM_FORMAT_XRGB8888;
146 else
147 dri2_surf->format = WL_DRM_FORMAT_ARGB8888;
148
149 switch (type) {
150 case EGL_WINDOW_BIT:
151 dri2_surf->wl_win = window;
152
153 dri2_surf->wl_win->private = dri2_surf;
154 dri2_surf->wl_win->resize_callback = resize_callback;
155
156 dri2_surf->base.Width = -1;
157 dri2_surf->base.Height = -1;
158 break;
159 default:
160 goto cleanup_surf;
161 }
162
163 dri2_surf->dri_drawable =
164 (*dri2_dpy->dri2->createNewDrawable) (dri2_dpy->dri_screen,
165 type == EGL_WINDOW_BIT ?
166 dri2_conf->dri_double_config :
167 dri2_conf->dri_single_config,
168 dri2_surf);
169 if (dri2_surf->dri_drawable == NULL) {
170 _eglError(EGL_BAD_ALLOC, "dri2->createNewDrawable");
171 goto cleanup_dri_drawable;
172 }
173
174 return &dri2_surf->base;
175
176 cleanup_dri_drawable:
177 dri2_dpy->core->destroyDrawable(dri2_surf->dri_drawable);
178 cleanup_surf:
179 free(dri2_surf);
180
181 return NULL;
182 }
183
184 /**
185 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
186 */
187 static _EGLSurface *
188 dri2_wl_create_window_surface(_EGLDriver *drv, _EGLDisplay *disp,
189 _EGLConfig *conf, void *native_window,
190 const EGLint *attrib_list)
191 {
192 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
193 _EGLSurface *surf;
194
195 surf = dri2_wl_create_surface(drv, disp, EGL_WINDOW_BIT, conf,
196 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 }
243
244 for (i = 0; i < __DRI_BUFFER_COUNT; i++)
245 if (dri2_surf->dri_buffers[i] &&
246 dri2_surf->dri_buffers[i]->attachment != __DRI_BUFFER_BACK_LEFT)
247 dri2_dpy->dri2->releaseBuffer(dri2_dpy->dri_screen,
248 dri2_surf->dri_buffers[i]);
249
250 if (dri2_surf->throttle_callback)
251 wl_callback_destroy(dri2_surf->throttle_callback);
252
253 if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
254 dri2_surf->wl_win->private = NULL;
255 dri2_surf->wl_win->resize_callback = NULL;
256 }
257
258 free(surf);
259
260 return EGL_TRUE;
261 }
262
263 static void
264 dri2_wl_release_buffers(struct dri2_egl_surface *dri2_surf)
265 {
266 struct dri2_egl_display *dri2_dpy =
267 dri2_egl_display(dri2_surf->base.Resource.Display);
268 int i;
269
270 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
271 if (dri2_surf->color_buffers[i].wl_buffer &&
272 !dri2_surf->color_buffers[i].locked)
273 wl_buffer_destroy(dri2_surf->color_buffers[i].wl_buffer);
274 if (dri2_surf->color_buffers[i].dri_image)
275 dri2_dpy->image->destroyImage(dri2_surf->color_buffers[i].dri_image);
276
277 dri2_surf->color_buffers[i].wl_buffer = NULL;
278 dri2_surf->color_buffers[i].dri_image = NULL;
279 dri2_surf->color_buffers[i].locked = 0;
280 }
281
282 for (i = 0; i < __DRI_BUFFER_COUNT; i++)
283 if (dri2_surf->dri_buffers[i] &&
284 dri2_surf->dri_buffers[i]->attachment != __DRI_BUFFER_BACK_LEFT)
285 dri2_dpy->dri2->releaseBuffer(dri2_dpy->dri_screen,
286 dri2_surf->dri_buffers[i]);
287 }
288
289 static int
290 get_back_bo(struct dri2_egl_surface *dri2_surf)
291 {
292 struct dri2_egl_display *dri2_dpy =
293 dri2_egl_display(dri2_surf->base.Resource.Display);
294 int i;
295
296 /* We always want to throttle to some event (either a frame callback or
297 * a sync request) after the commit so that we can be sure the
298 * compositor has had a chance to handle it and send us a release event
299 * before we look for a free buffer */
300 while (dri2_surf->throttle_callback != NULL)
301 if (wl_display_dispatch_queue(dri2_dpy->wl_dpy,
302 dri2_dpy->wl_queue) == -1)
303 return -1;
304
305 if (dri2_surf->back == NULL) {
306 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
307 /* Get an unlocked buffer, preferrably one with a dri_buffer
308 * already allocated. */
309 if (dri2_surf->color_buffers[i].locked)
310 continue;
311 if (dri2_surf->back == NULL)
312 dri2_surf->back = &dri2_surf->color_buffers[i];
313 else if (dri2_surf->back->dri_image == NULL)
314 dri2_surf->back = &dri2_surf->color_buffers[i];
315 }
316 }
317
318 if (dri2_surf->back == NULL)
319 return -1;
320 if (dri2_surf->back->dri_image == NULL) {
321 dri2_surf->back->dri_image =
322 dri2_dpy->image->createImage(dri2_dpy->dri_screen,
323 dri2_surf->base.Width,
324 dri2_surf->base.Height,
325 __DRI_IMAGE_FORMAT_ARGB8888,
326 __DRI_IMAGE_USE_SHARE,
327 NULL);
328 dri2_surf->back->age = 0;
329 }
330 if (dri2_surf->back->dri_image == NULL)
331 return -1;
332
333 dri2_surf->back->locked = 1;
334
335 return 0;
336 }
337
338
339 static void
340 back_bo_to_dri_buffer(struct dri2_egl_surface *dri2_surf, __DRIbuffer *buffer)
341 {
342 struct dri2_egl_display *dri2_dpy =
343 dri2_egl_display(dri2_surf->base.Resource.Display);
344 __DRIimage *image;
345 int name, pitch;
346
347 image = dri2_surf->back->dri_image;
348
349 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_NAME, &name);
350 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_STRIDE, &pitch);
351
352 buffer->attachment = __DRI_BUFFER_BACK_LEFT;
353 buffer->name = name;
354 buffer->pitch = pitch;
355 buffer->cpp = 4;
356 buffer->flags = 0;
357 }
358
359 static int
360 get_aux_bo(struct dri2_egl_surface *dri2_surf,
361 unsigned int attachment, unsigned int format, __DRIbuffer *buffer)
362 {
363 struct dri2_egl_display *dri2_dpy =
364 dri2_egl_display(dri2_surf->base.Resource.Display);
365 __DRIbuffer *b = dri2_surf->dri_buffers[attachment];
366
367 if (b == NULL) {
368 b = dri2_dpy->dri2->allocateBuffer(dri2_dpy->dri_screen,
369 attachment, format,
370 dri2_surf->base.Width,
371 dri2_surf->base.Height);
372 dri2_surf->dri_buffers[attachment] = b;
373 }
374 if (b == NULL)
375 return -1;
376
377 memcpy(buffer, b, sizeof *buffer);
378
379 return 0;
380 }
381
382 static int
383 update_buffers(struct dri2_egl_surface *dri2_surf)
384 {
385 struct dri2_egl_display *dri2_dpy =
386 dri2_egl_display(dri2_surf->base.Resource.Display);
387 int i;
388
389 if (dri2_surf->base.Type == EGL_WINDOW_BIT &&
390 (dri2_surf->base.Width != dri2_surf->wl_win->width ||
391 dri2_surf->base.Height != dri2_surf->wl_win->height)) {
392
393 dri2_wl_release_buffers(dri2_surf);
394
395 dri2_surf->base.Width = dri2_surf->wl_win->width;
396 dri2_surf->base.Height = dri2_surf->wl_win->height;
397 dri2_surf->dx = dri2_surf->wl_win->dx;
398 dri2_surf->dy = dri2_surf->wl_win->dy;
399 }
400
401 if (get_back_bo(dri2_surf) < 0) {
402 _eglError(EGL_BAD_ALLOC, "failed to allocate color buffer");
403 return -1;
404 }
405
406 /* If we have an extra unlocked buffer at this point, we had to do triple
407 * buffering for a while, but now can go back to just double buffering.
408 * That means we can free any unlocked buffer now. */
409 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
410 if (!dri2_surf->color_buffers[i].locked &&
411 dri2_surf->color_buffers[i].wl_buffer) {
412 wl_buffer_destroy(dri2_surf->color_buffers[i].wl_buffer);
413 dri2_dpy->image->destroyImage(dri2_surf->color_buffers[i].dri_image);
414 dri2_surf->color_buffers[i].wl_buffer = NULL;
415 dri2_surf->color_buffers[i].dri_image = NULL;
416 }
417 }
418
419 return 0;
420 }
421
422 static __DRIbuffer *
423 dri2_wl_get_buffers_with_format(__DRIdrawable * driDrawable,
424 int *width, int *height,
425 unsigned int *attachments, int count,
426 int *out_count, void *loaderPrivate)
427 {
428 struct dri2_egl_surface *dri2_surf = loaderPrivate;
429 int i, j;
430
431 if (update_buffers(dri2_surf) < 0)
432 return NULL;
433
434 for (i = 0, j = 0; i < 2 * count; i += 2, j++) {
435 switch (attachments[i]) {
436 case __DRI_BUFFER_BACK_LEFT:
437 back_bo_to_dri_buffer(dri2_surf, &dri2_surf->buffers[j]);
438 break;
439 default:
440 if (get_aux_bo(dri2_surf, attachments[i], attachments[i + 1],
441 &dri2_surf->buffers[j]) < 0) {
442 _eglError(EGL_BAD_ALLOC, "failed to allocate aux buffer");
443 return NULL;
444 }
445 break;
446 }
447 }
448
449 *out_count = j;
450 if (j == 0)
451 return NULL;
452
453 *width = dri2_surf->base.Width;
454 *height = dri2_surf->base.Height;
455
456 return dri2_surf->buffers;
457 }
458
459 static __DRIbuffer *
460 dri2_wl_get_buffers(__DRIdrawable * driDrawable,
461 int *width, int *height,
462 unsigned int *attachments, int count,
463 int *out_count, void *loaderPrivate)
464 {
465 unsigned int *attachments_with_format;
466 __DRIbuffer *buffer;
467 const unsigned int format = 32;
468 int i;
469
470 attachments_with_format = calloc(count, 2 * sizeof(unsigned int));
471 if (!attachments_with_format) {
472 *out_count = 0;
473 return NULL;
474 }
475
476 for (i = 0; i < count; ++i) {
477 attachments_with_format[2*i] = attachments[i];
478 attachments_with_format[2*i + 1] = format;
479 }
480
481 buffer =
482 dri2_wl_get_buffers_with_format(driDrawable,
483 width, height,
484 attachments_with_format, count,
485 out_count, loaderPrivate);
486
487 free(attachments_with_format);
488
489 return buffer;
490 }
491
492 static int
493 image_get_buffers(__DRIdrawable *driDrawable,
494 unsigned int format,
495 uint32_t *stamp,
496 void *loaderPrivate,
497 uint32_t buffer_mask,
498 struct __DRIimageList *buffers)
499 {
500 struct dri2_egl_surface *dri2_surf = loaderPrivate;
501
502 if (update_buffers(dri2_surf) < 0)
503 return 0;
504
505 buffers->image_mask = __DRI_IMAGE_BUFFER_BACK;
506 buffers->back = dri2_surf->back->dri_image;
507
508 return 1;
509 }
510
511 static void
512 dri2_wl_flush_front_buffer(__DRIdrawable * driDrawable, void *loaderPrivate)
513 {
514 (void) driDrawable;
515 (void) loaderPrivate;
516 }
517
518 static const __DRIimageLoaderExtension image_loader_extension = {
519 .base = { __DRI_IMAGE_LOADER, 1 },
520
521 .getBuffers = image_get_buffers,
522 .flushFrontBuffer = dri2_wl_flush_front_buffer,
523 };
524
525 static void
526 wayland_throttle_callback(void *data,
527 struct wl_callback *callback,
528 uint32_t time)
529 {
530 struct dri2_egl_surface *dri2_surf = data;
531
532 dri2_surf->throttle_callback = NULL;
533 wl_callback_destroy(callback);
534 }
535
536 static const struct wl_callback_listener throttle_listener = {
537 wayland_throttle_callback
538 };
539
540 static void
541 create_wl_buffer(struct dri2_egl_surface *dri2_surf)
542 {
543 struct dri2_egl_display *dri2_dpy =
544 dri2_egl_display(dri2_surf->base.Resource.Display);
545 int fd, stride, name;
546
547 if (dri2_surf->current->wl_buffer != NULL)
548 return;
549
550 if (dri2_dpy->capabilities & WL_DRM_CAPABILITY_PRIME) {
551 dri2_dpy->image->queryImage(dri2_surf->current->dri_image,
552 __DRI_IMAGE_ATTRIB_FD, &fd);
553 dri2_dpy->image->queryImage(dri2_surf->current->dri_image,
554 __DRI_IMAGE_ATTRIB_STRIDE, &stride);
555
556 dri2_surf->current->wl_buffer =
557 wl_drm_create_prime_buffer(dri2_dpy->wl_drm,
558 fd,
559 dri2_surf->base.Width,
560 dri2_surf->base.Height,
561 dri2_surf->format,
562 0, stride,
563 0, 0,
564 0, 0);
565 close(fd);
566 } else {
567 dri2_dpy->image->queryImage(dri2_surf->current->dri_image,
568 __DRI_IMAGE_ATTRIB_NAME, &name);
569 dri2_dpy->image->queryImage(dri2_surf->current->dri_image,
570 __DRI_IMAGE_ATTRIB_STRIDE, &stride);
571
572 dri2_surf->current->wl_buffer =
573 wl_drm_create_buffer(dri2_dpy->wl_drm,
574 name,
575 dri2_surf->base.Width,
576 dri2_surf->base.Height,
577 stride,
578 dri2_surf->format);
579 }
580
581 wl_proxy_set_queue((struct wl_proxy *) dri2_surf->current->wl_buffer,
582 dri2_dpy->wl_queue);
583 wl_buffer_add_listener(dri2_surf->current->wl_buffer,
584 &wl_buffer_listener, dri2_surf);
585 }
586
587 /**
588 * Called via eglSwapBuffers(), drv->API.SwapBuffers().
589 */
590 static EGLBoolean
591 dri2_wl_swap_buffers_with_damage(_EGLDriver *drv,
592 _EGLDisplay *disp,
593 _EGLSurface *draw,
594 const EGLint *rects,
595 EGLint n_rects)
596 {
597 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
598 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
599 struct dri2_egl_context *dri2_ctx;
600 _EGLContext *ctx;
601 int i;
602
603 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++)
604 if (dri2_surf->color_buffers[i].age > 0)
605 dri2_surf->color_buffers[i].age++;
606
607 /* Make sure we have a back buffer in case we're swapping without ever
608 * rendering. */
609 if (get_back_bo(dri2_surf) < 0) {
610 _eglError(EGL_BAD_ALLOC, "dri2_swap_buffers");
611 return EGL_FALSE;
612 }
613
614 if (draw->SwapInterval > 0) {
615 dri2_surf->throttle_callback =
616 wl_surface_frame(dri2_surf->wl_win->surface);
617 wl_callback_add_listener(dri2_surf->throttle_callback,
618 &throttle_listener, dri2_surf);
619 wl_proxy_set_queue((struct wl_proxy *) dri2_surf->throttle_callback,
620 dri2_dpy->wl_queue);
621 }
622
623 dri2_surf->back->age = 1;
624 dri2_surf->current = dri2_surf->back;
625 dri2_surf->back = NULL;
626
627 create_wl_buffer(dri2_surf);
628
629 wl_surface_attach(dri2_surf->wl_win->surface,
630 dri2_surf->current->wl_buffer,
631 dri2_surf->dx, dri2_surf->dy);
632
633 dri2_surf->wl_win->attached_width = dri2_surf->base.Width;
634 dri2_surf->wl_win->attached_height = dri2_surf->base.Height;
635 /* reset resize growing parameters */
636 dri2_surf->dx = 0;
637 dri2_surf->dy = 0;
638
639 if (n_rects == 0) {
640 wl_surface_damage(dri2_surf->wl_win->surface,
641 0, 0, INT32_MAX, INT32_MAX);
642 } else {
643 for (i = 0; i < n_rects; i++) {
644 const int *rect = &rects[i * 4];
645 wl_surface_damage(dri2_surf->wl_win->surface,
646 rect[0],
647 dri2_surf->base.Height - rect[1] - rect[3],
648 rect[2], rect[3]);
649 }
650 }
651
652 dri2_flush_drawable_for_swapbuffers(disp, draw);
653 (*dri2_dpy->flush->invalidate)(dri2_surf->dri_drawable);
654
655 wl_surface_commit(dri2_surf->wl_win->surface);
656
657 /* If we're not waiting for a frame callback then we'll at least throttle
658 * to a sync callback so that we always give a chance for the compositor to
659 * handle the commit and send a release event before checking for a free
660 * buffer */
661 if (dri2_surf->throttle_callback == NULL) {
662 dri2_surf->throttle_callback = wl_display_sync(dri2_dpy->wl_dpy);
663 wl_callback_add_listener(dri2_surf->throttle_callback,
664 &throttle_listener, dri2_surf);
665 wl_proxy_set_queue((struct wl_proxy *) dri2_surf->throttle_callback,
666 dri2_dpy->wl_queue);
667 }
668
669 wl_display_flush(dri2_dpy->wl_dpy);
670
671 return EGL_TRUE;
672 }
673
674 static EGLint
675 dri2_wl_query_buffer_age(_EGLDriver *drv,
676 _EGLDisplay *disp, _EGLSurface *surface)
677 {
678 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surface);
679
680 if (get_back_bo(dri2_surf) < 0) {
681 _eglError(EGL_BAD_ALLOC, "dri2_query_buffer_age");
682 return 0;
683 }
684
685 return dri2_surf->back->age;
686 }
687
688 static EGLBoolean
689 dri2_wl_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
690 {
691 return dri2_wl_swap_buffers_with_damage (drv, disp, draw, NULL, 0);
692 }
693
694 static struct wl_buffer *
695 dri2_wl_create_wayland_buffer_from_image(_EGLDriver *drv,
696 _EGLDisplay *disp,
697 _EGLImage *img)
698 {
699 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
700 struct dri2_egl_image *dri2_img = dri2_egl_image(img);
701 __DRIimage *image = dri2_img->dri_image;
702 struct wl_buffer *buffer;
703 int width, height, format, pitch;
704 enum wl_drm_format wl_format;
705
706 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_FORMAT, &format);
707
708 switch (format) {
709 case __DRI_IMAGE_FORMAT_ARGB8888:
710 if (!(dri2_dpy->formats & HAS_ARGB8888))
711 goto bad_format;
712 wl_format = WL_DRM_FORMAT_ARGB8888;
713 break;
714 case __DRI_IMAGE_FORMAT_XRGB8888:
715 if (!(dri2_dpy->formats & HAS_XRGB8888))
716 goto bad_format;
717 wl_format = WL_DRM_FORMAT_XRGB8888;
718 break;
719 default:
720 goto bad_format;
721 }
722
723 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_WIDTH, &width);
724 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_HEIGHT, &height);
725 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_STRIDE, &pitch);
726
727 if (dri2_dpy->capabilities & WL_DRM_CAPABILITY_PRIME) {
728 int fd;
729
730 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_FD, &fd);
731
732 buffer =
733 wl_drm_create_prime_buffer(dri2_dpy->wl_drm,
734 fd,
735 width, height,
736 wl_format,
737 0, pitch,
738 0, 0,
739 0, 0);
740
741 close(fd);
742 } else {
743 int name;
744
745 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_NAME, &name);
746
747 buffer =
748 wl_drm_create_buffer(dri2_dpy->wl_drm,
749 name,
750 width, height,
751 pitch,
752 wl_format);
753 }
754
755 /* The buffer object will have been created with our internal event queue
756 * because it is using the wl_drm object as a proxy factory. We want the
757 * buffer to be used by the application so we'll reset it to the display's
758 * default event queue */
759 if (buffer)
760 wl_proxy_set_queue((struct wl_proxy *) buffer, NULL);
761
762 return buffer;
763
764 bad_format:
765 _eglError(EGL_BAD_MATCH, "unsupported image format");
766 return NULL;
767 }
768
769 static int
770 dri2_wl_authenticate(_EGLDisplay *disp, uint32_t id)
771 {
772 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
773 int ret = 0;
774
775 dri2_dpy->authenticated = 0;
776
777 wl_drm_authenticate(dri2_dpy->wl_drm, id);
778 if (roundtrip(dri2_dpy) < 0)
779 ret = -1;
780
781 if (!dri2_dpy->authenticated)
782 ret = -1;
783
784 /* reset authenticated */
785 dri2_dpy->authenticated = 1;
786
787 return ret;
788 }
789
790 static void
791 drm_handle_device(void *data, struct wl_drm *drm, const char *device)
792 {
793 struct dri2_egl_display *dri2_dpy = data;
794 drm_magic_t magic;
795
796 dri2_dpy->device_name = strdup(device);
797 if (!dri2_dpy->device_name)
798 return;
799
800 #ifdef O_CLOEXEC
801 dri2_dpy->fd = open(dri2_dpy->device_name, O_RDWR | O_CLOEXEC);
802 if (dri2_dpy->fd == -1 && errno == EINVAL)
803 #endif
804 {
805 dri2_dpy->fd = open(dri2_dpy->device_name, O_RDWR);
806 if (dri2_dpy->fd != -1)
807 fcntl(dri2_dpy->fd, F_SETFD, fcntl(dri2_dpy->fd, F_GETFD) |
808 FD_CLOEXEC);
809 }
810 if (dri2_dpy->fd == -1) {
811 _eglLog(_EGL_WARNING, "wayland-egl: could not open %s (%s)",
812 dri2_dpy->device_name, strerror(errno));
813 return;
814 }
815
816 drmGetMagic(dri2_dpy->fd, &magic);
817 wl_drm_authenticate(dri2_dpy->wl_drm, magic);
818 }
819
820 static void
821 drm_handle_format(void *data, struct wl_drm *drm, uint32_t format)
822 {
823 struct dri2_egl_display *dri2_dpy = data;
824
825 switch (format) {
826 case WL_DRM_FORMAT_ARGB8888:
827 dri2_dpy->formats |= HAS_ARGB8888;
828 break;
829 case WL_DRM_FORMAT_XRGB8888:
830 dri2_dpy->formats |= HAS_XRGB8888;
831 break;
832 case WL_DRM_FORMAT_RGB565:
833 dri2_dpy->formats |= HAS_RGB565;
834 break;
835 }
836 }
837
838 static void
839 drm_handle_capabilities(void *data, struct wl_drm *drm, uint32_t value)
840 {
841 struct dri2_egl_display *dri2_dpy = data;
842
843 dri2_dpy->capabilities = value;
844 }
845
846 static void
847 drm_handle_authenticated(void *data, struct wl_drm *drm)
848 {
849 struct dri2_egl_display *dri2_dpy = data;
850
851 dri2_dpy->authenticated = 1;
852 }
853
854 static const struct wl_drm_listener drm_listener = {
855 drm_handle_device,
856 drm_handle_format,
857 drm_handle_authenticated,
858 drm_handle_capabilities
859 };
860
861 static void
862 registry_handle_global(void *data, struct wl_registry *registry, uint32_t name,
863 const char *interface, uint32_t version)
864 {
865 struct dri2_egl_display *dri2_dpy = data;
866
867 if (version > 1)
868 version = 2;
869 if (strcmp(interface, "wl_drm") == 0) {
870 dri2_dpy->wl_drm =
871 wl_registry_bind(registry, name, &wl_drm_interface, version);
872 wl_drm_add_listener(dri2_dpy->wl_drm, &drm_listener, dri2_dpy);
873 }
874 }
875
876 static void
877 registry_handle_global_remove(void *data, struct wl_registry *registry,
878 uint32_t name)
879 {
880 }
881
882 static const struct wl_registry_listener registry_listener = {
883 registry_handle_global,
884 registry_handle_global_remove
885 };
886
887 static EGLBoolean
888 dri2_wl_swap_interval(_EGLDriver *drv,
889 _EGLDisplay *disp,
890 _EGLSurface *surf,
891 EGLint interval)
892 {
893 if (interval > surf->Config->MaxSwapInterval)
894 interval = surf->Config->MaxSwapInterval;
895 else if (interval < surf->Config->MinSwapInterval)
896 interval = surf->Config->MinSwapInterval;
897
898 surf->SwapInterval = interval;
899
900 return EGL_TRUE;
901 }
902
903 static void
904 dri2_wl_setup_swap_interval(struct dri2_egl_display *dri2_dpy)
905 {
906 GLint vblank_mode = DRI_CONF_VBLANK_DEF_INTERVAL_1;
907
908 /* We can't use values greater than 1 on Wayland because we are using the
909 * frame callback to synchronise the frame and the only way we be sure to
910 * get a frame callback is to attach a new buffer. Therefore we can't just
911 * sit drawing nothing to wait until the next ‘n’ frame callbacks */
912
913 if (dri2_dpy->config)
914 dri2_dpy->config->configQueryi(dri2_dpy->dri_screen,
915 "vblank_mode", &vblank_mode);
916 switch (vblank_mode) {
917 case DRI_CONF_VBLANK_NEVER:
918 dri2_dpy->min_swap_interval = 0;
919 dri2_dpy->max_swap_interval = 0;
920 dri2_dpy->default_swap_interval = 0;
921 break;
922 case DRI_CONF_VBLANK_ALWAYS_SYNC:
923 dri2_dpy->min_swap_interval = 1;
924 dri2_dpy->max_swap_interval = 1;
925 dri2_dpy->default_swap_interval = 1;
926 break;
927 case DRI_CONF_VBLANK_DEF_INTERVAL_0:
928 dri2_dpy->min_swap_interval = 0;
929 dri2_dpy->max_swap_interval = 1;
930 dri2_dpy->default_swap_interval = 0;
931 break;
932 default:
933 case DRI_CONF_VBLANK_DEF_INTERVAL_1:
934 dri2_dpy->min_swap_interval = 0;
935 dri2_dpy->max_swap_interval = 1;
936 dri2_dpy->default_swap_interval = 1;
937 break;
938 }
939 }
940
941 static struct dri2_egl_display_vtbl dri2_wl_display_vtbl = {
942 .authenticate = dri2_wl_authenticate,
943 .create_window_surface = dri2_wl_create_window_surface,
944 .create_pixmap_surface = dri2_wl_create_pixmap_surface,
945 .create_pbuffer_surface = dri2_fallback_create_pbuffer_surface,
946 .destroy_surface = dri2_wl_destroy_surface,
947 .create_image = dri2_create_image_khr,
948 .swap_interval = dri2_wl_swap_interval,
949 .swap_buffers = dri2_wl_swap_buffers,
950 .swap_buffers_with_damage = dri2_wl_swap_buffers_with_damage,
951 .swap_buffers_region = dri2_fallback_swap_buffers_region,
952 .post_sub_buffer = dri2_fallback_post_sub_buffer,
953 .copy_buffers = dri2_fallback_copy_buffers,
954 .query_buffer_age = dri2_wl_query_buffer_age,
955 .create_wayland_buffer_from_image = dri2_wl_create_wayland_buffer_from_image,
956 .get_sync_values = dri2_fallback_get_sync_values,
957 };
958
959 EGLBoolean
960 dri2_initialize_wayland(_EGLDriver *drv, _EGLDisplay *disp)
961 {
962 struct dri2_egl_display *dri2_dpy;
963 const __DRIconfig *config;
964 uint32_t types;
965 int i;
966 static const unsigned int argb_masks[4] =
967 { 0xff0000, 0xff00, 0xff, 0xff000000 };
968 static const unsigned int rgb_masks[4] = { 0xff0000, 0xff00, 0xff, 0 };
969 static const unsigned int rgb565_masks[4] = { 0xf800, 0x07e0, 0x001f, 0 };
970
971 loader_set_logger(_eglLog);
972
973 dri2_dpy = calloc(1, sizeof *dri2_dpy);
974 if (!dri2_dpy)
975 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
976
977 disp->DriverData = (void *) dri2_dpy;
978 if (disp->PlatformDisplay == NULL) {
979 dri2_dpy->wl_dpy = wl_display_connect(NULL);
980 if (dri2_dpy->wl_dpy == NULL)
981 goto cleanup_dpy;
982 dri2_dpy->own_device = 1;
983 } else {
984 dri2_dpy->wl_dpy = disp->PlatformDisplay;
985 }
986
987 dri2_dpy->wl_queue = wl_display_create_queue(dri2_dpy->wl_dpy);
988
989 if (dri2_dpy->own_device)
990 wl_display_dispatch_pending(dri2_dpy->wl_dpy);
991
992 dri2_dpy->wl_registry = wl_display_get_registry(dri2_dpy->wl_dpy);
993 wl_proxy_set_queue((struct wl_proxy *) dri2_dpy->wl_registry,
994 dri2_dpy->wl_queue);
995 wl_registry_add_listener(dri2_dpy->wl_registry,
996 &registry_listener, dri2_dpy);
997 if (roundtrip(dri2_dpy) < 0 || dri2_dpy->wl_drm == NULL)
998 goto cleanup_dpy;
999
1000 if (roundtrip(dri2_dpy) < 0 || dri2_dpy->fd == -1)
1001 goto cleanup_drm;
1002
1003 if (roundtrip(dri2_dpy) < 0 || !dri2_dpy->authenticated)
1004 goto cleanup_fd;
1005
1006 dri2_dpy->driver_name = loader_get_driver_for_fd(dri2_dpy->fd, 0);
1007 if (dri2_dpy->driver_name == NULL) {
1008 _eglError(EGL_BAD_ALLOC, "DRI2: failed to get driver name");
1009 goto cleanup_fd;
1010 }
1011
1012 if (!dri2_load_driver(disp))
1013 goto cleanup_driver_name;
1014
1015 dri2_dpy->dri2_loader_extension.base.name = __DRI_DRI2_LOADER;
1016 dri2_dpy->dri2_loader_extension.base.version = 3;
1017 dri2_dpy->dri2_loader_extension.getBuffers = dri2_wl_get_buffers;
1018 dri2_dpy->dri2_loader_extension.flushFrontBuffer = dri2_wl_flush_front_buffer;
1019 dri2_dpy->dri2_loader_extension.getBuffersWithFormat =
1020 dri2_wl_get_buffers_with_format;
1021
1022 dri2_dpy->extensions[0] = &dri2_dpy->dri2_loader_extension.base;
1023 dri2_dpy->extensions[1] = &image_loader_extension.base;
1024 dri2_dpy->extensions[2] = &image_lookup_extension.base;
1025 dri2_dpy->extensions[3] = &use_invalidate.base;
1026 dri2_dpy->extensions[4] = NULL;
1027
1028 dri2_dpy->swap_available = EGL_TRUE;
1029
1030 if (!dri2_create_screen(disp))
1031 goto cleanup_driver;
1032
1033 dri2_wl_setup_swap_interval(dri2_dpy);
1034
1035 /* The server shouldn't advertise WL_DRM_CAPABILITY_PRIME if the driver
1036 * doesn't have createImageFromFds, since we're using the same driver on
1037 * both sides. We don't want crash if that happens anyway, so fall back to
1038 * gem names if we don't have prime support. */
1039
1040 if (dri2_dpy->image->base.version < 7 ||
1041 dri2_dpy->image->createImageFromFds == NULL)
1042 dri2_dpy->capabilities &= ~WL_DRM_CAPABILITY_PRIME;
1043
1044 types = EGL_WINDOW_BIT;
1045 for (i = 0; dri2_dpy->driver_configs[i]; i++) {
1046 config = dri2_dpy->driver_configs[i];
1047 if (dri2_dpy->formats & HAS_XRGB8888)
1048 dri2_add_config(disp, config, i + 1, types, NULL, rgb_masks);
1049 if (dri2_dpy->formats & HAS_ARGB8888)
1050 dri2_add_config(disp, config, i + 1, types, NULL, argb_masks);
1051 if (dri2_dpy->formats & HAS_RGB565)
1052 dri2_add_config(disp, config, i + 1, types, NULL, rgb565_masks);
1053 }
1054
1055 disp->Extensions.WL_bind_wayland_display = EGL_TRUE;
1056 disp->Extensions.WL_create_wayland_buffer_from_image = EGL_TRUE;
1057 disp->Extensions.EXT_buffer_age = EGL_TRUE;
1058
1059 disp->Extensions.EXT_swap_buffers_with_damage = EGL_TRUE;
1060
1061 /* we're supporting EGL 1.4 */
1062 disp->VersionMajor = 1;
1063 disp->VersionMinor = 4;
1064
1065 /* Fill vtbl last to prevent accidentally calling virtual function during
1066 * initialization.
1067 */
1068 dri2_dpy->vtbl = &dri2_wl_display_vtbl;
1069
1070 return EGL_TRUE;
1071
1072 cleanup_driver:
1073 dlclose(dri2_dpy->driver);
1074 cleanup_driver_name:
1075 free(dri2_dpy->driver_name);
1076 cleanup_fd:
1077 close(dri2_dpy->fd);
1078 cleanup_drm:
1079 free(dri2_dpy->device_name);
1080 wl_drm_destroy(dri2_dpy->wl_drm);
1081 cleanup_dpy:
1082 free(dri2_dpy);
1083
1084 return EGL_FALSE;
1085 }