xref: /aosp_15_r20/frameworks/native/libs/nativedisplay/AChoreographer.cpp (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker  * Copyright (C) 2015 The Android Open Source Project
3*38e8c45fSAndroid Build Coastguard Worker  *
4*38e8c45fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*38e8c45fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*38e8c45fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*38e8c45fSAndroid Build Coastguard Worker  *
8*38e8c45fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*38e8c45fSAndroid Build Coastguard Worker  *
10*38e8c45fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*38e8c45fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*38e8c45fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*38e8c45fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*38e8c45fSAndroid Build Coastguard Worker  * limitations under the License.
15*38e8c45fSAndroid Build Coastguard Worker  */
16*38e8c45fSAndroid Build Coastguard Worker 
17*38e8c45fSAndroid Build Coastguard Worker #include <android-base/thread_annotations.h>
18*38e8c45fSAndroid Build Coastguard Worker #include <android/gui/ISurfaceComposer.h>
19*38e8c45fSAndroid Build Coastguard Worker #include <gui/Choreographer.h>
20*38e8c45fSAndroid Build Coastguard Worker #include <jni.h>
21*38e8c45fSAndroid Build Coastguard Worker #include <private/android/choreographer.h>
22*38e8c45fSAndroid Build Coastguard Worker #include <utils/Looper.h>
23*38e8c45fSAndroid Build Coastguard Worker #include <utils/Timers.h>
24*38e8c45fSAndroid Build Coastguard Worker 
25*38e8c45fSAndroid Build Coastguard Worker #include <cinttypes>
26*38e8c45fSAndroid Build Coastguard Worker #include <mutex>
27*38e8c45fSAndroid Build Coastguard Worker #include <optional>
28*38e8c45fSAndroid Build Coastguard Worker #include <queue>
29*38e8c45fSAndroid Build Coastguard Worker #include <thread>
30*38e8c45fSAndroid Build Coastguard Worker 
31*38e8c45fSAndroid Build Coastguard Worker #undef LOG_TAG
32*38e8c45fSAndroid Build Coastguard Worker #define LOG_TAG "AChoreographer"
33*38e8c45fSAndroid Build Coastguard Worker 
34*38e8c45fSAndroid Build Coastguard Worker using namespace android;
35*38e8c45fSAndroid Build Coastguard Worker 
AChoreographer_to_Choreographer(AChoreographer * choreographer)36*38e8c45fSAndroid Build Coastguard Worker static inline Choreographer* AChoreographer_to_Choreographer(AChoreographer* choreographer) {
37*38e8c45fSAndroid Build Coastguard Worker     return reinterpret_cast<Choreographer*>(choreographer);
38*38e8c45fSAndroid Build Coastguard Worker }
39*38e8c45fSAndroid Build Coastguard Worker 
AChoreographer_to_Choreographer(const AChoreographer * choreographer)40*38e8c45fSAndroid Build Coastguard Worker static inline const Choreographer* AChoreographer_to_Choreographer(
41*38e8c45fSAndroid Build Coastguard Worker         const AChoreographer* choreographer) {
42*38e8c45fSAndroid Build Coastguard Worker     return reinterpret_cast<const Choreographer*>(choreographer);
43*38e8c45fSAndroid Build Coastguard Worker }
44*38e8c45fSAndroid Build Coastguard Worker 
45*38e8c45fSAndroid Build Coastguard Worker static inline const ChoreographerFrameCallbackDataImpl*
AChoreographerFrameCallbackData_to_ChoreographerFrameCallbackDataImpl(const AChoreographerFrameCallbackData * data)46*38e8c45fSAndroid Build Coastguard Worker AChoreographerFrameCallbackData_to_ChoreographerFrameCallbackDataImpl(
47*38e8c45fSAndroid Build Coastguard Worker         const AChoreographerFrameCallbackData* data) {
48*38e8c45fSAndroid Build Coastguard Worker     return reinterpret_cast<const ChoreographerFrameCallbackDataImpl*>(data);
49*38e8c45fSAndroid Build Coastguard Worker }
50*38e8c45fSAndroid Build Coastguard Worker 
51*38e8c45fSAndroid Build Coastguard Worker // Glue for private C api
52*38e8c45fSAndroid Build Coastguard Worker namespace android {
AChoreographer_signalRefreshRateCallbacks(nsecs_t vsyncPeriod)53*38e8c45fSAndroid Build Coastguard Worker void AChoreographer_signalRefreshRateCallbacks(nsecs_t vsyncPeriod) {
54*38e8c45fSAndroid Build Coastguard Worker     Choreographer::signalRefreshRateCallbacks(vsyncPeriod);
55*38e8c45fSAndroid Build Coastguard Worker }
56*38e8c45fSAndroid Build Coastguard Worker 
AChoreographer_initJVM(JNIEnv * env)57*38e8c45fSAndroid Build Coastguard Worker void AChoreographer_initJVM(JNIEnv* env) {
58*38e8c45fSAndroid Build Coastguard Worker     Choreographer::initJVM(env);
59*38e8c45fSAndroid Build Coastguard Worker }
60*38e8c45fSAndroid Build Coastguard Worker 
AChoreographer_routeGetInstance()61*38e8c45fSAndroid Build Coastguard Worker AChoreographer* AChoreographer_routeGetInstance() {
62*38e8c45fSAndroid Build Coastguard Worker     return AChoreographer_getInstance();
63*38e8c45fSAndroid Build Coastguard Worker }
AChoreographer_routePostFrameCallback(AChoreographer * choreographer,AChoreographer_frameCallback callback,void * data)64*38e8c45fSAndroid Build Coastguard Worker void AChoreographer_routePostFrameCallback(AChoreographer* choreographer,
65*38e8c45fSAndroid Build Coastguard Worker                                            AChoreographer_frameCallback callback, void* data) {
66*38e8c45fSAndroid Build Coastguard Worker #pragma clang diagnostic push
67*38e8c45fSAndroid Build Coastguard Worker #pragma clang diagnostic ignored "-Wdeprecated-declarations"
68*38e8c45fSAndroid Build Coastguard Worker     return AChoreographer_postFrameCallback(choreographer, callback, data);
69*38e8c45fSAndroid Build Coastguard Worker #pragma clang diagnostic pop
70*38e8c45fSAndroid Build Coastguard Worker }
AChoreographer_routePostFrameCallbackDelayed(AChoreographer * choreographer,AChoreographer_frameCallback callback,void * data,long delayMillis)71*38e8c45fSAndroid Build Coastguard Worker void AChoreographer_routePostFrameCallbackDelayed(AChoreographer* choreographer,
72*38e8c45fSAndroid Build Coastguard Worker                                                   AChoreographer_frameCallback callback, void* data,
73*38e8c45fSAndroid Build Coastguard Worker                                                   long delayMillis) {
74*38e8c45fSAndroid Build Coastguard Worker #pragma clang diagnostic push
75*38e8c45fSAndroid Build Coastguard Worker #pragma clang diagnostic ignored "-Wdeprecated-declarations"
76*38e8c45fSAndroid Build Coastguard Worker     return AChoreographer_postFrameCallbackDelayed(choreographer, callback, data, delayMillis);
77*38e8c45fSAndroid Build Coastguard Worker #pragma clang diagnostic pop
78*38e8c45fSAndroid Build Coastguard Worker }
AChoreographer_routePostFrameCallback64(AChoreographer * choreographer,AChoreographer_frameCallback64 callback,void * data)79*38e8c45fSAndroid Build Coastguard Worker void AChoreographer_routePostFrameCallback64(AChoreographer* choreographer,
80*38e8c45fSAndroid Build Coastguard Worker                                              AChoreographer_frameCallback64 callback, void* data) {
81*38e8c45fSAndroid Build Coastguard Worker     return AChoreographer_postFrameCallback64(choreographer, callback, data);
82*38e8c45fSAndroid Build Coastguard Worker }
AChoreographer_routePostFrameCallbackDelayed64(AChoreographer * choreographer,AChoreographer_frameCallback64 callback,void * data,uint32_t delayMillis)83*38e8c45fSAndroid Build Coastguard Worker void AChoreographer_routePostFrameCallbackDelayed64(AChoreographer* choreographer,
84*38e8c45fSAndroid Build Coastguard Worker                                                     AChoreographer_frameCallback64 callback,
85*38e8c45fSAndroid Build Coastguard Worker                                                     void* data, uint32_t delayMillis) {
86*38e8c45fSAndroid Build Coastguard Worker     return AChoreographer_postFrameCallbackDelayed64(choreographer, callback, data, delayMillis);
87*38e8c45fSAndroid Build Coastguard Worker }
AChoreographer_routePostVsyncCallback(AChoreographer * choreographer,AChoreographer_vsyncCallback callback,void * data)88*38e8c45fSAndroid Build Coastguard Worker void AChoreographer_routePostVsyncCallback(AChoreographer* choreographer,
89*38e8c45fSAndroid Build Coastguard Worker                                            AChoreographer_vsyncCallback callback, void* data) {
90*38e8c45fSAndroid Build Coastguard Worker     return AChoreographer_postVsyncCallback(choreographer, callback, data);
91*38e8c45fSAndroid Build Coastguard Worker }
AChoreographer_routeRegisterRefreshRateCallback(AChoreographer * choreographer,AChoreographer_refreshRateCallback callback,void * data)92*38e8c45fSAndroid Build Coastguard Worker void AChoreographer_routeRegisterRefreshRateCallback(AChoreographer* choreographer,
93*38e8c45fSAndroid Build Coastguard Worker                                                      AChoreographer_refreshRateCallback callback,
94*38e8c45fSAndroid Build Coastguard Worker                                                      void* data) {
95*38e8c45fSAndroid Build Coastguard Worker     return AChoreographer_registerRefreshRateCallback(choreographer, callback, data);
96*38e8c45fSAndroid Build Coastguard Worker }
AChoreographer_routeUnregisterRefreshRateCallback(AChoreographer * choreographer,AChoreographer_refreshRateCallback callback,void * data)97*38e8c45fSAndroid Build Coastguard Worker void AChoreographer_routeUnregisterRefreshRateCallback(AChoreographer* choreographer,
98*38e8c45fSAndroid Build Coastguard Worker                                                        AChoreographer_refreshRateCallback callback,
99*38e8c45fSAndroid Build Coastguard Worker                                                        void* data) {
100*38e8c45fSAndroid Build Coastguard Worker     return AChoreographer_unregisterRefreshRateCallback(choreographer, callback, data);
101*38e8c45fSAndroid Build Coastguard Worker }
AChoreographerFrameCallbackData_routeGetFrameTimeNanos(const AChoreographerFrameCallbackData * data)102*38e8c45fSAndroid Build Coastguard Worker int64_t AChoreographerFrameCallbackData_routeGetFrameTimeNanos(
103*38e8c45fSAndroid Build Coastguard Worker         const AChoreographerFrameCallbackData* data) {
104*38e8c45fSAndroid Build Coastguard Worker     return AChoreographerFrameCallbackData_getFrameTimeNanos(data);
105*38e8c45fSAndroid Build Coastguard Worker }
AChoreographerFrameCallbackData_routeGetFrameTimelinesLength(const AChoreographerFrameCallbackData * data)106*38e8c45fSAndroid Build Coastguard Worker size_t AChoreographerFrameCallbackData_routeGetFrameTimelinesLength(
107*38e8c45fSAndroid Build Coastguard Worker         const AChoreographerFrameCallbackData* data) {
108*38e8c45fSAndroid Build Coastguard Worker     return AChoreographerFrameCallbackData_getFrameTimelinesLength(data);
109*38e8c45fSAndroid Build Coastguard Worker }
AChoreographerFrameCallbackData_routeGetPreferredFrameTimelineIndex(const AChoreographerFrameCallbackData * data)110*38e8c45fSAndroid Build Coastguard Worker size_t AChoreographerFrameCallbackData_routeGetPreferredFrameTimelineIndex(
111*38e8c45fSAndroid Build Coastguard Worker         const AChoreographerFrameCallbackData* data) {
112*38e8c45fSAndroid Build Coastguard Worker     return AChoreographerFrameCallbackData_getPreferredFrameTimelineIndex(data);
113*38e8c45fSAndroid Build Coastguard Worker }
AChoreographerFrameCallbackData_routeGetFrameTimelineVsyncId(const AChoreographerFrameCallbackData * data,size_t index)114*38e8c45fSAndroid Build Coastguard Worker AVsyncId AChoreographerFrameCallbackData_routeGetFrameTimelineVsyncId(
115*38e8c45fSAndroid Build Coastguard Worker         const AChoreographerFrameCallbackData* data, size_t index) {
116*38e8c45fSAndroid Build Coastguard Worker     return AChoreographerFrameCallbackData_getFrameTimelineVsyncId(data, index);
117*38e8c45fSAndroid Build Coastguard Worker }
AChoreographerFrameCallbackData_routeGetFrameTimelineExpectedPresentationTimeNanos(const AChoreographerFrameCallbackData * data,size_t index)118*38e8c45fSAndroid Build Coastguard Worker int64_t AChoreographerFrameCallbackData_routeGetFrameTimelineExpectedPresentationTimeNanos(
119*38e8c45fSAndroid Build Coastguard Worker         const AChoreographerFrameCallbackData* data, size_t index) {
120*38e8c45fSAndroid Build Coastguard Worker     return AChoreographerFrameCallbackData_getFrameTimelineExpectedPresentationTimeNanos(data,
121*38e8c45fSAndroid Build Coastguard Worker                                                                                          index);
122*38e8c45fSAndroid Build Coastguard Worker }
AChoreographerFrameCallbackData_routeGetFrameTimelineDeadlineNanos(const AChoreographerFrameCallbackData * data,size_t index)123*38e8c45fSAndroid Build Coastguard Worker int64_t AChoreographerFrameCallbackData_routeGetFrameTimelineDeadlineNanos(
124*38e8c45fSAndroid Build Coastguard Worker         const AChoreographerFrameCallbackData* data, size_t index) {
125*38e8c45fSAndroid Build Coastguard Worker     return AChoreographerFrameCallbackData_getFrameTimelineDeadlineNanos(data, index);
126*38e8c45fSAndroid Build Coastguard Worker }
127*38e8c45fSAndroid Build Coastguard Worker 
AChoreographer_getFrameInterval(const AChoreographer * choreographer)128*38e8c45fSAndroid Build Coastguard Worker int64_t AChoreographer_getFrameInterval(const AChoreographer* choreographer) {
129*38e8c45fSAndroid Build Coastguard Worker     return AChoreographer_to_Choreographer(choreographer)->getFrameInterval();
130*38e8c45fSAndroid Build Coastguard Worker }
131*38e8c45fSAndroid Build Coastguard Worker 
AChoreographer_getStartTimeNanosForVsyncId(AVsyncId vsyncId)132*38e8c45fSAndroid Build Coastguard Worker int64_t AChoreographer_getStartTimeNanosForVsyncId(AVsyncId vsyncId) {
133*38e8c45fSAndroid Build Coastguard Worker     return Choreographer::getStartTimeNanosForVsyncId(vsyncId);
134*38e8c45fSAndroid Build Coastguard Worker }
135*38e8c45fSAndroid Build Coastguard Worker 
136*38e8c45fSAndroid Build Coastguard Worker } // namespace android
137*38e8c45fSAndroid Build Coastguard Worker 
138*38e8c45fSAndroid Build Coastguard Worker /* Glue for the NDK interface */
139*38e8c45fSAndroid Build Coastguard Worker 
Choreographer_to_AChoreographer(Choreographer * choreographer)140*38e8c45fSAndroid Build Coastguard Worker static inline AChoreographer* Choreographer_to_AChoreographer(Choreographer* choreographer) {
141*38e8c45fSAndroid Build Coastguard Worker     return reinterpret_cast<AChoreographer*>(choreographer);
142*38e8c45fSAndroid Build Coastguard Worker }
143*38e8c45fSAndroid Build Coastguard Worker 
AChoreographer_getInstance()144*38e8c45fSAndroid Build Coastguard Worker AChoreographer* AChoreographer_getInstance() {
145*38e8c45fSAndroid Build Coastguard Worker     return Choreographer_to_AChoreographer(Choreographer::getForThread());
146*38e8c45fSAndroid Build Coastguard Worker }
147*38e8c45fSAndroid Build Coastguard Worker 
AChoreographer_postFrameCallback(AChoreographer * choreographer,AChoreographer_frameCallback callback,void * data)148*38e8c45fSAndroid Build Coastguard Worker void AChoreographer_postFrameCallback(AChoreographer* choreographer,
149*38e8c45fSAndroid Build Coastguard Worker                                       AChoreographer_frameCallback callback, void* data) {
150*38e8c45fSAndroid Build Coastguard Worker     AChoreographer_to_Choreographer(choreographer)
151*38e8c45fSAndroid Build Coastguard Worker             ->postFrameCallbackDelayed(callback, nullptr, nullptr, data, 0, CALLBACK_ANIMATION);
152*38e8c45fSAndroid Build Coastguard Worker }
AChoreographer_postFrameCallbackDelayed(AChoreographer * choreographer,AChoreographer_frameCallback callback,void * data,long delayMillis)153*38e8c45fSAndroid Build Coastguard Worker void AChoreographer_postFrameCallbackDelayed(AChoreographer* choreographer,
154*38e8c45fSAndroid Build Coastguard Worker                                              AChoreographer_frameCallback callback, void* data,
155*38e8c45fSAndroid Build Coastguard Worker                                              long delayMillis) {
156*38e8c45fSAndroid Build Coastguard Worker     AChoreographer_to_Choreographer(choreographer)
157*38e8c45fSAndroid Build Coastguard Worker             ->postFrameCallbackDelayed(callback, nullptr, nullptr, data, ms2ns(delayMillis),
158*38e8c45fSAndroid Build Coastguard Worker                                        CALLBACK_ANIMATION);
159*38e8c45fSAndroid Build Coastguard Worker }
AChoreographer_postVsyncCallback(AChoreographer * choreographer,AChoreographer_vsyncCallback callback,void * data)160*38e8c45fSAndroid Build Coastguard Worker void AChoreographer_postVsyncCallback(AChoreographer* choreographer,
161*38e8c45fSAndroid Build Coastguard Worker                                       AChoreographer_vsyncCallback callback, void* data) {
162*38e8c45fSAndroid Build Coastguard Worker     AChoreographer_to_Choreographer(choreographer)
163*38e8c45fSAndroid Build Coastguard Worker             ->postFrameCallbackDelayed(nullptr, nullptr, callback, data, 0, CALLBACK_ANIMATION);
164*38e8c45fSAndroid Build Coastguard Worker }
AChoreographer_postFrameCallback64(AChoreographer * choreographer,AChoreographer_frameCallback64 callback,void * data)165*38e8c45fSAndroid Build Coastguard Worker void AChoreographer_postFrameCallback64(AChoreographer* choreographer,
166*38e8c45fSAndroid Build Coastguard Worker                                         AChoreographer_frameCallback64 callback, void* data) {
167*38e8c45fSAndroid Build Coastguard Worker     AChoreographer_to_Choreographer(choreographer)
168*38e8c45fSAndroid Build Coastguard Worker             ->postFrameCallbackDelayed(nullptr, callback, nullptr, data, 0, CALLBACK_ANIMATION);
169*38e8c45fSAndroid Build Coastguard Worker }
AChoreographer_postFrameCallbackDelayed64(AChoreographer * choreographer,AChoreographer_frameCallback64 callback,void * data,uint32_t delayMillis)170*38e8c45fSAndroid Build Coastguard Worker void AChoreographer_postFrameCallbackDelayed64(AChoreographer* choreographer,
171*38e8c45fSAndroid Build Coastguard Worker                                                AChoreographer_frameCallback64 callback, void* data,
172*38e8c45fSAndroid Build Coastguard Worker                                                uint32_t delayMillis) {
173*38e8c45fSAndroid Build Coastguard Worker     AChoreographer_to_Choreographer(choreographer)
174*38e8c45fSAndroid Build Coastguard Worker             ->postFrameCallbackDelayed(nullptr, callback, nullptr, data, ms2ns(delayMillis),
175*38e8c45fSAndroid Build Coastguard Worker                                        CALLBACK_ANIMATION);
176*38e8c45fSAndroid Build Coastguard Worker }
AChoreographer_registerRefreshRateCallback(AChoreographer * choreographer,AChoreographer_refreshRateCallback callback,void * data)177*38e8c45fSAndroid Build Coastguard Worker void AChoreographer_registerRefreshRateCallback(AChoreographer* choreographer,
178*38e8c45fSAndroid Build Coastguard Worker                                                 AChoreographer_refreshRateCallback callback,
179*38e8c45fSAndroid Build Coastguard Worker                                                 void* data) {
180*38e8c45fSAndroid Build Coastguard Worker     AChoreographer_to_Choreographer(choreographer)->registerRefreshRateCallback(callback, data);
181*38e8c45fSAndroid Build Coastguard Worker }
AChoreographer_unregisterRefreshRateCallback(AChoreographer * choreographer,AChoreographer_refreshRateCallback callback,void * data)182*38e8c45fSAndroid Build Coastguard Worker void AChoreographer_unregisterRefreshRateCallback(AChoreographer* choreographer,
183*38e8c45fSAndroid Build Coastguard Worker                                                   AChoreographer_refreshRateCallback callback,
184*38e8c45fSAndroid Build Coastguard Worker                                                   void* data) {
185*38e8c45fSAndroid Build Coastguard Worker     AChoreographer_to_Choreographer(choreographer)->unregisterRefreshRateCallback(callback, data);
186*38e8c45fSAndroid Build Coastguard Worker }
187*38e8c45fSAndroid Build Coastguard Worker 
AChoreographerFrameCallbackData_getFrameTimeNanos(const AChoreographerFrameCallbackData * data)188*38e8c45fSAndroid Build Coastguard Worker int64_t AChoreographerFrameCallbackData_getFrameTimeNanos(
189*38e8c45fSAndroid Build Coastguard Worker         const AChoreographerFrameCallbackData* data) {
190*38e8c45fSAndroid Build Coastguard Worker     const ChoreographerFrameCallbackDataImpl* frameCallbackData =
191*38e8c45fSAndroid Build Coastguard Worker             AChoreographerFrameCallbackData_to_ChoreographerFrameCallbackDataImpl(data);
192*38e8c45fSAndroid Build Coastguard Worker     LOG_ALWAYS_FATAL_IF(!frameCallbackData->choreographer->inCallback(),
193*38e8c45fSAndroid Build Coastguard Worker                         "Data is only valid in callback");
194*38e8c45fSAndroid Build Coastguard Worker     return frameCallbackData->frameTimeNanos;
195*38e8c45fSAndroid Build Coastguard Worker }
AChoreographerFrameCallbackData_getFrameTimelinesLength(const AChoreographerFrameCallbackData * data)196*38e8c45fSAndroid Build Coastguard Worker size_t AChoreographerFrameCallbackData_getFrameTimelinesLength(
197*38e8c45fSAndroid Build Coastguard Worker         const AChoreographerFrameCallbackData* data) {
198*38e8c45fSAndroid Build Coastguard Worker     const ChoreographerFrameCallbackDataImpl* frameCallbackData =
199*38e8c45fSAndroid Build Coastguard Worker             AChoreographerFrameCallbackData_to_ChoreographerFrameCallbackDataImpl(data);
200*38e8c45fSAndroid Build Coastguard Worker     LOG_ALWAYS_FATAL_IF(!frameCallbackData->choreographer->inCallback(),
201*38e8c45fSAndroid Build Coastguard Worker                         "Data is only valid in callback");
202*38e8c45fSAndroid Build Coastguard Worker     return frameCallbackData->vsyncEventData.frameTimelinesLength;
203*38e8c45fSAndroid Build Coastguard Worker }
AChoreographerFrameCallbackData_getPreferredFrameTimelineIndex(const AChoreographerFrameCallbackData * data)204*38e8c45fSAndroid Build Coastguard Worker size_t AChoreographerFrameCallbackData_getPreferredFrameTimelineIndex(
205*38e8c45fSAndroid Build Coastguard Worker         const AChoreographerFrameCallbackData* data) {
206*38e8c45fSAndroid Build Coastguard Worker     const ChoreographerFrameCallbackDataImpl* frameCallbackData =
207*38e8c45fSAndroid Build Coastguard Worker             AChoreographerFrameCallbackData_to_ChoreographerFrameCallbackDataImpl(data);
208*38e8c45fSAndroid Build Coastguard Worker     LOG_ALWAYS_FATAL_IF(!frameCallbackData->choreographer->inCallback(),
209*38e8c45fSAndroid Build Coastguard Worker                         "Data is only valid in callback");
210*38e8c45fSAndroid Build Coastguard Worker     return frameCallbackData->vsyncEventData.preferredFrameTimelineIndex;
211*38e8c45fSAndroid Build Coastguard Worker }
AChoreographerFrameCallbackData_getFrameTimelineVsyncId(const AChoreographerFrameCallbackData * data,size_t index)212*38e8c45fSAndroid Build Coastguard Worker AVsyncId AChoreographerFrameCallbackData_getFrameTimelineVsyncId(
213*38e8c45fSAndroid Build Coastguard Worker         const AChoreographerFrameCallbackData* data, size_t index) {
214*38e8c45fSAndroid Build Coastguard Worker     const ChoreographerFrameCallbackDataImpl* frameCallbackData =
215*38e8c45fSAndroid Build Coastguard Worker             AChoreographerFrameCallbackData_to_ChoreographerFrameCallbackDataImpl(data);
216*38e8c45fSAndroid Build Coastguard Worker     LOG_ALWAYS_FATAL_IF(!frameCallbackData->choreographer->inCallback(),
217*38e8c45fSAndroid Build Coastguard Worker                         "Data is only valid in callback");
218*38e8c45fSAndroid Build Coastguard Worker     LOG_ALWAYS_FATAL_IF(index >= VsyncEventData::kFrameTimelinesCapacity, "Index out of bounds");
219*38e8c45fSAndroid Build Coastguard Worker     return frameCallbackData->vsyncEventData.frameTimelines[index].vsyncId;
220*38e8c45fSAndroid Build Coastguard Worker }
AChoreographerFrameCallbackData_getFrameTimelineExpectedPresentationTimeNanos(const AChoreographerFrameCallbackData * data,size_t index)221*38e8c45fSAndroid Build Coastguard Worker int64_t AChoreographerFrameCallbackData_getFrameTimelineExpectedPresentationTimeNanos(
222*38e8c45fSAndroid Build Coastguard Worker         const AChoreographerFrameCallbackData* data, size_t index) {
223*38e8c45fSAndroid Build Coastguard Worker     const ChoreographerFrameCallbackDataImpl* frameCallbackData =
224*38e8c45fSAndroid Build Coastguard Worker             AChoreographerFrameCallbackData_to_ChoreographerFrameCallbackDataImpl(data);
225*38e8c45fSAndroid Build Coastguard Worker     LOG_ALWAYS_FATAL_IF(!frameCallbackData->choreographer->inCallback(),
226*38e8c45fSAndroid Build Coastguard Worker                         "Data is only valid in callback");
227*38e8c45fSAndroid Build Coastguard Worker     LOG_ALWAYS_FATAL_IF(index >= VsyncEventData::kFrameTimelinesCapacity, "Index out of bounds");
228*38e8c45fSAndroid Build Coastguard Worker     return frameCallbackData->vsyncEventData.frameTimelines[index].expectedPresentationTime;
229*38e8c45fSAndroid Build Coastguard Worker }
AChoreographerFrameCallbackData_getFrameTimelineDeadlineNanos(const AChoreographerFrameCallbackData * data,size_t index)230*38e8c45fSAndroid Build Coastguard Worker int64_t AChoreographerFrameCallbackData_getFrameTimelineDeadlineNanos(
231*38e8c45fSAndroid Build Coastguard Worker         const AChoreographerFrameCallbackData* data, size_t index) {
232*38e8c45fSAndroid Build Coastguard Worker     const ChoreographerFrameCallbackDataImpl* frameCallbackData =
233*38e8c45fSAndroid Build Coastguard Worker             AChoreographerFrameCallbackData_to_ChoreographerFrameCallbackDataImpl(data);
234*38e8c45fSAndroid Build Coastguard Worker     LOG_ALWAYS_FATAL_IF(!frameCallbackData->choreographer->inCallback(),
235*38e8c45fSAndroid Build Coastguard Worker                         "Data is only valid in callback");
236*38e8c45fSAndroid Build Coastguard Worker     LOG_ALWAYS_FATAL_IF(index >= VsyncEventData::kFrameTimelinesCapacity, "Index out of bounds");
237*38e8c45fSAndroid Build Coastguard Worker     return frameCallbackData->vsyncEventData.frameTimelines[index].deadlineTimestamp;
238*38e8c45fSAndroid Build Coastguard Worker }
239*38e8c45fSAndroid Build Coastguard Worker 
AChoreographer_create()240*38e8c45fSAndroid Build Coastguard Worker AChoreographer* AChoreographer_create() {
241*38e8c45fSAndroid Build Coastguard Worker     Choreographer* choreographer = new Choreographer(nullptr);
242*38e8c45fSAndroid Build Coastguard Worker     status_t result = choreographer->initialize();
243*38e8c45fSAndroid Build Coastguard Worker     if (result != OK) {
244*38e8c45fSAndroid Build Coastguard Worker         ALOGW("Failed to initialize");
245*38e8c45fSAndroid Build Coastguard Worker         return nullptr;
246*38e8c45fSAndroid Build Coastguard Worker     }
247*38e8c45fSAndroid Build Coastguard Worker     return Choreographer_to_AChoreographer(choreographer);
248*38e8c45fSAndroid Build Coastguard Worker }
249*38e8c45fSAndroid Build Coastguard Worker 
AChoreographer_destroy(AChoreographer * choreographer)250*38e8c45fSAndroid Build Coastguard Worker void AChoreographer_destroy(AChoreographer* choreographer) {
251*38e8c45fSAndroid Build Coastguard Worker     if (choreographer == nullptr) {
252*38e8c45fSAndroid Build Coastguard Worker         return;
253*38e8c45fSAndroid Build Coastguard Worker     }
254*38e8c45fSAndroid Build Coastguard Worker 
255*38e8c45fSAndroid Build Coastguard Worker     delete AChoreographer_to_Choreographer(choreographer);
256*38e8c45fSAndroid Build Coastguard Worker }
257*38e8c45fSAndroid Build Coastguard Worker 
AChoreographer_getFd(const AChoreographer * choreographer)258*38e8c45fSAndroid Build Coastguard Worker int AChoreographer_getFd(const AChoreographer* choreographer) {
259*38e8c45fSAndroid Build Coastguard Worker     return AChoreographer_to_Choreographer(choreographer)->getFd();
260*38e8c45fSAndroid Build Coastguard Worker }
261*38e8c45fSAndroid Build Coastguard Worker 
AChoreographer_handlePendingEvents(AChoreographer * choreographer,void * data)262*38e8c45fSAndroid Build Coastguard Worker void AChoreographer_handlePendingEvents(AChoreographer* choreographer, void* data) {
263*38e8c45fSAndroid Build Coastguard Worker     // Pass dummy fd and events args to handleEvent, since the underlying
264*38e8c45fSAndroid Build Coastguard Worker     // DisplayEventDispatcher doesn't need them outside of validating that a
265*38e8c45fSAndroid Build Coastguard Worker     // Looper instance didn't break, but these args circumvent those checks.
266*38e8c45fSAndroid Build Coastguard Worker     Choreographer* impl = AChoreographer_to_Choreographer(choreographer);
267*38e8c45fSAndroid Build Coastguard Worker     impl->handleEvent(-1, Looper::EVENT_INPUT, data);
268*38e8c45fSAndroid Build Coastguard Worker }
269