Merge "Avoid allocation in methods of Outline" into nyc-dev

This commit is contained in:
Chris Craik
2016-03-17 17:21:32 +00:00
committed by Android (Google) Code Review

View File

@@ -17,10 +17,13 @@
package android.graphics; package android.graphics;
import android.annotation.FloatRange; import android.annotation.FloatRange;
import android.annotation.IntDef;
import android.annotation.NonNull; import android.annotation.NonNull;
import android.annotation.Nullable;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
/** /**
* Defines a simple shape, used for bounding graphical regions. * Defines a simple shape, used for bounding graphical regions.
* <p> * <p>
@@ -34,11 +37,28 @@ import android.graphics.drawable.Drawable;
public final class Outline { public final class Outline {
private static final float RADIUS_UNDEFINED = Float.NEGATIVE_INFINITY; private static final float RADIUS_UNDEFINED = Float.NEGATIVE_INFINITY;
/** @hide */ private static final int MODE_EMPTY = 0;
public Path mPath; private static final int MODE_RECT = 1;
private static final int MODE_CONVEX_PATH = 2;
/** @hide */ /** @hide */
public Rect mRect; @Retention(RetentionPolicy.SOURCE)
@IntDef(flag = false,
value = {
MODE_EMPTY,
MODE_RECT,
MODE_CONVEX_PATH,
})
public @interface Mode {}
@Mode
private int mMode = MODE_EMPTY;
/** @hide */
public final Path mPath = new Path();
/** @hide */
public final Rect mRect = new Rect();
/** @hide */ /** @hide */
public float mRadius = RADIUS_UNDEFINED; public float mRadius = RADIUS_UNDEFINED;
/** @hide */ /** @hide */
@@ -63,8 +83,9 @@ public final class Outline {
* @see #isEmpty() * @see #isEmpty()
*/ */
public void setEmpty() { public void setEmpty() {
mPath = null; mMode = MODE_EMPTY;
mRect = null; mPath.rewind();
mRect.setEmpty();
mRadius = RADIUS_UNDEFINED; mRadius = RADIUS_UNDEFINED;
} }
@@ -77,7 +98,7 @@ public final class Outline {
* @see #setEmpty() * @see #setEmpty()
*/ */
public boolean isEmpty() { public boolean isEmpty() {
return mRect == null && mPath == null; return mMode == MODE_EMPTY;
} }
@@ -90,7 +111,7 @@ public final class Outline {
* @see {@link android.view.View#setClipToOutline(boolean)} * @see {@link android.view.View#setClipToOutline(boolean)}
*/ */
public boolean canClip() { public boolean canClip() {
return !isEmpty() && mRect != null; return mMode != MODE_CONVEX_PATH;
} }
/** /**
@@ -122,19 +143,9 @@ public final class Outline {
* @param src Source outline to copy from. * @param src Source outline to copy from.
*/ */
public void set(@NonNull Outline src) { public void set(@NonNull Outline src) {
if (src.mPath != null) { mMode = src.mMode;
if (mPath == null) { mPath.set(src.mPath);
mPath = new Path(); mRect.set(src.mRect);
}
mPath.set(src.mPath);
mRect = null;
}
if (src.mRect != null) {
if (mRect == null) {
mRect = new Rect();
}
mRect.set(src.mRect);
}
mRadius = src.mRadius; mRadius = src.mRadius;
mAlpha = src.mAlpha; mAlpha = src.mAlpha;
} }
@@ -165,10 +176,10 @@ public final class Outline {
return; return;
} }
if (mRect == null) mRect = new Rect(); mMode = MODE_RECT;
mRect.set(left, top, right, bottom); mRect.set(left, top, right, bottom);
mRadius = radius; mRadius = radius;
mPath = null; mPath.rewind();
} }
/** /**
@@ -188,7 +199,7 @@ public final class Outline {
* bounds, or {@code false} if no outline bounds are set * bounds, or {@code false} if no outline bounds are set
*/ */
public boolean getRect(@NonNull Rect outRect) { public boolean getRect(@NonNull Rect outRect) {
if (mRect == null) { if (mMode != MODE_RECT) {
return false; return false;
} }
outRect.set(mRect); outRect.set(mRect);
@@ -221,10 +232,10 @@ public final class Outline {
return; return;
} }
if (mPath == null) mPath = new Path(); mMode = MODE_CONVEX_PATH;
mPath.reset(); mPath.rewind();
mPath.addOval(left, top, right, bottom, Path.Direction.CW); mPath.addOval(left, top, right, bottom, Path.Direction.CW);
mRect = null; mRect.setEmpty();
mRadius = RADIUS_UNDEFINED; mRadius = RADIUS_UNDEFINED;
} }
@@ -248,10 +259,10 @@ public final class Outline {
if (!convexPath.isConvex()) { if (!convexPath.isConvex()) {
throw new IllegalArgumentException("path must be convex"); throw new IllegalArgumentException("path must be convex");
} }
if (mPath == null) mPath = new Path();
mMode = MODE_CONVEX_PATH;
mPath.set(convexPath); mPath.set(convexPath);
mRect = null; mRect.setEmpty();
mRadius = RADIUS_UNDEFINED; mRadius = RADIUS_UNDEFINED;
} }
@@ -259,9 +270,9 @@ public final class Outline {
* Offsets the Outline by (dx,dy) * Offsets the Outline by (dx,dy)
*/ */
public void offset(int dx, int dy) { public void offset(int dx, int dy) {
if (mRect != null) { if (mMode == MODE_RECT) {
mRect.offset(dx, dy); mRect.offset(dx, dy);
} else if (mPath != null) { } else if (mMode == MODE_CONVEX_PATH) {
mPath.offset(dx, dy); mPath.offset(dx, dy);
} }
} }