From 8d2c7ee02c8fde53effc787c172b10b14ae34d42 Mon Sep 17 00:00:00 2001 From: nicolasroard Date: Mon, 11 Mar 2013 19:16:03 -0700 Subject: [PATCH] Various bugfixes/improvements for Fx Filters: - fix bug in history with Fx filters - don't load Fx bitmaps at startup - free bitmaps when unneeded - fix history preview bitmaps Change-Id: I5631a5bcec12c121871b3575066c201486896a14 --- .../gallery3d/filtershow/FilterShowActivity.java | 20 ++++-------- .../gallery3d/filtershow/HistoryAdapter.java | 2 ++ .../gallery3d/filtershow/PanelController.java | 1 + .../filtershow/filters/FilterFxRepresentation.java | 26 +++++++-------- .../filtershow/filters/FilterRepresentation.java | 6 +++- .../filtershow/filters/ImageFilterFx.java | 37 +++++++++++++++++++--- .../gallery3d/filtershow/presets/ImagePreset.java | 2 ++ 7 files changed, 61 insertions(+), 33 deletions(-) diff --git a/src/com/android/gallery3d/filtershow/FilterShowActivity.java b/src/com/android/gallery3d/filtershow/FilterShowActivity.java index 1c1a2bc03..286fcbdf0 100644 --- a/src/com/android/gallery3d/filtershow/FilterShowActivity.java +++ b/src/com/android/gallery3d/filtershow/FilterShowActivity.java @@ -64,14 +64,7 @@ import com.android.gallery3d.filtershow.editors.EditorRotate; import com.android.gallery3d.filtershow.editors.EditorStraighten; import com.android.gallery3d.filtershow.editors.EditorTinyPlanet; import com.android.gallery3d.filtershow.editors.ImageOnlyEditor; -import com.android.gallery3d.filtershow.filters.FilterColorBorderRepresentation; -import com.android.gallery3d.filtershow.filters.FilterFxRepresentation; -import com.android.gallery3d.filtershow.filters.FilterImageBorderRepresentation; -import com.android.gallery3d.filtershow.filters.FilterRepresentation; -import com.android.gallery3d.filtershow.filters.FiltersManager; -import com.android.gallery3d.filtershow.filters.ImageFilter; -import com.android.gallery3d.filtershow.filters.ImageFilterBorder; -import com.android.gallery3d.filtershow.filters.ImageFilterRS; +import com.android.gallery3d.filtershow.filters.*; import com.android.gallery3d.filtershow.imageshow.GeometryMetadata; import com.android.gallery3d.filtershow.imageshow.ImageCrop; import com.android.gallery3d.filtershow.imageshow.ImageShow; @@ -343,10 +336,15 @@ public class FilterShowActivity extends Activity implements OnItemClickListener, ImageFilterRS.setRenderScriptContext(this); Resources res = getResources(); + // TODO: add a mechanism to set the resources in FiltersManagmer ImageFilterBorder filterBorder = (ImageFilterBorder) FiltersManager.getManager().getFilter(ImageFilterBorder.class); filterBorder.setResources(res); filterBorder = (ImageFilterBorder) FiltersManager.getPreviewManager().getFilter(ImageFilterBorder.class); filterBorder.setResources(res); + ImageFilterFx filterFx = (ImageFilterFx) FiltersManager.getManager().getFilter(ImageFilterFx.class); + filterFx.setResources(res); + filterFx = (ImageFilterFx) FiltersManager.getPreviewManager().getFilter(ImageFilterFx.class); + filterFx.setResources(res); ImageShow.setDefaultBackgroundColor(res.getColor(R.color.background_screen)); // TODO: get those values from XML. @@ -807,13 +805,8 @@ public class FilterShowActivity extends Activity implements OnItemClickListener, R.string.ffx_x_process }; - BitmapFactory.Options o = new BitmapFactory.Options(); - o.inScaled = false; - for (int i = 0; i < drawid.length; i++) { - Bitmap b = BitmapFactory.decodeResource(getResources(), drawid[i], o); FilterFxRepresentation fx = new FilterFxRepresentation(getString(fxNameid[i]), drawid[i], fxNameid[i]); - fx.setFxBitmap(b); fxArray[p++] = fx; } @@ -922,6 +915,7 @@ public class FilterShowActivity extends Activity implements OnItemClickListener, mMasterImage.setHistoryAdapter(mHistoryAdapter); mMasterImage.setStateAdapter(mImageStateAdapter); mMasterImage.setActivity(this); + mMasterImage.setImageLoader(mImageLoader); } // ////////////////////////////////////////////////////////////////////////////// diff --git a/src/com/android/gallery3d/filtershow/HistoryAdapter.java b/src/com/android/gallery3d/filtershow/HistoryAdapter.java index dc42f4027..2afb7d2a6 100644 --- a/src/com/android/gallery3d/filtershow/HistoryAdapter.java +++ b/src/com/android/gallery3d/filtershow/HistoryAdapter.java @@ -201,6 +201,8 @@ public class HistoryAdapter extends ArrayAdapter { Bitmap bmp = item.getPreviewImage(); if (bmp != null) { preview.setImageBitmap(bmp); + } else { + preview.setImageResource(android.R.color.transparent); } if (position == mCurrentPresetPosition) { view.setBackgroundColor(Color.WHITE); diff --git a/src/com/android/gallery3d/filtershow/PanelController.java b/src/com/android/gallery3d/filtershow/PanelController.java index 645334c75..206f54f64 100644 --- a/src/com/android/gallery3d/filtershow/PanelController.java +++ b/src/com/android/gallery3d/filtershow/PanelController.java @@ -523,6 +523,7 @@ public class PanelController implements OnClickListener { if (filterRepresentation.allowsMultipleInstances()) { representation.updateTempParametersFrom(filterRepresentation); copy.setHistoryName(filterRepresentation.getName()); + representation.synchronizeRepresentation(); } filterRepresentation = representation; } diff --git a/src/com/android/gallery3d/filtershow/filters/FilterFxRepresentation.java b/src/com/android/gallery3d/filtershow/filters/FilterFxRepresentation.java index d4128dc79..6e2e7ea16 100644 --- a/src/com/android/gallery3d/filtershow/filters/FilterFxRepresentation.java +++ b/src/com/android/gallery3d/filtershow/filters/FilterFxRepresentation.java @@ -17,10 +17,11 @@ package com.android.gallery3d.filtershow.filters; import android.graphics.Bitmap; +import com.android.gallery3d.app.Log; import com.android.gallery3d.filtershow.editors.ImageOnlyEditor; public class FilterFxRepresentation extends FilterRepresentation { - private Bitmap mFxBitmap = null; + private static final String LOGTAG = "FilterFxRepresentation"; // TODO: When implementing serialization, we should find a unique way of // specifying bitmaps / names (the resource IDs being random) private int mBitmapResource = 0; @@ -41,26 +42,24 @@ public class FilterFxRepresentation extends FilterRepresentation { } public String toString() { - return "FilterFx: " + getName(); + return "FilterFx: " + hashCode() + " : " + getName() + " bitmap rsc: " + mBitmapResource; } @Override - public FilterRepresentation clone() throws CloneNotSupportedException { + public synchronized FilterRepresentation clone() throws CloneNotSupportedException { FilterFxRepresentation representation = (FilterFxRepresentation) super.clone(); representation.setName(getName()); representation.setBitmapResource(getBitmapResource()); representation.setNameResource(getNameResource()); - representation.setFxBitmap(getFxBitmap()); return representation; } - public void useParametersFrom(FilterRepresentation a) { + public synchronized void useParametersFrom(FilterRepresentation a) { if (a instanceof FilterFxRepresentation) { FilterFxRepresentation representation = (FilterFxRepresentation) a; setName(representation.getName()); setBitmapResource(representation.getBitmapResource()); setNameResource(representation.getNameResource()); - setFxBitmap(representation.getFxBitmap()); } } @@ -79,16 +78,15 @@ public class FilterFxRepresentation extends FilterRepresentation { return false; } - public boolean allowsMultipleInstances() { - return true; - } - - public Bitmap getFxBitmap() { - return mFxBitmap; + public boolean same(FilterRepresentation representation) { + if (!super.same(representation)) { + return false; + } + return equals(representation); } - public void setFxBitmap(Bitmap fxBitmap) { - mFxBitmap = fxBitmap; + public boolean allowsMultipleInstances() { + return true; } public int getNameResource() { diff --git a/src/com/android/gallery3d/filtershow/filters/FilterRepresentation.java b/src/com/android/gallery3d/filtershow/filters/FilterRepresentation.java index e0dc905e3..b92460d99 100644 --- a/src/com/android/gallery3d/filtershow/filters/FilterRepresentation.java +++ b/src/com/android/gallery3d/filtershow/filters/FilterRepresentation.java @@ -21,6 +21,7 @@ import com.android.gallery3d.filtershow.editors.BasicEditor; public class FilterRepresentation implements Cloneable { private static final String LOGTAG = "FilterRepresentation"; + private static final boolean DEBUG = false; private String mName; private int mPriority = TYPE_NORMAL; private Class mFilterClass; @@ -62,7 +63,10 @@ public class FilterRepresentation implements Cloneable { representation.setShowEditingControls(showEditingControls()); representation.setShowParameterValue(showParameterValue()); representation.setShowUtilityPanel(showUtilityPanel()); - Log.v(LOGTAG, "cloning from <" + this + "> to <" + representation + ">"); + representation.mTempRepresentation = null; + if (DEBUG) { + Log.v(LOGTAG, "cloning from <" + this + "> to <" + representation + ">"); + } return representation; } diff --git a/src/com/android/gallery3d/filtershow/filters/ImageFilterFx.java b/src/com/android/gallery3d/filtershow/filters/ImageFilterFx.java index c2a7b7bfd..5404cd149 100644 --- a/src/com/android/gallery3d/filtershow/filters/ImageFilterFx.java +++ b/src/com/android/gallery3d/filtershow/filters/ImageFilterFx.java @@ -16,15 +16,27 @@ package com.android.gallery3d.filtershow.filters; +import android.content.res.Resources; import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import com.android.gallery3d.app.Log; public class ImageFilterFx extends ImageFilter { - private static final String TAG = "ImageFilterFx"; + private static final String LOGTAG = "ImageFilterFx"; private FilterFxRepresentation mParameters = null; + private Bitmap mFxBitmap = null; + private Resources mResources = null; + private int mFxBitmapId = -1; public ImageFilterFx() { } + @Override + public void freeResources() { + if (mFxBitmap != null) mFxBitmap.recycle(); + mFxBitmap = null; + } + public void useRepresentation(FilterRepresentation representation) { FilterFxRepresentation parameters = (FilterFxRepresentation) representation; mParameters = parameters; @@ -38,17 +50,32 @@ public class ImageFilterFx extends ImageFilter { @Override public Bitmap apply(Bitmap bitmap, float scaleFactor, int quality) { - if (getParameters() == null || getParameters().getFxBitmap() ==null) { + if (getParameters() == null || mResources == null) { return bitmap; } int w = bitmap.getWidth(); int h = bitmap.getHeight(); - int fxw = getParameters().getFxBitmap().getWidth(); - int fxh = getParameters().getFxBitmap().getHeight(); + if (mFxBitmap == null || mFxBitmapId != getParameters().getBitmapResource()) { + BitmapFactory.Options o = new BitmapFactory.Options(); + o.inScaled = false; + mFxBitmapId = getParameters().getBitmapResource(); + mFxBitmap = BitmapFactory.decodeResource(mResources, mFxBitmapId, o); + } + + if (mFxBitmap == null) { + return bitmap; + } + + int fxw = mFxBitmap.getWidth(); + int fxh = mFxBitmap.getHeight(); - nativeApplyFilter(bitmap, w, h, getParameters().getFxBitmap(), fxw, fxh); + nativeApplyFilter(bitmap, w, h, mFxBitmap, fxw, fxh); return bitmap; } + + public void setResources(Resources resources) { + mResources = resources; + } } diff --git a/src/com/android/gallery3d/filtershow/presets/ImagePreset.java b/src/com/android/gallery3d/filtershow/presets/ImagePreset.java index 87d9c8183..e27afe580 100644 --- a/src/com/android/gallery3d/filtershow/presets/ImagePreset.java +++ b/src/com/android/gallery3d/filtershow/presets/ImagePreset.java @@ -54,6 +54,7 @@ public class ImagePreset { public final GeometryMetadata mGeoData = new GeometryMetadata(); private boolean mPartialRendering = false; private Rect mPartialRenderingBounds; + private Bitmap mPreviewImage; private FilterEnvironment mEnvironment = new FilterEnvironment(); @@ -90,6 +91,7 @@ public class ImagePreset { mHistoryName = source.name(); mIsFxPreset = source.isFx(); mImageLoader = source.getImageLoader(); + mPreviewImage = source.getPreviewImage(); mGeoData.set(source.mGeoData); } -- 2.11.0