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