xref: /aosp_15_r20/hardware/interfaces/neuralnetworks/1.3/utils/include/nnapi/hal/1.3/Device.h (revision 4d7e907c777eeecc4c5bd7cf640a754fac206ff7)
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_3_UTILS_DEVICE_H
18 #define ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_3_UTILS_DEVICE_H
19 
20 #include <android/hardware/neuralnetworks/1.3/IDevice.h>
21 #include <nnapi/IBuffer.h>
22 #include <nnapi/IDevice.h>
23 #include <nnapi/OperandTypes.h>
24 #include <nnapi/Result.h>
25 #include <nnapi/Types.h>
26 #include <nnapi/hal/1.0/ProtectCallback.h>
27 #include <nnapi/hal/CommonUtils.h>
28 
29 #include <functional>
30 #include <memory>
31 #include <optional>
32 #include <string>
33 #include <vector>
34 
35 // See hardware/interfaces/neuralnetworks/utils/README.md for more information on HIDL interface
36 // lifetimes across processes and for protecting asynchronous calls across HIDL.
37 
38 namespace android::hardware::neuralnetworks::V1_3::utils {
39 
40 // Class that adapts V1_3::IDevice to nn::IDevice.
41 class Device final : public nn::IDevice {
42     struct PrivateConstructorTag {};
43 
44   public:
45     static nn::GeneralResult<std::shared_ptr<const Device>> create(std::string name,
46                                                                    sp<V1_3::IDevice> device);
47 
48     Device(PrivateConstructorTag tag, std::string name, std::string versionString,
49            nn::DeviceType deviceType, std::vector<nn::Extension> extensions,
50            nn::Capabilities capabilities, std::pair<uint32_t, uint32_t> numberOfCacheFilesNeeded,
51            sp<V1_3::IDevice> device, hal::utils::DeathHandler deathHandler);
52 
53     const std::string& getName() const override;
54     const std::string& getVersionString() const override;
55     nn::Version getFeatureLevel() const override;
56     nn::DeviceType getType() const override;
57     const std::vector<nn::Extension>& getSupportedExtensions() const override;
58     const nn::Capabilities& getCapabilities() const override;
59     std::pair<uint32_t, uint32_t> getNumberOfCacheFilesNeeded() const override;
60 
61     nn::GeneralResult<void> wait() const override;
62 
63     nn::GeneralResult<std::vector<bool>> getSupportedOperations(
64             const nn::Model& model) const override;
65 
66     nn::GeneralResult<nn::SharedPreparedModel> prepareModel(
67             const nn::Model& model, nn::ExecutionPreference preference, nn::Priority priority,
68             nn::OptionalTimePoint deadline, const std::vector<nn::SharedHandle>& modelCache,
69             const std::vector<nn::SharedHandle>& dataCache, const nn::CacheToken& token,
70             const std::vector<nn::TokenValuePair>& hints,
71             const std::vector<nn::ExtensionNameAndPrefix>& extensionNameToPrefix) const override;
72 
73     nn::GeneralResult<nn::SharedPreparedModel> prepareModelFromCache(
74             nn::OptionalTimePoint deadline, const std::vector<nn::SharedHandle>& modelCache,
75             const std::vector<nn::SharedHandle>& dataCache,
76             const nn::CacheToken& token) const override;
77 
78     nn::GeneralResult<nn::SharedBuffer> allocate(
79             const nn::BufferDesc& desc, const std::vector<nn::SharedPreparedModel>& preparedModels,
80             const std::vector<nn::BufferRole>& inputRoles,
81             const std::vector<nn::BufferRole>& outputRoles) const override;
82 
83   private:
84     const std::string kName;
85     const std::string kVersionString;
86     const nn::DeviceType kDeviceType;
87     const std::vector<nn::Extension> kExtensions;
88     const nn::Capabilities kCapabilities;
89     const std::pair<uint32_t, uint32_t> kNumberOfCacheFilesNeeded;
90     const sp<V1_3::IDevice> kDevice;
91     const hal::utils::DeathHandler kDeathHandler;
92 };
93 
94 }  // namespace android::hardware::neuralnetworks::V1_3::utils
95 
96 #endif  // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_3_UTILS_DEVICE_H
97