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