Michael Butler | 3670c38 | 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 | |
Michael Butler | 7a655bb | 2020-12-13 23:06:06 -0800 | [diff] [blame] | 36 | // See hardware/interfaces/neuralnetworks/utils/README.md for more information on HIDL interface |
| 37 | // lifetimes across processes. |
| 38 | |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 39 | namespace android::hardware::neuralnetworks::V1_3::utils { |
| 40 | |
| 41 | nn::GeneralResult<std::shared_ptr<const Buffer>> Buffer::create( |
| 42 | sp<V1_3::IBuffer> buffer, nn::Request::MemoryDomainToken token) { |
| 43 | if (buffer == nullptr) { |
Michael Butler | 98ed9ba | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 44 | return NN_ERROR() << "V1_3::utils::Buffer::create must have non-null buffer"; |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 45 | } |
| 46 | if (token == static_cast<nn::Request::MemoryDomainToken>(0)) { |
Michael Butler | 98ed9ba | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 47 | return NN_ERROR() << "V1_3::utils::Buffer::create must have non-zero token"; |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | return std::make_shared<const Buffer>(PrivateConstructorTag{}, std::move(buffer), token); |
| 51 | } |
| 52 | |
| 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 | |
| 60 | nn::Request::MemoryDomainToken Buffer::getToken() const { |
| 61 | return kToken; |
| 62 | } |
| 63 | |
| 64 | nn::GeneralResult<void> Buffer::copyTo(const nn::Memory& dst) const { |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 65 | const auto hidlDst = NN_TRY(convert(dst)); |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 66 | |
| 67 | const auto ret = kBuffer->copyTo(hidlDst); |
Michael Butler | 61f508e | 2020-11-22 20:25:34 -0800 | [diff] [blame] | 68 | const auto status = HANDLE_TRANSPORT_FAILURE(ret); |
Michael Butler | 98ed9ba | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 69 | HANDLE_HAL_STATUS(status) << "IBuffer::copyTo failed with " << toString(status); |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 70 | |
| 71 | return {}; |
| 72 | } |
| 73 | |
| 74 | nn::GeneralResult<void> Buffer::copyFrom(const nn::Memory& src, |
| 75 | const nn::Dimensions& dimensions) const { |
Michael Butler | 32acc06 | 2020-11-22 19:36:30 -0800 | [diff] [blame] | 76 | const auto hidlSrc = NN_TRY(convert(src)); |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 77 | const auto hidlDimensions = hidl_vec<uint32_t>(dimensions); |
| 78 | |
| 79 | const auto ret = kBuffer->copyFrom(hidlSrc, hidlDimensions); |
Michael Butler | 61f508e | 2020-11-22 20:25:34 -0800 | [diff] [blame] | 80 | const auto status = HANDLE_TRANSPORT_FAILURE(ret); |
Michael Butler | 98ed9ba | 2020-12-06 21:50:59 -0800 | [diff] [blame] | 81 | HANDLE_HAL_STATUS(status) << "IBuffer::copyFrom failed with " << toString(status); |
Michael Butler | 3670c38 | 2020-08-06 23:22:35 -0700 | [diff] [blame] | 82 | |
| 83 | return {}; |
| 84 | } |
| 85 | |
| 86 | } // namespace android::hardware::neuralnetworks::V1_3::utils |