blob: c0c22fbd6abdace6c33ce12f78ca84b77f527488 [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 "PreparedModel.h"
18
19#include "Callbacks.h"
20#include "Conversions.h"
21#include "Utils.h"
22
23#include <android/hardware/neuralnetworks/1.0/IPreparedModel.h>
24#include <android/hardware/neuralnetworks/1.0/types.h>
25#include <nnapi/IPreparedModel.h>
26#include <nnapi/Result.h>
27#include <nnapi/Types.h>
28#include <nnapi/hal/CommonUtils.h>
29#include <nnapi/hal/HandleError.h>
30#include <nnapi/hal/ProtectCallback.h>
31
32#include <memory>
33#include <tuple>
34#include <utility>
35#include <vector>
36
Michael Butleraad934b2020-12-13 23:06:06 -080037// See hardware/interfaces/neuralnetworks/utils/README.md for more information on HIDL interface
38// lifetimes across processes and for protecting asynchronous calls across HIDL.
39
Michael Butler4b276a72020-08-06 23:22:35 -070040namespace android::hardware::neuralnetworks::V1_0::utils {
41
42nn::GeneralResult<std::shared_ptr<const PreparedModel>> PreparedModel::create(
43 sp<V1_0::IPreparedModel> preparedModel) {
44 if (preparedModel == nullptr) {
Michael Butler7fd03c22020-12-06 21:50:59 -080045 return NN_ERROR() << "V1_0::utils::PreparedModel::create must have non-null preparedModel";
Michael Butler4b276a72020-08-06 23:22:35 -070046 }
47
48 auto deathHandler = NN_TRY(hal::utils::DeathHandler::create(preparedModel));
49 return std::make_shared<const PreparedModel>(PrivateConstructorTag{}, std::move(preparedModel),
50 std::move(deathHandler));
51}
52
53PreparedModel::PreparedModel(PrivateConstructorTag /*tag*/, sp<V1_0::IPreparedModel> preparedModel,
54 hal::utils::DeathHandler deathHandler)
55 : kPreparedModel(std::move(preparedModel)), kDeathHandler(std::move(deathHandler)) {}
56
57nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> PreparedModel::execute(
58 const nn::Request& request, nn::MeasureTiming /*measure*/,
59 const nn::OptionalTimePoint& /*deadline*/,
Michael Butler4024d8f2020-12-04 17:38:20 -080060 const nn::OptionalDuration& /*loopTimeoutDuration*/) const {
Michael Butler4b276a72020-08-06 23:22:35 -070061 // Ensure that request is ready for IPC.
62 std::optional<nn::Request> maybeRequestInShared;
63 const nn::Request& requestInShared = NN_TRY(hal::utils::makeExecutionFailure(
64 hal::utils::flushDataFromPointerToShared(&request, &maybeRequestInShared)));
65
66 const auto hidlRequest = NN_TRY(hal::utils::makeExecutionFailure(convert(requestInShared)));
67
68 const auto cb = sp<ExecutionCallback>::make();
69 const auto scoped = kDeathHandler.protectCallback(cb.get());
70
71 const auto ret = kPreparedModel->execute(hidlRequest, cb);
Michael Butlercca3e202020-11-22 20:25:34 -080072 const auto status = HANDLE_TRANSPORT_FAILURE(ret);
Michael Butler7fd03c22020-12-06 21:50:59 -080073 HANDLE_HAL_STATUS(status) << "execution failed with " << toString(status);
Michael Butler4b276a72020-08-06 23:22:35 -070074
75 auto result = NN_TRY(cb->get());
76 NN_TRY(hal::utils::makeExecutionFailure(
77 hal::utils::unflushDataFromSharedToPointer(request, maybeRequestInShared)));
78
79 return result;
80}
81
82nn::GeneralResult<std::pair<nn::SyncFence, nn::ExecuteFencedInfoCallback>>
Michael Butler4024d8f2020-12-04 17:38:20 -080083PreparedModel::executeFenced(const nn::Request& /*request*/,
84 const std::vector<nn::SyncFence>& /*waitFor*/,
85 nn::MeasureTiming /*measure*/,
86 const nn::OptionalTimePoint& /*deadline*/,
87 const nn::OptionalDuration& /*loopTimeoutDuration*/,
88 const nn::OptionalDuration& /*timeoutDurationAfterFence*/) const {
Michael Butler4b276a72020-08-06 23:22:35 -070089 return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE)
90 << "IPreparedModel::executeFenced is not supported on 1.0 HAL service";
91}
92
93std::any PreparedModel::getUnderlyingResource() const {
94 sp<V1_0::IPreparedModel> resource = kPreparedModel;
95 return resource;
96}
97
98} // namespace android::hardware::neuralnetworks::V1_0::utils