Cache animation time value to reduce work and synchronize animation.

Change-Id: I00081bc6037c01dafc56cf017dcc1da448c1d106
diff --git a/src/com/android/gallery3d/ui/AdaptiveBackground.java b/src/com/android/gallery3d/ui/AdaptiveBackground.java
index 42cb2cc..d7c9906 100644
--- a/src/com/android/gallery3d/ui/AdaptiveBackground.java
+++ b/src/com/android/gallery3d/ui/AdaptiveBackground.java
@@ -110,8 +110,7 @@
                 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 0000000..793e8b6
--- /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 227e67e..9508006 100644
--- a/src/com/android/gallery3d/ui/CropView.java
+++ b/src/com/android/gallery3d/ui/CropView.java
@@ -167,7 +167,7 @@
     @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 b2d83f5..c6c70ce 100644
--- a/src/com/android/gallery3d/ui/EdgeEffect.java
+++ b/src/com/android/gallery3d/ui/EdgeEffect.java
@@ -20,7 +20,6 @@
 
 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 @@
      *                      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 @@
         mGlowAlphaFinish = 0.f;
         mGlowScaleYFinish = 0.f;
 
-        mStartTime = AnimationUtils.currentAnimationTimeMillis();
+        mStartTime = AnimationTime.get();
         mDuration = RECEDE_TIME;
     }
 
@@ -262,7 +261,7 @@
         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 @@
     }
 
     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 @@
             switch (mState) {
                 case STATE_ABSORB:
                     mState = STATE_RECEDE;
-                    mStartTime = AnimationUtils.currentAnimationTimeMillis();
+                    mStartTime = AnimationTime.get();
                     mDuration = RECEDE_TIME;
 
                     mEdgeAlphaStart = mEdgeAlpha;
@@ -373,7 +372,7 @@
                     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 fea9509..eb78edd 100644
--- a/src/com/android/gallery3d/ui/GLCanvas.java
+++ b/src/com/android/gallery3d/ui/GLCanvas.java
@@ -37,12 +37,6 @@
     // 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 b8961f9..85bf771 100644
--- a/src/com/android/gallery3d/ui/GLCanvasImpl.java
+++ b/src/com/android/gallery3d/ui/GLCanvasImpl.java
@@ -61,8 +61,6 @@
 
     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 @@
         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 @@
         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 27bc885..d11de12 100644
--- a/src/com/android/gallery3d/ui/GLRootView.java
+++ b/src/com/android/gallery3d/ui/GLRootView.java
@@ -61,6 +61,7 @@
     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 @@
         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 @@
     @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 @@
         } 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 @@
             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 b4af9bc..7ae980d 100644
--- a/src/com/android/gallery3d/ui/GLView.java
+++ b/src/com/android/gallery3d/ui/GLView.java
@@ -234,7 +234,7 @@
         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 22ca357..d3cc8e6 100644
--- a/src/com/android/gallery3d/ui/ProgressSpinner.java
+++ b/src/com/android/gallery3d/ui/ProgressSpinner.java
@@ -55,7 +55,7 @@
     }
 
     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 1bd700b..95f4a8d 100644
--- a/src/com/android/gallery3d/ui/SlideshowView.java
+++ b/src/com/android/gallery3d/ui/SlideshowView.java
@@ -90,14 +90,14 @@
 
     @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 @@
             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 f1c261b..9a0d5ab 100644
--- a/src/com/android/gallery3d/ui/SlotView.java
+++ b/src/com/android/gallery3d/ui/SlotView.java
@@ -255,8 +255,8 @@
     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 @@
 
         float interpolate = 1f;
         if (mAnimation != null) {
-            more |= mAnimation.calculate(currentTimeMillis);
+            more |= mAnimation.calculate(animTime);
             interpolate = mAnimation.value;
         }