1 /*
2 * Copyright (C) 2015 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
17 #define LOG_TAG "Choreographer"
18 //#define LOG_NDEBUG 0
19
20 #include <android-base/thread_annotations.h>
21 #include <gui/DisplayEventDispatcher.h>
22 #include <gui/ISurfaceComposer.h>
23 #include <gui/SurfaceComposerClient.h>
24 #include <jni.h>
25 #include <private/android/choreographer.h>
26 #include <utils/Looper.h>
27 #include <utils/Timers.h>
28
29 #include <cinttypes>
30 #include <mutex>
31 #include <optional>
32 #include <queue>
33 #include <thread>
34
35 namespace {
36 struct {
37 // Global JVM that is provided by zygote
38 JavaVM* jvm = nullptr;
39 struct {
40 jclass clazz;
41 jmethodID getInstance;
42 jmethodID registerNativeChoreographerForRefreshRateCallbacks;
43 jmethodID unregisterNativeChoreographerForRefreshRateCallbacks;
44 } displayManagerGlobal;
45 } gJni;
46
47 // Gets the JNIEnv* for this thread, and performs one-off initialization if we
48 // have never retrieved a JNIEnv* pointer before.
getJniEnv()49 JNIEnv* getJniEnv() {
50 if (gJni.jvm == nullptr) {
51 ALOGW("AChoreographer: No JVM provided!");
52 return nullptr;
53 }
54
55 JNIEnv* env = nullptr;
56 if (gJni.jvm->GetEnv((void**)&env, JNI_VERSION_1_4) != JNI_OK) {
57 ALOGD("Attaching thread to JVM for AChoreographer");
58 JavaVMAttachArgs args = {JNI_VERSION_1_4, "AChoreographer_env", NULL};
59 jint attachResult = gJni.jvm->AttachCurrentThreadAsDaemon(&env, (void*)&args);
60 if (attachResult != JNI_OK) {
61 ALOGE("Unable to attach thread. Error: %d", attachResult);
62 return nullptr;
63 }
64 }
65 if (env == nullptr) {
66 ALOGW("AChoreographer: No JNI env available!");
67 }
68 return env;
69 }
70
toString(bool value)71 inline const char* toString(bool value) {
72 return value ? "true" : "false";
73 }
74 } // namespace
75
76 namespace android {
77
78 struct FrameCallback {
79 AChoreographer_frameCallback callback;
80 AChoreographer_frameCallback64 callback64;
81 void* data;
82 nsecs_t dueTime;
83
operator <android::FrameCallback84 inline bool operator<(const FrameCallback& rhs) const {
85 // Note that this is intentionally flipped because we want callbacks due sooner to be at
86 // the head of the queue
87 return dueTime > rhs.dueTime;
88 }
89 };
90
91 struct RefreshRateCallback {
92 AChoreographer_refreshRateCallback callback;
93 void* data;
94 bool firstCallbackFired = false;
95 };
96
97 class Choreographer;
98
99 struct {
100 std::mutex lock;
101 std::vector<Choreographer*> ptrs GUARDED_BY(lock);
102 bool registeredToDisplayManager GUARDED_BY(lock) = false;
103
104 std::atomic<nsecs_t> mLastKnownVsync = -1;
105 } gChoreographers;
106
107 class Choreographer : public DisplayEventDispatcher, public MessageHandler {
108 public:
109 explicit Choreographer(const sp<Looper>& looper) EXCLUDES(gChoreographers.lock);
110 void postFrameCallbackDelayed(AChoreographer_frameCallback cb,
111 AChoreographer_frameCallback64 cb64, void* data, nsecs_t delay);
112 void registerRefreshRateCallback(AChoreographer_refreshRateCallback cb, void* data)
113 EXCLUDES(gChoreographers.lock);
114 void unregisterRefreshRateCallback(AChoreographer_refreshRateCallback cb, void* data);
115 // Drains the queue of pending vsync periods and dispatches refresh rate
116 // updates to callbacks.
117 // The assumption is that this method is only called on a single
118 // processing thread, either by looper or by AChoreographer_handleEvents
119 void handleRefreshRateUpdates();
120 void scheduleLatestConfigRequest();
121
122 enum {
123 MSG_SCHEDULE_CALLBACKS = 0,
124 MSG_SCHEDULE_VSYNC = 1,
125 MSG_HANDLE_REFRESH_RATE_UPDATES = 2,
126 };
127 virtual void handleMessage(const Message& message) override;
128
129 static Choreographer* getForThread();
130 virtual ~Choreographer() override EXCLUDES(gChoreographers.lock);
131 int64_t getVsyncId() const;
132 int64_t getFrameDeadline() const;
133 int64_t getFrameInterval() const;
134
135 private:
136 Choreographer(const Choreographer&) = delete;
137
138 void dispatchVsync(nsecs_t timestamp, PhysicalDisplayId displayId, uint32_t count,
139 VsyncEventData vsyncEventData) override;
140 void dispatchHotplug(nsecs_t timestamp, PhysicalDisplayId displayId, bool connected) override;
141 void dispatchModeChanged(nsecs_t timestamp, PhysicalDisplayId displayId, int32_t modeId,
142 nsecs_t vsyncPeriod) override;
143 void dispatchNullEvent(nsecs_t, PhysicalDisplayId) override;
144 void dispatchFrameRateOverrides(nsecs_t timestamp, PhysicalDisplayId displayId,
145 std::vector<FrameRateOverride> overrides) override;
146
147 void scheduleCallbacks();
148
149 std::mutex mLock;
150 // Protected by mLock
151 std::priority_queue<FrameCallback> mFrameCallbacks;
152 std::vector<RefreshRateCallback> mRefreshRateCallbacks;
153
154 nsecs_t mLatestVsyncPeriod = -1;
155 VsyncEventData mLastVsyncEventData;
156
157 const sp<Looper> mLooper;
158 const std::thread::id mThreadId;
159 };
160
161 static thread_local Choreographer* gChoreographer;
getForThread()162 Choreographer* Choreographer::getForThread() {
163 if (gChoreographer == nullptr) {
164 sp<Looper> looper = Looper::getForThread();
165 if (!looper.get()) {
166 ALOGW("No looper prepared for thread");
167 return nullptr;
168 }
169 gChoreographer = new Choreographer(looper);
170 status_t result = gChoreographer->initialize();
171 if (result != OK) {
172 ALOGW("Failed to initialize");
173 return nullptr;
174 }
175 }
176 return gChoreographer;
177 }
178
Choreographer(const sp<Looper> & looper)179 Choreographer::Choreographer(const sp<Looper>& looper)
180 : DisplayEventDispatcher(looper, ISurfaceComposer::VsyncSource::eVsyncSourceApp),
181 mLooper(looper),
182 mThreadId(std::this_thread::get_id()) {
183 std::lock_guard<std::mutex> _l(gChoreographers.lock);
184 gChoreographers.ptrs.push_back(this);
185 }
186
~Choreographer()187 Choreographer::~Choreographer() {
188 std::lock_guard<std::mutex> _l(gChoreographers.lock);
189 gChoreographers.ptrs.erase(std::remove_if(gChoreographers.ptrs.begin(),
190 gChoreographers.ptrs.end(),
191 [=](Choreographer* c) { return c == this; }),
192 gChoreographers.ptrs.end());
193 // Only poke DisplayManagerGlobal to unregister if we previously registered
194 // callbacks.
195 if (gChoreographers.ptrs.empty() && gChoreographers.registeredToDisplayManager) {
196 JNIEnv* env = getJniEnv();
197 if (env == nullptr) {
198 ALOGW("JNI environment is unavailable, skipping choreographer cleanup");
199 return;
200 }
201 jobject dmg = env->CallStaticObjectMethod(gJni.displayManagerGlobal.clazz,
202 gJni.displayManagerGlobal.getInstance);
203 if (dmg == nullptr) {
204 ALOGW("DMS is not initialized yet, skipping choreographer cleanup");
205 } else {
206 env->CallVoidMethod(dmg,
207 gJni.displayManagerGlobal
208 .unregisterNativeChoreographerForRefreshRateCallbacks);
209 env->DeleteLocalRef(dmg);
210 }
211 }
212 }
213
postFrameCallbackDelayed(AChoreographer_frameCallback cb,AChoreographer_frameCallback64 cb64,void * data,nsecs_t delay)214 void Choreographer::postFrameCallbackDelayed(
215 AChoreographer_frameCallback cb, AChoreographer_frameCallback64 cb64, void* data, nsecs_t delay) {
216 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
217 FrameCallback callback{cb, cb64, data, now + delay};
218 {
219 std::lock_guard<std::mutex> _l{mLock};
220 mFrameCallbacks.push(callback);
221 }
222 if (callback.dueTime <= now) {
223 if (std::this_thread::get_id() != mThreadId) {
224 if (mLooper != nullptr) {
225 Message m{MSG_SCHEDULE_VSYNC};
226 mLooper->sendMessage(this, m);
227 } else {
228 scheduleVsync();
229 }
230 } else {
231 scheduleVsync();
232 }
233 } else {
234 if (mLooper != nullptr) {
235 Message m{MSG_SCHEDULE_CALLBACKS};
236 mLooper->sendMessageDelayed(delay, this, m);
237 } else {
238 scheduleCallbacks();
239 }
240 }
241 }
242
registerRefreshRateCallback(AChoreographer_refreshRateCallback cb,void * data)243 void Choreographer::registerRefreshRateCallback(AChoreographer_refreshRateCallback cb, void* data) {
244 std::lock_guard<std::mutex> _l{mLock};
245 for (const auto& callback : mRefreshRateCallbacks) {
246 // Don't re-add callbacks.
247 if (cb == callback.callback && data == callback.data) {
248 return;
249 }
250 }
251 mRefreshRateCallbacks.emplace_back(
252 RefreshRateCallback{.callback = cb, .data = data, .firstCallbackFired = false});
253 bool needsRegistration = false;
254 {
255 std::lock_guard<std::mutex> _l2(gChoreographers.lock);
256 needsRegistration = !gChoreographers.registeredToDisplayManager;
257 }
258 if (needsRegistration) {
259 JNIEnv* env = getJniEnv();
260 if (env == nullptr) {
261 ALOGW("JNI environment is unavailable, skipping registration");
262 return;
263 }
264 jobject dmg = env->CallStaticObjectMethod(gJni.displayManagerGlobal.clazz,
265 gJni.displayManagerGlobal.getInstance);
266 if (dmg == nullptr) {
267 ALOGW("DMS is not initialized yet: skipping registration");
268 return;
269 } else {
270 env->CallVoidMethod(dmg,
271 gJni.displayManagerGlobal
272 .registerNativeChoreographerForRefreshRateCallbacks,
273 reinterpret_cast<int64_t>(this));
274 env->DeleteLocalRef(dmg);
275 {
276 std::lock_guard<std::mutex> _l2(gChoreographers.lock);
277 gChoreographers.registeredToDisplayManager = true;
278 }
279 }
280 } else {
281 scheduleLatestConfigRequest();
282 }
283 }
284
unregisterRefreshRateCallback(AChoreographer_refreshRateCallback cb,void * data)285 void Choreographer::unregisterRefreshRateCallback(AChoreographer_refreshRateCallback cb,
286 void* data) {
287 std::lock_guard<std::mutex> _l{mLock};
288 mRefreshRateCallbacks.erase(std::remove_if(mRefreshRateCallbacks.begin(),
289 mRefreshRateCallbacks.end(),
290 [&](const RefreshRateCallback& callback) {
291 return cb == callback.callback &&
292 data == callback.data;
293 }),
294 mRefreshRateCallbacks.end());
295 }
296
scheduleLatestConfigRequest()297 void Choreographer::scheduleLatestConfigRequest() {
298 if (mLooper != nullptr) {
299 Message m{MSG_HANDLE_REFRESH_RATE_UPDATES};
300 mLooper->sendMessage(this, m);
301 } else {
302 // If the looper thread is detached from Choreographer, then refresh rate
303 // changes will be handled in AChoreographer_handlePendingEvents, so we
304 // need to wake up the looper thread by writing to the write-end of the
305 // socket the looper is listening on.
306 // Fortunately, these events are small so sending packets across the
307 // socket should be atomic across processes.
308 DisplayEventReceiver::Event event;
309 event.header = DisplayEventReceiver::Event::Header{DisplayEventReceiver::DISPLAY_EVENT_NULL,
310 PhysicalDisplayId(0), systemTime()};
311 injectEvent(event);
312 }
313 }
314
scheduleCallbacks()315 void Choreographer::scheduleCallbacks() {
316 const nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
317 nsecs_t dueTime;
318 {
319 std::lock_guard<std::mutex> _l{mLock};
320 // If there are no pending callbacks then don't schedule a vsync
321 if (mFrameCallbacks.empty()) {
322 return;
323 }
324 dueTime = mFrameCallbacks.top().dueTime;
325 }
326
327 if (dueTime <= now) {
328 ALOGV("choreographer %p ~ scheduling vsync", this);
329 scheduleVsync();
330 return;
331 }
332 }
333
handleRefreshRateUpdates()334 void Choreographer::handleRefreshRateUpdates() {
335 std::vector<RefreshRateCallback> callbacks{};
336 const nsecs_t pendingPeriod = gChoreographers.mLastKnownVsync.load();
337 const nsecs_t lastPeriod = mLatestVsyncPeriod;
338 if (pendingPeriod > 0) {
339 mLatestVsyncPeriod = pendingPeriod;
340 }
341 {
342 std::lock_guard<std::mutex> _l{mLock};
343 for (auto& cb : mRefreshRateCallbacks) {
344 callbacks.push_back(cb);
345 cb.firstCallbackFired = true;
346 }
347 }
348
349 for (auto& cb : callbacks) {
350 if (!cb.firstCallbackFired || (pendingPeriod > 0 && pendingPeriod != lastPeriod)) {
351 cb.callback(pendingPeriod, cb.data);
352 }
353 }
354 }
355
356 // TODO(b/74619554): The PhysicalDisplayId is ignored because SF only emits VSYNC events for the
357 // internal display and DisplayEventReceiver::requestNextVsync only allows requesting VSYNC for
358 // the internal display implicitly.
dispatchVsync(nsecs_t timestamp,PhysicalDisplayId,uint32_t,VsyncEventData vsyncEventData)359 void Choreographer::dispatchVsync(nsecs_t timestamp, PhysicalDisplayId, uint32_t,
360 VsyncEventData vsyncEventData) {
361 std::vector<FrameCallback> callbacks{};
362 {
363 std::lock_guard<std::mutex> _l{mLock};
364 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
365 while (!mFrameCallbacks.empty() && mFrameCallbacks.top().dueTime < now) {
366 callbacks.push_back(mFrameCallbacks.top());
367 mFrameCallbacks.pop();
368 }
369 }
370 mLastVsyncEventData = vsyncEventData;
371 for (const auto& cb : callbacks) {
372 if (cb.callback64 != nullptr) {
373 cb.callback64(timestamp, cb.data);
374 } else if (cb.callback != nullptr) {
375 cb.callback(timestamp, cb.data);
376 }
377 }
378 }
379
dispatchHotplug(nsecs_t,PhysicalDisplayId displayId,bool connected)380 void Choreographer::dispatchHotplug(nsecs_t, PhysicalDisplayId displayId, bool connected) {
381 ALOGV("choreographer %p ~ received hotplug event (displayId=%s, connected=%s), ignoring.",
382 this, to_string(displayId).c_str(), toString(connected));
383 }
384
dispatchModeChanged(nsecs_t,PhysicalDisplayId,int32_t,nsecs_t)385 void Choreographer::dispatchModeChanged(nsecs_t, PhysicalDisplayId, int32_t, nsecs_t) {
386 LOG_ALWAYS_FATAL("dispatchModeChanged was called but was never registered");
387 }
388
dispatchFrameRateOverrides(nsecs_t,PhysicalDisplayId,std::vector<FrameRateOverride>)389 void Choreographer::dispatchFrameRateOverrides(nsecs_t, PhysicalDisplayId,
390 std::vector<FrameRateOverride>) {
391 LOG_ALWAYS_FATAL("dispatchFrameRateOverrides was called but was never registered");
392 }
393
dispatchNullEvent(nsecs_t,PhysicalDisplayId)394 void Choreographer::dispatchNullEvent(nsecs_t, PhysicalDisplayId) {
395 ALOGV("choreographer %p ~ received null event.", this);
396 handleRefreshRateUpdates();
397 }
398
handleMessage(const Message & message)399 void Choreographer::handleMessage(const Message& message) {
400 switch (message.what) {
401 case MSG_SCHEDULE_CALLBACKS:
402 scheduleCallbacks();
403 break;
404 case MSG_SCHEDULE_VSYNC:
405 scheduleVsync();
406 break;
407 case MSG_HANDLE_REFRESH_RATE_UPDATES:
408 handleRefreshRateUpdates();
409 break;
410 }
411 }
412
getVsyncId() const413 int64_t Choreographer::getVsyncId() const {
414 return mLastVsyncEventData.id;
415 }
416
getFrameDeadline() const417 int64_t Choreographer::getFrameDeadline() const {
418 return mLastVsyncEventData.deadlineTimestamp;
419 }
420
getFrameInterval() const421 int64_t Choreographer::getFrameInterval() const {
422 return mLastVsyncEventData.frameInterval;
423 }
424
425 } // namespace android
426 using namespace android;
427
AChoreographer_to_Choreographer(AChoreographer * choreographer)428 static inline Choreographer* AChoreographer_to_Choreographer(AChoreographer* choreographer) {
429 return reinterpret_cast<Choreographer*>(choreographer);
430 }
431
AChoreographer_to_Choreographer(const AChoreographer * choreographer)432 static inline const Choreographer* AChoreographer_to_Choreographer(
433 const AChoreographer* choreographer) {
434 return reinterpret_cast<const Choreographer*>(choreographer);
435 }
436
437 // Glue for private C api
438 namespace android {
AChoreographer_signalRefreshRateCallbacks(nsecs_t vsyncPeriod)439 void AChoreographer_signalRefreshRateCallbacks(nsecs_t vsyncPeriod) EXCLUDES(gChoreographers.lock) {
440 std::lock_guard<std::mutex> _l(gChoreographers.lock);
441 gChoreographers.mLastKnownVsync.store(vsyncPeriod);
442 for (auto c : gChoreographers.ptrs) {
443 c->scheduleLatestConfigRequest();
444 }
445 }
446
AChoreographer_initJVM(JNIEnv * env)447 void AChoreographer_initJVM(JNIEnv* env) {
448 env->GetJavaVM(&gJni.jvm);
449 // Now we need to find the java classes.
450 jclass dmgClass = env->FindClass("android/hardware/display/DisplayManagerGlobal");
451 gJni.displayManagerGlobal.clazz = static_cast<jclass>(env->NewGlobalRef(dmgClass));
452 gJni.displayManagerGlobal.getInstance =
453 env->GetStaticMethodID(dmgClass, "getInstance",
454 "()Landroid/hardware/display/DisplayManagerGlobal;");
455 gJni.displayManagerGlobal.registerNativeChoreographerForRefreshRateCallbacks =
456 env->GetMethodID(dmgClass, "registerNativeChoreographerForRefreshRateCallbacks", "()V");
457 gJni.displayManagerGlobal.unregisterNativeChoreographerForRefreshRateCallbacks =
458 env->GetMethodID(dmgClass, "unregisterNativeChoreographerForRefreshRateCallbacks",
459 "()V");
460 }
461
AChoreographer_routeGetInstance()462 AChoreographer* AChoreographer_routeGetInstance() {
463 return AChoreographer_getInstance();
464 }
AChoreographer_routePostFrameCallback(AChoreographer * choreographer,AChoreographer_frameCallback callback,void * data)465 void AChoreographer_routePostFrameCallback(AChoreographer* choreographer,
466 AChoreographer_frameCallback callback, void* data) {
467 #pragma clang diagnostic push
468 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
469 return AChoreographer_postFrameCallback(choreographer, callback, data);
470 #pragma clang diagnostic pop
471 }
AChoreographer_routePostFrameCallbackDelayed(AChoreographer * choreographer,AChoreographer_frameCallback callback,void * data,long delayMillis)472 void AChoreographer_routePostFrameCallbackDelayed(AChoreographer* choreographer,
473 AChoreographer_frameCallback callback, void* data,
474 long delayMillis) {
475 #pragma clang diagnostic push
476 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
477 return AChoreographer_postFrameCallbackDelayed(choreographer, callback, data, delayMillis);
478 #pragma clang diagnostic pop
479 }
AChoreographer_routePostFrameCallback64(AChoreographer * choreographer,AChoreographer_frameCallback64 callback,void * data)480 void AChoreographer_routePostFrameCallback64(AChoreographer* choreographer,
481 AChoreographer_frameCallback64 callback, void* data) {
482 return AChoreographer_postFrameCallback64(choreographer, callback, data);
483 }
AChoreographer_routePostFrameCallbackDelayed64(AChoreographer * choreographer,AChoreographer_frameCallback64 callback,void * data,uint32_t delayMillis)484 void AChoreographer_routePostFrameCallbackDelayed64(AChoreographer* choreographer,
485 AChoreographer_frameCallback64 callback,
486 void* data, uint32_t delayMillis) {
487 return AChoreographer_postFrameCallbackDelayed64(choreographer, callback, data, delayMillis);
488 }
AChoreographer_routeRegisterRefreshRateCallback(AChoreographer * choreographer,AChoreographer_refreshRateCallback callback,void * data)489 void AChoreographer_routeRegisterRefreshRateCallback(AChoreographer* choreographer,
490 AChoreographer_refreshRateCallback callback,
491 void* data) {
492 return AChoreographer_registerRefreshRateCallback(choreographer, callback, data);
493 }
AChoreographer_routeUnregisterRefreshRateCallback(AChoreographer * choreographer,AChoreographer_refreshRateCallback callback,void * data)494 void AChoreographer_routeUnregisterRefreshRateCallback(AChoreographer* choreographer,
495 AChoreographer_refreshRateCallback callback,
496 void* data) {
497 return AChoreographer_unregisterRefreshRateCallback(choreographer, callback, data);
498 }
499
AChoreographer_getVsyncId(const AChoreographer * choreographer)500 int64_t AChoreographer_getVsyncId(const AChoreographer* choreographer) {
501 return AChoreographer_to_Choreographer(choreographer)->getVsyncId();
502 }
503
AChoreographer_getFrameDeadline(const AChoreographer * choreographer)504 int64_t AChoreographer_getFrameDeadline(const AChoreographer* choreographer) {
505 return AChoreographer_to_Choreographer(choreographer)->getFrameDeadline();
506 }
507
AChoreographer_getFrameInterval(const AChoreographer * choreographer)508 int64_t AChoreographer_getFrameInterval(const AChoreographer* choreographer) {
509 return AChoreographer_to_Choreographer(choreographer)->getFrameInterval();
510 }
511
512 } // namespace android
513
514 /* Glue for the NDK interface */
515
Choreographer_to_AChoreographer(Choreographer * choreographer)516 static inline AChoreographer* Choreographer_to_AChoreographer(Choreographer* choreographer) {
517 return reinterpret_cast<AChoreographer*>(choreographer);
518 }
519
AChoreographer_getInstance()520 AChoreographer* AChoreographer_getInstance() {
521 return Choreographer_to_AChoreographer(Choreographer::getForThread());
522 }
523
AChoreographer_postFrameCallback(AChoreographer * choreographer,AChoreographer_frameCallback callback,void * data)524 void AChoreographer_postFrameCallback(AChoreographer* choreographer,
525 AChoreographer_frameCallback callback, void* data) {
526 AChoreographer_to_Choreographer(choreographer)->postFrameCallbackDelayed(
527 callback, nullptr, data, 0);
528 }
AChoreographer_postFrameCallbackDelayed(AChoreographer * choreographer,AChoreographer_frameCallback callback,void * data,long delayMillis)529 void AChoreographer_postFrameCallbackDelayed(AChoreographer* choreographer,
530 AChoreographer_frameCallback callback, void* data, long delayMillis) {
531 AChoreographer_to_Choreographer(choreographer)->postFrameCallbackDelayed(
532 callback, nullptr, data, ms2ns(delayMillis));
533 }
AChoreographer_postFrameCallback64(AChoreographer * choreographer,AChoreographer_frameCallback64 callback,void * data)534 void AChoreographer_postFrameCallback64(AChoreographer* choreographer,
535 AChoreographer_frameCallback64 callback, void* data) {
536 AChoreographer_to_Choreographer(choreographer)->postFrameCallbackDelayed(
537 nullptr, callback, data, 0);
538 }
AChoreographer_postFrameCallbackDelayed64(AChoreographer * choreographer,AChoreographer_frameCallback64 callback,void * data,uint32_t delayMillis)539 void AChoreographer_postFrameCallbackDelayed64(AChoreographer* choreographer,
540 AChoreographer_frameCallback64 callback, void* data, uint32_t delayMillis) {
541 AChoreographer_to_Choreographer(choreographer)->postFrameCallbackDelayed(
542 nullptr, callback, data, ms2ns(delayMillis));
543 }
AChoreographer_registerRefreshRateCallback(AChoreographer * choreographer,AChoreographer_refreshRateCallback callback,void * data)544 void AChoreographer_registerRefreshRateCallback(AChoreographer* choreographer,
545 AChoreographer_refreshRateCallback callback,
546 void* data) {
547 AChoreographer_to_Choreographer(choreographer)->registerRefreshRateCallback(callback, data);
548 }
AChoreographer_unregisterRefreshRateCallback(AChoreographer * choreographer,AChoreographer_refreshRateCallback callback,void * data)549 void AChoreographer_unregisterRefreshRateCallback(AChoreographer* choreographer,
550 AChoreographer_refreshRateCallback callback,
551 void* data) {
552 AChoreographer_to_Choreographer(choreographer)->unregisterRefreshRateCallback(callback, data);
553 }
554
AChoreographer_create()555 AChoreographer* AChoreographer_create() {
556 Choreographer* choreographer = new Choreographer(nullptr);
557 status_t result = choreographer->initialize();
558 if (result != OK) {
559 ALOGW("Failed to initialize");
560 return nullptr;
561 }
562 return Choreographer_to_AChoreographer(choreographer);
563 }
564
AChoreographer_destroy(AChoreographer * choreographer)565 void AChoreographer_destroy(AChoreographer* choreographer) {
566 if (choreographer == nullptr) {
567 return;
568 }
569
570 delete AChoreographer_to_Choreographer(choreographer);
571 }
572
AChoreographer_getFd(const AChoreographer * choreographer)573 int AChoreographer_getFd(const AChoreographer* choreographer) {
574 return AChoreographer_to_Choreographer(choreographer)->getFd();
575 }
576
AChoreographer_handlePendingEvents(AChoreographer * choreographer,void * data)577 void AChoreographer_handlePendingEvents(AChoreographer* choreographer, void* data) {
578 // Pass dummy fd and events args to handleEvent, since the underlying
579 // DisplayEventDispatcher doesn't need them outside of validating that a
580 // Looper instance didn't break, but these args circumvent those checks.
581 Choreographer* impl = AChoreographer_to_Choreographer(choreographer);
582 impl->handleEvent(-1, Looper::EVENT_INPUT, data);
583 }
584