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 #pragma once
18 
19 #include <aidl/android/hardware/neuralnetworks/BnBuffer.h>
20 #include <aidl/android/hardware/neuralnetworks/BnDevice.h>
21 
22 #include <memory>
23 #include <stack>
24 #include <string>
25 #include <utility>
26 #include <vector>
27 
28 #include "NeuralNetworksShim.h"
29 #include "ShimBufferTracker.h"
30 #include "SupportLibrary.h"
31 #include "SupportLibraryWrapper.h"
32 
33 #include <android-base/macros.h>
34 #include <android-base/thread_annotations.h>
35 
36 namespace aidl::android::hardware::neuralnetworks {
37 
38 class ShimDevice : public BnDevice {
39    public:
40     ShimDevice(std::shared_ptr<const NnApiSupportLibrary>, ANeuralNetworksDevice*,
41                std::string serviceName);
42     ::ndk::ScopedAStatus allocate(const BufferDesc& desc,
43                                   const std::vector<IPreparedModelParcel>& preparedModels,
44                                   const std::vector<BufferRole>& inputRoles,
45                                   const std::vector<BufferRole>& outputRoles,
46                                   DeviceBuffer* deviceBuffer) override;
47     ::ndk::ScopedAStatus getCapabilities(Capabilities* capabilities) override;
48     ::ndk::ScopedAStatus getNumberOfCacheFilesNeeded(
49             NumberOfCacheFiles* numberOfCacheFiles) override;
50     ::ndk::ScopedAStatus getSupportedExtensions(std::vector<Extension>* extensions) override;
51     ::ndk::ScopedAStatus getSupportedOperations(const Model& model,
52                                                 std::vector<bool>* supportedOperations) override;
53     ::ndk::ScopedAStatus getType(DeviceType* deviceType) override;
54     ::ndk::ScopedAStatus getVersionString(std::string* versionString) override;
55     ::ndk::ScopedAStatus prepareModel(
56             const Model& model, ExecutionPreference preference, Priority priority,
57             int64_t deadlineNs, const std::vector<::ndk::ScopedFileDescriptor>& modelCache,
58             const std::vector<::ndk::ScopedFileDescriptor>& dataCache,
59             const std::vector<uint8_t>& token,
60             const std::shared_ptr<IPreparedModelCallback>& callback) override;
61     ::ndk::ScopedAStatus prepareModelWithConfig(
62             const Model& model, const PrepareModelConfig& config,
63             const std::shared_ptr<IPreparedModelCallback>& callback) override;
64     ::ndk::ScopedAStatus prepareModelFromCache(
65             int64_t deadlineNs, const std::vector<::ndk::ScopedFileDescriptor>& modelCache,
66             const std::vector<::ndk::ScopedFileDescriptor>& dataCache,
67             const std::vector<uint8_t>& token,
68             const std::shared_ptr<IPreparedModelCallback>& callback) override;
69 
70    private:
71     ndk::ScopedAStatus prepareModelCommon(
72             const Model& model, ExecutionPreference preference, Priority priority,
73             int64_t deadlineNs, const std::vector<::ndk::ScopedFileDescriptor>& modelCache,
74             const std::vector<::ndk::ScopedFileDescriptor>& dataCache,
75             const std::vector<uint8_t>& token, const std::vector<TokenValuePair>& compilationHints,
76             const std::vector<ExtensionNameAndPrefix>& extensionNameToPrefix,
77             const std::shared_ptr<IPreparedModelCallback>& callback);
78 
79     std::shared_ptr<const NnApiSupportLibrary> mNnapi;
80     std::shared_ptr<ShimBufferTracker> mBufferTracker;
81     std::string mServiceName;
82     ANeuralNetworksDevice* mDevice;
83     Capabilities mCapabilities;
84     NumberOfCacheFiles mNumberOfCacheFiles;
85     std::vector<Extension> mExtensions;
86 };
87 
88 }  // namespace aidl::android::hardware::neuralnetworks
89