diff options
author | Chih-Chung Chang <chihchung@google.com> | 2012-04-17 20:35:14 +0800 |
---|---|---|
committer | Chih-Chung Chang <chihchung@google.com> | 2012-04-18 20:31:10 +0800 |
commit | 2eaa2112c460e931ad0261b408213d3d1217d96f (patch) | |
tree | e0b9a6e51284ec5e369a3d9926704b1c69190139 /src/com/android/gallery3d/ui | |
parent | 146df37d6a6c7ca8686f5934b3d90b16c24a2583 (diff) | |
download | android_packages_apps_Snap-2eaa2112c460e931ad0261b408213d3d1217d96f.tar.gz android_packages_apps_Snap-2eaa2112c460e931ad0261b408213d3d1217d96f.tar.bz2 android_packages_apps_Snap-2eaa2112c460e931ad0261b408213d3d1217d96f.zip |
Add thumb pool the cache Bitmap used for thumbnails.
Change-Id: Ib710f2a6fc1aa86fe4abdd18d1fa7ac71396a3b4
Diffstat (limited to 'src/com/android/gallery3d/ui')
10 files changed, 67 insertions, 85 deletions
diff --git a/src/com/android/gallery3d/ui/AlbumLabelMaker.java b/src/com/android/gallery3d/ui/AlbumLabelMaker.java index 43a9055a4..f837092b8 100644 --- a/src/com/android/gallery3d/ui/AlbumLabelMaker.java +++ b/src/com/android/gallery3d/ui/AlbumLabelMaker.java @@ -1,4 +1,18 @@ -// Copyright 2012 Google Inc. All Rights Reserved. +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.android.gallery3d.ui; @@ -106,7 +120,7 @@ public class AlbumLabelMaker { mLabelWidth = width; int borders = 2 * BORDER_SIZE; mBitmapPool = new BitmapPool( - width + borders, mSpec.labelBackgroundHeight + borders); + width + borders, mSpec.labelBackgroundHeight + borders, 16); } public ThreadPool.Job<Bitmap> requestLabel( @@ -192,7 +206,7 @@ public class AlbumLabelMaker { } } - public void reycleLabel(Bitmap label) { + public void recycleLabel(Bitmap label) { mBitmapPool.recycle(label); } diff --git a/src/com/android/gallery3d/ui/AlbumSetSlidingWindow.java b/src/com/android/gallery3d/ui/AlbumSetSlidingWindow.java index 10943eaa6..7fa7df48a 100644 --- a/src/com/android/gallery3d/ui/AlbumSetSlidingWindow.java +++ b/src/com/android/gallery3d/ui/AlbumSetSlidingWindow.java @@ -492,7 +492,7 @@ public class AlbumSetSlidingWindow implements AlbumSetDataLoader.DataListener { @Override protected void recycleBitmap(Bitmap bitmap) { - mLabelMaker.reycleLabel(bitmap); + mLabelMaker.recycleLabel(bitmap); } @Override diff --git a/src/com/android/gallery3d/ui/BitmapScreenNail.java b/src/com/android/gallery3d/ui/BitmapScreenNail.java index 3481aa18e..064e1af0e 100644 --- a/src/com/android/gallery3d/ui/BitmapScreenNail.java +++ b/src/com/android/gallery3d/ui/BitmapScreenNail.java @@ -20,6 +20,8 @@ import android.graphics.Bitmap; import android.graphics.RectF; import android.util.Log; +import com.android.gallery3d.data.MediaItem; + // This is a ScreenNail wraps a Bitmap. It also includes the rotation // information. The getWidth() and getHeight() methods return the width/height // before rotation. @@ -28,7 +30,7 @@ public class BitmapScreenNail implements ScreenNail { private final int mWidth; private final int mHeight; private final int mRotation; - private final Bitmap mBitmap; + private Bitmap mBitmap; private BitmapTexture mTexture; public BitmapScreenNail(Bitmap bitmap, int rotation) { @@ -60,11 +62,15 @@ public class BitmapScreenNail implements ScreenNail { } @Override - public void pauseDraw() { + public void recycle() { if (mTexture != null) { mTexture.recycle(); mTexture = null; } + if (mBitmap != null) { + MediaItem.getThumbPool().recycle(mBitmap); + mBitmap = null; + } } @Override diff --git a/src/com/android/gallery3d/ui/BitmapTileProvider.java b/src/com/android/gallery3d/ui/BitmapTileProvider.java index 1e78cfd33..99b64d42e 100644 --- a/src/com/android/gallery3d/ui/BitmapTileProvider.java +++ b/src/com/android/gallery3d/ui/BitmapTileProvider.java @@ -96,7 +96,7 @@ public class BitmapTileProvider implements TileImageView.Model { BitmapUtils.recycleSilently(bitmap); } if (mScreenNail != null) { - mScreenNail.pauseDraw(); + mScreenNail.recycle(); } } diff --git a/src/com/android/gallery3d/ui/PhotoView.java b/src/com/android/gallery3d/ui/PhotoView.java index b2a4be49d..f0b4a12e7 100644 --- a/src/com/android/gallery3d/ui/PhotoView.java +++ b/src/com/android/gallery3d/ui/PhotoView.java @@ -52,6 +52,7 @@ public class PhotoView extends GLView { // ScreenNail at previous (negative offset) or next (positive offset) // positions. Returns null if the specified ScreenNail is unavailable. public ScreenNail getScreenNail(int offset); + public void setNeedFullImage(boolean enabled); } public interface PhotoTapListener { @@ -90,10 +91,6 @@ public class PhotoView extends GLView { // SCREEN_NAIL_MAX. private final RangeArray<Picture> mPictures = new RangeArray<Picture>(-SCREEN_NAIL_MAX, SCREEN_NAIL_MAX); - private final RangeBoolArray mReused = - new RangeBoolArray(-SCREEN_NAIL_MAX, SCREEN_NAIL_MAX); - private final RangeArray<ScreenNail> mTempScreenNail = - new RangeArray<ScreenNail>(-SCREEN_NAIL_MAX, SCREEN_NAIL_MAX); private final long mDataVersion[] = new long[2 * SCREEN_NAIL_MAX + 1]; private final int mFromIndex[] = new int[2 * SCREEN_NAIL_MAX + 1]; @@ -247,11 +244,6 @@ public class PhotoView extends GLView { } if (!changed) return; - // Remembers those ScreenNail which are reused. - for (int i = -SCREEN_NAIL_MAX; i <= SCREEN_NAIL_MAX; i++) { - mReused.put(i, false); - } - // Create the mFromIndex array, which records the index where the picture // come from. The value Integer.MAX_VALUE means it's a new picture. for (int i = 0; i < N; i++) { @@ -265,7 +257,6 @@ public class PhotoView extends GLView { int j; for (j = 0; j < N; j++) { if (mDataVersion[j] == v) { - mReused.put(j - SCREEN_NAIL_MAX, true); break; } } @@ -280,25 +271,8 @@ public class PhotoView extends GLView { // Move the boxes mPositionController.moveBox(mFromIndex); - // Free those ScreenNails that are not reused. - for (int i = -SCREEN_NAIL_MAX; i <= SCREEN_NAIL_MAX; i++) { - if (!mReused.get(i)) mPictures.get(i).updateScreenNail(null); - } - - // Collect the reused ScreenNails, so we don't need to re-upload the - // textures. + // Update the ScreenNails. for (int i = -SCREEN_NAIL_MAX; i <= SCREEN_NAIL_MAX; i++) { - mTempScreenNail.put(i, mPictures.get(i).releaseScreenNail()); - } - - // Put back the reused ScreenNails. - for (int i = -SCREEN_NAIL_MAX; i <= SCREEN_NAIL_MAX; i++) { - int j = mFromIndex[i + SCREEN_NAIL_MAX]; - if (j != Integer.MAX_VALUE) { - ScreenNail s = mTempScreenNail.get(j); - mTempScreenNail.put(j, null); - mPictures.get(i).updateScreenNail(s); - } mPictures.get(i).reload(); } @@ -317,11 +291,7 @@ public class PhotoView extends GLView { private interface Picture { void reload(); void draw(GLCanvas canvas, Rect r); - - void updateScreenNail(ScreenNail s); - // Release the ownership of the ScreenNail from this entry. - ScreenNail releaseScreenNail(); - + void setScreenNail(ScreenNail s); boolean isEnabled(); }; @@ -352,8 +322,7 @@ public class PhotoView extends GLView { getRotated(mRotation, w, h), getRotated(mRotation, h, w)); } - updateScreenNail(mModel == null - ? null : mModel.getScreenNail(0)); + setScreenNail(mModel == null ? null : mModel.getScreenNail(0)); updateLoadingState(); } @@ -374,14 +343,9 @@ public class PhotoView extends GLView { } @Override - public void updateScreenNail(ScreenNail s) { + public void setScreenNail(ScreenNail s) { mIsNonBitmap = (s != null && !(s instanceof BitmapScreenNail)); - mTileView.updateScreenNail(s); - } - - @Override - public ScreenNail releaseScreenNail() { - return mTileView.releaseScreenNail(); + mTileView.setScreenNail(s); } @Override @@ -489,8 +453,7 @@ public class PhotoView extends GLView { @Override public void reload() { - updateScreenNail(mModel == null ? null - : mModel.getScreenNail(mIndex)); + setScreenNail(mModel == null ? null : mModel.getScreenNail(mIndex)); } @Override @@ -533,12 +496,9 @@ public class PhotoView extends GLView { } @Override - public void updateScreenNail(ScreenNail s) { + public void setScreenNail(ScreenNail s) { mEnabled = (s != null); if (mScreenNail == s) return; - if (mScreenNail != null) { - mScreenNail.pauseDraw(); - } mScreenNail = s; if (mScreenNail != null) { mRotation = mScreenNail.getRotation(); @@ -553,13 +513,6 @@ public class PhotoView extends GLView { } @Override - public ScreenNail releaseScreenNail() { - ScreenNail s = mScreenNail; - mScreenNail = null; - return s; - } - - @Override public boolean isEnabled() { return mEnabled; } @@ -704,6 +657,7 @@ public class PhotoView extends GLView { if (mFilmMode == enabled) return; mFilmMode = enabled; mPositionController.setFilmMode(mFilmMode); + mModel.setNeedFullImage(!enabled); } //////////////////////////////////////////////////////////////////////////// @@ -724,7 +678,7 @@ public class PhotoView extends GLView { mPositionController.skipAnimation(); mTileView.freeTextures(); for (int i = -SCREEN_NAIL_MAX; i <= SCREEN_NAIL_MAX; i++) { - mPictures.get(i).updateScreenNail(null); + mPictures.get(i).setScreenNail(null); } } diff --git a/src/com/android/gallery3d/ui/PositionController.java b/src/com/android/gallery3d/ui/PositionController.java index 6650d246c..a5eb22425 100644 --- a/src/com/android/gallery3d/ui/PositionController.java +++ b/src/com/android/gallery3d/ui/PositionController.java @@ -578,7 +578,7 @@ class PositionController { if (changed) redraw(); } - public boolean advanceAnimation() { + public void advanceAnimation() { boolean changed = false; changed |= mPlatform.advanceAnimation(); for (int i = -BOX_MAX; i <= BOX_MAX; i++) { @@ -588,7 +588,6 @@ class PositionController { changed |= mGaps.get(i).advanceAnimation(); } if (changed) redraw(); - return changed; } //////////////////////////////////////////////////////////////////////////// diff --git a/src/com/android/gallery3d/ui/ScreenNail.java b/src/com/android/gallery3d/ui/ScreenNail.java index 58ae8c942..25adc77da 100644 --- a/src/com/android/gallery3d/ui/ScreenNail.java +++ b/src/com/android/gallery3d/ui/ScreenNail.java @@ -22,8 +22,12 @@ public interface ScreenNail { public int getHeight(); public int getRotation(); public void draw(GLCanvas canvas, int x, int y, int width, int height); - public void noDraw(); // we do not need to draw this ScreenNail in this frame. - public void pauseDraw(); // we do not expect to draw this ScreenNail for some time. + + // We do not need to draw this ScreenNail in this frame. + public void noDraw(); + + // This ScreenNail will not be used anymore. Release related resources. + public void recycle(); // This is only used by TileImageView to back up the tiles not yet loaded. public void draw(GLCanvas canvas, RectF source, RectF dest); diff --git a/src/com/android/gallery3d/ui/SurfaceTextureScreenNail.java b/src/com/android/gallery3d/ui/SurfaceTextureScreenNail.java index 3a8f2b0a4..f8442d059 100644 --- a/src/com/android/gallery3d/ui/SurfaceTextureScreenNail.java +++ b/src/com/android/gallery3d/ui/SurfaceTextureScreenNail.java @@ -109,7 +109,7 @@ public abstract class SurfaceTextureScreenNail implements ScreenNail, abstract public void noDraw(); @Override - abstract public void pauseDraw(); + abstract public void recycle(); @Override abstract public void onFrameAvailable(SurfaceTexture surfaceTexture); diff --git a/src/com/android/gallery3d/ui/TileImageView.java b/src/com/android/gallery3d/ui/TileImageView.java index 610a34688..b37cf9c4a 100644 --- a/src/com/android/gallery3d/ui/TileImageView.java +++ b/src/com/android/gallery3d/ui/TileImageView.java @@ -153,18 +153,10 @@ public class TileImageView extends GLView { if (model != null) notifyModelInvalidated(); } - public void updateScreenNail(ScreenNail s) { - if (mScreenNail == s) return; - if (mScreenNail != null) mScreenNail.pauseDraw(); + public void setScreenNail(ScreenNail s) { mScreenNail = s; } - public ScreenNail releaseScreenNail() { - ScreenNail s = mScreenNail; - mScreenNail = null; - return s; - } - public void notifyModelInvalidated() { invalidateTiles(); if (mModel == null) { @@ -173,7 +165,7 @@ public class TileImageView extends GLView { mImageHeight = 0; mLevelCount = 0; } else { - updateScreenNail(mModel.getScreenNail()); + setScreenNail(mModel.getScreenNail()); mImageWidth = mModel.getImageWidth(); mImageHeight = mModel.getImageHeight(); mLevelCount = mModel.getLevelCount(); @@ -389,7 +381,7 @@ public class TileImageView extends GLView { tile = mRecycledQueue.pop(); } } - updateScreenNail(null); + setScreenNail(null); } public void prepareTextures() { @@ -399,7 +391,7 @@ public class TileImageView extends GLView { if (mIsTextureFreed) { layoutTiles(mCenterX, mCenterY, mScale, mRotation); mIsTextureFreed = false; - updateScreenNail(mModel != null ? mModel.getScreenNail() : null); + setScreenNail(mModel == null ? null : mModel.getScreenNail()); } } diff --git a/src/com/android/gallery3d/ui/TileImageViewAdapter.java b/src/com/android/gallery3d/ui/TileImageViewAdapter.java index a441c21c2..8061657fd 100644 --- a/src/com/android/gallery3d/ui/TileImageViewAdapter.java +++ b/src/com/android/gallery3d/ui/TileImageViewAdapter.java @@ -28,6 +28,7 @@ import com.android.gallery3d.common.Utils; public class TileImageViewAdapter implements TileImageView.Model { private static final String TAG = "TileImageViewAdapter"; protected ScreenNail mScreenNail; + protected boolean mOwnScreenNail; protected BitmapRegionDecoder mRegionDecoder; protected int mImageWidth; protected int mImageHeight; @@ -37,8 +38,10 @@ public class TileImageViewAdapter implements TileImageView.Model { public TileImageViewAdapter() { } - public TileImageViewAdapter(Bitmap bitmap, BitmapRegionDecoder regionDecoder) { - mScreenNail = new BitmapScreenNail(Utils.checkNotNull(bitmap), 0); + public TileImageViewAdapter( + Bitmap bitmap, BitmapRegionDecoder regionDecoder) { + Utils.checkNotNull(bitmap); + updateScreenNail(new BitmapScreenNail(bitmap, 0), true); mRegionDecoder = regionDecoder; mImageWidth = regionDecoder.getWidth(); mImageHeight = regionDecoder.getHeight(); @@ -46,7 +49,7 @@ public class TileImageViewAdapter implements TileImageView.Model { } public synchronized void clear() { - mScreenNail = null; + updateScreenNail(null, false); mImageWidth = 0; mImageHeight = 0; mLevelCount = 0; @@ -55,7 +58,8 @@ public class TileImageViewAdapter implements TileImageView.Model { } public synchronized void setScreenNail(Bitmap bitmap, int width, int height) { - mScreenNail = new BitmapScreenNail(Utils.checkNotNull(bitmap), 0); + Utils.checkNotNull(bitmap); + updateScreenNail(new BitmapScreenNail(bitmap, 0), true); mImageWidth = width; mImageHeight = height; mRegionDecoder = null; @@ -65,7 +69,8 @@ public class TileImageViewAdapter implements TileImageView.Model { public synchronized void setScreenNail( ScreenNail screenNail, int width, int height) { - mScreenNail = Utils.checkNotNull(screenNail); + Utils.checkNotNull(screenNail); + updateScreenNail(screenNail, false); mImageWidth = width; mImageHeight = height; mRegionDecoder = null; @@ -73,6 +78,14 @@ public class TileImageViewAdapter implements TileImageView.Model { mFailedToLoad = false; } + private void updateScreenNail(ScreenNail screenNail, boolean own) { + if (mScreenNail != null && mOwnScreenNail) { + mScreenNail.recycle(); + } + mScreenNail = screenNail; + mOwnScreenNail = own; + } + public synchronized void setRegionDecoder(BitmapRegionDecoder decoder) { mRegionDecoder = Utils.checkNotNull(decoder); mImageWidth = decoder.getWidth(); |