From a9be5b3d573b1b5511300272fca5b320bdc0aae0 Mon Sep 17 00:00:00 2001 From: nicolasroard Date: Mon, 1 Jul 2013 16:58:51 -0700 Subject: Pipeline refactoring Add SharedBuffer and Buffer class Change-Id: I823a9520541e3be5321a3deb5e58a358588b6cf3 --- .../filtershow/cache/CachingPipeline.java | 17 ++-- .../filtershow/cache/FilteringPipeline.java | 5 +- .../filtershow/cache/TripleBufferBitmap.java | 92 ---------------------- .../filtershow/imageshow/MasterImage.java | 19 +++-- .../gallery3d/filtershow/pipeline/Buffer.java | 56 +++++++++++++ .../filtershow/pipeline/SharedBuffer.java | 84 ++++++++++++++++++++ .../filtershow/presets/FilterEnvironment.java | 7 +- 7 files changed, 172 insertions(+), 108 deletions(-) delete mode 100644 src/com/android/gallery3d/filtershow/cache/TripleBufferBitmap.java create mode 100644 src/com/android/gallery3d/filtershow/pipeline/Buffer.java create mode 100644 src/com/android/gallery3d/filtershow/pipeline/SharedBuffer.java (limited to 'src') diff --git a/src/com/android/gallery3d/filtershow/cache/CachingPipeline.java b/src/com/android/gallery3d/filtershow/cache/CachingPipeline.java index 494c0a636..9901e5705 100644 --- a/src/com/android/gallery3d/filtershow/cache/CachingPipeline.java +++ b/src/com/android/gallery3d/filtershow/cache/CachingPipeline.java @@ -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(); } diff --git a/src/com/android/gallery3d/filtershow/cache/FilteringPipeline.java b/src/com/android/gallery3d/filtershow/cache/FilteringPipeline.java index a0b289733..cac7e056d 100644 --- a/src/com/android/gallery3d/filtershow/cache/FilteringPipeline.java +++ b/src/com/android/gallery3d/filtershow/cache/FilteringPipeline.java @@ -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); diff --git a/src/com/android/gallery3d/filtershow/cache/TripleBufferBitmap.java b/src/com/android/gallery3d/filtershow/cache/TripleBufferBitmap.java deleted file mode 100644 index ba7b76925..000000000 --- a/src/com/android/gallery3d/filtershow/cache/TripleBufferBitmap.java +++ /dev/null @@ -1,92 +0,0 @@ -/* - * 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.cache; - -import android.graphics.Bitmap; - -public class TripleBufferBitmap { - - private static String LOGTAG = "TripleBufferBitmap"; - - private volatile Bitmap mBitmaps[] = new Bitmap[3]; - private volatile Bitmap mProducer = null; - private volatile Bitmap mConsumer = null; - private volatile Bitmap 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 synchronized void setProducer(Bitmap producer) { - mProducer = producer; - } - - public synchronized Bitmap getProducer() { - return mProducer; - } - - public synchronized Bitmap getConsumer() { - return mConsumer; - } - - public synchronized void swapProducer() { - Bitmap intermediate = mIntermediate; - mIntermediate = mProducer; - mProducer = intermediate; - mNeedsSwap = true; - } - - public synchronized void swapConsumer() { - if (!mNeedsSwap) { - return; - } - Bitmap intermediate = mIntermediate; - mIntermediate = mConsumer; - mConsumer = intermediate; - mNeedsSwap = false; - } - - public synchronized void invalidate() { - mNeedsRepaint = true; - } - - public synchronized boolean checkRepaintNeeded() { - if (mNeedsRepaint) { - mNeedsRepaint = false; - return true; - } - return false; - } - -} diff --git a/src/com/android/gallery3d/filtershow/imageshow/MasterImage.java b/src/com/android/gallery3d/filtershow/imageshow/MasterImage.java index 44b9d82bc..76670754e 100644 --- a/src/com/android/gallery3d/filtershow/imageshow/MasterImage.java +++ b/src/com/android/gallery3d/filtershow/imageshow/MasterImage.java @@ -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 index 000000000..72685167a --- /dev/null +++ b/src/com/android/gallery3d/filtershow/pipeline/Buffer.java @@ -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); + } + } + +} + diff --git a/src/com/android/gallery3d/filtershow/pipeline/SharedBuffer.java b/src/com/android/gallery3d/filtershow/pipeline/SharedBuffer.java new file mode 100644 index 000000000..587174668 --- /dev/null +++ b/src/com/android/gallery3d/filtershow/pipeline/SharedBuffer.java @@ -0,0 +1,84 @@ +/* + * 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.util.Log; + +public class SharedBuffer { + + private static final String LOGTAG = "SharedBuffer"; + + private volatile Buffer mProducer = null; + private volatile Buffer mConsumer = null; + private volatile Buffer mIntermediate = null; + private volatile boolean mNeedsSwap = false; + + private volatile boolean mNeedsRepaint = true; + + public SharedBuffer() { + } + + public synchronized void setProducer(Bitmap producer) { + mProducer = new Buffer(producer); + } + + public synchronized Buffer getProducer() { + return mProducer; + } + + public synchronized Buffer getConsumer() { + return mConsumer; + } + + public synchronized void swapProducer() { + if (mProducer != null) { + mProducer.sync(); + } + Buffer intermediate = mIntermediate; + mIntermediate = mProducer; + mProducer = intermediate; + mNeedsSwap = true; + } + + public synchronized void swapConsumer() { + if (!mNeedsSwap) { + return; + } + if (mConsumer != null) { + mConsumer.sync(); + } + Buffer intermediate = mIntermediate; + mIntermediate = mConsumer; + mConsumer = intermediate; + mNeedsSwap = false; + } + + public synchronized void invalidate() { + mNeedsRepaint = true; + } + + public synchronized boolean checkRepaintNeeded() { + if (mNeedsRepaint) { + mNeedsRepaint = false; + return true; + } + return false; + } + +} + diff --git a/src/com/android/gallery3d/filtershow/presets/FilterEnvironment.java b/src/com/android/gallery3d/filtershow/presets/FilterEnvironment.java index 8d59c9f54..6a130a3e3 100644 --- a/src/com/android/gallery3d/filtershow/presets/FilterEnvironment.java +++ b/src/com/android/gallery3d/filtershow/presets/FilterEnvironment.java @@ -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 generalParameters = new HashMap(); - public void cache(Bitmap bitmap) { + public void cache(Buffer buffer) { + if (buffer == null) { + return; + } + Bitmap bitmap = buffer.getBitmap(); if (bitmap == null) { return; } -- cgit v1.2.3