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