diff options
author | Chris Craik <ccraik@google.com> | 2014-04-23 15:06:06 -0700 |
---|---|---|
committer | Chris Craik <ccraik@google.com> | 2014-04-23 16:52:09 -0700 |
commit | 4eb541aff092a057b27b917f09d33aba226dffed (patch) | |
tree | 52a549817704c6a13d4a76fbe8e9364008e19f79 /framesequence | |
parent | 3105099a73d4fea3408ea0cf6b358fff77dc8b67 (diff) | |
download | android_frameworks_ex-4eb541aff092a057b27b917f09d33aba226dffed.tar.gz android_frameworks_ex-4eb541aff092a057b27b917f09d33aba226dffed.tar.bz2 android_frameworks_ex-4eb541aff092a057b27b917f09d33aba226dffed.zip |
Store BitmapProvider within FrameSequenceDrawable
Change-Id: Ifd10c796cc6107b33e7bc67835e16ad3c0851953
Diffstat (limited to 'framesequence')
3 files changed, 31 insertions, 26 deletions
diff --git a/framesequence/samples/RastermillSamples/src/com/android/rastermill/samples/AnimatedGifTest.java b/framesequence/samples/RastermillSamples/src/com/android/rastermill/samples/AnimatedGifTest.java index 71f35e6..af84a60 100644 --- a/framesequence/samples/RastermillSamples/src/com/android/rastermill/samples/AnimatedGifTest.java +++ b/framesequence/samples/RastermillSamples/src/com/android/rastermill/samples/AnimatedGifTest.java @@ -110,7 +110,7 @@ public class AnimatedGifTest extends Activity { super.onPause(); ImageView imageView = (ImageView) findViewById(R.id.imageview); - mDrawable.recycle(mProvider); + mDrawable.recycle(); if (!mProvider.isEmpty()) throw new IllegalStateException("All bitmaps not recycled"); mDrawable = null; diff --git a/framesequence/src/android/support/rastermill/FrameSequence.java b/framesequence/src/android/support/rastermill/FrameSequence.java index d6bde0f..d2bd128 100644 --- a/framesequence/src/android/support/rastermill/FrameSequence.java +++ b/framesequence/src/android/support/rastermill/FrameSequence.java @@ -103,7 +103,7 @@ public class FrameSequence { * information (in the case of gif, a recall buffer) that will be used to construct * frames based upon data recorded before previousFrameNr. * - * Note: {@link #recycle()} *must* be called before the object is GC'd to free native resources + * Note: {@link #destroy()} *must* be called before the object is GC'd to free native resources * * Note: State holds a native ref to its FrameSequence instance, so its FrameSequence should * remain ref'd while it is in use @@ -115,7 +115,7 @@ public class FrameSequence { mNativeState = nativeState; } - public void recycle() { + public void destroy() { if (mNativeState != 0) { nativeDestroyState(mNativeState); mNativeState = 0; @@ -128,11 +128,9 @@ public class FrameSequence { throw new IllegalArgumentException("Bitmap passed must be non-null and ARGB_8888"); } if (mNativeState == 0) { - throw new IllegalStateException("attempted to draw recycled FrameSequenceState"); + throw new IllegalStateException("attempted to draw destroyed FrameSequenceState"); } return nativeGetFrame(mNativeState, frameNr, output, previousFrameNr); } } - - // TODO: add recycle() cleanup method } diff --git a/framesequence/src/android/support/rastermill/FrameSequenceDrawable.java b/framesequence/src/android/support/rastermill/FrameSequenceDrawable.java index 8f26857..cda0cfe 100644 --- a/framesequence/src/android/support/rastermill/FrameSequenceDrawable.java +++ b/framesequence/src/android/support/rastermill/FrameSequenceDrawable.java @@ -63,6 +63,8 @@ public class FrameSequenceDrawable extends Drawable implements Animatable, Runna /** * Called by FrameSequenceDrawable to release a Bitmap it no longer needs. The Bitmap * will no longer be used at all by the drawable, so it is safe to reuse elsewhere. + * + * This method may be called by FrameSequenceDrawable on any thread. */ public abstract void releaseBitmap(Bitmap bitmap); } @@ -121,7 +123,8 @@ public class FrameSequenceDrawable extends Drawable implements Animatable, Runna //Protects the fields below private final Object mLock = new Object(); - private boolean mRecycled = false; + private final BitmapProvider mBitmapProvider; + private boolean mDestroyed = false; private Bitmap mFrontBitmap; private Bitmap mBackBitmap; @@ -148,7 +151,7 @@ public class FrameSequenceDrawable extends Drawable implements Animatable, Runna int nextFrame; Bitmap bitmap; synchronized (mLock) { - if (mRecycled) return; + if (mDestroyed) return; nextFrame = mNextFrameToDecode; if (nextFrame < 0) { @@ -197,13 +200,14 @@ public class FrameSequenceDrawable extends Drawable implements Animatable, Runna } public FrameSequenceDrawable(FrameSequence frameSequence, BitmapProvider bitmapProvider) { - if (frameSequence == null) throw new IllegalArgumentException(); + if (frameSequence == null || bitmapProvider == null) throw new IllegalArgumentException(); mFrameSequence = frameSequence; mFrameSequenceState = frameSequence.createState(); final int width = frameSequence.getWidth(); final int height = frameSequence.getHeight(); + mBitmapProvider = bitmapProvider; mFrontBitmap = acquireAndValidateBitmap(bitmapProvider, width, height); mBackBitmap = acquireAndValidateBitmap(bitmapProvider, width, height); mSrcRect = new Rect(0, 0, width, height); @@ -217,25 +221,29 @@ public class FrameSequenceDrawable extends Drawable implements Animatable, Runna initializeDecodingThread(); } - private void checkRecycledLocked() { - if (mRecycled) { + private void checkDestroyedLocked() { + if (mDestroyed) { throw new IllegalStateException("Cannot perform operation on recycled drawable"); } } - public boolean isRecycled() { + public boolean isDestroyed() { synchronized (mLock) { - return mRecycled; + return mDestroyed; } } /** * Marks the drawable as permanently recycled (and thus unusable), and releases any owned - * Bitmaps drawable to the BitmapProvider. + * Bitmaps drawable to its BitmapProvider, if attached. * - * @param bitmapProvider Provider to recieve recycled bitmaps. Must be non-null. + * If no BitmapProvider is attached to the drawable, recycle() is called on the Bitmaps. */ - public void recycle(BitmapProvider bitmapProvider) { + public void destroy() { + destroy(mBitmapProvider); + } + + private void destroy(BitmapProvider bitmapProvider) { if (bitmapProvider == null) { throw new IllegalStateException("BitmapProvider must be non-null"); } @@ -243,18 +251,17 @@ public class FrameSequenceDrawable extends Drawable implements Animatable, Runna Bitmap bitmapToReleaseA; Bitmap bitmapToReleaseB; synchronized (mLock) { - checkRecycledLocked(); + checkDestroyedLocked(); bitmapToReleaseA = mFrontBitmap; bitmapToReleaseB = mBackBitmap; mFrontBitmap = null; mBackBitmap = null; - mRecycled = true; + mDestroyed = true; } - // For simplicity and safety, we don't recycle the state object here - + // For simplicity and safety, we don't destroy the state object here bitmapProvider.releaseBitmap(bitmapToReleaseA); bitmapProvider.releaseBitmap(bitmapToReleaseB); } @@ -262,9 +269,9 @@ public class FrameSequenceDrawable extends Drawable implements Animatable, Runna @Override protected void finalize() throws Throwable { try { - mFrameSequenceState.recycle(); - if (!mRecycled) { - recycle(sAllocatingBitmapProvider); + mFrameSequenceState.destroy(); + if (!mDestroyed) { + destroy(); } } finally { super.finalize(); @@ -274,7 +281,7 @@ public class FrameSequenceDrawable extends Drawable implements Animatable, Runna @Override public void draw(Canvas canvas) { synchronized (mLock) { - checkRecycledLocked(); + checkDestroyedLocked(); if (mState == STATE_WAITING_TO_SWAP) { // may have failed to schedule mark ready runnable, // so go ahead and swap if swapping is due @@ -332,7 +339,7 @@ public class FrameSequenceDrawable extends Drawable implements Animatable, Runna public void start() { if (!isRunning()) { synchronized (mLock) { - checkRecycledLocked(); + checkDestroyedLocked(); if (mState == STATE_SCHEDULED) return; // already scheduled mCurrentLoop = 0; scheduleDecodeLocked(); @@ -350,7 +357,7 @@ public class FrameSequenceDrawable extends Drawable implements Animatable, Runna @Override public boolean isRunning() { synchronized (mLock) { - return mNextFrameToDecode > -1 && !mRecycled; + return mNextFrameToDecode > -1 && !mDestroyed; } } |