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 | |
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')
11 files changed, 197 insertions, 107 deletions
diff --git a/src/org/cyanogenmod/wallpapers/photophase/PhotoFrame.java b/src/org/cyanogenmod/wallpapers/photophase/PhotoFrame.java index e1ddea7..78fc265 100644 --- a/src/org/cyanogenmod/wallpapers/photophase/PhotoFrame.java +++ b/src/org/cyanogenmod/wallpapers/photophase/PhotoFrame.java @@ -136,10 +136,16 @@ public class PhotoFrame implements TextureRequestor { */ private void setTextureHandle(GLESTextureInfo ti, final float[] textureCoords) { // Recycle the previous handle - if (mTextureInfo != null && mTextureInfo.handle != 0) { - int[] textures = new int[]{mTextureInfo.handle}; - GLES20.glDeleteTextures(1, textures, 0); - GLESUtil.glesCheckError("glDeleteTextures"); + if (mTextureInfo != null) { + if (GLES20.glIsTexture(mTextureInfo.handle)) { + int[] textures = new int[]{mTextureInfo.handle}; + GLES20.glDeleteTextures(1, textures, 0); + GLESUtil.glesCheckError("glDeleteTextures"); + } + if (mTextureInfo.bitmap != null) { + mTextureInfo.bitmap.recycle(); + mTextureInfo.bitmap = null; + } } // Initialize vertex byte buffer for shape coordinates diff --git a/src/org/cyanogenmod/wallpapers/photophase/PhotoPhaseWallpaper.java b/src/org/cyanogenmod/wallpapers/photophase/PhotoPhaseWallpaper.java index 8385032..625212c 100644 --- a/src/org/cyanogenmod/wallpapers/photophase/PhotoPhaseWallpaper.java +++ b/src/org/cyanogenmod/wallpapers/photophase/PhotoPhaseWallpaper.java @@ -168,7 +168,7 @@ public class PhotoPhaseWallpaper public void onLowMemory() { super.onLowMemory(); Log.i(TAG, "onLowMemory"); - for(PhotoPhaseRenderer renderer : mRenderers) { + for (PhotoPhaseRenderer renderer : mRenderers) { // Pause the wallpaper and destroy the cached textures renderer.onPause(); renderer.onLowMemory(); diff --git a/src/org/cyanogenmod/wallpapers/photophase/TextureManager.java b/src/org/cyanogenmod/wallpapers/photophase/TextureManager.java index 25da19f..700a728 100644 --- a/src/org/cyanogenmod/wallpapers/photophase/TextureManager.java +++ b/src/org/cyanogenmod/wallpapers/photophase/TextureManager.java @@ -52,7 +52,7 @@ public class TextureManager implements OnMediaPictureDiscoveredListener { static final List<GLESTextureInfo> sRecycledBitmaps = new ArrayList<GLESTextureInfo>(); final Context mContext; - final EffectContext mEffectContext; + final Effects mEffects; final Object mSync; final List<TextureRequestor> mPendingRequests; final FixedQueue<GLESTextureInfo> mQueue = new FixedQueue<GLESTextureInfo>(QUEUE_SIZE); @@ -85,15 +85,22 @@ public class TextureManager implements OnMediaPictureDiscoveredListener { public void run() { try { // If we have bitmap to reused then pick up from the recycled list - Effect effect = Effects.getNextEffect(mEffectContext); + Effect effect = mEffects.getNextEffect(); if (sRecycledBitmaps.size() > 0) { // Bind to the GLES context GLESTextureInfo oldTextureInfo = sRecycledBitmaps.remove(0); - ti = GLESUtil.loadTexture(oldTextureInfo.bitmap, effect, mScreenDimensions); - ti.path = oldTextureInfo.path; - oldTextureInfo.bitmap = null; - if (effect != null) { - effect.release(); + try { + ti = GLESUtil.loadTexture(oldTextureInfo.bitmap, effect, mScreenDimensions); + ti.path = oldTextureInfo.path; + } finally { + if (GLES20.glIsTexture(oldTextureInfo.handle)) { + int[] textures = new int[] {oldTextureInfo.handle}; + GLES20.glDeleteTextures(1, textures, 0); + GLESUtil.glesCheckError("glDeleteTextures"); + } + if (oldTextureInfo.bitmap != null) { + oldTextureInfo.bitmap.recycle(); + } } } else { // Load and bind to the GLES context. The effect is applied when the image @@ -114,6 +121,13 @@ public class TextureManager implements OnMediaPictureDiscoveredListener { TextureRequestor requestor = mPendingRequests.remove(0); fixAspectRatio(requestor, ti); requestor.setTextureHandle(ti); + + // Clean up memory + if (ti.bitmap != null) { + ti.bitmap.recycle(); + ti.bitmap = null; + } + } else { // Add to the queue (only valid textures) if (ti.handle > 0) { @@ -147,7 +161,7 @@ public class TextureManager implements OnMediaPictureDiscoveredListener { GLESSurfaceDispatcher dispatcher, int requestors, Rect screenDimensions) { super(); mContext = ctx; - mEffectContext = effectCtx; + mEffects = new Effects(effectCtx); mDispatcher = dispatcher; mScreenDimensions = screenDimensions; mDimensions = screenDimensions; // For now, use the screen dimensions as the preferred dimensions for bitmaps @@ -236,6 +250,13 @@ public class TextureManager implements OnMediaPictureDiscoveredListener { GLESTextureInfo ti = mQueue.remove(); fixAspectRatio(requestor, ti); requestor.setTextureHandle(ti); + + // Clean up memory + if (ti.bitmap != null) { + ti.bitmap.recycle(); + ti.bitmap = null; + } + } catch (EmptyQueueException eqex) { // Add to queue of pending request to be notified when // we have a new bitmap in the queue @@ -370,6 +391,7 @@ public class TextureManager implements OnMediaPictureDiscoveredListener { public void recycle() { // Destroy the media discovery task mPictureDiscoverer.recycle(); + mEffects.release(); // Destroy the background task if (mBackgroundTask != null) { @@ -405,7 +427,7 @@ public class TextureManager implements OnMediaPictureDiscoveredListener { } /** - * Methdo that fix the aspect ratio of a image to fit the destination target + * Method that fix the aspect ratio of a image to fit the destination target * * @param request The requestor target * @param ti The original texture information @@ -428,15 +450,15 @@ public class TextureManager implements OnMediaPictureDiscoveredListener { pixels.width(), pixels.height(), ThumbnailUtils.OPTIONS_RECYCLE_INPUT); - GLESTextureInfo dst = GLESUtil.loadTexture( - thumb, ti.effect == null ? null : ti.effect, mScreenDimensions); + GLESTextureInfo dst = GLESUtil.loadTexture(thumb, ti.effect, mScreenDimensions); // Destroy references int[] textures = new int[]{ti.handle}; GLES20.glDeleteTextures(1, textures, 0); GLESUtil.glesCheckError("glDeleteTextures"); - if (ti.effect != null) { - ti.effect.release(); + if (ti.bitmap != null) { + ti.bitmap.recycle(); + ti.bitmap = null; } // Swap references 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); diff --git a/src/org/cyanogenmod/wallpapers/photophase/shapes/OopsShape.java b/src/org/cyanogenmod/wallpapers/photophase/shapes/OopsShape.java index e657fe4..838acd9 100644 --- a/src/org/cyanogenmod/wallpapers/photophase/shapes/OopsShape.java +++ b/src/org/cyanogenmod/wallpapers/photophase/shapes/OopsShape.java @@ -136,8 +136,10 @@ public class OopsShape implements DrawableShape { mMessage = ctx.getString(resourceMessageId); // Load the textures - mOopsImageTexture = GLESUtil.loadTexture(ctx, R.drawable.bg_cid_oops, null, null, false); - mOopsTextTexture = GLESUtil.loadTexture(text2Bitmap(ctx, mMessage), null, null); + mOopsImageTexture = GLESUtil.loadTexture(ctx, R.drawable.bg_cid_oops, null, null, true); + Bitmap textBitmap = text2Bitmap(ctx, mMessage); + mOopsTextTexture = GLESUtil.loadTexture(textBitmap, null, null); + textBitmap.recycle(); } /** diff --git a/src/org/cyanogenmod/wallpapers/photophase/transitions/CubeTransition.java b/src/org/cyanogenmod/wallpapers/photophase/transitions/CubeTransition.java index d603a72..bfe300b 100644 --- a/src/org/cyanogenmod/wallpapers/photophase/transitions/CubeTransition.java +++ b/src/org/cyanogenmod/wallpapers/photophase/transitions/CubeTransition.java @@ -67,6 +67,10 @@ public class CubeTransition extends Transition { private boolean mRunning; private long mTime; + private FloatBuffer mPositionBuffer; + private float[] mTranslationMatrix; + private float[] mVertex; + private float mAmount; /** @@ -79,6 +83,7 @@ public class CubeTransition extends Transition { super(ctx, tm, VERTEX_SHADER, FRAGMENT_SHADER); // Initialized + mTranslationMatrix = new float[16]; reset(); } @@ -114,6 +119,17 @@ public class CubeTransition extends Transition { super.select(target); mAmount = getAmount(); + // Create the interal buffer + float[] vertex = target.getFrameVertex(); + if (mPositionBuffer == null) { + ByteBuffer bb = ByteBuffer.allocateDirect(vertex.length * 4); // (# of coordinate values * 4 bytes per float) + bb.order(ByteOrder.nativeOrder()); + mPositionBuffer = bb.asFloatBuffer(); + } + if (mVertex == null) { + mVertex = new float[vertex.length]; + } + // Random mode List<WINDOW_MODES> modes = new ArrayList<CubeTransition.WINDOW_MODES>( @@ -215,33 +231,31 @@ public class CubeTransition extends Transition { GLESUtil.glesCheckError("glEnableVertexAttribArray"); // Position - float[] vertex = cloneVertex(); + setInternalVertex(); float interpolation = delta > 0.5f ? 1 - delta : delta; - float w = Math.abs(vertex[6] - vertex[4]); + float w = Math.abs(mVertex[6] - mVertex[4]); switch (mMode) { case RIGHT_TO_LEFT: - vertex[1] -= interpolation * mAmount; - vertex[5] += interpolation * mAmount; - vertex[4] += w * delta; - vertex[0] = vertex[4]; + mVertex[1] -= interpolation * mAmount; + mVertex[5] += interpolation * mAmount; + mVertex[4] += w * delta; + mVertex[0] = mVertex[4]; break; case LEFT_TO_RIGHT: - vertex[3] -= interpolation * mAmount; - vertex[7] += interpolation * mAmount; - vertex[6] -= w * delta; - vertex[2] = vertex[6]; + mVertex[3] -= interpolation * mAmount; + mVertex[7] += interpolation * mAmount; + mVertex[6] -= w * delta; + mVertex[2] = mVertex[6]; break; default: break; } - ByteBuffer bb = ByteBuffer.allocateDirect(vertex.length * 4); // (# of coordinate values * 4 bytes per float) - bb.order(ByteOrder.nativeOrder()); - FloatBuffer positionBuffer = bb.asFloatBuffer(); - positionBuffer.put(vertex); - positionBuffer.position(0); - GLES20.glVertexAttribPointer(mPositionHandlers[0], 2, GLES20.GL_FLOAT, false, 0, positionBuffer); + mPositionBuffer.position(0); + mPositionBuffer.put(mVertex); + mPositionBuffer.position(0); + GLES20.glVertexAttribPointer(mPositionHandlers[0], 2, GLES20.GL_FLOAT, false, 0, mPositionBuffer); GLESUtil.glesCheckError("glVertexAttribPointer"); GLES20.glEnableVertexAttribArray(mPositionHandlers[0]); GLESUtil.glesCheckError("glEnableVertexAttribArray"); @@ -254,12 +268,12 @@ public class CubeTransition extends Transition { case RIGHT_TO_LEFT: angle = delta * 90; rotateY = -1.0f; - translateX = vertex[2] * -1; + translateX = mVertex[2] * -1; break; case LEFT_TO_RIGHT: angle = delta * -90; rotateY = -1.0f; - translateX = vertex[0] * -1; + translateX = mVertex[0] * -1; break; default: @@ -267,12 +281,11 @@ public class CubeTransition extends Transition { } // Apply the projection and view transformation - float[] translationMatrix = new float[16]; Matrix.setIdentityM(matrix, 0); - Matrix.translateM(translationMatrix, 0, matrix, 0, -translateX, 0.0f, 0.0f); - Matrix.rotateM(translationMatrix, 0, translationMatrix, 0, angle, 0.0f, rotateY, 0.0f); - Matrix.translateM(translationMatrix, 0, translationMatrix, 0, translateX, 0.0f, 0.0f); - GLES20.glUniformMatrix4fv(mMVPMatrixHandlers[0], 1, false, translationMatrix, 0); + Matrix.translateM(mTranslationMatrix, 0, matrix, 0, -translateX, 0.0f, 0.0f); + Matrix.rotateM(mTranslationMatrix, 0, mTranslationMatrix, 0, angle, 0.0f, rotateY, 0.0f); + Matrix.translateM(mTranslationMatrix, 0, mTranslationMatrix, 0, translateX, 0.0f, 0.0f); + GLES20.glUniformMatrix4fv(mMVPMatrixHandlers[0], 1, false, mTranslationMatrix, 0); GLESUtil.glesCheckError("glUniformMatrix4fv"); // Draw @@ -322,33 +335,31 @@ public class CubeTransition extends Transition { GLESUtil.glesCheckError("glEnableVertexAttribArray"); // Position - float[] vertex = cloneVertex(); + setInternalVertex(); float interpolation = delta > 0.5f ? 1 - delta : delta; - float w = Math.abs(vertex[6] - vertex[4]); + float w = Math.abs(mVertex[6] - mVertex[4]); switch (mMode) { case LEFT_TO_RIGHT: - vertex[1] -= interpolation * mAmount; - vertex[5] += interpolation * mAmount; - vertex[4] += w * (1 - delta); - vertex[0] = vertex[4]; + mVertex[1] -= interpolation * mAmount; + mVertex[5] += interpolation * mAmount; + mVertex[4] += w * (1 - delta); + mVertex[0] = mVertex[4]; break; case RIGHT_TO_LEFT: - vertex[3] -= interpolation * mAmount; - vertex[7] += interpolation * mAmount; - vertex[6] -= w * (1 - delta); - vertex[2] = vertex[6]; + mVertex[3] -= interpolation * mAmount; + mVertex[7] += interpolation * mAmount; + mVertex[6] -= w * (1 - delta); + mVertex[2] = mVertex[6]; break; default: break; } - ByteBuffer bb = ByteBuffer.allocateDirect(vertex.length * 4); // (# of coordinate values * 4 bytes per float) - bb.order(ByteOrder.nativeOrder()); - FloatBuffer positionBuffer = bb.asFloatBuffer(); - positionBuffer.put(vertex); - positionBuffer.position(0); - GLES20.glVertexAttribPointer(mPositionHandlers[1], 2, GLES20.GL_FLOAT, false, 0, positionBuffer); + mPositionBuffer.position(0); + mPositionBuffer.put(mVertex); + mPositionBuffer.position(0); + GLES20.glVertexAttribPointer(mPositionHandlers[1], 2, GLES20.GL_FLOAT, false, 0, mPositionBuffer); GLESUtil.glesCheckError("glVertexAttribPointer"); GLES20.glEnableVertexAttribArray(mPositionHandlers[1]); GLESUtil.glesCheckError("glEnableVertexAttribArray"); @@ -361,12 +372,12 @@ public class CubeTransition extends Transition { case LEFT_TO_RIGHT: angle = 90 - (delta * 90); rotateY = -1.0f; - translateX = vertex[2] * -1; + translateX = mVertex[2] * -1; break; case RIGHT_TO_LEFT: angle = -90 + (delta * 90); rotateY = -1.0f; - translateX = vertex[0] * -1; + translateX = mVertex[0] * -1; break; default: @@ -374,12 +385,11 @@ public class CubeTransition extends Transition { } // Apply the projection and view transformation - float[] translationMatrix = new float[16]; Matrix.setIdentityM(matrix, 0); - Matrix.translateM(translationMatrix, 0, matrix, 0, -translateX, 0.0f, 0.0f); - Matrix.rotateM(translationMatrix, 0, translationMatrix, 0, angle, 0.0f, rotateY, 0.0f); - Matrix.translateM(translationMatrix, 0, translationMatrix, 0, translateX, 0.0f, 0.0f); - GLES20.glUniformMatrix4fv(mMVPMatrixHandlers[1], 1, false, translationMatrix, 0); + Matrix.translateM(mTranslationMatrix, 0, matrix, 0, -translateX, 0.0f, 0.0f); + Matrix.rotateM(mTranslationMatrix, 0, mTranslationMatrix, 0, angle, 0.0f, rotateY, 0.0f); + Matrix.translateM(mTranslationMatrix, 0, mTranslationMatrix, 0, translateX, 0.0f, 0.0f); + GLES20.glUniformMatrix4fv(mMVPMatrixHandlers[1], 1, false, mTranslationMatrix, 0); GLESUtil.glesCheckError("glUniformMatrix4fv"); // Draw @@ -451,15 +461,11 @@ public class CubeTransition extends Transition { } /** - * Method that copy the vertex array - * - * @return The copy of the vertex + * Method that prepares the internal vertex array */ - private float[] cloneVertex() { + private void setInternalVertex() { float[] originalVertex = mTarget.getFrameVertex(); - float[] vertex = new float[originalVertex.length]; - System.arraycopy(originalVertex, 0, vertex, 0, originalVertex.length); - return vertex; + System.arraycopy(originalVertex, 0, mVertex, 0, originalVertex.length); } /** diff --git a/src/org/cyanogenmod/wallpapers/photophase/transitions/FlipTransition.java b/src/org/cyanogenmod/wallpapers/photophase/transitions/FlipTransition.java index 295add9..8d0bf79 100644 --- a/src/org/cyanogenmod/wallpapers/photophase/transitions/FlipTransition.java +++ b/src/org/cyanogenmod/wallpapers/photophase/transitions/FlipTransition.java @@ -57,6 +57,8 @@ public class FlipTransition extends Transition { private FLIP_MODES mMode; + private float[] mTranslationMatrix; + private boolean mRunning; private long mTime; @@ -70,6 +72,7 @@ public class FlipTransition extends Transition { super(ctx, tm, VERTEX_SHADER, FRAGMENT_SHADER); // Initialized + mTranslationMatrix = new float[16]; reset(); } @@ -233,12 +236,11 @@ public class FlipTransition extends Transition { } // Apply the projection and view transformation - float[] translationMatrix = new float[16]; Matrix.setIdentityM(matrix, 0); - Matrix.translateM(translationMatrix, 0, matrix, 0, -translateX, -translateY, 0.0f); - Matrix.rotateM(translationMatrix, 0, translationMatrix, 0, angle, rotateX, rotateY, 0.0f); - Matrix.translateM(translationMatrix, 0, translationMatrix, 0, translateX, translateY, 0.0f); - GLES20.glUniformMatrix4fv(mMVPMatrixHandlers[index], 1, false, translationMatrix, 0); + Matrix.translateM(mTranslationMatrix, 0, matrix, 0, -translateX, -translateY, 0.0f); + Matrix.rotateM(mTranslationMatrix, 0, mTranslationMatrix, 0, angle, rotateX, rotateY, 0.0f); + Matrix.translateM(mTranslationMatrix, 0, mTranslationMatrix, 0, translateX, translateY, 0.0f); + GLES20.glUniformMatrix4fv(mMVPMatrixHandlers[index], 1, false, mTranslationMatrix, 0); GLESUtil.glesCheckError("glUniformMatrix4fv"); // Draw diff --git a/src/org/cyanogenmod/wallpapers/photophase/transitions/Transition.java b/src/org/cyanogenmod/wallpapers/photophase/transitions/Transition.java index da8b1b0..cdd5438 100644 --- a/src/org/cyanogenmod/wallpapers/photophase/transitions/Transition.java +++ b/src/org/cyanogenmod/wallpapers/photophase/transitions/Transition.java @@ -182,7 +182,7 @@ public abstract class Transition { createProgram(index); } GLES20.glUseProgram(mProgramHandlers[index]); - GLESUtil.glesCheckError("glUseProgram(" + index + ")"); + GLESUtil.glesCheckError("glUseProgram()"); } /** @@ -195,11 +195,11 @@ public abstract class Transition { GLES20.glDeleteProgram(mProgramHandlers[i]); GLESUtil.glesCheckError("glDeleteProgram"); } - mProgramHandlers[i] = 0; - mTextureHandlers[i] = 0; - mPositionHandlers[i] = 0; - mTextureCoordHandlers[i] = 0; - mMVPMatrixHandlers[i] = 0; + mProgramHandlers[i] = -1; + mTextureHandlers[i] = -1; + mPositionHandlers[i] = -1; + mTextureCoordHandlers[i] = -1; + mMVPMatrixHandlers[i] = -1; } mTransitionTarget = null; mTarget = null; diff --git a/src/org/cyanogenmod/wallpapers/photophase/transitions/WindowTransition.java b/src/org/cyanogenmod/wallpapers/photophase/transitions/WindowTransition.java index d580c8b..b09cfaa 100644 --- a/src/org/cyanogenmod/wallpapers/photophase/transitions/WindowTransition.java +++ b/src/org/cyanogenmod/wallpapers/photophase/transitions/WindowTransition.java @@ -65,6 +65,8 @@ public class WindowTransition extends Transition { private WINDOW_MODES mMode; + private float[] mTranslationMatrix; + private boolean mRunning; private long mTime; @@ -81,6 +83,7 @@ public class WindowTransition extends Transition { super(ctx, tm, VERTEX_SHADER, FRAGMENT_SHADER); // Initialized + mTranslationMatrix = new float[16]; reset(); } @@ -275,12 +278,11 @@ public class WindowTransition extends Transition { } // Apply the projection and view transformation - float[] translationMatrix = new float[16]; Matrix.setIdentityM(matrix, 0); - Matrix.translateM(translationMatrix, 0, matrix, 0, -translateX, 0.0f, 0.0f); - Matrix.rotateM(translationMatrix, 0, translationMatrix, 0, angle, 0.0f, rotateY, 0.0f); - Matrix.translateM(translationMatrix, 0, translationMatrix, 0, translateX, 0.0f, 0.0f); - GLES20.glUniformMatrix4fv(mMVPMatrixHandlers[0], 1, false, translationMatrix, 0); + Matrix.translateM(mTranslationMatrix, 0, matrix, 0, -translateX, 0.0f, 0.0f); + Matrix.rotateM(mTranslationMatrix, 0, mTranslationMatrix, 0, angle, 0.0f, rotateY, 0.0f); + Matrix.translateM(mTranslationMatrix, 0, mTranslationMatrix, 0, translateX, 0.0f, 0.0f); + GLES20.glUniformMatrix4fv(mMVPMatrixHandlers[0], 1, false, mTranslationMatrix, 0); GLESUtil.glesCheckError("glUniformMatrix4fv"); // Draw diff --git a/src/org/cyanogenmod/wallpapers/photophase/utils/GLESUtil.java b/src/org/cyanogenmod/wallpapers/photophase/utils/GLESUtil.java index d2bd46e..cc1073c 100644 --- a/src/org/cyanogenmod/wallpapers/photophase/utils/GLESUtil.java +++ b/src/org/cyanogenmod/wallpapers/photophase/utils/GLESUtil.java @@ -29,6 +29,8 @@ import android.util.Log; import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.Reader; /** @@ -40,6 +42,8 @@ public final class GLESUtil { private static final boolean DEBUG = false; + private static final Object sSync = new Object(); + /** * A helper class to deal with OpenGL float colors. */ @@ -438,8 +442,10 @@ public final class GLESUtil { // Has a effect? int handle = textureNames[0]; if (effect != null) { - // Apply the effect - effect.apply(textureNames[0], screenDim.width(), screenDim.height(), textureNames[1]); + // Apply the effect (we need a thread-safe call here) + synchronized (sSync) { + effect.apply(textureNames[0], screenDim.width(), screenDim.height(), textureNames[1]); + } handle = textureNames[1]; // Delete the unused texture @@ -495,14 +501,14 @@ public final class GLESUtil { * @throws IOException If an error occurs while loading the resource */ private static String readResource(Resources res, int resId) { - InputStream is = res.openRawResource(resId); + Reader reader = new InputStreamReader(res.openRawResource(resId)); try { final int BUFFER = 1024; - byte[] data = new byte[BUFFER]; + char[] data = new char[BUFFER]; int read = 0; StringBuilder sb = new StringBuilder(); - while ((read = is.read(data, 0, BUFFER)) != -1) { - sb.append(new String(data, 0 ,read)); + while ((read = reader.read(data, 0, BUFFER)) != -1) { + sb.append(data, 0, read); } return sb.toString(); } catch (Exception e) { @@ -510,7 +516,7 @@ public final class GLESUtil { return null; } finally { try { - is.close(); + reader.close(); } catch (Exception ex) { // Ignore } |