Fix edge case cache issues

Also add additional debugging information.

bug:10856175

Change-Id: Ie352e2dd6a86511abea97b622ed7c3b3cccd8479
diff --git a/src/com/android/gallery3d/filtershow/pipeline/CacheProcessing.java b/src/com/android/gallery3d/filtershow/pipeline/CacheProcessing.java
index 4dad736..c2912ed 100644
--- a/src/com/android/gallery3d/filtershow/pipeline/CacheProcessing.java
+++ b/src/com/android/gallery3d/filtershow/pipeline/CacheProcessing.java
@@ -29,6 +29,7 @@
 public class CacheProcessing {
     private static final String LOGTAG = "CacheProcessing";
     private static final boolean DEBUG = false;
+    private static final boolean NO_CACHING = false;
     private Vector<CacheStep> mSteps = new Vector<CacheStep>();
 
     static class CacheStep {
@@ -104,15 +105,25 @@
                 for (FilterRepresentation representation : representations) {
                     geometry.add(representation);
                 }
+                if (DEBUG) {
+                    Log.v(LOGTAG, "Apply geometry to bitmap " + cacheBitmap);
+                }
                 cacheBitmap = GeometryMathUtils.applyGeometryRepresentations(geometry, cacheBitmap);
             } else {
                 for (FilterRepresentation representation : representations) {
+                    if (DEBUG) {
+                        Log.v(LOGTAG, "Apply " + representation.getSerializationName()
+                                + " to bitmap " + cacheBitmap);
+                    }
                     cacheBitmap = environment.applyRepresentation(representation, cacheBitmap);
                 }
             }
             if (cacheBitmap != source) {
                 environment.cache(source);
             }
+            if (DEBUG) {
+                Log.v(LOGTAG, "Apply returns bitmap " + cacheBitmap);
+            }
             return cacheBitmap;
         }
     }
@@ -178,6 +189,20 @@
                     + mSteps.size() + " cacheBitmap: " + cacheBitmap);
         }
 
+        if (NO_CACHING) {
+            cacheBitmap = environment.getBitmapCopy(originalBitmap,
+                    BitmapCache.PREVIEW_CACHE_NO_ROOT);
+            for (int i = 0; i < mSteps.size(); i++) {
+                CacheStep step = mSteps.elementAt(i);
+                Bitmap prev = cacheBitmap;
+                cacheBitmap = step.apply(environment, cacheBitmap);
+                if (prev != cacheBitmap) {
+                    environment.cache(prev);
+                }
+            }
+            return cacheBitmap;
+        }
+
         Bitmap originalCopy = null;
         int lastPositionCached = -1;
         for (int i = findBaseImageIndex; i < mSteps.size(); i++) {
@@ -227,9 +252,8 @@
             displayNbBitmapsInCache();
         }
         if (lastPositionCached != -1) {
-            Bitmap bitmap = mSteps.elementAt(lastPositionCached).cache;
+            // The last element will never be reused, remove it from the cache.
             mSteps.elementAt(lastPositionCached).cache = null;
-            environment.cache(bitmap);
         }
         if (contains(cacheBitmap)) {
             return environment.getBitmapCopy(cacheBitmap, BitmapCache.PREVIEW_CACHE_NO_APPLY);