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 #include "Execution.h"
18 
19 #include "Callbacks.h"
20 #include "Conversions.h"
21 #include "Utils.h"
22 
23 #include <android/hardware/neuralnetworks/1.0/IPreparedModel.h>
24 #include <android/hardware/neuralnetworks/1.0/types.h>
25 #include <nnapi/IExecution.h>
26 #include <nnapi/Result.h>
27 #include <nnapi/TypeUtils.h>
28 #include <nnapi/Types.h>
29 #include <nnapi/hal/CommonUtils.h>
30 #include <nnapi/hal/HandleError.h>
31 #include <nnapi/hal/ProtectCallback.h>
32 
33 #include <memory>
34 #include <utility>
35 #include <vector>
36 
37 // See hardware/interfaces/neuralnetworks/utils/README.md for more information on HIDL interface
38 // lifetimes across processes and for protecting asynchronous calls across HIDL.
39 
40 namespace android::hardware::neuralnetworks::V1_0::utils {
41 
create(std::shared_ptr<const PreparedModel> preparedModel,Request request,hal::utils::RequestRelocation relocation)42 nn::GeneralResult<std::shared_ptr<const Execution>> Execution::create(
43         std::shared_ptr<const PreparedModel> preparedModel, Request request,
44         hal::utils::RequestRelocation relocation) {
45     if (preparedModel == nullptr) {
46         return NN_ERROR() << "V1_0::utils::Execution::create must have non-null preparedModel";
47     }
48 
49     return std::make_shared<const Execution>(PrivateConstructorTag{}, std::move(preparedModel),
50                                              std::move(request), std::move(relocation));
51 }
52 
Execution(PrivateConstructorTag,std::shared_ptr<const PreparedModel> preparedModel,Request request,hal::utils::RequestRelocation relocation)53 Execution::Execution(PrivateConstructorTag /*tag*/,
54                      std::shared_ptr<const PreparedModel> preparedModel, Request request,
55                      hal::utils::RequestRelocation relocation)
56     : kPreparedModel(std::move(preparedModel)),
57       kRequest(std::move(request)),
58       kRelocation(std::move(relocation)) {}
59 
compute(const nn::OptionalTimePoint &) const60 nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>> Execution::compute(
61         const nn::OptionalTimePoint& /*deadline*/) const {
62     return kPreparedModel->executeInternal(kRequest, kRelocation);
63 }
64 
computeFenced(const std::vector<nn::SyncFence> &,const nn::OptionalTimePoint &,const nn::OptionalDuration &) const65 nn::GeneralResult<std::pair<nn::SyncFence, nn::ExecuteFencedInfoCallback>> Execution::computeFenced(
66         const std::vector<nn::SyncFence>& /*waitFor*/, const nn::OptionalTimePoint& /*deadline*/,
67         const nn::OptionalDuration& /*timeoutDurationAfterFence*/) const {
68     return NN_ERROR(nn::ErrorStatus::GENERAL_FAILURE)
69            << "IExecution::computeFenced is not supported on 1.0 HAL service";
70 }
71 
72 }  // namespace android::hardware::neuralnetworks::V1_0::utils
73