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