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