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 #ifndef ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_CONVERSIONS_H
18 #define ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_CONVERSIONS_H
19 
20 #include <aidl/android/hardware/neuralnetworks/BufferDesc.h>
21 #include <aidl/android/hardware/neuralnetworks/BufferRole.h>
22 #include <aidl/android/hardware/neuralnetworks/Capabilities.h>
23 #include <aidl/android/hardware/neuralnetworks/DataLocation.h>
24 #include <aidl/android/hardware/neuralnetworks/DeviceType.h>
25 #include <aidl/android/hardware/neuralnetworks/ErrorStatus.h>
26 #include <aidl/android/hardware/neuralnetworks/ExecutionPreference.h>
27 #include <aidl/android/hardware/neuralnetworks/Extension.h>
28 #include <aidl/android/hardware/neuralnetworks/ExtensionNameAndPrefix.h>
29 #include <aidl/android/hardware/neuralnetworks/ExtensionOperandTypeInformation.h>
30 #include <aidl/android/hardware/neuralnetworks/Memory.h>
31 #include <aidl/android/hardware/neuralnetworks/Model.h>
32 #include <aidl/android/hardware/neuralnetworks/Operand.h>
33 #include <aidl/android/hardware/neuralnetworks/OperandExtraParams.h>
34 #include <aidl/android/hardware/neuralnetworks/OperandLifeTime.h>
35 #include <aidl/android/hardware/neuralnetworks/OperandPerformance.h>
36 #include <aidl/android/hardware/neuralnetworks/OperandType.h>
37 #include <aidl/android/hardware/neuralnetworks/Operation.h>
38 #include <aidl/android/hardware/neuralnetworks/OperationType.h>
39 #include <aidl/android/hardware/neuralnetworks/OutputShape.h>
40 #include <aidl/android/hardware/neuralnetworks/PerformanceInfo.h>
41 #include <aidl/android/hardware/neuralnetworks/Priority.h>
42 #include <aidl/android/hardware/neuralnetworks/Request.h>
43 #include <aidl/android/hardware/neuralnetworks/RequestArgument.h>
44 #include <aidl/android/hardware/neuralnetworks/RequestMemoryPool.h>
45 #include <aidl/android/hardware/neuralnetworks/Subgraph.h>
46 #include <aidl/android/hardware/neuralnetworks/SymmPerChannelQuantParams.h>
47 #include <aidl/android/hardware/neuralnetworks/Timing.h>
48 
49 #include <android/binder_auto_utils.h>
50 #include <nnapi/Result.h>
51 #include <nnapi/Types.h>
52 #include <nnapi/hal/CommonUtils.h>
53 
54 #include <vector>
55 
56 namespace android::nn {
57 
58 GeneralResult<OperandType> unvalidatedConvert(const aidl_hal::OperandType& operandType);
59 GeneralResult<OperationType> unvalidatedConvert(const aidl_hal::OperationType& operationType);
60 GeneralResult<DeviceType> unvalidatedConvert(const aidl_hal::DeviceType& deviceType);
61 GeneralResult<Priority> unvalidatedConvert(const aidl_hal::Priority& priority);
62 GeneralResult<Capabilities> unvalidatedConvert(const aidl_hal::Capabilities& capabilities);
63 GeneralResult<Capabilities::OperandPerformance> unvalidatedConvert(
64         const aidl_hal::OperandPerformance& operandPerformance);
65 GeneralResult<Capabilities::PerformanceInfo> unvalidatedConvert(
66         const aidl_hal::PerformanceInfo& performanceInfo);
67 GeneralResult<DataLocation> unvalidatedConvert(const aidl_hal::DataLocation& location);
68 GeneralResult<Operand> unvalidatedConvert(const aidl_hal::Operand& operand);
69 GeneralResult<Operand::ExtraParams> unvalidatedConvert(
70         const std::optional<aidl_hal::OperandExtraParams>& optionalExtraParams);
71 GeneralResult<Operand::LifeTime> unvalidatedConvert(
72         const aidl_hal::OperandLifeTime& operandLifeTime);
73 GeneralResult<Operand::SymmPerChannelQuantParams> unvalidatedConvert(
74         const aidl_hal::SymmPerChannelQuantParams& symmPerChannelQuantParams);
75 GeneralResult<Operation> unvalidatedConvert(const aidl_hal::Operation& operation);
76 GeneralResult<Model> unvalidatedConvert(const aidl_hal::Model& model);
77 GeneralResult<Model::ExtensionNameAndPrefix> unvalidatedConvert(
78         const aidl_hal::ExtensionNameAndPrefix& extensionNameAndPrefix);
79 GeneralResult<Model::OperandValues> unvalidatedConvert(const std::vector<uint8_t>& operandValues);
80 GeneralResult<Model::Subgraph> unvalidatedConvert(const aidl_hal::Subgraph& subgraph);
81 GeneralResult<OutputShape> unvalidatedConvert(const aidl_hal::OutputShape& outputShape);
82 GeneralResult<MeasureTiming> unvalidatedConvert(bool measureTiming);
83 GeneralResult<SharedMemory> unvalidatedConvert(const aidl_hal::Memory& memory);
84 GeneralResult<Timing> unvalidatedConvert(const aidl_hal::Timing& timing);
85 GeneralResult<BufferDesc> unvalidatedConvert(const aidl_hal::BufferDesc& bufferDesc);
86 GeneralResult<BufferRole> unvalidatedConvert(const aidl_hal::BufferRole& bufferRole);
87 GeneralResult<Request> unvalidatedConvert(const aidl_hal::Request& request);
88 GeneralResult<Request::Argument> unvalidatedConvert(
89         const aidl_hal::RequestArgument& requestArgument);
90 GeneralResult<Request::MemoryPool> unvalidatedConvert(
91         const aidl_hal::RequestMemoryPool& memoryPool);
92 GeneralResult<ErrorStatus> unvalidatedConvert(const aidl_hal::ErrorStatus& errorStatus);
93 GeneralResult<ExecutionPreference> unvalidatedConvert(
94         const aidl_hal::ExecutionPreference& executionPreference);
95 GeneralResult<Extension> unvalidatedConvert(const aidl_hal::Extension& extension);
96 GeneralResult<Extension::OperandTypeInformation> unvalidatedConvert(
97         const aidl_hal::ExtensionOperandTypeInformation& operandTypeInformation);
98 GeneralResult<SharedHandle> unvalidatedConvert(
99         const ::aidl::android::hardware::common::NativeHandle& handle);
100 GeneralResult<SyncFence> unvalidatedConvert(const ndk::ScopedFileDescriptor& syncFence);
101 
102 GeneralResult<std::vector<Operation>> unvalidatedConvert(
103         const std::vector<aidl_hal::Operation>& operations);
104 
105 GeneralResult<Capabilities> convert(const aidl_hal::Capabilities& capabilities);
106 GeneralResult<DeviceType> convert(const aidl_hal::DeviceType& deviceType);
107 GeneralResult<ErrorStatus> convert(const aidl_hal::ErrorStatus& errorStatus);
108 GeneralResult<ExecutionPreference> convert(
109         const aidl_hal::ExecutionPreference& executionPreference);
110 GeneralResult<SharedMemory> convert(const aidl_hal::Memory& memory);
111 GeneralResult<Model> convert(const aidl_hal::Model& model);
112 GeneralResult<OperandType> convert(const aidl_hal::OperandType& operandType);
113 GeneralResult<Priority> convert(const aidl_hal::Priority& priority);
114 GeneralResult<Request> convert(const aidl_hal::Request& request);
115 GeneralResult<Timing> convert(const aidl_hal::Timing& timing);
116 GeneralResult<SyncFence> convert(const ndk::ScopedFileDescriptor& syncFence);
117 
118 GeneralResult<std::vector<Extension>> convert(const std::vector<aidl_hal::Extension>& extension);
119 GeneralResult<std::vector<SharedMemory>> convert(const std::vector<aidl_hal::Memory>& memories);
120 GeneralResult<std::vector<OutputShape>> convert(
121         const std::vector<aidl_hal::OutputShape>& outputShapes);
122 
123 GeneralResult<std::vector<uint32_t>> toUnsigned(const std::vector<int32_t>& vec);
124 
125 }  // namespace android::nn
126 
127 namespace aidl::android::hardware::neuralnetworks::utils {
128 
129 namespace nn = ::android::nn;
130 
131 nn::GeneralResult<std::vector<uint8_t>> unvalidatedConvert(const nn::CacheToken& cacheToken);
132 nn::GeneralResult<BufferDesc> unvalidatedConvert(const nn::BufferDesc& bufferDesc);
133 nn::GeneralResult<BufferRole> unvalidatedConvert(const nn::BufferRole& bufferRole);
134 nn::GeneralResult<bool> unvalidatedConvert(const nn::MeasureTiming& measureTiming);
135 nn::GeneralResult<Memory> unvalidatedConvert(const nn::SharedMemory& memory);
136 nn::GeneralResult<OutputShape> unvalidatedConvert(const nn::OutputShape& outputShape);
137 nn::GeneralResult<ErrorStatus> unvalidatedConvert(const nn::ErrorStatus& errorStatus);
138 nn::GeneralResult<ExecutionPreference> unvalidatedConvert(
139         const nn::ExecutionPreference& executionPreference);
140 nn::GeneralResult<OperandType> unvalidatedConvert(const nn::OperandType& operandType);
141 nn::GeneralResult<OperandLifeTime> unvalidatedConvert(const nn::Operand::LifeTime& operandLifeTime);
142 nn::GeneralResult<DataLocation> unvalidatedConvert(const nn::DataLocation& location);
143 nn::GeneralResult<std::optional<OperandExtraParams>> unvalidatedConvert(
144         const nn::Operand::ExtraParams& extraParams);
145 nn::GeneralResult<Operand> unvalidatedConvert(const nn::Operand& operand);
146 nn::GeneralResult<OperationType> unvalidatedConvert(const nn::OperationType& operationType);
147 nn::GeneralResult<Operation> unvalidatedConvert(const nn::Operation& operation);
148 nn::GeneralResult<Subgraph> unvalidatedConvert(const nn::Model::Subgraph& subgraph);
149 nn::GeneralResult<std::vector<uint8_t>> unvalidatedConvert(
150         const nn::Model::OperandValues& operandValues);
151 nn::GeneralResult<ExtensionNameAndPrefix> unvalidatedConvert(
152         const nn::Model::ExtensionNameAndPrefix& extensionNameToPrefix);
153 nn::GeneralResult<Model> unvalidatedConvert(const nn::Model& model);
154 nn::GeneralResult<Priority> unvalidatedConvert(const nn::Priority& priority);
155 nn::GeneralResult<Request> unvalidatedConvert(const nn::Request& request);
156 nn::GeneralResult<RequestArgument> unvalidatedConvert(const nn::Request::Argument& requestArgument);
157 nn::GeneralResult<RequestMemoryPool> unvalidatedConvert(const nn::Request::MemoryPool& memoryPool);
158 nn::GeneralResult<Timing> unvalidatedConvert(const nn::Timing& timing);
159 nn::GeneralResult<int64_t> unvalidatedConvert(const nn::Duration& duration);
160 nn::GeneralResult<int64_t> unvalidatedConvert(const nn::OptionalDuration& optionalDuration);
161 nn::GeneralResult<int64_t> unvalidatedConvert(const nn::OptionalTimePoint& optionalTimePoint);
162 nn::GeneralResult<ndk::ScopedFileDescriptor> unvalidatedConvert(const nn::SyncFence& syncFence);
163 nn::GeneralResult<common::NativeHandle> unvalidatedConvert(const nn::SharedHandle& sharedHandle);
164 nn::GeneralResult<ndk::ScopedFileDescriptor> unvalidatedConvertCache(
165         const nn::SharedHandle& handle);
166 
167 nn::GeneralResult<std::vector<uint8_t>> convert(const nn::CacheToken& cacheToken);
168 nn::GeneralResult<BufferDesc> convert(const nn::BufferDesc& bufferDesc);
169 nn::GeneralResult<bool> convert(const nn::MeasureTiming& measureTiming);
170 nn::GeneralResult<Memory> convert(const nn::SharedMemory& memory);
171 nn::GeneralResult<ErrorStatus> convert(const nn::ErrorStatus& errorStatus);
172 nn::GeneralResult<ExecutionPreference> convert(const nn::ExecutionPreference& executionPreference);
173 nn::GeneralResult<Model> convert(const nn::Model& model);
174 nn::GeneralResult<Priority> convert(const nn::Priority& priority);
175 nn::GeneralResult<Request> convert(const nn::Request& request);
176 nn::GeneralResult<Timing> convert(const nn::Timing& timing);
177 nn::GeneralResult<int64_t> convert(const nn::OptionalDuration& optionalDuration);
178 nn::GeneralResult<int64_t> convert(const nn::OptionalTimePoint& optionalTimePoint);
179 
180 nn::GeneralResult<std::vector<BufferRole>> convert(const std::vector<nn::BufferRole>& bufferRoles);
181 nn::GeneralResult<std::vector<OutputShape>> convert(
182         const std::vector<nn::OutputShape>& outputShapes);
183 nn::GeneralResult<std::vector<ndk::ScopedFileDescriptor>> convert(
184         const std::vector<nn::SharedHandle>& handles);
185 nn::GeneralResult<std::vector<ndk::ScopedFileDescriptor>> convert(
186         const std::vector<nn::SyncFence>& syncFences);
187 
188 nn::GeneralResult<std::vector<int32_t>> toSigned(const std::vector<uint32_t>& vec);
189 
190 }  // namespace aidl::android::hardware::neuralnetworks::utils
191 
192 #endif  // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_AIDL_CONVERSIONS_H
193