OSDN Git Service

Pipeline refactoring
authornicolasroard <nicolasroard@google.com>
Mon, 1 Jul 2013 23:58:51 +0000 (16:58 -0700)
committernicolasroard <nicolasroard@google.com>
Tue, 2 Jul 2013 00:45:59 +0000 (17:45 -0700)
Add SharedBuffer and Buffer class

Change-Id: I823a9520541e3be5321a3deb5e58a358588b6cf3

src/com/android/gallery3d/filtershow/cache/CachingPipeline.java
src/com/android/gallery3d/filtershow/cache/FilteringPipeline.java
src/com/android/gallery3d/filtershow/imageshow/MasterImage.java
src/com/android/gallery3d/filtershow/pipeline/Buffer.java [new file with mode: 0644]
src/com/android/gallery3d/filtershow/pipeline/SharedBuffer.java [moved from src/com/android/gallery3d/filtershow/cache/TripleBufferBitmap.java with 57% similarity]
src/com/android/gallery3d/filtershow/presets/FilterEnvironment.java

index 494c0a6..9901e57 100644 (file)
@@ -27,6 +27,7 @@ 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 com.android.gallery3d.filtershow.pipeline.SharedBuffer;
 import com.android.gallery3d.filtershow.presets.FilterEnvironment;
 import com.android.gallery3d.filtershow.presets.ImagePreset;
 import com.android.gallery3d.filtershow.presets.PipelineInterface;
@@ -342,7 +343,7 @@ public class CachingPipeline implements PipelineInterface {
                 FilterEnvironment.QUALITY_PREVIEW);
     }
 
