fix: Improve Android resource efficiency/cleanup (use class members for CoroutineScope
and FrameProcessorThread
) (#335)
* fix: Use custom CoroutineScope * fix: Use custom `CoroutineScope` * Make `frameProcessorThread` and `coroutineScope` instance variables * Update VisionCameraScheduler.java * Remove `HybridData::resetNative()` calls they're called by a Java GC destructor anyways. * Update CameraViewManager.kt * Update CameraView.kt
This commit is contained in:
@@ -39,14 +39,6 @@ public abstract class FrameProcessorPlugin {
|
||||
mHybridData = initHybrid(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void finalize() throws Throwable {
|
||||
super.finalize();
|
||||
if (mHybridData != null) {
|
||||
mHybridData.resetNative();
|
||||
}
|
||||
}
|
||||
|
||||
private native @NonNull HybridData initHybrid(@NonNull String name);
|
||||
|
||||
/**
|
||||
|
@@ -10,9 +10,10 @@ import com.mrousavy.camera.CameraView
|
||||
import com.mrousavy.camera.ViewNotFoundError
|
||||
import com.swmansion.reanimated.Scheduler
|
||||
import java.lang.ref.WeakReference
|
||||
import java.util.concurrent.ExecutorService
|
||||
|
||||
@Suppress("KotlinJniMissingFunction") // I use fbjni, Android Studio is not smart enough to realize that.
|
||||
class FrameProcessorRuntimeManager(context: ReactApplicationContext) {
|
||||
class FrameProcessorRuntimeManager(context: ReactApplicationContext, frameProcessorThread: ExecutorService) {
|
||||
companion object {
|
||||
const val TAG = "FrameProcessorRuntime"
|
||||
val Plugins: ArrayList<FrameProcessorPlugin> = ArrayList()
|
||||
@@ -30,7 +31,7 @@ class FrameProcessorRuntimeManager(context: ReactApplicationContext) {
|
||||
|
||||
init {
|
||||
val holder = context.catalystInstance.jsCallInvokerHolder as CallInvokerHolderImpl
|
||||
mScheduler = VisionCameraScheduler()
|
||||
mScheduler = VisionCameraScheduler(frameProcessorThread)
|
||||
mContext = WeakReference(context)
|
||||
mHybridData = initHybrid(context.javaScriptContextHolder.get(), holder, mScheduler)
|
||||
initializeRuntime()
|
||||
@@ -42,10 +43,7 @@ class FrameProcessorRuntimeManager(context: ReactApplicationContext) {
|
||||
Log.i(TAG, "Successfully installed ${Plugins.count()} Frame Processor Plugins!")
|
||||
}
|
||||
|
||||
fun destroy() {
|
||||
mHybridData.resetNative()
|
||||
}
|
||||
|
||||
@Suppress("unused")
|
||||
@DoNotStrip
|
||||
@Keep
|
||||
fun findCameraViewById(viewId: Int): CameraView {
|
||||
|
@@ -2,28 +2,26 @@ package com.mrousavy.camera.frameprocessor;
|
||||
|
||||
import com.facebook.jni.HybridData;
|
||||
import com.facebook.proguard.annotations.DoNotStrip;
|
||||
import com.mrousavy.camera.CameraViewModule;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
|
||||
@SuppressWarnings("JavaJniMissingFunction") // using fbjni here
|
||||
public class VisionCameraScheduler {
|
||||
@SuppressWarnings({"unused", "FieldCanBeLocal"})
|
||||
@DoNotStrip
|
||||
private final HybridData mHybridData;
|
||||
private final ExecutorService frameProcessorThread;
|
||||
|
||||
public VisionCameraScheduler() {
|
||||
public VisionCameraScheduler(ExecutorService frameProcessorThread) {
|
||||
this.frameProcessorThread = frameProcessorThread;
|
||||
mHybridData = initHybrid();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void finalize() throws Throwable {
|
||||
mHybridData.resetNative();
|
||||
super.finalize();
|
||||
}
|
||||
|
||||
private native HybridData initHybrid();
|
||||
private native void triggerUI();
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
@DoNotStrip
|
||||
private void scheduleTrigger() {
|
||||
CameraViewModule.Companion.getFrameProcessorThread().submit(this::triggerUI);
|
||||
frameProcessorThread.submit(this::triggerUI);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user