Pipeline refactoring

Remove the temp representations and instead use a triple
buffer for ImagePresets.

Change-Id: I4cdcfbe4941af72b38fe42385085ff4a20eb78cc
diff --git a/src/com/android/gallery3d/filtershow/filters/FilterRepresentation.java b/src/com/android/gallery3d/filtershow/filters/FilterRepresentation.java
index a60410d..e6018bb 100644
--- a/src/com/android/gallery3d/filtershow/filters/FilterRepresentation.java
+++ b/src/com/android/gallery3d/filtershow/filters/FilterRepresentation.java
@@ -47,8 +47,6 @@
     public static final byte TYPE_TINYPLANET = 6;
     protected static final String NAME_TAG = "Name";
 
-    private FilterRepresentation mTempRepresentation = null;
-
     public FilterRepresentation(String name) {
         mName = name;
     }
@@ -67,8 +65,6 @@
         representation.setShowParameterValue(showParameterValue());
         representation.mSerializationName = mSerializationName;
 
-        representation.mTempRepresentation =
-                mTempRepresentation != null ? mTempRepresentation.clone() : null;
         if (DEBUG) {
             Log.v(LOGTAG, "cloning from <" + this + "> to <" + representation + ">");
         }
@@ -140,28 +136,6 @@
     public void useParametersFrom(FilterRepresentation a) {
     }
 
-    public void clearTempRepresentation() {
-        mTempRepresentation = null;
-    }
-
-    public synchronized void updateTempParametersFrom(FilterRepresentation representation) {
-        if (mTempRepresentation == null) {
-            try {
-                mTempRepresentation = representation.clone();
-            } catch (CloneNotSupportedException e) {
-                e.printStackTrace();
-            }
-        } else {
-            mTempRepresentation.useParametersFrom(representation);
-        }
-    }
-
-    public synchronized void synchronizeRepresentation() {
-        if (mTempRepresentation != null) {
-            useParametersFrom(mTempRepresentation);
-        }
-    }
-
     public boolean allowsSingleInstanceOnly() {
         return false;
     }
