summaryrefslogtreecommitdiffstats
path: root/src/com/android/gallery3d/filtershow/pipeline
diff options
context:
space:
mode:
authornicolasroard <nicolasroard@google.com>2013-08-15 09:58:50 -0700
committernicolasroard <nicolasroard@google.com>2013-08-15 09:58:50 -0700
commitbd9a4acc7a9b607d18d08cb2199d46afa023548b (patch)
tree985436d651cfd13e405b865a1786db964dfdba28 /src/com/android/gallery3d/filtershow/pipeline
parent2228e7f0000db4c3f7fb92519f9f4c2856dd47a1 (diff)
downloadandroid_packages_apps_Gallery2-bd9a4acc7a9b607d18d08cb2199d46afa023548b.tar.gz
android_packages_apps_Gallery2-bd9a4acc7a9b607d18d08cb2199d46afa023548b.tar.bz2
android_packages_apps_Gallery2-bd9a4acc7a9b607d18d08cb2199d46afa023548b.zip
Improves bitmap reuse / memory usage
bug:8782701 Change-Id: Ic655d6f1704dd0429e96819566f50927de02b994
Diffstat (limited to 'src/com/android/gallery3d/filtershow/pipeline')
-rw-r--r--src/com/android/gallery3d/filtershow/pipeline/Buffer.java18
-rw-r--r--src/com/android/gallery3d/filtershow/pipeline/CacheProcessing.java3
-rw-r--r--src/com/android/gallery3d/filtershow/pipeline/CachingPipeline.java5
-rw-r--r--src/com/android/gallery3d/filtershow/pipeline/FilterEnvironment.java44
-rw-r--r--src/com/android/gallery3d/filtershow/pipeline/SharedBuffer.java5
5 files changed, 32 insertions, 43 deletions
diff --git a/src/com/android/gallery3d/filtershow/pipeline/Buffer.java b/src/com/android/gallery3d/filtershow/pipeline/Buffer.java
index 744451229..7b51a75d7 100644
--- a/src/com/android/gallery3d/filtershow/pipeline/Buffer.java
+++ b/src/com/android/gallery3d/filtershow/pipeline/Buffer.java
@@ -19,19 +19,23 @@ package com.android.gallery3d.filtershow.pipeline;
import android.graphics.Bitmap;
import android.support.v8.renderscript.Allocation;
import android.support.v8.renderscript.RenderScript;
+import android.util.Log;
+import com.android.gallery3d.filtershow.cache.BitmapCache;
+import com.android.gallery3d.filtershow.imageshow.MasterImage;
public class Buffer {
private static final String LOGTAG = "Buffer";
private Bitmap mBitmap;
private Allocation mAllocation;
private boolean mUseAllocation = false;
- private static final Bitmap.Config BITMAP_CONFIG = Bitmap.Config.ARGB_8888;
private ImagePreset mPreset;
public Buffer(Bitmap bitmap) {
RenderScript rs = CachingPipeline.getRenderScriptContext();
if (bitmap != null) {
- mBitmap = bitmap.copy(BITMAP_CONFIG, true);
+ BitmapCache cache = MasterImage.getImage().getBitmapCache();
+ cache.cache(mBitmap);
+ mBitmap = cache.getBitmapCopy(bitmap);
}
if (mUseAllocation) {
// TODO: recreate the allocation when the RS context changes
@@ -41,10 +45,6 @@ public class Buffer {
}
}
- public void setBitmap(Bitmap bitmap) {
- mBitmap = bitmap.copy(BITMAP_CONFIG, true);
- }
-
public Bitmap getBitmap() {
return mBitmap;
}
@@ -70,5 +70,11 @@ public class Buffer {
mPreset.updateWith(preset);
}
}
+
+ public void remove() {
+ BitmapCache cache = MasterImage.getImage().getBitmapCache();
+ cache.cache(mBitmap);
+ mBitmap = null;
+ }
}
diff --git a/src/com/android/gallery3d/filtershow/pipeline/CacheProcessing.java b/src/com/android/gallery3d/filtershow/pipeline/CacheProcessing.java
index edd07d226..8f6eda261 100644
--- a/src/com/android/gallery3d/filtershow/pipeline/CacheProcessing.java
+++ b/src/com/android/gallery3d/filtershow/pipeline/CacheProcessing.java
@@ -145,6 +145,7 @@ public class CacheProcessing {
if (similar) {
similarUpToIndex = i;
} else {
+ environment.cache(cacheStep.cache);
mSteps.remove(i);
mSteps.insertElementAt(newStep, i);
}
@@ -188,9 +189,9 @@ public class CacheProcessing {
Log.v(LOGTAG, "i: " + i + " get new copy for cacheBitmap "
+ cacheBitmap + " apply...");
}
+ environment.cache(step.cache);
cacheBitmap = environment.getBitmapCopy(cacheBitmap);
cacheBitmap = step.apply(environment, cacheBitmap);
- environment.cache(step.cache);
step.cache = cacheBitmap;
}
}
diff --git a/src/com/android/gallery3d/filtershow/pipeline/CachingPipeline.java b/src/com/android/gallery3d/filtershow/pipeline/CachingPipeline.java
index 932e2fc00..0794277b0 100644
--- a/src/com/android/gallery3d/filtershow/pipeline/CachingPipeline.java
+++ b/src/com/android/gallery3d/filtershow/pipeline/CachingPipeline.java
@@ -157,6 +157,7 @@ public class CachingPipeline implements PipelineInterface {
private void setupEnvironment(ImagePreset preset, boolean highResPreview) {
mEnvironment.setPipeline(this);
mEnvironment.setFiltersManager(mFiltersManager);
+ mEnvironment.setBitmapCache(MasterImage.getImage().getBitmapCache());
if (highResPreview) {
mEnvironment.setScaleFactor(mHighResPreviewScaleFactor);
} else {
@@ -213,8 +214,7 @@ public class CachingPipeline implements PipelineInterface {
if (bitmap == null) {
return;
}
- // TODO: use a cache of bitmaps
- bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
+ bitmap = mEnvironment.getBitmapCopy(bitmap);
bitmap = preset.applyGeometry(bitmap, mEnvironment);
mEnvironment.setQuality(FilterEnvironment.QUALITY_PREVIEW);
@@ -251,6 +251,7 @@ public class CachingPipeline implements PipelineInterface {
if (request.getType() == RenderingRequest.PARTIAL_RENDERING) {
MasterImage master = MasterImage.getImage();
bitmap = ImageLoader.getScaleOneImageForPreset(master.getActivity(),
+ mEnvironment,
master.getUri(), request.getBounds(),
request.getDestination());
if (bitmap == null) {
diff --git a/src/com/android/gallery3d/filtershow/pipeline/FilterEnvironment.java b/src/com/android/gallery3d/filtershow/pipeline/FilterEnvironment.java
index b540d96dd..2757aff11 100644
--- a/src/com/android/gallery3d/filtershow/pipeline/FilterEnvironment.java
+++ b/src/com/android/gallery3d/filtershow/pipeline/FilterEnvironment.java
@@ -20,6 +20,7 @@ import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.support.v8.renderscript.Allocation;
+import com.android.gallery3d.filtershow.cache.BitmapCache;
import com.android.gallery3d.filtershow.filters.FilterRepresentation;
import com.android.gallery3d.filtershow.filters.FilterUserPresetRepresentation;
import com.android.gallery3d.filtershow.filters.FiltersManagerInterface;
@@ -36,6 +37,7 @@ public class FilterEnvironment {
private FiltersManagerInterface mFiltersManager;
private PipelineInterface mPipeline;
private volatile boolean mStop = false;
+ private BitmapCache mBitmapCache;
public static final int QUALITY_ICON = 0;
public static final int QUALITY_PREVIEW = 1;
@@ -49,53 +51,27 @@ public class FilterEnvironment {
this.mStop = stop;
}
- private HashMap<Long, WeakReference<Bitmap>>
- bitmapCach = new HashMap<Long, WeakReference<Bitmap>>();
-
private HashMap<Integer, Integer>
generalParameters = new HashMap<Integer, Integer>();
+ public void setBitmapCache(BitmapCache cache) {
+ mBitmapCache = cache;
+ }
+
public void cache(Buffer buffer) {
- if (buffer == null) {
- return;
- }
- Bitmap bitmap = buffer.getBitmap();
- cache(bitmap);
+ mBitmapCache.cache(buffer);
}
public void cache(Bitmap bitmap) {
- if (bitmap == null) {
- return;
- }
- Long key = calcKey(bitmap.getWidth(), bitmap.getHeight());
- bitmapCach.put(key, new WeakReference<Bitmap>(bitmap));
+ mBitmapCache.cache(bitmap);
}
public Bitmap getBitmap(int w, int h) {
- Long key = calcKey(w, h);
- WeakReference<Bitmap> ref = bitmapCach.remove(key);
- Bitmap bitmap = null;
- if (ref != null) {
- bitmap = ref.get();
- }
- if (bitmap == null
- || bitmap.getWidth() != w
- || bitmap.getHeight() != h) {
- bitmap = Bitmap.createBitmap(
- w, h, Bitmap.Config.ARGB_8888);
- }
- return bitmap;
+ return mBitmapCache.getBitmap(w, h);
}
public Bitmap getBitmapCopy(Bitmap source) {
- Bitmap bitmap = getBitmap(source.getWidth(), source.getHeight());
- Canvas canvas = new Canvas(bitmap);
- canvas.drawBitmap(source, 0, 0, null);
- return bitmap;
- }
-
- private Long calcKey(long w, long h) {
- return (w << 32) | h;
+ return mBitmapCache.getBitmapCopy(source);
}
public void setImagePreset(ImagePreset imagePreset) {
diff --git a/src/com/android/gallery3d/filtershow/pipeline/SharedBuffer.java b/src/com/android/gallery3d/filtershow/pipeline/SharedBuffer.java
index 98e69f60e..871e4cd6c 100644
--- a/src/com/android/gallery3d/filtershow/pipeline/SharedBuffer.java
+++ b/src/com/android/gallery3d/filtershow/pipeline/SharedBuffer.java
@@ -30,6 +30,11 @@ public class SharedBuffer {
private volatile boolean mNeedsRepaint = true;
public void setProducer(Bitmap producer) {
+ synchronized (this) {
+ if (mProducer != null) {
+ mProducer.remove();
+ }
+ }
Buffer buffer = new Buffer(producer);
synchronized (this) {
mProducer = buffer;