egl/dri2: Dispatch API.QueryBufferAge by display, not driver
[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, EGLNativeWindowType 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 dri2_egl_surface *dri2_surf;
129
130 (void) drv;
131
132 dri2_surf = malloc(sizeof *dri2_surf);
133 if (!dri2_surf) {
134 _eglError(EGL_BAD_ALLOC, "dri2_create_surface");
135 return NULL;
136 }
137
138 memset(dri2_surf, 0, sizeof *dri2_surf);
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 = (struct wl_egl_window *) 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, EGLNativeWindowType 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 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 /**
205 * Called via eglDestroySurface(), drv->API.DestroySurface().
206 */
207 static EGLBoolean
208 dri2_wl_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
209 {
210 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
211 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
212 int i;
213
214 (void) drv;
215
216 if (!_eglPutSurface(surf))
217 return EGL_TRUE;
218
219 (*dri2_dpy->core->destroyDrawable)(dri2_surf->dri_drawable);
220
221 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
222 if (dri2_surf->color_buffers[i].wl_buffer)
223 wl_buffer_destroy(dri2_surf->color_buffers[i].wl_buffer);
224 if (dri2_surf->color_buffers[i].dri_image)
225 dri2_dpy->image->destroyImage(dri2_surf->color_buffers[i].dri_image);
226 }
227
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 if (dri2_surf->throttle_callback)
235 wl_callback_destroy(dri2_surf->throttle_callback);
236
237 if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
238 dri2_surf->wl_win->private = NULL;
239 dri2_surf->wl_win->resize_callback = NULL;
240 }
241
242 free(surf);
243
244 return EGL_TRUE;
245 }
246
247 static void
248 dri2_wl_release_buffers(struct dri2_egl_surface *dri2_surf)
249 {
250 struct dri2_egl_display *dri2_dpy =
251 dri2_egl_display(dri2_surf->base.Resource.Display);
252 int i;
253
254 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
255 if (dri2_surf->color_buffers[i].wl_buffer &&
256 !dri2_surf->color_buffers[i].locked)
257 wl_buffer_destroy(dri2_surf->color_buffers[i].wl_buffer);
258 if (dri2_surf->color_buffers[i].dri_image)
259 dri2_dpy->image->destroyImage(dri2_surf->color_buffers[i].dri_image);
260
261 dri2_surf->color_buffers[i].wl_buffer = NULL;
262 dri2_surf->color_buffers[i].dri_image = NULL;
263 dri2_surf->color_buffers[i].locked = 0;
264 }
265
266 for (i = 0; i < __DRI_BUFFER_COUNT; i++)
267 if (dri2_surf->dri_buffers[i] &&
268 dri2_surf->dri_buffers[i]->attachment != __DRI_BUFFER_BACK_LEFT)
269 dri2_dpy->dri2->releaseBuffer(dri2_dpy->dri_screen,
270 dri2_surf->dri_buffers[i]);
271 }
272
273 static int
274 get_back_bo(struct dri2_egl_surface *dri2_surf)
275 {
276 struct dri2_egl_display *dri2_dpy =
277 dri2_egl_display(dri2_surf->base.Resource.Display);
278 int i;
279
280 /* We always want to throttle to some event (either a frame callback or
281 * a sync request) after the commit so that we can be sure the
282 * compositor has had a chance to handle it and send us a release event
283 * before we look for a free buffer */
284 while (dri2_surf->throttle_callback != NULL)
285 if (wl_display_dispatch_queue(dri2_dpy->wl_dpy,
286 dri2_dpy->wl_queue) == -1)
287 return -1;
288
289 if (dri2_surf->back == NULL) {
290 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
291 /* Get an unlocked buffer, preferrably one with a dri_buffer
292 * already allocated. */
293 if (dri2_surf->color_buffers[i].locked)
294 continue;
295 if (dri2_surf->back == NULL)
296 dri2_surf->back = &dri2_surf->color_buffers[i];
297 else if (dri2_surf->back->dri_image == NULL)
298 dri2_surf->back = &dri2_surf->color_buffers[i];
299 }
300 }
301
302 if (dri2_surf->back == NULL)
303 return -1;
304 if (dri2_surf->back->dri_image == NULL) {
305 dri2_surf->back->dri_image =
306 dri2_dpy->image->createImage(dri2_dpy->dri_screen,
307 dri2_surf->base.Width,
308 dri2_surf->base.Height,
309 __DRI_IMAGE_FORMAT_ARGB8888,
310 __DRI_IMAGE_USE_SHARE,
311 NULL);
312 dri2_surf->back->age = 0;
313 }
314 if (dri2_surf->back->dri_image == NULL)
315 return -1;
316
317 dri2_surf->back->locked = 1;
318
319 return 0;
320 }
321
322
323 static void
324 back_bo_to_dri_buffer(struct dri2_egl_surface *dri2_surf, __DRIbuffer *buffer)
325 {
326 struct dri2_egl_display *dri2_dpy =
327 dri2_egl_display(dri2_surf->base.Resource.Display);
328 __DRIimage *image;
329 int name, pitch;
330
331 image = dri2_surf->back->dri_image;
332
333 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_NAME, &name);
334 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_STRIDE, &pitch);
335
336 buffer->attachment = __DRI_BUFFER_BACK_LEFT;
337 buffer->name = name;
338 buffer->pitch = pitch;
339 buffer->cpp = 4;
340 buffer->flags = 0;
341 }
342
343 static int
344 get_aux_bo(struct dri2_egl_surface *dri2_surf,
345 unsigned int attachment, unsigned int format, __DRIbuffer *buffer)
346 {
347 struct dri2_egl_display *dri2_dpy =
348 dri2_egl_display(dri2_surf->base.Resource.Display);
349 __DRIbuffer *b = dri2_surf->dri_buffers[attachment];
350
351 if (b == NULL) {
352 b = dri2_dpy->dri2->allocateBuffer(dri2_dpy->dri_screen,
353 attachment, format,
354 dri2_surf->base.Width,
355 dri2_surf->base.Height);
356 dri2_surf->dri_buffers[attachment] = b;
357 }
358 if (b == NULL)
359 return -1;
360
361 memcpy(buffer, b, sizeof *buffer);
362
363 return 0;
364 }
365
366 static int
367 update_buffers(struct dri2_egl_surface *dri2_surf)
368 {
369 struct dri2_egl_display *dri2_dpy =
370 dri2_egl_display(dri2_surf->base.Resource.Display);
371 int i;
372
373 if (dri2_surf->base.Type == EGL_WINDOW_BIT &&
374 (dri2_surf->base.Width != dri2_surf->wl_win->width ||
375 dri2_surf->base.Height != dri2_surf->wl_win->height)) {
376
377 dri2_wl_release_buffers(dri2_surf);
378
379 dri2_surf->base.Width = dri2_surf->wl_win->width;
380 dri2_surf->base.Height = dri2_surf->wl_win->height;
381 dri2_surf->dx = dri2_surf->wl_win->dx;
382 dri2_surf->dy = dri2_surf->wl_win->dy;
383 }
384
385 if (get_back_bo(dri2_surf) < 0) {
386 _eglError(EGL_BAD_ALLOC, "failed to allocate color buffer");
387 return -1;
388 }
389
390 /* If we have an extra unlocked buffer at this point, we had to do triple
391 * buffering for a while, but now can go back to just double buffering.
392 * That means we can free any unlocked buffer now. */
393 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
394 if (!dri2_surf->color_buffers[i].locked &&
395 dri2_surf->color_buffers[i].wl_buffer) {
396 wl_buffer_destroy(dri2_surf->color_buffers[i].wl_buffer);
397 dri2_dpy->image->destroyImage(dri2_surf->color_buffers[i].dri_image);
398 dri2_surf->color_buffers[i].wl_buffer = NULL;
399 dri2_surf->color_buffers[i].dri_image = NULL;
400 }
401 }
402
403 return 0;
404 }
405
406 static __DRIbuffer *
407 dri2_wl_get_buffers_with_format(__DRIdrawable * driDrawable,
408 int *width, int *height,
409 unsigned int *attachments, int count,
410 int *out_count, void *loaderPrivate)
411 {
412 struct dri2_egl_surface *dri2_surf = loaderPrivate;
413 int i, j;
414
415 if (update_buffers(dri2_surf) < 0)
416 return NULL;
417
418 for (i = 0, j = 0; i < 2 * count; i += 2, j++) {
419 switch (attachments[i]) {
420 case __DRI_BUFFER_BACK_LEFT:
421 back_bo_to_dri_buffer(dri2_surf, &dri2_surf->buffers[j]);
422 break;
423 default:
424 if (get_aux_bo(dri2_surf, attachments[i], attachments[i + 1],
425 &dri2_surf->buffers[j]) < 0) {
426 _eglError(EGL_BAD_ALLOC, "failed to allocate aux buffer");
427 return NULL;
428 }
429 break;
430 }
431 }
432
433 *out_count = j;
434 if (j == 0)
435 return NULL;
436
437 *width = dri2_surf->base.Width;
438 *height = dri2_surf->base.Height;
439
440 return dri2_surf->buffers;
441 }
442
443 static __DRIbuffer *
444 dri2_wl_get_buffers(__DRIdrawable * driDrawable,
445 int *width, int *height,
446 unsigned int *attachments, int count,
447 int *out_count, void *loaderPrivate)
448 {
449 unsigned int *attachments_with_format;
450 __DRIbuffer *buffer;
451 const unsigned int format = 32;
452 int i;
453
454 attachments_with_format = calloc(count * 2, sizeof(unsigned int));
455 if (!attachments_with_format) {
456 *out_count = 0;
457 return NULL;
458 }
459
460 for (i = 0; i < count; ++i) {
461 attachments_with_format[2*i] = attachments[i];
462 attachments_with_format[2*i + 1] = format;
463 }
464
465 buffer =
466 dri2_wl_get_buffers_with_format(driDrawable,
467 width, height,
468 attachments_with_format, count,
469 out_count, loaderPrivate);
470
471 free(attachments_with_format);
472
473 return buffer;
474 }
475
476 static int
477 image_get_buffers(__DRIdrawable *driDrawable,
478 unsigned int format,
479 uint32_t *stamp,
480 void *loaderPrivate,
481 uint32_t buffer_mask,
482 struct __DRIimageList *buffers)
483 {
484 struct dri2_egl_surface *dri2_surf = loaderPrivate;
485
486 if (update_buffers(dri2_surf) < 0)
487 return 0;
488
489 buffers->image_mask = __DRI_IMAGE_BUFFER_BACK;
490 buffers->back = dri2_surf->back->dri_image;
491
492 return 1;
493 }
494
495 static void
496 dri2_wl_flush_front_buffer(__DRIdrawable * driDrawable, void *loaderPrivate)
497 {
498 (void) driDrawable;
499 (void) loaderPrivate;
500 }
501
502 static const __DRIimageLoaderExtension image_loader_extension = {
503 .base = { __DRI_IMAGE_LOADER, 1 },
504
505 .getBuffers = image_get_buffers,
506 .flushFrontBuffer = dri2_wl_flush_front_buffer,
507 };
508
509 static void
510 wayland_throttle_callback(void *data,
511 struct wl_callback *callback,
512 uint32_t time)
513 {
514 struct dri2_egl_surface *dri2_surf = data;
515
516 dri2_surf->throttle_callback = NULL;
517 wl_callback_destroy(callback);
518 }
519
520 static const struct wl_callback_listener throttle_listener = {
521 wayland_throttle_callback
522 };
523
524 static void
525 create_wl_buffer(struct dri2_egl_surface *dri2_surf)
526 {
527 struct dri2_egl_display *dri2_dpy =
528 dri2_egl_display(dri2_surf->base.Resource.Display);
529 int fd, stride, name;
530
531 if (dri2_surf->current->wl_buffer != NULL)
532 return;
533
534 if (dri2_dpy->capabilities & WL_DRM_CAPABILITY_PRIME) {
535 dri2_dpy->image->queryImage(dri2_surf->current->dri_image,
536 __DRI_IMAGE_ATTRIB_FD, &fd);
537 dri2_dpy->image->queryImage(dri2_surf->current->dri_image,
538 __DRI_IMAGE_ATTRIB_STRIDE, &stride);
539
540 dri2_surf->current->wl_buffer =
541 wl_drm_create_prime_buffer(dri2_dpy->wl_drm,
542 fd,
543 dri2_surf->base.Width,
544 dri2_surf->base.Height,
545 dri2_surf->format,
546 0, stride,
547 0, 0,
548 0, 0);
549 close(fd);
550 } else {
551 dri2_dpy->image->queryImage(dri2_surf->current->dri_image,
552 __DRI_IMAGE_ATTRIB_NAME, &name);
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_buffer(dri2_dpy->wl_drm,
558 name,
559 dri2_surf->base.Width,
560 dri2_surf->base.Height,
561 stride,
562 dri2_surf->format);
563 }
564
565 wl_proxy_set_queue((struct wl_proxy *) dri2_surf->current->wl_buffer,
566 dri2_dpy->wl_queue);
567 wl_buffer_add_listener(dri2_surf->current->wl_buffer,
568 &wl_buffer_listener, dri2_surf);
569 }
570
571 /**
572 * Called via eglSwapBuffers(), drv->API.SwapBuffers().
573 */
574 static EGLBoolean
575 dri2_wl_swap_buffers_with_damage(_EGLDriver *drv,
576 _EGLDisplay *disp,
577 _EGLSurface *draw,
578 const EGLint *rects,
579 EGLint n_rects)
580 {
581 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
582 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
583 struct dri2_egl_context *dri2_ctx;
584 _EGLContext *ctx;
585 int i;
586
587 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++)
588 if (dri2_surf->color_buffers[i].age > 0)
589 dri2_surf->color_buffers[i].age++;
590
591 /* Make sure we have a back buffer in case we're swapping without ever
592 * rendering. */
593 if (get_back_bo(dri2_surf) < 0) {
594 _eglError(EGL_BAD_ALLOC, "dri2_swap_buffers");
595 return EGL_FALSE;
596 }
597
598 if (draw->SwapInterval > 0) {
599 dri2_surf->throttle_callback =
600 wl_surface_frame(dri2_surf->wl_win->surface);
601 wl_callback_add_listener(dri2_surf->throttle_callback,
602 &throttle_listener, dri2_surf);
603 wl_proxy_set_queue((struct wl_proxy *) dri2_surf->throttle_callback,
604 dri2_dpy->wl_queue);
605 }
606
607 dri2_surf->back->age = 1;
608 dri2_surf->current = dri2_surf->back;
609 dri2_surf->back = NULL;
610
611 create_wl_buffer(dri2_surf);
612
613 wl_surface_attach(dri2_surf->wl_win->surface,
614 dri2_surf->current->wl_buffer,
615 dri2_surf->dx, dri2_surf->dy);
616
617 dri2_surf->wl_win->attached_width = dri2_surf->base.Width;
618 dri2_surf->wl_win->attached_height = dri2_surf->base.Height;
619 /* reset resize growing parameters */
620 dri2_surf->dx = 0;
621 dri2_surf->dy = 0;
622
623 if (n_rects == 0) {
624 wl_surface_damage(dri2_surf->wl_win->surface,
625 0, 0, INT32_MAX, INT32_MAX);
626 } else {
627 for (i = 0; i < n_rects; i++) {
628 const int *rect = &rects[i * 4];
629 wl_surface_damage(dri2_surf->wl_win->surface,
630 rect[0],
631 dri2_surf->base.Height - rect[1] - rect[3],
632 rect[2], rect[3]);
633 }
634 }
635
636 if (dri2_dpy->flush->base.version >= 4) {
637 ctx = _eglGetCurrentContext();
638 dri2_ctx = dri2_egl_context(ctx);
639 (*dri2_dpy->flush->flush_with_flags)(dri2_ctx->dri_context,
640 dri2_surf->dri_drawable,
641 __DRI2_FLUSH_DRAWABLE,
642 __DRI2_THROTTLE_SWAPBUFFER);
643 } else {
644 (*dri2_dpy->flush->flush)(dri2_surf->dri_drawable);
645 }
646
647 (*dri2_dpy->flush->invalidate)(dri2_surf->dri_drawable);
648
649 wl_surface_commit(dri2_surf->wl_win->surface);
650
651 /* If we're not waiting for a frame callback then we'll at least throttle
652 * to a sync callback so that we always give a chance for the compositor to
653 * handle the commit and send a release event before checking for a free
654 * buffer */
655 if (dri2_surf->throttle_callback == NULL) {
656 dri2_surf->throttle_callback = wl_display_sync(dri2_dpy->wl_dpy);
657 wl_callback_add_listener(dri2_surf->throttle_callback,
658 &throttle_listener, dri2_surf);
659 wl_proxy_set_queue((struct wl_proxy *) dri2_surf->throttle_callback,
660 dri2_dpy->wl_queue);
661 }
662
663 wl_display_flush(dri2_dpy->wl_dpy);
664
665 return EGL_TRUE;
666 }
667
668 static EGLint
669 dri2_wl_query_buffer_age(_EGLDriver *drv,
670 _EGLDisplay *disp, _EGLSurface *surface)
671 {
672 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surface);
673
674 if (get_back_bo(dri2_surf) < 0) {
675 _eglError(EGL_BAD_ALLOC, "dri2_query_buffer_age");
676 return 0;
677 }
678
679 return dri2_surf->back->age;
680 }
681
682 static EGLBoolean
683 dri2_wl_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
684 {
685 return dri2_wl_swap_buffers_with_damage (drv, disp, draw, NULL, 0);
686 }
687
688 static struct wl_buffer *
689 dri2_wl_create_wayland_buffer_from_image_wl(_EGLDriver *drv,
690 _EGLDisplay *disp,
691 _EGLImage *img)
692 {
693 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
694 struct dri2_egl_image *dri2_img = dri2_egl_image(img);
695 __DRIimage *image = dri2_img->dri_image;
696 struct wl_buffer *buffer;
697 int width, height, format, pitch;
698 enum wl_drm_format wl_format;
699
700 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_FORMAT, &format);
701
702 switch (format) {
703 case __DRI_IMAGE_FORMAT_ARGB8888:
704 if (!(dri2_dpy->formats & HAS_ARGB8888))
705 goto bad_format;
706 wl_format = WL_DRM_FORMAT_ARGB8888;
707 break;
708 case __DRI_IMAGE_FORMAT_XRGB8888:
709 if (!(dri2_dpy->formats & HAS_XRGB8888))
710 goto bad_format;
711 wl_format = WL_DRM_FORMAT_XRGB8888;
712 break;
713 default:
714 goto bad_format;
715 }
716
717 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_WIDTH, &width);
718 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_HEIGHT, &height);
719 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_STRIDE, &pitch);
720
721 if (dri2_dpy->capabilities & WL_DRM_CAPABILITY_PRIME) {
722 int fd;
723
724 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_FD, &fd);
725
726 buffer =
727 wl_drm_create_prime_buffer(dri2_dpy->wl_drm,
728 fd,
729 width, height,
730 wl_format,
731 0, pitch,
732 0, 0,
733 0, 0);
734
735 close(fd);
736 } else {
737 int name;
738
739 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_NAME, &name);
740
741 buffer =
742 wl_drm_create_buffer(dri2_dpy->wl_drm,
743 name,
744 width, height,
745 pitch,
746 wl_format);
747 }
748
749 /* The buffer object will have been created with our internal event queue
750 * because it is using the wl_drm object as a proxy factory. We want the
751 * buffer to be used by the application so we'll reset it to the display's
752 * default event queue */
753 if (buffer)
754 wl_proxy_set_queue((struct wl_proxy *) buffer, NULL);
755
756 return buffer;
757
758 bad_format:
759 _eglError(EGL_BAD_MATCH, "unsupported image format");
760 return NULL;
761 }
762
763 static int
764 dri2_wl_authenticate(_EGLDisplay *disp, uint32_t id)
765 {
766 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
767 int ret = 0;
768
769 dri2_dpy->authenticated = 0;
770
771 wl_drm_authenticate(dri2_dpy->wl_drm, id);
772 if (roundtrip(dri2_dpy) < 0)
773 ret = -1;
774
775 if (!dri2_dpy->authenticated)
776 ret = -1;
777
778 /* reset authenticated */
779 dri2_dpy->authenticated = 1;
780
781 return ret;
782 }
783
784 /**
785 * Called via eglTerminate(), drv->API.Terminate().
786 */
787 static EGLBoolean
788 dri2_wl_terminate(_EGLDriver *drv, _EGLDisplay *disp)
789 {
790 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
791
792 _eglReleaseDisplayResources(drv, disp);
793 _eglCleanupDisplay(disp);
794
795 dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen);
796 close(dri2_dpy->fd);
797 dlclose(dri2_dpy->driver);
798 free(dri2_dpy->driver_name);
799 free(dri2_dpy->device_name);
800 wl_drm_destroy(dri2_dpy->wl_drm);
801 if (dri2_dpy->own_device)
802 wl_display_disconnect(dri2_dpy->wl_dpy);
803 free(dri2_dpy);
804 disp->DriverData = NULL;
805
806 return EGL_TRUE;
807 }
808
809 static void
810 drm_handle_device(void *data, struct wl_drm *drm, const char *device)
811 {
812 struct dri2_egl_display *dri2_dpy = data;
813 drm_magic_t magic;
814
815 dri2_dpy->device_name = strdup(device);
816 if (!dri2_dpy->device_name)
817 return;
818
819 #ifdef O_CLOEXEC
820 dri2_dpy->fd = open(dri2_dpy->device_name, O_RDWR | O_CLOEXEC);
821 if (dri2_dpy->fd == -1 && errno == EINVAL)
822 #endif
823 {
824 dri2_dpy->fd = open(dri2_dpy->device_name, O_RDWR);
825 if (dri2_dpy->fd != -1)
826 fcntl(dri2_dpy->fd, F_SETFD, fcntl(dri2_dpy->fd, F_GETFD) |
827 FD_CLOEXEC);
828 }
829 if (dri2_dpy->fd == -1) {
830 _eglLog(_EGL_WARNING, "wayland-egl: could not open %s (%s)",
831 dri2_dpy->device_name, strerror(errno));
832 return;
833 }
834
835 drmGetMagic(dri2_dpy->fd, &magic);
836 wl_drm_authenticate(dri2_dpy->wl_drm, magic);
837 }
838
839 static void
840 drm_handle_format(void *data, struct wl_drm *drm, uint32_t format)
841 {
842 struct dri2_egl_display *dri2_dpy = data;
843
844 switch (format) {
845 case WL_DRM_FORMAT_ARGB8888:
846 dri2_dpy->formats |= HAS_ARGB8888;
847 break;
848 case WL_DRM_FORMAT_XRGB8888:
849 dri2_dpy->formats |= HAS_XRGB8888;
850 break;
851 case WL_DRM_FORMAT_RGB565:
852 dri2_dpy->formats |= HAS_RGB565;
853 break;
854 }
855 }
856
857 static void
858 drm_handle_capabilities(void *data, struct wl_drm *drm, uint32_t value)
859 {
860 struct dri2_egl_display *dri2_dpy = data;
861
862 dri2_dpy->capabilities = value;
863 }
864
865 static void
866 drm_handle_authenticated(void *data, struct wl_drm *drm)
867 {
868 struct dri2_egl_display *dri2_dpy = data;
869
870 dri2_dpy->authenticated = 1;
871 }
872
873 static const struct wl_drm_listener drm_listener = {
874 drm_handle_device,
875 drm_handle_format,
876 drm_handle_authenticated,
877 drm_handle_capabilities
878 };
879
880 static void
881 registry_handle_global(void *data, struct wl_registry *registry, uint32_t name,
882 const char *interface, uint32_t version)
883 {
884 struct dri2_egl_display *dri2_dpy = data;
885
886 if (version > 1)
887 version = 2;
888 if (strcmp(interface, "wl_drm") == 0) {
889 dri2_dpy->wl_drm =
890 wl_registry_bind(registry, name, &wl_drm_interface, version);
891 wl_drm_add_listener(dri2_dpy->wl_drm, &drm_listener, dri2_dpy);
892 }
893 }
894
895 static void
896 registry_handle_global_remove(void *data, struct wl_registry *registry,
897 uint32_t name)
898 {
899 }
900
901 static const struct wl_registry_listener registry_listener = {
902 registry_handle_global,
903 registry_handle_global_remove
904 };
905
906 static EGLBoolean
907 dri2_wl_swap_interval(_EGLDriver *drv,
908 _EGLDisplay *disp,
909 _EGLSurface *surf,
910 EGLint interval)
911 {
912 if (interval > surf->Config->MaxSwapInterval)
913 interval = surf->Config->MaxSwapInterval;
914 else if (interval < surf->Config->MinSwapInterval)
915 interval = surf->Config->MinSwapInterval;
916
917 surf->SwapInterval = interval;
918
919 return EGL_TRUE;
920 }
921
922 static void
923 dri2_wl_setup_swap_interval(struct dri2_egl_display *dri2_dpy)
924 {
925 GLint vblank_mode = DRI_CONF_VBLANK_DEF_INTERVAL_1;
926
927 /* We can't use values greater than 1 on Wayland because we are using the
928 * frame callback to synchronise the frame and the only way we be sure to
929 * get a frame callback is to attach a new buffer. Therefore we can't just
930 * sit drawing nothing to wait until the next ‘n’ frame callbacks */
931
932 if (dri2_dpy->config)
933 dri2_dpy->config->configQueryi(dri2_dpy->dri_screen,
934 "vblank_mode", &vblank_mode);
935 switch (vblank_mode) {
936 case DRI_CONF_VBLANK_NEVER:
937 dri2_dpy->min_swap_interval = 0;
938 dri2_dpy->max_swap_interval = 0;
939 dri2_dpy->default_swap_interval = 0;
940 break;
941 case DRI_CONF_VBLANK_ALWAYS_SYNC:
942 dri2_dpy->min_swap_interval = 1;
943 dri2_dpy->max_swap_interval = 1;
944 dri2_dpy->default_swap_interval = 1;
945 break;
946 case DRI_CONF_VBLANK_DEF_INTERVAL_0:
947 dri2_dpy->min_swap_interval = 0;
948 dri2_dpy->max_swap_interval = 1;
949 dri2_dpy->default_swap_interval = 0;
950 break;
951 default:
952 case DRI_CONF_VBLANK_DEF_INTERVAL_1:
953 dri2_dpy->min_swap_interval = 0;
954 dri2_dpy->max_swap_interval = 1;
955 dri2_dpy->default_swap_interval = 1;
956 break;
957 }
958 }
959
960 static struct dri2_egl_display_vtbl dri2_wl_display_vtbl = {
961 .authenticate = dri2_wl_authenticate,
962 .create_window_surface = dri2_wl_create_window_surface,
963 .create_pixmap_surface = dri2_fallback_create_pixmap_surface,
964 .create_pbuffer_surface = dri2_fallback_create_pbuffer_surface,
965 .destroy_surface = dri2_wl_destroy_surface,
966 .swap_interval = dri2_wl_swap_interval,
967 .swap_buffers = dri2_wl_swap_buffers,
968 .swap_buffers_with_damage = dri2_wl_swap_buffers_with_damage,
969 .query_buffer_age = dri2_wl_query_buffer_age,
970 };
971
972 EGLBoolean
973 dri2_initialize_wayland(_EGLDriver *drv, _EGLDisplay *disp)
974 {
975 struct dri2_egl_display *dri2_dpy;
976 const __DRIconfig *config;
977 uint32_t types;
978 int i;
979 static const unsigned int argb_masks[4] =
980 { 0xff0000, 0xff00, 0xff, 0xff000000 };
981 static const unsigned int rgb_masks[4] = { 0xff0000, 0xff00, 0xff, 0 };
982 static const unsigned int rgb565_masks[4] = { 0xf800, 0x07e0, 0x001f, 0 };
983
984 loader_set_logger(_eglLog);
985
986 drv->API.Terminate = dri2_wl_terminate;
987
988 drv->API.CreateWaylandBufferFromImageWL =
989 dri2_wl_create_wayland_buffer_from_image_wl;
990
991 dri2_dpy = calloc(1, sizeof *dri2_dpy);
992 if (!dri2_dpy)
993 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
994
995 disp->DriverData = (void *) dri2_dpy;
996 if (disp->PlatformDisplay == NULL) {
997 dri2_dpy->wl_dpy = wl_display_connect(NULL);
998 if (dri2_dpy->wl_dpy == NULL)
999 goto cleanup_dpy;
1000 dri2_dpy->own_device = 1;
1001 } else {
1002 dri2_dpy->wl_dpy = disp->PlatformDisplay;
1003 }
1004
1005 dri2_dpy->wl_queue = wl_display_create_queue(dri2_dpy->wl_dpy);
1006
1007 if (dri2_dpy->own_device)
1008 wl_display_dispatch_pending(dri2_dpy->wl_dpy);
1009
1010 dri2_dpy->wl_registry = wl_display_get_registry(dri2_dpy->wl_dpy);
1011 wl_proxy_set_queue((struct wl_proxy *) dri2_dpy->wl_registry,
1012 dri2_dpy->wl_queue);
1013 wl_registry_add_listener(dri2_dpy->wl_registry,
1014 &registry_listener, dri2_dpy);
1015 if (roundtrip(dri2_dpy) < 0 || dri2_dpy->wl_drm == NULL)
1016 goto cleanup_dpy;
1017
1018 if (roundtrip(dri2_dpy) < 0 || dri2_dpy->fd == -1)
1019 goto cleanup_drm;
1020
1021 if (roundtrip(dri2_dpy) < 0 || !dri2_dpy->authenticated)
1022 goto cleanup_fd;
1023
1024 dri2_dpy->driver_name = loader_get_driver_for_fd(dri2_dpy->fd, 0);
1025 if (dri2_dpy->driver_name == NULL) {
1026 _eglError(EGL_BAD_ALLOC, "DRI2: failed to get driver name");
1027 goto cleanup_fd;
1028 }
1029
1030 if (!dri2_load_driver(disp))
1031 goto cleanup_driver_name;
1032
1033 dri2_dpy->dri2_loader_extension.base.name = __DRI_DRI2_LOADER;
1034 dri2_dpy->dri2_loader_extension.base.version = 3;
1035 dri2_dpy->dri2_loader_extension.getBuffers = dri2_wl_get_buffers;
1036 dri2_dpy->dri2_loader_extension.flushFrontBuffer = dri2_wl_flush_front_buffer;
1037 dri2_dpy->dri2_loader_extension.getBuffersWithFormat =
1038 dri2_wl_get_buffers_with_format;
1039
1040 dri2_dpy->extensions[0] = &dri2_dpy->dri2_loader_extension.base;
1041 dri2_dpy->extensions[1] = &image_loader_extension.base;
1042 dri2_dpy->extensions[2] = &image_lookup_extension.base;
1043 dri2_dpy->extensions[3] = &use_invalidate.base;
1044 dri2_dpy->extensions[4] = NULL;
1045
1046 dri2_dpy->swap_available = EGL_TRUE;
1047
1048 if (!dri2_create_screen(disp))
1049 goto cleanup_driver;
1050
1051 dri2_wl_setup_swap_interval(dri2_dpy);
1052
1053 /* The server shouldn't advertise WL_DRM_CAPABILITY_PRIME if the driver
1054 * doesn't have createImageFromFds, since we're using the same driver on
1055 * both sides. We don't want crash if that happens anyway, so fall back to
1056 * gem names if we don't have prime support. */
1057
1058 if (dri2_dpy->image->base.version < 7 ||
1059 dri2_dpy->image->createImageFromFds == NULL)
1060 dri2_dpy->capabilities &= WL_DRM_CAPABILITY_PRIME;
1061
1062 types = EGL_WINDOW_BIT;
1063 for (i = 0; dri2_dpy->driver_configs[i]; i++) {
1064 config = dri2_dpy->driver_configs[i];
1065 if (dri2_dpy->formats & HAS_XRGB8888)
1066 dri2_add_config(disp, config, i + 1, types, NULL, rgb_masks);
1067 if (dri2_dpy->formats & HAS_ARGB8888)
1068 dri2_add_config(disp, config, i + 1, types, NULL, argb_masks);
1069 if (dri2_dpy->formats & HAS_RGB565)
1070 dri2_add_config(disp, config, i + 1, types, NULL, rgb565_masks);
1071 }
1072
1073 disp->Extensions.WL_bind_wayland_display = EGL_TRUE;
1074 disp->Extensions.WL_create_wayland_buffer_from_image = EGL_TRUE;
1075 disp->Extensions.EXT_buffer_age = EGL_TRUE;
1076
1077 disp->Extensions.EXT_swap_buffers_with_damage = EGL_TRUE;
1078
1079 /* we're supporting EGL 1.4 */
1080 disp->VersionMajor = 1;
1081 disp->VersionMinor = 4;
1082
1083 /* Fill vtbl last to prevent accidentally calling virtual function during
1084 * initialization.
1085 */
1086 dri2_dpy->vtbl = &dri2_wl_display_vtbl;
1087
1088 return EGL_TRUE;
1089
1090 cleanup_driver:
1091 dlclose(dri2_dpy->driver);
1092 cleanup_driver_name:
1093 free(dri2_dpy->driver_name);
1094 cleanup_fd:
1095 close(dri2_dpy->fd);
1096 cleanup_drm:
1097 free(dri2_dpy->device_name);
1098 wl_drm_destroy(dri2_dpy->wl_drm);
1099 cleanup_dpy:
1100 free(dri2_dpy);
1101
1102 return EGL_FALSE;
1103 }