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_DEVICE_H
18 #define ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_UTILS_ADAPTER_DEVICE_H
19 
20 #include "nnapi/hal/Adapter.h"
21 
22 #include <android/hardware/neuralnetworks/1.0/IPreparedModelCallback.h>
23 #include <android/hardware/neuralnetworks/1.0/types.h>
24 #include <android/hardware/neuralnetworks/1.1/types.h>
25 #include <android/hardware/neuralnetworks/1.2/IPreparedModelCallback.h>
26 #include <android/hardware/neuralnetworks/1.2/types.h>
27 #include <android/hardware/neuralnetworks/1.3/IDevice.h>
28 #include <android/hardware/neuralnetworks/1.3/IPreparedModelCallback.h>
29 #include <android/hardware/neuralnetworks/1.3/types.h>
30 #include <nnapi/IDevice.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 using CacheToken = hidl_array<uint8_t, nn::kByteSizeOfCacheToken>;
40 
41 // Class that adapts nn::IDevice to V1_3::IDevice.
42 class Device final : public V1_3::IDevice {
43   public:
44     Device(nn::SharedDevice device, Executor executor);
45 
46     Return<void> getCapabilities(getCapabilities_cb cb) override;
47     Return<void> getCapabilities_1_1(getCapabilities_1_1_cb cb) override;
48     Return<void> getCapabilities_1_2(getCapabilities_1_2_cb cb) override;
49     Return<void> getCapabilities_1_3(getCapabilities_1_3_cb cb) override;
50     Return<void> getVersionString(getVersionString_cb cb) override;
51     Return<void> getType(getType_cb cb) override;
52     Return<void> getSupportedExtensions(getSupportedExtensions_cb) override;
53     Return<void> getSupportedOperations(const V1_0::Model& model,
54                                         getSupportedOperations_cb cb) override;
55     Return<void> getSupportedOperations_1_1(const V1_1::Model& model,
56                                             getSupportedOperations_1_1_cb cb) override;
57     Return<void> getSupportedOperations_1_2(const V1_2::Model& model,
58                                             getSupportedOperations_1_2_cb cb) override;
59     Return<void> getSupportedOperations_1_3(const V1_3::Model& model,
60                                             getSupportedOperations_1_3_cb cb) override;
61     Return<void> getNumberOfCacheFilesNeeded(getNumberOfCacheFilesNeeded_cb cb) override;
62     Return<V1_0::ErrorStatus> prepareModel(
63             const V1_0::Model& model, const sp<V1_0::IPreparedModelCallback>& callback) override;
64     Return<V1_0::ErrorStatus> prepareModel_1_1(
65             const V1_1::Model& model, V1_1::ExecutionPreference preference,
66             const sp<V1_0::IPreparedModelCallback>& callback) override;
67     Return<V1_0::ErrorStatus> prepareModel_1_2(
68             const V1_2::Model& model, V1_1::ExecutionPreference preference,
69             const hidl_vec<hidl_handle>& modelCache, const hidl_vec<hidl_handle>& dataCache,
70             const CacheToken& token, const sp<V1_2::IPreparedModelCallback>& callback) override;
71     Return<V1_3::ErrorStatus> prepareModel_1_3(
72             const V1_3::Model& model, V1_1::ExecutionPreference preference, V1_3::Priority priority,
73             const V1_3::OptionalTimePoint& deadline, const hidl_vec<hidl_handle>& modelCache,
74             const hidl_vec<hidl_handle>& dataCache, const CacheToken& token,
75             const sp<V1_3::IPreparedModelCallback>& callback) override;
76     Return<V1_0::ErrorStatus> prepareModelFromCache(
77             const hidl_vec<hidl_handle>& modelCache, const hidl_vec<hidl_handle>& dataCache,
78             const CacheToken& token, const sp<V1_2::IPreparedModelCallback>& callback) override;
79     Return<V1_3::ErrorStatus> prepareModelFromCache_1_3(
80             const V1_3::OptionalTimePoint& deadline, const hidl_vec<hidl_handle>& modelCache,
81             const hidl_vec<hidl_handle>& dataCache, const CacheToken& token,
82             const sp<V1_3::IPreparedModelCallback>& callback) override;
83     Return<V1_0::DeviceStatus> getStatus() override;
84     Return<void> allocate(const V1_3::BufferDesc& desc,
85                           const hidl_vec<sp<V1_3::IPreparedModel>>& preparedModels,
86                           const hidl_vec<V1_3::BufferRole>& inputRoles,
87                           const hidl_vec<V1_3::BufferRole>& outputRoles, allocate_cb cb) override;
88 
89   private:
90     const nn::SharedDevice kDevice;
91     const Executor kExecutor;
92 };
93 
94 }  // namespace android::hardware::neuralnetworks::adapter
95 
96 #endif  // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_UTILS_ADAPTER_DEVICE_H
97