blob: 76a0b07d8605fbc5dcad3bf6795e353d1c194c0e [file] [log] [blame]
Lev Proleev6b6dfcd2020-11-11 18:28:50 +00001/*
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 Butlerf03ebd92021-03-25 15:27:38 -070019#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 Proleev900c28a2021-01-26 19:40:20 +000023#include <android/binder_status.h>
Lev Proleev6b6dfcd2020-11-11 18:28:50 +000024#include <nnapi/Result.h>
Michael Butlerf03ebd92021-03-25 15:27:38 -070025#include <nnapi/SharedMemory.h>
Lev Proleev6b6dfcd2020-11-11 18:28:50 +000026
27namespace aidl::android::hardware::neuralnetworks::utils {
Lev Proleevc185e882020-12-15 19:25:32 +000028namespace {
Lev Proleev6b6dfcd2020-11-11 18:28:50 +000029
Michael Butlerf03ebd92021-03-25 15:27:38 -070030nn::GeneralResult<ndk::ScopedFileDescriptor> clone(const ndk::ScopedFileDescriptor& fd);
31using utils::clone;
32
Lev Proleevc185e882020-12-15 19:25:32 +000033template <typename Type>
34nn::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
43template <typename Type>
Lev Proleev900c28a2021-01-26 19:40:20 +000044nn::GeneralResult<std::vector<Type>> clone(const std::vector<Type>& arguments) {
Lev Proleevc185e882020-12-15 19:25:32 +000045 return cloneVec(arguments);
46}
47
Michael Butlerf03ebd92021-03-25 15:27:38 -070048nn::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
53nn::GeneralResult<common::NativeHandle> clone(const common::NativeHandle& handle) {
54 return common::NativeHandle{
55 .fds = NN_TRY(cloneVec(handle.fds)),
56 .ints = handle.ints,
57 };
58}
59
Lev Proleevc185e882020-12-15 19:25:32 +000060} // namespace
61
Lev Proleev900c28a2021-01-26 19:40:20 +000062nn::GeneralResult<Memory> clone(const Memory& memory) {
Michael Butlerf03ebd92021-03-25 15:27:38 -070063 switch (memory.getTag()) {
64 case Memory::Tag::ashmem: {
65 const auto& ashmem = memory.get<Memory::Tag::ashmem>();
66 auto handle = common::Ashmem{
67 .fd = NN_TRY(clone(ashmem.fd)),
68 .size = ashmem.size,
69 };
70 return Memory::make<Memory::Tag::ashmem>(std::move(handle));
Lev Proleevc185e882020-12-15 19:25:32 +000071 }
Michael Butlerf03ebd92021-03-25 15:27:38 -070072 case Memory::Tag::mappableFile: {
73 const auto& memFd = memory.get<Memory::Tag::mappableFile>();
74 auto handle = common::MappableFile{
75 .length = memFd.length,
76 .prot = memFd.prot,
77 .fd = NN_TRY(clone(memFd.fd)),
78 .offset = memFd.offset,
79 };
80 return Memory::make<Memory::Tag::mappableFile>(std::move(handle));
81 }
82 case Memory::Tag::hardwareBuffer: {
83 const auto& hardwareBuffer = memory.get<Memory::Tag::hardwareBuffer>();
84 auto handle = graphics::common::HardwareBuffer{
85 .description = hardwareBuffer.description,
86 .handle = NN_TRY(clone(hardwareBuffer.handle)),
87 };
88 return Memory::make<Memory::Tag::hardwareBuffer>(std::move(handle));
89 }
Lev Proleevc185e882020-12-15 19:25:32 +000090 }
Jooyung Han0bdded62022-02-26 21:10:12 +090091 return (NN_ERROR() << "Unrecognized Memory::Tag: " << underlyingType(memory.getTag()))
Michael Butlerf03ebd92021-03-25 15:27:38 -070092 .
93 operator nn::GeneralResult<Memory>();
Lev Proleevc185e882020-12-15 19:25:32 +000094}
95
Lev Proleev900c28a2021-01-26 19:40:20 +000096nn::GeneralResult<RequestMemoryPool> clone(const RequestMemoryPool& requestPool) {
Lev Proleevc185e882020-12-15 19:25:32 +000097 using Tag = RequestMemoryPool::Tag;
98 switch (requestPool.getTag()) {
99 case Tag::pool:
100 return RequestMemoryPool::make<Tag::pool>(NN_TRY(clone(requestPool.get<Tag::pool>())));
101 case Tag::token:
102 return RequestMemoryPool::make<Tag::token>(requestPool.get<Tag::token>());
103 }
104 // Using explicit type conversion because std::variant inside the RequestMemoryPool confuses the
105 // compiler.
Jooyung Han0bdded62022-02-26 21:10:12 +0900106 return (NN_ERROR() << "Unrecognized request pool tag: " << underlyingType(requestPool.getTag()))
Lev Proleevc185e882020-12-15 19:25:32 +0000107 .
Lev Proleev900c28a2021-01-26 19:40:20 +0000108 operator nn::GeneralResult<RequestMemoryPool>();
Lev Proleevc185e882020-12-15 19:25:32 +0000109}
110
Lev Proleev900c28a2021-01-26 19:40:20 +0000111nn::GeneralResult<Request> clone(const Request& request) {
Lev Proleevc185e882020-12-15 19:25:32 +0000112 return Request{
113 .inputs = request.inputs,
114 .outputs = request.outputs,
115 .pools = NN_TRY(clone(request.pools)),
116 };
117}
118
Lev Proleev900c28a2021-01-26 19:40:20 +0000119nn::GeneralResult<Model> clone(const Model& model) {
Lev Proleevc185e882020-12-15 19:25:32 +0000120 return Model{
Lev Proleev6b6dfcd2020-11-11 18:28:50 +0000121 .main = model.main,
122 .referenced = model.referenced,
123 .operandValues = model.operandValues,
Lev Proleevc185e882020-12-15 19:25:32 +0000124 .pools = NN_TRY(clone(model.pools)),
Lev Proleev6b6dfcd2020-11-11 18:28:50 +0000125 .relaxComputationFloat32toFloat16 = model.relaxComputationFloat32toFloat16,
126 .extensionNameToPrefix = model.extensionNameToPrefix,
127 };
Lev Proleev6b6dfcd2020-11-11 18:28:50 +0000128}
129
Lev Proleev900c28a2021-01-26 19:40:20 +0000130nn::GeneralResult<void> handleTransportError(const ndk::ScopedAStatus& ret) {
131 if (ret.getStatus() == STATUS_DEAD_OBJECT) {
132 return nn::error(nn::ErrorStatus::DEAD_OBJECT)
133 << "Binder transaction returned STATUS_DEAD_OBJECT: " << ret.getDescription();
134 }
135 if (ret.isOk()) {
136 return {};
137 }
138 if (ret.getExceptionCode() != EX_SERVICE_SPECIFIC) {
139 return nn::error(nn::ErrorStatus::GENERAL_FAILURE)
140 << "Binder transaction returned exception: " << ret.getDescription();
141 }
142 return nn::error(static_cast<nn::ErrorStatus>(ret.getServiceSpecificError()))
143 << ret.getMessage();
144}
145
Lev Proleev6b6dfcd2020-11-11 18:28:50 +0000146} // namespace aidl::android::hardware::neuralnetworks::utils