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 */ 16package android.view; 17 18import com.android.ide.common.rendering.api.LayoutLog; 19import com.android.layoutlib.bridge.Bridge; 20import com.android.tools.layoutlib.annotations.LayoutlibDelegate; 21import com.android.tools.layoutlib.java.System_Delegate; 22 23import java.lang.reflect.Field; 24import 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 */ 33public class Choreographer_Delegate { 34 static final AtomicReference<Choreographer> mInstance = new AtomicReference<Choreographer>(); 35 36 @LayoutlibDelegate 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 46 public static float getRefreshRate() { 47 return 60.f; 48 } 49 50 @LayoutlibDelegate 51 static void scheduleVsyncLocked(Choreographer thisChoreographer) { 52 // do nothing 53 } 54 55 public static void doFrame(long frameTimeNanos) { 56 Choreographer thisChoreographer = Choreographer.getInstance(); 57 58 thisChoreographer.mLastFrameTimeNanos = frameTimeNanos - thisChoreographer 59 .getFrameIntervalNanos(); 60 thisChoreographer.mFrameInfo.markInputHandlingStart(); 61 thisChoreographer.doCallbacks(Choreographer.CALLBACK_INPUT, frameTimeNanos); 62 63 thisChoreographer.mFrameInfo.markAnimationsStart(); 64 thisChoreographer.doCallbacks(Choreographer.CALLBACK_ANIMATION, frameTimeNanos); 65 66 thisChoreographer.mFrameInfo.markPerformTraversalsStart(); 67 thisChoreographer.doCallbacks(Choreographer.CALLBACK_TRAVERSAL, frameTimeNanos); 68 69 thisChoreographer.doCallbacks(Choreographer.CALLBACK_COMMIT, frameTimeNanos); 70 } 71 72 public static void dispose() { 73 try { 74 Field threadInstanceField = Choreographer.class.getDeclaredField("sThreadInstance"); 75 threadInstanceField.setAccessible(true); 76 @SuppressWarnings("unchecked") ThreadLocal<Choreographer> threadInstance = 77 (ThreadLocal<Choreographer>) threadInstanceField.get(null); 78 threadInstance.remove(); 79 } catch (ReflectiveOperationException e) { 80 assert false; 81 Bridge.getLog().error(LayoutLog.TAG_BROKEN, 82 "Unable to clear Choreographer memory.", e, null); 83 } 84 } 85} 86