Support AIDL IAllocator for direct channel tests

Bug: 234673324
Test: atest VtsHalSensorsV2_1TargetTest -> DirectChannelGralloc pass on
      device using AIDL IAllocator
Change-Id: I169d3bc863f7e588f42e408f151064066b9960cc
This commit is contained in:
Brian Duddie
2022-06-09 12:37:53 -07:00
parent 079b3790ac
commit c325337352
6 changed files with 114 additions and 25 deletions

View File

@@ -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",

View File

@@ -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: [

View File

@@ -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: [

View File

@@ -24,8 +24,9 @@ package {
cc_test {
name: "VtsAidlHalSensorsTargetTest",
defaults: [
"VtsHalTargetTestDefaults",
"use_libaidlvintf_gtest_helper_static",
"VtsHalSensorsDefaults",
"VtsHalTargetTestDefaults",
],
srcs: [
"VtsAidlHalSensorsTargetTest.cpp",

View File

@@ -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",

View File

@@ -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 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 <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 @@ class GrallocHalWrapper : public IGrallocHalWrapper {
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 @@ class GrallocHalWrapper : public IGrallocHalWrapper {
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 @@ native_handle_t* GrallocHalWrapper<AllocatorT, MapperT>::allocate(uint32_t size)
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 @@ GrallocHalWrapper<AllocatorT, MapperT>::getDescriptor(uint32_t size) {
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 @@ native_handle_t* GrallocHalWrapper<AllocatorT, MapperT>::importBuffer(
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 @@ void* GrallocHalWrapper<AllocatorT, MapperT>::lock(native_handle_t* bufferHandle
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 @@ GrallocWrapper::GrallocWrapper() {
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 {