summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authornicolasroard <nicolasroard@google.com>2013-03-20 14:55:48 -0700
committernicolasroard <nicolasroard@google.com>2013-03-20 19:34:06 -0700
commit7847f8d2fef696ac30d502a9875afeb0c727a5f1 (patch)
tree0a3a4fa20f83c43d3bf1128d709f422adf2d6b68 /src
parenta40965b95af786ed2aa13ff63e38b92881f3c315 (diff)
downloadandroid_packages_apps_Snap-7847f8d2fef696ac30d502a9875afeb0c727a5f1.tar.gz
android_packages_apps_Snap-7847f8d2fef696ac30d502a9875afeb0c727a5f1.tar.bz2
android_packages_apps_Snap-7847f8d2fef696ac30d502a9875afeb0c727a5f1.zip
Fix crashes with RS filters
bug:8418537 Change-Id: I37f8ad8a77f04faed9f721122872f00f7aaaa365
Diffstat (limited to 'src')
-rw-r--r--src/com/android/gallery3d/filtershow/cache/CachingPipeline.java57
-rw-r--r--src/com/android/gallery3d/filtershow/filters/ImageFilter.java18
-rw-r--r--src/com/android/gallery3d/filtershow/filters/ImageFilterRS.java78
-rw-r--r--src/com/android/gallery3d/filtershow/filters/ImageFilterSharpen.java10
-rw-r--r--src/com/android/gallery3d/filtershow/presets/FilterEnvironment.java16
5 files changed, 107 insertions, 72 deletions
diff --git a/src/com/android/gallery3d/filtershow/cache/CachingPipeline.java b/src/com/android/gallery3d/filtershow/cache/CachingPipeline.java
index 9d4da0214..8d7fcd5d5 100644
--- a/src/com/android/gallery3d/filtershow/cache/CachingPipeline.java
+++ b/src/com/android/gallery3d/filtershow/cache/CachingPipeline.java
@@ -30,6 +30,8 @@ public class CachingPipeline {
private static final String LOGTAG = "CachingPipeline";
private boolean DEBUG = false;
+ private static final Bitmap.Config BITMAP_CONFIG = Bitmap.Config.ARGB_8888;
+
private FiltersManager mFiltersManager = null;
private volatile Bitmap mOriginalBitmap = null;
private volatile Bitmap mResizedOriginalBitmap = null;
@@ -37,6 +39,11 @@ public class CachingPipeline {
private volatile Allocation mOriginalAllocation = null;
private volatile Allocation mFiltersOnlyOriginalAllocation = null;
+ protected volatile Allocation mInPixelsAllocation;
+ protected volatile Allocation mOutPixelsAllocation;
+ private volatile int mWidth = 0;
+ private volatile int mHeight = 0;
+
private volatile GeometryMetadata mPreviousGeometry = null;
private volatile float mPreviewScaleFactor = 1.0f;
@@ -60,6 +67,21 @@ public class CachingPipeline {
}
mPreviousGeometry = null;
mPreviewScaleFactor = 1.0f;
+
+ destroyPixelAllocations();
+ }
+
+ private synchronized void destroyPixelAllocations() {
+ if (mInPixelsAllocation != null) {
+ mInPixelsAllocation.destroy();
+ mInPixelsAllocation = null;
+ }
+ if (mOutPixelsAllocation != null) {
+ mOutPixelsAllocation.destroy();
+ mOutPixelsAllocation = null;
+ }
+ mWidth = 0;
+ mHeight = 0;
}
private String getType(RenderingRequest request) {
@@ -85,6 +107,7 @@ public class CachingPipeline {
preset.setScaleFactor(mPreviewScaleFactor);
preset.setQuality(ImagePreset.QUALITY_PREVIEW);
preset.setupEnvironment(mFiltersManager);
+ preset.getEnvironment().setCachingPipeline(this);
}
public void setOriginal(Bitmap bitmap) {
@@ -244,4 +267,38 @@ public class CachingPipeline {
public synchronized boolean isInitialized() {
return mOriginalBitmap != null;
}
+
+ public boolean prepareRenderscriptAllocations(Bitmap bitmap) {
+ RenderScript RS = ImageFilterRS.getRenderScriptContext();
+ boolean needsUpdate = false;
+ if (mOutPixelsAllocation == null || mInPixelsAllocation == null ||
+ bitmap.getWidth() != mWidth || bitmap.getHeight() != mHeight) {
+ destroyPixelAllocations();
+ Bitmap bitmapBuffer = bitmap;
+ if (bitmap.getConfig() == null || bitmap.getConfig() != BITMAP_CONFIG) {
+ bitmapBuffer = bitmap.copy(BITMAP_CONFIG, true);
+ }
+ mOutPixelsAllocation = Allocation.createFromBitmap(RS, bitmapBuffer,
+ Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
+ mInPixelsAllocation = Allocation.createTyped(RS,
+ mOutPixelsAllocation.getType());
+ needsUpdate = true;
+ }
+ mInPixelsAllocation.copyFrom(bitmap);
+ if (bitmap.getWidth() != mWidth
+ || bitmap.getHeight() != mHeight) {
+ mWidth = bitmap.getWidth();
+ mHeight = bitmap.getHeight();
+ needsUpdate = true;
+ }
+ return needsUpdate;
+ }
+
+ public Allocation getInPixelsAllocation() {
+ return mInPixelsAllocation;
+ }
+
+ public Allocation getOutPixelsAllocation() {
+ return mOutPixelsAllocation;
+ }
}
diff --git a/src/com/android/gallery3d/filtershow/filters/ImageFilter.java b/src/com/android/gallery3d/filtershow/filters/ImageFilter.java
index bd9dcafcf..63a76627a 100644
--- a/src/com/android/gallery3d/filtershow/filters/ImageFilter.java
+++ b/src/com/android/gallery3d/filtershow/filters/ImageFilter.java
@@ -22,11 +22,11 @@ import android.widget.Toast;
import com.android.gallery3d.filtershow.FilterShowActivity;
import com.android.gallery3d.filtershow.imageshow.GeometryMetadata;
+import com.android.gallery3d.filtershow.presets.FilterEnvironment;
import com.android.gallery3d.filtershow.presets.ImagePreset;
public abstract class ImageFilter implements Cloneable {
-
- private ImagePreset mImagePreset;
+ private FilterEnvironment mEnvironment = null;
protected String mName = "Original";
private final String LOGTAG = "ImageFilter";
@@ -79,11 +79,7 @@ public abstract class ImageFilter implements Cloneable {
}
public ImagePreset getImagePreset() {
- return mImagePreset;
- }
-
- public void setImagePreset(ImagePreset imagePreset) {
- mImagePreset = imagePreset;
+ return getEnvironment().getImagePreset();
}
public abstract void useRepresentation(FilterRepresentation representation);
@@ -103,4 +99,12 @@ public abstract class ImageFilter implements Cloneable {
w, h);
return originalToScreen;
}
+
+ public void setEnvironment(FilterEnvironment environment) {
+ mEnvironment = environment;
+ }
+
+ public FilterEnvironment getEnvironment() {
+ return mEnvironment;
+ }
}
diff --git a/src/com/android/gallery3d/filtershow/filters/ImageFilterRS.java b/src/com/android/gallery3d/filtershow/filters/ImageFilterRS.java
index 338cf51cd..b91051dbb 100644
--- a/src/com/android/gallery3d/filtershow/filters/ImageFilterRS.java
+++ b/src/com/android/gallery3d/filtershow/filters/ImageFilterRS.java
@@ -23,53 +23,15 @@ import android.support.v8.renderscript.*;
import android.util.Log;
import android.content.res.Resources;
import com.android.gallery3d.R;
+import com.android.gallery3d.filtershow.cache.CachingPipeline;
public abstract class ImageFilterRS extends ImageFilter {
private static final String LOGTAG = "ImageFilterRS";
- protected static volatile Allocation mInPixelsAllocation;
- protected static volatile Allocation mOutPixelsAllocation;
-
private static volatile RenderScript sRS = null;
- private static volatile int sWidth = 0;
- private static volatile int sHeight = 0;
-
private static volatile Resources sResources = null;
private volatile boolean mResourcesLoaded = false;
- private static final Bitmap.Config BITMAP_CONFIG = Bitmap.Config.ARGB_8888;
-
- private volatile Bitmap mSourceBitmap = null;
-
- public Bitmap getSourceBitmap() {
- return mSourceBitmap;
- }
-
- // This must be used inside block synchronized on ImageFilterRS class object
- protected void prepare(Bitmap bitmap, float scaleFactor, int quality) {
- if (mOutPixelsAllocation == null || mInPixelsAllocation == null ||
- bitmap.getWidth() != sWidth || bitmap.getHeight() != sHeight) {
- destroyPixelAllocations();
- Bitmap bitmapBuffer = bitmap;
- if (bitmap.getConfig() == null || bitmap.getConfig() != BITMAP_CONFIG) {
- bitmapBuffer = bitmap.copy(BITMAP_CONFIG, true);
- }
- mOutPixelsAllocation = Allocation.createFromBitmap(sRS, bitmapBuffer,
- Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);
- mInPixelsAllocation = Allocation.createTyped(sRS,
- mOutPixelsAllocation.getType());
- }
- mInPixelsAllocation.copyFrom(bitmap);
- if (bitmap.getWidth() != sWidth
- || bitmap.getHeight() != sHeight || !isResourcesLoaded()) {
- freeResources();
- createFilter(sResources, scaleFactor, quality);
- sWidth = bitmap.getWidth();
- sHeight = bitmap.getHeight();
- setResourcesLoaded(true);
- }
- }
-
// This must be used inside block synchronized on ImageFilterRS class object
protected abstract void createFilter(android.content.res.Resources res,
float scaleFactor, int quality);
@@ -79,7 +41,17 @@ public abstract class ImageFilterRS extends ImageFilter {
// This must be used inside block synchronized on ImageFilterRS class object
protected void update(Bitmap bitmap) {
- mOutPixelsAllocation.copyTo(bitmap);
+ getOutPixelsAllocation().copyTo(bitmap);
+ }
+
+ protected Allocation getInPixelsAllocation() {
+ CachingPipeline pipeline = getEnvironment().getCachingPipeline();
+ return pipeline.getInPixelsAllocation();
+ }
+
+ protected Allocation getOutPixelsAllocation() {
+ CachingPipeline pipeline = getEnvironment().getCachingPipeline();
+ return pipeline.getOutPixelsAllocation();
}
@Override
@@ -93,8 +65,14 @@ public abstract class ImageFilterRS extends ImageFilter {
Log.w(LOGTAG, "Cannot apply before calling createRenderScriptContext");
return bitmap;
}
- mSourceBitmap = bitmap;
- prepare(bitmap, scaleFactor, quality);
+ CachingPipeline pipeline = getEnvironment().getCachingPipeline();
+ boolean needsUpdate = pipeline.prepareRenderscriptAllocations(bitmap);
+ if (needsUpdate || !isResourcesLoaded()) {
+ // the allocations changed size
+ freeResources();
+ createFilter(sResources, scaleFactor, quality);
+ setResourcesLoaded(true);
+ }
runFilter();
update(bitmap);
}
@@ -108,7 +86,6 @@ public abstract class ImageFilterRS extends ImageFilter {
displayLowMemoryToast();
Log.e(LOGTAG, "not enough memory for filter " + getName(), e);
}
- mSourceBitmap = null;
return bitmap;
}
@@ -123,24 +100,9 @@ public abstract class ImageFilterRS extends ImageFilter {
}
sRS = RenderScript.create(context);
sResources = context.getResources();
- destroyPixelAllocations();
- }
-
- private static synchronized void destroyPixelAllocations() {
- if (mInPixelsAllocation != null) {
- mInPixelsAllocation.destroy();
- mInPixelsAllocation = null;
- }
- if (mOutPixelsAllocation != null) {
- mOutPixelsAllocation.destroy();
- mOutPixelsAllocation = null;
- }
- sWidth = 0;
- sHeight = 0;
}
public static synchronized void destroyRenderScriptContext() {
- destroyPixelAllocations();
sRS.destroy();
sRS = null;
sResources = null;
diff --git a/src/com/android/gallery3d/filtershow/filters/ImageFilterSharpen.java b/src/com/android/gallery3d/filtershow/filters/ImageFilterSharpen.java
index af31735a6..f545cd986 100644
--- a/src/com/android/gallery3d/filtershow/filters/ImageFilterSharpen.java
+++ b/src/com/android/gallery3d/filtershow/filters/ImageFilterSharpen.java
@@ -63,8 +63,8 @@ public class ImageFilterSharpen extends ImageFilterRS {
@Override
protected void createFilter(android.content.res.Resources res, float scaleFactor,
int quality) {
- int w = mInPixelsAllocation.getType().getX();
- int h = mInPixelsAllocation.getType().getY();
+ int w = getInPixelsAllocation().getType().getX();
+ int h = getInPixelsAllocation().getType().getY();
mScaleFactor = scaleFactor;
if (mScript == null) {
@@ -97,9 +97,9 @@ public class ImageFilterSharpen extends ImageFilterRS {
return;
}
computeKernel();
- mScript.set_gIn(mInPixelsAllocation);
- mScript.bind_gPixels(mInPixelsAllocation);
- mScript.forEach_root(mInPixelsAllocation, mOutPixelsAllocation);
+ mScript.set_gIn(getInPixelsAllocation());
+ mScript.bind_gPixels(getInPixelsAllocation());
+ mScript.forEach_root(getInPixelsAllocation(), getOutPixelsAllocation());
}
}
diff --git a/src/com/android/gallery3d/filtershow/presets/FilterEnvironment.java b/src/com/android/gallery3d/filtershow/presets/FilterEnvironment.java
index c20502290..c45036012 100644
--- a/src/com/android/gallery3d/filtershow/presets/FilterEnvironment.java
+++ b/src/com/android/gallery3d/filtershow/presets/FilterEnvironment.java
@@ -17,6 +17,7 @@
package com.android.gallery3d.filtershow.presets;
import android.graphics.Bitmap;
+import com.android.gallery3d.filtershow.cache.CachingPipeline;
import com.android.gallery3d.filtershow.filters.FilterRepresentation;
import com.android.gallery3d.filtershow.filters.FiltersManager;
import com.android.gallery3d.filtershow.filters.ImageFilter;
@@ -26,6 +27,7 @@ public class FilterEnvironment {
private float mScaleFactor;
private int mQuality;
private FiltersManager mFiltersManager;
+ private CachingPipeline mCachingPipeline;
public void setImagePreset(ImagePreset imagePreset) {
mImagePreset = imagePreset;
@@ -62,7 +64,17 @@ public class FilterEnvironment {
public Bitmap applyRepresentation(FilterRepresentation representation, Bitmap bitmap) {
ImageFilter filter = mFiltersManager.getFilterForRepresentation(representation);
filter.useRepresentation(representation);
- filter.setImagePreset(mImagePreset);
- return filter.apply(bitmap, mScaleFactor, mQuality);
+ filter.setEnvironment(this);
+ Bitmap ret = filter.apply(bitmap, mScaleFactor, mQuality);
+ filter.setEnvironment(null);
+ return ret;
+ }
+
+ public CachingPipeline getCachingPipeline() {
+ return mCachingPipeline;
+ }
+
+ public void setCachingPipeline(CachingPipeline cachingPipeline) {
+ mCachingPipeline = cachingPipeline;
}
}