Imported Upstream version 1.12.0
[platform/core/ml/nnfw.git] / runtime / contrib / android / api / src / main / native / onert-native-api.cpp
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
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 "onert-native-api.h"
18
19 #include <android/log.h>
20
21 #include "onert-native-internal.h"
22 #include "onert-native-helper.h"
23
24 namespace
25 {
26
27 // android log tag
28 const char *JTAG = "ONERT_NATIVE";
29
30 } // namespace
31
32 JNIEXPORT jlong JNICALL Java_com_samsung_onert_NativeSessionWrapper_nativeCreateSession(JNIEnv *,
33                                                                                         jobject)
34 {
35   Handle sess = jni::createSession();
36   if (sess == 0)
37   {
38     __android_log_print(ANDROID_LOG_ERROR, JTAG, "%s] nnfw_create_session is failed",
39                         __PRETTY_FUNCTION__);
40   }
41   return sess;
42 }
43
44 JNIEXPORT void JNICALL Java_com_samsung_onert_NativeSessionWrapper_nativeCloseSession(JNIEnv *,
45                                                                                       jobject,
46                                                                                       jlong handle)
47 {
48   if (jni_helper::verifyHandle(handle) == JNI_FALSE)
49     return;
50
51   jni::closeSession(handle);
52 }
53
54 JNIEXPORT jboolean JNICALL Java_com_samsung_onert_NativeSessionWrapper_nativeLoadModelFromFile(
55   JNIEnv *env, jobject, jlong handle, jstring jnnpkg_path)
56 {
57   if (jni_helper::verifyHandle(handle) == JNI_FALSE)
58     return JNI_FALSE;
59
60   const char *nnpkg_path = env->GetStringUTFChars(jnnpkg_path, 0);
61   __android_log_print(ANDROID_LOG_DEBUG, JTAG, "%s] nnpkg_path: %s", __PRETTY_FUNCTION__,
62                       nnpkg_path);
63
64   bool result = jni::loadModel(handle, nnpkg_path);
65
66   env->ReleaseStringUTFChars(jnnpkg_path, nnpkg_path);
67
68   if (result == false)
69   {
70     __android_log_print(ANDROID_LOG_ERROR, JTAG, "%s] failed", __PRETTY_FUNCTION__);
71     return JNI_FALSE;
72   }
73   return JNI_TRUE;
74 }
75
76 JNIEXPORT jboolean JNICALL Java_com_samsung_onert_NativeSessionWrapper_nativePrepare(JNIEnv *,
77                                                                                      jobject,
78                                                                                      jlong handle)
79 {
80   if (jni_helper::verifyHandle(handle) == JNI_FALSE)
81     return JNI_FALSE;
82
83   if (jni::prepare(handle) == false)
84   {
85     __android_log_print(ANDROID_LOG_ERROR, JTAG, "%s] failed", __PRETTY_FUNCTION__);
86     return JNI_FALSE;
87   }
88   return JNI_TRUE;
89 }
90
91 JNIEXPORT jboolean JNICALL Java_com_samsung_onert_NativeSessionWrapper_nativeRun(JNIEnv *, jobject,
92                                                                                  jlong handle)
93 {
94   if (jni_helper::verifyHandle(handle) == JNI_FALSE)
95     return JNI_FALSE;
96
97   if (jni::run(handle) == false)
98   {
99     __android_log_print(ANDROID_LOG_ERROR, JTAG, "%s] failed", __PRETTY_FUNCTION__);
100     return JNI_FALSE;
101   }
102   return JNI_TRUE;
103 }
104
105 JNIEXPORT jboolean JNICALL Java_com_samsung_onert_NativeSessionWrapper_nativeSetInput(
106   JNIEnv *env, jobject, jlong handle, jint jindex, jint jtype, jobject jbuf, jint jbufsize)
107 {
108   if (jni_helper::verifyHandle(handle) == JNI_FALSE)
109     return JNI_FALSE;
110
111   jni::TensorParams params;
112   if (jni_helper::getTensorParams(env, jindex, jtype, jbuf, jbufsize, params) == JNI_FALSE)
113   {
114     __android_log_print(ANDROID_LOG_ERROR, JTAG, "%s] failed getTensorParams", __PRETTY_FUNCTION__);
115     return JNI_FALSE;
116   }
117
118   __android_log_print(ANDROID_LOG_ERROR, JTAG, "%s] index(%d), type(%d), buf(%p), buf_sz(%lu)",
119                       __PRETTY_FUNCTION__, params.index, params.type, params.buffer,
120                       params.buffer_size);
121
122   if (jni::setInput(handle, params) == false)
123   {
124     __android_log_print(ANDROID_LOG_ERROR, JTAG, "%s] failed native setInput", __PRETTY_FUNCTION__);
125     return JNI_FALSE;
126   }
127
128   return JNI_TRUE;
129 }
130
131 JNIEXPORT jboolean JNICALL Java_com_samsung_onert_NativeSessionWrapper_nativeSetOutput(
132   JNIEnv *env, jobject, jlong handle, jint jindex, jint jtype, jobject jbuf, jint jbufsize)
133 {
134   if (jni_helper::verifyHandle(handle) == JNI_FALSE)
135     return JNI_FALSE;
136
137   jni::TensorParams params;
138   if (jni_helper::getTensorParams(env, jindex, jtype, jbuf, jbufsize, params) == JNI_FALSE)
139   {
140     __android_log_print(ANDROID_LOG_ERROR, JTAG, "%s] failed getTensorParams", __PRETTY_FUNCTION__);
141     return JNI_FALSE;
142   }
143
144   __android_log_print(ANDROID_LOG_ERROR, JTAG, "%s] index(%d), type(%d), buf(%p), buf_sz(%lu)",
145                       __PRETTY_FUNCTION__, params.index, params.type, params.buffer,
146                       params.buffer_size);
147
148   if (jni::setOutput(handle, params) == false)
149   {
150     __android_log_print(ANDROID_LOG_ERROR, JTAG, "%s] failed native setOutput",
151                         __PRETTY_FUNCTION__);
152     return JNI_FALSE;
153   }
154
155   return JNI_TRUE;
156 }
157
158 JNIEXPORT jboolean JNICALL Java_com_samsung_onert_NativeSessionWrapper_nativeSetInputLayout(
159   JNIEnv *, jobject, jlong handle, jint jindex, jint jlayout)
160 {
161   if (jni_helper::verifyHandle(handle) == JNI_FALSE)
162     return JNI_FALSE;
163
164   jni::LayoutParams params;
165   if (jni_helper::getLayoutParams(jindex, jlayout, params) == JNI_FALSE)
166   {
167     __android_log_print(ANDROID_LOG_ERROR, JTAG, "%s] failed", __PRETTY_FUNCTION__);
168     return JNI_FALSE;
169   }
170
171   if (jni::setInputLayout(handle, params) == false)
172   {
173     __android_log_print(ANDROID_LOG_ERROR, JTAG, "%s] failed", __PRETTY_FUNCTION__);
174     return JNI_FALSE;
175   }
176
177   return JNI_TRUE;
178 }
179
180 JNIEXPORT jboolean JNICALL Java_com_samsung_onert_NativeSessionWrapper_nativeSetOutputLayout(
181   JNIEnv *, jobject, jlong handle, jint jindex, jint jlayout)
182 {
183   if (jni_helper::verifyHandle(handle) == JNI_FALSE)
184     return JNI_FALSE;
185
186   jni::LayoutParams params;
187   if (jni_helper::getLayoutParams(jindex, jlayout, params) == JNI_FALSE)
188   {
189     __android_log_print(ANDROID_LOG_ERROR, JTAG, "%s] failed", __PRETTY_FUNCTION__);
190     return JNI_FALSE;
191   }
192
193   if (jni::setOutputLayout(handle, params) == false)
194   {
195     __android_log_print(ANDROID_LOG_ERROR, JTAG, "%s] failed", __PRETTY_FUNCTION__);
196     return JNI_FALSE;
197   }
198
199   return JNI_TRUE;
200 }
201
202 JNIEXPORT jint JNICALL Java_com_samsung_onert_NativeSessionWrapper_nativeGetInputSize(JNIEnv *,
203                                                                                       jobject,
204                                                                                       jlong handle)
205 {
206   if (jni_helper::verifyHandle(handle) == JNI_FALSE)
207     return -1;
208
209   int size = 0;
210   if ((size = jni::getInputSize(handle)) < 0)
211   {
212     __android_log_print(ANDROID_LOG_ERROR, JTAG, "%s] failed", __PRETTY_FUNCTION__);
213     return -1;
214   }
215
216   return static_cast<jint>(size);
217 }
218
219 JNIEXPORT jint JNICALL Java_com_samsung_onert_NativeSessionWrapper_nativeGetOutputSize(JNIEnv *,
220                                                                                        jobject,
221                                                                                        jlong handle)
222 {
223   if (jni_helper::verifyHandle(handle) == JNI_FALSE)
224     return -1;
225
226   int size = 0;
227   if ((size = jni::getOutputSize(handle)) < 0)
228   {
229     __android_log_print(ANDROID_LOG_ERROR, JTAG, "%s] failed", __PRETTY_FUNCTION__);
230     return -1;
231   }
232
233   return static_cast<jint>(size);
234 }
235
236 JNIEXPORT jboolean JNICALL Java_com_samsung_onert_NativeSessionWrapper_nativeSetAvailableBackends(
237   JNIEnv *env, jobject, jlong handle, jstring jbackends)
238 {
239   if (jni_helper::verifyHandle(handle) == JNI_FALSE)
240     return JNI_FALSE;
241
242   const char *backends = env->GetStringUTFChars(jbackends, 0);
243   __android_log_print(ANDROID_LOG_DEBUG, JTAG, "%s] backends: %s", __PRETTY_FUNCTION__, backends);
244
245   auto result = jni::setAvailableBackends(handle, backends);
246
247   env->ReleaseStringUTFChars(jbackends, backends);
248
249   if (result == false)
250   {
251     __android_log_print(ANDROID_LOG_ERROR, JTAG, "%s] failed", __PRETTY_FUNCTION__);
252     return JNI_FALSE;
253   }
254   return JNI_TRUE;
255 }
256
257 JNIEXPORT jboolean JNICALL Java_com_samsung_onert_NativeSessionWrapper_nativeGetInputTensorInfo(
258   JNIEnv *env, jobject, jlong handle, jint jindex, jobject jinfo)
259 {
260   if (jni_helper::verifyHandle(handle) == JNI_FALSE)
261     return JNI_FALSE;
262
263   jni::TensorInfo tensor_info;
264   if (jni_helper::getInputTensorInfo(handle, jindex, tensor_info) == JNI_FALSE)
265   {
266     __android_log_print(ANDROID_LOG_ERROR, JTAG, "%s] failed", __PRETTY_FUNCTION__);
267     return JNI_FALSE;
268   }
269
270   if (jni_helper::setTensorInfoToJava(env, tensor_info, jinfo) == JNI_FALSE)
271   {
272     __android_log_print(ANDROID_LOG_ERROR, JTAG, "%s] failed", __PRETTY_FUNCTION__);
273     return JNI_FALSE;
274   }
275
276   return JNI_TRUE;
277 }
278
279 JNIEXPORT jboolean JNICALL Java_com_samsung_onert_NativeSessionWrapper_nativeGetOutputTensorInfo(
280   JNIEnv *env, jobject, jlong handle, jint jindex, jobject jinfo)
281 {
282   if (jni_helper::verifyHandle(handle) == JNI_FALSE)
283     return JNI_FALSE;
284
285   jni::TensorInfo tensor_info;
286   if (jni_helper::getOutputTensorInfo(handle, jindex, tensor_info) == JNI_FALSE)
287   {
288     __android_log_print(ANDROID_LOG_ERROR, JTAG, "%s] failed", __PRETTY_FUNCTION__);
289     return JNI_FALSE;
290   }
291
292   if (jni_helper::setTensorInfoToJava(env, tensor_info, jinfo) == JNI_FALSE)
293   {
294     __android_log_print(ANDROID_LOG_ERROR, JTAG, "%s] failed", __PRETTY_FUNCTION__);
295     return JNI_FALSE;
296   }
297
298   return JNI_TRUE;
299 }