blob: a607a083c0e89eb9e069e49a799f420c527eda58 [file] [log] [blame]
Slava Shklyaev1d6b4652019-05-14 14:15:14 +01001/*
2 * Copyright (C) 2019 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
Michael Butler23d0e562019-07-16 16:52:06 -070017#define LOG_TAG "Callbacks"
18
Slava Shklyaev1d6b4652019-05-14 14:15:14 +010019#include "1.2/Callbacks.h"
Michael Butler23d0e562019-07-16 16:52:06 -070020
Slava Shklyaev1d6b4652019-05-14 14:15:14 +010021#include <android-base/logging.h>
22
Michael Butler23d0e562019-07-16 16:52:06 -070023#include <limits>
Slava Shklyaev1d6b4652019-05-14 14:15:14 +010024
Michael Butler23d0e562019-07-16 16:52:06 -070025namespace android::hardware::neuralnetworks::V1_2::implementation {
Slava Shklyaev1d6b4652019-05-14 14:15:14 +010026
Michael Butler23d0e562019-07-16 16:52:06 -070027constexpr Timing kNoTiming = {.timeOnDevice = std::numeric_limits<uint64_t>::max(),
28 .timeInDriver = std::numeric_limits<uint64_t>::max()};
Slava Shklyaev1d6b4652019-05-14 14:15:14 +010029
Michael Butler23d0e562019-07-16 16:52:06 -070030// PreparedModelCallback methods begin here
Slava Shklyaev1d6b4652019-05-14 14:15:14 +010031
32Return<void> PreparedModelCallback::notify(ErrorStatus errorStatus,
33 const sp<V1_0::IPreparedModel>& preparedModel) {
Michael Butler23d0e562019-07-16 16:52:06 -070034 {
35 std::lock_guard<std::mutex> hold(mMutex);
36
37 // quick-return if object has already been notified
38 if (mNotified) {
39 return Void();
40 }
41
42 // store results and mark as notified
43 mErrorStatus = errorStatus;
44 mPreparedModel = preparedModel;
45 mNotified = true;
46 }
47
48 mCondition.notify_all();
Slava Shklyaev1d6b4652019-05-14 14:15:14 +010049 return Void();
50}
51
52Return<void> PreparedModelCallback::notify_1_2(ErrorStatus errorStatus,
53 const sp<V1_2::IPreparedModel>& preparedModel) {
Michael Butler23d0e562019-07-16 16:52:06 -070054 return notify(errorStatus, preparedModel);
Slava Shklyaev1d6b4652019-05-14 14:15:14 +010055}
56
Michael Butler23d0e562019-07-16 16:52:06 -070057void PreparedModelCallback::wait() const {
58 std::unique_lock<std::mutex> lock(mMutex);
59 mCondition.wait(lock, [this] { return mNotified; });
60}
61
62ErrorStatus PreparedModelCallback::getStatus() const {
Slava Shklyaev1d6b4652019-05-14 14:15:14 +010063 wait();
64 return mErrorStatus;
65}
66
Michael Butler23d0e562019-07-16 16:52:06 -070067sp<V1_0::IPreparedModel> PreparedModelCallback::getPreparedModel() const {
Slava Shklyaev1d6b4652019-05-14 14:15:14 +010068 wait();
69 return mPreparedModel;
70}
71
Michael Butler23d0e562019-07-16 16:52:06 -070072// ExecutionCallback methods begin here
Slava Shklyaev1d6b4652019-05-14 14:15:14 +010073
74Return<void> ExecutionCallback::notify(ErrorStatus errorStatus) {
Michael Butler23d0e562019-07-16 16:52:06 -070075 notifyInternal(errorStatus, {}, kNoTiming);
Slava Shklyaev1d6b4652019-05-14 14:15:14 +010076 return Void();
77}
78
79Return<void> ExecutionCallback::notify_1_2(ErrorStatus errorStatus,
80 const hidl_vec<OutputShape>& outputShapes,
81 const Timing& timing) {
Michael Butler23d0e562019-07-16 16:52:06 -070082 if (errorStatus == ErrorStatus::OUTPUT_INSUFFICIENT_SIZE) {
83 // outputShapes must not be empty if OUTPUT_INSUFFICIENT_SIZE.
84 if (outputShapes.size() == 0) {
85 LOG(ERROR) << "Notified with empty output shape vector when OUTPUT_INSUFFICIENT_SIZE";
86 notifyInternal(ErrorStatus::GENERAL_FAILURE, {}, kNoTiming);
87 return Void();
88 }
89 } else if (errorStatus != ErrorStatus::NONE) {
90 // outputShapes must be empty if errorStatus is neither NONE nor OUTPUT_INSUFFICIENT_SIZE.
91 if (outputShapes.size() != 0) {
92 LOG(ERROR) << "Notified with non-empty output shape vector when error status is "
93 "neither NONE nor OUTPUT_INSUFFICIENT_SIZE";
94 notifyInternal(ErrorStatus::GENERAL_FAILURE, {}, kNoTiming);
95 return Void();
96 }
97 }
98 notifyInternal(errorStatus, outputShapes, timing);
Slava Shklyaev1d6b4652019-05-14 14:15:14 +010099 return Void();
100}
101
Michael Butler23d0e562019-07-16 16:52:06 -0700102void ExecutionCallback::wait() const {
103 std::unique_lock<std::mutex> lock(mMutex);
104 mCondition.wait(lock, [this] { return mNotified; });
105}
106
107ErrorStatus ExecutionCallback::getStatus() const {
Slava Shklyaev1d6b4652019-05-14 14:15:14 +0100108 wait();
109 return mErrorStatus;
110}
111
Michael Butler23d0e562019-07-16 16:52:06 -0700112const std::vector<OutputShape>& ExecutionCallback::getOutputShapes() const {
Slava Shklyaev1d6b4652019-05-14 14:15:14 +0100113 wait();
114 return mOutputShapes;
115}
116
Michael Butler23d0e562019-07-16 16:52:06 -0700117Timing ExecutionCallback::getTiming() const {
Slava Shklyaev1d6b4652019-05-14 14:15:14 +0100118 wait();
119 return mTiming;
120}
121
Michael Butler23d0e562019-07-16 16:52:06 -0700122void ExecutionCallback::notifyInternal(ErrorStatus errorStatus,
123 const hidl_vec<OutputShape>& outputShapes,
124 const Timing& timing) {
125 {
126 std::lock_guard<std::mutex> hold(mMutex);
127
128 // quick-return if object has already been notified
129 if (mNotified) {
130 return;
131 }
132
133 mErrorStatus = errorStatus;
134 mOutputShapes = outputShapes;
135 mTiming = timing;
136 mNotified = true;
137 }
138 mCondition.notify_all();
139}
140
141} // namespace android::hardware::neuralnetworks::V1_2::implementation