Merge "Test for getHashChain() in IBase." into oc-dev
diff --git a/camera/device/1.0/ICameraDevicePreviewCallback.hal b/camera/device/1.0/ICameraDevicePreviewCallback.hal
index 4c9b517..e772301 100644
--- a/camera/device/1.0/ICameraDevicePreviewCallback.hal
+++ b/camera/device/1.0/ICameraDevicePreviewCallback.hal
@@ -17,7 +17,6 @@
 package android.hardware.camera.device@1.0;
 
 import android.hardware.camera.common@1.0::types;
-import android.hardware.graphics.allocator@2.0::types;
 import android.hardware.graphics.common@1.0::types;
 
 /**
@@ -89,7 +88,7 @@
      *
      * @return Status The status code for this operation.
      */
-    setUsage(ProducerUsage usage) generates (Status status);
+    setUsage(ProducerUsageFlags usage) generates (Status status);
 
     /**
      * Set the expected buffering mode for the preview output.
diff --git a/camera/device/1.0/default/Android.bp b/camera/device/1.0/default/Android.bp
index 5688fc1..a2fd0b0 100644
--- a/camera/device/1.0/default/Android.bp
+++ b/camera/device/1.0/default/Android.bp
@@ -22,8 +22,8 @@
         "libbinder",
     ],
     static_libs: [
-        "android.hardware.camera.common@1.0-helper"
+        "android.hardware.camera.common@1.0-helper",
+        "libgrallocusage"
     ],
     export_include_dirs: ["."]
 }
-
diff --git a/camera/device/1.0/default/CameraDevice.cpp b/camera/device/1.0/default/CameraDevice.cpp
index 6eeda1c..5e3fcf2 100644
--- a/camera/device/1.0/default/CameraDevice.cpp
+++ b/camera/device/1.0/default/CameraDevice.cpp
@@ -20,6 +20,8 @@
 #include <hardware/gralloc1.h>
 #include <utils/Trace.h>
 
+#include <grallocusage/GrallocUsageConversion.h>
+
 #include "CameraDevice_1_0.h"
 
 namespace android {
@@ -29,7 +31,6 @@
 namespace V1_0 {
 namespace implementation {
 
-using ::android::hardware::graphics::allocator::V2_0::ProducerUsage;
 using ::android::hardware::graphics::common::V1_0::PixelFormat;
 
 HandleImporter& CameraDevice::sHandleImporter = HandleImporter::getInstance();
@@ -252,7 +253,10 @@
     }
 
     object->cleanUpCirculatingBuffers();
-    return getStatusT(object->mPreviewCallback->setUsage((ProducerUsage) usage));
+    ProducerUsageFlags producerUsage;
+    uint64_t consumerUsage;
+    ::android_convertGralloc0To1Usage(usage, &producerUsage, &consumerUsage);
+    return getStatusT(object->mPreviewCallback->setUsage(producerUsage));
 }
 
 int CameraDevice::sSetSwapInterval(struct preview_stream_ops *w, int interval) {
diff --git a/camera/device/1.0/types.hal b/camera/device/1.0/types.hal
index ce5205e..0b3445f 100644
--- a/camera/device/1.0/types.hal
+++ b/camera/device/1.0/types.hal
@@ -16,6 +16,10 @@
 
 package android.hardware.camera.device@1.0;
 
+import android.hardware.graphics.allocator@2.0::types;
+
+typedef bitfield<ProducerUsage> ProducerUsageFlags;
+
 enum CameraFacing : uint32_t {
     /** The facing of the camera is opposite to that of the screen. */
     BACK = 0,
diff --git a/camera/device/3.2/ICameraDeviceSession.hal b/camera/device/3.2/ICameraDeviceSession.hal
index e186c8d..731fc76 100644
--- a/camera/device/3.2/ICameraDeviceSession.hal
+++ b/camera/device/3.2/ICameraDeviceSession.hal
@@ -183,6 +183,11 @@
      * client, the HAL must process the requests in order of lowest index to
      * highest index.
      *
+     * The cachesToRemove argument contains a list of buffer caches (see
+     * StreamBuffer document for more information on buffer cache) to be removed
+     * by camera HAL. Camera HAL must remove these cache entries whether or not
+     * this method returns OK.
+     *
      * The actual request processing is asynchronous, with the results of
      * capture being returned by the HAL through the processCaptureResult()
      * call. This call requires the result metadata to be available, but output
@@ -238,7 +243,8 @@
      *     that HAL processed successfully before HAL runs into an error.
      *
      */
