Lev Proleev | 6b6dfcd | 2020-11-11 18:28:50 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2021 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 "Utils.h" |
| 18 | |
Michael Butler | f03ebd9 | 2021-03-25 15:27:38 -0700 | [diff] [blame] | 19 | #include <aidl/android/hardware/common/Ashmem.h> |
| 20 | #include <aidl/android/hardware/common/MappableFile.h> |
| 21 | #include <aidl/android/hardware/graphics/common/HardwareBuffer.h> |
| 22 | #include <android/binder_auto_utils.h> |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 23 | #include <android/binder_status.h> |
Lev Proleev | 6b6dfcd | 2020-11-11 18:28:50 +0000 | [diff] [blame] | 24 | #include <nnapi/Result.h> |
Michael Butler | f03ebd9 | 2021-03-25 15:27:38 -0700 | [diff] [blame] | 25 | #include <nnapi/SharedMemory.h> |
Lev Proleev | 6b6dfcd | 2020-11-11 18:28:50 +0000 | [diff] [blame] | 26 | |
| 27 | namespace aidl::android::hardware::neuralnetworks::utils { |
Lev Proleev | c185e88 | 2020-12-15 19:25:32 +0000 | [diff] [blame] | 28 | namespace { |
Lev Proleev | 6b6dfcd | 2020-11-11 18:28:50 +0000 | [diff] [blame] | 29 | |
Michael Butler | f03ebd9 | 2021-03-25 15:27:38 -0700 | [diff] [blame] | 30 | nn::GeneralResult<ndk::ScopedFileDescriptor> clone(const ndk::ScopedFileDescriptor& fd); |
| 31 | using utils::clone; |
| 32 | |
Lev Proleev | c185e88 | 2020-12-15 19:25:32 +0000 | [diff] [blame] | 33 | template <typename Type> |
| 34 | nn::GeneralResult<std::vector<Type>> cloneVec(const std::vector<Type>& arguments) { |
| 35 | std::vector<Type> clonedObjects; |
| 36 | clonedObjects.reserve(arguments.size()); |
| 37 | for (const auto& argument : arguments) { |
| 38 | clonedObjects.push_back(NN_TRY(clone(argument))); |
| 39 | } |
| 40 | return clonedObjects; |
| 41 | } |
| 42 | |
| 43 | template <typename Type> |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 44 | nn::GeneralResult<std::vector<Type>> clone(const std::vector<Type>& arguments) { |
Lev Proleev | c185e88 | 2020-12-15 19:25:32 +0000 | [diff] [blame] | 45 | return cloneVec(arguments); |
| 46 | } |
| 47 | |
Michael Butler | f03ebd9 | 2021-03-25 15:27:38 -0700 | [diff] [blame] | 48 | nn::GeneralResult<ndk::ScopedFileDescriptor> clone(const ndk::ScopedFileDescriptor& fd) { |
| 49 | auto duplicatedFd = NN_TRY(nn::dupFd(fd.get())); |
| 50 | return ndk::ScopedFileDescriptor(duplicatedFd.release()); |
| 51 | } |
| 52 | |
| 53 | nn::GeneralResult<common::NativeHandle> clone(const common::NativeHandle& handle) { |
Michael Butler | ad643b6 | 2022-04-25 22:36:51 -0700 | [diff] [blame^] | 54 | auto fds = NN_TRY(cloneVec(handle.fds)); |
Michael Butler | f03ebd9 | 2021-03-25 15:27:38 -0700 | [diff] [blame] | 55 | return common::NativeHandle{ |
Michael Butler | ad643b6 | 2022-04-25 22:36:51 -0700 | [diff] [blame^] | 56 | .fds = std::move(fds), |
Michael Butler | f03ebd9 | 2021-03-25 15:27:38 -0700 | [diff] [blame] | 57 | .ints = handle.ints, |
| 58 | }; |
| 59 | } |
| 60 | |
Lev Proleev | c185e88 | 2020-12-15 19:25:32 +0000 | [diff] [blame] | 61 | } // namespace |
| 62 | |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 63 | nn::GeneralResult<Memory> clone(const Memory& memory) { |
Michael Butler | f03ebd9 | 2021-03-25 15:27:38 -0700 | [diff] [blame] | 64 | switch (memory.getTag()) { |
| 65 | case Memory::Tag::ashmem: { |
| 66 | const auto& ashmem = memory.get<Memory::Tag::ashmem>(); |
Michael Butler | ad643b6 | 2022-04-25 22:36:51 -0700 | [diff] [blame^] | 67 | auto fd = NN_TRY(clone(ashmem.fd)); |
Michael Butler | f03ebd9 | 2021-03-25 15:27:38 -0700 | [diff] [blame] | 68 | auto handle = common::Ashmem{ |
Michael Butler | ad643b6 | 2022-04-25 22:36:51 -0700 | [diff] [blame^] | 69 | .fd = std::move(fd), |
Michael Butler | f03ebd9 | 2021-03-25 15:27:38 -0700 | [diff] [blame] | 70 | .size = ashmem.size, |
| 71 | }; |
| 72 | return Memory::make<Memory::Tag::ashmem>(std::move(handle)); |
Lev Proleev | c185e88 | 2020-12-15 19:25:32 +0000 | [diff] [blame] | 73 | } |
Michael Butler | f03ebd9 | 2021-03-25 15:27:38 -0700 | [diff] [blame] | 74 | case Memory::Tag::mappableFile: { |
| 75 | const auto& memFd = memory.get<Memory::Tag::mappableFile>(); |
Michael Butler | ad643b6 | 2022-04-25 22:36:51 -0700 | [diff] [blame^] | 76 | auto fd = NN_TRY(clone(memFd.fd)); |
Michael Butler | f03ebd9 | 2021-03-25 15:27:38 -0700 | [diff] [blame] | 77 | auto handle = common::MappableFile{ |
| 78 | .length = memFd.length, |
| 79 | .prot = memFd.prot, |
Michael Butler | ad643b6 | 2022-04-25 22:36:51 -0700 | [diff] [blame^] | 80 | .fd = std::move(fd), |
Michael Butler | f03ebd9 | 2021-03-25 15:27:38 -0700 | [diff] [blame] | 81 | .offset = memFd.offset, |
| 82 | }; |
| 83 | return Memory::make<Memory::Tag::mappableFile>(std::move(handle)); |
| 84 | } |
| 85 | case Memory::Tag::hardwareBuffer: { |
| 86 | const auto& hardwareBuffer = memory.get<Memory::Tag::hardwareBuffer>(); |
Michael Butler | ad643b6 | 2022-04-25 22:36:51 -0700 | [diff] [blame^] | 87 | auto handle = NN_TRY(clone(hardwareBuffer.handle)); |
| 88 | auto ahwbHandle = graphics::common::HardwareBuffer{ |
Michael Butler | f03ebd9 | 2021-03-25 15:27:38 -0700 | [diff] [blame] | 89 | .description = hardwareBuffer.description, |
Michael Butler | ad643b6 | 2022-04-25 22:36:51 -0700 | [diff] [blame^] | 90 | .handle = std::move(handle), |
Michael Butler | f03ebd9 | 2021-03-25 15:27:38 -0700 | [diff] [blame] | 91 | }; |
Michael Butler | ad643b6 | 2022-04-25 22:36:51 -0700 | [diff] [blame^] | 92 | return Memory::make<Memory::Tag::hardwareBuffer>(std::move(ahwbHandle)); |
Michael Butler | f03ebd9 | 2021-03-25 15:27:38 -0700 | [diff] [blame] | 93 | } |
Lev Proleev | c185e88 | 2020-12-15 19:25:32 +0000 | [diff] [blame] | 94 | } |
Jooyung Han | 0bdded6 | 2022-02-26 21:10:12 +0900 | [diff] [blame] | 95 | return (NN_ERROR() << "Unrecognized Memory::Tag: " << underlyingType(memory.getTag())) |
Michael Butler | f03ebd9 | 2021-03-25 15:27:38 -0700 | [diff] [blame] | 96 | . |
| 97 | operator nn::GeneralResult<Memory>(); |
Lev Proleev | c185e88 | 2020-12-15 19:25:32 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 100 | nn::GeneralResult<RequestMemoryPool> clone(const RequestMemoryPool& requestPool) { |
Lev Proleev | c185e88 | 2020-12-15 19:25:32 +0000 | [diff] [blame] | 101 | using Tag = RequestMemoryPool::Tag; |
| 102 | switch (requestPool.getTag()) { |
| 103 | case Tag::pool: |
| 104 | return RequestMemoryPool::make<Tag::pool>(NN_TRY(clone(requestPool.get<Tag::pool>()))); |
| 105 | case Tag::token: |
| 106 | return RequestMemoryPool::make<Tag::token>(requestPool.get<Tag::token>()); |
| 107 | } |
| 108 | // Using explicit type conversion because std::variant inside the RequestMemoryPool confuses the |
| 109 | // compiler. |
Jooyung Han | 0bdded6 | 2022-02-26 21:10:12 +0900 | [diff] [blame] | 110 | return (NN_ERROR() << "Unrecognized request pool tag: " << underlyingType(requestPool.getTag())) |
Lev Proleev | c185e88 | 2020-12-15 19:25:32 +0000 | [diff] [blame] | 111 | . |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 112 | operator nn::GeneralResult<RequestMemoryPool>(); |
Lev Proleev | c185e88 | 2020-12-15 19:25:32 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 115 | nn::GeneralResult<Request> clone(const Request& request) { |
Michael Butler | ad643b6 | 2022-04-25 22:36:51 -0700 | [diff] [blame^] | 116 | auto pools = NN_TRY(clone(request.pools)); |
Lev Proleev | c185e88 | 2020-12-15 19:25:32 +0000 | [diff] [blame] | 117 | return Request{ |
| 118 | .inputs = request.inputs, |
| 119 | .outputs = request.outputs, |
Michael Butler | ad643b6 | 2022-04-25 22:36:51 -0700 | [diff] [blame^] | 120 | .pools = std::move(pools), |
Lev Proleev | c185e88 | 2020-12-15 19:25:32 +0000 | [diff] [blame] | 121 | }; |
| 122 | } |
| 123 | |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 124 | nn::GeneralResult<Model> clone(const Model& model) { |
Michael Butler | ad643b6 | 2022-04-25 22:36:51 -0700 | [diff] [blame^] | 125 | auto pools = NN_TRY(clone(model.pools)); |
Lev Proleev | c185e88 | 2020-12-15 19:25:32 +0000 | [diff] [blame] | 126 | return Model{ |
Lev Proleev | 6b6dfcd | 2020-11-11 18:28:50 +0000 | [diff] [blame] | 127 | .main = model.main, |
| 128 | .referenced = model.referenced, |
| 129 | .operandValues = model.operandValues, |
Michael Butler | ad643b6 | 2022-04-25 22:36:51 -0700 | [diff] [blame^] | 130 | .pools = std::move(pools), |
Lev Proleev | 6b6dfcd | 2020-11-11 18:28:50 +0000 | [diff] [blame] | 131 | .relaxComputationFloat32toFloat16 = model.relaxComputationFloat32toFloat16, |
| 132 | .extensionNameToPrefix = model.extensionNameToPrefix, |
| 133 | }; |
Lev Proleev | 6b6dfcd | 2020-11-11 18:28:50 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Lev Proleev | 900c28a | 2021-01-26 19:40:20 +0000 | [diff] [blame] | 136 | nn::GeneralResult<void> handleTransportError(const ndk::ScopedAStatus& ret) { |
| 137 | if (ret.getStatus() == STATUS_DEAD_OBJECT) { |
| 138 | return nn::error(nn::ErrorStatus::DEAD_OBJECT) |
| 139 | << "Binder transaction returned STATUS_DEAD_OBJECT: " << ret.getDescription(); |
| 140 | } |
| 141 | if (ret.isOk()) { |
| 142 | return {}; |
| 143 | } |
| 144 | if (ret.getExceptionCode() != EX_SERVICE_SPECIFIC) { |
| 145 | return nn::error(nn::ErrorStatus::GENERAL_FAILURE) |
| 146 | << "Binder transaction returned exception: " << ret.getDescription(); |
| 147 | } |
| 148 | return nn::error(static_cast<nn::ErrorStatus>(ret.getServiceSpecificError())) |
| 149 | << ret.getMessage(); |
| 150 | } |
| 151 | |
Lev Proleev | 6b6dfcd | 2020-11-11 18:28:50 +0000 | [diff] [blame] | 152 | } // namespace aidl::android::hardware::neuralnetworks::utils |