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
This commit is contained in:
Ahmed El-Helw 2016-04-11 12:50:35 -07:00
parent 5d6e73d0eb
commit cbf195e165
2 changed files with 11 additions and 5 deletions

View File

@ -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);
}
}

View File

@ -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);
}
}
}