Camera: Allocate graphics buffers via 'IAllocator'

Camera graphic buffers should always be allocated using the
respective HIDL API.

Bug: 77681294
Test: run vts --skip-all-system-status-check --skip-preconditions
--primary-abi-only --module VtsHalCameraProviderV2_4Target -l INFO

Change-Id: Ibef88548fc3d29df606e4f66f5f3f4e89d3bf654
Merged-In: Ibef88548fc3d29df606e4f66f5f3f4e89d3bf654
diff --git a/camera/provider/2.4/vts/functional/Android.bp b/camera/provider/2.4/vts/functional/Android.bp
index 81d3de1..55829f7 100644
--- a/camera/provider/2.4/vts/functional/Android.bp
+++ b/camera/provider/2.4/vts/functional/Android.bp
@@ -36,8 +36,11 @@
         "android.hardware.camera.device@3.2",
         "android.hardware.camera.device@3.3",
         "android.hardware.camera.provider@2.4",
+        "android.hardware.graphics.allocator@2.0",
         "android.hardware.graphics.common@1.0",
         "android.hardware.graphics.mapper@2.0",
+        "android.hidl.allocator@1.0",
         "libgrallocusage",
+        "libhidlmemory",
     ],
 }
diff --git a/camera/provider/2.4/vts/functional/VtsHalCameraProviderV2_4TargetTest.cpp b/camera/provider/2.4/vts/functional/VtsHalCameraProviderV2_4TargetTest.cpp
index 0c3fe37..6a1d91e 100644
--- a/camera/provider/2.4/vts/functional/VtsHalCameraProviderV2_4TargetTest.cpp
+++ b/camera/provider/2.4/vts/functional/VtsHalCameraProviderV2_4TargetTest.cpp
@@ -43,6 +43,13 @@
 #include <system/camera_metadata.h>
 #include <ui/GraphicBuffer.h>
 
+#include <android/hardware/graphics/allocator/2.0/IAllocator.h>
+#include <android/hardware/graphics/mapper/2.0/IMapper.h>
+#include <android/hardware/graphics/mapper/2.0/types.h>
+#include <android/hidl/allocator/1.0/IAllocator.h>
+#include <android/hidl/memory/1.0/IMapper.h>
+#include <android/hidl/memory/1.0/IMemory.h>
+
 #include <VtsHalHidlTargetTestBase.h>
 #include <VtsHalHidlTargetTestEnvBase.h>
 
@@ -99,6 +106,9 @@
 using ::android::hardware::camera::device::V1_0::HandleTimestampMessage;
 using ::android::hardware::MessageQueue;
 using ::android::hardware::kSynchronizedReadWrite;
+using ::android::hidl::allocator::V1_0::IAllocator;
+using ::android::hidl::memory::V1_0::IMemory;
+using ::android::hidl::memory::V1_0::IMapper;
 using ResultMetadataQueue = MessageQueue<uint8_t, kSynchronizedReadWrite>;
 using ::android::hidl::manager::V1_0::IServiceManager;
 
@@ -606,6 +616,8 @@
     void setParameters(
             const sp<::android::hardware::camera::device::V1_0::ICameraDevice> &device,
             const CameraParameters &cameraParams);
+    void allocateGraphicBuffer(uint32_t width, uint32_t height, uint64_t usage,
+            PixelFormat format, hidl_handle *buffer_handle /*out*/);
     void waitForFrameLocked(DataCallbackMsg msgFrame,
             std::unique_lock<std::mutex> &l);
     void openEmptyDeviceSession(const std::string &name,
@@ -2994,15 +3006,15 @@
                                                                });
                 ASSERT_TRUE(ret.isOk());
 
-                sp<GraphicBuffer> gb = new GraphicBuffer(
-                    previewStream.width, previewStream.height,
-                    static_cast<int32_t>(halStreamConfig.streams[0].overrideFormat), 1,
+                hidl_handle buffer_handle;
+                allocateGraphicBuffer(previewStream.width, previewStream.height,
                     android_convertGralloc1To0Usage(halStreamConfig.streams[0].producerUsage,
-                                                    halStreamConfig.streams[0].consumerUsage));
-                ASSERT_NE(nullptr, gb.get());
+                        halStreamConfig.streams[0].consumerUsage),
+                    halStreamConfig.streams[0].overrideFormat, &buffer_handle);
+
                 StreamBuffer outputBuffer = {halStreamConfig.streams[0].id,
                                              bufferId,
-                                             hidl_handle(gb->getNativeBuffer()->handle),
+                                             buffer_handle,
                                              BufferStatus::OK,
                                              nullptr,
                                              nullptr};
@@ -3128,15 +3140,15 @@
                                        &supportsPartialResults /*out*/,
                                        &partialResultCount /*out*/);
 
