Skip to content

Commit

Permalink
Merge pull request #70 from Shopify/bugfix/15-fix-android-viewmanager
Browse files Browse the repository at this point in the history
Fix crashes in android when removing/recreating views and surfaces
  • Loading branch information
chrfalch authored Dec 30, 2021
2 parents 4451bcf + b104467 commit 51c0626
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
4 changes: 3 additions & 1 deletion package/android/cpp/jni/JniSkiaDrawView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,9 @@ namespace RNSkia
return false;
}

if (!_skRenderTarget.isValid() || _prevWidth != _width ||
if (_skSurface == nullptr ||
!_skRenderTarget.isValid() ||
_prevWidth != _width ||
_prevHeight != _height)
{
RNSkMeasureTime measure =
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -10,10 +9,11 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.HashMap;

public class RNSkiaViewManager extends BaseViewManager<SkiaDrawView, LayoutShadowNode> {

private SkiaDrawView mView;
private int mNativeId;
final private HashMap<SkiaDrawView, Integer> mViewMapping = new HashMap();

@NonNull
@Override
Expand All @@ -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")
Expand All @@ -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);
}
}
4 changes: 4 additions & 0 deletions package/cpp/rnskia/RNSkDrawView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@ void RNSkDrawView::drawInSurface(sk_sp<SkSurface> surface, int width,
std::shared_ptr<RNSkPlatformContext> context) {

try {
if(getIsRemoved()) {
return;
}

// Get the canvas
auto skCanvas = surface->getCanvas();
_jsiCanvas->setCanvas(skCanvas);
Expand Down

0 comments on commit 51c0626

Please sign in to comment.