OSDN Git Service

Make Gallery2 use platform RenderScript
[android-x86/packages-apps-Gallery2.git] / src / com / android / gallery3d / filtershow / filters / ImageFilterRS.java
index cfbb560..3c54456 100644 (file)
@@ -18,17 +18,18 @@ package com.android.gallery3d.filtershow.filters;
 
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
-import android.support.v8.renderscript.*;
+import android.renderscript.*;
 import android.util.Log;
 import android.content.res.Resources;
 import com.android.gallery3d.R;
-import com.android.gallery3d.filtershow.cache.CachingPipeline;
+import com.android.gallery3d.filtershow.pipeline.PipelineInterface;
 
 public abstract class ImageFilterRS extends ImageFilter {
     private static final String LOGTAG = "ImageFilterRS";
     private boolean DEBUG = false;
     private int mLastInputWidth = 0;
     private int mLastInputHeight = 0;
+    private long mLastTimeCalled;
 
     public static boolean PERF_LOGGING = false;
 
@@ -51,26 +52,36 @@ public abstract class ImageFilterRS extends ImageFilter {
     }
 
     protected RenderScript getRenderScriptContext() {
-        return CachingPipeline.getRenderScriptContext();
+        PipelineInterface pipeline = getEnvironment().getPipeline();
+        return pipeline.getRSContext();
     }
 
     protected Allocation getInPixelsAllocation() {
-        CachingPipeline pipeline = getEnvironment().getCachingPipeline();
+        PipelineInterface pipeline = getEnvironment().getPipeline();
         return pipeline.getInPixelsAllocation();
     }
 
     protected Allocation getOutPixelsAllocation() {
-        CachingPipeline pipeline = getEnvironment().getCachingPipeline();
+        PipelineInterface pipeline = getEnvironment().getPipeline();
         return pipeline.getOutPixelsAllocation();
     }
 
     @Override
     public void apply(Allocation in, Allocation out) {
         long startOverAll = System.nanoTime();
+        if (PERF_LOGGING) {
+            long delay = (startOverAll - mLastTimeCalled) / 1000;
+            String msg = String.format("%s; image size %dx%d; ", getName(),
+                    in.getType().getX(), in.getType().getY());
+            msg += String.format("called after %.2f ms (%.2f FPS); ",
+                    delay / 1000.f, 1000000.f / delay);
+            Log.i(LOGTAG, msg);
+        }
+        mLastTimeCalled = startOverAll;
         long startFilter = 0;
         long endFilter = 0;
         if (!mResourcesLoaded) {
-            CachingPipeline pipeline = getEnvironment().getCachingPipeline();
+            PipelineInterface pipeline = getEnvironment().getPipeline();
             createFilter(pipeline.getResources(), getEnvironment().getScaleFactor(),
                     getEnvironment().getQuality(), in);
             mResourcesLoaded = true;
@@ -102,7 +113,7 @@ public abstract class ImageFilterRS extends ImageFilter {
             return bitmap;
         }
         try {
-            CachingPipeline pipeline = getEnvironment().getCachingPipeline();
+            PipelineInterface pipeline = getEnvironment().getPipeline();
             if (DEBUG) {
                 Log.v(LOGTAG, "apply filter " + getName() + " in pipeline " + pipeline.getName());
             }
@@ -137,27 +148,24 @@ public abstract class ImageFilterRS extends ImageFilter {
             displayLowMemoryToast();
             Log.e(LOGTAG, "not enough memory for filter " + getName(), e);
         }
-
         return bitmap;
     }
 
-    protected static Allocation convertBitmap(Bitmap bitmap) {
-        return Allocation.createFromBitmap(CachingPipeline.getRenderScriptContext(), bitmap,
+    protected static Allocation convertBitmap(RenderScript RS, Bitmap bitmap) {
+        return Allocation.createFromBitmap(RS, bitmap,
                 Allocation.MipmapControl.MIPMAP_NONE,
                 Allocation.USAGE_SCRIPT | Allocation.USAGE_GRAPHICS_TEXTURE);
     }
 
-    private static Allocation convertRGBAtoA(Bitmap bitmap) {
-        RenderScript RS = CachingPipeline.getRenderScriptContext();
+    private static Allocation convertRGBAtoA(RenderScript RS, Bitmap bitmap) {
         if (RS != mRScache || mGreyConvert == null) {
-            mGreyConvert = new ScriptC_grey(RS, RS.getApplicationContext().getResources(),
-                                            R.raw.grey);
+            mGreyConvert = new ScriptC_grey(RS);
             mRScache = RS;
         }
 
         Type.Builder tb_a8 = new Type.Builder(RS, Element.A_8(RS));
 
-        Allocation bitmapTemp = convertBitmap(bitmap);
+        Allocation bitmapTemp = convertBitmap(RS, bitmap);
         if (bitmapTemp.getType().getElement().isCompatible(Element.A_8(RS))) {
             return bitmapTemp;
         }
@@ -173,28 +181,26 @@ public abstract class ImageFilterRS extends ImageFilter {
     }
 
     public Allocation loadScaledResourceAlpha(int resource, int inSampleSize) {
-        Resources res = CachingPipeline.getResources();
+        Resources res = getEnvironment().getPipeline().getResources();
         final BitmapFactory.Options options = new BitmapFactory.Options();
-        options.inPreferredConfig = Bitmap.Config.ALPHA_8;
         options.inSampleSize      = inSampleSize;
         Bitmap bitmap = BitmapFactory.decodeResource(
                 res,
                 resource, options);
-        Allocation ret = convertRGBAtoA(bitmap);
+        Allocation ret = convertRGBAtoA(getRenderScriptContext(), bitmap);
         bitmap.recycle();
         return ret;
     }
 
     public Allocation loadScaledResourceAlpha(int resource, int w, int h, int inSampleSize) {
-        Resources res = CachingPipeline.getResources();
+        Resources res = getEnvironment().getPipeline().getResources();
         final BitmapFactory.Options options = new BitmapFactory.Options();
-        options.inPreferredConfig = Bitmap.Config.ALPHA_8;
         options.inSampleSize      = inSampleSize;
         Bitmap bitmap = BitmapFactory.decodeResource(
                 res,
                 resource, options);
         Bitmap resizeBitmap = Bitmap.createScaledBitmap(bitmap, w, h, true);
-        Allocation ret = convertRGBAtoA(resizeBitmap);
+        Allocation ret = convertRGBAtoA(getRenderScriptContext(), resizeBitmap);
         resizeBitmap.recycle();
         bitmap.recycle();
         return ret;
@@ -205,13 +211,13 @@ public abstract class ImageFilterRS extends ImageFilter {
     }
 
     public Allocation loadResource(int resource) {
-        Resources res = CachingPipeline.getResources();
+        Resources res = getEnvironment().getPipeline().getResources();
         final BitmapFactory.Options options = new BitmapFactory.Options();
         options.inPreferredConfig = Bitmap.Config.ARGB_8888;
         Bitmap bitmap = BitmapFactory.decodeResource(
                 res,
                 resource, options);
-        Allocation ret = convertBitmap(bitmap);
+        Allocation ret = convertBitmap(getRenderScriptContext(), bitmap);
         bitmap.recycle();
         return ret;
     }
@@ -232,7 +238,7 @@ public abstract class ImageFilterRS extends ImageFilter {
     /**
      * RS Script objects (and all other RS objects) should be cleared here
      */
-    abstract protected void resetScripts();
+    public abstract void resetScripts();
 
     /**
      * Scripts values should be bound here
@@ -244,6 +250,8 @@ public abstract class ImageFilterRS extends ImageFilter {
             return;
         }
         resetAllocations();
+        mLastInputWidth = 0;
+        mLastInputHeight = 0;
         setResourcesLoaded(false);
     }
 }