1 /*
2  * Copyright (C) 2019 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 #include <android-base/logging.h>
18 #include <hidl/LegacySupport.h>
19 
20 #include <thread>
21 #include <vector>
22 
23 #include "HalInterfaces.h"
24 #include "SampleDriver.h"
25 #include "Utils.h"
26 #include "ValidateHal.h"
27 
28 namespace android {
29 namespace nn {
30 namespace sample_driver {
31 
32 // A base class for sample drivers that support only a subset of NNAPI
33 // operations. Classes of such drivers should inherit from this class and
34 // implement getSupportedOperationsImpl function which is used for filtering out
35 // unsupported ops.
36 class SampleDriverPartial : public SampleDriver {
37    public:
38     SampleDriverPartial(const char* name, const IOperationResolver* operationResolver =
39                                                   BuiltinOperationResolver::get())
SampleDriver(name,operationResolver)40         : SampleDriver(name, operationResolver) {}
41     hal::Return<void> getSupportedOperations_1_3(const hal::V1_3::Model& model,
42                                                  getSupportedOperations_1_3_cb cb) override;
43     hal::Return<hal::ErrorStatus> prepareModel_1_3(
44             const hal::V1_3::Model& model, hal::ExecutionPreference preference,
45             hal::Priority priority, const hal::OptionalTimePoint& deadline,
46             const hal::hidl_vec<hal::hidl_handle>& modelCache,
47             const hal::hidl_vec<hal::hidl_handle>& dataCache, const hal::CacheToken& token,
48             const sp<hal::V1_3::IPreparedModelCallback>& callback) override;
49 
50    protected:
51     // Given a valid NNAPI Model returns a boolean vector that indicates which
52     // ops in the model are supported by a driver.
53     virtual std::vector<bool> getSupportedOperationsImpl(const hal::V1_3::Model& model) const = 0;
54 };
55 
56 }  // namespace sample_driver
57 }  // namespace nn
58 }  // namespace android
59