blob: c1dd1d9e707cb376d0a9cba5a57feb03006fcdfd [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) {
45 return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT)
46 << "V1_0::utils::PreparedModel::create must have non-null preparedModel";
47 }
48
49 auto deathHandler = NN_TRY(hal::utils::DeathHandler::create(preparedModel));
50 return std::make_shared<const PreparedModel>(PrivateConstructorTag{}, std::move(preparedModel),
51 std::move(deathHandler));
52}
53
54PreparedModel::PreparedModel(PrivateConstructorTag /*tag*/, sp<V1_0::IPreparedModel> preparedModel,
55 hal::utils::DeathHandler deathHandler)
56 : kPreparedModel(std::move(preparedModel)), kDeathHandler(std::move(deathHandler)) {}
57
58nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> PreparedModel::execute(
59 const nn::Request& request, nn::MeasureTiming /*measure*/,
60 const nn::OptionalTimePoint& /*deadline*/,
Michael Butler4024d8f2020-12-04 17:38:20 -080061 const nn::OptionalDuration& /*loopTimeoutDuration*/) const {
Michael Butler4b276a72020-08-06 23:22:35 -070062 // Ensure that request is ready for IPC.
63 std::optional<nn::Request> maybeRequestInShared;
64 const nn::Request& requestInShared = NN_TRY(hal::utils::makeExecutionFailure(
65 hal::utils::flushDataFromPointerToShared(&request, &maybeRequestInShared)));
66
67 const auto hidlRequest = NN_TRY(hal::utils::makeExecutionFailure(convert(requestInShared)));
68
69 const auto cb = sp<ExecutionCallback>::make();
70 const auto scoped = kDeathHandler.protectCallback(cb.get());
71
72 const auto ret = kPreparedModel->execute(hidlRequest, cb);
Michael Butlercca3e202020-11-22 20:25:34 -080073 const auto status = HANDLE_TRANSPORT_FAILURE(ret);
Michael Butler4b276a72020-08-06 23:22:35 -070074 if (status != ErrorStatus::NONE) {
Michael Butler6547b2a2020-11-22 19:36:30 -080075 const auto canonical = nn::convert(status).value_or(nn::ErrorStatus::GENERAL_FAILURE);
Michael Butler4b276a72020-08-06 23:22:35 -070076 return NN_ERROR(canonical) << "execute failed with " << toString(status);
77 }
78
79 auto result = NN_TRY(cb->get());
80 NN_TRY(hal::utils::makeExecutionFailure(
81 hal::utils::unflushDataFromSharedToPointer(request, maybeRequestInShared)));
82
83 return result;
84}
85
86nn::GeneralResult<std::pair<nn::SyncFence, nn::ExecuteFencedInfoCallback>>
Michael Butler4024d8f2020-12-04 17:38:20 -080087PreparedModel::executeFenced(const nn::Request& /*request*/,
88 const std::vector<nn::SyncFence>& /*waitFor*/,
89 nn::MeasureTiming /*measure*/,
90 const nn::OptionalTimePoint& /*deadline*/,
91 const nn::OptionalDuration& /*loopTimeoutDuration*/,
92 const nn::OptionalDuration& /*timeoutDurationAfterFence*/) const {
Michael Butler4b276a72020-08-06 23:22:35 -070093 return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE)
94 << "IPreparedModel::executeFenced is not supported on 1.0 HAL service";
95}
96
97std::any PreparedModel::getUnderlyingResource() const {
98 sp<V1_0::IPreparedModel> resource = kPreparedModel;
99 return resource;
100}
101
102} // namespace android::hardware::neuralnetworks::V1_0::utils