-    public synchronized void compute(TripleBufferBitmap buffer, ImagePreset preset, int type) {
+    public synchronized void compute(SharedBuffer buffer, ImagePreset preset, int type) {
         synchronized (CachingPipeline.class) {
             if (getRenderScriptContext() == null) {
                 return;
@@ -361,16 +362,20 @@ public class CachingPipeline implements PipelineInterface {
             if (updateOriginalAllocation(preset)) {
                 resizedOriginalBitmap = mResizedOriginalBitmap;
                 mEnvironment.cache(buffer.getProducer());
-                buffer.updateProducerBitmap(resizedOriginalBitmap);
+                buffer.setProducer(resizedOriginalBitmap);
+            }
+
+            Bitmap bitmap = null;
+            if (buffer.getProducer() != null) {
+                bitmap = buffer.getProducer().getBitmap();
             }
-            Bitmap bitmap = buffer.getProducer();
             long time2 = System.currentTimeMillis();
 
             if (bitmap == null || (bitmap.getWidth() != resizedOriginalBitmap.getWidth())
                     || (bitmap.getHeight() != resizedOriginalBitmap.getHeight())) {
                 mEnvironment.cache(buffer.getProducer());
-                buffer.updateProducerBitmap(resizedOriginalBitmap);
-                bitmap = buffer.getProducer();
+                buffer.setProducer(resizedOriginalBitmap);
+                bitmap = buffer.getProducer().getBitmap();
             }
             mOriginalAllocation.copyTo(bitmap);
 
@@ -393,7 +398,7 @@ public class CachingPipeline implements PipelineInterface {
     }
 
     public boolean needsRepaint() {
-        TripleBufferBitmap buffer = MasterImage.getImage().getDoubleBuffer();
+        SharedBuffer buffer = MasterImage.getImage().getPreviewBuffer();
         return buffer.checkRepaintNeeded();
     }
 
index a0b2897..cac7e05 100644 (file)
@@ -26,6 +26,7 @@ import com.android.gallery3d.filtershow.filters.FiltersManager;
 import com.android.gallery3d.filtershow.filters.ImageFilterRS;
 import com.android.gallery3d.filtershow.imageshow.GeometryMetadata;
 import com.android.gallery3d.filtershow.imageshow.MasterImage;
+import com.android.gallery3d.filtershow.pipeline.SharedBuffer;
 import com.android.gallery3d.filtershow.presets.ImagePreset;
 
 public class FilteringPipeline implements Handler.Callback {
@@ -71,7 +72,7 @@ public class FilteringPipeline implements Handler.Callback {
         public void handleMessage(Message msg) {
             switch (msg.what) {
                 case NEW_PRESET: {
-                    TripleBufferBitmap buffer = MasterImage.getImage().getDoubleBuffer();
+                    SharedBuffer buffer = MasterImage.getImage().getPreviewBuffer();
                     buffer.swapConsumer();
                     MasterImage.getImage().notifyObservers();
                     if (mHasUnhandledPreviewRequest) {
@@ -96,7 +97,7 @@ public class FilteringPipeline implements Handler.Callback {
         switch (msg.what) {
             case COMPUTE_PRESET: {
                 ImagePreset preset = (ImagePreset) msg.obj;
-                TripleBufferBitmap buffer = MasterImage.getImage().getDoubleBuffer();
+                SharedBuffer buffer = MasterImage.getImage().getPreviewBuffer();
                 mPreviewPipeline.compute(buffer, preset, COMPUTE_PRESET);
                 buffer.swapProducer();
                 Message uimsg = mUIHandler.obtainMessage(NEW_PRESET);
index 44b9d82..7667075 100644 (file)
@@ -31,9 +31,10 @@ import com.android.gallery3d.filtershow.cache.FilteringPipeline;
 import com.android.gallery3d.filtershow.cache.ImageLoader;
 import com.android.gallery3d.filtershow.cache.RenderingRequest;
 import com.android.gallery3d.filtershow.cache.RenderingRequestCaller;
-import com.android.gallery3d.filtershow.cache.TripleBufferBitmap;
 import com.android.gallery3d.filtershow.filters.FilterRepresentation;
 import com.android.gallery3d.filtershow.filters.ImageFilter;
+import com.android.gallery3d.filtershow.pipeline.Buffer;
+import com.android.gallery3d.filtershow.pipeline.SharedBuffer;
 import com.android.gallery3d.filtershow.presets.ImagePreset;
 import com.android.gallery3d.filtershow.state.StateAdapter;
 
@@ -56,7 +57,7 @@ public class MasterImage implements RenderingRequestCaller {
     private ImagePreset mGeometryOnlyPreset = null;
     private ImagePreset mFiltersOnlyPreset = null;
 
-    private TripleBufferBitmap mFilteredPreview = new TripleBufferBitmap();
+    private SharedBuffer mPreviewBuffer = new SharedBuffer();
 
     private Bitmap mGeometryOnlyBitmap = null;
     private Bitmap mFiltersOnlyBitmap = null;
@@ -250,8 +251,8 @@ public class MasterImage implements RenderingRequestCaller {
         }
     }
 
-    public TripleBufferBitmap getDoubleBuffer() {
-        return mFilteredPreview;
+    public SharedBuffer getPreviewBuffer() {
+        return mPreviewBuffer;
     }
 
     public void setOriginalGeometry(Bitmap originalBitmapLarge) {
@@ -265,7 +266,11 @@ public class MasterImage implements RenderingRequestCaller {
     }
 
     public Bitmap getFilteredImage() {
-        return mFilteredPreview.getConsumer();
+        Buffer consumer = mPreviewBuffer.getConsumer();
+        if (consumer != null) {
+            return consumer.getBitmap();
+        }
+        return null;
     }
 
     public Bitmap getFiltersOnlyImage() {
@@ -344,7 +349,7 @@ public class MasterImage implements RenderingRequestCaller {
     }
 
     public void invalidatePreview() {
-        mFilteredPreview.invalidate();
+        mPreviewBuffer.invalidate();
         invalidatePartialPreview();
         invalidateHighresPreview();
         needsUpdatePartialPreview();
@@ -373,7 +378,7 @@ public class MasterImage implements RenderingRequestCaller {
         Point translate = getTranslation();
         float scaleFactor = getScaleFactor();
         m.postTranslate(translate.x, translate.y);
-        m.postScale(scaleFactor, scaleFactor, mImageShowSize.x/2.0f, mImageShowSize.y/2.0f);
+        m.postScale(scaleFactor, scaleFactor, mImageShowSize.x / 2.0f, mImageShowSize.y / 2.0f);
         return m;
     }
 
diff --git a/src/com/android/gallery3d/filtershow/pipeline/Buffer.java b/src/com/android/gallery3d/filtershow/pipeline/Buffer.java
new file mode 100644 (file)
index 0000000..7268516
--- /dev/null
@@ -0,0 +1,56 @@
+/*
+ * 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.support.v8.renderscript.Allocation;
+import android.support.v8.renderscript.RenderScript;
+import com.android.gallery3d.filtershow.cache.CachingPipeline;
+
+public class Buffer {
+    private Bitmap mBitmap;
+    private Allocation mAllocation;
+    private boolean mUseAllocation = false;
+    private static final Bitmap.Config BITMAP_CONFIG = Bitmap.Config.ARGB_8888;
+
+    public Buffer(Bitmap bitmap) {
+        RenderScript rs = CachingPipeline.getRenderScriptContext();
+        mBitmap = bitmap.copy(BITMAP_CONFIG, true);
+        if (mUseAllocation) {
+            // TODO: recreate the allocation when the RS context changes
+            mAllocation = Allocation.createFromBitmap(rs, mBitmap,
+                    Allocation.MipmapControl.MIPMAP_NONE,
+                    Allocation.USAGE_SHARED | Allocation.USAGE_SCRIPT);
+        }
+    }
+
+    public Bitmap getBitmap() {
+        return mBitmap;
+    }
+
+    public Allocation getAllocation() {
+        return mAllocation;
+    }
+
+    public void sync() {
+        if (mUseAllocation) {
+            mAllocation.copyTo(mBitmap);
+        }
+    }
+
+}
+
  * limitations under the License.
  */
 
-package com.android.gallery3d.filtershow.cache;
+package com.android.gallery3d.filtershow.pipeline;
 
 import android.graphics.Bitmap;
+import android.util.Log;
 
-public class TripleBufferBitmap {
+public class SharedBuffer {
 
-    private static String LOGTAG = "TripleBufferBitmap";
+    private static final String LOGTAG = "SharedBuffer";
 
-    private volatile Bitmap mBitmaps[] = new Bitmap[3];
-    private volatile Bitmap mProducer = null;
-    private volatile Bitmap mConsumer = null;
-    private volatile Bitmap mIntermediate = null;
+    private volatile Buffer mProducer = null;
+    private volatile Buffer mConsumer = null;
+    private volatile Buffer mIntermediate = null;
     private volatile boolean mNeedsSwap = false;
 
-    private final Bitmap.Config mBitmapConfig = Bitmap.Config.ARGB_8888;
     private volatile boolean mNeedsRepaint = true;
 
-    public TripleBufferBitmap() {
-
-    }
-
-    public synchronized void updateBitmaps(Bitmap bitmap) {
-        mBitmaps[0] = bitmap.copy(mBitmapConfig, true);
-        mBitmaps[1] = bitmap.copy(mBitmapConfig, true);
-        mBitmaps[2] = bitmap.copy(mBitmapConfig, true);
-        mProducer = mBitmaps[0];
-        mConsumer = mBitmaps[1];
-        mIntermediate = mBitmaps[2];
-    }
-
-    public synchronized void updateProducerBitmap(Bitmap bitmap) {
-        mProducer = bitmap.copy(mBitmapConfig, true);
+    public SharedBuffer() {
     }
 
     public synchronized void setProducer(Bitmap producer) {
-        mProducer = producer;
+        mProducer = new Buffer(producer);
     }
 
-    public synchronized Bitmap getProducer() {
+    public synchronized Buffer getProducer() {
         return mProducer;
     }
 
-    public synchronized Bitmap getConsumer() {
+    public synchronized Buffer getConsumer() {
         return mConsumer;
     }
 
     public synchronized void swapProducer() {
-        Bitmap intermediate = mIntermediate;
+        if (mProducer != null) {
+            mProducer.sync();
+        }
+        Buffer intermediate = mIntermediate;
         mIntermediate = mProducer;
         mProducer = intermediate;
         mNeedsSwap = true;
@@ -71,7 +59,10 @@ public class TripleBufferBitmap {
         if (!mNeedsSwap) {
             return;
         }
-        Bitmap intermediate = mIntermediate;
+        if (mConsumer != null) {
+            mConsumer.sync();
+        }
+        Buffer intermediate = mIntermediate;
         mIntermediate = mConsumer;
         mConsumer = intermediate;
         mNeedsSwap = false;
@@ -90,3 +81,4 @@ public class TripleBufferBitmap {
     }
 
 }
+
index 8d59c9f..6a130a3 100644 (file)
@@ -22,6 +22,7 @@ import android.support.v8.renderscript.Allocation;
 import com.android.gallery3d.filtershow.filters.FilterRepresentation;
 import com.android.gallery3d.filtershow.filters.FiltersManagerInterface;
 import com.android.gallery3d.filtershow.filters.ImageFilter;
+import com.android.gallery3d.filtershow.pipeline.Buffer;
 
 import java.lang.ref.WeakReference;
 import java.util.HashMap;
@@ -53,7 +54,11 @@ public class FilterEnvironment {
     private HashMap<Integer, Integer>
                     generalParameters = new HashMap<Integer, Integer>();
 
-    public void cache(Bitmap bitmap) {
+    public void cache(Buffer buffer) {
+        if (buffer == null) {
+            return;
+        }
+        Bitmap bitmap = buffer.getBitmap();
         if (bitmap == null) {
             return;
         }