1 /*
2  * Copyright (C) 2017 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 "android.hardware.neuralnetworks@1.0-impl-hvx"
18 
19 #include "PreparedModel.h"
20 #include <android-base/logging.h>
21 #include <thread>
22 #include "HexagonUtils.h"
23 #include "ValidateHal.h"
24 
25 namespace android {
26 namespace hardware {
27 namespace neuralnetworks {
28 namespace V1_0 {
29 namespace implementation {
30 
PreparedModel(const Model & neuralNetworksModel,const std::shared_ptr<hexagon::Model> & hexagonModel)31 PreparedModel::PreparedModel(const Model& neuralNetworksModel,
32                              const std::shared_ptr<hexagon::Model>& hexagonModel)
33     : mNeuralNetworksModel(neuralNetworksModel), mHexagonModel(hexagonModel) {}
34 
~PreparedModel()35 PreparedModel::~PreparedModel() {}
36 
asyncExecute(const std::shared_ptr<hexagon::Model> & model,const Request & request,const sp<IExecutionCallback> & callback)37 static void asyncExecute(const std::shared_ptr<hexagon::Model>& model, const Request& request,
38                          const sp<IExecutionCallback>& callback) {
39     ErrorStatus status =
40         model->execute(request) == true ? ErrorStatus::NONE : ErrorStatus::GENERAL_FAILURE;
41     Return<void> ret = callback->notify(status);
42     if (!ret.isOk()) {
43         LOG(ERROR) << "Error in callback's return type: " << ret.description();
44     }
45 }
46 
execute(const Request & request,const sp<IExecutionCallback> & callback)47 Return<ErrorStatus> PreparedModel::execute(const Request& request,
48                                            const sp<IExecutionCallback>& callback) {
49     if (callback.get() == nullptr) {
50         LOG(ERROR) << "invalid callback passed to execute";
51         return ErrorStatus::INVALID_ARGUMENT;
52     }
53 
54     if (!nn::validateRequest(request, mNeuralNetworksModel)) {
55         Return<void> ret = callback->notify(ErrorStatus::INVALID_ARGUMENT);
56         if (!ret.isOk()) {
57             LOG(ERROR) << "Error in callback's return type: " << ret.description();
58         }
59         return ErrorStatus::INVALID_ARGUMENT;
60     }
61     if (!hexagon::isHexagonAvailable()) {
62         Return<void> ret = callback->notify(ErrorStatus::DEVICE_UNAVAILABLE);
63         if (!ret.isOk()) {
64             LOG(ERROR) << "Error in callback's return type: " << ret.description();
65         }
66         return ErrorStatus::DEVICE_UNAVAILABLE;
67     }
68 
69     // TODO: once nnlib hanging issue is resolved, make this function
70     // asynchronous again
71     asyncExecute(mHexagonModel, request, callback);
72 
73     return ErrorStatus::NONE;
74 }
75 
76 }  // namespace implementation
77 }  // namespace V1_0
78 }  // namespace neuralnetworks
79 }  // namespace hardware
80 }  // namespace android
81