From dfb86e174b3f6db99ea670e3226a0486dd101e21 Mon Sep 17 00:00:00 2001 From: Marc Rousavy Date: Fri, 25 Aug 2023 12:22:44 +0200 Subject: [PATCH] feat: Replace `*NativeMap` and `*NativeArray` with `Map` and `List` for faster JSI -> JNI calls (#1720) Replaces `ReadableNativeMap`/`WritableNativeMap` with `Map` and `ReadableNativeArray`/`WritableNativeArray` with `List`, making the JSI -> JNI conversion a bit faster and more logical. Also, we could now convert Array Buffers or HostObjects if we wanted to. --- android/CMakeLists.txt | 24 ++-- android/src/main/cpp/VisionCamera.cpp | 8 +- .../{ => frameprocessor}/FrameHostObject.cpp | 0 .../{ => frameprocessor}/FrameHostObject.h | 2 +- .../FrameProcessorPluginHostObject.cpp | 2 +- .../FrameProcessorPluginHostObject.h | 2 +- .../{ => frameprocessor}/JSIJNIConversion.cpp | 104 +++++++++++------- .../{ => frameprocessor}/JSIJNIConversion.h | 3 +- .../VisionCameraProxy.cpp | 4 +- .../{ => frameprocessor}/VisionCameraProxy.h | 4 +- .../java-bindings/JFrame.cpp | 0 .../java-bindings/JFrame.h | 0 .../java-bindings/JFrameProcessor.cpp | 0 .../java-bindings/JFrameProcessor.h | 0 .../java-bindings/JFrameProcessorPlugin.cpp | 4 +- .../java-bindings/JFrameProcessorPlugin.h | 3 +- .../java-bindings/JVisionCameraProxy.cpp | 3 +- .../java-bindings/JVisionCameraProxy.h | 3 +- .../java-bindings/JVisionCameraScheduler.cpp | 0 .../java-bindings/JVisionCameraScheduler.h | 0 .../src/main/cpp/java-bindings/JArrayList.h | 21 ---- .../src/main/cpp/java-bindings/JHashMap.cpp | 20 ---- android/src/main/cpp/java-bindings/JHashMap.h | 23 ---- .../frameprocessor/FrameProcessorPlugin.java | 4 +- .../FrameProcessorPluginRegistry.java | 9 +- .../frameprocessor/VisionCameraProxy.kt | 4 +- .../FRAME_PROCESSORS_CREATE_OVERVIEW.mdx | 4 +- .../FRAME_PROCESSOR_CREATE_PLUGIN_ANDROID.mdx | 4 +- .../example/ExampleFrameProcessorPlugin.java | 36 +++--- .../camera/example/MainApplication.java | 1 - 30 files changed, 125 insertions(+), 167 deletions(-) rename android/src/main/cpp/{ => frameprocessor}/FrameHostObject.cpp (100%) rename android/src/main/cpp/{ => frameprocessor}/FrameHostObject.h (95%) rename android/src/main/cpp/{ => frameprocessor}/FrameProcessorPluginHostObject.cpp (96%) rename android/src/main/cpp/{ => frameprocessor}/FrameProcessorPluginHostObject.h (93%) rename android/src/main/cpp/{ => frameprocessor}/JSIJNIConversion.cpp (54%) rename android/src/main/cpp/{ => frameprocessor}/JSIJNIConversion.h (67%) rename android/src/main/cpp/{ => frameprocessor}/VisionCameraProxy.cpp (98%) rename android/src/main/cpp/{ => frameprocessor}/VisionCameraProxy.h (93%) rename android/src/main/cpp/{ => frameprocessor}/java-bindings/JFrame.cpp (100%) rename android/src/main/cpp/{ => frameprocessor}/java-bindings/JFrame.h (100%) rename android/src/main/cpp/{ => frameprocessor}/java-bindings/JFrameProcessor.cpp (100%) rename android/src/main/cpp/{ => frameprocessor}/java-bindings/JFrameProcessor.h (100%) rename android/src/main/cpp/{ => frameprocessor}/java-bindings/JFrameProcessorPlugin.cpp (84%) rename android/src/main/cpp/{ => frameprocessor}/java-bindings/JFrameProcessorPlugin.h (78%) rename android/src/main/cpp/{ => frameprocessor}/java-bindings/JVisionCameraProxy.cpp (97%) rename android/src/main/cpp/{ => frameprocessor}/java-bindings/JVisionCameraProxy.h (94%) rename android/src/main/cpp/{ => frameprocessor}/java-bindings/JVisionCameraScheduler.cpp (100%) rename android/src/main/cpp/{ => frameprocessor}/java-bindings/JVisionCameraScheduler.h (100%) delete mode 100644 android/src/main/cpp/java-bindings/JArrayList.h delete mode 100644 android/src/main/cpp/java-bindings/JHashMap.cpp delete mode 100644 android/src/main/cpp/java-bindings/JHashMap.h diff --git a/android/CMakeLists.txt b/android/CMakeLists.txt index 00785bb..97406ef 100644 --- a/android/CMakeLists.txt +++ b/android/CMakeLists.txt @@ -24,18 +24,19 @@ add_library( ${PACKAGE_NAME} SHARED ../cpp/JSITypedArray.cpp - src/main/cpp/FrameHostObject.cpp - src/main/cpp/FrameProcessorPluginHostObject.cpp - src/main/cpp/JSIJNIConversion.cpp src/main/cpp/VisionCamera.cpp - src/main/cpp/VisionCameraProxy.cpp + # Frame Processor + src/main/cpp/frameprocessor/FrameHostObject.cpp + src/main/cpp/frameprocessor/FrameProcessorPluginHostObject.cpp + src/main/cpp/frameprocessor/JSIJNIConversion.cpp + src/main/cpp/frameprocessor/VisionCameraProxy.cpp + src/main/cpp/frameprocessor/java-bindings/JFrame.cpp + src/main/cpp/frameprocessor/java-bindings/JFrameProcessor.cpp + src/main/cpp/frameprocessor/java-bindings/JFrameProcessorPlugin.cpp + src/main/cpp/frameprocessor/java-bindings/JVisionCameraProxy.cpp + src/main/cpp/frameprocessor/java-bindings/JVisionCameraScheduler.cpp + # Skia Frame Processor src/main/cpp/skia/SkiaRenderer.cpp - src/main/cpp/java-bindings/JFrame.cpp - src/main/cpp/java-bindings/JFrameProcessor.cpp - src/main/cpp/java-bindings/JFrameProcessorPlugin.cpp - src/main/cpp/java-bindings/JHashMap.cpp - src/main/cpp/java-bindings/JVisionCameraProxy.cpp - src/main/cpp/java-bindings/JVisionCameraScheduler.cpp ) # Header Search Paths (includes) @@ -44,6 +45,9 @@ target_include_directories( PRIVATE "../cpp" "src/main/cpp" + "src/main/cpp/frameprocessor" + "src/main/cpp/frameprocessor/java-bindings" + "src/main/cpp/skia" "${NODE_MODULES_DIR}/react-native/ReactCommon" "${NODE_MODULES_DIR}/react-native/ReactCommon/callinvoker" "${NODE_MODULES_DIR}/react-native/ReactAndroid/src/main/jni/react/turbomodule" # <-- CallInvokerHolder JNI wrapper diff --git a/android/src/main/cpp/VisionCamera.cpp b/android/src/main/cpp/VisionCamera.cpp index f58de85..275df90 100644 --- a/android/src/main/cpp/VisionCamera.cpp +++ b/android/src/main/cpp/VisionCamera.cpp @@ -1,10 +1,10 @@ #include #include -#include "java-bindings/JVisionCameraScheduler.h" -#include "java-bindings/JFrameProcessor.h" -#include "java-bindings/JVisionCameraProxy.h" +#include "JVisionCameraScheduler.h" +#include "JFrameProcessor.h" +#include "JVisionCameraProxy.h" #include "VisionCameraProxy.h" -#include "skia/SkiaRenderer.h" +#include "SkiaRenderer.h" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *) { return facebook::jni::initialize(vm, [] { diff --git a/android/src/main/cpp/FrameHostObject.cpp b/android/src/main/cpp/frameprocessor/FrameHostObject.cpp similarity index 100% rename from android/src/main/cpp/FrameHostObject.cpp rename to android/src/main/cpp/frameprocessor/FrameHostObject.cpp diff --git a/android/src/main/cpp/FrameHostObject.h b/android/src/main/cpp/frameprocessor/FrameHostObject.h similarity index 95% rename from android/src/main/cpp/FrameHostObject.h rename to android/src/main/cpp/frameprocessor/FrameHostObject.h index 7ce7e30..297ee24 100644 --- a/android/src/main/cpp/FrameHostObject.h +++ b/android/src/main/cpp/frameprocessor/FrameHostObject.h @@ -10,7 +10,7 @@ #include #include -#include "java-bindings/JFrame.h" +#include "JFrame.h" namespace vision { diff --git a/android/src/main/cpp/FrameProcessorPluginHostObject.cpp b/android/src/main/cpp/frameprocessor/FrameProcessorPluginHostObject.cpp similarity index 96% rename from android/src/main/cpp/FrameProcessorPluginHostObject.cpp rename to android/src/main/cpp/frameprocessor/FrameProcessorPluginHostObject.cpp index 43312d3..8f2711c 100644 --- a/android/src/main/cpp/FrameProcessorPluginHostObject.cpp +++ b/android/src/main/cpp/frameprocessor/FrameProcessorPluginHostObject.cpp @@ -34,7 +34,7 @@ jsi::Value FrameProcessorPluginHostObject::get(jsi::Runtime& runtime, const jsi: auto frame = frameHostObject->frame; // Options are second argument (possibly undefined) - local_ref options = nullptr; + local_ref> options = nullptr; if (count > 1) { options = JSIJNIConversion::convertJSIObjectToJNIMap(runtime, arguments[1].asObject(runtime)); } diff --git a/android/src/main/cpp/FrameProcessorPluginHostObject.h b/android/src/main/cpp/frameprocessor/FrameProcessorPluginHostObject.h similarity index 93% rename from android/src/main/cpp/FrameProcessorPluginHostObject.h rename to android/src/main/cpp/frameprocessor/FrameProcessorPluginHostObject.h index a9420af..f4eb49d 100644 --- a/android/src/main/cpp/FrameProcessorPluginHostObject.h +++ b/android/src/main/cpp/frameprocessor/FrameProcessorPluginHostObject.h @@ -5,7 +5,7 @@ #pragma once #include -#include "java-bindings/JFrameProcessorPlugin.h" +#include "JFrameProcessorPlugin.h" #include #include #include diff --git a/android/src/main/cpp/JSIJNIConversion.cpp b/android/src/main/cpp/frameprocessor/JSIJNIConversion.cpp similarity index 54% rename from android/src/main/cpp/JSIJNIConversion.cpp rename to android/src/main/cpp/frameprocessor/JSIJNIConversion.cpp index 5655a1b..558b1b9 100644 --- a/android/src/main/cpp/JSIJNIConversion.cpp +++ b/android/src/main/cpp/frameprocessor/JSIJNIConversion.cpp @@ -13,25 +13,71 @@ #include #include -#include -#include -#include - -#include -#include - #include "FrameHostObject.h" -#include "java-bindings/JFrame.h" -#include "java-bindings/JArrayList.h" -#include "java-bindings/JHashMap.h" +#include "JFrame.h" namespace vision { using namespace facebook; -jni::local_ref JSIJNIConversion::convertJSIObjectToJNIMap(jsi::Runtime& runtime, const jsi::Object& object) { - auto dynamic = jsi::dynamicFromValue(runtime, jsi::Value(runtime, object)); - return react::ReadableNativeMap::createWithContents(std::move(dynamic)); +jni::local_ref> JSIJNIConversion::convertJSIObjectToJNIMap(jsi::Runtime& runtime, const jsi::Object& object) { + auto propertyNames = object.getPropertyNames(runtime); + auto size = propertyNames.size(runtime); + auto hashMap = jni::JHashMap::create(); + + for (size_t i = 0; i < size; i++) { + auto propName = propertyNames.getValueAtIndex(runtime, i).asString(runtime); + auto key = jni::make_jstring(propName.utf8(runtime)); + auto value = object.getProperty(runtime, propName); + + if (value.isNull() || value.isUndefined()) { + // null + + hashMap->put(key, nullptr); + + } else if (value.isBool()) { + // Boolean + + auto boolean = value.getBool(); + hashMap->put(key, jni::JBoolean::valueOf(boolean)); + + } else if (value.isNumber()) { + // Double + + auto number = value.getNumber(); + hashMap->put(key, jni::JDouble::valueOf(number)); + + } else if (value.isString()) { + // String + + auto str = value.getString(runtime).utf8(runtime); + hashMap->put(key, jni::make_jstring(str)); + + } else if (value.isObject()) { + // Object + + auto valueAsObject = value.getObject(runtime); + + if (valueAsObject.isArray(runtime)) { + // List + + } else if (valueAsObject.isHostObject(runtime)) { + throw std::runtime_error("You can't pass HostObjects here."); + } else { + // Map + + auto map = convertJSIObjectToJNIMap(runtime, valueAsObject); + hashMap->put(key, map); + + } + + } else { + auto stringRepresentation = value.toString(runtime).utf8(runtime); + throw std::runtime_error("Failed to convert jsi::Value to JNI value - unsupported type!" + stringRepresentation); + } + } + + return hashMap; } jsi::Value JSIJNIConversion::convertJNIObjectToJSIValue(jsi::Runtime &runtime, const jni::local_ref& object) { @@ -66,10 +112,10 @@ jsi::Value JSIJNIConversion::convertJNIObjectToJSIValue(jsi::Runtime &runtime, c return jsi::String::createFromUtf8(runtime, object->toString()); - } else if (object->isInstanceOf(JArrayList::javaClassStatic())) { - // ArrayList + } else if (object->isInstanceOf(JList::javaClassStatic())) { + // List - auto arrayList = static_ref_cast>(object); + auto arrayList = static_ref_cast>(object); auto size = arrayList->size(); auto result = jsi::Array(runtime, size); @@ -80,19 +126,10 @@ jsi::Value JSIJNIConversion::convertJNIObjectToJSIValue(jsi::Runtime &runtime, c } return result; - } else if (object->isInstanceOf(react::ReadableArray::javaClassStatic())) { - // ReadableArray + } else if (object->isInstanceOf(JMap::javaClassStatic())) { + // Map - static const auto toArrayListFunc = react::ReadableArray::javaClassLocal()->getMethod()>("toArrayList"); - - // call recursive, this time ArrayList - auto array = toArrayListFunc(object.get()); - return convertJNIObjectToJSIValue(runtime, array); - - } else if (object->isInstanceOf(JHashMap::javaClassStatic())) { - // HashMap - - auto map = static_ref_cast>(object); + auto map = static_ref_cast>(object); auto result = jsi::Object(runtime); for (const auto& entry : *map) { @@ -103,16 +140,7 @@ jsi::Value JSIJNIConversion::convertJNIObjectToJSIValue(jsi::Runtime &runtime, c } return result; - } else if (object->isInstanceOf(react::ReadableMap::javaClassStatic())) { - // ReadableMap - - static const auto toHashMapFunc = react::ReadableMap::javaClassLocal()->getMethod()>("toHashMap"); - - // call recursive, this time HashMap - auto hashMap = toHashMapFunc(object.get()); - return convertJNIObjectToJSIValue(runtime, hashMap); - - } else if (object->isInstanceOf(JFrame::javaClassStatic())) { + } if (object->isInstanceOf(JFrame::javaClassStatic())) { // Frame auto frame = static_ref_cast(object); diff --git a/android/src/main/cpp/JSIJNIConversion.h b/android/src/main/cpp/frameprocessor/JSIJNIConversion.h similarity index 67% rename from android/src/main/cpp/JSIJNIConversion.h rename to android/src/main/cpp/frameprocessor/JSIJNIConversion.h index 3e55848..56a9e9f 100644 --- a/android/src/main/cpp/JSIJNIConversion.h +++ b/android/src/main/cpp/frameprocessor/JSIJNIConversion.h @@ -7,7 +7,6 @@ #include #include #include -#include namespace vision { @@ -15,7 +14,7 @@ namespace JSIJNIConversion { using namespace facebook; -jni::local_ref convertJSIObjectToJNIMap(jsi::Runtime& runtime, const jsi::Object& object); +jni::local_ref> convertJSIObjectToJNIMap(jsi::Runtime& runtime, const jsi::Object& object); jsi::Value convertJNIObjectToJSIValue(jsi::Runtime& runtime, const jni::local_ref& object); diff --git a/android/src/main/cpp/VisionCameraProxy.cpp b/android/src/main/cpp/frameprocessor/VisionCameraProxy.cpp similarity index 98% rename from android/src/main/cpp/VisionCameraProxy.cpp rename to android/src/main/cpp/frameprocessor/VisionCameraProxy.cpp index b41d1e2..3667235 100644 --- a/android/src/main/cpp/VisionCameraProxy.cpp +++ b/android/src/main/cpp/frameprocessor/VisionCameraProxy.cpp @@ -5,8 +5,8 @@ #include "VisionCameraProxy.h" #include -#include "java-bindings/JFrameProcessor.h" -#include "java-bindings/JFrameProcessorPlugin.h" +#include "JFrameProcessor.h" +#include "JFrameProcessorPlugin.h" #include "JSIJNIConversion.h" #include diff --git a/android/src/main/cpp/VisionCameraProxy.h b/android/src/main/cpp/frameprocessor/VisionCameraProxy.h similarity index 93% rename from android/src/main/cpp/VisionCameraProxy.h rename to android/src/main/cpp/frameprocessor/VisionCameraProxy.h index 65830b1..38fa457 100644 --- a/android/src/main/cpp/VisionCameraProxy.h +++ b/android/src/main/cpp/frameprocessor/VisionCameraProxy.h @@ -6,8 +6,8 @@ #include -#include "java-bindings/JVisionCameraScheduler.h" -#include "java-bindings/JVisionCameraProxy.h" +#include "JVisionCameraScheduler.h" +#include "JVisionCameraProxy.h" #include #include diff --git a/android/src/main/cpp/java-bindings/JFrame.cpp b/android/src/main/cpp/frameprocessor/java-bindings/JFrame.cpp similarity index 100% rename from android/src/main/cpp/java-bindings/JFrame.cpp rename to android/src/main/cpp/frameprocessor/java-bindings/JFrame.cpp diff --git a/android/src/main/cpp/java-bindings/JFrame.h b/android/src/main/cpp/frameprocessor/java-bindings/JFrame.h similarity index 100% rename from android/src/main/cpp/java-bindings/JFrame.h rename to android/src/main/cpp/frameprocessor/java-bindings/JFrame.h diff --git a/android/src/main/cpp/java-bindings/JFrameProcessor.cpp b/android/src/main/cpp/frameprocessor/java-bindings/JFrameProcessor.cpp similarity index 100% rename from android/src/main/cpp/java-bindings/JFrameProcessor.cpp rename to android/src/main/cpp/frameprocessor/java-bindings/JFrameProcessor.cpp diff --git a/android/src/main/cpp/java-bindings/JFrameProcessor.h b/android/src/main/cpp/frameprocessor/java-bindings/JFrameProcessor.h similarity index 100% rename from android/src/main/cpp/java-bindings/JFrameProcessor.h rename to android/src/main/cpp/frameprocessor/java-bindings/JFrameProcessor.h diff --git a/android/src/main/cpp/java-bindings/JFrameProcessorPlugin.cpp b/android/src/main/cpp/frameprocessor/java-bindings/JFrameProcessorPlugin.cpp similarity index 84% rename from android/src/main/cpp/java-bindings/JFrameProcessorPlugin.cpp rename to android/src/main/cpp/frameprocessor/java-bindings/JFrameProcessorPlugin.cpp index 9fa1d45..c958d09 100644 --- a/android/src/main/cpp/java-bindings/JFrameProcessorPlugin.cpp +++ b/android/src/main/cpp/frameprocessor/java-bindings/JFrameProcessorPlugin.cpp @@ -12,10 +12,10 @@ namespace vision { using namespace facebook; using namespace jni; -using TCallback = jobject(alias_ref, alias_ref params); +using TCallback = jobject(alias_ref, alias_ref> params); local_ref JFrameProcessorPlugin::callback(const alias_ref& frame, - const alias_ref& params) const { + const alias_ref>& params) const { auto callbackMethod = getClass()->getMethod("callback"); auto result = callbackMethod(self(), frame, params); diff --git a/android/src/main/cpp/java-bindings/JFrameProcessorPlugin.h b/android/src/main/cpp/frameprocessor/java-bindings/JFrameProcessorPlugin.h similarity index 78% rename from android/src/main/cpp/java-bindings/JFrameProcessorPlugin.h rename to android/src/main/cpp/frameprocessor/java-bindings/JFrameProcessorPlugin.h index f6f7090..b68872e 100644 --- a/android/src/main/cpp/java-bindings/JFrameProcessorPlugin.h +++ b/android/src/main/cpp/frameprocessor/java-bindings/JFrameProcessorPlugin.h @@ -7,7 +7,6 @@ #include #include #include -#include #include "JFrame.h" @@ -24,7 +23,7 @@ struct JFrameProcessorPlugin : public JavaClass { * Call the plugin. */ local_ref callback(const alias_ref& frame, - const alias_ref& params) const; + const alias_ref>& params) const; }; } // namespace vision diff --git a/android/src/main/cpp/java-bindings/JVisionCameraProxy.cpp b/android/src/main/cpp/frameprocessor/java-bindings/JVisionCameraProxy.cpp similarity index 97% rename from android/src/main/cpp/java-bindings/JVisionCameraProxy.cpp rename to android/src/main/cpp/frameprocessor/java-bindings/JVisionCameraProxy.cpp index cf48a32..c702c25 100644 --- a/android/src/main/cpp/java-bindings/JVisionCameraProxy.cpp +++ b/android/src/main/cpp/frameprocessor/java-bindings/JVisionCameraProxy.cpp @@ -9,7 +9,6 @@ #include #include -#include #include "FrameProcessorPluginHostObject.h" #include "JSITypedArray.h" @@ -24,7 +23,7 @@ namespace vision { using TSelf = local_ref::jhybriddata>; using TJSCallInvokerHolder = jni::alias_ref; using TScheduler = jni::alias_ref; -using TOptions = jni::local_ref; +using TOptions = jni::local_ref>; JVisionCameraProxy::JVisionCameraProxy(const jni::alias_ref& javaThis, jsi::Runtime* runtime, diff --git a/android/src/main/cpp/java-bindings/JVisionCameraProxy.h b/android/src/main/cpp/frameprocessor/java-bindings/JVisionCameraProxy.h similarity index 94% rename from android/src/main/cpp/java-bindings/JVisionCameraProxy.h rename to android/src/main/cpp/frameprocessor/java-bindings/JVisionCameraProxy.h index 9840e4f..713df73 100644 --- a/android/src/main/cpp/java-bindings/JVisionCameraProxy.h +++ b/android/src/main/cpp/frameprocessor/java-bindings/JVisionCameraProxy.h @@ -6,7 +6,6 @@ #include #include -#include #include #include "JFrameProcessorPlugin.h" @@ -34,7 +33,7 @@ class JVisionCameraProxy : public jni::HybridClass { const jsi::Object& frameProcessor); void removeFrameProcessor(int viewTag); jni::local_ref getFrameProcessorPlugin(const std::string& name, - jni::local_ref options); + jni::local_ref> options); jsi::Runtime* getJSRuntime() { return _runtime; } diff --git a/android/src/main/cpp/java-bindings/JVisionCameraScheduler.cpp b/android/src/main/cpp/frameprocessor/java-bindings/JVisionCameraScheduler.cpp similarity index 100% rename from android/src/main/cpp/java-bindings/JVisionCameraScheduler.cpp rename to android/src/main/cpp/frameprocessor/java-bindings/JVisionCameraScheduler.cpp diff --git a/android/src/main/cpp/java-bindings/JVisionCameraScheduler.h b/android/src/main/cpp/frameprocessor/java-bindings/JVisionCameraScheduler.h similarity index 100% rename from android/src/main/cpp/java-bindings/JVisionCameraScheduler.h rename to android/src/main/cpp/frameprocessor/java-bindings/JVisionCameraScheduler.h diff --git a/android/src/main/cpp/java-bindings/JArrayList.h b/android/src/main/cpp/java-bindings/JArrayList.h deleted file mode 100644 index 1b96134..0000000 --- a/android/src/main/cpp/java-bindings/JArrayList.h +++ /dev/null @@ -1,21 +0,0 @@ -// -// Created by Marc Rousavy on 24.06.21. -// - -#pragma once - -#include -#include - -namespace vision { - -using namespace facebook; -using namespace jni; - -// TODO: Remove when fbjni 0.2.3 releases. -template -struct JArrayList : JavaClass, JList> { - constexpr static auto kJavaDescriptor = "Ljava/util/ArrayList;"; -}; - -} // namespace vision diff --git a/android/src/main/cpp/java-bindings/JHashMap.cpp b/android/src/main/cpp/java-bindings/JHashMap.cpp deleted file mode 100644 index affd464..0000000 --- a/android/src/main/cpp/java-bindings/JHashMap.cpp +++ /dev/null @@ -1,20 +0,0 @@ -// -// Created by Marc Rousavy on 25.06.21. -// - -#include "JHashMap.h" - -#include -#include - -namespace vision { - -using namespace facebook; -using namespace jni; - -template -local_ref> JHashMap::create() { - return JHashMap::newInstance(); -} - -} // namespace vision diff --git a/android/src/main/cpp/java-bindings/JHashMap.h b/android/src/main/cpp/java-bindings/JHashMap.h deleted file mode 100644 index b11d3c8..0000000 --- a/android/src/main/cpp/java-bindings/JHashMap.h +++ /dev/null @@ -1,23 +0,0 @@ -// -// Created by Marc Rousavy on 25.06.21. -// - -#pragma once - -#include -#include - -namespace vision { - -using namespace facebook; -using namespace jni; - -// TODO: Remove when fbjni 0.2.3 releases. -template -struct JHashMap : JavaClass, JMap> { - constexpr static auto kJavaDescriptor = "Ljava/util/HashMap;"; - - static local_ref> create(); -}; - -} // namespace vision diff --git a/android/src/main/java/com/mrousavy/camera/frameprocessor/FrameProcessorPlugin.java b/android/src/main/java/com/mrousavy/camera/frameprocessor/FrameProcessorPlugin.java index c9119f8..4793706 100644 --- a/android/src/main/java/com/mrousavy/camera/frameprocessor/FrameProcessorPlugin.java +++ b/android/src/main/java/com/mrousavy/camera/frameprocessor/FrameProcessorPlugin.java @@ -4,7 +4,7 @@ import androidx.annotation.Keep; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import com.facebook.proguard.annotations.DoNotStrip; -import com.facebook.react.bridge.ReadableNativeMap; +import java.util.Map; /** * Declares a Frame Processor Plugin. @@ -21,5 +21,5 @@ public abstract class FrameProcessorPlugin { */ @DoNotStrip @Keep - public abstract @Nullable Object callback(@NonNull Frame frame, @Nullable ReadableNativeMap params); + public abstract @Nullable Object callback(@NonNull Frame frame, @Nullable Map params); } diff --git a/android/src/main/java/com/mrousavy/camera/frameprocessor/FrameProcessorPluginRegistry.java b/android/src/main/java/com/mrousavy/camera/frameprocessor/FrameProcessorPluginRegistry.java index ecbd2a2..3a3c618 100644 --- a/android/src/main/java/com/mrousavy/camera/frameprocessor/FrameProcessorPluginRegistry.java +++ b/android/src/main/java/com/mrousavy/camera/frameprocessor/FrameProcessorPluginRegistry.java @@ -2,12 +2,9 @@ package com.mrousavy.camera.frameprocessor; import androidx.annotation.Keep; import androidx.annotation.Nullable; - import com.facebook.proguard.annotations.DoNotStrip; -import com.facebook.react.bridge.ReadableNativeMap; - -import java.util.HashMap; import java.util.Map; +import java.util.HashMap; @DoNotStrip @Keep @@ -24,7 +21,7 @@ public class FrameProcessorPluginRegistry { @DoNotStrip @Keep - public static FrameProcessorPlugin getPlugin(String name, ReadableNativeMap options) { + public static FrameProcessorPlugin getPlugin(String name, Map options) { PluginInitializer initializer = Plugins.get(name); if (initializer == null) { return null; @@ -33,6 +30,6 @@ public class FrameProcessorPluginRegistry { } public interface PluginInitializer { - FrameProcessorPlugin initializePlugin(@Nullable ReadableNativeMap options); + FrameProcessorPlugin initializePlugin(@Nullable Map options); } } diff --git a/android/src/main/java/com/mrousavy/camera/frameprocessor/VisionCameraProxy.kt b/android/src/main/java/com/mrousavy/camera/frameprocessor/VisionCameraProxy.kt index 30b42e8..5bf11a3 100644 --- a/android/src/main/java/com/mrousavy/camera/frameprocessor/VisionCameraProxy.kt +++ b/android/src/main/java/com/mrousavy/camera/frameprocessor/VisionCameraProxy.kt @@ -6,7 +6,6 @@ import androidx.annotation.UiThread import com.facebook.jni.HybridData import com.facebook.proguard.annotations.DoNotStrip import com.facebook.react.bridge.ReactApplicationContext -import com.facebook.react.bridge.ReadableNativeMap import com.facebook.react.bridge.UiThreadUtil import com.facebook.react.turbomodule.core.CallInvokerHolderImpl import com.facebook.react.uimanager.UIManagerHelper @@ -14,7 +13,6 @@ import com.mrousavy.camera.CameraView import com.mrousavy.camera.ViewNotFoundError import java.lang.ref.WeakReference - @Suppress("KotlinJniMissingFunction") // we use fbjni. class VisionCameraProxy(context: ReactApplicationContext) { companion object { @@ -71,7 +69,7 @@ class VisionCameraProxy(context: ReactApplicationContext) { @DoNotStrip @Keep - fun getFrameProcessorPlugin(name: String, options: ReadableNativeMap): FrameProcessorPlugin { + fun getFrameProcessorPlugin(name: String, options: Map): FrameProcessorPlugin { return FrameProcessorPluginRegistry.getPlugin(name, options) } diff --git a/docs/docs/guides/FRAME_PROCESSORS_CREATE_OVERVIEW.mdx b/docs/docs/guides/FRAME_PROCESSORS_CREATE_OVERVIEW.mdx index 13fe39d..b452c2d 100644 --- a/docs/docs/guides/FRAME_PROCESSORS_CREATE_OVERVIEW.mdx +++ b/docs/docs/guides/FRAME_PROCESSORS_CREATE_OVERVIEW.mdx @@ -39,8 +39,8 @@ Similar to a TurboModule, the Frame Processor Plugin Registry API automatically | `number` | `NSNumber*` (double) | `Double` | | `boolean` | `NSNumber*` (boolean) | `Boolean` | | `string` | `NSString*` | `String` | -| `[]` | `NSArray*` | `ReadableNativeArray` | -| `{}` | `NSDictionary*` | `ReadableNativeMap` | +| `[]` | `NSArray*` | `List` | +| `{}` | `NSDictionary*` | `Map` | | `undefined` / `null` | `nil` | `null` | | `(any, any) => void` | [`RCTResponseSenderBlock`][4] | `(Object, Object) -> void` | | [`Frame`][1] | [`Frame*`][2] | [`Frame`][3] | diff --git a/docs/docs/guides/FRAME_PROCESSOR_CREATE_PLUGIN_ANDROID.mdx b/docs/docs/guides/FRAME_PROCESSOR_CREATE_PLUGIN_ANDROID.mdx index 6de0deb..681b5db 100644 --- a/docs/docs/guides/FRAME_PROCESSOR_CREATE_PLUGIN_ANDROID.mdx +++ b/docs/docs/guides/FRAME_PROCESSOR_CREATE_PLUGIN_ANDROID.mdx @@ -61,7 +61,7 @@ import com.mrousavy.camera.frameprocessor.FrameProcessorPlugin; public class FaceDetectorFrameProcessorPlugin extends FrameProcessorPlugin { @Override - public Object callback(Frame frame, ReadableNativeMap arguments) { + public Object callback(Frame frame, Map arguments) { // code goes here return null; } @@ -126,7 +126,7 @@ import com.mrousavy.camera.frameprocessor.FrameProcessorPlugin class FaceDetectorFrameProcessorPlugin: FrameProcessorPlugin() { - override fun callback(frame: Frame, arguments: ReadableNativeMap): Any? { + override fun callback(frame: Frame, arguments: Map): Any? { // code goes here return null } diff --git a/example/android/app/src/main/java/com/mrousavy/camera/example/ExampleFrameProcessorPlugin.java b/example/android/app/src/main/java/com/mrousavy/camera/example/ExampleFrameProcessorPlugin.java index 376ea16..d877807 100644 --- a/example/android/app/src/main/java/com/mrousavy/camera/example/ExampleFrameProcessorPlugin.java +++ b/example/android/app/src/main/java/com/mrousavy/camera/example/ExampleFrameProcessorPlugin.java @@ -3,40 +3,40 @@ package com.mrousavy.camera.example; import android.media.Image; import android.util.Log; -import com.facebook.react.bridge.ReadableNativeMap; -import com.facebook.react.bridge.WritableNativeArray; -import com.facebook.react.bridge.WritableNativeMap; import com.mrousavy.camera.frameprocessor.Frame; import com.mrousavy.camera.frameprocessor.FrameProcessorPlugin; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; +import java.util.Map; public class ExampleFrameProcessorPlugin extends FrameProcessorPlugin { @Override - public Object callback(@NotNull Frame frame, @Nullable ReadableNativeMap params) { - HashMap hashMap = params != null ? params.toHashMap() : new HashMap<>(); + public Object callback(@NotNull Frame frame, @Nullable Map params) { + if (params == null) return null; Image image = frame.getImage(); - Log.d("ExamplePlugin", image.getWidth() + " x " + image.getHeight() + " Image with format #" + image.getFormat() + ". Logging " + hashMap.size() + " parameters:"); + Log.d("ExamplePlugin", image.getWidth() + " x " + image.getHeight() + " Image with format #" + image.getFormat() + ". Logging " + params.size() + " parameters:"); - for (String key : hashMap.keySet()) { - Object value = hashMap.get(key); - Log.d("ExamplePlugin", " -> " + (value == null ? "(null)" : value.toString() + " (" + value.getClass().getName() + ")")); + for (String key : params.keySet()) { + Object value = params.get(key); + Log.d("ExamplePlugin", " -> " + (value == null ? "(null)" : value + " (" + value.getClass().getName() + ")")); } - WritableNativeMap map = new WritableNativeMap(); - map.putString("example_str", "Test"); - map.putBoolean("example_bool", true); - map.putDouble("example_double", 5.3); + Map map = new HashMap<>(); + map.put("example_str", "Test"); + map.put("example_bool", true); + map.put("example_double", 5.3); - WritableNativeArray array = new WritableNativeArray(); - array.pushString("Hello!"); - array.pushBoolean(true); - array.pushDouble(17.38); + List array = new ArrayList<>(); + array.add("Hello!"); + array.add(true); + array.add(17.38); - map.putArray("example_array", array); + map.put("example_array", array); return map; } diff --git a/example/android/app/src/main/java/com/mrousavy/camera/example/MainApplication.java b/example/android/app/src/main/java/com/mrousavy/camera/example/MainApplication.java index ba7e6fb..60248d3 100644 --- a/example/android/app/src/main/java/com/mrousavy/camera/example/MainApplication.java +++ b/example/android/app/src/main/java/com/mrousavy/camera/example/MainApplication.java @@ -8,7 +8,6 @@ import com.facebook.react.PackageList; import com.facebook.react.ReactApplication; import com.facebook.react.ReactNativeHost; import com.facebook.react.ReactPackage; -import com.facebook.react.bridge.ReadableNativeMap; import com.facebook.soloader.SoLoader; import java.util.List; import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint;