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_PACKAGES_MODULES_NEURALNETWORKS_DRIVER_SAMPLE_LIMITED_SUPPORT_DEVICE_H 18 #define ANDROID_PACKAGES_MODULES_NEURALNETWORKS_DRIVER_SAMPLE_LIMITED_SUPPORT_DEVICE_H 19 20 #include <nnapi/IBuffer.h> 21 #include <nnapi/IDevice.h> 22 #include <nnapi/OperandTypes.h> 23 #include <nnapi/Result.h> 24 #include <nnapi/Types.h> 25 26 #include <functional> 27 #include <memory> 28 #include <optional> 29 #include <string> 30 #include <utility> 31 #include <vector> 32 33 namespace android::nn::sample { 34 35 // Class that adapts an IDevice object to allow the caller to: 36 // (1) provide custom Capabilities 37 // (2) customize the behavior of getSupportedOperations 38 class LimitedSupportDevice final : public IDevice { 39 public: 40 using SupportedOperationsFunction = 41 std::function<GeneralResult<std::vector<bool>>(const Model&)>; 42 43 // Precondition: device != nullptr 44 // Precondition: validate(capabilities).ok() 45 // Precondition: supportedOperationsFunction != nullptr 46 LimitedSupportDevice(SharedDevice device, Capabilities capabilities, 47 SupportedOperationsFunction supportedOperationsFunction); 48 49 const std::string& getName() const override; 50 const std::string& getVersionString() const override; 51 Version getFeatureLevel() const override; 52 DeviceType getType() const override; 53 const std::vector<Extension>& getSupportedExtensions() const override; 54 const Capabilities& getCapabilities() const override; 55 std::pair<uint32_t, uint32_t> getNumberOfCacheFilesNeeded() const override; 56 57 GeneralResult<void> wait() const override; 58 59 GeneralResult<std::vector<bool>> getSupportedOperations(const Model& model) const override; 60 61 GeneralResult<SharedPreparedModel> prepareModel(const Model& model, 62 ExecutionPreference preference, 63 Priority priority, OptionalTimePoint deadline, 64 const std::vector<SharedHandle>& modelCache, 65 const std::vector<SharedHandle>& dataCache, 66 const CacheToken& token) const override; 67 68 GeneralResult<SharedPreparedModel> prepareModelFromCache( 69 OptionalTimePoint deadline, const std::vector<SharedHandle>& modelCache, 70 const std::vector<SharedHandle>& dataCache, const CacheToken& token) const override; 71 72 GeneralResult<SharedBuffer> allocate(const BufferDesc& desc, 73 const std::vector<SharedPreparedModel>& preparedModels, 74 const std::vector<BufferRole>& inputRoles, 75 const std::vector<BufferRole>& outputRoles) const override; 76 77 private: 78 const nn::SharedDevice kDevice; 79 const Capabilities kCapabilities; 80 const SupportedOperationsFunction kSupportedOperationsFunction; 81 }; 82 83 } // namespace android::nn::sample 84 85 #endif // ANDROID_PACKAGES_MODULES_NEURALNETWORKS_DRIVER_SAMPLE_LIMITED_SUPPORT_DEVICE_H 86