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 #ifndef ANDROID_PACKAGES_MODULES_NEURALNETWORKS_DRIVER_SAMPLE_SAMPLE_DRIVER_UTILS_H
18 #define ANDROID_PACKAGES_MODULES_NEURALNETWORKS_DRIVER_SAMPLE_SAMPLE_DRIVER_UTILS_H
19 
20 #include <HalInterfaces.h>
21 #include <hwbinder/IPCThreadState.h>
22 
23 #include <string>
24 #include <thread>
25 #include <utility>
26 #include <vector>
27 
28 #include "SampleDriver.h"
29 
30 namespace android {
31 namespace nn {
32 namespace sample_driver {
33 
34 // Starts and runs the driver service.  Typically called from main().
35 // This will return only once the service shuts down.
36 int run(const sp<V1_3::IDevice>& device, const std::string& name);
37 
38 void notify(const sp<V1_0::IPreparedModelCallback>& callback, const V1_3::ErrorStatus& status,
39             const sp<SamplePreparedModel>& preparedModel);
40 
41 void notify(const sp<V1_2::IPreparedModelCallback>& callback, const V1_3::ErrorStatus& status,
42             const sp<SamplePreparedModel>& preparedModel);
43 
44 void notify(const sp<V1_3::IPreparedModelCallback>& callback, const V1_3::ErrorStatus& status,
45             const sp<SamplePreparedModel>& preparedModel);
46 
47 void notify(const sp<V1_0::IExecutionCallback>& callback, const V1_3::ErrorStatus& status,
48             const hardware::hidl_vec<V1_2::OutputShape>&, V1_2::Timing);
49 
50 void notify(const sp<V1_2::IExecutionCallback>& callback, const V1_3::ErrorStatus& status,
51             const hardware::hidl_vec<V1_2::OutputShape>& outputShapes, V1_2::Timing timing);
52 
53 void notify(const sp<V1_3::IExecutionCallback>& callback, const V1_3::ErrorStatus& status,
54             const hardware::hidl_vec<V1_2::OutputShape>& outputShapes, V1_2::Timing timing);
55 
56 template <typename T_Model, typename T_IPreparedModelCallback>
57 V1_3::ErrorStatus prepareModelBase(const T_Model& model, const SampleDriver* driver,
58                                    V1_1::ExecutionPreference preference, V1_3::Priority priority,
59                                    const V1_3::OptionalTimePoint& halDeadline,
60                                    const sp<T_IPreparedModelCallback>& callback,
61                                    bool isFullModelSupported = true) {
62     const uid_t userId = hardware::IPCThreadState::self()->getCallingUid();
63     if (callback.get() == nullptr) {
64         LOG(ERROR) << "invalid callback passed to prepareModelBase";
65         return V1_3::ErrorStatus::INVALID_ARGUMENT;
66     }
67     if (VLOG_IS_ON(DRIVER)) {
68         VLOG(DRIVER) << "prepareModelBase";
69         logModelToInfo(model);
70     }
71     if (!validateModel(model) || !validateExecutionPreference(preference) ||
72         !validatePriority(priority)) {
73         notify(callback, V1_3::ErrorStatus::INVALID_ARGUMENT, nullptr);
74         return V1_3::ErrorStatus::INVALID_ARGUMENT;
75     }
76     if (!isFullModelSupported) {
77         notify(callback, V1_3::ErrorStatus::INVALID_ARGUMENT, nullptr);
78         return V1_3::ErrorStatus::NONE;
79     }
80     const auto deadline = makeDeadline(halDeadline);
81     if (hasDeadlinePassed(deadline)) {
82         notify(callback, V1_3::ErrorStatus::MISSED_DEADLINE_PERSISTENT, nullptr);
83         return V1_3::ErrorStatus::NONE;
84     }
85 
86     // asynchronously prepare the model from a new, detached thread
87     std::thread([model, driver, preference, userId, priority, callback] {
88         sp<SamplePreparedModel> preparedModel =
89                 new SamplePreparedModel(convertToV1_3(model), driver, preference, userId, priority);
90         if (!preparedModel->initialize()) {
91             notify(callback, V1_3::ErrorStatus::INVALID_ARGUMENT, nullptr);
92             return;
93         }
94         notify(callback, V1_3::ErrorStatus::NONE, preparedModel);
95     }).detach();
96 
97     return V1_3::ErrorStatus::NONE;
98 }
99 
100 }  // namespace sample_driver
101 }  // namespace nn
102 }  // namespace android
103 
104 #endif  // ANDROID_PACKAGES_MODULES_NEURALNETWORKS_DRIVER_SAMPLE_SAMPLE_DRIVER_UTILS_H
105