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 | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 19 | import android.hardware.display.DisplayManagerGlobal; |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 20 | import android.os.Handler; |
| 21 | import android.os.Looper; |
| 22 | import android.os.Message; |
| 23 | import android.os.SystemClock; |
| 24 | import android.os.SystemProperties; |
| 25 | import android.util.Log; |
| 26 | |
| 27 | /** |
Jeff Brown | cae8049 | 2012-05-21 16:33:39 -0700 | [diff] [blame] | 28 | * Coordinates the timing of animations, input and drawing. |
| 29 | * <p> |
| 30 | * The choreographer receives timing pulses (such as vertical synchronization) |
| 31 | * from the display subsystem then schedules work to occur as part of rendering |
| 32 | * the next display frame. |
| 33 | * </p><p> |
| 34 | * Applications typically interact with the choreographer indirectly using |
| 35 | * higher level abstractions in the animation framework or the view hierarchy. |
| 36 | * Here are some examples of things you can do using the higher-level APIs. |
| 37 | * </p> |
| 38 | * <ul> |
| 39 | * <li>To post an animation to be processed on a regular time basis synchronized with |
| 40 | * display frame rendering, use {@link android.animation.ValueAnimator#start}.</li> |
| 41 | * <li>To post a {@link Runnable} to be invoked once at the beginning of the next display |
| 42 | * frame, use {@link View#postOnAnimation}.</li> |
| 43 | * <li>To post a {@link Runnable} to be invoked once at the beginning of the next display |
| 44 | * frame after a delay, use {@link View#postOnAnimationDelayed}.</li> |
| 45 | * <li>To post a call to {@link View#invalidate()} to occur once at the beginning of the |
| 46 | * next display frame, use {@link View#postInvalidateOnAnimation()} or |
| 47 | * {@link View#postInvalidateOnAnimation(int, int, int, int)}.</li> |
| 48 | * <li>To ensure that the contents of a {@link View} scroll smoothly and are drawn in |
| 49 | * sync with display frame rendering, do nothing. This already happens automatically. |
| 50 | * {@link View#onDraw} will be called at the appropriate time.</li> |
| 51 | * </ul> |
| 52 | * <p> |
| 53 | * However, there are a few cases where you might want to use the functions of the |
| 54 | * choreographer directly in your application. Here are some examples. |
| 55 | * </p> |
| 56 | * <ul> |
| 57 | * <li>If your application does its rendering in a different thread, possibly using GL, |
| 58 | * or does not use the animation framework or view hierarchy at all |
| 59 | * and you want to ensure that it is appropriately synchronized with the display, then use |
| 60 | * {@link Choreographer#postFrameCallback}.</li> |
| 61 | * <li>... and that's about it.</li> |
| 62 | * </ul> |
| 63 | * <p> |
| 64 | * Each {@link Looper} thread has its own choreographer. Other threads can |
| 65 | * post callbacks to run on the choreographer but they will run on the {@link Looper} |
| 66 | * to which the choreographer belongs. |
| 67 | * </p> |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 68 | */ |
Jeff Brown | 9685885 | 2012-02-14 13:45:06 -0800 | [diff] [blame] | 69 | public final class Choreographer { |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 70 | private static final String TAG = "Choreographer"; |
| 71 | private static final boolean DEBUG = false; |
| 72 | |
| 73 | // The default amount of time in ms between animation frames. |
| 74 | // When vsync is not enabled, we want to have some idea of how long we should |
| 75 | // wait before posting the next animation message. It is important that the |
| 76 | // default value be less than the true inter-frame delay on all devices to avoid |
| 77 | // situations where we might skip frames by waiting too long (we must compensate |
| 78 | // for jitter and hardware variations). Regardless of this value, the animation |
| 79 | // and display loop is ultimately rate-limited by how fast new graphics buffers can |
| 80 | // be dequeued. |
| 81 | private static final long DEFAULT_FRAME_DELAY = 10; |
| 82 | |
| 83 | // The number of milliseconds between animation frames. |
Jeff Brown | 87d0b03 | 2012-02-03 11:01:21 -0800 | [diff] [blame] | 84 | private static volatile long sFrameDelay = DEFAULT_FRAME_DELAY; |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 85 | |
| 86 | // Thread local storage for the choreographer. |
| 87 | private static final ThreadLocal<Choreographer> sThreadInstance = |
| 88 | new ThreadLocal<Choreographer>() { |
| 89 | @Override |
| 90 | protected Choreographer initialValue() { |
| 91 | Looper looper = Looper.myLooper(); |
| 92 | if (looper == null) { |
| 93 | throw new IllegalStateException("The current thread must have a looper!"); |
| 94 | } |
| 95 | return new Choreographer(looper); |
| 96 | } |
| 97 | }; |
| 98 | |
Jeff Brown | ebb2d8d | 2012-03-23 17:14:34 -0700 | [diff] [blame] | 99 | // Enable/disable vsync for animations and drawing. |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 100 | private static final boolean USE_VSYNC = SystemProperties.getBoolean( |
| 101 | "debug.choreographer.vsync", true); |
| 102 | |
Jeff Brown | 20c4f87 | 2012-04-26 17:38:21 -0700 | [diff] [blame] | 103 | // Enable/disable using the frame time instead of returning now. |
| 104 | private static final boolean USE_FRAME_TIME = SystemProperties.getBoolean( |
| 105 | "debug.choreographer.frametime", true); |
| 106 | |
Jeff Brown | 4fdf9c6 | 2012-06-11 15:25:48 -0700 | [diff] [blame] | 107 | // Set a limit to warn about skipped frames. |
| 108 | // Skipped frames imply jank. |
| 109 | private static final int SKIPPED_FRAME_WARNING_LIMIT = SystemProperties.getInt( |
| 110 | "debug.choreographer.skipwarning", 30); |
| 111 | |
Jeff Brown | 20c4f87 | 2012-04-26 17:38:21 -0700 | [diff] [blame] | 112 | private static final long NANOS_PER_MS = 1000000; |
| 113 | |
Jeff Brown | ebb2d8d | 2012-03-23 17:14:34 -0700 | [diff] [blame] | 114 | private static final int MSG_DO_FRAME = 0; |
| 115 | private static final int MSG_DO_SCHEDULE_VSYNC = 1; |
| 116 | private static final int MSG_DO_SCHEDULE_CALLBACK = 2; |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 117 | |
Jeff Brown | cae8049 | 2012-05-21 16:33:39 -0700 | [diff] [blame] | 118 | // All frame callbacks posted by applications have this token. |
| 119 | private static final Object FRAME_CALLBACK_TOKEN = new Object() { |
| 120 | public String toString() { return "FRAME_CALLBACK_TOKEN"; } |
| 121 | }; |
| 122 | |
Jeff Brown | 87d0b03 | 2012-02-03 11:01:21 -0800 | [diff] [blame] | 123 | private final Object mLock = new Object(); |
| 124 | |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 125 | private final Looper mLooper; |
Jeff Brown | 9685885 | 2012-02-14 13:45:06 -0800 | [diff] [blame] | 126 | private final FrameHandler mHandler; |
Jeff Brown | cae8049 | 2012-05-21 16:33:39 -0700 | [diff] [blame] | 127 | |
| 128 | // The display event receiver can only be accessed by the looper thread to which |
| 129 | // it is attached. We take care to ensure that we post message to the looper |
| 130 | // if appropriate when interacting with the display event receiver. |
Jeff Brown | 1654d0b | 2012-02-15 15:40:52 -0800 | [diff] [blame] | 131 | private final FrameDisplayEventReceiver mDisplayEventReceiver; |
Jeff Brown | 9685885 | 2012-02-14 13:45:06 -0800 | [diff] [blame] | 132 | |
Jeff Brown | cae8049 | 2012-05-21 16:33:39 -0700 | [diff] [blame] | 133 | private CallbackRecord mCallbackPool; |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 134 | |
Jeff Brown | ebb2d8d | 2012-03-23 17:14:34 -0700 | [diff] [blame] | 135 | private final CallbackQueue[] mCallbackQueues; |
Jeff Brown | 9685885 | 2012-02-14 13:45:06 -0800 | [diff] [blame] | 136 | |
Jeff Brown | ebb2d8d | 2012-03-23 17:14:34 -0700 | [diff] [blame] | 137 | private boolean mFrameScheduled; |
Jeff Brown | 20c4f87 | 2012-04-26 17:38:21 -0700 | [diff] [blame] | 138 | private boolean mCallbacksRunning; |
| 139 | private long mLastFrameTimeNanos; |
Jeff Brown | 59bbef0 | 2012-05-07 16:43:25 -0700 | [diff] [blame] | 140 | private long mFrameIntervalNanos; |
Jeff Brown | ebb2d8d | 2012-03-23 17:14:34 -0700 | [diff] [blame] | 141 | |
| 142 | /** |
| 143 | * Callback type: Input callback. Runs first. |
Jeff Brown | cae8049 | 2012-05-21 16:33:39 -0700 | [diff] [blame] | 144 | * @hide |
Jeff Brown | ebb2d8d | 2012-03-23 17:14:34 -0700 | [diff] [blame] | 145 | */ |
| 146 | public static final int CALLBACK_INPUT = 0; |
| 147 | |
| 148 | /** |
| 149 | * Callback type: Animation callback. Runs before traversals. |
Jeff Brown | cae8049 | 2012-05-21 16:33:39 -0700 | [diff] [blame] | 150 | * @hide |
Jeff Brown | ebb2d8d | 2012-03-23 17:14:34 -0700 | [diff] [blame] | 151 | */ |
| 152 | public static final int CALLBACK_ANIMATION = 1; |
| 153 | |
| 154 | /** |
| 155 | * Callback type: Traversal callback. Handles layout and draw. Runs last |
| 156 | * after all other asynchronous messages have been handled. |
Jeff Brown | cae8049 | 2012-05-21 16:33:39 -0700 | [diff] [blame] | 157 | * @hide |
Jeff Brown | ebb2d8d | 2012-03-23 17:14:34 -0700 | [diff] [blame] | 158 | */ |
| 159 | public static final int CALLBACK_TRAVERSAL = 2; |
| 160 | |
| 161 | private static final int CALLBACK_LAST = CALLBACK_TRAVERSAL; |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 162 | |
| 163 | private Choreographer(Looper looper) { |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 164 | mLooper = looper; |
Jeff Brown | 9685885 | 2012-02-14 13:45:06 -0800 | [diff] [blame] | 165 | mHandler = new FrameHandler(looper); |
Jeff Brown | 1654d0b | 2012-02-15 15:40:52 -0800 | [diff] [blame] | 166 | mDisplayEventReceiver = USE_VSYNC ? new FrameDisplayEventReceiver(looper) : null; |
Jeff Brown | 20c4f87 | 2012-04-26 17:38:21 -0700 | [diff] [blame] | 167 | mLastFrameTimeNanos = Long.MIN_VALUE; |
Jeff Brown | d32460c | 2012-07-20 16:15:36 -0700 | [diff] [blame] | 168 | |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 169 | mFrameIntervalNanos = (long)(1000000000 / getRefreshRate()); |
Jeff Brown | ebb2d8d | 2012-03-23 17:14:34 -0700 | [diff] [blame] | 170 | |
| 171 | mCallbackQueues = new CallbackQueue[CALLBACK_LAST + 1]; |
| 172 | for (int i = 0; i <= CALLBACK_LAST; i++) { |
| 173 | mCallbackQueues[i] = new CallbackQueue(); |
| 174 | } |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 175 | } |
| 176 | |
Jeff Brown | bd6e150 | 2012-08-28 03:27:37 -0700 | [diff] [blame] | 177 | private static float getRefreshRate() { |
| 178 | DisplayInfo di = DisplayManagerGlobal.getInstance().getDisplayInfo( |
| 179 | Display.DEFAULT_DISPLAY); |
| 180 | return di.refreshRate; |
| 181 | } |
| 182 | |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 183 | /** |
Dianne Hackborn | 8bcd54b | 2012-01-31 19:04:53 -0800 | [diff] [blame] | 184 | * Gets the choreographer for the calling thread. Must be called from |
| 185 | * 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] | 186 | * |
| 187 | * @return The choreographer for this thread. |
| 188 | * @throws IllegalStateException if the thread does not have a looper. |
| 189 | */ |
| 190 | public static Choreographer getInstance() { |
| 191 | return sThreadInstance.get(); |
| 192 | } |
| 193 | |
| 194 | /** |
Jeff Brown | cae8049 | 2012-05-21 16:33:39 -0700 | [diff] [blame] | 195 | * The amount of time, in milliseconds, between each frame of the animation. |
| 196 | * <p> |
| 197 | * This is a requested time that the animation will attempt to honor, but the actual delay |
| 198 | * between frames may be different, depending on system load and capabilities. This is a static |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 199 | * function because the same delay will be applied to all animations, since they are all |
| 200 | * run off of a single timing loop. |
Jeff Brown | cae8049 | 2012-05-21 16:33:39 -0700 | [diff] [blame] | 201 | * </p><p> |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 202 | * The frame delay may be ignored when the animation system uses an external timing |
| 203 | * source, such as the display refresh rate (vsync), to govern animations. |
Jeff Brown | cae8049 | 2012-05-21 16:33:39 -0700 | [diff] [blame] | 204 | * </p> |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 205 | * |
| 206 | * @return the requested time between frames, in milliseconds |
Jeff Brown | cae8049 | 2012-05-21 16:33:39 -0700 | [diff] [blame] | 207 | * @hide |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 208 | */ |
| 209 | public static long getFrameDelay() { |
| 210 | return sFrameDelay; |
| 211 | } |
| 212 | |
| 213 | /** |
Jeff Brown | cae8049 | 2012-05-21 16:33:39 -0700 | [diff] [blame] | 214 | * The amount of time, in milliseconds, between each frame of the animation. |
| 215 | * <p> |
| 216 | * This is a requested time that the animation will attempt to honor, but the actual delay |
| 217 | * between frames may be different, depending on system load and capabilities. This is a static |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 218 | * function because the same delay will be applied to all animations, since they are all |
| 219 | * run off of a single timing loop. |
Jeff Brown | cae8049 | 2012-05-21 16:33:39 -0700 | [diff] [blame] | 220 | * </p><p> |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 221 | * The frame delay may be ignored when the animation system uses an external timing |
| 222 | * source, such as the display refresh rate (vsync), to govern animations. |
Jeff Brown | cae8049 | 2012-05-21 16:33:39 -0700 | [diff] [blame] | 223 | * </p> |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 224 | * |
| 225 | * @param frameDelay the requested time between frames, in milliseconds |
Jeff Brown | cae8049 | 2012-05-21 16:33:39 -0700 | [diff] [blame] | 226 | * @hide |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 227 | */ |
| 228 | public static void setFrameDelay(long frameDelay) { |
| 229 | sFrameDelay = frameDelay; |
| 230 | } |
| 231 | |
| 232 | /** |
Jeff Brown | 7ae9d5f | 2012-03-05 19:33:49 -0800 | [diff] [blame] | 233 | * Subtracts typical frame delay time from a delay interval in milliseconds. |
Jeff Brown | cae8049 | 2012-05-21 16:33:39 -0700 | [diff] [blame] | 234 | * <p> |
Jeff Brown | 7ae9d5f | 2012-03-05 19:33:49 -0800 | [diff] [blame] | 235 | * This method can be used to compensate for animation delay times that have baked |
| 236 | * in assumptions about the frame delay. For example, it's quite common for code to |
| 237 | * assume a 60Hz frame time and bake in a 16ms delay. When we call |
| 238 | * {@link #postAnimationCallbackDelayed} we want to know how long to wait before |
| 239 | * posting the animation callback but let the animation timer take care of the remaining |
| 240 | * frame delay time. |
Jeff Brown | cae8049 | 2012-05-21 16:33:39 -0700 | [diff] [blame] | 241 | * </p><p> |
Jeff Brown | 7ae9d5f | 2012-03-05 19:33:49 -0800 | [diff] [blame] | 242 | * This method is somewhat conservative about how much of the frame delay it |
| 243 | * subtracts. It uses the same value returned by {@link #getFrameDelay} which by |
| 244 | * default is 10ms even though many parts of the system assume 16ms. Consequently, |
| 245 | * we might still wait 6ms before posting an animation callback that we want to run |
| 246 | * on the next frame, but this is much better than waiting a whole 16ms and likely |
| 247 | * missing the deadline. |
Jeff Brown | cae8049 | 2012-05-21 16:33:39 -0700 | [diff] [blame] | 248 | * </p> |
Jeff Brown | 7ae9d5f | 2012-03-05 19:33:49 -0800 | [diff] [blame] | 249 | * |
| 250 | * @param delayMillis The original delay time including an assumed frame delay. |
| 251 | * @return The adjusted delay time with the assumed frame delay subtracted out. |
Jeff Brown | cae8049 | 2012-05-21 16:33:39 -0700 | [diff] [blame] | 252 | * @hide |
Jeff Brown | 7ae9d5f | 2012-03-05 19:33:49 -0800 | [diff] [blame] | 253 | */ |
| 254 | public static long subtractFrameDelay(long delayMillis) { |
| 255 | final long frameDelay = sFrameDelay; |
| 256 | return delayMillis <= frameDelay ? 0 : delayMillis - frameDelay; |
| 257 | } |
| 258 | |
| 259 | /** |
Jeff Brown | ebb2d8d | 2012-03-23 17:14:34 -0700 | [diff] [blame] | 260 | * Posts a callback to run on the next frame. |
Jeff Brown | cae8049 | 2012-05-21 16:33:39 -0700 | [diff] [blame] | 261 | * <p> |
| 262 | * The callback runs once then is automatically removed. |
| 263 | * </p> |
Jeff Brown | 4a06c80 | 2012-02-15 15:06:01 -0800 | [diff] [blame] | 264 | * |
Jeff Brown | ebb2d8d | 2012-03-23 17:14:34 -0700 | [diff] [blame] | 265 | * @param callbackType The callback type. |
| 266 | * @param action The callback action to run during the next frame. |
Jeff Brown | 7ae9d5f | 2012-03-05 19:33:49 -0800 | [diff] [blame] | 267 | * @param token The callback token, or null if none. |
Jeff Brown | 4a06c80 | 2012-02-15 15:06:01 -0800 | [diff] [blame] | 268 | * |
Jeff Brown | ebb2d8d | 2012-03-23 17:14:34 -0700 | [diff] [blame] | 269 | * @see #removeCallbacks |
Jeff Brown | cae8049 | 2012-05-21 16:33:39 -0700 | [diff] [blame] | 270 | * @hide |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 271 | */ |
Jeff Brown | ebb2d8d | 2012-03-23 17:14:34 -0700 | [diff] [blame] | 272 | public void postCallback(int callbackType, Runnable action, Object token) { |
| 273 | postCallbackDelayed(callbackType, action, token, 0); |
Jeff Brown | 87d0b03 | 2012-02-03 11:01:21 -0800 | [diff] [blame] | 274 | } |
| 275 | |
Jeff Brown | 4a06c80 | 2012-02-15 15:06:01 -0800 | [diff] [blame] | 276 | /** |
Jeff Brown | cae8049 | 2012-05-21 16:33:39 -0700 | [diff] [blame] | 277 | * Posts a callback to run on the next frame after the specified delay. |
| 278 | * <p> |
| 279 | * The callback runs once then is automatically removed. |
| 280 | * </p> |
Jeff Brown | 2b6cb9a | 2012-03-05 17:21:01 -0800 | [diff] [blame] | 281 | * |
Jeff Brown | ebb2d8d | 2012-03-23 17:14:34 -0700 | [diff] [blame] | 282 | * @param callbackType The callback type. |
| 283 | * @param action The callback action to run during the next frame after the specified delay. |
Jeff Brown | 7ae9d5f | 2012-03-05 19:33:49 -0800 | [diff] [blame] | 284 | * @param token The callback token, or null if none. |
Jeff Brown | 2b6cb9a | 2012-03-05 17:21:01 -0800 | [diff] [blame] | 285 | * @param delayMillis The delay time in milliseconds. |
| 286 | * |
Jeff Brown | ebb2d8d | 2012-03-23 17:14:34 -0700 | [diff] [blame] | 287 | * @see #removeCallback |
Jeff Brown | cae8049 | 2012-05-21 16:33:39 -0700 | [diff] [blame] | 288 | * @hide |
Jeff Brown | 2b6cb9a | 2012-03-05 17:21:01 -0800 | [diff] [blame] | 289 | */ |
Jeff Brown | ebb2d8d | 2012-03-23 17:14:34 -0700 | [diff] [blame] | 290 | public void postCallbackDelayed(int callbackType, |
| 291 | Runnable action, Object token, long delayMillis) { |
Jeff Brown | 7ae9d5f | 2012-03-05 19:33:49 -0800 | [diff] [blame] | 292 | if (action == null) { |
| 293 | throw new IllegalArgumentException("action must not be null"); |
Jeff Brown | 2b6cb9a | 2012-03-05 17:21:01 -0800 | [diff] [blame] | 294 | } |
Jeff Brown | ebb2d8d | 2012-03-23 17:14:34 -0700 | [diff] [blame] | 295 | if (callbackType < 0 || callbackType > CALLBACK_LAST) { |
| 296 | throw new IllegalArgumentException("callbackType is invalid"); |
| 297 | } |
Jeff Brown | 7ae9d5f | 2012-03-05 19:33:49 -0800 | [diff] [blame] | 298 | |
Jeff Brown | cae8049 | 2012-05-21 16:33:39 -0700 | [diff] [blame] | 299 | postCallbackDelayedInternal(callbackType, action, token, delayMillis); |
| 300 | } |
| 301 | |
| 302 | private void postCallbackDelayedInternal(int callbackType, |
| 303 | Object action, Object token, long delayMillis) { |
Jeff Brown | 43ea54b | 2012-03-09 14:37:48 -0800 | [diff] [blame] | 304 | if (DEBUG) { |
Jeff Brown | ebb2d8d | 2012-03-23 17:14:34 -0700 | [diff] [blame] | 305 | Log.d(TAG, "PostCallback: type=" + callbackType |
| 306 | + ", action=" + action + ", token=" + token |
Jeff Brown | 43ea54b | 2012-03-09 14:37:48 -0800 | [diff] [blame] | 307 | + ", delayMillis=" + delayMillis); |
| 308 | } |
| 309 | |
Jeff Brown | 7ae9d5f | 2012-03-05 19:33:49 -0800 | [diff] [blame] | 310 | synchronized (mLock) { |
| 311 | final long now = SystemClock.uptimeMillis(); |
| 312 | final long dueTime = now + delayMillis; |
Jeff Brown | ebb2d8d | 2012-03-23 17:14:34 -0700 | [diff] [blame] | 313 | mCallbackQueues[callbackType].addCallbackLocked(dueTime, action, token); |
Jeff Brown | 7ae9d5f | 2012-03-05 19:33:49 -0800 | [diff] [blame] | 314 | |
| 315 | if (dueTime <= now) { |
Jeff Brown | ebb2d8d | 2012-03-23 17:14:34 -0700 | [diff] [blame] | 316 | scheduleFrameLocked(now); |
Jeff Brown | 7ae9d5f | 2012-03-05 19:33:49 -0800 | [diff] [blame] | 317 | } else { |
Jeff Brown | ebb2d8d | 2012-03-23 17:14:34 -0700 | [diff] [blame] | 318 | Message msg = mHandler.obtainMessage(MSG_DO_SCHEDULE_CALLBACK, action); |
| 319 | msg.arg1 = callbackType; |
| 320 | msg.setAsynchronous(true); |
Jeff Brown | 7ae9d5f | 2012-03-05 19:33:49 -0800 | [diff] [blame] | 321 | mHandler.sendMessageAtTime(msg, dueTime); |
| 322 | } |
Jeff Brown | 2b6cb9a | 2012-03-05 17:21:01 -0800 | [diff] [blame] | 323 | } |
| 324 | } |
| 325 | |
| 326 | /** |
Jeff Brown | ebb2d8d | 2012-03-23 17:14:34 -0700 | [diff] [blame] | 327 | * Removes callbacks that have the specified action and token. |
Jeff Brown | 4a06c80 | 2012-02-15 15:06:01 -0800 | [diff] [blame] | 328 | * |
Jeff Brown | ebb2d8d | 2012-03-23 17:14:34 -0700 | [diff] [blame] | 329 | * @param callbackType The callback type. |
Jeff Brown | 7ae9d5f | 2012-03-05 19:33:49 -0800 | [diff] [blame] | 330 | * @param action The action property of the callbacks to remove, or null to remove |
| 331 | * callbacks with any action. |
| 332 | * @param token The token property of the callbacks to remove, or null to remove |
| 333 | * callbacks with any token. |
Jeff Brown | 4a06c80 | 2012-02-15 15:06:01 -0800 | [diff] [blame] | 334 | * |
Jeff Brown | ebb2d8d | 2012-03-23 17:14:34 -0700 | [diff] [blame] | 335 | * @see #postCallback |
| 336 | * @see #postCallbackDelayed |
Jeff Brown | cae8049 | 2012-05-21 16:33:39 -0700 | [diff] [blame] | 337 | * @hide |
Jeff Brown | 4a06c80 | 2012-02-15 15:06:01 -0800 | [diff] [blame] | 338 | */ |
Jeff Brown | ebb2d8d | 2012-03-23 17:14:34 -0700 | [diff] [blame] | 339 | public void removeCallbacks(int callbackType, Runnable action, Object token) { |
| 340 | if (callbackType < 0 || callbackType > CALLBACK_LAST) { |
| 341 | throw new IllegalArgumentException("callbackType is invalid"); |
| 342 | } |
| 343 | |
Jeff Brown | cae8049 | 2012-05-21 16:33:39 -0700 | [diff] [blame] | 344 | removeCallbacksInternal(callbackType, action, token); |
| 345 | } |
| 346 | |
| 347 | private void removeCallbacksInternal(int callbackType, Object action, Object token) { |
Jeff Brown | 43ea54b | 2012-03-09 14:37:48 -0800 | [diff] [blame] | 348 | if (DEBUG) { |
Jeff Brown | ebb2d8d | 2012-03-23 17:14:34 -0700 | [diff] [blame] | 349 | Log.d(TAG, "RemoveCallbacks: type=" + callbackType |
| 350 | + ", action=" + action + ", token=" + token); |
Jeff Brown | 43ea54b | 2012-03-09 14:37:48 -0800 | [diff] [blame] | 351 | } |
| 352 | |
Jeff Brown | 4a06c80 | 2012-02-15 15:06:01 -0800 | [diff] [blame] | 353 | synchronized (mLock) { |
Jeff Brown | ebb2d8d | 2012-03-23 17:14:34 -0700 | [diff] [blame] | 354 | mCallbackQueues[callbackType].removeCallbacksLocked(action, token); |
Jeff Brown | 7ae9d5f | 2012-03-05 19:33:49 -0800 | [diff] [blame] | 355 | if (action != null && token == null) { |
Jeff Brown | ebb2d8d | 2012-03-23 17:14:34 -0700 | [diff] [blame] | 356 | mHandler.removeMessages(MSG_DO_SCHEDULE_CALLBACK, action); |
Jeff Brown | 7ae9d5f | 2012-03-05 19:33:49 -0800 | [diff] [blame] | 357 | } |
Jeff Brown | 4a06c80 | 2012-02-15 15:06:01 -0800 | [diff] [blame] | 358 | } |
| 359 | } |
| 360 | |
Jeff Brown | 20c4f87 | 2012-04-26 17:38:21 -0700 | [diff] [blame] | 361 | /** |
Jeff Brown | cae8049 | 2012-05-21 16:33:39 -0700 | [diff] [blame] | 362 | * Posts a frame callback to run on the next frame. |
| 363 | * <p> |
| 364 | * The callback runs once then is automatically removed. |
| 365 | * </p> |
Jeff Brown | 20c4f87 | 2012-04-26 17:38:21 -0700 | [diff] [blame] | 366 | * |
Jeff Brown | cae8049 | 2012-05-21 16:33:39 -0700 | [diff] [blame] | 367 | * @param callback The frame callback to run during the next frame. |
| 368 | * |
| 369 | * @see #postFrameCallbackDelayed |
| 370 | * @see #removeFrameCallback |
| 371 | */ |
| 372 | public void postFrameCallback(FrameCallback callback) { |
| 373 | postFrameCallbackDelayed(callback, 0); |
| 374 | } |
| 375 | |
| 376 | /** |
| 377 | * Posts a frame callback to run on the next frame after the specified delay. |
| 378 | * <p> |
| 379 | * The callback runs once then is automatically removed. |
| 380 | * </p> |
| 381 | * |
| 382 | * @param callback The frame callback to run during the next frame. |
| 383 | * @param delayMillis The delay time in milliseconds. |
| 384 | * |
| 385 | * @see #postFrameCallback |
| 386 | * @see #removeFrameCallback |
| 387 | */ |
| 388 | public void postFrameCallbackDelayed(FrameCallback callback, long delayMillis) { |
| 389 | if (callback == null) { |
| 390 | throw new IllegalArgumentException("callback must not be null"); |
| 391 | } |
| 392 | |
| 393 | postCallbackDelayedInternal(CALLBACK_ANIMATION, |
| 394 | callback, FRAME_CALLBACK_TOKEN, delayMillis); |
| 395 | } |
| 396 | |
| 397 | /** |
| 398 | * Removes a previously posted frame callback. |
| 399 | * |
| 400 | * @param callback The frame callback to remove. |
| 401 | * |
| 402 | * @see #postFrameCallback |
| 403 | * @see #postFrameCallbackDelayed |
| 404 | */ |
| 405 | public void removeFrameCallback(FrameCallback callback) { |
| 406 | if (callback == null) { |
| 407 | throw new IllegalArgumentException("callback must not be null"); |
| 408 | } |
| 409 | |
| 410 | removeCallbacksInternal(CALLBACK_ANIMATION, callback, FRAME_CALLBACK_TOKEN); |
| 411 | } |
| 412 | |
| 413 | /** |
| 414 | * Gets the time when the current frame started. |
| 415 | * <p> |
| 416 | * This method provides the time in nanoseconds when the frame started being rendered. |
| 417 | * The frame time provides a stable time base for synchronizing animations |
| 418 | * and drawing. It should be used instead of {@link SystemClock#uptimeMillis()} |
| 419 | * or {@link System#nanoTime()} for animations and drawing in the UI. Using the frame |
| 420 | * time helps to reduce inter-frame jitter because the frame time is fixed at the time |
| 421 | * the frame was scheduled to start, regardless of when the animations or drawing |
| 422 | * callback actually runs. All callbacks that run as part of rendering a frame will |
| 423 | * observe the same frame time so using the frame time also helps to synchronize effects |
| 424 | * that are performed by different callbacks. |
| 425 | * </p><p> |
| 426 | * Please note that the framework already takes care to process animations and |
| 427 | * drawing using the frame time as a stable time base. Most applications should |
| 428 | * not need to use the frame time information directly. |
| 429 | * </p><p> |
Jeff Brown | 20c4f87 | 2012-04-26 17:38:21 -0700 | [diff] [blame] | 430 | * This method should only be called from within a callback. |
Jeff Brown | cae8049 | 2012-05-21 16:33:39 -0700 | [diff] [blame] | 431 | * </p> |
Jeff Brown | 20c4f87 | 2012-04-26 17:38:21 -0700 | [diff] [blame] | 432 | * |
| 433 | * @return The frame start time, in the {@link SystemClock#uptimeMillis()} time base. |
| 434 | * |
| 435 | * @throws IllegalStateException if no frame is in progress. |
Jeff Brown | cae8049 | 2012-05-21 16:33:39 -0700 | [diff] [blame] | 436 | * @hide |
Jeff Brown | 20c4f87 | 2012-04-26 17:38:21 -0700 | [diff] [blame] | 437 | */ |
| 438 | public long getFrameTime() { |
| 439 | return getFrameTimeNanos() / NANOS_PER_MS; |
| 440 | } |
| 441 | |
| 442 | /** |
| 443 | * Same as {@link #getFrameTime()} but with nanosecond precision. |
| 444 | * |
| 445 | * @return The frame start time, in the {@link System#nanoTime()} time base. |
| 446 | * |
| 447 | * @throws IllegalStateException if no frame is in progress. |
Jeff Brown | cae8049 | 2012-05-21 16:33:39 -0700 | [diff] [blame] | 448 | * @hide |
Jeff Brown | 20c4f87 | 2012-04-26 17:38:21 -0700 | [diff] [blame] | 449 | */ |
| 450 | public long getFrameTimeNanos() { |
| 451 | synchronized (mLock) { |
| 452 | if (!mCallbacksRunning) { |
| 453 | throw new IllegalStateException("This method must only be called as " |
| 454 | + "part of a callback while a frame is in progress."); |
| 455 | } |
| 456 | return USE_FRAME_TIME ? mLastFrameTimeNanos : System.nanoTime(); |
| 457 | } |
| 458 | } |
| 459 | |
Jeff Brown | ebb2d8d | 2012-03-23 17:14:34 -0700 | [diff] [blame] | 460 | private void scheduleFrameLocked(long now) { |
| 461 | if (!mFrameScheduled) { |
| 462 | mFrameScheduled = true; |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 463 | if (USE_VSYNC) { |
| 464 | if (DEBUG) { |
Jeff Brown | ebb2d8d | 2012-03-23 17:14:34 -0700 | [diff] [blame] | 465 | Log.d(TAG, "Scheduling next frame on vsync."); |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 466 | } |
Jeff Brown | 58aedbc | 2012-02-13 20:15:01 -0800 | [diff] [blame] | 467 | |
| 468 | // If running on the Looper thread, then schedule the vsync immediately, |
| 469 | // otherwise post a message to schedule the vsync from the UI thread |
| 470 | // as soon as possible. |
Jeff Brown | 58aedbc | 2012-02-13 20:15:01 -0800 | [diff] [blame] | 471 | if (isRunningOnLooperThreadLocked()) { |
Jeff Brown | 7ae9d5f | 2012-03-05 19:33:49 -0800 | [diff] [blame] | 472 | scheduleVsyncLocked(); |
Jeff Brown | 58aedbc | 2012-02-13 20:15:01 -0800 | [diff] [blame] | 473 | } else { |
Jeff Brown | e0dbd00 | 2012-02-15 19:34:58 -0800 | [diff] [blame] | 474 | Message msg = mHandler.obtainMessage(MSG_DO_SCHEDULE_VSYNC); |
| 475 | msg.setAsynchronous(true); |
| 476 | mHandler.sendMessageAtFrontOfQueue(msg); |
Jeff Brown | 58aedbc | 2012-02-13 20:15:01 -0800 | [diff] [blame] | 477 | } |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 478 | } else { |
Jeff Brown | 20c4f87 | 2012-04-26 17:38:21 -0700 | [diff] [blame] | 479 | final long nextFrameTime = Math.max( |
| 480 | mLastFrameTimeNanos / NANOS_PER_MS + sFrameDelay, now); |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 481 | if (DEBUG) { |
Jeff Brown | ebb2d8d | 2012-03-23 17:14:34 -0700 | [diff] [blame] | 482 | Log.d(TAG, "Scheduling next frame in " + (nextFrameTime - now) + " ms."); |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 483 | } |
Jeff Brown | ebb2d8d | 2012-03-23 17:14:34 -0700 | [diff] [blame] | 484 | Message msg = mHandler.obtainMessage(MSG_DO_FRAME); |
Jeff Brown | e0dbd00 | 2012-02-15 19:34:58 -0800 | [diff] [blame] | 485 | msg.setAsynchronous(true); |
Jeff Brown | ebb2d8d | 2012-03-23 17:14:34 -0700 | [diff] [blame] | 486 | mHandler.sendMessageAtTime(msg, nextFrameTime); |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 487 | } |
| 488 | } |
| 489 | } |
| 490 | |
Jeff Brown | cae8049 | 2012-05-21 16:33:39 -0700 | [diff] [blame] | 491 | void doFrame(long frameTimeNanos, int frame) { |
Jeff Brown | 59bbef0 | 2012-05-07 16:43:25 -0700 | [diff] [blame] | 492 | final long startNanos; |
Jeff Brown | ebb2d8d | 2012-03-23 17:14:34 -0700 | [diff] [blame] | 493 | synchronized (mLock) { |
| 494 | if (!mFrameScheduled) { |
| 495 | return; // no work to do |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 496 | } |
Jeff Brown | 59bbef0 | 2012-05-07 16:43:25 -0700 | [diff] [blame] | 497 | |
| 498 | startNanos = System.nanoTime(); |
Jeff Brown | cae8049 | 2012-05-21 16:33:39 -0700 | [diff] [blame] | 499 | final long jitterNanos = startNanos - frameTimeNanos; |
Jeff Brown | 59bbef0 | 2012-05-07 16:43:25 -0700 | [diff] [blame] | 500 | if (jitterNanos >= mFrameIntervalNanos) { |
Jeff Brown | 4fdf9c6 | 2012-06-11 15:25:48 -0700 | [diff] [blame] | 501 | final long skippedFrames = jitterNanos / mFrameIntervalNanos; |
| 502 | if (skippedFrames >= SKIPPED_FRAME_WARNING_LIMIT) { |
Jeff Brown | 265f1cc | 2012-06-11 18:01:06 -0700 | [diff] [blame] | 503 | Log.i(TAG, "Skipped " + skippedFrames + " frames! " |
Jeff Brown | 4fdf9c6 | 2012-06-11 15:25:48 -0700 | [diff] [blame] | 504 | + "The application may be doing too much work on its main thread."); |
| 505 | } |
Jeff Brown | 59bbef0 | 2012-05-07 16:43:25 -0700 | [diff] [blame] | 506 | final long lastFrameOffset = jitterNanos % mFrameIntervalNanos; |
| 507 | if (DEBUG) { |
| 508 | Log.d(TAG, "Missed vsync by " + (jitterNanos * 0.000001f) + " ms " |
| 509 | + "which is more than the frame interval of " |
| 510 | + (mFrameIntervalNanos * 0.000001f) + " ms! " |
Jeff Brown | 4fdf9c6 | 2012-06-11 15:25:48 -0700 | [diff] [blame] | 511 | + "Skipping " + skippedFrames + " frames and setting frame " |
| 512 | + "time to " + (lastFrameOffset * 0.000001f) + " ms in the past."); |
Jeff Brown | 59bbef0 | 2012-05-07 16:43:25 -0700 | [diff] [blame] | 513 | } |
Jeff Brown | cae8049 | 2012-05-21 16:33:39 -0700 | [diff] [blame] | 514 | frameTimeNanos = startNanos - lastFrameOffset; |
Jeff Brown | 59bbef0 | 2012-05-07 16:43:25 -0700 | [diff] [blame] | 515 | } |
| 516 | |
Jeff Brown | cae8049 | 2012-05-21 16:33:39 -0700 | [diff] [blame] | 517 | if (frameTimeNanos < mLastFrameTimeNanos) { |
Jeff Brown | 59bbef0 | 2012-05-07 16:43:25 -0700 | [diff] [blame] | 518 | if (DEBUG) { |
| 519 | Log.d(TAG, "Frame time appears to be going backwards. May be due to a " |
Jeff Brown | 4fdf9c6 | 2012-06-11 15:25:48 -0700 | [diff] [blame] | 520 | + "previously skipped frame. Waiting for next vsync."); |
Jeff Brown | 59bbef0 | 2012-05-07 16:43:25 -0700 | [diff] [blame] | 521 | } |
| 522 | scheduleVsyncLocked(); |
| 523 | return; |
| 524 | } |
| 525 | |
Jeff Brown | ebb2d8d | 2012-03-23 17:14:34 -0700 | [diff] [blame] | 526 | mFrameScheduled = false; |
Jeff Brown | cae8049 | 2012-05-21 16:33:39 -0700 | [diff] [blame] | 527 | mLastFrameTimeNanos = frameTimeNanos; |
Jeff Brown | 20c4f87 | 2012-04-26 17:38:21 -0700 | [diff] [blame] | 528 | } |
| 529 | |
Jeff Brown | cae8049 | 2012-05-21 16:33:39 -0700 | [diff] [blame] | 530 | doCallbacks(Choreographer.CALLBACK_INPUT, frameTimeNanos); |
| 531 | doCallbacks(Choreographer.CALLBACK_ANIMATION, frameTimeNanos); |
| 532 | doCallbacks(Choreographer.CALLBACK_TRAVERSAL, frameTimeNanos); |
Jeff Brown | ebb2d8d | 2012-03-23 17:14:34 -0700 | [diff] [blame] | 533 | |
| 534 | if (DEBUG) { |
Jeff Brown | 20c4f87 | 2012-04-26 17:38:21 -0700 | [diff] [blame] | 535 | final long endNanos = System.nanoTime(); |
Jeff Brown | ebb2d8d | 2012-03-23 17:14:34 -0700 | [diff] [blame] | 536 | Log.d(TAG, "Frame " + frame + ": Finished, took " |
Jeff Brown | 20c4f87 | 2012-04-26 17:38:21 -0700 | [diff] [blame] | 537 | + (endNanos - startNanos) * 0.000001f + " ms, latency " |
Jeff Brown | cae8049 | 2012-05-21 16:33:39 -0700 | [diff] [blame] | 538 | + (startNanos - frameTimeNanos) * 0.000001f + " ms."); |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 539 | } |
| 540 | } |
| 541 | |
Jeff Brown | cae8049 | 2012-05-21 16:33:39 -0700 | [diff] [blame] | 542 | void doCallbacks(int callbackType, long frameTimeNanos) { |
| 543 | CallbackRecord callbacks; |
Jeff Brown | 9685885 | 2012-02-14 13:45:06 -0800 | [diff] [blame] | 544 | synchronized (mLock) { |
Jeff Brown | cae8049 | 2012-05-21 16:33:39 -0700 | [diff] [blame] | 545 | // We use "now" to determine when callbacks become due because it's possible |
| 546 | // for earlier processing phases in a frame to post callbacks that should run |
| 547 | // in a following phase, such as an input event that causes an animation to start. |
Jeff Brown | 20c4f87 | 2012-04-26 17:38:21 -0700 | [diff] [blame] | 548 | final long now = SystemClock.uptimeMillis(); |
| 549 | callbacks = mCallbackQueues[callbackType].extractDueCallbacksLocked(now); |
| 550 | if (callbacks == null) { |
| 551 | return; |
| 552 | } |
| 553 | mCallbacksRunning = true; |
Jeff Brown | 9685885 | 2012-02-14 13:45:06 -0800 | [diff] [blame] | 554 | } |
Jeff Brown | 20c4f87 | 2012-04-26 17:38:21 -0700 | [diff] [blame] | 555 | try { |
Jeff Brown | cae8049 | 2012-05-21 16:33:39 -0700 | [diff] [blame] | 556 | for (CallbackRecord c = callbacks; c != null; c = c.next) { |
Jeff Brown | ebb2d8d | 2012-03-23 17:14:34 -0700 | [diff] [blame] | 557 | if (DEBUG) { |
| 558 | Log.d(TAG, "RunCallback: type=" + callbackType |
| 559 | + ", action=" + c.action + ", token=" + c.token |
| 560 | + ", latencyMillis=" + (SystemClock.uptimeMillis() - c.dueTime)); |
| 561 | } |
Jeff Brown | cae8049 | 2012-05-21 16:33:39 -0700 | [diff] [blame] | 562 | c.run(frameTimeNanos); |
Jeff Brown | ebb2d8d | 2012-03-23 17:14:34 -0700 | [diff] [blame] | 563 | } |
Jeff Brown | 20c4f87 | 2012-04-26 17:38:21 -0700 | [diff] [blame] | 564 | } finally { |
Jeff Brown | 9685885 | 2012-02-14 13:45:06 -0800 | [diff] [blame] | 565 | synchronized (mLock) { |
Jeff Brown | 20c4f87 | 2012-04-26 17:38:21 -0700 | [diff] [blame] | 566 | mCallbacksRunning = false; |
Jeff Brown | ebb2d8d | 2012-03-23 17:14:34 -0700 | [diff] [blame] | 567 | do { |
Jeff Brown | cae8049 | 2012-05-21 16:33:39 -0700 | [diff] [blame] | 568 | final CallbackRecord next = callbacks.next; |
Jeff Brown | ebb2d8d | 2012-03-23 17:14:34 -0700 | [diff] [blame] | 569 | recycleCallbackLocked(callbacks); |
| 570 | callbacks = next; |
| 571 | } while (callbacks != null); |
Jeff Brown | 9685885 | 2012-02-14 13:45:06 -0800 | [diff] [blame] | 572 | } |
| 573 | } |
Jeff Brown | 9685885 | 2012-02-14 13:45:06 -0800 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | void doScheduleVsync() { |
| 577 | synchronized (mLock) { |
Jeff Brown | ebb2d8d | 2012-03-23 17:14:34 -0700 | [diff] [blame] | 578 | if (mFrameScheduled) { |
Jeff Brown | 7ae9d5f | 2012-03-05 19:33:49 -0800 | [diff] [blame] | 579 | scheduleVsyncLocked(); |
| 580 | } |
Jeff Brown | 9685885 | 2012-02-14 13:45:06 -0800 | [diff] [blame] | 581 | } |
| 582 | } |
| 583 | |
Jeff Brown | ebb2d8d | 2012-03-23 17:14:34 -0700 | [diff] [blame] | 584 | void doScheduleCallback(int callbackType) { |
Jeff Brown | 7ae9d5f | 2012-03-05 19:33:49 -0800 | [diff] [blame] | 585 | synchronized (mLock) { |
Jeff Brown | ebb2d8d | 2012-03-23 17:14:34 -0700 | [diff] [blame] | 586 | if (!mFrameScheduled) { |
| 587 | final long now = SystemClock.uptimeMillis(); |
| 588 | if (mCallbackQueues[callbackType].hasDueCallbacksLocked(now)) { |
| 589 | scheduleFrameLocked(now); |
| 590 | } |
Jeff Brown | 7ae9d5f | 2012-03-05 19:33:49 -0800 | [diff] [blame] | 591 | } |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 592 | } |
| 593 | } |
| 594 | |
Jeff Brown | 7ae9d5f | 2012-03-05 19:33:49 -0800 | [diff] [blame] | 595 | private void scheduleVsyncLocked() { |
| 596 | mDisplayEventReceiver.scheduleVsync(); |
| 597 | } |
| 598 | |
Jeff Brown | 58aedbc | 2012-02-13 20:15:01 -0800 | [diff] [blame] | 599 | private boolean isRunningOnLooperThreadLocked() { |
| 600 | return Looper.myLooper() == mLooper; |
| 601 | } |
| 602 | |
Jeff Brown | cae8049 | 2012-05-21 16:33:39 -0700 | [diff] [blame] | 603 | private CallbackRecord obtainCallbackLocked(long dueTime, Object action, Object token) { |
| 604 | CallbackRecord callback = mCallbackPool; |
Jeff Brown | 9685885 | 2012-02-14 13:45:06 -0800 | [diff] [blame] | 605 | if (callback == null) { |
Jeff Brown | cae8049 | 2012-05-21 16:33:39 -0700 | [diff] [blame] | 606 | callback = new CallbackRecord(); |
Jeff Brown | 9685885 | 2012-02-14 13:45:06 -0800 | [diff] [blame] | 607 | } else { |
| 608 | mCallbackPool = callback.next; |
| 609 | callback.next = null; |
| 610 | } |
Jeff Brown | 7ae9d5f | 2012-03-05 19:33:49 -0800 | [diff] [blame] | 611 | callback.dueTime = dueTime; |
| 612 | callback.action = action; |
| 613 | callback.token = token; |
Jeff Brown | 9685885 | 2012-02-14 13:45:06 -0800 | [diff] [blame] | 614 | return callback; |
| 615 | } |
| 616 | |
Jeff Brown | cae8049 | 2012-05-21 16:33:39 -0700 | [diff] [blame] | 617 | private void recycleCallbackLocked(CallbackRecord callback) { |
Jeff Brown | 7ae9d5f | 2012-03-05 19:33:49 -0800 | [diff] [blame] | 618 | callback.action = null; |
| 619 | callback.token = null; |
Jeff Brown | 9685885 | 2012-02-14 13:45:06 -0800 | [diff] [blame] | 620 | callback.next = mCallbackPool; |
| 621 | mCallbackPool = callback; |
| 622 | } |
| 623 | |
Jeff Brown | cae8049 | 2012-05-21 16:33:39 -0700 | [diff] [blame] | 624 | /** |
| 625 | * Implement this interface to receive a callback when a new display frame is |
| 626 | * being rendered. The callback is invoked on the {@link Looper} thread to |
| 627 | * which the {@link Choreographer} is attached. |
| 628 | */ |
| 629 | public interface FrameCallback { |
| 630 | /** |
| 631 | * Called when a new display frame is being rendered. |
| 632 | * <p> |
| 633 | * This method provides the time in nanoseconds when the frame started being rendered. |
| 634 | * The frame time provides a stable time base for synchronizing animations |
| 635 | * and drawing. It should be used instead of {@link SystemClock#uptimeMillis()} |
| 636 | * or {@link System#nanoTime()} for animations and drawing in the UI. Using the frame |
| 637 | * time helps to reduce inter-frame jitter because the frame time is fixed at the time |
| 638 | * the frame was scheduled to start, regardless of when the animations or drawing |
| 639 | * callback actually runs. All callbacks that run as part of rendering a frame will |
| 640 | * observe the same frame time so using the frame time also helps to synchronize effects |
| 641 | * that are performed by different callbacks. |
| 642 | * </p><p> |
| 643 | * Please note that the framework already takes care to process animations and |
| 644 | * drawing using the frame time as a stable time base. Most applications should |
| 645 | * not need to use the frame time information directly. |
| 646 | * </p> |
| 647 | * |
| 648 | * @param frameTimeNanos The time in nanoseconds when the frame started being rendered, |
| 649 | * in the {@link System#nanoTime()} timebase. Divide this value by {@code 1000000} |
| 650 | * to convert it to the {@link SystemClock#uptimeMillis()} time base. |
| 651 | */ |
| 652 | public void doFrame(long frameTimeNanos); |
| 653 | } |
| 654 | |
Jeff Brown | 9685885 | 2012-02-14 13:45:06 -0800 | [diff] [blame] | 655 | private final class FrameHandler extends Handler { |
| 656 | public FrameHandler(Looper looper) { |
| 657 | super(looper); |
| 658 | } |
| 659 | |
| 660 | @Override |
| 661 | public void handleMessage(Message msg) { |
| 662 | switch (msg.what) { |
Jeff Brown | ebb2d8d | 2012-03-23 17:14:34 -0700 | [diff] [blame] | 663 | case MSG_DO_FRAME: |
Jeff Brown | 20c4f87 | 2012-04-26 17:38:21 -0700 | [diff] [blame] | 664 | doFrame(System.nanoTime(), 0); |
Jeff Brown | 9685885 | 2012-02-14 13:45:06 -0800 | [diff] [blame] | 665 | break; |
| 666 | case MSG_DO_SCHEDULE_VSYNC: |
| 667 | doScheduleVsync(); |
| 668 | break; |
Jeff Brown | ebb2d8d | 2012-03-23 17:14:34 -0700 | [diff] [blame] | 669 | case MSG_DO_SCHEDULE_CALLBACK: |
| 670 | doScheduleCallback(msg.arg1); |
Jeff Brown | 2b6cb9a | 2012-03-05 17:21:01 -0800 | [diff] [blame] | 671 | break; |
Jeff Brown | 9685885 | 2012-02-14 13:45:06 -0800 | [diff] [blame] | 672 | } |
| 673 | } |
| 674 | } |
| 675 | |
Jeff Brown | b080660 | 2012-05-16 12:50:41 -0700 | [diff] [blame] | 676 | private final class FrameDisplayEventReceiver extends DisplayEventReceiver |
| 677 | implements Runnable { |
Jeff Brown | 4fdf9c6 | 2012-06-11 15:25:48 -0700 | [diff] [blame] | 678 | private boolean mHavePendingVsync; |
Jeff Brown | b080660 | 2012-05-16 12:50:41 -0700 | [diff] [blame] | 679 | private long mTimestampNanos; |
| 680 | private int mFrame; |
| 681 | |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 682 | public FrameDisplayEventReceiver(Looper looper) { |
| 683 | super(looper); |
| 684 | } |
| 685 | |
| 686 | @Override |
Jeff Brown | e87bf03 | 2012-09-20 18:30:13 -0700 | [diff] [blame] | 687 | public void onVsync(long timestampNanos, int builtInDisplayId, int frame) { |
| 688 | // Ignore vsync from secondary display. |
| 689 | // This can be problematic because the call to scheduleVsync() is a one-shot. |
| 690 | // We need to ensure that we will still receive the vsync from the primary |
| 691 | // display which is the one we really care about. Ideally we should schedule |
| 692 | // vsync for a particular display. |
| 693 | // At this time Surface Flinger won't send us vsyncs for secondary displays |
| 694 | // but that could change in the future so let's log a message to help us remember |
| 695 | // that we need to fix this. |
Mathias Agopian | 3866f0d | 2013-02-11 22:08:48 -0800 | [diff] [blame^] | 696 | if (builtInDisplayId != SurfaceControl.BUILT_IN_DISPLAY_ID_MAIN) { |
Jeff Brown | e87bf03 | 2012-09-20 18:30:13 -0700 | [diff] [blame] | 697 | Log.d(TAG, "Received vsync from secondary display, but we don't support " |
| 698 | + "this case yet. Choreographer needs a way to explicitly request " |
| 699 | + "vsync for a specific display to ensure it doesn't lose track " |
| 700 | + "of its scheduled vsync."); |
| 701 | scheduleVsync(); |
| 702 | return; |
| 703 | } |
| 704 | |
Jeff Brown | b080660 | 2012-05-16 12:50:41 -0700 | [diff] [blame] | 705 | // Post the vsync event to the Handler. |
| 706 | // The idea is to prevent incoming vsync events from completely starving |
| 707 | // the message queue. If there are no messages in the queue with timestamps |
| 708 | // earlier than the frame time, then the vsync event will be processed immediately. |
| 709 | // Otherwise, messages that predate the vsync event will be handled first. |
Jeff Brown | 4fdf9c6 | 2012-06-11 15:25:48 -0700 | [diff] [blame] | 710 | long now = System.nanoTime(); |
| 711 | if (timestampNanos > now) { |
| 712 | Log.w(TAG, "Frame time is " + ((timestampNanos - now) * 0.000001f) |
| 713 | + " ms in the future! Check that graphics HAL is generating vsync " |
| 714 | + "timestamps using the correct timebase."); |
| 715 | timestampNanos = now; |
| 716 | } |
| 717 | |
Jeff Brown | ba726113 | 2012-06-14 23:48:40 -0700 | [diff] [blame] | 718 | if (mHavePendingVsync) { |
| 719 | Log.w(TAG, "Already have a pending vsync event. There should only be " |
| 720 | + "one at a time."); |
| 721 | } else { |
| 722 | mHavePendingVsync = true; |
| 723 | } |
Jeff Brown | 4fdf9c6 | 2012-06-11 15:25:48 -0700 | [diff] [blame] | 724 | |
Jeff Brown | b080660 | 2012-05-16 12:50:41 -0700 | [diff] [blame] | 725 | mTimestampNanos = timestampNanos; |
| 726 | mFrame = frame; |
| 727 | Message msg = Message.obtain(mHandler, this); |
| 728 | msg.setAsynchronous(true); |
| 729 | mHandler.sendMessageAtTime(msg, timestampNanos / NANOS_PER_MS); |
| 730 | } |
| 731 | |
| 732 | @Override |
| 733 | public void run() { |
Jeff Brown | 4fdf9c6 | 2012-06-11 15:25:48 -0700 | [diff] [blame] | 734 | mHavePendingVsync = false; |
Jeff Brown | b080660 | 2012-05-16 12:50:41 -0700 | [diff] [blame] | 735 | doFrame(mTimestampNanos, mFrame); |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 736 | } |
| 737 | } |
Jeff Brown | 9685885 | 2012-02-14 13:45:06 -0800 | [diff] [blame] | 738 | |
Jeff Brown | cae8049 | 2012-05-21 16:33:39 -0700 | [diff] [blame] | 739 | private static final class CallbackRecord { |
| 740 | public CallbackRecord next; |
Jeff Brown | 7ae9d5f | 2012-03-05 19:33:49 -0800 | [diff] [blame] | 741 | public long dueTime; |
Jeff Brown | cae8049 | 2012-05-21 16:33:39 -0700 | [diff] [blame] | 742 | public Object action; // Runnable or FrameCallback |
Jeff Brown | 7ae9d5f | 2012-03-05 19:33:49 -0800 | [diff] [blame] | 743 | public Object token; |
Jeff Brown | cae8049 | 2012-05-21 16:33:39 -0700 | [diff] [blame] | 744 | |
| 745 | public void run(long frameTimeNanos) { |
| 746 | if (token == FRAME_CALLBACK_TOKEN) { |
| 747 | ((FrameCallback)action).doFrame(frameTimeNanos); |
| 748 | } else { |
| 749 | ((Runnable)action).run(); |
| 750 | } |
| 751 | } |
Jeff Brown | 9685885 | 2012-02-14 13:45:06 -0800 | [diff] [blame] | 752 | } |
Jeff Brown | 43ea54b | 2012-03-09 14:37:48 -0800 | [diff] [blame] | 753 | |
| 754 | private final class CallbackQueue { |
Jeff Brown | cae8049 | 2012-05-21 16:33:39 -0700 | [diff] [blame] | 755 | private CallbackRecord mHead; |
Jeff Brown | 43ea54b | 2012-03-09 14:37:48 -0800 | [diff] [blame] | 756 | |
| 757 | public boolean hasDueCallbacksLocked(long now) { |
| 758 | return mHead != null && mHead.dueTime <= now; |
| 759 | } |
| 760 | |
Jeff Brown | cae8049 | 2012-05-21 16:33:39 -0700 | [diff] [blame] | 761 | public CallbackRecord extractDueCallbacksLocked(long now) { |
| 762 | CallbackRecord callbacks = mHead; |
Jeff Brown | 43ea54b | 2012-03-09 14:37:48 -0800 | [diff] [blame] | 763 | if (callbacks == null || callbacks.dueTime > now) { |
| 764 | return null; |
| 765 | } |
| 766 | |
Jeff Brown | cae8049 | 2012-05-21 16:33:39 -0700 | [diff] [blame] | 767 | CallbackRecord last = callbacks; |
| 768 | CallbackRecord next = last.next; |
Jeff Brown | 43ea54b | 2012-03-09 14:37:48 -0800 | [diff] [blame] | 769 | while (next != null) { |
| 770 | if (next.dueTime > now) { |
| 771 | last.next = null; |
| 772 | break; |
| 773 | } |
| 774 | last = next; |
| 775 | next = next.next; |
| 776 | } |
| 777 | mHead = next; |
| 778 | return callbacks; |
| 779 | } |
| 780 | |
Jeff Brown | cae8049 | 2012-05-21 16:33:39 -0700 | [diff] [blame] | 781 | public void addCallbackLocked(long dueTime, Object action, Object token) { |
| 782 | CallbackRecord callback = obtainCallbackLocked(dueTime, action, token); |
| 783 | CallbackRecord entry = mHead; |
Jeff Brown | 43ea54b | 2012-03-09 14:37:48 -0800 | [diff] [blame] | 784 | if (entry == null) { |
| 785 | mHead = callback; |
| 786 | return; |
| 787 | } |
| 788 | if (dueTime < entry.dueTime) { |
| 789 | callback.next = entry; |
| 790 | mHead = callback; |
| 791 | return; |
| 792 | } |
| 793 | while (entry.next != null) { |
| 794 | if (dueTime < entry.next.dueTime) { |
| 795 | callback.next = entry.next; |
| 796 | break; |
| 797 | } |
| 798 | entry = entry.next; |
| 799 | } |
| 800 | entry.next = callback; |
| 801 | } |
| 802 | |
Jeff Brown | cae8049 | 2012-05-21 16:33:39 -0700 | [diff] [blame] | 803 | public void removeCallbacksLocked(Object action, Object token) { |
| 804 | CallbackRecord predecessor = null; |
| 805 | for (CallbackRecord callback = mHead; callback != null;) { |
| 806 | final CallbackRecord next = callback.next; |
Jeff Brown | 43ea54b | 2012-03-09 14:37:48 -0800 | [diff] [blame] | 807 | if ((action == null || callback.action == action) |
| 808 | && (token == null || callback.token == token)) { |
| 809 | if (predecessor != null) { |
| 810 | predecessor.next = next; |
| 811 | } else { |
| 812 | mHead = next; |
| 813 | } |
| 814 | recycleCallbackLocked(callback); |
| 815 | } else { |
| 816 | predecessor = callback; |
| 817 | } |
| 818 | callback = next; |
| 819 | } |
| 820 | } |
| 821 | } |
Jeff Brown | 96e942d | 2011-11-30 19:55:01 -0800 | [diff] [blame] | 822 | } |