feat: Replace *NativeMap and *NativeArray with Map<K,V> and List<T> for faster JSI -> JNI calls (#1720)

Replaces `ReadableNativeMap`/`WritableNativeMap` with `Map<String, Object>` and `ReadableNativeArray`/`WritableNativeArray` with `List<Object>`, making the JSI -> JNI conversion a bit faster and more logical.
Also, we could now convert Array Buffers or HostObjects if we wanted to.
This commit is contained in:
Marc Rousavy
2023-08-25 12:22:44 +02:00
committed by GitHub
parent f87bc74de1
commit dfb86e174b
30 changed files with 125 additions and 167 deletions

View File

@@ -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<String, Object> hashMap = params != null ? params.toHashMap() : new HashMap<>();
public Object callback(@NotNull Frame frame, @Nullable Map<String, Object> 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<String, Object> 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<Object> 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;
}

View File

@@ -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;