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