-    processCaptureRequest(vec<CaptureRequest> requests)
+    processCaptureRequest(vec<CaptureRequest> requests,
+            vec<BufferCache> cachesToRemove)
             generates (Status status, uint32_t numRequestProcessed);
 
     /**
diff --git a/camera/device/3.2/default/Android.bp b/camera/device/3.2/default/Android.bp
index e0dc5ff..5a81d41 100644
--- a/camera/device/3.2/default/Android.bp
+++ b/camera/device/3.2/default/Android.bp
@@ -17,7 +17,8 @@
         "libcamera_metadata"
     ],
     static_libs: [
-        "android.hardware.camera.common@1.0-helper"
+        "android.hardware.camera.common@1.0-helper",
+        "libgrallocusage"
     ],
     export_include_dirs: ["."]
 }
diff --git a/camera/device/3.2/default/CameraDeviceSession.cpp b/camera/device/3.2/default/CameraDeviceSession.cpp
index fb1d1ff..f2f9e5e 100644
--- a/camera/device/3.2/default/CameraDeviceSession.cpp
+++ b/camera/device/3.2/default/CameraDeviceSession.cpp
@@ -611,7 +611,7 @@
                 return Void();
             }
             mStreamMap[id].rotation = (int) requestedConfiguration.streams[i].rotation;
-            mStreamMap[id].usage = (uint32_t) requestedConfiguration.streams[i].usage;
+            mStreamMap[id].usage = convertFromHidlUsage(requestedConfiguration.streams[i].usage);
         }
         streams[i] = &mStreamMap[id];
     }
@@ -678,8 +678,32 @@
     mCirculatingBuffers.erase(id);
 }
 
+void CameraDeviceSession::updateBufferCaches(const hidl_vec<BufferCache>& cachesToRemove) {
+    Mutex::Autolock _l(mInflightLock);
+    for (auto& cache : cachesToRemove) {
+        auto cbsIt = mCirculatingBuffers.find(cache.streamId);
+        if (cbsIt == mCirculatingBuffers.end()) {
+            // The stream could have been removed
+            continue;
+        }
+        CirculatingBuffers& cbs = cbsIt->second;
+        auto it = cbs.find(cache.bufferId);
+        if (it != cbs.end()) {
+            sHandleImporter.freeBuffer(it->second);
+            cbs.erase(it);
+        } else {
+            ALOGE("%s: stream %d buffer %" PRIu64 " is not cached",
+                    __FUNCTION__, cache.streamId, cache.bufferId);
+        }
+    }
+}
+
 Return<void> CameraDeviceSession::processCaptureRequest(
-        const hidl_vec<CaptureRequest>& requests, processCaptureRequest_cb _hidl_cb)  {
+        const hidl_vec<CaptureRequest>& requests,
+        const hidl_vec<BufferCache>& cachesToRemove,
+        processCaptureRequest_cb _hidl_cb)  {
+    updateBufferCaches(cachesToRemove);
+
     uint32_t numRequestProcessed = 0;
     Status s = Status::OK;
     for (size_t i = 0; i < requests.size(); i++, numRequestProcessed++) {
diff --git a/camera/device/3.2/default/CameraDeviceSession.h b/camera/device/3.2/default/CameraDeviceSession.h
index 8923c05..781056e 100644
--- a/camera/device/3.2/default/CameraDeviceSession.h
+++ b/camera/device/3.2/default/CameraDeviceSession.h
@@ -85,7 +85,9 @@
     Return<void> configureStreams(
             const StreamConfiguration& requestedConfiguration, configureStreams_cb _hidl_cb) override;
     Return<void> processCaptureRequest(
-            const hidl_vec<CaptureRequest>& requests, processCaptureRequest_cb _hidl_cb) override;
+            const hidl_vec<CaptureRequest>& requests,
+            const hidl_vec<BufferCache>& cachesToRemove,
+            processCaptureRequest_cb _hidl_cb) override;
     Return<Status> flush() override;
     Return<void> close() override;
 
@@ -234,6 +236,8 @@
 
     void cleanupBuffersLocked(int id);
 
+    void updateBufferCaches(const hidl_vec<BufferCache>& cachesToRemove);
+
     Status processOneCaptureRequest(const CaptureRequest& request);
     /**
      * Static callback forwarding methods from HAL to instance
diff --git a/camera/device/3.2/default/convert.cpp b/camera/device/3.2/default/convert.cpp
index 35676df..c99d903 100644
--- a/camera/device/3.2/default/convert.cpp
+++ b/camera/device/3.2/default/convert.cpp
@@ -15,8 +15,12 @@
  */
 
 #define LOG_TAG "android.hardware.camera.device@3.2-convert-impl"
+#include <inttypes.h>
+
 #include <android/log.h>
 
