OSDN Git Service

a65338eb9c404b680514953a60bb57efc7fac35b
[android-x86/external-mesa.git] / src / egl / drivers / dri2 / platform_wayland.c
1 /*
2  * Copyright © 2011-2012 Intel Corporation
3  * Copyright © 2012 Collabora, Ltd.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23  * DEALINGS IN THE SOFTWARE.
24  *
25  * Authors:
26  *    Kristian Høgsberg <krh@bitplanet.net>
27  *    Benjamin Franzke <benjaminfranzke@googlemail.com>
28  */
29
30 #include <stdint.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <limits.h>
34 #include <dlfcn.h>
35 #include <errno.h>
36 #include <unistd.h>
37 #include <fcntl.h>
38 #include <xf86drm.h>
39 #include <sys/mman.h>
40
41 #include "egl_dri2.h"
42 #include "egl_dri2_fallbacks.h"
43 #include "loader.h"
44
45 #include <wayland-client.h>
46 #include "wayland-drm-client-protocol.h"
47
48 enum wl_drm_format_flags {
49    HAS_ARGB8888 = 1,
50    HAS_XRGB8888 = 2,
51    HAS_RGB565 = 4,
52 };
53
54 static EGLBoolean
55 dri2_wl_swap_interval(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf,
56                       EGLint interval);
57
58 static int
59 roundtrip(struct dri2_egl_display *dri2_dpy)
60 {
61    return wl_display_roundtrip_queue(dri2_dpy->wl_dpy, dri2_dpy->wl_queue);
62 }
63
64 static void
65 wl_buffer_release(void *data, struct wl_buffer *buffer)
66 {
67    struct dri2_egl_surface *dri2_surf = data;
68    int i;
69
70    for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); ++i)
71       if (dri2_surf->color_buffers[i].wl_buffer == buffer)
72          break;
73
74    if (i == ARRAY_SIZE(dri2_surf->color_buffers)) {
75       wl_buffer_destroy(buffer);
76       return;
77    }
78
79    dri2_surf->color_buffers[i].locked = 0;
80 }
81
82 static const struct wl_buffer_listener wl_buffer_listener = {
83    .release = wl_buffer_release
84 };
85
86 static void
87 resize_callback(struct wl_egl_window *wl_win, void *data)
88 {
89    struct dri2_egl_surface *dri2_surf = data;
90    struct dri2_egl_display *dri2_dpy =
91       dri2_egl_display(dri2_surf->base.Resource.Display);
92
93    dri2_dpy->flush->invalidate(dri2_surf->dri_drawable);
94 }
95
96 static void
97 destroy_window_callback(void *data)
98 {
99    struct dri2_egl_surface *dri2_surf = data;
100    dri2_surf->wl_win = NULL;
101 }
102
103 /**
104  * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
105  */
106 static _EGLSurface *
107 dri2_wl_create_window_surface(_EGLDriver *drv, _EGLDisplay *disp,
108                               _EGLConfig *conf, void *native_window,
109                               const EGLint *attrib_list)
110 {
111    __DRIcreateNewDrawableFunc createNewDrawable;
112    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
113    struct dri2_egl_config *dri2_conf = dri2_egl_config(conf);
114    struct wl_egl_window *window = native_window;
115    struct dri2_egl_surface *dri2_surf;
116    const __DRIconfig *config;
117
118    dri2_surf = calloc(1, sizeof *dri2_surf);
119    if (!dri2_surf) {
120       _eglError(EGL_BAD_ALLOC, "dri2_create_surface");
121       return NULL;
122    }
123
124    if (!_eglInitSurface(&dri2_surf->base, disp, EGL_WINDOW_BIT, conf, attrib_list))
125       goto cleanup_surf;
126
127    if (dri2_dpy->wl_drm) {
128       if (conf->RedSize == 5)
129          dri2_surf->format = WL_DRM_FORMAT_RGB565;
130       else if (conf->AlphaSize == 0)
131          dri2_surf->format = WL_DRM_FORMAT_XRGB8888;
132       else
133          dri2_surf->format = WL_DRM_FORMAT_ARGB8888;
134    } else {
135       assert(dri2_dpy->wl_shm);
136       if (conf->RedSize == 5)
137          dri2_surf->format = WL_SHM_FORMAT_RGB565;
138       else if (conf->AlphaSize == 0)
139          dri2_surf->format = WL_SHM_FORMAT_XRGB8888;
140       else
141          dri2_surf->format = WL_SHM_FORMAT_ARGB8888;
142    }
143
144    if (!window) {
145       _eglError(EGL_BAD_NATIVE_WINDOW, "dri2_create_surface");
146       goto cleanup_surf;
147    }
148
149    dri2_surf->wl_win = window;
150    dri2_surf->wl_queue = wl_display_create_queue(dri2_dpy->wl_dpy);
151    if (!dri2_surf->wl_queue) {
152       _eglError(EGL_BAD_ALLOC, "dri2_create_surface");
153       goto cleanup_surf;
154    }
155
156    if (dri2_dpy->wl_drm) {
157       dri2_surf->wl_drm_wrapper = wl_proxy_create_wrapper(dri2_dpy->wl_drm);
158       if (!dri2_surf->wl_drm_wrapper) {
159          _eglError(EGL_BAD_ALLOC, "dri2_create_surface");
160          goto cleanup_queue;
161       }
162       wl_proxy_set_queue((struct wl_proxy *)dri2_surf->wl_drm_wrapper,
163                          dri2_surf->wl_queue);
164    }
165
166    dri2_surf->wl_dpy_wrapper = wl_proxy_create_wrapper(dri2_dpy->wl_dpy);
167    if (!dri2_surf->wl_dpy_wrapper) {
168       _eglError(EGL_BAD_ALLOC, "dri2_create_surface");
169       goto cleanup_drm;
170    }
171    wl_proxy_set_queue((struct wl_proxy *)dri2_surf->wl_dpy_wrapper,
172                       dri2_surf->wl_queue);
173
174    dri2_surf->wl_surface_wrapper = wl_proxy_create_wrapper(window->surface);
175    if (!dri2_surf->wl_surface_wrapper) {
176       _eglError(EGL_BAD_ALLOC, "dri2_create_surface");
177       goto cleanup_dpy_wrapper;
178    }
179    wl_proxy_set_queue((struct wl_proxy *)dri2_surf->wl_surface_wrapper,
180                       dri2_surf->wl_queue);
181
182    dri2_surf->wl_win->private = dri2_surf;
183    dri2_surf->wl_win->destroy_window_callback = destroy_window_callback;
184
185    dri2_surf->base.Width =  -1;
186    dri2_surf->base.Height = -1;
187
188    config = dri2_get_dri_config(dri2_conf, EGL_WINDOW_BIT,
189                                 dri2_surf->base.GLColorspace);
190
191    if (dri2_dpy->dri2) {
192       dri2_surf->wl_win->resize_callback = resize_callback;
193
194       createNewDrawable = dri2_dpy->dri2->createNewDrawable;
195    } else {
196       createNewDrawable = dri2_dpy->swrast->createNewDrawable;
197    }
198
199    dri2_surf->dri_drawable = (*createNewDrawable)(dri2_dpy->dri_screen, config,
200                                                   dri2_surf);
201     if (dri2_surf->dri_drawable == NULL) {
202       _eglError(EGL_BAD_ALLOC, "createNewDrawable");
203        goto cleanup_surf_wrapper;
204     }
205
206    dri2_wl_swap_interval(drv, disp, &dri2_surf->base,
207                          dri2_dpy->default_swap_interval);
208
209    return &dri2_surf->base;
210
211  cleanup_surf_wrapper:
212    wl_proxy_wrapper_destroy(dri2_surf->wl_surface_wrapper);
213  cleanup_dpy_wrapper:
214    wl_proxy_wrapper_destroy(dri2_surf->wl_dpy_wrapper);
215  cleanup_drm:
216    if (dri2_surf->wl_drm_wrapper)
217       wl_proxy_wrapper_destroy(dri2_surf->wl_drm_wrapper);
218  cleanup_queue:
219    wl_event_queue_destroy(dri2_surf->wl_queue);
220  cleanup_surf:
221    free(dri2_surf);
222
223    return NULL;
224 }
225
226 static _EGLSurface *
227 dri2_wl_create_pixmap_surface(_EGLDriver *drv, _EGLDisplay *disp,
228                               _EGLConfig *conf, void *native_window,
229                               const EGLint *attrib_list)
230 {
231    /* From the EGL_EXT_platform_wayland spec, version 3:
232     *
233     *   It is not valid to call eglCreatePlatformPixmapSurfaceEXT with a <dpy>
234     *   that belongs to Wayland. Any such call fails and generates
235     *   EGL_BAD_PARAMETER.
236     */
237    _eglError(EGL_BAD_PARAMETER, "cannot create EGL pixmap surfaces on "
238              "Wayland");
239    return NULL;
240 }
241
242 /**
243  * Called via eglDestroySurface(), drv->API.DestroySurface().
244  */
245 static EGLBoolean
246 dri2_wl_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
247 {
248    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
249    struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
250    int i;
251
252    (void) drv;
253
254    dri2_dpy->core->destroyDrawable(dri2_surf->dri_drawable);
255
256    for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
257       if (dri2_surf->color_buffers[i].wl_buffer)
258          wl_buffer_destroy(dri2_surf->color_buffers[i].wl_buffer);
259       if (dri2_surf->color_buffers[i].dri_image)
260          dri2_dpy->image->destroyImage(dri2_surf->color_buffers[i].dri_image);
261       if (dri2_surf->color_buffers[i].linear_copy)
262          dri2_dpy->image->destroyImage(dri2_surf->color_buffers[i].linear_copy);
263       if (dri2_surf->color_buffers[i].data)
264          munmap(dri2_surf->color_buffers[i].data,
265                 dri2_surf->color_buffers[i].data_size);
266    }
267
268    if (dri2_dpy->dri2) {
269       for (i = 0; i < __DRI_BUFFER_COUNT; i++)
270          if (dri2_surf->dri_buffers[i] &&
271              dri2_surf->dri_buffers[i]->attachment != __DRI_BUFFER_BACK_LEFT)
272             dri2_dpy->dri2->releaseBuffer(dri2_dpy->dri_screen,
273                                           dri2_surf->dri_buffers[i]);
274    }
275
276    if (dri2_surf->throttle_callback)
277       wl_callback_destroy(dri2_surf->throttle_callback);
278
279    if (dri2_surf->wl_win) {
280       dri2_surf->wl_win->private = NULL;
281       dri2_surf->wl_win->resize_callback = NULL;
282       dri2_surf->wl_win->destroy_window_callback = NULL;
283    }
284
285    if (dri2_surf->wl_drm_wrapper)
286       wl_proxy_wrapper_destroy(dri2_surf->wl_drm_wrapper);
287    wl_proxy_wrapper_destroy(dri2_surf->wl_surface_wrapper);
288    wl_proxy_wrapper_destroy(dri2_surf->wl_dpy_wrapper);
289    wl_event_queue_destroy(dri2_surf->wl_queue);
290
291    free(surf);
292
293    return EGL_TRUE;
294 }
295
296 static void
297 dri2_wl_release_buffers(struct dri2_egl_surface *dri2_surf)
298 {
299    struct dri2_egl_display *dri2_dpy =
300       dri2_egl_display(dri2_surf->base.Resource.Display);
301    int i;
302
303    for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
304       if (dri2_surf->color_buffers[i].wl_buffer &&
305           !dri2_surf->color_buffers[i].locked)
306          wl_buffer_destroy(dri2_surf->color_buffers[i].wl_buffer);
307       if (dri2_surf->color_buffers[i].dri_image)
308          dri2_dpy->image->destroyImage(dri2_surf->color_buffers[i].dri_image);
309       if (dri2_surf->color_buffers[i].linear_copy)
310          dri2_dpy->image->destroyImage(dri2_surf->color_buffers[i].linear_copy);
311       if (dri2_surf->color_buffers[i].data)
312          munmap(dri2_surf->color_buffers[i].data,
313                 dri2_surf->color_buffers[i].data_size);
314
315       dri2_surf->color_buffers[i].wl_buffer = NULL;
316       dri2_surf->color_buffers[i].dri_image = NULL;
317       dri2_surf->color_buffers[i].linear_copy = NULL;
318       dri2_surf->color_buffers[i].data = NULL;
319       dri2_surf->color_buffers[i].locked = 0;
320    }
321
322    if (dri2_dpy->dri2) {
323       for (i = 0; i < __DRI_BUFFER_COUNT; i++)
324          if (dri2_surf->dri_buffers[i] &&
325              dri2_surf->dri_buffers[i]->attachment != __DRI_BUFFER_BACK_LEFT)
326             dri2_dpy->dri2->releaseBuffer(dri2_dpy->dri_screen,
327                                           dri2_surf->dri_buffers[i]);
328    }
329 }
330
331 static int
332 get_back_bo(struct dri2_egl_surface *dri2_surf)
333 {
334    struct dri2_egl_display *dri2_dpy =
335       dri2_egl_display(dri2_surf->base.Resource.Display);
336    int i, use_flags;
337    unsigned int dri_image_format;
338
339    /* currently supports three WL DRM formats,
340     * WL_DRM_FORMAT_ARGB8888, WL_DRM_FORMAT_XRGB8888,
341     * and WL_DRM_FORMAT_RGB565
342     */
343    switch (dri2_surf->format) {
344    case WL_DRM_FORMAT_ARGB8888:
345       dri_image_format = __DRI_IMAGE_FORMAT_ARGB8888;
346       break;
347    case WL_DRM_FORMAT_XRGB8888:
348       dri_image_format = __DRI_IMAGE_FORMAT_XRGB8888;
349       break;
350    case WL_DRM_FORMAT_RGB565:
351       dri_image_format = __DRI_IMAGE_FORMAT_RGB565;
352       break;
353    default:
354       /* format is not supported */
355       return -1;
356    }
357
358    /* There might be a buffer release already queued that wasn't processed */
359    wl_display_dispatch_queue_pending(dri2_dpy->wl_dpy, dri2_surf->wl_queue);
360
361    while (dri2_surf->back == NULL) {
362       for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
363          /* Get an unlocked buffer, preferrably one with a dri_buffer
364           * already allocated. */
365          if (dri2_surf->color_buffers[i].locked)
366             continue;
367          if (dri2_surf->back == NULL)
368             dri2_surf->back = &dri2_surf->color_buffers[i];
369          else if (dri2_surf->back->dri_image == NULL)
370             dri2_surf->back = &dri2_surf->color_buffers[i];
371       }
372
373       if (dri2_surf->back)
374          break;
375
376       /* If we don't have a buffer, then block on the server to release one for
377        * us, and try again. wl_display_dispatch_queue will process any pending
378        * events, however not all servers flush on issuing a buffer release
379        * event. So, we spam the server with roundtrips as they always cause a
380        * client flush.
381        */
382       if (wl_display_roundtrip_queue(dri2_dpy->wl_dpy,
383                                      dri2_surf->wl_queue) < 0)
384           return -1;
385    }
386
387    if (dri2_surf->back == NULL)
388       return -1;
389
390    use_flags = __DRI_IMAGE_USE_SHARE | __DRI_IMAGE_USE_BACKBUFFER;
391
392    if (dri2_dpy->is_different_gpu &&
393        dri2_surf->back->linear_copy == NULL) {
394        dri2_surf->back->linear_copy =
395           dri2_dpy->image->createImage(dri2_dpy->dri_screen,
396                                       dri2_surf->base.Width,
397                                       dri2_surf->base.Height,
398                                       dri_image_format,
399                                       use_flags |
400                                       __DRI_IMAGE_USE_LINEAR,
401                                       NULL);
402       if (dri2_surf->back->linear_copy == NULL)
403           return -1;
404    }
405
406    if (dri2_surf->back->dri_image == NULL) {
407       dri2_surf->back->dri_image =
408          dri2_dpy->image->createImage(dri2_dpy->dri_screen,
409                                       dri2_surf->base.Width,
410                                       dri2_surf->base.Height,
411                                       dri_image_format,
412                                       dri2_dpy->is_different_gpu ?
413                                          0 : use_flags,
414                                       NULL);
415       dri2_surf->back->age = 0;
416    }
417    if (dri2_surf->back->dri_image == NULL)
418       return -1;
419
420    dri2_surf->back->locked = 1;
421
422    return 0;
423 }
424
425
426 static void
427 back_bo_to_dri_buffer(struct dri2_egl_surface *dri2_surf, __DRIbuffer *buffer)
428 {
429    struct dri2_egl_display *dri2_dpy =
430       dri2_egl_display(dri2_surf->base.Resource.Display);
431    __DRIimage *image;
432    int name, pitch;
433
434    image = dri2_surf->back->dri_image;
435
436    dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_NAME, &name);
437    dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_STRIDE, &pitch);
438
439    buffer->attachment = __DRI_BUFFER_BACK_LEFT;
440    buffer->name = name;
441    buffer->pitch = pitch;
442    buffer->cpp = 4;
443    buffer->flags = 0;
444 }
445
446 static int
447 get_aux_bo(struct dri2_egl_surface *dri2_surf,
448            unsigned int attachment, unsigned int format, __DRIbuffer *buffer)
449 {
450    struct dri2_egl_display *dri2_dpy =
451       dri2_egl_display(dri2_surf->base.Resource.Display);
452    __DRIbuffer *b = dri2_surf->dri_buffers[attachment];
453
454    if (b == NULL) {
455       b = dri2_dpy->dri2->allocateBuffer(dri2_dpy->dri_screen,
456                                          attachment, format,
457                                          dri2_surf->base.Width,
458                                          dri2_surf->base.Height);
459       dri2_surf->dri_buffers[attachment] = b;
460    }
461    if (b == NULL)
462       return -1;
463
464    memcpy(buffer, b, sizeof *buffer);
465
466    return 0;
467 }
468
469 static int
470 update_buffers(struct dri2_egl_surface *dri2_surf)
471 {
472    struct dri2_egl_display *dri2_dpy =
473       dri2_egl_display(dri2_surf->base.Resource.Display);
474    int i;
475
476    if (dri2_surf->base.Width != dri2_surf->wl_win->width ||
477        dri2_surf->base.Height != dri2_surf->wl_win->height) {
478
479       dri2_wl_release_buffers(dri2_surf);
480
481       dri2_surf->base.Width  = dri2_surf->wl_win->width;
482       dri2_surf->base.Height = dri2_surf->wl_win->height;
483       dri2_surf->dx = dri2_surf->wl_win->dx;
484       dri2_surf->dy = dri2_surf->wl_win->dy;
485    }
486
487    if (get_back_bo(dri2_surf) < 0) {
488       _eglError(EGL_BAD_ALLOC, "failed to allocate color buffer");
489       return -1;
490    }
491
492    /* If we have an extra unlocked buffer at this point, we had to do triple
493     * buffering for a while, but now can go back to just double buffering.
494     * That means we can free any unlocked buffer now. */
495    for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
496       if (!dri2_surf->color_buffers[i].locked &&
497           dri2_surf->color_buffers[i].wl_buffer) {
498          wl_buffer_destroy(dri2_surf->color_buffers[i].wl_buffer);
499          dri2_dpy->image->destroyImage(dri2_surf->color_buffers[i].dri_image);
500          if (dri2_dpy->is_different_gpu)
501             dri2_dpy->image->destroyImage(dri2_surf->color_buffers[i].linear_copy);
502          dri2_surf->color_buffers[i].wl_buffer = NULL;
503          dri2_surf->color_buffers[i].dri_image = NULL;
504          dri2_surf->color_buffers[i].linear_copy = NULL;
505       }
506    }
507
508    return 0;
509 }
510
511 static __DRIbuffer *
512 dri2_wl_get_buffers_with_format(__DRIdrawable * driDrawable,
513                                 int *width, int *height,
514                                 unsigned int *attachments, int count,
515                                 int *out_count, void *loaderPrivate)
516 {
517    struct dri2_egl_surface *dri2_surf = loaderPrivate;
518    int i, j;
519
520    if (update_buffers(dri2_surf) < 0)
521       return NULL;
522
523    for (i = 0, j = 0; i < 2 * count; i += 2, j++) {
524       switch (attachments[i]) {
525       case __DRI_BUFFER_BACK_LEFT:
526          back_bo_to_dri_buffer(dri2_surf, &dri2_surf->buffers[j]);
527          break;
528       default:
529          if (get_aux_bo(dri2_surf, attachments[i], attachments[i + 1],
530                         &dri2_surf->buffers[j]) < 0) {
531             _eglError(EGL_BAD_ALLOC, "failed to allocate aux buffer");
532             return NULL;
533          }
534          break;
535       }
536    }
537
538    *out_count = j;
539    if (j == 0)
540            return NULL;
541
542    *width = dri2_surf->base.Width;
543    *height = dri2_surf->base.Height;
544
545    return dri2_surf->buffers;
546 }
547
548 static __DRIbuffer *
549 dri2_wl_get_buffers(__DRIdrawable * driDrawable,
550                     int *width, int *height,
551                     unsigned int *attachments, int count,
552                     int *out_count, void *loaderPrivate)
553 {
554    struct dri2_egl_surface *dri2_surf = loaderPrivate;
555    unsigned int *attachments_with_format;
556    __DRIbuffer *buffer;
557    unsigned int bpp;
558
559    int i;
560
561    switch (dri2_surf->format) {
562    case WL_DRM_FORMAT_ARGB8888:
563    case WL_DRM_FORMAT_XRGB8888:
564       bpp = 32;
565       break;
566    case WL_DRM_FORMAT_RGB565:
567       bpp = 16;
568       break;
569    default:
570       /* format is not supported */
571       return NULL;
572    }
573
574    attachments_with_format = calloc(count, 2 * sizeof(unsigned int));
575    if (!attachments_with_format) {
576       *out_count = 0;
577       return NULL;
578    }
579
580    for (i = 0; i < count; ++i) {
581       attachments_with_format[2*i] = attachments[i];
582       attachments_with_format[2*i + 1] = bpp;
583    }
584
585    buffer =
586       dri2_wl_get_buffers_with_format(driDrawable,
587                                       width, height,
588                                       attachments_with_format, count,
589                                       out_count, loaderPrivate);
590
591    free(attachments_with_format);
592
593    return buffer;
594 }
595
596 static int
597 image_get_buffers(__DRIdrawable *driDrawable,
598                   unsigned int format,
599                   uint32_t *stamp,
600                   void *loaderPrivate,
601                   uint32_t buffer_mask,
602                   struct __DRIimageList *buffers)
603 {
604    struct dri2_egl_surface *dri2_surf = loaderPrivate;
605
606    if (update_buffers(dri2_surf) < 0)
607       return 0;
608
609    buffers->image_mask = __DRI_IMAGE_BUFFER_BACK;
610    buffers->back = dri2_surf->back->dri_image;
611
612    return 1;
613 }
614
615 static void
616 dri2_wl_flush_front_buffer(__DRIdrawable * driDrawable, void *loaderPrivate)
617 {
618    (void) driDrawable;
619    (void) loaderPrivate;
620 }
621
622 static const __DRIdri2LoaderExtension dri2_loader_extension = {
623    .base = { __DRI_DRI2_LOADER, 3 },
624
625    .getBuffers           = dri2_wl_get_buffers,
626    .flushFrontBuffer     = dri2_wl_flush_front_buffer,
627    .getBuffersWithFormat = dri2_wl_get_buffers_with_format,
628 };
629
630 static const __DRIimageLoaderExtension image_loader_extension = {
631    .base = { __DRI_IMAGE_LOADER, 1 },
632
633    .getBuffers          = image_get_buffers,
634    .flushFrontBuffer    = dri2_wl_flush_front_buffer,
635 };
636
637 static void
638 wayland_throttle_callback(void *data,
639                           struct wl_callback *callback,
640                           uint32_t time)
641 {
642    struct dri2_egl_surface *dri2_surf = data;
643
644    dri2_surf->throttle_callback = NULL;
645    wl_callback_destroy(callback);
646 }
647
648 static const struct wl_callback_listener throttle_listener = {
649    .done = wayland_throttle_callback
650 };
651
652 static void
653 create_wl_buffer(struct dri2_egl_surface *dri2_surf)
654 {
655    struct dri2_egl_display *dri2_dpy =
656       dri2_egl_display(dri2_surf->base.Resource.Display);
657    __DRIimage *image;
658    int fd, stride, name;
659
660    if (dri2_surf->current->wl_buffer != NULL)
661       return;
662
663    if (dri2_dpy->is_different_gpu) {
664       image = dri2_surf->current->linear_copy;
665    } else {
666       image = dri2_surf->current->dri_image;
667    }
668    if (dri2_dpy->capabilities & WL_DRM_CAPABILITY_PRIME) {
669       dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_FD, &fd);
670       dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_STRIDE, &stride);
671
672       dri2_surf->current->wl_buffer =
673          wl_drm_create_prime_buffer(dri2_surf->wl_drm_wrapper,
674                                     fd,
675                                     dri2_surf->base.Width,
676                                     dri2_surf->base.Height,
677                                     dri2_surf->format,
678                                     0, stride,
679                                     0, 0,
680                                     0, 0);
681       close(fd);
682    } else {
683       dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_NAME, &name);
684       dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_STRIDE, &stride);
685
686       dri2_surf->current->wl_buffer =
687          wl_drm_create_buffer(dri2_surf->wl_drm_wrapper,
688                               name,
689                               dri2_surf->base.Width,
690                               dri2_surf->base.Height,
691                               stride,
692                               dri2_surf->format);
693    }
694
695    wl_buffer_add_listener(dri2_surf->current->wl_buffer,
696                           &wl_buffer_listener, dri2_surf);
697 }
698
699 static EGLBoolean
700 try_damage_buffer(struct dri2_egl_surface *dri2_surf,
701                   const EGLint *rects,
702                   EGLint n_rects)
703 {
704    int i;
705
706    if (wl_proxy_get_version((struct wl_proxy *) dri2_surf->wl_surface_wrapper)
707        < WL_SURFACE_DAMAGE_BUFFER_SINCE_VERSION)
708       return EGL_FALSE;
709
710    for (i = 0; i < n_rects; i++) {
711       const int *rect = &rects[i * 4];
712
713       wl_surface_damage_buffer(dri2_surf->wl_surface_wrapper,
714                                rect[0],
715                                dri2_surf->base.Height - rect[1] - rect[3],
716                                rect[2], rect[3]);
717    }
718    return EGL_TRUE;
719 }
720 /**
721  * Called via eglSwapBuffers(), drv->API.SwapBuffers().
722  */
723 static EGLBoolean
724 dri2_wl_swap_buffers_with_damage(_EGLDriver *drv,
725                                  _EGLDisplay *disp,
726                                  _EGLSurface *draw,
727                                  const EGLint *rects,
728                                  EGLint n_rects)
729 {
730    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
731    struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
732    int i;
733
734    while (dri2_surf->throttle_callback != NULL)
735       if (wl_display_dispatch_queue(dri2_dpy->wl_dpy,
736                                     dri2_surf->wl_queue) == -1)
737          return -1;
738
739    for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++)
740       if (dri2_surf->color_buffers[i].age > 0)
741          dri2_surf->color_buffers[i].age++;
742
743    /* Make sure we have a back buffer in case we're swapping without ever
744     * rendering. */
745    if (get_back_bo(dri2_surf) < 0) {
746       _eglError(EGL_BAD_ALLOC, "dri2_swap_buffers");
747       return EGL_FALSE;
748    }
749
750    if (draw->SwapInterval > 0) {
751       dri2_surf->throttle_callback =
752          wl_surface_frame(dri2_surf->wl_surface_wrapper);
753       wl_callback_add_listener(dri2_surf->throttle_callback,
754                                &throttle_listener, dri2_surf);
755    }
756
757    dri2_surf->back->age = 1;
758    dri2_surf->current = dri2_surf->back;
759    dri2_surf->back = NULL;
760
761    create_wl_buffer(dri2_surf);
762
763    wl_surface_attach(dri2_surf->wl_surface_wrapper,
764                      dri2_surf->current->wl_buffer,
765                      dri2_surf->dx, dri2_surf->dy);
766
767    dri2_surf->wl_win->attached_width  = dri2_surf->base.Width;
768    dri2_surf->wl_win->attached_height = dri2_surf->base.Height;
769    /* reset resize growing parameters */
770    dri2_surf->dx = 0;
771    dri2_surf->dy = 0;
772
773    /* If the compositor doesn't support damage_buffer, we deliberately
774     * ignore the damage region and post maximum damage, due to
775     * https://bugs.freedesktop.org/78190 */
776    if (!n_rects || !try_damage_buffer(dri2_surf, rects, n_rects))
777       wl_surface_damage(dri2_surf->wl_surface_wrapper,
778                         0, 0, INT32_MAX, INT32_MAX);
779
780    if (dri2_dpy->is_different_gpu) {
781       _EGLContext *ctx = _eglGetCurrentContext();
782       struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
783       dri2_dpy->image->blitImage(dri2_ctx->dri_context,
784                                  dri2_surf->current->linear_copy,
785                                  dri2_surf->current->dri_image,
786                                  0, 0, dri2_surf->base.Width,
787                                  dri2_surf->base.Height,
788                                  0, 0, dri2_surf->base.Width,
789                                  dri2_surf->base.Height, 0);
790    }
791
792    dri2_flush_drawable_for_swapbuffers(disp, draw);
793    dri2_dpy->flush->invalidate(dri2_surf->dri_drawable);
794
795    wl_surface_commit(dri2_surf->wl_surface_wrapper);
796
797    /* If we're not waiting for a frame callback then we'll at least throttle
798     * to a sync callback so that we always give a chance for the compositor to
799     * handle the commit and send a release event before checking for a free
800     * buffer */
801    if (dri2_surf->throttle_callback == NULL) {
802       dri2_surf->throttle_callback = wl_display_sync(dri2_surf->wl_dpy_wrapper);
803       wl_callback_add_listener(dri2_surf->throttle_callback,
804                                &throttle_listener, dri2_surf);
805    }
806
807    wl_display_flush(dri2_dpy->wl_dpy);
808
809    return EGL_TRUE;
810 }
811
812 static EGLint
813 dri2_wl_query_buffer_age(_EGLDriver *drv,
814                          _EGLDisplay *disp, _EGLSurface *surface)
815 {
816    struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surface);
817
818    if (get_back_bo(dri2_surf) < 0) {
819       _eglError(EGL_BAD_ALLOC, "dri2_query_buffer_age");
820       return -1;
821    }
822
823    return dri2_surf->back->age;
824 }
825
826 static EGLBoolean
827 dri2_wl_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
828 {
829    return dri2_wl_swap_buffers_with_damage (drv, disp, draw, NULL, 0);
830 }
831
832 static struct wl_buffer *
833 dri2_wl_create_wayland_buffer_from_image(_EGLDriver *drv,
834                                           _EGLDisplay *disp,
835                                           _EGLImage *img)
836 {
837    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
838    struct dri2_egl_image *dri2_img = dri2_egl_image(img);
839    __DRIimage *image = dri2_img->dri_image;
840    struct wl_buffer *buffer;
841    int width, height, format, pitch;
842    enum wl_drm_format wl_format;
843
844    dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_FORMAT, &format);
845
846    switch (format) {
847    case __DRI_IMAGE_FORMAT_ARGB8888:
848       if (!(dri2_dpy->formats & HAS_ARGB8888))
849          goto bad_format;
850       wl_format = WL_DRM_FORMAT_ARGB8888;
851       break;
852    case __DRI_IMAGE_FORMAT_XRGB8888:
853       if (!(dri2_dpy->formats & HAS_XRGB8888))
854          goto bad_format;
855       wl_format = WL_DRM_FORMAT_XRGB8888;
856       break;
857    default:
858       goto bad_format;
859    }
860
861    dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_WIDTH, &width);
862    dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_HEIGHT, &height);
863    dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_STRIDE, &pitch);
864
865    if (dri2_dpy->capabilities & WL_DRM_CAPABILITY_PRIME) {
866       int fd;
867
868       dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_FD, &fd);
869
870       buffer =
871          wl_drm_create_prime_buffer(dri2_dpy->wl_drm,
872                                     fd,
873                                     width, height,
874                                     wl_format,
875                                     0, pitch,
876                                     0, 0,
877                                     0, 0);
878
879       close(fd);
880    } else {
881       int name;
882
883       dri2_dpy->image->queryImage(image, __DRI_IMAGE_ATTRIB_NAME, &name);
884
885       buffer =
886          wl_drm_create_buffer(dri2_dpy->wl_drm,
887                               name,
888                               width, height,
889                               pitch,
890                               wl_format);
891    }
892
893    /* The buffer object will have been created with our internal event queue
894     * because it is using the wl_drm object as a proxy factory. We want the
895     * buffer to be used by the application so we'll reset it to the display's
896     * default event queue */
897    if (buffer)
898       wl_proxy_set_queue((struct wl_proxy *) buffer, NULL);
899
900    return buffer;
901
902 bad_format:
903    _eglError(EGL_BAD_MATCH, "unsupported image format");
904    return NULL;
905 }
906
907 static int
908 dri2_wl_authenticate(_EGLDisplay *disp, uint32_t id)
909 {
910    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
911    int ret = 0;
912
913    if (dri2_dpy->is_render_node) {
914       _eglLog(_EGL_WARNING, "wayland-egl: client asks server to "
915                             "authenticate for render-nodes");
916       return 0;
917    }
918    dri2_dpy->authenticated = 0;
919
920    wl_drm_authenticate(dri2_dpy->wl_drm, id);
921    if (roundtrip(dri2_dpy) < 0)
922       ret = -1;
923
924    if (!dri2_dpy->authenticated)
925       ret = -1;
926
927    /* reset authenticated */
928    dri2_dpy->authenticated = 1;
929
930    return ret;
931 }
932
933 static void
934 drm_handle_device(void *data, struct wl_drm *drm, const char *device)
935 {
936    struct dri2_egl_display *dri2_dpy = data;
937    drm_magic_t magic;
938
939    dri2_dpy->device_name = strdup(device);
940    if (!dri2_dpy->device_name)
941       return;
942
943    dri2_dpy->fd = loader_open_device(dri2_dpy->device_name);
944    if (dri2_dpy->fd == -1) {
945       _eglLog(_EGL_WARNING, "wayland-egl: could not open %s (%s)",
946               dri2_dpy->device_name, strerror(errno));
947       return;
948    }
949
950    if (drmGetNodeTypeFromFd(dri2_dpy->fd) == DRM_NODE_RENDER) {
951       dri2_dpy->authenticated = 1;
952    } else {
953       drmGetMagic(dri2_dpy->fd, &magic);
954       wl_drm_authenticate(dri2_dpy->wl_drm, magic);
955    }
956 }
957
958 static void
959 drm_handle_format(void *data, struct wl_drm *drm, uint32_t format)
960 {
961    struct dri2_egl_display *dri2_dpy = data;
962
963    switch (format) {
964    case WL_DRM_FORMAT_ARGB8888:
965       dri2_dpy->formats |= HAS_ARGB8888;
966       break;
967    case WL_DRM_FORMAT_XRGB8888:
968       dri2_dpy->formats |= HAS_XRGB8888;
969       break;
970    case WL_DRM_FORMAT_RGB565:
971       dri2_dpy->formats |= HAS_RGB565;
972       break;
973    }
974 }
975
976 static void
977 drm_handle_capabilities(void *data, struct wl_drm *drm, uint32_t value)
978 {
979    struct dri2_egl_display *dri2_dpy = data;
980
981    dri2_dpy->capabilities = value;
982 }
983
984 static void
985 drm_handle_authenticated(void *data, struct wl_drm *drm)
986 {
987    struct dri2_egl_display *dri2_dpy = data;
988
989    dri2_dpy->authenticated = 1;
990 }
991
992 static const struct wl_drm_listener drm_listener = {
993    .device = drm_handle_device,
994    .format = drm_handle_format,
995    .authenticated = drm_handle_authenticated,
996    .capabilities = drm_handle_capabilities
997 };
998
999 static void
1000 registry_handle_global_drm(void *data, struct wl_registry *registry, uint32_t name,
1001                        const char *interface, uint32_t version)
1002 {
1003    struct dri2_egl_display *dri2_dpy = data;
1004
1005    if (version > 1)
1006       version = 2;
1007    if (strcmp(interface, "wl_drm") == 0) {
1008       dri2_dpy->wl_drm =
1009          wl_registry_bind(registry, name, &wl_drm_interface, version);
1010       wl_drm_add_listener(dri2_dpy->wl_drm, &drm_listener, dri2_dpy);
1011    }
1012 }
1013
1014 static void
1015 registry_handle_global_remove(void *data, struct wl_registry *registry,
1016                               uint32_t name)
1017 {
1018 }
1019
1020 static const struct wl_registry_listener registry_listener_drm = {
1021    .global = registry_handle_global_drm,
1022    .global_remove = registry_handle_global_remove
1023 };
1024
1025 static EGLBoolean
1026 dri2_wl_swap_interval(_EGLDriver *drv,
1027                    _EGLDisplay *disp,
1028                    _EGLSurface *surf,
1029                    EGLint interval)
1030 {
1031    if (interval > surf->Config->MaxSwapInterval)
1032       interval = surf->Config->MaxSwapInterval;
1033    else if (interval < surf->Config->MinSwapInterval)
1034       interval = surf->Config->MinSwapInterval;
1035
1036    surf->SwapInterval = interval;
1037
1038    return EGL_TRUE;
1039 }
1040
1041 static void
1042 dri2_wl_setup_swap_interval(struct dri2_egl_display *dri2_dpy)
1043 {
1044    GLint vblank_mode = DRI_CONF_VBLANK_DEF_INTERVAL_1;
1045
1046    /* We can't use values greater than 1 on Wayland because we are using the
1047     * frame callback to synchronise the frame and the only way we be sure to
1048     * get a frame callback is to attach a new buffer. Therefore we can't just
1049     * sit drawing nothing to wait until the next ‘n’ frame callbacks */
1050
1051    if (dri2_dpy->config)
1052       dri2_dpy->config->configQueryi(dri2_dpy->dri_screen,
1053                                      "vblank_mode", &vblank_mode);
1054    switch (vblank_mode) {
1055    case DRI_CONF_VBLANK_NEVER:
1056       dri2_dpy->min_swap_interval = 0;
1057       dri2_dpy->max_swap_interval = 0;
1058       dri2_dpy->default_swap_interval = 0;
1059       break;
1060    case DRI_CONF_VBLANK_ALWAYS_SYNC:
1061       dri2_dpy->min_swap_interval = 1;
1062       dri2_dpy->max_swap_interval = 1;
1063       dri2_dpy->default_swap_interval = 1;
1064       break;
1065    case DRI_CONF_VBLANK_DEF_INTERVAL_0:
1066       dri2_dpy->min_swap_interval = 0;
1067       dri2_dpy->max_swap_interval = 1;
1068       dri2_dpy->default_swap_interval = 0;
1069       break;
1070    default:
1071    case DRI_CONF_VBLANK_DEF_INTERVAL_1:
1072       dri2_dpy->min_swap_interval = 0;
1073       dri2_dpy->max_swap_interval = 1;
1074       dri2_dpy->default_swap_interval = 1;
1075       break;
1076    }
1077 }
1078
1079 static struct dri2_egl_display_vtbl dri2_wl_display_vtbl = {
1080    .authenticate = dri2_wl_authenticate,
1081    .create_window_surface = dri2_wl_create_window_surface,
1082    .create_pixmap_surface = dri2_wl_create_pixmap_surface,
1083    .create_pbuffer_surface = dri2_fallback_create_pbuffer_surface,
1084    .destroy_surface = dri2_wl_destroy_surface,
1085    .create_image = dri2_create_image_khr,
1086    .swap_interval = dri2_wl_swap_interval,
1087    .swap_buffers = dri2_wl_swap_buffers,
1088    .swap_buffers_with_damage = dri2_wl_swap_buffers_with_damage,
1089    .swap_buffers_region = dri2_fallback_swap_buffers_region,
1090    .post_sub_buffer = dri2_fallback_post_sub_buffer,
1091    .copy_buffers = dri2_fallback_copy_buffers,
1092    .query_buffer_age = dri2_wl_query_buffer_age,
1093    .create_wayland_buffer_from_image = dri2_wl_create_wayland_buffer_from_image,
1094    .get_sync_values = dri2_fallback_get_sync_values,
1095    .get_dri_drawable = dri2_surface_get_dri_drawable,
1096 };
1097
1098 static const __DRIextension *dri2_loader_extensions[] = {
1099    &dri2_loader_extension.base,
1100    &image_loader_extension.base,
1101    &image_lookup_extension.base,
1102    &use_invalidate.base,
1103    NULL,
1104 };
1105
1106 static const __DRIextension *image_loader_extensions[] = {
1107    &image_loader_extension.base,
1108    &image_lookup_extension.base,
1109    &use_invalidate.base,
1110    NULL,
1111 };
1112
1113 static EGLBoolean
1114 dri2_wl_add_configs_for_visuals(_EGLDriver *drv, _EGLDisplay *disp)
1115 {
1116    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1117    static const struct {
1118       const char *format_name;
1119       int has_format;
1120       unsigned int rgba_masks[4];
1121    } visuals[] = {
1122       { "XRGB8888", HAS_XRGB8888, { 0xff0000, 0xff00, 0x00ff, 0xff000000 } },
1123       { "ARGB8888", HAS_ARGB8888, { 0xff0000, 0xff00, 0x00ff, 0 } },
1124       { "RGB565",   HAS_RGB565,   { 0x00f800, 0x07e0, 0x001f, 0 } },
1125    };
1126    unsigned int format_count[ARRAY_SIZE(visuals)] = { 0 };
1127    unsigned int count, i, j;
1128
1129    count = 0;
1130    for (i = 0; dri2_dpy->driver_configs[i]; i++) {
1131       for (j = 0; j < ARRAY_SIZE(visuals); j++) {
1132          struct dri2_egl_config *dri2_conf;
1133
1134          if (!(dri2_dpy->formats & visuals[j].has_format))
1135             continue;
1136
1137          dri2_conf = dri2_add_config(disp, dri2_dpy->driver_configs[i],
1138                count + 1, EGL_WINDOW_BIT, NULL, visuals[j].rgba_masks);
1139          if (dri2_conf) {
1140             if (dri2_conf->base.ConfigID == count + 1)
1141                count++;
1142             format_count[j]++;
1143          }
1144       }
1145    }
1146
1147    for (i = 0; i < ARRAY_SIZE(format_count); i++) {
1148       if (!format_count[i]) {
1149          _eglLog(_EGL_DEBUG, "No DRI config supports native format %s",
1150                  visuals[i].format_name);
1151       }
1152    }
1153
1154    return (count != 0);
1155 }
1156
1157 static EGLBoolean
1158 dri2_initialize_wayland_drm(_EGLDriver *drv, _EGLDisplay *disp)
1159 {
1160    struct dri2_egl_display *dri2_dpy;
1161
1162    loader_set_logger(_eglLog);
1163
1164    dri2_dpy = calloc(1, sizeof *dri2_dpy);
1165    if (!dri2_dpy)
1166       return _eglError(EGL_BAD_ALLOC, "eglInitialize");
1167
1168    disp->DriverData = (void *) dri2_dpy;
1169    if (disp->PlatformDisplay == NULL) {
1170       dri2_dpy->wl_dpy = wl_display_connect(NULL);
1171       if (dri2_dpy->wl_dpy == NULL)
1172          goto cleanup_dpy;
1173       dri2_dpy->own_device = 1;
1174    } else {
1175       dri2_dpy->wl_dpy = disp->PlatformDisplay;
1176    }
1177
1178    dri2_dpy->wl_queue = wl_display_create_queue(dri2_dpy->wl_dpy);
1179
1180    dri2_dpy->wl_dpy_wrapper = wl_proxy_create_wrapper(dri2_dpy->wl_dpy);
1181    if (dri2_dpy->wl_dpy_wrapper == NULL)
1182       goto cleanup_dpy_wrapper;
1183
1184    wl_proxy_set_queue((struct wl_proxy *) dri2_dpy->wl_dpy_wrapper,
1185                       dri2_dpy->wl_queue);
1186
1187    if (dri2_dpy->own_device)
1188       wl_display_dispatch_pending(dri2_dpy->wl_dpy);
1189
1190    dri2_dpy->wl_registry = wl_display_get_registry(dri2_dpy->wl_dpy_wrapper);
1191    wl_registry_add_listener(dri2_dpy->wl_registry,
1192                             &registry_listener_drm, dri2_dpy);
1193    if (roundtrip(dri2_dpy) < 0 || dri2_dpy->wl_drm == NULL)
1194       goto cleanup_registry;
1195
1196    if (roundtrip(dri2_dpy) < 0 || dri2_dpy->fd == -1)
1197       goto cleanup_drm;
1198
1199    if (roundtrip(dri2_dpy) < 0 || !dri2_dpy->authenticated)
1200       goto cleanup_fd;
1201
1202    dri2_dpy->fd = loader_get_user_preferred_fd(dri2_dpy->fd,
1203                                                &dri2_dpy->is_different_gpu);
1204    if (dri2_dpy->is_different_gpu) {
1205       free(dri2_dpy->device_name);
1206       dri2_dpy->device_name = loader_get_device_name_for_fd(dri2_dpy->fd);
1207       if (!dri2_dpy->device_name) {
1208          _eglError(EGL_BAD_ALLOC, "wayland-egl: failed to get device name "
1209                                   "for requested GPU");
1210          goto cleanup_fd;
1211       }
1212    }
1213
1214    /* we have to do the check now, because loader_get_user_preferred_fd
1215     * will return a render-node when the requested gpu is different
1216     * to the server, but also if the client asks for the same gpu than
1217     * the server by requesting its pci-id */
1218    dri2_dpy->is_render_node = drmGetNodeTypeFromFd(dri2_dpy->fd) == DRM_NODE_RENDER;
1219
1220    dri2_dpy->driver_name = loader_get_driver_for_fd(dri2_dpy->fd);
1221    if (dri2_dpy->driver_name == NULL) {
1222       _eglError(EGL_BAD_ALLOC, "DRI2: failed to get driver name");
1223       goto cleanup_fd;
1224    }
1225
1226    if (!dri2_load_driver(disp))
1227       goto cleanup_driver_name;
1228
1229    /* render nodes cannot use Gem names, and thus do not support
1230     * the __DRI_DRI2_LOADER extension */
1231    if (!dri2_dpy->is_render_node)
1232       dri2_dpy->loader_extensions = dri2_loader_extensions;
1233    else
1234       dri2_dpy->loader_extensions = image_loader_extensions;
1235
1236    if (!dri2_create_screen(disp))
1237       goto cleanup_driver;
1238
1239    dri2_wl_setup_swap_interval(dri2_dpy);
1240
1241    /* To use Prime, we must have _DRI_IMAGE v7 at least.
1242     * createImageFromFds support indicates that Prime export/import
1243     * is supported by the driver. Fall back to
1244     * gem names if we don't have Prime support. */
1245
1246    if (dri2_dpy->image->base.version < 7 ||
1247        dri2_dpy->image->createImageFromFds == NULL)
1248       dri2_dpy->capabilities &= ~WL_DRM_CAPABILITY_PRIME;
1249
1250    /* We cannot use Gem names with render-nodes, only prime fds (dma-buf).
1251     * The server needs to accept them */
1252    if (dri2_dpy->is_render_node &&
1253        !(dri2_dpy->capabilities & WL_DRM_CAPABILITY_PRIME)) {
1254       _eglLog(_EGL_WARNING, "wayland-egl: display is not render-node capable");
1255       goto cleanup_screen;
1256    }
1257
1258    if (dri2_dpy->is_different_gpu &&
1259        (dri2_dpy->image->base.version < 9 ||
1260         dri2_dpy->image->blitImage == NULL)) {
1261       _eglLog(_EGL_WARNING, "wayland-egl: Different GPU selected, but the "
1262                             "Image extension in the driver is not "
1263                             "compatible. Version 9 or later and blitImage() "
1264                             "are required");
1265       goto cleanup_screen;
1266    }
1267
1268    if (!dri2_wl_add_configs_for_visuals(drv, disp)) {
1269       _eglError(EGL_NOT_INITIALIZED, "DRI2: failed to add configs");
1270       goto cleanup_screen;
1271    }
1272
1273    dri2_set_WL_bind_wayland_display(drv, disp);
1274    /* When cannot convert EGLImage to wl_buffer when on a different gpu,
1275     * because the buffer of the EGLImage has likely a tiling mode the server
1276     * gpu won't support. These is no way to check for now. Thus do not support the
1277     * extension */
1278    if (!dri2_dpy->is_different_gpu) {
1279       disp->Extensions.WL_create_wayland_buffer_from_image = EGL_TRUE;
1280    } else {
1281       dri2_wl_display_vtbl.create_wayland_buffer_from_image =
1282          dri2_fallback_create_wayland_buffer_from_image;
1283    }
1284    disp->Extensions.EXT_buffer_age = EGL_TRUE;
1285
1286    disp->Extensions.EXT_swap_buffers_with_damage = EGL_TRUE;
1287
1288    /* Fill vtbl last to prevent accidentally calling virtual function during
1289     * initialization.
1290     */
1291    dri2_dpy->vtbl = &dri2_wl_display_vtbl;
1292
1293    return EGL_TRUE;
1294
1295  cleanup_screen:
1296    dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen);
1297  cleanup_driver:
1298    dlclose(dri2_dpy->driver);
1299  cleanup_driver_name:
1300    free(dri2_dpy->driver_name);
1301  cleanup_fd:
1302    close(dri2_dpy->fd);
1303  cleanup_drm:
1304    free(dri2_dpy->device_name);
1305    wl_drm_destroy(dri2_dpy->wl_drm);
1306  cleanup_registry:
1307    wl_registry_destroy(dri2_dpy->wl_registry);
1308    wl_proxy_wrapper_destroy(dri2_dpy->wl_dpy_wrapper);
1309  cleanup_dpy_wrapper:
1310    wl_event_queue_destroy(dri2_dpy->wl_queue);
1311    if (disp->PlatformDisplay == NULL)
1312       wl_display_disconnect(dri2_dpy->wl_dpy);
1313  cleanup_dpy:
1314    free(dri2_dpy);
1315    disp->DriverData = NULL;
1316
1317    return EGL_FALSE;
1318 }
1319
1320 static int
1321 dri2_wl_swrast_get_stride_for_format(int format, int w)
1322 {
1323    if (format == WL_SHM_FORMAT_RGB565)
1324       return 2 * w;
1325    else /* ARGB8888 || XRGB8888 */
1326       return 4 * w;
1327 }
1328
1329 /*
1330  * Taken from weston shared/os-compatibility.c
1331  */
1332
1333 #ifndef HAVE_MKOSTEMP
1334
1335 static int
1336 set_cloexec_or_close(int fd)
1337 {
1338    long flags;
1339
1340    if (fd == -1)
1341       return -1;
1342
1343    flags = fcntl(fd, F_GETFD);
1344    if (flags == -1)
1345       goto err;
1346
1347    if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1)
1348       goto err;
1349
1350    return fd;
1351
1352 err:
1353    close(fd);
1354    return -1;
1355 }
1356
1357 #endif
1358
1359 /*
1360  * Taken from weston shared/os-compatibility.c
1361  */
1362
1363 static int
1364 create_tmpfile_cloexec(char *tmpname)
1365 {
1366    int fd;
1367
1368 #ifdef HAVE_MKOSTEMP
1369    fd = mkostemp(tmpname, O_CLOEXEC);
1370    if (fd >= 0)
1371       unlink(tmpname);
1372 #else
1373    fd = mkstemp(tmpname);
1374    if (fd >= 0) {
1375       fd = set_cloexec_or_close(fd);
1376       unlink(tmpname);
1377    }
1378 #endif
1379
1380    return fd;
1381 }
1382
1383 /*
1384  * Taken from weston shared/os-compatibility.c
1385  *
1386  * Create a new, unique, anonymous file of the given size, and
1387  * return the file descriptor for it. The file descriptor is set
1388  * CLOEXEC. The file is immediately suitable for mmap()'ing
1389  * the given size at offset zero.
1390  *
1391  * The file should not have a permanent backing store like a disk,
1392  * but may have if XDG_RUNTIME_DIR is not properly implemented in OS.
1393  *
1394  * The file name is deleted from the file system.
1395  *
1396  * The file is suitable for buffer sharing between processes by
1397  * transmitting the file descriptor over Unix sockets using the
1398  * SCM_RIGHTS methods.
1399  *
1400  * If the C library implements posix_fallocate(), it is used to
1401  * guarantee that disk space is available for the file at the
1402  * given size. If disk space is insufficent, errno is set to ENOSPC.
1403  * If posix_fallocate() is not supported, program may receive
1404  * SIGBUS on accessing mmap()'ed file contents instead.
1405  */
1406 static int
1407 os_create_anonymous_file(off_t size)
1408 {
1409    static const char template[] = "/mesa-shared-XXXXXX";
1410    const char *path;
1411    char *name;
1412    int fd;
1413    int ret;
1414
1415    path = getenv("XDG_RUNTIME_DIR");
1416    if (!path) {
1417       errno = ENOENT;
1418       return -1;
1419    }
1420
1421    name = malloc(strlen(path) + sizeof(template));
1422    if (!name)
1423       return -1;
1424
1425    strcpy(name, path);
1426    strcat(name, template);
1427
1428    fd = create_tmpfile_cloexec(name);
1429
1430    free(name);
1431
1432    if (fd < 0)
1433       return -1;
1434
1435    ret = ftruncate(fd, size);
1436    if (ret < 0) {
1437       close(fd);
1438       return -1;
1439    }
1440
1441    return fd;
1442 }
1443
1444
1445 static EGLBoolean
1446 dri2_wl_swrast_allocate_buffer(struct dri2_egl_surface *dri2_surf,
1447                                int format, int w, int h,
1448                                void **data, int *size,
1449                                struct wl_buffer **buffer)
1450 {
1451    struct dri2_egl_display *dri2_dpy =
1452       dri2_egl_display(dri2_surf->base.Resource.Display);
1453    struct wl_shm_pool *pool;
1454    int fd, stride, size_map;
1455    void *data_map;
1456
1457    stride = dri2_wl_swrast_get_stride_for_format(format, w);
1458    size_map = h * stride;
1459
1460    /* Create a sharable buffer */
1461    fd = os_create_anonymous_file(size_map);
1462    if (fd < 0)
1463       return EGL_FALSE;
1464
1465    data_map = mmap(NULL, size_map, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
1466    if (data_map == MAP_FAILED) {
1467       close(fd);
1468       return EGL_FALSE;
1469    }
1470
1471    /* Share it in a wl_buffer */
1472    pool = wl_shm_create_pool(dri2_dpy->wl_shm, fd, size_map);
1473    wl_proxy_set_queue((struct wl_proxy *)pool, dri2_surf->wl_queue);
1474    *buffer = wl_shm_pool_create_buffer(pool, 0, w, h, stride, format);
1475    wl_shm_pool_destroy(pool);
1476    close(fd);
1477
1478    *data = data_map;
1479    *size = size_map;
1480    return EGL_TRUE;
1481 }
1482
1483 static int
1484 swrast_update_buffers(struct dri2_egl_surface *dri2_surf)
1485 {
1486    struct dri2_egl_display *dri2_dpy =
1487       dri2_egl_display(dri2_surf->base.Resource.Display);
1488    int i;
1489
1490    /* we need to do the following operations only once per frame */
1491    if (dri2_surf->back)
1492       return 0;
1493
1494    if (dri2_surf->base.Width != dri2_surf->wl_win->width ||
1495        dri2_surf->base.Height != dri2_surf->wl_win->height) {
1496
1497       dri2_wl_release_buffers(dri2_surf);
1498
1499       dri2_surf->base.Width  = dri2_surf->wl_win->width;
1500       dri2_surf->base.Height = dri2_surf->wl_win->height;
1501       dri2_surf->dx = dri2_surf->wl_win->dx;
1502       dri2_surf->dy = dri2_surf->wl_win->dy;
1503       dri2_surf->current = NULL;
1504    }
1505
1506    /* find back buffer */
1507
1508    /* There might be a buffer release already queued that wasn't processed */
1509    wl_display_dispatch_queue_pending(dri2_dpy->wl_dpy, dri2_surf->wl_queue);
1510
1511    /* try get free buffer already created */
1512    for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
1513       if (!dri2_surf->color_buffers[i].locked &&
1514           dri2_surf->color_buffers[i].wl_buffer) {
1515           dri2_surf->back = &dri2_surf->color_buffers[i];
1516           break;
1517       }
1518    }
1519
1520    /* else choose any another free location */
1521    if (!dri2_surf->back) {
1522       for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
1523          if (!dri2_surf->color_buffers[i].locked) {
1524              dri2_surf->back = &dri2_surf->color_buffers[i];
1525              if (!dri2_wl_swrast_allocate_buffer(dri2_surf,
1526                                                  dri2_surf->format,
1527                                                  dri2_surf->base.Width,
1528                                                  dri2_surf->base.Height,
1529                                                  &dri2_surf->back->data,
1530                                                  &dri2_surf->back->data_size,
1531                                                  &dri2_surf->back->wl_buffer)) {
1532                 _eglError(EGL_BAD_ALLOC, "failed to allocate color buffer");
1533                  return -1;
1534              }
1535              wl_buffer_add_listener(dri2_surf->back->wl_buffer,
1536                                     &wl_buffer_listener, dri2_surf);
1537              break;
1538          }
1539       }
1540    }
1541
1542    if (!dri2_surf->back) {
1543       _eglError(EGL_BAD_ALLOC, "failed to find free buffer");
1544       return -1;
1545    }
1546
1547    dri2_surf->back->locked = 1;
1548
1549    /* If we have an extra unlocked buffer at this point, we had to do triple
1550     * buffering for a while, but now can go back to just double buffering.
1551     * That means we can free any unlocked buffer now. */
1552    for (i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
1553       if (!dri2_surf->color_buffers[i].locked &&
1554           dri2_surf->color_buffers[i].wl_buffer) {
1555          wl_buffer_destroy(dri2_surf->color_buffers[i].wl_buffer);
1556          munmap(dri2_surf->color_buffers[i].data,
1557                 dri2_surf->color_buffers[i].data_size);
1558          dri2_surf->color_buffers[i].wl_buffer = NULL;
1559          dri2_surf->color_buffers[i].data = NULL;
1560       }
1561    }
1562
1563    return 0;
1564 }
1565
1566 static void*
1567 dri2_wl_swrast_get_frontbuffer_data(struct dri2_egl_surface *dri2_surf)
1568 {
1569    /* if there has been a resize: */
1570    if (!dri2_surf->current)
1571       return NULL;
1572
1573    return dri2_surf->current->data;
1574 }
1575
1576 static void*
1577 dri2_wl_swrast_get_backbuffer_data(struct dri2_egl_surface *dri2_surf)
1578 {
1579    assert(dri2_surf->back);
1580    return dri2_surf->back->data;
1581 }
1582
1583 static void
1584 dri2_wl_swrast_commit_backbuffer(struct dri2_egl_surface *dri2_surf)
1585 {
1586    struct dri2_egl_display *dri2_dpy = dri2_egl_display(dri2_surf->base.Resource.Display);
1587
1588    while (dri2_surf->throttle_callback != NULL)
1589       if (wl_display_dispatch_queue(dri2_dpy->wl_dpy,
1590                                     dri2_surf->wl_queue) == -1)
1591          return;
1592
1593    if (dri2_surf->base.SwapInterval > 0) {
1594       dri2_surf->throttle_callback =
1595          wl_surface_frame(dri2_surf->wl_surface_wrapper);
1596       wl_callback_add_listener(dri2_surf->throttle_callback,
1597                                &throttle_listener, dri2_surf);
1598    }
1599
1600    dri2_surf->current = dri2_surf->back;
1601    dri2_surf->back = NULL;
1602
1603    wl_surface_attach(dri2_surf->wl_surface_wrapper,
1604                      dri2_surf->current->wl_buffer,
1605                      dri2_surf->dx, dri2_surf->dy);
1606
1607    dri2_surf->wl_win->attached_width  = dri2_surf->base.Width;
1608    dri2_surf->wl_win->attached_height = dri2_surf->base.Height;
1609    /* reset resize growing parameters */
1610    dri2_surf->dx = 0;
1611    dri2_surf->dy = 0;
1612
1613    wl_surface_damage(dri2_surf->wl_surface_wrapper,
1614                      0, 0, INT32_MAX, INT32_MAX);
1615    wl_surface_commit(dri2_surf->wl_surface_wrapper);
1616
1617    /* If we're not waiting for a frame callback then we'll at least throttle
1618     * to a sync callback so that we always give a chance for the compositor to
1619     * handle the commit and send a release event before checking for a free
1620     * buffer */
1621    if (dri2_surf->throttle_callback == NULL) {
1622       dri2_surf->throttle_callback = wl_display_sync(dri2_dpy->wl_dpy_wrapper);
1623       wl_callback_add_listener(dri2_surf->throttle_callback,
1624                                &throttle_listener, dri2_surf);
1625    }
1626
1627    wl_display_flush(dri2_dpy->wl_dpy);
1628 }
1629
1630 static void
1631 dri2_wl_swrast_get_drawable_info(__DRIdrawable * draw,
1632                                  int *x, int *y, int *w, int *h,
1633                                  void *loaderPrivate)
1634 {
1635    struct dri2_egl_surface *dri2_surf = loaderPrivate;
1636
1637    (void) swrast_update_buffers(dri2_surf);
1638    *x = 0;
1639    *y = 0;
1640    *w = dri2_surf->base.Width;
1641    *h = dri2_surf->base.Height;
1642 }
1643
1644 static void
1645 dri2_wl_swrast_get_image(__DRIdrawable * read,
1646                          int x, int y, int w, int h,
1647                          char *data, void *loaderPrivate)
1648 {
1649    struct dri2_egl_surface *dri2_surf = loaderPrivate;
1650    int copy_width = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, w);
1651    int x_offset = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, x);
1652    int src_stride = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, dri2_surf->base.Width);
1653    int dst_stride = copy_width;
1654    char *src, *dst;
1655
1656    src = dri2_wl_swrast_get_frontbuffer_data(dri2_surf);
1657    if (!src) {
1658       memset(data, 0, copy_width * h);
1659       return;
1660    }
1661
1662    assert(data != src);
1663    assert(copy_width <= src_stride);
1664
1665    src += x_offset;
1666    src += y * src_stride;
1667    dst = data;
1668
1669    if (copy_width > src_stride-x_offset)
1670       copy_width = src_stride-x_offset;
1671    if (h > dri2_surf->base.Height-y)
1672       h = dri2_surf->base.Height-y;
1673
1674    for (; h>0; h--) {
1675       memcpy(dst, src, copy_width);
1676       src += src_stride;
1677       dst += dst_stride;
1678    }
1679 }
1680
1681 static void
1682 dri2_wl_swrast_put_image2(__DRIdrawable * draw, int op,
1683                          int x, int y, int w, int h, int stride,
1684                          char *data, void *loaderPrivate)
1685 {
1686    struct dri2_egl_surface *dri2_surf = loaderPrivate;
1687    int copy_width = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, w);
1688    int dst_stride = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, dri2_surf->base.Width);
1689    int x_offset = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, x);
1690    char *src, *dst;
1691
1692    assert(copy_width <= stride);
1693
1694    (void) swrast_update_buffers(dri2_surf);
1695    dst = dri2_wl_swrast_get_backbuffer_data(dri2_surf);
1696
1697    /* partial copy, copy old content */
1698    if (copy_width < dst_stride)
1699       dri2_wl_swrast_get_image(draw, 0, 0,
1700                                dri2_surf->base.Width, dri2_surf->base.Height,
1701                                dst, loaderPrivate);
1702
1703    dst += x_offset;
1704    dst += y * dst_stride;
1705
1706    src = data;
1707
1708    /* drivers expect we do these checks (and some rely on it) */
1709    if (copy_width > dst_stride-x_offset)
1710       copy_width = dst_stride-x_offset;
1711    if (h > dri2_surf->base.Height-y)
1712       h = dri2_surf->base.Height-y;
1713
1714    for (; h>0; h--) {
1715       memcpy(dst, src, copy_width);
1716       src += stride;
1717       dst += dst_stride;
1718    }
1719    dri2_wl_swrast_commit_backbuffer(dri2_surf);
1720 }
1721
1722 static void
1723 dri2_wl_swrast_put_image(__DRIdrawable * draw, int op,
1724                          int x, int y, int w, int h,
1725                          char *data, void *loaderPrivate)
1726 {
1727    struct dri2_egl_surface *dri2_surf = loaderPrivate;
1728    int stride;
1729
1730    stride = dri2_wl_swrast_get_stride_for_format(dri2_surf->format, w);
1731    dri2_wl_swrast_put_image2(draw, op, x, y, w, h,
1732                              stride, data, loaderPrivate);
1733 }
1734
1735 static EGLBoolean
1736 dri2_wl_swrast_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
1737 {
1738    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
1739    struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
1740
1741    dri2_dpy->core->swapBuffers(dri2_surf->dri_drawable);
1742    return EGL_TRUE;
1743 }
1744
1745 static void
1746 shm_handle_format(void *data, struct wl_shm *shm, uint32_t format)
1747 {
1748    struct dri2_egl_display *dri2_dpy = data;
1749
1750    switch (format) {
1751    case WL_SHM_FORMAT_ARGB8888:
1752       dri2_dpy->formats |= HAS_ARGB8888;
1753       break;
1754    case WL_SHM_FORMAT_XRGB8888:
1755       dri2_dpy->formats |= HAS_XRGB8888;
1756       break;
1757    case WL_SHM_FORMAT_RGB565:
1758       dri2_dpy->formats |= HAS_RGB565;
1759       break;
1760    }
1761 }
1762
1763 static const struct wl_shm_listener shm_listener = {
1764    .format = shm_handle_format
1765 };
1766
1767 static void
1768 registry_handle_global_swrast(void *data, struct wl_registry *registry, uint32_t name,
1769                               const char *interface, uint32_t version)
1770 {
1771    struct dri2_egl_display *dri2_dpy = data;
1772
1773    if (strcmp(interface, "wl_shm") == 0) {
1774       dri2_dpy->wl_shm =
1775          wl_registry_bind(registry, name, &wl_shm_interface, 1);
1776       wl_shm_add_listener(dri2_dpy->wl_shm, &shm_listener, dri2_dpy);
1777    }
1778 }
1779
1780 static const struct wl_registry_listener registry_listener_swrast = {
1781    .global = registry_handle_global_swrast,
1782    .global_remove = registry_handle_global_remove
1783 };
1784
1785 static struct dri2_egl_display_vtbl dri2_wl_swrast_display_vtbl = {
1786    .authenticate = NULL,
1787    .create_window_surface = dri2_wl_create_window_surface,
1788    .create_pixmap_surface = dri2_wl_create_pixmap_surface,
1789    .create_pbuffer_surface = dri2_fallback_create_pbuffer_surface,
1790    .destroy_surface = dri2_wl_destroy_surface,
1791    .create_image = dri2_fallback_create_image_khr,
1792    .swap_interval = dri2_wl_swap_interval,
1793    .swap_buffers = dri2_wl_swrast_swap_buffers,
1794    .swap_buffers_with_damage = dri2_fallback_swap_buffers_with_damage,
1795    .swap_buffers_region = dri2_fallback_swap_buffers_region,
1796    .post_sub_buffer = dri2_fallback_post_sub_buffer,
1797    .copy_buffers = dri2_fallback_copy_buffers,
1798    .query_buffer_age = dri2_fallback_query_buffer_age,
1799    .create_wayland_buffer_from_image = dri2_fallback_create_wayland_buffer_from_image,
1800    .get_sync_values = dri2_fallback_get_sync_values,
1801    .get_dri_drawable = dri2_surface_get_dri_drawable,
1802 };
1803
1804 static const __DRIswrastLoaderExtension swrast_loader_extension = {
1805    .base = { __DRI_SWRAST_LOADER, 2 },
1806
1807    .getDrawableInfo = dri2_wl_swrast_get_drawable_info,
1808    .putImage        = dri2_wl_swrast_put_image,
1809    .getImage        = dri2_wl_swrast_get_image,
1810    .putImage2       = dri2_wl_swrast_put_image2,
1811 };
1812
1813 static const __DRIextension *swrast_loader_extensions[] = {
1814    &swrast_loader_extension.base,
1815    NULL,
1816 };
1817
1818 static EGLBoolean
1819 dri2_initialize_wayland_swrast(_EGLDriver *drv, _EGLDisplay *disp)
1820 {
1821    struct dri2_egl_display *dri2_dpy;
1822
1823    loader_set_logger(_eglLog);
1824
1825    dri2_dpy = calloc(1, sizeof *dri2_dpy);
1826    if (!dri2_dpy)
1827       return _eglError(EGL_BAD_ALLOC, "eglInitialize");
1828
1829    disp->DriverData = (void *) dri2_dpy;
1830    if (disp->PlatformDisplay == NULL) {
1831       dri2_dpy->wl_dpy = wl_display_connect(NULL);
1832       if (dri2_dpy->wl_dpy == NULL)
1833          goto cleanup_dpy;
1834       dri2_dpy->own_device = 1;
1835    } else {
1836       dri2_dpy->wl_dpy = disp->PlatformDisplay;
1837    }
1838
1839    dri2_dpy->wl_queue = wl_display_create_queue(dri2_dpy->wl_dpy);
1840
1841    dri2_dpy->wl_dpy_wrapper = wl_proxy_create_wrapper(dri2_dpy->wl_dpy);
1842    if (dri2_dpy->wl_dpy_wrapper == NULL)
1843       goto cleanup_dpy_wrapper;
1844
1845    wl_proxy_set_queue((struct wl_proxy *) dri2_dpy->wl_dpy_wrapper,
1846                       dri2_dpy->wl_queue);
1847
1848    if (dri2_dpy->own_device)
1849       wl_display_dispatch_pending(dri2_dpy->wl_dpy);
1850
1851    dri2_dpy->wl_registry = wl_display_get_registry(dri2_dpy->wl_dpy_wrapper);
1852    wl_registry_add_listener(dri2_dpy->wl_registry,
1853                             &registry_listener_swrast, dri2_dpy);
1854
1855    if (roundtrip(dri2_dpy) < 0 || dri2_dpy->wl_shm == NULL)
1856       goto cleanup_registry;
1857
1858    if (roundtrip(dri2_dpy) < 0 || dri2_dpy->formats == 0)
1859       goto cleanup_shm;
1860
1861    dri2_dpy->fd = -1;
1862    dri2_dpy->driver_name = strdup("swrast");
1863    if (!dri2_load_driver_swrast(disp))
1864       goto cleanup_shm;
1865
1866    dri2_dpy->loader_extensions = swrast_loader_extensions;
1867
1868    if (!dri2_create_screen(disp))
1869       goto cleanup_driver;
1870
1871    dri2_wl_setup_swap_interval(dri2_dpy);
1872
1873    if (!dri2_wl_add_configs_for_visuals(drv, disp)) {
1874       _eglError(EGL_NOT_INITIALIZED, "DRI2: failed to add configs");
1875       goto cleanup_screen;
1876    }
1877
1878    /* Fill vtbl last to prevent accidentally calling virtual function during
1879     * initialization.
1880     */
1881    dri2_dpy->vtbl = &dri2_wl_swrast_display_vtbl;
1882
1883    return EGL_TRUE;
1884
1885  cleanup_screen:
1886    dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen);
1887  cleanup_driver:
1888    dlclose(dri2_dpy->driver);
1889  cleanup_shm:
1890    wl_shm_destroy(dri2_dpy->wl_shm);
1891  cleanup_registry:
1892    wl_registry_destroy(dri2_dpy->wl_registry);
1893    wl_proxy_wrapper_destroy(dri2_dpy->wl_dpy_wrapper);
1894  cleanup_dpy_wrapper:
1895    wl_event_queue_destroy(dri2_dpy->wl_queue);
1896    if (disp->PlatformDisplay == NULL)
1897       wl_display_disconnect(dri2_dpy->wl_dpy);
1898  cleanup_dpy:
1899    free(dri2_dpy);
1900    disp->DriverData = NULL;
1901
1902    return EGL_FALSE;
1903 }
1904
1905 EGLBoolean
1906 dri2_initialize_wayland(_EGLDriver *drv, _EGLDisplay *disp)
1907 {
1908    EGLBoolean initialized = EGL_TRUE;
1909
1910    int hw_accel = (getenv("LIBGL_ALWAYS_SOFTWARE") == NULL);
1911
1912    if (hw_accel) {
1913       if (!dri2_initialize_wayland_drm(drv, disp)) {
1914          initialized = dri2_initialize_wayland_swrast(drv, disp);
1915       }
1916    } else {
1917       initialized = dri2_initialize_wayland_swrast(drv, disp);
1918    }
1919
1920    return initialized;
1921
1922 }