diff options
author | nicolasroard <nicolasroard@google.com> | 2013-02-05 17:09:05 -0800 |
---|---|---|
committer | nicolasroard <nicolasroard@google.com> | 2013-02-06 12:52:34 -0800 |
commit | 082803ec883c8182485872bbc34b63705b04d101 (patch) | |
tree | 60d8746608089445ba162625439f1d5df281b4c6 /src | |
parent | 083074c72999425c7e8a54ecafc965ae17dcbea8 (diff) | |
download | android_packages_apps_Snap-082803ec883c8182485872bbc34b63705b04d101.tar.gz android_packages_apps_Snap-082803ec883c8182485872bbc34b63705b04d101.tar.bz2 android_packages_apps_Snap-082803ec883c8182485872bbc34b63705b04d101.zip |
Refactor to use direct manipulations of FilterRepresentations.
Change-Id: If5f92f137283cb126835be004d315942ff95021c
Diffstat (limited to 'src')
10 files changed, 124 insertions, 56 deletions
diff --git a/src/com/android/gallery3d/filtershow/FilterShowActivity.java b/src/com/android/gallery3d/filtershow/FilterShowActivity.java index 56cfd3da9..db8055b4c 100644 --- a/src/com/android/gallery3d/filtershow/FilterShowActivity.java +++ b/src/com/android/gallery3d/filtershow/FilterShowActivity.java @@ -58,6 +58,7 @@ import android.widget.Toast; import com.android.gallery3d.R; import com.android.gallery3d.data.LocalAlbum; +import com.android.gallery3d.filtershow.cache.FilteringPipeline; import com.android.gallery3d.filtershow.cache.ImageLoader; import com.android.gallery3d.filtershow.editors.BasicEditor; import com.android.gallery3d.filtershow.editors.EditorDraw; @@ -468,6 +469,12 @@ public class FilterShowActivity extends Activity implements OnItemClickListener, cannotLoadImage(); } + Bitmap largeBitmap = mImageLoader.getOriginalBitmapLarge(); + FilteringPipeline pipeline = FilteringPipeline.getPipeline(); + pipeline.setOriginal(largeBitmap); + float previewScale = (float) largeBitmap.getWidth() / (float) mImageLoader.getOriginalBounds().width(); + pipeline.setPreviewScaleFactor(previewScale); + Bitmap bmap = mImageLoader.getOriginalBitmapSmall(); if (bmap != null && bmap.getWidth() > 0 && bmap.getHeight() > 0) { float w = bmap.getWidth(); diff --git a/src/com/android/gallery3d/filtershow/PanelController.java b/src/com/android/gallery3d/filtershow/PanelController.java index 65161e4c8..a852632dc 100644 --- a/src/com/android/gallery3d/filtershow/PanelController.java +++ b/src/com/android/gallery3d/filtershow/PanelController.java @@ -406,6 +406,7 @@ public class PanelController implements OnClickListener { return mMasterImage.getPreset(); } + /** public ImageFilter setImagePreset(ImageFilter filter, String name) { ImagePreset copy = new ImagePreset(getImagePreset()); copy.add(filter); @@ -414,6 +415,7 @@ public class PanelController implements OnClickListener { mMasterImage.setPreset(copy, true); return filter; } + */ // TODO: remove this. public void ensureFilter(String name) { diff --git a/src/com/android/gallery3d/filtershow/cache/FilteringPipeline.java b/src/com/android/gallery3d/filtershow/cache/FilteringPipeline.java index 69f91aca0..e59b6c3eb 100644 --- a/src/com/android/gallery3d/filtershow/cache/FilteringPipeline.java +++ b/src/com/android/gallery3d/filtershow/cache/FilteringPipeline.java @@ -22,21 +22,15 @@ import android.os.Process; import android.support.v8.renderscript.*; import android.util.Log; -import com.android.gallery3d.filtershow.filters.FilterRepresentation; -import com.android.gallery3d.filtershow.filters.ImageFilter; import com.android.gallery3d.filtershow.filters.ImageFilterRS; -import com.android.gallery3d.filtershow.filters.ImageFilterVignette; import com.android.gallery3d.filtershow.imageshow.GeometryMetadata; import com.android.gallery3d.filtershow.imageshow.MasterImage; import com.android.gallery3d.filtershow.presets.ImagePreset; -import java.util.HashMap; - public class FilteringPipeline implements Handler.Callback { private final static FilteringPipeline gPipeline = new FilteringPipeline(); private static final String LOGTAG = "FilteringPipeline"; - private ImagePreset mPreviousPreset = null; private ImagePreset mPreviousGeometryPreset = null; private ImagePreset mPreviousFiltersPreset = null; private GeometryMetadata mPreviousGeometry = null; @@ -141,8 +135,10 @@ public class FilteringPipeline implements Handler.Callback { Log.v(LOGTAG,"setOriginal, size " + bitmap.getWidth() + " x " + bitmap.getHeight()); updateOriginalAllocation(MasterImage.getImage().getPreset()); updatePreviewBuffer(); + /* updateFiltersOnlyPreviewBuffer(); updateGeometryOnlyPreviewBuffer(); + */ } public synchronized boolean updateOriginalAllocation(ImagePreset preset) { @@ -182,16 +178,17 @@ public class FilteringPipeline implements Handler.Callback { if (mOriginalAllocation == null) { return; } + if (mProcessingHandler.hasMessages(COMPUTE_PRESET)) { + return; + } if (!needsRepaint()) { return; } - if (mProcessingHandler.hasMessages(COMPUTE_PRESET)) { - mProcessingHandler.removeMessages(COMPUTE_PRESET); + if (MasterImage.getImage().getPreset() == null) { + return; } Message msg = mProcessingHandler.obtainMessage(COMPUTE_PRESET); - ImagePreset preset = new ImagePreset(MasterImage.getImage().getPreset()); - setPresetParameters(preset); - msg.obj = preset; + msg.obj = MasterImage.getImage().getPreset(); mProcessingHandler.sendMessage(msg); } @@ -286,7 +283,6 @@ public class FilteringPipeline implements Handler.Callback { + ") took " + time + " ms, " + time2 + " ms for the filter, on thread " + thread); } if (type == COMPUTE_PRESET) { - mPreviousPreset = preset; if (mResizeFactor > 0.6 && time > MAX_PROCESS_TIME && (System.currentTimeMillis() + 1000 > mResizeTime)) { mResizeTime = System.currentTimeMillis(); mResizeFactor *= RESIZE_FACTOR; @@ -299,14 +295,8 @@ public class FilteringPipeline implements Handler.Callback { } private synchronized boolean needsRepaint() { - ImagePreset preset = MasterImage.getImage().getPreset(); - if (preset == null || mPreviousPreset == null) { - return true; - } - if (preset.equals(mPreviousPreset)) { - return false; - } - return true; + TripleBufferBitmap buffer = MasterImage.getImage().getDoubleBuffer(); + return buffer.checkRepaintNeeded(); } private synchronized boolean needsGeometryRepaint() { diff --git a/src/com/android/gallery3d/filtershow/cache/ImageLoader.java b/src/com/android/gallery3d/filtershow/cache/ImageLoader.java index ade775d09..da8e52856 100644 --- a/src/com/android/gallery3d/filtershow/cache/ImageLoader.java +++ b/src/com/android/gallery3d/filtershow/cache/ImageLoader.java @@ -212,10 +212,6 @@ public class ImageLoader { mOriginalBitmapLarge = rotateToPortrait(mOriginalBitmapLarge, mOrientation); } mZoomOrientation = mOrientation; - FilteringPipeline pipeline = FilteringPipeline.getPipeline(); - pipeline.setOriginal(mOriginalBitmapLarge); - float previewScale = (float) mOriginalBitmapLarge.getWidth() / (float) getOriginalBounds().width(); - pipeline.setPreviewScaleFactor(previewScale); warnListeners(); } diff --git a/src/com/android/gallery3d/filtershow/cache/TripleBufferBitmap.java b/src/com/android/gallery3d/filtershow/cache/TripleBufferBitmap.java index c4837ad4a..cc14bf65f 100644 --- a/src/com/android/gallery3d/filtershow/cache/TripleBufferBitmap.java +++ b/src/com/android/gallery3d/filtershow/cache/TripleBufferBitmap.java @@ -30,6 +30,7 @@ public class TripleBufferBitmap { private boolean mNeedsSwap = false; private final Bitmap.Config mBitmapConfig = Bitmap.Config.ARGB_8888; + private boolean mNeedsRepaint = true; public TripleBufferBitmap() { @@ -68,4 +69,17 @@ public class TripleBufferBitmap { 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/editors/BasicEditor.java b/src/com/android/gallery3d/filtershow/editors/BasicEditor.java index fa3bd4a4a..b7f5d7d26 100644 --- a/src/com/android/gallery3d/filtershow/editors/BasicEditor.java +++ b/src/com/android/gallery3d/filtershow/editors/BasicEditor.java @@ -20,12 +20,12 @@ import com.android.gallery3d.R; import com.android.gallery3d.filtershow.filters.*; import android.content.Context; -import android.util.Log; import android.view.View; import android.widget.FrameLayout; import android.widget.SeekBar; import android.widget.SeekBar.OnSeekBarChangeListener; import com.android.gallery3d.filtershow.imageshow.MasterImage; +import com.android.gallery3d.filtershow.presets.ImagePreset; /** * The basic editor that all the one parameter filters @@ -47,8 +47,8 @@ public class BasicEditor extends Editor implements OnSeekBarChangeListener { protected BasicEditor(int id, int layoutID, int viewID) { super(id); - int mLayoutID = layoutID; - int mViewID = viewID; + mLayoutID = layoutID; + mViewID = viewID; } @Override @@ -61,9 +61,9 @@ public class BasicEditor extends Editor implements OnSeekBarChangeListener { @Override public void reflectCurrentFilter() { - FilterRepresentation filterRepresentation = MasterImage.getImage().getCurrentFilterRepresentation(); - if (filterRepresentation != null && filterRepresentation instanceof FilterBasicRepresentation) { - FilterBasicRepresentation interval = (FilterBasicRepresentation) filterRepresentation; + super.reflectCurrentFilter(); + if (getLocalRepresentation() != null && getLocalRepresentation() instanceof FilterBasicRepresentation) { + FilterBasicRepresentation interval = (FilterBasicRepresentation) getLocalRepresentation(); boolean f = interval.showParameterValue(); mSeekBar.setVisibility((f) ? View.VISIBLE : View.INVISIBLE); int value = interval.getValue(); @@ -76,9 +76,8 @@ public class BasicEditor extends Editor implements OnSeekBarChangeListener { @Override public void onProgressChanged(SeekBar sbar, int progress, boolean arg2) { - FilterRepresentation filterRepresentation = MasterImage.getImage().getCurrentFilterRepresentation(); - if (filterRepresentation != null && filterRepresentation instanceof FilterBasicRepresentation) { - FilterBasicRepresentation interval = (FilterBasicRepresentation) filterRepresentation; + if (getLocalRepresentation() != null && getLocalRepresentation() instanceof FilterBasicRepresentation) { + FilterBasicRepresentation interval = (FilterBasicRepresentation) getLocalRepresentation(); int value = progress + interval.getMinimum(); interval.setValue(value); mImageShow.onNewValue(value); @@ -86,9 +85,7 @@ public class BasicEditor extends Editor implements OnSeekBarChangeListener { if (interval.showParameterValue()) { mPanelController.onNewValue(value); } - - Log.v(LOGTAG, " #### progress=" + value); - MasterImage.getImage().updateBuffers(); + commitLocalRepresentation(); } } diff --git a/src/com/android/gallery3d/filtershow/editors/Editor.java b/src/com/android/gallery3d/filtershow/editors/Editor.java index a9b2fd425..bea591c63 100644 --- a/src/com/android/gallery3d/filtershow/editors/Editor.java +++ b/src/com/android/gallery3d/filtershow/editors/Editor.java @@ -25,7 +25,10 @@ import android.widget.LinearLayout; import com.android.gallery3d.filtershow.PanelController; import com.android.gallery3d.filtershow.cache.ImageLoader; +import com.android.gallery3d.filtershow.filters.FilterRepresentation; import com.android.gallery3d.filtershow.imageshow.ImageShow; +import com.android.gallery3d.filtershow.imageshow.MasterImage; +import com.android.gallery3d.filtershow.presets.ImagePreset; /** * Base class for Editors Must contain a mImageShow and a top level view @@ -38,6 +41,7 @@ public class Editor { protected PanelController mPanelController; protected int mID; private final String LOGTAG = "Editor"; + protected FilterRepresentation mLocalRepresentation = null; public void setPanelController(PanelController panelController) { this.mPanelController = panelController; @@ -53,6 +57,7 @@ public class Editor { public void createEditor(Context context,FrameLayout frameLayout) { mContext = context; mFrameLayout = frameLayout; + mLocalRepresentation = null; } protected void unpack(int viewid, int layoutid) { @@ -105,10 +110,25 @@ public class Editor { mView.setVisibility(visible); } + public FilterRepresentation getLocalRepresentation() { + if (mLocalRepresentation == null) { + ImagePreset preset = MasterImage.getImage().getPreset(); + FilterRepresentation filterRepresentation = MasterImage.getImage().getCurrentFilterRepresentation(); + mLocalRepresentation = preset.getFilterRepresentationCopyFrom(filterRepresentation); + } + return mLocalRepresentation; + } + + public void commitLocalRepresentation() { + ImagePreset preset = MasterImage.getImage().getPreset(); + preset.updateFilterRepresentation(getLocalRepresentation()); + } + /** * called after the filter is set and the select is called */ public void reflectCurrentFilter() { + mLocalRepresentation = null; } public boolean useUtilityPanel() { diff --git a/src/com/android/gallery3d/filtershow/filters/ImageFilterRS.java b/src/com/android/gallery3d/filtershow/filters/ImageFilterRS.java index 368e29a78..ca32fee46 100644 --- a/src/com/android/gallery3d/filtershow/filters/ImageFilterRS.java +++ b/src/com/android/gallery3d/filtershow/filters/ImageFilterRS.java @@ -45,8 +45,7 @@ public class ImageFilterRS extends ImageFilter { mOutPixelsAllocation.destroy(); } Bitmap bitmapBuffer = bitmap.copy(mBitmapConfig, true); - mOutPixelsAllocation = Allocation.createFromBitmap(mRS, bitmapBuffer, - Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT); + mOutPixelsAllocation = Allocation.createFromBitmap(mRS, bitmapBuffer); mInPixelsAllocation = Allocation.createTyped(mRS, mOutPixelsAllocation.getType()); sOldBitmap = bitmap; diff --git a/src/com/android/gallery3d/filtershow/imageshow/MasterImage.java b/src/com/android/gallery3d/filtershow/imageshow/MasterImage.java index 9eae1177c..5ce03a63a 100644 --- a/src/com/android/gallery3d/filtershow/imageshow/MasterImage.java +++ b/src/com/android/gallery3d/filtershow/imageshow/MasterImage.java @@ -187,6 +187,7 @@ public class MasterImage { if (mGeometryOnlyPreset == null || !newPreset.same(mGeometryOnlyPreset)) { mGeometryOnlyPreset = newPreset; + mGeometryOnlyPreview.invalidate(); } } if (force || mFiltersOnlyPreset == null) { @@ -195,16 +196,10 @@ public class MasterImage { if (mFiltersOnlyPreset == null || !newPreset.same(mFiltersOnlyPreset)) { mFiltersOnlyPreset = newPreset; + mFilteredPreview.invalidate(); } } mActivity.enableSave(hasModifications()); - updateBuffers(); - } - - public void updateBuffers() { - FilteringPipeline.getPipeline().updatePreviewBuffer(); - FilteringPipeline.getPipeline().updateGeometryOnlyPreviewBuffer(); - FilteringPipeline.getPipeline().updateFiltersOnlyPreviewBuffer(); } public FilterRepresentation getCurrentFilterRepresentation() { @@ -214,4 +209,9 @@ public class MasterImage { public void setCurrentFilterRepresentation(FilterRepresentation currentFilterRepresentation) { mCurrentFilterRepresentation = currentFilterRepresentation; } + + public void invalidatePreview() { + mFilteredPreview.invalidate(); + FilteringPipeline.getPipeline().updatePreviewBuffer(); + } } diff --git a/src/com/android/gallery3d/filtershow/presets/ImagePreset.java b/src/com/android/gallery3d/filtershow/presets/ImagePreset.java index 6e9ab9fc9..aba868829 100644 --- a/src/com/android/gallery3d/filtershow/presets/ImagePreset.java +++ b/src/com/android/gallery3d/filtershow/presets/ImagePreset.java @@ -27,14 +27,15 @@ import com.android.gallery3d.filtershow.filters.ImageFilter; import com.android.gallery3d.filtershow.filters.ImageFilterCurves; import com.android.gallery3d.filtershow.imageshow.GeometryMetadata; import com.android.gallery3d.filtershow.imageshow.ImageShow; +import com.android.gallery3d.filtershow.imageshow.MasterImage; +import java.util.HashMap; import java.util.Vector; public class ImagePreset { private static final String LOGTAG = "ImagePreset"; - private ImageShow mEndPoint = null; private FilterRepresentation mImageBorder = null; private float mScaleFactor = 1.0f; private boolean mIsHighQuality = false; @@ -51,10 +52,6 @@ public class ImagePreset { public final GeometryMetadata mGeoData = new GeometryMetadata(); - enum FullRotate { - ZERO, NINETY, HUNDRED_EIGHTY, TWO_HUNDRED_SEVENTY - } - public ImagePreset() { setup(); } @@ -92,6 +89,52 @@ public class ImagePreset { mGeoData.set(source.mGeoData); } + public FilterRepresentation getFilterRepresentation(int position) { + FilterRepresentation representation = null; + try { + representation = mFilters.elementAt(position).clone(); + } catch (CloneNotSupportedException e) { + e.printStackTrace(); + } + return representation; + } + + public int getPositionForRepresentation(FilterRepresentation representation) { + for (int i = 0; i < mFilters.size(); i++) { + if (mFilters.elementAt(i).getFilterClass() == representation.getFilterClass()) { + return i; + } + } + return -1; + } + + public FilterRepresentation getFilterRepresentationCopyFrom(FilterRepresentation filterRepresentation) { + // TODO: add concept of position in the filters (to allow multiple instances) + if (filterRepresentation == null) { + return null; + } + int position = getPositionForRepresentation(filterRepresentation); + if (position == -1) { + return null; + } + FilterRepresentation representation = null; + try { + representation = mFilters.elementAt(position).clone(); + } catch (CloneNotSupportedException e) { + e.printStackTrace(); + } + return representation; + } + + public void updateFilterRepresentation(FilterRepresentation representation) { + synchronized (mFilters) { + int position = getPositionForRepresentation(representation); + FilterRepresentation old = mFilters.elementAt(position); + old.useParametersFrom(representation); + } + MasterImage.getImage().invalidatePreview(); + } + public void setDoApplyGeometry(boolean value) { mDoApplyGeometry = value; } @@ -104,7 +147,7 @@ public class ImagePreset { return mDoApplyFilters; } - public GeometryMetadata getGeometry() { + public synchronized GeometryMetadata getGeometry() { return mGeoData; } @@ -144,7 +187,7 @@ public class ImagePreset { return true; } - public void setGeometry(GeometryMetadata m) { + public synchronized void setGeometry(GeometryMetadata m) { mGeoData.set(m); } @@ -314,9 +357,6 @@ public class ImagePreset { representation.setImagePreset(this); } - public void add(ImageFilter filter) { - } - public FilterRepresentation getRepresentation(FilterRepresentation filterRepresentation) { for (int i = 0; i < mFilters.size(); i++) { FilterRepresentation representation = mFilters.elementAt(i); @@ -361,7 +401,10 @@ public class ImagePreset { to = mFilters.size(); } for (int i = from; i < to; i++) { - FilterRepresentation representation = mFilters.elementAt(i); + FilterRepresentation representation = null; + synchronized (mFilters) { + representation = mFilters.elementAt(i); + } ImageFilter filter = FiltersManager.getManager().getFilterForRepresentation(representation); filter.useRepresentation(representation); bitmap = filter.apply(bitmap, mScaleFactor, mIsHighQuality); |