platform_wayland.c revision f09c66b3cc9542b0a8f795e34b882c2b8f9f26f7
1/*
2 * Copyright © 2011 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
43static void
44sync_callback(void *data)
45{
46   int *done = data;
47
48   *done = 1;
49}
50
51static void
52force_roundtrip(struct wl_display *display)
53{
54   int done = 0;
55
56   wl_display_sync_callback(display, sync_callback, &done);
57   wl_display_iterate(display, WL_DISPLAY_WRITABLE);
58   while (!done)
59      wl_display_iterate(display, WL_DISPLAY_READABLE);
60}
61
62
63/**
64 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
65 */
66static _EGLSurface *
67dri2_create_surface(_EGLDriver *drv, _EGLDisplay *disp, EGLint type,
68		    _EGLConfig *conf, EGLNativeWindowType window,
69		    const EGLint *attrib_list)
70{
71   struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
72   struct dri2_egl_config *dri2_conf = dri2_egl_config(conf);
73   struct dri2_egl_surface *dri2_surf;
74   struct dri2_egl_buffer *dri2_buf;
75   int i;
76
77   (void) drv;
78
79   dri2_surf = malloc(sizeof *dri2_surf);
80   if (!dri2_surf) {
81      _eglError(EGL_BAD_ALLOC, "dri2_create_surface");
82      return NULL;
83   }
84
85   if (!_eglInitSurface(&dri2_surf->base, disp, type, conf, attrib_list))
86      goto cleanup_surf;
87
88   for (i = 0; i < WL_BUFFER_COUNT; ++i)
89      dri2_surf->wl_drm_buffer[i] = NULL;
90
91   for (i = 0; i < __DRI_BUFFER_COUNT; ++i)
92      dri2_surf->dri_buffers[i] = NULL;
93
94   dri2_surf->pending_buffer = NULL;
95   dri2_surf->block_swap_buffers = EGL_FALSE;
96
97   switch (type) {
98   case EGL_WINDOW_BIT:
99      dri2_surf->wl_win = (struct wl_egl_window *) window;
100      dri2_surf->type = DRI2_WINDOW_SURFACE;
101
102      dri2_surf->base.Width =  -1;
103      dri2_surf->base.Height = -1;
104      break;
105   case EGL_PIXMAP_BIT:
106      dri2_surf->wl_pix = (struct wl_egl_pixmap *) window;
107      dri2_surf->type = DRI2_PIXMAP_SURFACE;
108
109      dri2_surf->base.Width  = dri2_surf->wl_pix->width;
110      dri2_surf->base.Height = dri2_surf->wl_pix->height;
111
112      if (dri2_surf->wl_pix->driver_private) {
113         dri2_buf = dri2_surf->wl_pix->driver_private;
114         dri2_surf->dri_buffers[__DRI_BUFFER_FRONT_LEFT] = dri2_buf->dri_buffer;
115      }
116      break;
117   default:
118      goto cleanup_surf;
119   }
120
121   dri2_surf->dri_drawable =
122      (*dri2_dpy->dri2->createNewDrawable) (dri2_dpy->dri_screen,
123					    type == EGL_WINDOW_BIT ?
124					    dri2_conf->dri_double_config :
125					    dri2_conf->dri_single_config,
126					    dri2_surf);
127   if (dri2_surf->dri_drawable == NULL) {
128      _eglError(EGL_BAD_ALLOC, "dri2->createNewDrawable");
129      goto cleanup_dri_drawable;
130   }
131
132   return &dri2_surf->base;
133
134 cleanup_dri_drawable:
135   dri2_dpy->core->destroyDrawable(dri2_surf->dri_drawable);
136 cleanup_surf:
137   free(dri2_surf);
138
139   return NULL;
140}
141
142/**
143 * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
144 */
145static _EGLSurface *
146dri2_create_window_surface(_EGLDriver *drv, _EGLDisplay *disp,
147			   _EGLConfig *conf, EGLNativeWindowType window,
148			   const EGLint *attrib_list)
149{
150   return dri2_create_surface(drv, disp, EGL_WINDOW_BIT, conf,
151			      window, attrib_list);
152}
153
154static _EGLSurface *
155dri2_create_pixmap_surface(_EGLDriver *drv, _EGLDisplay *disp,
156			   _EGLConfig *conf, EGLNativePixmapType pixmap,
157			   const EGLint *attrib_list)
158{
159   return dri2_create_surface(drv, disp, EGL_PIXMAP_BIT, conf,
160			      pixmap, attrib_list);
161}
162
163/**
164 * Called via eglDestroySurface(), drv->API.DestroySurface().
165 */
166static EGLBoolean
167dri2_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
168{
169   struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
170   struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
171   int i;
172
173   (void) drv;
174
175   if (!_eglPutSurface(surf))
176      return EGL_TRUE;
177
178   (*dri2_dpy->core->destroyDrawable)(dri2_surf->dri_drawable);
179
180   for (i = 0; i < WL_BUFFER_COUNT; ++i)
181      if (dri2_surf->wl_drm_buffer[i])
182         wl_buffer_destroy(dri2_surf->wl_drm_buffer[i]);
183
184   for (i = 0; i < __DRI_BUFFER_COUNT; ++i)
185      if (dri2_surf->dri_buffers[i] && !(i == __DRI_BUFFER_FRONT_LEFT &&
186          dri2_surf->type == DRI2_PIXMAP_SURFACE))
187         dri2_dpy->dri2->releaseBuffer(dri2_dpy->dri_screen,
188                                       dri2_surf->dri_buffers[i]);
189
190   free(surf);
191
192   return EGL_TRUE;
193}
194
195static void
196dri2_wl_egl_pixmap_destroy(struct wl_egl_pixmap *egl_pixmap)
197{
198   struct dri2_egl_buffer *dri2_buf = egl_pixmap->driver_private;
199
200   assert(dri2_buf);
201
202   dri2_buf->dri2_dpy->dri2->releaseBuffer(dri2_buf->dri2_dpy->dri_screen,
203                                           dri2_buf->dri_buffer);
204
205   free(dri2_buf);
206
207   egl_pixmap->driver_private = NULL;
208   egl_pixmap->destroy = NULL;
209}
210
211static struct wl_buffer *
212wayland_create_buffer(struct dri2_egl_surface *dri2_surf,
213                      __DRIbuffer *buffer,
214                      struct wl_visual *visual)
215{
216   struct dri2_egl_display *dri2_dpy =
217      dri2_egl_display(dri2_surf->base.Resource.Display);
218
219   return wl_drm_create_buffer(dri2_dpy->wl_drm, buffer->name,
220                               dri2_surf->base.Width, dri2_surf->base.Height,
221                               buffer->pitch, visual);
222}
223
224static void
225dri2_process_back_buffer(struct dri2_egl_surface *dri2_surf, unsigned format)
226{
227   struct dri2_egl_display *dri2_dpy =
228      dri2_egl_display(dri2_surf->base.Resource.Display);
229
230   (void) format;
231
232   switch (dri2_surf->type) {
233   case DRI2_WINDOW_SURFACE:
234      /* allocate a front buffer for our double-buffered window*/
235      dri2_surf->dri_buffers[__DRI_BUFFER_FRONT_LEFT] =
236         dri2_dpy->dri2->allocateBuffer(dri2_dpy->dri_screen,
237               __DRI_BUFFER_FRONT_LEFT, format,
238               dri2_surf->base.Width, dri2_surf->base.Height);
239      break;
240   default:
241      break;
242   }
243}
244
245static void
246dri2_process_front_buffer(struct dri2_egl_surface *dri2_surf, unsigned format)
247{
248   struct dri2_egl_display *dri2_dpy =
249      dri2_egl_display(dri2_surf->base.Resource.Display);
250   struct dri2_egl_buffer *dri2_buf;
251
252   switch (dri2_surf->type) {
253   case DRI2_PIXMAP_SURFACE:
254      dri2_buf = malloc(sizeof *dri2_buf);
255      if (!dri2_buf)
256         return;
257
258      dri2_buf->dri_buffer = dri2_surf->dri_buffers[__DRI_BUFFER_FRONT_LEFT];
259      dri2_buf->dri2_dpy   = dri2_dpy;
260
261      dri2_surf->wl_pix->driver_private = dri2_buf;
262      dri2_surf->wl_pix->destroy        = dri2_wl_egl_pixmap_destroy;
263      break;
264   default:
265      break;
266   }
267}
268
269static void
270dri2_release_pending_buffer(void *data)
271{
272   struct dri2_egl_surface *dri2_surf = data;
273   struct dri2_egl_display *dri2_dpy =
274      dri2_egl_display(dri2_surf->base.Resource.Display);
275
276   /* FIXME: print internal error */
277   if (!dri2_surf->pending_buffer)
278      return;
279
280   dri2_dpy->dri2->releaseBuffer(dri2_dpy->dri_screen,
281                                 dri2_surf->pending_buffer);
282   dri2_surf->pending_buffer = NULL;
283}
284
285static void
286dri2_release_buffers(struct dri2_egl_surface *dri2_surf)
287{
288   struct dri2_egl_display *dri2_dpy =
289      dri2_egl_display(dri2_surf->base.Resource.Display);
290   int i;
291
292   for (i = 0; i < __DRI_BUFFER_COUNT; ++i) {
293      if (dri2_surf->dri_buffers[i]) {
294         switch (i) {
295         case __DRI_BUFFER_FRONT_LEFT:
296            if (dri2_surf->pending_buffer)
297               force_roundtrip(dri2_dpy->wl_dpy);
298            dri2_surf->pending_buffer = dri2_surf->dri_buffers[i];
299            wl_display_sync_callback(dri2_dpy->wl_dpy,
300                                     dri2_release_pending_buffer, dri2_surf);
301            break;
302         default:
303            dri2_dpy->dri2->releaseBuffer(dri2_dpy->dri_screen,
304                                          dri2_surf->dri_buffers[i]);
305            break;
306         }
307         dri2_surf->dri_buffers[i] = NULL;
308      }
309   }
310}
311
312static __DRIbuffer *
313dri2_get_buffers_with_format(__DRIdrawable * driDrawable,
314			     int *width, int *height,
315			     unsigned int *attachments, int count,
316			     int *out_count, void *loaderPrivate)
317{
318   struct dri2_egl_surface *dri2_surf = loaderPrivate;
319   struct dri2_egl_display *dri2_dpy =
320      dri2_egl_display(dri2_surf->base.Resource.Display);
321   int i;
322
323   if (dri2_surf->type == DRI2_WINDOW_SURFACE &&
324       (dri2_surf->base.Width != dri2_surf->wl_win->width ||
325        dri2_surf->base.Height != dri2_surf->wl_win->height)) {
326
327      dri2_release_buffers(dri2_surf);
328
329      dri2_surf->base.Width  = dri2_surf->wl_win->width;
330      dri2_surf->base.Height = dri2_surf->wl_win->height;
331      dri2_surf->dx = dri2_surf->wl_win->dx;
332      dri2_surf->dy = dri2_surf->wl_win->dy;
333
334      for (i = 0; i < WL_BUFFER_COUNT; ++i) {
335         if (dri2_surf->wl_drm_buffer[i])
336            wl_buffer_destroy(dri2_surf->wl_drm_buffer[i]);
337         dri2_surf->wl_drm_buffer[i]  = NULL;
338      }
339   }
340
341   dri2_surf->buffer_count = 0;
342   for (i = 0; i < 2*count; i+=2) {
343      assert(attachments[i] < __DRI_BUFFER_COUNT);
344      assert(dri2_surf->buffer_count < 5);
345
346      if (dri2_surf->dri_buffers[attachments[i]] == NULL) {
347
348         dri2_surf->dri_buffers[attachments[i]] =
349            dri2_dpy->dri2->allocateBuffer(dri2_dpy->dri_screen,
350                  attachments[i], attachments[i+1],
351                  dri2_surf->base.Width, dri2_surf->base.Height);
352
353         if (!dri2_surf->dri_buffers[attachments[i]])
354            continue;
355
356         if (attachments[i] == __DRI_BUFFER_FRONT_LEFT)
357            dri2_process_front_buffer(dri2_surf, attachments[i+1]);
358         else if (attachments[i] == __DRI_BUFFER_BACK_LEFT)
359            dri2_process_back_buffer(dri2_surf, attachments[i+1]);
360      }
361
362      memcpy(&dri2_surf->buffers[dri2_surf->buffer_count],
363             dri2_surf->dri_buffers[attachments[i]],
364             sizeof(__DRIbuffer));
365
366      dri2_surf->buffer_count++;
367   }
368
369   assert(dri2_surf->type == DRI2_PIXMAP_SURFACE ||
370          dri2_surf->dri_buffers[__DRI_BUFFER_BACK_LEFT]);
371
372   if (dri2_surf->type == DRI2_PIXMAP_SURFACE && !dri2_surf->wl_pix->buffer)
373      dri2_surf->wl_pix->buffer =
374         wayland_create_buffer(dri2_surf,
375			       dri2_surf->dri_buffers[__DRI_BUFFER_FRONT_LEFT],
376			       dri2_surf->wl_pix->visual);
377
378   *out_count = dri2_surf->buffer_count;
379   if (dri2_surf->buffer_count == 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
388static __DRIbuffer *
389dri2_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
422static void
423dri2_flush_front_buffer(__DRIdrawable * driDrawable, void *loaderPrivate)
424{
425   (void) driDrawable;
426
427   /* FIXME: Does EGL support front buffer rendering at all? */
428
429#if 0
430   struct dri2_egl_surface *dri2_surf = loaderPrivate;
431
432   dri2WaitGL(dri2_surf);
433#else
434   (void) loaderPrivate;
435#endif
436}
437
438static void
439wayland_frame_callback(struct wl_surface *surface, void *data, uint32_t time)
440{
441   struct dri2_egl_surface *dri2_surf = data;
442
443   dri2_surf->block_swap_buffers = EGL_FALSE;
444}
445
446static inline void
447pointer_swap(const void **p1, const void **p2)
448{
449   const void *tmp = *p1;
450   *p1 = *p2;
451   *p2 = tmp;
452}
453
454/**
455 * Called via eglSwapBuffers(), drv->API.SwapBuffers().
456 */
457static EGLBoolean
458dri2_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
459{
460   struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
461   struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
462   struct dri2_egl_driver *dri2_drv = dri2_egl_driver(drv);
463
464   while (dri2_surf->block_swap_buffers)
465      wl_display_iterate(dri2_dpy->wl_dpy, WL_DISPLAY_READABLE);
466
467   dri2_surf->block_swap_buffers = EGL_TRUE;
468   wl_display_frame_callback(dri2_dpy->wl_dpy,
469	 		     dri2_surf->wl_win->surface,
470			     wayland_frame_callback, dri2_surf);
471
472   if (dri2_surf->type == DRI2_WINDOW_SURFACE) {
473      pointer_swap(
474	    (const void **) &dri2_surf->dri_buffers[__DRI_BUFFER_FRONT_LEFT],
475	    (const void **) &dri2_surf->dri_buffers[__DRI_BUFFER_BACK_LEFT]);
476
477      dri2_surf->dri_buffers[__DRI_BUFFER_FRONT_LEFT]->attachment =
478	 __DRI_BUFFER_FRONT_LEFT;
479      dri2_surf->dri_buffers[__DRI_BUFFER_BACK_LEFT]->attachment =
480	 __DRI_BUFFER_BACK_LEFT;
481
482      pointer_swap((const void **) &dri2_surf->wl_drm_buffer[WL_BUFFER_FRONT],
483		   (const void **) &dri2_surf->wl_drm_buffer[WL_BUFFER_BACK]);
484
485      if (!dri2_surf->wl_drm_buffer[WL_BUFFER_FRONT])
486	 dri2_surf->wl_drm_buffer[WL_BUFFER_FRONT] =
487	    wayland_create_buffer(dri2_surf,
488		  dri2_surf->dri_buffers[__DRI_BUFFER_FRONT_LEFT],
489		  dri2_surf->wl_win->visual);
490
491      wl_surface_attach(dri2_surf->wl_win->surface,
492	    dri2_surf->wl_drm_buffer[WL_BUFFER_FRONT],
493	    dri2_surf->dx, dri2_surf->dy);
494
495      dri2_surf->wl_win->attached_width  = dri2_surf->base.Width;
496      dri2_surf->wl_win->attached_height = dri2_surf->base.Height;
497      /* reset resize growing parameters */
498      dri2_surf->dx = 0;
499      dri2_surf->dy = 0;
500
501      wl_surface_damage(dri2_surf->wl_win->surface, 0, 0,
502	    dri2_surf->base.Width, dri2_surf->base.Height);
503   }
504
505   _EGLContext *ctx;
506   if (dri2_drv->glFlush) {
507      ctx = _eglGetCurrentContext();
508      if (ctx && ctx->DrawSurface == &dri2_surf->base)
509         dri2_drv->glFlush();
510   }
511
512   (*dri2_dpy->flush->flush)(dri2_surf->dri_drawable);
513   (*dri2_dpy->flush->invalidate)(dri2_surf->dri_drawable);
514
515   return EGL_TRUE;
516}
517
518/**
519 * Called via eglCreateImageKHR(), drv->API.CreateImageKHR().
520 */
521static _EGLImage *
522dri2_create_image_khr_pixmap(_EGLDisplay *disp, _EGLContext *ctx,
523			     EGLClientBuffer buffer, const EGLint *attr_list)
524{
525   struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
526   struct wl_egl_pixmap *wl_egl_pixmap = (struct wl_egl_pixmap *) buffer;
527   struct dri2_egl_buffer *dri2_buf;
528   EGLint wl_attr_list[] = {
529		EGL_WIDTH,		0,
530		EGL_HEIGHT,		0,
531		EGL_DRM_BUFFER_STRIDE_MESA,	0,
532		EGL_DRM_BUFFER_FORMAT_MESA,	EGL_DRM_BUFFER_FORMAT_ARGB32_MESA,
533		EGL_NONE
534   };
535
536   dri2_buf = malloc(sizeof *dri2_buf);
537   if (!dri2_buf)
538           return NULL;
539
540   dri2_buf->dri2_dpy = dri2_dpy;
541   dri2_buf->dri_buffer =
542      dri2_dpy->dri2->allocateBuffer(dri2_dpy->dri_screen,
543				     __DRI_BUFFER_FRONT_LEFT, 32,
544				     wl_egl_pixmap->width,
545				     wl_egl_pixmap->height);
546
547   wl_egl_pixmap->destroy = dri2_wl_egl_pixmap_destroy;
548   wl_egl_pixmap->driver_private = dri2_buf;
549
550   wl_egl_pixmap->buffer =
551      wl_drm_create_buffer(dri2_dpy->wl_drm,
552			   dri2_buf->dri_buffer->name,
553			   wl_egl_pixmap->width,
554			   wl_egl_pixmap->height,
555			   dri2_buf->dri_buffer->pitch,
556			   wl_egl_pixmap->visual);
557
558   wl_attr_list[1] = wl_egl_pixmap->width;
559   wl_attr_list[3] = wl_egl_pixmap->height;
560   wl_attr_list[5] = dri2_buf->dri_buffer->pitch / 4;
561
562   return dri2_create_image_khr(disp->Driver, disp, ctx, EGL_DRM_BUFFER_MESA,
563	(EGLClientBuffer)(intptr_t) dri2_buf->dri_buffer->name, wl_attr_list);
564}
565
566static _EGLImage *
567dri2_wayland_create_image_khr(_EGLDriver *drv, _EGLDisplay *disp,
568			      _EGLContext *ctx, EGLenum target,
569			      EGLClientBuffer buffer, const EGLint *attr_list)
570{
571   (void) drv;
572
573   switch (target) {
574   case EGL_NATIVE_PIXMAP_KHR:
575      return dri2_create_image_khr_pixmap(disp, ctx, buffer, attr_list);
576   default:
577      return dri2_create_image_khr(drv, disp, ctx, target, buffer, attr_list);
578   }
579}
580
581static int
582dri2_wayland_authenticate(_EGLDisplay *disp, uint32_t id)
583{
584   struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
585   int ret = 0;
586
587   dri2_dpy->authenticated = false;
588
589   wl_drm_authenticate(dri2_dpy->wl_drm, id);
590   force_roundtrip(dri2_dpy->wl_dpy);
591
592   if (!dri2_dpy->authenticated)
593      ret = -1;
594
595   /* reset authenticated */
596   dri2_dpy->authenticated = true;
597
598   return ret;
599}
600
601/**
602 * Called via eglTerminate(), drv->API.Terminate().
603 */
604static EGLBoolean
605dri2_terminate(_EGLDriver *drv, _EGLDisplay *disp)
606{
607   struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
608
609   _eglReleaseDisplayResources(drv, disp);
610   _eglCleanupDisplay(disp);
611
612   dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen);
613   close(dri2_dpy->fd);
614   dlclose(dri2_dpy->driver);
615   free(dri2_dpy->driver_name);
616   free(dri2_dpy);
617   disp->DriverData = NULL;
618
619   return EGL_TRUE;
620}
621
622static void
623drm_handle_device(void *data, struct wl_drm *drm, const char *device)
624{
625   struct dri2_egl_display *dri2_dpy = data;
626   drm_magic_t magic;
627
628   dri2_dpy->device_name = strdup(device);
629   if (!dri2_dpy->device_name)
630      return;
631
632   dri2_dpy->fd = open(dri2_dpy->device_name, O_RDWR);
633   if (dri2_dpy->fd == -1) {
634      _eglLog(_EGL_WARNING, "wayland-egl: could not open %s (%s)",
635	      dri2_dpy->device_name, strerror(errno));
636      return;
637   }
638
639   drmGetMagic(dri2_dpy->fd, &magic);
640   wl_drm_authenticate(dri2_dpy->wl_drm, magic);
641}
642
643static void
644drm_handle_authenticated(void *data, struct wl_drm *drm)
645{
646   struct dri2_egl_display *dri2_dpy = data;
647
648   dri2_dpy->authenticated = true;
649}
650
651static const struct wl_drm_listener drm_listener = {
652	drm_handle_device,
653	drm_handle_authenticated
654};
655
656EGLBoolean
657dri2_initialize_wayland(_EGLDriver *drv, _EGLDisplay *disp)
658{
659   struct dri2_egl_display *dri2_dpy;
660   uint32_t id;
661   int i;
662
663   drv->API.CreateWindowSurface = dri2_create_window_surface;
664   drv->API.CreatePixmapSurface = dri2_create_pixmap_surface;
665   drv->API.DestroySurface = dri2_destroy_surface;
666   drv->API.SwapBuffers = dri2_swap_buffers;
667   drv->API.CreateImageKHR = dri2_wayland_create_image_khr;
668   drv->API.Terminate = dri2_terminate;
669
670   dri2_dpy = malloc(sizeof *dri2_dpy);
671   if (!dri2_dpy)
672      return _eglError(EGL_BAD_ALLOC, "eglInitialize");
673
674   memset(dri2_dpy, 0, sizeof *dri2_dpy);
675
676   disp->DriverData = (void *) dri2_dpy;
677   dri2_dpy->wl_dpy = disp->PlatformDisplay;
678
679   id = wl_display_get_global(dri2_dpy->wl_dpy, "wl_drm", 1);
680   if (id == 0)
681      wl_display_iterate(dri2_dpy->wl_dpy, WL_DISPLAY_READABLE);
682   id = wl_display_get_global(dri2_dpy->wl_dpy, "wl_drm", 1);
683   if (id == 0)
684      goto cleanup_dpy;
685   dri2_dpy->wl_drm = wl_drm_create(dri2_dpy->wl_dpy, id, 1);
686   if (!dri2_dpy->wl_drm)
687      goto cleanup_dpy;
688   wl_drm_add_listener(dri2_dpy->wl_drm, &drm_listener, dri2_dpy);
689   force_roundtrip(dri2_dpy->wl_dpy);
690   if (dri2_dpy->fd == -1)
691      goto cleanup_drm;
692
693   force_roundtrip(dri2_dpy->wl_dpy);
694   if (!dri2_dpy->authenticated)
695      goto cleanup_fd;
696
697   dri2_dpy->driver_name = dri2_get_driver_for_fd(dri2_dpy->fd);
698   if (dri2_dpy->driver_name == NULL) {
699      _eglError(EGL_BAD_ALLOC, "DRI2: failed to get driver name");
700      goto cleanup_fd;
701   }
702
703   if (!dri2_load_driver(disp))
704      goto cleanup_driver_name;
705
706   dri2_dpy->dri2_loader_extension.base.name = __DRI_DRI2_LOADER;
707   dri2_dpy->dri2_loader_extension.base.version = 3;
708   dri2_dpy->dri2_loader_extension.getBuffers = dri2_get_buffers;
709   dri2_dpy->dri2_loader_extension.flushFrontBuffer = dri2_flush_front_buffer;
710   dri2_dpy->dri2_loader_extension.getBuffersWithFormat =
711      dri2_get_buffers_with_format;
712
713   dri2_dpy->extensions[0] = &dri2_dpy->dri2_loader_extension.base;
714   dri2_dpy->extensions[1] = &image_lookup_extension.base;
715   dri2_dpy->extensions[2] = NULL;
716
717   if (!dri2_create_screen(disp))
718      goto cleanup_driver;
719
720   for (i = 0; dri2_dpy->driver_configs[i]; i++)
721      dri2_add_config(disp, dri2_dpy->driver_configs[i], i + 1, 0,
722		      EGL_WINDOW_BIT | EGL_PIXMAP_BIT, NULL);
723
724
725   disp->Extensions.KHR_image_pixmap = EGL_TRUE;
726
727   disp->Extensions.WL_bind_wayland_display = EGL_TRUE;
728   dri2_dpy->authenticate = dri2_wayland_authenticate;
729
730   /* we're supporting EGL 1.4 */
731   disp->VersionMajor = 1;
732   disp->VersionMinor = 4;
733
734   return EGL_TRUE;
735
736 cleanup_driver:
737   dlclose(dri2_dpy->driver);
738 cleanup_driver_name:
739   free(dri2_dpy->driver_name);
740 cleanup_fd:
741   close(dri2_dpy->fd);
742 cleanup_drm:
743   free(dri2_dpy->device_name);
744   wl_drm_destroy(dri2_dpy->wl_drm);
745 cleanup_dpy:
746   free(dri2_dpy);
747
748   return EGL_FALSE;
749}
750