1 /*
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/IExecutionCallback.h>
24 #include <android/hardware/neuralnetworks/1.0/IPreparedModelCallback.h>
25 #include <android/hardware/neuralnetworks/1.0/types.h>
26 #include <nnapi/IPreparedModel.h>
27 #include <nnapi/Result.h>
28 #include <nnapi/Types.h>
29 #include <nnapi/hal/CommonUtils.h>
30 #include <nnapi/hal/HandleError.h>
31 #include <nnapi/hal/ProtectCallback.h>
32 #include <nnapi/hal/TransferValue.h>
33 
34 #include <utility>
35 
36 // See hardware/interfaces/neuralnetworks/utils/README.md for more information on HIDL interface
37 // lifetimes across processes and for protecting asynchronous calls across HIDL.
38 
39 namespace android::hardware::neuralnetworks::V1_0::utils {
40 
supportedOperationsCallback(ErrorStatus status,const hidl_vec<bool> & supportedOperations)41 nn::GeneralResult<std::vector<bool>> supportedOperationsCallback(
42         ErrorStatus status, const hidl_vec<bool>& supportedOperations) {
43     HANDLE_HAL_STATUS(status) << "get supported operations failed with " << toString(status);
44     return supportedOperations;
45 }
46 
prepareModelCallback(ErrorStatus status,const sp<IPreparedModel> & preparedModel)47 nn::GeneralResult<nn::SharedPreparedModel> prepareModelCallback(
48         ErrorStatus status, const sp<IPreparedModel>& preparedModel) {
49     HANDLE_HAL_STATUS(status) << "model preparation failed with " << toString(status);
50     return NN_TRY(PreparedModel::create(preparedModel));
51 }
52 
executionCallback(ErrorStatus status)53 nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> executionCallback(
54         ErrorStatus status) {
55     HANDLE_HAL_STATUS(status) << "execution failed with " << toString(status);
56     return {};
57 }
58 
notify(ErrorStatus status,const sp<IPreparedModel> & preparedModel)59 Return<void> PreparedModelCallback::notify(ErrorStatus status,
60                                            const sp<IPreparedModel>& preparedModel) {
61     mData.put(prepareModelCallback(status, preparedModel));
62     return Void();
63 }
64 
notifyAsDeadObject()65 void PreparedModelCallback::notifyAsDeadObject() {
66     mData.put(NN_ERROR(nn::ErrorStatus::DEAD_OBJECT) << "Dead object");
67 }
68 
get()69 PreparedModelCallback::Data PreparedModelCallback::get() {
70     return mData.take();
71 }
72 
73 // ExecutionCallback methods begin here
74 
notify(ErrorStatus status)75 Return<void> ExecutionCallback::notify(ErrorStatus status) {
76     mData.put(executionCallback(status));
77     return Void();
78 }
79 
notifyAsDeadObject()80 void ExecutionCallback::notifyAsDeadObject() {
81     mData.put(NN_ERROR(nn::ErrorStatus::DEAD_OBJECT) << "Dead object");
82 }
83 
get()84 ExecutionCallback::Data ExecutionCallback::get() {
85     return mData.take();
86 }
87 
88 }  // namespace android::hardware::neuralnetworks::V1_0::utils
89