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 "Buffer.h"
18
19 #include <aidl/android/hardware/neuralnetworks/BnBuffer.h>
20 #include <aidl/android/hardware/neuralnetworks/Memory.h>
21 #include <android-base/logging.h>
22 #include <android/binder_auto_utils.h>
23 #include <nnapi/IBuffer.h>
24 #include <nnapi/Result.h>
25 #include <nnapi/Types.h>
26 #include <nnapi/hal/aidl/Conversions.h>
27
28 namespace aidl::android::hardware::neuralnetworks::adapter {
29 namespace {
30
31 template <typename Type>
convertInput(const Type & object)32 auto convertInput(const Type& object) -> decltype(nn::convert(std::declval<Type>())) {
33 auto result = nn::convert(object);
34 if (!result.has_value()) {
35 result.error().code = nn::ErrorStatus::INVALID_ARGUMENT;
36 }
37 return result;
38 }
39
inputToUnsigned(const std::vector<int32_t> & dims)40 nn::GeneralResult<std::vector<uint32_t>> inputToUnsigned(const std::vector<int32_t>& dims) {
41 auto result = nn::toUnsigned(dims);
42 if (!result.has_value()) {
43 result.error().code = nn::ErrorStatus::INVALID_ARGUMENT;
44 }
45 return result;
46 }
47
copyTo(const nn::IBuffer & buffer,const Memory & dst)48 nn::GeneralResult<void> copyTo(const nn::IBuffer& buffer, const Memory& dst) {
49 const auto nnDst = NN_TRY(convertInput(dst));
50 return buffer.copyTo(nnDst);
51 }
52
copyFrom(const nn::IBuffer & buffer,const Memory & src,const std::vector<int32_t> & dimensions)53 nn::GeneralResult<void> copyFrom(const nn::IBuffer& buffer, const Memory& src,
54 const std::vector<int32_t>& dimensions) {
55 const auto nnSrc = NN_TRY(convertInput(src));
56 const auto nnDims = NN_TRY(inputToUnsigned(dimensions));
57 return buffer.copyFrom(nnSrc, nnDims);
58 }
59
60 } // namespace
61
Buffer(nn::SharedBuffer buffer)62 Buffer::Buffer(nn::SharedBuffer buffer) : kBuffer(std::move(buffer)) {
63 CHECK(kBuffer != nullptr);
64 }
65
copyTo(const Memory & dst)66 ndk::ScopedAStatus Buffer::copyTo(const Memory& dst) {
67 const auto result = adapter::copyTo(*kBuffer, dst);
68 if (!result.has_value()) {
69 const auto& [message, code] = result.error();
70 const auto aidlCode = utils::convert(code).value_or(ErrorStatus::GENERAL_FAILURE);
71 return ndk::ScopedAStatus::fromServiceSpecificErrorWithMessage(
72 static_cast<int32_t>(aidlCode), message.c_str());
73 }
74 return ndk::ScopedAStatus::ok();
75 }
76
copyFrom(const Memory & src,const std::vector<int32_t> & dimensions)77 ndk::ScopedAStatus Buffer::copyFrom(const Memory& src, const std::vector<int32_t>& dimensions) {
78 const auto result = adapter::copyFrom(*kBuffer, src, dimensions);
79 if (!result.has_value()) {
80 const auto& [message, code] = result.error();
81 const auto aidlCode = utils::convert(code).value_or(ErrorStatus::GENERAL_FAILURE);
82 return ndk::ScopedAStatus::fromServiceSpecificErrorWithMessage(
83 static_cast<int32_t>(aidlCode), message.c_str());
84 }
85 return ndk::ScopedAStatus::ok();
86 }
87
88 } // namespace aidl::android::hardware::neuralnetworks::adapter
89