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