OSDN Git Service

Implements N-1 Caching
authornicolasroard <nicolasroard@google.com>
Wed, 31 Jul 2013 03:01:21 +0000 (20:01 -0700)
committernicolasroard <nicolasroard@google.com>
Wed, 31 Jul 2013 22:16:59 +0000 (15:16 -0700)
Change-Id: Ief1a04dbe4f6ced15f937177a556733cfcdb5879

src/com/android/gallery3d/filtershow/filters/FilterVignetteRepresentation.java
src/com/android/gallery3d/filtershow/pipeline/CacheProcessing.java [new file with mode: 0644]
src/com/android/gallery3d/filtershow/pipeline/CachingPipeline.java
src/com/android/gallery3d/filtershow/pipeline/ImagePreset.java

index b666e63..42a7406 100644 (file)
@@ -114,6 +114,23 @@ public class FilterVignetteRepresentation extends FilterBasicRepresentation impl
         return getValue() == 0;
     }
 
+    @Override
+    public boolean equals(FilterRepresentation representation) {
+        if (!super.equals(representation)) {
+            return false;
+        }
+        if (representation instanceof FilterVignetteRepresentation) {
+            FilterVignetteRepresentation rep = (FilterVignetteRepresentation) representation;
+            if (rep.getCenterX() == getCenterX()
+                    && rep.getCenterY() == getCenterY()
+                    && rep.getRadiusX() == getRadiusX()
+                    && rep.getRadiusY() == getRadiusY()) {
+                return true;
+            }
+        }
+        return false;
+    }
+
     private static final String[] sParams = {
             "Name", "value", "mCenterX", "mCenterY", "mRadiusX",
             "mRadiusY"
diff --git a/src/com/android/gallery3d/filtershow/pipeline/CacheProcessing.java b/src/com/android/gallery3d/filtershow/pipeline/CacheProcessing.java
new file mode 100644 (file)
index 0000000..e358eba
--- /dev/null
@@ -0,0 +1,172 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.gallery3d.filtershow.pipeline;
+
+import android.graphics.Bitmap;
+import android.util.Log;
+import com.android.gallery3d.filtershow.filters.FilterRepresentation;
+
+import java.util.Vector;
+
+public class CacheProcessing {
+    private static final String LOGTAG = "CacheProcessing";
+    private static final boolean DEBUG = false;
+    private Vector<CacheStep> mSteps = new Vector<CacheStep>();
+
+    static class CacheStep {
+        FilterRepresentation representation;
+        Bitmap cache;
+    }
+
+    public Bitmap process(Bitmap originalBitmap,
+                          Vector<FilterRepresentation> filters,
+                          FilterEnvironment environment) {
+        Bitmap cacheBitmap = originalBitmap;
+
+        // New set of filters, let's clear the cache and rebuild it.
+        if (filters.size() != mSteps.size()) {
+            mSteps.clear();
+            for (int i = 0; i < filters.size(); i++) {
+                FilterRepresentation representation = filters.elementAt(i);
+                CacheStep step = new CacheStep();
+                step.representation = representation.copy();
+                mSteps.add(step);
+            }
+        }
+
+        if (DEBUG) {
+            displayFilters(filters);
+        }
+
+        // First, let's find how similar we are in our cache
+        // compared to the current list of filters
+        int similarUpToIndex = 0;
+        for (int i = 0; i < filters.size(); i++) {
+            FilterRepresentation representation = filters.elementAt(i);
+            CacheStep step = mSteps.elementAt(i);
+            boolean similar = step.representation.equals(representation);
+            if (similar) {
+                similarUpToIndex = i;
+            } else {
+                break;
+            }
+        }
+        if (DEBUG) {
+            Log.v(LOGTAG, "similar up to index " + similarUpToIndex);
+        }
+
+        // Now, let's get the earliest cached result in our pipeline
+        int findBaseImageIndex = similarUpToIndex;
+        while (findBaseImageIndex > 0
+                && mSteps.elementAt(findBaseImageIndex).cache == null) {
+            findBaseImageIndex--;
+        }
+        cacheBitmap = mSteps.elementAt(findBaseImageIndex).cache;
+        boolean emptyStack = false;
+        if (cacheBitmap == null) {
+            emptyStack = true;
+            // Damn, it's an empty stack, we have to start from scratch
+            // TODO: use a bitmap cache + RS allocation instead of Bitmap.copy()
+            cacheBitmap = originalBitmap.copy(Bitmap.Config.ARGB_8888, true);
+            if (DEBUG) {
+                Log.v(LOGTAG, "empty stack");
+            }
+        }
+
+        // Ok, so sadly the earliest cached result is before the index we want.
+        // We have to rebuild a new result for this position, and then cache it.
+        if (findBaseImageIndex != similarUpToIndex) {
+            if (DEBUG) {
+                Log.v(LOGTAG, "rebuild cacheBitmap from " + findBaseImageIndex
+                        + " to " + similarUpToIndex);
+            }
+            // rebuild the cache image for this step
+            if (!emptyStack) {
+                cacheBitmap = cacheBitmap.copy(Bitmap.Config.ARGB_8888, true);
+            }
+            for (int i = findBaseImageIndex; i <= similarUpToIndex; i++) {
+                FilterRepresentation representation = filters.elementAt(i);
+                cacheBitmap = environment.applyRepresentation(representation, cacheBitmap);
+                if (DEBUG) {
+                    Log.v(LOGTAG, " - " + i  + " => apply " + representation.getName());
+                }
+            }
+            // Let's cache it!
+            mSteps.elementAt(similarUpToIndex).cache = cacheBitmap;
+        }
+
+        if (DEBUG) {
+            Log.v(LOGTAG, "process pipeline from " + similarUpToIndex
+                    + " to " + (filters.size() - 1));
+        }
+
+        // Now we are good to go, let's use the cacheBitmap as a starting point
+        for (int i = similarUpToIndex + 1; i < filters.size(); i++) {
+            FilterRepresentation representation = filters.elementAt(i);
+            CacheStep currentStep = mSteps.elementAt(i);
+            cacheBitmap = cacheBitmap.copy(Bitmap.Config.ARGB_8888, true);
+            cacheBitmap = environment.applyRepresentation(representation, cacheBitmap);
+            currentStep.representation = representation.copy();
+            currentStep.cache = cacheBitmap;
+            if (DEBUG) {
+                Log.v(LOGTAG, " - " + i  + " => apply " + representation.getName());
+            }
+        }
+
+        if (DEBUG) {
+            Log.v(LOGTAG, "now let's cleanup the cache...");
+            displayNbBitmapsInCache();
+        }
+
+        // Let's see if we can cleanup the cache for unused bitmaps
+        for (int i = 0; i < similarUpToIndex; i++) {
+            CacheStep currentStep = mSteps.elementAt(i);
+            currentStep.cache = null;
+        }
+
+        if (DEBUG) {
+            Log.v(LOGTAG, "cleanup done...");
+            displayNbBitmapsInCache();
+        }
+        return cacheBitmap;
+    }
+
+    private void displayFilters(Vector<FilterRepresentation> filters) {
+        Log.v(LOGTAG, "------>>>");
+        for (int i = 0; i < filters.size(); i++) {
+            FilterRepresentation representation = filters.elementAt(i);
+            CacheStep step = mSteps.elementAt(i);
+            boolean similar = step.representation.equals(representation);
+            Log.v(LOGTAG, "[" + i + "] - " + representation.getName()
+                    + " similar rep ? " + (similar ? "YES" : "NO")
+                    + " -- bitmap: " + step.cache);
+        }
+        Log.v(LOGTAG, "<<<------");
+    }
+
+    private void displayNbBitmapsInCache() {
+        int nbBitmapsCached = 0;
+        for (int i = 0; i < mSteps.size(); i++) {
+            CacheStep step = mSteps.elementAt(i);
+            if (step.cache != null) {
+                nbBitmapsCached++;
+            }
+        }
+        Log.v(LOGTAG, "nb bitmaps in cache: " + nbBitmapsCached + " / " + mSteps.size());
+    }
+
+}
index 535d02f..beba993 100644 (file)
@@ -24,11 +24,14 @@ import android.support.v8.renderscript.RenderScript;
 import android.util.Log;
 
 import com.android.gallery3d.filtershow.cache.ImageLoader;
+import com.android.gallery3d.filtershow.filters.FilterRepresentation;
 import com.android.gallery3d.filtershow.filters.FiltersManager;
 import com.android.gallery3d.filtershow.filters.ImageFilterGeometry;
 import com.android.gallery3d.filtershow.imageshow.GeometryMetadata;
 import com.android.gallery3d.filtershow.imageshow.MasterImage;
 
+import java.util.Vector;
+
 public class CachingPipeline implements PipelineInterface {
     private static final String LOGTAG = "CachingPipeline";
     private boolean DEBUG = false;
@@ -43,6 +46,8 @@ public class CachingPipeline implements PipelineInterface {
     private volatile Bitmap mResizedOriginalBitmap = null;
 
     private FilterEnvironment mEnvironment = new FilterEnvironment();
+    private CacheProcessing mCachedProcessing = new CacheProcessing();
+
 
     private volatile Allocation mOriginalAllocation = null;
     private volatile Allocation mFiltersOnlyOriginalAllocation =  null;
@@ -338,7 +343,17 @@ public class CachingPipeline implements PipelineInterface {
                 FilterEnvironment.QUALITY_PREVIEW);
     }
 
-    public synchronized void compute(SharedBuffer buffer, ImagePreset preset, int type) {
+    public void compute(SharedBuffer buffer, ImagePreset preset, int type) {
+        if (getRenderScriptContext() == null) {
+            return;
+        }
+        setupEnvironment(preset, false);
+        Vector<FilterRepresentation> filters = preset.getFilters();
+        Bitmap result = mCachedProcessing.process(mOriginalBitmap, filters, mEnvironment);
+        buffer.setProducer(result);
+    }
+
+    public synchronized void computeOld(SharedBuffer buffer, ImagePreset preset, int type) {
         synchronized (CachingPipeline.class) {
             if (getRenderScriptContext() == null) {
                 return;
index 28ae269..9bb3e00 100644 (file)
@@ -79,6 +79,10 @@ public class ImagePreset {
         }
     }
 
+    public Vector<FilterRepresentation> getFilters() {
+        return mFilters;
+    }
+
     public FilterRepresentation getFilterRepresentation(int position) {
         FilterRepresentation representation = null;