@@ -39,10 +39,6 @@ public final class Choreographer {
3939 private static final String TAG = "Choreographer" ;
4040 private static final boolean DEBUG = false ;
4141
42- // Amount of time in ms to wait before actually disposing of the display event
43- // receiver when it has not been needed for some time.
44- private static final long DISPOSE_RECEIVER_DELAY = 30 * 1000 ;
45-
4642 // The default amount of time in ms between animation frames.
4743 // When vsync is not enabled, we want to have some idea of how long we should
4844 // wait before posting the next animation message. It is important that the
@@ -85,12 +81,12 @@ protected Choreographer initialValue() {
8581 private static final int MSG_DO_ANIMATION = 0 ;
8682 private static final int MSG_DO_DRAW = 1 ;
8783 private static final int MSG_DO_SCHEDULE_VSYNC = 2 ;
88- private static final int MSG_DO_DISPOSE_RECEIVER = 3 ;
8984
9085 private final Object mLock = new Object ();
9186
9287 private final Looper mLooper ;
9388 private final FrameHandler mHandler ;
89+ private final FrameDisplayEventReceiver mDisplayEventReceiver ;
9490
9591 private Callback mCallbackPool ;
9692
@@ -99,14 +95,13 @@ protected Choreographer initialValue() {
9995
10096 private boolean mAnimationScheduled ;
10197 private boolean mDrawScheduled ;
102- private boolean mFrameDisplayEventReceiverNeeded ;
103- private FrameDisplayEventReceiver mFrameDisplayEventReceiver ;
10498 private long mLastAnimationTime ;
10599 private long mLastDrawTime ;
106100
107101 private Choreographer (Looper looper ) {
108102 mLooper = looper ;
109103 mHandler = new FrameHandler (looper );
104+ mDisplayEventReceiver = USE_VSYNC ? new FrameDisplayEventReceiver (looper ) : null ;
110105 mLastAnimationTime = Long .MIN_VALUE ;
111106 mLastDrawTime = Long .MIN_VALUE ;
112107 }
@@ -187,7 +182,6 @@ public void removeAnimationCallback(Runnable runnable) {
187182 }
188183 synchronized (mLock ) {
189184 mAnimationCallbacks = removeCallbackLocked (mAnimationCallbacks , runnable );
190- stopTimingLoopIfNoCallbacksLocked ();
191185 }
192186 }
193187
@@ -224,7 +218,6 @@ public void removeDrawCallback(Runnable runnable) {
224218 }
225219 synchronized (mLock ) {
226220 mDrawCallbacks = removeCallbackLocked (mDrawCallbacks , runnable );
227- stopTimingLoopIfNoCallbacksLocked ();
228221 }
229222 }
230223
@@ -239,12 +232,6 @@ private void scheduleAnimationLocked() {
239232 // If running on the Looper thread, then schedule the vsync immediately,
240233 // otherwise post a message to schedule the vsync from the UI thread
241234 // as soon as possible.
242- if (!mFrameDisplayEventReceiverNeeded ) {
243- mFrameDisplayEventReceiverNeeded = true ;
244- if (mFrameDisplayEventReceiver != null ) {
245- mHandler .removeMessages (MSG_DO_DISPOSE_RECEIVER );
246- }
247- }
248235 if (isRunningOnLooperThreadLocked ()) {
249236 doScheduleVsyncLocked ();
250237 } else {
@@ -355,57 +342,8 @@ void doScheduleVsync() {
355342 }
356343
357344 private void doScheduleVsyncLocked () {
358- if (mFrameDisplayEventReceiverNeeded && mAnimationScheduled ) {
359- if (mFrameDisplayEventReceiver == null ) {
360- mFrameDisplayEventReceiver = new FrameDisplayEventReceiver (mLooper );
361- }
362- mFrameDisplayEventReceiver .scheduleVsync ();
363- }
364- }
365-
366- void doDisposeReceiver () {
367- synchronized (mLock ) {
368- if (!mFrameDisplayEventReceiverNeeded && mFrameDisplayEventReceiver != null ) {
369- mFrameDisplayEventReceiver .dispose ();
370- mFrameDisplayEventReceiver = null ;
371- }
372- }
373- }
374-
375- private void stopTimingLoopIfNoCallbacksLocked () {
376- if (mAnimationCallbacks == null && mDrawCallbacks == null ) {
377- if (DEBUG ) {
378- Log .d (TAG , "Stopping timing loop." );
379- }
380-
381- if (mAnimationScheduled ) {
382- mAnimationScheduled = false ;
383- if (USE_VSYNC ) {
384- mHandler .removeMessages (MSG_DO_SCHEDULE_VSYNC );
385- } else {
386- mHandler .removeMessages (MSG_DO_ANIMATION );
387- }
388- }
389-
390- if (mDrawScheduled ) {
391- mDrawScheduled = false ;
392- if (!USE_ANIMATION_TIMER_FOR_DRAW ) {
393- mHandler .removeMessages (MSG_DO_DRAW );
394- }
395- }
396-
397- // Post a message to dispose the display event receiver if we haven't needed
398- // it again after a certain amount of time has elapsed. Another reason to
399- // defer disposal is that it is possible for use to attempt to dispose the
400- // receiver while handling a vsync event that it dispatched, which might
401- // cause a few problems...
402- if (mFrameDisplayEventReceiverNeeded ) {
403- mFrameDisplayEventReceiverNeeded = false ;
404- if (mFrameDisplayEventReceiver != null ) {
405- mHandler .sendEmptyMessageDelayed (MSG_DO_DISPOSE_RECEIVER ,
406- DISPOSE_RECEIVER_DELAY );
407- }
408- }
345+ if (mAnimationScheduled ) {
346+ mDisplayEventReceiver .scheduleVsync ();
409347 }
410348 }
411349
@@ -495,9 +433,6 @@ public void handleMessage(Message msg) {
495433 case MSG_DO_SCHEDULE_VSYNC :
496434 doScheduleVsync ();
497435 break ;
498- case MSG_DO_DISPOSE_RECEIVER :
499- doDisposeReceiver ();
500- break ;
501436 }
502437 }
503438 }
0 commit comments