Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 482eb53c authored by ztenghui's avatar ztenghui
Browse files

Enable the bitmap cache for the VectorDrawable

b/16299765

Change-Id: Ia2c0fd366abc097d1ce485936de74e4e898cc07a
parent 1e6eb17a
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -209,6 +209,7 @@ public class AnimatedVectorDrawable extends Drawable implements Animatable {
                    if (drawableRes != 0) {
                        mAnimatedVectorState.mVectorDrawable = (VectorDrawable) res.getDrawable(
                                drawableRes, theme).mutate();
                        mAnimatedVectorState.mVectorDrawable.setAllowCaching(false);
                    }
                    a.recycle();
                } else if (TARGET.equals(tagName)) {
@@ -258,6 +259,7 @@ public class AnimatedVectorDrawable extends Drawable implements Animatable {
                mChangingConfigurations = copy.mChangingConfigurations;
                // TODO: Make sure the constant state are handled correctly.
                mVectorDrawable = new VectorDrawable();
                mVectorDrawable.setAllowCaching(false);
                mAnimators = new ArrayList<Animator>();
            }
        }
+53 −1
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.content.res.Resources.Theme;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
@@ -142,6 +143,10 @@ public class VectorDrawable extends Drawable {

    private boolean mMutated;

    // AnimatedVectorDrawable needs to turn off the cache all the time, otherwise,
    // caching the bitmap by default is allowed.
    private boolean mAllowCaching = true;

    public VectorDrawable() {
        mVectorState = new VectorDrawableState();
    }
@@ -183,7 +188,23 @@ public class VectorDrawable extends Drawable {
        final int saveCount = canvas.save();
        final Rect bounds = getBounds();
        canvas.translate(bounds.left, bounds.top);

        if (!mAllowCaching) {
            mVectorState.mVPathRenderer.draw(canvas, bounds.width(), bounds.height());
        } else {
            Bitmap bitmap = mVectorState.mCachedBitmap;
            if (bitmap == null || !mVectorState.canReuseCache(bounds.width(),
                    bounds.height())) {
                bitmap = Bitmap.createBitmap(bounds.width(), bounds.height(),
                        Bitmap.Config.ARGB_8888);
                Canvas tmpCanvas = new Canvas(bitmap);
                mVectorState.mVPathRenderer.draw(tmpCanvas, bounds.width(), bounds.height());
                mVectorState.mCachedBitmap = bitmap;

                mVectorState.updateCacheStates();
            }
            canvas.drawBitmap(bitmap, null, bounds, null);
        }
        canvas.restoreToCount(saveCount);
    }

@@ -444,6 +465,10 @@ public class VectorDrawable extends Drawable {
        return super.getChangingConfigurations() | mVectorState.mChangingConfigurations;
    }

    void setAllowCaching(boolean allowCaching) {
        mAllowCaching = allowCaching;
    }

    private static class VectorDrawableState extends ConstantState {
        int[] mThemeAttrs;
        int mChangingConfigurations;
@@ -451,6 +476,12 @@ public class VectorDrawable extends Drawable {
        ColorStateList mTint;
        Mode mTintMode;

        Bitmap mCachedBitmap;
        int[] mCachedThemeAttrs;
        ColorStateList mCachedTint;
        Mode mCachedTintMode;
        int mCachedRootAlpha;

        // Deep copy for mutate() or implicitly mutate.
        public VectorDrawableState(VectorDrawableState copy) {
            if (copy != null) {
@@ -462,6 +493,27 @@ public class VectorDrawable extends Drawable {
            }
        }

        public boolean canReuseCache(int width, int height) {
            if (mCachedThemeAttrs == mThemeAttrs
                    && mCachedTint == mTint
                    && mCachedTintMode == mTintMode
                    && width == mCachedBitmap.getWidth()
                    && height == mCachedBitmap.getHeight()
                    && mCachedRootAlpha == mVPathRenderer.getRootAlpha())  {
                return true;
            }
            return false;
        }

        public void updateCacheStates() {
            // Use shallow copy here and shallow comparison in canReuseCache(),
            // likely hit cache miss more, but practically not much difference.
            mCachedThemeAttrs = mThemeAttrs;
            mCachedTint = mTint;
            mCachedTintMode = mTintMode;
            mCachedRootAlpha = mVPathRenderer.getRootAlpha();
        }

        public VectorDrawableState() {
            mVPathRenderer = new VPathRenderer();
        }