summaryrefslogtreecommitdiffstats
path: root/src/com/android/gallery3d/filtershow/filters
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/com/android/gallery3d/filtershow/filters
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/com/android/gallery3d/filtershow/filters')
-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
3 files changed, 36 insertions, 70 deletions
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());
}
}