• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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 <android/hardware/neuralnetworks/1.0/types.h>
20 #include <android/hardware/neuralnetworks/1.1/types.h>
21 #include <android/hardware/neuralnetworks/1.2/types.h>
22 #include <android/hardware/neuralnetworks/1.3/IBuffer.h>
23 #include <android/hardware/neuralnetworks/1.3/types.h>
24 #include <nnapi/IPreparedModel.h>
25 #include <nnapi/Result.h>
26 #include <nnapi/Types.h>
27 #include <nnapi/hal/1.0/Conversions.h>
28 #include <nnapi/hal/1.0/HandleError.h>
29 
30 #include "Conversions.h"
31 #include "Utils.h"
32 
33 #include <memory>
34 #include <utility>
35 
36 // See hardware/interfaces/neuralnetworks/utils/README.md for more information on HIDL interface
37 // lifetimes across processes.
38 
39 namespace android::hardware::neuralnetworks::V1_3::utils {
40 
create(sp<V1_3::IBuffer> buffer,nn::Request::MemoryDomainToken token)41 nn::GeneralResult<std::shared_ptr<const Buffer>> Buffer::create(
42         sp<V1_3::IBuffer> buffer, nn::Request::MemoryDomainToken token) {
43     if (buffer == nullptr) {
44         return NN_ERROR() << "V1_3::utils::Buffer::create must have non-null buffer";
45     }
46     if (token == static_cast<nn::Request::MemoryDomainToken>(0)) {
47         return NN_ERROR() << "V1_3::utils::Buffer::create must have non-zero token";
48     }
49 
50     return std::make_shared<const Buffer>(PrivateConstructorTag{}, std::move(buffer), token);
51 }
52 
Buffer(PrivateConstructorTag,sp<V1_3::IBuffer> buffer,nn::Request::MemoryDomainToken token)53 Buffer::Buffer(PrivateConstructorTag /*tag*/, sp<V1_3::IBuffer> buffer,
54                nn::Request::MemoryDomainToken token)
55     : kBuffer(std::move(buffer)), kToken(token) {
56     CHECK(kBuffer != nullptr);
57     CHECK(kToken != static_cast<nn::Request::MemoryDomainToken>(0));
58 }
59 
getToken() const60 nn::Request::MemoryDomainToken Buffer::getToken() const {
61     return kToken;
62 }
63 
copyTo(const nn::SharedMemory & dst) const64 nn::GeneralResult<void> Buffer::copyTo(const nn::SharedMemory& dst) const {
65     const auto hidlDst = NN_TRY(convert(dst));
66 
67     const auto ret = kBuffer->copyTo(hidlDst);
68     const auto status = HANDLE_TRANSPORT_FAILURE(ret);
69     HANDLE_STATUS_HIDL(status) << "IBuffer::copyTo failed with " << toString(status);
70 
71     return {};
72 }
73 
copyFrom(const nn::SharedMemory & src,const nn::Dimensions & dimensions) const74 nn::GeneralResult<void> Buffer::copyFrom(const nn::SharedMemory& src,
75                                          const nn::Dimensions& dimensions) const {
76     const auto hidlSrc = NN_TRY(convert(src));
77     const auto hidlDimensions = hidl_vec<uint32_t>(dimensions);
78 
79     const auto ret = kBuffer->copyFrom(hidlSrc, hidlDimensions);
80     const auto status = HANDLE_TRANSPORT_FAILURE(ret);
81     HANDLE_STATUS_HIDL(status) << "IBuffer::copyFrom failed with " << toString(status);
82 
83     return {};
84 }
85 
86 }  // namespace android::hardware::neuralnetworks::V1_3::utils
87