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

Commit 04f294bf authored by linus_lee's avatar linus_lee Committed by Danny Baumann
Browse files

Eleven: Add some caching logic to BitmapWithColors for perf optimization

Change-Id: I9fe5e83b9b1b5bb8ca24978436ed371eec2db399
parent 0107486c
Loading
Loading
Loading
Loading
+1 −1
Original line number Original line Diff line number Diff line
@@ -202,7 +202,7 @@ public class ImageFetcher extends ImageWorker {
            artwork = mImageCache.getArtworkFromFile(mContext, albumId);
            artwork = mImageCache.getArtworkFromFile(mContext, albumId);
        }
        }
        if (artwork != null) {
        if (artwork != null) {
            return new BitmapWithColors(artwork);
            return new BitmapWithColors(artwork, key.hashCode());
        }
        }


        return LetterTileDrawable.createDefaultBitmap(mContext, key, ImageType.ALBUM, false,
        return LetterTileDrawable.createDefaultBitmap(mContext, key, ImageType.ALBUM, false,
+53 −37
Original line number Original line Diff line number Diff line
@@ -18,25 +18,36 @@ package com.cyanogenmod.eleven.utils;
import android.graphics.Bitmap;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Color;
import android.support.v7.graphics.Palette;
import android.support.v7.graphics.Palette;
import android.util.LruCache;


public class BitmapWithColors {
public class BitmapWithColors {
    private static final class BitmapColors {
        public int mVibrantColor;
        public int mVibrantDarkColor;

        public BitmapColors(int vibrantColor, int vibrantDarkColor) {
            mVibrantColor = vibrantColor;
            mVibrantDarkColor = vibrantDarkColor;
        }
    }

    private static final int CACHE_SIZE_MAX = 20;
    private static final LruCache<Integer, BitmapColors> sCachedColors =
            new LruCache<Integer, BitmapColors>(CACHE_SIZE_MAX);

    private Bitmap mBitmap;
    private Bitmap mBitmap;
    private int mVibrantColor;
    private int mBitmapKey;
    private int mVibrantDarkColor;
    private BitmapColors mColors;
    private boolean mColorsLoaded = false;


    public BitmapWithColors(Bitmap bitmap) {
    public BitmapWithColors(Bitmap bitmap, int bitmapKey) {
        mBitmap = bitmap;
        mBitmap = bitmap;
        mVibrantColor = Color.TRANSPARENT;
        mBitmapKey = bitmapKey;
        mVibrantDarkColor = Color.TRANSPARENT;
        mColorsLoaded = false;
    }
    }


    public BitmapWithColors(Bitmap bitmap, int vibrantColor, int vibrantDarkColor) {
    public BitmapWithColors(Bitmap bitmap, int bitmapKey, int vibrantColor, int vibrantDarkColor) {
        mBitmap = bitmap;
        mBitmap = bitmap;
        mVibrantColor = vibrantColor;
        mBitmapKey = bitmapKey;
        mVibrantDarkColor = vibrantDarkColor;
        mColors = new BitmapColors(vibrantColor, vibrantDarkColor);
        mColorsLoaded = true;
    }
    }


    public Bitmap getBitmap() {
    public Bitmap getBitmap() {
@@ -45,26 +56,40 @@ public class BitmapWithColors {


    public int getVibrantColor() {
    public int getVibrantColor() {
        loadColorsIfNeeded();
        loadColorsIfNeeded();
        return mVibrantColor;
        if (mColors.mVibrantColor == Color.TRANSPARENT) {
            return mColors.mVibrantDarkColor;
        }
        return mColors.mVibrantColor;
    }
    }


    public int getVibrantDarkColor() {
    public int getVibrantDarkColor() {
        loadColorsIfNeeded();
        loadColorsIfNeeded();
        return mVibrantDarkColor;
        if (mColors.mVibrantDarkColor == Color.TRANSPARENT) {
            return mColors.mVibrantColor;
        }
        return mColors.mVibrantDarkColor;
    }
    }


    private void loadColorsIfNeeded() {
    private synchronized void loadColorsIfNeeded() {
        synchronized (this) {
        if (mColors != null) {
            if (mColorsLoaded) {
            return;
            return;
        }
        }

        synchronized (sCachedColors) {
            mColors = sCachedColors.get(mBitmapKey);
        }
        if (mColors != null) {
            return;
        }
        }


        final Palette p = Palette.generate(mBitmap);
        final Palette p = Palette.generate(mBitmap);
        if (p == null) {
            return;
        }

        int vibrantColor = Color.TRANSPARENT;
        int vibrantColor = Color.TRANSPARENT;
        int vibrantDarkColor = Color.TRANSPARENT;
        int vibrantDarkColor = Color.TRANSPARENT;


        if (p != null) {
        Palette.Swatch swatch = p.getDarkVibrantSwatch();
        Palette.Swatch swatch = p.getDarkVibrantSwatch();
        if (swatch != null) {
        if (swatch != null) {
            vibrantDarkColor = swatch.getRgb();
            vibrantDarkColor = swatch.getRgb();
@@ -73,19 +98,10 @@ public class BitmapWithColors {
        if (swatch != null) {
        if (swatch != null) {
            vibrantColor = swatch.getRgb();
            vibrantColor = swatch.getRgb();
        }
        }
        }


        if (vibrantColor == Color.TRANSPARENT && vibrantDarkColor != Color.TRANSPARENT) {
        mColors = new BitmapColors(vibrantColor, vibrantDarkColor);
            vibrantColor = vibrantDarkColor;
        synchronized (sCachedColors) {
        }
            sCachedColors.put(mBitmapKey, mColors);
        if (vibrantColor != Color.TRANSPARENT && vibrantDarkColor == Color.TRANSPARENT) {
            vibrantDarkColor = vibrantColor;
        }

        synchronized (this) {
            mColorsLoaded = true;
            mVibrantColor = vibrantColor;
            mVibrantDarkColor = vibrantDarkColor;
        }
        }
    }
    }
}
}
+1 −1
Original line number Original line Diff line number Diff line
@@ -366,6 +366,6 @@ public class LetterTileDrawable extends Drawable {
        drawBitmap(defaultBitmap, defaultBitmap.getWidth(), defaultBitmap.getHeight(), canvas,
        drawBitmap(defaultBitmap, defaultBitmap.getWidth(), defaultBitmap.getHeight(), canvas,
                bounds, 1, 0, paint);
                bounds, 1, 0, paint);


        return new BitmapWithColors(createdBitmap, color, vibrantDarkColor);
        return new BitmapWithColors(createdBitmap, identifier.hashCode(), color, vibrantDarkColor);
    }
    }
}
}