Support AIDL IAllocator for direct channel tests
Bug: 234673324
Test: atest VtsHalSensorsV2_1TargetTest -> DirectChannelGralloc pass on
device using AIDL IAllocator
Change-Id: I169d3bc863f7e588f42e408f151064066b9960cc
diff --git a/sensors/common/vts/utils/GrallocWrapper.cpp b/sensors/common/vts/utils/GrallocWrapper.cpp
index 47d1f42..e6e0888 100644
--- a/sensors/common/vts/utils/GrallocWrapper.cpp
+++ b/sensors/common/vts/utils/GrallocWrapper.cpp
@@ -16,6 +16,9 @@
#include "GrallocWrapper.h"
+#include <aidl/android/hardware/graphics/allocator/IAllocator.h>
+#include <aidlcommonsupport/NativeHandle.h>
+#include <android/binder_manager.h>
#include <android/hardware/graphics/allocator/2.0/IAllocator.h>
#include <android/hardware/graphics/allocator/3.0/IAllocator.h>
#include <android/hardware/graphics/allocator/4.0/IAllocator.h>
@@ -27,8 +30,10 @@
#include <utils/Log.h>
#include <cinttypes>
+#include <memory>
#include <type_traits>
+using IAllocatorAidl = ::aidl::android::hardware::graphics::allocator::IAllocator;
using IAllocator2 = ::android::hardware::graphics::allocator::V2_0::IAllocator;
using IAllocator3 = ::android::hardware::graphics::allocator::V3_0::IAllocator;
using IAllocator4 = ::android::hardware::graphics::allocator::V4_0::IAllocator;
@@ -41,6 +46,9 @@
using Error3 = ::android::hardware::graphics::mapper::V3_0::Error;
using Error4 = ::android::hardware::graphics::mapper::V4_0::Error;
+using ::aidl::android::hardware::common::NativeHandle;
+using ::aidl::android::hardware::graphics::allocator::AllocationResult;
+
using ::android::hardware::graphics::common::V1_0::BufferUsage;
using ::android::hardware::graphics::common::V1_0::PixelFormat;
@@ -97,10 +105,11 @@
// Since all the type and function names are the same for the things we use across the major HAL
// versions, we use template magic to avoid repeating ourselves.
-template <typename AllocatorT, typename MapperT>
+template <typename AllocatorT, typename MapperT,
+ template <typename> typename AllocatorWrapperT = sp>
class GrallocHalWrapper : public IGrallocHalWrapper {
public:
- GrallocHalWrapper(const sp<AllocatorT>& allocator, const sp<MapperT>& mapper)
+ GrallocHalWrapper(const AllocatorWrapperT<AllocatorT>& allocator, const sp<MapperT>& mapper)
: mAllocator(allocator), mMapper(mapper) {
if (mapper->isRemote()) {
ALOGE("Mapper is in passthrough mode");
@@ -116,7 +125,7 @@
private:
static constexpr uint64_t kBufferUsage =
static_cast<uint64_t>(BufferUsage::SENSOR_DIRECT_DATA | BufferUsage::CPU_READ_OFTEN);
- sp<AllocatorT> mAllocator;
+ AllocatorWrapperT<AllocatorT> mAllocator;
sp<MapperT> mMapper;
// v2.0 and v3.0 use vec<uint32_t> for BufferDescriptor, but v4.0 uses vec<uint8_t>, so use
@@ -128,8 +137,34 @@
native_handle_t* importBuffer(const hidl_handle& rawHandle);
};
-template <typename AllocatorT, typename MapperT>
-native_handle_t* GrallocHalWrapper<AllocatorT, MapperT>::allocate(uint32_t size) {
+template <>
+native_handle_t* GrallocHalWrapper<IAllocatorAidl, IMapper4, std::shared_ptr>::allocate(
+ uint32_t size) {
+ constexpr uint32_t kBufferCount = 1;
+ BufferDescriptorT descriptor = getDescriptor(size);
+ native_handle_t* bufferHandle = nullptr;
+
+ AllocationResult result;
+ auto status = mAllocator->allocate(descriptor, kBufferCount, &result);
+ if (!status.isOk()) {
+ status_t error = status.getExceptionCode();
+ ALOGE("Failed to allocate buffer: %" PRId32, static_cast<int32_t>(error));
+ } else if (result.buffers.size() != kBufferCount) {
+ ALOGE("Invalid buffer array size (got %zu, expected %" PRIu32 ")", result.buffers.size(),
+ kBufferCount);
+ } else {
+ // Convert from AIDL NativeHandle to native_handle_t to hidl_handle
+ hidl_handle hidlHandle;
+ hidlHandle.setTo(dupFromAidl(result.buffers[0]), /*shouldOwn*/ true);
+ bufferHandle = importBuffer(hidlHandle);
+ }
+
+ return bufferHandle;
+}
+
+template <typename AllocatorT, typename MapperT, template <typename> typename AllocatorWrapperT>
+native_handle_t* GrallocHalWrapper<AllocatorT, MapperT, AllocatorWrapperT>::allocate(
+ uint32_t size) {
constexpr uint32_t kBufferCount = 1;
BufferDescriptorT descriptor = getDescriptor(size);
native_handle_t* bufferHandle = nullptr;
@@ -149,17 +184,18 @@
return bufferHandle;
}
-template <typename AllocatorT, typename MapperT>
-void GrallocHalWrapper<AllocatorT, MapperT>::freeBuffer(native_handle_t* bufferHandle) {
+template <typename AllocatorT, typename MapperT, template <typename> typename AllocatorWrapperT>
+void GrallocHalWrapper<AllocatorT, MapperT, AllocatorWrapperT>::freeBuffer(
+ native_handle_t* bufferHandle) {
auto error = mMapper->freeBuffer(bufferHandle);
if (!error.isOk() || failed(error)) {
ALOGE("Failed to free buffer %p", bufferHandle);
}
}
-template <typename AllocatorT, typename MapperT>
-typename GrallocHalWrapper<AllocatorT, MapperT>::BufferDescriptorT
-GrallocHalWrapper<AllocatorT, MapperT>::getDescriptor(uint32_t size) {
+template <typename AllocatorT, typename MapperT, template <typename> typename AllocatorWrapperT>
+typename GrallocHalWrapper<AllocatorT, MapperT, AllocatorWrapperT>::BufferDescriptorT
+GrallocHalWrapper<AllocatorT, MapperT, AllocatorWrapperT>::getDescriptor(uint32_t size) {
typename MapperT::BufferDescriptorInfo descriptorInfo = {
.width = size,
.height = 1,
@@ -181,8 +217,8 @@
return descriptor;
}
-template <typename AllocatorT, typename MapperT>
-native_handle_t* GrallocHalWrapper<AllocatorT, MapperT>::importBuffer(
+template <typename AllocatorT, typename MapperT, template <typename> typename AllocatorWrapperT>
+native_handle_t* GrallocHalWrapper<AllocatorT, MapperT, AllocatorWrapperT>::importBuffer(
const hidl_handle& rawHandle) {
native_handle_t* bufferHandle = nullptr;
@@ -198,8 +234,9 @@
return bufferHandle;
}
-template <typename AllocatorT, typename MapperT>
-void* GrallocHalWrapper<AllocatorT, MapperT>::lock(native_handle_t* bufferHandle) {
+template <typename AllocatorT, typename MapperT, template <typename> typename AllocatorWrapperT>
+void* GrallocHalWrapper<AllocatorT, MapperT, AllocatorWrapperT>::lock(
+ native_handle_t* bufferHandle) {
// Per the HAL, all-zeros Rect means the entire buffer
typename MapperT::Rect accessRegion = {};
hidl_handle acquireFenceHandle; // No fence needed, already safe to lock
@@ -218,8 +255,9 @@
return data;
}
-template <typename AllocatorT, typename MapperT>
-void GrallocHalWrapper<AllocatorT, MapperT>::unlock(native_handle_t* bufferHandle) {
+template <typename AllocatorT, typename MapperT, template <typename> typename AllocatorWrapperT>
+void GrallocHalWrapper<AllocatorT, MapperT, AllocatorWrapperT>::unlock(
+ native_handle_t* bufferHandle) {
mMapper->unlock(bufferHandle, [&](auto error, const hidl_handle& /*releaseFence*/) {
if (failed(error)) {
ALOGE("Failed to unlock buffer %p: %" PRId32, bufferHandle,
@@ -234,8 +272,22 @@
sp<IAllocator4> allocator4 = IAllocator4::getService();
sp<IMapper4> mapper4 = IMapper4::getService();
- if (allocator4 != nullptr && mapper4 != nullptr) {
- ALOGD("Using IAllocator/IMapper v4.0");
+ const auto kAllocatorSvc = std::string(IAllocatorAidl::descriptor) + "/default";
+ std::shared_ptr<IAllocatorAidl> allocatorAidl;
+ if (AServiceManager_isDeclared(kAllocatorSvc.c_str())) {
+ allocatorAidl = IAllocatorAidl::fromBinder(
+ ndk::SpAIBinder(AServiceManager_checkService(kAllocatorSvc.c_str())));
+ }
+
+ // As of T, AIDL Allocator is supported only with HIDL Mapper4
+ // (ref: VtsHalGraphicsAllocatorAidl_TargetTest.cpp)
+ if (allocatorAidl != nullptr && mapper4 != nullptr) {
+ ALOGD("Using AIDL IAllocator + HIDL IMapper v4.0");
+ mGrallocHal = std::unique_ptr<IGrallocHalWrapper>(
+ new GrallocHalWrapper<IAllocatorAidl, IMapper4, std::shared_ptr>(allocatorAidl,
+ mapper4));
+ } else if (allocator4 != nullptr && mapper4 != nullptr) {
+ ALOGD("AIDL IAllocator not found, using HIDL IAllocator/IMapper v4.0");
mGrallocHal = std::unique_ptr<IGrallocHalWrapper>(
new GrallocHalWrapper<IAllocator4, IMapper4>(allocator4, mapper4));
} else {