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