egl/wayland: Fix unused variable warnings
[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 int i;
600
601 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++)
602 if (dri2_surf->color_buffers[i].age > 0)
603 dri2_surf->color_buffers[i].age++;
604
605 /* Make sure we have a back buffer in case we're swapping without ever
606 * rendering. */
607 if (get_back_bo(dri2_surf) < 0) {
608 _eglError(EGL_BAD_ALLOC, "dri2_swap_buffers");
609 return EGL_FALSE;
610 }
611
612 if (draw->SwapInterval > 0) {
613 dri2_surf->throttle_callback =
614 wl_surface_frame(dri2_surf->wl_win->surface);
615 wl_callback_add_listener(dri2_surf->throttle_callback,
616 &throttle_listener, dri2_surf);
617 wl_proxy_set_queue((struct wl_proxy *) dri2_surf->throttle_callback,
618 dri2_dpy->wl_queue);
619 }
620
621 dri2_surf->back->age = 1;
622 dri2_surf->current = dri2_surf->back;
623 dri2_surf->back = NULL;
624
625 create_wl_buffer(dri2_surf);
626
627 wl_surface_attach(dri2_surf->wl_win->surface,
628 dri2_surf->current->wl_buffer,
629 dri2_surf->dx, dri2_surf->dy);
630
631 dri2_surf->wl_win->attached_width = dri2_surf->base.Width;
632 dri2_surf->wl_win->attached_height = dri2_surf->base.Height;
633 /* reset resize growing parameters */
634 dri2_surf->dx = 0;
635 dri2_surf->dy = 0;
636
637 if (n_rects == 0) {
638 wl_surface_damage(dri2_surf->wl_win->surface,
639 0, 0, INT32_MAX, INT32_MAX);
640 } else {
641 for (i = 0; i < n_rects; i++) {
642 const int *rect = &rects[i * 4];
643 wl_surface_damage(dri2_surf->wl_win->surface,
644 rect[0],
645 dri2_surf->base.Height - rect[1] - rect[3],
646 rect[2], rect[3]);
647 }
648 }
649
650 dri2_flush_drawable_for_swapbuffers(disp, draw);
651 (*dri2_dpy->flush->invalidate)(dri2_surf->dri_drawable);
652
653 wl_surface_commit(dri2_surf->wl_win->surface);
654
655 /* If we're not waiting for a frame callback then we'll at least throttle
656 * to a sync callback so that we always give a chance for the compositor to
657 * handle the commit and send a release event before checking for a free
658 * buffer */
659 if (dri2_surf->throttle_callback == NULL) {
660 dri2_surf->throttle_callback = wl_display_sync(dri2_dpy->wl_dpy);
661 wl_callback_add_listener(dri2_surf->throttle_callback,
662 &throttle_listener, dri2_surf);
663 wl_proxy_set_queue((struct wl_proxy *) dri2_surf->throttle_callback,
664 dri2_dpy->wl_queue);
665 }
666
667 wl_display_flush(dri2_dpy->wl_dpy);
668
669 return EGL_TRUE;
670 }
671
672 static EGLint
673 dri2_wl_query_buffer_age(_EGLDriver *drv,
674 _EGLDisplay *disp, _EGLSurface *surface)
675 {
676 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surface);
677
678 if (get_back_bo(dri2_surf) < 0) {
679 _eglError(EGL_BAD_ALLOC, "dri2_query_buffer_age");
680 return 0;
681 }
682
683 return dri2_surf->back->age;
684 }
685
686 static EGLBoolean
687 dri2_wl_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
688 {
689 return dri2_wl_swap_buffers_with_damage (drv, disp, draw, NULL, 0);
690 }
691
692 static struct wl_buffer *
693 dri2_wl_create_wayland_buffer_from_image(_EGLDriver *drv,
694 _EGLDisplay *disp,
695 _EGLImage *img)
696 {
697 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
698 struct dri2_egl_image *dri2_img = dri2_egl_image(img);
699 __DRIimage *image = dri2_img->dri_image;
700 struct wl_buffer *buffer;
701 int width, height, format, pitch;
702 enum wl_drm_format wl_format;
703
704 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_FORMAT, &format);
705
706 switch (format) {
707 case __DRI_IMAGE_FORMAT_ARGB8888:
708 if (!(dri2_dpy->formats & HAS_ARGB8888))
709 goto bad_format;
710 wl_format = WL_DRM_FORMAT_ARGB8888;
711 break;
712 case __DRI_IMAGE_FORMAT_XRGB8888:
713 if (!(dri2_dpy->formats & HAS_XRGB8888))
714 goto bad_format;
715 wl_format = WL_DRM_FORMAT_XRGB8888;
716 break;
717 default:
718 goto bad_format;
719 }
720
721 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_WIDTH, &width);
722 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_HEIGHT, &height);
723 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_STRIDE, &pitch);
724
725 if (dri2_dpy->capabilities & WL_DRM_CAPABILITY_PRIME) {
726 int fd;
727
728 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_FD, &fd);
729
730 buffer =
731 wl_drm_create_prime_buffer(dri2_dpy->wl_drm,
732 fd,
733 width, height,
734 wl_format,
735 0, pitch,
736 0, 0,
737 0, 0);
738
739 close(fd);
740 } else {
741 int name;
742
743 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_NAME, &name);
744
745 buffer =
746 wl_drm_create_buffer(dri2_dpy->wl_drm,
747 name,
748 width, height,
749 pitch,
750 wl_format);
751 }
752
753 /* The buffer object will have been created with our internal event queue
754 * because it is using the wl_drm object as a proxy factory. We want the
755 * buffer to be used by the application so we'll reset it to the display's
756 * default event queue */
757 if (buffer)
758 wl_proxy_set_queue((struct wl_proxy *) buffer, NULL);
759
760 return buffer;
761
762 bad_format:
763 _eglError(EGL_BAD_MATCH, "unsupported image format");
764 return NULL;
765 }
766
767 static int
768 dri2_wl_authenticate(_EGLDisplay *disp, uint32_t id)
769 {
770 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
771 int ret = 0;
772
773 dri2_dpy->authenticated = 0;
774
775 wl_drm_authenticate(dri2_dpy->wl_drm, id);
776 if (roundtrip(dri2_dpy) < 0)
777 ret = -1;
778
779 if (!dri2_dpy->authenticated)
780 ret = -1;
781
782 /* reset authenticated */
783 dri2_dpy->authenticated = 1;
784
785 return ret;
786 }
787
788 static void
789 drm_handle_device(void *data, struct wl_drm *drm, const char *device)
790 {
791 struct dri2_egl_display *dri2_dpy = data;
792 drm_magic_t magic;
793
794 dri2_dpy->device_name = strdup(device);
795 if (!dri2_dpy->device_name)
796 return;
797
798 #ifdef O_CLOEXEC
799 dri2_dpy->fd = open(dri2_dpy->device_name, O_RDWR | O_CLOEXEC);
800 if (dri2_dpy->fd == -1 && errno == EINVAL)
801 #endif
802 {
803 dri2_dpy->fd = open(dri2_dpy->device_name, O_RDWR);
804 if (dri2_dpy->fd != -1)
805 fcntl(dri2_dpy->fd, F_SETFD, fcntl(dri2_dpy->fd, F_GETFD) |
806 FD_CLOEXEC);
807 }
808 if (dri2_dpy->fd == -1) {
809 _eglLog(_EGL_WARNING, "wayland-egl: could not open %s (%s)",
810 dri2_dpy->device_name, strerror(errno));
811 return;
812 }
813
814 drmGetMagic(dri2_dpy->fd, &magic);
815 wl_drm_authenticate(dri2_dpy->wl_drm, magic);
816 }
817
818 static void
819 drm_handle_format(void *data, struct wl_drm *drm, uint32_t format)
820 {
821 struct dri2_egl_display *dri2_dpy = data;
822
823 switch (format) {
824 case WL_DRM_FORMAT_ARGB8888:
825 dri2_dpy->formats |= HAS_ARGB8888;
826 break;
827 case WL_DRM_FORMAT_XRGB8888:
828 dri2_dpy->formats |= HAS_XRGB8888;
829 break;
830 case WL_DRM_FORMAT_RGB565:
831 dri2_dpy->formats |= HAS_RGB565;
832 break;
833 }
834 }
835
836 static void
837 drm_handle_capabilities(void *data, struct wl_drm *drm, uint32_t value)
838 {
839 struct dri2_egl_display *dri2_dpy = data;
840
841 dri2_dpy->capabilities = value;
842 }
843
844 static void
845 drm_handle_authenticated(void *data, struct wl_drm *drm)
846 {
847 struct dri2_egl_display *dri2_dpy = data;
848
849 dri2_dpy->authenticated = 1;
850 }
851
852 static const struct wl_drm_listener drm_listener = {
853 drm_handle_device,
854 drm_handle_format,
855 drm_handle_authenticated,
856 drm_handle_capabilities
857 };
858
859 static void
860 registry_handle_global(void *data, struct wl_registry *registry, uint32_t name,
861 const char *interface, uint32_t version)
862 {
863 struct dri2_egl_display *dri2_dpy = data;
864
865 if (version > 1)
866 version = 2;
867 if (strcmp(interface, "wl_drm") == 0) {
868 dri2_dpy->wl_drm =
869 wl_registry_bind(registry, name, &wl_drm_interface, version);
870 wl_drm_add_listener(dri2_dpy->wl_drm, &drm_listener, dri2_dpy);
871 }
872 }
873
874 static void
875 registry_handle_global_remove(void *data, struct wl_registry *registry,
876 uint32_t name)
877 {
878 }
879
880 static const struct wl_registry_listener registry_listener = {
881 registry_handle_global,
882 registry_handle_global_remove
883 };
884
885 static EGLBoolean
886 dri2_wl_swap_interval(_EGLDriver *drv,
887 _EGLDisplay *disp,
888 _EGLSurface *surf,
889 EGLint interval)
890 {
891 if (interval > surf->Config->MaxSwapInterval)
892 interval = surf->Config->MaxSwapInterval;
893 else if (interval < surf->Config->MinSwapInterval)
894 interval = surf->Config->MinSwapInterval;
895
896 surf->SwapInterval = interval;
897
898 return EGL_TRUE;
899 }
900
901 static void
902 dri2_wl_setup_swap_interval(struct dri2_egl_display *dri2_dpy)
903 {
904 GLint vblank_mode = DRI_CONF_VBLANK_DEF_INTERVAL_1;
905
906 /* We can't use values greater than 1 on Wayland because we are using the
907 * frame callback to synchronise the frame and the only way we be sure to
908 * get a frame callback is to attach a new buffer. Therefore we can't just
909 * sit drawing nothing to wait until the next ‘n’ frame callbacks */
910
911 if (dri2_dpy->config)
912 dri2_dpy->config->configQueryi(dri2_dpy->dri_screen,
913 "vblank_mode", &vblank_mode);
914 switch (vblank_mode) {
915 case DRI_CONF_VBLANK_NEVER:
916 dri2_dpy->min_swap_interval = 0;
917 dri2_dpy->max_swap_interval = 0;
918 dri2_dpy->default_swap_interval = 0;
919 break;
920 case DRI_CONF_VBLANK_ALWAYS_SYNC:
921 dri2_dpy->min_swap_interval = 1;
922 dri2_dpy->max_swap_interval = 1;
923 dri2_dpy->default_swap_interval = 1;
924 break;
925 case DRI_CONF_VBLANK_DEF_INTERVAL_0:
926 dri2_dpy->min_swap_interval = 0;
927 dri2_dpy->max_swap_interval = 1;
928 dri2_dpy->default_swap_interval = 0;
929 break;
930 default:
931 case DRI_CONF_VBLANK_DEF_INTERVAL_1:
932 dri2_dpy->min_swap_interval = 0;
933 dri2_dpy->max_swap_interval = 1;
934 dri2_dpy->default_swap_interval = 1;
935 break;
936 }
937 }
938
939 static struct dri2_egl_display_vtbl dri2_wl_display_vtbl = {
940 .authenticate = dri2_wl_authenticate,
941 .create_window_surface = dri2_wl_create_window_surface,
942 .create_pixmap_surface = dri2_wl_create_pixmap_surface,
943 .create_pbuffer_surface = dri2_fallback_create_pbuffer_surface,
944 .destroy_surface = dri2_wl_destroy_surface,
945 .create_image = dri2_create_image_khr,
946 .swap_interval = dri2_wl_swap_interval,
947 .swap_buffers = dri2_wl_swap_buffers,
948 .swap_buffers_with_damage = dri2_wl_swap_buffers_with_damage,
949 .swap_buffers_region = dri2_fallback_swap_buffers_region,
950 .post_sub_buffer = dri2_fallback_post_sub_buffer,
951 .copy_buffers = dri2_fallback_copy_buffers,
952 .query_buffer_age = dri2_wl_query_buffer_age,
953 .create_wayland_buffer_from_image = dri2_wl_create_wayland_buffer_from_image,
954 .get_sync_values = dri2_fallback_get_sync_values,
955 };
956
957 EGLBoolean
958 dri2_initialize_wayland(_EGLDriver *drv, _EGLDisplay *disp)
959 {
960 struct dri2_egl_display *dri2_dpy;
961 const __DRIconfig *config;
962 uint32_t types;
963 int i;
964 static const unsigned int argb_masks[4] =
965 { 0xff0000, 0xff00, 0xff, 0xff000000 };
966 static const unsigned int rgb_masks[4] = { 0xff0000, 0xff00, 0xff, 0 };
967 static const unsigned int rgb565_masks[4] = { 0xf800, 0x07e0, 0x001f, 0 };
968
969 loader_set_logger(_eglLog);
970
971 dri2_dpy = calloc(1, sizeof *dri2_dpy);
972 if (!dri2_dpy)
973 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
974
975 disp->DriverData = (void *) dri2_dpy;
976 if (disp->PlatformDisplay == NULL) {
977 dri2_dpy->wl_dpy = wl_display_connect(NULL);
978 if (dri2_dpy->wl_dpy == NULL)
979 goto cleanup_dpy;
980 dri2_dpy->own_device = 1;
981 } else {
982 dri2_dpy->wl_dpy = disp->PlatformDisplay;
983 }
984
985 dri2_dpy->wl_queue = wl_display_create_queue(dri2_dpy->wl_dpy);
986
987 if (dri2_dpy->own_device)
988 wl_display_dispatch_pending(dri2_dpy->wl_dpy);
989
990 dri2_dpy->wl_registry = wl_display_get_registry(dri2_dpy->wl_dpy);
991 wl_proxy_set_queue((struct wl_proxy *) dri2_dpy->wl_registry,
992 dri2_dpy->wl_queue);
993 wl_registry_add_listener(dri2_dpy->wl_registry,
994 &registry_listener, dri2_dpy);
995 if (roundtrip(dri2_dpy) < 0 || dri2_dpy->wl_drm == NULL)
996 goto cleanup_dpy;
997
998 if (roundtrip(dri2_dpy) < 0 || dri2_dpy->fd == -1)
999 goto cleanup_drm;
1000
1001 if (roundtrip(dri2_dpy) < 0 || !dri2_dpy->authenticated)
1002 goto cleanup_fd;
1003
1004 dri2_dpy->driver_name = loader_get_driver_for_fd(dri2_dpy->fd, 0);
1005 if (dri2_dpy->driver_name == NULL) {
1006 _eglError(EGL_BAD_ALLOC, "DRI2: failed to get driver name");
1007 goto cleanup_fd;
1008 }
1009
1010 if (!dri2_load_driver(disp))
1011 goto cleanup_driver_name;
1012
1013 dri2_dpy->dri2_loader_extension.base.name = __DRI_DRI2_LOADER;
1014 dri2_dpy->dri2_loader_extension.base.version = 3;
1015 dri2_dpy->dri2_loader_extension.getBuffers = dri2_wl_get_buffers;
1016 dri2_dpy->dri2_loader_extension.flushFrontBuffer = dri2_wl_flush_front_buffer;
1017 dri2_dpy->dri2_loader_extension.getBuffersWithFormat =
1018 dri2_wl_get_buffers_with_format;
1019
1020 dri2_dpy->extensions[0] = &dri2_dpy->dri2_loader_extension.base;
1021 dri2_dpy->extensions[1] = &image_loader_extension.base;
1022 dri2_dpy->extensions[2] = &image_lookup_extension.base;
1023 dri2_dpy->extensions[3] = &use_invalidate.base;
1024 dri2_dpy->extensions[4] = NULL;
1025
1026 dri2_dpy->swap_available = EGL_TRUE;
1027
1028 if (!dri2_create_screen(disp))
1029 goto cleanup_driver;
1030
1031 dri2_wl_setup_swap_interval(dri2_dpy);
1032
1033 /* The server shouldn't advertise WL_DRM_CAPABILITY_PRIME if the driver
1034 * doesn't have createImageFromFds, since we're using the same driver on
1035 * both sides. We don't want crash if that happens anyway, so fall back to
1036 * gem names if we don't have prime support. */
1037
1038 if (dri2_dpy->image->base.version < 7 ||
1039 dri2_dpy->image->createImageFromFds == NULL)
1040 dri2_dpy->capabilities &= ~WL_DRM_CAPABILITY_PRIME;
1041
1042 types = EGL_WINDOW_BIT;
1043 for (i = 0; dri2_dpy->driver_configs[i]; i++) {
1044 config = dri2_dpy->driver_configs[i];
1045 if (dri2_dpy->formats & HAS_XRGB8888)
1046 dri2_add_config(disp, config, i + 1, types, NULL, rgb_masks);
1047 if (dri2_dpy->formats & HAS_ARGB8888)
1048 dri2_add_config(disp, config, i + 1, types, NULL, argb_masks);
1049 if (dri2_dpy->formats & HAS_RGB565)
1050 dri2_add_config(disp, config, i + 1, types, NULL, rgb565_masks);
1051 }
1052
1053 disp->Extensions.WL_bind_wayland_display = EGL_TRUE;
1054 disp->Extensions.WL_create_wayland_buffer_from_image = EGL_TRUE;
1055 disp->Extensions.EXT_buffer_age = EGL_TRUE;
1056
1057 disp->Extensions.EXT_swap_buffers_with_damage = EGL_TRUE;
1058
1059 /* we're supporting EGL 1.4 */
1060 disp->VersionMajor = 1;
1061 disp->VersionMinor = 4;
1062
1063 /* Fill vtbl last to prevent accidentally calling virtual function during
1064 * initialization.
1065 */
1066 dri2_dpy->vtbl = &dri2_wl_display_vtbl;
1067
1068 return EGL_TRUE;
1069
1070 cleanup_driver:
1071 dlclose(dri2_dpy->driver);
1072 cleanup_driver_name:
1073 free(dri2_dpy->driver_name);
1074 cleanup_fd:
1075 close(dri2_dpy->fd);
1076 cleanup_drm:
1077 free(dri2_dpy->device_name);
1078 wl_drm_destroy(dri2_dpy->wl_drm);
1079 cleanup_dpy:
1080 free(dri2_dpy);
1081
1082 return EGL_FALSE;
1083 }