1 /*
2  * Copyright (C) 2021 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_AIDL_UTILS_DEVICE_H
18 #define ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_DEVICE_H
19 
20 #include <aidl/android/hardware/neuralnetworks/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/aidl/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 AIDL interface
36 // lifetimes across processes and for protecting asynchronous calls across AIDL.
37 
38 namespace aidl::android::hardware::neuralnetworks::utils {
39 
40 // Class that adapts aidl_hal::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(
46             std::string name, std::shared_ptr<aidl_hal::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            std::shared_ptr<aidl_hal::IDevice> device, 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,
70             const nn::CacheToken& token) 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     DeathMonitor* getDeathMonitor() const;
83 
84   private:
85     const std::string kName;
86     const std::string kVersionString;
87     const nn::DeviceType kDeviceType;
88     const std::vector<nn::Extension> kExtensions;
89     const nn::Capabilities kCapabilities;
90     const std::pair<uint32_t, uint32_t> kNumberOfCacheFilesNeeded;
91     const std::shared_ptr<aidl_hal::IDevice> kDevice;
92     const DeathHandler kDeathHandler;
93 };
94 
95 }  // namespace aidl::android::hardware::neuralnetworks::utils
96 
97 #endif  // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_UTILS_DEVICE_H
98