blob: c64386e8db790d17bf1938ae4f1d66b720fde819 [file] [log] [blame]
Jeff Brown96e942d2011-11-30 19:55:01 -08001/*
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
17package android.view;
18
Jorim Jaggi34a0cdb2017-06-08 15:40:38 -070019import static android.view.DisplayEventReceiver.VSYNC_SOURCE_APP;
20import static android.view.DisplayEventReceiver.VSYNC_SOURCE_SURFACE_FLINGER;
21
Doris Liu197a6742017-08-16 17:12:42 -070022import android.annotation.TestApi;
Mathew Inwooda570dee2018-08-17 14:56:00 +010023import android.annotation.UnsupportedAppUsage;
John Reck8785ceb2018-10-29 16:45:58 -070024import android.graphics.FrameInfo;
Jeff Brownbd6e1502012-08-28 03:27:37 -070025import android.hardware.display.DisplayManagerGlobal;
Mathew Inwood8c854f82018-09-14 12:35:36 +010026import android.os.Build;
Jeff Brown96e942d2011-11-30 19:55:01 -080027import android.os.Handler;
28import android.os.Looper;
29import android.os.Message;
30import android.os.SystemClock;
31import android.os.SystemProperties;
Chris Craike22c59b2015-05-21 18:33:37 -070032import android.os.Trace;
Jeff Brown96e942d2011-11-30 19:55:01 -080033import android.util.Log;
Jeff Brown5182c782013-10-15 20:31:52 -070034import android.util.TimeUtils;
John Recka2acb4f2016-08-05 07:58:37 -070035import android.view.animation.AnimationUtils;
Jeff Brown5182c782013-10-15 20:31:52 -070036
37import java.io.PrintWriter;
Jeff Brown96e942d2011-11-30 19:55:01 -080038
39/**
Jeff Browncae80492012-05-21 16:33:39 -070040 * Coordinates the timing of animations, input and drawing.
41 * <p>
42 * The choreographer receives timing pulses (such as vertical synchronization)
43 * from the display subsystem then schedules work to occur as part of rendering
44 * the next display frame.
45 * </p><p>
46 * Applications typically interact with the choreographer indirectly using
47 * higher level abstractions in the animation framework or the view hierarchy.
48 * Here are some examples of things you can do using the higher-level APIs.
49 * </p>
50 * <ul>
51 * <li>To post an animation to be processed on a regular time basis synchronized with
52 * display frame rendering, use {@link android.animation.ValueAnimator#start}.</li>
53 * <li>To post a {@link Runnable} to be invoked once at the beginning of the next display
54 * frame, use {@link View#postOnAnimation}.</li>
55 * <li>To post a {@link Runnable} to be invoked once at the beginning of the next display
56 * frame after a delay, use {@link View#postOnAnimationDelayed}.</li>
57 * <li>To post a call to {@link View#invalidate()} to occur once at the beginning of the
58 * next display frame, use {@link View#postInvalidateOnAnimation()} or
59 * {@link View#postInvalidateOnAnimation(int, int, int, int)}.</li>
60 * <li>To ensure that the contents of a {@link View} scroll smoothly and are drawn in
61 * sync with display frame rendering, do nothing. This already happens automatically.
62 * {@link View#onDraw} will be called at the appropriate time.</li>
63 * </ul>
64 * <p>
65 * However, there are a few cases where you might want to use the functions of the
66 * choreographer directly in your application. Here are some examples.
67 * </p>
68 * <ul>
69 * <li>If your application does its rendering in a different thread, possibly using GL,
70 * or does not use the animation framework or view hierarchy at all
71 * and you want to ensure that it is appropriately synchronized with the display, then use
72 * {@link Choreographer#postFrameCallback}.</li>
73 * <li>... and that's about it.</li>
74 * </ul>
75 * <p>
76 * Each {@link Looper} thread has its own choreographer. Other threads can
77 * post callbacks to run on the choreographer but they will run on the {@link Looper}
78 * to which the choreographer belongs.
79 * </p>
Jeff Brown96e942d2011-11-30 19:55:01 -080080 */
Jeff Brown96858852012-02-14 13:45:06 -080081public final class Choreographer {
Jeff Brown96e942d2011-11-30 19:55:01 -080082 private static final String TAG = "Choreographer";
Jeff Brownc42b28d2015-04-06 19:49:02 -070083
84 // Prints debug messages about jank which was detected (low volume).
85 private static final boolean DEBUG_JANK = false;
86
87 // Prints debug messages about every frame and callback registered (high volume).
88 private static final boolean DEBUG_FRAMES = false;
Jeff Brown96e942d2011-11-30 19:55:01 -080089
90 // The default amount of time in ms between animation frames.
91 // When vsync is not enabled, we want to have some idea of how long we should
92 // wait before posting the next animation message. It is important that the
93 // default value be less than the true inter-frame delay on all devices to avoid
94 // situations where we might skip frames by waiting too long (we must compensate
95 // for jitter and hardware variations). Regardless of this value, the animation
96 // and display loop is ultimately rate-limited by how fast new graphics buffers can
97 // be dequeued.
98 private static final long DEFAULT_FRAME_DELAY = 10;
99
100 // The number of milliseconds between animation frames.
Jeff Brown87d0b032012-02-03 11:01:21 -0800101 private static volatile long sFrameDelay = DEFAULT_FRAME_DELAY;
Jeff Brown96e942d2011-11-30 19:55:01 -0800102
103 // Thread local storage for the choreographer.
104 private static final ThreadLocal<Choreographer> sThreadInstance =
105 new ThreadLocal<Choreographer>() {
106 @Override
107 protected Choreographer initialValue() {
108 Looper looper = Looper.myLooper();
109 if (looper == null) {
110 throw new IllegalStateException("The current thread must have a looper!");
111 }
Jorim Jaggib29e3182018-04-30 18:51:56 +0200112 Choreographer choreographer = new Choreographer(looper, VSYNC_SOURCE_APP);
113 if (looper == Looper.getMainLooper()) {
114 mMainInstance = choreographer;
115 }
116 return choreographer;
Jeff Brown96e942d2011-11-30 19:55:01 -0800117 }
118 };
119
Jorim Jaggib29e3182018-04-30 18:51:56 +0200120 private static volatile Choreographer mMainInstance;
121
Jorim Jaggi34a0cdb2017-06-08 15:40:38 -0700122 // Thread local storage for the SF choreographer.
123 private static final ThreadLocal<Choreographer> sSfThreadInstance =
124 new ThreadLocal<Choreographer>() {
125 @Override
126 protected Choreographer initialValue() {
127 Looper looper = Looper.myLooper();
128 if (looper == null) {
129 throw new IllegalStateException("The current thread must have a looper!");
130 }
131 return new Choreographer(looper, VSYNC_SOURCE_SURFACE_FLINGER);
132 }
133 };
134
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700135 // Enable/disable vsync for animations and drawing.
Nader Jawad21a4f7c2019-02-06 14:10:06 -0800136 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 123769497)
Jeff Brown96e942d2011-11-30 19:55:01 -0800137 private static final boolean USE_VSYNC = SystemProperties.getBoolean(
138 "debug.choreographer.vsync", true);
139
Jeff Brown20c4f872012-04-26 17:38:21 -0700140 // Enable/disable using the frame time instead of returning now.
141 private static final boolean USE_FRAME_TIME = SystemProperties.getBoolean(
142 "debug.choreographer.frametime", true);
143
Jeff Brown4fdf9c62012-06-11 15:25:48 -0700144 // Set a limit to warn about skipped frames.
145 // Skipped frames imply jank.
146 private static final int SKIPPED_FRAME_WARNING_LIMIT = SystemProperties.getInt(
147 "debug.choreographer.skipwarning", 30);
148
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700149 private static final int MSG_DO_FRAME = 0;
150 private static final int MSG_DO_SCHEDULE_VSYNC = 1;
151 private static final int MSG_DO_SCHEDULE_CALLBACK = 2;
Jeff Brown96e942d2011-11-30 19:55:01 -0800152
Jeff Browncae80492012-05-21 16:33:39 -0700153 // All frame callbacks posted by applications have this token.
154 private static final Object FRAME_CALLBACK_TOKEN = new Object() {
155 public String toString() { return "FRAME_CALLBACK_TOKEN"; }
156 };
157
Mathew Inwood8c854f82018-09-14 12:35:36 +0100158 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Jeff Brown87d0b032012-02-03 11:01:21 -0800159 private final Object mLock = new Object();
160
Jeff Brown96e942d2011-11-30 19:55:01 -0800161 private final Looper mLooper;
Jeff Brown96858852012-02-14 13:45:06 -0800162 private final FrameHandler mHandler;
Jeff Browncae80492012-05-21 16:33:39 -0700163
164 // The display event receiver can only be accessed by the looper thread to which
165 // it is attached. We take care to ensure that we post message to the looper
166 // if appropriate when interacting with the display event receiver.
Mathew Inwooda570dee2018-08-17 14:56:00 +0100167 @UnsupportedAppUsage
Jeff Brown1654d0b2012-02-15 15:40:52 -0800168 private final FrameDisplayEventReceiver mDisplayEventReceiver;
Jeff Brown96858852012-02-14 13:45:06 -0800169
Jeff Browncae80492012-05-21 16:33:39 -0700170 private CallbackRecord mCallbackPool;
Jeff Brown96e942d2011-11-30 19:55:01 -0800171
Mathew Inwooda570dee2018-08-17 14:56:00 +0100172 @UnsupportedAppUsage
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700173 private final CallbackQueue[] mCallbackQueues;
Jeff Brown96858852012-02-14 13:45:06 -0800174
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700175 private boolean mFrameScheduled;
Jeff Brown20c4f872012-04-26 17:38:21 -0700176 private boolean mCallbacksRunning;
Mathew Inwooda570dee2018-08-17 14:56:00 +0100177 @UnsupportedAppUsage
Jeff Brown20c4f872012-04-26 17:38:21 -0700178 private long mLastFrameTimeNanos;
Mathew Inwooda570dee2018-08-17 14:56:00 +0100179 @UnsupportedAppUsage
Jeff Brown59bbef02012-05-07 16:43:25 -0700180 private long mFrameIntervalNanos;
Jeff Brownc42b28d2015-04-06 19:49:02 -0700181 private boolean mDebugPrintNextFrameTimeDelta;
John Reck9f516442017-09-25 10:27:21 -0700182 private int mFPSDivisor = 1;
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700183
184 /**
John Reckba6adf62015-02-19 14:36:50 -0800185 * Contains information about the current frame for jank-tracking,
186 * mainly timings of key events along with a bit of metadata about
187 * view tree state
188 *
189 * TODO: Is there a better home for this? Currently Choreographer
190 * is the only one with CALLBACK_ANIMATION start time, hence why this
191 * resides here.
192 *
193 * @hide
194 */
195 FrameInfo mFrameInfo = new FrameInfo();
196
197 /**
Chris Craike22c59b2015-05-21 18:33:37 -0700198 * Must be kept in sync with CALLBACK_* ints below, used to index into this array.
199 * @hide
200 */
201 private static final String[] CALLBACK_TRACE_TITLES = {
Jorim Jaggi02a741f2018-12-12 17:40:19 -0800202 "input", "animation", "insets_animation", "traversal", "commit"
Chris Craike22c59b2015-05-21 18:33:37 -0700203 };
204
205 /**
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700206 * Callback type: Input callback. Runs first.
Jeff Browncae80492012-05-21 16:33:39 -0700207 * @hide
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700208 */
209 public static final int CALLBACK_INPUT = 0;
210
211 /**
Jorim Jaggi02a741f2018-12-12 17:40:19 -0800212 * Callback type: Animation callback. Runs before {@link #CALLBACK_INSETS_ANIMATION}.
Jeff Browncae80492012-05-21 16:33:39 -0700213 * @hide
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700214 */
Doris Liu197a6742017-08-16 17:12:42 -0700215 @TestApi
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700216 public static final int CALLBACK_ANIMATION = 1;
217
218 /**
Jorim Jaggi02a741f2018-12-12 17:40:19 -0800219 * Callback type: Animation callback to handle inset updates. This is separate from
220 * {@link #CALLBACK_ANIMATION} as we need to "gather" all inset animation updates via
221 * {@link WindowInsetsAnimationController#changeInsets} for multiple ongoing animations but then
222 * update the whole view system with a single callback to {@link View#dispatchWindowInsetsAnimationProgress}
223 * that contains all the combined updated insets.
224 * <p>
225 * Both input and animation may change insets, so we need to run this after these callbacks, but
226 * before traversals.
227 * <p>
228 * Runs before traversals.
229 * @hide
230 */
231 public static final int CALLBACK_INSETS_ANIMATION = 2;
232
233 /**
Jeff Brownc42b28d2015-04-06 19:49:02 -0700234 * Callback type: Traversal callback. Handles layout and draw. Runs
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700235 * after all other asynchronous messages have been handled.
Jeff Browncae80492012-05-21 16:33:39 -0700236 * @hide
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700237 */
Jorim Jaggi02a741f2018-12-12 17:40:19 -0800238 public static final int CALLBACK_TRAVERSAL = 3;
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700239
Jeff Brownc42b28d2015-04-06 19:49:02 -0700240 /**
241 * Callback type: Commit callback. Handles post-draw operations for the frame.
242 * Runs after traversal completes. The {@link #getFrameTime() frame time} reported
243 * during this callback may be updated to reflect delays that occurred while
244 * traversals were in progress in case heavy layout operations caused some frames
245 * to be skipped. The frame time reported during this callback provides a better
246 * estimate of the start time of the frame in which animations (and other updates
247 * to the view hierarchy state) actually took effect.
248 * @hide
249 */
Jorim Jaggi02a741f2018-12-12 17:40:19 -0800250 public static final int CALLBACK_COMMIT = 4;
Jeff Brownc42b28d2015-04-06 19:49:02 -0700251
252 private static final int CALLBACK_LAST = CALLBACK_COMMIT;
Jeff Brown96e942d2011-11-30 19:55:01 -0800253
Jorim Jaggi34a0cdb2017-06-08 15:40:38 -0700254 private Choreographer(Looper looper, int vsyncSource) {
Jeff Brown96e942d2011-11-30 19:55:01 -0800255 mLooper = looper;
Jeff Brown96858852012-02-14 13:45:06 -0800256 mHandler = new FrameHandler(looper);
Jorim Jaggi34a0cdb2017-06-08 15:40:38 -0700257 mDisplayEventReceiver = USE_VSYNC
258 ? new FrameDisplayEventReceiver(looper, vsyncSource)
259 : null;
Jeff Brown20c4f872012-04-26 17:38:21 -0700260 mLastFrameTimeNanos = Long.MIN_VALUE;
Jeff Brownd32460c2012-07-20 16:15:36 -0700261
Jeff Brownbd6e1502012-08-28 03:27:37 -0700262 mFrameIntervalNanos = (long)(1000000000 / getRefreshRate());
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700263
264 mCallbackQueues = new CallbackQueue[CALLBACK_LAST + 1];
265 for (int i = 0; i <= CALLBACK_LAST; i++) {
266 mCallbackQueues[i] = new CallbackQueue();
267 }
Makoto Onukib708cbe2018-01-09 12:35:06 -0800268 // b/68769804: For low FPS experiments.
269 setFPSDivisor(SystemProperties.getInt(ThreadedRenderer.DEBUG_FPS_DIVISOR, 1));
Jeff Brown96e942d2011-11-30 19:55:01 -0800270 }
271
Jeff Brownbd6e1502012-08-28 03:27:37 -0700272 private static float getRefreshRate() {
273 DisplayInfo di = DisplayManagerGlobal.getInstance().getDisplayInfo(
274 Display.DEFAULT_DISPLAY);
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700275 return di.getMode().getRefreshRate();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700276 }
277
Jeff Brown96e942d2011-11-30 19:55:01 -0800278 /**
Dianne Hackborn8bcd54b2012-01-31 19:04:53 -0800279 * Gets the choreographer for the calling thread. Must be called from
280 * a thread that already has a {@link android.os.Looper} associated with it.
Jeff Brown96e942d2011-11-30 19:55:01 -0800281 *
282 * @return The choreographer for this thread.
283 * @throws IllegalStateException if the thread does not have a looper.
284 */
285 public static Choreographer getInstance() {
286 return sThreadInstance.get();
287 }
288
Jorim Jaggi34a0cdb2017-06-08 15:40:38 -0700289 /**
290 * @hide
291 */
Mathew Inwooda570dee2018-08-17 14:56:00 +0100292 @UnsupportedAppUsage
Jorim Jaggi34a0cdb2017-06-08 15:40:38 -0700293 public static Choreographer getSfInstance() {
294 return sSfThreadInstance.get();
295 }
296
Jorim Jaggib29e3182018-04-30 18:51:56 +0200297 /**
298 * @return The Choreographer of the main thread, if it exists, or {@code null} otherwise.
299 * @hide
300 */
301 public static Choreographer getMainThreadInstance() {
302 return mMainInstance;
303 }
304
John Reckac04f4e2016-06-23 10:21:45 -0700305 /** Destroys the calling thread's choreographer
306 * @hide
307 */
308 public static void releaseInstance() {
309 Choreographer old = sThreadInstance.get();
310 sThreadInstance.remove();
311 old.dispose();
312 }
313
314 private void dispose() {
315 mDisplayEventReceiver.dispose();
316 }
317
Jeff Brown96e942d2011-11-30 19:55:01 -0800318 /**
Jeff Browncae80492012-05-21 16:33:39 -0700319 * The amount of time, in milliseconds, between each frame of the animation.
320 * <p>
321 * This is a requested time that the animation will attempt to honor, but the actual delay
322 * between frames may be different, depending on system load and capabilities. This is a static
Jeff Brown96e942d2011-11-30 19:55:01 -0800323 * function because the same delay will be applied to all animations, since they are all
324 * run off of a single timing loop.
Jeff Browncae80492012-05-21 16:33:39 -0700325 * </p><p>
Jeff Brown96e942d2011-11-30 19:55:01 -0800326 * The frame delay may be ignored when the animation system uses an external timing
327 * source, such as the display refresh rate (vsync), to govern animations.
Jeff Browncae80492012-05-21 16:33:39 -0700328 * </p>
Jeff Brown96e942d2011-11-30 19:55:01 -0800329 *
330 * @return the requested time between frames, in milliseconds
Jeff Browncae80492012-05-21 16:33:39 -0700331 * @hide
Jeff Brown96e942d2011-11-30 19:55:01 -0800332 */
Doris Liu197a6742017-08-16 17:12:42 -0700333 @TestApi
Jeff Brown96e942d2011-11-30 19:55:01 -0800334 public static long getFrameDelay() {
335 return sFrameDelay;
336 }
337
338 /**
Jeff Browncae80492012-05-21 16:33:39 -0700339 * The amount of time, in milliseconds, between each frame of the animation.
340 * <p>
341 * This is a requested time that the animation will attempt to honor, but the actual delay
342 * between frames may be different, depending on system load and capabilities. This is a static
Jeff Brown96e942d2011-11-30 19:55:01 -0800343 * function because the same delay will be applied to all animations, since they are all
344 * run off of a single timing loop.
Jeff Browncae80492012-05-21 16:33:39 -0700345 * </p><p>
Jeff Brown96e942d2011-11-30 19:55:01 -0800346 * The frame delay may be ignored when the animation system uses an external timing
347 * source, such as the display refresh rate (vsync), to govern animations.
Jeff Browncae80492012-05-21 16:33:39 -0700348 * </p>
Jeff Brown96e942d2011-11-30 19:55:01 -0800349 *
350 * @param frameDelay the requested time between frames, in milliseconds
Jeff Browncae80492012-05-21 16:33:39 -0700351 * @hide
Jeff Brown96e942d2011-11-30 19:55:01 -0800352 */
Doris Liu197a6742017-08-16 17:12:42 -0700353 @TestApi
Jeff Brown96e942d2011-11-30 19:55:01 -0800354 public static void setFrameDelay(long frameDelay) {
355 sFrameDelay = frameDelay;
356 }
357
358 /**
Jeff Brown7ae9d5f2012-03-05 19:33:49 -0800359 * Subtracts typical frame delay time from a delay interval in milliseconds.
Jeff Browncae80492012-05-21 16:33:39 -0700360 * <p>
Jeff Brown7ae9d5f2012-03-05 19:33:49 -0800361 * This method can be used to compensate for animation delay times that have baked
362 * in assumptions about the frame delay. For example, it's quite common for code to
363 * assume a 60Hz frame time and bake in a 16ms delay. When we call
364 * {@link #postAnimationCallbackDelayed} we want to know how long to wait before
365 * posting the animation callback but let the animation timer take care of the remaining
366 * frame delay time.
Jeff Browncae80492012-05-21 16:33:39 -0700367 * </p><p>
Jeff Brown7ae9d5f2012-03-05 19:33:49 -0800368 * This method is somewhat conservative about how much of the frame delay it
369 * subtracts. It uses the same value returned by {@link #getFrameDelay} which by
370 * default is 10ms even though many parts of the system assume 16ms. Consequently,
371 * we might still wait 6ms before posting an animation callback that we want to run
372 * on the next frame, but this is much better than waiting a whole 16ms and likely
373 * missing the deadline.
Jeff Browncae80492012-05-21 16:33:39 -0700374 * </p>
Jeff Brown7ae9d5f2012-03-05 19:33:49 -0800375 *
376 * @param delayMillis The original delay time including an assumed frame delay.
377 * @return The adjusted delay time with the assumed frame delay subtracted out.
Jeff Browncae80492012-05-21 16:33:39 -0700378 * @hide
Jeff Brown7ae9d5f2012-03-05 19:33:49 -0800379 */
380 public static long subtractFrameDelay(long delayMillis) {
381 final long frameDelay = sFrameDelay;
382 return delayMillis <= frameDelay ? 0 : delayMillis - frameDelay;
383 }
384
John Reck18f16e62014-05-02 16:46:41 -0700385 /**
386 * @return The refresh rate as the nanoseconds between frames
387 * @hide
388 */
John Reck315c3292014-05-09 19:21:04 -0700389 public long getFrameIntervalNanos() {
John Reck18f16e62014-05-02 16:46:41 -0700390 return mFrameIntervalNanos;
391 }
392
Jeff Brown5182c782013-10-15 20:31:52 -0700393 void dump(String prefix, PrintWriter writer) {
394 String innerPrefix = prefix + " ";
395 writer.print(prefix); writer.println("Choreographer:");
396 writer.print(innerPrefix); writer.print("mFrameScheduled=");
397 writer.println(mFrameScheduled);
398 writer.print(innerPrefix); writer.print("mLastFrameTime=");
399 writer.println(TimeUtils.formatUptime(mLastFrameTimeNanos / 1000000));
400 }
401
Jeff Brown7ae9d5f2012-03-05 19:33:49 -0800402 /**
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700403 * Posts a callback to run on the next frame.
Jeff Browncae80492012-05-21 16:33:39 -0700404 * <p>
405 * The callback runs once then is automatically removed.
406 * </p>
Jeff Brown4a06c802012-02-15 15:06:01 -0800407 *
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700408 * @param callbackType The callback type.
409 * @param action The callback action to run during the next frame.
Jeff Brown7ae9d5f2012-03-05 19:33:49 -0800410 * @param token The callback token, or null if none.
Jeff Brown4a06c802012-02-15 15:06:01 -0800411 *
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700412 * @see #removeCallbacks
Jeff Browncae80492012-05-21 16:33:39 -0700413 * @hide
Jeff Brown96e942d2011-11-30 19:55:01 -0800414 */
Doris Liu4f8a98c2018-01-16 18:13:57 -0800415 @TestApi
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700416 public void postCallback(int callbackType, Runnable action, Object token) {
417 postCallbackDelayed(callbackType, action, token, 0);
Jeff Brown87d0b032012-02-03 11:01:21 -0800418 }
419
Jeff Brown4a06c802012-02-15 15:06:01 -0800420 /**
Jeff Browncae80492012-05-21 16:33:39 -0700421 * Posts a callback to run on the next frame after the specified delay.
422 * <p>
423 * The callback runs once then is automatically removed.
424 * </p>
Jeff Brown2b6cb9a2012-03-05 17:21:01 -0800425 *
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700426 * @param callbackType The callback type.
427 * @param action The callback action to run during the next frame after the specified delay.
Jeff Brown7ae9d5f2012-03-05 19:33:49 -0800428 * @param token The callback token, or null if none.
Jeff Brown2b6cb9a2012-03-05 17:21:01 -0800429 * @param delayMillis The delay time in milliseconds.
430 *
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700431 * @see #removeCallback
Jeff Browncae80492012-05-21 16:33:39 -0700432 * @hide
Jeff Brown2b6cb9a2012-03-05 17:21:01 -0800433 */
Doris Liu4f8a98c2018-01-16 18:13:57 -0800434 @TestApi
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700435 public void postCallbackDelayed(int callbackType,
436 Runnable action, Object token, long delayMillis) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -0800437 if (action == null) {
438 throw new IllegalArgumentException("action must not be null");
Jeff Brown2b6cb9a2012-03-05 17:21:01 -0800439 }
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700440 if (callbackType < 0 || callbackType > CALLBACK_LAST) {
441 throw new IllegalArgumentException("callbackType is invalid");
442 }
Jeff Brown7ae9d5f2012-03-05 19:33:49 -0800443
Jeff Browncae80492012-05-21 16:33:39 -0700444 postCallbackDelayedInternal(callbackType, action, token, delayMillis);
445 }
446
447 private void postCallbackDelayedInternal(int callbackType,
448 Object action, Object token, long delayMillis) {
Jeff Brownc42b28d2015-04-06 19:49:02 -0700449 if (DEBUG_FRAMES) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700450 Log.d(TAG, "PostCallback: type=" + callbackType
451 + ", action=" + action + ", token=" + token
Jeff Brown43ea54b2012-03-09 14:37:48 -0800452 + ", delayMillis=" + delayMillis);
453 }
454
Jeff Brown7ae9d5f2012-03-05 19:33:49 -0800455 synchronized (mLock) {
456 final long now = SystemClock.uptimeMillis();
457 final long dueTime = now + delayMillis;
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700458 mCallbackQueues[callbackType].addCallbackLocked(dueTime, action, token);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -0800459
460 if (dueTime <= now) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700461 scheduleFrameLocked(now);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -0800462 } else {
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700463 Message msg = mHandler.obtainMessage(MSG_DO_SCHEDULE_CALLBACK, action);
464 msg.arg1 = callbackType;
465 msg.setAsynchronous(true);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -0800466 mHandler.sendMessageAtTime(msg, dueTime);
467 }
Jeff Brown2b6cb9a2012-03-05 17:21:01 -0800468 }
469 }
470
471 /**
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700472 * Removes callbacks that have the specified action and token.
Jeff Brown4a06c802012-02-15 15:06:01 -0800473 *
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700474 * @param callbackType The callback type.
Jeff Brown7ae9d5f2012-03-05 19:33:49 -0800475 * @param action The action property of the callbacks to remove, or null to remove
476 * callbacks with any action.
477 * @param token The token property of the callbacks to remove, or null to remove
478 * callbacks with any token.
Jeff Brown4a06c802012-02-15 15:06:01 -0800479 *
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700480 * @see #postCallback
481 * @see #postCallbackDelayed
Jeff Browncae80492012-05-21 16:33:39 -0700482 * @hide
Jeff Brown4a06c802012-02-15 15:06:01 -0800483 */
Doris Liu4f8a98c2018-01-16 18:13:57 -0800484 @TestApi
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700485 public void removeCallbacks(int callbackType, Runnable action, Object token) {
486 if (callbackType < 0 || callbackType > CALLBACK_LAST) {
487 throw new IllegalArgumentException("callbackType is invalid");
488 }
489
Jeff Browncae80492012-05-21 16:33:39 -0700490 removeCallbacksInternal(callbackType, action, token);
491 }
492
493 private void removeCallbacksInternal(int callbackType, Object action, Object token) {
Jeff Brownc42b28d2015-04-06 19:49:02 -0700494 if (DEBUG_FRAMES) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700495 Log.d(TAG, "RemoveCallbacks: type=" + callbackType
496 + ", action=" + action + ", token=" + token);
Jeff Brown43ea54b2012-03-09 14:37:48 -0800497 }
498
Jeff Brown4a06c802012-02-15 15:06:01 -0800499 synchronized (mLock) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700500 mCallbackQueues[callbackType].removeCallbacksLocked(action, token);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -0800501 if (action != null && token == null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700502 mHandler.removeMessages(MSG_DO_SCHEDULE_CALLBACK, action);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -0800503 }
Jeff Brown4a06c802012-02-15 15:06:01 -0800504 }
505 }
506
Jeff Brown20c4f872012-04-26 17:38:21 -0700507 /**
Jeff Browncae80492012-05-21 16:33:39 -0700508 * Posts a frame callback to run on the next frame.
509 * <p>
510 * The callback runs once then is automatically removed.
511 * </p>
Jeff Brown20c4f872012-04-26 17:38:21 -0700512 *
Jeff Browncae80492012-05-21 16:33:39 -0700513 * @param callback The frame callback to run during the next frame.
514 *
515 * @see #postFrameCallbackDelayed
516 * @see #removeFrameCallback
517 */
518 public void postFrameCallback(FrameCallback callback) {
519 postFrameCallbackDelayed(callback, 0);
520 }
521
522 /**
523 * Posts a frame callback to run on the next frame after the specified delay.
524 * <p>
525 * The callback runs once then is automatically removed.
526 * </p>
527 *
528 * @param callback The frame callback to run during the next frame.
529 * @param delayMillis The delay time in milliseconds.
530 *
531 * @see #postFrameCallback
532 * @see #removeFrameCallback
533 */
534 public void postFrameCallbackDelayed(FrameCallback callback, long delayMillis) {
535 if (callback == null) {
536 throw new IllegalArgumentException("callback must not be null");
537 }
538
539 postCallbackDelayedInternal(CALLBACK_ANIMATION,
540 callback, FRAME_CALLBACK_TOKEN, delayMillis);
541 }
542
543 /**
544 * Removes a previously posted frame callback.
545 *
546 * @param callback The frame callback to remove.
547 *
548 * @see #postFrameCallback
549 * @see #postFrameCallbackDelayed
550 */
551 public void removeFrameCallback(FrameCallback callback) {
552 if (callback == null) {
553 throw new IllegalArgumentException("callback must not be null");
554 }
555
556 removeCallbacksInternal(CALLBACK_ANIMATION, callback, FRAME_CALLBACK_TOKEN);
557 }
558
559 /**
560 * Gets the time when the current frame started.
561 * <p>
John Reck8d8af3c2014-07-01 15:23:45 -0700562 * This method provides the time in milliseconds when the frame started being rendered.
Jeff Browncae80492012-05-21 16:33:39 -0700563 * The frame time provides a stable time base for synchronizing animations
564 * and drawing. It should be used instead of {@link SystemClock#uptimeMillis()}
565 * or {@link System#nanoTime()} for animations and drawing in the UI. Using the frame
566 * time helps to reduce inter-frame jitter because the frame time is fixed at the time
567 * the frame was scheduled to start, regardless of when the animations or drawing
568 * callback actually runs. All callbacks that run as part of rendering a frame will
569 * observe the same frame time so using the frame time also helps to synchronize effects
570 * that are performed by different callbacks.
571 * </p><p>
572 * Please note that the framework already takes care to process animations and
573 * drawing using the frame time as a stable time base. Most applications should
574 * not need to use the frame time information directly.
575 * </p><p>
Jeff Brown20c4f872012-04-26 17:38:21 -0700576 * This method should only be called from within a callback.
Jeff Browncae80492012-05-21 16:33:39 -0700577 * </p>
Jeff Brown20c4f872012-04-26 17:38:21 -0700578 *
579 * @return The frame start time, in the {@link SystemClock#uptimeMillis()} time base.
580 *
581 * @throws IllegalStateException if no frame is in progress.
Jeff Browncae80492012-05-21 16:33:39 -0700582 * @hide
Jeff Brown20c4f872012-04-26 17:38:21 -0700583 */
Mathew Inwooda570dee2018-08-17 14:56:00 +0100584 @UnsupportedAppUsage
Jeff Brown20c4f872012-04-26 17:38:21 -0700585 public long getFrameTime() {
John Reck315c3292014-05-09 19:21:04 -0700586 return getFrameTimeNanos() / TimeUtils.NANOS_PER_MS;
Jeff Brown20c4f872012-04-26 17:38:21 -0700587 }
588
589 /**
590 * Same as {@link #getFrameTime()} but with nanosecond precision.
591 *
592 * @return The frame start time, in the {@link System#nanoTime()} time base.
593 *
594 * @throws IllegalStateException if no frame is in progress.
Jeff Browncae80492012-05-21 16:33:39 -0700595 * @hide
Jeff Brown20c4f872012-04-26 17:38:21 -0700596 */
Mathew Inwooda570dee2018-08-17 14:56:00 +0100597 @UnsupportedAppUsage
Jeff Brown20c4f872012-04-26 17:38:21 -0700598 public long getFrameTimeNanos() {
599 synchronized (mLock) {
600 if (!mCallbacksRunning) {
601 throw new IllegalStateException("This method must only be called as "
602 + "part of a callback while a frame is in progress.");
603 }
604 return USE_FRAME_TIME ? mLastFrameTimeNanos : System.nanoTime();
605 }
606 }
607
Jorim Jaggid6d6de62017-03-31 15:05:13 +0200608 /**
609 * Like {@link #getLastFrameTimeNanos}, but always returns the last frame time, not matter
610 * whether callbacks are currently running.
611 * @return The frame start time of the last frame, in the {@link System#nanoTime()} time base.
612 * @hide
613 */
614 public long getLastFrameTimeNanos() {
615 synchronized (mLock) {
616 return USE_FRAME_TIME ? mLastFrameTimeNanos : System.nanoTime();
617 }
618 }
619
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700620 private void scheduleFrameLocked(long now) {
621 if (!mFrameScheduled) {
622 mFrameScheduled = true;
Jeff Brown96e942d2011-11-30 19:55:01 -0800623 if (USE_VSYNC) {
Jeff Brownc42b28d2015-04-06 19:49:02 -0700624 if (DEBUG_FRAMES) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700625 Log.d(TAG, "Scheduling next frame on vsync.");
Jeff Brown96e942d2011-11-30 19:55:01 -0800626 }
Jeff Brown58aedbc2012-02-13 20:15:01 -0800627
628 // If running on the Looper thread, then schedule the vsync immediately,
629 // otherwise post a message to schedule the vsync from the UI thread
630 // as soon as possible.
Jeff Brown58aedbc2012-02-13 20:15:01 -0800631 if (isRunningOnLooperThreadLocked()) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -0800632 scheduleVsyncLocked();
Jeff Brown58aedbc2012-02-13 20:15:01 -0800633 } else {
Jeff Browne0dbd002012-02-15 19:34:58 -0800634 Message msg = mHandler.obtainMessage(MSG_DO_SCHEDULE_VSYNC);
635 msg.setAsynchronous(true);
636 mHandler.sendMessageAtFrontOfQueue(msg);
Jeff Brown58aedbc2012-02-13 20:15:01 -0800637 }
Jeff Brown96e942d2011-11-30 19:55:01 -0800638 } else {
Jeff Brown20c4f872012-04-26 17:38:21 -0700639 final long nextFrameTime = Math.max(
John Reck315c3292014-05-09 19:21:04 -0700640 mLastFrameTimeNanos / TimeUtils.NANOS_PER_MS + sFrameDelay, now);
Jeff Brownc42b28d2015-04-06 19:49:02 -0700641 if (DEBUG_FRAMES) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700642 Log.d(TAG, "Scheduling next frame in " + (nextFrameTime - now) + " ms.");
Jeff Brown96e942d2011-11-30 19:55:01 -0800643 }
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700644 Message msg = mHandler.obtainMessage(MSG_DO_FRAME);
Jeff Browne0dbd002012-02-15 19:34:58 -0800645 msg.setAsynchronous(true);
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700646 mHandler.sendMessageAtTime(msg, nextFrameTime);
Jeff Brown96e942d2011-11-30 19:55:01 -0800647 }
648 }
649 }
650
John Reck9f516442017-09-25 10:27:21 -0700651 void setFPSDivisor(int divisor) {
652 if (divisor <= 0) divisor = 1;
653 mFPSDivisor = divisor;
Makoto Onukib708cbe2018-01-09 12:35:06 -0800654 ThreadedRenderer.setFPSDivisor(divisor);
John Reck9f516442017-09-25 10:27:21 -0700655 }
656
Mathew Inwooda570dee2018-08-17 14:56:00 +0100657 @UnsupportedAppUsage
Jeff Browncae80492012-05-21 16:33:39 -0700658 void doFrame(long frameTimeNanos, int frame) {
Jeff Brown59bbef02012-05-07 16:43:25 -0700659 final long startNanos;
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700660 synchronized (mLock) {
661 if (!mFrameScheduled) {
662 return; // no work to do
Jeff Brown96e942d2011-11-30 19:55:01 -0800663 }
Jeff Brown59bbef02012-05-07 16:43:25 -0700664
Jeff Brownc42b28d2015-04-06 19:49:02 -0700665 if (DEBUG_JANK && mDebugPrintNextFrameTimeDelta) {
666 mDebugPrintNextFrameTimeDelta = false;
667 Log.d(TAG, "Frame time delta: "
668 + ((frameTimeNanos - mLastFrameTimeNanos) * 0.000001f) + " ms");
669 }
670
John Reckba6adf62015-02-19 14:36:50 -0800671 long intendedFrameTimeNanos = frameTimeNanos;
Jeff Brown59bbef02012-05-07 16:43:25 -0700672 startNanos = System.nanoTime();
Jeff Browncae80492012-05-21 16:33:39 -0700673 final long jitterNanos = startNanos - frameTimeNanos;
Jeff Brown59bbef02012-05-07 16:43:25 -0700674 if (jitterNanos >= mFrameIntervalNanos) {
Jeff Brown4fdf9c62012-06-11 15:25:48 -0700675 final long skippedFrames = jitterNanos / mFrameIntervalNanos;
676 if (skippedFrames >= SKIPPED_FRAME_WARNING_LIMIT) {
Jeff Brown265f1cc2012-06-11 18:01:06 -0700677 Log.i(TAG, "Skipped " + skippedFrames + " frames! "
Jeff Brown4fdf9c62012-06-11 15:25:48 -0700678 + "The application may be doing too much work on its main thread.");
679 }
Jeff Brown59bbef02012-05-07 16:43:25 -0700680 final long lastFrameOffset = jitterNanos % mFrameIntervalNanos;
Jeff Brownc42b28d2015-04-06 19:49:02 -0700681 if (DEBUG_JANK) {
Jeff Brown59bbef02012-05-07 16:43:25 -0700682 Log.d(TAG, "Missed vsync by " + (jitterNanos * 0.000001f) + " ms "
683 + "which is more than the frame interval of "
684 + (mFrameIntervalNanos * 0.000001f) + " ms! "
Jeff Brown4fdf9c62012-06-11 15:25:48 -0700685 + "Skipping " + skippedFrames + " frames and setting frame "
686 + "time to " + (lastFrameOffset * 0.000001f) + " ms in the past.");
Jeff Brown59bbef02012-05-07 16:43:25 -0700687 }
Jeff Browncae80492012-05-21 16:33:39 -0700688 frameTimeNanos = startNanos - lastFrameOffset;
Jeff Brown59bbef02012-05-07 16:43:25 -0700689 }
690
Jeff Browncae80492012-05-21 16:33:39 -0700691 if (frameTimeNanos < mLastFrameTimeNanos) {
Jeff Brownc42b28d2015-04-06 19:49:02 -0700692 if (DEBUG_JANK) {
Jeff Brown59bbef02012-05-07 16:43:25 -0700693 Log.d(TAG, "Frame time appears to be going backwards. May be due to a "
Jeff Brown4fdf9c62012-06-11 15:25:48 -0700694 + "previously skipped frame. Waiting for next vsync.");
Jeff Brown59bbef02012-05-07 16:43:25 -0700695 }
696 scheduleVsyncLocked();
697 return;
698 }
699
John Reck9f516442017-09-25 10:27:21 -0700700 if (mFPSDivisor > 1) {
701 long timeSinceVsync = frameTimeNanos - mLastFrameTimeNanos;
702 if (timeSinceVsync < (mFrameIntervalNanos * mFPSDivisor) && timeSinceVsync > 0) {
703 scheduleVsyncLocked();
704 return;
705 }
706 }
707
John Reckba6adf62015-02-19 14:36:50 -0800708 mFrameInfo.setVsync(intendedFrameTimeNanos, frameTimeNanos);
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700709 mFrameScheduled = false;
Jeff Browncae80492012-05-21 16:33:39 -0700710 mLastFrameTimeNanos = frameTimeNanos;
Jeff Brown20c4f872012-04-26 17:38:21 -0700711 }
712
Chris Craike22c59b2015-05-21 18:33:37 -0700713 try {
714 Trace.traceBegin(Trace.TRACE_TAG_VIEW, "Choreographer#doFrame");
John Recka2acb4f2016-08-05 07:58:37 -0700715 AnimationUtils.lockAnimationClock(frameTimeNanos / TimeUtils.NANOS_PER_MS);
John Reckba6adf62015-02-19 14:36:50 -0800716
Chris Craike22c59b2015-05-21 18:33:37 -0700717 mFrameInfo.markInputHandlingStart();
718 doCallbacks(Choreographer.CALLBACK_INPUT, frameTimeNanos);
John Reckba6adf62015-02-19 14:36:50 -0800719
Chris Craike22c59b2015-05-21 18:33:37 -0700720 mFrameInfo.markAnimationsStart();
721 doCallbacks(Choreographer.CALLBACK_ANIMATION, frameTimeNanos);
Jorim Jaggi02a741f2018-12-12 17:40:19 -0800722 doCallbacks(Choreographer.CALLBACK_INSETS_ANIMATION, frameTimeNanos);
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700723
Chris Craike22c59b2015-05-21 18:33:37 -0700724 mFrameInfo.markPerformTraversalsStart();
725 doCallbacks(Choreographer.CALLBACK_TRAVERSAL, frameTimeNanos);
726
727 doCallbacks(Choreographer.CALLBACK_COMMIT, frameTimeNanos);
728 } finally {
John Recka2acb4f2016-08-05 07:58:37 -0700729 AnimationUtils.unlockAnimationClock();
Chris Craike22c59b2015-05-21 18:33:37 -0700730 Trace.traceEnd(Trace.TRACE_TAG_VIEW);
731 }
Jeff Brownc42b28d2015-04-06 19:49:02 -0700732
733 if (DEBUG_FRAMES) {
Jeff Brown20c4f872012-04-26 17:38:21 -0700734 final long endNanos = System.nanoTime();
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700735 Log.d(TAG, "Frame " + frame + ": Finished, took "
Jeff Brown20c4f872012-04-26 17:38:21 -0700736 + (endNanos - startNanos) * 0.000001f + " ms, latency "
Jeff Browncae80492012-05-21 16:33:39 -0700737 + (startNanos - frameTimeNanos) * 0.000001f + " ms.");
Jeff Brown96e942d2011-11-30 19:55:01 -0800738 }
739 }
740
Jeff Browncae80492012-05-21 16:33:39 -0700741 void doCallbacks(int callbackType, long frameTimeNanos) {
742 CallbackRecord callbacks;
Jeff Brown96858852012-02-14 13:45:06 -0800743 synchronized (mLock) {
Jeff Browncae80492012-05-21 16:33:39 -0700744 // We use "now" to determine when callbacks become due because it's possible
745 // for earlier processing phases in a frame to post callbacks that should run
746 // in a following phase, such as an input event that causes an animation to start.
Jeff Brownc42b28d2015-04-06 19:49:02 -0700747 final long now = System.nanoTime();
748 callbacks = mCallbackQueues[callbackType].extractDueCallbacksLocked(
749 now / TimeUtils.NANOS_PER_MS);
Jeff Brown20c4f872012-04-26 17:38:21 -0700750 if (callbacks == null) {
751 return;
752 }
753 mCallbacksRunning = true;
Jeff Brownc42b28d2015-04-06 19:49:02 -0700754
755 // Update the frame time if necessary when committing the frame.
756 // We only update the frame time if we are more than 2 frames late reaching
757 // the commit phase. This ensures that the frame time which is observed by the
758 // callbacks will always increase from one frame to the next and never repeat.
759 // We never want the next frame's starting frame time to end up being less than
760 // or equal to the previous frame's commit frame time. Keep in mind that the
761 // next frame has most likely already been scheduled by now so we play it
762 // safe by ensuring the commit time is always at least one frame behind.
763 if (callbackType == Choreographer.CALLBACK_COMMIT) {
764 final long jitterNanos = now - frameTimeNanos;
Chris Craike22c59b2015-05-21 18:33:37 -0700765 Trace.traceCounter(Trace.TRACE_TAG_VIEW, "jitterNanos", (int) jitterNanos);
Jeff Brownc42b28d2015-04-06 19:49:02 -0700766 if (jitterNanos >= 2 * mFrameIntervalNanos) {
767 final long lastFrameOffset = jitterNanos % mFrameIntervalNanos
768 + mFrameIntervalNanos;
769 if (DEBUG_JANK) {
770 Log.d(TAG, "Commit callback delayed by " + (jitterNanos * 0.000001f)
771 + " ms which is more than twice the frame interval of "
772 + (mFrameIntervalNanos * 0.000001f) + " ms! "
773 + "Setting frame time to " + (lastFrameOffset * 0.000001f)
774 + " ms in the past.");
775 mDebugPrintNextFrameTimeDelta = true;
776 }
777 frameTimeNanos = now - lastFrameOffset;
778 mLastFrameTimeNanos = frameTimeNanos;
779 }
780 }
Jeff Brown96858852012-02-14 13:45:06 -0800781 }
Jeff Brown20c4f872012-04-26 17:38:21 -0700782 try {
Chris Craike22c59b2015-05-21 18:33:37 -0700783 Trace.traceBegin(Trace.TRACE_TAG_VIEW, CALLBACK_TRACE_TITLES[callbackType]);
Jeff Browncae80492012-05-21 16:33:39 -0700784 for (CallbackRecord c = callbacks; c != null; c = c.next) {
Jeff Brownc42b28d2015-04-06 19:49:02 -0700785 if (DEBUG_FRAMES) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700786 Log.d(TAG, "RunCallback: type=" + callbackType
787 + ", action=" + c.action + ", token=" + c.token
788 + ", latencyMillis=" + (SystemClock.uptimeMillis() - c.dueTime));
789 }
Jeff Browncae80492012-05-21 16:33:39 -0700790 c.run(frameTimeNanos);
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700791 }
Jeff Brown20c4f872012-04-26 17:38:21 -0700792 } finally {
Jeff Brown96858852012-02-14 13:45:06 -0800793 synchronized (mLock) {
Jeff Brown20c4f872012-04-26 17:38:21 -0700794 mCallbacksRunning = false;
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700795 do {
Jeff Browncae80492012-05-21 16:33:39 -0700796 final CallbackRecord next = callbacks.next;
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700797 recycleCallbackLocked(callbacks);
798 callbacks = next;
799 } while (callbacks != null);
Jeff Brown96858852012-02-14 13:45:06 -0800800 }
Chris Craike22c59b2015-05-21 18:33:37 -0700801 Trace.traceEnd(Trace.TRACE_TAG_VIEW);
Jeff Brown96858852012-02-14 13:45:06 -0800802 }
Jeff Brown96858852012-02-14 13:45:06 -0800803 }
804
805 void doScheduleVsync() {
806 synchronized (mLock) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700807 if (mFrameScheduled) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -0800808 scheduleVsyncLocked();
809 }
Jeff Brown96858852012-02-14 13:45:06 -0800810 }
811 }
812
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700813 void doScheduleCallback(int callbackType) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -0800814 synchronized (mLock) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700815 if (!mFrameScheduled) {
816 final long now = SystemClock.uptimeMillis();
817 if (mCallbackQueues[callbackType].hasDueCallbacksLocked(now)) {
818 scheduleFrameLocked(now);
819 }
Jeff Brown7ae9d5f2012-03-05 19:33:49 -0800820 }
Jeff Brown96e942d2011-11-30 19:55:01 -0800821 }
822 }
823
Mathew Inwooda570dee2018-08-17 14:56:00 +0100824 @UnsupportedAppUsage
Jeff Brown7ae9d5f2012-03-05 19:33:49 -0800825 private void scheduleVsyncLocked() {
826 mDisplayEventReceiver.scheduleVsync();
827 }
828
Jeff Brown58aedbc2012-02-13 20:15:01 -0800829 private boolean isRunningOnLooperThreadLocked() {
830 return Looper.myLooper() == mLooper;
831 }
832
Jeff Browncae80492012-05-21 16:33:39 -0700833 private CallbackRecord obtainCallbackLocked(long dueTime, Object action, Object token) {
834 CallbackRecord callback = mCallbackPool;
Jeff Brown96858852012-02-14 13:45:06 -0800835 if (callback == null) {
Jeff Browncae80492012-05-21 16:33:39 -0700836 callback = new CallbackRecord();
Jeff Brown96858852012-02-14 13:45:06 -0800837 } else {
838 mCallbackPool = callback.next;
839 callback.next = null;
840 }
Jeff Brown7ae9d5f2012-03-05 19:33:49 -0800841 callback.dueTime = dueTime;
842 callback.action = action;
843 callback.token = token;
Jeff Brown96858852012-02-14 13:45:06 -0800844 return callback;
845 }
846
Jeff Browncae80492012-05-21 16:33:39 -0700847 private void recycleCallbackLocked(CallbackRecord callback) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -0800848 callback.action = null;
849 callback.token = null;
Jeff Brown96858852012-02-14 13:45:06 -0800850 callback.next = mCallbackPool;
851 mCallbackPool = callback;
852 }
853
Jeff Browncae80492012-05-21 16:33:39 -0700854 /**
855 * Implement this interface to receive a callback when a new display frame is
856 * being rendered. The callback is invoked on the {@link Looper} thread to
857 * which the {@link Choreographer} is attached.
858 */
859 public interface FrameCallback {
860 /**
861 * Called when a new display frame is being rendered.
862 * <p>
863 * This method provides the time in nanoseconds when the frame started being rendered.
864 * The frame time provides a stable time base for synchronizing animations
865 * and drawing. It should be used instead of {@link SystemClock#uptimeMillis()}
866 * or {@link System#nanoTime()} for animations and drawing in the UI. Using the frame
867 * time helps to reduce inter-frame jitter because the frame time is fixed at the time
868 * the frame was scheduled to start, regardless of when the animations or drawing
869 * callback actually runs. All callbacks that run as part of rendering a frame will
870 * observe the same frame time so using the frame time also helps to synchronize effects
871 * that are performed by different callbacks.
872 * </p><p>
873 * Please note that the framework already takes care to process animations and
874 * drawing using the frame time as a stable time base. Most applications should
875 * not need to use the frame time information directly.
876 * </p>
877 *
878 * @param frameTimeNanos The time in nanoseconds when the frame started being rendered,
879 * in the {@link System#nanoTime()} timebase. Divide this value by {@code 1000000}
880 * to convert it to the {@link SystemClock#uptimeMillis()} time base.
881 */
882 public void doFrame(long frameTimeNanos);
883 }
884
Jeff Brown96858852012-02-14 13:45:06 -0800885 private final class FrameHandler extends Handler {
886 public FrameHandler(Looper looper) {
887 super(looper);
888 }
889
890 @Override
891 public void handleMessage(Message msg) {
892 switch (msg.what) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700893 case MSG_DO_FRAME:
Jeff Brown20c4f872012-04-26 17:38:21 -0700894 doFrame(System.nanoTime(), 0);
Jeff Brown96858852012-02-14 13:45:06 -0800895 break;
896 case MSG_DO_SCHEDULE_VSYNC:
897 doScheduleVsync();
898 break;
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700899 case MSG_DO_SCHEDULE_CALLBACK:
900 doScheduleCallback(msg.arg1);
Jeff Brown2b6cb9a2012-03-05 17:21:01 -0800901 break;
Jeff Brown96858852012-02-14 13:45:06 -0800902 }
903 }
904 }
905
Jeff Brownb0806602012-05-16 12:50:41 -0700906 private final class FrameDisplayEventReceiver extends DisplayEventReceiver
907 implements Runnable {
Jeff Brown4fdf9c62012-06-11 15:25:48 -0700908 private boolean mHavePendingVsync;
Jeff Brownb0806602012-05-16 12:50:41 -0700909 private long mTimestampNanos;
910 private int mFrame;
911
Jorim Jaggi34a0cdb2017-06-08 15:40:38 -0700912 public FrameDisplayEventReceiver(Looper looper, int vsyncSource) {
913 super(looper, vsyncSource);
Jeff Brown96e942d2011-11-30 19:55:01 -0800914 }
915
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800916 // TODO(b/116025192): physicalDisplayId is ignored because SF only emits VSYNC events for
917 // the internal display and DisplayEventReceiver#scheduleVsync only allows requesting VSYNC
918 // for the internal display implicitly.
Jeff Brown96e942d2011-11-30 19:55:01 -0800919 @Override
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800920 public void onVsync(long timestampNanos, long physicalDisplayId, int frame) {
Jeff Brownb0806602012-05-16 12:50:41 -0700921 // Post the vsync event to the Handler.
922 // The idea is to prevent incoming vsync events from completely starving
923 // the message queue. If there are no messages in the queue with timestamps
924 // earlier than the frame time, then the vsync event will be processed immediately.
925 // Otherwise, messages that predate the vsync event will be handled first.
Jeff Brown4fdf9c62012-06-11 15:25:48 -0700926 long now = System.nanoTime();
927 if (timestampNanos > now) {
928 Log.w(TAG, "Frame time is " + ((timestampNanos - now) * 0.000001f)
929 + " ms in the future! Check that graphics HAL is generating vsync "
930 + "timestamps using the correct timebase.");
931 timestampNanos = now;
932 }
933
Jeff Brownba7261132012-06-14 23:48:40 -0700934 if (mHavePendingVsync) {
935 Log.w(TAG, "Already have a pending vsync event. There should only be "
936 + "one at a time.");
937 } else {
938 mHavePendingVsync = true;
939 }
Jeff Brown4fdf9c62012-06-11 15:25:48 -0700940
Jeff Brownb0806602012-05-16 12:50:41 -0700941 mTimestampNanos = timestampNanos;
942 mFrame = frame;
943 Message msg = Message.obtain(mHandler, this);
944 msg.setAsynchronous(true);
John Reck315c3292014-05-09 19:21:04 -0700945 mHandler.sendMessageAtTime(msg, timestampNanos / TimeUtils.NANOS_PER_MS);
Jeff Brownb0806602012-05-16 12:50:41 -0700946 }
947
948 @Override
949 public void run() {
Jeff Brown4fdf9c62012-06-11 15:25:48 -0700950 mHavePendingVsync = false;
Jeff Brownb0806602012-05-16 12:50:41 -0700951 doFrame(mTimestampNanos, mFrame);
Jeff Brown96e942d2011-11-30 19:55:01 -0800952 }
953 }
Jeff Brown96858852012-02-14 13:45:06 -0800954
Jeff Browncae80492012-05-21 16:33:39 -0700955 private static final class CallbackRecord {
956 public CallbackRecord next;
Jeff Brown7ae9d5f2012-03-05 19:33:49 -0800957 public long dueTime;
Jeff Browncae80492012-05-21 16:33:39 -0700958 public Object action; // Runnable or FrameCallback
Jeff Brown7ae9d5f2012-03-05 19:33:49 -0800959 public Object token;
Jeff Browncae80492012-05-21 16:33:39 -0700960
Mathew Inwooda570dee2018-08-17 14:56:00 +0100961 @UnsupportedAppUsage
Jeff Browncae80492012-05-21 16:33:39 -0700962 public void run(long frameTimeNanos) {
963 if (token == FRAME_CALLBACK_TOKEN) {
964 ((FrameCallback)action).doFrame(frameTimeNanos);
965 } else {
966 ((Runnable)action).run();
967 }
968 }
Jeff Brown96858852012-02-14 13:45:06 -0800969 }
Jeff Brown43ea54b2012-03-09 14:37:48 -0800970
971 private final class CallbackQueue {
Jeff Browncae80492012-05-21 16:33:39 -0700972 private CallbackRecord mHead;
Jeff Brown43ea54b2012-03-09 14:37:48 -0800973
974 public boolean hasDueCallbacksLocked(long now) {
975 return mHead != null && mHead.dueTime <= now;
976 }
977
Jeff Browncae80492012-05-21 16:33:39 -0700978 public CallbackRecord extractDueCallbacksLocked(long now) {
979 CallbackRecord callbacks = mHead;
Jeff Brown43ea54b2012-03-09 14:37:48 -0800980 if (callbacks == null || callbacks.dueTime > now) {
981 return null;
982 }
983
Jeff Browncae80492012-05-21 16:33:39 -0700984 CallbackRecord last = callbacks;
985 CallbackRecord next = last.next;
Jeff Brown43ea54b2012-03-09 14:37:48 -0800986 while (next != null) {
987 if (next.dueTime > now) {
988 last.next = null;
989 break;
990 }
991 last = next;
992 next = next.next;
993 }
994 mHead = next;
995 return callbacks;
996 }
997
Mathew Inwooda570dee2018-08-17 14:56:00 +0100998 @UnsupportedAppUsage
Jeff Browncae80492012-05-21 16:33:39 -0700999 public void addCallbackLocked(long dueTime, Object action, Object token) {
1000 CallbackRecord callback = obtainCallbackLocked(dueTime, action, token);
1001 CallbackRecord entry = mHead;
Jeff Brown43ea54b2012-03-09 14:37:48 -08001002 if (entry == null) {
1003 mHead = callback;
1004 return;
1005 }
1006 if (dueTime < entry.dueTime) {
1007 callback.next = entry;
1008 mHead = callback;
1009 return;
1010 }
1011 while (entry.next != null) {
1012 if (dueTime < entry.next.dueTime) {
1013 callback.next = entry.next;
1014 break;
1015 }
1016 entry = entry.next;
1017 }
1018 entry.next = callback;
1019 }
1020
Jeff Browncae80492012-05-21 16:33:39 -07001021 public void removeCallbacksLocked(Object action, Object token) {
1022 CallbackRecord predecessor = null;
1023 for (CallbackRecord callback = mHead; callback != null;) {
1024 final CallbackRecord next = callback.next;
Jeff Brown43ea54b2012-03-09 14:37:48 -08001025 if ((action == null || callback.action == action)
1026 && (token == null || callback.token == token)) {
1027 if (predecessor != null) {
1028 predecessor.next = next;
1029 } else {
1030 mHead = next;
1031 }
1032 recycleCallbackLocked(callback);
1033 } else {
1034 predecessor = callback;
1035 }
1036 callback = next;
1037 }
1038 }
1039 }
Jeff Brown96e942d2011-11-30 19:55:01 -08001040}