egl-wayland: Make sure we allocate a back buffer even if nothing was rendered
[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_buffer)
208 dri2_dpy->dri2->releaseBuffer(dri2_dpy->dri_screen,
209 dri2_surf->color_buffers[i].dri_buffer);
210 }
211
212 for (i = 0; i < __DRI_BUFFER_COUNT; i++)
213 if (dri2_surf->dri_buffers[i] &&
214 dri2_surf->dri_buffers[i]->attachment != __DRI_BUFFER_BACK_LEFT)
215 dri2_dpy->dri2->releaseBuffer(dri2_dpy->dri_screen,
216 dri2_surf->dri_buffers[i]);
217
218 if (dri2_surf->frame_callback)
219 wl_callback_destroy(dri2_surf->frame_callback);
220
221 if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
222 dri2_surf->wl_win->private = NULL;
223 dri2_surf->wl_win->resize_callback = NULL;
224 }
225
226 free(surf);
227
228 return EGL_TRUE;
229 }
230
231 static void
232 dri2_release_buffers(struct dri2_egl_surface *dri2_surf)
233 {
234 struct dri2_egl_display *dri2_dpy =
235 dri2_egl_display(dri2_surf->base.Resource.Display);
236 int i;
237
238 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
239 if (dri2_surf->color_buffers[i].wl_buffer &&
240 !dri2_surf->color_buffers[i].locked)
241 wl_buffer_destroy(dri2_surf->color_buffers[i].wl_buffer);
242 if (dri2_surf->color_buffers[i].dri_buffer)
243 dri2_dpy->dri2->releaseBuffer(dri2_dpy->dri_screen,
244 dri2_surf->color_buffers[i].dri_buffer);
245
246 dri2_surf->color_buffers[i].wl_buffer = NULL;
247 dri2_surf->color_buffers[i].dri_buffer = NULL;
248 dri2_surf->color_buffers[i].locked = 0;
249 }
250
251 for (i = 0; i < __DRI_BUFFER_COUNT; i++)
252 if (dri2_surf->dri_buffers[i] &&
253 dri2_surf->dri_buffers[i]->attachment != __DRI_BUFFER_BACK_LEFT)
254 dri2_dpy->dri2->releaseBuffer(dri2_dpy->dri_screen,
255 dri2_surf->dri_buffers[i]);
256 }
257
258 static int
259 get_back_bo(struct dri2_egl_surface *dri2_surf, __DRIbuffer *buffer)
260 {
261 struct dri2_egl_display *dri2_dpy =
262 dri2_egl_display(dri2_surf->base.Resource.Display);
263 int i;
264
265 /* There might be a buffer release already queued that wasn't processed */
266 wl_display_dispatch_queue_pending(dri2_dpy->wl_dpy, dri2_dpy->wl_queue);
267
268 if (dri2_surf->back == NULL) {
269 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
270 /* Get an unlocked buffer, preferrably one with a dri_buffer already
271 * allocated. */
272 if (dri2_surf->color_buffers[i].locked)
273 continue;
274 if (dri2_surf->back == NULL)
275 dri2_surf->back = &dri2_surf->color_buffers[i];
276 else if (dri2_surf->back->dri_buffer == NULL)
277 dri2_surf->back = &dri2_surf->color_buffers[i];
278 }
279 }
280
281 if (dri2_surf->back == NULL)
282 return -1;
283 if (dri2_surf->back->dri_buffer == NULL) {
284 dri2_surf->back->dri_buffer =
285 dri2_dpy->dri2->allocateBuffer(dri2_dpy->dri_screen,
286 __DRI_BUFFER_BACK_LEFT, 32,
287 dri2_surf->base.Width,
288 dri2_surf->base.Height);
289 dri2_surf->back->age = 0;
290 }
291 if (dri2_surf->back->dri_buffer == NULL)
292 return -1;
293
294 dri2_surf->back->locked = 1;
295 memcpy(buffer, dri2_surf->back->dri_buffer, sizeof *buffer);
296
297 return 0;
298 }
299
300 static int
301 get_aux_bo(struct dri2_egl_surface *dri2_surf,
302 unsigned int attachment, unsigned int format, __DRIbuffer *buffer)
303 {
304 struct dri2_egl_display *dri2_dpy =
305 dri2_egl_display(dri2_surf->base.Resource.Display);
306 __DRIbuffer *b = dri2_surf->dri_buffers[attachment];
307
308 if (b == NULL) {
309 b = dri2_dpy->dri2->allocateBuffer(dri2_dpy->dri_screen,
310 attachment, format,
311 dri2_surf->base.Width,
312 dri2_surf->base.Height);
313 dri2_surf->dri_buffers[attachment] = b;
314 }
315 if (b == NULL)
316 return -1;
317
318 memcpy(buffer, b, sizeof *buffer);
319
320 return 0;
321 }
322
323 static __DRIbuffer *
324 dri2_get_buffers_with_format(__DRIdrawable * driDrawable,
325 int *width, int *height,
326 unsigned int *attachments, int count,
327 int *out_count, void *loaderPrivate)
328 {
329 struct dri2_egl_surface *dri2_surf = loaderPrivate;
330 struct dri2_egl_display *dri2_dpy =
331 dri2_egl_display(dri2_surf->base.Resource.Display);
332 int i, j;
333
334 if (dri2_surf->base.Type == EGL_WINDOW_BIT &&
335 (dri2_surf->base.Width != dri2_surf->wl_win->width ||
336 dri2_surf->base.Height != dri2_surf->wl_win->height)) {
337
338 dri2_release_buffers(dri2_surf);
339
340 dri2_surf->base.Width = dri2_surf->wl_win->width;
341 dri2_surf->base.Height = dri2_surf->wl_win->height;
342 dri2_surf->dx = dri2_surf->wl_win->dx;
343 dri2_surf->dy = dri2_surf->wl_win->dy;
344 }
345
346 for (i = 0, j = 0; i < 2 * count; i += 2, j++) {
347 switch (attachments[i]) {
348 case __DRI_BUFFER_BACK_LEFT:
349 if (get_back_bo(dri2_surf, &dri2_surf->buffers[j]) < 0) {
350 _eglError(EGL_BAD_ALLOC, "failed to allocate color buffer");
351 return NULL;
352 }
353 break;
354 default:
355 if (get_aux_bo(dri2_surf, attachments[i], attachments[i + 1],
356 &dri2_surf->buffers[j]) < 0) {
357 _eglError(EGL_BAD_ALLOC, "failed to allocate aux buffer");
358 return NULL;
359 }
360 break;
361 }
362 }
363
364 /* If we have an extra unlocked buffer at this point, we had to do triple
365 * buffering for a while, but now can go back to just double buffering.
366 * That means we can free any unlocked buffer now. */
367 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
368 if (!dri2_surf->color_buffers[i].locked &&
369 dri2_surf->color_buffers[i].wl_buffer) {
370 wl_buffer_destroy(dri2_surf->color_buffers[i].wl_buffer);
371 dri2_dpy->dri2->releaseBuffer(dri2_dpy->dri_screen,
372 dri2_surf->color_buffers[i].dri_buffer);
373 dri2_surf->color_buffers[i].wl_buffer = NULL;
374 dri2_surf->color_buffers[i].dri_buffer = NULL;
375 }
376 }
377
378 *out_count = j;
379 if (j == 0)
380 return NULL;
381
382 *width = dri2_surf->base.Width;
383 *height = dri2_surf->base.Height;
384
385 return dri2_surf->buffers;
386 }
387
388 static __DRIbuffer *
389 dri2_get_buffers(__DRIdrawable * driDrawable,
390 int *width, int *height,
391 unsigned int *attachments, int count,
392 int *out_count, void *loaderPrivate)
393 {
394 unsigned int *attachments_with_format;
395 __DRIbuffer *buffer;
396 const unsigned int format = 32;
397 int i;
398
399 attachments_with_format = calloc(count * 2, sizeof(unsigned int));
400 if (!attachments_with_format) {
401 *out_count = 0;
402 return NULL;
403 }
404
405 for (i = 0; i < count; ++i) {
406 attachments_with_format[2*i] = attachments[i];
407 attachments_with_format[2*i + 1] = format;
408 }
409
410 buffer =
411 dri2_get_buffers_with_format(driDrawable,
412 width, height,
413 attachments_with_format, count,
414 out_count, loaderPrivate);
415
416 free(attachments_with_format);
417
418 return buffer;
419 }
420
421 static void
422 dri2_flush_front_buffer(__DRIdrawable * driDrawable, void *loaderPrivate)
423 {
424 (void) driDrawable;
425 (void) loaderPrivate;
426 }
427
428 static void
429 wayland_frame_callback(void *data, struct wl_callback *callback, uint32_t time)
430 {
431 struct dri2_egl_surface *dri2_surf = data;
432
433 dri2_surf->frame_callback = NULL;
434 wl_callback_destroy(callback);
435 }
436
437 static const struct wl_callback_listener frame_listener = {
438 wayland_frame_callback
439 };
440
441 /**
442 * Called via eglSwapBuffers(), drv->API.SwapBuffers().
443 */
444 static EGLBoolean
445 dri2_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
446 {
447 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
448 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
449 __DRIbuffer buffer;
450 int i, ret = 0;
451
452 while (dri2_surf->frame_callback && ret != -1)
453 ret = wl_display_dispatch_queue(dri2_dpy->wl_dpy, dri2_dpy->wl_queue);
454 if (ret < 0)
455 return EGL_FALSE;
456
457 dri2_surf->frame_callback = wl_surface_frame(dri2_surf->wl_win->surface);
458 wl_callback_add_listener(dri2_surf->frame_callback,
459 &frame_listener, dri2_surf);
460 wl_proxy_set_queue((struct wl_proxy *) dri2_surf->frame_callback,
461 dri2_dpy->wl_queue);
462
463 for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++)
464 if (dri2_surf->color_buffers[i].age > 0)
465 dri2_surf->color_buffers[i].age++;
466
467 /* Make sure we have a back buffer in case we're swapping without ever
468 * rendering. */
469 if (get_back_bo(dri2_surf, &buffer) < 0) {
470 _eglError(EGL_BAD_ALLOC, "dri2_swap_buffers");
471 return EGL_FALSE;
472 }
473
474 dri2_surf->back->age = 1;
475 dri2_surf->current = dri2_surf->back;
476 dri2_surf->back = NULL;
477
478 if (dri2_surf->current->wl_buffer == NULL) {
479 dri2_surf->current->wl_buffer =
480 wl_drm_create_buffer(dri2_dpy->wl_drm,
481 dri2_surf->current->dri_buffer->name,
482 dri2_surf->base.Width,
483 dri2_surf->base.Height,
484 dri2_surf->current->dri_buffer->pitch,
485 dri2_surf->format);
486 wl_proxy_set_queue((struct wl_proxy *) dri2_surf->current->wl_buffer,
487 dri2_dpy->wl_queue);
488 wl_buffer_add_listener(dri2_surf->current->wl_buffer,
489 &wl_buffer_listener, dri2_surf);
490 }
491
492 wl_surface_attach(dri2_surf->wl_win->surface,
493 dri2_surf->current->wl_buffer,
494 dri2_surf->dx, dri2_surf->dy);
495
496 dri2_surf->wl_win->attached_width = dri2_surf->base.Width;
497 dri2_surf->wl_win->attached_height = dri2_surf->base.Height;
498 /* reset resize growing parameters */
499 dri2_surf->dx = 0;
500 dri2_surf->dy = 0;
501
502 wl_surface_damage(dri2_surf->wl_win->surface, 0, 0,
503 dri2_surf->base.Width, dri2_surf->base.Height);
504
505 wl_surface_commit(dri2_surf->wl_win->surface);
506
507 (*dri2_dpy->flush->flush)(dri2_surf->dri_drawable);
508 (*dri2_dpy->flush->invalidate)(dri2_surf->dri_drawable);
509
510 return EGL_TRUE;
511 }
512
513 static EGLint
514 dri2_query_buffer_age(_EGLDriver *drv,
515 _EGLDisplay *disp, _EGLSurface *surface)
516 {
517 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surface);
518 __DRIbuffer buffer;
519
520 if (get_back_bo(dri2_surf, &buffer) < 0) {
521 _eglError(EGL_BAD_ALLOC, "dri2_query_buffer_age");
522 return 0;
523 }
524
525 return dri2_surf->back->age;
526 }
527
528 static int
529 dri2_wayland_authenticate(_EGLDisplay *disp, uint32_t id)
530 {
531 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
532 int ret = 0;
533
534 dri2_dpy->authenticated = 0;
535
536 wl_drm_authenticate(dri2_dpy->wl_drm, id);
537 if (roundtrip(dri2_dpy) < 0)
538 ret = -1;
539
540 if (!dri2_dpy->authenticated)
541 ret = -1;
542
543 /* reset authenticated */
544 dri2_dpy->authenticated = 1;
545
546 return ret;
547 }
548
549 /**
550 * Called via eglTerminate(), drv->API.Terminate().
551 */
552 static EGLBoolean
553 dri2_terminate(_EGLDriver *drv, _EGLDisplay *disp)
554 {
555 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
556
557 _eglReleaseDisplayResources(drv, disp);
558 _eglCleanupDisplay(disp);
559
560 dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen);
561 close(dri2_dpy->fd);
562 dlclose(dri2_dpy->driver);
563 free(dri2_dpy->driver_name);
564 free(dri2_dpy->device_name);
565 wl_drm_destroy(dri2_dpy->wl_drm);
566 if (dri2_dpy->own_device)
567 wl_display_disconnect(dri2_dpy->wl_dpy);
568 free(dri2_dpy);
569 disp->DriverData = NULL;
570
571 return EGL_TRUE;
572 }
573
574 static void
575 drm_handle_device(void *data, struct wl_drm *drm, const char *device)
576 {
577 struct dri2_egl_display *dri2_dpy = data;
578 drm_magic_t magic;
579
580 dri2_dpy->device_name = strdup(device);
581 if (!dri2_dpy->device_name)
582 return;
583
584 #ifdef O_CLOEXEC
585 dri2_dpy->fd = open(dri2_dpy->device_name, O_RDWR | O_CLOEXEC);
586 if (dri2_dpy->fd == -1 && errno == EINVAL)
587 #endif
588 {
589 dri2_dpy->fd = open(dri2_dpy->device_name, O_RDWR);
590 if (dri2_dpy->fd != -1)
591 fcntl(dri2_dpy->fd, F_SETFD, fcntl(dri2_dpy->fd, F_GETFD) |
592 FD_CLOEXEC);
593 }
594 if (dri2_dpy->fd == -1) {
595 _eglLog(_EGL_WARNING, "wayland-egl: could not open %s (%s)",
596 dri2_dpy->device_name, strerror(errno));
597 return;
598 }
599
600 drmGetMagic(dri2_dpy->fd, &magic);
601 wl_drm_authenticate(dri2_dpy->wl_drm, magic);
602 }
603
604 static void
605 drm_handle_format(void *data, struct wl_drm *drm, uint32_t format)
606 {
607 struct dri2_egl_display *dri2_dpy = data;
608
609 switch (format) {
610 case WL_DRM_FORMAT_ARGB8888:
611 dri2_dpy->formats |= HAS_ARGB8888;
612 break;
613 case WL_DRM_FORMAT_XRGB8888:
614 dri2_dpy->formats |= HAS_XRGB8888;
615 break;
616 }
617 }
618
619 static void
620 drm_handle_authenticated(void *data, struct wl_drm *drm)
621 {
622 struct dri2_egl_display *dri2_dpy = data;
623
624 dri2_dpy->authenticated = 1;
625 }
626
627 static const struct wl_drm_listener drm_listener = {
628 drm_handle_device,
629 drm_handle_format,
630 drm_handle_authenticated
631 };
632
633 static void
634 registry_handle_global(void *data, struct wl_registry *registry, uint32_t name,
635 const char *interface, uint32_t version)
636 {
637 struct dri2_egl_display *dri2_dpy = data;
638
639 if (strcmp(interface, "wl_drm") == 0) {
640 dri2_dpy->wl_drm =
641 wl_registry_bind(registry, name, &wl_drm_interface, 1);
642 wl_drm_add_listener(dri2_dpy->wl_drm, &drm_listener, dri2_dpy);
643 }
644 }
645
646 static const struct wl_registry_listener registry_listener = {
647 registry_handle_global
648 };
649
650 EGLBoolean
651 dri2_initialize_wayland(_EGLDriver *drv, _EGLDisplay *disp)
652 {
653 struct dri2_egl_display *dri2_dpy;
654 const __DRIconfig *config;
655 uint32_t types;
656 int i;
657 static const unsigned int argb_masks[4] =
658 { 0xff0000, 0xff00, 0xff, 0xff000000 };
659 static const unsigned int rgb_masks[4] = { 0xff0000, 0xff00, 0xff, 0 };
660
661 drv->API.CreateWindowSurface = dri2_create_window_surface;
662 drv->API.DestroySurface = dri2_destroy_surface;
663 drv->API.SwapBuffers = dri2_swap_buffers;
664 drv->API.Terminate = dri2_terminate;
665 drv->API.QueryBufferAge = dri2_query_buffer_age;
666
667 dri2_dpy = calloc(1, sizeof *dri2_dpy);
668 if (!dri2_dpy)
669 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
670
671 disp->DriverData = (void *) dri2_dpy;
672 if (disp->PlatformDisplay == NULL) {
673 dri2_dpy->wl_dpy = wl_display_connect(NULL);
674 if (dri2_dpy->wl_dpy == NULL)
675 goto cleanup_dpy;
676 dri2_dpy->own_device = 1;
677 } else {
678 dri2_dpy->wl_dpy = disp->PlatformDisplay;
679 }
680
681 dri2_dpy->wl_queue = wl_display_create_queue(dri2_dpy->wl_dpy);
682 dri2_dpy->wl_registry = wl_display_get_registry(dri2_dpy->wl_dpy);
683 wl_proxy_set_queue((struct wl_proxy *) dri2_dpy->wl_registry,
684 dri2_dpy->wl_queue);
685 wl_registry_add_listener(dri2_dpy->wl_registry,
686 &registry_listener, dri2_dpy);
687 if (roundtrip(dri2_dpy) < 0 || dri2_dpy->wl_drm == NULL)
688 goto cleanup_dpy;
689
690 if (roundtrip(dri2_dpy) < 0 || dri2_dpy->fd == -1)
691 goto cleanup_drm;
692
693 if (roundtrip(dri2_dpy) < 0 || !dri2_dpy->authenticated)
694 goto cleanup_fd;
695
696 dri2_dpy->driver_name = dri2_get_driver_for_fd(dri2_dpy->fd);
697 if (dri2_dpy->driver_name == NULL) {
698 _eglError(EGL_BAD_ALLOC, "DRI2: failed to get driver name");
699 goto cleanup_fd;
700 }
701
702 if (!dri2_load_driver(disp))
703 goto cleanup_driver_name;
704
705 dri2_dpy->dri2_loader_extension.base.name = __DRI_DRI2_LOADER;
706 dri2_dpy->dri2_loader_extension.base.version = 3;
707 dri2_dpy->dri2_loader_extension.getBuffers = dri2_get_buffers;
708 dri2_dpy->dri2_loader_extension.flushFrontBuffer = dri2_flush_front_buffer;
709 dri2_dpy->dri2_loader_extension.getBuffersWithFormat =
710 dri2_get_buffers_with_format;
711
712 dri2_dpy->extensions[0] = &dri2_dpy->dri2_loader_extension.base;
713 dri2_dpy->extensions[1] = &image_lookup_extension.base;
714 dri2_dpy->extensions[2] = &use_invalidate.base;
715 dri2_dpy->extensions[3] = NULL;
716
717 if (!dri2_create_screen(disp))
718 goto cleanup_driver;
719
720 types = EGL_WINDOW_BIT;
721 for (i = 0; dri2_dpy->driver_configs[i]; i++) {
722 config = dri2_dpy->driver_configs[i];
723 if (dri2_dpy->formats & HAS_XRGB8888)
724 dri2_add_config(disp, config, i + 1, 0, types, NULL, rgb_masks);
725 if (dri2_dpy->formats & HAS_ARGB8888)
726 dri2_add_config(disp, config, i + 1, 0, types, NULL, argb_masks);
727 }
728
729 disp->Extensions.WL_bind_wayland_display = EGL_TRUE;
730 disp->Extensions.EXT_buffer_age = EGL_TRUE;
731 dri2_dpy->authenticate = dri2_wayland_authenticate;
732
733 /* we're supporting EGL 1.4 */
734 disp->VersionMajor = 1;
735 disp->VersionMinor = 4;
736
737 return EGL_TRUE;
738
739 cleanup_driver:
740 dlclose(dri2_dpy->driver);
741 cleanup_driver_name:
742 free(dri2_dpy->driver_name);
743 cleanup_fd:
744 close(dri2_dpy->fd);
745 cleanup_drm:
746 free(dri2_dpy->device_name);
747 wl_drm_destroy(dri2_dpy->wl_drm);
748 cleanup_dpy:
749 free(dri2_dpy);
750
751 return EGL_FALSE;
752 }