diff options
author | Jorge Ruesga <jorge@ruesga.com> | 2013-08-19 23:08:06 +0200 |
---|---|---|
committer | Jorge Ruesga <jorge@ruesga.com> | 2013-08-19 23:08:06 +0200 |
commit | 8a28e0b1dd9386c70732860e71d5ee91643d61f8 (patch) | |
tree | f840965e28d7412a7ca04d3f4f95a9e50e037004 /src/org/cyanogenmod/wallpapers/photophase/effects | |
parent | faee676e463486aee0d34c5df8a8b935c5412829 (diff) | |
download | android_packages_wallpapers_PhotoPhase-8a28e0b1dd9386c70732860e71d5ee91643d61f8.tar.gz android_packages_wallpapers_PhotoPhase-8a28e0b1dd9386c70732860e71d5ee91643d61f8.tar.bz2 android_packages_wallpapers_PhotoPhase-8a28e0b1dd9386c70732860e71d5ee91643d61f8.zip |
Fix some memory leaks and improve allocations (#26)
Signed-off-by: Jorge Ruesga <jorge@ruesga.com>
Diffstat (limited to 'src/org/cyanogenmod/wallpapers/photophase/effects')
-rw-r--r-- | src/org/cyanogenmod/wallpapers/photophase/effects/Effects.java | 49 | ||||
-rw-r--r-- | src/org/cyanogenmod/wallpapers/photophase/effects/PhotoPhaseEffect.java | 17 |
2 files changed, 55 insertions, 11 deletions
diff --git a/src/org/cyanogenmod/wallpapers/photophase/effects/Effects.java b/src/org/cyanogenmod/wallpapers/photophase/effects/Effects.java index ef28ee5..0ab3f6c 100644 --- a/src/org/cyanogenmod/wallpapers/photophase/effects/Effects.java +++ b/src/org/cyanogenmod/wallpapers/photophase/effects/Effects.java @@ -25,7 +25,9 @@ import org.cyanogenmod.wallpapers.photophase.preferences.PreferencesProvider.Pre import org.cyanogenmod.wallpapers.photophase.utils.Utils; import java.util.Arrays; +import java.util.HashMap; import java.util.List; +import java.util.Map; /** * A class that manages all the supported effects @@ -153,17 +155,42 @@ public class Effects { } } + private final Map<EFFECTS, Effect> mCachedEffects; + private final EffectContext mEffectContext; + + /** + * Constructor of <code>Effects</code> + * + * @param effectContext The current effect context + */ + public Effects(EffectContext effectContext) { + super(); + mCachedEffects = new HashMap<Effects.EFFECTS, Effect>(); + mEffectContext = effectContext; + } + + /** + * Method that that release the cached data + */ + public void release() { + if (mCachedEffects != null) { + for (Effect effect : mCachedEffects.values()) { + effect.release(); + } + mCachedEffects.clear(); + } + } /** * Method that return the next effect to use with the picture. * - * @param effectContext The android media effects context * @return Effect The next effect to use or null if no need to apply any effect */ @SuppressWarnings("boxing") - public static Effect getNextEffect(EffectContext effectContext) { + public Effect getNextEffect() { // Get a new instance of a effect factory - EffectFactory effectFactory = effectContext.getFactory(); + EffectFactory effectFactory = mEffectContext.getFactory(); + Effect effect = null; // Get an effect based on the user preference List<EFFECTS> effects = Arrays.asList(Preferences.General.Effects.getEffectTypes()); @@ -175,11 +202,19 @@ public class Effects { nextEffect = effects.get(pos); } if (nextEffect == null) { - return null; + if (EffectFactory.isEffectSupported(PhotoPhaseEffectFactory.EFFECT_NULL)) { + effect = effectFactory.createEffect(PhotoPhaseEffectFactory.EFFECT_NULL); + mCachedEffects.put(nextEffect, effect); + } + return effect; + } + + // Has a cached effect? + if (mCachedEffects.containsKey(nextEffect)) { + return mCachedEffects.get(nextEffect); } // Select the effect if is available - Effect effect = null; if (nextEffect.compareTo(EFFECTS.AUTOFIX) == 0) { if (EffectFactory.isEffectSupported(EffectFactory.EFFECT_AUTOFIX)) { effect = effectFactory.createEffect(EffectFactory.EFFECT_AUTOFIX); @@ -293,7 +328,11 @@ public class Effects { // the frames if (effect == null && EffectFactory.isEffectSupported(PhotoPhaseEffectFactory.EFFECT_NULL)) { effect = effectFactory.createEffect(PhotoPhaseEffectFactory.EFFECT_NULL); + nextEffect = EFFECTS.NO_EFFECT; } + + // Cache the effects + mCachedEffects.put(nextEffect, effect); return effect; } } diff --git a/src/org/cyanogenmod/wallpapers/photophase/effects/PhotoPhaseEffect.java b/src/org/cyanogenmod/wallpapers/photophase/effects/PhotoPhaseEffect.java index cdb8b29..7935a79 100644 --- a/src/org/cyanogenmod/wallpapers/photophase/effects/PhotoPhaseEffect.java +++ b/src/org/cyanogenmod/wallpapers/photophase/effects/PhotoPhaseEffect.java @@ -59,6 +59,7 @@ public abstract class PhotoPhaseEffect extends Effect { FloatBuffer mTexVertices; FloatBuffer mPosVertices; + IntBuffer mTexBuffer; /** * An abstract constructor of <code>Effect</code> to follow the rules @@ -120,7 +121,7 @@ public abstract class PhotoPhaseEffect extends Effect { * {@inheritDoc} */ @Override - public final void apply(int inputTexId, int width, int height, int outputTexId) { + public final synchronized void apply(int inputTexId, int width, int height, int outputTexId) { // Create a framebuffer object and call the effect apply method to draw the effect int[] fb = new int[1]; GLES20.glGenFramebuffers(1, fb, 0); @@ -128,18 +129,22 @@ public abstract class PhotoPhaseEffect extends Effect { GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, fb[0]); GLESUtil.glesCheckError("glBindFramebuffer"); - // Render on the whole framebuffer, + // Render on the whole framebuffer GLES20.glViewport(0, 0, width, height); GLESUtil.glesCheckError("glViewport"); - // Create a new output texturure + // Create a new output texture GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, outputTexId); GLESUtil.glesCheckError("glBindTexture"); - IntBuffer texBuffer = IntBuffer.wrap(new int[width * height]); - GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGB, width, height, 0, GLES20.GL_RGB, GLES20.GL_UNSIGNED_BYTE, texBuffer); + int bytes = width * height; + if (mTexBuffer == null || mTexBuffer.capacity() < bytes) { + mTexBuffer = IntBuffer.wrap(new int[bytes]); + } + mTexBuffer.clear(); + GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGB, width, height, 0, GLES20.GL_RGB, GLES20.GL_UNSIGNED_BYTE, mTexBuffer); GLESUtil.glesCheckError("glTexImage2D"); - // Set the + // Set the parameters GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST); GLESUtil.glesCheckError("glTexParameteri"); GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST); |