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