blob: ccb0778ac59e84f8cf87bf88add7376382e7274d [file] [log] [blame]
Lev Proleevc185e882020-12-15 19:25:32 +00001/*
2 * Copyright (C) 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ANDROID_HARDWARE_NEURALNETWORKS_AIDL_UTILS_H
18#define ANDROID_HARDWARE_NEURALNETWORKS_AIDL_UTILS_H
19
20#include <android-base/logging.h>
Lev Proleevc185e882020-12-15 19:25:32 +000021#include <gtest/gtest.h>
22
23#include <algorithm>
Xusong Wang8805b2d2022-01-21 10:15:19 -080024#include <array>
Lev Proleevc185e882020-12-15 19:25:32 +000025#include <iosfwd>
26#include <string>
27#include <utility>
28#include <vector>
29
30#include <aidl/android/hardware/neuralnetworks/IDevice.h>
31#include <aidl/android/hardware/neuralnetworks/Memory.h>
32#include <aidl/android/hardware/neuralnetworks/Operand.h>
33#include <aidl/android/hardware/neuralnetworks/OperandType.h>
34#include <aidl/android/hardware/neuralnetworks/Priority.h>
35#include <aidl/android/hardware/neuralnetworks/Request.h>
36
37#include <TestHarness.h>
38#include <nnapi/SharedMemory.h>
39
40namespace aidl::android::hardware::neuralnetworks {
41
42namespace nn = ::android::nn;
43
44inline constexpr Priority kDefaultPriority = Priority::MEDIUM;
45
Lev Proleev8df7d6e2021-04-14 20:54:27 +010046inline constexpr Timing kNoTiming = {.timeOnDeviceNs = -1, .timeInDriverNs = -1};
Lev Proleevc185e882020-12-15 19:25:32 +000047inline constexpr int64_t kNoDeadline = -1;
48inline constexpr int64_t kOmittedTimeoutDuration = -1;
49inline constexpr int64_t kNoDuration = -1;
50inline const std::vector<uint8_t> kEmptyCacheToken(IDevice::BYTE_SIZE_OF_CACHE_TOKEN);
Xusong Wang8805b2d2022-01-21 10:15:19 -080051inline const std::array<uint8_t, IDevice::BYTE_SIZE_OF_CACHE_TOKEN> kEmptyCacheTokenArray{};
Lev Proleevc185e882020-12-15 19:25:32 +000052
53// Returns the amount of space needed to store a value of the specified type.
54//
55// Aborts if the specified type is an extension type or OEM type.
56uint32_t sizeOfData(OperandType type);
57
58// Returns the amount of space needed to store a value of the dimensions and
59// type of this operand. For a non-extension, non-OEM tensor with unspecified
60// rank or at least one unspecified dimension, returns zero.
61//
62// Aborts if the specified type is an extension type or OEM type.
63uint32_t sizeOfData(const Operand& operand);
64
65// Convenience class to manage the lifetime of memory resources.
66class TestMemoryBase {
67 DISALLOW_COPY_AND_ASSIGN(TestMemoryBase);
68
69 public:
70 TestMemoryBase() = default;
71 virtual ~TestMemoryBase() = default;
72 uint8_t* getPointer() const { return mPtr; }
73 const Memory* getAidlMemory() const { return &mAidlMemory; }
74
75 protected:
76 uint8_t* mPtr = nullptr;
77 Memory mAidlMemory;
78 bool mIsValid = false;
79};
80
81class TestAshmem : public TestMemoryBase {
82 public:
Xusong Wang378a9382021-05-21 14:58:40 -070083 // If aidlReadonly is true, getAidlMemory will return a sAIDL memory with readonly access;
84 // otherwise, the sAIDL memory has read-write access. This only affects the sAIDL memory.
85 // getPointer will always return a valid address with read-write access.
86 static std::unique_ptr<TestAshmem> create(uint32_t size, bool aidlReadonly = false);
Lev Proleevc185e882020-12-15 19:25:32 +000087
88 // Prefer TestAshmem::create.
89 // The constructor calls initialize, which constructs the memory resources. This is a workaround
90 // that gtest macros cannot be used directly in a constructor.
Xusong Wang378a9382021-05-21 14:58:40 -070091 TestAshmem(uint32_t size, bool aidlReadonly) { initialize(size, aidlReadonly); }
Lev Proleevc185e882020-12-15 19:25:32 +000092
93 private:
Xusong Wang378a9382021-05-21 14:58:40 -070094 void initialize(uint32_t size, bool aidlReadonly);
Lev Proleevc185e882020-12-15 19:25:32 +000095 nn::Mapping mMappedMemory;
96};
97
98class TestBlobAHWB : public TestMemoryBase {
99 public:
100 static std::unique_ptr<TestBlobAHWB> create(uint32_t size);
101
102 // Prefer TestBlobAHWB::create.
103 // The constructor calls initialize, which constructs the memory resources. This is a
104 // workaround that gtest macros cannot be used directly in a constructor.
105 TestBlobAHWB(uint32_t size) { initialize(size); }
Lev Proleevc185e882020-12-15 19:25:32 +0000106
107 private:
108 void initialize(uint32_t size);
Xusong Wangd2ecde52021-08-23 11:36:19 -0700109 nn::SharedMemory mMemory;
Lev Proleevc185e882020-12-15 19:25:32 +0000110 nn::Mapping mMapping;
111};
112
113enum class MemoryType { ASHMEM, BLOB_AHWB, DEVICE };
114
Xusong Wang72e06c22022-01-11 14:25:55 -0800115std::string toString(MemoryType type);
116
Lev Proleevc185e882020-12-15 19:25:32 +0000117// Manages the lifetime of memory resources used in an execution.
118class ExecutionContext {
119 DISALLOW_COPY_AND_ASSIGN(ExecutionContext);
120
121 public:
122 static constexpr uint32_t kInputPoolIndex = 0;
123 static constexpr uint32_t kOutputPoolIndex = 1;
124
125 ExecutionContext() = default;
126
127 // Create HIDL Request from the TestModel struct.
128 Request createRequest(const test_helper::TestModel& testModel,
129 MemoryType memoryType = MemoryType::ASHMEM);
130
131 // After execution, copy out output results from the output memory pool.
132 std::vector<test_helper::TestBuffer> getOutputBuffers(const Request& request) const;
133
134 private:
135 std::unique_ptr<TestMemoryBase> mInputMemory, mOutputMemory;
136};
137
138template <typename Type>
139using Named = std::pair<std::string, Type>;
140
141template <typename Type>
142const std::string& getName(const Named<Type>& namedData) {
143 return namedData.first;
144}
145
146template <typename Type>
147const Type& getData(const Named<Type>& namedData) {
148 return namedData.second;
149}
150
151std::string gtestCompliantName(std::string name);
152
153// pretty-print values for error messages
154::std::ostream& operator<<(::std::ostream& os, ErrorStatus errorStatus);
155
156} // namespace aidl::android::hardware::neuralnetworks
157
158#endif // ANDROID_HARDWARE_NEURALNETWORKS_AIDL_UTILS_H