Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 02b1d722 authored by Devin Moore's avatar Devin Moore Committed by Brian Duddie
Browse files

Migrate sensors VTS to use libui

There is a new graphics.mapper HAL and instead of adding yet another
combination of mapper/allocator, use libui that handles all of that for
us.

Backport requested in b/381205010.

Test: VtsAidlHalSensorsTargetTest
Bug: 304843606
Fixes: 381205010
(cherry picked from https://googleplex-android-review.googlesource.com/q/commit:9d67c77d35a86fc2b38e563400d1161c30145027)
Merged-In: Icf4a218de6afe0ae77a4520a6f78a4f7da9bedd4
Change-Id: Icf4a218de6afe0ae77a4520a6f78a4f7da9bedd4
parent 73a53868
Loading
Loading
Loading
Loading
+63 −19
Original line number Diff line number Diff line
@@ -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,6 +59,15 @@ class SensorsAidlTestSharedMemory {
    }

    ISensors::SharedMemInfo getSharedMemInfo() const {
        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,
@@ -60,6 +75,7 @@ class SensorsAidlTestSharedMemory {
                    .memoryHandle = android::dupToAidl(mNativeHandle)};
            return mem;
        }
    }
    char* getBuffer() const { return mBuffer; }
    size_t getSize() const { return mSize; }
    std::vector<Event> parseEvents(int64_t lastCounter = -1, size_t offset = 0) const {
@@ -141,17 +157,26 @@ class SensorsAidlTestSharedMemory {
            }
            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 @@ class SensorsAidlTestSharedMemory {
                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 @@ class SensorsAidlTestSharedMemory {

    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);
};
+15 −14
Original line number Diff line number Diff line
@@ -52,8 +52,6 @@ using ::android::hardware::sensors::V1_0::SharedMemType;
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 @@ class EventCallback : public IEventCallback<EventType> {
    }

    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 @@ class EventCallback : public IEventCallback<EventType> {
        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 @@ TEST_P(SensorsHidlTest, InjectSensorEventData) {
    }

    // 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 @@ void SensorsHidlTest::runFlushTest(const std::vector<SensorInfoType>& sensors, b
        }

        // 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, Activate) {
}

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 @@ TEST_P(SensorsHidlTest, NoStaleEvents) {

    // 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 @@ TEST_P(SensorsHidlTest, NoStaleEvents) {
    }

    // 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 @@ TEST_P(SensorsHidlTest, NoStaleEvents) {
        // 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));
    }
}
+2 −12
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@ cc_defaults {
        "libbinder_ndk",
        "libutils",
        "libvndksupport",
        "libui",
    ],
    static_libs: [
        "libaidlcommonsupport",
@@ -50,9 +51,6 @@ cc_library_static {
        "android.hardware.graphics.common-ndk_shared",
    ],
    cflags: ["-DLOG_TAG=\"sensors_hidl_hal_test\""],
    srcs: [
        "GrallocWrapper.cpp",
    ],
    export_include_dirs: [
        "include",
    ],
@@ -64,6 +62,7 @@ cc_library_static {
        "libbinder_ndk",
        "libutils",
        "libvndksupport",
        "libui",
    ],
    static_libs: [
        "android.hardware.sensors@1.0",
@@ -71,13 +70,4 @@ cc_library_static {
        "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",
    ],
}
+0 −354

File deleted.

Preview size limit exceeded, changes collapsed.

+0 −58
Original line number Diff line number Diff line
/*
 * 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
Loading