OSDN Git Service

Fixed more threading bugs in the ImageFilterRS and the FiltersManager.
authorRuben Brunk <rubenbrunk@google.com>
Fri, 15 Mar 2013 01:04:15 +0000 (18:04 -0700)
committerRuben Brunk <rubenbrunk@google.com>
Fri, 15 Mar 2013 01:25:11 +0000 (18:25 -0700)
Bug: 8392832

Change-Id: I87b3af9ddb62524f02858a82eac2cc3bd2209418

src/com/android/gallery3d/filtershow/filters/ImageFilterRS.java
src/com/android/gallery3d/filtershow/filters/ImageFilterSharpen.java
src_pd/com/android/gallery3d/filtershow/filters/FiltersManager.java

index 2ebd61f..729aef8 100644 (file)
@@ -35,12 +35,12 @@ public abstract class ImageFilterRS extends ImageFilter {
     private static volatile int sHeight = 0;
 
     private static volatile Resources sResources = null;
-    private boolean mResourcesLoaded = false;
+    private volatile boolean mResourcesLoaded = false;
 
     private static final Bitmap.Config BITMAP_CONFIG = Bitmap.Config.ARGB_8888;
 
     // This must be used inside block synchronized on ImageFilterRS class object
-    public void prepare(Bitmap bitmap, float scaleFactor, int quality) {
+    protected void prepare(Bitmap bitmap, float scaleFactor, int quality) {
         if (mOutPixelsAllocation == null || mInPixelsAllocation == null ||
                 bitmap.getWidth() != sWidth || bitmap.getHeight() != sHeight) {
             destroyPixelAllocations();
@@ -65,14 +65,14 @@ public abstract class ImageFilterRS extends ImageFilter {
     }
 
     // This must be used inside block synchronized on ImageFilterRS class object
-    abstract public void createFilter(android.content.res.Resources res,
+    protected abstract void createFilter(android.content.res.Resources res,
             float scaleFactor, int quality);
 
     // This must be used inside block synchronized on ImageFilterRS class object
-    abstract public void runFilter();
+    protected abstract void runFilter();
 
     // This must be used inside block synchronized on ImageFilterRS class object
-    public void update(Bitmap bitmap) {
+    protected void update(Bitmap bitmap) {
         mOutPixelsAllocation.copyTo(bitmap);
     }
 
@@ -84,7 +84,7 @@ public abstract class ImageFilterRS extends ImageFilter {
         try {
             synchronized(ImageFilterRS.class) {
                 if (sRS == null)  {
-                    Log.w(LOGTAG, "Cannot apply before calling setRenderScriptContext");
+                    Log.w(LOGTAG, "Cannot apply before calling createRenderScriptContext");
                     return bitmap;
                 }
                 prepare(bitmap, scaleFactor, quality);
@@ -199,13 +199,18 @@ public abstract class ImageFilterRS extends ImageFilter {
         mResourcesLoaded = resourcesLoaded;
     }
 
+    // TODO:
+    // Ideally, every filter would destroy _every_ renderscript allocation,
+    // script, and anything else that depends on a certain RS context here.
     abstract protected void resetAllocations();
 
     public void freeResources() {
         if (!isResourcesLoaded()) {
             return;
         }
-        resetAllocations();
-        setResourcesLoaded(false);
+        synchronized(ImageFilterRS.class) {
+            resetAllocations();
+            setResourcesLoaded(false);
+        }
     }
 }
index 9f4c7e5..5f0a5f8 100644 (file)
@@ -52,7 +52,7 @@ public class ImageFilterSharpen extends ImageFilterRS {
     }
 
     @Override
-    public void createFilter(android.content.res.Resources res, float scaleFactor,
+    protected void createFilter(android.content.res.Resources res, float scaleFactor,
             int quality) {
         int w = mInPixelsAllocation.getType().getX();
         int h = mInPixelsAllocation.getType().getY();
@@ -83,7 +83,7 @@ public class ImageFilterSharpen extends ImageFilterRS {
     }
 
     @Override
-    public void runFilter() {
+    protected void runFilter() {
         if (mParameters == null) {
             return;
         }
index 246e3f5..4b6b1d3 100644 (file)
@@ -43,6 +43,7 @@ public class FiltersManager extends BaseFiltersManager {
 
     public static void reset() {
         sInstance = null;
+        sPreviewInstance = null;
     }
 
 }