From cbf195e1658668b0b8cbcb52f5629c3417c8eccf Mon Sep 17 00:00:00 2001 From: Ahmed El-Helw Date: Mon, 11 Apr 2016 12:50:35 -0700 Subject: [PATCH] Do less work in Nodes when clipping isn't needed Summary: By default, Nodes causes views to not be clipped, unless overflow is explicitly set to hidden. Consequently, Nodes sets all the clipping bounds to negative infinity, and does some extra work (saving the canvas layer, clipping, etc) before drawing. This optimization skips the extra work when it's not needed. Reviewed By: sriramramani Differential Revision: D3161268 --- .../react/flat/AbstractClippingDrawCommand.java | 4 +++- .../main/java/com/facebook/react/flat/DrawView.java | 12 ++++++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/flat/AbstractClippingDrawCommand.java b/ReactAndroid/src/main/java/com/facebook/react/flat/AbstractClippingDrawCommand.java index 2919b52e9..e3a4298d8 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/flat/AbstractClippingDrawCommand.java +++ b/ReactAndroid/src/main/java/com/facebook/react/flat/AbstractClippingDrawCommand.java @@ -13,6 +13,7 @@ import android.graphics.Canvas; /* package */ abstract class AbstractClippingDrawCommand implements DrawCommand { + protected boolean mNeedsClipping; private float mClipLeft; private float mClipTop; private float mClipRight; @@ -36,6 +37,7 @@ import android.graphics.Canvas; mClipTop = clipTop; mClipRight = clipRight; mClipBottom = clipBottom; + mNeedsClipping = mClipLeft != Float.NEGATIVE_INFINITY; } public final float getClipLeft() { @@ -60,7 +62,7 @@ import android.graphics.Canvas; // shows up during screenshot testing. Note that checking one side is enough, since if one side // is infinite, all sides will be infinite, since we only set infinite for all sides at the // same time - conversely, if one side is finite, all sides will be finite. - if (mClipLeft != Float.NEGATIVE_INFINITY) { + if (mNeedsClipping) { canvas.clipRect(mClipLeft, mClipTop, mClipRight, mClipBottom); } } diff --git a/ReactAndroid/src/main/java/com/facebook/react/flat/DrawView.java b/ReactAndroid/src/main/java/com/facebook/react/flat/DrawView.java index dc811a2a8..350506cd3 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/flat/DrawView.java +++ b/ReactAndroid/src/main/java/com/facebook/react/flat/DrawView.java @@ -21,9 +21,13 @@ import android.graphics.Canvas; @Override public void draw(FlatViewGroup parent, Canvas canvas) { - canvas.save(); - applyClipping(canvas); - parent.drawNextChild(canvas); - canvas.restore(); + if (mNeedsClipping) { + canvas.save(); + applyClipping(canvas); + parent.drawNextChild(canvas); + canvas.restore(); + } else { + parent.drawNextChild(canvas); + } } }