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_UTILS_ADAPTER_PREPARED_MODEL_H
18 #define ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_UTILS_ADAPTER_PREPARED_MODEL_H
19 
20 #include "nnapi/hal/Adapter.h"
21 
22 #include <android/hardware/neuralnetworks/1.0/IExecutionCallback.h>
23 #include <android/hardware/neuralnetworks/1.0/types.h>
24 #include <android/hardware/neuralnetworks/1.2/IBurstCallback.h>
25 #include <android/hardware/neuralnetworks/1.2/IExecutionCallback.h>
26 #include <android/hardware/neuralnetworks/1.2/types.h>
27 #include <android/hardware/neuralnetworks/1.3/IExecutionCallback.h>
28 #include <android/hardware/neuralnetworks/1.3/IPreparedModel.h>
29 #include <android/hardware/neuralnetworks/1.3/types.h>
30 #include <nnapi/IPreparedModel.h>
31 #include <nnapi/Types.h>
32 #include <memory>
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::adapter {
38 
39 // Class that adapts nn::IPreparedModel to V1_3::IPreparedModel.
40 class PreparedModel final : public V1_3::IPreparedModel {
41   public:
42     PreparedModel(nn::SharedPreparedModel preparedModel, Executor executor, uid_t userId);
43 
44     Return<V1_0::ErrorStatus> execute(const V1_0::Request& request,
45                                       const sp<V1_0::IExecutionCallback>& callback) override;
46     Return<V1_0::ErrorStatus> execute_1_2(const V1_0::Request& request, V1_2::MeasureTiming measure,
47                                           const sp<V1_2::IExecutionCallback>& callback) override;
48     Return<V1_3::ErrorStatus> execute_1_3(const V1_3::Request& request, V1_2::MeasureTiming measure,
49                                           const V1_3::OptionalTimePoint& deadline,
50                                           const V1_3::OptionalTimeoutDuration& loopTimeoutDuration,
51                                           const sp<V1_3::IExecutionCallback>& callback) override;
52     Return<void> executeSynchronously(const V1_0::Request& request, V1_2::MeasureTiming measure,
53                                       executeSynchronously_cb cb) override;
54     Return<void> executeSynchronously_1_3(const V1_3::Request& request, V1_2::MeasureTiming measure,
55                                           const V1_3::OptionalTimePoint& deadline,
56                                           const V1_3::OptionalTimeoutDuration& loopTimeoutDuration,
57                                           executeSynchronously_1_3_cb cb) override;
58     Return<void> configureExecutionBurst(
59             const sp<V1_2::IBurstCallback>& callback,
60             const MQDescriptorSync<V1_2::FmqRequestDatum>& requestChannel,
61             const MQDescriptorSync<V1_2::FmqResultDatum>& resultChannel,
62             configureExecutionBurst_cb cb) override;
63     Return<void> executeFenced(const V1_3::Request& request, const hidl_vec<hidl_handle>& waitFor,
64                                V1_2::MeasureTiming measure, const V1_3::OptionalTimePoint& deadline,
65                                const V1_3::OptionalTimeoutDuration& loopTimeoutDuration,
66                                const V1_3::OptionalTimeoutDuration& duration,
67                                executeFenced_cb callback) override;
68 
69     nn::SharedPreparedModel getUnderlyingPreparedModel() const;
70 
71   private:
72     const nn::SharedPreparedModel kPreparedModel;
73     const Executor kExecutor;
74     const uid_t kUserId;
75 };
76 
77 }  // namespace android::hardware::neuralnetworks::adapter
78 
79 #endif  // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_UTILS_ADAPTER_PREPARED_MODEL_H
80