Enable throttling in SwapBuffers
[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, 0, 0,
604 dri2_surf->base.Width, dri2_surf->base.Height);
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 int
655 dri2_wayland_authenticate(_EGLDisplay *disp, uint32_t id)
656 {
657 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
658 int ret = 0;
659
660 dri2_dpy->authenticated = 0;
661
662 wl_drm_authenticate(dri2_dpy->wl_drm, id);
663 if (roundtrip(dri2_dpy) < 0)
664 ret = -1;
665
666 if (!dri2_dpy->authenticated)
667 ret = -1;
668
669 /* reset authenticated */
670 dri2_dpy->authenticated = 1;
671
672 return ret;
673 }
674
675 /**
676 * Called via eglTerminate(), drv->API.Terminate().
677 */
678 static EGLBoolean
679 dri2_terminate(_EGLDriver *drv, _EGLDisplay *disp)
680 {
681 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
682
683 _eglReleaseDisplayResources(drv, disp);
684 _eglCleanupDisplay(disp);
685
686 dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen);
687 close(dri2_dpy->fd);
688 dlclose(dri2_dpy->driver);
689 free(dri2_dpy->driver_name);
690 free(dri2_dpy->device_name);
691 wl_drm_destroy(dri2_dpy->wl_drm);
692 if (dri2_dpy->own_device)
693 wl_display_disconnect(dri2_dpy->wl_dpy);
694 free(dri2_dpy);
695 disp->DriverData = NULL;
696
697 return EGL_TRUE;
698 }
699
700 static void
701 drm_handle_device(void *data, struct wl_drm *drm, const char *device)
702 {
703 struct dri2_egl_display *dri2_dpy = data;
704 drm_magic_t magic;
705
706 dri2_dpy->device_name = strdup(device);
707 if (!dri2_dpy->device_name)
708 return;
709
710 #ifdef O_CLOEXEC
711 dri2_dpy->fd = open(dri2_dpy->device_name, O_RDWR | O_CLOEXEC);
712 if (dri2_dpy->fd == -1 && errno == EINVAL)
713 #endif
714 {
715 dri2_dpy->fd = open(dri2_dpy->device_name, O_RDWR);
716 if (dri2_dpy->fd != -1)
717 fcntl(dri2_dpy->fd, F_SETFD, fcntl(dri2_dpy->fd, F_GETFD) |
718 FD_CLOEXEC);
719 }
720 if (dri2_dpy->fd == -1) {
721 _eglLog(_EGL_WARNING, "wayland-egl: could not open %s (%s)",
722 dri2_dpy->device_name, strerror(errno));
723 return;
724 }
725
726 drmGetMagic(dri2_dpy->fd, &magic);
727 wl_drm_authenticate(dri2_dpy->wl_drm, magic);
728 }
729
730 static void
731 drm_handle_format(void *data, struct wl_drm *drm, uint32_t format)
732 {
733 struct dri2_egl_display *dri2_dpy = data;
734
735 switch (format) {
736 case WL_DRM_FORMAT_ARGB8888:
737 dri2_dpy->formats |= HAS_ARGB8888;
738 break;
739 case WL_DRM_FORMAT_XRGB8888:
740 dri2_dpy->formats |= HAS_XRGB8888;
741 break;
742 case WL_DRM_FORMAT_RGB565:
743 dri2_dpy->formats |= HAS_RGB565;
744 break;
745 }
746 }
747
748 static void
749 drm_handle_capabilities(void *data, struct wl_drm *drm, uint32_t value)
750 {
751 struct dri2_egl_display *dri2_dpy = data;
752
753 dri2_dpy->capabilities = value;
754 }
755
756 static void
757 drm_handle_authenticated(void *data, struct wl_drm *drm)
758 {
759 struct dri2_egl_display *dri2_dpy = data;
760
761 dri2_dpy->authenticated = 1;
762 }
763
764 static const struct wl_drm_listener drm_listener = {
765 drm_handle_device,
766 drm_handle_format,
767 drm_handle_authenticated,
768 drm_handle_capabilities
769 };
770
771 static void
772 registry_handle_global(void *data, struct wl_registry *registry, uint32_t name,
773 const char *interface, uint32_t version)
774 {
775 struct dri2_egl_display *dri2_dpy = data;
776
777 if (version > 1)
778 version = 2;
779 if (strcmp(interface, "wl_drm") == 0) {
780 dri2_dpy->wl_drm =
781 wl_registry_bind(registry, name, &wl_drm_interface, version);
782 wl_drm_add_listener(dri2_dpy->wl_drm, &drm_listener, dri2_dpy);
783 }
784 }
785
786 static void
787 registry_handle_global_remove(void *data, struct wl_registry *registry,
788 uint32_t name)
789 {
790 }
791
792 static const struct wl_registry_listener registry_listener = {
793 registry_handle_global,
794 registry_handle_global_remove
795 };
796
797 EGLBoolean
798 dri2_initialize_wayland(_EGLDriver *drv, _EGLDisplay *disp)
799 {
800 struct dri2_egl_display *dri2_dpy;
801 const __DRIconfig *config;
802 uint32_t types;
803 int i;
804 static const unsigned int argb_masks[4] =
805 { 0xff0000, 0xff00, 0xff, 0xff000000 };
806 static const unsigned int rgb_masks[4] = { 0xff0000, 0xff00, 0xff, 0 };
807 static const unsigned int rgb565_masks[4] = { 0xf800, 0x07e0, 0x001f, 0 };
808
809 drv->API.CreateWindowSurface = dri2_create_window_surface;
810 drv->API.DestroySurface = dri2_destroy_surface;
811 drv->API.SwapBuffers = dri2_swap_buffers;
812 drv->API.SwapBuffersWithDamageEXT = dri2_swap_buffers_with_damage;
813 drv->API.Terminate = dri2_terminate;
814 drv->API.QueryBufferAge = dri2_query_buffer_age;
815
816 dri2_dpy = calloc(1, sizeof *dri2_dpy);
817 if (!dri2_dpy)
818 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
819
820 disp->DriverData = (void *) dri2_dpy;
821 if (disp->PlatformDisplay == NULL) {
822 dri2_dpy->wl_dpy = wl_display_connect(NULL);
823 if (dri2_dpy->wl_dpy == NULL)
824 goto cleanup_dpy;
825 dri2_dpy->own_device = 1;
826 } else {
827 dri2_dpy->wl_dpy = disp->PlatformDisplay;
828 }
829
830 dri2_dpy->wl_queue = wl_display_create_queue(dri2_dpy->wl_dpy);
831
832 if (dri2_dpy->own_device)
833 wl_display_dispatch_pending(dri2_dpy->wl_dpy);
834
835 dri2_dpy->wl_registry = wl_display_get_registry(dri2_dpy->wl_dpy);
836 wl_proxy_set_queue((struct wl_proxy *) dri2_dpy->wl_registry,
837 dri2_dpy->wl_queue);
838 wl_registry_add_listener(dri2_dpy->wl_registry,
839 &registry_listener, dri2_dpy);
840 if (roundtrip(dri2_dpy) < 0 || dri2_dpy->wl_drm == NULL)
841 goto cleanup_dpy;
842
843 if (roundtrip(dri2_dpy) < 0 || dri2_dpy->fd == -1)
844 goto cleanup_drm;
845
846 if (roundtrip(dri2_dpy) < 0 || !dri2_dpy->authenticated)
847 goto cleanup_fd;
848
849 dri2_dpy->driver_name = dri2_get_driver_for_fd(dri2_dpy->fd);
850 if (dri2_dpy->driver_name == NULL) {
851 _eglError(EGL_BAD_ALLOC, "DRI2: failed to get driver name");
852 goto cleanup_fd;
853 }
854
855 if (!dri2_load_driver(disp))
856 goto cleanup_driver_name;
857
858 dri2_dpy->dri2_loader_extension.base.name = __DRI_DRI2_LOADER;
859 dri2_dpy->dri2_loader_extension.base.version = 3;
860 dri2_dpy->dri2_loader_extension.getBuffers = dri2_get_buffers;
861 dri2_dpy->dri2_loader_extension.flushFrontBuffer = dri2_flush_front_buffer;
862 dri2_dpy->dri2_loader_extension.getBuffersWithFormat =
863 dri2_get_buffers_with_format;
864
865 dri2_dpy->extensions[0] = &dri2_dpy->dri2_loader_extension.base;
866 dri2_dpy->extensions[1] = &image_loader_extension.base;
867 dri2_dpy->extensions[2] = &image_lookup_extension.base;
868 dri2_dpy->extensions[3] = &use_invalidate.base;
869 dri2_dpy->extensions[4] = NULL;
870
871 if (!dri2_create_screen(disp))
872 goto cleanup_driver;
873
874 /* The server shouldn't advertise WL_DRM_CAPABILITY_PRIME if the driver
875 * doesn't have createImageFromFds, since we're using the same driver on
876 * both sides. We don't want crash if that happens anyway, so fall back to
877 * gem names if we don't have prime support. */
878
879 if (dri2_dpy->image->base.version < 7 ||
880 dri2_dpy->image->createImageFromFds == NULL)
881 dri2_dpy->capabilities &= WL_DRM_CAPABILITY_PRIME;
882
883 types = EGL_WINDOW_BIT;
884 for (i = 0; dri2_dpy->driver_configs[i]; i++) {
885 config = dri2_dpy->driver_configs[i];
886 if (dri2_dpy->formats & HAS_XRGB8888)
887 dri2_add_config(disp, config, i + 1, types, NULL, rgb_masks);
888 if (dri2_dpy->formats & HAS_ARGB8888)
889 dri2_add_config(disp, config, i + 1, types, NULL, argb_masks);
890 if (dri2_dpy->formats & HAS_RGB565)
891 dri2_add_config(disp, config, i + 1, types, NULL, rgb565_masks);
892 }
893
894 disp->Extensions.WL_bind_wayland_display = EGL_TRUE;
895 disp->Extensions.EXT_buffer_age = EGL_TRUE;
896 dri2_dpy->authenticate = dri2_wayland_authenticate;
897
898 disp->Extensions.EXT_swap_buffers_with_damage = EGL_TRUE;
899
900 /* we're supporting EGL 1.4 */
901 disp->VersionMajor = 1;
902 disp->VersionMinor = 4;
903
904 return EGL_TRUE;
905
906 cleanup_driver:
907 dlclose(dri2_dpy->driver);
908 cleanup_driver_name:
909 free(dri2_dpy->driver_name);
910 cleanup_fd:
911 close(dri2_dpy->fd);
912 cleanup_drm:
913 free(dri2_dpy->device_name);
914 wl_drm_destroy(dri2_dpy->wl_drm);
915 cleanup_dpy:
916 free(dri2_dpy);
917
918 return EGL_FALSE;
919 }