OSDN Git Service

Revert "Revert "Make Gallery2 use platform RenderScript""
[android-x86/packages-apps-Gallery2.git] / src / com / android / gallery3d / filtershow / pipeline / FilterEnvironment.java
index f97dc75..0b84f52 100644 (file)
 package com.android.gallery3d.filtershow.pipeline;
 
 import android.graphics.Bitmap;
-import android.support.v8.renderscript.Allocation;
+import android.graphics.Canvas;
+import android.renderscript.Allocation;
 
+import com.android.gallery3d.app.Log;
+import com.android.gallery3d.filtershow.cache.BitmapCache;
 import com.android.gallery3d.filtershow.filters.FilterRepresentation;
+import com.android.gallery3d.filtershow.filters.FilterUserPresetRepresentation;
 import com.android.gallery3d.filtershow.filters.FiltersManagerInterface;
 import com.android.gallery3d.filtershow.filters.ImageFilter;
 
@@ -34,6 +38,7 @@ public class FilterEnvironment {
     private FiltersManagerInterface mFiltersManager;
     private PipelineInterface mPipeline;
     private volatile boolean mStop = false;
+    private BitmapCache mBitmapCache;
 
     public static final int QUALITY_ICON = 0;
     public static final int QUALITY_PREVIEW = 1;
@@ -47,40 +52,27 @@ public class FilterEnvironment {
         this.mStop = stop;
     }
 
-    private HashMap<Long, WeakReference<Bitmap>>
-            bitmapCach = new HashMap<Long, WeakReference<Bitmap>>();
-
     private HashMap<Integer, Integer>
                     generalParameters = new HashMap<Integer, Integer>();
 
+    public void setBitmapCache(BitmapCache cache) {
+        mBitmapCache = cache;
+    }
+
     public void cache(Buffer buffer) {
-        if (buffer == null) {
-            return;
-        }
-        Bitmap bitmap = buffer.getBitmap();
-        if (bitmap == null) {
-            return;
-        }
-        Long key = calcKey(bitmap.getWidth(), bitmap.getHeight());
-        bitmapCach.put(key, new WeakReference<Bitmap>(bitmap));
+        mBitmapCache.cache(buffer);
     }
 
-    public Bitmap getBitmap(int w, int h) {
-        Long key = calcKey(w, h);
-        WeakReference<Bitmap> ref = bitmapCach.remove(key);
-        Bitmap bitmap = null;
-        if (ref != null) {
-            bitmap = ref.get();
-        }
-        if (bitmap == null) {
-            bitmap = Bitmap.createBitmap(
-                    w, h, Bitmap.Config.ARGB_8888);
-        }
-        return bitmap;
+    public void cache(Bitmap bitmap) {
+        mBitmapCache.cache(bitmap);
     }
 
-    private Long calcKey(long w, long h) {
-        return (w << 32) | (h << 32);
+    public Bitmap getBitmap(int w, int h, int type) {
+        return mBitmapCache.getBitmap(w, h, type);
+    }
+
+    public Bitmap getBitmapCopy(Bitmap source, int type) {
+        return mBitmapCache.getBitmapCopy(source, type);
     }
 
     public void setImagePreset(ImagePreset imagePreset) {
@@ -128,10 +120,22 @@ public class FilterEnvironment {
     }
 
     public Bitmap applyRepresentation(FilterRepresentation representation, Bitmap bitmap) {
+        if (representation instanceof FilterUserPresetRepresentation) {
+            // we allow instances of FilterUserPresetRepresentation in a preset only to know if one
+            // has been applied (so we can show this in the UI). But as all the filters in them are
+            // applied directly they do not themselves need to do any kind of filtering.
+            return bitmap;
+        }
         ImageFilter filter = mFiltersManager.getFilterForRepresentation(representation);
+        if (filter == null){
+            Log.e(LOGTAG,"No ImageFilter for "+representation.getSerializationName());
+        }
         filter.useRepresentation(representation);
         filter.setEnvironment(this);
         Bitmap ret = filter.apply(bitmap, mScaleFactor, mQuality);
+        if (bitmap != ret) {
+            cache(bitmap);
+        }
         filter.setGeneralParameters();
         filter.setEnvironment(null);
         return ret;
@@ -164,4 +168,7 @@ public class FilterEnvironment {
         generalParameters.put(id, value);
     }
 
+    public BitmapCache getBimapCache() {
+        return mBitmapCache;
+    }
 }