From 024f7bf0f1e74149ea92b942cf168ccddf317073 Mon Sep 17 00:00:00 2001 From: MediaPipe Team Date: Thu, 16 Apr 2020 11:29:01 -0700 Subject: [PATCH] Project import generated by Copybara. GitOrigin-RevId: a6e7ccd12eb2ad9da2f723eb344658295ed85d46 --- .../tflite/tflite_inference_calculator.cc | 10 ++++----- mediapipe/docs/android_archive_library.md | 5 +++-- mediapipe/framework/calculator_graph_test.cc | 6 ++--- .../framework/jni/register_natives.cc | 20 +++++++++++++++++ .../com/google/mediapipe/mediapipe_aar.bzl | 22 +++++++++---------- 5 files changed, 41 insertions(+), 22 deletions(-) diff --git a/mediapipe/calculators/tflite/tflite_inference_calculator.cc b/mediapipe/calculators/tflite/tflite_inference_calculator.cc index af6572c0..b0eb3c50 100644 --- a/mediapipe/calculators/tflite/tflite_inference_calculator.cc +++ b/mediapipe/calculators/tflite/tflite_inference_calculator.cc @@ -23,9 +23,9 @@ #include "mediapipe/framework/calculator_framework.h" #include "mediapipe/framework/port/ret_check.h" -#if !defined(__EMSCRIPTEN__) +#if !defined(__EMSCRIPTEN__) || defined(__EMSCRIPTEN_PTHREADS__) #include "mediapipe/util/cpu_util.h" -#endif // !__EMSCRIPTEN__ +#endif // !__EMSCRIPTEN__ || __EMSCRIPTEN_PTHREADS__ #include "mediapipe/util/resource_util.h" #include "tensorflow/lite/error_reporter.h" @@ -121,7 +121,7 @@ struct GPUData { // Returns number of threads to configure XNNPACK delegate with. // (Equal to user provided value if specified. Otherwise, it returns number of -// high cores (hard-coded to 1 for __EMSCRIPTEN__)) +// high cores (hard-coded to 1 for Emscripten without Threads extension)) int GetXnnpackNumThreads( const mediapipe::TfLiteInferenceCalculatorOptions& opts) { static constexpr int kDefaultNumThreads = -1; @@ -129,11 +129,11 @@ int GetXnnpackNumThreads( opts.delegate().xnnpack().num_threads() != kDefaultNumThreads) { return opts.delegate().xnnpack().num_threads(); } -#if !defined(__EMSCRIPTEN__) +#if !defined(__EMSCRIPTEN__) || defined(__EMSCRIPTEN_PTHREADS__) return InferHigherCoreIds().size(); #else return 1; -#endif // !__EMSCRIPTEN__ +#endif // !__EMSCRIPTEN__ || __EMSCRIPTEN_PTHREADS__ } // Calculator Header Section diff --git a/mediapipe/docs/android_archive_library.md b/mediapipe/docs/android_archive_library.md index 0b6ccdf2..5d00e3ff 100644 --- a/mediapipe/docs/android_archive_library.md +++ b/mediapipe/docs/android_archive_library.md @@ -32,13 +32,14 @@ project. 2. Run the Bazel build command to generate the AAR. ```bash - bazel build -c opt --fat_apk_cpu=arm64-v8a,armeabi-v7a //path/to/the/aar/build/file:aar_name + bazel build -c opt --host_crosstool_top=@bazel_tools//tools/cpp:toolchain --fat_apk_cpu=arm64-v8a,armeabi-v7a \ + //path/to/the/aar/build/file:aar_name ``` For the face detection AAR target we made in the step 1, run: ```bash - bazel build -c opt --fat_apk_cpu=arm64-v8a,armeabi-v7a \ + bazel build -c opt --host_crosstool_top=@bazel_tools//tools/cpp:toolchain --fat_apk_cpu=arm64-v8a,armeabi-v7a \ //mediapipe/examples/android/src/java/com/google/mediapipe/apps/aar_example:mp_face_detection_aar # It should print: diff --git a/mediapipe/framework/calculator_graph_test.cc b/mediapipe/framework/calculator_graph_test.cc index 6f749100..b25752b4 100644 --- a/mediapipe/framework/calculator_graph_test.cc +++ b/mediapipe/framework/calculator_graph_test.cc @@ -527,7 +527,7 @@ REGISTER_CALCULATOR(Modulo3SourceCalculator); // calculator. class OutputAllSourceCalculator : public CalculatorBase { public: - static const int kNumOutputPackets = 100; + static constexpr int kNumOutputPackets = 100; static ::mediapipe::Status GetContract(CalculatorContract* cc) { cc->Outputs().Index(0).Set(); @@ -550,7 +550,7 @@ REGISTER_CALCULATOR(OutputAllSourceCalculator); // progress. class OutputOneAtATimeSourceCalculator : public CalculatorBase { public: - static const int kNumOutputPackets = 1000; + static constexpr int kNumOutputPackets = 1000; static ::mediapipe::Status GetContract(CalculatorContract* cc) { cc->Outputs().Index(0).Set(); @@ -577,7 +577,7 @@ REGISTER_CALCULATOR(OutputOneAtATimeSourceCalculator); // input stream connected to this calculator can become full. class DecimatorCalculator : public CalculatorBase { public: - static const int kDecimationRatio = 101; + static constexpr int kDecimationRatio = 101; static ::mediapipe::Status GetContract(CalculatorContract* cc) { cc->Inputs().Index(0).SetAny(); diff --git a/mediapipe/java/com/google/mediapipe/framework/jni/register_natives.cc b/mediapipe/java/com/google/mediapipe/framework/jni/register_natives.cc index f584ade1..08a3b6dc 100644 --- a/mediapipe/java/com/google/mediapipe/framework/jni/register_natives.cc +++ b/mediapipe/java/com/google/mediapipe/framework/jni/register_natives.cc @@ -56,6 +56,19 @@ void AddJNINativeMethod(std::vector *methods, void RegisterNativesVector(JNIEnv *env, jclass cls, const std::vector &methods) { + // A client Java project may not use some methods and classes that we attempt + // to register and could be removed by Proguard. In that case, we want to + // avoid triggering a crash due to ClassNotFoundException, so we are trading + // safety check here in exchange for flexibility to list out all registrations + // without worrying about usage subset by client Java projects. + if (!cls || methods.empty()) { + LOG(INFO) << "Skipping registration and clearing exception. Class or " + "native methods not found, may be unused and/or trimmed by " + "Proguard."; + env->ExceptionClear(); + return; + } + JNINativeMethod *methods_array = new JNINativeMethod[methods.size()]; for (int i = 0; i < methods.size(); i++) { JNINativeMethod jniNativeMethod{ @@ -97,6 +110,13 @@ void RegisterGraphNatives(JNIEnv *env) { (void *)&GRAPH_METHOD(nativeStartRunningGraph)); AddJNINativeMethod(&graph_methods, graph, "nativeSetParentGlContext", "(JJ)V", (void *)&GRAPH_METHOD(nativeSetParentGlContext)); + AddJNINativeMethod(&graph_methods, graph, "nativeCloseAllPacketSources", + "(J)V", + (void *)&GRAPH_METHOD(nativeCloseAllPacketSources)); + AddJNINativeMethod(&graph_methods, graph, "nativeWaitUntilGraphDone", "(J)V", + (void *)&GRAPH_METHOD(nativeWaitUntilGraphDone)); + AddJNINativeMethod(&graph_methods, graph, "nativeReleaseGraph", "(J)V", + (void *)&GRAPH_METHOD(nativeReleaseGraph)); RegisterNativesVector(env, graph_class, graph_methods); } diff --git a/mediapipe/java/com/google/mediapipe/mediapipe_aar.bzl b/mediapipe/java/com/google/mediapipe/mediapipe_aar.bzl index 6078318a..702771ae 100644 --- a/mediapipe/java/com/google/mediapipe/mediapipe_aar.bzl +++ b/mediapipe/java/com/google/mediapipe/mediapipe_aar.bzl @@ -130,11 +130,11 @@ cat > $(OUTS) <