Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/photo_picker/PickerCategoryView.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/photo_picker/PickerCategoryView.java b/chrome/android/java/src/org/chromium/chrome/browser/photo_picker/PickerCategoryView.java |
| index 6c73972eadb3c87da1d3b42c7a18259e2ac6809e..cbe9d7b7c031065d787725ee3ddae002aeabe7f7 100644 |
| --- a/chrome/android/java/src/org/chromium/chrome/browser/photo_picker/PickerCategoryView.java |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/photo_picker/PickerCategoryView.java |
| @@ -6,10 +6,12 @@ package org.chromium.chrome.browser.photo_picker; |
| import android.app.Activity; |
| import android.content.Context; |
| +import android.graphics.Bitmap; |
| import android.graphics.Rect; |
| import android.support.v7.widget.GridLayoutManager; |
| import android.support.v7.widget.RecyclerView; |
| import android.support.v7.widget.Toolbar.OnMenuItemClickListener; |
| +import android.util.LruCache; |
| import android.view.LayoutInflater; |
| import android.view.MenuItem; |
| import android.view.View; |
| @@ -28,6 +30,8 @@ import java.util.List; |
| */ |
| public class PickerCategoryView extends RelativeLayout |
| implements FileEnumWorkerTask.FilesEnumeratedCallback, OnMenuItemClickListener { |
| + private static final int KILOBYTE = 1024; |
| + |
| // The dialog that owns us. |
| private PhotoPickerDialog mDialog; |
| @@ -55,6 +59,14 @@ public class PickerCategoryView extends RelativeLayout |
| // The {@link SelectionDelegate} keeping track of which images are selected. |
| private SelectionDelegate<PickerBitmap> mSelectionDelegate; |
| + // A low-resolution cache for images. Helpful for cache misses from the high-resolution cache |
| + // to avoid showing gray squares (we show pixelated versions instead until image can be loaded |
| + // off disk, which is much less jarring). |
| + private LruCache<String, Bitmap> mLowResBitmaps; |
| + |
| + // A high-resolution cache for images. |
| + private LruCache<String, Bitmap> mHighResBitmaps; |
|
Michael van Ouwerkerk
2017/04/27 17:07:35
What guarantee do we have that these caches do not
Finnur
2017/05/04 16:05:15
This object is destroyed when the dialog goes away
|
| + |
| /** |
| * The number of columns to show. Note: mColumns and mPadding (see below) should both be even |
| * numbers or both odd, not a mix (the column padding will not be of uniform thickness if they |
| @@ -106,7 +118,22 @@ public class PickerCategoryView extends RelativeLayout |
| mRecyclerView.setLayoutManager(mLayoutManager); |
| mRecyclerView.addItemDecoration(new GridSpacingItemDecoration(mColumns, mPadding)); |
| - // TODO(finnur): Implement caching. |
| + final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / KILOBYTE); |
| + final int cacheSizeLarge = maxMemory / 2; // 1/2 of the available memory. |
| + final int cacheSizeSmall = maxMemory / 8; // 1/8th of the available memory. |
| + mLowResBitmaps = new LruCache<String, Bitmap>(cacheSizeSmall) { |
| + @Override |
| + protected int sizeOf(String key, Bitmap bitmap) { |
| + return bitmap.getByteCount() / KILOBYTE; |
| + } |
| + }; |
| + mHighResBitmaps = new LruCache<String, Bitmap>(cacheSizeLarge) { |
|
Michael van Ouwerkerk
2017/04/27 17:07:35
Please ping memory-dev@chromium.org https://groups
Finnur
2017/05/04 16:05:15
Will do, thanks.
|
| + @Override |
| + protected int sizeOf(String key, Bitmap bitmap) { |
| + return bitmap.getByteCount() / KILOBYTE; |
| + } |
| + }; |
| + |
| // TODO(finnur): Remove this once the decoder service is in place. |
| prepareBitmaps(); |
| } |
| @@ -176,6 +203,14 @@ public class PickerCategoryView extends RelativeLayout |
| return mPickerBitmaps; |
| } |
| + public LruCache<String, Bitmap> getLowResBitmaps() { |
| + return mLowResBitmaps; |
| + } |
| + |
| + public LruCache<String, Bitmap> getHighResBitmaps() { |
| + return mHighResBitmaps; |
| + } |
| + |
| public boolean isMultiSelectAllowed() { |
| return mMultiSelectionAllowed; |
| } |