OSDN Git Service

Make Gallery2 use platform RenderScript
[android-x86/packages-apps-Gallery2.git] / src / com / android / gallery3d / filtershow / pipeline / ImagePreset.java
index 1876f25..9166ee3 100644 (file)
@@ -18,7 +18,7 @@ package com.android.gallery3d.filtershow.pipeline;
 
 import android.graphics.Bitmap;
 import android.graphics.Rect;
-import android.support.v8.renderscript.Allocation;
+import android.renderscript.Allocation;
 import android.util.JsonReader;
 import android.util.JsonWriter;
 import android.util.Log;
@@ -41,7 +41,6 @@ import com.android.gallery3d.filtershow.imageshow.GeometryMathUtils;
 import com.android.gallery3d.filtershow.imageshow.MasterImage;
 import com.android.gallery3d.filtershow.state.State;
 import com.android.gallery3d.filtershow.state.StateAdapter;
-import com.android.gallery3d.util.UsageStatistics;
 
 import java.io.IOException;
 import java.io.StringReader;
@@ -53,6 +52,7 @@ import java.util.Vector;
 public class ImagePreset {
 
     private static final String LOGTAG = "ImagePreset";
+    public static final String JASON_SAVED = "Saved";
 
     private Vector<FilterRepresentation> mFilters = new Vector<FilterRepresentation>();
 
@@ -268,8 +268,20 @@ public class ImagePreset {
         for (int i = 0; i < preset.mFilters.size(); i++) {
             FilterRepresentation a = preset.mFilters.elementAt(i);
             FilterRepresentation b = mFilters.elementAt(i);
-
-            if (!a.equals(b)) {
+            boolean isGeometry = false;
+            if (a instanceof FilterRotateRepresentation
+                    || a instanceof FilterMirrorRepresentation
+                    || a instanceof FilterCropRepresentation
+                    || a instanceof FilterStraightenRepresentation) {
+                isGeometry = true;
+            }
+            boolean evaluate = true;
+            if (!isGeometry && mDoApplyGeometry && !mDoApplyFilters) {
+                evaluate = false;
+            } else if (isGeometry && !mDoApplyGeometry && mDoApplyFilters) {
+                evaluate = false;
+            }
+            if (evaluate && !a.equals(b)) {
                 return false;
             }
         }
@@ -335,12 +347,18 @@ public class ImagePreset {
     public void addFilter(FilterRepresentation representation) {
         if (representation instanceof FilterUserPresetRepresentation) {
             ImagePreset preset = ((FilterUserPresetRepresentation) representation).getImagePreset();
-            // user preset replace everything but geometry
-            mFilters.clear();
-            for (int i = 0; i < preset.nbFilters(); i++) {
-                addFilter(preset.getFilterRepresentation(i));
+            if (preset.nbFilters() == 1
+                && preset.contains(FilterRepresentation.TYPE_FX)) {
+                FilterRepresentation rep = preset.getFilterRepresentationForType(
+                        FilterRepresentation.TYPE_FX);
+                addFilter(rep);
+            } else {
+                // user preset replaces everything
+                mFilters.clear();
+                for (int i = 0; i < preset.nbFilters(); i++) {
+                    addFilter(preset.getFilterRepresentation(i));
+                }
             }
-            mFilters.add(representation);
         } else if (representation.getFilterType() == FilterRepresentation.TYPE_GEOMETRY) {
             // Add geometry filter, removing duplicates and do-nothing operations.
             for (int i = 0; i < mFilters.size(); i++) {
@@ -364,56 +382,38 @@ public class ImagePreset {
                 mFilters.add(representation);
             }
         } else if (representation.getFilterType() == FilterRepresentation.TYPE_FX) {
-            boolean found = false;
+            boolean replaced = false;
             for (int i = 0; i < mFilters.size(); i++) {
                 FilterRepresentation current = mFilters.elementAt(i);
-                int type = current.getFilterType();
-                if (found) {
-                    if (type != FilterRepresentation.TYPE_VIGNETTE) {
-                        mFilters.remove(i);
-                        continue;
-                    }
-                }
-                if (type == FilterRepresentation.TYPE_FX) {
-                    if (current instanceof FilterUserPresetRepresentation) {
-                        ImagePreset preset = ((FilterUserPresetRepresentation) current)
-                                .getImagePreset();
-                        // If we had an existing user preset, let's remove all the presets that
-                        // were added by it
-                        for (int j = 0; j < preset.nbFilters(); j++) {
-                            FilterRepresentation rep = preset.getFilterRepresentation(j);
-                            int pos = getPositionForRepresentation(rep);
-                            if (pos != -1) {
-                                mFilters.remove(pos);
-                            }
-                        }
-                        int pos = getPositionForRepresentation(current);
-                        if (pos != -1) {
-                            mFilters.remove(pos);
-                        } else {
-                            pos = 0;
-                        }
-                        if (!isNoneFxFilter(representation)) {
-                            mFilters.add(pos, representation);
-                        }
-
-                    } else {
-                        mFilters.remove(i);
-                        if (!isNoneFxFilter(representation)) {
-                            mFilters.add(i, representation);
-                        }
+                if (current.getFilterType() == FilterRepresentation.TYPE_FX) {
+                    mFilters.remove(i);
+                    replaced = true;
+                    if (!isNoneFxFilter(representation)) {
+                        mFilters.add(i, representation);
                     }
-                    found = true;
+                    break;
                 }
             }
-            if (!found) {
-                if (!isNoneFxFilter(representation)) {
-                    mFilters.add(representation);
-                }
+            if (!replaced && !isNoneFxFilter(representation)) {
+                mFilters.add(0, representation);
             }
         } else {
             mFilters.add(representation);
         }
+        // Enforces Filter type ordering for borders
+        FilterRepresentation border = null;
+        for (int i = 0; i < mFilters.size();) {
+            FilterRepresentation rep = mFilters.elementAt(i);
+            if (rep.getFilterType() == FilterRepresentation.TYPE_BORDER) {
+                border = rep;
+                mFilters.remove(i);
+                continue;
+            }
+            i++;
+        }
+        if (border != null) {
+            mFilters.add(border);
+        }
     }
 
     private boolean isNoneBorderFilter(FilterRepresentation representation) {
@@ -488,8 +488,6 @@ public class ImagePreset {
         if (border != null && mDoApplyGeometry) {
             bitmap = environment.applyRepresentation(border, bitmap);
             if (environment.getQuality() == FilterEnvironment.QUALITY_FINAL) {
-                UsageStatistics.onEvent(UsageStatistics.COMPONENT_EDITOR,
-                        "SaveBorder", border.getSerializationName(), 1);
             }
         }
         return bitmap;
@@ -507,10 +505,6 @@ public class ImagePreset {
             if (to == -1) {
                 to = mFilters.size();
             }
-            if (environment.getQuality() == FilterEnvironment.QUALITY_FINAL) {
-                UsageStatistics.onEvent(UsageStatistics.COMPONENT_EDITOR,
-                        "SaveFilters", "Total", to - from + 1);
-            }
             for (int i = from; i < to; i++) {
                 FilterRepresentation representation = mFilters.elementAt(i);
                 if (representation.getFilterType() == FilterRepresentation.TYPE_GEOMETRY) {
@@ -528,10 +522,6 @@ public class ImagePreset {
                 if (tmp != bitmap) {
                     environment.cache(tmp);
                 }
-                if (environment.getQuality() == FilterEnvironment.QUALITY_FINAL) {
-                    UsageStatistics.onEvent(UsageStatistics.COMPONENT_EDITOR,
-                            "SaveFilter", representation.getSerializationName(), 1);
-                }
                 if (environment.needsStop()) {
                     return bitmap;
                 }
@@ -691,6 +681,7 @@ public class ImagePreset {
             }
             reader.close();
         } catch (Exception e) {
+            Log.e(LOGTAG, "\""+filterString+"\"");
             Log.e(LOGTAG, "parsing the filter parameters:", e);
             return false;
         }