Bug 1177166 - Remove allocation in TopSitesThumbnailView.onLayout. r=mhaigh
authorMichael Comella <michael.l.comella@gmail.com>
Wed, 24 Jun 2015 14:03:06 -0700
changeset 268460 1334841730922daf695b651349b55872d77e06af
parent 268459 a5bcca1a23962666193a399bd26e72494894acc3
child 268461 e796ac2a6f7a7b95245bce5cd40d66ce309d6ede
push id4932
push userjlund@mozilla.com
push dateMon, 10 Aug 2015 18:23:06 +0000
treeherdermozilla-esr52@6dd5a4f5f745 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmhaigh
bugs1177166
milestone41.0a1
Bug 1177166 - Remove allocation in TopSitesThumbnailView.onLayout. r=mhaigh
mobile/android/base/home/TopSitesThumbnailView.java
--- a/mobile/android/base/home/TopSitesThumbnailView.java
+++ b/mobile/android/base/home/TopSitesThumbnailView.java
@@ -25,16 +25,23 @@ import android.widget.ImageView;
  * A height constrained ImageView to show thumbnails of top and pinned sites.
  */
 public class TopSitesThumbnailView extends ImageView {
     private static final String LOGTAG = "GeckoTopSitesThumbnailView";
 
     // 27.34% opacity filter for the dominant color.
     private static final int COLOR_FILTER = 0x46FFFFFF;
 
+    // Cache variables used in onMeasure.
+    //
+    // Note: we have two matrices because we can't change it in place - see ImageView.getImageMatrix docs.
+    private final RectF mLayoutRect = new RectF();
+    private Matrix mLayoutCurrentMatrix = new Matrix();
+    private Matrix mLayoutNextMatrix = new Matrix();
+
     // Default filter color for "Add a bookmark" views.
     private final int mDefaultColor = getResources().getColor(R.color.top_site_default);
 
     // Stroke width for the border.
     private final float mStrokeWidth = getResources().getDisplayMetrics().density * 2;
 
     // Paint for drawing the border.
     private final Paint mBorderPaint;
@@ -63,58 +70,76 @@ public class TopSitesThumbnailView exten
         mBorderPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
         mBorderPaint.setColor(res.getColor(R.color.top_site_border));
         mBorderPaint.setStyle(Paint.Style.STROKE);
     }
 
     public void setImageBitmap(Bitmap bm, boolean resize) {
         super.setImageBitmap(bm);
         mResize = resize;
+        clearLayoutVars();
+
+        updateImageMatrix();
+    }
+
+    private void clearLayoutVars() {
+        mLayoutRect.setEmpty();
+    }
+
+    private void updateImageMatrix() {
+        if (!HardwareUtils.isTablet() || !mResize) {
+            return;
+        }
+
+        // No work to be done here - assumes the rect gets reset when a new bitmap is set.
+        if (mLayoutRect.right == mWidth && mLayoutRect.bottom == mHeight) {
+            return;
+        }
+
+        setScaleType(ScaleType.MATRIX);
+
+        mLayoutRect.set(0, 0, mWidth, mHeight);
+        mLayoutNextMatrix.setRectToRect(mLayoutRect, mLayoutRect, Matrix.ScaleToFit.CENTER);
+        setImageMatrix(mLayoutNextMatrix);
+
+        final Matrix swapReferenceMatrix = mLayoutCurrentMatrix;
+        mLayoutCurrentMatrix = mLayoutNextMatrix;
+        mLayoutNextMatrix = swapReferenceMatrix;
     }
 
     @Override
     public void setImageResource(int resId) {
         super.setImageResource(resId);
         mResize = false;
     }
 
     @Override
     public void setImageDrawable(Drawable drawable) {
         super.setImageDrawable(drawable);
         mResize = false;
     }
 
-    @Override
-    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
-        super.onLayout(changed, left, top, right, bottom);
-        if (HardwareUtils.isTablet() && mResize) {
-            setScaleType(ScaleType.MATRIX);
-            RectF rect = new RectF(0, 0, mWidth, mHeight);
-            Matrix matrix = new Matrix();
-            matrix.setRectToRect(rect, rect, Matrix.ScaleToFit.CENTER);
-            setImageMatrix(matrix);
-        }
-    }
-
     /**
      * Measure the view to determine the measured width and height.
      * The height is constrained by the measured width.
      *
      * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
      * @param heightMeasureSpec vertical space requirements as imposed by the parent, but ignored.
      */
     @Override
     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
         // Default measuring.
         super.onMeasure(widthMeasureSpec, heightMeasureSpec);
 
         // Force the height based on the aspect ratio.
         mWidth = getMeasuredWidth();
         mHeight = (int) (mWidth * ThumbnailHelper.THUMBNAIL_ASPECT_RATIO);
         setMeasuredDimension(mWidth, mHeight);
+
+        updateImageMatrix();
     }
 
     /**
      * {@inheritDoc}
      */
     @Override
     public void onDraw(Canvas canvas) {
         super.onDraw(canvas);