• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 prepareModelFromCache(
62             int64_t deadlineNs, const std::vector<::ndk::ScopedFileDescriptor>& modelCache,
63             const std::vector<::ndk::ScopedFileDescriptor>& dataCache,
64             const std::vector<uint8_t>& token,
65             const std::shared_ptr<IPreparedModelCallback>& callback) override;
66 
67    private:
68     std::shared_ptr<const NnApiSupportLibrary> mNnapi;
69     std::shared_ptr<ShimBufferTracker> mBufferTracker;
70     std::string mServiceName;
71     ANeuralNetworksDevice* mDevice;
72     Capabilities mCapabilities;
73     NumberOfCacheFiles mNumberOfCacheFiles;
74     std::vector<Extension> mExtensions;
75 };
76 
77 }  // namespace aidl::android::hardware::neuralnetworks
78