2 * Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 using System.Runtime.InteropServices;
19 using Tizen.MachineLearning.Inference;
21 internal static partial class Interop
23 internal static partial class Libraries
25 public const string Nnstreamer = "libcapi-nnstreamer.so.0";
28 internal static partial class SingleShot
30 /* int ml_single_open (ml_single_h *single, const char *model, const ml_tensors_info_h input_info, const ml_tensors_info_h output_info, ml_nnfw_type_e nnfw, ml_nnfw_hw_e hw) */
31 [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_single_open", CallingConvention = CallingConvention.Cdecl)]
32 internal static extern NNStreamerError OpenSingle(out IntPtr single_handle, string model_path, IntPtr input_info, IntPtr output_info, NNFWType nn_type, HWType hw_type);
34 /* int ml_single_close (ml_single_h single) */
35 [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_single_close", CallingConvention = CallingConvention.Cdecl)]
36 internal static extern NNStreamerError CloseSingle(IntPtr single_handle);
38 /* int ml_single_invoke (ml_single_h single, const ml_tensors_data_h input, ml_tensors_data_h *output) */
39 [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_single_invoke", CallingConvention = CallingConvention.Cdecl)]
40 internal static extern NNStreamerError InvokeSingle(IntPtr single_handle, IntPtr input_data, out IntPtr output_data);
42 /* int ml_single_invoke_dynamic (ml_single_h single, const ml_tensors_data_h input, const ml_tensors_info_h in_info, ml_tensors_data_h * output, ml_tensors_info_h * out_info) */
43 [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_single_invoke_dynamic", CallingConvention = CallingConvention.Cdecl)]
44 internal static extern NNStreamerError InvokeSingleDynamic(IntPtr single_handle, IntPtr input_data, IntPtr input_info, out IntPtr output_data, out IntPtr output_info);
46 /* int ml_single_get_input_info (ml_single_h single, ml_tensors_info_h *info) */
47 [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_single_get_input_info", CallingConvention = CallingConvention.Cdecl)]
48 internal static extern NNStreamerError GetInputTensorsInfo(IntPtr single_handle, out IntPtr input_info);
50 /* int ml_single_get_output_info (ml_single_h single, ml_tensors_info_h *info) */
51 [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_single_get_output_info", CallingConvention = CallingConvention.Cdecl)]
52 internal static extern NNStreamerError GetOutputTensorsInfo(IntPtr single_handle, out IntPtr output_info);
54 /* int ml_single_set_input_info (ml_single_h single, const ml_tensors_info_h info) */
55 [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_single_set_input_info", CallingConvention = CallingConvention.Cdecl)]
56 internal static extern NNStreamerError SetInputInfo(IntPtr single_handle, IntPtr in_handle);
58 /* int ml_single_set_timeout (ml_single_h single, unsigned int timeout)*/
59 [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_single_set_timeout", CallingConvention = CallingConvention.Cdecl)]
60 internal static extern NNStreamerError SetTimeout(IntPtr single_handle, int time_ms);
62 /* int ml_single_set_property (ml_single_h single, const char *name, const char *value) */
63 [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_single_set_property", CallingConvention = CallingConvention.Cdecl)]
64 internal static extern NNStreamerError SetValue(IntPtr single_handle, string name, string value);
66 /* int ml_single_get_property (ml_single_h single, const char *name, char **value) */
67 [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_single_get_property", CallingConvention = CallingConvention.Cdecl)]
68 internal static extern NNStreamerError GetValue(IntPtr single_handle, string name, out IntPtr value);
71 internal static partial class Util
73 /* int ml_tensors_info_create (ml_tensors_info_h *info) */
74 [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_tensors_info_create", CallingConvention = CallingConvention.Cdecl)]
75 internal static extern NNStreamerError CreateTensorsInfo(out IntPtr info);
77 /* int ml_tensors_info_destroy (ml_tensors_info_h info) */
78 [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_tensors_info_destroy", CallingConvention = CallingConvention.Cdecl)]
79 internal static extern NNStreamerError DestroyTensorsInfo(IntPtr info);
81 /* int ml_tensors_info_set_count (ml_tensors_info_h info, unsigned int count) */
82 [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_tensors_info_set_count", CallingConvention = CallingConvention.Cdecl)]
83 internal static extern NNStreamerError SetTensorsCount(IntPtr info, int count);
85 /* int ml_tensors_info_get_count (ml_tensors_info_h info, unsigned int *count) */
86 [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_tensors_info_get_count", CallingConvention = CallingConvention.Cdecl)]
87 internal static extern NNStreamerError GetTensorsCount(IntPtr info, out int count);
89 /* int ml_tensors_info_set_tensor_name (ml_tensors_info_h info, unsigned int index, const char *name) */
90 [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_tensors_info_set_tensor_name", CallingConvention = CallingConvention.Cdecl)]
91 internal static extern NNStreamerError SetTensorName(IntPtr info, int index, string name);
93 /* int ml_tensors_info_get_tensor_name (ml_tensors_info_h info, unsigned int index, char **name) */
94 [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_tensors_info_get_tensor_name", CallingConvention = CallingConvention.Cdecl)]
95 internal static extern NNStreamerError GetTensorName(IntPtr info, int index, out string name);
97 /* int ml_tensors_info_set_tensor_type (ml_tensors_info_h info, unsigned int index, const ml_tensor_type_e type) */
98 [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_tensors_info_set_tensor_type", CallingConvention = CallingConvention.Cdecl)]
99 internal static extern NNStreamerError SetTensorType(IntPtr info, int index, TensorType type);
101 /* int ml_tensors_info_get_tensor_type (ml_tensors_info_h info, unsigned int index, ml_tensor_type_e *type) */
102 [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_tensors_info_get_tensor_type", CallingConvention = CallingConvention.Cdecl)]
103 internal static extern NNStreamerError GetTensorType(IntPtr info, int index, out TensorType type);
105 /* int ml_tensors_info_set_tensor_dimension (ml_tensors_info_h info, unsigned int index, const ml_tensor_dimension dimension) */
106 [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_tensors_info_set_tensor_dimension", CallingConvention = CallingConvention.Cdecl)]
107 internal static extern NNStreamerError SetTensorDimension(IntPtr info, int index, int[] dimension);
109 /* int ml_tensors_info_get_tensor_dimension (ml_tensors_info_h info, unsigned int index, ml_tensor_dimension dimension) */
110 [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_tensors_info_get_tensor_dimension", CallingConvention = CallingConvention.Cdecl)]
111 internal static extern NNStreamerError GetTensorDimension(IntPtr info, int index, [In, Out] uint[] dimension);
113 /* int ml_tensors_data_create (const ml_tensors_info_h info, ml_tensors_data_h *data) */
114 [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_tensors_data_create", CallingConvention = CallingConvention.Cdecl)]
115 internal static extern NNStreamerError CreateTensorsData(IntPtr info, out IntPtr data);
117 /* int ml_tensors_data_destroy (ml_tensors_data_h data) */
118 [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_tensors_data_destroy", CallingConvention = CallingConvention.Cdecl)]
119 internal static extern NNStreamerError DestroyTensorsData(IntPtr data);
121 /* int ml_tensors_data_get_tensor_data (ml_tensors_data_h data, unsigned int index, void **raw_data, size_t *data_size) */
122 [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_tensors_data_get_tensor_data", CallingConvention = CallingConvention.Cdecl)]
123 internal static extern NNStreamerError GetTensorData(IntPtr data, int index, out IntPtr raw_data, out int data_size);
125 /* int ml_tensors_data_set_tensor_data (ml_tensors_data_h data, unsigned int index, const void *raw_data, const size_t data_size) */
126 [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_tensors_data_set_tensor_data", CallingConvention = CallingConvention.Cdecl)]
127 internal static extern NNStreamerError SetTensorData(IntPtr data, int index, byte[] raw_data, int data_size);
129 /* int ml_check_nnfw_availability (ml_nnfw_type_e nnfw, ml_nnfw_hw_e hw, bool *available); */
130 [DllImport(Libraries.Nnstreamer, EntryPoint = "ml_check_nnfw_availability", CallingConvention = CallingConvention.Cdecl)]
131 internal static extern NNStreamerError CheckNNFWAvailability(NNFWType nnfw, HWType hw, out bool available);
133 internal static byte[] IntPtrToByteArray(IntPtr unmanagedByteArray, int size)
135 byte[] retByte = new byte[size];
136 Marshal.Copy(unmanagedByteArray, retByte, 0, size);
140 internal static string IntPtrToString(IntPtr val)
142 return (val != IntPtr.Zero) ? Marshal.PtrToStringAnsi(val) : string.Empty;