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