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