From d09121f9fb7cf571a6b71c4174835dd4b7359595 Mon Sep 17 00:00:00 2001 From: Owen Lin Date: Wed, 27 Jun 2012 19:01:21 +0800 Subject: Don't reuse bitmaps in BitmapRegionDecoder before JB. Change-Id: I8a2f520f98116af6861fa2cd1460e6f848bbb4e4 --- src/com/android/gallery3d/ui/TileImageView.java | 13 +++--- .../android/gallery3d/ui/TileImageViewAdapter.java | 50 ++++++++++++++++++++++ 2 files changed, 58 insertions(+), 5 deletions(-) (limited to 'src/com/android/gallery3d/ui') diff --git a/src/com/android/gallery3d/ui/TileImageView.java b/src/com/android/gallery3d/ui/TileImageView.java index adbf850c1..fc61efd64 100644 --- a/src/com/android/gallery3d/ui/TileImageView.java +++ b/src/com/android/gallery3d/ui/TileImageView.java @@ -23,6 +23,7 @@ import android.graphics.RectF; import android.util.FloatMath; import com.android.gallery3d.app.GalleryContext; +import com.android.gallery3d.common.ApiHelper; import com.android.gallery3d.common.LongSparseArray; import com.android.gallery3d.common.Utils; import com.android.gallery3d.data.BitmapPool; @@ -48,7 +49,9 @@ public class TileImageView extends GLView { private static final int UPLOAD_LIMIT = 1; private static final BitmapPool sTilePool = - new BitmapPool(BITMAP_SIZE, BITMAP_SIZE, 128); + ApiHelper.HAS_REUSING_BITMAP_IN_BITMAP_REGION_DECODER + ? new BitmapPool(BITMAP_SIZE, BITMAP_SIZE, 128) + : null; /* * This is the tile state in the CPU side. @@ -378,7 +381,7 @@ public class TileImageView extends GLView { } } setScreenNail(null); - sTilePool.clear(); + if (sTilePool != null) sTilePool.clear(); } public void prepareTextures() { @@ -487,7 +490,7 @@ public class TileImageView extends GLView { if (tile.mTileState == STATE_RECYCLING) { tile.mTileState = STATE_RECYCLED; if (tile.mDecodedTile != null) { - sTilePool.recycle(tile.mDecodedTile); + if (sTilePool != null) sTilePool.recycle(tile.mDecodedTile); tile.mDecodedTile = null; } mRecycledQueue.push(tile); @@ -515,7 +518,7 @@ public class TileImageView extends GLView { } tile.mTileState = STATE_RECYCLED; if (tile.mDecodedTile != null) { - sTilePool.recycle(tile.mDecodedTile); + if (sTilePool != null) sTilePool.recycle(tile.mDecodedTile); tile.mDecodedTile = null; } mRecycledQueue.push(tile); @@ -653,7 +656,7 @@ public class TileImageView extends GLView { @Override protected void onFreeBitmap(Bitmap bitmap) { - sTilePool.recycle(bitmap); + if (sTilePool != null) sTilePool.recycle(bitmap); } boolean decode() { diff --git a/src/com/android/gallery3d/ui/TileImageViewAdapter.java b/src/com/android/gallery3d/ui/TileImageViewAdapter.java index 0b4ac0375..4865e5c67 100644 --- a/src/com/android/gallery3d/ui/TileImageViewAdapter.java +++ b/src/com/android/gallery3d/ui/TileImageViewAdapter.java @@ -20,8 +20,10 @@ import android.graphics.Bitmap; import android.graphics.Bitmap.Config; import android.graphics.BitmapFactory; import android.graphics.BitmapRegionDecoder; +import android.graphics.Canvas; import android.graphics.Rect; +import com.android.gallery3d.common.ApiHelper; import com.android.gallery3d.common.Utils; import com.android.gallery3d.data.BitmapPool; @@ -108,6 +110,11 @@ public class TileImageViewAdapter implements TileImageView.Model { @Override public Bitmap getTile(int level, int x, int y, int tileSize, int borderSize, BitmapPool pool) { + + if (!ApiHelper.HAS_REUSING_BITMAP_IN_BITMAP_REGION_DECODER) { + return getTileWithoutReusingBitmap(level, x, y, tileSize, borderSize); + } + int b = borderSize << level; int t = tileSize << level; @@ -158,6 +165,49 @@ public class TileImageViewAdapter implements TileImageView.Model { return bitmap; } + private Bitmap getTileWithoutReusingBitmap( + int level, int x, int y, int tileSize, int borderSize) { + int b = borderSize << level; + int t = tileSize << level; + Rect wantRegion = new Rect(x - b, y - b, x + t + b, y + t + b); + + BitmapRegionDecoder regionDecoder; + Rect overlapRegion; + + synchronized (this) { + regionDecoder = mRegionDecoder; + if (regionDecoder == null) return null; + overlapRegion = new Rect(0, 0, mImageWidth, mImageHeight); + Utils.assertTrue(overlapRegion.intersect(wantRegion)); + } + + BitmapFactory.Options options = new BitmapFactory.Options(); + options.inPreferredConfig = Config.ARGB_8888; + options.inPreferQualityOverSpeed = true; + options.inSampleSize = (1 << level); + Bitmap bitmap = null; + + // In CropImage, we may call the decodeRegion() concurrently. + synchronized (regionDecoder) { + bitmap = regionDecoder.decodeRegion(overlapRegion, options); + } + + if (bitmap == null) { + Log.w(TAG, "fail in decoding region"); + } + + if (wantRegion.equals(overlapRegion)) return bitmap; + + int s = tileSize + 2 * borderSize; + Bitmap result = Bitmap.createBitmap(s, s, Config.ARGB_8888); + Canvas canvas = new Canvas(result); + canvas.drawBitmap(bitmap, + (overlapRegion.left - wantRegion.left) >> level, + (overlapRegion.top - wantRegion.top) >> level, null); + return result; + } + + @Override public ScreenNail getScreenNail() { return mScreenNail; -- cgit v1.2.3