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