Add layered image support to gralloc1 hal.

Bug: 31686534
Test: manual
Change-Id: I6442413072cef2a19abd3aacddf964ca1f4e7481
diff --git a/graphics/mapper/2.0/default/GrallocMapper.cpp b/graphics/mapper/2.0/default/GrallocMapper.cpp
index eeca5c7..2af1d2c 100644
--- a/graphics/mapper/2.0/default/GrallocMapper.cpp
+++ b/graphics/mapper/2.0/default/GrallocMapper.cpp
@@ -15,10 +15,13 @@
 
 #define LOG_TAG "GrallocMapperPassthrough"
 
+#include <android/hardware/graphics/allocator/2.0/IAllocator.h>
 #include <android/hardware/graphics/mapper/2.0/IMapper.h>
 #include <hardware/gralloc1.h>
 #include <log/log.h>
 
+#include <unordered_set>
+
 namespace android {
 namespace hardware {
 namespace graphics {
@@ -26,6 +29,8 @@
 namespace V2_0 {
 namespace implementation {
 
+using Capability = allocator::V2_0::IAllocator::Capability;
+
 class GrallocDevice : public Device {
 public:
     GrallocDevice();
@@ -38,6 +43,8 @@
             uint32_t* outWidth, uint32_t* outHeight);
     Error getFormat(const native_handle_t* bufferHandle,
             PixelFormat* outFormat);
+    Error getLayerCount(const native_handle_t* bufferHandle,
+            uint32_t* outLayerCount);
     Error getProducerUsageMask(const native_handle_t* bufferHandle,
             uint64_t* outUsageMask);
     Error getConsumerUsageMask(const native_handle_t* bufferHandle,
@@ -58,15 +65,21 @@
             int32_t* outReleaseFence);
 
 private:
+    void initCapabilities();
+
     void initDispatch();
+    bool hasCapability(Capability capability) const;
 
     gralloc1_device_t* mDevice;
 
+    std::unordered_set<Capability> mCapabilities;
+
     struct {
         GRALLOC1_PFN_RETAIN retain;
         GRALLOC1_PFN_RELEASE release;
         GRALLOC1_PFN_GET_DIMENSIONS getDimensions;
         GRALLOC1_PFN_GET_FORMAT getFormat;
+        GRALLOC1_PFN_GET_LAYER_COUNT getLayerCount;
         GRALLOC1_PFN_GET_PRODUCER_USAGE getProducerUsage;
         GRALLOC1_PFN_GET_CONSUMER_USAGE getConsumerUsage;
         GRALLOC1_PFN_GET_BACKING_STORE getBackingStore;
@@ -96,6 +109,7 @@
         LOG_ALWAYS_FATAL("failed to open gralloc1 device");
     }
 
+    initCapabilities();
     initDispatch();
 }
 
@@ -104,6 +118,19 @@
     gralloc1_close(mDevice);
 }
 
+void GrallocDevice::initCapabilities()
+{
+    uint32_t count;
+    mDevice->getCapabilities(mDevice, &count, nullptr);
+
+    std::vector<Capability> caps(count);
+    mDevice->getCapabilities(mDevice, &count, reinterpret_cast<
+              std::underlying_type<Capability>::type*>(caps.data()));
+    caps.resize(count);
+
+    mCapabilities.insert(caps.cbegin(), caps.cend());
+}
+
 void GrallocDevice::initDispatch()
 {
 #define CHECK_FUNC(func, desc) do {                                   \
@@ -118,6 +145,9 @@
     CHECK_FUNC(release, GRALLOC1_FUNCTION_RELEASE);
     CHECK_FUNC(getDimensions, GRALLOC1_FUNCTION_GET_DIMENSIONS);
     CHECK_FUNC(getFormat, GRALLOC1_FUNCTION_GET_FORMAT);
+    if (hasCapability(Capability::LAYERED_BUFFERS)) {
+        CHECK_FUNC(getLayerCount, GRALLOC1_FUNCTION_GET_LAYER_COUNT);
+    }
     CHECK_FUNC(getProducerUsage, GRALLOC1_FUNCTION_GET_PRODUCER_USAGE);
     CHECK_FUNC(getConsumerUsage, GRALLOC1_FUNCTION_GET_CONSUMER_USAGE);
     CHECK_FUNC(getBackingStore, GRALLOC1_FUNCTION_GET_BACKING_STORE);
@@ -130,6 +160,11 @@
 #undef CHECK_FUNC
 }
 
+bool GrallocDevice::hasCapability(Capability capability) const
+{
+    return (mCapabilities.count(capability) > 0);
+}
+
 Error GrallocDevice::retain(const native_handle_t* bufferHandle)
 {
     int32_t error = mDispatch.retain(mDevice, bufferHandle);
@@ -158,6 +193,19 @@
     return static_cast<Error>(error);
 }
 
+Error GrallocDevice::getLayerCount(const native_handle_t* bufferHandle,
+        uint32_t* outLayerCount)
+{
+    if (hasCapability(Capability::LAYERED_BUFFERS)) {
+        int32_t error = mDispatch.getLayerCount(mDevice, bufferHandle,
+                outLayerCount);
+        return static_cast<Error>(error);
+    } else {
+        *outLayerCount = 1;
+        return Error::NONE;
+    }
+}
+
 Error GrallocDevice::getProducerUsageMask(const native_handle_t* bufferHandle,
         uint64_t* outUsageMask)
 {
@@ -238,6 +286,7 @@
         .release = release,
         .getDimensions = getDimensions,
         .getFormat = getFormat,
+        .getLayerCount = getLayerCount,
         .getProducerUsageMask = getProducerUsageMask,
         .getConsumerUsageMask = getConsumerUsageMask,
         .getBackingStore = getBackingStore,
@@ -296,6 +345,12 @@
         return cast(device)->getFormat(bufferHandle, outFormat);
     }
 
+    static Error getLayerCount(Device* device,
+            const native_handle_t* bufferHandle, uint32_t* outLayerCount)
+    {
+        return cast(device)->getLayerCount(bufferHandle, outLayerCount);
+    }
+
     static Error getProducerUsageMask(Device* device,
             const native_handle_t* bufferHandle, uint64_t* outUsageMask)
     {