Adds a method to check if there's an NNAPI accelerator available.

Bug: 141353636
Bug: 137150012

Test: CtsNNAPIBenchmarkTestCases

Change-Id: Ic0d066f29b1b0b4d10c7f9105dd342aca27408cf
Merged-In: Ic0d066f29b1b0b4d10c7f9105dd342aca27408cf
(cherry picked from commit 5e82b1b805a378504872e3724121c857c2eccc6b)
diff --git a/jni/benchmark_jni.cpp b/jni/benchmark_jni.cpp
index 8da014c..37183c2 100644
--- a/jni/benchmark_jni.cpp
+++ b/jni/benchmark_jni.cpp
@@ -16,6 +16,8 @@
 
 #include "run_tflite.h"
 
+#include "tensorflow/lite/nnapi/nnapi_implementation.h"
+
 #include <jni.h>
 #include <string>
 #include <iomanip>
@@ -375,3 +377,13 @@
     model->dumpAllLayers(dumpPathStr, data.data());
     env->ReleaseStringUTFChars(dumpPath, dumpPathStr);
 }
+
+extern "C"
+JNIEXPORT jboolean
+JNICALL
+Java_com_android_nn_benchmark_core_NNTestBase_hasAccelerator() {
+  uint32_t device_count = 0;
+  NnApiImplementation()->ANeuralNetworks_getDeviceCount(&device_count);
+  // We only consider a real device, not 'nnapi-reference'.
+  return device_count > 1;
+}
diff --git a/src/com/android/nn/benchmark/core/NNTestBase.java b/src/com/android/nn/benchmark/core/NNTestBase.java
index d497396..553a138 100644
--- a/src/com/android/nn/benchmark/core/NNTestBase.java
+++ b/src/com/android/nn/benchmark/core/NNTestBase.java
@@ -40,6 +40,10 @@
         System.loadLibrary("nnbenchmark_jni");
     }
 
+    // Does the device has any NNAPI accelerator?
+    // We only consider a real device, not 'nnapi-reference'.
+    public static native boolean hasAccelerator();
+
     private synchronized native long initModel(
             String modelFileName,
             boolean useNNApi,