blob: f73d3f8253459317e7dd43d8763e7de019a95ec0 [file] [log] [blame]
Michael Butler4b276a72020-08-06 23:22:35 -07001/*
2 * Copyright (C) 2020 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#include "Device.h"
18
19#include "Conversions.h"
20#include "Utils.h"
21
22#include <android/hardware/neuralnetworks/1.0/types.h>
23#include <android/hardware/neuralnetworks/1.1/IDevice.h>
24#include <android/hardware/neuralnetworks/1.1/types.h>
25#include <nnapi/IBuffer.h>
26#include <nnapi/IDevice.h>
27#include <nnapi/IPreparedModel.h>
28#include <nnapi/OperandTypes.h>
29#include <nnapi/Result.h>
30#include <nnapi/Types.h>
31#include <nnapi/hal/1.0/Callbacks.h>
32#include <nnapi/hal/CommonUtils.h>
33#include <nnapi/hal/HandleError.h>
34#include <nnapi/hal/ProtectCallback.h>
35
36#include <functional>
37#include <memory>
38#include <optional>
39#include <string>
40#include <vector>
41
42namespace android::hardware::neuralnetworks::V1_1::utils {
43namespace {
44
45nn::GeneralResult<nn::Capabilities> initCapabilities(V1_1::IDevice* device) {
46 CHECK(device != nullptr);
47
48 nn::GeneralResult<nn::Capabilities> result = NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE)
49 << "uninitialized";
50 const auto cb = [&result](V1_0::ErrorStatus status, const Capabilities& capabilities) {
51 if (status != V1_0::ErrorStatus::NONE) {
Michael Butler6547b2a2020-11-22 19:36:30 -080052 const auto canonical = nn::convert(status).value_or(nn::ErrorStatus::GENERAL_FAILURE);
Michael Butler4b276a72020-08-06 23:22:35 -070053 result = NN_ERROR(canonical) << "getCapabilities_1_1 failed with " << toString(status);
54 } else {
Michael Butler6547b2a2020-11-22 19:36:30 -080055 result = nn::convert(capabilities);
Michael Butler4b276a72020-08-06 23:22:35 -070056 }
57 };
58
59 const auto ret = device->getCapabilities_1_1(cb);
Michael Butlercca3e202020-11-22 20:25:34 -080060 HANDLE_TRANSPORT_FAILURE(ret);
Michael Butler4b276a72020-08-06 23:22:35 -070061
62 return result;
63}
64
65} // namespace
66
67nn::GeneralResult<std::shared_ptr<const Device>> Device::create(std::string name,
68 sp<V1_1::IDevice> device) {
69 if (name.empty()) {
70 return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT)
71 << "V1_1::utils::Device::create must have non-empty name";
72 }
73 if (device == nullptr) {
74 return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT)
75 << "V1_1::utils::Device::create must have non-null device";
76 }
77
78 auto capabilities = NN_TRY(initCapabilities(device.get()));
79
80 auto deathHandler = NN_TRY(hal::utils::DeathHandler::create(device));
81 return std::make_shared<const Device>(PrivateConstructorTag{}, std::move(name),
82 std::move(capabilities), std::move(device),
83 std::move(deathHandler));
84}
85
86Device::Device(PrivateConstructorTag /*tag*/, std::string name, nn::Capabilities capabilities,
87 sp<V1_1::IDevice> device, hal::utils::DeathHandler deathHandler)
88 : kName(std::move(name)),
89 kCapabilities(std::move(capabilities)),
90 kDevice(std::move(device)),
91 kDeathHandler(std::move(deathHandler)) {}
92
93const std::string& Device::getName() const {
94 return kName;
95}
96
97const std::string& Device::getVersionString() const {
98 return kVersionString;
99}
100
101nn::Version Device::getFeatureLevel() const {
102 return nn::Version::ANDROID_P;
103}
104
105nn::DeviceType Device::getType() const {
106 return nn::DeviceType::UNKNOWN;
107}
108
109const std::vector<nn::Extension>& Device::getSupportedExtensions() const {
110 return kExtensions;
111}
112
113const nn::Capabilities& Device::getCapabilities() const {
114 return kCapabilities;
115}
116
117std::pair<uint32_t, uint32_t> Device::getNumberOfCacheFilesNeeded() const {
118 return std::make_pair(/*numModelCache=*/0, /*numDataCache=*/0);
119}
120
121nn::GeneralResult<void> Device::wait() const {
122 const auto ret = kDevice->ping();
Michael Butlercca3e202020-11-22 20:25:34 -0800123 HANDLE_TRANSPORT_FAILURE(ret);
124 return {};
Michael Butler4b276a72020-08-06 23:22:35 -0700125}
126
127nn::GeneralResult<std::vector<bool>> Device::getSupportedOperations(const nn::Model& model) const {
128 // Ensure that model is ready for IPC.
129 std::optional<nn::Model> maybeModelInShared;
130 const nn::Model& modelInShared =
131 NN_TRY(hal::utils::flushDataFromPointerToShared(&model, &maybeModelInShared));
132
133 const auto hidlModel = NN_TRY(convert(modelInShared));
134
135 nn::GeneralResult<std::vector<bool>> result = NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE)
136 << "uninitialized";
137 auto cb = [&result, &model](V1_0::ErrorStatus status,
138 const hidl_vec<bool>& supportedOperations) {
139 if (status != V1_0::ErrorStatus::NONE) {
Michael Butler6547b2a2020-11-22 19:36:30 -0800140 const auto canonical = nn::convert(status).value_or(nn::ErrorStatus::GENERAL_FAILURE);
Michael Butler4b276a72020-08-06 23:22:35 -0700141 result = NN_ERROR(canonical)
142 << "getSupportedOperations_1_1 failed with " << toString(status);
143 } else if (supportedOperations.size() != model.main.operations.size()) {
144 result = NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE)
145 << "getSupportedOperations_1_1 returned vector of size "
146 << supportedOperations.size() << " but expected "
147 << model.main.operations.size();
148 } else {
149 result = supportedOperations;
150 }
151 };
152
153 const auto ret = kDevice->getSupportedOperations_1_1(hidlModel, cb);
Michael Butlercca3e202020-11-22 20:25:34 -0800154 HANDLE_TRANSPORT_FAILURE(ret);
Michael Butler4b276a72020-08-06 23:22:35 -0700155
156 return result;
157}
158
159nn::GeneralResult<nn::SharedPreparedModel> Device::prepareModel(
160 const nn::Model& model, nn::ExecutionPreference preference, nn::Priority /*priority*/,
Slava Shklyaev49817a02020-10-27 18:44:01 +0000161 nn::OptionalTimePoint /*deadline*/, const std::vector<nn::SharedHandle>& /*modelCache*/,
162 const std::vector<nn::SharedHandle>& /*dataCache*/, const nn::CacheToken& /*token*/) const {
Michael Butler4b276a72020-08-06 23:22:35 -0700163 // Ensure that model is ready for IPC.
164 std::optional<nn::Model> maybeModelInShared;
165 const nn::Model& modelInShared =
166 NN_TRY(hal::utils::flushDataFromPointerToShared(&model, &maybeModelInShared));
167
168 const auto hidlModel = NN_TRY(convert(modelInShared));
169 const auto hidlPreference = NN_TRY(convert(preference));
170
171 const auto cb = sp<V1_0::utils::PreparedModelCallback>::make();
172 const auto scoped = kDeathHandler.protectCallback(cb.get());
173
174 const auto ret = kDevice->prepareModel_1_1(hidlModel, hidlPreference, cb);
Michael Butlercca3e202020-11-22 20:25:34 -0800175 const auto status = HANDLE_TRANSPORT_FAILURE(ret);
Michael Butler4b276a72020-08-06 23:22:35 -0700176 if (status != V1_0::ErrorStatus::NONE) {
Michael Butler6547b2a2020-11-22 19:36:30 -0800177 const auto canonical = nn::convert(status).value_or(nn::ErrorStatus::GENERAL_FAILURE);
Michael Butler4b276a72020-08-06 23:22:35 -0700178 return NN_ERROR(canonical) << "prepareModel failed with " << toString(status);
179 }
180
181 return cb->get();
182}
183
184nn::GeneralResult<nn::SharedPreparedModel> Device::prepareModelFromCache(
Slava Shklyaev49817a02020-10-27 18:44:01 +0000185 nn::OptionalTimePoint /*deadline*/, const std::vector<nn::SharedHandle>& /*modelCache*/,
186 const std::vector<nn::SharedHandle>& /*dataCache*/, const nn::CacheToken& /*token*/) const {
Michael Butler4b276a72020-08-06 23:22:35 -0700187 return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE)
188 << "IDevice::prepareModelFromCache not supported on 1.1 HAL service";
189}
190
191nn::GeneralResult<nn::SharedBuffer> Device::allocate(
192 const nn::BufferDesc& /*desc*/,
193 const std::vector<nn::SharedPreparedModel>& /*preparedModels*/,
194 const std::vector<nn::BufferRole>& /*inputRoles*/,
195 const std::vector<nn::BufferRole>& /*outputRoles*/) const {
196 return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE)
197 << "IDevice::allocate not supported on 1.1 HAL service";
198}
199
200} // namespace android::hardware::neuralnetworks::V1_1::utils