blob: 285c515c20477b947c98f5bd465a90aaa90fe2bb [file] [log] [blame]
Michael Butler3670c382020-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 "Callbacks.h"
20#include "Conversions.h"
21#include "Utils.h"
22
23#include <android/hardware/neuralnetworks/1.0/IDevice.h>
24#include <android/hardware/neuralnetworks/1.0/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/CommonUtils.h>
32#include <nnapi/hal/HandleError.h>
33#include <nnapi/hal/ProtectCallback.h>
34
35#include <functional>
36#include <memory>
37#include <optional>
38#include <string>
39#include <vector>
40
41namespace android::hardware::neuralnetworks::V1_0::utils {
42namespace {
43
44nn::GeneralResult<nn::Capabilities> initCapabilities(V1_0::IDevice* device) {
45 CHECK(device != nullptr);
46
47 nn::GeneralResult<nn::Capabilities> result = NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE)
48 << "uninitialized";
49 const auto cb = [&result](ErrorStatus status, const Capabilities& capabilities) {
50 if (status != ErrorStatus::NONE) {
Michael Butler32acc062020-11-22 19:36:30 -080051 const auto canonical = nn::convert(status).value_or(nn::ErrorStatus::GENERAL_FAILURE);
Michael Butler3670c382020-08-06 23:22:35 -070052 result = NN_ERROR(canonical) << "getCapabilities failed with " << toString(status);
53 } else {
Michael Butler32acc062020-11-22 19:36:30 -080054 result = nn::convert(capabilities);
Michael Butler3670c382020-08-06 23:22:35 -070055 }
56 };
57
58 const auto ret = device->getCapabilities(cb);
Michael Butler61f508e2020-11-22 20:25:34 -080059 HANDLE_TRANSPORT_FAILURE(ret);
Michael Butler3670c382020-08-06 23:22:35 -070060
61 return result;
62}
63
64} // namespace
65
66nn::GeneralResult<std::shared_ptr<const Device>> Device::create(std::string name,
67 sp<V1_0::IDevice> device) {
68 if (name.empty()) {
69 return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT)
70 << "V1_0::utils::Device::create must have non-empty name";
71 }
72 if (device == nullptr) {
73 return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT)
74 << "V1_0::utils::Device::create must have non-null device";
75 }
76
77 auto capabilities = NN_TRY(initCapabilities(device.get()));
78
79 auto deathHandler = NN_TRY(hal::utils::DeathHandler::create(device));
80 return std::make_shared<const Device>(PrivateConstructorTag{}, std::move(name),
81 std::move(capabilities), std::move(device),
82 std::move(deathHandler));
83}
84
85Device::Device(PrivateConstructorTag /*tag*/, std::string name, nn::Capabilities capabilities,
86 sp<V1_0::IDevice> device, hal::utils::DeathHandler deathHandler)
87 : kName(std::move(name)),
88 kCapabilities(std::move(capabilities)),
89 kDevice(std::move(device)),
90 kDeathHandler(std::move(deathHandler)) {}
91
92const std::string& Device::getName() const {
93 return kName;
94}
95
96const std::string& Device::getVersionString() const {
97 return kVersionString;
98}
99
100nn::Version Device::getFeatureLevel() const {
101 return nn::Version::ANDROID_OC_MR1;
102}
103
104nn::DeviceType Device::getType() const {
105 return nn::DeviceType::OTHER;
106}
107
108const std::vector<nn::Extension>& Device::getSupportedExtensions() const {
109 return kExtensions;
110}
111
112const nn::Capabilities& Device::getCapabilities() const {
113 return kCapabilities;
114}
115
116std::pair<uint32_t, uint32_t> Device::getNumberOfCacheFilesNeeded() const {
117 return std::make_pair(/*numModelCache=*/0, /*numDataCache=*/0);
118}
119
120nn::GeneralResult<void> Device::wait() const {
121 const auto ret = kDevice->ping();
Michael Butler61f508e2020-11-22 20:25:34 -0800122 HANDLE_TRANSPORT_FAILURE(ret);
123 return {};
Michael Butler3670c382020-08-06 23:22:35 -0700124}
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](ErrorStatus status, const hidl_vec<bool>& supportedOperations) {
137 if (status != ErrorStatus::NONE) {
Michael Butler32acc062020-11-22 19:36:30 -0800138 const auto canonical = nn::convert(status).value_or(nn::ErrorStatus::GENERAL_FAILURE);
Michael Butler3670c382020-08-06 23:22:35 -0700139 result = NN_ERROR(canonical)
140 << "getSupportedOperations failed with " << toString(status);
141 } else if (supportedOperations.size() != model.main.operations.size()) {
142 result = NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE)
143 << "getSupportedOperations returned vector of size "
144 << supportedOperations.size() << " but expected "
145 << model.main.operations.size();
146 } else {
147 result = supportedOperations;
148 }
149 };
150
151 const auto ret = kDevice->getSupportedOperations(hidlModel, cb);
Michael Butler61f508e2020-11-22 20:25:34 -0800152 HANDLE_TRANSPORT_FAILURE(ret);
Michael Butler3670c382020-08-06 23:22:35 -0700153
154 return result;
155}
156
157nn::GeneralResult<nn::SharedPreparedModel> Device::prepareModel(
158 const nn::Model& model, nn::ExecutionPreference /*preference*/, nn::Priority /*priority*/,
Slava Shklyaevd4290b82020-10-27 18:44:01 +0000159 nn::OptionalTimePoint /*deadline*/, const std::vector<nn::SharedHandle>& /*modelCache*/,
160 const std::vector<nn::SharedHandle>& /*dataCache*/, const nn::CacheToken& /*token*/) const {
Michael Butler3670c382020-08-06 23:22:35 -0700161 // Ensure that model is ready for IPC.
162 std::optional<nn::Model> maybeModelInShared;
163 const nn::Model& modelInShared =
164 NN_TRY(hal::utils::flushDataFromPointerToShared(&model, &maybeModelInShared));
165
166 const auto hidlModel = NN_TRY(convert(modelInShared));
167
168 const auto cb = sp<PreparedModelCallback>::make();
169 const auto scoped = kDeathHandler.protectCallback(cb.get());
170
171 const auto ret = kDevice->prepareModel(hidlModel, cb);
Michael Butler61f508e2020-11-22 20:25:34 -0800172 const auto status = HANDLE_TRANSPORT_FAILURE(ret);
Michael Butler3670c382020-08-06 23:22:35 -0700173 if (status != ErrorStatus::NONE) {
Michael Butler32acc062020-11-22 19:36:30 -0800174 const auto canonical = nn::convert(status).value_or(nn::ErrorStatus::GENERAL_FAILURE);
Michael Butler3670c382020-08-06 23:22:35 -0700175 return NN_ERROR(canonical) << "prepareModel failed with " << toString(status);
176 }
177
178 return cb->get();
179}
180
181nn::GeneralResult<nn::SharedPreparedModel> Device::prepareModelFromCache(
Slava Shklyaevd4290b82020-10-27 18:44:01 +0000182 nn::OptionalTimePoint /*deadline*/, const std::vector<nn::SharedHandle>& /*modelCache*/,
183 const std::vector<nn::SharedHandle>& /*dataCache*/, const nn::CacheToken& /*token*/) const {
Michael Butler3670c382020-08-06 23:22:35 -0700184 return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE)
185 << "IDevice::prepareModelFromCache not supported on 1.0 HAL service";
186}
187
188nn::GeneralResult<nn::SharedBuffer> Device::allocate(
189 const nn::BufferDesc& /*desc*/,
190 const std::vector<nn::SharedPreparedModel>& /*preparedModels*/,
191 const std::vector<nn::BufferRole>& /*inputRoles*/,
192 const std::vector<nn::BufferRole>& /*outputRoles*/) const {
193 return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE)
194 << "IDevice::allocate not supported on 1.0 HAL service";
195}
196
197} // namespace android::hardware::neuralnetworks::V1_0::utils