gralloc: add get and set BufferMetadata support

Add get and set functions that allow buffer metadata to be set
on the buffer.

This patch has been reviewed by vendors on AOSP. It is a combination of the
following patches. They have been squashed (and updated) to make them easier to
review. Two of the patches are proposals by ARM that have been incorporated into
this patch.

https://android-review.googlesource.com/c/platform/hardware/interfaces/+/1107574
https://android-review.googlesource.com/c/platform/hardware/interfaces/+/1109946
https://android-review.googlesource.com/c/platform/hardware/interfaces/+/1124857
https://android-review.googlesource.com/c/platform/hardware/interfaces/+/1126861

Bug: 141632767, 141631993
Test: VtsHalGraphicsMapperV4_0TargetTest

Change-Id: I0e79d73a7687f2f5b51828a7454888e6d8ff9460
diff --git a/graphics/mapper/4.0/utils/vts/MapperVts.cpp b/graphics/mapper/4.0/utils/vts/MapperVts.cpp
index 056b7c9..531f311 100644
--- a/graphics/mapper/4.0/utils/vts/MapperVts.cpp
+++ b/graphics/mapper/4.0/utils/vts/MapperVts.cpp
@@ -14,6 +14,7 @@
  * limitations under the License.
  */
 
+#include <gralloctypes/Gralloc4.h>
 #include <mapper-vts/4.0/MapperVts.h>
 
 #include <VtsHalHidlTargetTestBase.h>
@@ -92,28 +93,39 @@
 
 std::vector<const native_handle_t*> Gralloc::allocate(const BufferDescriptor& descriptor,
                                                       uint32_t count, bool import,
-                                                      uint32_t* outStride) {
+                                                      bool allowFailure, uint32_t* outStride) {
     std::vector<const native_handle_t*> bufferHandles;
     bufferHandles.reserve(count);
-    mAllocator->allocate(descriptor, count,
-                         [&](const auto& tmpError, const auto& tmpStride, const auto& tmpBuffers) {
-                             ASSERT_EQ(Error::NONE, tmpError) << "failed to allocate buffers";
-                             ASSERT_EQ(count, tmpBuffers.size()) << "invalid buffer array";
+    mAllocator->allocate(
+            descriptor, count,
+            [&](const auto& tmpError, const auto& tmpStride, const auto& tmpBuffers) {
+                ASSERT_EQ(Error::NONE, tmpError) << "failed to allocate buffers";
+                ASSERT_EQ(count, tmpBuffers.size()) << "invalid buffer array";
 
-                             for (uint32_t i = 0; i < count; i++) {
-                                 if (import) {
-                                     ASSERT_NO_FATAL_FAILURE(
-                                             bufferHandles.push_back(importBuffer(tmpBuffers[i])));
-                                 } else {
-                                     ASSERT_NO_FATAL_FAILURE(
-                                             bufferHandles.push_back(cloneBuffer(tmpBuffers[i])));
-                                 }
-                             }
+                for (uint32_t i = 0; i < count; i++) {
+                    const native_handle_t* bufferHandle = nullptr;
+                    if (import) {
+                        if (allowFailure) {
+                            bufferHandle = importBuffer(tmpBuffers[i]);
+                        } else {
+                            ASSERT_NO_FATAL_FAILURE(bufferHandle = importBuffer(tmpBuffers[i]));
+                        }
+                    } else {
+                        if (allowFailure) {
+                            bufferHandle = cloneBuffer(tmpBuffers[i]);
+                        } else {
+                            ASSERT_NO_FATAL_FAILURE(bufferHandle = cloneBuffer(tmpBuffers[i]));
+                        }
+                    }
+                    if (bufferHandle) {
+                        bufferHandles.push_back(bufferHandle);
+                    }
+                }
 
-                             if (outStride) {
-                                 *outStride = tmpStride;
-                             }
-                         });
+                if (outStride) {
+                    *outStride = tmpStride;
+                }
+            });
 
     if (::testing::Test::HasFatalFailure()) {
         bufferHandles.clear();
@@ -123,17 +135,20 @@
 }
 
 const native_handle_t* Gralloc::allocate(const IMapper::BufferDescriptorInfo& descriptorInfo,
-                                         bool import, uint32_t* outStride) {
+                                         bool import, bool allowFailure, uint32_t* outStride) {
     BufferDescriptor descriptor = createDescriptor(descriptorInfo);
     if (::testing::Test::HasFatalFailure()) {
         return nullptr;
     }
 
-    auto buffers = allocate(descriptor, 1, import, outStride);
+    auto buffers = allocate(descriptor, 1, import, allowFailure, outStride);
     if (::testing::Test::HasFatalFailure()) {
         return nullptr;
     }
 
+    if (buffers.size() != 1) {
+        return nullptr;
+    }
     return buffers[0];
 }
 
@@ -167,6 +182,10 @@
 }
 
 void Gralloc::freeBuffer(const native_handle_t* bufferHandle) {
+    if (bufferHandle == nullptr) {
+        return;
+    }
+
     auto buffer = const_cast<native_handle_t*>(bufferHandle);
 
     if (mImportedBuffers.erase(bufferHandle)) {
@@ -296,6 +315,35 @@
     return supported;
 }
 
+Error Gralloc::get(const native_handle_t* bufferHandle, const IMapper::MetadataType& metadataType,
+                   hidl_vec<uint8_t>* outVec) {
+    Error err;
+    mMapper->get(const_cast<native_handle_t*>(bufferHandle), metadataType,
+                 [&](const auto& tmpError, const hidl_vec<uint8_t>& tmpVec) {
+                     err = tmpError;
+                     *outVec = tmpVec;
+                 });
+    return err;
+}
+
+Error Gralloc::set(const native_handle_t* bufferHandle, const IMapper::MetadataType& metadataType,
+                   const hidl_vec<uint8_t>& vec) {
+    return mMapper->set(const_cast<native_handle_t*>(bufferHandle), metadataType, vec);
+}
+
+Error Gralloc::getFromBufferDescriptorInfo(const IMapper::BufferDescriptorInfo& descriptorInfo,
+                                           const IMapper::MetadataType& metadataType,
+                                           hidl_vec<uint8_t>* outVec) {
+    Error err;
+    mMapper->getFromBufferDescriptorInfo(
+            descriptorInfo, metadataType,
+            [&](const auto& tmpError, const hidl_vec<uint8_t>& tmpVec) {
+                err = tmpError;
+                *outVec = tmpVec;
+            });
+    return err;
+}
+
 }  // namespace vts
 }  // namespace V4_0
 }  // namespace mapper