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 #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_0::utils {
39 
40 // Class that adapts V1_0::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_0::IDevice> device);
47 
48     Device(PrivateConstructorTag tag, std::string name, nn::Capabilities capabilities,
49            sp<V1_0::IDevice> device, hal::utils::DeathHandler deathHandler);
50 
51     const std::string& getName() const override;
52     const std::string& getVersionString() const override;
53     nn::Version getFeatureLevel() const override;
54     nn::DeviceType getType() const override;
55     const std::vector<nn::Extension>& getSupportedExtensions() const override;
56     const nn::Capabilities& getCapabilities() const override;
57     std::pair<uint32_t, uint32_t> getNumberOfCacheFilesNeeded() const override;
58 
59     nn::GeneralResult<void> wait() const override;
60 
61     nn::GeneralResult<std::vector<bool>> getSupportedOperations(
62             const nn::Model& model) const override;
63 
64     nn::GeneralResult<nn::SharedPreparedModel> prepareModel(
65             const nn::Model& model, nn::ExecutionPreference preference, nn::Priority priority,
66             nn::OptionalTimePoint deadline, const std::vector<nn::SharedHandle>& modelCache,
67             const std::vector<nn::SharedHandle>& dataCache,
68             const nn::CacheToken& token) const override;
69 
70     nn::GeneralResult<nn::SharedPreparedModel> prepareModelFromCache(
71             nn::OptionalTimePoint deadline, const std::vector<nn::SharedHandle>& modelCache,
72             const std::vector<nn::SharedHandle>& dataCache,
73             const nn::CacheToken& token) const override;
74 
75     nn::GeneralResult<nn::SharedBuffer> allocate(
76             const nn::BufferDesc& desc, const std::vector<nn::SharedPreparedModel>& preparedModels,
77             const std::vector<nn::BufferRole>& inputRoles,
78             const std::vector<nn::BufferRole>& outputRoles) const override;
79 
80   private:
81     const std::string kName;
82     const std::string kVersionString = "UNKNOWN";
83     const std::vector<nn::Extension> kExtensions;
84     const nn::Capabilities kCapabilities;
85     const sp<V1_0::IDevice> kDevice;
86     const hal::utils::DeathHandler kDeathHandler;
87 };
88 
89 }  // namespace android::hardware::neuralnetworks::V1_0::utils
90 
91 #endif  // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_0_UTILS_DEVICE_H
92