Merge "Move LightCycle undo to settings area." into gb-ub-photos-arches
diff --git a/src/com/android/gallery3d/app/PhotoDataAdapter.java b/src/com/android/gallery3d/app/PhotoDataAdapter.java
index 5ab022a..66f2874 100644
--- a/src/com/android/gallery3d/app/PhotoDataAdapter.java
+++ b/src/com/android/gallery3d/app/PhotoDataAdapter.java
@@ -35,7 +35,6 @@
import com.android.gallery3d.ui.ScreenNail;
import com.android.gallery3d.ui.SynchronizedHandler;
import com.android.gallery3d.ui.TileImageViewAdapter;
-import com.android.gallery3d.ui.TiledTexture;
import com.android.gallery3d.util.Future;
import com.android.gallery3d.util.FutureListener;
import com.android.gallery3d.util.MediaSetUtils;
@@ -60,8 +59,8 @@
private static final int MSG_RUN_OBJECT = 3;
private static final int MSG_UPDATE_IMAGE_REQUESTS = 4;
- private static final int MIN_LOAD_COUNT = 16;
- private static final int DATA_CACHE_SIZE = 256;
+ private static final int MIN_LOAD_COUNT = 8;
+ private static final int DATA_CACHE_SIZE = 32;
private static final int SCREEN_NAIL_MAX = PhotoView.SCREEN_NAIL_MAX;
private static final int IMAGE_CACHE_SIZE = 2 * SCREEN_NAIL_MAX + 1;
@@ -163,7 +162,6 @@
private DataListener mDataListener;
private final SourceListener mSourceListener = new SourceListener();
- private final TiledTexture.Uploader mUploader;
// The path of the current viewing item will be stored in mItemPath.
// If mItemPath is not null, mCurrentIndex is only a hint for where we
@@ -185,8 +183,6 @@
Arrays.fill(mChanges, MediaObject.INVALID_DATA_VERSION);
- mUploader = new TiledTexture.Uploader(activity.getGLRoot());
-
mMainHandler = new SynchronizedHandler(activity.getGLRoot()) {
@SuppressWarnings("unchecked")
@Override
@@ -325,7 +321,6 @@
}
}
updateImageRequests();
- updateScreenNailUploadQueue();
}
private void updateFullImage(Path path, Future<BitmapRegionDecoder> future) {
@@ -350,8 +345,6 @@
@Override
public void resume() {
mIsActive = true;
- TiledTexture.prepareResources();
-
mSource.addContentListener(mSourceListener);
updateImageCache();
updateImageRequests();
@@ -378,9 +371,6 @@
}
mImageCache.clear();
mTileProvider.clear();
-
- mUploader.clear();
- TiledTexture.freeResources();
}
private MediaItem getItem(int index) {
@@ -404,7 +394,6 @@
updateImageCache();
updateImageRequests();
updateTileProvider();
- updateScreenNailUploadQueue();
if (mDataListener != null) {
mDataListener.onPhotoChanged(index, mItemPath);
@@ -413,32 +402,6 @@
fireDataChange();
}
- private void uploadScreenNail(int offset) {
- int index = mCurrentIndex + offset;
- if (index < mActiveStart || index >= mActiveEnd) return;
-
- MediaItem item = getItem(index);
- if (item == null) return;
-
- ImageEntry e = mImageCache.get(item.getPath());
- if (e == null) return;
-
- ScreenNail s = e.screenNail;
- if (s instanceof BitmapScreenNail) {
- TiledTexture t = ((BitmapScreenNail) s).getTexture();
- if (t != null && !t.isReady()) mUploader.addTexture(t);
- }
- }
-
- private void updateScreenNailUploadQueue() {
- mUploader.clear();
- uploadScreenNail(0);
- for (int i = 1; i < IMAGE_CACHE_SIZE; ++i) {
- uploadScreenNail(i);
- uploadScreenNail(-i);
- }
- }
-
@Override
public void moveTo(int index) {
updateCurrentIndex(index);
diff --git a/src/com/android/gallery3d/filtershow/cache/BitmapCache.java b/src/com/android/gallery3d/filtershow/cache/BitmapCache.java
index 93d57ed..e04c366 100644
--- a/src/com/android/gallery3d/filtershow/cache/BitmapCache.java
+++ b/src/com/android/gallery3d/filtershow/cache/BitmapCache.java
@@ -1,25 +1,24 @@
package com.android.gallery3d.filtershow.cache;
-import java.nio.ByteBuffer;
+import android.graphics.Bitmap;
import com.android.gallery3d.filtershow.presets.ImagePreset;
-import android.graphics.Bitmap;
-import android.util.Log;
+import java.nio.ByteBuffer;
public class BitmapCache {
private static final String LOGTAG = "BitmapCache";
static int mNbItems = 20;
- private Bitmap[] mBitmaps = new Bitmap[mNbItems];
- private Object[] mKeys = new Object[mNbItems];
- private long[] mIndices = new long[mNbItems];
- private boolean[] mBusyStatus = new boolean[mNbItems];
+ private final Bitmap[] mBitmaps = new Bitmap[mNbItems];
+ private final Object[] mKeys = new Object[mNbItems];
+ private final long[] mIndices = new long[mNbItems];
+ private final boolean[] mBusyStatus = new boolean[mNbItems];
private Bitmap mOriginalBitmap = null;
private ByteBuffer mBuffer = null;
- private Bitmap.Config mConfig = Bitmap.Config.ARGB_8888;
+ private final Bitmap.Config mConfig = Bitmap.Config.ARGB_8888;
private long mIndex = 0;
public void setOriginalBitmap(Bitmap original) {
@@ -57,8 +56,7 @@
public Bitmap put(ImagePreset preset, int pos) {
mBitmaps[pos] = mOriginalBitmap.copy(mConfig, true);
Bitmap bitmap = mBitmaps[pos];
-
- preset.apply(bitmap);
+ bitmap = preset.apply(bitmap);
mKeys[pos] = preset;
mIndices[pos] = mIndex++;
return bitmap;
@@ -80,7 +78,7 @@
ImagePreset preset = (ImagePreset) mKeys[pos];
mBitmaps[pos] = mOriginalBitmap.copy(mConfig, true);
Bitmap bitmap = mBitmaps[pos];
- preset.apply(bitmap);
+ bitmap = preset.apply(bitmap);
mIndices[pos] = mIndex++;
}
diff --git a/src/com/android/gallery3d/filtershow/cache/DelayedPresetCache.java b/src/com/android/gallery3d/filtershow/cache/DelayedPresetCache.java
index 8acb539..361190b 100644
--- a/src/com/android/gallery3d/filtershow/cache/DelayedPresetCache.java
+++ b/src/com/android/gallery3d/filtershow/cache/DelayedPresetCache.java
@@ -14,7 +14,7 @@
private final static int COMPUTE_PRESET = 1;
private Handler mProcessingHandler = null;
- private Handler mUIHandler = new Handler() {
+ private final Handler mUIHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
@@ -41,13 +41,14 @@
return false;
}
- public DelayedPresetCache(int size) {
- super(size);
+ public DelayedPresetCache(ImageLoader loader, int size) {
+ super(loader, size);
mHandlerThread = new HandlerThread("ImageProcessing", Process.THREAD_PRIORITY_BACKGROUND);
mHandlerThread.start();
mProcessingHandler = new Handler(mHandlerThread.getLooper(), this);
}
+ @Override
protected void willCompute(CachedPreset cache) {
if (cache == null) {
return;
diff --git a/src/com/android/gallery3d/filtershow/cache/DirectPresetCache.java b/src/com/android/gallery3d/filtershow/cache/DirectPresetCache.java
index 1ba8356..67bd49b 100644
--- a/src/com/android/gallery3d/filtershow/cache/DirectPresetCache.java
+++ b/src/com/android/gallery3d/filtershow/cache/DirectPresetCache.java
@@ -1,23 +1,23 @@
package com.android.gallery3d.filtershow.cache;
-import java.util.Vector;
-
import android.graphics.Bitmap;
-import android.util.Log;
import com.android.gallery3d.filtershow.imageshow.ImageShow;
import com.android.gallery3d.filtershow.presets.ImagePreset;
+import java.util.Vector;
+
public class DirectPresetCache implements Cache {
private static final String LOGTAG = "DirectPresetCache";
private Bitmap mOriginalBitmap = null;
- private Vector<ImageShow> mObservers = new Vector<ImageShow>();
- private Vector<CachedPreset> mCache = new Vector<CachedPreset>();
+ private final Vector<ImageShow> mObservers = new Vector<ImageShow>();
+ private final Vector<CachedPreset> mCache = new Vector<CachedPreset>();
private int mCacheSize = 1;
- private Bitmap.Config mBitmapConfig = Bitmap.Config.ARGB_8888;
+ private final Bitmap.Config mBitmapConfig = Bitmap.Config.ARGB_8888;
private long mGlobalAge = 0;
+ private ImageLoader mLoader = null;
protected class CachedPreset {
private Bitmap mBitmap = null;
@@ -34,10 +34,12 @@
}
}
- public DirectPresetCache(int size) {
+ public DirectPresetCache(ImageLoader loader, int size) {
+ mLoader = loader;
mCacheSize = size;
}
+ @Override
public void setOriginalBitmap(Bitmap bitmap) {
mOriginalBitmap = bitmap;
notifyObservers();
@@ -50,6 +52,7 @@
}
}
+ @Override
public void addObserver(ImageShow observer) {
if (!mObservers.contains(observer)) {
mObservers.add(observer);
@@ -66,6 +69,7 @@
return null;
}
+ @Override
public Bitmap get(ImagePreset preset) {
// Log.v(LOGTAG, "get preset " + preset.name() + " : " + preset);
CachedPreset cache = getCachedPreset(preset);
@@ -77,6 +81,7 @@
return null;
}
+ @Override
public void reset(ImagePreset preset) {
CachedPreset cache = getCachedPreset(preset);
if (cache != null && !cache.mBusy) {
@@ -120,10 +125,16 @@
protected void compute(CachedPreset cache) {
cache.mBitmap = null;
cache.mBitmap = mOriginalBitmap.copy(mBitmapConfig, true);
- cache.mPreset.apply(cache.mBitmap);
+ float scaleFactor = (float) cache.mBitmap.getWidth() / (float) mLoader.getOriginalBounds().width();
+ if (scaleFactor < 1.0f) {
+ cache.mPreset.setIsHighQuality(false);
+ }
+ cache.mPreset.setScaleFactor(scaleFactor);
+ cache.mBitmap = cache.mPreset.apply(cache.mBitmap);
cache.mAge = mGlobalAge++;
}
+ @Override
public void prepare(ImagePreset preset) {
// Log.v(LOGTAG, "prepare preset " + preset.name() + " : " + preset);
CachedPreset cache = getCachedPreset(preset);
diff --git a/src/com/android/gallery3d/filtershow/cache/ImageLoader.java b/src/com/android/gallery3d/filtershow/cache/ImageLoader.java
index 9944f5f..8d8024e 100644
--- a/src/com/android/gallery3d/filtershow/cache/ImageLoader.java
+++ b/src/com/android/gallery3d/filtershow/cache/ImageLoader.java
@@ -35,16 +35,16 @@
public class ImageLoader {
private static final String LOGTAG = "ImageLoader";
- private Vector<ImageShow> mListeners = new Vector<ImageShow>();
+ private final Vector<ImageShow> mListeners = new Vector<ImageShow>();
private Bitmap mOriginalBitmapSmall = null;
private Bitmap mOriginalBitmapLarge = null;
private Bitmap mBackgroundBitmap = null;
private Bitmap mFullOriginalBitmap = null;
private Bitmap mSaveCopy = null;
- private Cache mCache = new DelayedPresetCache(30);
- private Cache mHiresCache = new DelayedPresetCache(2);
- private ZoomCache mZoomCache = new ZoomCache();
+ private Cache mCache = null;
+ private Cache mHiresCache = null;
+ private final ZoomCache mZoomCache = new ZoomCache();
private int mOrientation = 0;
private HistoryAdapter mAdapter = null;
@@ -57,6 +57,8 @@
public ImageLoader(Context context) {
mContext = context;
+ mCache = new DelayedPresetCache(this, 30);
+ mHiresCache = new DelayedPresetCache(this, 2);
}
public void loadBitmap(Uri uri) {
@@ -230,7 +232,7 @@
if (bmp != null) {
// TODO: this workaround for RS might not be needed ultimately
Bitmap bmp2 = bmp.copy(Bitmap.Config.ARGB_8888, true);
- imagePreset.apply(bmp2);
+ bmp2 = imagePreset.apply(bmp2);
mZoomCache.setImage(imagePreset, bounds, bmp2);
return bmp2;
}
@@ -289,6 +291,8 @@
// TODO: on <3.x we need a copy of the bitmap (inMutable doesn't
// exist)
mSaveCopy = mFullOriginalBitmap;
+ preset.setIsHighQuality(true);
+ preset.setScaleFactor(1.0f);
ProcessedBitmap processedBitmap = new ProcessedBitmap(mSaveCopy, preset);
new SaveCopyTask(mContext, mUri, destination, new SaveCopyTask.Callback() {
diff --git a/src/com/android/gallery3d/filtershow/filters/ImageFilter.java b/src/com/android/gallery3d/filtershow/filters/ImageFilter.java
index c039fce..662e8ed 100644
--- a/src/com/android/gallery3d/filtershow/filters/ImageFilter.java
+++ b/src/com/android/gallery3d/filtershow/filters/ImageFilter.java
@@ -2,7 +2,6 @@
package com.android.gallery3d.filtershow.filters;
import android.graphics.Bitmap;
-import android.util.Log;
public class ImageFilter implements Cloneable {
@@ -26,8 +25,9 @@
return mName;
}
- public void apply(Bitmap bitmap) {
+ public Bitmap apply(Bitmap bitmap, float scaleFactor, boolean highQuality) {
// do nothing here, subclasses will implement filtering here
+ return bitmap;
}
public int getParameter() {
diff --git a/src/com/android/gallery3d/filtershow/filters/ImageFilterBW.java b/src/com/android/gallery3d/filtershow/filters/ImageFilterBW.java
index bc3dd09..bdbd1d5 100644
--- a/src/com/android/gallery3d/filtershow/filters/ImageFilterBW.java
+++ b/src/com/android/gallery3d/filtershow/filters/ImageFilterBW.java
@@ -11,10 +11,12 @@
native protected void nativeApplyFilter(Bitmap bitmap, int w, int h);
- public void apply(Bitmap bitmap) {
+ @Override
+ public Bitmap apply(Bitmap bitmap, float scaleFactor, boolean highQuality) {
int w = bitmap.getWidth();
int h = bitmap.getHeight();
nativeApplyFilter(bitmap, w, h);
+ return bitmap;
}
}
diff --git a/src/com/android/gallery3d/filtershow/filters/ImageFilterBWBlue.java b/src/com/android/gallery3d/filtershow/filters/ImageFilterBWBlue.java
index 7368be5..b3c8a9e 100644
--- a/src/com/android/gallery3d/filtershow/filters/ImageFilterBWBlue.java
+++ b/src/com/android/gallery3d/filtershow/filters/ImageFilterBWBlue.java
@@ -11,10 +11,12 @@
native protected void nativeApplyFilter(Bitmap bitmap, int w, int h);
- public void apply(Bitmap bitmap) {
+ @Override
+ public Bitmap apply(Bitmap bitmap, float scaleFactor, boolean highQuality) {
int w = bitmap.getWidth();
int h = bitmap.getHeight();
nativeApplyFilter(bitmap, w, h);
+ return bitmap;
}
}
diff --git a/src/com/android/gallery3d/filtershow/filters/ImageFilterBWGreen.java b/src/com/android/gallery3d/filtershow/filters/ImageFilterBWGreen.java
index f4e6c61..e3963dc 100644
--- a/src/com/android/gallery3d/filtershow/filters/ImageFilterBWGreen.java
+++ b/src/com/android/gallery3d/filtershow/filters/ImageFilterBWGreen.java
@@ -11,10 +11,12 @@
native protected void nativeApplyFilter(Bitmap bitmap, int w, int h);
- public void apply(Bitmap bitmap) {
+ @Override
+ public Bitmap apply(Bitmap bitmap, float scaleFactor, boolean highQuality) {
int w = bitmap.getWidth();
int h = bitmap.getHeight();
nativeApplyFilter(bitmap, w, h);
+ return bitmap;
}
}
diff --git a/src/com/android/gallery3d/filtershow/filters/ImageFilterBWRed.java b/src/com/android/gallery3d/filtershow/filters/ImageFilterBWRed.java
index bb7d661..19b3c6b 100644
--- a/src/com/android/gallery3d/filtershow/filters/ImageFilterBWRed.java
+++ b/src/com/android/gallery3d/filtershow/filters/ImageFilterBWRed.java
@@ -11,10 +11,12 @@
native protected void nativeApplyFilter(Bitmap bitmap, int w, int h);
- public void apply(Bitmap bitmap) {
+ @Override
+ public Bitmap apply(Bitmap bitmap, float scaleFactor, boolean highQuality) {
int w = bitmap.getWidth();
int h = bitmap.getHeight();
nativeApplyFilter(bitmap, w, h);
+ return bitmap;
}
}
diff --git a/src/com/android/gallery3d/filtershow/filters/ImageFilterBorder.java b/src/com/android/gallery3d/filtershow/filters/ImageFilterBorder.java
index 7df7a2b..67904c6 100644
--- a/src/com/android/gallery3d/filtershow/filters/ImageFilterBorder.java
+++ b/src/com/android/gallery3d/filtershow/filters/ImageFilterBorder.java
@@ -1,13 +1,10 @@
package com.android.gallery3d.filtershow.filters;
-import com.android.gallery3d.R;
-
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
-import android.graphics.drawable.NinePatchDrawable;
public class ImageFilterBorder extends ImageFilter {
Drawable mNinePatch = null;
@@ -24,6 +21,7 @@
mNinePatch = ninePatch;
}
+ @Override
public boolean same(ImageFilter filter) {
boolean isBorderFilter = super.same(filter);
if (!isBorderFilter) {
@@ -41,9 +39,10 @@
mNinePatch = ninePatch;
}
- public void apply(Bitmap bitmap) {
+ @Override
+ public Bitmap apply(Bitmap bitmap, float scaleFactor, boolean highQuality) {
if (mNinePatch == null) {
- return;
+ return bitmap;
}
int w = bitmap.getWidth();
@@ -53,5 +52,6 @@
Canvas canvas = new Canvas(bitmap);
mNinePatch.setBounds(bounds);
mNinePatch.draw(canvas);
+ return bitmap;
}
}
diff --git a/src/com/android/gallery3d/filtershow/filters/ImageFilterContrast.java b/src/com/android/gallery3d/filtershow/filters/ImageFilterContrast.java
index b3ef74d..5117f60 100644
--- a/src/com/android/gallery3d/filtershow/filters/ImageFilterContrast.java
+++ b/src/com/android/gallery3d/filtershow/filters/ImageFilterContrast.java
@@ -11,11 +11,13 @@
native protected void nativeApplyFilter(Bitmap bitmap, int w, int h, float strength);
- public void apply(Bitmap bitmap) {
+ @Override
+ public Bitmap apply(Bitmap bitmap, float scaleFactor, boolean highQuality) {
int w = bitmap.getWidth();
int h = bitmap.getHeight();
float p = mParameter;
float value = p;
nativeApplyFilter(bitmap, w, h, value);
+ return bitmap;
}
}
diff --git a/src/com/android/gallery3d/filtershow/filters/ImageFilterCurves.java b/src/com/android/gallery3d/filtershow/filters/ImageFilterCurves.java
index 5de8ff4..01b280b 100644
--- a/src/com/android/gallery3d/filtershow/filters/ImageFilterCurves.java
+++ b/src/com/android/gallery3d/filtershow/filters/ImageFilterCurves.java
@@ -1,16 +1,15 @@
package com.android.gallery3d.filtershow.filters;
-import com.android.gallery3d.filtershow.ui.Spline;
-
import android.graphics.Bitmap;
-import android.util.Log;
+
+import com.android.gallery3d.filtershow.ui.Spline;
public class ImageFilterCurves extends ImageFilter {
private static final String LOGTAG = "ImageFilterCurves";
- private float[] mCurve = new float[256];
+ private final float[] mCurve = new float[256];
private boolean mUseRed = true;
private boolean mUseGreen = true;
@@ -49,6 +48,7 @@
}
}
+ @Override
public boolean same(ImageFilter filter) {
boolean isCurveFilter = super.same(filter);
if (!isCurveFilter) {
@@ -69,8 +69,8 @@
}
}
- public void apply(Bitmap bitmap) {
-
+ @Override
+ public Bitmap apply(Bitmap bitmap, float scaleFactor, boolean highQuality) {
int[] redGradient = null;
if (mUseRed) {
redGradient = new int[256];
@@ -89,6 +89,7 @@
nativeApplyGradientFilter(bitmap, bitmap.getWidth(), bitmap.getHeight(),
redGradient, greenGradient, blueGradient);
+ return bitmap;
}
public void setSpline(Spline spline) {
diff --git a/src/com/android/gallery3d/filtershow/filters/ImageFilterExposure.java b/src/com/android/gallery3d/filtershow/filters/ImageFilterExposure.java
index 85b6e4f..ee87413 100644
--- a/src/com/android/gallery3d/filtershow/filters/ImageFilterExposure.java
+++ b/src/com/android/gallery3d/filtershow/filters/ImageFilterExposure.java
@@ -12,11 +12,12 @@
native protected void nativeApplyFilter(Bitmap bitmap, int w, int h, float bright);
@Override
- public void apply(Bitmap bitmap) {
+ public Bitmap apply(Bitmap bitmap, float scaleFactor, boolean highQuality) {
int w = bitmap.getWidth();
int h = bitmap.getHeight();
int p = mParameter;
float value = p;
nativeApplyFilter(bitmap, w, h, value);
+ return bitmap;
}
}
diff --git a/src/com/android/gallery3d/filtershow/filters/ImageFilterGradient.java b/src/com/android/gallery3d/filtershow/filters/ImageFilterGradient.java
index d27dd34..4fbd086 100644
--- a/src/com/android/gallery3d/filtershow/filters/ImageFilterGradient.java
+++ b/src/com/android/gallery3d/filtershow/filters/ImageFilterGradient.java
@@ -10,8 +10,6 @@
import android.graphics.Paint.Style;
import android.graphics.Shader.TileMode;
-import com.android.gallery3d.filtershow.ui.Spline;
-
public class ImageFilterGradient extends ImageFilter {
private Bitmap mGradientBitmap = null;
@@ -50,8 +48,8 @@
mPositions = positions;
}
- public void apply(Bitmap bitmap) {
-
+ @Override
+ public Bitmap apply(Bitmap bitmap, float scaleFactor, boolean highQuality) {
createGradient();
int[] gradient = new int[256];
int[] redGradient = new int[256];
@@ -66,6 +64,7 @@
}
nativeApplyGradientFilter(bitmap, bitmap.getWidth(), bitmap.getHeight(),
redGradient, greenGradient, blueGradient);
+ return bitmap;
}
public void createGradient() {
diff --git a/src/com/android/gallery3d/filtershow/filters/ImageFilterHue.java b/src/com/android/gallery3d/filtershow/filters/ImageFilterHue.java
index 874e7ec..6f6f9e8 100644
--- a/src/com/android/gallery3d/filtershow/filters/ImageFilterHue.java
+++ b/src/com/android/gallery3d/filtershow/filters/ImageFilterHue.java
@@ -20,7 +20,8 @@
native protected void nativeApplyFilter(Bitmap bitmap, int w, int h, float []matrix);
- public void apply(Bitmap bitmap) {
+ @Override
+ public Bitmap apply(Bitmap bitmap, float scaleFactor, boolean highQuality) {
int w = bitmap.getWidth();
int h = bitmap.getHeight();
float p = mParameter;
@@ -29,5 +30,7 @@
cmatrix.setHue(value);
nativeApplyFilter(bitmap, w, h, cmatrix.getMatrix());
+
+ return bitmap;
}
}
diff --git a/src/com/android/gallery3d/filtershow/filters/ImageFilterRS.java b/src/com/android/gallery3d/filtershow/filters/ImageFilterRS.java
index ab2d304..ab92e4e 100644
--- a/src/com/android/gallery3d/filtershow/filters/ImageFilterRS.java
+++ b/src/com/android/gallery3d/filtershow/filters/ImageFilterRS.java
@@ -31,9 +31,10 @@
mOutPixelsAllocation.copyTo(bitmap);
}
- public void apply(Bitmap bitmap) {
+ @Override
+ public Bitmap apply(Bitmap bitmap, float scaleFactor, boolean highQuality) {
if (bitmap == null) {
- return;
+ return bitmap;
}
try {
prepare(bitmap);
@@ -45,6 +46,7 @@
} catch (android.renderscript.RSRuntimeException e) {
Log.e(LOGTAG, "RS runtime exception ? " + e);
}
+ return bitmap;
}
public static RenderScript getRenderScriptContext() {
diff --git a/src/com/android/gallery3d/filtershow/filters/ImageFilterSaturated.java b/src/com/android/gallery3d/filtershow/filters/ImageFilterSaturated.java
index d3db441..9047056 100644
--- a/src/com/android/gallery3d/filtershow/filters/ImageFilterSaturated.java
+++ b/src/com/android/gallery3d/filtershow/filters/ImageFilterSaturated.java
@@ -11,11 +11,13 @@
native protected void nativeApplyFilter(Bitmap bitmap, int w, int h, float saturation);
- public void apply(Bitmap bitmap) {
+ @Override
+ public Bitmap apply(Bitmap bitmap, float scaleFactor, boolean highQuality) {
int w = bitmap.getWidth();
int h = bitmap.getHeight();
int p = mParameter;
float value = 1 + p / 100.0f;
nativeApplyFilter(bitmap, w, h, value);
+ return bitmap;
}
}
diff --git a/src/com/android/gallery3d/filtershow/filters/ImageFilterShadows.java b/src/com/android/gallery3d/filtershow/filters/ImageFilterShadows.java
index 39c9506..9b379a1 100644
--- a/src/com/android/gallery3d/filtershow/filters/ImageFilterShadows.java
+++ b/src/com/android/gallery3d/filtershow/filters/ImageFilterShadows.java
@@ -11,8 +11,8 @@
private final float MID = .5f;
private final float HIGHLIGHT = .9f;
- private float []baseX = {0f,SHADOW,MID,HIGHLIGHT,1f};
- private float []baseY = {0f,SHADOW,MID,HIGHLIGHT,1f};
+ private final float []baseX = {0f,SHADOW,MID,HIGHLIGHT,1f};
+ private final float []baseY = {0f,SHADOW,MID,HIGHLIGHT,1f};
public ImageFilterShadows() {
mName = "Shadows";
@@ -50,12 +50,14 @@
native protected void nativeApplyFilter(Bitmap bitmap, int w, int h, short []valMap);
- public void apply(Bitmap bitmap) {
+ @Override
+ public Bitmap apply(Bitmap bitmap, float scaleFactor, boolean highQuality) {
int w = bitmap.getWidth();
int h = bitmap.getHeight();
float p = mParameter;
baseY[1] = (float)(SHADOW*Math.pow(4, mParameter/100.));
nativeApplyFilter(bitmap, w, h, calcMap());
+ return bitmap;
}
}
diff --git a/src/com/android/gallery3d/filtershow/filters/ImageFilterStraighten.java b/src/com/android/gallery3d/filtershow/filters/ImageFilterStraighten.java
index 483d901..db85292 100644
--- a/src/com/android/gallery3d/filtershow/filters/ImageFilterStraighten.java
+++ b/src/com/android/gallery3d/filtershow/filters/ImageFilterStraighten.java
@@ -1,16 +1,13 @@
package com.android.gallery3d.filtershow.filters;
-import java.nio.ByteBuffer;
-
import android.graphics.Bitmap;
-import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
public class ImageFilterStraighten extends ImageFilter {
- private Bitmap.Config mConfig = Bitmap.Config.ARGB_8888;
+ private final Bitmap.Config mConfig = Bitmap.Config.ARGB_8888;
private float mRotation;
private float mZoomFactor;
@@ -39,7 +36,8 @@
mZoomFactor = zoomFactor;
}
- public void apply(Bitmap bitmap) {
+ @Override
+ public Bitmap apply(Bitmap bitmap, float scaleFactor, boolean highQuality) {
// TODO: implement bilinear or bicubic here... for now, just use
// canvas to do a simple implementation...
// TODO: and be more memory efficient! (do it in native?)
@@ -64,6 +62,7 @@
temp.recycle();
temp = null;
pixels = null;
+ return bitmap;
}
}
diff --git a/src/com/android/gallery3d/filtershow/filters/ImageFilterVibrance.java b/src/com/android/gallery3d/filtershow/filters/ImageFilterVibrance.java
index fd437ee..8281573 100644
--- a/src/com/android/gallery3d/filtershow/filters/ImageFilterVibrance.java
+++ b/src/com/android/gallery3d/filtershow/filters/ImageFilterVibrance.java
@@ -12,11 +12,13 @@
native protected void nativeApplyFilter(Bitmap bitmap, int w, int h, float bright);
@Override
- public void apply(Bitmap bitmap) {
+ public Bitmap apply(Bitmap bitmap, float scaleFactor, boolean highQuality) {
int w = bitmap.getWidth();
int h = bitmap.getHeight();
int p = mParameter;
float value = p;
nativeApplyFilter(bitmap, w, h, value);
+
+ return bitmap;
}
}
diff --git a/src/com/android/gallery3d/filtershow/filters/ImageFilterVignette.java b/src/com/android/gallery3d/filtershow/filters/ImageFilterVignette.java
index 24699a2..4c43410 100644
--- a/src/com/android/gallery3d/filtershow/filters/ImageFilterVignette.java
+++ b/src/com/android/gallery3d/filtershow/filters/ImageFilterVignette.java
@@ -11,11 +11,14 @@
native protected void nativeApplyFilter(Bitmap bitmap, int w, int h, float strength);
- public void apply(Bitmap bitmap) {
+ @Override
+ public Bitmap apply(Bitmap bitmap, float scaleFactor, boolean highQuality) {
int w = bitmap.getWidth();
int h = bitmap.getHeight();
float p = mParameter;
float value = p / 100.0f;
nativeApplyFilter(bitmap, w, h, value);
+
+ return bitmap;
}
}
diff --git a/src/com/android/gallery3d/filtershow/presets/ImagePreset.java b/src/com/android/gallery3d/filtershow/presets/ImagePreset.java
index 8dd81d4..c0c3103 100644
--- a/src/com/android/gallery3d/filtershow/presets/ImagePreset.java
+++ b/src/com/android/gallery3d/filtershow/presets/ImagePreset.java
@@ -32,6 +32,9 @@
protected boolean mVerticalFlip = false;
protected RectF mCrop = null;
+ private float mScaleFactor = 1.0f;
+ private boolean mIsHighQuality = false;
+
public ImagePreset() {
setup();
}
@@ -50,6 +53,9 @@
mStraightenRotate = source.mStraightenRotate;
mStraightenZoom = source.mStraightenZoom;
+
+ mScaleFactor = source.mScaleFactor;
+ mIsHighQuality = source.mIsHighQuality;
}
public void setStraightenRotation(float rotate, float zoom) {
@@ -57,7 +63,7 @@
mStraightenZoom = zoom;
}
- private Bitmap applyGeometry(Bitmap original) {
+ private Bitmap applyGeometry(Bitmap original, float scaleFactor, boolean highQuality) {
Bitmap bitmap = original;
if (mFullRotate != FullRotate.ZERO) {
@@ -67,7 +73,7 @@
if (mStraightenRotate != 0) {
// TODO: keep the instances around
ImageFilter straighten = new ImageFilterStraighten(mStraightenRotate, mStraightenZoom);
- straighten.apply(bitmap);
+ bitmap = straighten.apply(bitmap, scaleFactor, highQuality);
straighten = null;
}
@@ -154,7 +160,7 @@
public Bitmap apply(Bitmap original) {
// First we apply any transform -- 90 rotate, flip, straighten, crop
- Bitmap bitmap = applyGeometry(original);
+ Bitmap bitmap = applyGeometry(original, mScaleFactor, mIsHighQuality);
// TODO -- apply borders separately
ImageFilter borderFilter = null;
@@ -164,11 +170,11 @@
// TODO don't use the name as an id
borderFilter = filter;
} else {
- filter.apply(bitmap);
+ bitmap = filter.apply(bitmap, mScaleFactor, mIsHighQuality);
}
}
if (borderFilter != null) {
- borderFilter.apply(bitmap);
+ bitmap = borderFilter.apply(bitmap, mScaleFactor, mIsHighQuality);
}
if (mEndPoint != null) {
mEndPoint.updateFilteredImage(bitmap);
@@ -185,4 +191,19 @@
imageStateAdapter.notifyDataSetChanged();
}
+ public float getScaleFactor() {
+ return mScaleFactor;
+ }
+
+ public boolean isHighQuality() {
+ return mIsHighQuality;
+ }
+
+ public void setIsHighQuality(boolean value) {
+ mIsHighQuality = value;
+ }
+
+ public void setScaleFactor(float value) {
+ mScaleFactor = value;
+ }
}
diff --git a/src/com/android/gallery3d/filtershow/tools/ProcessedBitmap.java b/src/com/android/gallery3d/filtershow/tools/ProcessedBitmap.java
index 2e23b92..24e38fb 100644
--- a/src/com/android/gallery3d/filtershow/tools/ProcessedBitmap.java
+++ b/src/com/android/gallery3d/filtershow/tools/ProcessedBitmap.java
@@ -6,13 +6,13 @@
public class ProcessedBitmap {
private Bitmap mBitmap;
- private ImagePreset mPreset;
+ private final ImagePreset mPreset;
public ProcessedBitmap(Bitmap bitmap, ImagePreset preset) {
mBitmap = bitmap;
mPreset = preset;
}
public Bitmap apply() {
- mPreset.apply(mBitmap);
+ mBitmap = mPreset.apply(mBitmap);
return mBitmap;
}
}
\ No newline at end of file
diff --git a/src/com/android/gallery3d/ui/BasicTexture.java b/src/com/android/gallery3d/ui/BasicTexture.java
index 99cf057..7b8e30d 100644
--- a/src/com/android/gallery3d/ui/BasicTexture.java
+++ b/src/com/android/gallery3d/ui/BasicTexture.java
@@ -42,8 +42,8 @@
protected int mWidth = UNSPECIFIED;
protected int mHeight = UNSPECIFIED;
- protected int mTextureWidth;
- protected int mTextureHeight;
+ private int mTextureWidth;
+ private int mTextureHeight;
private boolean mHasBorder;
diff --git a/src/com/android/gallery3d/ui/BitmapScreenNail.java b/src/com/android/gallery3d/ui/BitmapScreenNail.java
index 9b62916..bf31bcb 100644
--- a/src/com/android/gallery3d/ui/BitmapScreenNail.java
+++ b/src/com/android/gallery3d/ui/BitmapScreenNail.java
@@ -46,16 +46,16 @@
private int mWidth;
private int mHeight;
- private long mAnimationStartTime = ANIMATION_NOT_NEEDED;
-
private Bitmap mBitmap;
- private TiledTexture mTexture;
+ private BitmapTexture mTexture;
+ private long mAnimationStartTime = ANIMATION_NOT_NEEDED;
public BitmapScreenNail(Bitmap bitmap) {
mWidth = bitmap.getWidth();
mHeight = bitmap.getHeight();
mBitmap = bitmap;
- mTexture = new TiledTexture(bitmap);
+ // We create mTexture lazily, so we don't incur the cost if we don't
+ // actually need it.
}
public BitmapScreenNail(int width, int height) {
@@ -103,14 +103,17 @@
BitmapScreenNail newer = (BitmapScreenNail) other;
mWidth = newer.mWidth;
mHeight = newer.mHeight;
- if (newer.mTexture != null) {
+ if (newer.mBitmap != null) {
recycleBitmap(MediaItem.getThumbPool(), mBitmap);
- if (mTexture != null) mTexture.recycle();
mBitmap = newer.mBitmap;
- mTexture = newer.mTexture;
newer.mBitmap = null;
- newer.mTexture = null;
+
+ if (mTexture != null) {
+ mTexture.recycle();
+ mTexture = null;
+ }
}
+
newer.recycle();
return this;
}
@@ -155,7 +158,7 @@
@Override
public void draw(GLCanvas canvas, int x, int y, int width, int height) {
- if (mTexture == null || !mTexture.isReady()) {
+ if (mBitmap == null) {
if (mAnimationStartTime == ANIMATION_NOT_NEEDED) {
mAnimationStartTime = ANIMATION_NEEDED;
}
@@ -165,12 +168,16 @@
return;
}
+ if (mTexture == null) {
+ mTexture = new BitmapTexture(mBitmap);
+ }
+
if (mAnimationStartTime == ANIMATION_NEEDED) {
- mAnimationStartTime = AnimationTime.get();
+ mAnimationStartTime = now();
}
if (isAnimating()) {
- mTexture.drawMixed(canvas, mPlaceholderColor, getRatio(), x, y,
+ canvas.drawMixed(mTexture, mPlaceholderColor, getRatio(), x, y,
width, height);
} else {
mTexture.draw(canvas, x, y, width, height);
@@ -179,26 +186,34 @@
@Override
public void draw(GLCanvas canvas, RectF source, RectF dest) {
- if (mTexture == null || !mTexture.isReady()) {
+ if (mBitmap == null) {
canvas.fillRect(dest.left, dest.top, dest.width(), dest.height(),
mPlaceholderColor);
return;
}
- mTexture.draw(canvas, source, dest);
+ if (mTexture == null) {
+ mTexture = new BitmapTexture(mBitmap);
+ }
+
+ canvas.drawTexture(mTexture, source, dest);
}
public boolean isAnimating() {
if (mAnimationStartTime < 0) return false;
- if (AnimationTime.get() - mAnimationStartTime >= DURATION) {
+ if (now() - mAnimationStartTime >= DURATION) {
mAnimationStartTime = ANIMATION_DONE;
return false;
}
return true;
}
+ private static long now() {
+ return AnimationTime.get();
+ }
+
private float getRatio() {
- float r = (float) (AnimationTime.get() - mAnimationStartTime) / DURATION;
+ float r = (float)(now() - mAnimationStartTime) / DURATION;
return Utils.clamp(1.0f - r, 0.0f, 1.0f);
}
@@ -206,10 +221,6 @@
return (mBitmap == null) || isAnimating();
}
- public TiledTexture getTexture() {
- return mTexture;
- }
-
public static void setMaxSide(int size) {
sMaxSide = size;
}
diff --git a/src/com/android/gallery3d/ui/GLCanvas.java b/src/com/android/gallery3d/ui/GLCanvas.java
index 6f8baef..e3a32ef 100644
--- a/src/com/android/gallery3d/ui/GLCanvas.java
+++ b/src/com/android/gallery3d/ui/GLCanvas.java
@@ -99,13 +99,6 @@
public void drawMixed(BasicTexture from, int toColor,
float ratio, int x, int y, int w, int h);
- // Draw a region of a texture and a specified color to the specified
- // rectangle. The actual color used is from * (1 - ratio) + to * ratio.
- // The region of the texture is defined by parameter "src". The target
- // rectangle is specified by parameter "target".
- public void drawMixed(BasicTexture from, int toColor,
- float ratio, RectF src, RectF target);
-
// Gets the underlying GL instance. This is used only when direct access to
// GL is needed.
public GL11 getGLInstance();
diff --git a/src/com/android/gallery3d/ui/GLCanvasImpl.java b/src/com/android/gallery3d/ui/GLCanvasImpl.java
index 45903b3..d83daf3 100644
--- a/src/com/android/gallery3d/ui/GLCanvasImpl.java
+++ b/src/com/android/gallery3d/ui/GLCanvasImpl.java
@@ -415,7 +415,7 @@
// This function changes the source coordinate to the texture coordinates.
// It also clips the source and target coordinates if it is beyond the
// bound of the texture.
- private static void convertCoordinate(RectF source, RectF target,
+ private void convertCoordinate(RectF source, RectF target,
BasicTexture texture) {
int width = texture.getWidth();
@@ -465,82 +465,6 @@
color[3] = alpha;
}
- private void setMixedColor(int toColor, float ratio, float alpha) {
- //
- // The formula we want:
- // alpha * ((1 - ratio) * from + ratio * to)
- //
- // The formula that GL supports is in the form of:
- // combo * from + (1 - combo) * to * scale
- //
- // So, we have combo = alpha * (1 - ratio)
- // and scale = alpha * ratio / (1 - combo)
- //
- float combo = alpha * (1 - ratio);
- float scale = alpha * ratio / (1 - combo);
-
- // Specify the interpolation factor via the alpha component of
- // GL_TEXTURE_ENV_COLORs.
- // RGB component are get from toColor and will used as SRC1
- float colorScale = scale * (toColor >>> 24) / (0xff * 0xff);
- setTextureColor(((toColor >>> 16) & 0xff) * colorScale,
- ((toColor >>> 8) & 0xff) * colorScale,
- (toColor & 0xff) * colorScale, combo);
- GL11 gl = mGL;
- gl.glTexEnvfv(GL11.GL_TEXTURE_ENV, GL11.GL_TEXTURE_ENV_COLOR, mTextureColor, 0);
-
- gl.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_COMBINE_RGB, GL11.GL_INTERPOLATE);
- gl.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_COMBINE_ALPHA, GL11.GL_INTERPOLATE);
- gl.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_SRC1_RGB, GL11.GL_CONSTANT);
- gl.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_OPERAND1_RGB, GL11.GL_SRC_COLOR);
- gl.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_SRC1_ALPHA, GL11.GL_CONSTANT);
- gl.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_OPERAND1_ALPHA, GL11.GL_SRC_ALPHA);
-
- // Wire up the interpolation factor for RGB.
- gl.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_SRC2_RGB, GL11.GL_CONSTANT);
- gl.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_OPERAND2_RGB, GL11.GL_SRC_ALPHA);
-
- // Wire up the interpolation factor for alpha.
- gl.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_SRC2_ALPHA, GL11.GL_CONSTANT);
- gl.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_OPERAND2_ALPHA, GL11.GL_SRC_ALPHA);
-
- }
-
- @Override
- public void drawMixed(BasicTexture from, int toColor, float ratio,
- RectF source, RectF target) {
- if (target.width() <= 0 || target.height() <= 0) return;
-
- if (ratio <= 0.01f) {
- drawTexture(from, source, target);
- return;
- } else if (ratio >= 1) {
- fillRect(target.left, target.top, target.width(), target.height(), toColor);
- return;
- }
-
- float alpha = mAlpha;
-
- // Copy the input to avoid changing it.
- mDrawTextureSourceRect.set(source);
- mDrawTextureTargetRect.set(target);
- source = mDrawTextureSourceRect;
- target = mDrawTextureTargetRect;
-
- mGLState.setBlendEnabled(mBlendEnabled && (!from.isOpaque()
- || !Utils.isOpaque(toColor) || alpha < OPAQUE_ALPHA));
-
- if (!bindTexture(from)) return;
-
- // Interpolate the RGB and alpha values between both textures.
- mGLState.setTexEnvMode(GL11.GL_COMBINE);
- setMixedColor(toColor, ratio, alpha);
- convertCoordinate(source, target, from);
- setTextureCoords(source);
- textureRect(target.left, target.top, target.width(), target.height());
- mGLState.setTexEnvMode(GL11.GL_REPLACE);
- }
-
private void drawMixed(BasicTexture from, int toColor,
float ratio, int x, int y, int width, int height, float alpha) {
// change from 0 to 0.01f to prevent getting divided by zero below
@@ -558,9 +482,45 @@
final GL11 gl = mGL;
if (!bindTexture(from)) return;
+ //
+ // The formula we want:
+ // alpha * ((1 - ratio) * from + ratio * to)
+ //
+ // The formula that GL supports is in the form of:
+ // combo * from + (1 - combo) * to * scale
+ //
+ // So, we have combo = alpha * (1 - ratio)
+ // and scale = alpha * ratio / (1 - combo)
+ //
+ float combo = alpha * (1 - ratio);
+ float scale = alpha * ratio / (1 - combo);
+
// Interpolate the RGB and alpha values between both textures.
mGLState.setTexEnvMode(GL11.GL_COMBINE);
- setMixedColor(toColor, ratio, alpha);
+
+ // Specify the interpolation factor via the alpha component of
+ // GL_TEXTURE_ENV_COLORs.
+ // RGB component are get from toColor and will used as SRC1
+ float colorScale = scale * (toColor >>> 24) / (0xff * 0xff);
+ setTextureColor(((toColor >>> 16) & 0xff) * colorScale,
+ ((toColor >>> 8) & 0xff) * colorScale,
+ (toColor & 0xff) * colorScale, combo);
+ gl.glTexEnvfv(GL11.GL_TEXTURE_ENV, GL11.GL_TEXTURE_ENV_COLOR, mTextureColor, 0);
+
+ gl.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_COMBINE_RGB, GL11.GL_INTERPOLATE);
+ gl.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_COMBINE_ALPHA, GL11.GL_INTERPOLATE);
+ gl.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_SRC1_RGB, GL11.GL_CONSTANT);
+ gl.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_OPERAND1_RGB, GL11.GL_SRC_COLOR);
+ gl.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_SRC1_ALPHA, GL11.GL_CONSTANT);
+ gl.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_OPERAND1_ALPHA, GL11.GL_SRC_ALPHA);
+
+ // Wire up the interpolation factor for RGB.
+ gl.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_SRC2_RGB, GL11.GL_CONSTANT);
+ gl.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_OPERAND2_RGB, GL11.GL_SRC_ALPHA);
+
+ // Wire up the interpolation factor for alpha.
+ gl.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_SRC2_ALPHA, GL11.GL_CONSTANT);
+ gl.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_OPERAND2_ALPHA, GL11.GL_SRC_ALPHA);
drawBoundTexture(from, x, y, width, height);
mGLState.setTexEnvMode(GL11.GL_REPLACE);
diff --git a/src/com/android/gallery3d/ui/TileImageView.java b/src/com/android/gallery3d/ui/TileImageView.java
index 18a7af8..5ce06be 100644
--- a/src/com/android/gallery3d/ui/TileImageView.java
+++ b/src/com/android/gallery3d/ui/TileImageView.java
@@ -575,10 +575,8 @@
}
if (tile == null) break;
if (!tile.isContentValid()) {
- boolean hasBeenLoaded = tile.isLoaded();
Utils.assertTrue(tile.mTileState == STATE_DECODED);
tile.updateContent(canvas);
- if (!hasBeenLoaded) tile.draw(canvas, 0, 0);
--quota;
}
}
@@ -623,6 +621,7 @@
}
}
+ // TODO: avoid drawing the unused part of the textures.
static boolean drawTile(
Tile tile, GLCanvas canvas, RectF source, RectF target) {
while (true) {
diff --git a/src/com/android/gallery3d/ui/TiledTexture.java b/src/com/android/gallery3d/ui/TiledTexture.java
deleted file mode 100644
index 6e9ad9e..0000000
--- a/src/com/android/gallery3d/ui/TiledTexture.java
+++ /dev/null
@@ -1,298 +0,0 @@
-/*
- * 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;
-
-import android.graphics.Bitmap;
-import android.graphics.Bitmap.Config;
-import android.graphics.Canvas;
-import android.graphics.Color;
-import android.graphics.Paint;
-import android.graphics.PorterDuff.Mode;
-import android.graphics.PorterDuffXfermode;
-import android.graphics.RectF;
-
-import com.android.gallery3d.ui.GLRoot.OnGLIdleListener;
-
-import java.util.ArrayDeque;
-import java.util.ArrayList;
-
-// This class is similar to BitmapTexture, except the bitmap is
-// split into tiles. By doing so, we may increase the time required to
-// upload the whole bitmap but we reduce the time of uploading each tile
-// so it make the animation more smooth and prevents jank.
-public class TiledTexture {
- private static final int CONTENT_SIZE = 254;
- private static final int BORDER_SIZE = 1;
- private static final int TILE_SIZE = CONTENT_SIZE + 2 * BORDER_SIZE;
- private static final int INIT_CAPACITY = 8;
-
- private static Tile sFreeTileHead = null;
- private static final Object sFreeTileLock = new Object();
-
- private static Bitmap sUploadBitmap;
- private static Canvas sCanvas;
- private static Paint sPaint;
-
- private int mUploadIndex = 0;
-
- private final Tile[] mTiles;
- private final int mWidth;
- private final int mHeight;
- private final RectF mSrcRect = new RectF();
- private final RectF mDestRect = new RectF();
-
- public static class Uploader implements OnGLIdleListener {
- private final ArrayDeque<TiledTexture> mTextures =
- new ArrayDeque<TiledTexture>(INIT_CAPACITY);
-
- private final GLRoot mGlRoot;
- private boolean mIsQueued = false;
-
- public Uploader(GLRoot glRoot) {
- mGlRoot = glRoot;
- }
-
- public synchronized void clear() {
- mTextures.clear();
- }
-
- public synchronized void addTexture(TiledTexture t) {
- if (t.isReady()) return;
- mTextures.addLast(t);
-
- if (mIsQueued) return;
- mIsQueued = true;
- mGlRoot.addOnGLIdleListener(this);
- }
-
-
- @Override
- public boolean onGLIdle(GLCanvas canvas, boolean renderRequested) {
- ArrayDeque<TiledTexture> deque = mTextures;
- synchronized (this) {
- if (!deque.isEmpty()) {
- TiledTexture t = deque.peekFirst();
- if (t.uploadNextTile(canvas)) {
- deque.removeFirst();
- mGlRoot.requestRender();
- }
- }
- mIsQueued = !mTextures.isEmpty();
-
- // return true to keep this listener in the queue
- return mIsQueued;
- }
- }
- }
-
- private static class Tile extends UploadedTexture {
- public int offsetX;
- public int offsetY;
- public Bitmap bitmap;
- public Tile nextFreeTile;
- public int contentWidth;
- public int contentHeight;
-
- @Override
- public void setSize(int width, int height) {
- contentWidth = width;
- contentHeight = height;
- mWidth = width + 2 * BORDER_SIZE;
- mHeight = height + 2 * BORDER_SIZE;
- mTextureWidth = TILE_SIZE;
- mTextureHeight = TILE_SIZE;
- }
-
- @Override
- protected Bitmap onGetBitmap() {
- int x = BORDER_SIZE - offsetX;
- int y = BORDER_SIZE - offsetY;
- int r = bitmap.getWidth() - x;
- int b = bitmap.getHeight() - y ;
- sCanvas.drawBitmap(bitmap, x, y, null);
- bitmap = null;
-
- // draw borders if need
- if (x > 0) sCanvas.drawLine(x - 1, 0, x - 1, TILE_SIZE, sPaint);
- if (y > 0) sCanvas.drawLine(0, y - 1, TILE_SIZE, y - 1, sPaint);
- if (r < CONTENT_SIZE) sCanvas.drawLine(r, 0, r, TILE_SIZE, sPaint);
- if (b < CONTENT_SIZE) sCanvas.drawLine(0, b, TILE_SIZE, b, sPaint);
-
- return sUploadBitmap;
- }
-
- @Override
- protected void onFreeBitmap(Bitmap bitmap) {
- // do nothing
- }
- }
-
- private static void freeTile(Tile tile) {
- tile.invalidateContent();
- tile.bitmap = null;
- synchronized (sFreeTileLock) {
- tile.nextFreeTile = sFreeTileHead;
- sFreeTileHead = tile;
- }
- }
-
- private static Tile obtainTile() {
- synchronized (sFreeTileLock) {
- Tile result = sFreeTileHead;
- if (result == null) return new Tile();
- sFreeTileHead = result.nextFreeTile;
- result.nextFreeTile = null;
- return result;
- }
- }
-
- private boolean uploadNextTile(GLCanvas canvas) {
- if (mUploadIndex == mTiles.length) return true;
- Tile next = mTiles[mUploadIndex++];
- boolean hasBeenLoad = next.isLoaded();
- next.updateContent(canvas);
-
- // It will take some time for a texture to be drawn for the first
- // time. When scrolling, we need to draw several tiles on the screen
- // at the same time. It may cause a UI jank even these textures has
- // been uploaded.
- if (!hasBeenLoad) next.draw(canvas, 0, 0);
- return mUploadIndex == mTiles.length;
- }
-
- public TiledTexture(Bitmap bitmap) {
- mWidth = bitmap.getWidth();
- mHeight = bitmap.getHeight();
- ArrayList<Tile> list = new ArrayList<Tile>();
-
- for (int x = 0, w = mWidth; x < w; x += CONTENT_SIZE) {
- for (int y = 0, h = mHeight; y < h; y += CONTENT_SIZE) {
- Tile tile = obtainTile();
- tile.offsetX = x;
- tile.offsetY = y;
- tile.bitmap = bitmap;
- tile.setSize(
- Math.min(CONTENT_SIZE, mWidth - x),
- Math.min(CONTENT_SIZE, mHeight - y));
- list.add(tile);
- }
- }
- mTiles = list.toArray(new Tile[list.size()]);
- }
-
- public boolean isReady() {
- return mUploadIndex == mTiles.length;
- }
-
- public void recycle() {
- for (int i = 0, n = mTiles.length; i < n; ++i) {
- freeTile(mTiles[i]);
- }
- }
-
- public static void freeResources() {
- sUploadBitmap = null;
- sCanvas = null;
- sPaint = null;
- }
-
- public static void prepareResources() {
- sUploadBitmap = Bitmap.createBitmap(TILE_SIZE, TILE_SIZE, Config.ARGB_8888);
- sCanvas = new Canvas(sUploadBitmap);
- sPaint = new Paint(Paint.FILTER_BITMAP_FLAG);
- sPaint.setColor(Color.TRANSPARENT);
- sPaint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
- }
-
- // We want to draw the "source" on the "target".
- // This method is to find the "output" rectangle which is
- // the corresponding area of the "src".
- // (x,y) target
- // (x0,y0) source +---------------+
- // +----------+ | |
- // | src | | output |
- // | +--+ | linear map | +----+ |
- // | +--+ | ----------> | | | |
- // | | by (scaleX, scaleY) | +----+ |
- // +----------+ | |
- // Texture +---------------+
- // Canvas
- private static void mapRect(RectF output,
- RectF src, float x0, float y0, float x, float y, float scaleX,
- float scaleY) {
- output.set(x + (src.left - x0) * scaleX,
- y + (src.top - y0) * scaleY,
- x + (src.right - x0) * scaleX,
- y + (src.bottom - y0) * scaleY);
- }
-
- // Draws a mixed color of this texture and a specified color onto the
- // a rectangle. The used color is: from * (1 - ratio) + to * ratio.
- public void drawMixed(GLCanvas canvas, int color, float ratio,
- int x, int y, int width, int height) {
- RectF src = mSrcRect;
- RectF dest = mDestRect;
- float scaleX = (float) width / mWidth ;
- float scaleY = (float) height / mHeight;
- for (int i = 0, n = mTiles.length; i < n; ++i) {
- Tile t = mTiles[i];
- src.set(0, 0, t.contentWidth, t.contentHeight);
- src.offset(t.offsetX, t.offsetY);
- mapRect(dest, src, 0, 0, x, y, scaleX, scaleY);
- src.offset(BORDER_SIZE - t.offsetX, BORDER_SIZE - t.offsetY);
- canvas.drawMixed(t, color, ratio, mSrcRect, mDestRect);
- }
- }
-
- // Draws the texture on to the specified rectangle.
- public void draw(GLCanvas canvas, int x, int y, int width, int height) {
- RectF src = mSrcRect;
- RectF dest = mDestRect;
- float scaleX = (float) width / mWidth ;
- float scaleY = (float) height / mHeight;
- for (int i = 0, n = mTiles.length; i < n; ++i) {
- Tile t = mTiles[i];
- src.set(0, 0, t.contentWidth, t.contentHeight);
- src.offset(t.offsetX, t.offsetY);
- mapRect(dest, src, 0, 0, x, y, scaleX, scaleY);
- src.offset(BORDER_SIZE - t.offsetX, BORDER_SIZE - t.offsetY);
- canvas.drawTexture(t, mSrcRect, mDestRect);
- }
- }
-
- // Draws a sub region of this texture on to the specified rectangle.
- public void draw(GLCanvas canvas, RectF source, RectF target) {
- RectF src = mSrcRect;
- RectF dest = mDestRect;
- float x0 = source.left;
- float y0 = source.top;
- float x = target.left;
- float y = target.top;
- float scaleX = target.width() / source.width();
- float scaleY = target.height() / source.height();
-
- for (int i = 0, n = mTiles.length; i < n; ++i) {
- Tile t = mTiles[i];
- src.set(0, 0, t.contentWidth, t.contentHeight);
- src.offset(t.offsetX, t.offsetY);
- if (!src.intersect(source)) continue;
- mapRect(dest, src, x0, y0, x, y, scaleX, scaleY);
- src.offset(BORDER_SIZE - t.offsetX, BORDER_SIZE - t.offsetY);
- canvas.drawTexture(t, src, dest);
- }
- }
-}
diff --git a/tests/src/com/android/gallery3d/ui/GLCanvasStub.java b/tests/src/com/android/gallery3d/ui/GLCanvasStub.java
index 5a08b85..2f2d753 100644
--- a/tests/src/com/android/gallery3d/ui/GLCanvasStub.java
+++ b/tests/src/com/android/gallery3d/ui/GLCanvasStub.java
@@ -83,6 +83,4 @@
public void dumpStatisticsAndClear() {}
public void beginRenderTarget(RawTexture texture) {}
public void endRenderTarget() {}
- public void drawMixed(BasicTexture from, int toColor,
- float ratio, RectF src, RectF target) {}
}