Add utils for AIDL types conversions

Add conversions between canonical types and NNAPI AIDL interface types
that are needed for AIDL sample driver implementation.

Bug: 172922059
Test: VtsNeuralnetworksTargetTest
Change-Id: I02803302e02457e52c752114b47b94239eff20e9
diff --git a/neuralnetworks/aidl/utils/src/Utils.cpp b/neuralnetworks/aidl/utils/src/Utils.cpp
new file mode 100644
index 0000000..04aa0e9
--- /dev/null
+++ b/neuralnetworks/aidl/utils/src/Utils.cpp
@@ -0,0 +1,56 @@
+/*
+ * Copyright (C) 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "Utils.h"
+
+#include <nnapi/Result.h>
+
+namespace aidl::android::hardware::neuralnetworks::utils {
+
+using ::android::nn::GeneralResult;
+
+GeneralResult<Model> copyModel(const Model& model) {
+    Model newModel{
+            .main = model.main,
+            .referenced = model.referenced,
+            .operandValues = model.operandValues,
+            .pools = {},
+            .relaxComputationFloat32toFloat16 = model.relaxComputationFloat32toFloat16,
+            .extensionNameToPrefix = model.extensionNameToPrefix,
+    };
+    newModel.pools.reserve(model.pools.size());
+    for (const auto& pool : model.pools) {
+        common::NativeHandle nativeHandle;
+        nativeHandle.ints = pool.handle.ints;
+        nativeHandle.fds.reserve(pool.handle.fds.size());
+        for (const auto& fd : pool.handle.fds) {
+            const int newFd = dup(fd.get());
+            if (newFd == -1) {
+                return NN_ERROR() << "Couldn't dup a file descriptor.";
+            }
+            nativeHandle.fds.emplace_back(newFd);
+        }
+        Memory memory = {
+                .handle = std::move(nativeHandle),
+                .size = pool.size,
+                .name = pool.name,
+        };
+        newModel.pools.push_back(std::move(memory));
+    }
+    return newModel;
+}
+
+}  // namespace aidl::android::hardware::neuralnetworks::utils