Skip to content

Commit

Permalink
Fallback to bitmap again!
Browse files Browse the repository at this point in the history
  • Loading branch information
magicismight committed Jan 8, 2017
1 parent 074166b commit 1623122
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 50 deletions.
22 changes: 19 additions & 3 deletions android/src/main/java/com/horcrux/svg/SvgView.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
import javax.annotation.Nullable;

/**
* Custom {@link View} implementation that draws an RNSVGSvg React view and its \children.
* Custom {@link View} implementation that draws an RNSVGSvg React view and its children.
*/
public class SvgView extends TextureView {
public class SvgView extends View {
public enum Events {
EVENT_DATA_URL("onDataURL");

Expand All @@ -49,6 +49,7 @@ public String toString() {
}
}

private @Nullable Bitmap mBitmap;
private RCTEventEmitter mEventEmitter;
private EventDispatcher mEventDispatcher;
private int mTargetTag;
Expand All @@ -58,11 +59,26 @@ public String toString() {

public SvgView(ReactContext reactContext) {
super(reactContext);
setOpaque(false);
mEventEmitter = reactContext.getJSModule(RCTEventEmitter.class);
mEventDispatcher = reactContext.getNativeModule(UIManagerModule.class).getEventDispatcher();
}

public void setBitmap(Bitmap bitmap) {
if (mBitmap != null) {
mBitmap.recycle();
}
mBitmap = bitmap;
invalidate();
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (mBitmap != null) {
canvas.drawBitmap(mBitmap, 0, 0, null);
}
}

private SvgViewShadowNode getShadowNode() {
return SvgViewShadowNode.getShadowNodeByTag(getId());
}
Expand Down
2 changes: 1 addition & 1 deletion android/src/main/java/com/horcrux/svg/SvgViewManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ protected SvgView createViewInstance(ThemedReactContext reactContext) {

@Override
public void updateExtraData(SvgView root, Object extraData) {
root.setSurfaceTextureListener((SvgViewShadowNode) extraData);
root.setBitmap((Bitmap) extraData);
}

@Override
Expand Down
56 changes: 10 additions & 46 deletions android/src/main/java/com/horcrux/svg/SvgViewShadowNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
/**
* Shadow node for RNSVG virtual tree root - RNSVGSvgView
*/
public class SvgViewShadowNode extends LayoutShadowNode implements TextureView.SurfaceTextureListener {
public class SvgViewShadowNode extends LayoutShadowNode {

private static final SparseArray<SvgViewShadowNode> mTagToShadowNode = new SparseArray<>();
private @Nullable Surface mSurface;
Expand Down Expand Up @@ -64,8 +64,7 @@ public boolean isVirtualAnchor() {
@Override
public void onCollectExtraUpdates(UIViewOperationQueue uiUpdater) {
super.onCollectExtraUpdates(uiUpdater);
drawOutput();
uiUpdater.enqueueUpdateExtraData(getReactTag(), this);
uiUpdater.enqueueUpdateExtraData(getReactTag(), drawOutput());
}

@Override
Expand All @@ -74,52 +73,17 @@ public void setReactTag(int reactTag) {
mTagToShadowNode.put(getReactTag(), this);
}

public void drawOutput() {
if (mSurface == null || !mSurface.isValid()) {
markChildrenUpdatesSeen(this);
return;
}

try {
Canvas canvas = mSurface.lockCanvas(null);
drawChildren(canvas);

if (mSurface != null) {
mSurface.unlockCanvasAndPost(canvas);
}

} catch (IllegalArgumentException | IllegalStateException e) {
FLog.e(ReactConstants.TAG, e.getClass().getSimpleName() + " in Svg.unlockCanvasAndPost");
}
}

private void markChildrenUpdatesSeen(ReactShadowNode shadowNode) {
for (int i = 0; i < shadowNode.getChildCount(); i++) {
ReactShadowNode child = shadowNode.getChildAt(i);
child.markUpdateSeen();
markChildrenUpdatesSeen(child);
}
}

@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
mSurface = new Surface(surface);
drawOutput();
}
public Object drawOutput() {
Bitmap bitmap = Bitmap.createBitmap(
(int) getLayoutWidth(),
(int) getLayoutHeight(),
Bitmap.Config.ARGB_8888);

@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
surface.release();
mSurface = null;
return true;
Canvas canvas = new Canvas(bitmap);
drawChildren(canvas);
return bitmap;
}

@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {}

@Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {}

private void drawChildren(Canvas canvas) {
canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
Paint paint = new Paint();
Expand Down

0 comments on commit 1623122

Please sign in to comment.