2 * Copyright (c) 2020 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.
17 #include "onert-native-api.h"
19 #include <android/log.h>
21 #include "onert-native-internal.h"
22 #include "onert-native-helper.h"
28 const char *JTAG = "ONERT_NATIVE";
32 JNIEXPORT jlong JNICALL Java_com_samsung_onert_NativeSessionWrapper_nativeCreateSession(JNIEnv *,
35 Handle sess = jni::createSession();
38 __android_log_print(ANDROID_LOG_ERROR, JTAG, "%s] nnfw_create_session is failed",
44 JNIEXPORT void JNICALL Java_com_samsung_onert_NativeSessionWrapper_nativeCloseSession(JNIEnv *,
48 if (jni_helper::verifyHandle(handle) == JNI_FALSE)
51 jni::closeSession(handle);
54 JNIEXPORT jboolean JNICALL Java_com_samsung_onert_NativeSessionWrapper_nativeLoadModelFromFile(
55 JNIEnv *env, jobject, jlong handle, jstring jnnpkg_path)
57 if (jni_helper::verifyHandle(handle) == JNI_FALSE)
60 const char *nnpkg_path = env->GetStringUTFChars(jnnpkg_path, 0);
61 __android_log_print(ANDROID_LOG_DEBUG, JTAG, "%s] nnpkg_path: %s", __PRETTY_FUNCTION__,
64 bool result = jni::loadModel(handle, nnpkg_path);
66 env->ReleaseStringUTFChars(jnnpkg_path, nnpkg_path);
70 __android_log_print(ANDROID_LOG_ERROR, JTAG, "%s] failed", __PRETTY_FUNCTION__);
76 JNIEXPORT jboolean JNICALL Java_com_samsung_onert_NativeSessionWrapper_nativePrepare(JNIEnv *,
80 if (jni_helper::verifyHandle(handle) == JNI_FALSE)
83 if (jni::prepare(handle) == false)
85 __android_log_print(ANDROID_LOG_ERROR, JTAG, "%s] failed", __PRETTY_FUNCTION__);
91 JNIEXPORT jboolean JNICALL Java_com_samsung_onert_NativeSessionWrapper_nativeRun(JNIEnv *, jobject,
94 if (jni_helper::verifyHandle(handle) == JNI_FALSE)
97 if (jni::run(handle) == false)
99 __android_log_print(ANDROID_LOG_ERROR, JTAG, "%s] failed", __PRETTY_FUNCTION__);
105 JNIEXPORT jboolean JNICALL Java_com_samsung_onert_NativeSessionWrapper_nativeSetInput(
106 JNIEnv *env, jobject, jlong handle, jint jindex, jint jtype, jobject jbuf, jint jbufsize)
108 if (jni_helper::verifyHandle(handle) == JNI_FALSE)
111 jni::TensorParams params;
112 if (jni_helper::getTensorParams(env, jindex, jtype, jbuf, jbufsize, params) == JNI_FALSE)
114 __android_log_print(ANDROID_LOG_ERROR, JTAG, "%s] failed getTensorParams", __PRETTY_FUNCTION__);
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,
122 if (jni::setInput(handle, params) == false)
124 __android_log_print(ANDROID_LOG_ERROR, JTAG, "%s] failed native setInput", __PRETTY_FUNCTION__);
131 JNIEXPORT jboolean JNICALL Java_com_samsung_onert_NativeSessionWrapper_nativeSetOutput(
132 JNIEnv *env, jobject, jlong handle, jint jindex, jint jtype, jobject jbuf, jint jbufsize)
134 if (jni_helper::verifyHandle(handle) == JNI_FALSE)
137 jni::TensorParams params;
138 if (jni_helper::getTensorParams(env, jindex, jtype, jbuf, jbufsize, params) == JNI_FALSE)
140 __android_log_print(ANDROID_LOG_ERROR, JTAG, "%s] failed getTensorParams", __PRETTY_FUNCTION__);
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,
148 if (jni::setOutput(handle, params) == false)
150 __android_log_print(ANDROID_LOG_ERROR, JTAG, "%s] failed native setOutput",
151 __PRETTY_FUNCTION__);
158 JNIEXPORT jboolean JNICALL Java_com_samsung_onert_NativeSessionWrapper_nativeSetInputLayout(
159 JNIEnv *, jobject, jlong handle, jint jindex, jint jlayout)
161 if (jni_helper::verifyHandle(handle) == JNI_FALSE)
164 jni::LayoutParams params;
165 if (jni_helper::getLayoutParams(jindex, jlayout, params) == JNI_FALSE)
167 __android_log_print(ANDROID_LOG_ERROR, JTAG, "%s] failed", __PRETTY_FUNCTION__);
171 if (jni::setInputLayout(handle, params) == false)
173 __android_log_print(ANDROID_LOG_ERROR, JTAG, "%s] failed", __PRETTY_FUNCTION__);
180 JNIEXPORT jboolean JNICALL Java_com_samsung_onert_NativeSessionWrapper_nativeSetOutputLayout(
181 JNIEnv *, jobject, jlong handle, jint jindex, jint jlayout)
183 if (jni_helper::verifyHandle(handle) == JNI_FALSE)
186 jni::LayoutParams params;
187 if (jni_helper::getLayoutParams(jindex, jlayout, params) == JNI_FALSE)
189 __android_log_print(ANDROID_LOG_ERROR, JTAG, "%s] failed", __PRETTY_FUNCTION__);
193 if (jni::setOutputLayout(handle, params) == false)
195 __android_log_print(ANDROID_LOG_ERROR, JTAG, "%s] failed", __PRETTY_FUNCTION__);
202 JNIEXPORT jint JNICALL Java_com_samsung_onert_NativeSessionWrapper_nativeGetInputSize(JNIEnv *,
206 if (jni_helper::verifyHandle(handle) == JNI_FALSE)
210 if ((size = jni::getInputSize(handle)) < 0)
212 __android_log_print(ANDROID_LOG_ERROR, JTAG, "%s] failed", __PRETTY_FUNCTION__);
216 return static_cast<jint>(size);
219 JNIEXPORT jint JNICALL Java_com_samsung_onert_NativeSessionWrapper_nativeGetOutputSize(JNIEnv *,
223 if (jni_helper::verifyHandle(handle) == JNI_FALSE)
227 if ((size = jni::getOutputSize(handle)) < 0)
229 __android_log_print(ANDROID_LOG_ERROR, JTAG, "%s] failed", __PRETTY_FUNCTION__);
233 return static_cast<jint>(size);
236 JNIEXPORT jboolean JNICALL Java_com_samsung_onert_NativeSessionWrapper_nativeSetAvailableBackends(
237 JNIEnv *env, jobject, jlong handle, jstring jbackends)
239 if (jni_helper::verifyHandle(handle) == JNI_FALSE)
242 const char *backends = env->GetStringUTFChars(jbackends, 0);
243 __android_log_print(ANDROID_LOG_DEBUG, JTAG, "%s] backends: %s", __PRETTY_FUNCTION__, backends);
245 auto result = jni::setAvailableBackends(handle, backends);
247 env->ReleaseStringUTFChars(jbackends, backends);
251 __android_log_print(ANDROID_LOG_ERROR, JTAG, "%s] failed", __PRETTY_FUNCTION__);
257 JNIEXPORT jboolean JNICALL Java_com_samsung_onert_NativeSessionWrapper_nativeGetInputTensorInfo(
258 JNIEnv *env, jobject, jlong handle, jint jindex, jobject jinfo)
260 if (jni_helper::verifyHandle(handle) == JNI_FALSE)
263 jni::TensorInfo tensor_info;
264 if (jni_helper::getInputTensorInfo(handle, jindex, tensor_info) == JNI_FALSE)
266 __android_log_print(ANDROID_LOG_ERROR, JTAG, "%s] failed", __PRETTY_FUNCTION__);
270 if (jni_helper::setTensorInfoToJava(env, tensor_info, jinfo) == JNI_FALSE)
272 __android_log_print(ANDROID_LOG_ERROR, JTAG, "%s] failed", __PRETTY_FUNCTION__);
279 JNIEXPORT jboolean JNICALL Java_com_samsung_onert_NativeSessionWrapper_nativeGetOutputTensorInfo(
280 JNIEnv *env, jobject, jlong handle, jint jindex, jobject jinfo)
282 if (jni_helper::verifyHandle(handle) == JNI_FALSE)
285 jni::TensorInfo tensor_info;
286 if (jni_helper::getOutputTensorInfo(handle, jindex, tensor_info) == JNI_FALSE)
288 __android_log_print(ANDROID_LOG_ERROR, JTAG, "%s] failed", __PRETTY_FUNCTION__);
292 if (jni_helper::setTensorInfoToJava(env, tensor_info, jinfo) == JNI_FALSE)
294 __android_log_print(ANDROID_LOG_ERROR, JTAG, "%s] failed", __PRETTY_FUNCTION__);