Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/compositor/layouts/content/TabContentManager.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/compositor/layouts/content/TabContentManager.java b/chrome/android/java/src/org/chromium/chrome/browser/compositor/layouts/content/TabContentManager.java |
| index 0d14ddee919f809d359ed9605e35452b0abed363..08a6b8c583911e2f17de2a5bd9e0bb55ee250cf0 100644 |
| --- a/chrome/android/java/src/org/chromium/chrome/browser/compositor/layouts/content/TabContentManager.java |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/compositor/layouts/content/TabContentManager.java |
| @@ -7,6 +7,7 @@ package org.chromium.chrome.browser.compositor.layouts.content; |
| import android.content.Context; |
| import android.graphics.Bitmap; |
| import android.graphics.Canvas; |
| +import android.os.AsyncTask; |
| import android.util.SparseArray; |
| import android.view.View; |
| @@ -24,6 +25,7 @@ import org.chromium.ui.base.DeviceFormFactor; |
| import java.io.File; |
| import java.util.ArrayList; |
| import java.util.List; |
| +import java.util.concurrent.ExecutionException; |
| /** |
| * The TabContentManager is responsible for serving tab contents to the UI components. Contents |
| @@ -33,12 +35,14 @@ import java.util.List; |
| public class TabContentManager { |
| private static final String THUMBNAIL_DIRECTORY = "textures"; |
| private final Context mContext; |
| - private final File mThumbnailsDir; |
| - private final float mThumbnailScale; |
| - private final int mFullResThumbnailsMaxSize; |
| private final ContentOffsetProvider mContentOffsetProvider; |
| + |
| + private File mThumbnailsDir; |
| + private float mThumbnailScale; |
| + private int mFullResThumbnailsMaxSize; |
| private int[] mPriorityTabIds; |
| private long mNativeTabContentManager; |
| + private long mNativeThumbnailCache; |
|
David Trainor- moved to gerrit
2015/05/08 06:12:38
You can probably remove this
Peter Wen
2015/05/11 16:48:19
Added comment to explain reason for storing this.
|
| /** |
| * A callback interface for decompressing the thumbnail for a tab into a bitmap. |
| @@ -57,6 +61,7 @@ public class TabContentManager { |
| new SparseArray<TabContentManager.DecompressThumbnailCallback>(); |
| private boolean mSnapshotsEnabled; |
| + private AsyncTask<Void, Void, Long> mNativeThumbnailCacheInitTask; |
| /** |
| * The Java interface for listening to thumbnail changes. |
| @@ -96,48 +101,81 @@ public class TabContentManager { |
| mContentOffsetProvider = contentOffsetProvider; |
| mSnapshotsEnabled = snapshotsEnabled; |
| - mThumbnailsDir = mContext.getDir(THUMBNAIL_DIRECTORY, Context.MODE_PRIVATE); |
| - String diskCachePath = mThumbnailsDir.getAbsolutePath(); |
| - |
| - // Override the cache size on the command line with --thumbnails=100 |
| - int defaultCacheSize = getIntegerResourceWithOverride(mContext, |
| - R.integer.default_thumbnail_cache_size, ChromeSwitches.THUMBNAILS); |
| - |
| - mFullResThumbnailsMaxSize = defaultCacheSize; |
| - |
| - int compressionQueueMaxSize = mContext.getResources().getInteger( |
| - R.integer.default_compression_queue_size); |
| - int writeQueueMaxSize = mContext.getResources().getInteger( |
| - R.integer.default_write_queue_size); |
| - |
| - // Override the cache size on the command line with |
| - // --approximation-thumbnails=100 |
| - int approximationCacheSize = getIntegerResourceWithOverride(mContext, |
| - R.integer.default_approximation_thumbnail_cache_size, |
| - ChromeSwitches.APPROXIMATION_THUMBNAILS); |
| - |
| - float thumbnailScale = 1.f; |
| - boolean useApproximationThumbnails; |
| - float deviceDensity = mContext.getResources().getDisplayMetrics().density; |
| - if (DeviceFormFactor.isTablet(mContext)) { |
| - // Scale all tablets to MDPI. |
| - thumbnailScale = 1.f / deviceDensity; |
| - useApproximationThumbnails = false; |
| - } else { |
| - // For phones, reduce the amount of memory usage by capturing a lower-res thumbnail for |
| - // devices with resolution higher than HDPI (crbug.com/357740). |
| - if (deviceDensity > 1.5f) { |
| - thumbnailScale = 1.5f / deviceDensity; |
| + mNativeTabContentManager = nativeInit(); |
| + startThumbnailCacheInitTask(); |
| + } |
| + |
| + /** |
| + * |
| + */ |
| + private void startThumbnailCacheInitTask() { |
| + mNativeThumbnailCacheInitTask = new AsyncTask<Void, Void, Long>() { |
| + @Override |
| + protected Long doInBackground(Void... unused) { |
| + mThumbnailsDir = mContext.getDir(THUMBNAIL_DIRECTORY, Context.MODE_PRIVATE); |
| + String diskCachePath = mThumbnailsDir.getAbsolutePath(); |
| + |
| + // Override the cache size on the command line with --thumbnails=100 |
| + int defaultCacheSize = getIntegerResourceWithOverride(mContext, |
| + R.integer.default_thumbnail_cache_size, ChromeSwitches.THUMBNAILS); |
| + |
| + mFullResThumbnailsMaxSize = defaultCacheSize; |
| + |
| + int compressionQueueMaxSize = mContext.getResources().getInteger( |
| + R.integer.default_compression_queue_size); |
| + int writeQueueMaxSize = mContext.getResources().getInteger( |
| + R.integer.default_write_queue_size); |
| + |
| + // Override the cache size on the command line with |
| + // --approximation-thumbnails=100 |
| + int approximationCacheSize = getIntegerResourceWithOverride(mContext, |
| + R.integer.default_approximation_thumbnail_cache_size, |
| + ChromeSwitches.APPROXIMATION_THUMBNAILS); |
| + |
| + float thumbnailScale = 1.f; |
| + boolean useApproximationThumbnails; |
| + float deviceDensity = mContext.getResources().getDisplayMetrics().density; |
| + if (DeviceFormFactor.isTablet(mContext)) { |
| + // Scale all tablets to MDPI. |
| + thumbnailScale = 1.f / deviceDensity; |
| + useApproximationThumbnails = false; |
| + } else { |
| + // For phones, reduce the amount of memory usage by capturing a lower-res |
| + // thumbnail for devices with resolution higher than HDPI (crbug.com/357740). |
| + if (deviceDensity > 1.5f) { |
| + thumbnailScale = 1.5f / deviceDensity; |
| + } |
| + useApproximationThumbnails = true; |
| + } |
| + mThumbnailScale = thumbnailScale; |
| + |
| + mPriorityTabIds = new int[mFullResThumbnailsMaxSize]; |
| + |
| + return nativeCreateThumbnailCache(diskCachePath, defaultCacheSize, |
| + approximationCacheSize, compressionQueueMaxSize, writeQueueMaxSize, |
| + useApproximationThumbnails); |
| } |
| - useApproximationThumbnails = true; |
| - } |
| - mThumbnailScale = thumbnailScale; |
| - mPriorityTabIds = new int[mFullResThumbnailsMaxSize]; |
| + @Override |
| + protected void onPostExecute(Long resultThumbnailCache) { |
| + updateThumbnailCache(resultThumbnailCache); |
| + } |
| + }.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR); |
| + } |
| - mNativeTabContentManager = nativeInit(diskCachePath, defaultCacheSize, |
| - approximationCacheSize, compressionQueueMaxSize, writeQueueMaxSize, |
| - useApproximationThumbnails); |
| + /** |
| + * Pass the new ThumbnailCache object through or destroy it. |
| + * @param resultThumbnailCache The newly created ThumbnailCache. |
| + */ |
| + private void updateThumbnailCache(Long resultThumbnailCache) { |
|
David Trainor- moved to gerrit
2015/05/08 06:12:38
use long instead of Long here. Or you could proba
Peter Wen
2015/05/11 16:48:19
Fixed to use long. Also used in blockOnThumbnailCa
|
| + if (mNativeThumbnailCache != 0) return; |
| + mNativeThumbnailCache = resultThumbnailCache; |
| + |
| + if (mNativeTabContentManager != 0) { |
| + nativeSetThumbnailCache(mNativeTabContentManager, mNativeThumbnailCache); |
| + } else { |
| + nativeDestroyThumbnailCache(mNativeThumbnailCache); |
| + } |
| } |
| /** |
| @@ -151,6 +189,16 @@ public class TabContentManager { |
| } |
| @CalledByNative |
| + private void blockOnThumbnailCacheCreation() { |
| + try { |
| + // The task does not run onPostExecute immediately on get, so we must manually do it. |
| + updateThumbnailCache(mNativeThumbnailCacheInitTask.get()); |
| + } catch (InterruptedException e) { |
| + } catch (ExecutionException e) { |
| + } |
| + } |
| + |
| + @CalledByNative |
| private long getNativePtr() { |
| return mNativeTabContentManager; |
| } |
| @@ -331,8 +379,10 @@ public class TabContentManager { |
| * @param modelSelector The selector that answers whether a tab is currently present. |
| */ |
| public void cleanupPersistentData(TabModelSelector modelSelector) { |
| + if (mNativeTabContentManager == 0) return; |
| + |
| File[] files = mThumbnailsDir.listFiles(); |
| - if (files == null || mNativeTabContentManager == 0) return; |
| + if (files == null) return; |
| for (File file : files) { |
| try { |
| @@ -365,9 +415,13 @@ public class TabContentManager { |
| } |
| // Class Object Methods |
| - private native long nativeInit(String diskCachePath, int defaultCacheSize, |
| - int approximationCacheSize, int compressionQueueMaxSize, int writeQueueMaxSize, |
| - boolean useApproximationThumbnail); |
| + private native long nativeInit(); |
| + private static native long nativeCreateThumbnailCache(String diskCachePath, |
| + int defaultCacheSize, int approximationCacheSize, int compressionQueueMaxSize, |
| + int writeQueueMaxSize, boolean useApproximationThumbnail); |
| + private static native void nativeDestroyThumbnailCache(long thumbnailCachePtr); |
| + private native void nativeSetThumbnailCache(long nativeTabContentManager, |
| + long thumbnailCachePtr); |
| private native boolean nativeHasFullCachedThumbnail(long nativeTabContentManager, int tabId); |
| private native void nativeCacheTab(long nativeTabContentManager, Object tab, |
| Object contentViewCore, float thumbnailScale); |