1 /**
2  * Copyright 2017 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 #include "run_tflite.h"
18 
19 #include <jni.h>
20 #include <string>
21 #include <iomanip>
22 #include <sstream>
23 #include <fcntl.h>
24 
25 #include <android/log.h>
26 
27 #include "tensorflow/lite/nnapi/nnapi_implementation.h"
28 
29 #define LOG_TAG "NN_BENCHMARK"
30 
31 
32 // This method loads the NNAPI SL from the given path.
33 // Is called by a synchronized method in NNTestBase that will cache the
34 // result. We expect this to be called only once per JVM and the handle
35 // to be released when the JVM is shut down.
36 extern "C" JNIEXPORT jlong JNICALL
Java_com_android_nn_benchmark_core_sl_SupportLibraryDriverHandler_loadNnApiSlHandle(JNIEnv * env,jobject,jstring _nnapiSlDriverPath)37 Java_com_android_nn_benchmark_core_sl_SupportLibraryDriverHandler_loadNnApiSlHandle(
38     JNIEnv *env, jobject /* clazz */, jstring _nnapiSlDriverPath) {
39   if (_nnapiSlDriverPath != NULL) {
40     const char *nnapiSlDriverPath =
41         env->GetStringUTFChars(_nnapiSlDriverPath, NULL);
42     std::unique_ptr<const tflite::nnapi::NnApiSupportLibrary> tmp =
43         tflite::nnapi::loadNnApiSupportLibrary(nnapiSlDriverPath);
44     if (!tmp) {
45       __android_log_print(ANDROID_LOG_ERROR, LOG_TAG,
46                           "Failed to load NNAPI SL driver from '%s'",
47                           nnapiSlDriverPath);
48       return false;
49     }
50     __android_log_print(ANDROID_LOG_INFO, LOG_TAG, "Loaded NNAPI SL");
51     return (jlong)(uintptr_t)tmp.release();
52   }
53 
54   return 0L;
55 }
56