egl: Allow creation of per surface out fence
[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 #include <fcntl.h>
33 #include <xf86drm.h>
34 #include <stdbool.h>
35 #include <sync/sync.h>
36
37 #include "loader.h"
38 #include "egl_dri2.h"
39 #include "egl_dri2_fallbacks.h"
40 #include "gralloc_drm.h"
41
42 #define ALIGN(val, align) (((val) + (align) - 1) & ~((align) - 1))
43
44 struct droid_yuv_format {
45 /* Lookup keys */
46 int native; /* HAL_PIXEL_FORMAT_ */
47 int is_ycrcb; /* 0 if chroma order is {Cb, Cr}, 1 if {Cr, Cb} */
48 int chroma_step; /* Distance in bytes between subsequent chroma pixels. */
49
50 /* Result */
51 int fourcc; /* __DRI_IMAGE_FOURCC_ */
52 };
53
54 /* The following table is used to look up a DRI image FourCC based
55 * on native format and information contained in android_ycbcr struct. */
56 static const struct droid_yuv_format droid_yuv_formats[] = {
57 /* Native format, YCrCb, Chroma step, DRI image FourCC */
58 { HAL_PIXEL_FORMAT_YCbCr_420_888, 0, 2, __DRI_IMAGE_FOURCC_NV12 },
59 { HAL_PIXEL_FORMAT_YCbCr_420_888, 0, 1, __DRI_IMAGE_FOURCC_YUV420 },
60 { HAL_PIXEL_FORMAT_YCbCr_420_888, 1, 1, __DRI_IMAGE_FOURCC_YVU420 },
61 { HAL_PIXEL_FORMAT_YV12, 1, 1, __DRI_IMAGE_FOURCC_YVU420 },
62 };
63
64 static int
65 get_fourcc_yuv(int native, int is_ycrcb, int chroma_step)
66 {
67 for (int i = 0; i < ARRAY_SIZE(droid_yuv_formats); ++i)
68 if (droid_yuv_formats[i].native == native &&
69 droid_yuv_formats[i].is_ycrcb == is_ycrcb &&
70 droid_yuv_formats[i].chroma_step == chroma_step)
71 return droid_yuv_formats[i].fourcc;
72
73 return -1;
74 }
75
76 static bool
77 is_yuv(int native)
78 {
79 for (int i = 0; i < ARRAY_SIZE(droid_yuv_formats); ++i)
80 if (droid_yuv_formats[i].native == native)
81 return true;
82
83 return false;
84 }
85
86 static int
87 get_format_bpp(int native)
88 {
89 int bpp;
90
91 switch (native) {
92 case HAL_PIXEL_FORMAT_RGBA_8888:
93 case HAL_PIXEL_FORMAT_RGBX_8888:
94 case HAL_PIXEL_FORMAT_BGRA_8888:
95 bpp = 4;
96 break;
97 case HAL_PIXEL_FORMAT_RGB_565:
98 bpp = 2;
99 break;
100 default:
101 bpp = 0;
102 break;
103 }
104
105 return bpp;
106 }
107
108 /* createImageFromFds requires fourcc format */
109 static int get_fourcc(int native)
110 {
111 switch (native) {
112 case HAL_PIXEL_FORMAT_RGB_565: return __DRI_IMAGE_FOURCC_RGB565;
113 case HAL_PIXEL_FORMAT_BGRA_8888: return __DRI_IMAGE_FOURCC_ARGB8888;
114 case HAL_PIXEL_FORMAT_RGBA_8888: return __DRI_IMAGE_FOURCC_ABGR8888;
115 case HAL_PIXEL_FORMAT_RGBX_8888: return __DRI_IMAGE_FOURCC_XBGR8888;
116 default:
117 _eglLog(_EGL_WARNING, "unsupported native buffer format 0x%x", native);
118 }
119 return -1;
120 }
121
122 static int get_format(int format)
123 {
124 switch (format) {
125 case HAL_PIXEL_FORMAT_BGRA_8888: return __DRI_IMAGE_FORMAT_ARGB8888;
126 case HAL_PIXEL_FORMAT_RGB_565: return __DRI_IMAGE_FORMAT_RGB565;
127 case HAL_PIXEL_FORMAT_RGBA_8888: return __DRI_IMAGE_FORMAT_ABGR8888;
128 case HAL_PIXEL_FORMAT_RGBX_8888: return __DRI_IMAGE_FORMAT_XBGR8888;
129 default:
130 _eglLog(_EGL_WARNING, "unsupported native buffer format 0x%x", format);
131 }
132 return -1;
133 }
134
135 static int
136 get_native_buffer_fd(struct ANativeWindowBuffer *buf)
137 {
138 native_handle_t *handle = (native_handle_t *)buf->handle;
139 /*
140 * Various gralloc implementations exist, but the dma-buf fd tends
141 * to be first. Access it directly to avoid a dependency on specific
142 * gralloc versions.
143 */
144 return (handle && handle->numFds) ? handle->data[0] : -1;
145 }
146
147 static int
148 get_native_buffer_name(struct ANativeWindowBuffer *buf)
149 {
150 return gralloc_drm_get_gem_handle(buf->handle);
151 }
152
153 static EGLBoolean
154 droid_window_dequeue_buffer(struct dri2_egl_surface *dri2_surf)
155 {
156 int fence_fd;
157
158 if (dri2_surf->window->dequeueBuffer(dri2_surf->window, &dri2_surf->buffer,
159 &fence_fd))
160 return EGL_FALSE;
161
162 /* If access to the buffer is controlled by a sync fence, then block on the
163 * fence.
164 *
165 * It may be more performant to postpone blocking until there is an
166 * immediate need to write to the buffer. But doing so would require adding
167 * hooks to the DRI2 loader.
168 *
169 * From the ANativeWindow::dequeueBuffer documentation:
170 *
171 * The libsync fence file descriptor returned in the int pointed to by
172 * the fenceFd argument will refer to the fence that must signal
173 * before the dequeued buffer may be written to. A value of -1
174 * indicates that the caller may access the buffer immediately without
175 * waiting on a fence. If a valid file descriptor is returned (i.e.
176 * any value except -1) then the caller is responsible for closing the
177 * file descriptor.
178 */
179 if (fence_fd >= 0) {
180 /* From the SYNC_IOC_WAIT documentation in <linux/sync.h>:
181 *
182 * Waits indefinitely if timeout < 0.
183 */
184 int timeout = -1;
185 sync_wait(fence_fd, timeout);
186 close(fence_fd);
187 }
188
189 dri2_surf->buffer->common.incRef(&dri2_surf->buffer->common);
190
191 /* Record all the buffers created by ANativeWindow and update back buffer
192 * for updating buffer's age in swap_buffers.
193 */
194 EGLBoolean updated = EGL_FALSE;
195 for (int i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
196 if (!dri2_surf->color_buffers[i].buffer) {
197 dri2_surf->color_buffers[i].buffer = dri2_surf->buffer;
198 }
199 if (dri2_surf->color_buffers[i].buffer == dri2_surf->buffer) {
200 dri2_surf->back = &dri2_surf->color_buffers[i];
201 updated = EGL_TRUE;
202 break;
203 }
204 }
205
206 if (!updated) {
207 /* In case of all the buffers were recreated by ANativeWindow, reset
208 * the color_buffers
209 */
210 for (int i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
211 dri2_surf->color_buffers[i].buffer = NULL;
212 dri2_surf->color_buffers[i].age = 0;
213 }
214 dri2_surf->color_buffers[0].buffer = dri2_surf->buffer;
215 dri2_surf->back = &dri2_surf->color_buffers[0];
216 }
217
218 return EGL_TRUE;
219 }
220
221 static EGLBoolean
222 droid_window_enqueue_buffer(_EGLDisplay *disp, struct dri2_egl_surface *dri2_surf)
223 {
224 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
225
226 /* To avoid blocking other EGL calls, release the display mutex before
227 * we enter droid_window_enqueue_buffer() and re-acquire the mutex upon
228 * return.
229 */
230 mtx_unlock(&disp->Mutex);
231
232 /* Queue the buffer without a sync fence. This informs the ANativeWindow
233 * that it may access the buffer immediately.
234 *
235 * From ANativeWindow::dequeueBuffer:
236 *
237 * The fenceFd argument specifies a libsync fence file descriptor for
238 * a fence that must signal before the buffer can be accessed. If
239 * the buffer can be accessed immediately then a value of -1 should
240 * be used. The caller must not use the file descriptor after it
241 * is passed to queueBuffer, and the ANativeWindow implementation
242 * is responsible for closing it.
243 */
244 int fence_fd = -1;
245 dri2_surf->window->queueBuffer(dri2_surf->window, dri2_surf->buffer,
246 fence_fd);
247
248 dri2_surf->buffer->common.decRef(&dri2_surf->buffer->common);
249 dri2_surf->buffer = NULL;
250 dri2_surf->back = NULL;
251
252 mtx_lock(&disp->Mutex);
253
254 if (dri2_surf->dri_image_back) {
255 dri2_dpy->image->destroyImage(dri2_surf->dri_image_back);
256 dri2_surf->dri_image_back = NULL;
257 }
258
259 return EGL_TRUE;
260 }
261
262 static void
263 droid_window_cancel_buffer(struct dri2_egl_surface *dri2_surf)
264 {
265 int ret;
266
267 ret = dri2_surf->window->cancelBuffer(dri2_surf->window, dri2_surf->buffer, -1);
268 if (ret < 0) {
269 _eglLog(_EGL_WARNING, "ANativeWindow::cancelBuffer failed");
270 dri2_surf->base.Lost = EGL_TRUE;
271 }
272 }
273
274 static _EGLSurface *
275 droid_create_surface(_EGLDriver *drv, _EGLDisplay *disp, EGLint type,
276 _EGLConfig *conf, void *native_window,
277 const EGLint *attrib_list)
278 {
279 __DRIcreateNewDrawableFunc createNewDrawable;
280 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
281 struct dri2_egl_config *dri2_conf = dri2_egl_config(conf);
282 struct dri2_egl_surface *dri2_surf;
283 struct ANativeWindow *window = native_window;
284 const __DRIconfig *config;
285
286 dri2_surf = calloc(1, sizeof *dri2_surf);
287 if (!dri2_surf) {
288 _eglError(EGL_BAD_ALLOC, "droid_create_surface");
289 return NULL;
290 }
291
292 if (!dri2_init_surface(&dri2_surf->base, disp, type, conf, attrib_list, false))
293 goto cleanup_surface;
294
295 if (type == EGL_WINDOW_BIT) {
296 int format;
297
298 if (window->common.magic != ANDROID_NATIVE_WINDOW_MAGIC) {
299 _eglError(EGL_BAD_NATIVE_WINDOW, "droid_create_surface");
300 goto cleanup_surface;
301 }
302 if (window->query(window, NATIVE_WINDOW_FORMAT, &format)) {
303 _eglError(EGL_BAD_NATIVE_WINDOW, "droid_create_surface");
304 goto cleanup_surface;
305 }
306
307 if (format != dri2_conf->base.NativeVisualID) {
308 _eglLog(_EGL_WARNING, "Native format mismatch: 0x%x != 0x%x",
309 format, dri2_conf->base.NativeVisualID);
310 }
311
312 window->query(window, NATIVE_WINDOW_WIDTH, &dri2_surf->base.Width);
313 window->query(window, NATIVE_WINDOW_HEIGHT, &dri2_surf->base.Height);
314 }
315
316 config = dri2_get_dri_config(dri2_conf, type,
317 dri2_surf->base.GLColorspace);
318 if (!config)
319 goto cleanup_surface;
320
321 if (dri2_dpy->image_driver)
322 createNewDrawable = dri2_dpy->image_driver->createNewDrawable;
323 else
324 createNewDrawable = dri2_dpy->dri2->createNewDrawable;
325
326 dri2_surf->dri_drawable = (*createNewDrawable)(dri2_dpy->dri_screen, config,
327 dri2_surf);
328 if (dri2_surf->dri_drawable == NULL) {
329 _eglError(EGL_BAD_ALLOC, "createNewDrawable");
330 goto cleanup_surface;
331 }
332
333 if (window) {
334 window->common.incRef(&window->common);
335 dri2_surf->window = window;
336 }
337
338 return &dri2_surf->base;
339
340 cleanup_surface:
341 free(dri2_surf);
342
343 return NULL;
344 }
345
346 static _EGLSurface *
347 droid_create_window_surface(_EGLDriver *drv, _EGLDisplay *disp,
348 _EGLConfig *conf, void *native_window,
349 const EGLint *attrib_list)
350 {
351 return droid_create_surface(drv, disp, EGL_WINDOW_BIT, conf,
352 native_window, attrib_list);
353 }
354
355 static _EGLSurface *
356 droid_create_pbuffer_surface(_EGLDriver *drv, _EGLDisplay *disp,
357 _EGLConfig *conf, const EGLint *attrib_list)
358 {
359 return droid_create_surface(drv, disp, EGL_PBUFFER_BIT, conf,
360 NULL, attrib_list);
361 }
362
363 static EGLBoolean
364 droid_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
365 {
366 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
367 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
368
369 dri2_egl_surface_free_local_buffers(dri2_surf);
370
371 if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
372 if (dri2_surf->buffer)
373 droid_window_cancel_buffer(dri2_surf);
374
375 dri2_surf->window->common.decRef(&dri2_surf->window->common);
376 }
377
378 if (dri2_surf->dri_image_back) {
379 _eglLog(_EGL_DEBUG, "%s : %d : destroy dri_image_back", __func__, __LINE__);
380 dri2_dpy->image->destroyImage(dri2_surf->dri_image_back);
381 dri2_surf->dri_image_back = NULL;
382 }
383
384 if (dri2_surf->dri_image_front) {
385 _eglLog(_EGL_DEBUG, "%s : %d : destroy dri_image_front", __func__, __LINE__);
386 dri2_dpy->image->destroyImage(dri2_surf->dri_image_front);
387 dri2_surf->dri_image_front = NULL;
388 }
389
390 dri2_dpy->core->destroyDrawable(dri2_surf->dri_drawable);
391
392 dri2_fini_surface(surf);
393 free(dri2_surf);
394
395 return EGL_TRUE;
396 }
397
398 static int
399 update_buffers(struct dri2_egl_surface *dri2_surf)
400 {
401 if (dri2_surf->base.Lost)
402 return -1;
403
404 if (dri2_surf->base.Type != EGL_WINDOW_BIT)
405 return 0;
406
407 /* try to dequeue the next back buffer */
408 if (!dri2_surf->buffer && !droid_window_dequeue_buffer(dri2_surf)) {
409 _eglLog(_EGL_WARNING, "Could not dequeue buffer from native window");
410 dri2_surf->base.Lost = EGL_TRUE;
411 return -1;
412 }
413
414 /* free outdated buffers and update the surface size */
415 if (dri2_surf->base.Width != dri2_surf->buffer->width ||
416 dri2_surf->base.Height != dri2_surf->buffer->height) {
417 dri2_egl_surface_free_local_buffers(dri2_surf);
418 dri2_surf->base.Width = dri2_surf->buffer->width;
419 dri2_surf->base.Height = dri2_surf->buffer->height;
420 }
421
422 return 0;
423 }
424
425 static int
426 get_front_bo(struct dri2_egl_surface *dri2_surf, unsigned int format)
427 {
428 struct dri2_egl_display *dri2_dpy =
429 dri2_egl_display(dri2_surf->base.Resource.Display);
430
431 if (dri2_surf->dri_image_front)
432 return 0;
433
434 if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
435 /* According current EGL spec, front buffer rendering
436 * for window surface is not supported now.
437 * and mesa doesn't have the implementation of this case.
438 * Add warning message, but not treat it as error.
439 */
440 _eglLog(_EGL_DEBUG, "DRI driver requested unsupported front buffer for window surface");
441 } else if (dri2_surf->base.Type == EGL_PBUFFER_BIT) {
442 dri2_surf->dri_image_front =
443 dri2_dpy->image->createImage(dri2_dpy->dri_screen,
444 dri2_surf->base.Width,
445 dri2_surf->base.Height,
446 format,
447 0,
448 dri2_surf);
449 if (!dri2_surf->dri_image_front) {
450 _eglLog(_EGL_WARNING, "dri2_image_front allocation failed");
451 return -1;
452 }
453 }
454
455 return 0;
456 }
457
458 static int
459 get_back_bo(struct dri2_egl_surface *dri2_surf)
460 {
461 struct dri2_egl_display *dri2_dpy =
462 dri2_egl_display(dri2_surf->base.Resource.Display);
463 int fourcc, pitch;
464 int offset = 0, fd;
465
466 if (dri2_surf->dri_image_back)
467 return 0;
468
469 if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
470 if (!dri2_surf->buffer) {
471 _eglLog(_EGL_WARNING, "Could not get native buffer");
472 return -1;
473 }
474
475 fd = get_native_buffer_fd(dri2_surf->buffer);
476 if (fd < 0) {
477 _eglLog(_EGL_WARNING, "Could not get native buffer FD");
478 return -1;
479 }
480
481 fourcc = get_fourcc(dri2_surf->buffer->format);
482
483 pitch = dri2_surf->buffer->stride *
484 get_format_bpp(dri2_surf->buffer->format);
485
486 if (fourcc == -1 || pitch == 0) {
487 _eglLog(_EGL_WARNING, "Invalid buffer fourcc(%x) or pitch(%d)",
488 fourcc, pitch);
489 return -1;
490 }
491
492 dri2_surf->dri_image_back =
493 dri2_dpy->image->createImageFromFds(dri2_dpy->dri_screen,
494 dri2_surf->base.Width,
495 dri2_surf->base.Height,
496 fourcc,
497 &fd,
498 1,
499 &pitch,
500 &offset,
501 dri2_surf);
502 if (!dri2_surf->dri_image_back) {
503 _eglLog(_EGL_WARNING, "failed to create DRI image from FD");
504 return -1;
505 }
506 } else if (dri2_surf->base.Type == EGL_PBUFFER_BIT) {
507 /* The EGL 1.5 spec states that pbuffers are single-buffered. Specifically,
508 * the spec states that they have a back buffer but no front buffer, in
509 * contrast to pixmaps, which have a front buffer but no back buffer.
510 *
511 * Single-buffered surfaces with no front buffer confuse Mesa; so we deviate
512 * from the spec, following the precedent of Mesa's EGL X11 platform. The
513 * X11 platform correctly assigns pbuffers to single-buffered configs, but
514 * assigns the pbuffer a front buffer instead of a back buffer.
515 *
516 * Pbuffers in the X11 platform mostly work today, so let's just copy its
517 * behavior instead of trying to fix (and hence potentially breaking) the
518 * world.
519 */
520 _eglLog(_EGL_DEBUG, "DRI driver requested unsupported back buffer for pbuffer surface");
521 }
522
523 return 0;
524 }
525
526 /* Some drivers will pass multiple bits in buffer_mask.
527 * For such case, will go through all the bits, and
528 * will not return error when unsupported buffer is requested, only
529 * return error when the allocation for supported buffer failed.
530 */
531 static int
532 droid_image_get_buffers(__DRIdrawable *driDrawable,
533 unsigned int format,
534 uint32_t *stamp,
535 void *loaderPrivate,
536 uint32_t buffer_mask,
537 struct __DRIimageList *images)
538 {
539 struct dri2_egl_surface *dri2_surf = loaderPrivate;
540
541 images->image_mask = 0;
542 images->front = NULL;
543 images->back = NULL;
544
545 if (update_buffers(dri2_surf) < 0)
546 return 0;
547
548 if (buffer_mask & __DRI_IMAGE_BUFFER_FRONT) {
549 if (get_front_bo(dri2_surf, format) < 0)
550 return 0;
551
552 if (dri2_surf->dri_image_front) {
553 images->front = dri2_surf->dri_image_front;
554 images->image_mask |= __DRI_IMAGE_BUFFER_FRONT;
555 }
556 }
557
558 if (buffer_mask & __DRI_IMAGE_BUFFER_BACK) {
559 if (get_back_bo(dri2_surf) < 0)
560 return 0;
561
562 if (dri2_surf->dri_image_back) {
563 images->back = dri2_surf->dri_image_back;
564 images->image_mask |= __DRI_IMAGE_BUFFER_BACK;
565 }
566 }
567
568 return 1;
569 }
570
571 static EGLint
572 droid_query_buffer_age(_EGLDriver *drv,
573 _EGLDisplay *disp, _EGLSurface *surface)
574 {
575 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surface);
576
577 if (update_buffers(dri2_surf) < 0) {
578 _eglError(EGL_BAD_ALLOC, "droid_query_buffer_age");
579 return -1;
580 }
581
582 return dri2_surf->back ? dri2_surf->back->age : 0;
583 }
584
585 static EGLBoolean
586 droid_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
587 {
588 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
589 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
590
591 if (dri2_surf->base.Type != EGL_WINDOW_BIT)
592 return EGL_TRUE;
593
594 for (int i = 0; i < ARRAY_SIZE(dri2_surf->color_buffers); i++) {
595 if (dri2_surf->color_buffers[i].age > 0)
596 dri2_surf->color_buffers[i].age++;
597 }
598
599 /* "XXX: we don't use get_back_bo() since it causes regressions in
600 * several dEQP tests.
601 */
602 if (dri2_surf->back)
603 dri2_surf->back->age = 1;
604
605 dri2_flush_drawable_for_swapbuffers(disp, draw);
606
607 /* dri2_surf->buffer can be null even when no error has occured. For
608 * example, if the user has called no GL rendering commands since the
609 * previous eglSwapBuffers, then the driver may have not triggered
610 * a callback to ANativeWindow::dequeueBuffer, in which case
611 * dri2_surf->buffer remains null.
612 */
613 if (dri2_surf->buffer)
614 droid_window_enqueue_buffer(disp, dri2_surf);
615
616 dri2_dpy->flush->invalidate(dri2_surf->dri_drawable);
617
618 return EGL_TRUE;
619 }
620
621 #if ANDROID_API_LEVEL >= 23
622 static EGLBoolean
623 droid_set_damage_region(_EGLDriver *drv,
624 _EGLDisplay *disp,
625 _EGLSurface *draw, const EGLint* rects, EGLint n_rects)
626 {
627 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
628 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
629 android_native_rect_t* droid_rects = NULL;
630 int ret;
631
632 if (n_rects == 0)
633 return EGL_TRUE;
634
635 droid_rects = malloc(n_rects * sizeof(android_native_rect_t));
636 if (droid_rects == NULL)
637 return _eglError(EGL_BAD_ALLOC, "eglSetDamageRegionKHR");
638
639 for (EGLint num_drects = 0; num_drects < n_rects; num_drects++) {
640 EGLint i = num_drects * 4;
641 droid_rects[num_drects].left = rects[i];
642 droid_rects[num_drects].bottom = rects[i + 1];
643 droid_rects[num_drects].right = rects[i] + rects[i + 2];
644 droid_rects[num_drects].top = rects[i + 1] + rects[i + 3];
645 }
646
647 /*
648 * XXX/TODO: Need to check for other return values
649 */
650
651 ret = native_window_set_surface_damage(dri2_surf->window, droid_rects, n_rects);
652 free(droid_rects);
653
654 return ret == 0 ? EGL_TRUE : EGL_FALSE;
655 }
656 #endif
657
658 static _EGLImage *
659 droid_create_image_from_prime_fd_yuv(_EGLDisplay *disp, _EGLContext *ctx,
660 struct ANativeWindowBuffer *buf, int fd)
661 {
662 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
663 struct android_ycbcr ycbcr;
664 size_t offsets[3];
665 size_t pitches[3];
666 int is_ycrcb;
667 int fourcc;
668 int ret;
669
670 if (!dri2_dpy->gralloc->lock_ycbcr) {
671 _eglLog(_EGL_WARNING, "Gralloc does not support lock_ycbcr");
672 return NULL;
673 }
674
675 memset(&ycbcr, 0, sizeof(ycbcr));
676 ret = dri2_dpy->gralloc->lock_ycbcr(dri2_dpy->gralloc, buf->handle,
677 0, 0, 0, 0, 0, &ycbcr);
678 if (ret) {
679 _eglLog(_EGL_WARNING, "gralloc->lock_ycbcr failed: %d", ret);
680 return NULL;
681 }
682 dri2_dpy->gralloc->unlock(dri2_dpy->gralloc, buf->handle);
683
684 /* When lock_ycbcr's usage argument contains no SW_READ/WRITE flags
685 * it will return the .y/.cb/.cr pointers based on a NULL pointer,
686 * so they can be interpreted as offsets. */
687 offsets[0] = (size_t)ycbcr.y;
688 /* We assume here that all the planes are located in one DMA-buf. */
689 is_ycrcb = (size_t)ycbcr.cr < (size_t)ycbcr.cb;
690 if (is_ycrcb) {
691 offsets[1] = (size_t)ycbcr.cr;
692 offsets[2] = (size_t)ycbcr.cb;
693 } else {
694 offsets[1] = (size_t)ycbcr.cb;
695 offsets[2] = (size_t)ycbcr.cr;
696 }
697
698 /* .ystride is the line length (in bytes) of the Y plane,
699 * .cstride is the line length (in bytes) of any of the remaining
700 * Cb/Cr/CbCr planes, assumed to be the same for Cb and Cr for fully
701 * planar formats. */
702 pitches[0] = ycbcr.ystride;
703 pitches[1] = pitches[2] = ycbcr.cstride;
704
705 /* .chroma_step is the byte distance between the same chroma channel
706 * values of subsequent pixels, assumed to be the same for Cb and Cr. */
707 fourcc = get_fourcc_yuv(buf->format, is_ycrcb, ycbcr.chroma_step);
708 if (fourcc == -1) {
709 _eglLog(_EGL_WARNING, "unsupported YUV format, native = %x, is_ycrcb = %d, chroma_step = %d",
710 buf->format, is_ycrcb, ycbcr.chroma_step);
711 return NULL;
712 }
713
714 if (ycbcr.chroma_step == 2) {
715 /* Semi-planar Y + CbCr or Y + CrCb format. */
716 const EGLint attr_list_2plane[] = {
717 EGL_WIDTH, buf->width,
718 EGL_HEIGHT, buf->height,
719 EGL_LINUX_DRM_FOURCC_EXT, fourcc,
720 EGL_DMA_BUF_PLANE0_FD_EXT, fd,
721 EGL_DMA_BUF_PLANE0_PITCH_EXT, pitches[0],
722 EGL_DMA_BUF_PLANE0_OFFSET_EXT, offsets[0],
723 EGL_DMA_BUF_PLANE1_FD_EXT, fd,
724 EGL_DMA_BUF_PLANE1_PITCH_EXT, pitches[1],
725 EGL_DMA_BUF_PLANE1_OFFSET_EXT, offsets[1],
726 EGL_NONE, 0
727 };
728
729 return dri2_create_image_dma_buf(disp, ctx, NULL, attr_list_2plane);
730 } else {
731 /* Fully planar Y + Cb + Cr or Y + Cr + Cb format. */
732 const EGLint attr_list_3plane[] = {
733 EGL_WIDTH, buf->width,
734 EGL_HEIGHT, buf->height,
735 EGL_LINUX_DRM_FOURCC_EXT, fourcc,
736 EGL_DMA_BUF_PLANE0_FD_EXT, fd,
737 EGL_DMA_BUF_PLANE0_PITCH_EXT, pitches[0],
738 EGL_DMA_BUF_PLANE0_OFFSET_EXT, offsets[0],
739 EGL_DMA_BUF_PLANE1_FD_EXT, fd,
740 EGL_DMA_BUF_PLANE1_PITCH_EXT, pitches[1],
741 EGL_DMA_BUF_PLANE1_OFFSET_EXT, offsets[1],
742 EGL_DMA_BUF_PLANE2_FD_EXT, fd,
743 EGL_DMA_BUF_PLANE2_PITCH_EXT, pitches[2],
744 EGL_DMA_BUF_PLANE2_OFFSET_EXT, offsets[2],
745 EGL_NONE, 0
746 };
747
748 return dri2_create_image_dma_buf(disp, ctx, NULL, attr_list_3plane);
749 }
750 }
751
752 static _EGLImage *
753 droid_create_image_from_prime_fd(_EGLDisplay *disp, _EGLContext *ctx,
754 struct ANativeWindowBuffer *buf, int fd)
755 {
756 unsigned int pitch;
757
758 if (is_yuv(buf->format))
759 return droid_create_image_from_prime_fd_yuv(disp, ctx, buf, fd);
760
761 const int fourcc = get_fourcc(buf->format);
762 if (fourcc == -1) {
763 _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR");
764 return NULL;
765 }
766
767 pitch = buf->stride * get_format_bpp(buf->format);
768 if (pitch == 0) {
769 _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR");
770 return NULL;
771 }
772
773 const EGLint attr_list[] = {
774 EGL_WIDTH, buf->width,
775 EGL_HEIGHT, buf->height,
776 EGL_LINUX_DRM_FOURCC_EXT, fourcc,
777 EGL_DMA_BUF_PLANE0_FD_EXT, fd,
778 EGL_DMA_BUF_PLANE0_PITCH_EXT, pitch,
779 EGL_DMA_BUF_PLANE0_OFFSET_EXT, 0,
780 EGL_NONE, 0
781 };
782
783 return dri2_create_image_dma_buf(disp, ctx, NULL, attr_list);
784 }
785
786 static _EGLImage *
787 droid_create_image_from_name(_EGLDisplay *disp, _EGLContext *ctx,
788 struct ANativeWindowBuffer *buf)
789 {
790 struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
791 struct dri2_egl_image *dri2_img;
792 int name;
793 int format;
794
795 name = get_native_buffer_name(buf);
796 if (!name) {
797 _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR");
798 return NULL;
799 }
800
801 format = get_format(buf->format);
802 if (format == -1)
803 return NULL;
804
805 dri2_img = calloc(1, sizeof(*dri2_img));
806 if (!dri2_img) {
807 _eglError(EGL_BAD_ALLOC, "droid_create_image_mesa_drm");
808 return NULL;
809 }
810
811 _eglInitImage(&dri2_img->base, disp);
812
813 dri2_img->dri_image =
814 dri2_dpy->image->createImageFromName(dri2_dpy->dri_screen,
815 buf->width,
816 buf->height,
817 format,
818 name,
819 buf->stride,
820 dri2_img);
821 if (!dri2_img->dri_image) {
822 free(dri2_img);
823 _eglError(EGL_BAD_ALLOC, "droid_create_image_mesa_drm");
824 return NULL;
825 }
826
827 return &dri2_img->base;
828 }
829
830 static EGLBoolean
831 droid_query_surface(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf,
832 EGLint attribute, EGLint *value)
833 {
834 struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
835 switch (attribute) {
836 case EGL_WIDTH:
837 if (dri2_surf->base.Type == EGL_WINDOW_BIT && dri2_surf->window) {
838 dri2_surf->window->query(dri2_surf->window,
839 NATIVE_WINDOW_DEFAULT_WIDTH, value);
840 return EGL_TRUE;
841 }
842 break;
843 case EGL_HEIGHT:
844 if (dri2_surf->base.Type == EGL_WINDOW_BIT && dri2_surf->window) {
845 dri2_surf->window->query(dri2_surf->window,
846 NATIVE_WINDOW_DEFAULT_HEIGHT, value);
847 return EGL_TRUE;
848 }
849 break;
850 default:
851 break;
852 }
853 return _eglQuerySurface(drv, dpy, surf, attribute, value);
854 }
855
856 static _EGLImage *
857 dri2_create_image_android_native_buffer(_EGLDisplay *disp,
858 _EGLContext *ctx,
859 struct ANativeWindowBuffer *buf)
860 {
861 int fd;
862
863 if (ctx != NULL) {
864 /* From the EGL_ANDROID_image_native_buffer spec:
865 *
866 * * If <target> is EGL_NATIVE_BUFFER_ANDROID and <ctx> is not
867 * EGL_NO_CONTEXT, the error EGL_BAD_CONTEXT is generated.
868 */
869 _eglError(EGL_BAD_CONTEXT, "eglCreateEGLImageKHR: for "
870 "EGL_NATIVE_BUFFER_ANDROID, the context must be "
871 "EGL_NO_CONTEXT");
872 return NULL;
873 }
874
875 if (!buf || buf->common.magic != ANDROID_NATIVE_BUFFER_MAGIC ||
876 buf->common.version != sizeof(*buf)) {
877 _eglError(EGL_BAD_PARAMETER, "eglCreateEGLImageKHR");
878 return NULL;
879 }
880
881 fd = get_native_buffer_fd(buf);
882 if (fd >= 0)
883 return droid_create_image_from_prime_fd(disp, ctx, buf, fd);
884
885 return droid_create_image_from_name(disp, ctx, buf);
886 }
887
888 static _EGLImage *
889 droid_create_image_khr(_EGLDriver *drv, _EGLDisplay *disp,
890 _EGLContext *ctx, EGLenum target,
891 EGLClientBuffer buffer, const EGLint *attr_list)
892 {
893 switch (target) {
894 case EGL_NATIVE_BUFFER_ANDROID:
895 return dri2_create_image_android_native_buffer(disp, ctx,
896 (struct ANativeWindowBuffer *) buffer);
897 default:
898 return dri2_create_image_khr(drv, disp, ctx, target, buffer, attr_list);
899 }
900 }
901
902 static void
903 droid_flush_front_buffer(__DRIdrawable * driDrawable, void *loaderPrivate)
904 {
905 }
906
907 static int
908 droid_get_buffers_parse_attachments(struct dri2_egl_surface *dri2_surf,
909 unsigned int *attachments, int count)
910 {
911 int num_buffers = 0;
912
913 /* fill dri2_surf->buffers */
914 for (int i = 0; i < count * 2; i += 2) {
915 __DRIbuffer *buf, *local;
916
917 assert(num_buffers < ARRAY_SIZE(dri2_surf->buffers));
918 buf = &dri2_surf->buffers[num_buffers];
919
920 switch (attachments[i]) {
921 case __DRI_BUFFER_BACK_LEFT:
922 if (dri2_surf->base.Type == EGL_WINDOW_BIT) {
923 buf->attachment = attachments[i];
924 buf->name = get_native_buffer_name(dri2_surf->buffer);
925 buf->cpp = get_format_bpp(dri2_surf->buffer->format);
926 buf->pitch = dri2_surf->buffer->stride * buf->cpp;
927 buf->flags = 0;
928
929 if (buf->name)
930 num_buffers++;
931
932 break;
933 }
934 /* fall through for pbuffers */
935 case __DRI_BUFFER_DEPTH:
936 case __DRI_BUFFER_STENCIL:
937 case __DRI_BUFFER_ACCUM:
938 case __DRI_BUFFER_DEPTH_STENCIL:
939 case __DRI_BUFFER_HIZ:
940 local = dri2_egl_surface_alloc_local_buffer(dri2_surf,
941 attachments[i], attachments[i + 1]);
942
943 if (local) {
944 *buf = *local;
945 num_buffers++;
946 }
947 break;
948 case __DRI_BUFFER_FRONT_LEFT:
949 case __DRI_BUFFER_FRONT_RIGHT:
950 case __DRI_BUFFER_FAKE_FRONT_LEFT:
951 case __DRI_BUFFER_FAKE_FRONT_RIGHT:
952 case __DRI_BUFFER_BACK_RIGHT:
953 default:
954 /* no front or right buffers */
955 break;
956 }
957 }
958
959 return num_buffers;
960 }
961
962 static __DRIbuffer *
963 droid_get_buffers_with_format(__DRIdrawable * driDrawable,
964 int *width, int *height,
965 unsigned int *attachments, int count,
966 int *out_count, void *loaderPrivate)
967 {
968 struct dri2_egl_surface *dri2_surf = loaderPrivate;
969
970 if (update_buffers(dri2_surf) < 0)
971 return NULL;
972
973 *out_count = droid_get_buffers_parse_attachments(dri2_surf, attachments, count);
974
975 if (width)
976 *width = dri2_surf->base.Width;
977 if (height)
978 *height = dri2_surf->base.Height;
979
980 return dri2_surf->buffers;
981 }
982
983 static unsigned
984 droid_get_capability(void *loaderPrivate, enum dri_loader_cap cap)
985 {
986 /* Note: loaderPrivate is _EGLDisplay* */
987 switch (cap) {
988 case DRI_LOADER_CAP_RGBA_ORDERING:
989 return 1;
990 default:
991 return 0;
992 }
993 }
994
995 static EGLBoolean
996 droid_add_configs_for_visuals(_EGLDriver *drv, _EGLDisplay *dpy)
997 {
998 struct dri2_egl_display *dri2_dpy = dri2_egl_display(dpy);
999 static const struct {
1000 int format;
1001 unsigned int rgba_masks[4];
1002 } visuals[] = {
1003 { HAL_PIXEL_FORMAT_RGBA_8888, { 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000 } },
1004 { HAL_PIXEL_FORMAT_RGBX_8888, { 0x000000ff, 0x0000ff00, 0x00ff0000, 0x00000000 } },
1005 { HAL_PIXEL_FORMAT_RGB_565, { 0x0000f800, 0x000007e0, 0x0000001f, 0x00000000 } },
1006 { HAL_PIXEL_FORMAT_BGRA_8888, { 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000 } },
1007 };
1008
1009 unsigned int format_count[ARRAY_SIZE(visuals)] = { 0 };
1010 int config_count = 0;
1011
1012 /* The nesting of loops is significant here. Also significant is the order
1013 * of the HAL pixel formats. Many Android apps (such as Google's official
1014 * NDK GLES2 example app), and even portions the core framework code (such
1015 * as SystemServiceManager in Nougat), incorrectly choose their EGLConfig.
1016 * They neglect to match the EGLConfig's EGL_NATIVE_VISUAL_ID against the
1017 * window's native format, and instead choose the first EGLConfig whose
1018 * channel sizes match those of the native window format while ignoring the
1019 * channel *ordering*.
1020 *
1021 * We can detect such buggy clients in logcat when they call
1022 * eglCreateSurface, by detecting the mismatch between the EGLConfig's
1023 * format and the window's format.
1024 *
1025 * As a workaround, we generate EGLConfigs such that all EGLConfigs for HAL
1026 * pixel format i precede those for HAL pixel format i+1. In my
1027 * (chadversary) testing on Android Nougat, this was good enough to pacify
1028 * the buggy clients.
1029 */
1030 for (int i = 0; i < ARRAY_SIZE(visuals); i++) {
1031 for (int j = 0; dri2_dpy->driver_configs[j]; j++) {
1032 const EGLint surface_type = EGL_WINDOW_BIT | EGL_PBUFFER_BIT;
1033
1034 const EGLint config_attrs[] = {
1035 EGL_NATIVE_VISUAL_ID, visuals[i].format,
1036 EGL_NATIVE_VISUAL_TYPE, visuals[i].format,
1037 EGL_FRAMEBUFFER_TARGET_ANDROID, EGL_TRUE,
1038 EGL_RECORDABLE_ANDROID, EGL_TRUE,
1039 EGL_NONE
1040 };
1041
1042 struct dri2_egl_config *dri2_conf =
1043 dri2_add_config(dpy, dri2_dpy->driver_configs[j],
1044 config_count + 1, surface_type, config_attrs,
1045 visuals[i].rgba_masks);
1046 if (dri2_conf) {
1047 if (dri2_conf->base.ConfigID == config_count + 1)
1048 config_count++;
1049 format_count[i]++;
1050 }
1051 }
1052 }
1053
1054 for (int i = 0; i < ARRAY_SIZE(format_count); i++) {
1055 if (!format_count[i]) {
1056 _eglLog(_EGL_DEBUG, "No DRI config supports native format 0x%x",
1057 visuals[i].format);
1058 }
1059 }
1060
1061 return (config_count != 0);
1062 }
1063
1064 static int
1065 droid_open_device(struct dri2_egl_display *dri2_dpy)
1066 {
1067 int fd = -1, err = -EINVAL;
1068
1069 if (dri2_dpy->gralloc->perform)
1070 err = dri2_dpy->gralloc->perform(dri2_dpy->gralloc,
1071 GRALLOC_MODULE_PERFORM_GET_DRM_FD,
1072 &fd);
1073 if (err || fd < 0) {
1074 _eglLog(_EGL_WARNING, "fail to get drm fd");
1075 fd = -1;
1076 }
1077
1078 return (fd >= 0) ? fcntl(fd, F_DUPFD_CLOEXEC, 3) : -1;
1079 }
1080
1081 static const struct dri2_egl_display_vtbl droid_display_vtbl = {
1082 .authenticate = NULL,
1083 .create_window_surface = droid_create_window_surface,
1084 .create_pixmap_surface = dri2_fallback_create_pixmap_surface,
1085 .create_pbuffer_surface = droid_create_pbuffer_surface,
1086 .destroy_surface = droid_destroy_surface,
1087 .create_image = droid_create_image_khr,
1088 .swap_interval = dri2_fallback_swap_interval,
1089 .swap_buffers = droid_swap_buffers,
1090 .swap_buffers_with_damage = dri2_fallback_swap_buffers_with_damage,
1091 .swap_buffers_region = dri2_fallback_swap_buffers_region,
1092 #if ANDROID_API_LEVEL >= 23
1093 .set_damage_region = droid_set_damage_region,
1094 #else
1095 .set_damage_region = dri2_fallback_set_damage_region,
1096 #endif
1097 .post_sub_buffer = dri2_fallback_post_sub_buffer,
1098 .copy_buffers = dri2_fallback_copy_buffers,
1099 .query_buffer_age = droid_query_buffer_age,
1100 .query_surface = droid_query_surface,
1101 .create_wayland_buffer_from_image = dri2_fallback_create_wayland_buffer_from_image,
1102 .get_sync_values = dri2_fallback_get_sync_values,
1103 .get_dri_drawable = dri2_surface_get_dri_drawable,
1104 };
1105
1106 static const __DRIdri2LoaderExtension droid_dri2_loader_extension = {
1107 .base = { __DRI_DRI2_LOADER, 4 },
1108
1109 .getBuffers = NULL,
1110 .flushFrontBuffer = droid_flush_front_buffer,
1111 .getBuffersWithFormat = droid_get_buffers_with_format,
1112 .getCapability = droid_get_capability,
1113 };
1114
1115 static const __DRIimageLoaderExtension droid_image_loader_extension = {
1116 .base = { __DRI_IMAGE_LOADER, 2 },
1117
1118 .getBuffers = droid_image_get_buffers,
1119 .flushFrontBuffer = droid_flush_front_buffer,
1120 .getCapability = droid_get_capability,
1121 };
1122
1123 static const __DRIextension *droid_dri2_loader_extensions[] = {
1124 &droid_dri2_loader_extension.base,
1125 &image_lookup_extension.base,
1126 &use_invalidate.base,
1127 NULL,
1128 };
1129
1130 static const __DRIextension *droid_image_loader_extensions[] = {
1131 &droid_image_loader_extension.base,
1132 &image_lookup_extension.base,
1133 &use_invalidate.base,
1134 NULL,
1135 };
1136
1137 EGLBoolean
1138 dri2_initialize_android(_EGLDriver *drv, _EGLDisplay *dpy)
1139 {
1140 struct dri2_egl_display *dri2_dpy;
1141 const char *err;
1142 int ret;
1143
1144 loader_set_logger(_eglLog);
1145
1146 dri2_dpy = calloc(1, sizeof(*dri2_dpy));
1147 if (!dri2_dpy)
1148 return _eglError(EGL_BAD_ALLOC, "eglInitialize");
1149
1150 dri2_dpy->fd = -1;
1151 ret = hw_get_module(GRALLOC_HARDWARE_MODULE_ID,
1152 (const hw_module_t **)&dri2_dpy->gralloc);
1153 if (ret) {
1154 err = "DRI2: failed to get gralloc module";
1155 goto cleanup;
1156 }
1157
1158 dpy->DriverData = (void *) dri2_dpy;
1159
1160 dri2_dpy->fd = droid_open_device(dri2_dpy);
1161 if (dri2_dpy->fd < 0) {
1162 err = "DRI2: failed to open device";
1163 goto cleanup;
1164 }
1165
1166 dri2_dpy->driver_name = loader_get_driver_for_fd(dri2_dpy->fd);
1167 if (dri2_dpy->driver_name == NULL) {
1168 err = "DRI2: failed to get driver name";
1169 goto cleanup;
1170 }
1171
1172 dri2_dpy->is_render_node = drmGetNodeTypeFromFd(dri2_dpy->fd) == DRM_NODE_RENDER;
1173
1174 /* render nodes cannot use Gem names, and thus do not support
1175 * the __DRI_DRI2_LOADER extension */
1176 if (!dri2_dpy->is_render_node) {
1177 dri2_dpy->loader_extensions = droid_dri2_loader_extensions;
1178 if (!dri2_load_driver(dpy)) {
1179 err = "DRI2: failed to load driver";
1180 goto cleanup;
1181 }
1182 } else {
1183 dri2_dpy->loader_extensions = droid_image_loader_extensions;
1184 if (!dri2_load_driver_dri3(dpy)) {
1185 err = "DRI3: failed to load driver";
1186 goto cleanup;
1187 }
1188 }
1189
1190 if (!dri2_create_screen(dpy)) {
1191 err = "DRI2: failed to create screen";
1192 goto cleanup;
1193 }
1194
1195 if (!dri2_setup_extensions(dpy)) {
1196 err = "DRI2: failed to setup extensions";
1197 goto cleanup;
1198 }
1199
1200 dri2_setup_screen(dpy);
1201
1202 if (!droid_add_configs_for_visuals(drv, dpy)) {
1203 err = "DRI2: failed to add configs";
1204 goto cleanup;
1205 }
1206
1207 dpy->Extensions.ANDROID_framebuffer_target = EGL_TRUE;
1208 dpy->Extensions.ANDROID_image_native_buffer = EGL_TRUE;
1209 dpy->Extensions.ANDROID_recordable = EGL_TRUE;
1210 dpy->Extensions.EXT_buffer_age = EGL_TRUE;
1211 #if ANDROID_API_LEVEL >= 23
1212 dpy->Extensions.KHR_partial_update = EGL_TRUE;
1213 #endif
1214
1215 /* Fill vtbl last to prevent accidentally calling virtual function during
1216 * initialization.
1217 */
1218 dri2_dpy->vtbl = &droid_display_vtbl;
1219
1220 return EGL_TRUE;
1221
1222 cleanup:
1223 dri2_display_destroy(dpy);
1224 return _eglError(EGL_NOT_INITIALIZED, err);
1225 }