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