blob: ea51245ac62bd0cb212a0ced6e550c8bd106d54a [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();
253 jobject dmg = env->CallStaticObjectMethod(gJni.displayManagerGlobal.clazz,
254 gJni.displayManagerGlobal.getInstance);
255 if (env == nullptr) {
256 ALOGW("JNI environment is unavailable, skipping registeration");
257 return;
258 }
259 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) {
366 ALOGV("choreographer %p ~ received hotplug event (displayId=%"
367 ANDROID_PHYSICAL_DISPLAY_ID_FORMAT ", connected=%s), ignoring.",
368 this, displayId, toString(connected));
369}
370
Alec Mouri60aee1c2019-10-28 16:18:59 -0700371// TODO(b/74619554): The PhysicalDisplayId is ignored because currently
372// Choreographer only supports dispatching VSYNC events for the internal
373// display, so as such Choreographer does not support the notion of multiple
374// displays. When multi-display choreographer is properly supported, then
375// PhysicalDisplayId should no longer be ignored.
Alec Mouri271de042020-04-27 22:38:19 -0700376void Choreographer::dispatchConfigChanged(nsecs_t, PhysicalDisplayId displayId, int32_t configId,
Alec Mouri60aee1c2019-10-28 16:18:59 -0700377 nsecs_t vsyncPeriod) {
Alec Mouri271de042020-04-27 22:38:19 -0700378 ALOGV("choreographer %p ~ received config change event "
379 "(displayId=%" ANDROID_PHYSICAL_DISPLAY_ID_FORMAT ", configId=%d).",
380 this, displayId, configId);
381
382 const nsecs_t lastPeriod = mLatestVsyncPeriod;
383 std::vector<RefreshRateCallback> callbacks{};
Alec Mouri60aee1c2019-10-28 16:18:59 -0700384 {
Alec Mouri271de042020-04-27 22:38:19 -0700385 std::lock_guard<std::mutex> _l{mLock};
386 for (auto& cb : mRefreshRateCallbacks) {
387 callbacks.push_back(cb);
388 cb.firstCallbackFired = true;
Alec Mouri60aee1c2019-10-28 16:18:59 -0700389 }
390 }
Alec Mouri271de042020-04-27 22:38:19 -0700391
392 for (auto& cb : callbacks) {
393 if (!cb.firstCallbackFired || (vsyncPeriod > 0 && vsyncPeriod != lastPeriod)) {
394 cb.callback(vsyncPeriod, cb.data);
395 }
396 }
397
398 mLatestVsyncPeriod = vsyncPeriod;
Alec Mouricc445222019-10-22 10:19:17 -0700399}
400
401void Choreographer::handleMessage(const Message& message) {
402 switch (message.what) {
403 case MSG_SCHEDULE_CALLBACKS:
404 scheduleCallbacks();
405 break;
406 case MSG_SCHEDULE_VSYNC:
407 scheduleVsync();
408 break;
Alec Mouri271de042020-04-27 22:38:19 -0700409 case MSG_HANDLE_REFRESH_RATE_UPDATES:
410 handleRefreshRateUpdates();
411 break;
Alec Mouricc445222019-10-22 10:19:17 -0700412 }
413}
414
Alec Mouri271de042020-04-27 22:38:19 -0700415} // namespace android
Alec Mouri50a931d2019-11-19 16:23:59 -0800416using namespace android;
Alec Mouricc445222019-10-22 10:19:17 -0700417
418static inline Choreographer* AChoreographer_to_Choreographer(AChoreographer* choreographer) {
419 return reinterpret_cast<Choreographer*>(choreographer);
420}
421
Alec Mouri271de042020-04-27 22:38:19 -0700422// Glue for private C api
423namespace android {
424void AChoreographer_signalRefreshRateCallbacks(nsecs_t vsyncPeriod) EXCLUDES(gChoreographers.lock) {
425 std::lock_guard<std::mutex> _l(gChoreographers.lock);
426 gChoreographers.mLastKnownVsync.store(vsyncPeriod);
427 for (auto c : gChoreographers.ptrs) {
428 c->scheduleLatestConfigRequest();
429 }
430}
431
432void AChoreographer_initJVM(JNIEnv* env) {
433 env->GetJavaVM(&gJni.jvm);
434 // Now we need to find the java classes.
435 jclass dmgClass = env->FindClass("android/hardware/display/DisplayManagerGlobal");
436 gJni.displayManagerGlobal.clazz = static_cast<jclass>(env->NewGlobalRef(dmgClass));
437 gJni.displayManagerGlobal.getInstance =
438 env->GetStaticMethodID(dmgClass, "getInstance",
439 "()Landroid/hardware/display/DisplayManagerGlobal;");
440 gJni.displayManagerGlobal.registerNativeChoreographerForRefreshRateCallbacks =
441 env->GetMethodID(dmgClass, "registerNativeChoreographerForRefreshRateCallbacks", "()V");
442 gJni.displayManagerGlobal.unregisterNativeChoreographerForRefreshRateCallbacks =
443 env->GetMethodID(dmgClass, "unregisterNativeChoreographerForRefreshRateCallbacks",
444 "()V");
445}
446
447AChoreographer* AChoreographer_routeGetInstance() {
448 return AChoreographer_getInstance();
449}
450void AChoreographer_routePostFrameCallback(AChoreographer* choreographer,
451 AChoreographer_frameCallback callback, void* data) {
452 return AChoreographer_postFrameCallback(choreographer, callback, data);
453}
454void AChoreographer_routePostFrameCallbackDelayed(AChoreographer* choreographer,
455 AChoreographer_frameCallback callback, void* data,
456 long delayMillis) {
457 return AChoreographer_postFrameCallbackDelayed(choreographer, callback, data, delayMillis);
458}
459void AChoreographer_routePostFrameCallback64(AChoreographer* choreographer,
460 AChoreographer_frameCallback64 callback, void* data) {
461 return AChoreographer_postFrameCallback64(choreographer, callback, data);
462}
463void AChoreographer_routePostFrameCallbackDelayed64(AChoreographer* choreographer,
464 AChoreographer_frameCallback64 callback,
465 void* data, uint32_t delayMillis) {
466 return AChoreographer_postFrameCallbackDelayed64(choreographer, callback, data, delayMillis);
467}
468void AChoreographer_routeRegisterRefreshRateCallback(AChoreographer* choreographer,
469 AChoreographer_refreshRateCallback callback,
470 void* data) {
471 return AChoreographer_registerRefreshRateCallback(choreographer, callback, data);
472}
473void AChoreographer_routeUnregisterRefreshRateCallback(AChoreographer* choreographer,
474 AChoreographer_refreshRateCallback callback,
475 void* data) {
476 return AChoreographer_unregisterRefreshRateCallback(choreographer, callback, data);
477}
478
479} // namespace android
480
481/* Glue for the NDK interface */
482
Alec Mouri921b2772020-02-05 19:03:28 -0800483static inline const Choreographer* AChoreographer_to_Choreographer(
484 const AChoreographer* choreographer) {
485 return reinterpret_cast<const Choreographer*>(choreographer);
486}
487
Alec Mouricc445222019-10-22 10:19:17 -0700488static inline AChoreographer* Choreographer_to_AChoreographer(Choreographer* choreographer) {
489 return reinterpret_cast<AChoreographer*>(choreographer);
490}
491
492AChoreographer* AChoreographer_getInstance() {
493 return Choreographer_to_AChoreographer(Choreographer::getForThread());
494}
495
496void AChoreographer_postFrameCallback(AChoreographer* choreographer,
497 AChoreographer_frameCallback callback, void* data) {
498 AChoreographer_to_Choreographer(choreographer)->postFrameCallbackDelayed(
499 callback, nullptr, data, 0);
500}
501void AChoreographer_postFrameCallbackDelayed(AChoreographer* choreographer,
502 AChoreographer_frameCallback callback, void* data, long delayMillis) {
503 AChoreographer_to_Choreographer(choreographer)->postFrameCallbackDelayed(
504 callback, nullptr, data, ms2ns(delayMillis));
505}
506void AChoreographer_postFrameCallback64(AChoreographer* choreographer,
507 AChoreographer_frameCallback64 callback, void* data) {
508 AChoreographer_to_Choreographer(choreographer)->postFrameCallbackDelayed(
509 nullptr, callback, data, 0);
510}
511void AChoreographer_postFrameCallbackDelayed64(AChoreographer* choreographer,
512 AChoreographer_frameCallback64 callback, void* data, uint32_t delayMillis) {
513 AChoreographer_to_Choreographer(choreographer)->postFrameCallbackDelayed(
514 nullptr, callback, data, ms2ns(delayMillis));
515}
Alec Mouri60aee1c2019-10-28 16:18:59 -0700516void AChoreographer_registerRefreshRateCallback(AChoreographer* choreographer,
517 AChoreographer_refreshRateCallback callback,
518 void* data) {
519 AChoreographer_to_Choreographer(choreographer)->registerRefreshRateCallback(callback, data);
520}
521void AChoreographer_unregisterRefreshRateCallback(AChoreographer* choreographer,
Alec Mouri33682e92020-01-10 15:11:15 -0800522 AChoreographer_refreshRateCallback callback,
523 void* data) {
524 AChoreographer_to_Choreographer(choreographer)->unregisterRefreshRateCallback(callback, data);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700525}
Alec Mouri50a931d2019-11-19 16:23:59 -0800526
527AChoreographer* AChoreographer_create() {
528 Choreographer* choreographer = new Choreographer(nullptr);
529 status_t result = choreographer->initialize();
530 if (result != OK) {
531 ALOGW("Failed to initialize");
532 return nullptr;
533 }
534 return Choreographer_to_AChoreographer(choreographer);
535}
536
537void AChoreographer_destroy(AChoreographer* choreographer) {
538 if (choreographer == nullptr) {
539 return;
540 }
541
542 delete AChoreographer_to_Choreographer(choreographer);
543}
544
Alec Mouri921b2772020-02-05 19:03:28 -0800545int AChoreographer_getFd(const AChoreographer* choreographer) {
Alec Mouri50a931d2019-11-19 16:23:59 -0800546 return AChoreographer_to_Choreographer(choreographer)->getFd();
547}
548
549void AChoreographer_handlePendingEvents(AChoreographer* choreographer, void* data) {
550 // Pass dummy fd and events args to handleEvent, since the underlying
551 // DisplayEventDispatcher doesn't need them outside of validating that a
552 // Looper instance didn't break, but these args circumvent those checks.
Alec Mouri271de042020-04-27 22:38:19 -0700553 Choreographer* impl = AChoreographer_to_Choreographer(choreographer);
554 impl->handleEvent(-1, Looper::EVENT_INPUT, data);
Alec Mouri50a931d2019-11-19 16:23:59 -0800555}