From 8cd1ce368af41d0135fdcd50de42d7b218e7e210 Mon Sep 17 00:00:00 2001 From: chrfalch Date: Fri, 24 Dec 2021 12:20:47 +0100 Subject: [PATCH 1/3] Fixed issue with storing nativeId and view as private members of the view manager. --- .../reactnative/skia/RNSkiaViewManager.java | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/package/android/src/main/java/com/shopify/reactnative/skia/RNSkiaViewManager.java b/package/android/src/main/java/com/shopify/reactnative/skia/RNSkiaViewManager.java index a523451935..6ed1bd76ea 100644 --- a/package/android/src/main/java/com/shopify/reactnative/skia/RNSkiaViewManager.java +++ b/package/android/src/main/java/com/shopify/reactnative/skia/RNSkiaViewManager.java @@ -1,6 +1,5 @@ package com.shopify.reactnative.skia; -import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.bridge.ReactContext; import com.facebook.react.uimanager.BaseViewManager; import com.facebook.react.uimanager.LayoutShadowNode; @@ -10,10 +9,11 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import java.util.HashMap; + public class RNSkiaViewManager extends BaseViewManager { - private SkiaDrawView mView; - private int mNativeId; + final private HashMap mViewMapping = new HashMap(); @NonNull @Override @@ -38,9 +38,10 @@ public void updateExtraData(SkiaDrawView root, Object extraData) { @Override public void setNativeId(@NonNull SkiaDrawView view, @Nullable String nativeId) { super.setNativeId(view, nativeId); - mNativeId = Integer.parseInt(nativeId); - RNSkiaModule skiaModule = ((ReactContext)mView.getContext()).getNativeModule(RNSkiaModule.class); - skiaModule.getSkiaManager().register(mNativeId, mView); + int nativeIdResolved = Integer.parseInt(nativeId); + RNSkiaModule skiaModule = ((ReactContext)view.getContext()).getNativeModule(RNSkiaModule.class); + skiaModule.getSkiaManager().register(nativeIdResolved, view); + mViewMapping.put(view, nativeIdResolved); } @ReactProp(name = "mode") @@ -57,14 +58,15 @@ public void setDebug(SkiaDrawView view, boolean show) { public void onDropViewInstance(@NonNull SkiaDrawView view) { super.onDropViewInstance(view); RNSkiaModule skiaModule = ((ReactContext)view.getContext()).getNativeModule(RNSkiaModule.class); - skiaModule.getSkiaManager().unregister(mNativeId); + Integer nativeId = mViewMapping.get(view); + skiaModule.getSkiaManager().unregister(nativeId); + mViewMapping.remove(view); view.onRemoved(); } @NonNull @Override protected SkiaDrawView createViewInstance(@NonNull ThemedReactContext reactContext) { - mView = new SkiaDrawView(reactContext); - return mView; + return new SkiaDrawView(reactContext); } } \ No newline at end of file From 52aaa0771d40c001ef09847fa16963af34e06067 Mon Sep 17 00:00:00 2001 From: chrfalch Date: Fri, 24 Dec 2021 12:29:19 +0100 Subject: [PATCH 2/3] Added check for removed in drawFrame --- package/cpp/rnskia/RNSkDrawView.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/package/cpp/rnskia/RNSkDrawView.cpp b/package/cpp/rnskia/RNSkDrawView.cpp index 6f0aff6dfd..7206069e29 100644 --- a/package/cpp/rnskia/RNSkDrawView.cpp +++ b/package/cpp/rnskia/RNSkDrawView.cpp @@ -134,6 +134,10 @@ void RNSkDrawView::drawInSurface(sk_sp surface, int width, std::shared_ptr context) { try { + if(getIsRemoved()) { + return; + } + // Get the canvas auto skCanvas = surface->getCanvas(); _jsiCanvas->setCanvas(skCanvas); From b104467296c11c5c7f378cdc698baded910c6833 Mon Sep 17 00:00:00 2001 From: chrfalch Date: Fri, 24 Dec 2021 12:53:09 +0100 Subject: [PATCH 3/3] Fixed recreation of skSurface after surfaceDestroy When the surface is invalidated (view is not visible) and the surface is available again, the skia surface needs to be recreated. Added a test to check for null _skSurface when surface is available. --- package/android/cpp/jni/JniSkiaDrawView.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/package/android/cpp/jni/JniSkiaDrawView.cpp b/package/android/cpp/jni/JniSkiaDrawView.cpp index a1423c9d46..e75855e2ff 100644 --- a/package/android/cpp/jni/JniSkiaDrawView.cpp +++ b/package/android/cpp/jni/JniSkiaDrawView.cpp @@ -251,7 +251,9 @@ namespace RNSkia return false; } - if (!_skRenderTarget.isValid() || _prevWidth != _width || + if (_skSurface == nullptr || + !_skRenderTarget.isValid() || + _prevWidth != _width || _prevHeight != _height) { RNSkMeasureTime measure =