blob: 614033e2684bd5fb85c554ad108a9f6e0aabeb13 [file] [log] [blame]
Michael Butler3670c382020-08-06 23:22:35 -07001/*
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/HandleError.h>
29
30#include "Conversions.h"
31#include "Utils.h"
32
33#include <memory>
34#include <utility>
35
Michael Butler7a655bb2020-12-13 23:06:06 -080036// See hardware/interfaces/neuralnetworks/utils/README.md for more information on HIDL interface
37// lifetimes across processes.
38
Michael Butler3670c382020-08-06 23:22:35 -070039namespace android::hardware::neuralnetworks::V1_3::utils {
40
41nn::GeneralResult<std::shared_ptr<const Buffer>> Buffer::create(
42 sp<V1_3::IBuffer> buffer, nn::Request::MemoryDomainToken token) {
43 if (buffer == nullptr) {
Michael Butler98ed9ba2020-12-06 21:50:59 -080044 return NN_ERROR() << "V1_3::utils::Buffer::create must have non-null buffer";
Michael Butler3670c382020-08-06 23:22:35 -070045 }
46 if (token == static_cast<nn::Request::MemoryDomainToken>(0)) {
Michael Butler98ed9ba2020-12-06 21:50:59 -080047 return NN_ERROR() << "V1_3::utils::Buffer::create must have non-zero token";
Michael Butler3670c382020-08-06 23:22:35 -070048 }
49
50 return std::make_shared<const Buffer>(PrivateConstructorTag{}, std::move(buffer), token);
51}
52
53Buffer::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
60nn::Request::MemoryDomainToken Buffer::getToken() const {
61 return kToken;
62}
63
64nn::GeneralResult<void> Buffer::copyTo(const nn::Memory& dst) const {
Michael Butler32acc062020-11-22 19:36:30 -080065 const auto hidlDst = NN_TRY(convert(dst));
Michael Butler3670c382020-08-06 23:22:35 -070066
67 const auto ret = kBuffer->copyTo(hidlDst);
Michael Butler61f508e2020-11-22 20:25:34 -080068 const auto status = HANDLE_TRANSPORT_FAILURE(ret);
Michael Butler98ed9ba2020-12-06 21:50:59 -080069 HANDLE_HAL_STATUS(status) << "IBuffer::copyTo failed with " << toString(status);
Michael Butler3670c382020-08-06 23:22:35 -070070
71 return {};
72}
73
74nn::GeneralResult<void> Buffer::copyFrom(const nn::Memory& src,
75 const nn::Dimensions& dimensions) const {
Michael Butler32acc062020-11-22 19:36:30 -080076 const auto hidlSrc = NN_TRY(convert(src));
Michael Butler3670c382020-08-06 23:22:35 -070077 const auto hidlDimensions = hidl_vec<uint32_t>(dimensions);
78
79 const auto ret = kBuffer->copyFrom(hidlSrc, hidlDimensions);
Michael Butler61f508e2020-11-22 20:25:34 -080080 const auto status = HANDLE_TRANSPORT_FAILURE(ret);
Michael Butler98ed9ba2020-12-06 21:50:59 -080081 HANDLE_HAL_STATUS(status) << "IBuffer::copyFrom failed with " << toString(status);
Michael Butler3670c382020-08-06 23:22:35 -070082
83 return {};
84}
85
86} // namespace android::hardware::neuralnetworks::V1_3::utils