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