blob: 597be6864494d14d6e0ea58fab9ac77ffa179672 [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
Jeff Brownbd6e1502012-08-28 03:27:37 -070022import android.hardware.display.DisplayManagerGlobal;
Jeff Brown96e942d2011-11-30 19:55:01 -080023import android.os.Handler;
24import android.os.Looper;
25import android.os.Message;
26import android.os.SystemClock;
27import android.os.SystemProperties;
Chris Craike22c59b2015-05-21 18:33:37 -070028import android.os.Trace;
Jeff Brown96e942d2011-11-30 19:55:01 -080029import android.util.Log;
Jeff Brown5182c782013-10-15 20:31:52 -070030import android.util.TimeUtils;
John Recka2acb4f2016-08-05 07:58:37 -070031import android.view.animation.AnimationUtils;
Jeff Brown5182c782013-10-15 20:31:52 -070032
33import java.io.PrintWriter;
Jeff Brown96e942d2011-11-30 19:55:01 -080034
35/**
Jeff Browncae80492012-05-21 16:33:39 -070036 * 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 Brown96e942d2011-11-30 19:55:01 -080076 */
Jeff Brown96858852012-02-14 13:45:06 -080077public final class Choreographer {
Jeff Brown96e942d2011-11-30 19:55:01 -080078 private static final String TAG = "Choreographer";
Jeff Brownc42b28d2015-04-06 19:49:02 -070079
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 Brown96e942d2011-11-30 19:55:01 -080085
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 Brown87d0b032012-02-03 11:01:21 -080097 private static volatile long sFrameDelay = DEFAULT_FRAME_DELAY;
Jeff Brown96e942d2011-11-30 19:55:01 -080098
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 Jaggi34a0cdb2017-06-08 15:40:38 -0700108 return new Choreographer(looper, VSYNC_SOURCE_APP);
Jeff Brown96e942d2011-11-30 19:55:01 -0800109 }
110 };
111
Jorim Jaggi34a0cdb2017-06-08 15:40:38 -0700112 // 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 Brownebb2d8d2012-03-23 17:14:34 -0700125 // Enable/disable vsync for animations and drawing.
Jeff Brown96e942d2011-11-30 19:55:01 -0800126 private static final boolean USE_VSYNC = SystemProperties.getBoolean(
127 "debug.choreographer.vsync", true);
128
Jeff Brown20c4f872012-04-26 17:38:21 -0700129 // 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 Brown4fdf9c62012-06-11 15:25:48 -0700133 // 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 Brownebb2d8d2012-03-23 17:14:34 -0700138 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 Brown96e942d2011-11-30 19:55:01 -0800141
Jeff Browncae80492012-05-21 16:33:39 -0700142 // 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 Brown87d0b032012-02-03 11:01:21 -0800147 private final Object mLock = new Object();
148
Jeff Brown96e942d2011-11-30 19:55:01 -0800149 private final Looper mLooper;
Jeff Brown96858852012-02-14 13:45:06 -0800150 private final FrameHandler mHandler;
Jeff Browncae80492012-05-21 16:33:39 -0700151
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 Brown1654d0b2012-02-15 15:40:52 -0800155 private final FrameDisplayEventReceiver mDisplayEventReceiver;
Jeff Brown96858852012-02-14 13:45:06 -0800156
Jeff Browncae80492012-05-21 16:33:39 -0700157 private CallbackRecord mCallbackPool;
Jeff Brown96e942d2011-11-30 19:55:01 -0800158
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700159 private final CallbackQueue[] mCallbackQueues;
Jeff Brown96858852012-02-14 13:45:06 -0800160
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700161 private boolean mFrameScheduled;
Jeff Brown20c4f872012-04-26 17:38:21 -0700162 private boolean mCallbacksRunning;
163 private long mLastFrameTimeNanos;
Jeff Brown59bbef02012-05-07 16:43:25 -0700164 private long mFrameIntervalNanos;
Jeff Brownc42b28d2015-04-06 19:49:02 -0700165 private boolean mDebugPrintNextFrameTimeDelta;
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700166
167 /**
John Reckba6adf62015-02-19 14:36:50 -0800168 * 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 Craike22c59b2015-05-21 18:33:37 -0700181 * 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 Brownebb2d8d2012-03-23 17:14:34 -0700189 * Callback type: Input callback. Runs first.
Jeff Browncae80492012-05-21 16:33:39 -0700190 * @hide
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700191 */
192 public static final int CALLBACK_INPUT = 0;
193
194 /**
195 * Callback type: Animation callback. Runs before traversals.
Jeff Browncae80492012-05-21 16:33:39 -0700196 * @hide
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700197 */
198 public static final int CALLBACK_ANIMATION = 1;
199
200 /**
Jeff Brownc42b28d2015-04-06 19:49:02 -0700201 * Callback type: Traversal callback. Handles layout and draw. Runs
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700202 * after all other asynchronous messages have been handled.
Jeff Browncae80492012-05-21 16:33:39 -0700203 * @hide
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700204 */
205 public static final int CALLBACK_TRAVERSAL = 2;
206
Jeff Brownc42b28d2015-04-06 19:49:02 -0700207 /**
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 Brown96e942d2011-11-30 19:55:01 -0800220
Jorim Jaggi34a0cdb2017-06-08 15:40:38 -0700221 private Choreographer(Looper looper, int vsyncSource) {
Jeff Brown96e942d2011-11-30 19:55:01 -0800222 mLooper = looper;
Jeff Brown96858852012-02-14 13:45:06 -0800223 mHandler = new FrameHandler(looper);
Jorim Jaggi34a0cdb2017-06-08 15:40:38 -0700224 mDisplayEventReceiver = USE_VSYNC
225 ? new FrameDisplayEventReceiver(looper, vsyncSource)
226 : null;
Jeff Brown20c4f872012-04-26 17:38:21 -0700227 mLastFrameTimeNanos = Long.MIN_VALUE;
Jeff Brownd32460c2012-07-20 16:15:36 -0700228
Jeff Brownbd6e1502012-08-28 03:27:37 -0700229 mFrameIntervalNanos = (long)(1000000000 / getRefreshRate());
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700230
231 mCallbackQueues = new CallbackQueue[CALLBACK_LAST + 1];
232 for (int i = 0; i <= CALLBACK_LAST; i++) {
233 mCallbackQueues[i] = new CallbackQueue();
234 }
Jeff Brown96e942d2011-11-30 19:55:01 -0800235 }
236
Jeff Brownbd6e1502012-08-28 03:27:37 -0700237 private static float getRefreshRate() {
238 DisplayInfo di = DisplayManagerGlobal.getInstance().getDisplayInfo(
239 Display.DEFAULT_DISPLAY);
P.Y. Laligandb3b9eb32015-05-11 15:02:07 -0700240 return di.getMode().getRefreshRate();
Jeff Brownbd6e1502012-08-28 03:27:37 -0700241 }
242
Jeff Brown96e942d2011-11-30 19:55:01 -0800243 /**
Dianne Hackborn8bcd54b2012-01-31 19:04:53 -0800244 * 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 Brown96e942d2011-11-30 19:55:01 -0800246 *
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 Jaggi34a0cdb2017-06-08 15:40:38 -0700254 /**
255 * @hide
256 */
257 public static Choreographer getSfInstance() {
258 return sSfThreadInstance.get();
259 }
260
John Reckac04f4e2016-06-23 10:21:45 -0700261 /** 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 Brown96e942d2011-11-30 19:55:01 -0800274 /**
Jeff Browncae80492012-05-21 16:33:39 -0700275 * 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 Brown96e942d2011-11-30 19:55:01 -0800279 * function because the same delay will be applied to all animations, since they are all
280 * run off of a single timing loop.
Jeff Browncae80492012-05-21 16:33:39 -0700281 * </p><p>
Jeff Brown96e942d2011-11-30 19:55:01 -0800282 * 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 Browncae80492012-05-21 16:33:39 -0700284 * </p>
Jeff Brown96e942d2011-11-30 19:55:01 -0800285 *
286 * @return the requested time between frames, in milliseconds
Jeff Browncae80492012-05-21 16:33:39 -0700287 * @hide
Jeff Brown96e942d2011-11-30 19:55:01 -0800288 */
289 public static long getFrameDelay() {
290 return sFrameDelay;
291 }
292
293 /**
Jeff Browncae80492012-05-21 16:33:39 -0700294 * 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 Brown96e942d2011-11-30 19:55:01 -0800298 * function because the same delay will be applied to all animations, since they are all
299 * run off of a single timing loop.
Jeff Browncae80492012-05-21 16:33:39 -0700300 * </p><p>
Jeff Brown96e942d2011-11-30 19:55:01 -0800301 * 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 Browncae80492012-05-21 16:33:39 -0700303 * </p>
Jeff Brown96e942d2011-11-30 19:55:01 -0800304 *
305 * @param frameDelay the requested time between frames, in milliseconds
Jeff Browncae80492012-05-21 16:33:39 -0700306 * @hide
Jeff Brown96e942d2011-11-30 19:55:01 -0800307 */
308 public static void setFrameDelay(long frameDelay) {
309 sFrameDelay = frameDelay;
310 }
311
312 /**
Jeff Brown7ae9d5f2012-03-05 19:33:49 -0800313 * Subtracts typical frame delay time from a delay interval in milliseconds.
Jeff Browncae80492012-05-21 16:33:39 -0700314 * <p>
Jeff Brown7ae9d5f2012-03-05 19:33:49 -0800315 * 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 Browncae80492012-05-21 16:33:39 -0700321 * </p><p>
Jeff Brown7ae9d5f2012-03-05 19:33:49 -0800322 * 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 Browncae80492012-05-21 16:33:39 -0700328 * </p>
Jeff Brown7ae9d5f2012-03-05 19:33:49 -0800329 *
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 Browncae80492012-05-21 16:33:39 -0700332 * @hide
Jeff Brown7ae9d5f2012-03-05 19:33:49 -0800333 */
334 public static long subtractFrameDelay(long delayMillis) {
335 final long frameDelay = sFrameDelay;
336 return delayMillis <= frameDelay ? 0 : delayMillis - frameDelay;
337 }
338
John Reck18f16e62014-05-02 16:46:41 -0700339 /**
340 * @return The refresh rate as the nanoseconds between frames
341 * @hide
342 */
John Reck315c3292014-05-09 19:21:04 -0700343 public long getFrameIntervalNanos() {
John Reck18f16e62014-05-02 16:46:41 -0700344 return mFrameIntervalNanos;
345 }
346
Jeff Brown5182c782013-10-15 20:31:52 -0700347 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 Brown7ae9d5f2012-03-05 19:33:49 -0800356 /**
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700357 * Posts a callback to run on the next frame.
Jeff Browncae80492012-05-21 16:33:39 -0700358 * <p>
359 * The callback runs once then is automatically removed.
360 * </p>
Jeff Brown4a06c802012-02-15 15:06:01 -0800361 *
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700362 * @param callbackType The callback type.
363 * @param action The callback action to run during the next frame.
Jeff Brown7ae9d5f2012-03-05 19:33:49 -0800364 * @param token The callback token, or null if none.
Jeff Brown4a06c802012-02-15 15:06:01 -0800365 *
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700366 * @see #removeCallbacks
Jeff Browncae80492012-05-21 16:33:39 -0700367 * @hide
Jeff Brown96e942d2011-11-30 19:55:01 -0800368 */
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700369 public void postCallback(int callbackType, Runnable action, Object token) {
370 postCallbackDelayed(callbackType, action, token, 0);
Jeff Brown87d0b032012-02-03 11:01:21 -0800371 }
372
Jeff Brown4a06c802012-02-15 15:06:01 -0800373 /**
Jeff Browncae80492012-05-21 16:33:39 -0700374 * 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 Brown2b6cb9a2012-03-05 17:21:01 -0800378 *
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700379 * @param callbackType The callback type.
380 * @param action The callback action to run during the next frame after the specified delay.
Jeff Brown7ae9d5f2012-03-05 19:33:49 -0800381 * @param token The callback token, or null if none.
Jeff Brown2b6cb9a2012-03-05 17:21:01 -0800382 * @param delayMillis The delay time in milliseconds.
383 *
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700384 * @see #removeCallback
Jeff Browncae80492012-05-21 16:33:39 -0700385 * @hide
Jeff Brown2b6cb9a2012-03-05 17:21:01 -0800386 */
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700387 public void postCallbackDelayed(int callbackType,
388 Runnable action, Object token, long delayMillis) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -0800389 if (action == null) {
390 throw new IllegalArgumentException("action must not be null");
Jeff Brown2b6cb9a2012-03-05 17:21:01 -0800391 }
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700392 if (callbackType < 0 || callbackType > CALLBACK_LAST) {
393 throw new IllegalArgumentException("callbackType is invalid");
394 }
Jeff Brown7ae9d5f2012-03-05 19:33:49 -0800395
Jeff Browncae80492012-05-21 16:33:39 -0700396 postCallbackDelayedInternal(callbackType, action, token, delayMillis);
397 }
398
399 private void postCallbackDelayedInternal(int callbackType,
400 Object action, Object token, long delayMillis) {
Jeff Brownc42b28d2015-04-06 19:49:02 -0700401 if (DEBUG_FRAMES) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700402 Log.d(TAG, "PostCallback: type=" + callbackType
403 + ", action=" + action + ", token=" + token
Jeff Brown43ea54b2012-03-09 14:37:48 -0800404 + ", delayMillis=" + delayMillis);
405 }
406
Jeff Brown7ae9d5f2012-03-05 19:33:49 -0800407 synchronized (mLock) {
408 final long now = SystemClock.uptimeMillis();
409 final long dueTime = now + delayMillis;
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700410 mCallbackQueues[callbackType].addCallbackLocked(dueTime, action, token);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -0800411
412 if (dueTime <= now) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700413 scheduleFrameLocked(now);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -0800414 } else {
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700415 Message msg = mHandler.obtainMessage(MSG_DO_SCHEDULE_CALLBACK, action);
416 msg.arg1 = callbackType;
417 msg.setAsynchronous(true);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -0800418 mHandler.sendMessageAtTime(msg, dueTime);
419 }
Jeff Brown2b6cb9a2012-03-05 17:21:01 -0800420 }
421 }
422
423 /**
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700424 * Removes callbacks that have the specified action and token.
Jeff Brown4a06c802012-02-15 15:06:01 -0800425 *
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700426 * @param callbackType The callback type.
Jeff Brown7ae9d5f2012-03-05 19:33:49 -0800427 * @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 Brown4a06c802012-02-15 15:06:01 -0800431 *
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700432 * @see #postCallback
433 * @see #postCallbackDelayed
Jeff Browncae80492012-05-21 16:33:39 -0700434 * @hide
Jeff Brown4a06c802012-02-15 15:06:01 -0800435 */
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700436 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 Browncae80492012-05-21 16:33:39 -0700441 removeCallbacksInternal(callbackType, action, token);
442 }
443
444 private void removeCallbacksInternal(int callbackType, Object action, Object token) {
Jeff Brownc42b28d2015-04-06 19:49:02 -0700445 if (DEBUG_FRAMES) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700446 Log.d(TAG, "RemoveCallbacks: type=" + callbackType
447 + ", action=" + action + ", token=" + token);
Jeff Brown43ea54b2012-03-09 14:37:48 -0800448 }
449
Jeff Brown4a06c802012-02-15 15:06:01 -0800450 synchronized (mLock) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700451 mCallbackQueues[callbackType].removeCallbacksLocked(action, token);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -0800452 if (action != null && token == null) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700453 mHandler.removeMessages(MSG_DO_SCHEDULE_CALLBACK, action);
Jeff Brown7ae9d5f2012-03-05 19:33:49 -0800454 }
Jeff Brown4a06c802012-02-15 15:06:01 -0800455 }
456 }
457
Jeff Brown20c4f872012-04-26 17:38:21 -0700458 /**
Jeff Browncae80492012-05-21 16:33:39 -0700459 * Posts a frame callback to run on the next frame.
460 * <p>
461 * The callback runs once then is automatically removed.
462 * </p>
Jeff Brown20c4f872012-04-26 17:38:21 -0700463 *
Jeff Browncae80492012-05-21 16:33:39 -0700464 * @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 Reck8d8af3c2014-07-01 15:23:45 -0700513 * This method provides the time in milliseconds when the frame started being rendered.
Jeff Browncae80492012-05-21 16:33:39 -0700514 * 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 Brown20c4f872012-04-26 17:38:21 -0700527 * This method should only be called from within a callback.
Jeff Browncae80492012-05-21 16:33:39 -0700528 * </p>
Jeff Brown20c4f872012-04-26 17:38:21 -0700529 *
530 * @return The frame start time, in the {@link SystemClock#uptimeMillis()} time base.
531 *
532 * @throws IllegalStateException if no frame is in progress.
Jeff Browncae80492012-05-21 16:33:39 -0700533 * @hide
Jeff Brown20c4f872012-04-26 17:38:21 -0700534 */
535 public long getFrameTime() {
John Reck315c3292014-05-09 19:21:04 -0700536 return getFrameTimeNanos() / TimeUtils.NANOS_PER_MS;
Jeff Brown20c4f872012-04-26 17:38:21 -0700537 }
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 Browncae80492012-05-21 16:33:39 -0700545 * @hide
Jeff Brown20c4f872012-04-26 17:38:21 -0700546 */
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 Jaggid6d6de62017-03-31 15:05:13 +0200557 /**
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 Brownebb2d8d2012-03-23 17:14:34 -0700569 private void scheduleFrameLocked(long now) {
570 if (!mFrameScheduled) {
571 mFrameScheduled = true;
Jeff Brown96e942d2011-11-30 19:55:01 -0800572 if (USE_VSYNC) {
Jeff Brownc42b28d2015-04-06 19:49:02 -0700573 if (DEBUG_FRAMES) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700574 Log.d(TAG, "Scheduling next frame on vsync.");
Jeff Brown96e942d2011-11-30 19:55:01 -0800575 }
Jeff Brown58aedbc2012-02-13 20:15:01 -0800576
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 Brown58aedbc2012-02-13 20:15:01 -0800580 if (isRunningOnLooperThreadLocked()) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -0800581 scheduleVsyncLocked();
Jeff Brown58aedbc2012-02-13 20:15:01 -0800582 } else {
Jeff Browne0dbd002012-02-15 19:34:58 -0800583 Message msg = mHandler.obtainMessage(MSG_DO_SCHEDULE_VSYNC);
584 msg.setAsynchronous(true);
585 mHandler.sendMessageAtFrontOfQueue(msg);
Jeff Brown58aedbc2012-02-13 20:15:01 -0800586 }
Jeff Brown96e942d2011-11-30 19:55:01 -0800587 } else {
Jeff Brown20c4f872012-04-26 17:38:21 -0700588 final long nextFrameTime = Math.max(
John Reck315c3292014-05-09 19:21:04 -0700589 mLastFrameTimeNanos / TimeUtils.NANOS_PER_MS + sFrameDelay, now);
Jeff Brownc42b28d2015-04-06 19:49:02 -0700590 if (DEBUG_FRAMES) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700591 Log.d(TAG, "Scheduling next frame in " + (nextFrameTime - now) + " ms.");
Jeff Brown96e942d2011-11-30 19:55:01 -0800592 }
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700593 Message msg = mHandler.obtainMessage(MSG_DO_FRAME);
Jeff Browne0dbd002012-02-15 19:34:58 -0800594 msg.setAsynchronous(true);
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700595 mHandler.sendMessageAtTime(msg, nextFrameTime);
Jeff Brown96e942d2011-11-30 19:55:01 -0800596 }
597 }
598 }
599
Jeff Browncae80492012-05-21 16:33:39 -0700600 void doFrame(long frameTimeNanos, int frame) {
Jeff Brown59bbef02012-05-07 16:43:25 -0700601 final long startNanos;
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700602 synchronized (mLock) {
603 if (!mFrameScheduled) {
604 return; // no work to do
Jeff Brown96e942d2011-11-30 19:55:01 -0800605 }
Jeff Brown59bbef02012-05-07 16:43:25 -0700606
Jeff Brownc42b28d2015-04-06 19:49:02 -0700607 if (DEBUG_JANK && mDebugPrintNextFrameTimeDelta) {
608 mDebugPrintNextFrameTimeDelta = false;
609 Log.d(TAG, "Frame time delta: "
610 + ((frameTimeNanos - mLastFrameTimeNanos) * 0.000001f) + " ms");
611 }
612
John Reckba6adf62015-02-19 14:36:50 -0800613 long intendedFrameTimeNanos = frameTimeNanos;
Jeff Brown59bbef02012-05-07 16:43:25 -0700614 startNanos = System.nanoTime();
Jeff Browncae80492012-05-21 16:33:39 -0700615 final long jitterNanos = startNanos - frameTimeNanos;
Jeff Brown59bbef02012-05-07 16:43:25 -0700616 if (jitterNanos >= mFrameIntervalNanos) {
Jeff Brown4fdf9c62012-06-11 15:25:48 -0700617 final long skippedFrames = jitterNanos / mFrameIntervalNanos;
618 if (skippedFrames >= SKIPPED_FRAME_WARNING_LIMIT) {
Jeff Brown265f1cc2012-06-11 18:01:06 -0700619 Log.i(TAG, "Skipped " + skippedFrames + " frames! "
Jeff Brown4fdf9c62012-06-11 15:25:48 -0700620 + "The application may be doing too much work on its main thread.");
621 }
Jeff Brown59bbef02012-05-07 16:43:25 -0700622 final long lastFrameOffset = jitterNanos % mFrameIntervalNanos;
Jeff Brownc42b28d2015-04-06 19:49:02 -0700623 if (DEBUG_JANK) {
Jeff Brown59bbef02012-05-07 16:43:25 -0700624 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 Brown4fdf9c62012-06-11 15:25:48 -0700627 + "Skipping " + skippedFrames + " frames and setting frame "
628 + "time to " + (lastFrameOffset * 0.000001f) + " ms in the past.");
Jeff Brown59bbef02012-05-07 16:43:25 -0700629 }
Jeff Browncae80492012-05-21 16:33:39 -0700630 frameTimeNanos = startNanos - lastFrameOffset;
Jeff Brown59bbef02012-05-07 16:43:25 -0700631 }
632
Jeff Browncae80492012-05-21 16:33:39 -0700633 if (frameTimeNanos < mLastFrameTimeNanos) {
Jeff Brownc42b28d2015-04-06 19:49:02 -0700634 if (DEBUG_JANK) {
Jeff Brown59bbef02012-05-07 16:43:25 -0700635 Log.d(TAG, "Frame time appears to be going backwards. May be due to a "
Jeff Brown4fdf9c62012-06-11 15:25:48 -0700636 + "previously skipped frame. Waiting for next vsync.");
Jeff Brown59bbef02012-05-07 16:43:25 -0700637 }
638 scheduleVsyncLocked();
639 return;
640 }
641
John Reckba6adf62015-02-19 14:36:50 -0800642 mFrameInfo.setVsync(intendedFrameTimeNanos, frameTimeNanos);
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700643 mFrameScheduled = false;
Jeff Browncae80492012-05-21 16:33:39 -0700644 mLastFrameTimeNanos = frameTimeNanos;
Jeff Brown20c4f872012-04-26 17:38:21 -0700645 }
646
Chris Craike22c59b2015-05-21 18:33:37 -0700647 try {
648 Trace.traceBegin(Trace.TRACE_TAG_VIEW, "Choreographer#doFrame");
John Recka2acb4f2016-08-05 07:58:37 -0700649 AnimationUtils.lockAnimationClock(frameTimeNanos / TimeUtils.NANOS_PER_MS);
John Reckba6adf62015-02-19 14:36:50 -0800650
Chris Craike22c59b2015-05-21 18:33:37 -0700651 mFrameInfo.markInputHandlingStart();
652 doCallbacks(Choreographer.CALLBACK_INPUT, frameTimeNanos);
John Reckba6adf62015-02-19 14:36:50 -0800653
Chris Craike22c59b2015-05-21 18:33:37 -0700654 mFrameInfo.markAnimationsStart();
655 doCallbacks(Choreographer.CALLBACK_ANIMATION, frameTimeNanos);
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700656
Chris Craike22c59b2015-05-21 18:33:37 -0700657 mFrameInfo.markPerformTraversalsStart();
658 doCallbacks(Choreographer.CALLBACK_TRAVERSAL, frameTimeNanos);
659
660 doCallbacks(Choreographer.CALLBACK_COMMIT, frameTimeNanos);
661 } finally {
John Recka2acb4f2016-08-05 07:58:37 -0700662 AnimationUtils.unlockAnimationClock();
Chris Craike22c59b2015-05-21 18:33:37 -0700663 Trace.traceEnd(Trace.TRACE_TAG_VIEW);
664 }
Jeff Brownc42b28d2015-04-06 19:49:02 -0700665
666 if (DEBUG_FRAMES) {
Jeff Brown20c4f872012-04-26 17:38:21 -0700667 final long endNanos = System.nanoTime();
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700668 Log.d(TAG, "Frame " + frame + ": Finished, took "
Jeff Brown20c4f872012-04-26 17:38:21 -0700669 + (endNanos - startNanos) * 0.000001f + " ms, latency "
Jeff Browncae80492012-05-21 16:33:39 -0700670 + (startNanos - frameTimeNanos) * 0.000001f + " ms.");
Jeff Brown96e942d2011-11-30 19:55:01 -0800671 }
672 }
673
Jeff Browncae80492012-05-21 16:33:39 -0700674 void doCallbacks(int callbackType, long frameTimeNanos) {
675 CallbackRecord callbacks;
Jeff Brown96858852012-02-14 13:45:06 -0800676 synchronized (mLock) {
Jeff Browncae80492012-05-21 16:33:39 -0700677 // 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 Brownc42b28d2015-04-06 19:49:02 -0700680 final long now = System.nanoTime();
681 callbacks = mCallbackQueues[callbackType].extractDueCallbacksLocked(
682 now / TimeUtils.NANOS_PER_MS);
Jeff Brown20c4f872012-04-26 17:38:21 -0700683 if (callbacks == null) {
684 return;
685 }
686 mCallbacksRunning = true;
Jeff Brownc42b28d2015-04-06 19:49:02 -0700687
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 Craike22c59b2015-05-21 18:33:37 -0700698 Trace.traceCounter(Trace.TRACE_TAG_VIEW, "jitterNanos", (int) jitterNanos);
Jeff Brownc42b28d2015-04-06 19:49:02 -0700699 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 Brown96858852012-02-14 13:45:06 -0800714 }
Jeff Brown20c4f872012-04-26 17:38:21 -0700715 try {
Chris Craike22c59b2015-05-21 18:33:37 -0700716 Trace.traceBegin(Trace.TRACE_TAG_VIEW, CALLBACK_TRACE_TITLES[callbackType]);
Jeff Browncae80492012-05-21 16:33:39 -0700717 for (CallbackRecord c = callbacks; c != null; c = c.next) {
Jeff Brownc42b28d2015-04-06 19:49:02 -0700718 if (DEBUG_FRAMES) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700719 Log.d(TAG, "RunCallback: type=" + callbackType
720 + ", action=" + c.action + ", token=" + c.token
721 + ", latencyMillis=" + (SystemClock.uptimeMillis() - c.dueTime));
722 }
Jeff Browncae80492012-05-21 16:33:39 -0700723 c.run(frameTimeNanos);
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700724 }
Jeff Brown20c4f872012-04-26 17:38:21 -0700725 } finally {
Jeff Brown96858852012-02-14 13:45:06 -0800726 synchronized (mLock) {
Jeff Brown20c4f872012-04-26 17:38:21 -0700727 mCallbacksRunning = false;
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700728 do {
Jeff Browncae80492012-05-21 16:33:39 -0700729 final CallbackRecord next = callbacks.next;
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700730 recycleCallbackLocked(callbacks);
731 callbacks = next;
732 } while (callbacks != null);
Jeff Brown96858852012-02-14 13:45:06 -0800733 }
Chris Craike22c59b2015-05-21 18:33:37 -0700734 Trace.traceEnd(Trace.TRACE_TAG_VIEW);
Jeff Brown96858852012-02-14 13:45:06 -0800735 }
Jeff Brown96858852012-02-14 13:45:06 -0800736 }
737
738 void doScheduleVsync() {
739 synchronized (mLock) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700740 if (mFrameScheduled) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -0800741 scheduleVsyncLocked();
742 }
Jeff Brown96858852012-02-14 13:45:06 -0800743 }
744 }
745
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700746 void doScheduleCallback(int callbackType) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -0800747 synchronized (mLock) {
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700748 if (!mFrameScheduled) {
749 final long now = SystemClock.uptimeMillis();
750 if (mCallbackQueues[callbackType].hasDueCallbacksLocked(now)) {
751 scheduleFrameLocked(now);
752 }
Jeff Brown7ae9d5f2012-03-05 19:33:49 -0800753 }
Jeff Brown96e942d2011-11-30 19:55:01 -0800754 }
755 }
756
Jeff Brown7ae9d5f2012-03-05 19:33:49 -0800757 private void scheduleVsyncLocked() {
758 mDisplayEventReceiver.scheduleVsync();
759 }
760
Jeff Brown58aedbc2012-02-13 20:15:01 -0800761 private boolean isRunningOnLooperThreadLocked() {
762 return Looper.myLooper() == mLooper;
763 }
764
Jeff Browncae80492012-05-21 16:33:39 -0700765 private CallbackRecord obtainCallbackLocked(long dueTime, Object action, Object token) {
766 CallbackRecord callback = mCallbackPool;
Jeff Brown96858852012-02-14 13:45:06 -0800767 if (callback == null) {
Jeff Browncae80492012-05-21 16:33:39 -0700768 callback = new CallbackRecord();
Jeff Brown96858852012-02-14 13:45:06 -0800769 } else {
770 mCallbackPool = callback.next;
771 callback.next = null;
772 }
Jeff Brown7ae9d5f2012-03-05 19:33:49 -0800773 callback.dueTime = dueTime;
774 callback.action = action;
775 callback.token = token;
Jeff Brown96858852012-02-14 13:45:06 -0800776 return callback;
777 }
778
Jeff Browncae80492012-05-21 16:33:39 -0700779 private void recycleCallbackLocked(CallbackRecord callback) {
Jeff Brown7ae9d5f2012-03-05 19:33:49 -0800780 callback.action = null;
781 callback.token = null;
Jeff Brown96858852012-02-14 13:45:06 -0800782 callback.next = mCallbackPool;
783 mCallbackPool = callback;
784 }
785
Jeff Browncae80492012-05-21 16:33:39 -0700786 /**
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 Brown96858852012-02-14 13:45:06 -0800817 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 Brownebb2d8d2012-03-23 17:14:34 -0700825 case MSG_DO_FRAME:
Jeff Brown20c4f872012-04-26 17:38:21 -0700826 doFrame(System.nanoTime(), 0);
Jeff Brown96858852012-02-14 13:45:06 -0800827 break;
828 case MSG_DO_SCHEDULE_VSYNC:
829 doScheduleVsync();
830 break;
Jeff Brownebb2d8d2012-03-23 17:14:34 -0700831 case MSG_DO_SCHEDULE_CALLBACK:
832 doScheduleCallback(msg.arg1);
Jeff Brown2b6cb9a2012-03-05 17:21:01 -0800833 break;
Jeff Brown96858852012-02-14 13:45:06 -0800834 }
835 }
836 }
837
Jeff Brownb0806602012-05-16 12:50:41 -0700838 private final class FrameDisplayEventReceiver extends DisplayEventReceiver
839 implements Runnable {
Jeff Brown4fdf9c62012-06-11 15:25:48 -0700840 private boolean mHavePendingVsync;
Jeff Brownb0806602012-05-16 12:50:41 -0700841 private long mTimestampNanos;
842 private int mFrame;
843
Jorim Jaggi34a0cdb2017-06-08 15:40:38 -0700844 public FrameDisplayEventReceiver(Looper looper, int vsyncSource) {
845 super(looper, vsyncSource);
Jeff Brown96e942d2011-11-30 19:55:01 -0800846 }
847
848 @Override
Jeff Browne87bf032012-09-20 18:30:13 -0700849 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 Agopian3866f0d2013-02-11 22:08:48 -0800858 if (builtInDisplayId != SurfaceControl.BUILT_IN_DISPLAY_ID_MAIN) {
Jeff Browne87bf032012-09-20 18:30:13 -0700859 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 Brownb0806602012-05-16 12:50:41 -0700867 // 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 Brown4fdf9c62012-06-11 15:25:48 -0700872 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 Brownba7261132012-06-14 23:48:40 -0700880 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 Brown4fdf9c62012-06-11 15:25:48 -0700886
Jeff Brownb0806602012-05-16 12:50:41 -0700887 mTimestampNanos = timestampNanos;
888 mFrame = frame;
889 Message msg = Message.obtain(mHandler, this);
890 msg.setAsynchronous(true);
John Reck315c3292014-05-09 19:21:04 -0700891 mHandler.sendMessageAtTime(msg, timestampNanos / TimeUtils.NANOS_PER_MS);
Jeff Brownb0806602012-05-16 12:50:41 -0700892 }
893
894 @Override
895 public void run() {
Jeff Brown4fdf9c62012-06-11 15:25:48 -0700896 mHavePendingVsync = false;
Jeff Brownb0806602012-05-16 12:50:41 -0700897 doFrame(mTimestampNanos, mFrame);
Jeff Brown96e942d2011-11-30 19:55:01 -0800898 }
899 }
Jeff Brown96858852012-02-14 13:45:06 -0800900
Jeff Browncae80492012-05-21 16:33:39 -0700901 private static final class CallbackRecord {
902 public CallbackRecord next;
Jeff Brown7ae9d5f2012-03-05 19:33:49 -0800903 public long dueTime;
Jeff Browncae80492012-05-21 16:33:39 -0700904 public Object action; // Runnable or FrameCallback
Jeff Brown7ae9d5f2012-03-05 19:33:49 -0800905 public Object token;
Jeff Browncae80492012-05-21 16:33:39 -0700906
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 Brown96858852012-02-14 13:45:06 -0800914 }
Jeff Brown43ea54b2012-03-09 14:37:48 -0800915
916 private final class CallbackQueue {
Jeff Browncae80492012-05-21 16:33:39 -0700917 private CallbackRecord mHead;
Jeff Brown43ea54b2012-03-09 14:37:48 -0800918
919 public boolean hasDueCallbacksLocked(long now) {
920 return mHead != null && mHead.dueTime <= now;
921 }
922
Jeff Browncae80492012-05-21 16:33:39 -0700923 public CallbackRecord extractDueCallbacksLocked(long now) {
924 CallbackRecord callbacks = mHead;
Jeff Brown43ea54b2012-03-09 14:37:48 -0800925 if (callbacks == null || callbacks.dueTime > now) {
926 return null;
927 }
928
Jeff Browncae80492012-05-21 16:33:39 -0700929 CallbackRecord last = callbacks;
930 CallbackRecord next = last.next;
Jeff Brown43ea54b2012-03-09 14:37:48 -0800931 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 Browncae80492012-05-21 16:33:39 -0700943 public void addCallbackLocked(long dueTime, Object action, Object token) {
944 CallbackRecord callback = obtainCallbackLocked(dueTime, action, token);
945 CallbackRecord entry = mHead;
Jeff Brown43ea54b2012-03-09 14:37:48 -0800946 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 Browncae80492012-05-21 16:33:39 -0700965 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 Brown43ea54b2012-03-09 14:37:48 -0800969 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 Brown96e942d2011-11-30 19:55:01 -0800984}