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