+#include <grallocusage/GrallocUsageConversion.h>
+
 #include "include/convert.h"
 
 namespace android {
@@ -28,6 +32,7 @@
 
 using ::android::hardware::graphics::common::V1_0::Dataspace;
 using ::android::hardware::graphics::common::V1_0::PixelFormat;
+using ::android::hardware::graphics::allocator::V2_0::ConsumerUsage;
 using ::android::hardware::camera::device::V3_2::ConsumerUsageFlags;
 using ::android::hardware::camera::device::V3_2::ProducerUsageFlags;
 
@@ -66,23 +71,37 @@
     dst->format = (int) src.format;
     dst->data_space = (android_dataspace_t) src.dataSpace;
     dst->rotation = (int) src.rotation;
-    dst->usage = (uint32_t) src.usage;
+    dst->usage = convertFromHidlUsage(src.usage);
     // Fields to be filled by HAL (max_buffers, priv) are initialized to 0
     dst->max_buffers = 0;
     dst->priv = 0;
     return;
 }
 
+uint32_t convertFromHidlUsage(ConsumerUsageFlags usage) {
+    uint32_t dstUsage = 0;
+    dstUsage = ::android_convertGralloc1To0Usage(/*producerUsage*/ 0, usage);
+    // Compatibility workaround - add HW_CAMERA_ZSL when ConsumerUsage.CAMERA is set, to
+    // match pre-HIDL expected usage flags
+    if ( (usage & ConsumerUsage::CAMERA) == static_cast<uint64_t>(ConsumerUsage::CAMERA)) {
+        dstUsage |= GRALLOC_USAGE_HW_CAMERA_ZSL;
+    }
+    return dstUsage;
+}
+
 void convertToHidl(const Camera3Stream* src, HalStream* dst) {
     dst->id = src->mId;
     dst->overrideFormat = (PixelFormat) src->format;
     dst->maxBuffers = src->max_buffers;
+    ConsumerUsageFlags consumerUsage;
+    ProducerUsageFlags producerUsage;
+    ::android_convertGralloc0To1Usage(src->usage, &producerUsage, &consumerUsage);
     if (src->stream_type == CAMERA3_STREAM_OUTPUT) {
         dst->consumerUsage = (ConsumerUsageFlags) 0;
-        dst->producerUsage = (ProducerUsageFlags) src->usage;
+        dst->producerUsage = producerUsage;
     } else if (src->stream_type == CAMERA3_STREAM_INPUT) {
         dst->producerUsage = (ProducerUsageFlags) 0;
-        dst->consumerUsage = (ConsumerUsageFlags) src->usage;
+        dst->consumerUsage = consumerUsage;
     } else {
         //Should not reach here per current HIDL spec, but we might end up adding
         // bi-directional stream to HIDL.
diff --git a/camera/device/3.2/default/include/convert.h b/camera/device/3.2/default/include/convert.h
index 96891f0..7c8e02f 100644
--- a/camera/device/3.2/default/include/convert.h
+++ b/camera/device/3.2/default/include/convert.h
@@ -45,6 +45,8 @@
 void convertFromHidl(const Stream &src, Camera3Stream* dst);
 void convertToHidl(const Camera3Stream* src, HalStream* dst);
 
+uint32_t convertFromHidlUsage(ConsumerUsageFlags usage);
+
 void convertFromHidl(
         buffer_handle_t*, BufferStatus, camera3_stream_t*, int acquireFence, // inputs
         camera3_stream_buffer_t* dst);
diff --git a/camera/device/3.2/types.hal b/camera/device/3.2/types.hal
index fd75528..5ae7a18 100644
--- a/camera/device/3.2/types.hal
+++ b/camera/device/3.2/types.hal
@@ -958,3 +958,25 @@
     uint32_t partialResult;
 
 };
+
+/**
+ * BufferCache:
+ *
+ * A list of cached bufferIds associated with a certain stream.
+ * Buffers are passed between camera service and camera HAL via bufferId except
+ * the first time a new buffer is being passed to HAL in CaptureRequest. Camera
+ * service and camera HAL therefore need to maintain a cached map of bufferId
+ * and corresponing native handle.
+ *
+ */
+struct BufferCache {
+    /**
+     * The ID of the stream this list is associated with.
+     */
+    int32_t streamId;
+
+    /**
+     * A cached buffer ID associated with streamId.
+     */
+    uint64_t bufferId;
+};
diff --git a/camera/provider/2.4/vts/functional/Android.bp b/camera/provider/2.4/vts/functional/Android.bp
index a0be5cb..02c42c9 100644
--- a/camera/provider/2.4/vts/functional/Android.bp
+++ b/camera/provider/2.4/vts/functional/Android.bp
@@ -33,7 +33,10 @@
         "libgui",
         "libui"
     ],
