blob: 28f3276389dca44f3a20f12311c39b83e6be738f [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>
Michael Butler49d95e02021-10-15 18:52:52 -070032#include <nnapi/hal/1.0/HandleError.h>
Michael Butlere8645c32021-10-15 18:42:32 -070033#include <nnapi/hal/1.0/ProtectCallback.h>
Michael Butler4b276a72020-08-06 23:22:35 -070034#include <nnapi/hal/CommonUtils.h>
Michael Butler4b276a72020-08-06 23:22:35 -070035
36#include <functional>
37#include <memory>
38#include <optional>
39#include <string>
40#include <vector>
41
Michael Butleraad934b2020-12-13 23:06:06 -080042// See hardware/interfaces/neuralnetworks/utils/README.md for more information on HIDL interface
43// lifetimes across processes and for protecting asynchronous calls across HIDL.
44
Michael Butler4b276a72020-08-06 23:22:35 -070045namespace android::hardware::neuralnetworks::V1_1::utils {
46namespace {
47
Michael Butler7fd03c22020-12-06 21:50:59 -080048nn::GeneralResult<nn::Capabilities> capabilitiesCallback(V1_0::ErrorStatus status,
49 const Capabilities& capabilities) {
Michael Butler49d95e02021-10-15 18:52:52 -070050 HANDLE_STATUS_HIDL(status) << "getting capabilities failed with " << toString(status);
Michael Butler7fd03c22020-12-06 21:50:59 -080051 return nn::convert(capabilities);
52}
53
54nn::GeneralResult<nn::Capabilities> getCapabilitiesFrom(V1_1::IDevice* device) {
Michael Butler4b276a72020-08-06 23:22:35 -070055 CHECK(device != nullptr);
56
Michael Butler7fd03c22020-12-06 21:50:59 -080057 auto cb = hal::utils::CallbackValue(capabilitiesCallback);
Michael Butler4b276a72020-08-06 23:22:35 -070058
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
Michael Butler7fd03c22020-12-06 21:50:59 -080062 return cb.take();
Michael Butler4b276a72020-08-06 23:22:35 -070063}
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
Michael Butler7fd03c22020-12-06 21:50:59 -080078 auto capabilities = NN_TRY(getCapabilitiesFrom(device.get()));
Michael Butler4b276a72020-08-06 23:22:35 -070079
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 {
Michael Butler60a7b862021-11-17 16:33:35 -0800102 return kVersion;
Michael Butler4b276a72020-08-06 23:22:35 -0700103}
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
Michael Butler7fd03c22020-12-06 21:50:59 -0800135 auto cb = hal::utils::CallbackValue(V1_0::utils::supportedOperationsCallback);
Michael Butler4b276a72020-08-06 23:22:35 -0700136
137 const auto ret = kDevice->getSupportedOperations_1_1(hidlModel, cb);
Michael Butlercca3e202020-11-22 20:25:34 -0800138 HANDLE_TRANSPORT_FAILURE(ret);
Michael Butler4b276a72020-08-06 23:22:35 -0700139
Michael Butler7fd03c22020-12-06 21:50:59 -0800140 return cb.take();
Michael Butler4b276a72020-08-06 23:22:35 -0700141}
142
143nn::GeneralResult<nn::SharedPreparedModel> Device::prepareModel(
144 const nn::Model& model, nn::ExecutionPreference preference, nn::Priority /*priority*/,
Slava Shklyaev49817a02020-10-27 18:44:01 +0000145 nn::OptionalTimePoint /*deadline*/, const std::vector<nn::SharedHandle>& /*modelCache*/,
Miao Wangb5c8a822021-10-26 20:03:05 +0000146 const std::vector<nn::SharedHandle>& /*dataCache*/, const nn::CacheToken& /*token*/,
147 const std::vector<nn::TokenValuePair>& /*hints*/,
148 const std::vector<nn::ExtensionNameAndPrefix>& /*extensionNameToPrefix*/) const {
Michael Butler4b276a72020-08-06 23:22:35 -0700149 // Ensure that model is ready for IPC.
150 std::optional<nn::Model> maybeModelInShared;
151 const nn::Model& modelInShared =
152 NN_TRY(hal::utils::flushDataFromPointerToShared(&model, &maybeModelInShared));
153
154 const auto hidlModel = NN_TRY(convert(modelInShared));
155 const auto hidlPreference = NN_TRY(convert(preference));
156
157 const auto cb = sp<V1_0::utils::PreparedModelCallback>::make();
158 const auto scoped = kDeathHandler.protectCallback(cb.get());
159
160 const auto ret = kDevice->prepareModel_1_1(hidlModel, hidlPreference, cb);
Michael Butlercca3e202020-11-22 20:25:34 -0800161 const auto status = HANDLE_TRANSPORT_FAILURE(ret);
Michael Butler49d95e02021-10-15 18:52:52 -0700162 HANDLE_STATUS_HIDL(status) << "model preparation failed with " << toString(status);
Michael Butler4b276a72020-08-06 23:22:35 -0700163
164 return cb->get();
165}
166
167nn::GeneralResult<nn::SharedPreparedModel> Device::prepareModelFromCache(
Slava Shklyaev49817a02020-10-27 18:44:01 +0000168 nn::OptionalTimePoint /*deadline*/, const std::vector<nn::SharedHandle>& /*modelCache*/,
169 const std::vector<nn::SharedHandle>& /*dataCache*/, const nn::CacheToken& /*token*/) const {
Michael Butler4b276a72020-08-06 23:22:35 -0700170 return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE)
171 << "IDevice::prepareModelFromCache not supported on 1.1 HAL service";
172}
173
174nn::GeneralResult<nn::SharedBuffer> Device::allocate(
175 const nn::BufferDesc& /*desc*/,
176 const std::vector<nn::SharedPreparedModel>& /*preparedModels*/,
177 const std::vector<nn::BufferRole>& /*inputRoles*/,
178 const std::vector<nn::BufferRole>& /*outputRoles*/) const {
179 return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE)
180 << "IDevice::allocate not supported on 1.1 HAL service";
181}
182
183} // namespace android::hardware::neuralnetworks::V1_1::utils