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_2_UTILS_DEVICE_H
18 #define ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_2_UTILS_DEVICE_H
19 
20 #include <android/hardware/neuralnetworks/1.2/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 #include <nnapi/hal/ProtectCallback.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_2::utils {
39 
40 // Retrieves the version string from the provided device object. On failure, this function returns
41 // with the appropriate nn::GeneralError.
42 nn::GeneralResult<std::string> getVersionStringFrom(V1_2::IDevice* device);
43 
44 // Retrieves the device type from the provided device object. On failure, this function returns with
45 // the appropriate nn::GeneralError.
46 nn::GeneralResult<nn::DeviceType> getDeviceTypeFrom(V1_2::IDevice* device);
47 
48 // Retrieves the extensions supported by the provided device object. On failure, this function
49 // returns with the appropriate nn::GeneralError.
50 nn::GeneralResult<std::vector<nn::Extension>> getSupportedExtensionsFrom(V1_2::IDevice* device);
51 
52 // Retrieves the number of model cache files and data cache files needed by the provided device
53 // object. On failure, this function returns with the appropriate nn::GeneralError.
54 nn::GeneralResult<std::pair<uint32_t, uint32_t>> getNumberOfCacheFilesNeededFrom(
55         V1_2::IDevice* device);
56 
57 // Class that adapts V1_2::IDevice to nn::IDevice.
58 class Device final : public nn::IDevice {
59     struct PrivateConstructorTag {};
60 
61   public:
62     static nn::GeneralResult<std::shared_ptr<const Device>> create(std::string name,
63                                                                    sp<V1_2::IDevice> device);
64 
65     Device(PrivateConstructorTag tag, std::string name, std::string versionString,
66            nn::DeviceType deviceType, std::vector<nn::Extension> extensions,
67            nn::Capabilities capabilities, std::pair<uint32_t, uint32_t> numberOfCacheFilesNeeded,
68            sp<V1_2::IDevice> device, hal::utils::DeathHandler deathHandler);
69 
70     const std::string& getName() const override;
71     const std::string& getVersionString() const override;
72     nn::Version getFeatureLevel() const override;
73     nn::DeviceType getType() const override;
74     const std::vector<nn::Extension>& getSupportedExtensions() const override;
75     const nn::Capabilities& getCapabilities() const override;
76     std::pair<uint32_t, uint32_t> getNumberOfCacheFilesNeeded() const override;
77 
78     nn::GeneralResult<void> wait() const override;
79 
80     nn::GeneralResult<std::vector<bool>> getSupportedOperations(
81             const nn::Model& model) const override;
82 
83     nn::GeneralResult<nn::SharedPreparedModel> prepareModel(
84             const nn::Model& model, nn::ExecutionPreference preference, nn::Priority priority,
85             nn::OptionalTimePoint deadline, const std::vector<nn::SharedHandle>& modelCache,
86             const std::vector<nn::SharedHandle>& dataCache,
87             const nn::CacheToken& token) const override;
88 
89     nn::GeneralResult<nn::SharedPreparedModel> prepareModelFromCache(
90             nn::OptionalTimePoint deadline, const std::vector<nn::SharedHandle>& modelCache,
91             const std::vector<nn::SharedHandle>& dataCache,
92             const nn::CacheToken& token) const override;
93 
94     nn::GeneralResult<nn::SharedBuffer> allocate(
95             const nn::BufferDesc& desc, const std::vector<nn::SharedPreparedModel>& preparedModels,
96             const std::vector<nn::BufferRole>& inputRoles,
97             const std::vector<nn::BufferRole>& outputRoles) const override;
98 
99   private:
100     const std::string kName;
101     const std::string kVersionString;
102     const nn::DeviceType kDeviceType;
103     const std::vector<nn::Extension> kExtensions;
104     const nn::Capabilities kCapabilities;
105     const std::pair<uint32_t, uint32_t> kNumberOfCacheFilesNeeded;
106     const sp<V1_2::IDevice> kDevice;
107     const hal::utils::DeathHandler kDeathHandler;
108 };
109 
110 }  // namespace android::hardware::neuralnetworks::V1_2::utils
111 
112 #endif  // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_2_UTILS_DEVICE_H
113