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_UTILS_COMMON_RESILIENT_DEVICE_H
18 #define ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_UTILS_COMMON_RESILIENT_DEVICE_H
19 
20 #include <android-base/thread_annotations.h>
21 #include <nnapi/IBuffer.h>
22 #include <nnapi/IDevice.h>
23 #include <nnapi/IPreparedModel.h>
24 #include <nnapi/Result.h>
25 #include <nnapi/Types.h>
26 
27 #include <functional>
28 #include <memory>
29 #include <mutex>
30 #include <string>
31 #include <vector>
32 
33 namespace android::hardware::neuralnetworks::utils {
34 
35 class ResilientDevice final : public nn::IDevice,
36                               public std::enable_shared_from_this<ResilientDevice> {
37     struct PrivateConstructorTag {};
38 
39   public:
40     using Factory = std::function<nn::GeneralResult<nn::SharedDevice>(bool blocking)>;
41 
42     static nn::GeneralResult<std::shared_ptr<const ResilientDevice>> create(Factory makeDevice);
43 
44     explicit ResilientDevice(PrivateConstructorTag tag, Factory makeDevice, std::string name,
45                              std::string versionString, std::vector<nn::Extension> extensions,
46                              nn::Capabilities capabilities, nn::SharedDevice device);
47 
48     nn::SharedDevice getDevice() const EXCLUDES(mMutex);
49     nn::GeneralResult<nn::SharedDevice> recover(const nn::IDevice* failingDevice,
50                                                 bool blocking) const EXCLUDES(mMutex);
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,
69             const nn::CacheToken& token) const override;
70 
71     nn::GeneralResult<nn::SharedPreparedModel> prepareModelFromCache(
72             nn::OptionalTimePoint deadline, const std::vector<nn::SharedHandle>& modelCache,
73             const std::vector<nn::SharedHandle>& dataCache,
74             const nn::CacheToken& token) const override;
75 
76     nn::GeneralResult<nn::SharedBuffer> allocate(
77             const nn::BufferDesc& desc, const std::vector<nn::SharedPreparedModel>& preparedModels,
78             const std::vector<nn::BufferRole>& inputRoles,
79             const std::vector<nn::BufferRole>& outputRoles) const override;
80 
81   private:
82     bool isValidInternal() const EXCLUDES(mMutex);
83     nn::GeneralResult<nn::SharedPreparedModel> prepareModelInternal(
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, const nn::CacheToken& token) const;
87     nn::GeneralResult<nn::SharedPreparedModel> prepareModelFromCacheInternal(
88             nn::OptionalTimePoint deadline, const std::vector<nn::SharedHandle>& modelCache,
89             const std::vector<nn::SharedHandle>& dataCache, const nn::CacheToken& token) const;
90     nn::GeneralResult<nn::SharedBuffer> allocateInternal(
91             const nn::BufferDesc& desc, const std::vector<nn::SharedPreparedModel>& preparedModels,
92             const std::vector<nn::BufferRole>& inputRoles,
93             const std::vector<nn::BufferRole>& outputRoles) const;
94 
95     const Factory kMakeDevice;
96     const std::string kName;
97     const std::string kVersionString;
98     const std::vector<nn::Extension> kExtensions;
99     const nn::Capabilities kCapabilities;
100     mutable std::mutex mMutex;
101     mutable nn::SharedDevice mDevice GUARDED_BY(mMutex);
102     mutable bool mIsValid GUARDED_BY(mMutex) = true;
103 };
104 
105 }  // namespace android::hardware::neuralnetworks::utils
106 
107 #endif  // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_UTILS_COMMON_RESILIENT_DEVICE_H
108