1 /*
2  * Copyright (C) 2012 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 
18 import com.android.tools.layoutlib.annotations.LayoutlibDelegate;
19 
20 import android.animation.AnimationHandler;
21 import android.util.TimeUtils;
22 import android.view.animation.AnimationUtils;
23 
24 import java.util.concurrent.atomic.AtomicReference;
25 
26 /**
27  * Delegate used to provide new implementation of a select few methods of {@link Choreographer}
28  *
29  * Through the layoutlib_create tool, the original  methods of Choreographer have been
30  * replaced by calls to methods of the same name in this delegate class.
31  *
32  */
33 public class Choreographer_Delegate {
34     private static final AtomicReference<Choreographer> mInstance = new AtomicReference<Choreographer>();
35 
36     @LayoutlibDelegate
getInstance()37     public static Choreographer getInstance() {
38         if (mInstance.get() == null) {
39             mInstance.compareAndSet(null, Choreographer.getInstance_Original());
40         }
41 
42         return mInstance.get();
43     }
44 
45     @LayoutlibDelegate
getRefreshRate()46     public static float getRefreshRate() {
47         return 60.f;
48     }
49 
50     @LayoutlibDelegate
scheduleVsyncLocked(Choreographer thisChoreographer)51     static void scheduleVsyncLocked(Choreographer thisChoreographer) {
52         // do nothing
53     }
54 
doFrame(long frameTimeNanos)55     public static void doFrame(long frameTimeNanos) {
56         Choreographer thisChoreographer = Choreographer.getInstance();
57 
58         AnimationUtils.lockAnimationClock(frameTimeNanos / TimeUtils.NANOS_PER_MS);
59 
60         try {
61             thisChoreographer.mLastFrameTimeNanos = frameTimeNanos - thisChoreographer.getFrameIntervalNanos();
62             thisChoreographer.mFrameInfo.markInputHandlingStart();
63             thisChoreographer.doCallbacks(Choreographer.CALLBACK_INPUT, frameTimeNanos);
64 
65             thisChoreographer.mFrameInfo.markAnimationsStart();
66             thisChoreographer.doCallbacks(Choreographer.CALLBACK_ANIMATION, frameTimeNanos);
67 
68             thisChoreographer.mFrameInfo.markPerformTraversalsStart();
69             thisChoreographer.doCallbacks(Choreographer.CALLBACK_TRAVERSAL, frameTimeNanos);
70 
71             thisChoreographer.doCallbacks(Choreographer.CALLBACK_COMMIT, frameTimeNanos);
72         } finally {
73             AnimationUtils.unlockAnimationClock();
74         }
75     }
76 
clearFrames()77     public static void clearFrames() {
78         Choreographer thisChoreographer = Choreographer.getInstance();
79 
80         thisChoreographer.removeCallbacks(Choreographer.CALLBACK_INPUT, null, null);
81         thisChoreographer.removeCallbacks(Choreographer.CALLBACK_ANIMATION, null, null);
82         thisChoreographer.removeCallbacks(Choreographer.CALLBACK_TRAVERSAL, null, null);
83         thisChoreographer.removeCallbacks(Choreographer.CALLBACK_COMMIT, null, null);
84 
85         // Release animation handler instance since it holds references to the callbacks
86         AnimationHandler.sAnimatorHandler.set(null);
87     }
88 
dispose()89     public static void dispose() {
90         clearFrames();
91         Choreographer.releaseInstance();
92     }
93 }
94