OSDN Git Service

egl: Remove every double semi-colon
[android-x86/external-mesa.git] / src / egl / drivers / dri2 / platform_android.c
1 /*
2  * Mesa 3-D graphics library
3  *
4  * Copyright (C) 2010-2011 Chia-I Wu <olvaffe@gmail.com>
5  * Copyright (C) 2010-2011 LunarG Inc.
6  *
7  * Based on platform_x11, which has
8  *
9  * Copyright © 2011 Intel Corporation
10  *
11  * Permission is hereby granted, free of charge, to any person obtaining a
12  * copy of this software and associated documentation files (the "Software"),
13  * to deal in the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15  * and/or sell copies of the Software, and to permit persons to whom the
16  * Software is furnished to do so, subject to the following conditions:
17  *
18  * The above copyright notice and this permission notice shall be included
19  * in all copies or substantial portions of the Software.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
24  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  */
29
30 #include <errno.h>
31 #include <dlfcn.h>
32
33 #if ANDROID_VERSION >= 0x402
34 #include <sync/sync.h>
35 #endif
36
37 #include "loader.h"
38 #include "egl_dri2.h"
39 #include "egl_dri2_fallbacks.h"
40 #include "gralloc_drm.h"
41
42 static int
43 get_format_bpp(int native)
44 {
45    int bpp;
46
47    switch (native) {
48    case HAL_PIXEL_FORMAT_RGBA_8888:
49    case HAL_PIXEL_FORMAT_RGBX_8888:
50    case HAL_PIXEL_FORMAT_BGRA_8888:
51       bpp = 4;
52       break;
53    case HAL_PIXEL_FORMAT_RGB_888:
54       bpp = 3;
55       break;
56    case HAL_PIXEL_FORMAT_RGB_565:
57       bpp = 2;
58       break;
59    default:
60       bpp = 0;
61       break;
62    }
63
64    return bpp;
65 }
66
67 static int
68 get_native_buffer_name(struct ANativeWindowBuffer *buf)
69 {
70    return gralloc_drm_get_gem_handle(buf->handle);
71 }
72
73 static EGLBoolean
74 droid_window_dequeue_buffer(struct dri2_egl_surface *dri2_surf)
75 {
76 #if ANDROID_VERSION >= 0x0402
77    int fence_fd;
78
79    if (dri2_surf->window->dequeueBuffer(dri2_surf->window, &dri2_surf->buffer,
80                                         &fence_fd))
81       return EGL_FALSE;
82
83    /* If access to the buffer is controlled by a sync fence, then block on the
84     * fence.
85     *
86     * It may be more performant to postpone blocking until there is an
87     * immediate need to write to the buffer. But doing so would require adding
88     * hooks to the DRI2 loader.
89     *
90     * From the ANativeWindow::dequeueBuffer documentation:
91     *
92     *    The libsync fence file descriptor returned in the int pointed to by
93     *    the fenceFd argument will refer to the fence that must signal
94     *    before the dequeued buffer may be written to.  A value of -1
95     *    indicates that the caller may access the buffer immediately without
96     *    waiting on a fence.  If a valid file descriptor is returned (i.e.
97     *    any value except -1) then the caller is responsible for closing the
98     *    file descriptor.
99     */
100     if (fence_fd >= 0) {
101        /* From the SYNC_IOC_WAIT documentation in <linux/sync.h>:
102         *
103         *    Waits indefinitely if timeout < 0.
104         */
105         int timeout = -1;
106         sync_wait(fence_fd, timeout);
107         close(fence_fd);
108    }
109
110    dri2_surf->buffer->common.incRef(&dri2_surf->buffer->common);
111 #else
112    if (dri2_surf->window->dequeueBuffer(dri2_surf->window, &dri2_surf->buffer))
113       return EGL_FALSE;
114
115    dri2_surf->buffer->common.incRef(&dri2_surf->buffer->common);
116    dri2_surf->window->lockBuffer(dri2_surf->window, dri2_surf->buffer);
117 #endif
118
119    return EGL_TRUE;
120 }
121
122 static EGLBoolean
123 droid_window_enqueue_buffer(struct dri2_egl_surface *dri2_surf)
124 {
125 #if ANDROID_VERSION >= 0x0402
126    /* Queue the buffer without a sync fence. This informs the ANativeWindow
127     * that it may access the buffer immediately.
128     *
129     * From ANativeWindow::dequeueBuffer:
130     *
131     *    The fenceFd argument specifies a libsync fence file descriptor for
132     *    a fence that must signal before the buffer can be accessed.  If
133     *    the buffer can be accessed immediately then a value of -1 should
134     *    be used.  The caller must not use the file descriptor after it
135     *    is passed to queueBuffer, and the ANativeWindow implementation
136     *    is responsible for closing it.
137     */
138    int fence_fd = -1;
139    dri2_surf->window->queueBuffer(dri2_surf->window, dri2_surf->buffer,
140                                   fence_fd);
141 #else
142    dri2_surf->window->queueBuffer(dri2_surf->window, dri2_surf->buffer);
143 #endif
144
145    dri2_surf->buffer->common.decRef(&dri2_surf->buffer->common);
146    dri2_surf->buffer = NULL;
147
148    return EGL_TRUE;
149 }
150
151 static void
152 droid_window_cancel_buffer(struct dri2_egl_surface *dri2_surf)
153 {
154    /* no cancel buffer? */
155    droid_window_enqueue_buffer(dri2_surf);
156 }
157
158 static __DRIbuffer *
159 droid_alloc_local_buffer(struct dri2_egl_surface *dri2_surf,
160                          unsigned int att, unsigned int format)
161 {
162    struct dri2_egl_display *dri2_dpy =
163       dri2_egl_display(dri2_surf->base.Resource.Display);
164
165    if (att >= ARRAY_SIZE(dri2_surf->local_buffers))
166       return NULL;
167
168    if (!dri2_surf->local_buffers[att]) {
169       dri2_surf->local_buffers[att] =
170          dri2_dpy->dri2->allocateBuffer(dri2_dpy->dri_screen, att, format,
171                dri2_surf->base.Width, dri2_surf->base.Height);
172    }
173
174    return dri2_surf->local_buffers[att];
175 }
176
177 static void
178 droid_free_local_buffers(struct dri2_egl_surface *dri2_surf)
179 {
180    struct dri2_egl_display *dri2_dpy =
181       dri2_egl_display(dri2_surf->base.Resource.Display);
182    int i;
183
184    for (i = 0; i < ARRAY_SIZE(dri2_surf->local_buffers); i++) {
185       if (dri2_surf->local_buffers[i]) {
186          dri2_dpy->dri2->releaseBuffer(dri2_dpy->dri_screen,
187                dri2_surf->local_buffers[i]);
188          dri2_surf->local_buffers[i] = NULL;
189       }
190    }
191 }
192
193 static _EGLSurface *
194 droid_create_surface(_EGLDriver *drv, _EGLDisplay *disp, EGLint type,
195                     _EGLConfig *conf, void *native_window,
196                     const EGLint *attrib_list)
197 {
198    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
199    struct dri2_egl_config *dri2_conf = dri2_egl_config(conf);
200    struct dri2_egl_surface *dri2_surf;
201    struct ANativeWindow *window = native_window;
202    const __DRIconfig *config;
203
204    dri2_surf = calloc(1, sizeof *dri2_surf);
205    if (!dri2_surf) {
206       _eglError(EGL_BAD_ALLOC, "droid_create_surface");
207       return NULL;
208    }
209
210    if (!_eglInitSurface(&dri2_surf->base, disp, type, conf, attrib_list))
211       goto cleanup_surface;
212
213    if (type == EGL_WINDOW_BIT) {
214       int format;
215
216       if (!window || window->common.magic != ANDROID_NATIVE_WINDOW_MAGIC) {
217          _eglError(EGL_BAD_NATIVE_WINDOW, "droid_create_surface");
218          goto cleanup_surface;
219       }
220       if (window->query(window, NATIVE_WINDOW_FORMAT, &format)) {
221          _eglError(EGL_BAD_NATIVE_WINDOW, "droid_create_surface");
222          goto cleanup_surface;
223       }
224
225       if (format != dri2_conf->base.NativeVisualID) {
226          _eglLog(_EGL_WARNING, "Native format mismatch: 0x%x != 0x%x",
227                format, dri2_conf->base.NativeVisualID);
228       }
229
230       window->query(window, NATIVE_WINDOW_WIDTH, &dri2_surf->base.Width);
231       window->query(window, NATIVE_WINDOW_HEIGHT, &dri2_surf->base.Height);
232    }
233
234    config = dri2_get_dri_config(dri2_conf, EGL_WINDOW_BIT,
235                                 dri2_surf->base.GLColorspace);
236
237    dri2_surf->dri_drawable =
238       (*dri2_dpy->dri2->createNewDrawable)(dri2_dpy->dri_screen, config,
239                                            dri2_surf);
240    if (dri2_surf->dri_drawable == NULL) {
241       _eglError(EGL_BAD_ALLOC, "dri2->createNewDrawable");
242       goto cleanup_surface;
243    }
244
245    if (window) {
246       window->common.incRef(&window->common);
247       dri2_surf->window = window;
248    }
249
250    return &dri2_surf->base;
251
252 cleanup_surface:
253    free(dri2_surf);
254
255    return NULL;
256 }
257
258 static _EGLSurface *
259 droid_create_window_surface(_EGLDriver *drv, _EGLDisplay *disp,
260                             _EGLConfig *conf, void *native_window,
261                             const EGLint *attrib_list)
262 {
263    return droid_create_surface(drv, disp, EGL_WINDOW_BIT, conf,
264                                native_window, attrib_list);
265 }
266
267 static _EGLSurface *
268 droid_create_pbuffer_surface(_EGLDriver *drv, _EGLDisplay *disp,
269                             _EGLConfig *conf, const EGLint *attrib_list)
270 {
271    return droid_create_surface(drv, disp, EGL_PBUFFER_BIT, conf,
272                               NULL, attrib_list);
273 }
274
275 static EGLBoolean
276 droid_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
277 {
278    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
279    struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
280
281    if (!_eglPutSurface(surf))
282       return EGL_TRUE;
283
284    droid_free_local_buffers(dri2_surf);
285
286    if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
287       if (dri2_surf->buffer)
288          droid_window_cancel_buffer(dri2_surf);
289
290       dri2_surf->window->common.decRef(&dri2_surf->window->common);
291    }
292
293    (*dri2_dpy->core->destroyDrawable)(dri2_surf->dri_drawable);
294
295    free(dri2_surf);
296
297    return EGL_TRUE;
298 }
299
300 static EGLBoolean
301 droid_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
302 {
303    struct dri2_egl_driver *dri2_drv = dri2_egl_driver(drv);
304    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
305    struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
306    _EGLContext *ctx;
307
308    if (dri2_surf->base.Type != EGL_WINDOW_BIT)
309       return EGL_TRUE;
310
311    if (dri2_drv->glFlush) {
312       ctx = _eglGetCurrentContext();
313       if (ctx && ctx->DrawSurface == &dri2_surf->base)
314          dri2_drv->glFlush();
315    }
316
317    dri2_flush_drawable_for_swapbuffers(disp, draw);
318
319    if (dri2_surf->buffer)
320       droid_window_enqueue_buffer(dri2_surf);
321
322    (*dri2_dpy->flush->invalidate)(dri2_surf->dri_drawable);
323
324    return EGL_TRUE;
325 }
326
327 static _EGLImage *
328 dri2_create_image_android_native_buffer(_EGLDisplay *disp, _EGLContext *ctx,
329                                         struct ANativeWindowBuffer *buf)
330 {
331    struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
332    struct dri2_egl_image *dri2_img;
333    int name;
334    EGLint format;
335
336    if (ctx != NULL) {
337       /* From the EGL_ANDROID_image_native_buffer spec:
338        *
339        *     * If <target> is EGL_NATIVE_BUFFER_ANDROID and <ctx> is not
340        *       EGL_NO_CONTEXT, the error EGL_BAD_CONTEXT is generated.
341        */
342       _eglError(EGL_BAD_CONTEXT, "eglCreateEGLImageKHR: for "
343                 "EGL_NATIVE_BUFFER_ANDROID, the context must be "
344                 "EGL_NO_CONTEXT");
345       return NULL;
346    }
347
348    if (!buf || buf->common.magic != ANDROID_NATIVE_BUFFER_MAGIC ||
349        buf->common.version != sizeof(*buf)) {
350       _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR");
351       return NULL;
352    }
353
354    name = get_native_buffer_name(buf);
355    if (!name) {
356       _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR");
357       return NULL;
358    }
359
360    /* see the table in droid_add_configs_for_visuals */
361    switch (buf->format) {
362    case HAL_PIXEL_FORMAT_BGRA_8888:
363       format = __DRI_IMAGE_FORMAT_ARGB8888;
364       break;
365    case HAL_PIXEL_FORMAT_RGB_565:
366       format = __DRI_IMAGE_FORMAT_RGB565;
367       break;
368    case HAL_PIXEL_FORMAT_RGBA_8888:
369       format = __DRI_IMAGE_FORMAT_ABGR8888;
370       break;
371    case HAL_PIXEL_FORMAT_RGBX_8888:
372       format = __DRI_IMAGE_FORMAT_XBGR8888;
373       break;
374    case HAL_PIXEL_FORMAT_RGB_888:
375       /* unsupported */
376    default:
377       _eglLog(_EGL_WARNING, "unsupported native buffer format 0x%x", buf->format);
378       return NULL;
379       break;
380    }
381
382    dri2_img = calloc(1, sizeof(*dri2_img));
383    if (!dri2_img) {
384       _eglError(EGL_BAD_ALLOC, "droid_create_image_mesa_drm");
385       return NULL;
386    }
387
388    if (!_eglInitImage(&dri2_img->base, disp)) {
389       free(dri2_img);
390       return NULL;
391    }
392
393    dri2_img->dri_image =
394       dri2_dpy->image->createImageFromName(dri2_dpy->dri_screen,
395                                            buf->width,
396                                            buf->height,
397                                            format,
398                                            name,
399                                            buf->stride,
400                                            dri2_img);
401    if (!dri2_img->dri_image) {
402       free(dri2_img);
403       _eglError(EGL_BAD_ALLOC, "droid_create_image_mesa_drm");
404       return NULL;
405    }
406
407    return &dri2_img->base;
408 }
409
410 static _EGLImage *
411 droid_create_image_khr(_EGLDriver *drv, _EGLDisplay *disp,
412                        _EGLContext *ctx, EGLenum target,
413                        EGLClientBuffer buffer, const EGLint *attr_list)
414 {
415    switch (target) {
416    case EGL_NATIVE_BUFFER_ANDROID:
417       return dri2_create_image_android_native_buffer(disp, ctx,
418             (struct ANativeWindowBuffer *) buffer);
419    default:
420       return dri2_create_image_khr(drv, disp, ctx, target, buffer, attr_list);
421    }
422 }
423
424 static void
425 droid_flush_front_buffer(__DRIdrawable * driDrawable, void *loaderPrivate)
426 {
427 }
428
429 static int
430 droid_get_buffers_parse_attachments(struct dri2_egl_surface *dri2_surf,
431                                     unsigned int *attachments, int count)
432 {
433    int num_buffers = 0, i;
434
435    /* fill dri2_surf->buffers */
436    for (i = 0; i < count * 2; i += 2) {
437       __DRIbuffer *buf, *local;
438
439       assert(num_buffers < ARRAY_SIZE(dri2_surf->buffers));
440       buf = &dri2_surf->buffers[num_buffers];
441
442       switch (attachments[i]) {
443       case __DRI_BUFFER_BACK_LEFT:
444          if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
445             buf->attachment = attachments[i];
446             buf->name = get_native_buffer_name(dri2_surf->buffer);
447             buf->cpp = get_format_bpp(dri2_surf->buffer->format);
448             buf->pitch = dri2_surf->buffer->stride * buf->cpp;
449             buf->flags = 0;
450
451             if (buf->name)
452                num_buffers++;
453
454             break;
455          }
456          /* fall through for pbuffers */
457       case __DRI_BUFFER_DEPTH:
458       case __DRI_BUFFER_STENCIL:
459       case __DRI_BUFFER_ACCUM:
460       case __DRI_BUFFER_DEPTH_STENCIL:
461       case __DRI_BUFFER_HIZ:
462          local = droid_alloc_local_buffer(dri2_surf,
463                attachments[i], attachments[i + 1]);
464
465          if (local) {
466             *buf = *local;
467             num_buffers++;
468          }
469          break;
470       case __DRI_BUFFER_FRONT_LEFT:
471       case __DRI_BUFFER_FRONT_RIGHT:
472       case __DRI_BUFFER_FAKE_FRONT_LEFT:
473       case __DRI_BUFFER_FAKE_FRONT_RIGHT:
474       case __DRI_BUFFER_BACK_RIGHT:
475       default:
476          /* no front or right buffers */
477          break;
478       }
479    }
480
481    return num_buffers;
482 }
483
484 static __DRIbuffer *
485 droid_get_buffers_with_format(__DRIdrawable * driDrawable,
486                              int *width, int *height,
487                              unsigned int *attachments, int count,
488                              int *out_count, void *loaderPrivate)
489 {
490    struct dri2_egl_surface *dri2_surf = loaderPrivate;
491    struct dri2_egl_display *dri2_dpy =
492       dri2_egl_display(dri2_surf->base.Resource.Display);
493    int i;
494
495    if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
496       /* try to dequeue the next back buffer */
497       if (!dri2_surf->buffer && !droid_window_dequeue_buffer(dri2_surf))
498          return NULL;
499
500       /* free outdated buffers and update the surface size */
501       if (dri2_surf->base.Width != dri2_surf->buffer->width ||
502           dri2_surf->base.Height != dri2_surf->buffer->height) {
503          droid_free_local_buffers(dri2_surf);
504          dri2_surf->base.Width = dri2_surf->buffer->width;
505          dri2_surf->base.Height = dri2_surf->buffer->height;
506       }
507    }
508
509    dri2_surf->buffer_count =
510       droid_get_buffers_parse_attachments(dri2_surf, attachments, count);
511
512    if (width)
513       *width = dri2_surf->base.Width;
514    if (height)
515       *height = dri2_surf->base.Height;
516
517    *out_count = dri2_surf->buffer_count;
518
519    return dri2_surf->buffers;
520 }
521
522 static EGLBoolean
523 droid_add_configs_for_visuals(_EGLDriver *drv, _EGLDisplay *dpy)
524 {
525    struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy);
526    const struct {
527       int format;
528       unsigned int rgba_masks[4];
529    } visuals[] = {
530       { HAL_PIXEL_FORMAT_RGBA_8888, { 0xff, 0xff00, 0xff0000, 0xff000000 } },
531       { HAL_PIXEL_FORMAT_RGBX_8888, { 0xff, 0xff00, 0xff0000, 0x0 } },
532       { HAL_PIXEL_FORMAT_RGB_888,   { 0xff, 0xff00, 0xff0000, 0x0 } },
533       { HAL_PIXEL_FORMAT_RGB_565,   { 0xf800, 0x7e0, 0x1f, 0x0 } },
534       { HAL_PIXEL_FORMAT_BGRA_8888, { 0xff0000, 0xff00, 0xff, 0xff000000 } },
535       { 0, { 0, 0, 0, 0 } }
536    };
537    EGLint config_attrs[] = {
538      EGL_NATIVE_VISUAL_ID,   0,
539      EGL_NATIVE_VISUAL_TYPE, 0,
540      EGL_FRAMEBUFFER_TARGET_ANDROID, EGL_TRUE,
541      EGL_RECORDABLE_ANDROID, EGL_TRUE,
542      EGL_NONE
543    };
544    int count, i, j;
545
546    count = 0;
547    for (i = 0; visuals[i].format; i++) {
548       int format_count = 0;
549
550       config_attrs[1] = visuals[i].format;
551       config_attrs[3] = visuals[i].format;
552
553       for (j = 0; dri2_dpy->driver_configs[j]; j++) {
554          const EGLint surface_type = EGL_WINDOW_BIT | EGL_PBUFFER_BIT;
555          struct dri2_egl_config *dri2_conf;
556          unsigned int double_buffered = 0;
557
558          dri2_dpy->core->getConfigAttrib(dri2_dpy->driver_configs[j],
559             __DRI_ATTRIB_DOUBLE_BUFFER, &double_buffered);
560
561          /* support only double buffered configs */
562          if (!double_buffered)
563             continue;
564
565          dri2_conf = dri2_add_config(dpy, dri2_dpy->driver_configs[j],
566                count + 1, surface_type, config_attrs, visuals[i].rgba_masks);
567          if (dri2_conf) {
568             count++;
569             format_count++;
570          }
571       }
572
573       if (!format_count) {
574          _eglLog(_EGL_DEBUG, "No DRI config supports native format 0x%x",
575                visuals[i].format);
576       }
577    }
578
579    /* post-process configs */
580    for (i = 0; i < dpy->Configs->Size; i++) {
581       struct dri2_egl_config *dri2_conf = dri2_egl_config(dpy->Configs->Elements[i]);
582
583       /* there is no front buffer so no OpenGL */
584       dri2_conf->base.RenderableType &= ~EGL_OPENGL_BIT;
585       dri2_conf->base.Conformant &= ~EGL_OPENGL_BIT;
586    }
587
588    return (count != 0);
589 }
590
591 static int
592 droid_open_device(void)
593 {
594    const hw_module_t *mod;
595    int fd = -1, err;
596
597    err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &mod);
598    if (!err) {
599       const gralloc_module_t *gr = (gralloc_module_t *) mod;
600
601       err = -EINVAL;
602       if (gr->perform)
603          err = gr->perform(gr, GRALLOC_MODULE_PERFORM_GET_DRM_FD, &fd);
604    }
605    if (err || fd < 0) {
606       _eglLog(_EGL_WARNING, "fail to get drm fd");
607       fd = -1;
608    }
609
610    return (fd >= 0) ? dup(fd) : -1;
611 }
612
613 /* support versions < JellyBean */
614 #ifndef ALOGW
615 #define ALOGW LOGW
616 #endif
617 #ifndef ALOGD
618 #define ALOGD LOGD
619 #endif
620 #ifndef ALOGI
621 #define ALOGI LOGI
622 #endif
623
624 static void
625 droid_log(EGLint level, const char *msg)
626 {
627    switch (level) {
628    case _EGL_DEBUG:
629       ALOGD("%s", msg);
630       break;
631    case _EGL_INFO:
632       ALOGI("%s", msg);
633       break;
634    case _EGL_WARNING:
635       ALOGW("%s", msg);
636       break;
637    case _EGL_FATAL:
638       LOG_FATAL("%s", msg);
639       break;
640    default:
641       break;
642    }
643 }
644
645 static struct dri2_egl_display_vtbl droid_display_vtbl = {
646    .authenticate = NULL,
647    .create_window_surface = droid_create_window_surface,
648    .create_pixmap_surface = dri2_fallback_create_pixmap_surface,
649    .create_pbuffer_surface = droid_create_pbuffer_surface,
650    .destroy_surface = droid_destroy_surface,
651    .create_image = droid_create_image_khr,
652    .swap_interval = dri2_fallback_swap_interval,
653    .swap_buffers = droid_swap_buffers,
654    .swap_buffers_with_damage = dri2_fallback_swap_buffers_with_damage,
655    .swap_buffers_region = dri2_fallback_swap_buffers_region,
656    .post_sub_buffer = dri2_fallback_post_sub_buffer,
657    .copy_buffers = dri2_fallback_copy_buffers,
658    .query_buffer_age = dri2_fallback_query_buffer_age,
659    .create_wayland_buffer_from_image = dri2_fallback_create_wayland_buffer_from_image,
660    .get_sync_values = dri2_fallback_get_sync_values,
661    .get_dri_drawable = dri2_surface_get_dri_drawable,
662 };
663
664 EGLBoolean
665 dri2_initialize_android(_EGLDriver *drv, _EGLDisplay *dpy)
666 {
667    struct dri2_egl_display *dri2_dpy;
668    const char *err;
669
670    _eglSetLogProc(droid_log);
671
672    loader_set_logger(_eglLog);
673
674    dri2_dpy = calloc(1, sizeof(*dri2_dpy));
675    if (!dri2_dpy)
676       return _eglError(EGL_BAD_ALLOC, "eglInitialize");
677
678    dpy->DriverData = (void *) dri2_dpy;
679
680    dri2_dpy->fd = droid_open_device();
681    if (dri2_dpy->fd < 0) {
682       err = "DRI2: failed to open device";
683       goto cleanup_display;
684    }
685
686    dri2_dpy->driver_name = loader_get_driver_for_fd(dri2_dpy->fd, 0);
687    if (dri2_dpy->driver_name == NULL) {
688       err = "DRI2: failed to get driver name";
689       goto cleanup_device;
690    }
691
692    if (!dri2_load_driver(dpy)) {
693       err = "DRI2: failed to load driver";
694       goto cleanup_driver_name;
695    }
696
697    dri2_dpy->dri2_loader_extension.base.name = __DRI_DRI2_LOADER;
698    dri2_dpy->dri2_loader_extension.base.version = 3;
699    dri2_dpy->dri2_loader_extension.getBuffers = NULL;
700    dri2_dpy->dri2_loader_extension.flushFrontBuffer = droid_flush_front_buffer;
701    dri2_dpy->dri2_loader_extension.getBuffersWithFormat =
702       droid_get_buffers_with_format;
703
704    dri2_dpy->extensions[0] = &dri2_dpy->dri2_loader_extension.base;
705    dri2_dpy->extensions[1] = &image_lookup_extension.base;
706    dri2_dpy->extensions[2] = &use_invalidate.base;
707    dri2_dpy->extensions[3] = NULL;
708
709    if (!dri2_create_screen(dpy)) {
710       err = "DRI2: failed to create screen";
711       goto cleanup_driver;
712    }
713
714    if (!droid_add_configs_for_visuals(drv, dpy)) {
715       err = "DRI2: failed to add configs";
716       goto cleanup_screen;
717    }
718
719    dpy->Extensions.ANDROID_framebuffer_target = EGL_TRUE;
720    dpy->Extensions.ANDROID_image_native_buffer = EGL_TRUE;
721    dpy->Extensions.ANDROID_recordable = EGL_TRUE;
722    dpy->Extensions.KHR_image_base = EGL_TRUE;
723
724    /* Fill vtbl last to prevent accidentally calling virtual function during
725     * initialization.
726     */
727    dri2_dpy->vtbl = &droid_display_vtbl;
728
729    return EGL_TRUE;
730
731 cleanup_screen:
732    dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen);
733 cleanup_driver:
734    dlclose(dri2_dpy->driver);
735 cleanup_driver_name:
736    free(dri2_dpy->driver_name);
737 cleanup_device:
738    close(dri2_dpy->fd);
739 cleanup_display:
740    free(dri2_dpy);
741
742    return _eglError(EGL_NOT_INITIALIZED, err);
743 }