-                sp<GraphicBuffer> gb = new GraphicBuffer(
-                    previewStream.width, previewStream.height,
-                    static_cast<int32_t>(halStreamConfig.streams[0].overrideFormat), 1,
+                hidl_handle buffer_handle;
+                allocateGraphicBuffer(previewStream.width, previewStream.height,
                     android_convertGralloc1To0Usage(halStreamConfig.streams[0].producerUsage,
-                                                    halStreamConfig.streams[0].consumerUsage));
+                        halStreamConfig.streams[0].consumerUsage),
+                    halStreamConfig.streams[0].overrideFormat, &buffer_handle);
 
                 StreamBuffer outputBuffer = {halStreamConfig.streams[0].id,
                                              bufferId,
-                                             hidl_handle(gb->getNativeBuffer()->handle),
+                                             buffer_handle,
                                              BufferStatus::OK,
                                              nullptr,
                                              nullptr};
@@ -3272,7 +3284,6 @@
                                        &previewStream /*out*/, &halStreamConfig /*out*/,
                                        &supportsPartialResults /*out*/,
                                        &partialResultCount /*out*/);
-
                 std::shared_ptr<ResultMetadataQueue> resultQueue;
                 auto resultQueueRet =
                     session->getCaptureResultMetadataQueue(
@@ -3300,15 +3311,15 @@
                                                                });
                 ASSERT_TRUE(ret.isOk());
 
-                sp<GraphicBuffer> gb = new GraphicBuffer(
-                    previewStream.width, previewStream.height,
-                    static_cast<int32_t>(halStreamConfig.streams[0].overrideFormat), 1,
+                hidl_handle buffer_handle;
+                allocateGraphicBuffer(previewStream.width, previewStream.height,
                     android_convertGralloc1To0Usage(halStreamConfig.streams[0].producerUsage,
-                                                    halStreamConfig.streams[0].consumerUsage));
-                ASSERT_NE(nullptr, gb.get());
+                        halStreamConfig.streams[0].consumerUsage),
+                    halStreamConfig.streams[0].overrideFormat, &buffer_handle);
+
                 StreamBuffer outputBuffer = {halStreamConfig.streams[0].id,
                                              bufferId,
-                                             hidl_handle(gb->getNativeBuffer()->handle),
+                                             buffer_handle,
                                              BufferStatus::OK,
                                              nullptr,
                                              nullptr};
@@ -3341,7 +3352,6 @@
                 Return<Status> returnStatus = session->flush();
                 ASSERT_TRUE(returnStatus.isOk());
                 ASSERT_EQ(Status::OK, returnStatus);
-
                 {
                     std::unique_lock<std::mutex> l(mLock);
                     while (!inflightReq.errorCodeValid &&
@@ -3369,7 +3379,6 @@
                                        << static_cast<uint32_t>(inflightReq.errorCode);
                         }
                     }
-
                     ret = session->close();
                     ASSERT_TRUE(ret.isOk());
                 }
@@ -3894,6 +3903,44 @@
     ASSERT_EQ(Status::OK, returnStatus);
 }
 
+void CameraHidlTest::allocateGraphicBuffer(uint32_t width, uint32_t height, uint64_t usage,
+        PixelFormat format, hidl_handle *buffer_handle /*out*/) {
+    ASSERT_NE(buffer_handle, nullptr);
+
+    sp<android::hardware::graphics::allocator::V2_0::IAllocator> allocator =
+        android::hardware::graphics::allocator::V2_0::IAllocator::getService();
+    ASSERT_NE(nullptr, allocator.get());
+
+    sp<android::hardware::graphics::mapper::V2_0::IMapper> mapper =
+        android::hardware::graphics::mapper::V2_0::IMapper::getService();
+    ASSERT_NE(mapper.get(), nullptr);
+
+    android::hardware::graphics::mapper::V2_0::IMapper::BufferDescriptorInfo descriptorInfo {};
+    descriptorInfo.width = width;
+    descriptorInfo.height = height;
+    descriptorInfo.layerCount = 1;
+    descriptorInfo.format = format;
+    descriptorInfo.usage = usage;
+
+    ::android::hardware::hidl_vec<uint32_t> descriptor;
+    auto ret = mapper->createDescriptor(
+        descriptorInfo, [&descriptor](android::hardware::graphics::mapper::V2_0::Error err,
+                            ::android::hardware::hidl_vec<uint32_t> desc) {
+            ASSERT_EQ(err, android::hardware::graphics::mapper::V2_0::Error::NONE);
+            descriptor = desc;
+        });
+    ASSERT_TRUE(ret.isOk());
+
+    ret = allocator->allocate(descriptor, 1u,
+        [&](android::hardware::graphics::mapper::V2_0::Error err, uint32_t /*stride*/,
+            const ::android::hardware::hidl_vec<::android::hardware::hidl_handle>& buffers) {
+            ASSERT_EQ(android::hardware::graphics::mapper::V2_0::Error::NONE, err);
+            ASSERT_EQ(buffers.size(), 1u);
+            *buffer_handle = buffers[0];
+        });
+    ASSERT_TRUE(ret.isOk());
+}
+
 int main(int argc, char **argv) {
   ::testing::AddGlobalTestEnvironment(CameraHidlEnvironment::Instance());
   ::testing::InitGoogleTest(&argc, argv);