-    static_libs: ["VtsHalHidlTargetTestBase"],
+    static_libs: [
+        "VtsHalHidlTargetTestBase",
+        "libgrallocusage"
+    ],
     cflags: [
         "-O0",
         "-g",
diff --git a/camera/provider/2.4/vts/functional/VtsHalCameraProviderV2_4TargetTest.cpp b/camera/provider/2.4/vts/functional/VtsHalCameraProviderV2_4TargetTest.cpp
index 25ffdc6..4d3c6eb 100644
--- a/camera/provider/2.4/vts/functional/VtsHalCameraProviderV2_4TargetTest.cpp
+++ b/camera/provider/2.4/vts/functional/VtsHalCameraProviderV2_4TargetTest.cpp
@@ -21,6 +21,7 @@
 #include "CameraParameters.h"
 #include <system/camera.h>
 #include <android/log.h>
+#include <grallocusage/GrallocUsageConversion.h>
 #include <ui/GraphicBuffer.h>
 #include <VtsHalHidlTargetTestBase.h>
 #include <gui/BufferQueue.h>
@@ -53,6 +54,7 @@
 using ::android::Surface;
 using ::android::CameraParameters;
 using ::android::hardware::graphics::common::V1_0::PixelFormat;
+using ::android::hardware::graphics::allocator::V2_0::ConsumerUsage;
 using ::android::hardware::graphics::allocator::V2_0::ProducerUsage;
 using ::android::hardware::camera::common::V1_0::Status;
 using ::android::hardware::camera::common::V1_0::CameraDeviceStatus;
@@ -60,7 +62,10 @@
 using ::android::hardware::camera::common::V1_0::TorchModeStatus;
 using ::android::hardware::camera::provider::V2_4::ICameraProvider;
 using ::android::hardware::camera::provider::V2_4::ICameraProviderCallback;
+using ::android::hardware::camera::device::V3_2::ProducerUsageFlags;
+using ::android::hardware::camera::device::V3_2::ConsumerUsageFlags;
 using ::android::hardware::camera::device::V3_2::ICameraDevice;
+using ::android::hardware::camera::device::V3_2::BufferCache;
 using ::android::hardware::camera::device::V3_2::CaptureRequest;
 using ::android::hardware::camera::device::V3_2::CaptureResult;
 using ::android::hardware::camera::device::V3_2::ICameraDeviceCallback;
@@ -79,6 +84,7 @@
 using ::android::hardware::camera::device::V3_2::MsgType;
 using ::android::hardware::camera::device::V3_2::ErrorMsg;
 using ::android::hardware::camera::device::V3_2::ErrorCode;
+using ::android::hardware::camera::device::V1_0::ProducerUsageFlags;
 using ::android::hardware::camera::device::V1_0::CameraFacing;
 using ::android::hardware::camera::device::V1_0::NotifyCallbackMsg;
 using ::android::hardware::camera::device::V1_0::CommandType;
@@ -232,7 +238,7 @@
     Return<Status> setCrop(int32_t left, int32_t top,
             int32_t right, int32_t bottom) override;
 
-    Return<Status> setUsage(ProducerUsage usage) override;
+    Return<Status> setUsage(ProducerUsageFlags usage) override;
 
     Return<Status> setSwapInterval(int32_t interval) override;
 
@@ -407,10 +413,11 @@
     return mapToStatus(rc);
 }
 
-Return<Status> PreviewWindowCb::setUsage(ProducerUsage usage) {
-    auto rc = native_window_set_usage(mAnw.get(), static_cast<int>(usage));
+Return<Status> PreviewWindowCb::setUsage(ProducerUsageFlags usage) {
+    int dstUsage = ::android_convertGralloc1To0Usage(usage, /*consumerUsage*/ 0);
+    auto rc = native_window_set_usage(mAnw.get(), dstUsage);
     if (rc == ::android::OK) {
-        mPreviewUsage =  static_cast<int>(usage);
+        mPreviewUsage = dstUsage;
     }
     return mapToStatus(rc);
 }
@@ -2198,7 +2205,7 @@
                             static_cast<uint32_t> (input.width),
                             static_cast<uint32_t> (input.height),
                             static_cast<PixelFormat> (input.format),
-                            GRALLOC_USAGE_HW_CAMERA_ZSL, 0,
+                            static_cast<ConsumerUsageFlags>(ConsumerUsage::CAMERA), 0,
                             StreamRotation::ROTATION_0};
                     Stream inputStream = {streamId++, StreamType::INPUT,
                             static_cast<uint32_t> (input.width),
@@ -2430,7 +2437,7 @@
                             static_cast<uint32_t> (blobIter.width),
                             static_cast<uint32_t> (blobIter.height),
                             static_cast<PixelFormat> (blobIter.format),
-                            GRALLOC_USAGE_HW_VIDEO_ENCODER, 0,
+                            static_cast<ConsumerUsageFlags>(ConsumerUsage::VIDEO_ENCODER), 0,
                             StreamRotation::ROTATION_0};
                     ::android::hardware::hidl_vec<Stream> streams = {
                             videoStream, blobStream};
