Migrate sensors VTS to use libui am: 02b1d722f9 am: e6c67b7631
Original change: https://android-review.googlesource.com/c/platform/hardware/interfaces/+/3432193
Change-Id: I57981fc433a413a8c5a1deb0f2ec5d41b9fb0b00
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
diff --git a/sensors/aidl/vts/SensorsAidlTestSharedMemory.h b/sensors/aidl/vts/SensorsAidlTestSharedMemory.h
index 4b5916a..200f26f 100644
--- a/sensors/aidl/vts/SensorsAidlTestSharedMemory.h
+++ b/sensors/aidl/vts/SensorsAidlTestSharedMemory.h
@@ -17,7 +17,11 @@
#ifndef ANDROID_SENSORS_AIDL_TEST_SHARED_MEMORY_H
#define ANDROID_SENSORS_AIDL_TEST_SHARED_MEMORY_H
-#include "sensors-vts-utils/GrallocWrapper.h"
+#include <aidl/android/hardware/graphics/common/BufferUsage.h>
+#include <aidl/android/hardware/graphics/common/PixelFormat.h>
+#include <ui/GraphicBuffer.h>
+#include <ui/GraphicBufferAllocator.h>
+#include <ui/GraphicBufferMapper.h>
#include <aidlcommonsupport/NativeHandle.h>
#include <android-base/macros.h>
@@ -28,6 +32,8 @@
#include <cutils/ashmem.h>
+using ::aidl::android::hardware::graphics::common::BufferUsage;
+using ::aidl::android::hardware::graphics::common::PixelFormat;
using ::aidl::android::hardware::sensors::BnSensors;
using ::aidl::android::hardware::sensors::Event;
using ::aidl::android::hardware::sensors::ISensors;
@@ -53,12 +59,22 @@
}
ISensors::SharedMemInfo getSharedMemInfo() const {
- ISensors::SharedMemInfo mem = {
- .type = mType,
- .format = ISensors::SharedMemInfo::SharedMemFormat::SENSORS_EVENT,
- .size = static_cast<int32_t>(mSize),
- .memoryHandle = android::dupToAidl(mNativeHandle)};
- return mem;
+ if (mType == ISensors::SharedMemInfo::SharedMemType::GRALLOC) {
+ ISensors::SharedMemInfo mem = {
+ .type = mType,
+ .format = ISensors::SharedMemInfo::SharedMemFormat::SENSORS_EVENT,
+ .size = static_cast<int32_t>(mSize),
+ .memoryHandle = android::dupToAidl(mBufferHandle)};
+ return mem;
+
+ } else {
+ ISensors::SharedMemInfo mem = {
+ .type = mType,
+ .format = ISensors::SharedMemInfo::SharedMemFormat::SENSORS_EVENT,
+ .size = static_cast<int32_t>(mSize),
+ .memoryHandle = android::dupToAidl(mNativeHandle)};
+ return mem;
+ }
}
char* getBuffer() const { return mBuffer; }
size_t getSize() const { return mSize; }
@@ -141,17 +157,26 @@
}
case ISensors::SharedMemInfo::SharedMemType::GRALLOC: {
if (mSize != 0) {
- mGrallocWrapper->freeBuffer(mNativeHandle);
- mNativeHandle = nullptr;
+ android::status_t status =
+ android::GraphicBufferAllocator::get().free(mBufferHandle);
+ if (status != android::OK) {
+ ALOGE("SensorsAidlTestSharedMemory Gralloc failed to free buffer. Status: "
+ "%s",
+ android::statusToString(status).c_str());
+ }
+ mBufferHandle = nullptr;
+ mBuffer = nullptr;
mSize = 0;
}
break;
}
default: {
- if (mNativeHandle != nullptr || mSize != 0 || mBuffer != nullptr) {
+ if (mNativeHandle != nullptr || mSize != 0 || mBuffer != nullptr ||
+ mBufferHandle != nullptr) {
ALOGE("SensorsAidlTestSharedMemory %p not properly destructed: "
- "type %d, native handle %p, size %zu, buffer %p",
- this, static_cast<int>(mType), mNativeHandle, mSize, mBuffer);
+ "type %d, native handle %p, size %zu, buffer %p, buffer handle %p",
+ this, static_cast<int>(mType), mNativeHandle, mSize, mBuffer,
+ mBufferHandle);
}
break;
}
@@ -185,14 +210,33 @@
break;
}
case ISensors::SharedMemInfo::SharedMemType::GRALLOC: {
- mGrallocWrapper = std::make_unique<::android::GrallocWrapper>();
- if (!mGrallocWrapper->isInitialized()) {
+ static constexpr uint64_t kBufferUsage =
+ static_cast<uint64_t>(BufferUsage::SENSOR_DIRECT_DATA) |
+ static_cast<uint64_t>(BufferUsage::CPU_READ_OFTEN) |
+ static_cast<uint64_t>(BufferUsage::CPU_WRITE_RARELY);
+
+ uint32_t stride = 0;
+ buffer_handle_t bufferHandle;
+ android::status_t status = android::GraphicBufferAllocator::get().allocate(
+ size, 1, static_cast<int>(PixelFormat::BLOB), 1, kBufferUsage,
+ &bufferHandle, &stride, "SensorVts");
+ if (status != android::OK) {
+ ALOGE("SensorsAidlTestSharedMemory failed to allocate memory. Status: %s",
+ android::statusToString(status).c_str());
break;
}
-
- std::pair<native_handle_t*, void*> buf = mGrallocWrapper->allocate(size);
- handle = buf.first;
- buffer = static_cast<char*>(buf.second);
+ // Per the HAL, all-zeros Rect means the entire buffer
+ android::Rect rect = {0, 0, 0, 0};
+ void* ret;
+ status = android::GraphicBufferMapper::get().lock(bufferHandle, kBufferUsage, rect,
+ &ret);
+ if (status != android::OK) {
+ ALOGE("SensorsAidlTestSharedMemory failed to import buffer: Status: %s",
+ android::statusToString(status).c_str());
+ } else {
+ buffer = static_cast<char*>(ret);
+ mBufferHandle = bufferHandle;
+ }
break;
}
default:
@@ -208,9 +252,9 @@
ISensors::SharedMemInfo::SharedMemType mType;
native_handle_t* mNativeHandle;
+ buffer_handle_t mBufferHandle;
size_t mSize;
char* mBuffer;
- std::unique_ptr<::android::GrallocWrapper> mGrallocWrapper;
DISALLOW_COPY_AND_ASSIGN(SensorsAidlTestSharedMemory);
};
diff --git a/sensors/common/vts/2_X/VtsHalSensorsV2_XTargetTest.h b/sensors/common/vts/2_X/VtsHalSensorsV2_XTargetTest.h
index b17ecb8..eb626ff37 100644
--- a/sensors/common/vts/2_X/VtsHalSensorsV2_XTargetTest.h
+++ b/sensors/common/vts/2_X/VtsHalSensorsV2_XTargetTest.h
@@ -52,8 +52,6 @@
using ::android::hardware::sensors::V1_0::Vec3;
using ::android::hardware::sensors::V2_1::implementation::convertToOldSensorInfos;
using std::chrono::duration_cast;
-using std::chrono::microseconds;
-using std::chrono::milliseconds;
using std::chrono::nanoseconds;
using EventV1_0 = ::android::hardware::sensors::V1_0::Event;
@@ -91,7 +89,7 @@
}
void waitForFlushEvents(const std::vector<SensorInfoType>& sensorsToWaitFor,
- int32_t numCallsToFlush, milliseconds timeout) {
+ int32_t numCallsToFlush, std::chrono::milliseconds timeout) {
std::unique_lock<std::recursive_mutex> lock(mFlushMutex);
mFlushCV.wait_for(lock, timeout,
[&] { return flushesReceived(sensorsToWaitFor, numCallsToFlush); });
@@ -102,7 +100,8 @@
return mEventMap[sensorHandle];
}
- void waitForEvents(const std::vector<SensorInfoType>& sensorsToWaitFor, milliseconds timeout) {
+ void waitForEvents(const std::vector<SensorInfoType>& sensorsToWaitFor,
+ std::chrono::milliseconds timeout) {
std::unique_lock<std::recursive_mutex> lock(mEventMutex);
mEventCV.wait_for(lock, timeout, [&] { return eventsReceived(sensorsToWaitFor); });
}
@@ -472,7 +471,7 @@
}
// Wait for events to be written back to the Event FMQ
- callback.waitForEvents(sensors, milliseconds(1000) /* timeout */);
+ callback.waitForEvents(sensors, std::chrono::milliseconds(1000) /* timeout */);
getEnvironment()->unregisterCallback();
for (const auto& s : sensors) {
@@ -633,7 +632,7 @@
}
// Wait up to one second for the flush events
- callback.waitForFlushEvents(sensorGroup, flushCalls, milliseconds(1000) /* timeout */);
+ callback.waitForFlushEvents(sensorGroup, flushCalls, std::chrono::milliseconds(1000) /* timeout */);
// Deactivate all sensors after waiting for flush events so pending flush events are not
// abandoned by the HAL.
@@ -760,8 +759,8 @@
}
TEST_P(SensorsHidlTest, NoStaleEvents) {
- constexpr milliseconds kFiveHundredMs(500);
- constexpr milliseconds kOneSecond(1000);
+ constexpr std::chrono::milliseconds kFiveHundredMs(500);
+ constexpr std::chrono::milliseconds kOneSecond(1000);
// Register the callback to receive sensor events
EventCallback callback;
@@ -769,10 +768,11 @@
// This test is not valid for one-shot, on-change or special-report-mode sensors
const std::vector<SensorInfoType> sensors = getNonOneShotAndNonOnChangeAndNonSpecialSensors();
- milliseconds maxMinDelay(0);
+ std::chrono::milliseconds maxMinDelay(0);
for (const SensorInfoType& sensor : sensors) {
- milliseconds minDelay = duration_cast<milliseconds>(microseconds(sensor.minDelay));
- maxMinDelay = milliseconds(std::max(maxMinDelay.count(), minDelay.count()));
+ std::chrono::milliseconds minDelay = duration_cast<std::chrono::milliseconds>(
+ std::chrono::microseconds(sensor.minDelay));
+ maxMinDelay = std::chrono::milliseconds(std::max(maxMinDelay.count(), minDelay.count()));
}
// Activate the sensors so that they start generating events
@@ -799,7 +799,7 @@
}
// Allow some time to pass, reset the callback, then reactivate the sensors
- usleep(duration_cast<microseconds>(kOneSecond + (5 * maxMinDelay)).count());
+ usleep(duration_cast<std::chrono::microseconds>(kOneSecond + (5 * maxMinDelay)).count());
callback.reset();
activateAllSensors(true);
callback.waitForEvents(sensors, kFiveHundredMs + (5 * maxMinDelay));
@@ -821,9 +821,10 @@
// Ensure that the first event received is not stale by ensuring that its timestamp is
// sufficiently different from the previous event
const EventType newEvent = callback.getEvents(sensor.sensorHandle).front();
- milliseconds delta = duration_cast<milliseconds>(
+ std::chrono::milliseconds delta = duration_cast<std::chrono::milliseconds>(
nanoseconds(newEvent.timestamp - lastEventTimestampMap[sensor.sensorHandle]));
- milliseconds sensorMinDelay = duration_cast<milliseconds>(microseconds(sensor.minDelay));
+ std::chrono::milliseconds sensorMinDelay = duration_cast<std::chrono::milliseconds>(
+ std::chrono::microseconds(sensor.minDelay));
ASSERT_GE(delta, kFiveHundredMs + (3 * sensorMinDelay));
}
}
diff --git a/sensors/common/vts/utils/Android.bp b/sensors/common/vts/utils/Android.bp
index b35280a..ab3984c 100644
--- a/sensors/common/vts/utils/Android.bp
+++ b/sensors/common/vts/utils/Android.bp
@@ -35,6 +35,7 @@
"libbinder_ndk",
"libutils",
"libvndksupport",
+ "libui",
],
static_libs: [
"libaidlcommonsupport",
@@ -50,9 +51,6 @@
"android.hardware.graphics.common-ndk_shared",
],
cflags: ["-DLOG_TAG=\"sensors_hidl_hal_test\""],
- srcs: [
- "GrallocWrapper.cpp",
- ],
export_include_dirs: [
"include",
],
@@ -64,6 +62,7 @@
"libbinder_ndk",
"libutils",
"libvndksupport",
+ "libui",
],
static_libs: [
"android.hardware.sensors@1.0",
@@ -71,13 +70,4 @@
"android.hardware.sensors@2.1",
"libaidlcommonsupport",
],
- whole_static_libs: [
- "android.hardware.graphics.allocator@2.0",
- "android.hardware.graphics.allocator@3.0",
- "android.hardware.graphics.allocator@4.0",
- "android.hardware.graphics.mapper@2.0",
- "android.hardware.graphics.mapper@2.1",
- "android.hardware.graphics.mapper@3.0",
- "android.hardware.graphics.mapper@4.0",
- ],
}
diff --git a/sensors/common/vts/utils/GrallocWrapper.cpp b/sensors/common/vts/utils/GrallocWrapper.cpp
deleted file mode 100644
index e6e0888..0000000
--- a/sensors/common/vts/utils/GrallocWrapper.cpp
+++ /dev/null
@@ -1,354 +0,0 @@
-/*
- * Copyright (C) 2017 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#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>
-#include <android/hardware/graphics/mapper/2.0/IMapper.h>
-#include <android/hardware/graphics/mapper/2.1/IMapper.h>
-#include <android/hardware/graphics/mapper/3.0/IMapper.h>
-#include <android/hardware/graphics/mapper/4.0/IMapper.h>
-
-#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;
-using IMapper2 = ::android::hardware::graphics::mapper::V2_0::IMapper;
-using IMapper2_1 = ::android::hardware::graphics::mapper::V2_1::IMapper;
-using IMapper3 = ::android::hardware::graphics::mapper::V3_0::IMapper;
-using IMapper4 = ::android::hardware::graphics::mapper::V4_0::IMapper;
-
-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;
-
-using ::android::hardware::hidl_handle;
-using ::android::hardware::hidl_string;
-using ::android::hardware::hidl_vec;
-
-namespace android {
-
-// Since we use the same APIs across allocator/mapper HALs but they have major
-// version differences (meaning they are not related through inheritance), we
-// create a common interface abstraction for the IAllocator + IMapper combination
-// (major versions need to match in the current HALs, e.g. IAllocator 3.0 needs to
-// be paired with IMapper 3.0, so these are tied together)
-class IGrallocHalWrapper {
- public:
- virtual ~IGrallocHalWrapper() = default;
-
- // IAllocator
- virtual native_handle_t* allocate(uint32_t size) = 0;
- virtual void freeBuffer(native_handle_t* bufferHandle) = 0;
-
- // IMapper
- virtual void* lock(native_handle_t* bufferHandle) = 0;
- virtual void unlock(native_handle_t* bufferHandle) = 0;
-};
-
-namespace {
-
-bool failed(Error2 error) {
- return (error != Error2::NONE);
-}
-bool failed(Error3 error) {
- return (error != Error3::NONE);
-}
-bool failed(Error4 error) {
- return (error != Error4::NONE);
-}
-
-template <typename>
-struct FirstArg;
-
-// Template specialization for pointer to a non-static member function, which exposes
-// the type of the first argument given to said function
-template <typename ReturnType, typename ClassT, typename Arg1, typename... OtherArgs>
-struct FirstArg<ReturnType (ClassT::*)(Arg1, OtherArgs...)> {
- using type = Arg1;
-};
-
-// Alias to FirstArg which also removes any reference type and const associated
-template <typename T>
-using BaseTypeOfFirstArg = typename std::remove_const<
- typename std::remove_reference<typename FirstArg<T>::type>::type>::type;
-
-// 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> typename AllocatorWrapperT = sp>
-class GrallocHalWrapper : public IGrallocHalWrapper {
- public:
- GrallocHalWrapper(const AllocatorWrapperT<AllocatorT>& allocator, const sp<MapperT>& mapper)
- : mAllocator(allocator), mMapper(mapper) {
- if (mapper->isRemote()) {
- ALOGE("Mapper is in passthrough mode");
- }
- }
-
- virtual native_handle_t* allocate(uint32_t size) override;
- virtual void freeBuffer(native_handle_t* bufferHandle) override;
-
- virtual void* lock(native_handle_t* bufferHandle) override;
- virtual void unlock(native_handle_t* bufferHandle) override;
-
- private:
- static constexpr uint64_t kBufferUsage =
- static_cast<uint64_t>(BufferUsage::SENSOR_DIRECT_DATA | BufferUsage::CPU_READ_OFTEN);
- 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
- // some template magic to deduce the right type based off of the first argument to allocate(),
- // which is always the version-specific BufferDescriptor type
- typedef BaseTypeOfFirstArg<decltype(&AllocatorT::allocate)> BufferDescriptorT;
-
- BufferDescriptorT getDescriptor(uint32_t size);
- native_handle_t* importBuffer(const hidl_handle& rawHandle);
-};
-
-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;
-
- auto callback = [&](auto error, uint32_t /*stride*/, const hidl_vec<hidl_handle>& buffers) {
- if (failed(error)) {
- ALOGE("Failed to allocate buffer: %" PRId32, static_cast<int32_t>(error));
- } else if (buffers.size() != kBufferCount) {
- ALOGE("Invalid buffer array size (got %zu, expected %" PRIu32 ")", buffers.size(),
- kBufferCount);
- } else {
- bufferHandle = importBuffer(buffers[0]);
- }
- };
-
- mAllocator->allocate(descriptor, kBufferCount, callback);
- return 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, 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,
- .layerCount = 1,
- .format = static_cast<decltype(descriptorInfo.format)>(PixelFormat::BLOB),
- .usage = kBufferUsage,
- };
-
- BufferDescriptorT descriptor;
- auto callback = [&](auto error, const BufferDescriptorT& tmpDescriptor) {
- if (failed(error)) {
- ALOGE("Failed to create descriptor: %" PRId32, static_cast<int32_t>(error));
- } else {
- descriptor = tmpDescriptor;
- }
- };
-
- mMapper->createDescriptor(descriptorInfo, callback);
- return descriptor;
-}
-
-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;
-
- mMapper->importBuffer(rawHandle, [&](auto error, void* tmpBuffer) {
- if (failed(error)) {
- ALOGE("Failed to import buffer %p: %" PRId32, rawHandle.getNativeHandle(),
- static_cast<int32_t>(error));
- } else {
- bufferHandle = static_cast<native_handle_t*>(tmpBuffer);
- }
- });
-
- return 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
-
- void* data = nullptr;
- mMapper->lock(bufferHandle, kBufferUsage, accessRegion, acquireFenceHandle,
- [&](auto error, void* tmpData, ...) { // V3/4 pass extra args we don't use
- if (failed(error)) {
- ALOGE("Failed to lock buffer %p: %" PRId32, bufferHandle,
- static_cast<int32_t>(error));
- } else {
- data = tmpData;
- }
- });
-
- return data;
-}
-
-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,
- static_cast<int32_t>(error));
- }
- });
-}
-
-} // anonymous namespace
-
-GrallocWrapper::GrallocWrapper() {
- sp<IAllocator4> allocator4 = IAllocator4::getService();
- sp<IMapper4> mapper4 = IMapper4::getService();
-
- 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 {
- ALOGD("Graphics HALs 4.0 not found (allocator %d mapper %d), falling back to 3.0",
- (allocator4 != nullptr), (mapper4 != nullptr));
-
- sp<IAllocator3> allocator3 = IAllocator3::getService();
- sp<IMapper3> mapper3 = IMapper3::getService();
-
- if (allocator3 != nullptr && mapper3 != nullptr) {
- mGrallocHal = std::unique_ptr<IGrallocHalWrapper>(
- new GrallocHalWrapper<IAllocator3, IMapper3>(allocator3, mapper3));
- } else {
- ALOGD("Graphics HALs 3.0 not found (allocator %d mapper %d), falling back to 2.x",
- (allocator3 != nullptr), (mapper3 != nullptr));
-
- sp<IAllocator2> allocator2 = IAllocator2::getService();
- sp<IMapper2> mapper2 = IMapper2_1::getService();
- if (mapper2 == nullptr) {
- mapper2 = IMapper2::getService();
- }
-
- if (allocator2 != nullptr && mapper2 != nullptr) {
- mGrallocHal = std::unique_ptr<IGrallocHalWrapper>(
- new GrallocHalWrapper<IAllocator2, IMapper2>(allocator2, mapper2));
- } else {
- ALOGE("Couldn't open graphics HALs (2.x allocator %d mapper %d)",
- (allocator2 != nullptr), (mapper2 != nullptr));
- }
- }
- }
-}
-
-GrallocWrapper::~GrallocWrapper() {
- for (auto bufferHandle : mAllocatedBuffers) {
- mGrallocHal->unlock(bufferHandle);
- mGrallocHal->freeBuffer(bufferHandle);
- }
- mAllocatedBuffers.clear();
-}
-
-std::pair<native_handle_t*, void*> GrallocWrapper::allocate(uint32_t size) {
- native_handle_t* bufferHandle = mGrallocHal->allocate(size);
- void* buffer = nullptr;
- if (bufferHandle) {
- buffer = mGrallocHal->lock(bufferHandle);
- if (buffer) {
- mAllocatedBuffers.insert(bufferHandle);
- } else {
- mGrallocHal->freeBuffer(bufferHandle);
- bufferHandle = nullptr;
- }
- }
- return std::make_pair<>(bufferHandle, buffer);
-}
-
-void GrallocWrapper::freeBuffer(native_handle_t* bufferHandle) {
- if (mAllocatedBuffers.erase(bufferHandle)) {
- mGrallocHal->unlock(bufferHandle);
- mGrallocHal->freeBuffer(bufferHandle);
- }
-}
-
-} // namespace android
diff --git a/sensors/common/vts/utils/include/sensors-vts-utils/GrallocWrapper.h b/sensors/common/vts/utils/include/sensors-vts-utils/GrallocWrapper.h
deleted file mode 100644
index ebbcb2c..0000000
--- a/sensors/common/vts/utils/include/sensors-vts-utils/GrallocWrapper.h
+++ /dev/null
@@ -1,58 +0,0 @@
-/*
- * Copyright (C) 2017 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#pragma once
-
-#include <utils/NativeHandle.h>
-
-#include <memory>
-#include <string>
-#include <unordered_set>
-#include <utility>
-
-namespace android {
-
-class IGrallocHalWrapper;
-
-// Reference: hardware/interfaces/graphics/mapper/2.0/vts/functional/
-class GrallocWrapper {
- public:
- GrallocWrapper();
- ~GrallocWrapper();
-
- // After constructing this object, this function must be called to check the result. If it
- // returns false, other methods are not safe to call.
- bool isInitialized() const { return (mGrallocHal != nullptr); };
-
- // Allocates a gralloc buffer suitable for direct channel sensors usage with the given size.
- // The buffer should be freed using freeBuffer when it's not needed anymore; otherwise it'll
- // be freed when this object is destroyed.
- // Returns a handle to the buffer, and a CPU-accessible pointer for reading. On failure, both
- // will be set to nullptr.
- std::pair<native_handle_t*, void*> allocate(uint32_t size);
-
- // Releases a gralloc buffer previously returned by allocate()
- void freeBuffer(native_handle_t* bufferHandle);
-
- private:
- std::unique_ptr<IGrallocHalWrapper> mGrallocHal;
-
- // Keep track of all cloned and imported handles. When a test fails with
- // ASSERT_*, the destructor will free the handles for the test.
- std::unordered_set<native_handle_t*> mAllocatedBuffers;
-};
-
-} // namespace android
diff --git a/sensors/common/vts/utils/include/sensors-vts-utils/SensorsTestSharedMemory.h b/sensors/common/vts/utils/include/sensors-vts-utils/SensorsTestSharedMemory.h
index 39084a4..b96adb3 100644
--- a/sensors/common/vts/utils/include/sensors-vts-utils/SensorsTestSharedMemory.h
+++ b/sensors/common/vts/utils/include/sensors-vts-utils/SensorsTestSharedMemory.h
@@ -17,7 +17,11 @@
#ifndef ANDROID_SENSORS_TEST_SHARED_MEMORY_H
#define ANDROID_SENSORS_TEST_SHARED_MEMORY_H
-#include "GrallocWrapper.h"
+#include <aidl/android/hardware/graphics/common/BufferUsage.h>
+#include <aidl/android/hardware/graphics/common/PixelFormat.h>
+#include <ui/GraphicBuffer.h>
+#include <ui/GraphicBufferAllocator.h>
+#include <ui/GraphicBufferMapper.h>
#include <android-base/macros.h>
#include <log/log.h>
@@ -28,6 +32,8 @@
#include <cutils/ashmem.h>
using namespace ::android::hardware::sensors::V1_0;
+using ::aidl::android::hardware::graphics::common::BufferUsage;
+using ::aidl::android::hardware::graphics::common::PixelFormat;
template <class SensorTypeVersion, class EventType>
class SensorsTestSharedMemory {
@@ -48,11 +54,20 @@
}
SharedMemInfo getSharedMemInfo() const {
- SharedMemInfo mem = {.type = mType,
- .format = SharedMemFormat::SENSORS_EVENT,
- .size = static_cast<uint32_t>(mSize),
- .memoryHandle = mNativeHandle};
- return mem;
+ if (mType == SharedMemType::GRALLOC) {
+ SharedMemInfo mem = {.type = mType,
+ .format = SharedMemFormat::SENSORS_EVENT,
+ .size = static_cast<uint32_t>(mSize),
+ .memoryHandle = mBufferHandle};
+ return mem;
+
+ } else {
+ SharedMemInfo mem = {.type = mType,
+ .format = SharedMemFormat::SENSORS_EVENT,
+ .size = static_cast<uint32_t>(mSize),
+ .memoryHandle = mNativeHandle};
+ return mem;
+ }
}
char* getBuffer() const { return mBuffer; }
size_t getSize() const { return mSize; }
@@ -128,17 +143,26 @@
}
case SharedMemType::GRALLOC: {
if (mSize != 0) {
- mGrallocWrapper->freeBuffer(mNativeHandle);
- mNativeHandle = nullptr;
+ android::status_t status =
+ android::GraphicBufferAllocator::get().free(mBufferHandle);
+ if (status != android::OK) {
+ ALOGE("SensorsAidlTestSharedMemory Gralloc failed to free buffer. Status: "
+ "%s",
+ android::statusToString(status).c_str());
+ }
+ mBufferHandle = nullptr;
+ mBuffer = nullptr;
mSize = 0;
}
break;
}
default: {
- if (mNativeHandle != nullptr || mSize != 0 || mBuffer != nullptr) {
- ALOGE("SensorsTestSharedMemory %p not properly destructed: "
- "type %d, native handle %p, size %zu, buffer %p",
- this, static_cast<int>(mType), mNativeHandle, mSize, mBuffer);
+ if (mNativeHandle != nullptr || mSize != 0 || mBuffer != nullptr ||
+ mBufferHandle != nullptr) {
+ ALOGE("SensorsAidlTestSharedMemory %p not properly destructed: "
+ "type %d, native handle %p, size %zu, buffer %p, buffer handle %p",
+ this, static_cast<int>(mType), mNativeHandle, mSize, mBuffer,
+ mBufferHandle);
}
break;
}
@@ -171,14 +195,33 @@
break;
}
case SharedMemType::GRALLOC: {
- mGrallocWrapper = std::make_unique<::android::GrallocWrapper>();
- if (!mGrallocWrapper->isInitialized()) {
+ static constexpr uint64_t kBufferUsage =
+ static_cast<uint64_t>(BufferUsage::SENSOR_DIRECT_DATA) |
+ static_cast<uint64_t>(BufferUsage::CPU_READ_OFTEN) |
+ static_cast<uint64_t>(BufferUsage::CPU_WRITE_RARELY);
+
+ uint32_t stride = 0;
+ buffer_handle_t bufferHandle;
+ android::status_t status = android::GraphicBufferAllocator::get().allocate(
+ size, 1, static_cast<int>(PixelFormat::BLOB), 1, kBufferUsage,
+ &bufferHandle, &stride, "SensorVts");
+ if (status != android::OK) {
+ ALOGE("SensorsAidlTestSharedMemory failed to allocate memory. Status: %s",
+ android::statusToString(status).c_str());
break;
}
-
- std::pair<native_handle_t*, void*> buf = mGrallocWrapper->allocate(size);
- handle = buf.first;
- buffer = static_cast<char*>(buf.second);
+ // Per the HAL, all-zeros Rect means the entire buffer
+ android::Rect rect = {0, 0, 0, 0};
+ void* ret;
+ status = android::GraphicBufferMapper::get().lock(bufferHandle, kBufferUsage, rect,
+ &ret);
+ if (status != android::OK) {
+ ALOGE("SensorsAidlTestSharedMemory failed to import buffer: Status: %s",
+ android::statusToString(status).c_str());
+ } else {
+ buffer = static_cast<char*>(ret);
+ mBufferHandle = bufferHandle;
+ }
break;
}
default:
@@ -194,9 +237,9 @@
SharedMemType mType;
native_handle_t* mNativeHandle;
+ buffer_handle_t mBufferHandle;
size_t mSize;
char* mBuffer;
- std::unique_ptr<::android::GrallocWrapper> mGrallocWrapper;
DISALLOW_COPY_AND_ASSIGN(SensorsTestSharedMemory);
};