blob: 2dd6c4fcaa819497794f394650ff78be5969a64c [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>
Orion Hodsone53587b2021-02-02 15:33:33 +000023#include <jni.h>
Alec Mouri271de042020-04-27 22:38:19 -070024#include <private/android/choreographer.h>
Alec Mouricc445222019-10-22 10:19:17 -070025#include <utils/Looper.h>
Alec Mouricc445222019-10-22 10:19:17 -070026#include <utils/Timers.h>
27
Alec Mouri60aee1c2019-10-28 16:18:59 -070028#include <cinttypes>
Alec Mouri271de042020-04-27 22:38:19 -070029#include <mutex>
Alec Mouri60aee1c2019-10-28 16:18:59 -070030#include <optional>
31#include <queue>
32#include <thread>
33
Alec Mouri271de042020-04-27 22:38:19 -070034namespace {
35struct {
36 // Global JVM that is provided by zygote
37 JavaVM* jvm = nullptr;
38 struct {
39 jclass clazz;
40 jmethodID getInstance;
41 jmethodID registerNativeChoreographerForRefreshRateCallbacks;
42 jmethodID unregisterNativeChoreographerForRefreshRateCallbacks;
43 } displayManagerGlobal;
44} gJni;
Alec Mouricc445222019-10-22 10:19:17 -070045
Alec Mouri271de042020-04-27 22:38:19 -070046// Gets the JNIEnv* for this thread, and performs one-off initialization if we
47// have never retrieved a JNIEnv* pointer before.
48JNIEnv* getJniEnv() {
49 if (gJni.jvm == nullptr) {
50 ALOGW("AChoreographer: No JVM provided!");
51 return nullptr;
52 }
53
54 JNIEnv* env = nullptr;
55 if (gJni.jvm->GetEnv((void**)&env, JNI_VERSION_1_4) != JNI_OK) {
56 ALOGD("Attaching thread to JVM for AChoreographer");
57 JavaVMAttachArgs args = {JNI_VERSION_1_4, "AChoreographer_env", NULL};
58 jint attachResult = gJni.jvm->AttachCurrentThreadAsDaemon(&env, (void*)&args);
59 if (attachResult != JNI_OK) {
60 ALOGE("Unable to attach thread. Error: %d", attachResult);
61 return nullptr;
62 }
63 }
64 if (env == nullptr) {
65 ALOGW("AChoreographer: No JNI env available!");
66 }
67 return env;
68}
69
70inline const char* toString(bool value) {
Alec Mouricc445222019-10-22 10:19:17 -070071 return value ? "true" : "false";
72}
Alec Mouri271de042020-04-27 22:38:19 -070073} // namespace
74
75namespace android {
Alec Mouricc445222019-10-22 10:19:17 -070076
77struct FrameCallback {
78 AChoreographer_frameCallback callback;
79 AChoreographer_frameCallback64 callback64;
80 void* data;
81 nsecs_t dueTime;
82
83 inline bool operator<(const FrameCallback& rhs) const {
84 // Note that this is intentionally flipped because we want callbacks due sooner to be at
85 // the head of the queue
86 return dueTime > rhs.dueTime;
87 }
88};
89
Alec Mouri60aee1c2019-10-28 16:18:59 -070090struct RefreshRateCallback {
91 AChoreographer_refreshRateCallback callback;
92 void* data;
Alec Mouri271de042020-04-27 22:38:19 -070093 bool firstCallbackFired = false;
Alec Mouri60aee1c2019-10-28 16:18:59 -070094};
Alec Mouricc445222019-10-22 10:19:17 -070095
Alec Mouri271de042020-04-27 22:38:19 -070096class Choreographer;
97
98struct {
99 std::mutex lock;
100 std::vector<Choreographer*> ptrs GUARDED_BY(lock);
101 bool registeredToDisplayManager GUARDED_BY(lock) = false;
102
103 std::atomic<nsecs_t> mLastKnownVsync = -1;
104} gChoreographers;
105
Alec Mouricc445222019-10-22 10:19:17 -0700106class Choreographer : public DisplayEventDispatcher, public MessageHandler {
107public:
Alec Mouri271de042020-04-27 22:38:19 -0700108 explicit Choreographer(const sp<Looper>& looper) EXCLUDES(gChoreographers.lock);
Alec Mouricc445222019-10-22 10:19:17 -0700109 void postFrameCallbackDelayed(AChoreographer_frameCallback cb,
110 AChoreographer_frameCallback64 cb64, void* data, nsecs_t delay);
Alec Mouri271de042020-04-27 22:38:19 -0700111 void registerRefreshRateCallback(AChoreographer_refreshRateCallback cb, void* data)
112 EXCLUDES(gChoreographers.lock);
Alec Mouri33682e92020-01-10 15:11:15 -0800113 void unregisterRefreshRateCallback(AChoreographer_refreshRateCallback cb, void* data);
Alec Mouri271de042020-04-27 22:38:19 -0700114 // Drains the queue of pending vsync periods and dispatches refresh rate
115 // updates to callbacks.
116 // The assumption is that this method is only called on a single
117 // processing thread, either by looper or by AChoreographer_handleEvents
118 void handleRefreshRateUpdates();
119 void scheduleLatestConfigRequest();
Alec Mouricc445222019-10-22 10:19:17 -0700120
121 enum {
122 MSG_SCHEDULE_CALLBACKS = 0,
Alec Mouri271de042020-04-27 22:38:19 -0700123 MSG_SCHEDULE_VSYNC = 1,
124 MSG_HANDLE_REFRESH_RATE_UPDATES = 2,
Alec Mouricc445222019-10-22 10:19:17 -0700125 };
126 virtual void handleMessage(const Message& message) override;
127
128 static Choreographer* getForThread();
Alec Mouri271de042020-04-27 22:38:19 -0700129 virtual ~Choreographer() override EXCLUDES(gChoreographers.lock);
Ady Abraham74e17562020-08-24 18:18:19 -0700130 int64_t getVsyncId() const;
Ady Abraham0d28d762020-10-05 17:50:41 -0700131 int64_t getFrameDeadline() const;
Jorim Jaggic0086af2021-02-12 18:18:11 +0100132 int64_t getFrameInterval() const;
Alec Mouricc445222019-10-22 10:19:17 -0700133
134private:
Alec Mouricc445222019-10-22 10:19:17 -0700135 Choreographer(const Choreographer&) = delete;
136
Ady Abraham74e17562020-08-24 18:18:19 -0700137 void dispatchVsync(nsecs_t timestamp, PhysicalDisplayId displayId, uint32_t count,
Ady Abraham0d28d762020-10-05 17:50:41 -0700138 VsyncEventData vsyncEventData) override;
Alec Mouricc445222019-10-22 10:19:17 -0700139 void dispatchHotplug(nsecs_t timestamp, PhysicalDisplayId displayId, bool connected) override;
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100140 void dispatchModeChanged(nsecs_t timestamp, PhysicalDisplayId displayId, int32_t modeId,
141 nsecs_t vsyncPeriod) override;
Alec Mouri967d5d72020-08-05 12:50:03 -0700142 void dispatchNullEvent(nsecs_t, PhysicalDisplayId) override;
Ady Abraham62f216c2020-10-13 19:07:23 -0700143 void dispatchFrameRateOverrides(nsecs_t timestamp, PhysicalDisplayId displayId,
144 std::vector<FrameRateOverride> overrides) override;
Alec Mouricc445222019-10-22 10:19:17 -0700145
146 void scheduleCallbacks();
147
Alec Mouri271de042020-04-27 22:38:19 -0700148 std::mutex mLock;
Alec Mouricc445222019-10-22 10:19:17 -0700149 // Protected by mLock
Alec Mouri60aee1c2019-10-28 16:18:59 -0700150 std::priority_queue<FrameCallback> mFrameCallbacks;
Alec Mouri60aee1c2019-10-28 16:18:59 -0700151 std::vector<RefreshRateCallback> mRefreshRateCallbacks;
Alec Mouricc445222019-10-22 10:19:17 -0700152
Alec Mouri271de042020-04-27 22:38:19 -0700153 nsecs_t mLatestVsyncPeriod = -1;
Ady Abraham0d28d762020-10-05 17:50:41 -0700154 VsyncEventData mLastVsyncEventData;
Alec Mouricc445222019-10-22 10:19:17 -0700155
156 const sp<Looper> mLooper;
157 const std::thread::id mThreadId;
158};
159
Alec Mouricc445222019-10-22 10:19:17 -0700160static thread_local Choreographer* gChoreographer;
161Choreographer* Choreographer::getForThread() {
162 if (gChoreographer == nullptr) {
163 sp<Looper> looper = Looper::getForThread();
164 if (!looper.get()) {
165 ALOGW("No looper prepared for thread");
166 return nullptr;
167 }
168 gChoreographer = new Choreographer(looper);
169 status_t result = gChoreographer->initialize();
170 if (result != OK) {
171 ALOGW("Failed to initialize");
172 return nullptr;
173 }
174 }
175 return gChoreographer;
176}
177
Alec Mouri60aee1c2019-10-28 16:18:59 -0700178Choreographer::Choreographer(const sp<Looper>& looper)
Ady Abraham62f216c2020-10-13 19:07:23 -0700179 : DisplayEventDispatcher(looper, ISurfaceComposer::VsyncSource::eVsyncSourceApp),
Alec Mouri60aee1c2019-10-28 16:18:59 -0700180 mLooper(looper),
Alec Mouri271de042020-04-27 22:38:19 -0700181 mThreadId(std::this_thread::get_id()) {
182 std::lock_guard<std::mutex> _l(gChoreographers.lock);
183 gChoreographers.ptrs.push_back(this);
184}
185
186Choreographer::~Choreographer() {
187 std::lock_guard<std::mutex> _l(gChoreographers.lock);
188 gChoreographers.ptrs.erase(std::remove_if(gChoreographers.ptrs.begin(),
189 gChoreographers.ptrs.end(),
190 [=](Choreographer* c) { return c == this; }),
191 gChoreographers.ptrs.end());
192 // Only poke DisplayManagerGlobal to unregister if we previously registered
193 // callbacks.
194 if (gChoreographers.ptrs.empty() && gChoreographers.registeredToDisplayManager) {
195 JNIEnv* env = getJniEnv();
196 if (env == nullptr) {
197 ALOGW("JNI environment is unavailable, skipping choreographer cleanup");
198 return;
199 }
200 jobject dmg = env->CallStaticObjectMethod(gJni.displayManagerGlobal.clazz,
201 gJni.displayManagerGlobal.getInstance);
202 if (dmg == nullptr) {
203 ALOGW("DMS is not initialized yet, skipping choreographer cleanup");
204 } else {
205 env->CallVoidMethod(dmg,
206 gJni.displayManagerGlobal
207 .unregisterNativeChoreographerForRefreshRateCallbacks);
208 env->DeleteLocalRef(dmg);
209 }
210 }
211}
Alec Mouricc445222019-10-22 10:19:17 -0700212
213void Choreographer::postFrameCallbackDelayed(
214 AChoreographer_frameCallback cb, AChoreographer_frameCallback64 cb64, void* data, nsecs_t delay) {
215 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
216 FrameCallback callback{cb, cb64, data, now + delay};
217 {
Alec Mouri271de042020-04-27 22:38:19 -0700218 std::lock_guard<std::mutex> _l{mLock};
Alec Mouri60aee1c2019-10-28 16:18:59 -0700219 mFrameCallbacks.push(callback);
Alec Mouricc445222019-10-22 10:19:17 -0700220 }
221 if (callback.dueTime <= now) {
222 if (std::this_thread::get_id() != mThreadId) {
Alec Mouri50a931d2019-11-19 16:23:59 -0800223 if (mLooper != nullptr) {
224 Message m{MSG_SCHEDULE_VSYNC};
225 mLooper->sendMessage(this, m);
226 } else {
227 scheduleVsync();
228 }
Alec Mouricc445222019-10-22 10:19:17 -0700229 } else {
230 scheduleVsync();
231 }
232 } else {
Alec Mouri50a931d2019-11-19 16:23:59 -0800233 if (mLooper != nullptr) {
234 Message m{MSG_SCHEDULE_CALLBACKS};
235 mLooper->sendMessageDelayed(delay, this, m);
236 } else {
237 scheduleCallbacks();
238 }
Alec Mouricc445222019-10-22 10:19:17 -0700239 }
240}
241
Alec Mouri60aee1c2019-10-28 16:18:59 -0700242void Choreographer::registerRefreshRateCallback(AChoreographer_refreshRateCallback cb, void* data) {
Alec Mouri271de042020-04-27 22:38:19 -0700243 std::lock_guard<std::mutex> _l{mLock};
244 for (const auto& callback : mRefreshRateCallbacks) {
245 // Don't re-add callbacks.
246 if (cb == callback.callback && data == callback.data) {
247 return;
248 }
249 }
250 mRefreshRateCallbacks.emplace_back(
251 RefreshRateCallback{.callback = cb, .data = data, .firstCallbackFired = false});
252 bool needsRegistration = false;
Alec Mouri60aee1c2019-10-28 16:18:59 -0700253 {
Alec Mouri271de042020-04-27 22:38:19 -0700254 std::lock_guard<std::mutex> _l2(gChoreographers.lock);
255 needsRegistration = !gChoreographers.registeredToDisplayManager;
256 }
257 if (needsRegistration) {
258 JNIEnv* env = getJniEnv();
Alec Mouri271de042020-04-27 22:38:19 -0700259 if (env == nullptr) {
Greg Kaiserb66d04b2020-05-11 06:52:15 -0700260 ALOGW("JNI environment is unavailable, skipping registration");
Alec Mouri271de042020-04-27 22:38:19 -0700261 return;
262 }
Greg Kaiserb66d04b2020-05-11 06:52:15 -0700263 jobject dmg = env->CallStaticObjectMethod(gJni.displayManagerGlobal.clazz,
264 gJni.displayManagerGlobal.getInstance);
Alec Mouri271de042020-04-27 22:38:19 -0700265 if (dmg == nullptr) {
266 ALOGW("DMS is not initialized yet: skipping registration");
267 return;
268 } else {
269 env->CallVoidMethod(dmg,
270 gJni.displayManagerGlobal
271 .registerNativeChoreographerForRefreshRateCallbacks,
272 reinterpret_cast<int64_t>(this));
273 env->DeleteLocalRef(dmg);
274 {
275 std::lock_guard<std::mutex> _l2(gChoreographers.lock);
276 gChoreographers.registeredToDisplayManager = true;
Alec Mouri33682e92020-01-10 15:11:15 -0800277 }
278 }
Alec Mouri271de042020-04-27 22:38:19 -0700279 } else {
280 scheduleLatestConfigRequest();
Alec Mouri60aee1c2019-10-28 16:18:59 -0700281 }
282}
283
Alec Mouri33682e92020-01-10 15:11:15 -0800284void Choreographer::unregisterRefreshRateCallback(AChoreographer_refreshRateCallback cb,
285 void* data) {
Alec Mouri271de042020-04-27 22:38:19 -0700286 std::lock_guard<std::mutex> _l{mLock};
287 mRefreshRateCallbacks.erase(std::remove_if(mRefreshRateCallbacks.begin(),
288 mRefreshRateCallbacks.end(),
289 [&](const RefreshRateCallback& callback) {
290 return cb == callback.callback &&
291 data == callback.data;
292 }),
293 mRefreshRateCallbacks.end());
294}
295
296void Choreographer::scheduleLatestConfigRequest() {
297 if (mLooper != nullptr) {
298 Message m{MSG_HANDLE_REFRESH_RATE_UPDATES};
299 mLooper->sendMessage(this, m);
300 } else {
301 // If the looper thread is detached from Choreographer, then refresh rate
302 // changes will be handled in AChoreographer_handlePendingEvents, so we
Alec Mouri967d5d72020-08-05 12:50:03 -0700303 // need to wake up the looper thread by writing to the write-end of the
304 // socket the looper is listening on.
305 // Fortunately, these events are small so sending packets across the
306 // socket should be atomic across processes.
307 DisplayEventReceiver::Event event;
308 event.header = DisplayEventReceiver::Event::Header{DisplayEventReceiver::DISPLAY_EVENT_NULL,
309 PhysicalDisplayId(0), systemTime()};
310 injectEvent(event);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700311 }
312}
313
Alec Mouricc445222019-10-22 10:19:17 -0700314void Choreographer::scheduleCallbacks() {
Alec Mouri134266b2020-03-03 19:22:29 -0800315 const nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
316 nsecs_t dueTime;
317 {
Alec Mouri271de042020-04-27 22:38:19 -0700318 std::lock_guard<std::mutex> _l{mLock};
Alec Mouri134266b2020-03-03 19:22:29 -0800319 // If there are no pending callbacks then don't schedule a vsync
320 if (mFrameCallbacks.empty()) {
321 return;
322 }
323 dueTime = mFrameCallbacks.top().dueTime;
324 }
325
326 if (dueTime <= now) {
Alec Mouricc445222019-10-22 10:19:17 -0700327 ALOGV("choreographer %p ~ scheduling vsync", this);
328 scheduleVsync();
329 return;
330 }
331}
332
Alec Mouri271de042020-04-27 22:38:19 -0700333void Choreographer::handleRefreshRateUpdates() {
334 std::vector<RefreshRateCallback> callbacks{};
335 const nsecs_t pendingPeriod = gChoreographers.mLastKnownVsync.load();
336 const nsecs_t lastPeriod = mLatestVsyncPeriod;
337 if (pendingPeriod > 0) {
338 mLatestVsyncPeriod = pendingPeriod;
339 }
340 {
341 std::lock_guard<std::mutex> _l{mLock};
342 for (auto& cb : mRefreshRateCallbacks) {
343 callbacks.push_back(cb);
344 cb.firstCallbackFired = true;
345 }
346 }
347
348 for (auto& cb : callbacks) {
349 if (!cb.firstCallbackFired || (pendingPeriod > 0 && pendingPeriod != lastPeriod)) {
350 cb.callback(pendingPeriod, cb.data);
351 }
352 }
353}
354
Alec Mouricc445222019-10-22 10:19:17 -0700355// TODO(b/74619554): The PhysicalDisplayId is ignored because SF only emits VSYNC events for the
356// internal display and DisplayEventReceiver::requestNextVsync only allows requesting VSYNC for
357// the internal display implicitly.
Ady Abraham0d28d762020-10-05 17:50:41 -0700358void Choreographer::dispatchVsync(nsecs_t timestamp, PhysicalDisplayId, uint32_t,
359 VsyncEventData vsyncEventData) {
Alec Mouricc445222019-10-22 10:19:17 -0700360 std::vector<FrameCallback> callbacks{};
361 {
Alec Mouri271de042020-04-27 22:38:19 -0700362 std::lock_guard<std::mutex> _l{mLock};
Alec Mouricc445222019-10-22 10:19:17 -0700363 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700364 while (!mFrameCallbacks.empty() && mFrameCallbacks.top().dueTime < now) {
365 callbacks.push_back(mFrameCallbacks.top());
366 mFrameCallbacks.pop();
Alec Mouricc445222019-10-22 10:19:17 -0700367 }
368 }
Ady Abraham0d28d762020-10-05 17:50:41 -0700369 mLastVsyncEventData = vsyncEventData;
Alec Mouricc445222019-10-22 10:19:17 -0700370 for (const auto& cb : callbacks) {
371 if (cb.callback64 != nullptr) {
372 cb.callback64(timestamp, cb.data);
373 } else if (cb.callback != nullptr) {
374 cb.callback(timestamp, cb.data);
375 }
376 }
377}
378
379void Choreographer::dispatchHotplug(nsecs_t, PhysicalDisplayId displayId, bool connected) {
Marin Shalamanova524a092020-07-27 21:39:55 +0200380 ALOGV("choreographer %p ~ received hotplug event (displayId=%s, connected=%s), ignoring.",
381 this, to_string(displayId).c_str(), toString(connected));
Alec Mouricc445222019-10-22 10:19:17 -0700382}
383
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100384void Choreographer::dispatchModeChanged(nsecs_t, PhysicalDisplayId, int32_t, nsecs_t) {
385 LOG_ALWAYS_FATAL("dispatchModeChanged was called but was never registered");
Ady Abraham62f216c2020-10-13 19:07:23 -0700386}
387
388void Choreographer::dispatchFrameRateOverrides(nsecs_t, PhysicalDisplayId,
389 std::vector<FrameRateOverride>) {
390 LOG_ALWAYS_FATAL("dispatchFrameRateOverrides was called but was never registered");
Alec Mouri967d5d72020-08-05 12:50:03 -0700391}
Alec Mouri271de042020-04-27 22:38:19 -0700392
Alec Mouri967d5d72020-08-05 12:50:03 -0700393void Choreographer::dispatchNullEvent(nsecs_t, PhysicalDisplayId) {
394 ALOGV("choreographer %p ~ received null event.", this);
395 handleRefreshRateUpdates();
Alec Mouricc445222019-10-22 10:19:17 -0700396}
397
398void Choreographer::handleMessage(const Message& message) {
399 switch (message.what) {
400 case MSG_SCHEDULE_CALLBACKS:
401 scheduleCallbacks();
402 break;
403 case MSG_SCHEDULE_VSYNC:
404 scheduleVsync();
405 break;
Alec Mouri271de042020-04-27 22:38:19 -0700406 case MSG_HANDLE_REFRESH_RATE_UPDATES:
407 handleRefreshRateUpdates();
408 break;
Alec Mouricc445222019-10-22 10:19:17 -0700409 }
410}
411
Ady Abraham74e17562020-08-24 18:18:19 -0700412int64_t Choreographer::getVsyncId() const {
Ady Abraham0d28d762020-10-05 17:50:41 -0700413 return mLastVsyncEventData.id;
414}
415
416int64_t Choreographer::getFrameDeadline() const {
417 return mLastVsyncEventData.deadlineTimestamp;
Ady Abraham74e17562020-08-24 18:18:19 -0700418}
419
Jorim Jaggic0086af2021-02-12 18:18:11 +0100420int64_t Choreographer::getFrameInterval() const {
421 return mLastVsyncEventData.frameInterval;
422}
423
Alec Mouri271de042020-04-27 22:38:19 -0700424} // namespace android
Alec Mouri50a931d2019-11-19 16:23:59 -0800425using namespace android;
Alec Mouricc445222019-10-22 10:19:17 -0700426
427static inline Choreographer* AChoreographer_to_Choreographer(AChoreographer* choreographer) {
428 return reinterpret_cast<Choreographer*>(choreographer);
429}
430
Ady Abraham74e17562020-08-24 18:18:19 -0700431static inline const Choreographer* AChoreographer_to_Choreographer(
432 const AChoreographer* choreographer) {
433 return reinterpret_cast<const Choreographer*>(choreographer);
434}
435
Alec Mouri271de042020-04-27 22:38:19 -0700436// Glue for private C api
437namespace android {
438void AChoreographer_signalRefreshRateCallbacks(nsecs_t vsyncPeriod) EXCLUDES(gChoreographers.lock) {
439 std::lock_guard<std::mutex> _l(gChoreographers.lock);
440 gChoreographers.mLastKnownVsync.store(vsyncPeriod);
441 for (auto c : gChoreographers.ptrs) {
442 c->scheduleLatestConfigRequest();
443 }
444}
445
446void AChoreographer_initJVM(JNIEnv* env) {
447 env->GetJavaVM(&gJni.jvm);
448 // Now we need to find the java classes.
449 jclass dmgClass = env->FindClass("android/hardware/display/DisplayManagerGlobal");
450 gJni.displayManagerGlobal.clazz = static_cast<jclass>(env->NewGlobalRef(dmgClass));
451 gJni.displayManagerGlobal.getInstance =
452 env->GetStaticMethodID(dmgClass, "getInstance",
453 "()Landroid/hardware/display/DisplayManagerGlobal;");
454 gJni.displayManagerGlobal.registerNativeChoreographerForRefreshRateCallbacks =
455 env->GetMethodID(dmgClass, "registerNativeChoreographerForRefreshRateCallbacks", "()V");
456 gJni.displayManagerGlobal.unregisterNativeChoreographerForRefreshRateCallbacks =
457 env->GetMethodID(dmgClass, "unregisterNativeChoreographerForRefreshRateCallbacks",
458 "()V");
459}
460
461AChoreographer* AChoreographer_routeGetInstance() {
462 return AChoreographer_getInstance();
463}
464void AChoreographer_routePostFrameCallback(AChoreographer* choreographer,
465 AChoreographer_frameCallback callback, void* data) {
Jiyong Park8cdf0762020-08-13 20:21:57 +0900466#pragma clang diagnostic push
467#pragma clang diagnostic ignored "-Wdeprecated-declarations"
Alec Mouri271de042020-04-27 22:38:19 -0700468 return AChoreographer_postFrameCallback(choreographer, callback, data);
Jiyong Park8cdf0762020-08-13 20:21:57 +0900469#pragma clang diagnostic pop
Alec Mouri271de042020-04-27 22:38:19 -0700470}
471void AChoreographer_routePostFrameCallbackDelayed(AChoreographer* choreographer,
472 AChoreographer_frameCallback callback, void* data,
473 long delayMillis) {
Jiyong Park8cdf0762020-08-13 20:21:57 +0900474#pragma clang diagnostic push
475#pragma clang diagnostic ignored "-Wdeprecated-declarations"
Alec Mouri271de042020-04-27 22:38:19 -0700476 return AChoreographer_postFrameCallbackDelayed(choreographer, callback, data, delayMillis);
Jiyong Park8cdf0762020-08-13 20:21:57 +0900477#pragma clang diagnostic pop
Alec Mouri271de042020-04-27 22:38:19 -0700478}
479void AChoreographer_routePostFrameCallback64(AChoreographer* choreographer,
480 AChoreographer_frameCallback64 callback, void* data) {
481 return AChoreographer_postFrameCallback64(choreographer, callback, data);
482}
483void AChoreographer_routePostFrameCallbackDelayed64(AChoreographer* choreographer,
484 AChoreographer_frameCallback64 callback,
485 void* data, uint32_t delayMillis) {
486 return AChoreographer_postFrameCallbackDelayed64(choreographer, callback, data, delayMillis);
487}
488void AChoreographer_routeRegisterRefreshRateCallback(AChoreographer* choreographer,
489 AChoreographer_refreshRateCallback callback,
490 void* data) {
491 return AChoreographer_registerRefreshRateCallback(choreographer, callback, data);
492}
493void AChoreographer_routeUnregisterRefreshRateCallback(AChoreographer* choreographer,
494 AChoreographer_refreshRateCallback callback,
495 void* data) {
496 return AChoreographer_unregisterRefreshRateCallback(choreographer, callback, data);
497}
498
Ady Abraham74e17562020-08-24 18:18:19 -0700499int64_t AChoreographer_getVsyncId(const AChoreographer* choreographer) {
500 return AChoreographer_to_Choreographer(choreographer)->getVsyncId();
501}
502
Ady Abraham0d28d762020-10-05 17:50:41 -0700503int64_t AChoreographer_getFrameDeadline(const AChoreographer* choreographer) {
504 return AChoreographer_to_Choreographer(choreographer)->getFrameDeadline();
505}
506
Jorim Jaggic0086af2021-02-12 18:18:11 +0100507int64_t AChoreographer_getFrameInterval(const AChoreographer* choreographer) {
508 return AChoreographer_to_Choreographer(choreographer)->getFrameInterval();
509}
510
Alec Mouri271de042020-04-27 22:38:19 -0700511} // namespace android
512
513/* Glue for the NDK interface */
514
Alec Mouricc445222019-10-22 10:19:17 -0700515static inline AChoreographer* Choreographer_to_AChoreographer(Choreographer* choreographer) {
516 return reinterpret_cast<AChoreographer*>(choreographer);
517}
518
519AChoreographer* AChoreographer_getInstance() {
520 return Choreographer_to_AChoreographer(Choreographer::getForThread());
521}
522
523void AChoreographer_postFrameCallback(AChoreographer* choreographer,
524 AChoreographer_frameCallback callback, void* data) {
525 AChoreographer_to_Choreographer(choreographer)->postFrameCallbackDelayed(
526 callback, nullptr, data, 0);
527}
528void AChoreographer_postFrameCallbackDelayed(AChoreographer* choreographer,
529 AChoreographer_frameCallback callback, void* data, long delayMillis) {
530 AChoreographer_to_Choreographer(choreographer)->postFrameCallbackDelayed(
531 callback, nullptr, data, ms2ns(delayMillis));
532}
533void AChoreographer_postFrameCallback64(AChoreographer* choreographer,
534 AChoreographer_frameCallback64 callback, void* data) {
535 AChoreographer_to_Choreographer(choreographer)->postFrameCallbackDelayed(
536 nullptr, callback, data, 0);
537}
538void AChoreographer_postFrameCallbackDelayed64(AChoreographer* choreographer,
539 AChoreographer_frameCallback64 callback, void* data, uint32_t delayMillis) {
540 AChoreographer_to_Choreographer(choreographer)->postFrameCallbackDelayed(
541 nullptr, callback, data, ms2ns(delayMillis));
542}
Alec Mouri60aee1c2019-10-28 16:18:59 -0700543void AChoreographer_registerRefreshRateCallback(AChoreographer* choreographer,
544 AChoreographer_refreshRateCallback callback,
545 void* data) {
546 AChoreographer_to_Choreographer(choreographer)->registerRefreshRateCallback(callback, data);
547}
548void AChoreographer_unregisterRefreshRateCallback(AChoreographer* choreographer,
Alec Mouri33682e92020-01-10 15:11:15 -0800549 AChoreographer_refreshRateCallback callback,
550 void* data) {
551 AChoreographer_to_Choreographer(choreographer)->unregisterRefreshRateCallback(callback, data);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700552}
Alec Mouri50a931d2019-11-19 16:23:59 -0800553
554AChoreographer* AChoreographer_create() {
555 Choreographer* choreographer = new Choreographer(nullptr);
556 status_t result = choreographer->initialize();
557 if (result != OK) {
558 ALOGW("Failed to initialize");
559 return nullptr;
560 }
561 return Choreographer_to_AChoreographer(choreographer);
562}
563
564void AChoreographer_destroy(AChoreographer* choreographer) {
565 if (choreographer == nullptr) {
566 return;
567 }
568
569 delete AChoreographer_to_Choreographer(choreographer);
570}
571
Alec Mouri921b2772020-02-05 19:03:28 -0800572int AChoreographer_getFd(const AChoreographer* choreographer) {
Alec Mouri50a931d2019-11-19 16:23:59 -0800573 return AChoreographer_to_Choreographer(choreographer)->getFd();
574}
575
576void AChoreographer_handlePendingEvents(AChoreographer* choreographer, void* data) {
577 // Pass dummy fd and events args to handleEvent, since the underlying
578 // DisplayEventDispatcher doesn't need them outside of validating that a
579 // Looper instance didn't break, but these args circumvent those checks.
Alec Mouri271de042020-04-27 22:38:19 -0700580 Choreographer* impl = AChoreographer_to_Choreographer(choreographer);
581 impl->handleEvent(-1, Looper::EVENT_INPUT, data);
Alec Mouri50a931d2019-11-19 16:23:59 -0800582}