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