From d50251d8439d22605c5abc7ae27473ab3a31f627 Mon Sep 17 00:00:00 2001
From: Roman Kiryanov <rkir@google.com>
Date: Thu, 16 May 2019 18:35:39 -0700
Subject: [PATCH] Do not read and do not convert pixels for camera

Camera is a special citizen, it delivers buffer bits
directly (no reading required) and supports
YUV_420 (interleaved), this is the only format it
supports.

Bug: 130295800
Test: emulator -wipe-data -no-snapshot -camera-back virtualscene
Test: take photo, take video
Change-Id: I0192084d88f27235d4d1a2c0a7598cd28200bc48
Merged-In: Ib37ec0a85a48d6a6ac6bbf6b9033ffd60ac7a727
Signed-off-by: Roman Kiryanov <rkir@google.com>
---
 shared/OpenglCodecCommon/gralloc_cb.h |  1 -
 system/gralloc/gralloc.cpp            | 18 ++++++++----------
 2 files changed, 8 insertions(+), 11 deletions(-)

diff --git a/shared/OpenglCodecCommon/gralloc_cb.h b/shared/OpenglCodecCommon/gralloc_cb.h
index ee4bd9a..bff33f3 100644
--- a/shared/OpenglCodecCommon/gralloc_cb.h
+++ b/shared/OpenglCodecCommon/gralloc_cb.h
@@ -32,7 +32,6 @@ enum EmulatorFrameworkFormat {
     FRAMEWORK_FORMAT_GL_COMPATIBLE = 0,
     FRAMEWORK_FORMAT_YV12 = 1,
     FRAMEWORK_FORMAT_YUV_420_888 = 2,              // (Y+)(U+)(V+)
-    FRAMEWORK_FORMAT_YUV_420_888_INTERLEAVED = 3,  // (Y+)(UV)+
 };
 
 //
diff --git a/system/gralloc/gralloc.cpp b/system/gralloc/gralloc.cpp
index a83ca96..e9140be 100644
--- a/system/gralloc/gralloc.cpp
+++ b/system/gralloc/gralloc.cpp
@@ -623,13 +623,7 @@ static int gralloc_alloc(alloc_device_t* dev,
             // We are going to use RGB888 on the host
             glFormat = GL_RGB;
             glType = GL_UNSIGNED_BYTE;
-
-            if (usage & (GRALLOC_USAGE_HW_CAMERA_READ | GRALLOC_USAGE_HW_CAMERA_WRITE)) {
-                // EmulatedFakeCamera3.cpp assumes it is NV21
-                selectedEmuFrameworkFormat = FRAMEWORK_FORMAT_YUV_420_888_INTERLEAVED;
-            } else {
-                selectedEmuFrameworkFormat = FRAMEWORK_FORMAT_YUV_420_888;
-            }
+            selectedEmuFrameworkFormat = FRAMEWORK_FORMAT_YUV_420_888;
             break;
         default:
             ALOGE("gralloc_alloc: Unknown format %d", format);
@@ -1205,7 +1199,9 @@ static int gralloc_lock(gralloc_module_t const* module,
             return -EBUSY;
         }
 
-        if (sw_read) {
+        // camera delivers bits to the buffer directly and does not require
+        // an explicit read, it also writes in YUV_420 (interleaved)
+        if (sw_read & !(usage & GRALLOC_USAGE_HW_CAMERA_MASK)) {
             void* rgb_addr = cpu_addr;
             char* tmpBuf = 0;
             if (cb->frameworkFormat == HAL_PIXEL_FORMAT_YV12 ||
@@ -1329,8 +1325,10 @@ static int gralloc_lock_ycbcr(gralloc_module_t const* module,
         return -EINVAL;
     }
 
+    usage |= (cb->usage & GRALLOC_USAGE_HW_CAMERA_MASK);
+
     void *vaddr;
-    int ret = gralloc_lock(module, handle, usage | GRALLOC_USAGE_SW_WRITE_MASK, l, t, w, h, &vaddr);
+    int ret = gralloc_lock(module, handle, usage, l, t, w, h, &vaddr);
     if (ret) {
         return ret;
     }
@@ -1367,7 +1365,7 @@ static int gralloc_lock_ycbcr(gralloc_module_t const* module,
             cStep = 1;
             break;
         case HAL_PIXEL_FORMAT_YCbCr_420_888:
-            if (cb->emuFrameworkFormat == FRAMEWORK_FORMAT_YUV_420_888_INTERLEAVED) {
+            if (usage & GRALLOC_USAGE_HW_CAMERA_MASK) {
                 yStride = cb->width;
                 cStride = cb->width;
                 yOffset = 0;
-- 
GitLab