Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.view; |
| 18 | |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 19 | import android.os.Handler; |
| 20 | import android.os.Looper; |
| 21 | import android.os.Message; |
| 22 | import android.os.SystemClock; |
| 23 | import android.os.SystemProperties; |
| 24 | import android.util.Log; |
| 25 | |
| 26 | /** |
Dianne Hackborn | 8bcd54b | 2012-01-31 19:04:53 -0800 | [diff] [blame] | 27 | * Coordinates animations and drawing for UI on a particular thread. |
Jeff Brown | 87d0b03 | 2012-02-03 11:01:21 -0800 | [diff] [blame] | 28 | * |
Jeff Brown | 4a06c80 | 2012-02-15 15:06:01 -0800 | [diff] [blame] | 29 | * This object is thread-safe. Other threads can post callbacks to run at a later time |
| 30 | * on the UI thread. |
Jeff Brown | 87d0b03 | 2012-02-03 11:01:21 -0800 | [diff] [blame] | 31 | * |
Jeff Brown | 58aedbc | 2012-02-13 20:15:01 -0800 | [diff] [blame] | 32 | * Ensuring thread-safety is a little tricky because the {@link DisplayEventReceiver} |
| 33 | * can only be accessed from the UI thread so operations that touch the event receiver |
| 34 | * are posted to the UI thread if needed. |
| 35 | * |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 36 | * @hide |
| 37 | */ |
Jeff Brown | 9685885 | 2012-02-14 13:45:06 -0800 | [diff] [blame] | 38 | public final class Choreographer { |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 39 | private static final String TAG = "Choreographer"; |
| 40 | private static final boolean DEBUG = false; |
| 41 | |
| 42 | // The default amount of time in ms between animation frames. |
| 43 | // When vsync is not enabled, we want to have some idea of how long we should |
| 44 | // wait before posting the next animation message. It is important that the |
| 45 | // default value be less than the true inter-frame delay on all devices to avoid |
| 46 | // situations where we might skip frames by waiting too long (we must compensate |
| 47 | // for jitter and hardware variations). Regardless of this value, the animation |
| 48 | // and display loop is ultimately rate-limited by how fast new graphics buffers can |
| 49 | // be dequeued. |
| 50 | private static final long DEFAULT_FRAME_DELAY = 10; |
| 51 | |
| 52 | // The number of milliseconds between animation frames. |
Jeff Brown | 87d0b03 | 2012-02-03 11:01:21 -0800 | [diff] [blame] | 53 | private static volatile long sFrameDelay = DEFAULT_FRAME_DELAY; |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 54 | |
| 55 | // Thread local storage for the choreographer. |
| 56 | private static final ThreadLocal<Choreographer> sThreadInstance = |
| 57 | new ThreadLocal<Choreographer>() { |
| 58 | @Override |
| 59 | protected Choreographer initialValue() { |
| 60 | Looper looper = Looper.myLooper(); |
| 61 | if (looper == null) { |
| 62 | throw new IllegalStateException("The current thread must have a looper!"); |
| 63 | } |
| 64 | return new Choreographer(looper); |
| 65 | } |
| 66 | }; |
| 67 | |
| 68 | // System property to enable/disable vsync for animations and drawing. |
| 69 | // Enabled by default. |
| 70 | private static final boolean USE_VSYNC = SystemProperties.getBoolean( |
| 71 | "debug.choreographer.vsync", true); |
| 72 | |
| 73 | // System property to enable/disable the use of the vsync / animation timer |
| 74 | // for drawing rather than drawing immediately. |
Jeff Brown | 90a3c5f | 2011-12-06 18:01:37 -0800 | [diff] [blame] | 75 | // Temporarily disabled by default because postponing performTraversals() violates |
| 76 | // assumptions about traversals happening in-order relative to other posted messages. |
| 77 | // Bug: 5721047 |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 78 | private static final boolean USE_ANIMATION_TIMER_FOR_DRAW = SystemProperties.getBoolean( |
Jeff Brown | 90a3c5f | 2011-12-06 18:01:37 -0800 | [diff] [blame] | 79 | "debug.choreographer.animdraw", false); |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 80 | |
| 81 | private static final int MSG_DO_ANIMATION = 0; |
| 82 | private static final int MSG_DO_DRAW = 1; |
Jeff Brown | 58aedbc | 2012-02-13 20:15:01 -0800 | [diff] [blame] | 83 | private static final int MSG_DO_SCHEDULE_VSYNC = 2; |
Jeff Brown | 7ae9d5f | 2012-03-05 19:33:49 -0800 | [diff] [blame] | 84 | private static final int MSG_DO_SCHEDULE_ANIMATION = 3; |
| 85 | private static final int MSG_DO_SCHEDULE_DRAW = 4; |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 86 | |
Jeff Brown | 87d0b03 | 2012-02-03 11:01:21 -0800 | [diff] [blame] | 87 | private final Object mLock = new Object(); |
| 88 | |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 89 | private final Looper mLooper; |
Jeff Brown | 9685885 | 2012-02-14 13:45:06 -0800 | [diff] [blame] | 90 | private final FrameHandler mHandler; |
Jeff Brown | 1654d0b | 2012-02-15 15:40:52 -0800 | [diff] [blame] | 91 | private final FrameDisplayEventReceiver mDisplayEventReceiver; |
Jeff Brown | 9685885 | 2012-02-14 13:45:06 -0800 | [diff] [blame] | 92 | |
| 93 | private Callback mCallbackPool; |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 94 | |
Jeff Brown | 43ea54b | 2012-03-09 14:37:48 -0800 | [diff] [blame] | 95 | private final CallbackQueue mAnimationCallbackQueue = new CallbackQueue(); |
| 96 | private final CallbackQueue mDrawCallbackQueue = new CallbackQueue(); |
Jeff Brown | 9685885 | 2012-02-14 13:45:06 -0800 | [diff] [blame] | 97 | |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 98 | private boolean mAnimationScheduled; |
| 99 | private boolean mDrawScheduled; |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 100 | private long mLastAnimationTime; |
| 101 | private long mLastDrawTime; |
| 102 | |
| 103 | private Choreographer(Looper looper) { |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 104 | mLooper = looper; |
Jeff Brown | 9685885 | 2012-02-14 13:45:06 -0800 | [diff] [blame] | 105 | mHandler = new FrameHandler(looper); |
Jeff Brown | 1654d0b | 2012-02-15 15:40:52 -0800 | [diff] [blame] | 106 | mDisplayEventReceiver = USE_VSYNC ? new FrameDisplayEventReceiver(looper) : null; |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 107 | mLastAnimationTime = Long.MIN_VALUE; |
| 108 | mLastDrawTime = Long.MIN_VALUE; |
| 109 | } |
| 110 | |
| 111 | /** |
Dianne Hackborn | 8bcd54b | 2012-01-31 19:04:53 -0800 | [diff] [blame] | 112 | * Gets the choreographer for the calling thread. Must be called from |
| 113 | * a thread that already has a {@link android.os.Looper} associated with it. |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 114 | * |
| 115 | * @return The choreographer for this thread. |
| 116 | * @throws IllegalStateException if the thread does not have a looper. |
| 117 | */ |
| 118 | public static Choreographer getInstance() { |
| 119 | return sThreadInstance.get(); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * The amount of time, in milliseconds, between each frame of the animation. This is a |
| 124 | * requested time that the animation will attempt to honor, but the actual delay between |
| 125 | * frames may be different, depending on system load and capabilities. This is a static |
| 126 | * function because the same delay will be applied to all animations, since they are all |
| 127 | * run off of a single timing loop. |
| 128 | * |
| 129 | * The frame delay may be ignored when the animation system uses an external timing |
| 130 | * source, such as the display refresh rate (vsync), to govern animations. |
| 131 | * |
| 132 | * @return the requested time between frames, in milliseconds |
| 133 | */ |
| 134 | public static long getFrameDelay() { |
| 135 | return sFrameDelay; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * The amount of time, in milliseconds, between each frame of the animation. This is a |
| 140 | * requested time that the animation will attempt to honor, but the actual delay between |
| 141 | * frames may be different, depending on system load and capabilities. This is a static |
| 142 | * function because the same delay will be applied to all animations, since they are all |
| 143 | * run off of a single timing loop. |
| 144 | * |
| 145 | * The frame delay may be ignored when the animation system uses an external timing |
| 146 | * source, such as the display refresh rate (vsync), to govern animations. |
| 147 | * |
| 148 | * @param frameDelay the requested time between frames, in milliseconds |
| 149 | */ |
| 150 | public static void setFrameDelay(long frameDelay) { |
| 151 | sFrameDelay = frameDelay; |
| 152 | } |
| 153 | |
| 154 | /** |
Jeff Brown | 7ae9d5f | 2012-03-05 19:33:49 -0800 | [diff] [blame] | 155 | * Subtracts typical frame delay time from a delay interval in milliseconds. |
| 156 | * |
| 157 | * This method can be used to compensate for animation delay times that have baked |
| 158 | * in assumptions about the frame delay. For example, it's quite common for code to |
| 159 | * assume a 60Hz frame time and bake in a 16ms delay. When we call |
| 160 | * {@link #postAnimationCallbackDelayed} we want to know how long to wait before |
| 161 | * posting the animation callback but let the animation timer take care of the remaining |
| 162 | * frame delay time. |
| 163 | * |
| 164 | * This method is somewhat conservative about how much of the frame delay it |
| 165 | * subtracts. It uses the same value returned by {@link #getFrameDelay} which by |
| 166 | * default is 10ms even though many parts of the system assume 16ms. Consequently, |
| 167 | * we might still wait 6ms before posting an animation callback that we want to run |
| 168 | * on the next frame, but this is much better than waiting a whole 16ms and likely |
| 169 | * missing the deadline. |
| 170 | * |
| 171 | * @param delayMillis The original delay time including an assumed frame delay. |
| 172 | * @return The adjusted delay time with the assumed frame delay subtracted out. |
| 173 | */ |
| 174 | public static long subtractFrameDelay(long delayMillis) { |
| 175 | final long frameDelay = sFrameDelay; |
| 176 | return delayMillis <= frameDelay ? 0 : delayMillis - frameDelay; |
| 177 | } |
| 178 | |
| 179 | /** |
Jeff Brown | 2b6cb9a | 2012-03-05 17:21:01 -0800 | [diff] [blame] | 180 | * Posts a callback to run on the next animation cycle. |
Jeff Brown | 4a06c80 | 2012-02-15 15:06:01 -0800 | [diff] [blame] | 181 | * The callback only runs once and then is automatically removed. |
| 182 | * |
Jeff Brown | 7ae9d5f | 2012-03-05 19:33:49 -0800 | [diff] [blame] | 183 | * @param action The callback action to run during the next animation cycle. |
| 184 | * @param token The callback token, or null if none. |
Jeff Brown | 4a06c80 | 2012-02-15 15:06:01 -0800 | [diff] [blame] | 185 | * |
| 186 | * @see #removeAnimationCallback |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 187 | */ |
Jeff Brown | 7ae9d5f | 2012-03-05 19:33:49 -0800 | [diff] [blame] | 188 | public void postAnimationCallback(Runnable action, Object token) { |
| 189 | postAnimationCallbackDelayed(action, token, 0); |
Jeff Brown | 87d0b03 | 2012-02-03 11:01:21 -0800 | [diff] [blame] | 190 | } |
| 191 | |
Jeff Brown | 4a06c80 | 2012-02-15 15:06:01 -0800 | [diff] [blame] | 192 | /** |
Jeff Brown | 2b6cb9a | 2012-03-05 17:21:01 -0800 | [diff] [blame] | 193 | * Posts a callback to run on the next animation cycle following the specified delay. |
| 194 | * The callback only runs once and then is automatically removed. |
| 195 | * |
Jeff Brown | 7ae9d5f | 2012-03-05 19:33:49 -0800 | [diff] [blame] | 196 | * @param action The callback action to run during the next animation cycle after |
Jeff Brown | 2b6cb9a | 2012-03-05 17:21:01 -0800 | [diff] [blame] | 197 | * the specified delay. |
Jeff Brown | 7ae9d5f | 2012-03-05 19:33:49 -0800 | [diff] [blame] | 198 | * @param token The callback token, or null if none. |
Jeff Brown | 2b6cb9a | 2012-03-05 17:21:01 -0800 | [diff] [blame] | 199 | * @param delayMillis The delay time in milliseconds. |
| 200 | * |
| 201 | * @see #removeAnimationCallback |
| 202 | */ |
Jeff Brown | 7ae9d5f | 2012-03-05 19:33:49 -0800 | [diff] [blame] | 203 | public void postAnimationCallbackDelayed(Runnable action, Object token, long delayMillis) { |
| 204 | if (action == null) { |
| 205 | throw new IllegalArgumentException("action must not be null"); |
Jeff Brown | 2b6cb9a | 2012-03-05 17:21:01 -0800 | [diff] [blame] | 206 | } |
Jeff Brown | 7ae9d5f | 2012-03-05 19:33:49 -0800 | [diff] [blame] | 207 | |
Jeff Brown | 43ea54b | 2012-03-09 14:37:48 -0800 | [diff] [blame] | 208 | if (DEBUG) { |
| 209 | Log.d(TAG, "PostAnimationCallback: " + action + ", token=" + token |
| 210 | + ", delayMillis=" + delayMillis); |
| 211 | } |
| 212 | |
Jeff Brown | 7ae9d5f | 2012-03-05 19:33:49 -0800 | [diff] [blame] | 213 | synchronized (mLock) { |
| 214 | final long now = SystemClock.uptimeMillis(); |
| 215 | final long dueTime = now + delayMillis; |
Jeff Brown | 43ea54b | 2012-03-09 14:37:48 -0800 | [diff] [blame] | 216 | mAnimationCallbackQueue.addCallbackLocked(dueTime, action, token); |
Jeff Brown | 7ae9d5f | 2012-03-05 19:33:49 -0800 | [diff] [blame] | 217 | |
| 218 | if (dueTime <= now) { |
| 219 | scheduleAnimationLocked(now); |
| 220 | } else { |
| 221 | Message msg = mHandler.obtainMessage(MSG_DO_SCHEDULE_ANIMATION, action); |
| 222 | mHandler.sendMessageAtTime(msg, dueTime); |
| 223 | } |
Jeff Brown | 2b6cb9a | 2012-03-05 17:21:01 -0800 | [diff] [blame] | 224 | } |
| 225 | } |
| 226 | |
| 227 | /** |
Jeff Brown | 7ae9d5f | 2012-03-05 19:33:49 -0800 | [diff] [blame] | 228 | * Removes animation callbacks that have the specified action and token. |
Jeff Brown | 4a06c80 | 2012-02-15 15:06:01 -0800 | [diff] [blame] | 229 | * |
Jeff Brown | 7ae9d5f | 2012-03-05 19:33:49 -0800 | [diff] [blame] | 230 | * @param action The action property of the callbacks to remove, or null to remove |
| 231 | * callbacks with any action. |
| 232 | * @param token The token property of the callbacks to remove, or null to remove |
| 233 | * callbacks with any token. |
Jeff Brown | 4a06c80 | 2012-02-15 15:06:01 -0800 | [diff] [blame] | 234 | * |
| 235 | * @see #postAnimationCallback |
Jeff Brown | 2b6cb9a | 2012-03-05 17:21:01 -0800 | [diff] [blame] | 236 | * @see #postAnimationCallbackDelayed |
Jeff Brown | 4a06c80 | 2012-02-15 15:06:01 -0800 | [diff] [blame] | 237 | */ |
Jeff Brown | 7ae9d5f | 2012-03-05 19:33:49 -0800 | [diff] [blame] | 238 | public void removeAnimationCallbacks(Runnable action, Object token) { |
Jeff Brown | 43ea54b | 2012-03-09 14:37:48 -0800 | [diff] [blame] | 239 | if (DEBUG) { |
| 240 | Log.d(TAG, "RemoveAnimationCallbacks: " + action + ", token=" + token); |
| 241 | } |
| 242 | |
Jeff Brown | 4a06c80 | 2012-02-15 15:06:01 -0800 | [diff] [blame] | 243 | synchronized (mLock) { |
Jeff Brown | 43ea54b | 2012-03-09 14:37:48 -0800 | [diff] [blame] | 244 | mAnimationCallbackQueue.removeCallbacksLocked(action, token); |
Jeff Brown | 7ae9d5f | 2012-03-05 19:33:49 -0800 | [diff] [blame] | 245 | if (action != null && token == null) { |
| 246 | mHandler.removeMessages(MSG_DO_SCHEDULE_ANIMATION, action); |
| 247 | } |
Jeff Brown | 4a06c80 | 2012-02-15 15:06:01 -0800 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | |
| 251 | /** |
Jeff Brown | 2b6cb9a | 2012-03-05 17:21:01 -0800 | [diff] [blame] | 252 | * Posts a callback to run on the next draw cycle. |
Jeff Brown | 4a06c80 | 2012-02-15 15:06:01 -0800 | [diff] [blame] | 253 | * The callback only runs once and then is automatically removed. |
| 254 | * |
Jeff Brown | 7ae9d5f | 2012-03-05 19:33:49 -0800 | [diff] [blame] | 255 | * @param action The callback action to run during the next draw cycle. |
| 256 | * @param token The callback token, or null if none. |
Jeff Brown | 4a06c80 | 2012-02-15 15:06:01 -0800 | [diff] [blame] | 257 | * |
| 258 | * @see #removeDrawCallback |
| 259 | */ |
Jeff Brown | 7ae9d5f | 2012-03-05 19:33:49 -0800 | [diff] [blame] | 260 | public void postDrawCallback(Runnable action, Object token) { |
| 261 | postDrawCallbackDelayed(action, token, 0); |
Jeff Brown | 4a06c80 | 2012-02-15 15:06:01 -0800 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | /** |
Jeff Brown | 2b6cb9a | 2012-03-05 17:21:01 -0800 | [diff] [blame] | 265 | * Posts a callback to run on the next draw cycle following the specified delay. |
| 266 | * The callback only runs once and then is automatically removed. |
| 267 | * |
Jeff Brown | 7ae9d5f | 2012-03-05 19:33:49 -0800 | [diff] [blame] | 268 | * @param action The callback action to run during the next animation cycle after |
Jeff Brown | 2b6cb9a | 2012-03-05 17:21:01 -0800 | [diff] [blame] | 269 | * the specified delay. |
Jeff Brown | 7ae9d5f | 2012-03-05 19:33:49 -0800 | [diff] [blame] | 270 | * @param token The callback token, or null if none. |
Jeff Brown | 2b6cb9a | 2012-03-05 17:21:01 -0800 | [diff] [blame] | 271 | * @param delayMillis The delay time in milliseconds. |
| 272 | * |
| 273 | * @see #removeDrawCallback |
| 274 | */ |
Jeff Brown | 7ae9d5f | 2012-03-05 19:33:49 -0800 | [diff] [blame] | 275 | public void postDrawCallbackDelayed(Runnable action, Object token, long delayMillis) { |
| 276 | if (action == null) { |
| 277 | throw new IllegalArgumentException("action must not be null"); |
Jeff Brown | 2b6cb9a | 2012-03-05 17:21:01 -0800 | [diff] [blame] | 278 | } |
Jeff Brown | 7ae9d5f | 2012-03-05 19:33:49 -0800 | [diff] [blame] | 279 | |
Jeff Brown | 43ea54b | 2012-03-09 14:37:48 -0800 | [diff] [blame] | 280 | if (DEBUG) { |
| 281 | Log.d(TAG, "PostDrawCallback: " + action + ", token=" + token |
| 282 | + ", delayMillis=" + delayMillis); |
| 283 | } |
| 284 | |
Jeff Brown | 7ae9d5f | 2012-03-05 19:33:49 -0800 | [diff] [blame] | 285 | synchronized (mLock) { |
| 286 | final long now = SystemClock.uptimeMillis(); |
| 287 | final long dueTime = now + delayMillis; |
Jeff Brown | 43ea54b | 2012-03-09 14:37:48 -0800 | [diff] [blame] | 288 | mDrawCallbackQueue.addCallbackLocked(dueTime, action, token); |
Jeff Brown | 7ae9d5f | 2012-03-05 19:33:49 -0800 | [diff] [blame] | 289 | scheduleDrawLocked(now); |
| 290 | |
| 291 | if (dueTime <= now) { |
| 292 | scheduleDrawLocked(now); |
| 293 | } else { |
| 294 | Message msg = mHandler.obtainMessage(MSG_DO_SCHEDULE_DRAW, action); |
| 295 | mHandler.sendMessageAtTime(msg, dueTime); |
| 296 | } |
Jeff Brown | 2b6cb9a | 2012-03-05 17:21:01 -0800 | [diff] [blame] | 297 | } |
| 298 | } |
| 299 | |
| 300 | /** |
Jeff Brown | 7ae9d5f | 2012-03-05 19:33:49 -0800 | [diff] [blame] | 301 | * Removes draw callbacks that have the specified action and token. |
Jeff Brown | 4a06c80 | 2012-02-15 15:06:01 -0800 | [diff] [blame] | 302 | * |
Jeff Brown | 7ae9d5f | 2012-03-05 19:33:49 -0800 | [diff] [blame] | 303 | * @param action The action property of the callbacks to remove, or null to remove |
| 304 | * callbacks with any action. |
| 305 | * @param token The token property of the callbacks to remove, or null to remove |
| 306 | * callbacks with any token. |
Jeff Brown | 4a06c80 | 2012-02-15 15:06:01 -0800 | [diff] [blame] | 307 | * |
| 308 | * @see #postDrawCallback |
Jeff Brown | 2b6cb9a | 2012-03-05 17:21:01 -0800 | [diff] [blame] | 309 | * @see #postDrawCallbackDelayed |
Jeff Brown | 4a06c80 | 2012-02-15 15:06:01 -0800 | [diff] [blame] | 310 | */ |
Jeff Brown | 7ae9d5f | 2012-03-05 19:33:49 -0800 | [diff] [blame] | 311 | public void removeDrawCallbacks(Runnable action, Object token) { |
Jeff Brown | 43ea54b | 2012-03-09 14:37:48 -0800 | [diff] [blame] | 312 | if (DEBUG) { |
| 313 | Log.d(TAG, "RemoveDrawCallbacks: " + action + ", token=" + token); |
| 314 | } |
| 315 | |
Jeff Brown | 4a06c80 | 2012-02-15 15:06:01 -0800 | [diff] [blame] | 316 | synchronized (mLock) { |
Jeff Brown | 43ea54b | 2012-03-09 14:37:48 -0800 | [diff] [blame] | 317 | mDrawCallbackQueue.removeCallbacksLocked(action, token); |
Jeff Brown | 7ae9d5f | 2012-03-05 19:33:49 -0800 | [diff] [blame] | 318 | if (action != null && token == null) { |
| 319 | mHandler.removeMessages(MSG_DO_SCHEDULE_DRAW, action); |
| 320 | } |
Jeff Brown | 4a06c80 | 2012-02-15 15:06:01 -0800 | [diff] [blame] | 321 | } |
| 322 | } |
| 323 | |
Jeff Brown | 7ae9d5f | 2012-03-05 19:33:49 -0800 | [diff] [blame] | 324 | private void scheduleAnimationLocked(long now) { |
Jeff Brown | 4a06c80 | 2012-02-15 15:06:01 -0800 | [diff] [blame] | 325 | if (!mAnimationScheduled) { |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 326 | mAnimationScheduled = true; |
| 327 | if (USE_VSYNC) { |
| 328 | if (DEBUG) { |
| 329 | Log.d(TAG, "Scheduling vsync for animation."); |
| 330 | } |
Jeff Brown | 58aedbc | 2012-02-13 20:15:01 -0800 | [diff] [blame] | 331 | |
| 332 | // If running on the Looper thread, then schedule the vsync immediately, |
| 333 | // otherwise post a message to schedule the vsync from the UI thread |
| 334 | // as soon as possible. |
Jeff Brown | 58aedbc | 2012-02-13 20:15:01 -0800 | [diff] [blame] | 335 | if (isRunningOnLooperThreadLocked()) { |
Jeff Brown | 7ae9d5f | 2012-03-05 19:33:49 -0800 | [diff] [blame] | 336 | scheduleVsyncLocked(); |
Jeff Brown | 58aedbc | 2012-02-13 20:15:01 -0800 | [diff] [blame] | 337 | } else { |
Jeff Brown | e0dbd00 | 2012-02-15 19:34:58 -0800 | [diff] [blame] | 338 | Message msg = mHandler.obtainMessage(MSG_DO_SCHEDULE_VSYNC); |
| 339 | msg.setAsynchronous(true); |
| 340 | mHandler.sendMessageAtFrontOfQueue(msg); |
Jeff Brown | 58aedbc | 2012-02-13 20:15:01 -0800 | [diff] [blame] | 341 | } |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 342 | } else { |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 343 | final long nextAnimationTime = Math.max(mLastAnimationTime + sFrameDelay, now); |
| 344 | if (DEBUG) { |
| 345 | Log.d(TAG, "Scheduling animation in " + (nextAnimationTime - now) + " ms."); |
| 346 | } |
Jeff Brown | e0dbd00 | 2012-02-15 19:34:58 -0800 | [diff] [blame] | 347 | Message msg = mHandler.obtainMessage(MSG_DO_ANIMATION); |
| 348 | msg.setAsynchronous(true); |
| 349 | mHandler.sendMessageAtTime(msg, nextAnimationTime); |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 350 | } |
| 351 | } |
| 352 | } |
| 353 | |
Jeff Brown | 7ae9d5f | 2012-03-05 19:33:49 -0800 | [diff] [blame] | 354 | private void scheduleDrawLocked(long now) { |
Jeff Brown | 4a06c80 | 2012-02-15 15:06:01 -0800 | [diff] [blame] | 355 | if (!mDrawScheduled) { |
Jeff Brown | 9685885 | 2012-02-14 13:45:06 -0800 | [diff] [blame] | 356 | mDrawScheduled = true; |
| 357 | if (USE_ANIMATION_TIMER_FOR_DRAW) { |
Jeff Brown | 7ae9d5f | 2012-03-05 19:33:49 -0800 | [diff] [blame] | 358 | scheduleAnimationLocked(now); |
Jeff Brown | 9685885 | 2012-02-14 13:45:06 -0800 | [diff] [blame] | 359 | } else { |
| 360 | if (DEBUG) { |
| 361 | Log.d(TAG, "Scheduling draw immediately."); |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 362 | } |
Jeff Brown | e0dbd00 | 2012-02-15 19:34:58 -0800 | [diff] [blame] | 363 | Message msg = mHandler.obtainMessage(MSG_DO_DRAW); |
| 364 | msg.setAsynchronous(true); |
Jeff Brown | 7ae9d5f | 2012-03-05 19:33:49 -0800 | [diff] [blame] | 365 | mHandler.sendMessageAtTime(msg, now); |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 366 | } |
| 367 | } |
| 368 | } |
| 369 | |
Jeff Brown | 9685885 | 2012-02-14 13:45:06 -0800 | [diff] [blame] | 370 | void doAnimation() { |
| 371 | doAnimationInner(); |
| 372 | |
| 373 | if (USE_ANIMATION_TIMER_FOR_DRAW) { |
| 374 | doDraw(); |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | void doAnimationInner() { |
| 379 | final long start; |
Jeff Brown | 7ae9d5f | 2012-03-05 19:33:49 -0800 | [diff] [blame] | 380 | Callback callbacks; |
Jeff Brown | 9685885 | 2012-02-14 13:45:06 -0800 | [diff] [blame] | 381 | synchronized (mLock) { |
| 382 | if (!mAnimationScheduled) { |
| 383 | return; // no work to do |
| 384 | } |
| 385 | mAnimationScheduled = false; |
| 386 | |
| 387 | start = SystemClock.uptimeMillis(); |
| 388 | if (DEBUG) { |
| 389 | Log.d(TAG, "Performing animation: " + Math.max(0, start - mLastAnimationTime) |
| 390 | + " ms have elapsed since previous animation."); |
| 391 | } |
| 392 | mLastAnimationTime = start; |
| 393 | |
Jeff Brown | 43ea54b | 2012-03-09 14:37:48 -0800 | [diff] [blame] | 394 | callbacks = mAnimationCallbackQueue.extractDueCallbacksLocked(start); |
Jeff Brown | 9685885 | 2012-02-14 13:45:06 -0800 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | if (callbacks != null) { |
| 398 | runCallbacks(callbacks); |
| 399 | synchronized (mLock) { |
| 400 | recycleCallbacksLocked(callbacks); |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | if (DEBUG) { |
| 405 | Log.d(TAG, "Animation took " + (SystemClock.uptimeMillis() - start) + " ms."); |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | void doDraw() { |
| 410 | final long start; |
Jeff Brown | 7ae9d5f | 2012-03-05 19:33:49 -0800 | [diff] [blame] | 411 | Callback callbacks; |
Jeff Brown | 9685885 | 2012-02-14 13:45:06 -0800 | [diff] [blame] | 412 | synchronized (mLock) { |
| 413 | if (!mDrawScheduled) { |
| 414 | return; // no work to do |
| 415 | } |
| 416 | mDrawScheduled = false; |
| 417 | |
| 418 | start = SystemClock.uptimeMillis(); |
| 419 | if (DEBUG) { |
| 420 | Log.d(TAG, "Performing draw: " + Math.max(0, start - mLastDrawTime) |
| 421 | + " ms have elapsed since previous draw."); |
| 422 | } |
| 423 | mLastDrawTime = start; |
| 424 | |
Jeff Brown | 43ea54b | 2012-03-09 14:37:48 -0800 | [diff] [blame] | 425 | callbacks = mDrawCallbackQueue.extractDueCallbacksLocked(start); |
Jeff Brown | 9685885 | 2012-02-14 13:45:06 -0800 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | if (callbacks != null) { |
| 429 | runCallbacks(callbacks); |
| 430 | synchronized (mLock) { |
| 431 | recycleCallbacksLocked(callbacks); |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | if (DEBUG) { |
| 436 | Log.d(TAG, "Draw took " + (SystemClock.uptimeMillis() - start) + " ms."); |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | void doScheduleVsync() { |
| 441 | synchronized (mLock) { |
Jeff Brown | 7ae9d5f | 2012-03-05 19:33:49 -0800 | [diff] [blame] | 442 | if (mAnimationScheduled) { |
| 443 | scheduleVsyncLocked(); |
| 444 | } |
Jeff Brown | 9685885 | 2012-02-14 13:45:06 -0800 | [diff] [blame] | 445 | } |
| 446 | } |
| 447 | |
Jeff Brown | 7ae9d5f | 2012-03-05 19:33:49 -0800 | [diff] [blame] | 448 | void doScheduleAnimation() { |
| 449 | synchronized (mLock) { |
| 450 | final long now = SystemClock.uptimeMillis(); |
Jeff Brown | 43ea54b | 2012-03-09 14:37:48 -0800 | [diff] [blame] | 451 | if (mAnimationCallbackQueue.hasDueCallbacksLocked(now)) { |
Jeff Brown | 7ae9d5f | 2012-03-05 19:33:49 -0800 | [diff] [blame] | 452 | scheduleAnimationLocked(now); |
| 453 | } |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 454 | } |
| 455 | } |
| 456 | |
Jeff Brown | 7ae9d5f | 2012-03-05 19:33:49 -0800 | [diff] [blame] | 457 | void doScheduleDraw() { |
| 458 | synchronized (mLock) { |
| 459 | final long now = SystemClock.uptimeMillis(); |
Jeff Brown | 43ea54b | 2012-03-09 14:37:48 -0800 | [diff] [blame] | 460 | if (mDrawCallbackQueue.hasDueCallbacksLocked(now)) { |
Jeff Brown | 7ae9d5f | 2012-03-05 19:33:49 -0800 | [diff] [blame] | 461 | scheduleDrawLocked(now); |
| 462 | } |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | private void scheduleVsyncLocked() { |
| 467 | mDisplayEventReceiver.scheduleVsync(); |
| 468 | } |
| 469 | |
Jeff Brown | 58aedbc | 2012-02-13 20:15:01 -0800 | [diff] [blame] | 470 | private boolean isRunningOnLooperThreadLocked() { |
| 471 | return Looper.myLooper() == mLooper; |
| 472 | } |
| 473 | |
Jeff Brown | 9685885 | 2012-02-14 13:45:06 -0800 | [diff] [blame] | 474 | private void runCallbacks(Callback head) { |
| 475 | while (head != null) { |
Jeff Brown | 43ea54b | 2012-03-09 14:37:48 -0800 | [diff] [blame] | 476 | if (DEBUG) { |
| 477 | Log.d(TAG, "RunCallback: " + head.action + ", token=" + head.token |
| 478 | + ", waitMillis=" + (SystemClock.uptimeMillis() - head.dueTime)); |
| 479 | } |
Jeff Brown | 7ae9d5f | 2012-03-05 19:33:49 -0800 | [diff] [blame] | 480 | head.action.run(); |
Jeff Brown | 9685885 | 2012-02-14 13:45:06 -0800 | [diff] [blame] | 481 | head = head.next; |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | private void recycleCallbacksLocked(Callback head) { |
| 486 | while (head != null) { |
| 487 | final Callback next = head.next; |
| 488 | recycleCallbackLocked(head); |
| 489 | head = next; |
| 490 | } |
| 491 | } |
| 492 | |
Jeff Brown | 7ae9d5f | 2012-03-05 19:33:49 -0800 | [diff] [blame] | 493 | private Callback obtainCallbackLocked(long dueTime, Runnable action, Object token) { |
Jeff Brown | 9685885 | 2012-02-14 13:45:06 -0800 | [diff] [blame] | 494 | Callback callback = mCallbackPool; |
| 495 | if (callback == null) { |
| 496 | callback = new Callback(); |
| 497 | } else { |
| 498 | mCallbackPool = callback.next; |
| 499 | callback.next = null; |
| 500 | } |
Jeff Brown | 7ae9d5f | 2012-03-05 19:33:49 -0800 | [diff] [blame] | 501 | callback.dueTime = dueTime; |
| 502 | callback.action = action; |
| 503 | callback.token = token; |
Jeff Brown | 9685885 | 2012-02-14 13:45:06 -0800 | [diff] [blame] | 504 | return callback; |
| 505 | } |
| 506 | |
| 507 | private void recycleCallbackLocked(Callback callback) { |
Jeff Brown | 7ae9d5f | 2012-03-05 19:33:49 -0800 | [diff] [blame] | 508 | callback.action = null; |
| 509 | callback.token = null; |
Jeff Brown | 9685885 | 2012-02-14 13:45:06 -0800 | [diff] [blame] | 510 | callback.next = mCallbackPool; |
| 511 | mCallbackPool = callback; |
| 512 | } |
| 513 | |
Jeff Brown | 9685885 | 2012-02-14 13:45:06 -0800 | [diff] [blame] | 514 | private final class FrameHandler extends Handler { |
| 515 | public FrameHandler(Looper looper) { |
| 516 | super(looper); |
| 517 | } |
| 518 | |
| 519 | @Override |
| 520 | public void handleMessage(Message msg) { |
| 521 | switch (msg.what) { |
| 522 | case MSG_DO_ANIMATION: |
| 523 | doAnimation(); |
| 524 | break; |
| 525 | case MSG_DO_DRAW: |
| 526 | doDraw(); |
| 527 | break; |
| 528 | case MSG_DO_SCHEDULE_VSYNC: |
| 529 | doScheduleVsync(); |
| 530 | break; |
Jeff Brown | 7ae9d5f | 2012-03-05 19:33:49 -0800 | [diff] [blame] | 531 | case MSG_DO_SCHEDULE_ANIMATION: |
| 532 | doScheduleAnimation(); |
Jeff Brown | 2b6cb9a | 2012-03-05 17:21:01 -0800 | [diff] [blame] | 533 | break; |
Jeff Brown | 7ae9d5f | 2012-03-05 19:33:49 -0800 | [diff] [blame] | 534 | case MSG_DO_SCHEDULE_DRAW: |
| 535 | doScheduleDraw(); |
Jeff Brown | 2b6cb9a | 2012-03-05 17:21:01 -0800 | [diff] [blame] | 536 | break; |
Jeff Brown | 9685885 | 2012-02-14 13:45:06 -0800 | [diff] [blame] | 537 | } |
| 538 | } |
| 539 | } |
| 540 | |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 541 | private final class FrameDisplayEventReceiver extends DisplayEventReceiver { |
| 542 | public FrameDisplayEventReceiver(Looper looper) { |
| 543 | super(looper); |
| 544 | } |
| 545 | |
| 546 | @Override |
| 547 | public void onVsync(long timestampNanos, int frame) { |
| 548 | doAnimation(); |
| 549 | } |
| 550 | } |
Jeff Brown | 9685885 | 2012-02-14 13:45:06 -0800 | [diff] [blame] | 551 | |
| 552 | private static final class Callback { |
| 553 | public Callback next; |
Jeff Brown | 7ae9d5f | 2012-03-05 19:33:49 -0800 | [diff] [blame] | 554 | public long dueTime; |
| 555 | public Runnable action; |
| 556 | public Object token; |
Jeff Brown | 9685885 | 2012-02-14 13:45:06 -0800 | [diff] [blame] | 557 | } |
Jeff Brown | 43ea54b | 2012-03-09 14:37:48 -0800 | [diff] [blame] | 558 | |
| 559 | private final class CallbackQueue { |
| 560 | private Callback mHead; |
| 561 | |
| 562 | public boolean hasDueCallbacksLocked(long now) { |
| 563 | return mHead != null && mHead.dueTime <= now; |
| 564 | } |
| 565 | |
| 566 | public Callback extractDueCallbacksLocked(long now) { |
| 567 | Callback callbacks = mHead; |
| 568 | if (callbacks == null || callbacks.dueTime > now) { |
| 569 | return null; |
| 570 | } |
| 571 | |
| 572 | Callback last = callbacks; |
| 573 | Callback next = last.next; |
| 574 | while (next != null) { |
| 575 | if (next.dueTime > now) { |
| 576 | last.next = null; |
| 577 | break; |
| 578 | } |
| 579 | last = next; |
| 580 | next = next.next; |
| 581 | } |
| 582 | mHead = next; |
| 583 | return callbacks; |
| 584 | } |
| 585 | |
| 586 | public void addCallbackLocked(long dueTime, Runnable action, Object token) { |
| 587 | Callback callback = obtainCallbackLocked(dueTime, action, token); |
| 588 | Callback entry = mHead; |
| 589 | if (entry == null) { |
| 590 | mHead = callback; |
| 591 | return; |
| 592 | } |
| 593 | if (dueTime < entry.dueTime) { |
| 594 | callback.next = entry; |
| 595 | mHead = callback; |
| 596 | return; |
| 597 | } |
| 598 | while (entry.next != null) { |
| 599 | if (dueTime < entry.next.dueTime) { |
| 600 | callback.next = entry.next; |
| 601 | break; |
| 602 | } |
| 603 | entry = entry.next; |
| 604 | } |
| 605 | entry.next = callback; |
| 606 | } |
| 607 | |
| 608 | public void removeCallbacksLocked(Runnable action, Object token) { |
| 609 | Callback predecessor = null; |
| 610 | for (Callback callback = mHead; callback != null;) { |
| 611 | final Callback next = callback.next; |
| 612 | if ((action == null || callback.action == action) |
| 613 | && (token == null || callback.token == token)) { |
| 614 | if (predecessor != null) { |
| 615 | predecessor.next = next; |
| 616 | } else { |
| 617 | mHead = next; |
| 618 | } |
| 619 | recycleCallbackLocked(callback); |
| 620 | } else { |
| 621 | predecessor = callback; |
| 622 | } |
| 623 | callback = next; |
| 624 | } |
| 625 | } |
| 626 | } |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 627 | } |