blob: a880031ad7e9f5ced2b5f9ce207c7d634fb67110 [file] [log] [blame]
Michael Butler4b276a72020-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
36namespace android::hardware::neuralnetworks::V1_3::utils {
37
38nn::GeneralResult<std::shared_ptr<const Buffer>> Buffer::create(
39 sp<V1_3::IBuffer> buffer, nn::Request::MemoryDomainToken token) {
40 if (buffer == nullptr) {
41 return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT)
42 << "V1_3::utils::Buffer::create must have non-null buffer";
43 }
44 if (token == static_cast<nn::Request::MemoryDomainToken>(0)) {
45 return NN_ERROR(nn::ErrorStatus::INVALID_ARGUMENT)
46 << "V1_3::utils::Buffer::create must have non-zero token";
47 }
48
49 return std::make_shared<const Buffer>(PrivateConstructorTag{}, std::move(buffer), token);
50}
51
52Buffer::Buffer(PrivateConstructorTag /*tag*/, sp<V1_3::IBuffer> buffer,
53 nn::Request::MemoryDomainToken token)
54 : kBuffer(std::move(buffer)), kToken(token) {
55 CHECK(kBuffer != nullptr);
56 CHECK(kToken != static_cast<nn::Request::MemoryDomainToken>(0));
57}
58
59nn::Request::MemoryDomainToken Buffer::getToken() const {
60 return kToken;
61}
62
63nn::GeneralResult<void> Buffer::copyTo(const nn::Memory& dst) const {
Michael Butler6547b2a2020-11-22 19:36:30 -080064 const auto hidlDst = NN_TRY(convert(dst));
Michael Butler4b276a72020-08-06 23:22:35 -070065
66 const auto ret = kBuffer->copyTo(hidlDst);
67 const auto status = NN_TRY(hal::utils::handleTransportError(ret));
68 if (status != ErrorStatus::NONE) {
Michael Butler6547b2a2020-11-22 19:36:30 -080069 const auto canonical = nn::convert(status).value_or(nn::ErrorStatus::GENERAL_FAILURE);
Michael Butler4b276a72020-08-06 23:22:35 -070070 return NN_ERROR(canonical) << "IBuffer::copyTo failed with " << toString(status);
71 }
72
73 return {};
74}
75
76nn::GeneralResult<void> Buffer::copyFrom(const nn::Memory& src,
77 const nn::Dimensions& dimensions) const {
Michael Butler6547b2a2020-11-22 19:36:30 -080078 const auto hidlSrc = NN_TRY(convert(src));
Michael Butler4b276a72020-08-06 23:22:35 -070079 const auto hidlDimensions = hidl_vec<uint32_t>(dimensions);
80
81 const auto ret = kBuffer->copyFrom(hidlSrc, hidlDimensions);
82 const auto status = NN_TRY(hal::utils::handleTransportError(ret));
83 if (status != ErrorStatus::NONE) {
Michael Butler6547b2a2020-11-22 19:36:30 -080084 const auto canonical = nn::convert(status).value_or(nn::ErrorStatus::GENERAL_FAILURE);
Michael Butler4b276a72020-08-06 23:22:35 -070085 return NN_ERROR(canonical) << "IBuffer::copyFrom failed with " << toString(status);
86 }
87
88 return {};
89}
90
91} // namespace android::hardware::neuralnetworks::V1_3::utils