diff --git a/src/com/android/gallery3d/filtershow/pipeline/SharedPreset.java b/src/com/android/gallery3d/filtershow/pipeline/SharedPreset.java
new file mode 100644
index 0000000..e874694
--- /dev/null
+++ b/src/com/android/gallery3d/filtershow/pipeline/SharedPreset.java
@@ -0,0 +1,44 @@
+/*
+ * 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 com.android.gallery3d.filtershow.presets.ImagePreset;
+
+public class SharedPreset {
+
+    private volatile ImagePreset mProducerPreset = null;
+    private volatile ImagePreset mConsumerPreset = null;
+    private volatile ImagePreset mIntermediatePreset = null;
+
+    public synchronized void enqueuePreset(ImagePreset preset) {
+        if (mProducerPreset == null || (!mProducerPreset.same(preset))) {
+            mProducerPreset = new ImagePreset(preset);
+        } else {
+            mProducerPreset.updateWith(preset);
+        }
+        ImagePreset temp = mIntermediatePreset;
+        mIntermediatePreset = mProducerPreset;
+        mProducerPreset = temp;
+    }
+
+    public synchronized ImagePreset dequeuePreset() {
+        ImagePreset temp = mConsumerPreset;
+        mConsumerPreset = mIntermediatePreset;
+        mIntermediatePreset = temp;
+        return mConsumerPreset;
+    }
+}
diff --git a/src/com/android/gallery3d/filtershow/presets/ImagePreset.java b/src/com/android/gallery3d/filtershow/presets/ImagePreset.java
index 4ec39f7..d36bafd 100644
--- a/src/com/android/gallery3d/filtershow/presets/ImagePreset.java
+++ b/src/com/android/gallery3d/filtershow/presets/ImagePreset.java
@@ -147,17 +147,15 @@
         if (representation == null) {
             return;
         }
-        synchronized (mFilters) {
-            if (representation instanceof GeometryMetadata) {
-                setGeometry((GeometryMetadata) representation);
-            } else {
-                int position = getPositionForRepresentation(representation);
-                if (position == -1) {
-                    return;
-                }
-                FilterRepresentation old = mFilters.elementAt(position);
-                old.updateTempParametersFrom(representation);
+        if (representation instanceof GeometryMetadata) {
+            setGeometry((GeometryMetadata) representation);
+        } else {
+            int position = getPositionForRepresentation(representation);
+            if (position == -1) {
+                return;
             }
+            FilterRepresentation old = mFilters.elementAt(position);
+            old.useParametersFrom(representation);
         }
         MasterImage.getImage().invalidatePreview();
         fillImageStateAdapter(MasterImage.getImage().getState());
@@ -175,7 +173,7 @@
         return mDoApplyFilters;
     }
 
-    public synchronized GeometryMetadata getGeometry() {
+    public GeometryMetadata getGeometry() {
         for (FilterRepresentation representation : mFilters) {
             if (representation instanceof GeometryMetadata) {
                 return (GeometryMetadata) representation;
@@ -223,7 +221,7 @@
         return true;
     }
 
-    public synchronized void setGeometry(GeometryMetadata representation) {
+    public void setGeometry(GeometryMetadata representation) {
         GeometryMetadata geoData = getGeometry();
         if (geoData != representation) {
             geoData.set(representation);
@@ -425,7 +423,6 @@
         // Returns a new bitmap.
         if (mDoApplyGeometry) {
             GeometryMetadata geoData = getGeometry();
-            geoData.synchronizeRepresentation();
             bitmap = environment.applyRepresentation(geoData, bitmap);
         }
         return bitmap;
@@ -436,7 +433,6 @@
         FilterRepresentation border = getFilterRepresentationForType(
                 FilterRepresentation.TYPE_BORDER);
         if (border != null && mDoApplyGeometry) {
-            border.synchronizeRepresentation();
             bitmap = environment.applyRepresentation(border, bitmap);
             if (environment.getQuality() == FilterEnvironment.QUALITY_FINAL) {
                 UsageStatistics.onEvent(UsageStatistics.COMPONENT_EDITOR,
@@ -463,11 +459,7 @@
                         "SaveFilters", "Total", to - from + 1);
             }
             for (int i = from; i < to; i++) {
-                FilterRepresentation representation = null;
-                synchronized (mFilters) {
-                    representation = mFilters.elementAt(i);
-                    representation.synchronizeRepresentation();
-                }
+                FilterRepresentation representation = mFilters.elementAt(i);
                 if (representation instanceof GeometryMetadata) {
                     // skip the geometry as it's already applied.
                     continue;
@@ -496,7 +488,6 @@
         FilterRepresentation border = getFilterRepresentationForType(
                 FilterRepresentation.TYPE_BORDER);
         if (border != null && mDoApplyGeometry) {
-            border.synchronizeRepresentation();
             // TODO: should keep the bitmap around
             Allocation bitmapIn = in;
             if (copyOut) {
@@ -518,11 +509,7 @@
                 to = mFilters.size();
             }
             for (int i = from; i < to; i++) {
-                FilterRepresentation representation = null;
-                synchronized (mFilters) {
-                    representation = mFilters.elementAt(i);
-                    representation.synchronizeRepresentation();
-                }
+                FilterRepresentation representation = mFilters.elementAt(i);
                 if (representation instanceof GeometryMetadata) {
                     // skip the geometry as it's already applied.
                     continue;
@@ -544,10 +531,7 @@
             return false;
         }
         for (int i = 0; i < mFilters.size(); i++) {
-            FilterRepresentation representation = null;
-            synchronized (mFilters) {
-                representation = mFilters.elementAt(i);
-            }
+            FilterRepresentation representation = mFilters.elementAt(i);
             if (representation instanceof GeometryMetadata
                 && ((GeometryMetadata) representation).hasModifications()) {
                 return false;
@@ -703,4 +687,16 @@
         FiltersManager filtersManager = FiltersManager.getManager();
         return filtersManager.createFilterFromName(name);
     }
+
+    public void updateWith(ImagePreset preset) {
+        if (preset.mFilters.size() != mFilters.size()) {
+            Log.e(LOGTAG, "Updating a preset with an incompatible one");
+            return;
+        }
+        for (int i = 0; i < mFilters.size(); i++) {
+            FilterRepresentation destRepresentation = mFilters.elementAt(i);
+            FilterRepresentation sourceRepresentation = preset.mFilters.elementAt(i);
+            destRepresentation.useParametersFrom(sourceRepresentation);
+        }
+    }
 }