xref: /aosp_15_r20/frameworks/native/libs/gui/Choreographer.cpp (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker  * Copyright 2022 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 // #define LOG_NDEBUG 0
18*38e8c45fSAndroid Build Coastguard Worker #define ATRACE_TAG ATRACE_TAG_GRAPHICS
19*38e8c45fSAndroid Build Coastguard Worker 
20*38e8c45fSAndroid Build Coastguard Worker #include <gui/Choreographer.h>
21*38e8c45fSAndroid Build Coastguard Worker #include <gui/TraceUtils.h>
22*38e8c45fSAndroid Build Coastguard Worker #include <jni.h>
23*38e8c45fSAndroid Build Coastguard Worker 
24*38e8c45fSAndroid Build Coastguard Worker #undef LOG_TAG
25*38e8c45fSAndroid Build Coastguard Worker #define LOG_TAG "AChoreographer"
26*38e8c45fSAndroid Build Coastguard Worker 
27*38e8c45fSAndroid Build Coastguard Worker namespace {
28*38e8c45fSAndroid Build Coastguard Worker struct {
29*38e8c45fSAndroid Build Coastguard Worker     // Global JVM that is provided by zygote
30*38e8c45fSAndroid Build Coastguard Worker     JavaVM* jvm = nullptr;
31*38e8c45fSAndroid Build Coastguard Worker     struct {
32*38e8c45fSAndroid Build Coastguard Worker         jclass clazz;
33*38e8c45fSAndroid Build Coastguard Worker         jmethodID getInstance;
34*38e8c45fSAndroid Build Coastguard Worker         jmethodID registerNativeChoreographerForRefreshRateCallbacks;
35*38e8c45fSAndroid Build Coastguard Worker         jmethodID unregisterNativeChoreographerForRefreshRateCallbacks;
36*38e8c45fSAndroid Build Coastguard Worker     } displayManagerGlobal;
37*38e8c45fSAndroid Build Coastguard Worker } gJni;
38*38e8c45fSAndroid Build Coastguard Worker 
39*38e8c45fSAndroid Build Coastguard Worker // Gets the JNIEnv* for this thread, and performs one-off initialization if we
40*38e8c45fSAndroid Build Coastguard Worker // have never retrieved a JNIEnv* pointer before.
getJniEnv()41*38e8c45fSAndroid Build Coastguard Worker JNIEnv* getJniEnv() {
42*38e8c45fSAndroid Build Coastguard Worker     if (gJni.jvm == nullptr) {
43*38e8c45fSAndroid Build Coastguard Worker         ALOGW("AChoreographer: No JVM provided!");
44*38e8c45fSAndroid Build Coastguard Worker         return nullptr;
45*38e8c45fSAndroid Build Coastguard Worker     }
46*38e8c45fSAndroid Build Coastguard Worker 
47*38e8c45fSAndroid Build Coastguard Worker     JNIEnv* env = nullptr;
48*38e8c45fSAndroid Build Coastguard Worker     if (gJni.jvm->GetEnv((void**)&env, JNI_VERSION_1_4) != JNI_OK) {
49*38e8c45fSAndroid Build Coastguard Worker         ALOGD("Attaching thread to JVM for AChoreographer");
50*38e8c45fSAndroid Build Coastguard Worker         JavaVMAttachArgs args = {JNI_VERSION_1_4, "AChoreographer_env", NULL};
51*38e8c45fSAndroid Build Coastguard Worker         jint attachResult = gJni.jvm->AttachCurrentThreadAsDaemon(&env, (void*)&args);
52*38e8c45fSAndroid Build Coastguard Worker         if (attachResult != JNI_OK) {
53*38e8c45fSAndroid Build Coastguard Worker             ALOGE("Unable to attach thread. Error: %d", attachResult);
54*38e8c45fSAndroid Build Coastguard Worker             return nullptr;
55*38e8c45fSAndroid Build Coastguard Worker         }
56*38e8c45fSAndroid Build Coastguard Worker     }
57*38e8c45fSAndroid Build Coastguard Worker     if (env == nullptr) {
58*38e8c45fSAndroid Build Coastguard Worker         ALOGW("AChoreographer: No JNI env available!");
59*38e8c45fSAndroid Build Coastguard Worker     }
60*38e8c45fSAndroid Build Coastguard Worker     return env;
61*38e8c45fSAndroid Build Coastguard Worker }
62*38e8c45fSAndroid Build Coastguard Worker 
toString(bool value)63*38e8c45fSAndroid Build Coastguard Worker inline const char* toString(bool value) {
64*38e8c45fSAndroid Build Coastguard Worker     return value ? "true" : "false";
65*38e8c45fSAndroid Build Coastguard Worker }
66*38e8c45fSAndroid Build Coastguard Worker } // namespace
67*38e8c45fSAndroid Build Coastguard Worker 
68*38e8c45fSAndroid Build Coastguard Worker namespace android {
69*38e8c45fSAndroid Build Coastguard Worker 
70*38e8c45fSAndroid Build Coastguard Worker Choreographer::Context Choreographer::gChoreographers;
71*38e8c45fSAndroid Build Coastguard Worker 
72*38e8c45fSAndroid Build Coastguard Worker static thread_local Choreographer* gChoreographer;
73*38e8c45fSAndroid Build Coastguard Worker 
initJVM(JNIEnv * env)74*38e8c45fSAndroid Build Coastguard Worker void Choreographer::initJVM(JNIEnv* env) {
75*38e8c45fSAndroid Build Coastguard Worker     env->GetJavaVM(&gJni.jvm);
76*38e8c45fSAndroid Build Coastguard Worker     // Now we need to find the java classes.
77*38e8c45fSAndroid Build Coastguard Worker     jclass dmgClass = env->FindClass("android/hardware/display/DisplayManagerGlobal");
78*38e8c45fSAndroid Build Coastguard Worker     gJni.displayManagerGlobal.clazz = static_cast<jclass>(env->NewGlobalRef(dmgClass));
79*38e8c45fSAndroid Build Coastguard Worker     gJni.displayManagerGlobal.getInstance =
80*38e8c45fSAndroid Build Coastguard Worker             env->GetStaticMethodID(dmgClass, "getInstance",
81*38e8c45fSAndroid Build Coastguard Worker                                    "()Landroid/hardware/display/DisplayManagerGlobal;");
82*38e8c45fSAndroid Build Coastguard Worker     gJni.displayManagerGlobal.registerNativeChoreographerForRefreshRateCallbacks =
83*38e8c45fSAndroid Build Coastguard Worker             env->GetMethodID(dmgClass, "registerNativeChoreographerForRefreshRateCallbacks", "()V");
84*38e8c45fSAndroid Build Coastguard Worker     gJni.displayManagerGlobal.unregisterNativeChoreographerForRefreshRateCallbacks =
85*38e8c45fSAndroid Build Coastguard Worker             env->GetMethodID(dmgClass, "unregisterNativeChoreographerForRefreshRateCallbacks",
86*38e8c45fSAndroid Build Coastguard Worker                              "()V");
87*38e8c45fSAndroid Build Coastguard Worker }
88*38e8c45fSAndroid Build Coastguard Worker 
getForThread()89*38e8c45fSAndroid Build Coastguard Worker Choreographer* Choreographer::getForThread() {
90*38e8c45fSAndroid Build Coastguard Worker     if (gChoreographer == nullptr) {
91*38e8c45fSAndroid Build Coastguard Worker         sp<Looper> looper = Looper::getForThread();
92*38e8c45fSAndroid Build Coastguard Worker         if (!looper.get()) {
93*38e8c45fSAndroid Build Coastguard Worker             ALOGW("No looper prepared for thread");
94*38e8c45fSAndroid Build Coastguard Worker             return nullptr;
95*38e8c45fSAndroid Build Coastguard Worker         }
96*38e8c45fSAndroid Build Coastguard Worker         gChoreographer = new Choreographer(looper);
97*38e8c45fSAndroid Build Coastguard Worker         status_t result = gChoreographer->initialize();
98*38e8c45fSAndroid Build Coastguard Worker         if (result != OK) {
99*38e8c45fSAndroid Build Coastguard Worker             ALOGW("Failed to initialize");
100*38e8c45fSAndroid Build Coastguard Worker             return nullptr;
101*38e8c45fSAndroid Build Coastguard Worker         }
102*38e8c45fSAndroid Build Coastguard Worker     }
103*38e8c45fSAndroid Build Coastguard Worker     return gChoreographer;
104*38e8c45fSAndroid Build Coastguard Worker }
105*38e8c45fSAndroid Build Coastguard Worker 
Choreographer(const sp<Looper> & looper,const sp<IBinder> & layerHandle)106*38e8c45fSAndroid Build Coastguard Worker Choreographer::Choreographer(const sp<Looper>& looper, const sp<IBinder>& layerHandle)
107*38e8c45fSAndroid Build Coastguard Worker       : DisplayEventDispatcher(looper, gui::ISurfaceComposer::VsyncSource::eVsyncSourceApp, {},
108*38e8c45fSAndroid Build Coastguard Worker                                layerHandle),
109*38e8c45fSAndroid Build Coastguard Worker         mLooper(looper),
110*38e8c45fSAndroid Build Coastguard Worker         mThreadId(std::this_thread::get_id()) {
111*38e8c45fSAndroid Build Coastguard Worker     std::lock_guard<std::mutex> _l(gChoreographers.lock);
112*38e8c45fSAndroid Build Coastguard Worker     gChoreographers.ptrs.push_back(this);
113*38e8c45fSAndroid Build Coastguard Worker }
114*38e8c45fSAndroid Build Coastguard Worker 
~Choreographer()115*38e8c45fSAndroid Build Coastguard Worker Choreographer::~Choreographer() {
116*38e8c45fSAndroid Build Coastguard Worker     std::lock_guard<std::mutex> _l(gChoreographers.lock);
117*38e8c45fSAndroid Build Coastguard Worker     gChoreographers.ptrs.erase(std::remove_if(gChoreographers.ptrs.begin(),
118*38e8c45fSAndroid Build Coastguard Worker                                               gChoreographers.ptrs.end(),
119*38e8c45fSAndroid Build Coastguard Worker                                               [=, this](Choreographer* c) { return c == this; }),
120*38e8c45fSAndroid Build Coastguard Worker                                gChoreographers.ptrs.end());
121*38e8c45fSAndroid Build Coastguard Worker     // Only poke DisplayManagerGlobal to unregister if we previously registered
122*38e8c45fSAndroid Build Coastguard Worker     // callbacks.
123*38e8c45fSAndroid Build Coastguard Worker     if (gChoreographers.ptrs.empty() && gChoreographers.registeredToDisplayManager) {
124*38e8c45fSAndroid Build Coastguard Worker         gChoreographers.registeredToDisplayManager = false;
125*38e8c45fSAndroid Build Coastguard Worker         JNIEnv* env = getJniEnv();
126*38e8c45fSAndroid Build Coastguard Worker         if (env == nullptr) {
127*38e8c45fSAndroid Build Coastguard Worker             ALOGW("JNI environment is unavailable, skipping choreographer cleanup");
128*38e8c45fSAndroid Build Coastguard Worker             return;
129*38e8c45fSAndroid Build Coastguard Worker         }
130*38e8c45fSAndroid Build Coastguard Worker         jobject dmg = env->CallStaticObjectMethod(gJni.displayManagerGlobal.clazz,
131*38e8c45fSAndroid Build Coastguard Worker                                                   gJni.displayManagerGlobal.getInstance);
132*38e8c45fSAndroid Build Coastguard Worker         if (dmg == nullptr) {
133*38e8c45fSAndroid Build Coastguard Worker             ALOGW("DMS is not initialized yet, skipping choreographer cleanup");
134*38e8c45fSAndroid Build Coastguard Worker         } else {
135*38e8c45fSAndroid Build Coastguard Worker             env->CallVoidMethod(dmg,
136*38e8c45fSAndroid Build Coastguard Worker                                 gJni.displayManagerGlobal
137*38e8c45fSAndroid Build Coastguard Worker                                         .unregisterNativeChoreographerForRefreshRateCallbacks);
138*38e8c45fSAndroid Build Coastguard Worker             env->DeleteLocalRef(dmg);
139*38e8c45fSAndroid Build Coastguard Worker         }
140*38e8c45fSAndroid Build Coastguard Worker     }
141*38e8c45fSAndroid Build Coastguard Worker }
142*38e8c45fSAndroid Build Coastguard Worker 
postFrameCallbackDelayed(AChoreographer_frameCallback cb,AChoreographer_frameCallback64 cb64,AChoreographer_vsyncCallback vsyncCallback,void * data,nsecs_t delay,CallbackType callbackType)143*38e8c45fSAndroid Build Coastguard Worker void Choreographer::postFrameCallbackDelayed(AChoreographer_frameCallback cb,
144*38e8c45fSAndroid Build Coastguard Worker                                              AChoreographer_frameCallback64 cb64,
145*38e8c45fSAndroid Build Coastguard Worker                                              AChoreographer_vsyncCallback vsyncCallback, void* data,
146*38e8c45fSAndroid Build Coastguard Worker                                              nsecs_t delay, CallbackType callbackType) {
147*38e8c45fSAndroid Build Coastguard Worker     nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
148*38e8c45fSAndroid Build Coastguard Worker     FrameCallback callback{cb, cb64, vsyncCallback, data, now + delay, callbackType};
149*38e8c45fSAndroid Build Coastguard Worker     {
150*38e8c45fSAndroid Build Coastguard Worker         std::lock_guard<std::mutex> _l{mLock};
151*38e8c45fSAndroid Build Coastguard Worker         mFrameCallbacks.push(callback);
152*38e8c45fSAndroid Build Coastguard Worker     }
153*38e8c45fSAndroid Build Coastguard Worker     if (callback.dueTime <= now) {
154*38e8c45fSAndroid Build Coastguard Worker         if (std::this_thread::get_id() != mThreadId) {
155*38e8c45fSAndroid Build Coastguard Worker             if (mLooper != nullptr) {
156*38e8c45fSAndroid Build Coastguard Worker                 Message m{MSG_SCHEDULE_VSYNC};
157*38e8c45fSAndroid Build Coastguard Worker                 mLooper->sendMessage(this, m);
158*38e8c45fSAndroid Build Coastguard Worker             } else {
159*38e8c45fSAndroid Build Coastguard Worker                 scheduleVsync();
160*38e8c45fSAndroid Build Coastguard Worker             }
161*38e8c45fSAndroid Build Coastguard Worker         } else {
162*38e8c45fSAndroid Build Coastguard Worker             scheduleVsync();
163*38e8c45fSAndroid Build Coastguard Worker         }
164*38e8c45fSAndroid Build Coastguard Worker     } else {
165*38e8c45fSAndroid Build Coastguard Worker         if (mLooper != nullptr) {
166*38e8c45fSAndroid Build Coastguard Worker             Message m{MSG_SCHEDULE_CALLBACKS};
167*38e8c45fSAndroid Build Coastguard Worker             mLooper->sendMessageDelayed(delay, this, m);
168*38e8c45fSAndroid Build Coastguard Worker         } else {
169*38e8c45fSAndroid Build Coastguard Worker             scheduleCallbacks();
170*38e8c45fSAndroid Build Coastguard Worker         }
171*38e8c45fSAndroid Build Coastguard Worker     }
172*38e8c45fSAndroid Build Coastguard Worker }
173*38e8c45fSAndroid Build Coastguard Worker 
registerRefreshRateCallback(AChoreographer_refreshRateCallback cb,void * data)174*38e8c45fSAndroid Build Coastguard Worker void Choreographer::registerRefreshRateCallback(AChoreographer_refreshRateCallback cb, void* data) {
175*38e8c45fSAndroid Build Coastguard Worker     std::lock_guard<std::mutex> _l{mLock};
176*38e8c45fSAndroid Build Coastguard Worker     for (const auto& callback : mRefreshRateCallbacks) {
177*38e8c45fSAndroid Build Coastguard Worker         // Don't re-add callbacks.
178*38e8c45fSAndroid Build Coastguard Worker         if (cb == callback.callback && data == callback.data) {
179*38e8c45fSAndroid Build Coastguard Worker             return;
180*38e8c45fSAndroid Build Coastguard Worker         }
181*38e8c45fSAndroid Build Coastguard Worker     }
182*38e8c45fSAndroid Build Coastguard Worker     mRefreshRateCallbacks.emplace_back(
183*38e8c45fSAndroid Build Coastguard Worker             RefreshRateCallback{.callback = cb, .data = data, .firstCallbackFired = false});
184*38e8c45fSAndroid Build Coastguard Worker     bool needsRegistration = false;
185*38e8c45fSAndroid Build Coastguard Worker     {
186*38e8c45fSAndroid Build Coastguard Worker         std::lock_guard<std::mutex> _l2(gChoreographers.lock);
187*38e8c45fSAndroid Build Coastguard Worker         needsRegistration = !gChoreographers.registeredToDisplayManager;
188*38e8c45fSAndroid Build Coastguard Worker     }
189*38e8c45fSAndroid Build Coastguard Worker     if (needsRegistration) {
190*38e8c45fSAndroid Build Coastguard Worker         JNIEnv* env = getJniEnv();
191*38e8c45fSAndroid Build Coastguard Worker         if (env == nullptr) {
192*38e8c45fSAndroid Build Coastguard Worker             ALOGW("JNI environment is unavailable, skipping registration");
193*38e8c45fSAndroid Build Coastguard Worker             return;
194*38e8c45fSAndroid Build Coastguard Worker         }
195*38e8c45fSAndroid Build Coastguard Worker         jobject dmg = env->CallStaticObjectMethod(gJni.displayManagerGlobal.clazz,
196*38e8c45fSAndroid Build Coastguard Worker                                                   gJni.displayManagerGlobal.getInstance);
197*38e8c45fSAndroid Build Coastguard Worker         if (dmg == nullptr) {
198*38e8c45fSAndroid Build Coastguard Worker             ALOGW("DMS is not initialized yet: skipping registration");
199*38e8c45fSAndroid Build Coastguard Worker             return;
200*38e8c45fSAndroid Build Coastguard Worker         } else {
201*38e8c45fSAndroid Build Coastguard Worker             env->CallVoidMethod(dmg,
202*38e8c45fSAndroid Build Coastguard Worker                                 gJni.displayManagerGlobal
203*38e8c45fSAndroid Build Coastguard Worker                                         .registerNativeChoreographerForRefreshRateCallbacks,
204*38e8c45fSAndroid Build Coastguard Worker                                 reinterpret_cast<int64_t>(this));
205*38e8c45fSAndroid Build Coastguard Worker             env->DeleteLocalRef(dmg);
206*38e8c45fSAndroid Build Coastguard Worker             {
207*38e8c45fSAndroid Build Coastguard Worker                 std::lock_guard<std::mutex> _l2(gChoreographers.lock);
208*38e8c45fSAndroid Build Coastguard Worker                 gChoreographers.registeredToDisplayManager = true;
209*38e8c45fSAndroid Build Coastguard Worker             }
210*38e8c45fSAndroid Build Coastguard Worker         }
211*38e8c45fSAndroid Build Coastguard Worker     } else {
212*38e8c45fSAndroid Build Coastguard Worker         scheduleLatestConfigRequest();
213*38e8c45fSAndroid Build Coastguard Worker     }
214*38e8c45fSAndroid Build Coastguard Worker }
215*38e8c45fSAndroid Build Coastguard Worker 
unregisterRefreshRateCallback(AChoreographer_refreshRateCallback cb,void * data)216*38e8c45fSAndroid Build Coastguard Worker void Choreographer::unregisterRefreshRateCallback(AChoreographer_refreshRateCallback cb,
217*38e8c45fSAndroid Build Coastguard Worker                                                   void* data) {
218*38e8c45fSAndroid Build Coastguard Worker     std::lock_guard<std::mutex> _l{mLock};
219*38e8c45fSAndroid Build Coastguard Worker     mRefreshRateCallbacks.erase(std::remove_if(mRefreshRateCallbacks.begin(),
220*38e8c45fSAndroid Build Coastguard Worker                                                mRefreshRateCallbacks.end(),
221*38e8c45fSAndroid Build Coastguard Worker                                                [&](const RefreshRateCallback& callback) {
222*38e8c45fSAndroid Build Coastguard Worker                                                    return cb == callback.callback &&
223*38e8c45fSAndroid Build Coastguard Worker                                                            data == callback.data;
224*38e8c45fSAndroid Build Coastguard Worker                                                }),
225*38e8c45fSAndroid Build Coastguard Worker                                 mRefreshRateCallbacks.end());
226*38e8c45fSAndroid Build Coastguard Worker }
227*38e8c45fSAndroid Build Coastguard Worker 
scheduleLatestConfigRequest()228*38e8c45fSAndroid Build Coastguard Worker void Choreographer::scheduleLatestConfigRequest() {
229*38e8c45fSAndroid Build Coastguard Worker     if (mLooper != nullptr) {
230*38e8c45fSAndroid Build Coastguard Worker         Message m{MSG_HANDLE_REFRESH_RATE_UPDATES};
231*38e8c45fSAndroid Build Coastguard Worker         mLooper->sendMessage(this, m);
232*38e8c45fSAndroid Build Coastguard Worker     } else {
233*38e8c45fSAndroid Build Coastguard Worker         // If the looper thread is detached from Choreographer, then refresh rate
234*38e8c45fSAndroid Build Coastguard Worker         // changes will be handled in AChoreographer_handlePendingEvents, so we
235*38e8c45fSAndroid Build Coastguard Worker         // need to wake up the looper thread by writing to the write-end of the
236*38e8c45fSAndroid Build Coastguard Worker         // socket the looper is listening on.
237*38e8c45fSAndroid Build Coastguard Worker         // Fortunately, these events are small so sending packets across the
238*38e8c45fSAndroid Build Coastguard Worker         // socket should be atomic across processes.
239*38e8c45fSAndroid Build Coastguard Worker         DisplayEventReceiver::Event event;
240*38e8c45fSAndroid Build Coastguard Worker         event.header =
241*38e8c45fSAndroid Build Coastguard Worker                 DisplayEventReceiver::Event::Header{DisplayEventReceiver::DISPLAY_EVENT_NULL,
242*38e8c45fSAndroid Build Coastguard Worker                                                     PhysicalDisplayId::fromPort(0), systemTime()};
243*38e8c45fSAndroid Build Coastguard Worker         injectEvent(event);
244*38e8c45fSAndroid Build Coastguard Worker     }
245*38e8c45fSAndroid Build Coastguard Worker }
246*38e8c45fSAndroid Build Coastguard Worker 
scheduleCallbacks()247*38e8c45fSAndroid Build Coastguard Worker void Choreographer::scheduleCallbacks() {
248*38e8c45fSAndroid Build Coastguard Worker     const nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
249*38e8c45fSAndroid Build Coastguard Worker     nsecs_t dueTime;
250*38e8c45fSAndroid Build Coastguard Worker     {
251*38e8c45fSAndroid Build Coastguard Worker         std::lock_guard<std::mutex> _l{mLock};
252*38e8c45fSAndroid Build Coastguard Worker         // If there are no pending callbacks then don't schedule a vsync
253*38e8c45fSAndroid Build Coastguard Worker         if (mFrameCallbacks.empty()) {
254*38e8c45fSAndroid Build Coastguard Worker             return;
255*38e8c45fSAndroid Build Coastguard Worker         }
256*38e8c45fSAndroid Build Coastguard Worker         dueTime = mFrameCallbacks.top().dueTime;
257*38e8c45fSAndroid Build Coastguard Worker     }
258*38e8c45fSAndroid Build Coastguard Worker 
259*38e8c45fSAndroid Build Coastguard Worker     if (dueTime <= now) {
260*38e8c45fSAndroid Build Coastguard Worker         ALOGV("choreographer %p ~ scheduling vsync", this);
261*38e8c45fSAndroid Build Coastguard Worker         scheduleVsync();
262*38e8c45fSAndroid Build Coastguard Worker         return;
263*38e8c45fSAndroid Build Coastguard Worker     }
264*38e8c45fSAndroid Build Coastguard Worker }
265*38e8c45fSAndroid Build Coastguard Worker 
handleRefreshRateUpdates()266*38e8c45fSAndroid Build Coastguard Worker void Choreographer::handleRefreshRateUpdates() {
267*38e8c45fSAndroid Build Coastguard Worker     std::vector<RefreshRateCallback> callbacks{};
268*38e8c45fSAndroid Build Coastguard Worker     const nsecs_t pendingPeriod = gChoreographers.mLastKnownVsync.load();
269*38e8c45fSAndroid Build Coastguard Worker     const nsecs_t lastPeriod = mLatestVsyncPeriod;
270*38e8c45fSAndroid Build Coastguard Worker     if (pendingPeriod > 0) {
271*38e8c45fSAndroid Build Coastguard Worker         mLatestVsyncPeriod = pendingPeriod;
272*38e8c45fSAndroid Build Coastguard Worker     }
273*38e8c45fSAndroid Build Coastguard Worker     {
274*38e8c45fSAndroid Build Coastguard Worker         std::lock_guard<std::mutex> _l{mLock};
275*38e8c45fSAndroid Build Coastguard Worker         for (auto& cb : mRefreshRateCallbacks) {
276*38e8c45fSAndroid Build Coastguard Worker             callbacks.push_back(cb);
277*38e8c45fSAndroid Build Coastguard Worker             cb.firstCallbackFired = true;
278*38e8c45fSAndroid Build Coastguard Worker         }
279*38e8c45fSAndroid Build Coastguard Worker     }
280*38e8c45fSAndroid Build Coastguard Worker 
281*38e8c45fSAndroid Build Coastguard Worker     for (auto& cb : callbacks) {
282*38e8c45fSAndroid Build Coastguard Worker         if (!cb.firstCallbackFired || (pendingPeriod > 0 && pendingPeriod != lastPeriod)) {
283*38e8c45fSAndroid Build Coastguard Worker             cb.callback(pendingPeriod, cb.data);
284*38e8c45fSAndroid Build Coastguard Worker         }
285*38e8c45fSAndroid Build Coastguard Worker     }
286*38e8c45fSAndroid Build Coastguard Worker }
287*38e8c45fSAndroid Build Coastguard Worker 
dispatchCallbacks(const std::vector<FrameCallback> & callbacks,VsyncEventData vsyncEventData,nsecs_t timestamp)288*38e8c45fSAndroid Build Coastguard Worker void Choreographer::dispatchCallbacks(const std::vector<FrameCallback>& callbacks,
289*38e8c45fSAndroid Build Coastguard Worker                                       VsyncEventData vsyncEventData, nsecs_t timestamp) {
290*38e8c45fSAndroid Build Coastguard Worker     for (const auto& cb : callbacks) {
291*38e8c45fSAndroid Build Coastguard Worker         if (cb.vsyncCallback != nullptr) {
292*38e8c45fSAndroid Build Coastguard Worker             ATRACE_FORMAT("AChoreographer_vsyncCallback %" PRId64,
293*38e8c45fSAndroid Build Coastguard Worker                           vsyncEventData.preferredVsyncId());
294*38e8c45fSAndroid Build Coastguard Worker             const ChoreographerFrameCallbackDataImpl frameCallbackData =
295*38e8c45fSAndroid Build Coastguard Worker                     createFrameCallbackData(timestamp);
296*38e8c45fSAndroid Build Coastguard Worker             registerStartTime();
297*38e8c45fSAndroid Build Coastguard Worker             mInCallback = true;
298*38e8c45fSAndroid Build Coastguard Worker             cb.vsyncCallback(reinterpret_cast<const AChoreographerFrameCallbackData*>(
299*38e8c45fSAndroid Build Coastguard Worker                                      &frameCallbackData),
300*38e8c45fSAndroid Build Coastguard Worker                              cb.data);
301*38e8c45fSAndroid Build Coastguard Worker             mInCallback = false;
302*38e8c45fSAndroid Build Coastguard Worker         } else if (cb.callback64 != nullptr) {
303*38e8c45fSAndroid Build Coastguard Worker             ATRACE_FORMAT("AChoreographer_frameCallback64");
304*38e8c45fSAndroid Build Coastguard Worker             cb.callback64(timestamp, cb.data);
305*38e8c45fSAndroid Build Coastguard Worker         } else if (cb.callback != nullptr) {
306*38e8c45fSAndroid Build Coastguard Worker             ATRACE_FORMAT("AChoreographer_frameCallback");
307*38e8c45fSAndroid Build Coastguard Worker             cb.callback(timestamp, cb.data);
308*38e8c45fSAndroid Build Coastguard Worker         }
309*38e8c45fSAndroid Build Coastguard Worker     }
310*38e8c45fSAndroid Build Coastguard Worker }
311*38e8c45fSAndroid Build Coastguard Worker 
dispatchVsync(nsecs_t timestamp,PhysicalDisplayId,uint32_t,VsyncEventData vsyncEventData)312*38e8c45fSAndroid Build Coastguard Worker void Choreographer::dispatchVsync(nsecs_t timestamp, PhysicalDisplayId, uint32_t,
313*38e8c45fSAndroid Build Coastguard Worker                                   VsyncEventData vsyncEventData) {
314*38e8c45fSAndroid Build Coastguard Worker     std::vector<FrameCallback> animationCallbacks{};
315*38e8c45fSAndroid Build Coastguard Worker     std::vector<FrameCallback> inputCallbacks{};
316*38e8c45fSAndroid Build Coastguard Worker     {
317*38e8c45fSAndroid Build Coastguard Worker         std::lock_guard<std::mutex> _l{mLock};
318*38e8c45fSAndroid Build Coastguard Worker         nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
319*38e8c45fSAndroid Build Coastguard Worker         while (!mFrameCallbacks.empty() && mFrameCallbacks.top().dueTime < now) {
320*38e8c45fSAndroid Build Coastguard Worker             if (mFrameCallbacks.top().callbackType == CALLBACK_INPUT) {
321*38e8c45fSAndroid Build Coastguard Worker                 inputCallbacks.push_back(mFrameCallbacks.top());
322*38e8c45fSAndroid Build Coastguard Worker             } else {
323*38e8c45fSAndroid Build Coastguard Worker                 animationCallbacks.push_back(mFrameCallbacks.top());
324*38e8c45fSAndroid Build Coastguard Worker             }
325*38e8c45fSAndroid Build Coastguard Worker             mFrameCallbacks.pop();
326*38e8c45fSAndroid Build Coastguard Worker         }
327*38e8c45fSAndroid Build Coastguard Worker     }
328*38e8c45fSAndroid Build Coastguard Worker     mLastVsyncEventData = vsyncEventData;
329*38e8c45fSAndroid Build Coastguard Worker     // Callbacks with type CALLBACK_INPUT should always run first
330*38e8c45fSAndroid Build Coastguard Worker     {
331*38e8c45fSAndroid Build Coastguard Worker         ATRACE_FORMAT("CALLBACK_INPUT");
332*38e8c45fSAndroid Build Coastguard Worker         dispatchCallbacks(inputCallbacks, vsyncEventData, timestamp);
333*38e8c45fSAndroid Build Coastguard Worker     }
334*38e8c45fSAndroid Build Coastguard Worker     {
335*38e8c45fSAndroid Build Coastguard Worker         ATRACE_FORMAT("CALLBACK_ANIMATION");
336*38e8c45fSAndroid Build Coastguard Worker         dispatchCallbacks(animationCallbacks, vsyncEventData, timestamp);
337*38e8c45fSAndroid Build Coastguard Worker     }
338*38e8c45fSAndroid Build Coastguard Worker }
339*38e8c45fSAndroid Build Coastguard Worker 
dispatchHotplug(nsecs_t,PhysicalDisplayId displayId,bool connected)340*38e8c45fSAndroid Build Coastguard Worker void Choreographer::dispatchHotplug(nsecs_t, PhysicalDisplayId displayId, bool connected) {
341*38e8c45fSAndroid Build Coastguard Worker     ALOGV("choreographer %p ~ received hotplug event (displayId=%s, connected=%s), ignoring.", this,
342*38e8c45fSAndroid Build Coastguard Worker           to_string(displayId).c_str(), toString(connected));
343*38e8c45fSAndroid Build Coastguard Worker }
344*38e8c45fSAndroid Build Coastguard Worker 
dispatchHotplugConnectionError(nsecs_t,int32_t connectionError)345*38e8c45fSAndroid Build Coastguard Worker void Choreographer::dispatchHotplugConnectionError(nsecs_t, int32_t connectionError) {
346*38e8c45fSAndroid Build Coastguard Worker     ALOGV("choreographer %p ~ received hotplug connection error event (connectionError=%d), "
347*38e8c45fSAndroid Build Coastguard Worker           "ignoring.",
348*38e8c45fSAndroid Build Coastguard Worker           this, connectionError);
349*38e8c45fSAndroid Build Coastguard Worker }
350*38e8c45fSAndroid Build Coastguard Worker 
dispatchModeChanged(nsecs_t,PhysicalDisplayId,int32_t,nsecs_t)351*38e8c45fSAndroid Build Coastguard Worker void Choreographer::dispatchModeChanged(nsecs_t, PhysicalDisplayId, int32_t, nsecs_t) {
352*38e8c45fSAndroid Build Coastguard Worker     LOG_ALWAYS_FATAL("dispatchModeChanged was called but was never registered");
353*38e8c45fSAndroid Build Coastguard Worker }
354*38e8c45fSAndroid Build Coastguard Worker 
dispatchFrameRateOverrides(nsecs_t,PhysicalDisplayId,std::vector<FrameRateOverride>)355*38e8c45fSAndroid Build Coastguard Worker void Choreographer::dispatchFrameRateOverrides(nsecs_t, PhysicalDisplayId,
356*38e8c45fSAndroid Build Coastguard Worker                                                std::vector<FrameRateOverride>) {
357*38e8c45fSAndroid Build Coastguard Worker     LOG_ALWAYS_FATAL("dispatchFrameRateOverrides was called but was never registered");
358*38e8c45fSAndroid Build Coastguard Worker }
359*38e8c45fSAndroid Build Coastguard Worker 
dispatchNullEvent(nsecs_t,PhysicalDisplayId)360*38e8c45fSAndroid Build Coastguard Worker void Choreographer::dispatchNullEvent(nsecs_t, PhysicalDisplayId) {
361*38e8c45fSAndroid Build Coastguard Worker     ALOGV("choreographer %p ~ received null event.", this);
362*38e8c45fSAndroid Build Coastguard Worker     handleRefreshRateUpdates();
363*38e8c45fSAndroid Build Coastguard Worker }
364*38e8c45fSAndroid Build Coastguard Worker 
dispatchHdcpLevelsChanged(PhysicalDisplayId displayId,int32_t connectedLevel,int32_t maxLevel)365*38e8c45fSAndroid Build Coastguard Worker void Choreographer::dispatchHdcpLevelsChanged(PhysicalDisplayId displayId, int32_t connectedLevel,
366*38e8c45fSAndroid Build Coastguard Worker                                               int32_t maxLevel) {
367*38e8c45fSAndroid Build Coastguard Worker     ALOGV("choreographer %p ~ received hdcp levels change event (displayId=%s, connectedLevel=%d, "
368*38e8c45fSAndroid Build Coastguard Worker           "maxLevel=%d), ignoring.",
369*38e8c45fSAndroid Build Coastguard Worker           this, to_string(displayId).c_str(), connectedLevel, maxLevel);
370*38e8c45fSAndroid Build Coastguard Worker }
371*38e8c45fSAndroid Build Coastguard Worker 
dispatchModeRejected(PhysicalDisplayId,int32_t)372*38e8c45fSAndroid Build Coastguard Worker void Choreographer::dispatchModeRejected(PhysicalDisplayId, int32_t) {
373*38e8c45fSAndroid Build Coastguard Worker     LOG_ALWAYS_FATAL("dispatchModeRejected was called but was never registered");
374*38e8c45fSAndroid Build Coastguard Worker }
375*38e8c45fSAndroid Build Coastguard Worker 
handleMessage(const Message & message)376*38e8c45fSAndroid Build Coastguard Worker void Choreographer::handleMessage(const Message& message) {
377*38e8c45fSAndroid Build Coastguard Worker     switch (message.what) {
378*38e8c45fSAndroid Build Coastguard Worker         case MSG_SCHEDULE_CALLBACKS:
379*38e8c45fSAndroid Build Coastguard Worker             scheduleCallbacks();
380*38e8c45fSAndroid Build Coastguard Worker             break;
381*38e8c45fSAndroid Build Coastguard Worker         case MSG_SCHEDULE_VSYNC:
382*38e8c45fSAndroid Build Coastguard Worker             scheduleVsync();
383*38e8c45fSAndroid Build Coastguard Worker             break;
384*38e8c45fSAndroid Build Coastguard Worker         case MSG_HANDLE_REFRESH_RATE_UPDATES:
385*38e8c45fSAndroid Build Coastguard Worker             handleRefreshRateUpdates();
386*38e8c45fSAndroid Build Coastguard Worker             break;
387*38e8c45fSAndroid Build Coastguard Worker     }
388*38e8c45fSAndroid Build Coastguard Worker }
389*38e8c45fSAndroid Build Coastguard Worker 
getFrameInterval() const390*38e8c45fSAndroid Build Coastguard Worker int64_t Choreographer::getFrameInterval() const {
391*38e8c45fSAndroid Build Coastguard Worker     return mLastVsyncEventData.frameInterval;
392*38e8c45fSAndroid Build Coastguard Worker }
393*38e8c45fSAndroid Build Coastguard Worker 
inCallback() const394*38e8c45fSAndroid Build Coastguard Worker bool Choreographer::inCallback() const {
395*38e8c45fSAndroid Build Coastguard Worker     return mInCallback;
396*38e8c45fSAndroid Build Coastguard Worker }
397*38e8c45fSAndroid Build Coastguard Worker 
createFrameCallbackData(nsecs_t timestamp) const398*38e8c45fSAndroid Build Coastguard Worker ChoreographerFrameCallbackDataImpl Choreographer::createFrameCallbackData(nsecs_t timestamp) const {
399*38e8c45fSAndroid Build Coastguard Worker     return {.frameTimeNanos = timestamp,
400*38e8c45fSAndroid Build Coastguard Worker             .vsyncEventData = mLastVsyncEventData,
401*38e8c45fSAndroid Build Coastguard Worker             .choreographer = this};
402*38e8c45fSAndroid Build Coastguard Worker }
403*38e8c45fSAndroid Build Coastguard Worker 
registerStartTime() const404*38e8c45fSAndroid Build Coastguard Worker void Choreographer::registerStartTime() const {
405*38e8c45fSAndroid Build Coastguard Worker     std::scoped_lock _l(gChoreographers.lock);
406*38e8c45fSAndroid Build Coastguard Worker     for (VsyncEventData::FrameTimeline frameTimeline : mLastVsyncEventData.frameTimelines) {
407*38e8c45fSAndroid Build Coastguard Worker         while (gChoreographers.startTimes.size() >= kMaxStartTimes) {
408*38e8c45fSAndroid Build Coastguard Worker             gChoreographers.startTimes.erase(gChoreographers.startTimes.begin());
409*38e8c45fSAndroid Build Coastguard Worker         }
410*38e8c45fSAndroid Build Coastguard Worker         gChoreographers.startTimes[frameTimeline.vsyncId] = systemTime(SYSTEM_TIME_MONOTONIC);
411*38e8c45fSAndroid Build Coastguard Worker     }
412*38e8c45fSAndroid Build Coastguard Worker }
413*38e8c45fSAndroid Build Coastguard Worker 
signalRefreshRateCallbacks(nsecs_t vsyncPeriod)414*38e8c45fSAndroid Build Coastguard Worker void Choreographer::signalRefreshRateCallbacks(nsecs_t vsyncPeriod) {
415*38e8c45fSAndroid Build Coastguard Worker     std::lock_guard<std::mutex> _l(gChoreographers.lock);
416*38e8c45fSAndroid Build Coastguard Worker     gChoreographers.mLastKnownVsync.store(vsyncPeriod);
417*38e8c45fSAndroid Build Coastguard Worker     for (auto c : gChoreographers.ptrs) {
418*38e8c45fSAndroid Build Coastguard Worker         c->scheduleLatestConfigRequest();
419*38e8c45fSAndroid Build Coastguard Worker     }
420*38e8c45fSAndroid Build Coastguard Worker }
421*38e8c45fSAndroid Build Coastguard Worker 
getStartTimeNanosForVsyncId(AVsyncId vsyncId)422*38e8c45fSAndroid Build Coastguard Worker int64_t Choreographer::getStartTimeNanosForVsyncId(AVsyncId vsyncId) {
423*38e8c45fSAndroid Build Coastguard Worker     std::scoped_lock _l(gChoreographers.lock);
424*38e8c45fSAndroid Build Coastguard Worker     const auto iter = gChoreographers.startTimes.find(vsyncId);
425*38e8c45fSAndroid Build Coastguard Worker     if (iter == gChoreographers.startTimes.end()) {
426*38e8c45fSAndroid Build Coastguard Worker         ALOGW("Start time was not found for vsync id: %" PRId64, vsyncId);
427*38e8c45fSAndroid Build Coastguard Worker         return 0;
428*38e8c45fSAndroid Build Coastguard Worker     }
429*38e8c45fSAndroid Build Coastguard Worker     return iter->second;
430*38e8c45fSAndroid Build Coastguard Worker }
431*38e8c45fSAndroid Build Coastguard Worker 
getLooper()432*38e8c45fSAndroid Build Coastguard Worker const sp<Looper> Choreographer::getLooper() {
433*38e8c45fSAndroid Build Coastguard Worker     return mLooper;
434*38e8c45fSAndroid Build Coastguard Worker }
435*38e8c45fSAndroid Build Coastguard Worker 
436*38e8c45fSAndroid Build Coastguard Worker } // namespace android
437