summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOwen Lin <owenlin@google.com>2012-06-28 22:33:34 -0700
committerAndroid (Google) Code Review <android-gerrit@google.com>2012-06-28 22:33:34 -0700
commit44c63eea58110b79cb4c2003c161bc2a5f9aa6f3 (patch)
tree14b99d8abdd500a9bb04dcb6193521dd8f637bb9
parent1fb473e5f9f61c3117e639286b2d62b2c34d0dbb (diff)
parentd09121f9fb7cf571a6b71c4174835dd4b7359595 (diff)
downloadandroid_packages_apps_Snap-44c63eea58110b79cb4c2003c161bc2a5f9aa6f3.tar.gz
android_packages_apps_Snap-44c63eea58110b79cb4c2003c161bc2a5f9aa6f3.tar.bz2
android_packages_apps_Snap-44c63eea58110b79cb4c2003c161bc2a5f9aa6f3.zip
Merge "Don't reuse bitmaps in BitmapRegionDecoder before JB."
-rw-r--r--gallerycommon/src/com/android/gallery3d/common/ApiHelper.java6
-rw-r--r--src/com/android/gallery3d/ui/TileImageView.java13
-rw-r--r--src/com/android/gallery3d/ui/TileImageViewAdapter.java50
3 files changed, 64 insertions, 5 deletions
diff --git a/gallerycommon/src/com/android/gallery3d/common/ApiHelper.java b/gallerycommon/src/com/android/gallery3d/common/ApiHelper.java
index 15026bbf3..c42c5fed6 100644
--- a/gallerycommon/src/com/android/gallery3d/common/ApiHelper.java
+++ b/gallerycommon/src/com/android/gallery3d/common/ApiHelper.java
@@ -16,9 +16,12 @@
package com.android.gallery3d.common;
+import android.annotation.TargetApi;
+import android.os.Build;
import android.provider.MediaStore.MediaColumns;
import android.view.View;
+@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public class ApiHelper {
public static final boolean HAS_VIEW_SYSTEM_UI_FLAG_LAYOUT_STABLE =
@@ -30,6 +33,9 @@ public class ApiHelper {
public static final boolean HAS_MEDIA_COLUMNS_WIDTH_AND_HEIGHT =
hasField(MediaColumns.class, "WIDTH");
+ public static final boolean HAS_REUSING_BITMAP_IN_BITMAP_REGION_DECODER =
+ Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN;
+
private static boolean hasField(Class<?> klass, String fieldName) {
try {
klass.getDeclaredField(fieldName);
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;