Adding support for different Mapper versions in IComposer VTS tests

Bug: 135139498
Test: build, boot, VtsHalGraphicsComposerV2_<1,2,3>TargetTest,
Change-Id: I05e2832c64d4c06eb4afd64c3323d7edbd19a5f4
diff --git a/graphics/composer/2.1/utils/vts/ComposerVts.cpp b/graphics/composer/2.1/utils/vts/ComposerVts.cpp
index 7ba67d4..c5d5823 100644
--- a/graphics/composer/2.1/utils/vts/ComposerVts.cpp
+++ b/graphics/composer/2.1/utils/vts/ComposerVts.cpp
@@ -315,6 +315,77 @@
     writer->reset();
 }
 
+Gralloc::Gralloc() {
+    [this] {
+        ASSERT_NO_FATAL_FAILURE(mGralloc3 = std::make_shared<Gralloc3>("default", "default",
+                                                                       /*errOnFailure=*/false));
+        if (mGralloc3->getAllocator() == nullptr || mGralloc3->getMapper() == nullptr) {
+            mGralloc3 = nullptr;
+            ASSERT_NO_FATAL_FAILURE(mGralloc2 = std::make_shared<Gralloc2>());
+        }
+    }();
+}
+
+const native_handle_t* Gralloc::allocate(uint32_t width, uint32_t height, uint32_t layerCount,
+                                         PixelFormat format, uint64_t usage, bool import,
+                                         uint32_t* outStride) {
+    if (mGralloc3) {
+        IMapper3::BufferDescriptorInfo info{};
+        info.width = width;
+        info.height = height;
+        info.layerCount = layerCount;
+        info.format = static_cast<android::hardware::graphics::common::V1_2::PixelFormat>(format);
+        info.usage = usage;
+        return mGralloc3->allocate(info, import, outStride);
+    } else {
+        IMapper2::BufferDescriptorInfo info{};
+        info.width = width;
+        info.height = height;
+        info.layerCount = layerCount;
+        info.format = format;
+        info.usage = usage;
+        return mGralloc2->allocate(info, import, outStride);
+    }
+}
+
+void* Gralloc::lock(const native_handle_t* bufferHandle, uint64_t cpuUsage,
+                    const AccessRegion& accessRegionRect, int acquireFence) {
+    if (mGralloc3) {
+        IMapper3::Rect accessRegion;
+        accessRegion.left = accessRegionRect.left;
+        accessRegion.top = accessRegionRect.top;
+        accessRegion.width = accessRegionRect.width;
+        accessRegion.height = accessRegionRect.height;
+        int32_t bytesPerPixel;
+        int32_t bytesPerStride;
+        return mGralloc3->lock(bufferHandle, cpuUsage, accessRegion, acquireFence, &bytesPerPixel,
+                               &bytesPerStride);
+    } else {
+        IMapper2::Rect accessRegion;
+        accessRegion.left = accessRegionRect.left;
+        accessRegion.top = accessRegionRect.top;
+        accessRegion.width = accessRegionRect.width;
+        accessRegion.height = accessRegionRect.height;
+        return mGralloc2->lock(bufferHandle, cpuUsage, accessRegion, acquireFence);
+    }
+}
+
+int Gralloc::unlock(const native_handle_t* bufferHandle) {
+    if (mGralloc3) {
+        return mGralloc3->unlock(bufferHandle);
+    } else {
+        return mGralloc2->unlock(bufferHandle);
+    }
+}
+
+void Gralloc::freeBuffer(const native_handle_t* bufferHandle) {
+    if (mGralloc3) {
+        mGralloc3->freeBuffer(bufferHandle);
+    } else {
+        mGralloc2->freeBuffer(bufferHandle);
+    }
+}
+
 }  // namespace vts
 }  // namespace V2_1
 }  // namespace composer