blob: ebc8909179fbca566852b82b5503fd39a2bd8527 [file] [log] [blame]
Alec Mouricc445222019-10-22 10:19:17 -07001/*
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
Alec Mouri271de042020-04-27 22:38:19 -070020#include <android-base/thread_annotations.h>
Alec Mouri77a53872019-10-28 16:44:36 -070021#include <gui/DisplayEventDispatcher.h>
Alec Mouricc445222019-10-22 10:19:17 -070022#include <gui/ISurfaceComposer.h>
23#include <gui/SurfaceComposerClient.h>
Alec Mouri271de042020-04-27 22:38:19 -070024#include <nativehelper/JNIHelp.h>
25#include <private/android/choreographer.h>
Alec Mouricc445222019-10-22 10:19:17 -070026#include <utils/Looper.h>
Alec Mouricc445222019-10-22 10:19:17 -070027#include <utils/Timers.h>
28
Alec Mouri60aee1c2019-10-28 16:18:59 -070029#include <cinttypes>
Alec Mouri271de042020-04-27 22:38:19 -070030#include <mutex>
Alec Mouri60aee1c2019-10-28 16:18:59 -070031#include <optional>
32#include <queue>
33#include <thread>
34
Alec Mouri271de042020-04-27 22:38:19 -070035namespace {
36struct {
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;
Alec Mouricc445222019-10-22 10:19:17 -070046
Alec Mouri271de042020-04-27 22:38:19 -070047// Gets the JNIEnv* for this thread, and performs one-off initialization if we
48// have never retrieved a JNIEnv* pointer before.
49JNIEnv* 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
71inline const char* toString(bool value) {
Alec Mouricc445222019-10-22 10:19:17 -070072 return value ? "true" : "false";
73}
Alec Mouri271de042020-04-27 22:38:19 -070074} // namespace
75
76namespace android {
Alec Mouricc445222019-10-22 10:19:17 -070077
78struct FrameCallback {
79 AChoreographer_frameCallback callback;
80 AChoreographer_frameCallback64 callback64;
81 void* data;
82 nsecs_t dueTime;
83
84 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
Alec Mouri60aee1c2019-10-28 16:18:59 -070091struct RefreshRateCallback {
92 AChoreographer_refreshRateCallback callback;
93 void* data;
Alec Mouri271de042020-04-27 22:38:19 -070094 bool firstCallbackFired = false;
Alec Mouri60aee1c2019-10-28 16:18:59 -070095};
Alec Mouricc445222019-10-22 10:19:17 -070096
Alec Mouri271de042020-04-27 22:38:19 -070097class Choreographer;
98
99struct {
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
Alec Mouricc445222019-10-22 10:19:17 -0700107class Choreographer : public DisplayEventDispatcher, public MessageHandler {
108public:
Alec Mouri271de042020-04-27 22:38:19 -0700109 explicit Choreographer(const sp<Looper>& looper) EXCLUDES(gChoreographers.lock);
Alec Mouricc445222019-10-22 10:19:17 -0700110 void postFrameCallbackDelayed(AChoreographer_frameCallback cb,
111 AChoreographer_frameCallback64 cb64, void* data, nsecs_t delay);
Alec Mouri271de042020-04-27 22:38:19 -0700112 void registerRefreshRateCallback(AChoreographer_refreshRateCallback cb, void* data)
113 EXCLUDES(gChoreographers.lock);
Alec Mouri33682e92020-01-10 15:11:15 -0800114 void unregisterRefreshRateCallback(AChoreographer_refreshRateCallback cb, void* data);
Alec Mouri271de042020-04-27 22:38:19 -0700115 // 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();
Alec Mouricc445222019-10-22 10:19:17 -0700121
122 enum {
123 MSG_SCHEDULE_CALLBACKS = 0,
Alec Mouri271de042020-04-27 22:38:19 -0700124 MSG_SCHEDULE_VSYNC = 1,
125 MSG_HANDLE_REFRESH_RATE_UPDATES = 2,
Alec Mouricc445222019-10-22 10:19:17 -0700126 };
127 virtual void handleMessage(const Message& message) override;
128
129 static Choreographer* getForThread();
Alec Mouri271de042020-04-27 22:38:19 -0700130 virtual ~Choreographer() override EXCLUDES(gChoreographers.lock);
Alec Mouricc445222019-10-22 10:19:17 -0700131
132private:
Alec Mouricc445222019-10-22 10:19:17 -0700133 Choreographer(const Choreographer&) = delete;
134
135 void dispatchVsync(nsecs_t timestamp, PhysicalDisplayId displayId, uint32_t count) override;
136 void dispatchHotplug(nsecs_t timestamp, PhysicalDisplayId displayId, bool connected) override;
Alec Mouri60aee1c2019-10-28 16:18:59 -0700137 void dispatchConfigChanged(nsecs_t timestamp, PhysicalDisplayId displayId, int32_t configId,
138 nsecs_t vsyncPeriod) override;
Alec Mouricc445222019-10-22 10:19:17 -0700139
140 void scheduleCallbacks();
141
Alec Mouri271de042020-04-27 22:38:19 -0700142 std::mutex mLock;
Alec Mouricc445222019-10-22 10:19:17 -0700143 // Protected by mLock
Alec Mouri60aee1c2019-10-28 16:18:59 -0700144 std::priority_queue<FrameCallback> mFrameCallbacks;
Alec Mouri60aee1c2019-10-28 16:18:59 -0700145 std::vector<RefreshRateCallback> mRefreshRateCallbacks;
Alec Mouricc445222019-10-22 10:19:17 -0700146
Alec Mouri271de042020-04-27 22:38:19 -0700147 nsecs_t mLatestVsyncPeriod = -1;
Alec Mouricc445222019-10-22 10:19:17 -0700148
149 const sp<Looper> mLooper;
150 const std::thread::id mThreadId;
151};
152
Alec Mouricc445222019-10-22 10:19:17 -0700153static thread_local Choreographer* gChoreographer;
154Choreographer* Choreographer::getForThread() {
155 if (gChoreographer == nullptr) {
156 sp<Looper> looper = Looper::getForThread();
157 if (!looper.get()) {
158 ALOGW("No looper prepared for thread");
159 return nullptr;
160 }
161 gChoreographer = new Choreographer(looper);
162 status_t result = gChoreographer->initialize();
163 if (result != OK) {
164 ALOGW("Failed to initialize");
165 return nullptr;
166 }
167 }
168 return gChoreographer;
169}
170
Alec Mouri60aee1c2019-10-28 16:18:59 -0700171Choreographer::Choreographer(const sp<Looper>& looper)
Alec Mouri271de042020-04-27 22:38:19 -0700172 : DisplayEventDispatcher(looper, ISurfaceComposer::VsyncSource::eVsyncSourceApp,
173 ISurfaceComposer::ConfigChanged::eConfigChangedDispatch),
Alec Mouri60aee1c2019-10-28 16:18:59 -0700174 mLooper(looper),
Alec Mouri271de042020-04-27 22:38:19 -0700175 mThreadId(std::this_thread::get_id()) {
176 std::lock_guard<std::mutex> _l(gChoreographers.lock);
177 gChoreographers.ptrs.push_back(this);
178}
179
180Choreographer::~Choreographer() {
181 std::lock_guard<std::mutex> _l(gChoreographers.lock);
182 gChoreographers.ptrs.erase(std::remove_if(gChoreographers.ptrs.begin(),
183 gChoreographers.ptrs.end(),
184 [=](Choreographer* c) { return c == this; }),
185 gChoreographers.ptrs.end());
186 // Only poke DisplayManagerGlobal to unregister if we previously registered
187 // callbacks.
188 if (gChoreographers.ptrs.empty() && gChoreographers.registeredToDisplayManager) {
189 JNIEnv* env = getJniEnv();
190 if (env == nullptr) {
191 ALOGW("JNI environment is unavailable, skipping choreographer cleanup");
192 return;
193 }
194 jobject dmg = env->CallStaticObjectMethod(gJni.displayManagerGlobal.clazz,
195 gJni.displayManagerGlobal.getInstance);
196 if (dmg == nullptr) {
197 ALOGW("DMS is not initialized yet, skipping choreographer cleanup");
198 } else {
199 env->CallVoidMethod(dmg,
200 gJni.displayManagerGlobal
201 .unregisterNativeChoreographerForRefreshRateCallbacks);
202 env->DeleteLocalRef(dmg);
203 }
204 }
205}
Alec Mouricc445222019-10-22 10:19:17 -0700206
207void Choreographer::postFrameCallbackDelayed(
208 AChoreographer_frameCallback cb, AChoreographer_frameCallback64 cb64, void* data, nsecs_t delay) {
209 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
210 FrameCallback callback{cb, cb64, data, now + delay};
211 {
Alec Mouri271de042020-04-27 22:38:19 -0700212 std::lock_guard<std::mutex> _l{mLock};
Alec Mouri60aee1c2019-10-28 16:18:59 -0700213 mFrameCallbacks.push(callback);
Alec Mouricc445222019-10-22 10:19:17 -0700214 }
215 if (callback.dueTime <= now) {
216 if (std::this_thread::get_id() != mThreadId) {
Alec Mouri50a931d2019-11-19 16:23:59 -0800217 if (mLooper != nullptr) {
218 Message m{MSG_SCHEDULE_VSYNC};
219 mLooper->sendMessage(this, m);
220 } else {
221 scheduleVsync();
222 }
Alec Mouricc445222019-10-22 10:19:17 -0700223 } else {
224 scheduleVsync();
225 }
226 } else {
Alec Mouri50a931d2019-11-19 16:23:59 -0800227 if (mLooper != nullptr) {
228 Message m{MSG_SCHEDULE_CALLBACKS};
229 mLooper->sendMessageDelayed(delay, this, m);
230 } else {
231 scheduleCallbacks();
232 }
Alec Mouricc445222019-10-22 10:19:17 -0700233 }
234}
235
Alec Mouri60aee1c2019-10-28 16:18:59 -0700236void Choreographer::registerRefreshRateCallback(AChoreographer_refreshRateCallback cb, void* data) {
Alec Mouri271de042020-04-27 22:38:19 -0700237 std::lock_guard<std::mutex> _l{mLock};
238 for (const auto& callback : mRefreshRateCallbacks) {
239 // Don't re-add callbacks.
240 if (cb == callback.callback && data == callback.data) {
241 return;
242 }
243 }
244 mRefreshRateCallbacks.emplace_back(
245 RefreshRateCallback{.callback = cb, .data = data, .firstCallbackFired = false});
246 bool needsRegistration = false;
Alec Mouri60aee1c2019-10-28 16:18:59 -0700247 {
Alec Mouri271de042020-04-27 22:38:19 -0700248 std::lock_guard<std::mutex> _l2(gChoreographers.lock);
249 needsRegistration = !gChoreographers.registeredToDisplayManager;
250 }
251 if (needsRegistration) {
252 JNIEnv* env = getJniEnv();
Alec Mouri271de042020-04-27 22:38:19 -0700253 if (env == nullptr) {
Greg Kaiserb66d04b2020-05-11 06:52:15 -0700254 ALOGW("JNI environment is unavailable, skipping registration");
Alec Mouri271de042020-04-27 22:38:19 -0700255 return;
256 }
Greg Kaiserb66d04b2020-05-11 06:52:15 -0700257 jobject dmg = env->CallStaticObjectMethod(gJni.displayManagerGlobal.clazz,
258 gJni.displayManagerGlobal.getInstance);
Alec Mouri271de042020-04-27 22:38:19 -0700259 if (dmg == nullptr) {
260 ALOGW("DMS is not initialized yet: skipping registration");
261 return;
262 } else {
263 env->CallVoidMethod(dmg,
264 gJni.displayManagerGlobal
265 .registerNativeChoreographerForRefreshRateCallbacks,
266 reinterpret_cast<int64_t>(this));
267 env->DeleteLocalRef(dmg);
268 {
269 std::lock_guard<std::mutex> _l2(gChoreographers.lock);
270 gChoreographers.registeredToDisplayManager = true;
Alec Mouri33682e92020-01-10 15:11:15 -0800271 }
272 }
Alec Mouri271de042020-04-27 22:38:19 -0700273 } else {
274 scheduleLatestConfigRequest();
Alec Mouri60aee1c2019-10-28 16:18:59 -0700275 }
276}
277
Alec Mouri33682e92020-01-10 15:11:15 -0800278void Choreographer::unregisterRefreshRateCallback(AChoreographer_refreshRateCallback cb,
279 void* data) {
Alec Mouri271de042020-04-27 22:38:19 -0700280 std::lock_guard<std::mutex> _l{mLock};
281 mRefreshRateCallbacks.erase(std::remove_if(mRefreshRateCallbacks.begin(),
282 mRefreshRateCallbacks.end(),
283 [&](const RefreshRateCallback& callback) {
284 return cb == callback.callback &&
285 data == callback.data;
286 }),
287 mRefreshRateCallbacks.end());
288}
289
290void Choreographer::scheduleLatestConfigRequest() {
291 if (mLooper != nullptr) {
292 Message m{MSG_HANDLE_REFRESH_RATE_UPDATES};
293 mLooper->sendMessage(this, m);
294 } else {
295 // If the looper thread is detached from Choreographer, then refresh rate
296 // changes will be handled in AChoreographer_handlePendingEvents, so we
297 // need to redispatch a config from SF
298 requestLatestConfig();
Alec Mouri60aee1c2019-10-28 16:18:59 -0700299 }
300}
301
Alec Mouricc445222019-10-22 10:19:17 -0700302void Choreographer::scheduleCallbacks() {
Alec Mouri134266b2020-03-03 19:22:29 -0800303 const nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
304 nsecs_t dueTime;
305 {
Alec Mouri271de042020-04-27 22:38:19 -0700306 std::lock_guard<std::mutex> _l{mLock};
Alec Mouri134266b2020-03-03 19:22:29 -0800307 // If there are no pending callbacks then don't schedule a vsync
308 if (mFrameCallbacks.empty()) {
309 return;
310 }
311 dueTime = mFrameCallbacks.top().dueTime;
312 }
313
314 if (dueTime <= now) {
Alec Mouricc445222019-10-22 10:19:17 -0700315 ALOGV("choreographer %p ~ scheduling vsync", this);
316 scheduleVsync();
317 return;
318 }
319}
320
Alec Mouri271de042020-04-27 22:38:19 -0700321void Choreographer::handleRefreshRateUpdates() {
322 std::vector<RefreshRateCallback> callbacks{};
323 const nsecs_t pendingPeriod = gChoreographers.mLastKnownVsync.load();
324 const nsecs_t lastPeriod = mLatestVsyncPeriod;
325 if (pendingPeriod > 0) {
326 mLatestVsyncPeriod = pendingPeriod;
327 }
328 {
329 std::lock_guard<std::mutex> _l{mLock};
330 for (auto& cb : mRefreshRateCallbacks) {
331 callbacks.push_back(cb);
332 cb.firstCallbackFired = true;
333 }
334 }
335
336 for (auto& cb : callbacks) {
337 if (!cb.firstCallbackFired || (pendingPeriod > 0 && pendingPeriod != lastPeriod)) {
338 cb.callback(pendingPeriod, cb.data);
339 }
340 }
341}
342
Alec Mouricc445222019-10-22 10:19:17 -0700343// TODO(b/74619554): The PhysicalDisplayId is ignored because SF only emits VSYNC events for the
344// internal display and DisplayEventReceiver::requestNextVsync only allows requesting VSYNC for
345// the internal display implicitly.
346void Choreographer::dispatchVsync(nsecs_t timestamp, PhysicalDisplayId, uint32_t) {
347 std::vector<FrameCallback> callbacks{};
348 {
Alec Mouri271de042020-04-27 22:38:19 -0700349 std::lock_guard<std::mutex> _l{mLock};
Alec Mouricc445222019-10-22 10:19:17 -0700350 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700351 while (!mFrameCallbacks.empty() && mFrameCallbacks.top().dueTime < now) {
352 callbacks.push_back(mFrameCallbacks.top());
353 mFrameCallbacks.pop();
Alec Mouricc445222019-10-22 10:19:17 -0700354 }
355 }
356 for (const auto& cb : callbacks) {
357 if (cb.callback64 != nullptr) {
358 cb.callback64(timestamp, cb.data);
359 } else if (cb.callback != nullptr) {
360 cb.callback(timestamp, cb.data);
361 }
362 }
363}
364
365void Choreographer::dispatchHotplug(nsecs_t, PhysicalDisplayId displayId, bool connected) {
Marin Shalamanova524a092020-07-27 21:39:55 +0200366 ALOGV("choreographer %p ~ received hotplug event (displayId=%s, connected=%s), ignoring.",
367 this, to_string(displayId).c_str(), toString(connected));
Alec Mouricc445222019-10-22 10:19:17 -0700368}
369
Alec Mouri60aee1c2019-10-28 16:18:59 -0700370// TODO(b/74619554): The PhysicalDisplayId is ignored because currently
371// Choreographer only supports dispatching VSYNC events for the internal
372// display, so as such Choreographer does not support the notion of multiple
373// displays. When multi-display choreographer is properly supported, then
374// PhysicalDisplayId should no longer be ignored.
Alec Mouri271de042020-04-27 22:38:19 -0700375void Choreographer::dispatchConfigChanged(nsecs_t, PhysicalDisplayId displayId, int32_t configId,
Alec Mouri60aee1c2019-10-28 16:18:59 -0700376 nsecs_t vsyncPeriod) {
Marin Shalamanova524a092020-07-27 21:39:55 +0200377 ALOGV("choreographer %p ~ received config change event (displayId=%s, configId=%d).",
378 this, to_string(displayId).c_str(), configId);
Alec Mouri271de042020-04-27 22:38:19 -0700379
380 const nsecs_t lastPeriod = mLatestVsyncPeriod;
381 std::vector<RefreshRateCallback> callbacks{};
Alec Mouri60aee1c2019-10-28 16:18:59 -0700382 {
Alec Mouri271de042020-04-27 22:38:19 -0700383 std::lock_guard<std::mutex> _l{mLock};
384 for (auto& cb : mRefreshRateCallbacks) {
385 callbacks.push_back(cb);
386 cb.firstCallbackFired = true;
Alec Mouri60aee1c2019-10-28 16:18:59 -0700387 }
388 }
Alec Mouri271de042020-04-27 22:38:19 -0700389
390 for (auto& cb : callbacks) {
391 if (!cb.firstCallbackFired || (vsyncPeriod > 0 && vsyncPeriod != lastPeriod)) {
392 cb.callback(vsyncPeriod, cb.data);
393 }
394 }
395
396 mLatestVsyncPeriod = vsyncPeriod;
Alec Mouricc445222019-10-22 10:19:17 -0700397}
398
399void 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;
Alec Mouri271de042020-04-27 22:38:19 -0700407 case MSG_HANDLE_REFRESH_RATE_UPDATES:
408 handleRefreshRateUpdates();
409 break;
Alec Mouricc445222019-10-22 10:19:17 -0700410 }
411}
412
Alec Mouri271de042020-04-27 22:38:19 -0700413} // namespace android
Alec Mouri50a931d2019-11-19 16:23:59 -0800414using namespace android;
Alec Mouricc445222019-10-22 10:19:17 -0700415
416static inline Choreographer* AChoreographer_to_Choreographer(AChoreographer* choreographer) {
417 return reinterpret_cast<Choreographer*>(choreographer);
418}
419
Alec Mouri271de042020-04-27 22:38:19 -0700420// Glue for private C api
421namespace android {
422void AChoreographer_signalRefreshRateCallbacks(nsecs_t vsyncPeriod) EXCLUDES(gChoreographers.lock) {
423 std::lock_guard<std::mutex> _l(gChoreographers.lock);
424 gChoreographers.mLastKnownVsync.store(vsyncPeriod);
425 for (auto c : gChoreographers.ptrs) {
426 c->scheduleLatestConfigRequest();
427 }
428}
429
430void AChoreographer_initJVM(JNIEnv* env) {
431 env->GetJavaVM(&gJni.jvm);
432 // Now we need to find the java classes.
433 jclass dmgClass = env->FindClass("android/hardware/display/DisplayManagerGlobal");
434 gJni.displayManagerGlobal.clazz = static_cast<jclass>(env->NewGlobalRef(dmgClass));
435 gJni.displayManagerGlobal.getInstance =
436 env->GetStaticMethodID(dmgClass, "getInstance",
437 "()Landroid/hardware/display/DisplayManagerGlobal;");
438 gJni.displayManagerGlobal.registerNativeChoreographerForRefreshRateCallbacks =
439 env->GetMethodID(dmgClass, "registerNativeChoreographerForRefreshRateCallbacks", "()V");
440 gJni.displayManagerGlobal.unregisterNativeChoreographerForRefreshRateCallbacks =
441 env->GetMethodID(dmgClass, "unregisterNativeChoreographerForRefreshRateCallbacks",
442 "()V");
443}
444
445AChoreographer* AChoreographer_routeGetInstance() {
446 return AChoreographer_getInstance();
447}
448void AChoreographer_routePostFrameCallback(AChoreographer* choreographer,
449 AChoreographer_frameCallback callback, void* data) {
450 return AChoreographer_postFrameCallback(choreographer, callback, data);
451}
452void AChoreographer_routePostFrameCallbackDelayed(AChoreographer* choreographer,
453 AChoreographer_frameCallback callback, void* data,
454 long delayMillis) {
455 return AChoreographer_postFrameCallbackDelayed(choreographer, callback, data, delayMillis);
456}
457void AChoreographer_routePostFrameCallback64(AChoreographer* choreographer,
458 AChoreographer_frameCallback64 callback, void* data) {
459 return AChoreographer_postFrameCallback64(choreographer, callback, data);
460}
461void AChoreographer_routePostFrameCallbackDelayed64(AChoreographer* choreographer,
462 AChoreographer_frameCallback64 callback,
463 void* data, uint32_t delayMillis) {
464 return AChoreographer_postFrameCallbackDelayed64(choreographer, callback, data, delayMillis);
465}
466void AChoreographer_routeRegisterRefreshRateCallback(AChoreographer* choreographer,
467 AChoreographer_refreshRateCallback callback,
468 void* data) {
469 return AChoreographer_registerRefreshRateCallback(choreographer, callback, data);
470}
471void AChoreographer_routeUnregisterRefreshRateCallback(AChoreographer* choreographer,
472 AChoreographer_refreshRateCallback callback,
473 void* data) {
474 return AChoreographer_unregisterRefreshRateCallback(choreographer, callback, data);
475}
476
477} // namespace android
478
479/* Glue for the NDK interface */
480
Alec Mouri921b2772020-02-05 19:03:28 -0800481static inline const Choreographer* AChoreographer_to_Choreographer(
482 const AChoreographer* choreographer) {
483 return reinterpret_cast<const Choreographer*>(choreographer);
484}
485
Alec Mouricc445222019-10-22 10:19:17 -0700486static inline AChoreographer* Choreographer_to_AChoreographer(Choreographer* choreographer) {
487 return reinterpret_cast<AChoreographer*>(choreographer);
488}
489
490AChoreographer* AChoreographer_getInstance() {
491 return Choreographer_to_AChoreographer(Choreographer::getForThread());
492}
493
494void AChoreographer_postFrameCallback(AChoreographer* choreographer,
495 AChoreographer_frameCallback callback, void* data) {
496 AChoreographer_to_Choreographer(choreographer)->postFrameCallbackDelayed(
497 callback, nullptr, data, 0);
498}
499void AChoreographer_postFrameCallbackDelayed(AChoreographer* choreographer,
500 AChoreographer_frameCallback callback, void* data, long delayMillis) {
501 AChoreographer_to_Choreographer(choreographer)->postFrameCallbackDelayed(
502 callback, nullptr, data, ms2ns(delayMillis));
503}
504void AChoreographer_postFrameCallback64(AChoreographer* choreographer,
505 AChoreographer_frameCallback64 callback, void* data) {
506 AChoreographer_to_Choreographer(choreographer)->postFrameCallbackDelayed(
507 nullptr, callback, data, 0);
508}
509void AChoreographer_postFrameCallbackDelayed64(AChoreographer* choreographer,
510 AChoreographer_frameCallback64 callback, void* data, uint32_t delayMillis) {
511 AChoreographer_to_Choreographer(choreographer)->postFrameCallbackDelayed(
512 nullptr, callback, data, ms2ns(delayMillis));
513}
Alec Mouri60aee1c2019-10-28 16:18:59 -0700514void AChoreographer_registerRefreshRateCallback(AChoreographer* choreographer,
515 AChoreographer_refreshRateCallback callback,
516 void* data) {
517 AChoreographer_to_Choreographer(choreographer)->registerRefreshRateCallback(callback, data);
518}
519void AChoreographer_unregisterRefreshRateCallback(AChoreographer* choreographer,
Alec Mouri33682e92020-01-10 15:11:15 -0800520 AChoreographer_refreshRateCallback callback,
521 void* data) {
522 AChoreographer_to_Choreographer(choreographer)->unregisterRefreshRateCallback(callback, data);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700523}
Alec Mouri50a931d2019-11-19 16:23:59 -0800524
525AChoreographer* AChoreographer_create() {
526 Choreographer* choreographer = new Choreographer(nullptr);
527 status_t result = choreographer->initialize();
528 if (result != OK) {
529 ALOGW("Failed to initialize");
530 return nullptr;
531 }
532 return Choreographer_to_AChoreographer(choreographer);
533}
534
535void AChoreographer_destroy(AChoreographer* choreographer) {
536 if (choreographer == nullptr) {
537 return;
538 }
539
540 delete AChoreographer_to_Choreographer(choreographer);
541}
542
Alec Mouri921b2772020-02-05 19:03:28 -0800543int AChoreographer_getFd(const AChoreographer* choreographer) {
Alec Mouri50a931d2019-11-19 16:23:59 -0800544 return AChoreographer_to_Choreographer(choreographer)->getFd();
545}
546
547void AChoreographer_handlePendingEvents(AChoreographer* choreographer, void* data) {
548 // Pass dummy fd and events args to handleEvent, since the underlying
549 // DisplayEventDispatcher doesn't need them outside of validating that a
550 // Looper instance didn't break, but these args circumvent those checks.
Alec Mouri271de042020-04-27 22:38:19 -0700551 Choreographer* impl = AChoreographer_to_Choreographer(choreographer);
552 impl->handleEvent(-1, Looper::EVENT_INPUT, data);
Alec Mouri50a931d2019-11-19 16:23:59 -0800553}