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