diff options
Diffstat (limited to 'graphics')
-rw-r--r-- | graphics/java/android/graphics/SurfaceTexture.java | 105 | ||||
-rw-r--r-- | graphics/java/android/graphics/drawable/VectorDrawable.java | 152 |
2 files changed, 58 insertions, 199 deletions
diff --git a/graphics/java/android/graphics/SurfaceTexture.java b/graphics/java/android/graphics/SurfaceTexture.java index d877502..3f8c45c 100644 --- a/graphics/java/android/graphics/SurfaceTexture.java +++ b/graphics/java/android/graphics/SurfaceTexture.java @@ -62,9 +62,8 @@ import android.view.Surface; * #updateTexImage} should not be called directly from the callback. */ public class SurfaceTexture { - - private EventHandler mEventHandler; - private OnFrameAvailableListener mOnFrameAvailableListener; + private final Looper mCreatorLooper; + private Handler mOnFrameAvailableHandler; /** * These fields are used by native code, do not access or modify. @@ -83,7 +82,8 @@ public class SurfaceTexture { /** * Exception thrown when a SurfaceTexture couldn't be created or resized. * - * @deprecated No longer thrown. {@link Surface.OutOfResourcesException} is used instead. + * @deprecated No longer thrown. {@link android.view.Surface.OutOfResourcesException} + * is used instead. */ @SuppressWarnings("serial") @Deprecated @@ -100,10 +100,10 @@ public class SurfaceTexture { * * @param texName the OpenGL texture object name (e.g. generated via glGenTextures) * - * @throws OutOfResourcesException If the SurfaceTexture cannot be created. + * @throws Surface.OutOfResourcesException If the SurfaceTexture cannot be created. */ public SurfaceTexture(int texName) { - init(texName, false); + this(texName, false); } /** @@ -121,20 +121,58 @@ public class SurfaceTexture { * @param texName the OpenGL texture object name (e.g. generated via glGenTextures) * @param singleBufferMode whether the SurfaceTexture will be in single buffered mode. * - * @throws throws OutOfResourcesException If the SurfaceTexture cannot be created. + * @throws Surface.OutOfResourcesException If the SurfaceTexture cannot be created. */ public SurfaceTexture(int texName, boolean singleBufferMode) { - init(texName, singleBufferMode); + mCreatorLooper = Looper.myLooper(); + nativeInit(texName, singleBufferMode, new WeakReference<SurfaceTexture>(this)); } /** * Register a callback to be invoked when a new image frame becomes available to the - * SurfaceTexture. Note that this callback may be called on an arbitrary thread, so it is not + * SurfaceTexture. + * <p> + * This callback may be called on an arbitrary thread, so it is not * safe to call {@link #updateTexImage} without first binding the OpenGL ES context to the * thread invoking the callback. + * </p> + * + * @param listener The listener to set. */ - public void setOnFrameAvailableListener(OnFrameAvailableListener l) { - mOnFrameAvailableListener = l; + public void setOnFrameAvailableListener(OnFrameAvailableListener listener) { + setOnFrameAvailableListener(listener, null); + } + + /** + * Register a callback to be invoked when a new image frame becomes available to the + * SurfaceTexture. + * <p> + * If no handler is specified, then this callback may be called on an arbitrary thread, + * so it is not safe to call {@link #updateTexImage} without first binding the OpenGL ES + * context to the thread invoking the callback. + * </p> + * + * @param listener The listener to set. + * @param handler The handler on which the listener should be invoked, or null + * to use an arbitrary thread. + */ + public void setOnFrameAvailableListener(final OnFrameAvailableListener listener, + Handler handler) { + if (listener != null) { + // Although we claim the thread is arbitrary, earlier implementation would + // prefer to send the callback on the creating looper or the main looper + // so we preserve this behavior here. + Looper looper = handler != null ? handler.getLooper() : + mCreatorLooper != null ? mCreatorLooper : Looper.getMainLooper(); + mOnFrameAvailableHandler = new Handler(looper, null, true /*async*/) { + @Override + public void handleMessage(Message msg) { + listener.onFrameAvailable(SurfaceTexture.this); + } + }; + } else { + mOnFrameAvailableHandler = null; + } } /** @@ -285,49 +323,22 @@ public class SurfaceTexture { } } - private class EventHandler extends Handler { - public EventHandler(Looper looper) { - super(looper); - } - - @Override - public void handleMessage(Message msg) { - if (mOnFrameAvailableListener != null) { - mOnFrameAvailableListener.onFrameAvailable(SurfaceTexture.this); - } - } - } - /** * This method is invoked from native code only. */ @SuppressWarnings({"UnusedDeclaration"}) - private static void postEventFromNative(Object selfRef) { - WeakReference weakSelf = (WeakReference)selfRef; - SurfaceTexture st = (SurfaceTexture)weakSelf.get(); - if (st == null) { - return; - } - - if (st.mEventHandler != null) { - Message m = st.mEventHandler.obtainMessage(); - st.mEventHandler.sendMessage(m); - } - } - - private void init(int texName, boolean singleBufferMode) throws Surface.OutOfResourcesException { - Looper looper; - if ((looper = Looper.myLooper()) != null) { - mEventHandler = new EventHandler(looper); - } else if ((looper = Looper.getMainLooper()) != null) { - mEventHandler = new EventHandler(looper); - } else { - mEventHandler = null; + private static void postEventFromNative(WeakReference<SurfaceTexture> weakSelf) { + SurfaceTexture st = weakSelf.get(); + if (st != null) { + Handler handler = st.mOnFrameAvailableHandler; + if (handler != null) { + handler.sendEmptyMessage(0); + } } - nativeInit(texName, singleBufferMode, new WeakReference<SurfaceTexture>(this)); } - private native void nativeInit(int texName, boolean singleBufferMode, Object weakSelf) + private native void nativeInit(int texName, boolean singleBufferMode, + WeakReference<SurfaceTexture> weakSelf) throws Surface.OutOfResourcesException; private native void nativeFinalize(); private native void nativeGetTransformMatrix(float[] mtx); diff --git a/graphics/java/android/graphics/drawable/VectorDrawable.java b/graphics/java/android/graphics/drawable/VectorDrawable.java index 0992717..77712b6 100644 --- a/graphics/java/android/graphics/drawable/VectorDrawable.java +++ b/graphics/java/android/graphics/drawable/VectorDrawable.java @@ -182,28 +182,14 @@ public class VectorDrawable extends Drawable { public VectorDrawable() { mVectorState = new VectorDrawableState(null); - mVectorState.mBasicAnimator = ObjectAnimator.ofFloat(this, "AnimationFraction", 0, 0); - - setDuration(DEFAULT_DURATION); } private VectorDrawable(VectorDrawableState state, Resources res, Theme theme) { mVectorState = new VectorDrawableState(state); - mVectorState.mBasicAnimator = ObjectAnimator.ofFloat(this, "AnimationFraction", 0, 0); if (theme != null && canApplyTheme()) { applyTheme(theme); } - - long duration = mVectorState.mVAnimatedPath.getTotalAnimationDuration(); - if (duration == -1) { - // If duration is infinite, set to 1 hour. - // TODO: Define correct approach for infinite. - duration = DEFAULT_INFINITE_DURATION; - mVectorState.mBasicAnimator.setFloatValues(0, duration / 1000); - mVectorState.mBasicAnimator.setInterpolator(new LinearInterpolator()); - } - setDuration(duration); } @Override @@ -212,118 +198,6 @@ public class VectorDrawable extends Drawable { } @Override - public void jumpToCurrentState() { - stop(); - } - - /** - * Starts the animation. - */ - public void start() { - mVectorState.mBasicAnimator.start(); - } - - /** - * Stops the animation and moves to the end state. - */ - public void stop() { - mVectorState.mBasicAnimator.end(); - } - - /** - * Returns the current completion value for the animation. - * - * @return the current point on the animation, typically between 0 and 1 - */ - public float geAnimationFraction() { - return mVectorState.mVAnimatedPath.getValue(); - } - - /** - * Set the current completion value for the animation. - * - * @param value the point along the animation, typically between 0 and 1 - */ - public void setAnimationFraction(float value) { - mVectorState.mVAnimatedPath.setAnimationFraction(value); - invalidateSelf(); - } - - /** - * set the amount of time the animation will take - * - * @param duration amount of time in milliseconds - */ - public void setDuration(long duration) { - mVectorState.mBasicAnimator.setDuration(duration); - } - - /** - * Defines what this animation should do when it reaches the end. This - * setting is applied only when the repeat count is either greater than 0 or - * {@link ValueAnimator#INFINITE}. - * - * @param mode the animation mode, either {@link ValueAnimator#RESTART} or - * {@link ValueAnimator#REVERSE} - */ - public void setRepeatMode(int mode) { - mVectorState.mBasicAnimator.setRepeatMode(mode); - } - - /** - * Sets animation to repeat - * - * @param repeat True if this drawable repeats its animation - */ - public void setRepeatCount(int repeat) { - mVectorState.mBasicAnimator.setRepeatCount(repeat); - } - - /** - * @return the animation repeat count, either a value greater than 0 or - * {@link ValueAnimator#INFINITE} - */ - public int getRepeatCount() { - return mVectorState.mBasicAnimator.getRepeatCount(); - } - - @Override - public boolean isStateful() { - return true; - } - - @Override - protected boolean onStateChange(int[] state) { - super.onStateChange(state); - - mVectorState.mVAnimatedPath.setState(state); - - final int direction = mVectorState.mVAnimatedPath.getTrigger(state); - if (direction > 0) { - animateForward(); - } else if (direction < 0) { - animateBackward(); - } - - invalidateSelf(); - return true; - } - - private void animateForward() { - if (!mVectorState.mBasicAnimator.isStarted()) { - mVectorState.mBasicAnimator.setFloatValues(0, 1); - start(); - } - } - - private void animateBackward() { - if (!mVectorState.mBasicAnimator.isStarted()) { - mVectorState.mBasicAnimator.setFloatValues(1, 0); - start(); - } - } - - @Override public void draw(Canvas canvas) { final int saveCount = canvas.save(); final Rect bounds = getBounds(); @@ -432,7 +306,6 @@ public class VectorDrawable extends Drawable { final VectorDrawable drawable = new VectorDrawable(); drawable.inflate(resources, xpp, attrs); - drawable.setAnimationFraction(0); return drawable; } catch (XmlPullParserException e) { @@ -536,35 +409,10 @@ public class VectorDrawable extends Drawable { private void setAnimatedPath(VAnimatedPath animatedPath) { mVectorState.mVAnimatedPath = animatedPath; - - long duration = mVectorState.mVAnimatedPath.getTotalAnimationDuration(); - if (duration == -1) { // if it set to infinite set to 1 hour - duration = DEFAULT_INFINITE_DURATION; // TODO define correct - // approach for infinite - mVectorState.mBasicAnimator.setFloatValues(0, duration / 1000); - mVectorState.mBasicAnimator.setInterpolator(new LinearInterpolator()); - } - - setDuration(duration); - setAnimationFraction(0); - } - - @Override - public boolean setVisible(boolean visible, boolean restart) { - boolean changed = super.setVisible(visible, restart); - if (visible) { - if (changed || restart) { - setAnimationFraction(0); - } - } else { - stop(); - } - return changed; } private static class VectorDrawableState extends ConstantState { int mChangingConfigurations; - ValueAnimator mBasicAnimator; VAnimatedPath mVAnimatedPath; Rect mPadding; |