summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authornicolasroard <nicolasroard@google.com>2013-07-30 20:01:21 -0700
committernicolasroard <nicolasroard@google.com>2013-07-31 15:16:59 -0700
commit7f2b1e548ad3b5cdf8775b0a12f8f9b389477f21 (patch)
tree91e880d9905ff2a0307ab58d539d57add0158b55 /src
parent6d29a648f497f827d623d7af0b679e98ea58bf90 (diff)
downloadandroid_packages_apps_Snap-7f2b1e548ad3b5cdf8775b0a12f8f9b389477f21.tar.gz
android_packages_apps_Snap-7f2b1e548ad3b5cdf8775b0a12f8f9b389477f21.tar.bz2
android_packages_apps_Snap-7f2b1e548ad3b5cdf8775b0a12f8f9b389477f21.zip
Implements N-1 Caching
Change-Id: Ief1a04dbe4f6ced15f937177a556733cfcdb5879
Diffstat (limited to 'src')
-rw-r--r--src/com/android/gallery3d/filtershow/filters/FilterVignetteRepresentation.java17
-rw-r--r--src/com/android/gallery3d/filtershow/pipeline/CacheProcessing.java172
-rw-r--r--src/com/android/gallery3d/filtershow/pipeline/CachingPipeline.java17
-rw-r--r--src/com/android/gallery3d/filtershow/pipeline/ImagePreset.java4
4 files changed, 209 insertions, 1 deletions
diff --git a/src/com/android/gallery3d/filtershow/filters/FilterVignetteRepresentation.java b/src/com/android/gallery3d/filtershow/filters/FilterVignetteRepresentation.java
index b666e6325..42a7406bc 100644
--- a/src/com/android/gallery3d/filtershow/filters/FilterVignetteRepresentation.java
+++ b/src/com/android/gallery3d/filtershow/filters/FilterVignetteRepresentation.java
@@ -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
index 000000000..e358ebaa3
--- /dev/null
+++ b/src/com/android/gallery3d/filtershow/pipeline/CacheProcessing.java
@@ -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());
+ }
+
+}
diff --git a/src/com/android/gallery3d/filtershow/pipeline/CachingPipeline.java b/src/com/android/gallery3d/filtershow/pipeline/CachingPipeline.java
index 535d02f0b..beba993e5 100644
--- a/src/com/android/gallery3d/filtershow/pipeline/CachingPipeline.java
+++ b/src/com/android/gallery3d/filtershow/pipeline/CachingPipeline.java
@@ -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;
diff --git a/src/com/android/gallery3d/filtershow/pipeline/ImagePreset.java b/src/com/android/gallery3d/filtershow/pipeline/ImagePreset.java
index 28ae2694e..9bb3e00f2 100644
--- a/src/com/android/gallery3d/filtershow/pipeline/ImagePreset.java
+++ b/src/com/android/gallery3d/filtershow/pipeline/ImagePreset.java
@@ -79,6 +79,10 @@ public class ImagePreset {
}
}
+ public Vector<FilterRepresentation> getFilters() {
+ return mFilters;
+ }
+
public FilterRepresentation getFilterRepresentation(int position) {
FilterRepresentation representation = null;