Bug 767980 - Take advantage of vsync'd drawing on post-JB in PropertyAnimator (r=mfinkle)
authorLucas Rocha <lucasr@mozilla.com>
Fri, 12 Oct 2012 12:57:06 +0100
changeset 110061 b992d80a7998c38df81f9dc7894927493c94f8da
parent 110060 0f085751610fbfc275ff10c36b07a29c16f972a9
child 110062 d2dabf55ea419d9c48d10920654d1cdad9e36728
push id16340
push userlrocha@mozilla.com
push dateFri, 12 Oct 2012 11:57:35 +0000
treeherdermozilla-inbound@9990a1074771 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmfinkle
bugs767980
milestone19.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 767980 - Take advantage of vsync'd drawing on post-JB in PropertyAnimator (r=mfinkle)
mobile/android/base/PropertyAnimator.java
--- a/mobile/android/base/PropertyAnimator.java
+++ b/mobile/android/base/PropertyAnimator.java
@@ -1,32 +1,33 @@
 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  * You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 package org.mozilla.gecko;
 
+import android.os.Build;
 import android.os.Handler;
 import android.util.Log;
+import android.view.Choreographer;
 import android.view.View;
 import android.view.ViewGroup;
 import android.view.animation.AnimationUtils;
 import android.view.animation.DecelerateInterpolator;
 import android.view.animation.Interpolator;
 
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Timer;
 import java.util.TimerTask;
 
