diff options
author | nicolasroard <nicolasroard@google.com> | 2013-09-08 23:14:57 -0700 |
---|---|---|
committer | nicolasroard <nicolasroard@google.com> | 2013-09-08 23:23:28 -0700 |
commit | ec1e009a7faea0478e361bc2d48d856ab48a0209 (patch) | |
tree | 3898839dddb1b2c14ca2d4bad74cf73addf4cb87 /src/com/android/gallery3d/filtershow/pipeline | |
parent | 34cfdfcd1867fff67fffbbac7a8e942485f08ea1 (diff) | |
download | android_packages_apps_Gallery2-ec1e009a7faea0478e361bc2d48d856ab48a0209.tar.gz android_packages_apps_Gallery2-ec1e009a7faea0478e361bc2d48d856ab48a0209.tar.bz2 android_packages_apps_Gallery2-ec1e009a7faea0478e361bc2d48d856ab48a0209.zip |
Improves memory management
Also adds some debugging tracking tools.
bug:10112287
Change-Id: I3f9b3d173db99818e5c9ae9a62b0ec38cd2b341b
Diffstat (limited to 'src/com/android/gallery3d/filtershow/pipeline')
7 files changed, 49 insertions, 21 deletions
diff --git a/src/com/android/gallery3d/filtershow/pipeline/Buffer.java b/src/com/android/gallery3d/filtershow/pipeline/Buffer.java index f9ba2684b..3b596886f 100644 --- a/src/com/android/gallery3d/filtershow/pipeline/Buffer.java +++ b/src/com/android/gallery3d/filtershow/pipeline/Buffer.java @@ -71,8 +71,9 @@ public class Buffer { public void remove() { BitmapCache cache = MasterImage.getImage().getBitmapCache(); - cache.cache(mBitmap); - mBitmap = null; + if (cache.cache(mBitmap)) { + mBitmap = null; + } } } diff --git a/src/com/android/gallery3d/filtershow/pipeline/CacheProcessing.java b/src/com/android/gallery3d/filtershow/pipeline/CacheProcessing.java index 3d560ef5f..4dad73630 100644 --- a/src/com/android/gallery3d/filtershow/pipeline/CacheProcessing.java +++ b/src/com/android/gallery3d/filtershow/pipeline/CacheProcessing.java @@ -18,6 +18,8 @@ package com.android.gallery3d.filtershow.pipeline; import android.graphics.Bitmap; import android.util.Log; + +import com.android.gallery3d.filtershow.cache.BitmapCache; import com.android.gallery3d.filtershow.filters.FilterRepresentation; import com.android.gallery3d.filtershow.imageshow.GeometryMathUtils; @@ -90,6 +92,7 @@ public class CacheProcessing { public Bitmap apply(FilterEnvironment environment, Bitmap cacheBitmap) { boolean onlyGeometry = true; + Bitmap source = cacheBitmap; for (FilterRepresentation representation : representations) { if (representation.getFilterType() != FilterRepresentation.TYPE_GEOMETRY) { onlyGeometry = false; @@ -107,6 +110,9 @@ public class CacheProcessing { cacheBitmap = environment.applyRepresentation(representation, cacheBitmap); } } + if (cacheBitmap != source) { + environment.cache(source); + } return cacheBitmap; } } @@ -116,7 +122,7 @@ public class CacheProcessing { FilterEnvironment environment) { if (filters.size() == 0) { - return environment.getBitmapCopy(originalBitmap); + return environment.getBitmapCopy(originalBitmap, BitmapCache.PREVIEW_CACHE_NO_FILTERS); } environment.getBimapCache().setCacheProcessing(this); @@ -176,7 +182,8 @@ public class CacheProcessing { int lastPositionCached = -1; for (int i = findBaseImageIndex; i < mSteps.size(); i++) { if (i == -1 || cacheBitmap == null) { - cacheBitmap = environment.getBitmapCopy(originalBitmap); + cacheBitmap = environment.getBitmapCopy(originalBitmap, + BitmapCache.PREVIEW_CACHE_NO_ROOT); originalCopy = cacheBitmap; if (DEBUG) { Log.v(LOGTAG, "i: " + i + " cacheBitmap: " + cacheBitmap + " w: " @@ -194,7 +201,7 @@ public class CacheProcessing { Log.v(LOGTAG, "i: " + i + " get new copy for cacheBitmap " + cacheBitmap + " apply..."); } - cacheBitmap = environment.getBitmapCopy(cacheBitmap); + cacheBitmap = environment.getBitmapCopy(cacheBitmap, BitmapCache.PREVIEW_CACHE); cacheBitmap = step.apply(environment, cacheBitmap); step.cache = cacheBitmap; lastPositionCached = i; @@ -210,8 +217,9 @@ public class CacheProcessing { // Let's see if we can cleanup the cache for unused bitmaps for (int i = 0; i < similarUpToIndex; i++) { CacheStep currentStep = mSteps.elementAt(i); - environment.cache(currentStep.cache); + Bitmap bitmap = currentStep.cache; currentStep.cache = null; + environment.cache(bitmap); } if (DEBUG) { @@ -219,7 +227,12 @@ public class CacheProcessing { displayNbBitmapsInCache(); } if (lastPositionCached != -1) { + Bitmap bitmap = mSteps.elementAt(lastPositionCached).cache; mSteps.elementAt(lastPositionCached).cache = null; + environment.cache(bitmap); + } + if (contains(cacheBitmap)) { + return environment.getBitmapCopy(cacheBitmap, BitmapCache.PREVIEW_CACHE_NO_APPLY); } return cacheBitmap; } diff --git a/src/com/android/gallery3d/filtershow/pipeline/CachingPipeline.java b/src/com/android/gallery3d/filtershow/pipeline/CachingPipeline.java index 4370013bc..2a4b09952 100644 --- a/src/com/android/gallery3d/filtershow/pipeline/CachingPipeline.java +++ b/src/com/android/gallery3d/filtershow/pipeline/CachingPipeline.java @@ -28,6 +28,7 @@ import android.support.v8.renderscript.Allocation; import android.support.v8.renderscript.RenderScript; import android.util.Log; +import com.android.gallery3d.filtershow.cache.BitmapCache; import com.android.gallery3d.filtershow.cache.ImageLoader; import com.android.gallery3d.filtershow.filters.FilterRepresentation; import com.android.gallery3d.filtershow.filters.FiltersManager; @@ -222,7 +223,7 @@ public class CachingPipeline implements PipelineInterface { if (bitmap == null) { return; } - bitmap = mEnvironment.getBitmapCopy(bitmap); + bitmap = mEnvironment.getBitmapCopy(bitmap, BitmapCache.HIGHRES); bitmap = preset.applyGeometry(bitmap, mEnvironment); mEnvironment.setQuality(FilterEnvironment.QUALITY_PREVIEW); @@ -247,7 +248,7 @@ public class CachingPipeline implements PipelineInterface { if (bitmap == null) { return; } - bitmap = mEnvironment.getBitmapCopy(bitmap); + bitmap = mEnvironment.getBitmapCopy(bitmap, BitmapCache.GEOMETRY); bitmap = preset.applyGeometry(bitmap, mEnvironment); if (!mEnvironment.needsStop()) { request.setBitmap(bitmap); @@ -269,7 +270,7 @@ public class CachingPipeline implements PipelineInterface { if (bitmap == null) { return; } - bitmap = mEnvironment.getBitmapCopy(bitmap); + bitmap = mEnvironment.getBitmapCopy(bitmap, BitmapCache.FILTERS); bitmap = preset.apply(bitmap, mEnvironment); if (!mEnvironment.needsStop()) { request.setBitmap(bitmap); @@ -352,7 +353,8 @@ public class CachingPipeline implements PipelineInterface { source = MasterImage.getImage().getLargeThumbnailBitmap(); } if (iconBounds != null) { - bitmap = mEnvironment.getBitmap(iconBounds.width(), iconBounds.height()); + bitmap = mEnvironment.getBitmap(iconBounds.width(), + iconBounds.height(), BitmapCache.ICON); Canvas canvas = new Canvas(bitmap); Matrix m = new Matrix(); float minSize = Math.min(source.getWidth(), source.getHeight()); @@ -364,7 +366,7 @@ public class CachingPipeline implements PipelineInterface { m.postTranslate(dx, dy); canvas.drawBitmap(source, m, new Paint(Paint.FILTER_BITMAP_FLAG)); } else { - bitmap = mEnvironment.getBitmapCopy(source); + bitmap = mEnvironment.getBitmapCopy(source, BitmapCache.ICON); } } Bitmap bmp = preset.apply(bitmap, mEnvironment); @@ -417,6 +419,7 @@ public class CachingPipeline implements PipelineInterface { } setupEnvironment(preset, false); Vector<FilterRepresentation> filters = preset.getFilters(); + buffer.removeProducer(); Bitmap result = mCachedProcessing.process(mOriginalBitmap, filters, mEnvironment); buffer.setProducer(result); } diff --git a/src/com/android/gallery3d/filtershow/pipeline/FilterEnvironment.java b/src/com/android/gallery3d/filtershow/pipeline/FilterEnvironment.java index f356ef26b..ebf83b720 100644 --- a/src/com/android/gallery3d/filtershow/pipeline/FilterEnvironment.java +++ b/src/com/android/gallery3d/filtershow/pipeline/FilterEnvironment.java @@ -67,12 +67,12 @@ public class FilterEnvironment { mBitmapCache.cache(bitmap); } - public Bitmap getBitmap(int w, int h) { - return mBitmapCache.getBitmap(w, h); + public Bitmap getBitmap(int w, int h, int type) { + return mBitmapCache.getBitmap(w, h, type); } - public Bitmap getBitmapCopy(Bitmap source) { - return mBitmapCache.getBitmapCopy(source); + public Bitmap getBitmapCopy(Bitmap source, int type) { + return mBitmapCache.getBitmapCopy(source, type); } public void setImagePreset(ImagePreset imagePreset) { diff --git a/src/com/android/gallery3d/filtershow/pipeline/ImagePreset.java b/src/com/android/gallery3d/filtershow/pipeline/ImagePreset.java index 3f71547dd..b57723a2c 100644 --- a/src/com/android/gallery3d/filtershow/pipeline/ImagePreset.java +++ b/src/com/android/gallery3d/filtershow/pipeline/ImagePreset.java @@ -461,7 +461,12 @@ public class ImagePreset { // Apply any transform -- 90 rotate, flip, straighten, crop // Returns a new bitmap. if (mDoApplyGeometry) { - bitmap = GeometryMathUtils.applyGeometryRepresentations(getGeometryFilters(), bitmap); + Bitmap bmp = GeometryMathUtils.applyGeometryRepresentations( + getGeometryFilters(), bitmap); + if (bmp != bitmap) { + environment.cache(bitmap); + } + return bmp; } return bitmap; } diff --git a/src/com/android/gallery3d/filtershow/pipeline/RenderingRequest.java b/src/com/android/gallery3d/filtershow/pipeline/RenderingRequest.java index 6498c53bf..4cb9e5ad5 100644 --- a/src/com/android/gallery3d/filtershow/pipeline/RenderingRequest.java +++ b/src/com/android/gallery3d/filtershow/pipeline/RenderingRequest.java @@ -21,6 +21,7 @@ import android.graphics.Bitmap; import android.graphics.Rect; import com.android.gallery3d.app.Log; import com.android.gallery3d.filtershow.FilterShowActivity; +import com.android.gallery3d.filtershow.cache.BitmapCache; import com.android.gallery3d.filtershow.filters.FiltersManager; import com.android.gallery3d.filtershow.imageshow.MasterImage; @@ -71,7 +72,7 @@ public class RenderingRequest { } else if (type != PARTIAL_RENDERING && type != HIGHRES_RENDERING && type != GEOMETRY_RENDERING && type != FILTERS_RENDERING) { bitmap = MasterImage.getImage().getBitmapCache().getBitmap( - source.getWidth(), source.getHeight()); + source.getWidth(), source.getHeight(), BitmapCache.RENDERING_REQUEST); } request.setBitmap(bitmap); diff --git a/src/com/android/gallery3d/filtershow/pipeline/SharedBuffer.java b/src/com/android/gallery3d/filtershow/pipeline/SharedBuffer.java index 871e4cd6c..267315ccd 100644 --- a/src/com/android/gallery3d/filtershow/pipeline/SharedBuffer.java +++ b/src/com/android/gallery3d/filtershow/pipeline/SharedBuffer.java @@ -30,15 +30,20 @@ public class SharedBuffer { private volatile boolean mNeedsRepaint = true; public void setProducer(Bitmap producer) { + removeProducer(); + Buffer buffer = new Buffer(producer); + synchronized (this) { + mProducer = buffer; + } + } + + public void removeProducer() { synchronized (this) { if (mProducer != null) { mProducer.remove(); + mProducer = null; } } - Buffer buffer = new Buffer(producer); - synchronized (this) { - mProducer = buffer; - } } public synchronized Buffer getProducer() { |