Some optimizations in blur outline generator

1) Using ALPHA_8 as the start and end bitmap. This removes one extra
   bitmap generation step
2) Using ByteBuffer on ALPHA_8 bitmap for clipAlpha. This allows us
   to use byteArray instead of intArray for representing pixels

Change-Id: I1b654c439fd491b6b91180ddc562bb97fad857aa
This commit is contained in:
Sunny Goyal
2016-08-31 16:02:40 -07:00
parent 35908f9e67
commit f28e6afafd
5 changed files with 37 additions and 41 deletions

View File

@@ -236,6 +236,7 @@ public class CellLayout extends ViewGroup implements BubbleTextShadowHandler {
for (int i = 0; i < mDragOutlines.length; i++) {
mDragOutlines[i] = new Rect(-1, -1, -1, -1);
}
mDragOutlinePaint.setColor(getResources().getColor(R.color.outline_color));
// When dragging things around the home screens, we show a green outline of
// where the item will land. The outlines gradually fade out, leaving a trail

View File

@@ -29,6 +29,10 @@ import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.SparseArray;
import com.android.launcher3.config.ProviderConfig;
import java.nio.ByteBuffer;
/**
* Utility class to generate shadow and outline effect, which are used for click feedback
* and drag-n-drop respectively.
@@ -79,50 +83,53 @@ public class HolographicOutlineHelper {
* Applies a more expensive and accurate outline to whatever is currently drawn in a specified
* bitmap.
*/
public void applyExpensiveOutlineWithBlur(Bitmap srcDst, Canvas srcDstCanvas, int color,
int outlineColor) {
applyExpensiveOutlineWithBlur(srcDst, srcDstCanvas, color, outlineColor, true);
public void applyExpensiveOutlineWithBlur(Bitmap srcDst, Canvas srcDstCanvas) {
applyExpensiveOutlineWithBlur(srcDst, srcDstCanvas, true);
}
public void applyExpensiveOutlineWithBlur(Bitmap srcDst, Canvas srcDstCanvas, int color,
int outlineColor, boolean clipAlpha) {
public void applyExpensiveOutlineWithBlur(Bitmap srcDst, Canvas srcDstCanvas,
boolean clipAlpha) {
if (ProviderConfig.IS_DOGFOOD_BUILD && srcDst.getConfig() != Bitmap.Config.ALPHA_8) {
throw new RuntimeException("Outline blue is only supported on alpha bitmaps");
}
// We start by removing most of the alpha channel so as to ignore shadows, and
// other types of partial transparency when defining the shape of the object
if (clipAlpha) {
int[] srcBuffer = new int[srcDst.getWidth() * srcDst.getHeight()];
srcDst.getPixels(srcBuffer,
0, srcDst.getWidth(), 0, 0, srcDst.getWidth(), srcDst.getHeight());
for (int i = 0; i < srcBuffer.length; i++) {
final int alpha = srcBuffer[i] >>> 24;
if (alpha < 188) {
srcBuffer[i] = 0;
byte[] pixels = new byte[srcDst.getWidth() * srcDst.getHeight()];
ByteBuffer buffer = ByteBuffer.wrap(pixels);
buffer.rewind();
srcDst.copyPixelsToBuffer(buffer);
for (int i = 0; i < pixels.length; i++) {
if ((pixels[i] & 0xFF) < 188) {
pixels[i] = 0;
}
}
srcDst.setPixels(srcBuffer,
0, srcDst.getWidth(), 0, 0, srcDst.getWidth(), srcDst.getHeight());
buffer.rewind();
srcDst.copyPixelsFromBuffer(buffer);
}
Bitmap glowShape = srcDst.extractAlpha();
// calculate the outer blur first
mBlurPaint.setMaskFilter(mMediumOuterBlurMaskFilter);
int[] outerBlurOffset = new int[2];
Bitmap thickOuterBlur = glowShape.extractAlpha(mBlurPaint, outerBlurOffset);
Bitmap thickOuterBlur = srcDst.extractAlpha(mBlurPaint, outerBlurOffset);
mBlurPaint.setMaskFilter(mThinOuterBlurMaskFilter);
int[] brightOutlineOffset = new int[2];
Bitmap brightOutline = glowShape.extractAlpha(mBlurPaint, brightOutlineOffset);
Bitmap brightOutline = srcDst.extractAlpha(mBlurPaint, brightOutlineOffset);
// calculate the inner blur
srcDstCanvas.setBitmap(glowShape);
srcDstCanvas.setBitmap(srcDst);
srcDstCanvas.drawColor(0xFF000000, PorterDuff.Mode.SRC_OUT);
mBlurPaint.setMaskFilter(mMediumInnerBlurMaskFilter);
int[] thickInnerBlurOffset = new int[2];
Bitmap thickInnerBlur = glowShape.extractAlpha(mBlurPaint, thickInnerBlurOffset);
Bitmap thickInnerBlur = srcDst.extractAlpha(mBlurPaint, thickInnerBlurOffset);
// mask out the inner blur
srcDstCanvas.setBitmap(thickInnerBlur);
srcDstCanvas.drawBitmap(glowShape, -thickInnerBlurOffset[0],
srcDstCanvas.drawBitmap(srcDst, -thickInnerBlurOffset[0],
-thickInnerBlurOffset[1], mErasePaint);
srcDstCanvas.drawRect(0, 0, -thickInnerBlurOffset[0], thickInnerBlur.getHeight(),
mErasePaint);
@@ -132,14 +139,12 @@ public class HolographicOutlineHelper {
// draw the inner and outer blur
srcDstCanvas.setBitmap(srcDst);
srcDstCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
mDrawPaint.setColor(color);
srcDstCanvas.drawBitmap(thickInnerBlur, thickInnerBlurOffset[0], thickInnerBlurOffset[1],
mDrawPaint);
srcDstCanvas.drawBitmap(thickOuterBlur, outerBlurOffset[0], outerBlurOffset[1],
mDrawPaint);
// draw the bright outline
mDrawPaint.setColor(outlineColor);
srcDstCanvas.drawBitmap(brightOutline, brightOutlineOffset[0], brightOutlineOffset[1],
mDrawPaint);
@@ -148,7 +153,6 @@ public class HolographicOutlineHelper {
brightOutline.recycle();
thickOuterBlur.recycle();
thickInnerBlur.recycle();
glowShape.recycle();
}
Bitmap createMediumDropShadow(BubbleTextView view) {

View File

@@ -27,7 +27,6 @@ import android.widget.TextView;
import com.android.launcher3.HolographicOutlineHelper;
import com.android.launcher3.Launcher;
import com.android.launcher3.PreloadIconDrawable;
import com.android.launcher3.R;
import com.android.launcher3.Workspace;
import com.android.launcher3.config.ProviderConfig;
import com.android.launcher3.folder.FolderIcon;
@@ -134,14 +133,12 @@ public class DragPreviewProvider {
* Responsibility for the bitmap is transferred to the caller.
*/
public Bitmap createDragOutline(Canvas canvas) {
final int outlineColor = mView.getResources().getColor(R.color.outline_color);
final Bitmap b = Bitmap.createBitmap(mView.getWidth() + DRAG_BITMAP_PADDING,
mView.getHeight() + DRAG_BITMAP_PADDING, Bitmap.Config.ARGB_8888);
mView.getHeight() + DRAG_BITMAP_PADDING, Bitmap.Config.ALPHA_8);
canvas.setBitmap(b);
drawDragView(canvas);
HolographicOutlineHelper.obtain(mView.getContext())
.applyExpensiveOutlineWithBlur(b, canvas, outlineColor, outlineColor);
.applyExpensiveOutlineWithBlur(b, canvas);
canvas.setBitmap(null);
return b;
}

View File

@@ -22,12 +22,9 @@ import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.ImageView;
import com.android.launcher3.BubbleTextView;
import com.android.launcher3.HolographicOutlineHelper;
import com.android.launcher3.Launcher;
import com.android.launcher3.R;
import com.android.launcher3.Utilities;
import com.android.launcher3.graphics.DragPreviewProvider;
@@ -45,23 +42,22 @@ public class ShortcutDragPreviewProvider extends DragPreviewProvider {
@Override
public Bitmap createDragOutline(Canvas canvas) {
Bitmap b = drawScaledPreview(canvas);
Bitmap b = drawScaledPreview(canvas, Bitmap.Config.ALPHA_8);
final int outlineColor = mView.getResources().getColor(R.color.outline_color);
HolographicOutlineHelper.obtain(mView.getContext())
.applyExpensiveOutlineWithBlur(b, canvas, outlineColor, outlineColor);
.applyExpensiveOutlineWithBlur(b, canvas);
canvas.setBitmap(null);
return b;
}
@Override
public Bitmap createDragBitmap(Canvas canvas) {
Bitmap b = drawScaledPreview(canvas);
Bitmap b = drawScaledPreview(canvas, Bitmap.Config.ARGB_8888);
canvas.setBitmap(null);
return b;
}
private Bitmap drawScaledPreview(Canvas canvas) {
private Bitmap drawScaledPreview(Canvas canvas, Bitmap.Config config) {
Drawable d = mView.getBackground();
Rect bounds = getDrawableBounds(d);
@@ -70,7 +66,7 @@ public class ShortcutDragPreviewProvider extends DragPreviewProvider {
final Bitmap b = Bitmap.createBitmap(
size + DRAG_BITMAP_PADDING,
size + DRAG_BITMAP_PADDING,
Bitmap.Config.ARGB_8888);
config);
canvas.setBitmap(b);
canvas.save(Canvas.MATRIX_SAVE_FLAG);

View File

@@ -24,7 +24,6 @@ import android.view.View;
import com.android.launcher3.HolographicOutlineHelper;
import com.android.launcher3.Launcher;
import com.android.launcher3.PendingAddItemInfo;
import com.android.launcher3.R;
import com.android.launcher3.Workspace;
import com.android.launcher3.graphics.DragPreviewProvider;
@@ -50,7 +49,7 @@ public class PendingItemPreviewProvider extends DragPreviewProvider {
int w = size[0];
int h = size[1];
final Bitmap b = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
final Bitmap b = Bitmap.createBitmap(w, h, Bitmap.Config.ALPHA_8);
canvas.setBitmap(b);
Rect src = new Rect(0, 0, mPreviewBitmap.getWidth(), mPreviewBitmap.getHeight());
@@ -68,9 +67,8 @@ public class PendingItemPreviewProvider extends DragPreviewProvider {
// Don't clip alpha values for the drag outline if we're using the default widget preview
boolean clipAlpha = !(mAddInfo instanceof PendingAddWidgetInfo &&
(((PendingAddWidgetInfo) mAddInfo).previewImage == 0));
final int outlineColor = mView.getResources().getColor(R.color.outline_color);
HolographicOutlineHelper.obtain(mView.getContext())
.applyExpensiveOutlineWithBlur(b, canvas, outlineColor, outlineColor, clipAlpha);
.applyExpensiveOutlineWithBlur(b, canvas, clipAlpha);
canvas.setBitmap(null);
return b;