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;
21
22import java.lang.reflect.Field;
23import java.util.concurrent.atomic.AtomicReference;
24
25/**
26 * Delegate used to provide new implementation of a select few methods of {@link Choreographer}
27 *
28 * Through the layoutlib_create tool, the original  methods of Choreographer have been
29 * replaced by calls to methods of the same name in this delegate class.
30 *
31 */
32public class Choreographer_Delegate {
33    static final AtomicReference<Choreographer> mInstance = new AtomicReference<Choreographer>();
34
35    @LayoutlibDelegate
36    public static Choreographer getInstance() {
37        if (mInstance.get() == null) {
38            mInstance.compareAndSet(null, Choreographer.getInstance_Original());
39        }
40
41        return mInstance.get();
42    }
43
44    @LayoutlibDelegate
45    public static float getRefreshRate() {
46        return 60.f;
47    }
48
49    @LayoutlibDelegate
50    static void scheduleVsyncLocked(Choreographer thisChoreographer) {
51        // do nothing
52    }
53
54    public static void doFrame(long frameTimeNanos) {
55        Choreographer thisChoreographer = Choreographer.getInstance();
56
57        thisChoreographer.mLastFrameTimeNanos = frameTimeNanos;
58
59        thisChoreographer.mFrameInfo.markInputHandlingStart();
60        thisChoreographer.doCallbacks(Choreographer.CALLBACK_INPUT, frameTimeNanos);
61
62        thisChoreographer.mFrameInfo.markAnimationsStart();
63        thisChoreographer.doCallbacks(Choreographer.CALLBACK_ANIMATION, frameTimeNanos);
64
65        thisChoreographer.mFrameInfo.markPerformTraversalsStart();
66        thisChoreographer.doCallbacks(Choreographer.CALLBACK_TRAVERSAL, frameTimeNanos);
67
68        thisChoreographer.doCallbacks(Choreographer.CALLBACK_COMMIT, frameTimeNanos);
69    }
70
71    public static void dispose() {
72        try {
73            Field threadInstanceField = Choreographer.class.getDeclaredField("sThreadInstance");
74            threadInstanceField.setAccessible(true);
75            @SuppressWarnings("unchecked") ThreadLocal<Choreographer> threadInstance =
76                    (ThreadLocal<Choreographer>) threadInstanceField.get(null);
77            threadInstance.remove();
78        } catch (ReflectiveOperationException e) {
79            assert false;
80            Bridge.getLog().error(LayoutLog.TAG_BROKEN,
81                    "Unable to clear Choreographer memory.", e, null);
82        }
83    }
84}
85