From c3253373527a2508ae29cb1e915522db171d2ca5 Mon Sep 17 00:00:00 2001 From: Brian Duddie Date: Thu, 9 Jun 2022 12:37:53 -0700 Subject: [PATCH] Support AIDL IAllocator for direct channel tests Bug: 234673324 Test: atest VtsHalSensorsV2_1TargetTest -> DirectChannelGralloc pass on device using AIDL IAllocator Change-Id: I169d3bc863f7e588f42e408f151064066b9960cc --- sensors/1.0/vts/functional/Android.bp | 10 ++- sensors/2.0/vts/functional/Android.bp | 9 ++- sensors/2.1/vts/functional/Android.bp | 6 +- sensors/aidl/vts/Android.bp | 3 +- sensors/common/vts/utils/Android.bp | 23 ++++++ sensors/common/vts/utils/GrallocWrapper.cpp | 88 ++++++++++++++++----- 6 files changed, 114 insertions(+), 25 deletions(-) diff --git a/sensors/1.0/vts/functional/Android.bp b/sensors/1.0/vts/functional/Android.bp index 274cfa7fd6..d53179a380 100644 --- a/sensors/1.0/vts/functional/Android.bp +++ b/sensors/1.0/vts/functional/Android.bp @@ -26,7 +26,10 @@ package { cc_test { name: "VtsHalSensorsV1_0TargetTest", cflags: ["-DLOG_TAG=\"sensors_hidl_hal_test\""], - defaults: ["VtsHalTargetTestDefaults"], + defaults: [ + "VtsHalSensorsDefaults", + "VtsHalTargetTestDefaults", + ], tidy_timeout_srcs: [ "VtsHalSensorsV1_0TargetTest.cpp", ], @@ -34,9 +37,12 @@ cc_test { "SensorsHidlEnvironmentV1_0.cpp", "VtsHalSensorsV1_0TargetTest.cpp", ], + shared_libs: [ + "libbinder_ndk", + "libvndksupport", + ], static_libs: [ "android.hardware.sensors@1.0", - "VtsHalSensorsTargetTestUtils", ], test_suites: [ "general-tests", diff --git a/sensors/2.0/vts/functional/Android.bp b/sensors/2.0/vts/functional/Android.bp index c4ec866fc1..62eaf6b765 100644 --- a/sensors/2.0/vts/functional/Android.bp +++ b/sensors/2.0/vts/functional/Android.bp @@ -26,7 +26,10 @@ package { cc_test { name: "VtsHalSensorsV2_0TargetTest", cflags: ["-DLOG_TAG=\"sensors_hidl_hal_test\""], - defaults: ["VtsHalTargetTestDefaults"], + defaults: [ + "VtsHalSensorsDefaults", + "VtsHalTargetTestDefaults", + ], tidy_timeout_srcs: [ "VtsHalSensorsV2_0TargetTest.cpp", ], @@ -36,13 +39,15 @@ cc_test { header_libs: [ "android.hardware.sensors@2.X-shared-utils", ], + shared_libs: [ + "libbinder_ndk", + ], static_libs: [ "android.hardware.sensors@1.0", "android.hardware.sensors@1.0-convert", "android.hardware.sensors@2.0", "android.hardware.sensors@2.1", "libfmq", - "VtsHalSensorsTargetTestUtils", "VtsHalSensorsV2_0TargetTest-lib", ], test_suites: [ diff --git a/sensors/2.1/vts/functional/Android.bp b/sensors/2.1/vts/functional/Android.bp index 3659e11ffc..61cfd14d15 100644 --- a/sensors/2.1/vts/functional/Android.bp +++ b/sensors/2.1/vts/functional/Android.bp @@ -28,7 +28,10 @@ cc_test { cflags: [ "-DLOG_TAG=\"sensors_hidl_hal_test\"", ], - defaults: ["VtsHalTargetTestDefaults"], + defaults: [ + "VtsHalSensorsDefaults", + "VtsHalTargetTestDefaults", + ], srcs: [ "VtsHalSensorsV2_1TargetTest.cpp", ], @@ -41,7 +44,6 @@ cc_test { "android.hardware.sensors@2.0", "android.hardware.sensors@2.1", "libfmq", - "VtsHalSensorsTargetTestUtils", "VtsHalSensorsV2_1TargetTest-lib", ], test_suites: [ diff --git a/sensors/aidl/vts/Android.bp b/sensors/aidl/vts/Android.bp index b5a5f15691..f3972ec2a3 100644 --- a/sensors/aidl/vts/Android.bp +++ b/sensors/aidl/vts/Android.bp @@ -24,8 +24,9 @@ package { cc_test { name: "VtsAidlHalSensorsTargetTest", defaults: [ - "VtsHalTargetTestDefaults", "use_libaidlvintf_gtest_helper_static", + "VtsHalSensorsDefaults", + "VtsHalTargetTestDefaults", ], srcs: [ "VtsAidlHalSensorsTargetTest.cpp", diff --git a/sensors/common/vts/utils/Android.bp b/sensors/common/vts/utils/Android.bp index 44bed6e002..08b6afa202 100644 --- a/sensors/common/vts/utils/Android.bp +++ b/sensors/common/vts/utils/Android.bp @@ -23,6 +23,23 @@ package { default_applicable_licenses: ["hardware_interfaces_license"], } +// Prefer to use these defaults to pull in VtsHalSensorsTargetTestUtils + its +// dependencies +cc_defaults { + name: "VtsHalSensorsDefaults", + shared_libs: [ + "android.hardware.graphics.allocator-V1-ndk", + "android.hardware.graphics.common-V3-ndk", + "libbinder_ndk", + "libutils", + "libvndksupport", + ], + static_libs: [ + "libaidlcommonsupport", + "VtsHalSensorsTargetTestUtils", + ], +} + cc_library_static { name: "VtsHalSensorsTargetTestUtils", defaults: ["VtsHalTargetTestDefaults"], @@ -36,13 +53,19 @@ cc_library_static { local_include_dirs: [ "include/sensors-vts-utils", ], + // Targets that depend on us need to also include these shared_libs: [ + "android.hardware.graphics.allocator-V1-ndk", + "android.hardware.graphics.common-V3-ndk", + "libbinder_ndk", "libutils", + "libvndksupport", ], static_libs: [ "android.hardware.sensors@1.0", "android.hardware.sensors@2.0", "android.hardware.sensors@2.1", + "libaidlcommonsupport", ], whole_static_libs: [ "android.hardware.graphics.allocator@2.0", diff --git a/sensors/common/vts/utils/GrallocWrapper.cpp b/sensors/common/vts/utils/GrallocWrapper.cpp index 47d1f42152..e6e088826b 100644 --- a/sensors/common/vts/utils/GrallocWrapper.cpp +++ b/sensors/common/vts/utils/GrallocWrapper.cpp @@ -16,6 +16,9 @@ #include "GrallocWrapper.h" +#include +#include +#include #include #include #include @@ -27,8 +30,10 @@ #include #include +#include #include +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 Error2 = ::android::hardware::graphics::mapper::V2_0::Error; 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 @@ using BaseTypeOfFirstArg = typename std::remove_const< // 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 +template typename AllocatorWrapperT = sp> class GrallocHalWrapper : public IGrallocHalWrapper { public: - GrallocHalWrapper(const sp& allocator, const sp& mapper) + GrallocHalWrapper(const AllocatorWrapperT& allocator, const sp& mapper) : mAllocator(allocator), mMapper(mapper) { if (mapper->isRemote()) { ALOGE("Mapper is in passthrough mode"); @@ -116,7 +125,7 @@ class GrallocHalWrapper : public IGrallocHalWrapper { private: static constexpr uint64_t kBufferUsage = static_cast(BufferUsage::SENSOR_DIRECT_DATA | BufferUsage::CPU_READ_OFTEN); - sp mAllocator; + AllocatorWrapperT mAllocator; sp mMapper; // v2.0 and v3.0 use vec for BufferDescriptor, but v4.0 uses vec, so use @@ -128,8 +137,34 @@ class GrallocHalWrapper : public IGrallocHalWrapper { native_handle_t* importBuffer(const hidl_handle& rawHandle); }; -template -native_handle_t* GrallocHalWrapper::allocate(uint32_t size) { +template <> +native_handle_t* GrallocHalWrapper::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(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 AllocatorWrapperT> +native_handle_t* GrallocHalWrapper::allocate( + uint32_t size) { constexpr uint32_t kBufferCount = 1; BufferDescriptorT descriptor = getDescriptor(size); native_handle_t* bufferHandle = nullptr; @@ -149,17 +184,18 @@ native_handle_t* GrallocHalWrapper::allocate(uint32_t size) return bufferHandle; } -template -void GrallocHalWrapper::freeBuffer(native_handle_t* bufferHandle) { +template typename AllocatorWrapperT> +void GrallocHalWrapper::freeBuffer( + native_handle_t* bufferHandle) { auto error = mMapper->freeBuffer(bufferHandle); if (!error.isOk() || failed(error)) { ALOGE("Failed to free buffer %p", bufferHandle); } } -template -typename GrallocHalWrapper::BufferDescriptorT -GrallocHalWrapper::getDescriptor(uint32_t size) { +template typename AllocatorWrapperT> +typename GrallocHalWrapper::BufferDescriptorT +GrallocHalWrapper::getDescriptor(uint32_t size) { typename MapperT::BufferDescriptorInfo descriptorInfo = { .width = size, .height = 1, @@ -181,8 +217,8 @@ GrallocHalWrapper::getDescriptor(uint32_t size) { return descriptor; } -template -native_handle_t* GrallocHalWrapper::importBuffer( +template typename AllocatorWrapperT> +native_handle_t* GrallocHalWrapper::importBuffer( const hidl_handle& rawHandle) { native_handle_t* bufferHandle = nullptr; @@ -198,8 +234,9 @@ native_handle_t* GrallocHalWrapper::importBuffer( return bufferHandle; } -template -void* GrallocHalWrapper::lock(native_handle_t* bufferHandle) { +template typename AllocatorWrapperT> +void* GrallocHalWrapper::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 @@ void* GrallocHalWrapper::lock(native_handle_t* bufferHandle return data; } -template -void GrallocHalWrapper::unlock(native_handle_t* bufferHandle) { +template typename AllocatorWrapperT> +void GrallocHalWrapper::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 @@ GrallocWrapper::GrallocWrapper() { sp allocator4 = IAllocator4::getService(); sp 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 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( + new GrallocHalWrapper(allocatorAidl, + mapper4)); + } else if (allocator4 != nullptr && mapper4 != nullptr) { + ALOGD("AIDL IAllocator not found, using HIDL IAllocator/IMapper v4.0"); mGrallocHal = std::unique_ptr( new GrallocHalWrapper(allocator4, mapper4)); } else {