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 private static final int MS_16 = 16000000; 37 38 @LayoutlibDelegate getInstance()39 public static Choreographer getInstance() { 40 if (mInstance.get() == null) { 41 mInstance.compareAndSet(null, Choreographer.getInstance_Original()); 42 } 43 44 return mInstance.get(); 45 } 46 47 @LayoutlibDelegate getRefreshRate()48 public static float getRefreshRate() { 49 return 60.f; 50 } 51 52 @LayoutlibDelegate scheduleVsyncLocked(Choreographer thisChoreographer)53 static void scheduleVsyncLocked(Choreographer thisChoreographer) { 54 // do nothing 55 } 56 doFrame(long frameTimeNanos)57 public static void doFrame(long frameTimeNanos) { 58 Choreographer thisChoreographer = Choreographer.getInstance(); 59 60 AnimationUtils.lockAnimationClock(frameTimeNanos / TimeUtils.NANOS_PER_MS); 61 62 try { 63 thisChoreographer.mLastFrameTimeNanos = frameTimeNanos - thisChoreographer.getFrameIntervalNanos(); 64 thisChoreographer.mFrameInfo.markInputHandlingStart(); 65 thisChoreographer.doCallbacks(Choreographer.CALLBACK_INPUT, frameTimeNanos, MS_16); 66 67 thisChoreographer.mFrameInfo.markAnimationsStart(); 68 thisChoreographer.doCallbacks(Choreographer.CALLBACK_ANIMATION, frameTimeNanos, MS_16); 69 70 thisChoreographer.mFrameInfo.markPerformTraversalsStart(); 71 thisChoreographer.doCallbacks(Choreographer.CALLBACK_TRAVERSAL, frameTimeNanos, MS_16); 72 73 thisChoreographer.doCallbacks(Choreographer.CALLBACK_COMMIT, frameTimeNanos, MS_16); 74 } finally { 75 AnimationUtils.unlockAnimationClock(); 76 } 77 } 78 clearFrames()79 public static void clearFrames() { 80 Choreographer thisChoreographer = Choreographer.getInstance(); 81 82 thisChoreographer.removeCallbacks(Choreographer.CALLBACK_INPUT, null, null); 83 thisChoreographer.removeCallbacks(Choreographer.CALLBACK_ANIMATION, null, null); 84 thisChoreographer.removeCallbacks(Choreographer.CALLBACK_TRAVERSAL, null, null); 85 thisChoreographer.removeCallbacks(Choreographer.CALLBACK_COMMIT, null, null); 86 87 // Release animation handler instance since it holds references to the callbacks 88 AnimationHandler.sAnimatorHandler.set(null); 89 } 90 dispose()91 public static void dispose() { 92 clearFrames(); 93 Choreographer.releaseInstance(); 94 } 95 } 96