• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/1.0/ProtectCallback.h>
31 #include <nnapi/hal/CommonUtils.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::kVersionFeatureLevel3. On failure, this function returns with the appropriate
42 // nn::GeneralError.
43 nn::GeneralResult<nn::SharedPreparedModel> prepareModelCallback(
44         V1_0::ErrorStatus status, const sp<IPreparedModel>& preparedModel);
45 
46 // Converts the results of IDevice::execute* to the NN canonical format. On success, this function
47 // returns with the output shapes and the timing information. On failure, this function returns with
48 // the appropriate nn::ExecutionError.
49 nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> executionCallback(
50         V1_0::ErrorStatus status, const hidl_vec<OutputShape>& outputShapes, const Timing& timing);
51 
52 // A HIDL callback class to receive the results of IDevice::prepareModel* asynchronously.
53 class PreparedModelCallback final : public IPreparedModelCallback,
54                                     public hal::utils::IProtectedCallback {
55   public:
56     using Data = nn::GeneralResult<nn::SharedPreparedModel>;
57 
58     Return<void> notify(V1_0::ErrorStatus status,
59                         const sp<V1_0::IPreparedModel>& preparedModel) override;
60     Return<void> notify_1_2(V1_0::ErrorStatus status,
61                             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_1_2 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(V1_0::ErrorStatus status) override;
77     Return<void> notify_1_2(V1_0::ErrorStatus status, const hidl_vec<OutputShape>& outputShapes,
78                             const Timing& timing) override;
79 
80     void notifyAsDeadObject() override;
81 
82     Data get();
83 
84   private:
85     hal::utils::TransferValue<Data> mData;
86 };
87 
88 }  // namespace android::hardware::neuralnetworks::V1_2::utils
89 
90 #endif  // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_2_UTILS_CALLBACKS_H
91