wayland: Add an extension to create wl_buffers from EGLImages
[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 /* There might be a buffer release already queued that wasn't processed */
267 wl_display_dispatch_queue_pending(dri2_dpy->wl_dpy, dri2_dpy->wl_queue);
268
269 if (dri2_surf->back == NULL) {
270 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
271 /* Get an unlocked buffer, preferrably one with a dri_buffer already
272 * allocated. */
273 if (dri2_surf->color_buffers[i].locked)
274 continue;
275 if (dri2_surf->back == NULL)
276 dri2_surf->back = &dri2_surf->color_buffers[i];
277 else if (dri2_surf->back->dri_image == NULL)
278 dri2_surf->back = &dri2_surf->color_buffers[i];
279 }
280 }
281
282 if (dri2_surf->back == NULL)
283 return -1;
284 if (dri2_surf->back->dri_image == NULL) {
285 dri2_surf->back->dri_image =
286 dri2_dpy->image->createImage(dri2_dpy->dri_screen,
287 dri2_surf->base.Width,
288 dri2_surf->base.Height,
289 __DRI_IMAGE_FORMAT_ARGB8888,
290 __DRI_IMAGE_USE_SHARE,
291 NULL);
292 dri2_surf->back->age = 0;
293 }
294 if (dri2_surf->back->dri_image == NULL)
295 return -1;
296
297 dri2_surf->back->locked = 1;
298
299 return 0;
300 }
301
302
303 static void
304 back_bo_to_dri_buffer(struct dri2_egl_surface *dri2_surf, __DRIbuffer *buffer)
305 {
306 struct dri2_egl_display *dri2_dpy =
307 dri2_egl_display(dri2_surf->base.Resource.Display);
308 __DRIimage *image;
309 int name, pitch;
310
311 image = dri2_surf->back->dri_image;
312
313 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_NAME, &name);
314 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_STRIDE, &pitch);
315
316 buffer->attachment = __DRI_BUFFER_BACK_LEFT;
317 buffer->name = name;
318 buffer->pitch = pitch;
319 buffer->cpp = 4;
320 buffer->flags = 0;
321 }
322
323 static int
324 get_aux_bo(struct dri2_egl_surface *dri2_surf,
325 unsigned int attachment, unsigned int format, __DRIbuffer *buffer)
326 {
327 struct dri2_egl_display *dri2_dpy =
328 dri2_egl_display(dri2_surf->base.Resource.Display);
329 __DRIbuffer *b = dri2_surf->dri_buffers[attachment];
330
331 if (b == NULL) {
332 b = dri2_dpy->dri2->allocateBuffer(dri2_dpy->dri_screen,
333 attachment, format,
334 dri2_surf->base.Width,
335 dri2_surf->base.Height);
336 dri2_surf->dri_buffers[attachment] = b;
337 }
338 if (b == NULL)
339 return -1;
340
341 memcpy(buffer, b, sizeof *buffer);
342
343 return 0;
344 }
345
346 static int
347 update_buffers(struct dri2_egl_surface *dri2_surf)
348 {
349 struct dri2_egl_display *dri2_dpy =
350 dri2_egl_display(dri2_surf->base.Resource.Display);
351 int i;
352
353 if (dri2_surf->base.Type == EGL_WINDOW_BIT &&
354 (dri2_surf->base.Width != dri2_surf->wl_win->width ||
355 dri2_surf->base.Height != dri2_surf->wl_win->height)) {
356
357 dri2_release_buffers(dri2_surf);
358
359 dri2_surf->base.Width = dri2_surf->wl_win->width;
360 dri2_surf->base.Height = dri2_surf->wl_win->height;
361 dri2_surf->dx = dri2_surf->wl_win->dx;
362 dri2_surf->dy = dri2_surf->wl_win->dy;
363 }
364
365 if (get_back_bo(dri2_surf) < 0) {
366 _eglError(EGL_BAD_ALLOC, "failed to allocate color buffer");
367 return -1;
368 }
369
370 /* If we have an extra unlocked buffer at this point, we had to do triple
371 * buffering for a while, but now can go back to just double buffering.
372 * That means we can free any unlocked buffer now. */
373 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
374 if (!dri2_surf->color_buffers[i].locked &&
375 dri2_surf->color_buffers[i].wl_buffer) {
376 wl_buffer_destroy(dri2_surf->color_buffers[i].wl_buffer);
377 dri2_dpy->image->destroyImage(dri2_surf->color_buffers[i].dri_image);
378 dri2_surf->color_buffers[i].wl_buffer = NULL;
379 dri2_surf->color_buffers[i].dri_image = NULL;
380 }
381 }
382
383 return 0;
384 }
385
386 static __DRIbuffer *
387 dri2_get_buffers_with_format(__DRIdrawable * driDrawable,
388 int *width, int *height,
389 unsigned int *attachments, int count,
390 int *out_count, void *loaderPrivate)
391 {
392 struct dri2_egl_surface *dri2_surf = loaderPrivate;
393 int i, j;
394
395 if (update_buffers(dri2_surf) < 0)
396 return NULL;
397
398 for (i = 0, j = 0; i < 2 * count; i += 2, j++) {
399 switch (attachments[i]) {
400 case __DRI_BUFFER_BACK_LEFT:
401 back_bo_to_dri_buffer(dri2_surf, &dri2_surf->buffers[j]);
402 break;
403 default:
404 if (get_aux_bo(dri2_surf, attachments[i], attachments[i + 1],
405 &dri2_surf->buffers[j]) < 0) {
406 _eglError(EGL_BAD_ALLOC, "failed to allocate aux buffer");
407 return NULL;
408 }
409 break;
410 }
411 }
412
413 *out_count = j;
414 if (j == 0)
415 return NULL;
416
417 *width = dri2_surf->base.Width;
418 *height = dri2_surf->base.Height;
419
420 return dri2_surf->buffers;
421 }
422
423 static __DRIbuffer *
424 dri2_get_buffers(__DRIdrawable * driDrawable,
425 int *width, int *height,
426 unsigned int *attachments, int count,
427 int *out_count, void *loaderPrivate)
428 {
429 unsigned int *attachments_with_format;
430 __DRIbuffer *buffer;
431 const unsigned int format = 32;
432 int i;
433
434 attachments_with_format = calloc(count * 2, sizeof(unsigned int));
435 if (!attachments_with_format) {
436 *out_count = 0;
437 return NULL;
438 }
439
440 for (i = 0; i < count; ++i) {
441 attachments_with_format[2*i] = attachments[i];
442 attachments_with_format[2*i + 1] = format;
443 }
444
445 buffer =
446 dri2_get_buffers_with_format(driDrawable,
447 width, height,
448 attachments_with_format, count,
449 out_count, loaderPrivate);
450
451 free(attachments_with_format);
452
453 return buffer;
454 }
455
456 static int
457 image_get_buffers(__DRIdrawable *driDrawable,
458 unsigned int format,
459 uint32_t *stamp,
460 void *loaderPrivate,
461 uint32_t buffer_mask,
462 struct __DRIimageList *buffers)
463 {
464 struct dri2_egl_surface *dri2_surf = loaderPrivate;
465
466 if (update_buffers(dri2_surf) < 0)
467 return 0;
468
469 buffers->image_mask = __DRI_IMAGE_BUFFER_BACK;
470 buffers->back = dri2_surf->back->dri_image;
471
472 return 1;
473 }
474
475 static void
476 dri2_flush_front_buffer(__DRIdrawable * driDrawable, void *loaderPrivate)
477 {
478 (void) driDrawable;
479 (void) loaderPrivate;
480 }
481
482 static const __DRIimageLoaderExtension image_loader_extension = {
483 { __DRI_IMAGE_LOADER, 1 },
484 image_get_buffers,
485 dri2_flush_front_buffer
486 };
487
488 static void
489 wayland_frame_callback(void *data, struct wl_callback *callback, uint32_t time)
490 {
491 struct dri2_egl_surface *dri2_surf = data;
492
493 dri2_surf->frame_callback = NULL;
494 wl_callback_destroy(callback);
495 }
496
497 static const struct wl_callback_listener frame_listener = {
498 wayland_frame_callback
499 };
500
501 static void
502 create_wl_buffer(struct dri2_egl_surface *dri2_surf)
503 {
504 struct dri2_egl_display *dri2_dpy =
505 dri2_egl_display(dri2_surf->base.Resource.Display);
506 int fd, stride, name;
507
508 if (dri2_surf->current->wl_buffer != NULL)
509 return;
510
511 if (dri2_dpy->capabilities & WL_DRM_CAPABILITY_PRIME) {
512 dri2_dpy->image->queryImage(dri2_surf->current->dri_image,
513 __DRI_IMAGE_ATTRIB_FD, &fd);
514 dri2_dpy->image->queryImage(dri2_surf->current->dri_image,
515 __DRI_IMAGE_ATTRIB_STRIDE, &stride);
516
517 dri2_surf->current->wl_buffer =
518 wl_drm_create_prime_buffer(dri2_dpy->wl_drm,
519 fd,
520 dri2_surf->base.Width,
521 dri2_surf->base.Height,
522 dri2_surf->format,
523 0, stride,
524 0, 0,
525 0, 0);
526 close(fd);
527 } else {
528 dri2_dpy->image->queryImage(dri2_surf->current->dri_image,
529 __DRI_IMAGE_ATTRIB_NAME, &name);
530 dri2_dpy->image->queryImage(dri2_surf->current->dri_image,
531 __DRI_IMAGE_ATTRIB_STRIDE, &stride);
532
533 dri2_surf->current->wl_buffer =
534 wl_drm_create_buffer(dri2_dpy->wl_drm,
535 name,
536 dri2_surf->base.Width,
537 dri2_surf->base.Height,
538 stride,
539 dri2_surf->format);
540 }
541
542 wl_proxy_set_queue((struct wl_proxy *) dri2_surf->current->wl_buffer,
543 dri2_dpy->wl_queue);
544 wl_buffer_add_listener(dri2_surf->current->wl_buffer,
545 &wl_buffer_listener, dri2_surf);
546 }
547
548 /**
549 * Called via eglSwapBuffers(), drv->API.SwapBuffers().
550 */
551 static EGLBoolean
552 dri2_swap_buffers_with_damage(_EGLDriver *drv,
553 _EGLDisplay *disp,
554 _EGLSurface *draw,
555 const EGLint *rects,
556 EGLint n_rects)
557 {
558 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
559 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
560 struct dri2_egl_context *dri2_ctx;
561 _EGLContext *ctx;
562 int i, ret = 0;
563
564 while (dri2_surf->frame_callback && ret != -1)
565 ret = wl_display_dispatch_queue(dri2_dpy->wl_dpy, dri2_dpy->wl_queue);
566 if (ret < 0)
567 return EGL_FALSE;
568
569 dri2_surf->frame_callback = wl_surface_frame(dri2_surf->wl_win->surface);
570 wl_callback_add_listener(dri2_surf->frame_callback,
571 &frame_listener, dri2_surf);
572 wl_proxy_set_queue((struct wl_proxy *) dri2_surf->frame_callback,
573 dri2_dpy->wl_queue);
574
575 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++)
576 if (dri2_surf->color_buffers[i].age > 0)
577 dri2_surf->color_buffers[i].age++;
578
579 /* Make sure we have a back buffer in case we're swapping without ever
580 * rendering. */
581 if (get_back_bo(dri2_surf) < 0) {
582 _eglError(EGL_BAD_ALLOC, "dri2_swap_buffers");
583 return EGL_FALSE;
584 }
585
586 dri2_surf->back->age = 1;
587 dri2_surf->current = dri2_surf->back;
588 dri2_surf->back = NULL;
589
590 create_wl_buffer(dri2_surf);
591
592 wl_surface_attach(dri2_surf->wl_win->surface,
593 dri2_surf->current->wl_buffer,
594 dri2_surf->dx, dri2_surf->dy);
595
596 dri2_surf->wl_win->attached_width = dri2_surf->base.Width;
597 dri2_surf->wl_win->attached_height = dri2_surf->base.Height;
598 /* reset resize growing parameters */
599 dri2_surf->dx = 0;
600 dri2_surf->dy = 0;
601
602 if (n_rects == 0) {
603 wl_surface_damage(dri2_surf->wl_win->surface,
604 0, 0, INT32_MAX, INT32_MAX);
605 } else {
606 for (i = 0; i < n_rects; i++) {
607 const int *rect = &rects[i * 4];
608 wl_surface_damage(dri2_surf->wl_win->surface,
609 rect[0],
610 dri2_surf->base.Height - rect[1] - rect[3],
611 rect[2], rect[3]);
612 }
613 }
614
615 if (dri2_dpy->flush->base.version >= 4) {
616 ctx = _eglGetCurrentContext();
617 dri2_ctx = dri2_egl_context(ctx);
618 (*dri2_dpy->flush->flush_with_flags)(dri2_ctx->dri_context,
619 dri2_surf->dri_drawable,
620 __DRI2_FLUSH_DRAWABLE,
621 __DRI2_THROTTLE_SWAPBUFFER);
622 } else {
623 (*dri2_dpy->flush->flush)(dri2_surf->dri_drawable);
624 }
625
626 (*dri2_dpy->flush->invalidate)(dri2_surf->dri_drawable);
627
628 wl_surface_commit(dri2_surf->wl_win->surface);
629 wl_display_flush(dri2_dpy->wl_dpy);
630
631 return EGL_TRUE;
632 }
633
634 static EGLint
635 dri2_query_buffer_age(_EGLDriver *drv,
636 _EGLDisplay *disp, _EGLSurface *surface)
637 {
638 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surface);
639
640 if (get_back_bo(dri2_surf) < 0) {
641 _eglError(EGL_BAD_ALLOC, "dri2_query_buffer_age");
642 return 0;
643 }
644
645 return dri2_surf->back->age;
646 }
647
648 static EGLBoolean
649 dri2_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
650 {
651 return dri2_swap_buffers_with_damage (drv, disp, draw, NULL, 0);
652 }
653
654 static struct wl_buffer *
655 dri2_create_wayland_buffer_from_image_wl(_EGLDriver *drv,
656 _EGLDisplay *disp,
657 _EGLImage *img)
658 {
659 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
660 struct dri2_egl_image *dri2_img = dri2_egl_image(img);
661 __DRIimage *image = dri2_img->dri_image;
662 struct wl_buffer *buffer;
663 int width, height, format, pitch;
664 enum wl_drm_format wl_format;
665
666 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_FORMAT, &format);
667
668 switch (format) {
669 case __DRI_IMAGE_FORMAT_ARGB8888:
670 if (!(dri2_dpy->formats & HAS_ARGB8888))
671 goto bad_format;
672 wl_format = WL_DRM_FORMAT_ARGB8888;
673 break;
674 case __DRI_IMAGE_FORMAT_XRGB8888:
675 if (!(dri2_dpy->formats & HAS_XRGB8888))
676 goto bad_format;
677 wl_format = WL_DRM_FORMAT_XRGB8888;
678 break;
679 default:
680 goto bad_format;
681 }
682
683 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_WIDTH, &width);
684 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_HEIGHT, &height);
685 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_STRIDE, &pitch);
686
687 if (dri2_dpy->capabilities & WL_DRM_CAPABILITY_PRIME) {
688 int fd;
689
690 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_FD, &fd);
691
692 buffer =
693 wl_drm_create_prime_buffer(dri2_dpy->wl_drm,
694 fd,
695 width, height,
696 wl_format,
697 0, pitch,
698 0, 0,
699 0, 0);
700
701 close(fd);
702 } else {
703 int name;
704
705 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_NAME, &name);
706
707 buffer =
708 wl_drm_create_buffer(dri2_dpy->wl_drm,
709 name,
710 width, height,
711 pitch,
712 wl_format);
713 }
714
715 /* The buffer object will have been created with our internal event queue
716 * because it is using the wl_drm object as a proxy factory. We want the
717 * buffer to be used by the application so we'll reset it to the display's
718 * default event queue */
719 if (buffer)
720 wl_proxy_set_queue((struct wl_proxy *) buffer, NULL);
721
722 return buffer;
723
724 bad_format:
725 _eglError(EGL_BAD_MATCH, "unsupported image format");
726 return NULL;
727 }
728
729 static int
730 dri2_wayland_authenticate(_EGLDisplay *disp, uint32_t id)
731 {
732 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
733 int ret = 0;
734
735 dri2_dpy->authenticated = 0;
736
737 wl_drm_authenticate(dri2_dpy->wl_drm, id);
738 if (roundtrip(dri2_dpy) < 0)
739 ret = -1;
740
741 if (!dri2_dpy->authenticated)
742 ret = -1;
743
744 /* reset authenticated */
745 dri2_dpy->authenticated = 1;
746
747 return ret;
748 }
749
750 /**
751 * Called via eglTerminate(), drv->API.Terminate().
752 */
753 static EGLBoolean
754 dri2_terminate(_EGLDriver *drv, _EGLDisplay *disp)
755 {
756 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
757
758 _eglReleaseDisplayResources(drv, disp);
759 _eglCleanupDisplay(disp);
760
761 dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen);
762 close(dri2_dpy->fd);
763 dlclose(dri2_dpy->driver);
764 free(dri2_dpy->driver_name);
765 free(dri2_dpy->device_name);
766 wl_drm_destroy(dri2_dpy->wl_drm);
767 if (dri2_dpy->own_device)
768 wl_display_disconnect(dri2_dpy->wl_dpy);
769 free(dri2_dpy);
770 disp->DriverData = NULL;
771
772 return EGL_TRUE;
773 }
774
775 static void
776 drm_handle_device(void *data, struct wl_drm *drm, const char *device)
777 {
778 struct dri2_egl_display *dri2_dpy = data;
779 drm_magic_t magic;
780
781 dri2_dpy->device_name = strdup(device);
782 if (!dri2_dpy->device_name)
783 return;
784
785 #ifdef O_CLOEXEC
786 dri2_dpy->fd = open(dri2_dpy->device_name, O_RDWR | O_CLOEXEC);
787 if (dri2_dpy->fd == -1 && errno == EINVAL)
788 #endif
789 {
790 dri2_dpy->fd = open(dri2_dpy->device_name, O_RDWR);
791 if (dri2_dpy->fd != -1)
792 fcntl(dri2_dpy->fd, F_SETFD, fcntl(dri2_dpy->fd, F_GETFD) |
793 FD_CLOEXEC);
794 }
795 if (dri2_dpy->fd == -1) {
796 _eglLog(_EGL_WARNING, "wayland-egl: could not open %s (%s)",
797 dri2_dpy->device_name, strerror(errno));
798 return;
799 }
800
801 drmGetMagic(dri2_dpy->fd, &magic);
802 wl_drm_authenticate(dri2_dpy->wl_drm, magic);
803 }
804
805 static void
806 drm_handle_format(void *data, struct wl_drm *drm, uint32_t format)
807 {
808 struct dri2_egl_display *dri2_dpy = data;
809
810 switch (format) {
811 case WL_DRM_FORMAT_ARGB8888:
812 dri2_dpy->formats |= HAS_ARGB8888;
813 break;
814 case WL_DRM_FORMAT_XRGB8888:
815 dri2_dpy->formats |= HAS_XRGB8888;
816 break;
817 case WL_DRM_FORMAT_RGB565:
818 dri2_dpy->formats |= HAS_RGB565;
819 break;
820 }
821 }
822
823 static void
824 drm_handle_capabilities(void *data, struct wl_drm *drm, uint32_t value)
825 {
826 struct dri2_egl_display *dri2_dpy = data;
827
828 dri2_dpy->capabilities = value;
829 }
830
831 static void
832 drm_handle_authenticated(void *data, struct wl_drm *drm)
833 {
834 struct dri2_egl_display *dri2_dpy = data;
835
836 dri2_dpy->authenticated = 1;
837 }
838
839 static const struct wl_drm_listener drm_listener = {
840 drm_handle_device,
841 drm_handle_format,
842 drm_handle_authenticated,
843 drm_handle_capabilities
844 };
845
846 static void
847 registry_handle_global(void *data, struct wl_registry *registry, uint32_t name,
848 const char *interface, uint32_t version)
849 {
850 struct dri2_egl_display *dri2_dpy = data;
851
852 if (version > 1)
853 version = 2;
854 if (strcmp(interface, "wl_drm") == 0) {
855 dri2_dpy->wl_drm =
856 wl_registry_bind(registry, name, &wl_drm_interface, version);
857 wl_drm_add_listener(dri2_dpy->wl_drm, &drm_listener, dri2_dpy);
858 }
859 }
860
861 static void
862 registry_handle_global_remove(void *data, struct wl_registry *registry,
863 uint32_t name)
864 {
865 }
866
867 static const struct wl_registry_listener registry_listener = {
868 registry_handle_global,
869 registry_handle_global_remove
870 };
871
872 EGLBoolean
873 dri2_initialize_wayland(_EGLDriver *drv, _EGLDisplay *disp)
874 {
875 struct dri2_egl_display *dri2_dpy;
876 const __DRIconfig *config;
877 uint32_t types;
878 int i;
879 static const unsigned int argb_masks[4] =
880 { 0xff0000, 0xff00, 0xff, 0xff000000 };
881 static const unsigned int rgb_masks[4] = { 0xff0000, 0xff00, 0xff, 0 };
882 static const unsigned int rgb565_masks[4] = { 0xf800, 0x07e0, 0x001f, 0 };
883
884 drv->API.CreateWindowSurface = dri2_create_window_surface;
885 drv->API.DestroySurface = dri2_destroy_surface;
886 drv->API.SwapBuffers = dri2_swap_buffers;
887 drv->API.SwapBuffersWithDamageEXT = dri2_swap_buffers_with_damage;
888 drv->API.Terminate = dri2_terminate;
889 drv->API.QueryBufferAge = dri2_query_buffer_age;
890 drv->API.CreateWaylandBufferFromImageWL =
891 dri2_create_wayland_buffer_from_image_wl;
892
893 dri2_dpy = calloc(1, sizeof *dri2_dpy);
894 if (!dri2_dpy)
895 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
896
897 disp->DriverData = (void *) dri2_dpy;
898 if (disp->PlatformDisplay == NULL) {
899 dri2_dpy->wl_dpy = wl_display_connect(NULL);
900 if (dri2_dpy->wl_dpy == NULL)
901 goto cleanup_dpy;
902 dri2_dpy->own_device = 1;
903 } else {
904 dri2_dpy->wl_dpy = disp->PlatformDisplay;
905 }
906
907 dri2_dpy->wl_queue = wl_display_create_queue(dri2_dpy->wl_dpy);
908
909 if (dri2_dpy->own_device)
910 wl_display_dispatch_pending(dri2_dpy->wl_dpy);
911
912 dri2_dpy->wl_registry = wl_display_get_registry(dri2_dpy->wl_dpy);
913 wl_proxy_set_queue((struct wl_proxy *) dri2_dpy->wl_registry,
914 dri2_dpy->wl_queue);
915 wl_registry_add_listener(dri2_dpy->wl_registry,
916 &registry_listener, dri2_dpy);
917 if (roundtrip(dri2_dpy) < 0 || dri2_dpy->wl_drm == NULL)
918 goto cleanup_dpy;
919
920 if (roundtrip(dri2_dpy) < 0 || dri2_dpy->fd == -1)
921 goto cleanup_drm;
922
923 if (roundtrip(dri2_dpy) < 0 || !dri2_dpy->authenticated)
924 goto cleanup_fd;
925
926 dri2_dpy->driver_name = dri2_get_driver_for_fd(dri2_dpy->fd);
927 if (dri2_dpy->driver_name == NULL) {
928 _eglError(EGL_BAD_ALLOC, "DRI2: failed to get driver name");
929 goto cleanup_fd;
930 }
931
932 if (!dri2_load_driver(disp))
933 goto cleanup_driver_name;
934
935 dri2_dpy->dri2_loader_extension.base.name = __DRI_DRI2_LOADER;
936 dri2_dpy->dri2_loader_extension.base.version = 3;
937 dri2_dpy->dri2_loader_extension.getBuffers = dri2_get_buffers;
938 dri2_dpy->dri2_loader_extension.flushFrontBuffer = dri2_flush_front_buffer;
939 dri2_dpy->dri2_loader_extension.getBuffersWithFormat =
940 dri2_get_buffers_with_format;
941
942 dri2_dpy->extensions[0] = &dri2_dpy->dri2_loader_extension.base;
943 dri2_dpy->extensions[1] = &image_loader_extension.base;
944 dri2_dpy->extensions[2] = &image_lookup_extension.base;
945 dri2_dpy->extensions[3] = &use_invalidate.base;
946 dri2_dpy->extensions[4] = NULL;
947
948 if (!dri2_create_screen(disp))
949 goto cleanup_driver;
950
951 /* The server shouldn't advertise WL_DRM_CAPABILITY_PRIME if the driver
952 * doesn't have createImageFromFds, since we're using the same driver on
953 * both sides. We don't want crash if that happens anyway, so fall back to
954 * gem names if we don't have prime support. */
955
956 if (dri2_dpy->image->base.version < 7 ||
957 dri2_dpy->image->createImageFromFds == NULL)
958 dri2_dpy->capabilities &= WL_DRM_CAPABILITY_PRIME;
959
960 types = EGL_WINDOW_BIT;
961 for (i = 0; dri2_dpy->driver_configs[i]; i++) {
962 config = dri2_dpy->driver_configs[i];
963 if (dri2_dpy->formats & HAS_XRGB8888)
964 dri2_add_config(disp, config, i + 1, types, NULL, rgb_masks);
965 if (dri2_dpy->formats & HAS_ARGB8888)
966 dri2_add_config(disp, config, i + 1, types, NULL, argb_masks);
967 if (dri2_dpy->formats & HAS_RGB565)
968 dri2_add_config(disp, config, i + 1, types, NULL, rgb565_masks);
969 }
970
971 disp->Extensions.WL_bind_wayland_display = EGL_TRUE;
972 disp->Extensions.WL_create_wayland_buffer_from_image = EGL_TRUE;
973 disp->Extensions.EXT_buffer_age = EGL_TRUE;
974 dri2_dpy->authenticate = dri2_wayland_authenticate;
975
976 disp->Extensions.EXT_swap_buffers_with_damage = EGL_TRUE;
977
978 /* we're supporting EGL 1.4 */
979 disp->VersionMajor = 1;
980 disp->VersionMinor = 4;
981
982 return EGL_TRUE;
983
984 cleanup_driver:
985 dlclose(dri2_dpy->driver);
986 cleanup_driver_name:
987 free(dri2_dpy->driver_name);
988 cleanup_fd:
989 close(dri2_dpy->fd);
990 cleanup_drm:
991 free(dri2_dpy->device_name);
992 wl_drm_destroy(dri2_dpy->wl_drm);
993 cleanup_dpy:
994 free(dri2_dpy);
995
996 return EGL_FALSE;
997 }