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