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 #ifndef ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_0_UTILS_CALLBACKS_H
18 #define ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_0_UTILS_CALLBACKS_H
19 
20 #include <android/hardware/neuralnetworks/1.0/IExecutionCallback.h>
21 #include <android/hardware/neuralnetworks/1.0/IPreparedModelCallback.h>
22 #include <android/hardware/neuralnetworks/1.0/types.h>
23 #include <nnapi/IPreparedModel.h>
24 #include <nnapi/Result.h>
25 #include <nnapi/Types.h>
26 #include <nnapi/hal/CommonUtils.h>
27 #include <nnapi/hal/TransferValue.h>
28 
29 #include "nnapi/hal/1.0/ProtectCallback.h"
30 
31 // See hardware/interfaces/neuralnetworks/utils/README.md for more information on HIDL interface
32 // lifetimes across processes and for protecting asynchronous calls across HIDL.
33 
34 namespace android::hardware::neuralnetworks::V1_0::utils {
35 
36 // Converts the results of IDevice::getSupportedOperations* to the NN canonical format. On success,
37 // this function returns with the supported operations as indicated by a driver. On failure, this
38 // function returns with the appropriate nn::GeneralError.
39 nn::GeneralResult<std::vector<bool>> supportedOperationsCallback(
40         ErrorStatus status, const hidl_vec<bool>& supportedOperations);
41 
42 // Converts the results of IDevice::prepareModel* to the NN canonical format. On success, this
43 // function returns with a non-null nn::SharedPreparedModel with a feature level of
44 // nn::kVersionFeatureLevel1. On failure, this function returns with the appropriate
45 // nn::GeneralError.
46 nn::GeneralResult<nn::SharedPreparedModel> prepareModelCallback(
47         ErrorStatus status, const sp<IPreparedModel>& preparedModel);
48 
49 // Converts the results of IDevice::execute* to the NN canonical format. On success, this function
50 // returns with an empty output shape vector and no timing information. On failure, this function
51 // returns with the appropriate nn::ExecutionError.
52 nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> executionCallback(
53         ErrorStatus status);
54 
55 // A HIDL callback class to receive the results of IDevice::prepareModel asynchronously.
56 class PreparedModelCallback final : public IPreparedModelCallback,
57                                     public hal::utils::IProtectedCallback {
58   public:
59     using Data = nn::GeneralResult<nn::SharedPreparedModel>;
60 
61     Return<void> notify(ErrorStatus status, const sp<IPreparedModel>& preparedModel) override;
62 
63     void notifyAsDeadObject() override;
64 
65     Data get();
66 
67   private:
68     hal::utils::TransferValue<Data> mData;
69 };
70 
71 // A HIDL callback class to receive the results of IDevice::execute asynchronously.
72 class ExecutionCallback final : public IExecutionCallback, public hal::utils::IProtectedCallback {
73   public:
74     using Data = nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>>;
75 
76     Return<void> notify(ErrorStatus status) override;
77 
78     void notifyAsDeadObject() override;
79 
80     Data get();
81 
82   private:
83     hal::utils::TransferValue<Data> mData;
84 };
85 
86 }  // namespace android::hardware::neuralnetworks::V1_0::utils
87 
88 #endif  // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_0_UTILS_CALLBACKS_H
89