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 <nnapi/IPreparedModel.h>
20 #include <nnapi/Result.h>
21 #include <nnapi/Types.h>
22
23 #include "Conversions.h"
24 #include "Utils.h"
25 #include "nnapi/hal/aidl/Conversions.h"
26
27 #include <memory>
28 #include <utility>
29
30 // See hardware/interfaces/neuralnetworks/utils/README.md for more information on AIDL interface
31 // lifetimes across processes.
32
33 namespace aidl::android::hardware::neuralnetworks::utils {
34
create(std::shared_ptr<aidl_hal::IBuffer> buffer,nn::Request::MemoryDomainToken token)35 nn::GeneralResult<std::shared_ptr<const Buffer>> Buffer::create(
36 std::shared_ptr<aidl_hal::IBuffer> buffer, nn::Request::MemoryDomainToken token) {
37 if (buffer == nullptr) {
38 return NN_ERROR() << "aidl_hal::utils::Buffer::create must have non-null buffer";
39 }
40 if (token == static_cast<nn::Request::MemoryDomainToken>(0)) {
41 return NN_ERROR() << "aidl_hal::utils::Buffer::create must have non-zero token";
42 }
43
44 return std::make_shared<const Buffer>(PrivateConstructorTag{}, std::move(buffer), token);
45 }
46
Buffer(PrivateConstructorTag,std::shared_ptr<aidl_hal::IBuffer> buffer,nn::Request::MemoryDomainToken token)47 Buffer::Buffer(PrivateConstructorTag /*tag*/, std::shared_ptr<aidl_hal::IBuffer> buffer,
48 nn::Request::MemoryDomainToken token)
49 : kBuffer(std::move(buffer)), kToken(token) {
50 CHECK(kBuffer != nullptr);
51 CHECK(kToken != static_cast<nn::Request::MemoryDomainToken>(0));
52 }
53
getToken() const54 nn::Request::MemoryDomainToken Buffer::getToken() const {
55 return kToken;
56 }
57
copyTo(const nn::SharedMemory & dst) const58 nn::GeneralResult<void> Buffer::copyTo(const nn::SharedMemory& dst) const {
59 const auto aidlDst = NN_TRY(convert(dst));
60
61 const auto ret = kBuffer->copyTo(aidlDst);
62 HANDLE_ASTATUS(ret) << "IBuffer::copyTo failed";
63
64 return {};
65 }
66
copyFrom(const nn::SharedMemory & src,const nn::Dimensions & dimensions) const67 nn::GeneralResult<void> Buffer::copyFrom(const nn::SharedMemory& src,
68 const nn::Dimensions& dimensions) const {
69 const auto aidlSrc = NN_TRY(convert(src));
70 const auto aidlDimensions = NN_TRY(toSigned(dimensions));
71
72 const auto ret = kBuffer->copyFrom(aidlSrc, aidlDimensions);
73 HANDLE_ASTATUS(ret) << "IBuffer::copyFrom failed";
74
75 return {};
76 }
77
78 } // namespace aidl::android::hardware::neuralnetworks::utils
79