740fc7d4e0ff21e2068e131260af7d7553905797
[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 };
47
48 static void
49 sync_callback(void *data, struct wl_callback *callback, uint32_t serial)
50 {
51 int *done = data;
52
53 *done = 1;
54 wl_callback_destroy(callback);
55 }
56
57 static const struct wl_callback_listener sync_listener = {
58 sync_callback
59 };
60
61 static int
62 roundtrip(struct dri2_egl_display *dri2_dpy)
63 {
64 struct wl_callback *callback;
65 int done = 0, ret = 0;
66
67 callback = wl_display_sync(dri2_dpy->wl_dpy);
68 wl_callback_add_listener(callback, &sync_listener, &done);
69 wl_proxy_set_queue((struct wl_proxy *) callback, dri2_dpy->wl_queue);
70 while (ret != -1 && !done)
71 ret = wl_display_dispatch_queue(dri2_dpy->wl_dpy, dri2_dpy->wl_queue);
72
73 if (!done)
74 wl_callback_destroy(callback);
75
76 return ret;
77 }
78
79 static void
80 wl_buffer_release(void *data, struct wl_buffer *buffer)
81 {
82 struct dri2_egl_surface *dri2_surf = data;
83 int i;
84
85 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); ++i)
86 if (dri2_surf->color_buffers[i].wl_buffer == buffer)
87 break;
88
89 if (i == ARRAY_SIZE(dri2_surf->color_buffers)) {
90 wl_buffer_destroy(buffer);
91 return;
92 }
93
94 dri2_surf->color_buffers[i].locked = 0;
95 }
96
97 static struct wl_buffer_listener wl_buffer_listener = {
98 wl_buffer_release
99 };
100
101 static void
102 resize_callback(struct wl_egl_window *wl_win, void *data)
103 {
104 struct dri2_egl_surface *dri2_surf = data;
105 struct dri2_egl_display *dri2_dpy =
106 dri2_egl_display(dri2_surf->base.Resource.Display);
107
108 (*dri2_dpy->flush->invalidate)(dri2_surf->dri_drawable);
109 }
110
111 /**
112 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
113 */
114 static _EGLSurface *
115 dri2_create_surface(_EGLDriver *drv, _EGLDisplay *disp, EGLint type,
116 _EGLConfig *conf, EGLNativeWindowType window,
117 const EGLint *attrib_list)
118 {
119 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
120 struct dri2_egl_config *dri2_conf = dri2_egl_config(conf);
121 struct dri2_egl_surface *dri2_surf;
122
123 (void) drv;
124
125 dri2_surf = malloc(sizeof *dri2_surf);
126 if (!dri2_surf) {
127 _eglError(EGL_BAD_ALLOC, "dri2_create_surface");
128 return NULL;
129 }
130
131 memset(dri2_surf, 0, sizeof *dri2_surf);
132 if (!_eglInitSurface(&dri2_surf->base, disp, type, conf, attrib_list))
133 goto cleanup_surf;
134
135 if (conf->AlphaSize == 0)
136 dri2_surf->format = WL_DRM_FORMAT_XRGB8888;
137 else
138 dri2_surf->format = WL_DRM_FORMAT_ARGB8888;
139
140 switch (type) {
141 case EGL_WINDOW_BIT:
142 dri2_surf->wl_win = (struct wl_egl_window *) window;
143
144 dri2_surf->wl_win->private = dri2_surf;
145 dri2_surf->wl_win->resize_callback = resize_callback;
146
147 dri2_surf->base.Width = -1;
148 dri2_surf->base.Height = -1;
149 break;
150 default:
151 goto cleanup_surf;
152 }
153
154 dri2_surf->dri_drawable =
155 (*dri2_dpy->dri2->createNewDrawable) (dri2_dpy->dri_screen,
156 type == EGL_WINDOW_BIT ?
157 dri2_conf->dri_double_config :
158 dri2_conf->dri_single_config,
159 dri2_surf);
160 if (dri2_surf->dri_drawable == NULL) {
161 _eglError(EGL_BAD_ALLOC, "dri2->createNewDrawable");
162 goto cleanup_dri_drawable;
163 }
164
165 return &dri2_surf->base;
166
167 cleanup_dri_drawable:
168 dri2_dpy->core->destroyDrawable(dri2_surf->dri_drawable);
169 cleanup_surf:
170 free(dri2_surf);
171
172 return NULL;
173 }
174
175 /**
176 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
177 */
178 static _EGLSurface *
179 dri2_create_window_surface(_EGLDriver *drv, _EGLDisplay *disp,
180 _EGLConfig *conf, EGLNativeWindowType window,
181 const EGLint *attrib_list)
182 {
183 return dri2_create_surface(drv, disp, EGL_WINDOW_BIT, conf,
184 window, attrib_list);
185 }
186
187 /**
188 * Called via eglDestroySurface(), drv->API.DestroySurface().
189 */
190 static EGLBoolean
191 dri2_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
192 {
193 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
194 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
195 int i;
196
197 (void) drv;
198
199 if (!_eglPutSurface(surf))
200 return EGL_TRUE;
201
202 (*dri2_dpy->core->destroyDrawable)(dri2_surf->dri_drawable);
203
204 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
205 if (dri2_surf->color_buffers[i].wl_buffer)
206 wl_buffer_destroy(dri2_surf->color_buffers[i].wl_buffer);
207 if (dri2_surf->color_buffers[i].dri_image)
208 dri2_dpy->image->destroyImage(dri2_surf->color_buffers[i].dri_image);
209 }
210
211 for (i = 0; i < __DRI_BUFFER_COUNT; i++)
212 if (dri2_surf->dri_buffers[i] &&
213 dri2_surf->dri_buffers[i]->attachment != __DRI_BUFFER_BACK_LEFT)
214 dri2_dpy->dri2->releaseBuffer(dri2_dpy->dri_screen,
215 dri2_surf->dri_buffers[i]);
216
217 if (dri2_surf->frame_callback)
218 wl_callback_destroy(dri2_surf->frame_callback);
219
220 if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
221 dri2_surf->wl_win->private = NULL;
222 dri2_surf->wl_win->resize_callback = NULL;
223 }
224
225 free(surf);
226
227 return EGL_TRUE;
228 }
229
230 static void
231 dri2_release_buffers(struct dri2_egl_surface *dri2_surf)
232 {
233 struct dri2_egl_display *dri2_dpy =
234 dri2_egl_display(dri2_surf->base.Resource.Display);
235 int i;
236
237 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
238 if (dri2_surf->color_buffers[i].wl_buffer &&
239 !dri2_surf->color_buffers[i].locked)
240 wl_buffer_destroy(dri2_surf->color_buffers[i].wl_buffer);
241 if (dri2_surf->color_buffers[i].dri_image)
242 dri2_dpy->image->destroyImage(dri2_surf->color_buffers[i].dri_image);
243
244 dri2_surf->color_buffers[i].wl_buffer = NULL;
245 dri2_surf->color_buffers[i].dri_image = NULL;
246 dri2_surf->color_buffers[i].locked = 0;
247 }
248
249 for (i = 0; i < __DRI_BUFFER_COUNT; i++)
250 if (dri2_surf->dri_buffers[i] &&
251 dri2_surf->dri_buffers[i]->attachment != __DRI_BUFFER_BACK_LEFT)
252 dri2_dpy->dri2->releaseBuffer(dri2_dpy->dri_screen,
253 dri2_surf->dri_buffers[i]);
254 }
255
256 static int
257 get_back_bo(struct dri2_egl_surface *dri2_surf, __DRIbuffer *buffer)
258 {
259 struct dri2_egl_display *dri2_dpy =
260 dri2_egl_display(dri2_surf->base.Resource.Display);
261 __DRIimage *image;
262 int i, name, pitch;
263
264 /* There might be a buffer release already queued that wasn't processed */
265 wl_display_dispatch_queue_pending(dri2_dpy->wl_dpy, dri2_dpy->wl_queue);
266
267 if (dri2_surf->back == NULL) {
268 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
269 /* Get an unlocked buffer, preferrably one with a dri_buffer already
270 * allocated. */
271 if (dri2_surf->color_buffers[i].locked)
272 continue;
273 if (dri2_surf->back == NULL)
274 dri2_surf->back = &dri2_surf->color_buffers[i];
275 else if (dri2_surf->back->dri_image == NULL)
276 dri2_surf->back = &dri2_surf->color_buffers[i];
277 }
278 }
279
280 if (dri2_surf->back == NULL)
281 return -1;
282 if (dri2_surf->back->dri_image == NULL) {
283 dri2_surf->back->dri_image =
284 dri2_dpy->image->createImage(dri2_dpy->dri_screen,
285 dri2_surf->base.Width,
286 dri2_surf->base.Height,
287 __DRI_IMAGE_FORMAT_ARGB8888,
288 __DRI_IMAGE_USE_SHARE,
289 NULL);
290 dri2_surf->back->age = 0;
291 }
292 if (dri2_surf->back->dri_image == NULL)
293 return -1;
294
295 image = dri2_surf->back->dri_image;
296
297 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_NAME, &name);
298 dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_STRIDE, &pitch);
299
300 dri2_surf->back->name = name;
301 dri2_surf->back->pitch = pitch;
302
303 buffer->attachment = __DRI_BUFFER_BACK_LEFT;
304 buffer->name = name;
305 buffer->pitch = pitch;
306 buffer->cpp = 4;
307 buffer->flags = 0;
308
309 dri2_surf->back->locked = 1;
310
311 return 0;
312 }
313
314 static int
315 get_aux_bo(struct dri2_egl_surface *dri2_surf,
316 unsigned int attachment, unsigned int format, __DRIbuffer *buffer)
317 {
318 struct dri2_egl_display *dri2_dpy =
319 dri2_egl_display(dri2_surf->base.Resource.Display);
320 __DRIbuffer *b = dri2_surf->dri_buffers[attachment];
321
322 if (b == NULL) {
323 b = dri2_dpy->dri2->allocateBuffer(dri2_dpy->dri_screen,
324 attachment, format,
325 dri2_surf->base.Width,
326 dri2_surf->base.Height);
327 dri2_surf->dri_buffers[attachment] = b;
328 }
329 if (b == NULL)
330 return -1;
331
332 memcpy(buffer, b, sizeof *buffer);
333
334 return 0;
335 }
336
337 static __DRIbuffer *
338 dri2_get_buffers_with_format(__DRIdrawable * driDrawable,
339 int *width, int *height,
340 unsigned int *attachments, int count,
341 int *out_count, void *loaderPrivate)
342 {
343 struct dri2_egl_surface *dri2_surf = loaderPrivate;
344 struct dri2_egl_display *dri2_dpy =
345 dri2_egl_display(dri2_surf->base.Resource.Display);
346 int i, j;
347
348 if (dri2_surf->base.Type == EGL_WINDOW_BIT &&
349 (dri2_surf->base.Width != dri2_surf->wl_win->width ||
350 dri2_surf->base.Height != dri2_surf->wl_win->height)) {
351
352 dri2_release_buffers(dri2_surf);
353
354 dri2_surf->base.Width = dri2_surf->wl_win->width;
355 dri2_surf->base.Height = dri2_surf->wl_win->height;
356 dri2_surf->dx = dri2_surf->wl_win->dx;
357 dri2_surf->dy = dri2_surf->wl_win->dy;
358 }
359
360 for (i = 0, j = 0; i < 2 * count; i += 2, j++) {
361 switch (attachments[i]) {
362 case __DRI_BUFFER_BACK_LEFT:
363 if (get_back_bo(dri2_surf, &dri2_surf->buffers[j]) < 0) {
364 _eglError(EGL_BAD_ALLOC, "failed to allocate color buffer");
365 return NULL;
366 }
367 break;
368 default:
369 if (get_aux_bo(dri2_surf, attachments[i], attachments[i + 1],
370 &dri2_surf->buffers[j]) < 0) {
371 _eglError(EGL_BAD_ALLOC, "failed to allocate aux buffer");
372 return NULL;
373 }
374 break;
375 }
376 }
377
378 /* If we have an extra unlocked buffer at this point, we had to do triple
379 * buffering for a while, but now can go back to just double buffering.
380 * That means we can free any unlocked buffer now. */
381 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
382 if (!dri2_surf->color_buffers[i].locked &&
383 dri2_surf->color_buffers[i].wl_buffer) {
384 wl_buffer_destroy(dri2_surf->color_buffers[i].wl_buffer);
385 dri2_dpy->image->destroyImage(dri2_surf->color_buffers[i].dri_image);
386 dri2_surf->color_buffers[i].wl_buffer = NULL;
387 dri2_surf->color_buffers[i].dri_image = NULL;
388 }
389 }
390
391 *out_count = j;
392 if (j == 0)
393 return NULL;
394
395 *width = dri2_surf->base.Width;
396 *height = dri2_surf->base.Height;
397
398 return dri2_surf->buffers;
399 }
400
401 static __DRIbuffer *
402 dri2_get_buffers(__DRIdrawable * driDrawable,
403 int *width, int *height,
404 unsigned int *attachments, int count,
405 int *out_count, void *loaderPrivate)
406 {
407 unsigned int *attachments_with_format;
408 __DRIbuffer *buffer;
409 const unsigned int format = 32;
410 int i;
411
412 attachments_with_format = calloc(count * 2, sizeof(unsigned int));
413 if (!attachments_with_format) {
414 *out_count = 0;
415 return NULL;
416 }
417
418 for (i = 0; i < count; ++i) {
419 attachments_with_format[2*i] = attachments[i];
420 attachments_with_format[2*i + 1] = format;
421 }
422
423 buffer =
424 dri2_get_buffers_with_format(driDrawable,
425 width, height,
426 attachments_with_format, count,
427 out_count, loaderPrivate);
428
429 free(attachments_with_format);
430
431 return buffer;
432 }
433
434 static void
435 dri2_flush_front_buffer(__DRIdrawable * driDrawable, void *loaderPrivate)
436 {
437 (void) driDrawable;
438 (void) loaderPrivate;
439 }
440
441 static void
442 wayland_frame_callback(void *data, struct wl_callback *callback, uint32_t time)
443 {
444 struct dri2_egl_surface *dri2_surf = data;
445
446 dri2_surf->frame_callback = NULL;
447 wl_callback_destroy(callback);
448 }
449
450 static const struct wl_callback_listener frame_listener = {
451 wayland_frame_callback
452 };
453
454 static void
455 create_wl_buffer(struct dri2_egl_surface *dri2_surf)
456 {
457 struct dri2_egl_display *dri2_dpy =
458 dri2_egl_display(dri2_surf->base.Resource.Display);
459 int fd;
460
461 if (dri2_surf->current->wl_buffer != NULL)
462 return;
463
464 if (dri2_dpy->capabilities & WL_DRM_CAPABILITY_PRIME) {
465 dri2_dpy->image->queryImage(dri2_surf->current->dri_image,
466 __DRI_IMAGE_ATTRIB_FD, &fd);
467
468 dri2_surf->current->wl_buffer =
469 wl_drm_create_prime_buffer(dri2_dpy->wl_drm,
470 fd,
471 dri2_surf->base.Width,
472 dri2_surf->base.Height,
473 dri2_surf->format,
474 0, dri2_surf->current->pitch,
475 0, 0,
476 0, 0);
477 close(fd);
478 } else {
479 dri2_surf->current->wl_buffer =
480 wl_drm_create_buffer(dri2_dpy->wl_drm,
481 dri2_surf->current->name,
482 dri2_surf->base.Width,
483 dri2_surf->base.Height,
484 dri2_surf->current->pitch,
485 dri2_surf->format);
486 }
487
488 wl_proxy_set_queue((struct wl_proxy *) dri2_surf->current->wl_buffer,
489 dri2_dpy->wl_queue);
490 wl_buffer_add_listener(dri2_surf->current->wl_buffer,
491 &wl_buffer_listener, dri2_surf);
492 }
493
494 /**
495 * Called via eglSwapBuffers(), drv->API.SwapBuffers().
496 */
497 static EGLBoolean
498 dri2_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
499 {
500 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
501 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
502 __DRIbuffer buffer;
503 int i, ret = 0;
504
505 while (dri2_surf->frame_callback && ret != -1)
506 ret = wl_display_dispatch_queue(dri2_dpy->wl_dpy, dri2_dpy->wl_queue);
507 if (ret < 0)
508 return EGL_FALSE;
509
510 dri2_surf->frame_callback = wl_surface_frame(dri2_surf->wl_win->surface);
511 wl_callback_add_listener(dri2_surf->frame_callback,
512 &frame_listener, dri2_surf);
513 wl_proxy_set_queue((struct wl_proxy *) dri2_surf->frame_callback,
514 dri2_dpy->wl_queue);
515
516 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++)
517 if (dri2_surf->color_buffers[i].age > 0)
518 dri2_surf->color_buffers[i].age++;
519
520 /* Make sure we have a back buffer in case we're swapping without ever
521 * rendering. */
522 if (get_back_bo(dri2_surf, &buffer) < 0) {
523 _eglError(EGL_BAD_ALLOC, "dri2_swap_buffers");
524 return EGL_FALSE;
525 }
526
527 dri2_surf->back->age = 1;
528 dri2_surf->current = dri2_surf->back;
529 dri2_surf->back = NULL;
530
531 create_wl_buffer(dri2_surf);
532
533 wl_surface_attach(dri2_surf->wl_win->surface,
534 dri2_surf->current->wl_buffer,
535 dri2_surf->dx, dri2_surf->dy);
536
537 dri2_surf->wl_win->attached_width = dri2_surf->base.Width;
538 dri2_surf->wl_win->attached_height = dri2_surf->base.Height;
539 /* reset resize growing parameters */
540 dri2_surf->dx = 0;
541 dri2_surf->dy = 0;
542
543 wl_surface_damage(dri2_surf->wl_win->surface, 0, 0,
544 dri2_surf->base.Width, dri2_surf->base.Height);
545
546 wl_surface_commit(dri2_surf->wl_win->surface);
547
548 (*dri2_dpy->flush->flush)(dri2_surf->dri_drawable);
549 (*dri2_dpy->flush->invalidate)(dri2_surf->dri_drawable);
550
551 return EGL_TRUE;
552 }
553
554 static EGLint
555 dri2_query_buffer_age(_EGLDriver *drv,
556 _EGLDisplay *disp, _EGLSurface *surface)
557 {
558 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surface);
559 __DRIbuffer buffer;
560
561 if (get_back_bo(dri2_surf, &buffer) < 0) {
562 _eglError(EGL_BAD_ALLOC, "dri2_query_buffer_age");
563 return 0;
564 }
565
566 return dri2_surf->back->age;
567 }
568
569 static int
570 dri2_wayland_authenticate(_EGLDisplay *disp, uint32_t id)
571 {
572 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
573 int ret = 0;
574
575 dri2_dpy->authenticated = 0;
576
577 wl_drm_authenticate(dri2_dpy->wl_drm, id);
578 if (roundtrip(dri2_dpy) < 0)
579 ret = -1;
580
581 if (!dri2_dpy->authenticated)
582 ret = -1;
583
584 /* reset authenticated */
585 dri2_dpy->authenticated = 1;
586
587 return ret;
588 }
589
590 /**
591 * Called via eglTerminate(), drv->API.Terminate().
592 */
593 static EGLBoolean
594 dri2_terminate(_EGLDriver *drv, _EGLDisplay *disp)
595 {
596 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
597
598 _eglReleaseDisplayResources(drv, disp);
599 _eglCleanupDisplay(disp);
600
601 dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen);
602 close(dri2_dpy->fd);
603 dlclose(dri2_dpy->driver);
604 free(dri2_dpy->driver_name);
605 free(dri2_dpy->device_name);
606 wl_drm_destroy(dri2_dpy->wl_drm);
607 if (dri2_dpy->own_device)
608 wl_display_disconnect(dri2_dpy->wl_dpy);
609 free(dri2_dpy);
610 disp->DriverData = NULL;
611
612 return EGL_TRUE;
613 }
614
615 static void
616 drm_handle_device(void *data, struct wl_drm *drm, const char *device)
617 {
618 struct dri2_egl_display *dri2_dpy = data;
619 drm_magic_t magic;
620
621 dri2_dpy->device_name = strdup(device);
622 if (!dri2_dpy->device_name)
623 return;
624
625 #ifdef O_CLOEXEC
626 dri2_dpy->fd = open(dri2_dpy->device_name, O_RDWR | O_CLOEXEC);
627 if (dri2_dpy->fd == -1 && errno == EINVAL)
628 #endif
629 {
630 dri2_dpy->fd = open(dri2_dpy->device_name, O_RDWR);
631 if (dri2_dpy->fd != -1)
632 fcntl(dri2_dpy->fd, F_SETFD, fcntl(dri2_dpy->fd, F_GETFD) |
633 FD_CLOEXEC);
634 }
635 if (dri2_dpy->fd == -1) {
636 _eglLog(_EGL_WARNING, "wayland-egl: could not open %s (%s)",
637 dri2_dpy->device_name, strerror(errno));
638 return;
639 }
640
641 drmGetMagic(dri2_dpy->fd, &magic);
642 wl_drm_authenticate(dri2_dpy->wl_drm, magic);
643 }
644
645 static void
646 drm_handle_format(void *data, struct wl_drm *drm, uint32_t format)
647 {
648 struct dri2_egl_display *dri2_dpy = data;
649
650 switch (format) {
651 case WL_DRM_FORMAT_ARGB8888:
652 dri2_dpy->formats |= HAS_ARGB8888;
653 break;
654 case WL_DRM_FORMAT_XRGB8888:
655 dri2_dpy->formats |= HAS_XRGB8888;
656 break;
657 }
658 }
659
660 static void
661 drm_handle_capabilities(void *data, struct wl_drm *drm, uint32_t value)
662 {
663 struct dri2_egl_display *dri2_dpy = data;
664
665 dri2_dpy->capabilities = value;
666 }
667
668 static void
669 drm_handle_authenticated(void *data, struct wl_drm *drm)
670 {
671 struct dri2_egl_display *dri2_dpy = data;
672
673 dri2_dpy->authenticated = 1;
674 }
675
676 static const struct wl_drm_listener drm_listener = {
677 drm_handle_device,
678 drm_handle_format,
679 drm_handle_authenticated,
680 drm_handle_capabilities
681 };
682
683 static void
684 registry_handle_global(void *data, struct wl_registry *registry, uint32_t name,
685 const char *interface, uint32_t version)
686 {
687 struct dri2_egl_display *dri2_dpy = data;
688
689 if (version > 1)
690 version = 2;
691 if (strcmp(interface, "wl_drm") == 0) {
692 dri2_dpy->wl_drm =
693 wl_registry_bind(registry, name, &wl_drm_interface, version);
694 wl_drm_add_listener(dri2_dpy->wl_drm, &drm_listener, dri2_dpy);
695 }
696 }
697
698 static const struct wl_registry_listener registry_listener = {
699 registry_handle_global
700 };
701
702 EGLBoolean
703 dri2_initialize_wayland(_EGLDriver *drv, _EGLDisplay *disp)
704 {
705 struct dri2_egl_display *dri2_dpy;
706 const __DRIconfig *config;
707 uint32_t types;
708 int i;
709 static const unsigned int argb_masks[4] =
710 { 0xff0000, 0xff00, 0xff, 0xff000000 };
711 static const unsigned int rgb_masks[4] = { 0xff0000, 0xff00, 0xff, 0 };
712
713 drv->API.CreateWindowSurface = dri2_create_window_surface;
714 drv->API.DestroySurface = dri2_destroy_surface;
715 drv->API.SwapBuffers = dri2_swap_buffers;
716 drv->API.Terminate = dri2_terminate;
717 drv->API.QueryBufferAge = dri2_query_buffer_age;
718
719 dri2_dpy = calloc(1, sizeof *dri2_dpy);
720 if (!dri2_dpy)
721 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
722
723 disp->DriverData = (void *) dri2_dpy;
724 if (disp->PlatformDisplay == NULL) {
725 dri2_dpy->wl_dpy = wl_display_connect(NULL);
726 if (dri2_dpy->wl_dpy == NULL)
727 goto cleanup_dpy;
728 dri2_dpy->own_device = 1;
729 } else {
730 dri2_dpy->wl_dpy = disp->PlatformDisplay;
731 }
732
733 dri2_dpy->wl_queue = wl_display_create_queue(dri2_dpy->wl_dpy);
734
735 if (dri2_dpy->own_device)
736 wl_display_dispatch_pending(dri2_dpy->wl_dpy);
737
738 dri2_dpy->wl_registry = wl_display_get_registry(dri2_dpy->wl_dpy);
739 wl_proxy_set_queue((struct wl_proxy *) dri2_dpy->wl_registry,
740 dri2_dpy->wl_queue);
741 wl_registry_add_listener(dri2_dpy->wl_registry,
742 &registry_listener, dri2_dpy);
743 if (roundtrip(dri2_dpy) < 0 || dri2_dpy->wl_drm == NULL)
744 goto cleanup_dpy;
745
746 if (roundtrip(dri2_dpy) < 0 || dri2_dpy->fd == -1)
747 goto cleanup_drm;
748
749 if (roundtrip(dri2_dpy) < 0 || !dri2_dpy->authenticated)
750 goto cleanup_fd;
751
752 dri2_dpy->driver_name = dri2_get_driver_for_fd(dri2_dpy->fd);
753 if (dri2_dpy->driver_name == NULL) {
754 _eglError(EGL_BAD_ALLOC, "DRI2: failed to get driver name");
755 goto cleanup_fd;
756 }
757
758 if (!dri2_load_driver(disp))
759 goto cleanup_driver_name;
760
761 dri2_dpy->dri2_loader_extension.base.name = __DRI_DRI2_LOADER;
762 dri2_dpy->dri2_loader_extension.base.version = 3;
763 dri2_dpy->dri2_loader_extension.getBuffers = dri2_get_buffers;
764 dri2_dpy->dri2_loader_extension.flushFrontBuffer = dri2_flush_front_buffer;
765 dri2_dpy->dri2_loader_extension.getBuffersWithFormat =
766 dri2_get_buffers_with_format;
767
768 dri2_dpy->extensions[0] = &dri2_dpy->dri2_loader_extension.base;
769 dri2_dpy->extensions[1] = &image_lookup_extension.base;
770 dri2_dpy->extensions[2] = &use_invalidate.base;
771 dri2_dpy->extensions[3] = NULL;
772
773 if (!dri2_create_screen(disp))
774 goto cleanup_driver;
775
776 types = EGL_WINDOW_BIT;
777 for (i = 0; dri2_dpy->driver_configs[i]; i++) {
778 config = dri2_dpy->driver_configs[i];
779 if (dri2_dpy->formats & HAS_XRGB8888)
780 dri2_add_config(disp, config, i + 1, 0, types, NULL, rgb_masks);
781 if (dri2_dpy->formats & HAS_ARGB8888)
782 dri2_add_config(disp, config, i + 1, 0, types, NULL, argb_masks);
783 }
784
785 disp->Extensions.WL_bind_wayland_display = EGL_TRUE;
786 disp->Extensions.EXT_buffer_age = EGL_TRUE;
787 dri2_dpy->authenticate = dri2_wayland_authenticate;
788
789 /* we're supporting EGL 1.4 */
790 disp->VersionMajor = 1;
791 disp->VersionMinor = 4;
792
793 return EGL_TRUE;
794
795 cleanup_driver:
796 dlclose(dri2_dpy->driver);
797 cleanup_driver_name:
798 free(dri2_dpy->driver_name);
799 cleanup_fd:
800 close(dri2_dpy->fd);
801 cleanup_drm:
802 free(dri2_dpy->device_name);
803 wl_drm_destroy(dri2_dpy->wl_drm);
804 cleanup_dpy:
805 free(dri2_dpy);
806
807 return EGL_FALSE;
808 }