Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 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/HandleError.h> |
| 29 | |
| 30 | #include "Conversions.h" |
| 31 | #include "Utils.h" |
| 32 | |
| 33 | #include <memory> |
| 34 | #include <utility> |
| 35 | |
| 36 | namespace android::hardware::neuralnetworks::V1_3::utils { |
| 37 | |
| 38 | nn::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 | |
| 52 | Buffer::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 | |
| 59 | nn::Request::MemoryDomainToken Buffer::getToken() const { |
| 60 | return kToken; |
| 61 | } |
| 62 | |
| 63 | nn::GeneralResult<void> Buffer::copyTo(const nn::Memory& dst) const { |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame^] | 64 | const auto hidlDst = NN_TRY(convert(dst)); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 65 | |
| 66 | const auto ret = kBuffer->copyTo(hidlDst); |
| 67 | const auto status = NN_TRY(hal::utils::handleTransportError(ret)); |
| 68 | if (status != ErrorStatus::NONE) { |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame^] | 69 | const auto canonical = nn::convert(status).value_or(nn::ErrorStatus::GENERAL_FAILURE); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 70 | return NN_ERROR(canonical) << "IBuffer::copyTo failed with " << toString(status); |
| 71 | } |
| 72 | |
| 73 | return {}; |
| 74 | } |
| 75 | |
| 76 | nn::GeneralResult<void> Buffer::copyFrom(const nn::Memory& src, |
| 77 | const nn::Dimensions& dimensions) const { |
Michael Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame^] | 78 | const auto hidlSrc = NN_TRY(convert(src)); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 79 | 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 Butler | 6547b2a | 2020-11-22 19:36:30 -0800 | [diff] [blame^] | 84 | const auto canonical = nn::convert(status).value_or(nn::ErrorStatus::GENERAL_FAILURE); |
Michael Butler | 4b276a7 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 85 | return NN_ERROR(canonical) << "IBuffer::copyFrom failed with " << toString(status); |
| 86 | } |
| 87 | |
| 88 | return {}; |
| 89 | } |
| 90 | |
| 91 | } // namespace android::hardware::neuralnetworks::V1_3::utils |