blob: ab3e0ca879202d6c582adbb744ecebbaa97be41a [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 "Callbacks.h"
18
19#include "Conversions.h"
20#include "PreparedModel.h"
21#include "Utils.h"
22
23#include <android/hardware/neuralnetworks/1.0/types.h>
24#include <android/hardware/neuralnetworks/1.2/IExecutionCallback.h>
25#include <android/hardware/neuralnetworks/1.2/IPreparedModelCallback.h>
26#include <android/hardware/neuralnetworks/1.2/types.h>
27#include <nnapi/IPreparedModel.h>
28#include <nnapi/Result.h>
29#include <nnapi/Types.h>
30#include <nnapi/hal/1.0/Conversions.h>
31#include <nnapi/hal/1.0/PreparedModel.h>
32#include <nnapi/hal/CommonUtils.h>
33#include <nnapi/hal/HandleError.h>
34#include <nnapi/hal/ProtectCallback.h>
35#include <nnapi/hal/TransferValue.h>
36
37#include <utility>
38
Michael Butler7a655bb2020-12-13 23:06:06 -080039// See hardware/interfaces/neuralnetworks/utils/README.md for more information on HIDL interface
40// lifetimes across processes and for protecting asynchronous calls across HIDL.
41
Michael Butler3670c382020-08-06 23:22:35 -070042namespace android::hardware::neuralnetworks::V1_2::utils {
43namespace {
44
45nn::GeneralResult<nn::SharedPreparedModel> convertPreparedModel(
46 const sp<V1_0::IPreparedModel>& preparedModel) {
47 return NN_TRY(V1_0::utils::PreparedModel::create(preparedModel));
48}
49
50nn::GeneralResult<nn::SharedPreparedModel> convertPreparedModel(
51 const sp<IPreparedModel>& preparedModel) {
52 return NN_TRY(utils::PreparedModel::create(preparedModel));
53}
54
55nn::GeneralResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>>
56convertExecutionGeneralResultsHelper(const hidl_vec<OutputShape>& outputShapes,
57 const Timing& timing) {
Michael Butler32acc062020-11-22 19:36:30 -080058 return std::make_pair(NN_TRY(nn::convert(outputShapes)), NN_TRY(nn::convert(timing)));
Michael Butler3670c382020-08-06 23:22:35 -070059}
60
61nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>>
62convertExecutionGeneralResults(const hidl_vec<OutputShape>& outputShapes, const Timing& timing) {
63 return hal::utils::makeExecutionFailure(
64 convertExecutionGeneralResultsHelper(outputShapes, timing));
65}
66
67} // namespace
68
69Return<void> PreparedModelCallback::notify(V1_0::ErrorStatus status,
70 const sp<V1_0::IPreparedModel>& preparedModel) {
71 if (status != V1_0::ErrorStatus::NONE) {
Michael Butler32acc062020-11-22 19:36:30 -080072 const auto canonical = nn::convert(status).value_or(nn::ErrorStatus::GENERAL_FAILURE);
Michael Butler3670c382020-08-06 23:22:35 -070073 notifyInternal(NN_ERROR(canonical) << "preparedModel failed with " << toString(status));
74 } else if (preparedModel == nullptr) {
75 notifyInternal(NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE)
76 << "Returned preparedModel is nullptr");
77 } else {
78 notifyInternal(convertPreparedModel(preparedModel));
79 }
80 return Void();
81}
82
83Return<void> PreparedModelCallback::notify_1_2(V1_0::ErrorStatus status,
84 const sp<IPreparedModel>& preparedModel) {
85 if (status != V1_0::ErrorStatus::NONE) {
Michael Butler32acc062020-11-22 19:36:30 -080086 const auto canonical = nn::convert(status).value_or(nn::ErrorStatus::GENERAL_FAILURE);
Michael Butler3670c382020-08-06 23:22:35 -070087 notifyInternal(NN_ERROR(canonical) << "preparedModel failed with " << toString(status));
88 } else if (preparedModel == nullptr) {
89 notifyInternal(NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE)
90 << "Returned preparedModel is nullptr");
91 } else {
92 notifyInternal(convertPreparedModel(preparedModel));
93 }
94 return Void();
95}
96
97void PreparedModelCallback::notifyAsDeadObject() {
98 notifyInternal(NN_ERROR(nn::ErrorStatus::DEAD_OBJECT) << "Dead object");
99}
100
101PreparedModelCallback::Data PreparedModelCallback::get() {
102 return mData.take();
103}
104
105void PreparedModelCallback::notifyInternal(PreparedModelCallback::Data result) {
106 mData.put(std::move(result));
107}
108
109// ExecutionCallback methods begin here
110
111Return<void> ExecutionCallback::notify(V1_0::ErrorStatus status) {
112 if (status != V1_0::ErrorStatus::NONE) {
Michael Butler32acc062020-11-22 19:36:30 -0800113 const auto canonical = nn::convert(status).value_or(nn::ErrorStatus::GENERAL_FAILURE);
Michael Butler3670c382020-08-06 23:22:35 -0700114 notifyInternal(NN_ERROR(canonical) << "execute failed with " << toString(status));
115 } else {
116 notifyInternal({});
117 }
118 return Void();
119}
120
121Return<void> ExecutionCallback::notify_1_2(V1_0::ErrorStatus status,
122 const hidl_vec<OutputShape>& outputShapes,
123 const Timing& timing) {
124 if (status != V1_0::ErrorStatus::NONE) {
Michael Butler32acc062020-11-22 19:36:30 -0800125 const auto canonical = nn::convert(status).value_or(nn::ErrorStatus::GENERAL_FAILURE);
Michael Butler3670c382020-08-06 23:22:35 -0700126 notifyInternal(NN_ERROR(canonical) << "execute failed with " << toString(status));
127 } else {
128 notifyInternal(convertExecutionGeneralResults(outputShapes, timing));
129 }
130 return Void();
131}
132
133void ExecutionCallback::notifyAsDeadObject() {
134 notifyInternal(NN_ERROR(nn::ErrorStatus::DEAD_OBJECT) << "Dead object");
135}
136
137ExecutionCallback::Data ExecutionCallback::get() {
138 return mData.take();
139}
140
141void ExecutionCallback::notifyInternal(ExecutionCallback::Data result) {
142 mData.put(std::move(result));
143}
144
145} // namespace android::hardware::neuralnetworks::V1_2::utils