@@ -2503,8 +2510,10 @@
 
             Status status = Status::INTERNAL_ERROR;
             uint32_t numRequestProcessed = 0;
+            hidl_vec<BufferCache> cachesToRemove;
             Return<void> returnStatus = session->processCaptureRequest(
                     {request},
+                    cachesToRemove,
                     [&status, &numRequestProcessed] (auto s, uint32_t n) {
                         status = s;
                         numRequestProcessed = n;
@@ -2535,6 +2544,7 @@
 
             returnStatus = session->processCaptureRequest(
                     {request},
+                    cachesToRemove,
                     [&status, &numRequestProcessed] (auto s, uint32_t n) {
                         status = s;
                         numRequestProcessed = n;
@@ -2601,8 +2611,10 @@
             //Settings were not correctly initialized, we should fail here
             Status status = Status::OK;
             uint32_t numRequestProcessed = 0;
+            hidl_vec<BufferCache> cachesToRemove;
             Return<void> ret = session->processCaptureRequest(
                     {request},
+                    cachesToRemove,
                     [&status, &numRequestProcessed] (auto s, uint32_t n) {
                         status = s;
                         numRequestProcessed = n;
@@ -2654,8 +2666,10 @@
             //Output buffers are missing, we should fail here
             Status status = Status::OK;
             uint32_t numRequestProcessed = 0;
+            hidl_vec<BufferCache> cachesToRemove;
             ret = session->processCaptureRequest(
                     {request},
+                    cachesToRemove,
                     [&status, &numRequestProcessed] (auto s, uint32_t n) {
                         status = s;
                         numRequestProcessed = n;
@@ -2723,8 +2737,10 @@
 
             Status status = Status::INTERNAL_ERROR;
             uint32_t numRequestProcessed = 0;
+            hidl_vec<BufferCache> cachesToRemove;
             ret = session->processCaptureRequest(
                     {request},
+                    cachesToRemove,
                     [&status, &numRequestProcessed] (auto s, uint32_t n) {
                         status = s;
                         numRequestProcessed = n;
diff --git a/configstore/1.0/default/android.hardware.configstore@1.0-service.rc b/configstore/1.0/default/android.hardware.configstore@1.0-service.rc
index 8741bdd..563d854 100644
--- a/configstore/1.0/default/android.hardware.configstore@1.0-service.rc
+++ b/configstore/1.0/default/android.hardware.configstore@1.0-service.rc
@@ -1,4 +1,4 @@
 service configstore-hal-1-0 /vendor/bin/hw/android.hardware.configstore@1.0-service
-    class hal
+    class hal animation
     user system
     group system
diff --git a/graphics/allocator/2.0/default/android.hardware.graphics.allocator@2.0-service.rc b/graphics/allocator/2.0/default/android.hardware.graphics.allocator@2.0-service.rc
index 3e2edda..9a08f66 100644
--- a/graphics/allocator/2.0/default/android.hardware.graphics.allocator@2.0-service.rc
+++ b/graphics/allocator/2.0/default/android.hardware.graphics.allocator@2.0-service.rc
@@ -1,5 +1,5 @@
 service gralloc-2-0 /vendor/bin/hw/android.hardware.graphics.allocator@2.0-service
-    class hal
+    class hal animation
     user system
     group graphics drmrpc
     onrestart restart surfaceflinger
diff --git a/graphics/composer/2.1/default/android.hardware.graphics.composer@2.1-service.rc b/graphics/composer/2.1/default/android.hardware.graphics.composer@2.1-service.rc
index b6060db..51b0e3b 100644
--- a/graphics/composer/2.1/default/android.hardware.graphics.composer@2.1-service.rc
+++ b/graphics/composer/2.1/default/android.hardware.graphics.composer@2.1-service.rc
@@ -1,5 +1,5 @@
 service hwcomposer-2-1 /vendor/bin/hw/android.hardware.graphics.composer@2.1-service
-    class hal
+    class hal animation
     user system
     group graphics drmrpc
     capabilities SYS_NICE