retain java value

This commit is contained in:
王劲鹏 2021-12-30 14:24:19 +08:00 committed by osborn
parent 7f39727cf4
commit c67803b717
3 changed files with 42 additions and 1 deletions

View File

@ -29,6 +29,7 @@ import com.github.pengfeizhou.jscore.JSONBuilder;
import org.json.JSONObject; import org.json.JSONObject;
import java.lang.ref.SoftReference;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
@ -69,6 +70,7 @@ public class DoricContext {
private final DoricPerformanceProfile performanceProfile; private final DoricPerformanceProfile performanceProfile;
private final Map<String, Animator> animators = new HashMap<>(); private final Map<String, Animator> animators = new HashMap<>();
private final Map<String, DoricResource> cachedResources = new WeakHashMap<>(); private final Map<String, DoricResource> cachedResources = new WeakHashMap<>();
private final ArrayList<SoftReference<RetainedJavaValue>> retainedJavaValues = new ArrayList<>();
public Collection<ViewNode<?>> allHeadNodes(String type) { public Collection<ViewNode<?>> allHeadNodes(String type) {
Map<String, ViewNode<?>> headNode = mHeadNodes.get(type); Map<String, ViewNode<?>> headNode = mHeadNodes.get(type);
@ -223,6 +225,8 @@ public class DoricContext {
return null; return null;
} }
}, ThreadMode.UI); }, ThreadMode.UI);
retainedJavaValues.clear();
} }
}); });
DoricContextManager.getInstance().destroyContext(this); DoricContextManager.getInstance().destroyContext(this);
@ -367,4 +371,12 @@ public class DoricContext {
public DoricResource getCachedResource(String resId) { public DoricResource getCachedResource(String resId) {
return this.cachedResources.get(resId); return this.cachedResources.get(resId);
} }
public void retainJavaValue(SoftReference<RetainedJavaValue> retainedJavaValue) {
retainedJavaValues.add(retainedJavaValue);
}
public void releaseJavaValue(SoftReference<RetainedJavaValue> retainedJavaValue) {
retainedJavaValues.remove(retainedJavaValue);
}
} }

View File

@ -0,0 +1,25 @@
package pub.doric;
import com.github.pengfeizhou.jscore.JavaValue;
import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
public class RetainedJavaValue extends JavaValue {
private final WeakReference<DoricContext> mDoricContext;
public RetainedJavaValue(WeakReference<DoricContext> doricContext, byte[] data) {
super(data);
this.mDoricContext = doricContext;
this.mDoricContext.get().retainJavaValue(new SoftReference<>(this));
this.memoryReleaser = new MemoryReleaser() {
@Override
public void deallocate(byte[] data) {
if (doricContext.get() != null) {
doricContext.get().releaseJavaValue(new SoftReference<>(RetainedJavaValue.this));
}
}
};
}
}

View File

@ -22,9 +22,11 @@ import com.github.pengfeizhou.jscore.JSONBuilder;
import com.github.pengfeizhou.jscore.JSObject; import com.github.pengfeizhou.jscore.JSObject;
import com.github.pengfeizhou.jscore.JavaValue; import com.github.pengfeizhou.jscore.JavaValue;
import java.lang.ref.WeakReference;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import pub.doric.DoricContext; import pub.doric.DoricContext;
import pub.doric.RetainedJavaValue;
import pub.doric.async.AsyncResult; import pub.doric.async.AsyncResult;
import pub.doric.extension.bridge.DoricMethod; import pub.doric.extension.bridge.DoricMethod;
import pub.doric.extension.bridge.DoricPlugin; import pub.doric.extension.bridge.DoricPlugin;
@ -95,7 +97,9 @@ public class ImageDecoderPlugin extends DoricJavaPlugin {
Bitmap bitmap = BitmapFactory.decodeByteArray(rawData, 0, rawData.length); Bitmap bitmap = BitmapFactory.decodeByteArray(rawData, 0, rawData.length);
ByteBuffer buffer = ByteBuffer.allocate(bitmap.getByteCount()); ByteBuffer buffer = ByteBuffer.allocate(bitmap.getByteCount());
bitmap.copyPixelsToBuffer(buffer); bitmap.copyPixelsToBuffer(buffer);
promise.resolve(new JavaValue(buffer.array()));
RetainedJavaValue retainedJavaValue = new RetainedJavaValue(new WeakReference<DoricContext>(getDoricContext()), buffer.array());
promise.resolve(retainedJavaValue);
} }
@Override @Override