blob: e45b17ec6e3cf72283d44db3446f522509b7909f [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);
60 NN_TRY(hal::utils::handleTransportError(ret));
61
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();
123 return hal::utils::handleTransportError(ret);
124}
125
126nn::GeneralResult<std::vector<bool>> Device::getSupportedOperations(const nn::Model& model) const {
127 // Ensure that model is ready for IPC.
128 std::optional<nn::Model> maybeModelInShared;
129 const nn::Model& modelInShared =
130 NN_TRY(hal::utils::flushDataFromPointerToShared(&model, &maybeModelInShared));
131
132 const auto hidlModel = NN_TRY(convert(modelInShared));
133
134 nn::GeneralResult<std::vector<bool>> result = NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE)
135 << "uninitialized";
136 auto cb = [&result, &model](V1_0::ErrorStatus status,
137 const hidl_vec<bool>& supportedOperations) {
138 if (status != V1_0::ErrorStatus::NONE) {
Michael Butler6547b2a2020-11-22 19:36:30 -0800139 const auto canonical = nn::convert(status).value_or(nn::ErrorStatus::GENERAL_FAILURE);
Michael Butler4b276a72020-08-06 23:22:35 -0700140 result = NN_ERROR(canonical)
141 << "getSupportedOperations_1_1 failed with " << toString(status);
142 } else if (supportedOperations.size() != model.main.operations.size()) {
143 result = NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE)
144 << "getSupportedOperations_1_1 returned vector of size "
145 << supportedOperations.size() << " but expected "
146 << model.main.operations.size();
147 } else {
148 result = supportedOperations;
149 }
150 };
151
152 const auto ret = kDevice->getSupportedOperations_1_1(hidlModel, cb);
153 NN_TRY(hal::utils::handleTransportError(ret));
154
155 return result;
156}
157
158nn::GeneralResult<nn::SharedPreparedModel> Device::prepareModel(
159 const nn::Model& model, nn::ExecutionPreference preference, nn::Priority /*priority*/,
Slava Shklyaev49817a02020-10-27 18:44:01 +0000160 nn::OptionalTimePoint /*deadline*/, const std::vector<nn::SharedHandle>& /*modelCache*/,
161 const std::vector<nn::SharedHandle>& /*dataCache*/, const nn::CacheToken& /*token*/) const {
Michael Butler4b276a72020-08-06 23:22:35 -0700162 // Ensure that model is ready for IPC.
163 std::optional<nn::Model> maybeModelInShared;
164 const nn::Model& modelInShared =
165 NN_TRY(hal::utils::flushDataFromPointerToShared(&model, &maybeModelInShared));
166
167 const auto hidlModel = NN_TRY(convert(modelInShared));
168 const auto hidlPreference = NN_TRY(convert(preference));
169
170 const auto cb = sp<V1_0::utils::PreparedModelCallback>::make();
171 const auto scoped = kDeathHandler.protectCallback(cb.get());
172
173 const auto ret = kDevice->prepareModel_1_1(hidlModel, hidlPreference, cb);
174 const auto status = NN_TRY(hal::utils::handleTransportError(ret));
175 if (status != V1_0::ErrorStatus::NONE) {
Michael Butler6547b2a2020-11-22 19:36:30 -0800176 const auto canonical = nn::convert(status).value_or(nn::ErrorStatus::GENERAL_FAILURE);
Michael Butler4b276a72020-08-06 23:22:35 -0700177 return NN_ERROR(canonical) << "prepareModel failed with " << toString(status);
178 }
179
180 return cb->get();
181}
182
183nn::GeneralResult<nn::SharedPreparedModel> Device::prepareModelFromCache(
Slava Shklyaev49817a02020-10-27 18:44:01 +0000184 nn::OptionalTimePoint /*deadline*/, const std::vector<nn::SharedHandle>& /*modelCache*/,
185 const std::vector<nn::SharedHandle>& /*dataCache*/, const nn::CacheToken& /*token*/) const {
Michael Butler4b276a72020-08-06 23:22:35 -0700186 return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE)
187 << "IDevice::prepareModelFromCache not supported on 1.1 HAL service";
188}
189
190nn::GeneralResult<nn::SharedBuffer> Device::allocate(
191 const nn::BufferDesc& /*desc*/,
192 const std::vector<nn::SharedPreparedModel>& /*preparedModels*/,
193 const std::vector<nn::BufferRole>& /*inputRoles*/,
194 const std::vector<nn::BufferRole>& /*outputRoles*/) const {
195 return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE)
196 << "IDevice::allocate not supported on 1.1 HAL service";
197}
198
199} // namespace android::hardware::neuralnetworks::V1_1::utils