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 #define LOG_TAG "SampleDriverAidlPartial"
18 
19 #include "SampleDriverAidlPartial.h"
20 
21 #include <android-base/logging.h>
22 #include <android/binder_auto_utils.h>
23 #include <nnapi/Validation.h>
24 #include <nnapi/hal/aidl/Conversions.h>
25 #include <nnapi/hal/aidl/Utils.h>
26 
27 #include <memory>
28 #include <thread>
29 #include <utility>
30 #include <vector>
31 
32 #include "SampleDriverAidlUtils.h"
33 
34 namespace android {
35 namespace nn {
36 namespace sample_driver_aidl {
37 
getSupportedOperations(const aidl_hal::Model & model,std::vector<bool> * supportedOperations)38 ndk::ScopedAStatus SampleDriverPartial::getSupportedOperations(
39         const aidl_hal::Model& model, std::vector<bool>* supportedOperations) {
40     VLOG(DRIVER) << "getSupportedOperations()";
41     const auto canonicalModel = convert(model);
42     if (!canonicalModel.has_value()) {
43         return toAStatus(aidl_hal::ErrorStatus::INVALID_ARGUMENT, canonicalModel.error().message);
44     }
45     *supportedOperations = getSupportedOperationsImpl(canonicalModel.value());
46     return ndk::ScopedAStatus::ok();
47 }
48 
prepareModel(const aidl_hal::Model & model,aidl_hal::ExecutionPreference preference,aidl_hal::Priority priority,int64_t deadline,const std::vector<ndk::ScopedFileDescriptor> &,const std::vector<ndk::ScopedFileDescriptor> &,const std::vector<uint8_t> &,const std::shared_ptr<aidl_hal::IPreparedModelCallback> & callback)49 ndk::ScopedAStatus SampleDriverPartial::prepareModel(
50         const aidl_hal::Model& model, aidl_hal::ExecutionPreference preference,
51         aidl_hal::Priority priority, int64_t deadline,
52         const std::vector<ndk::ScopedFileDescriptor>&,
53         const std::vector<ndk::ScopedFileDescriptor>&, const std::vector<uint8_t>&,
54         const std::shared_ptr<aidl_hal::IPreparedModelCallback>& callback) {
55     const auto canonicalModel = convert(model);
56     if (!canonicalModel.has_value()) {
57         notify(callback, aidl_hal::ErrorStatus::INVALID_ARGUMENT, nullptr);
58         return toAStatus(aidl_hal::ErrorStatus::INVALID_ARGUMENT, canonicalModel.error().message);
59     }
60     std::vector<bool> supported = getSupportedOperationsImpl(canonicalModel.value());
61     bool isModelFullySupported =
62             std::all_of(supported.begin(), supported.end(), [](bool v) { return v; });
63     auto copiedModel = aidl_hal::utils::clone(model);
64     if (!copiedModel.has_value()) {
65         return toAStatus(aidl_hal::ErrorStatus::GENERAL_FAILURE, copiedModel.error().message);
66     }
67     return prepareModelBase(std::move(copiedModel).value(), this, preference, priority, deadline,
68                             callback, isModelFullySupported);
69 }
70 
71 }  // namespace sample_driver_aidl
72 }  // namespace nn
73 }  // namespace android
74