Slava Shklyaev | 73ee79d | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 1 | /* |
| 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 Butler | 051cf39 | 2019-07-16 16:52:06 -0700 | [diff] [blame^] | 17 | #define LOG_TAG "Callbacks" |
| 18 | |
Slava Shklyaev | 73ee79d | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 19 | #include "1.2/Callbacks.h" |
Michael Butler | 051cf39 | 2019-07-16 16:52:06 -0700 | [diff] [blame^] | 20 | |
Slava Shklyaev | 73ee79d | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 21 | #include <android-base/logging.h> |
| 22 | |
Michael Butler | 051cf39 | 2019-07-16 16:52:06 -0700 | [diff] [blame^] | 23 | #include <limits> |
Slava Shklyaev | 73ee79d | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 24 | |
Michael Butler | 051cf39 | 2019-07-16 16:52:06 -0700 | [diff] [blame^] | 25 | namespace android::hardware::neuralnetworks::V1_2::implementation { |
Slava Shklyaev | 73ee79d | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 26 | |
Michael Butler | 051cf39 | 2019-07-16 16:52:06 -0700 | [diff] [blame^] | 27 | constexpr Timing kNoTiming = {.timeOnDevice = std::numeric_limits<uint64_t>::max(), |
| 28 | .timeInDriver = std::numeric_limits<uint64_t>::max()}; |
Slava Shklyaev | 73ee79d | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 29 | |
Michael Butler | 051cf39 | 2019-07-16 16:52:06 -0700 | [diff] [blame^] | 30 | // PreparedModelCallback methods begin here |
Slava Shklyaev | 73ee79d | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 31 | |
| 32 | Return<void> PreparedModelCallback::notify(ErrorStatus errorStatus, |
| 33 | const sp<V1_0::IPreparedModel>& preparedModel) { |
Michael Butler | 051cf39 | 2019-07-16 16:52:06 -0700 | [diff] [blame^] | 34 | { |
| 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 Shklyaev | 73ee79d | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 49 | return Void(); |
| 50 | } |
| 51 | |
| 52 | Return<void> PreparedModelCallback::notify_1_2(ErrorStatus errorStatus, |
| 53 | const sp<V1_2::IPreparedModel>& preparedModel) { |
Michael Butler | 051cf39 | 2019-07-16 16:52:06 -0700 | [diff] [blame^] | 54 | return notify(errorStatus, preparedModel); |
Slava Shklyaev | 73ee79d | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 55 | } |
| 56 | |
Michael Butler | 051cf39 | 2019-07-16 16:52:06 -0700 | [diff] [blame^] | 57 | void PreparedModelCallback::wait() const { |
| 58 | std::unique_lock<std::mutex> lock(mMutex); |
| 59 | mCondition.wait(lock, [this] { return mNotified; }); |
| 60 | } |
| 61 | |
| 62 | ErrorStatus PreparedModelCallback::getStatus() const { |
Slava Shklyaev | 73ee79d | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 63 | wait(); |
| 64 | return mErrorStatus; |
| 65 | } |
| 66 | |
Michael Butler | 051cf39 | 2019-07-16 16:52:06 -0700 | [diff] [blame^] | 67 | sp<V1_0::IPreparedModel> PreparedModelCallback::getPreparedModel() const { |
Slava Shklyaev | 73ee79d | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 68 | wait(); |
| 69 | return mPreparedModel; |
| 70 | } |
| 71 | |
Michael Butler | 051cf39 | 2019-07-16 16:52:06 -0700 | [diff] [blame^] | 72 | // ExecutionCallback methods begin here |
Slava Shklyaev | 73ee79d | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 73 | |
| 74 | Return<void> ExecutionCallback::notify(ErrorStatus errorStatus) { |
Michael Butler | 051cf39 | 2019-07-16 16:52:06 -0700 | [diff] [blame^] | 75 | notifyInternal(errorStatus, {}, kNoTiming); |
Slava Shklyaev | 73ee79d | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 76 | return Void(); |
| 77 | } |
| 78 | |
| 79 | Return<void> ExecutionCallback::notify_1_2(ErrorStatus errorStatus, |
| 80 | const hidl_vec<OutputShape>& outputShapes, |
| 81 | const Timing& timing) { |
Michael Butler | 051cf39 | 2019-07-16 16:52:06 -0700 | [diff] [blame^] | 82 | 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 Shklyaev | 73ee79d | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 99 | return Void(); |
| 100 | } |
| 101 | |
Michael Butler | 051cf39 | 2019-07-16 16:52:06 -0700 | [diff] [blame^] | 102 | void ExecutionCallback::wait() const { |
| 103 | std::unique_lock<std::mutex> lock(mMutex); |
| 104 | mCondition.wait(lock, [this] { return mNotified; }); |
| 105 | } |
| 106 | |
| 107 | ErrorStatus ExecutionCallback::getStatus() const { |
Slava Shklyaev | 73ee79d | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 108 | wait(); |
| 109 | return mErrorStatus; |
| 110 | } |
| 111 | |
Michael Butler | 051cf39 | 2019-07-16 16:52:06 -0700 | [diff] [blame^] | 112 | const std::vector<OutputShape>& ExecutionCallback::getOutputShapes() const { |
Slava Shklyaev | 73ee79d | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 113 | wait(); |
| 114 | return mOutputShapes; |
| 115 | } |
| 116 | |
Michael Butler | 051cf39 | 2019-07-16 16:52:06 -0700 | [diff] [blame^] | 117 | Timing ExecutionCallback::getTiming() const { |
Slava Shklyaev | 73ee79d | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 118 | wait(); |
| 119 | return mTiming; |
| 120 | } |
| 121 | |
Michael Butler | 051cf39 | 2019-07-16 16:52:06 -0700 | [diff] [blame^] | 122 | void 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 |