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_ADAPTER_H
18 #define ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_UTILS_ADAPTER_ADAPTER_H
19 
20 #include <android/hardware/neuralnetworks/1.3/IDevice.h>
21 #include <nnapi/IDevice.h>
22 #include <nnapi/Types.h>
23 #include <functional>
24 #include <memory>
25 
26 // See hardware/interfaces/neuralnetworks/utils/README.md for more information on HIDL interface
27 // lifetimes across processes and for protecting asynchronous calls across HIDL.
28 
29 namespace android::hardware::neuralnetworks::adapter {
30 
31 /**
32  * A self-contained unit of work to be executed.
33  */
34 using Task = std::function<void()>;
35 
36 /**
37  * A type-erased executor which executes a task asynchronously.
38  *
39  * This executor is also provided an optional deadline for when the caller expects is the upper
40  * bound for the amount of time to complete the task. If needed, the Executor can retrieve the
41  * Application ID (Android User ID) by calling IPCThreadState::self()->getCallingUid() in
42  * hwbinder/IPCThreadState.h.
43  */
44 using Executor = std::function<void(Task, nn::OptionalTimePoint)>;
45 
46 /**
47  * Adapt an NNAPI canonical interface object to a HIDL NN HAL interface object.
48  *
49  * @param device NNAPI canonical IDevice interface object to be adapted.
50  * @param executor Type-erased executor to handle executing tasks asynchronously.
51  * @return HIDL NN HAL IDevice interface object.
52  */
53 sp<V1_3::IDevice> adapt(nn::SharedDevice device, Executor executor);
54 
55 /**
56  * Adapt an NNAPI canonical interface object to a HIDL NN HAL interface object.
57  *
58  * This function uses a default executor, which will execute tasks from a detached thread.
59  *
60  * @param device NNAPI canonical IDevice interface object to be adapted.
61  * @return HIDL NN HAL IDevice interface object.
62  */
63 sp<V1_3::IDevice> adapt(nn::SharedDevice device);
64 
65 }  // namespace android::hardware::neuralnetworks::adapter
66 
67 #endif  // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_UTILS_ADAPTER_ADAPTER_H
68