-public class PropertyAnimator extends TimerTask {
+public class PropertyAnimator implements Runnable {
     private static final String LOGTAG = "GeckoPropertyAnimator";
 
-    private Timer mTimer;
     private Interpolator mInterpolator;
 
     public static enum Property {
         SHRINK_LEFT,
         SHRINK_TOP,
         SLIDE_TOP,
         SLIDE_LEFT
     }
@@ -43,26 +44,24 @@ public class PropertyAnimator extends Ti
         public void onPropertyAnimationEnd();
     }
 
     private long mStartTime;
     private long mDuration;
     private float mDurationReciprocal;
     private List<ElementHolder> mElementsList;
     private PropertyAnimationListener mListener;
-
-    // Default refresh rate in ms.
-    private static final int INTERVAL = 10;
+    private FramePoster mFramePoster;
 
     public PropertyAnimator(int duration) {
         mDuration = duration;
         mDurationReciprocal = 1.0f / (float) mDuration;
-        mTimer = new Timer();
         mInterpolator = new DecelerateInterpolator();
         mElementsList = new ArrayList<ElementHolder>();
+        mFramePoster = FramePoster.create(this);
     }
 
     public void attach(View view, Property property, int to) {
         if (!(view instanceof ViewGroup) && (property == Property.SHRINK_LEFT ||
                                              property == Property.SHRINK_TOP)) {
             Log.i(LOGTAG, "Margin can only be animated on Viewgroups");
             return;
         }
@@ -83,25 +82,29 @@ public class PropertyAnimator extends Ti
 
     public void setPropertyAnimationListener(PropertyAnimationListener listener) {
         mListener = listener;
     }
 
     @Override
     public void run() {
         int timePassed = (int) (AnimationUtils.currentAnimationTimeMillis() - mStartTime);
+        if (timePassed >= mDuration) {
+            stop();
+            return;
+        }
+
         float interpolation = mInterpolator.getInterpolation(timePassed * mDurationReciprocal);
 
         for (ElementHolder element : mElementsList) { 
             int delta = element.from + (int) ((element.to - element.from) * interpolation);
             invalidate(element, delta);
         }
 
-        if (timePassed >= mDuration)
-            stop();
+        mFramePoster.postNextAnimationFrame();
     }
 
     public void start() {
         mStartTime = AnimationUtils.currentAnimationTimeMillis();
 
         // Fix the from value based on current position and property
         for (ElementHolder element : mElementsList) {
             if (element.property == Property.SLIDE_TOP)
@@ -113,27 +116,25 @@ public class PropertyAnimator extends Ti
                 if (element.property == Property.SHRINK_TOP)
                     element.from = params.topMargin;
                 else if (element.property == Property.SHRINK_LEFT)
                     element.from = params.leftMargin;
             }
         }
 
         if (mDuration != 0) {
-            mTimer.scheduleAtFixedRate(this, 0, INTERVAL);
+            mFramePoster.postFirstAnimationFrame();
 
             if (mListener != null)
                 mListener.onPropertyAnimationStart();
         }
     }
 
     public void stop() {
-        cancel();
-        mTimer.cancel();
-        mTimer.purge();
+        mFramePoster.cancelAnimationFrame();
 
         // Make sure to snap to the end position.
         for (ElementHolder element : mElementsList) { 
             invalidate(element, element.to);
         }
 
         mElementsList.clear();
 
@@ -141,41 +142,103 @@ public class PropertyAnimator extends Ti
             mListener.onPropertyAnimationEnd();
             mListener = null;
         }
     }
 
     private void invalidate(final ElementHolder element, final int delta) {
         final View view = element.view;
 
-        Handler handler = view.getHandler();
-        if (handler == null)
+        // check to see if the view was detached between the check above and this code
+        // getting run on the UI thread.
+        if (view.getHandler() == null)
             return;
 
-        // Post the layout changes on the view's UI thread.
-        handler.post(new Runnable() {
-            @Override
-            public void run() {
-                // check to see if the view was detached between the check above and this code
-                // getting run on the UI thread.
-                if (view.getHandler() == null)
-                    return;
-            
-                if (element.property == Property.SLIDE_TOP) {
-                    view.scrollTo(view.getScrollX(), delta);
-                    return;
-                } else if (element.property == Property.SLIDE_LEFT) {
-                    view.scrollTo(delta, view.getScrollY());
-                    return;
+        if (element.property == Property.SLIDE_TOP) {
+            view.scrollTo(view.getScrollX(), delta);
+            return;
+        } else if (element.property == Property.SLIDE_LEFT) {
+            view.scrollTo(delta, view.getScrollY());
+            return;
+        }
+
+        ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
+
+        if (element.property == Property.SHRINK_TOP)
+            params.setMargins(params.leftMargin, delta, params.rightMargin, params.bottomMargin);
+        else if (element.property == Property.SHRINK_LEFT)
+            params.setMargins(delta, params.topMargin, params.rightMargin, params.bottomMargin);
+
+        view.requestLayout();
+    }
+
+    private static abstract class FramePoster {
+        public static FramePoster create(Runnable r) {
+            if (Build.VERSION.SDK_INT >= 16)
+                return new FramePosterPostJB(r);
+            else
+                return new FramePosterPreJB(r);
+        }
+
+        public abstract void postFirstAnimationFrame();
+        public abstract void postNextAnimationFrame();
+        public abstract void cancelAnimationFrame();
+    }
+
+    private static class FramePosterPreJB extends FramePoster {
+        // Default refresh rate in ms.
+        private static final int INTERVAL = 10;
+
+        private Handler mHandler;
+        private Runnable mRunnable;
+
+        public FramePosterPreJB(Runnable r) {
+            mHandler = new Handler();
+            mRunnable = r;
+        }
+
+        @Override
+        public void postFirstAnimationFrame() {
+            mHandler.post(mRunnable);
+        }
+
+        @Override
+        public void postNextAnimationFrame() {
+            mHandler.postDelayed(mRunnable, INTERVAL);
+        }
+
+        @Override
+        public void cancelAnimationFrame() {
+            mHandler.removeCallbacks(mRunnable);
+        }
+    }
+
+    private static class FramePosterPostJB extends FramePoster {
+        private Choreographer mChoreographer;
+        private Choreographer.FrameCallback mCallback;
+
+        public FramePosterPostJB(final Runnable r) {
+            mChoreographer = Choreographer.getInstance();
+
+            mCallback = new Choreographer.FrameCallback() {
+                @Override
+                public void doFrame(long frameTimeNanos) {
+                    r.run();
                 }
+            };
+        }
 
-                ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) view.getLayoutParams();
+        @Override
+        public void postFirstAnimationFrame() {
+            postNextAnimationFrame();
+        }
 
-                if (element.property == Property.SHRINK_TOP)
-                    params.setMargins(params.leftMargin, delta, params.rightMargin, params.bottomMargin);
-                else if (element.property == Property.SHRINK_LEFT)
-                    params.setMargins(delta, params.topMargin, params.rightMargin, params.bottomMargin);
+        @Override
+        public void postNextAnimationFrame() {
+            mChoreographer.postFrameCallback(mCallback);
+        }
 
-                view.requestLayout();
-            }
-        });
+        @Override
+        public void cancelAnimationFrame() {
+            mChoreographer.removeFrameCallback(mCallback);
+        }
     }
 }