blob: ff1b5e6d34ff6f89819d28ebe39203e1d6ddee81 [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 Mouri967d5d72020-08-05 12:50:03 -0700139 void dispatchNullEvent(nsecs_t, PhysicalDisplayId) override;
Alec Mouricc445222019-10-22 10:19:17 -0700140
141 void scheduleCallbacks();
142
Alec Mouri271de042020-04-27 22:38:19 -0700143 std::mutex mLock;
Alec Mouricc445222019-10-22 10:19:17 -0700144 // Protected by mLock
Alec Mouri60aee1c2019-10-28 16:18:59 -0700145 std::priority_queue<FrameCallback> mFrameCallbacks;
Alec Mouri60aee1c2019-10-28 16:18:59 -0700146 std::vector<RefreshRateCallback> mRefreshRateCallbacks;
Alec Mouricc445222019-10-22 10:19:17 -0700147
Alec Mouri271de042020-04-27 22:38:19 -0700148 nsecs_t mLatestVsyncPeriod = -1;
Alec Mouricc445222019-10-22 10:19:17 -0700149
150 const sp<Looper> mLooper;
151 const std::thread::id mThreadId;
152};
153
Alec Mouricc445222019-10-22 10:19:17 -0700154static thread_local Choreographer* gChoreographer;
155Choreographer* Choreographer::getForThread() {
156 if (gChoreographer == nullptr) {
157 sp<Looper> looper = Looper::getForThread();
158 if (!looper.get()) {
159 ALOGW("No looper prepared for thread");
160 return nullptr;
161 }
162 gChoreographer = new Choreographer(looper);
163 status_t result = gChoreographer->initialize();
164 if (result != OK) {
165 ALOGW("Failed to initialize");
166 return nullptr;
167 }
168 }
169 return gChoreographer;
170}
171
Alec Mouri60aee1c2019-10-28 16:18:59 -0700172Choreographer::Choreographer(const sp<Looper>& looper)
Alec Mouri271de042020-04-27 22:38:19 -0700173 : DisplayEventDispatcher(looper, ISurfaceComposer::VsyncSource::eVsyncSourceApp,
Alec Mouri967d5d72020-08-05 12:50:03 -0700174 ISurfaceComposer::ConfigChanged::eConfigChangedSuppress),
Alec Mouri60aee1c2019-10-28 16:18:59 -0700175 mLooper(looper),
Alec Mouri271de042020-04-27 22:38:19 -0700176 mThreadId(std::this_thread::get_id()) {
177 std::lock_guard<std::mutex> _l(gChoreographers.lock);
178 gChoreographers.ptrs.push_back(this);
179}
180
181Choreographer::~Choreographer() {
182 std::lock_guard<std::mutex> _l(gChoreographers.lock);
183 gChoreographers.ptrs.erase(std::remove_if(gChoreographers.ptrs.begin(),
184 gChoreographers.ptrs.end(),
185 [=](Choreographer* c) { return c == this; }),
186 gChoreographers.ptrs.end());
187 // Only poke DisplayManagerGlobal to unregister if we previously registered
188 // callbacks.
189 if (gChoreographers.ptrs.empty() && gChoreographers.registeredToDisplayManager) {
190 JNIEnv* env = getJniEnv();
191 if (env == nullptr) {
192 ALOGW("JNI environment is unavailable, skipping choreographer cleanup");
193 return;
194 }
195 jobject dmg = env->CallStaticObjectMethod(gJni.displayManagerGlobal.clazz,
196 gJni.displayManagerGlobal.getInstance);
197 if (dmg == nullptr) {
198 ALOGW("DMS is not initialized yet, skipping choreographer cleanup");
199 } else {
200 env->CallVoidMethod(dmg,
201 gJni.displayManagerGlobal
202 .unregisterNativeChoreographerForRefreshRateCallbacks);
203 env->DeleteLocalRef(dmg);
204 }
205 }
206}
Alec Mouricc445222019-10-22 10:19:17 -0700207
208void Choreographer::postFrameCallbackDelayed(
209 AChoreographer_frameCallback cb, AChoreographer_frameCallback64 cb64, void* data, nsecs_t delay) {
210 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
211 FrameCallback callback{cb, cb64, data, now + delay};
212 {
Alec Mouri271de042020-04-27 22:38:19 -0700213 std::lock_guard<std::mutex> _l{mLock};
Alec Mouri60aee1c2019-10-28 16:18:59 -0700214 mFrameCallbacks.push(callback);
Alec Mouricc445222019-10-22 10:19:17 -0700215 }
216 if (callback.dueTime <= now) {
217 if (std::this_thread::get_id() != mThreadId) {
Alec Mouri50a931d2019-11-19 16:23:59 -0800218 if (mLooper != nullptr) {
219 Message m{MSG_SCHEDULE_VSYNC};
220 mLooper->sendMessage(this, m);
221 } else {
222 scheduleVsync();
223 }
Alec Mouricc445222019-10-22 10:19:17 -0700224 } else {
225 scheduleVsync();
226 }
227 } else {
Alec Mouri50a931d2019-11-19 16:23:59 -0800228 if (mLooper != nullptr) {
229 Message m{MSG_SCHEDULE_CALLBACKS};
230 mLooper->sendMessageDelayed(delay, this, m);
231 } else {
232 scheduleCallbacks();
233 }
Alec Mouricc445222019-10-22 10:19:17 -0700234 }
235}
236
Alec Mouri60aee1c2019-10-28 16:18:59 -0700237void Choreographer::registerRefreshRateCallback(AChoreographer_refreshRateCallback cb, void* data) {
Alec Mouri271de042020-04-27 22:38:19 -0700238 std::lock_guard<std::mutex> _l{mLock};
239 for (const auto& callback : mRefreshRateCallbacks) {
240 // Don't re-add callbacks.
241 if (cb == callback.callback && data == callback.data) {
242 return;
243 }
244 }
245 mRefreshRateCallbacks.emplace_back(
246 RefreshRateCallback{.callback = cb, .data = data, .firstCallbackFired = false});
247 bool needsRegistration = false;
Alec Mouri60aee1c2019-10-28 16:18:59 -0700248 {
Alec Mouri271de042020-04-27 22:38:19 -0700249 std::lock_guard<std::mutex> _l2(gChoreographers.lock);
250 needsRegistration = !gChoreographers.registeredToDisplayManager;
251 }
252 if (needsRegistration) {
253 JNIEnv* env = getJniEnv();
Alec Mouri271de042020-04-27 22:38:19 -0700254 if (env == nullptr) {
Greg Kaiserb66d04b2020-05-11 06:52:15 -0700255 ALOGW("JNI environment is unavailable, skipping registration");
Alec Mouri271de042020-04-27 22:38:19 -0700256 return;
257 }
Greg Kaiserb66d04b2020-05-11 06:52:15 -0700258 jobject dmg = env->CallStaticObjectMethod(gJni.displayManagerGlobal.clazz,
259 gJni.displayManagerGlobal.getInstance);
Alec Mouri271de042020-04-27 22:38:19 -0700260 if (dmg == nullptr) {
261 ALOGW("DMS is not initialized yet: skipping registration");
262 return;
263 } else {
264 env->CallVoidMethod(dmg,
265 gJni.displayManagerGlobal
266 .registerNativeChoreographerForRefreshRateCallbacks,
267 reinterpret_cast<int64_t>(this));
268 env->DeleteLocalRef(dmg);
269 {
270 std::lock_guard<std::mutex> _l2(gChoreographers.lock);
271 gChoreographers.registeredToDisplayManager = true;
Alec Mouri33682e92020-01-10 15:11:15 -0800272 }
273 }
Alec Mouri271de042020-04-27 22:38:19 -0700274 } else {
275 scheduleLatestConfigRequest();
Alec Mouri60aee1c2019-10-28 16:18:59 -0700276 }
277}
278
Alec Mouri33682e92020-01-10 15:11:15 -0800279void Choreographer::unregisterRefreshRateCallback(AChoreographer_refreshRateCallback cb,
280 void* data) {
Alec Mouri271de042020-04-27 22:38:19 -0700281 std::lock_guard<std::mutex> _l{mLock};
282 mRefreshRateCallbacks.erase(std::remove_if(mRefreshRateCallbacks.begin(),
283 mRefreshRateCallbacks.end(),
284 [&](const RefreshRateCallback& callback) {
285 return cb == callback.callback &&
286 data == callback.data;
287 }),
288 mRefreshRateCallbacks.end());
289}
290
291void Choreographer::scheduleLatestConfigRequest() {
292 if (mLooper != nullptr) {
293 Message m{MSG_HANDLE_REFRESH_RATE_UPDATES};
294 mLooper->sendMessage(this, m);
295 } else {
296 // If the looper thread is detached from Choreographer, then refresh rate
297 // changes will be handled in AChoreographer_handlePendingEvents, so we
Alec Mouri967d5d72020-08-05 12:50:03 -0700298 // need to wake up the looper thread by writing to the write-end of the
299 // socket the looper is listening on.
300 // Fortunately, these events are small so sending packets across the
301 // socket should be atomic across processes.
302 DisplayEventReceiver::Event event;
303 event.header = DisplayEventReceiver::Event::Header{DisplayEventReceiver::DISPLAY_EVENT_NULL,
304 PhysicalDisplayId(0), systemTime()};
305 injectEvent(event);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700306 }
307}
308
Alec Mouricc445222019-10-22 10:19:17 -0700309void Choreographer::scheduleCallbacks() {
Alec Mouri134266b2020-03-03 19:22:29 -0800310 const nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
311 nsecs_t dueTime;
312 {
Alec Mouri271de042020-04-27 22:38:19 -0700313 std::lock_guard<std::mutex> _l{mLock};
Alec Mouri134266b2020-03-03 19:22:29 -0800314 // If there are no pending callbacks then don't schedule a vsync
315 if (mFrameCallbacks.empty()) {
316 return;
317 }
318 dueTime = mFrameCallbacks.top().dueTime;
319 }
320
321 if (dueTime <= now) {
Alec Mouricc445222019-10-22 10:19:17 -0700322 ALOGV("choreographer %p ~ scheduling vsync", this);
323 scheduleVsync();
324 return;
325 }
326}
327
Alec Mouri271de042020-04-27 22:38:19 -0700328void Choreographer::handleRefreshRateUpdates() {
329 std::vector<RefreshRateCallback> callbacks{};
330 const nsecs_t pendingPeriod = gChoreographers.mLastKnownVsync.load();
331 const nsecs_t lastPeriod = mLatestVsyncPeriod;
332 if (pendingPeriod > 0) {
333 mLatestVsyncPeriod = pendingPeriod;
334 }
335 {
336 std::lock_guard<std::mutex> _l{mLock};
337 for (auto& cb : mRefreshRateCallbacks) {
338 callbacks.push_back(cb);
339 cb.firstCallbackFired = true;
340 }
341 }
342
343 for (auto& cb : callbacks) {
344 if (!cb.firstCallbackFired || (pendingPeriod > 0 && pendingPeriod != lastPeriod)) {
345 cb.callback(pendingPeriod, cb.data);
346 }
347 }
348}
349
Alec Mouricc445222019-10-22 10:19:17 -0700350// TODO(b/74619554): The PhysicalDisplayId is ignored because SF only emits VSYNC events for the
351// internal display and DisplayEventReceiver::requestNextVsync only allows requesting VSYNC for
352// the internal display implicitly.
353void Choreographer::dispatchVsync(nsecs_t timestamp, PhysicalDisplayId, uint32_t) {
354 std::vector<FrameCallback> callbacks{};
355 {
Alec Mouri271de042020-04-27 22:38:19 -0700356 std::lock_guard<std::mutex> _l{mLock};
Alec Mouricc445222019-10-22 10:19:17 -0700357 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700358 while (!mFrameCallbacks.empty() && mFrameCallbacks.top().dueTime < now) {
359 callbacks.push_back(mFrameCallbacks.top());
360 mFrameCallbacks.pop();
Alec Mouricc445222019-10-22 10:19:17 -0700361 }
362 }
363 for (const auto& cb : callbacks) {
364 if (cb.callback64 != nullptr) {
365 cb.callback64(timestamp, cb.data);
366 } else if (cb.callback != nullptr) {
367 cb.callback(timestamp, cb.data);
368 }
369 }
370}
371
372void Choreographer::dispatchHotplug(nsecs_t, PhysicalDisplayId displayId, bool connected) {
Marin Shalamanova524a092020-07-27 21:39:55 +0200373 ALOGV("choreographer %p ~ received hotplug event (displayId=%s, connected=%s), ignoring.",
374 this, to_string(displayId).c_str(), toString(connected));
Alec Mouricc445222019-10-22 10:19:17 -0700375}
376
Alec Mouri60aee1c2019-10-28 16:18:59 -0700377// TODO(b/74619554): The PhysicalDisplayId is ignored because currently
378// Choreographer only supports dispatching VSYNC events for the internal
379// display, so as such Choreographer does not support the notion of multiple
380// displays. When multi-display choreographer is properly supported, then
381// PhysicalDisplayId should no longer be ignored.
Alec Mouri271de042020-04-27 22:38:19 -0700382void Choreographer::dispatchConfigChanged(nsecs_t, PhysicalDisplayId displayId, int32_t configId,
Alec Mouri967d5d72020-08-05 12:50:03 -0700383 nsecs_t) {
Marin Shalamanova524a092020-07-27 21:39:55 +0200384 ALOGV("choreographer %p ~ received config change event (displayId=%s, configId=%d).",
385 this, to_string(displayId).c_str(), configId);
Alec Mouri967d5d72020-08-05 12:50:03 -0700386}
Alec Mouri271de042020-04-27 22:38:19 -0700387
Alec Mouri967d5d72020-08-05 12:50:03 -0700388void Choreographer::dispatchNullEvent(nsecs_t, PhysicalDisplayId) {
389 ALOGV("choreographer %p ~ received null event.", this);
390 handleRefreshRateUpdates();
Alec Mouricc445222019-10-22 10:19:17 -0700391}
392
393void Choreographer::handleMessage(const Message& message) {
394 switch (message.what) {
395 case MSG_SCHEDULE_CALLBACKS:
396 scheduleCallbacks();
397 break;
398 case MSG_SCHEDULE_VSYNC:
399 scheduleVsync();
400 break;
Alec Mouri271de042020-04-27 22:38:19 -0700401 case MSG_HANDLE_REFRESH_RATE_UPDATES:
402 handleRefreshRateUpdates();
403 break;
Alec Mouricc445222019-10-22 10:19:17 -0700404 }
405}
406
Alec Mouri271de042020-04-27 22:38:19 -0700407} // namespace android
Alec Mouri50a931d2019-11-19 16:23:59 -0800408using namespace android;
Alec Mouricc445222019-10-22 10:19:17 -0700409
410static inline Choreographer* AChoreographer_to_Choreographer(AChoreographer* choreographer) {
411 return reinterpret_cast<Choreographer*>(choreographer);
412}
413
Alec Mouri271de042020-04-27 22:38:19 -0700414// Glue for private C api
415namespace android {
416void AChoreographer_signalRefreshRateCallbacks(nsecs_t vsyncPeriod) EXCLUDES(gChoreographers.lock) {
417 std::lock_guard<std::mutex> _l(gChoreographers.lock);
418 gChoreographers.mLastKnownVsync.store(vsyncPeriod);
419 for (auto c : gChoreographers.ptrs) {
420 c->scheduleLatestConfigRequest();
421 }
422}
423
424void AChoreographer_initJVM(JNIEnv* env) {
425 env->GetJavaVM(&gJni.jvm);
426 // Now we need to find the java classes.
427 jclass dmgClass = env->FindClass("android/hardware/display/DisplayManagerGlobal");
428 gJni.displayManagerGlobal.clazz = static_cast<jclass>(env->NewGlobalRef(dmgClass));
429 gJni.displayManagerGlobal.getInstance =
430 env->GetStaticMethodID(dmgClass, "getInstance",
431 "()Landroid/hardware/display/DisplayManagerGlobal;");
432 gJni.displayManagerGlobal.registerNativeChoreographerForRefreshRateCallbacks =
433 env->GetMethodID(dmgClass, "registerNativeChoreographerForRefreshRateCallbacks", "()V");
434 gJni.displayManagerGlobal.unregisterNativeChoreographerForRefreshRateCallbacks =
435 env->GetMethodID(dmgClass, "unregisterNativeChoreographerForRefreshRateCallbacks",
436 "()V");
437}
438
439AChoreographer* AChoreographer_routeGetInstance() {
440 return AChoreographer_getInstance();
441}
442void AChoreographer_routePostFrameCallback(AChoreographer* choreographer,
443 AChoreographer_frameCallback callback, void* data) {
444 return AChoreographer_postFrameCallback(choreographer, callback, data);
445}
446void AChoreographer_routePostFrameCallbackDelayed(AChoreographer* choreographer,
447 AChoreographer_frameCallback callback, void* data,
448 long delayMillis) {
449 return AChoreographer_postFrameCallbackDelayed(choreographer, callback, data, delayMillis);
450}
451void AChoreographer_routePostFrameCallback64(AChoreographer* choreographer,
452 AChoreographer_frameCallback64 callback, void* data) {
453 return AChoreographer_postFrameCallback64(choreographer, callback, data);
454}
455void AChoreographer_routePostFrameCallbackDelayed64(AChoreographer* choreographer,
456 AChoreographer_frameCallback64 callback,
457 void* data, uint32_t delayMillis) {
458 return AChoreographer_postFrameCallbackDelayed64(choreographer, callback, data, delayMillis);
459}
460void AChoreographer_routeRegisterRefreshRateCallback(AChoreographer* choreographer,
461 AChoreographer_refreshRateCallback callback,
462 void* data) {
463 return AChoreographer_registerRefreshRateCallback(choreographer, callback, data);
464}
465void AChoreographer_routeUnregisterRefreshRateCallback(AChoreographer* choreographer,
466 AChoreographer_refreshRateCallback callback,
467 void* data) {
468 return AChoreographer_unregisterRefreshRateCallback(choreographer, callback, data);
469}
470
471} // namespace android
472
473/* Glue for the NDK interface */
474
Alec Mouri921b2772020-02-05 19:03:28 -0800475static inline const Choreographer* AChoreographer_to_Choreographer(
476 const AChoreographer* choreographer) {
477 return reinterpret_cast<const Choreographer*>(choreographer);
478}
479
Alec Mouricc445222019-10-22 10:19:17 -0700480static inline AChoreographer* Choreographer_to_AChoreographer(Choreographer* choreographer) {
481 return reinterpret_cast<AChoreographer*>(choreographer);
482}
483
484AChoreographer* AChoreographer_getInstance() {
485 return Choreographer_to_AChoreographer(Choreographer::getForThread());
486}
487
488void AChoreographer_postFrameCallback(AChoreographer* choreographer,
489 AChoreographer_frameCallback callback, void* data) {
490 AChoreographer_to_Choreographer(choreographer)->postFrameCallbackDelayed(
491 callback, nullptr, data, 0);
492}
493void AChoreographer_postFrameCallbackDelayed(AChoreographer* choreographer,
494 AChoreographer_frameCallback callback, void* data, long delayMillis) {
495 AChoreographer_to_Choreographer(choreographer)->postFrameCallbackDelayed(
496 callback, nullptr, data, ms2ns(delayMillis));
497}
498void AChoreographer_postFrameCallback64(AChoreographer* choreographer,
499 AChoreographer_frameCallback64 callback, void* data) {
500 AChoreographer_to_Choreographer(choreographer)->postFrameCallbackDelayed(
501 nullptr, callback, data, 0);
502}
503void AChoreographer_postFrameCallbackDelayed64(AChoreographer* choreographer,
504 AChoreographer_frameCallback64 callback, void* data, uint32_t delayMillis) {
505 AChoreographer_to_Choreographer(choreographer)->postFrameCallbackDelayed(
506 nullptr, callback, data, ms2ns(delayMillis));
507}
Alec Mouri60aee1c2019-10-28 16:18:59 -0700508void AChoreographer_registerRefreshRateCallback(AChoreographer* choreographer,
509 AChoreographer_refreshRateCallback callback,
510 void* data) {
511 AChoreographer_to_Choreographer(choreographer)->registerRefreshRateCallback(callback, data);
512}
513void AChoreographer_unregisterRefreshRateCallback(AChoreographer* choreographer,
Alec Mouri33682e92020-01-10 15:11:15 -0800514 AChoreographer_refreshRateCallback callback,
515 void* data) {
516 AChoreographer_to_Choreographer(choreographer)->unregisterRefreshRateCallback(callback, data);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700517}
Alec Mouri50a931d2019-11-19 16:23:59 -0800518
519AChoreographer* AChoreographer_create() {
520 Choreographer* choreographer = new Choreographer(nullptr);
521 status_t result = choreographer->initialize();
522 if (result != OK) {
523 ALOGW("Failed to initialize");
524 return nullptr;
525 }
526 return Choreographer_to_AChoreographer(choreographer);
527}
528
529void AChoreographer_destroy(AChoreographer* choreographer) {
530 if (choreographer == nullptr) {
531 return;
532 }
533
534 delete AChoreographer_to_Choreographer(choreographer);
535}
536
Alec Mouri921b2772020-02-05 19:03:28 -0800537int AChoreographer_getFd(const AChoreographer* choreographer) {
Alec Mouri50a931d2019-11-19 16:23:59 -0800538 return AChoreographer_to_Choreographer(choreographer)->getFd();
539}
540
541void AChoreographer_handlePendingEvents(AChoreographer* choreographer, void* data) {
542 // Pass dummy fd and events args to handleEvent, since the underlying
543 // DisplayEventDispatcher doesn't need them outside of validating that a
544 // Looper instance didn't break, but these args circumvent those checks.
Alec Mouri271de042020-04-27 22:38:19 -0700545 Choreographer* impl = AChoreographer_to_Choreographer(choreographer);
546 impl->handleEvent(-1, Looper::EVENT_INPUT, data);
Alec Mouri50a931d2019-11-19 16:23:59 -0800547}