Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(7025)

Unified Diff: chrome/android/java/src/org/chromium/chrome/browser/photo_picker/PickerCategoryView.java

Issue 2927083002: Photo Picker dialog: Drop image caches under memory pressure. (Closed)
Patch Set: Fix merge conflict Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 171718d604843e4c43c4372b1a1cce5e4be54c35..d186e6bc0ce49ed0124e3c3cceb784fd76615f5d 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
@@ -4,7 +4,6 @@
package org.chromium.chrome.browser.photo_picker;
-import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Configuration;
@@ -18,9 +17,11 @@ import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
+import org.chromium.base.DiscardableReferencePool.DiscardableReference;
import org.chromium.base.VisibleForTesting;
import org.chromium.base.metrics.RecordHistogram;
import org.chromium.chrome.R;
+import org.chromium.chrome.browser.ChromeActivity;
import org.chromium.chrome.browser.util.ConversionUtils;
import org.chromium.chrome.browser.widget.selection.SelectableListLayout;
import org.chromium.chrome.browser.widget.selection.SelectionDelegate;
@@ -51,8 +52,8 @@ public class PickerCategoryView extends RelativeLayout
// The view containing the RecyclerView and the toolbar, etc.
private SelectableListLayout<PickerBitmap> mSelectableListLayout;
- // Our context.
- private Context mContext;
+ // Our activity.
+ private ChromeActivity mActivity;
// The list of images on disk, sorted by last-modified first.
private List<PickerBitmap> mPickerBitmaps;
@@ -81,13 +82,19 @@ 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 low-resolution cache for images, lazily created. 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 DiscardableReference<LruCache<String, Bitmap>> mLowResBitmaps;
- // A high-resolution cache for images.
- private LruCache<String, Bitmap> mHighResBitmaps;
+ // A high-resolution cache for images, lazily created.
+ private DiscardableReference<LruCache<String, Bitmap>> mHighResBitmaps;
+
+ // The size of the low-res cache.
+ private int mCacheSizeLarge;
+
+ // The size of the high-res cache.
+ private int mCacheSizeSmall;
/**
* The number of columns to show. Note: mColumns and mPadding (see below) should both be even
@@ -111,21 +118,13 @@ public class PickerCategoryView extends RelativeLayout
// A list of files to use for testing (instead of reading files on disk).
private static List<PickerBitmap> sTestFiles;
+ @SuppressWarnings("unchecked") // mSelectableListLayout
public PickerCategoryView(Context context) {
super(context);
- postConstruction(context);
- }
-
- /**
- * A helper function for initializing the PickerCategoryView.
- * @param context The context to use.
- */
- @SuppressWarnings("unchecked") // mSelectableListLayout
- private void postConstruction(Context context) {
- mContext = context;
+ mActivity = (ChromeActivity) context;
mDecoderServiceHost = new DecoderServiceHost(this);
- mDecoderServiceHost.bind(mContext);
+ mDecoderServiceHost.bind(mActivity);
enumerateBitmaps();
@@ -147,17 +146,15 @@ public class PickerCategoryView extends RelativeLayout
calculateGridMetrics();
- mLayoutManager = new GridLayoutManager(mContext, mColumns);
+ mLayoutManager = new GridLayoutManager(mActivity, mColumns);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(mLayoutManager);
mSpacingDecoration = new GridSpacingItemDecoration(mColumns, mPadding);
mRecyclerView.addItemDecoration(mSpacingDecoration);
final long maxMemory = ConversionUtils.bytesToKilobytes(Runtime.getRuntime().maxMemory());
- final long cacheSizeLarge = maxMemory / 2; // 1/2 of the available memory.
- final long cacheSizeSmall = maxMemory / 8; // 1/8th of the available memory.
- mLowResBitmaps = new LruCache<String, Bitmap>((int) (cacheSizeSmall));
- mHighResBitmaps = new LruCache<String, Bitmap>((int) (cacheSizeLarge));
+ mCacheSizeLarge = (int) (maxMemory / 2); // 1/2 of the available memory.
+ mCacheSizeSmall = (int) (maxMemory / 8); // 1/8th of the available memory.
}
@Override
@@ -181,7 +178,7 @@ public class PickerCategoryView extends RelativeLayout
}
if (mDecoderServiceHost != null) {
- mDecoderServiceHost.unbind(mContext);
+ mDecoderServiceHost.unbind(mActivity);
mDecoderServiceHost = null;
}
}
@@ -280,11 +277,19 @@ public class PickerCategoryView extends RelativeLayout
}
public LruCache<String, Bitmap> getLowResBitmaps() {
- return mLowResBitmaps;
+ if (mLowResBitmaps == null || mLowResBitmaps.get() == null) {
+ mLowResBitmaps =
+ mActivity.getReferencePool().put(new LruCache<String, Bitmap>(mCacheSizeSmall));
+ }
+ return mLowResBitmaps.get();
}
public LruCache<String, Bitmap> getHighResBitmaps() {
- return mHighResBitmaps;
+ if (mHighResBitmaps == null || mHighResBitmaps.get() == null) {
+ mHighResBitmaps =
+ mActivity.getReferencePool().put(new LruCache<String, Bitmap>(mCacheSizeLarge));
+ }
+ return mHighResBitmaps.get();
}
public boolean isMultiSelectAllowed() {
@@ -312,12 +317,12 @@ public class PickerCategoryView extends RelativeLayout
*/
private void calculateGridMetrics() {
Rect appRect = new Rect();
- ((Activity) mContext).getWindow().getDecorView().getWindowVisibleDisplayFrame(appRect);
+ mActivity.getWindow().getDecorView().getWindowVisibleDisplayFrame(appRect);
int width = appRect.width();
int minSize =
- mContext.getResources().getDimensionPixelSize(R.dimen.photo_picker_tile_min_size);
- mPadding = mContext.getResources().getDimensionPixelSize(R.dimen.photo_picker_tile_gap);
+ mActivity.getResources().getDimensionPixelSize(R.dimen.photo_picker_tile_min_size);
+ mPadding = mActivity.getResources().getDimensionPixelSize(R.dimen.photo_picker_tile_gap);
mColumns = Math.max(1, (width - mPadding) / (minSize + mPadding));
mImageSize = (width - mPadding * (mColumns + 1)) / (mColumns);
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698