egl/wayland: Flush the wl_display at the end of 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 int i, ret = 0;
561
562 while (dri2_surf->frame_callback && ret != -1)
563 ret = wl_display_dispatch_queue(dri2_dpy->wl_dpy, dri2_dpy->wl_queue);
564 if (ret < 0)
565 return EGL_FALSE;
566
567 dri2_surf->frame_callback = wl_surface_frame(dri2_surf->wl_win->surface);
568 wl_callback_add_listener(dri2_surf->frame_callback,
569 &frame_listener, dri2_surf);
570 wl_proxy_set_queue((struct wl_proxy *) dri2_surf->frame_callback,
571 dri2_dpy->wl_queue);
572
573 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++)
574 if (dri2_surf->color_buffers[i].age > 0)
575 dri2_surf->color_buffers[i].age++;
576
577 /* Make sure we have a back buffer in case we're swapping without ever
578 * rendering. */
579 if (get_back_bo(dri2_surf) < 0) {
580 _eglError(EGL_BAD_ALLOC, "dri2_swap_buffers");
581 return EGL_FALSE;
582 }
583
584 dri2_surf->back->age = 1;
585 dri2_surf->current = dri2_surf->back;
586 dri2_surf->back = NULL;
587
588 create_wl_buffer(dri2_surf);
589
590 wl_surface_attach(dri2_surf->wl_win->surface,
591 dri2_surf->current->wl_buffer,
592 dri2_surf->dx, dri2_surf->dy);
593
594 dri2_surf->wl_win->attached_width = dri2_surf->base.Width;
595 dri2_surf->wl_win->attached_height = dri2_surf->base.Height;
596 /* reset resize growing parameters */
597 dri2_surf->dx = 0;
598 dri2_surf->dy = 0;
599
600 if (n_rects == 0) {
601 wl_surface_damage(dri2_surf->wl_win->surface, 0, 0,
602 dri2_surf->base.Width, dri2_surf->base.Height);
603 } else {
604 for (i = 0; i < n_rects; i++) {
605 const int *rect = &rects[i * 4];
606 wl_surface_damage(dri2_surf->wl_win->surface,
607 rect[0],
608 dri2_surf->base.Height - rect[1] - rect[3],
609 rect[2], rect[3]);
610 }
611 }
612
613 wl_surface_commit(dri2_surf->wl_win->surface);
614
615 (*dri2_dpy->flush->flush)(dri2_surf->dri_drawable);
616 (*dri2_dpy->flush->invalidate)(dri2_surf->dri_drawable);
617
618 wl_display_flush(dri2_dpy->wl_dpy);
619
620 return EGL_TRUE;
621 }
622
623 static EGLint
624 dri2_query_buffer_age(_EGLDriver *drv,
625 _EGLDisplay *disp, _EGLSurface *surface)
626 {
627 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surface);
628
629 if (get_back_bo(dri2_surf) < 0) {
630 _eglError(EGL_BAD_ALLOC, "dri2_query_buffer_age");
631 return 0;
632 }
633
634 return dri2_surf->back->age;
635 }
636
637 static EGLBoolean
638 dri2_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
639 {
640 return dri2_swap_buffers_with_damage (drv, disp, draw, NULL, 0);
641 }
642
643 static int
644 dri2_wayland_authenticate(_EGLDisplay *disp, uint32_t id)
645 {
646 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
647 int ret = 0;
648
649 dri2_dpy->authenticated = 0;
650
651 wl_drm_authenticate(dri2_dpy->wl_drm, id);
652 if (roundtrip(dri2_dpy) < 0)
653 ret = -1;
654
655 if (!dri2_dpy->authenticated)
656 ret = -1;
657
658 /* reset authenticated */
659 dri2_dpy->authenticated = 1;
660
661 return ret;
662 }
663
664 /**
665 * Called via eglTerminate(), drv->API.Terminate().
666 */
667 static EGLBoolean
668 dri2_terminate(_EGLDriver *drv, _EGLDisplay *disp)
669 {
670 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
671
672 _eglReleaseDisplayResources(drv, disp);
673 _eglCleanupDisplay(disp);
674
675 dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen);
676 close(dri2_dpy->fd);
677 dlclose(dri2_dpy->driver);
678 free(dri2_dpy->driver_name);
679 free(dri2_dpy->device_name);
680 wl_drm_destroy(dri2_dpy->wl_drm);
681 if (dri2_dpy->own_device)
682 wl_display_disconnect(dri2_dpy->wl_dpy);
683 free(dri2_dpy);
684 disp->DriverData = NULL;
685
686 return EGL_TRUE;
687 }
688
689 static void
690 drm_handle_device(void *data, struct wl_drm *drm, const char *device)
691 {
692 struct dri2_egl_display *dri2_dpy = data;
693 drm_magic_t magic;
694
695 dri2_dpy->device_name = strdup(device);
696 if (!dri2_dpy->device_name)
697 return;
698
699 #ifdef O_CLOEXEC
700 dri2_dpy->fd = open(dri2_dpy->device_name, O_RDWR | O_CLOEXEC);
701 if (dri2_dpy->fd == -1 && errno == EINVAL)
702 #endif
703 {
704 dri2_dpy->fd = open(dri2_dpy->device_name, O_RDWR);
705 if (dri2_dpy->fd != -1)
706 fcntl(dri2_dpy->fd, F_SETFD, fcntl(dri2_dpy->fd, F_GETFD) |
707 FD_CLOEXEC);
708 }
709 if (dri2_dpy->fd == -1) {
710 _eglLog(_EGL_WARNING, "wayland-egl: could not open %s (%s)",
711 dri2_dpy->device_name, strerror(errno));
712 return;
713 }
714
715 drmGetMagic(dri2_dpy->fd, &magic);
716 wl_drm_authenticate(dri2_dpy->wl_drm, magic);
717 }
718
719 static void
720 drm_handle_format(void *data, struct wl_drm *drm, uint32_t format)
721 {
722 struct dri2_egl_display *dri2_dpy = data;
723
724 switch (format) {
725 case WL_DRM_FORMAT_ARGB8888:
726 dri2_dpy->formats |= HAS_ARGB8888;
727 break;
728 case WL_DRM_FORMAT_XRGB8888:
729 dri2_dpy->formats |= HAS_XRGB8888;
730 break;
731 case WL_DRM_FORMAT_RGB565:
732 dri2_dpy->formats |= HAS_RGB565;
733 break;
734 }
735 }
736
737 static void
738 drm_handle_capabilities(void *data, struct wl_drm *drm, uint32_t value)
739 {
740 struct dri2_egl_display *dri2_dpy = data;
741
742 dri2_dpy->capabilities = value;
743 }
744
745 static void
746 drm_handle_authenticated(void *data, struct wl_drm *drm)
747 {
748 struct dri2_egl_display *dri2_dpy = data;
749
750 dri2_dpy->authenticated = 1;
751 }
752
753 static const struct wl_drm_listener drm_listener = {
754 drm_handle_device,
755 drm_handle_format,
756 drm_handle_authenticated,
757 drm_handle_capabilities
758 };
759
760 static void
761 registry_handle_global(void *data, struct wl_registry *registry, uint32_t name,
762 const char *interface, uint32_t version)
763 {
764 struct dri2_egl_display *dri2_dpy = data;
765
766 if (version > 1)
767 version = 2;
768 if (strcmp(interface, "wl_drm") == 0) {
769 dri2_dpy->wl_drm =
770 wl_registry_bind(registry, name, &wl_drm_interface, version);
771 wl_drm_add_listener(dri2_dpy->wl_drm, &drm_listener, dri2_dpy);
772 }
773 }
774
775 static void
776 registry_handle_global_remove(void *data, struct wl_registry *registry,
777 uint32_t name)
778 {
779 }
780
781 static const struct wl_registry_listener registry_listener = {
782 registry_handle_global,
783 registry_handle_global_remove
784 };
785
786 EGLBoolean
787 dri2_initialize_wayland(_EGLDriver *drv, _EGLDisplay *disp)
788 {
789 struct dri2_egl_display *dri2_dpy;
790 const __DRIconfig *config;
791 uint32_t types;
792 int i;
793 static const unsigned int argb_masks[4] =
794 { 0xff0000, 0xff00, 0xff, 0xff000000 };
795 static const unsigned int rgb_masks[4] = { 0xff0000, 0xff00, 0xff, 0 };
796 static const unsigned int rgb565_masks[4] = { 0xf800, 0x07e0, 0x001f, 0 };
797
798 drv->API.CreateWindowSurface = dri2_create_window_surface;
799 drv->API.DestroySurface = dri2_destroy_surface;
800 drv->API.SwapBuffers = dri2_swap_buffers;
801 drv->API.SwapBuffersWithDamageEXT = dri2_swap_buffers_with_damage;
802 drv->API.Terminate = dri2_terminate;
803 drv->API.QueryBufferAge = dri2_query_buffer_age;
804
805 dri2_dpy = calloc(1, sizeof *dri2_dpy);
806 if (!dri2_dpy)
807 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
808
809 disp->DriverData = (void *) dri2_dpy;
810 if (disp->PlatformDisplay == NULL) {
811 dri2_dpy->wl_dpy = wl_display_connect(NULL);
812 if (dri2_dpy->wl_dpy == NULL)
813 goto cleanup_dpy;
814 dri2_dpy->own_device = 1;
815 } else {
816 dri2_dpy->wl_dpy = disp->PlatformDisplay;
817 }
818
819 dri2_dpy->wl_queue = wl_display_create_queue(dri2_dpy->wl_dpy);
820
821 if (dri2_dpy->own_device)
822 wl_display_dispatch_pending(dri2_dpy->wl_dpy);
823
824 dri2_dpy->wl_registry = wl_display_get_registry(dri2_dpy->wl_dpy);
825 wl_proxy_set_queue((struct wl_proxy *) dri2_dpy->wl_registry,
826 dri2_dpy->wl_queue);
827 wl_registry_add_listener(dri2_dpy->wl_registry,
828 &registry_listener, dri2_dpy);
829 if (roundtrip(dri2_dpy) < 0 || dri2_dpy->wl_drm == NULL)
830 goto cleanup_dpy;
831
832 if (roundtrip(dri2_dpy) < 0 || dri2_dpy->fd == -1)
833 goto cleanup_drm;
834
835 if (roundtrip(dri2_dpy) < 0 || !dri2_dpy->authenticated)
836 goto cleanup_fd;
837
838 dri2_dpy->driver_name = dri2_get_driver_for_fd(dri2_dpy->fd);
839 if (dri2_dpy->driver_name == NULL) {
840 _eglError(EGL_BAD_ALLOC, "DRI2: failed to get driver name");
841 goto cleanup_fd;
842 }
843
844 if (!dri2_load_driver(disp))
845 goto cleanup_driver_name;
846
847 dri2_dpy->dri2_loader_extension.base.name = __DRI_DRI2_LOADER;
848 dri2_dpy->dri2_loader_extension.base.version = 3;
849 dri2_dpy->dri2_loader_extension.getBuffers = dri2_get_buffers;
850 dri2_dpy->dri2_loader_extension.flushFrontBuffer = dri2_flush_front_buffer;
851 dri2_dpy->dri2_loader_extension.getBuffersWithFormat =
852 dri2_get_buffers_with_format;
853
854 dri2_dpy->extensions[0] = &dri2_dpy->dri2_loader_extension.base;
855 dri2_dpy->extensions[1] = &image_loader_extension.base;
856 dri2_dpy->extensions[2] = &image_lookup_extension.base;
857 dri2_dpy->extensions[3] = &use_invalidate.base;
858 dri2_dpy->extensions[4] = NULL;
859
860 if (!dri2_create_screen(disp))
861 goto cleanup_driver;
862
863 /* The server shouldn't advertise WL_DRM_CAPABILITY_PRIME if the driver
864 * doesn't have createImageFromFds, since we're using the same driver on
865 * both sides. We don't want crash if that happens anyway, so fall back to
866 * gem names if we don't have prime support. */
867
868 if (dri2_dpy->image->base.version < 7 ||
869 dri2_dpy->image->createImageFromFds == NULL)
870 dri2_dpy->capabilities &= WL_DRM_CAPABILITY_PRIME;
871
872 types = EGL_WINDOW_BIT;
873 for (i = 0; dri2_dpy->driver_configs[i]; i++) {
874 config = dri2_dpy->driver_configs[i];
875 if (dri2_dpy->formats & HAS_XRGB8888)
876 dri2_add_config(disp, config, i + 1, types, NULL, rgb_masks);
877 if (dri2_dpy->formats & HAS_ARGB8888)
878 dri2_add_config(disp, config, i + 1, types, NULL, argb_masks);
879 if (dri2_dpy->formats & HAS_RGB565)
880 dri2_add_config(disp, config, i + 1, types, NULL, rgb565_masks);
881 }
882
883 disp->Extensions.WL_bind_wayland_display = EGL_TRUE;
884 disp->Extensions.EXT_buffer_age = EGL_TRUE;
885 dri2_dpy->authenticate = dri2_wayland_authenticate;
886
887 disp->Extensions.EXT_swap_buffers_with_damage = EGL_TRUE;
888
889 /* we're supporting EGL 1.4 */
890 disp->VersionMajor = 1;
891 disp->VersionMinor = 4;
892
893 return EGL_TRUE;
894
895 cleanup_driver:
896 dlclose(dri2_dpy->driver);
897 cleanup_driver_name:
898 free(dri2_dpy->driver_name);
899 cleanup_fd:
900 close(dri2_dpy->fd);
901 cleanup_drm:
902 free(dri2_dpy->device_name);
903 wl_drm_destroy(dri2_dpy->wl_drm);
904 cleanup_dpy:
905 free(dri2_dpy);
906
907 return EGL_FALSE;
908 }