summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChih-Chung Chang <chihchung@google.com>2012-02-17 10:02:27 +0800
committerChih-Chung Chang <chihchung@google.com>2012-02-17 10:04:10 +0800
commitb3d01963463a11d27c286ffbf7f715f59bbecf88 (patch)
tree66f12741bf9b68bb0283067998bb264a31789042
parented0e84d75ed4f75ac9589bb3ea04b7327f8745f2 (diff)
downloadandroid_packages_apps_Snap-b3d01963463a11d27c286ffbf7f715f59bbecf88.tar.gz
android_packages_apps_Snap-b3d01963463a11d27c286ffbf7f715f59bbecf88.tar.bz2
android_packages_apps_Snap-b3d01963463a11d27c286ffbf7f715f59bbecf88.zip
Cache animation time value to reduce work and synchronize animation.
Change-Id: I00081bc6037c01dafc56cf017dcc1da448c1d106
-rw-r--r--src/com/android/gallery3d/ui/AdaptiveBackground.java3
-rw-r--r--src/com/android/gallery3d/ui/AnimationTime.java33
-rw-r--r--src/com/android/gallery3d/ui/CropView.java2
-rw-r--r--src/com/android/gallery3d/ui/EdgeEffect.java13
-rw-r--r--src/com/android/gallery3d/ui/GLCanvas.java6
-rw-r--r--src/com/android/gallery3d/ui/GLCanvasImpl.java11
-rw-r--r--src/com/android/gallery3d/ui/GLRootView.java10
-rw-r--r--src/com/android/gallery3d/ui/GLView.java2
-rw-r--r--src/com/android/gallery3d/ui/ProgressSpinner.java2
-rw-r--r--src/com/android/gallery3d/ui/SlideshowView.java8
-rw-r--r--src/com/android/gallery3d/ui/SlotView.java6
-rw-r--r--tests/src/com/android/gallery3d/ui/GLCanvasTest.java20
12 files changed, 56 insertions, 60 deletions
diff --git a/src/com/android/gallery3d/ui/AdaptiveBackground.java b/src/com/android/gallery3d/ui/AdaptiveBackground.java
index 42cb2ccdb..d7c99063d 100644
--- a/src/com/android/gallery3d/ui/AdaptiveBackground.java
+++ b/src/com/android/gallery3d/ui/AdaptiveBackground.java
@@ -110,8 +110,7 @@ public class AdaptiveBackground extends GLView {
mBackground.draw(canvas, i - scroll, 0, width, height);
}
} else {
- boolean moreAnimation =
- mAnimation.calculate(canvas.currentAnimationTimeMillis());
+ boolean moreAnimation = mAnimation.calculate(AnimationTime.get());
float ratio = mAnimation.get();
for (int i = start, n = scroll + getWidth(); i < n; i += width) {
canvas.drawMixed(mOldBackground,
diff --git a/src/com/android/gallery3d/ui/AnimationTime.java b/src/com/android/gallery3d/ui/AnimationTime.java
new file mode 100644
index 000000000..793e8b607
--- /dev/null
+++ b/src/com/android/gallery3d/ui/AnimationTime.java
@@ -0,0 +1,33 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.gallery3d.ui;
+
+import android.os.SystemClock;
+
+public class AnimationTime {
+ private static volatile long sTime;
+
+ // Sets current time as the animation time.
+ public static void setNow() {
+ sTime = SystemClock.uptimeMillis();
+ }
+
+ // Returns the animation time.
+ public static long get() {
+ return sTime;
+ }
+}
diff --git a/src/com/android/gallery3d/ui/CropView.java b/src/com/android/gallery3d/ui/CropView.java
index 227e67e11..95080063a 100644
--- a/src/com/android/gallery3d/ui/CropView.java
+++ b/src/com/android/gallery3d/ui/CropView.java
@@ -167,7 +167,7 @@ public class CropView extends GLView {
@Override
public void render(GLCanvas canvas) {
AnimationController a = mAnimation;
- if (a.calculate(canvas.currentAnimationTimeMillis())) invalidate();
+ if (a.calculate(AnimationTime.get())) invalidate();
setImageViewPosition(a.getCenterX(), a.getCenterY(), a.getScale());
super.render(canvas);
}
diff --git a/src/com/android/gallery3d/ui/EdgeEffect.java b/src/com/android/gallery3d/ui/EdgeEffect.java
index b2d83f5ba..c6c70ceac 100644
--- a/src/com/android/gallery3d/ui/EdgeEffect.java
+++ b/src/com/android/gallery3d/ui/EdgeEffect.java
@@ -20,7 +20,6 @@ import com.android.gallery3d.R;
import android.content.Context;
import android.graphics.Rect;
-import android.view.animation.AnimationUtils;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.Interpolator;
@@ -179,7 +178,7 @@ public class EdgeEffect {
* back toward the edge reached to initiate the effect.
*/
public void onPull(float deltaDistance) {
- final long now = AnimationUtils.currentAnimationTimeMillis();
+ final long now = AnimationTime.get();
if (mState == STATE_PULL_DECAY && now - mStartTime < mDuration) {
return;
}
@@ -244,7 +243,7 @@ public class EdgeEffect {
mGlowAlphaFinish = 0.f;
mGlowScaleYFinish = 0.f;
- mStartTime = AnimationUtils.currentAnimationTimeMillis();
+ mStartTime = AnimationTime.get();
mDuration = RECEDE_TIME;
}
@@ -262,7 +261,7 @@ public class EdgeEffect {
mState = STATE_ABSORB;
velocity = Math.max(MIN_VELOCITY, Math.abs(velocity));
- mStartTime = AnimationUtils.currentAnimationTimeMillis();
+ mStartTime = AnimationTime.get();
mDuration = 0.1f + (velocity * 0.03f);
// The edge should always be at least partially visible, regardless
@@ -343,7 +342,7 @@ public class EdgeEffect {
}
private void update() {
- final long time = AnimationUtils.currentAnimationTimeMillis();
+ final long time = AnimationTime.get();
final float t = Math.min((time - mStartTime) / mDuration, 1.f);
final float interp = mInterpolator.getInterpolation(t);
@@ -357,7 +356,7 @@ public class EdgeEffect {
switch (mState) {
case STATE_ABSORB:
mState = STATE_RECEDE;
- mStartTime = AnimationUtils.currentAnimationTimeMillis();
+ mStartTime = AnimationTime.get();
mDuration = RECEDE_TIME;
mEdgeAlphaStart = mEdgeAlpha;
@@ -373,7 +372,7 @@ public class EdgeEffect {
break;
case STATE_PULL:
mState = STATE_PULL_DECAY;
- mStartTime = AnimationUtils.currentAnimationTimeMillis();
+ mStartTime = AnimationTime.get();
mDuration = PULL_DECAY_TIME;
mEdgeAlphaStart = mEdgeAlpha;
diff --git a/src/com/android/gallery3d/ui/GLCanvas.java b/src/com/android/gallery3d/ui/GLCanvas.java
index fea95092e..eb78eddde 100644
--- a/src/com/android/gallery3d/ui/GLCanvas.java
+++ b/src/com/android/gallery3d/ui/GLCanvas.java
@@ -37,12 +37,6 @@ public interface GLCanvas {
// Clear the drawing buffers. This should only be used by GLRoot.
public void clearBuffer();
- // This is the time value used to calculate the animation in the current
- // frame. The "set" function should only called by GLRoot, and the
- // "time" parameter must be nonnegative.
- public void setCurrentAnimationTimeMillis(long time);
- public long currentAnimationTimeMillis();
-
public void setBlendEnabled(boolean enabled);
// Sets and gets the current alpha, alpha must be in [0, 1].
diff --git a/src/com/android/gallery3d/ui/GLCanvasImpl.java b/src/com/android/gallery3d/ui/GLCanvasImpl.java
index b8961f9d1..85bf7716d 100644
--- a/src/com/android/gallery3d/ui/GLCanvasImpl.java
+++ b/src/com/android/gallery3d/ui/GLCanvasImpl.java
@@ -61,8 +61,6 @@ public class GLCanvasImpl implements GLCanvas {
private final GLState mGLState;
- private long mAnimationTime;
-
private float mAlpha;
private final Rect mClipRect = new Rect();
private final Stack<ConfigState> mRestoreStack =
@@ -112,10 +110,6 @@ public class GLCanvasImpl implements GLCanvas {
gl.glScissor(0, 0, width, height);
}
- public long currentAnimationTimeMillis() {
- return mAnimationTime;
- }
-
public void setAlpha(float alpha) {
Utils.assertTrue(alpha >= 0 && alpha <= 1);
mAlpha = alpha;
@@ -780,11 +774,6 @@ public class GLCanvasImpl implements GLCanvas {
return mGL;
}
- public void setCurrentAnimationTimeMillis(long time) {
- Utils.assertTrue(time >= 0);
- mAnimationTime = time;
- }
-
public void clearBuffer() {
mGL.glClear(GL10.GL_COLOR_BUFFER_BIT);
}
diff --git a/src/com/android/gallery3d/ui/GLRootView.java b/src/com/android/gallery3d/ui/GLRootView.java
index 27bc88539..d11de1229 100644
--- a/src/com/android/gallery3d/ui/GLRootView.java
+++ b/src/com/android/gallery3d/ui/GLRootView.java
@@ -61,6 +61,7 @@ public class GLRootView extends GLSurfaceView
private static final boolean DEBUG_DRAWING_STAT = false;
private static final boolean DEBUG_PROFILE = false;
+ private static final boolean DEBUG_PROFILE_SLOW_ONLY = false;
private static final int FLAG_INITIALIZED = 1;
private static final int FLAG_NEED_LAYOUT = 2;
@@ -104,6 +105,7 @@ public class GLRootView extends GLSurfaceView
setEGLConfigChooser(mEglConfigChooser);
setRenderer(this);
getHolder().setFormat(PixelFormat.RGB_565);
+ AnimationTime.setNow();
// Uncomment this to enable gl error check.
//setDebugFlags(DEBUG_CHECK_GL_ERROR);
@@ -269,7 +271,7 @@ public class GLRootView extends GLSurfaceView
@Override
public void onDrawFrame(GL10 gl) {
long t0;
- if (DEBUG_PROFILE) {
+ if (DEBUG_PROFILE_SLOW_ONLY) {
Profile.hold();
t0 = System.nanoTime();
}
@@ -279,7 +281,7 @@ public class GLRootView extends GLSurfaceView
} finally {
mRenderLock.unlock();
}
- if (DEBUG_PROFILE) {
+ if (DEBUG_PROFILE_SLOW_ONLY) {
long t = System.nanoTime();
long durationInMs = (t - mLastDrawFinishTime) / 1000000;
long durationDrawInMs = (t - t0) / 1000000;
@@ -317,13 +319,13 @@ public class GLRootView extends GLSurfaceView
gl.glScissor(clip.left, clip.top, clip.width(), clip.height());
}
- mCanvas.setCurrentAnimationTimeMillis(SystemClock.uptimeMillis());
+ AnimationTime.setNow();
if (mContentView != null) {
mContentView.render(mCanvas);
}
if (!mAnimations.isEmpty()) {
- long now = SystemClock.uptimeMillis();
+ long now = AnimationTime.get();
for (int i = 0, n = mAnimations.size(); i < n; i++) {
mAnimations.get(i).setStartTime(now);
}
diff --git a/src/com/android/gallery3d/ui/GLView.java b/src/com/android/gallery3d/ui/GLView.java
index b4af9bcdd..7ae980d06 100644
--- a/src/com/android/gallery3d/ui/GLView.java
+++ b/src/com/android/gallery3d/ui/GLView.java
@@ -234,7 +234,7 @@ public class GLView {
CanvasAnimation anim = component.mAnimation;
if (anim != null) {
canvas.save(anim.getCanvasSaveFlags());
- if (anim.calculate(canvas.currentAnimationTimeMillis())) {
+ if (anim.calculate(AnimationTime.get())) {
invalidate();
} else {
component.mAnimation = null;
diff --git a/src/com/android/gallery3d/ui/ProgressSpinner.java b/src/com/android/gallery3d/ui/ProgressSpinner.java
index 22ca3572a..d3cc8e627 100644
--- a/src/com/android/gallery3d/ui/ProgressSpinner.java
+++ b/src/com/android/gallery3d/ui/ProgressSpinner.java
@@ -55,7 +55,7 @@ public class ProgressSpinner {
}
public void draw(GLCanvas canvas, int x, int y) {
- long now = canvas.currentAnimationTimeMillis();
+ long now = AnimationTime.get();
if (mAnimationTimestamp == -1) mAnimationTimestamp = now;
mOuterDegree += (now - mAnimationTimestamp) * ROTATE_SPEED_OUTER;
mInnerDegree += (now - mAnimationTimestamp) * ROTATE_SPEED_INNER;
diff --git a/src/com/android/gallery3d/ui/SlideshowView.java b/src/com/android/gallery3d/ui/SlideshowView.java
index 1bd700b69..95f4a8d23 100644
--- a/src/com/android/gallery3d/ui/SlideshowView.java
+++ b/src/com/android/gallery3d/ui/SlideshowView.java
@@ -90,14 +90,14 @@ public class SlideshowView extends GLView {
@Override
protected void render(GLCanvas canvas) {
- long currentTimeMillis = canvas.currentAnimationTimeMillis();
- boolean requestRender = mTransitionAnimation.calculate(currentTimeMillis);
+ long animTime = AnimationTime.get();
+ boolean requestRender = mTransitionAnimation.calculate(animTime);
GL11 gl = canvas.getGLInstance();
gl.glBlendFunc(GL11.GL_ONE, GL11.GL_ONE);
float alpha = mPrevTexture == null ? 1f : mTransitionAnimation.get();
if (mPrevTexture != null && alpha != 1f) {
- requestRender |= mPrevAnimation.calculate(currentTimeMillis);
+ requestRender |= mPrevAnimation.calculate(animTime);
canvas.save(GLCanvas.SAVE_FLAG_ALPHA | GLCanvas.SAVE_FLAG_MATRIX);
canvas.setAlpha(1f - alpha);
mPrevAnimation.apply(canvas);
@@ -107,7 +107,7 @@ public class SlideshowView extends GLView {
canvas.restore();
}
if (mCurrentTexture != null) {
- requestRender |= mCurrentAnimation.calculate(currentTimeMillis);
+ requestRender |= mCurrentAnimation.calculate(animTime);
canvas.save(GLCanvas.SAVE_FLAG_ALPHA | GLCanvas.SAVE_FLAG_MATRIX);
canvas.setAlpha(alpha);
mCurrentAnimation.apply(canvas);
diff --git a/src/com/android/gallery3d/ui/SlotView.java b/src/com/android/gallery3d/ui/SlotView.java
index f1c261b97..9a0d5abec 100644
--- a/src/com/android/gallery3d/ui/SlotView.java
+++ b/src/com/android/gallery3d/ui/SlotView.java
@@ -255,8 +255,8 @@ public class SlotView extends GLView {
protected void render(GLCanvas canvas) {
super.render(canvas);
- long currentTimeMillis = canvas.currentAnimationTimeMillis();
- boolean more = mScroller.advanceAnimation(currentTimeMillis);
+ long animTime = AnimationTime.get();
+ boolean more = mScroller.advanceAnimation(animTime);
int oldX = mScrollX;
updateScrollPosition(mScroller.getPosition(), false);
@@ -281,7 +281,7 @@ public class SlotView extends GLView {
float interpolate = 1f;
if (mAnimation != null) {
- more |= mAnimation.calculate(currentTimeMillis);
+ more |= mAnimation.calculate(animTime);
interpolate = mAnimation.value;
}
diff --git a/tests/src/com/android/gallery3d/ui/GLCanvasTest.java b/tests/src/com/android/gallery3d/ui/GLCanvasTest.java
index 528b04f67..ca34d7c3c 100644
--- a/tests/src/com/android/gallery3d/ui/GLCanvasTest.java
+++ b/tests/src/com/android/gallery3d/ui/GLCanvasTest.java
@@ -66,26 +66,6 @@ public class GLCanvasTest extends TestCase {
}
@SmallTest
- public void testAnimationTime() {
- GL11 glStub = new GLStub();
- GLCanvas canvas = new GLCanvasImpl(glStub);
-
- long[] testData = {0, 1, 2, 1000, 10000, Long.MAX_VALUE};
-
- for (long v : testData) {
- canvas.setCurrentAnimationTimeMillis(v);
- assertEquals(v, canvas.currentAnimationTimeMillis());
- }
-
- try {
- canvas.setCurrentAnimationTimeMillis(-1);
- fail();
- } catch (Throwable ex) {
- // expected.
- }
- }
-
- @SmallTest
public void testSetColor() {
new SetColorTest().run();
}