PageRenderTime 54ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/core/java/android/view/Choreographer.java

https://bitbucket.org/iainh/projectzzz-platform-frameworks-base
Java | 798 lines | 432 code | 77 blank | 289 comment | 85 complexity | 74d6477a8c4ce1b4c0a8a8eaab3242bb MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, CC0-1.0
  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. package android.view;
  17. import android.os.Handler;
  18. import android.os.Looper;
  19. import android.os.Message;
  20. import android.os.SystemClock;
  21. import android.os.SystemProperties;
  22. import android.util.Log;
  23. /**
  24. * Coordinates the timing of animations, input and drawing.
  25. * <p>
  26. * The choreographer receives timing pulses (such as vertical synchronization)
  27. * from the display subsystem then schedules work to occur as part of rendering
  28. * the next display frame.
  29. * </p><p>
  30. * Applications typically interact with the choreographer indirectly using
  31. * higher level abstractions in the animation framework or the view hierarchy.
  32. * Here are some examples of things you can do using the higher-level APIs.
  33. * </p>
  34. * <ul>
  35. * <li>To post an animation to be processed on a regular time basis synchronized with
  36. * display frame rendering, use {@link android.animation.ValueAnimator#start}.</li>
  37. * <li>To post a {@link Runnable} to be invoked once at the beginning of the next display
  38. * frame, use {@link View#postOnAnimation}.</li>
  39. * <li>To post a {@link Runnable} to be invoked once at the beginning of the next display
  40. * frame after a delay, use {@link View#postOnAnimationDelayed}.</li>
  41. * <li>To post a call to {@link View#invalidate()} to occur once at the beginning of the
  42. * next display frame, use {@link View#postInvalidateOnAnimation()} or
  43. * {@link View#postInvalidateOnAnimation(int, int, int, int)}.</li>
  44. * <li>To ensure that the contents of a {@link View} scroll smoothly and are drawn in
  45. * sync with display frame rendering, do nothing. This already happens automatically.
  46. * {@link View#onDraw} will be called at the appropriate time.</li>
  47. * </ul>
  48. * <p>
  49. * However, there are a few cases where you might want to use the functions of the
  50. * choreographer directly in your application. Here are some examples.
  51. * </p>
  52. * <ul>
  53. * <li>If your application does its rendering in a different thread, possibly using GL,
  54. * or does not use the animation framework or view hierarchy at all
  55. * and you want to ensure that it is appropriately synchronized with the display, then use
  56. * {@link Choreographer#postFrameCallback}.</li>
  57. * <li>... and that's about it.</li>
  58. * </ul>
  59. * <p>
  60. * Each {@link Looper} thread has its own choreographer. Other threads can
  61. * post callbacks to run on the choreographer but they will run on the {@link Looper}
  62. * to which the choreographer belongs.
  63. * </p>
  64. */
  65. public final class Choreographer {
  66. private static final String TAG = "Choreographer";
  67. private static final boolean DEBUG = false;
  68. // The default amount of time in ms between animation frames.
  69. // When vsync is not enabled, we want to have some idea of how long we should
  70. // wait before posting the next animation message. It is important that the
  71. // default value be less than the true inter-frame delay on all devices to avoid
  72. // situations where we might skip frames by waiting too long (we must compensate
  73. // for jitter and hardware variations). Regardless of this value, the animation
  74. // and display loop is ultimately rate-limited by how fast new graphics buffers can
  75. // be dequeued.
  76. private static final long DEFAULT_FRAME_DELAY = 10;
  77. // The number of milliseconds between animation frames.
  78. private static volatile long sFrameDelay = DEFAULT_FRAME_DELAY;
  79. // Thread local storage for the choreographer.
  80. private static final ThreadLocal<Choreographer> sThreadInstance =
  81. new ThreadLocal<Choreographer>() {
  82. @Override
  83. protected Choreographer initialValue() {
  84. Looper looper = Looper.myLooper();
  85. if (looper == null) {
  86. throw new IllegalStateException("The current thread must have a looper!");
  87. }
  88. return new Choreographer(looper);
  89. }
  90. };
  91. // Enable/disable vsync for animations and drawing.
  92. private static final boolean USE_VSYNC = SystemProperties.getBoolean(
  93. "debug.choreographer.vsync", true);
  94. // Enable/disable using the frame time instead of returning now.
  95. private static final boolean USE_FRAME_TIME = SystemProperties.getBoolean(
  96. "debug.choreographer.frametime", true);
  97. // Set a limit to warn about skipped frames.
  98. // Skipped frames imply jank.
  99. private static final int SKIPPED_FRAME_WARNING_LIMIT = SystemProperties.getInt(
  100. "debug.choreographer.skipwarning", 30);
  101. private static final long NANOS_PER_MS = 1000000;
  102. private static final int MSG_DO_FRAME = 0;
  103. private static final int MSG_DO_SCHEDULE_VSYNC = 1;
  104. private static final int MSG_DO_SCHEDULE_CALLBACK = 2;
  105. // All frame callbacks posted by applications have this token.
  106. private static final Object FRAME_CALLBACK_TOKEN = new Object() {
  107. public String toString() { return "FRAME_CALLBACK_TOKEN"; }
  108. };
  109. private final Object mLock = new Object();
  110. private final Looper mLooper;
  111. private final FrameHandler mHandler;
  112. // The display event receiver can only be accessed by the looper thread to which
  113. // it is attached. We take care to ensure that we post message to the looper
  114. // if appropriate when interacting with the display event receiver.
  115. private final FrameDisplayEventReceiver mDisplayEventReceiver;
  116. private CallbackRecord mCallbackPool;
  117. private final CallbackQueue[] mCallbackQueues;
  118. private boolean mFrameScheduled;
  119. private boolean mCallbacksRunning;
  120. private long mLastFrameTimeNanos;
  121. private long mFrameIntervalNanos;
  122. /**
  123. * Callback type: Input callback. Runs first.
  124. * @hide
  125. */
  126. public static final int CALLBACK_INPUT = 0;
  127. /**
  128. * Callback type: Animation callback. Runs before traversals.
  129. * @hide
  130. */
  131. public static final int CALLBACK_ANIMATION = 1;
  132. /**
  133. * Callback type: Traversal callback. Handles layout and draw. Runs last
  134. * after all other asynchronous messages have been handled.
  135. * @hide
  136. */
  137. public static final int CALLBACK_TRAVERSAL = 2;
  138. private static final int CALLBACK_LAST = CALLBACK_TRAVERSAL;
  139. private Choreographer(Looper looper) {
  140. mLooper = looper;
  141. mHandler = new FrameHandler(looper);
  142. mDisplayEventReceiver = USE_VSYNC ? new FrameDisplayEventReceiver(looper) : null;
  143. mLastFrameTimeNanos = Long.MIN_VALUE;
  144. mFrameIntervalNanos = (long)(1000000000 /
  145. new Display(Display.DEFAULT_DISPLAY, null).getRefreshRate());
  146. mCallbackQueues = new CallbackQueue[CALLBACK_LAST + 1];
  147. for (int i = 0; i <= CALLBACK_LAST; i++) {
  148. mCallbackQueues[i] = new CallbackQueue();
  149. }
  150. }
  151. /**
  152. * Gets the choreographer for the calling thread. Must be called from
  153. * a thread that already has a {@link android.os.Looper} associated with it.
  154. *
  155. * @return The choreographer for this thread.
  156. * @throws IllegalStateException if the thread does not have a looper.
  157. */
  158. public static Choreographer getInstance() {
  159. return sThreadInstance.get();
  160. }
  161. /**
  162. * The amount of time, in milliseconds, between each frame of the animation.
  163. * <p>
  164. * This is a requested time that the animation will attempt to honor, but the actual delay
  165. * between frames may be different, depending on system load and capabilities. This is a static
  166. * function because the same delay will be applied to all animations, since they are all
  167. * run off of a single timing loop.
  168. * </p><p>
  169. * The frame delay may be ignored when the animation system uses an external timing
  170. * source, such as the display refresh rate (vsync), to govern animations.
  171. * </p>
  172. *
  173. * @return the requested time between frames, in milliseconds
  174. * @hide
  175. */
  176. public static long getFrameDelay() {
  177. return sFrameDelay;
  178. }
  179. /**
  180. * The amount of time, in milliseconds, between each frame of the animation.
  181. * <p>
  182. * This is a requested time that the animation will attempt to honor, but the actual delay
  183. * between frames may be different, depending on system load and capabilities. This is a static
  184. * function because the same delay will be applied to all animations, since they are all
  185. * run off of a single timing loop.
  186. * </p><p>
  187. * The frame delay may be ignored when the animation system uses an external timing
  188. * source, such as the display refresh rate (vsync), to govern animations.
  189. * </p>
  190. *
  191. * @param frameDelay the requested time between frames, in milliseconds
  192. * @hide
  193. */
  194. public static void setFrameDelay(long frameDelay) {
  195. sFrameDelay = frameDelay;
  196. }
  197. /**
  198. * Subtracts typical frame delay time from a delay interval in milliseconds.
  199. * <p>
  200. * This method can be used to compensate for animation delay times that have baked
  201. * in assumptions about the frame delay. For example, it's quite common for code to
  202. * assume a 60Hz frame time and bake in a 16ms delay. When we call
  203. * {@link #postAnimationCallbackDelayed} we want to know how long to wait before
  204. * posting the animation callback but let the animation timer take care of the remaining
  205. * frame delay time.
  206. * </p><p>
  207. * This method is somewhat conservative about how much of the frame delay it
  208. * subtracts. It uses the same value returned by {@link #getFrameDelay} which by
  209. * default is 10ms even though many parts of the system assume 16ms. Consequently,
  210. * we might still wait 6ms before posting an animation callback that we want to run
  211. * on the next frame, but this is much better than waiting a whole 16ms and likely
  212. * missing the deadline.
  213. * </p>
  214. *
  215. * @param delayMillis The original delay time including an assumed frame delay.
  216. * @return The adjusted delay time with the assumed frame delay subtracted out.
  217. * @hide
  218. */
  219. public static long subtractFrameDelay(long delayMillis) {
  220. final long frameDelay = sFrameDelay;
  221. return delayMillis <= frameDelay ? 0 : delayMillis - frameDelay;
  222. }
  223. /**
  224. * Posts a callback to run on the next frame.
  225. * <p>
  226. * The callback runs once then is automatically removed.
  227. * </p>
  228. *
  229. * @param callbackType The callback type.
  230. * @param action The callback action to run during the next frame.
  231. * @param token The callback token, or null if none.
  232. *
  233. * @see #removeCallbacks
  234. * @hide
  235. */
  236. public void postCallback(int callbackType, Runnable action, Object token) {
  237. postCallbackDelayed(callbackType, action, token, 0);
  238. }
  239. /**
  240. * Posts a callback to run on the next frame after the specified delay.
  241. * <p>
  242. * The callback runs once then is automatically removed.
  243. * </p>
  244. *
  245. * @param callbackType The callback type.
  246. * @param action The callback action to run during the next frame after the specified delay.
  247. * @param token The callback token, or null if none.
  248. * @param delayMillis The delay time in milliseconds.
  249. *
  250. * @see #removeCallback
  251. * @hide
  252. */
  253. public void postCallbackDelayed(int callbackType,
  254. Runnable action, Object token, long delayMillis) {
  255. if (action == null) {
  256. throw new IllegalArgumentException("action must not be null");
  257. }
  258. if (callbackType < 0 || callbackType > CALLBACK_LAST) {
  259. throw new IllegalArgumentException("callbackType is invalid");
  260. }
  261. postCallbackDelayedInternal(callbackType, action, token, delayMillis);
  262. }
  263. private void postCallbackDelayedInternal(int callbackType,
  264. Object action, Object token, long delayMillis) {
  265. if (DEBUG) {
  266. Log.d(TAG, "PostCallback: type=" + callbackType
  267. + ", action=" + action + ", token=" + token
  268. + ", delayMillis=" + delayMillis);
  269. }
  270. synchronized (mLock) {
  271. final long now = SystemClock.uptimeMillis();
  272. final long dueTime = now + delayMillis;
  273. mCallbackQueues[callbackType].addCallbackLocked(dueTime, action, token);
  274. if (dueTime <= now) {
  275. scheduleFrameLocked(now);
  276. } else {
  277. Message msg = mHandler.obtainMessage(MSG_DO_SCHEDULE_CALLBACK, action);
  278. msg.arg1 = callbackType;
  279. msg.setAsynchronous(true);
  280. mHandler.sendMessageAtTime(msg, dueTime);
  281. }
  282. }
  283. }
  284. /**
  285. * Removes callbacks that have the specified action and token.
  286. *
  287. * @param callbackType The callback type.
  288. * @param action The action property of the callbacks to remove, or null to remove
  289. * callbacks with any action.
  290. * @param token The token property of the callbacks to remove, or null to remove
  291. * callbacks with any token.
  292. *
  293. * @see #postCallback
  294. * @see #postCallbackDelayed
  295. * @hide
  296. */
  297. public void removeCallbacks(int callbackType, Runnable action, Object token) {
  298. if (callbackType < 0 || callbackType > CALLBACK_LAST) {
  299. throw new IllegalArgumentException("callbackType is invalid");
  300. }
  301. removeCallbacksInternal(callbackType, action, token);
  302. }
  303. private void removeCallbacksInternal(int callbackType, Object action, Object token) {
  304. if (DEBUG) {
  305. Log.d(TAG, "RemoveCallbacks: type=" + callbackType
  306. + ", action=" + action + ", token=" + token);
  307. }
  308. synchronized (mLock) {
  309. mCallbackQueues[callbackType].removeCallbacksLocked(action, token);
  310. if (action != null && token == null) {
  311. mHandler.removeMessages(MSG_DO_SCHEDULE_CALLBACK, action);
  312. }
  313. }
  314. }
  315. /**
  316. * Posts a frame callback to run on the next frame.
  317. * <p>
  318. * The callback runs once then is automatically removed.
  319. * </p>
  320. *
  321. * @param callback The frame callback to run during the next frame.
  322. *
  323. * @see #postFrameCallbackDelayed
  324. * @see #removeFrameCallback
  325. */
  326. public void postFrameCallback(FrameCallback callback) {
  327. postFrameCallbackDelayed(callback, 0);
  328. }
  329. /**
  330. * Posts a frame callback to run on the next frame after the specified delay.
  331. * <p>
  332. * The callback runs once then is automatically removed.
  333. * </p>
  334. *
  335. * @param callback The frame callback to run during the next frame.
  336. * @param delayMillis The delay time in milliseconds.
  337. *
  338. * @see #postFrameCallback
  339. * @see #removeFrameCallback
  340. */
  341. public void postFrameCallbackDelayed(FrameCallback callback, long delayMillis) {
  342. if (callback == null) {
  343. throw new IllegalArgumentException("callback must not be null");
  344. }
  345. postCallbackDelayedInternal(CALLBACK_ANIMATION,
  346. callback, FRAME_CALLBACK_TOKEN, delayMillis);
  347. }
  348. /**
  349. * Removes a previously posted frame callback.
  350. *
  351. * @param callback The frame callback to remove.
  352. *
  353. * @see #postFrameCallback
  354. * @see #postFrameCallbackDelayed
  355. */
  356. public void removeFrameCallback(FrameCallback callback) {
  357. if (callback == null) {
  358. throw new IllegalArgumentException("callback must not be null");
  359. }
  360. removeCallbacksInternal(CALLBACK_ANIMATION, callback, FRAME_CALLBACK_TOKEN);
  361. }
  362. /**
  363. * Gets the time when the current frame started.
  364. * <p>
  365. * This method provides the time in nanoseconds when the frame started being rendered.
  366. * The frame time provides a stable time base for synchronizing animations
  367. * and drawing. It should be used instead of {@link SystemClock#uptimeMillis()}
  368. * or {@link System#nanoTime()} for animations and drawing in the UI. Using the frame
  369. * time helps to reduce inter-frame jitter because the frame time is fixed at the time
  370. * the frame was scheduled to start, regardless of when the animations or drawing
  371. * callback actually runs. All callbacks that run as part of rendering a frame will
  372. * observe the same frame time so using the frame time also helps to synchronize effects
  373. * that are performed by different callbacks.
  374. * </p><p>
  375. * Please note that the framework already takes care to process animations and
  376. * drawing using the frame time as a stable time base. Most applications should
  377. * not need to use the frame time information directly.
  378. * </p><p>
  379. * This method should only be called from within a callback.
  380. * </p>
  381. *
  382. * @return The frame start time, in the {@link SystemClock#uptimeMillis()} time base.
  383. *
  384. * @throws IllegalStateException if no frame is in progress.
  385. * @hide
  386. */
  387. public long getFrameTime() {
  388. return getFrameTimeNanos() / NANOS_PER_MS;
  389. }
  390. /**
  391. * Same as {@link #getFrameTime()} but with nanosecond precision.
  392. *
  393. * @return The frame start time, in the {@link System#nanoTime()} time base.
  394. *
  395. * @throws IllegalStateException if no frame is in progress.
  396. * @hide
  397. */
  398. public long getFrameTimeNanos() {
  399. synchronized (mLock) {
  400. if (!mCallbacksRunning) {
  401. throw new IllegalStateException("This method must only be called as "
  402. + "part of a callback while a frame is in progress.");
  403. }
  404. return USE_FRAME_TIME ? mLastFrameTimeNanos : System.nanoTime();
  405. }
  406. }
  407. private void scheduleFrameLocked(long now) {
  408. if (!mFrameScheduled) {
  409. mFrameScheduled = true;
  410. if (USE_VSYNC) {
  411. if (DEBUG) {
  412. Log.d(TAG, "Scheduling next frame on vsync.");
  413. }
  414. // If running on the Looper thread, then schedule the vsync immediately,
  415. // otherwise post a message to schedule the vsync from the UI thread
  416. // as soon as possible.
  417. if (isRunningOnLooperThreadLocked()) {
  418. scheduleVsyncLocked();
  419. } else {
  420. Message msg = mHandler.obtainMessage(MSG_DO_SCHEDULE_VSYNC);
  421. msg.setAsynchronous(true);
  422. mHandler.sendMessageAtFrontOfQueue(msg);
  423. }
  424. } else {
  425. final long nextFrameTime = Math.max(
  426. mLastFrameTimeNanos / NANOS_PER_MS + sFrameDelay, now);
  427. if (DEBUG) {
  428. Log.d(TAG, "Scheduling next frame in " + (nextFrameTime - now) + " ms.");
  429. }
  430. Message msg = mHandler.obtainMessage(MSG_DO_FRAME);
  431. msg.setAsynchronous(true);
  432. mHandler.sendMessageAtTime(msg, nextFrameTime);
  433. }
  434. }
  435. }
  436. void doFrame(long frameTimeNanos, int frame) {
  437. final long startNanos;
  438. synchronized (mLock) {
  439. if (!mFrameScheduled) {
  440. return; // no work to do
  441. }
  442. startNanos = System.nanoTime();
  443. final long jitterNanos = startNanos - frameTimeNanos;
  444. if (jitterNanos >= mFrameIntervalNanos) {
  445. final long skippedFrames = jitterNanos / mFrameIntervalNanos;
  446. if (skippedFrames >= SKIPPED_FRAME_WARNING_LIMIT) {
  447. Log.i(TAG, "Skipped " + skippedFrames + " frames! "
  448. + "The application may be doing too much work on its main thread.");
  449. }
  450. final long lastFrameOffset = jitterNanos % mFrameIntervalNanos;
  451. if (DEBUG) {
  452. Log.d(TAG, "Missed vsync by " + (jitterNanos * 0.000001f) + " ms "
  453. + "which is more than the frame interval of "
  454. + (mFrameIntervalNanos * 0.000001f) + " ms! "
  455. + "Skipping " + skippedFrames + " frames and setting frame "
  456. + "time to " + (lastFrameOffset * 0.000001f) + " ms in the past.");
  457. }
  458. frameTimeNanos = startNanos - lastFrameOffset;
  459. }
  460. if (frameTimeNanos < mLastFrameTimeNanos) {
  461. if (DEBUG) {
  462. Log.d(TAG, "Frame time appears to be going backwards. May be due to a "
  463. + "previously skipped frame. Waiting for next vsync.");
  464. }
  465. scheduleVsyncLocked();
  466. return;
  467. }
  468. mFrameScheduled = false;
  469. mLastFrameTimeNanos = frameTimeNanos;
  470. }
  471. doCallbacks(Choreographer.CALLBACK_INPUT, frameTimeNanos);
  472. doCallbacks(Choreographer.CALLBACK_ANIMATION, frameTimeNanos);
  473. doCallbacks(Choreographer.CALLBACK_TRAVERSAL, frameTimeNanos);
  474. if (DEBUG) {
  475. final long endNanos = System.nanoTime();
  476. Log.d(TAG, "Frame " + frame + ": Finished, took "
  477. + (endNanos - startNanos) * 0.000001f + " ms, latency "
  478. + (startNanos - frameTimeNanos) * 0.000001f + " ms.");
  479. }
  480. }
  481. void doCallbacks(int callbackType, long frameTimeNanos) {
  482. CallbackRecord callbacks;
  483. synchronized (mLock) {
  484. // We use "now" to determine when callbacks become due because it's possible
  485. // for earlier processing phases in a frame to post callbacks that should run
  486. // in a following phase, such as an input event that causes an animation to start.
  487. final long now = SystemClock.uptimeMillis();
  488. callbacks = mCallbackQueues[callbackType].extractDueCallbacksLocked(now);
  489. if (callbacks == null) {
  490. return;
  491. }
  492. mCallbacksRunning = true;
  493. }
  494. try {
  495. for (CallbackRecord c = callbacks; c != null; c = c.next) {
  496. if (DEBUG) {
  497. Log.d(TAG, "RunCallback: type=" + callbackType
  498. + ", action=" + c.action + ", token=" + c.token
  499. + ", latencyMillis=" + (SystemClock.uptimeMillis() - c.dueTime));
  500. }
  501. c.run(frameTimeNanos);
  502. }
  503. } finally {
  504. synchronized (mLock) {
  505. mCallbacksRunning = false;
  506. do {
  507. final CallbackRecord next = callbacks.next;
  508. recycleCallbackLocked(callbacks);
  509. callbacks = next;
  510. } while (callbacks != null);
  511. }
  512. }
  513. }
  514. void doScheduleVsync() {
  515. synchronized (mLock) {
  516. if (mFrameScheduled) {
  517. scheduleVsyncLocked();
  518. }
  519. }
  520. }
  521. void doScheduleCallback(int callbackType) {
  522. synchronized (mLock) {
  523. if (!mFrameScheduled) {
  524. final long now = SystemClock.uptimeMillis();
  525. if (mCallbackQueues[callbackType].hasDueCallbacksLocked(now)) {
  526. scheduleFrameLocked(now);
  527. }
  528. }
  529. }
  530. }
  531. private void scheduleVsyncLocked() {
  532. mDisplayEventReceiver.scheduleVsync();
  533. }
  534. private boolean isRunningOnLooperThreadLocked() {
  535. return Looper.myLooper() == mLooper;
  536. }
  537. private CallbackRecord obtainCallbackLocked(long dueTime, Object action, Object token) {
  538. CallbackRecord callback = mCallbackPool;
  539. if (callback == null) {
  540. callback = new CallbackRecord();
  541. } else {
  542. mCallbackPool = callback.next;
  543. callback.next = null;
  544. }
  545. callback.dueTime = dueTime;
  546. callback.action = action;
  547. callback.token = token;
  548. return callback;
  549. }
  550. private void recycleCallbackLocked(CallbackRecord callback) {
  551. callback.action = null;
  552. callback.token = null;
  553. callback.next = mCallbackPool;
  554. mCallbackPool = callback;
  555. }
  556. /**
  557. * Implement this interface to receive a callback when a new display frame is
  558. * being rendered. The callback is invoked on the {@link Looper} thread to
  559. * which the {@link Choreographer} is attached.
  560. */
  561. public interface FrameCallback {
  562. /**
  563. * Called when a new display frame is being rendered.
  564. * <p>
  565. * This method provides the time in nanoseconds when the frame started being rendered.
  566. * The frame time provides a stable time base for synchronizing animations
  567. * and drawing. It should be used instead of {@link SystemClock#uptimeMillis()}
  568. * or {@link System#nanoTime()} for animations and drawing in the UI. Using the frame
  569. * time helps to reduce inter-frame jitter because the frame time is fixed at the time
  570. * the frame was scheduled to start, regardless of when the animations or drawing
  571. * callback actually runs. All callbacks that run as part of rendering a frame will
  572. * observe the same frame time so using the frame time also helps to synchronize effects
  573. * that are performed by different callbacks.
  574. * </p><p>
  575. * Please note that the framework already takes care to process animations and
  576. * drawing using the frame time as a stable time base. Most applications should
  577. * not need to use the frame time information directly.
  578. * </p>
  579. *
  580. * @param frameTimeNanos The time in nanoseconds when the frame started being rendered,
  581. * in the {@link System#nanoTime()} timebase. Divide this value by {@code 1000000}
  582. * to convert it to the {@link SystemClock#uptimeMillis()} time base.
  583. */
  584. public void doFrame(long frameTimeNanos);
  585. }
  586. private final class FrameHandler extends Handler {
  587. public FrameHandler(Looper looper) {
  588. super(looper);
  589. }
  590. @Override
  591. public void handleMessage(Message msg) {
  592. switch (msg.what) {
  593. case MSG_DO_FRAME:
  594. doFrame(System.nanoTime(), 0);
  595. break;
  596. case MSG_DO_SCHEDULE_VSYNC:
  597. doScheduleVsync();
  598. break;
  599. case MSG_DO_SCHEDULE_CALLBACK:
  600. doScheduleCallback(msg.arg1);
  601. break;
  602. }
  603. }
  604. }
  605. private final class FrameDisplayEventReceiver extends DisplayEventReceiver
  606. implements Runnable {
  607. private boolean mHavePendingVsync;
  608. private long mTimestampNanos;
  609. private int mFrame;
  610. public FrameDisplayEventReceiver(Looper looper) {
  611. super(looper);
  612. }
  613. @Override
  614. public void onVsync(long timestampNanos, int frame) {
  615. // Post the vsync event to the Handler.
  616. // The idea is to prevent incoming vsync events from completely starving
  617. // the message queue. If there are no messages in the queue with timestamps
  618. // earlier than the frame time, then the vsync event will be processed immediately.
  619. // Otherwise, messages that predate the vsync event will be handled first.
  620. long now = System.nanoTime();
  621. if (timestampNanos > now) {
  622. Log.w(TAG, "Frame time is " + ((timestampNanos - now) * 0.000001f)
  623. + " ms in the future! Check that graphics HAL is generating vsync "
  624. + "timestamps using the correct timebase.");
  625. timestampNanos = now;
  626. }
  627. if (mHavePendingVsync) {
  628. Log.w(TAG, "Already have a pending vsync event. There should only be "
  629. + "one at a time.");
  630. } else {
  631. mHavePendingVsync = true;
  632. }
  633. mTimestampNanos = timestampNanos;
  634. mFrame = frame;
  635. Message msg = Message.obtain(mHandler, this);
  636. msg.setAsynchronous(true);
  637. mHandler.sendMessageAtTime(msg, timestampNanos / NANOS_PER_MS);
  638. }
  639. @Override
  640. public void run() {
  641. mHavePendingVsync = false;
  642. doFrame(mTimestampNanos, mFrame);
  643. }
  644. }
  645. private static final class CallbackRecord {
  646. public CallbackRecord next;
  647. public long dueTime;
  648. public Object action; // Runnable or FrameCallback
  649. public Object token;
  650. public void run(long frameTimeNanos) {
  651. if (token == FRAME_CALLBACK_TOKEN) {
  652. ((FrameCallback)action).doFrame(frameTimeNanos);
  653. } else {
  654. ((Runnable)action).run();
  655. }
  656. }
  657. }
  658. private final class CallbackQueue {
  659. private CallbackRecord mHead;
  660. public boolean hasDueCallbacksLocked(long now) {
  661. return mHead != null && mHead.dueTime <= now;
  662. }
  663. public CallbackRecord extractDueCallbacksLocked(long now) {
  664. CallbackRecord callbacks = mHead;
  665. if (callbacks == null || callbacks.dueTime > now) {
  666. return null;
  667. }
  668. CallbackRecord last = callbacks;
  669. CallbackRecord next = last.next;
  670. while (next != null) {
  671. if (next.dueTime > now) {
  672. last.next = null;
  673. break;
  674. }
  675. last = next;
  676. next = next.next;
  677. }
  678. mHead = next;
  679. return callbacks;
  680. }
  681. public void addCallbackLocked(long dueTime, Object action, Object token) {
  682. CallbackRecord callback = obtainCallbackLocked(dueTime, action, token);
  683. CallbackRecord entry = mHead;
  684. if (entry == null) {
  685. mHead = callback;
  686. return;
  687. }
  688. if (dueTime < entry.dueTime) {
  689. callback.next = entry;
  690. mHead = callback;
  691. return;
  692. }
  693. while (entry.next != null) {
  694. if (dueTime < entry.next.dueTime) {
  695. callback.next = entry.next;
  696. break;
  697. }
  698. entry = entry.next;
  699. }
  700. entry.next = callback;
  701. }
  702. public void removeCallbacksLocked(Object action, Object token) {
  703. CallbackRecord predecessor = null;
  704. for (CallbackRecord callback = mHead; callback != null;) {
  705. final CallbackRecord next = callback.next;
  706. if ((action == null || callback.action == action)
  707. && (token == null || callback.token == token)) {
  708. if (predecessor != null) {
  709. predecessor.next = next;
  710. } else {
  711. mHead = next;
  712. }
  713. recycleCallbackLocked(callback);
  714. } else {
  715. predecessor = callback;
  716. }
  717. callback = next;
  718. }
  719. }
  720. }
  721. }