blob: 3ce381b122b0a50d0f046200d382d9538ef4d861 [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 {
Rachel Lee18c34372022-01-20 13:57:18 -080076using gui::VsyncEventData;
Alec Mouricc445222019-10-22 10:19:17 -070077
78struct FrameCallback {
79 AChoreographer_frameCallback callback;
80 AChoreographer_frameCallback64 callback64;
Rachel Lee4879d812021-08-25 11:50:11 -070081 AChoreographer_extendedFrameCallback extendedCallback;
Alec Mouricc445222019-10-22 10:19:17 -070082 void* data;
83 nsecs_t dueTime;
84
85 inline bool operator<(const FrameCallback& rhs) const {
86 // Note that this is intentionally flipped because we want callbacks due sooner to be at
87 // the head of the queue
88 return dueTime > rhs.dueTime;
89 }
90};
91
Alec Mouri60aee1c2019-10-28 16:18:59 -070092struct RefreshRateCallback {
93 AChoreographer_refreshRateCallback callback;
94 void* data;
Alec Mouri271de042020-04-27 22:38:19 -070095 bool firstCallbackFired = false;
Alec Mouri60aee1c2019-10-28 16:18:59 -070096};
Alec Mouricc445222019-10-22 10:19:17 -070097
Alec Mouri271de042020-04-27 22:38:19 -070098class Choreographer;
99
Rachel Lee4879d812021-08-25 11:50:11 -0700100/**
101 * Implementation of AChoreographerFrameCallbackData.
102 */
103struct ChoreographerFrameCallbackDataImpl {
Rachel Lee4879d812021-08-25 11:50:11 -0700104 int64_t frameTimeNanos{0};
105
Rachel Leeb9c5a772022-02-04 21:17:37 -0800106 VsyncEventData vsyncEventData;
Rachel Lee4879d812021-08-25 11:50:11 -0700107
108 const Choreographer* choreographer;
109};
110
Alec Mouri271de042020-04-27 22:38:19 -0700111struct {
112 std::mutex lock;
113 std::vector<Choreographer*> ptrs GUARDED_BY(lock);
114 bool registeredToDisplayManager GUARDED_BY(lock) = false;
115
116 std::atomic<nsecs_t> mLastKnownVsync = -1;
117} gChoreographers;
118
Alec Mouricc445222019-10-22 10:19:17 -0700119class Choreographer : public DisplayEventDispatcher, public MessageHandler {
120public:
Alec Mouri271de042020-04-27 22:38:19 -0700121 explicit Choreographer(const sp<Looper>& looper) EXCLUDES(gChoreographers.lock);
Alec Mouricc445222019-10-22 10:19:17 -0700122 void postFrameCallbackDelayed(AChoreographer_frameCallback cb,
Rachel Lee4879d812021-08-25 11:50:11 -0700123 AChoreographer_frameCallback64 cb64,
124 AChoreographer_extendedFrameCallback extendedCallback, void* data,
125 nsecs_t delay);
Alec Mouri271de042020-04-27 22:38:19 -0700126 void registerRefreshRateCallback(AChoreographer_refreshRateCallback cb, void* data)
127 EXCLUDES(gChoreographers.lock);
Alec Mouri33682e92020-01-10 15:11:15 -0800128 void unregisterRefreshRateCallback(AChoreographer_refreshRateCallback cb, void* data);
Alec Mouri271de042020-04-27 22:38:19 -0700129 // Drains the queue of pending vsync periods and dispatches refresh rate
130 // updates to callbacks.
131 // The assumption is that this method is only called on a single
132 // processing thread, either by looper or by AChoreographer_handleEvents
133 void handleRefreshRateUpdates();
134 void scheduleLatestConfigRequest();
Alec Mouricc445222019-10-22 10:19:17 -0700135
136 enum {
137 MSG_SCHEDULE_CALLBACKS = 0,
Alec Mouri271de042020-04-27 22:38:19 -0700138 MSG_SCHEDULE_VSYNC = 1,
139 MSG_HANDLE_REFRESH_RATE_UPDATES = 2,
Alec Mouricc445222019-10-22 10:19:17 -0700140 };
141 virtual void handleMessage(const Message& message) override;
142
143 static Choreographer* getForThread();
Alec Mouri271de042020-04-27 22:38:19 -0700144 virtual ~Choreographer() override EXCLUDES(gChoreographers.lock);
Jorim Jaggic0086af2021-02-12 18:18:11 +0100145 int64_t getFrameInterval() const;
Rachel Lee4879d812021-08-25 11:50:11 -0700146 bool inCallback() const;
Alec Mouricc445222019-10-22 10:19:17 -0700147
148private:
Alec Mouricc445222019-10-22 10:19:17 -0700149 Choreographer(const Choreographer&) = delete;
150
Ady Abraham74e17562020-08-24 18:18:19 -0700151 void dispatchVsync(nsecs_t timestamp, PhysicalDisplayId displayId, uint32_t count,
Ady Abraham0d28d762020-10-05 17:50:41 -0700152 VsyncEventData vsyncEventData) override;
Alec Mouricc445222019-10-22 10:19:17 -0700153 void dispatchHotplug(nsecs_t timestamp, PhysicalDisplayId displayId, bool connected) override;
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100154 void dispatchModeChanged(nsecs_t timestamp, PhysicalDisplayId displayId, int32_t modeId,
155 nsecs_t vsyncPeriod) override;
Alec Mouri967d5d72020-08-05 12:50:03 -0700156 void dispatchNullEvent(nsecs_t, PhysicalDisplayId) override;
Ady Abraham62f216c2020-10-13 19:07:23 -0700157 void dispatchFrameRateOverrides(nsecs_t timestamp, PhysicalDisplayId displayId,
158 std::vector<FrameRateOverride> overrides) override;
Alec Mouricc445222019-10-22 10:19:17 -0700159
160 void scheduleCallbacks();
161
Rachel Lee4879d812021-08-25 11:50:11 -0700162 ChoreographerFrameCallbackDataImpl createFrameCallbackData(nsecs_t timestamp) const;
163
Alec Mouri271de042020-04-27 22:38:19 -0700164 std::mutex mLock;
Alec Mouricc445222019-10-22 10:19:17 -0700165 // Protected by mLock
Alec Mouri60aee1c2019-10-28 16:18:59 -0700166 std::priority_queue<FrameCallback> mFrameCallbacks;
Alec Mouri60aee1c2019-10-28 16:18:59 -0700167 std::vector<RefreshRateCallback> mRefreshRateCallbacks;
Alec Mouricc445222019-10-22 10:19:17 -0700168
Alec Mouri271de042020-04-27 22:38:19 -0700169 nsecs_t mLatestVsyncPeriod = -1;
Ady Abraham0d28d762020-10-05 17:50:41 -0700170 VsyncEventData mLastVsyncEventData;
Rachel Lee4879d812021-08-25 11:50:11 -0700171 bool mInCallback = false;
Alec Mouricc445222019-10-22 10:19:17 -0700172
173 const sp<Looper> mLooper;
174 const std::thread::id mThreadId;
175};
176
Alec Mouricc445222019-10-22 10:19:17 -0700177static thread_local Choreographer* gChoreographer;
178Choreographer* Choreographer::getForThread() {
179 if (gChoreographer == nullptr) {
180 sp<Looper> looper = Looper::getForThread();
181 if (!looper.get()) {
182 ALOGW("No looper prepared for thread");
183 return nullptr;
184 }
185 gChoreographer = new Choreographer(looper);
186 status_t result = gChoreographer->initialize();
187 if (result != OK) {
188 ALOGW("Failed to initialize");
189 return nullptr;
190 }
191 }
192 return gChoreographer;
193}
194
Alec Mouri60aee1c2019-10-28 16:18:59 -0700195Choreographer::Choreographer(const sp<Looper>& looper)
Ady Abraham62f216c2020-10-13 19:07:23 -0700196 : DisplayEventDispatcher(looper, ISurfaceComposer::VsyncSource::eVsyncSourceApp),
Alec Mouri60aee1c2019-10-28 16:18:59 -0700197 mLooper(looper),
Alec Mouri271de042020-04-27 22:38:19 -0700198 mThreadId(std::this_thread::get_id()) {
199 std::lock_guard<std::mutex> _l(gChoreographers.lock);
200 gChoreographers.ptrs.push_back(this);
201}
202
203Choreographer::~Choreographer() {
204 std::lock_guard<std::mutex> _l(gChoreographers.lock);
205 gChoreographers.ptrs.erase(std::remove_if(gChoreographers.ptrs.begin(),
206 gChoreographers.ptrs.end(),
207 [=](Choreographer* c) { return c == this; }),
208 gChoreographers.ptrs.end());
209 // Only poke DisplayManagerGlobal to unregister if we previously registered
210 // callbacks.
211 if (gChoreographers.ptrs.empty() && gChoreographers.registeredToDisplayManager) {
Ady Abraham6a8986b2021-08-18 13:44:14 -0700212 gChoreographers.registeredToDisplayManager = false;
Alec Mouri271de042020-04-27 22:38:19 -0700213 JNIEnv* env = getJniEnv();
214 if (env == nullptr) {
215 ALOGW("JNI environment is unavailable, skipping choreographer cleanup");
216 return;
217 }
218 jobject dmg = env->CallStaticObjectMethod(gJni.displayManagerGlobal.clazz,
219 gJni.displayManagerGlobal.getInstance);
220 if (dmg == nullptr) {
221 ALOGW("DMS is not initialized yet, skipping choreographer cleanup");
222 } else {
223 env->CallVoidMethod(dmg,
224 gJni.displayManagerGlobal
225 .unregisterNativeChoreographerForRefreshRateCallbacks);
226 env->DeleteLocalRef(dmg);
227 }
228 }
229}
Alec Mouricc445222019-10-22 10:19:17 -0700230
Rachel Lee4879d812021-08-25 11:50:11 -0700231void Choreographer::postFrameCallbackDelayed(AChoreographer_frameCallback cb,
232 AChoreographer_frameCallback64 cb64,
233 AChoreographer_extendedFrameCallback extendedCallback,
234 void* data, nsecs_t delay) {
Alec Mouricc445222019-10-22 10:19:17 -0700235 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Rachel Lee4879d812021-08-25 11:50:11 -0700236 FrameCallback callback{cb, cb64, extendedCallback, data, now + delay};
Alec Mouricc445222019-10-22 10:19:17 -0700237 {
Alec Mouri271de042020-04-27 22:38:19 -0700238 std::lock_guard<std::mutex> _l{mLock};
Alec Mouri60aee1c2019-10-28 16:18:59 -0700239 mFrameCallbacks.push(callback);
Alec Mouricc445222019-10-22 10:19:17 -0700240 }
241 if (callback.dueTime <= now) {
242 if (std::this_thread::get_id() != mThreadId) {
Alec Mouri50a931d2019-11-19 16:23:59 -0800243 if (mLooper != nullptr) {
244 Message m{MSG_SCHEDULE_VSYNC};
245 mLooper->sendMessage(this, m);
246 } else {
247 scheduleVsync();
248 }
Alec Mouricc445222019-10-22 10:19:17 -0700249 } else {
250 scheduleVsync();
251 }
252 } else {
Alec Mouri50a931d2019-11-19 16:23:59 -0800253 if (mLooper != nullptr) {
254 Message m{MSG_SCHEDULE_CALLBACKS};
255 mLooper->sendMessageDelayed(delay, this, m);
256 } else {
257 scheduleCallbacks();
258 }
Alec Mouricc445222019-10-22 10:19:17 -0700259 }
260}
261
Alec Mouri60aee1c2019-10-28 16:18:59 -0700262void Choreographer::registerRefreshRateCallback(AChoreographer_refreshRateCallback cb, void* data) {
Alec Mouri271de042020-04-27 22:38:19 -0700263 std::lock_guard<std::mutex> _l{mLock};
264 for (const auto& callback : mRefreshRateCallbacks) {
265 // Don't re-add callbacks.
266 if (cb == callback.callback && data == callback.data) {
267 return;
268 }
269 }
270 mRefreshRateCallbacks.emplace_back(
271 RefreshRateCallback{.callback = cb, .data = data, .firstCallbackFired = false});
272 bool needsRegistration = false;
Alec Mouri60aee1c2019-10-28 16:18:59 -0700273 {
Alec Mouri271de042020-04-27 22:38:19 -0700274 std::lock_guard<std::mutex> _l2(gChoreographers.lock);
275 needsRegistration = !gChoreographers.registeredToDisplayManager;
276 }
277 if (needsRegistration) {
278 JNIEnv* env = getJniEnv();
Alec Mouri271de042020-04-27 22:38:19 -0700279 if (env == nullptr) {
Greg Kaiserb66d04b2020-05-11 06:52:15 -0700280 ALOGW("JNI environment is unavailable, skipping registration");
Alec Mouri271de042020-04-27 22:38:19 -0700281 return;
282 }
Greg Kaiserb66d04b2020-05-11 06:52:15 -0700283 jobject dmg = env->CallStaticObjectMethod(gJni.displayManagerGlobal.clazz,
284 gJni.displayManagerGlobal.getInstance);
Alec Mouri271de042020-04-27 22:38:19 -0700285 if (dmg == nullptr) {
286 ALOGW("DMS is not initialized yet: skipping registration");
287 return;
288 } else {
289 env->CallVoidMethod(dmg,
290 gJni.displayManagerGlobal
291 .registerNativeChoreographerForRefreshRateCallbacks,
292 reinterpret_cast<int64_t>(this));
293 env->DeleteLocalRef(dmg);
294 {
295 std::lock_guard<std::mutex> _l2(gChoreographers.lock);
296 gChoreographers.registeredToDisplayManager = true;
Alec Mouri33682e92020-01-10 15:11:15 -0800297 }
298 }
Alec Mouri271de042020-04-27 22:38:19 -0700299 } else {
300 scheduleLatestConfigRequest();
Alec Mouri60aee1c2019-10-28 16:18:59 -0700301 }
302}
303
Alec Mouri33682e92020-01-10 15:11:15 -0800304void Choreographer::unregisterRefreshRateCallback(AChoreographer_refreshRateCallback cb,
305 void* data) {
Alec Mouri271de042020-04-27 22:38:19 -0700306 std::lock_guard<std::mutex> _l{mLock};
307 mRefreshRateCallbacks.erase(std::remove_if(mRefreshRateCallbacks.begin(),
308 mRefreshRateCallbacks.end(),
309 [&](const RefreshRateCallback& callback) {
310 return cb == callback.callback &&
311 data == callback.data;
312 }),
313 mRefreshRateCallbacks.end());
314}
315
316void Choreographer::scheduleLatestConfigRequest() {
317 if (mLooper != nullptr) {
318 Message m{MSG_HANDLE_REFRESH_RATE_UPDATES};
319 mLooper->sendMessage(this, m);
320 } else {
321 // If the looper thread is detached from Choreographer, then refresh rate
322 // changes will be handled in AChoreographer_handlePendingEvents, so we
Alec Mouri967d5d72020-08-05 12:50:03 -0700323 // need to wake up the looper thread by writing to the write-end of the
324 // socket the looper is listening on.
325 // Fortunately, these events are small so sending packets across the
326 // socket should be atomic across processes.
327 DisplayEventReceiver::Event event;
Dominik Laskowskif1833852021-03-23 15:06:50 -0700328 event.header =
329 DisplayEventReceiver::Event::Header{DisplayEventReceiver::DISPLAY_EVENT_NULL,
330 PhysicalDisplayId::fromPort(0), systemTime()};
Alec Mouri967d5d72020-08-05 12:50:03 -0700331 injectEvent(event);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700332 }
333}
334
Alec Mouricc445222019-10-22 10:19:17 -0700335void Choreographer::scheduleCallbacks() {
Alec Mouri134266b2020-03-03 19:22:29 -0800336 const nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
337 nsecs_t dueTime;
338 {
Alec Mouri271de042020-04-27 22:38:19 -0700339 std::lock_guard<std::mutex> _l{mLock};
Alec Mouri134266b2020-03-03 19:22:29 -0800340 // If there are no pending callbacks then don't schedule a vsync
341 if (mFrameCallbacks.empty()) {
342 return;
343 }
344 dueTime = mFrameCallbacks.top().dueTime;
345 }
346
347 if (dueTime <= now) {
Alec Mouricc445222019-10-22 10:19:17 -0700348 ALOGV("choreographer %p ~ scheduling vsync", this);
349 scheduleVsync();
350 return;
351 }
352}
353
Alec Mouri271de042020-04-27 22:38:19 -0700354void Choreographer::handleRefreshRateUpdates() {
355 std::vector<RefreshRateCallback> callbacks{};
356 const nsecs_t pendingPeriod = gChoreographers.mLastKnownVsync.load();
357 const nsecs_t lastPeriod = mLatestVsyncPeriod;
358 if (pendingPeriod > 0) {
359 mLatestVsyncPeriod = pendingPeriod;
360 }
361 {
362 std::lock_guard<std::mutex> _l{mLock};
363 for (auto& cb : mRefreshRateCallbacks) {
364 callbacks.push_back(cb);
365 cb.firstCallbackFired = true;
366 }
367 }
368
369 for (auto& cb : callbacks) {
370 if (!cb.firstCallbackFired || (pendingPeriod > 0 && pendingPeriod != lastPeriod)) {
371 cb.callback(pendingPeriod, cb.data);
372 }
373 }
374}
375
Alec Mouricc445222019-10-22 10:19:17 -0700376// TODO(b/74619554): The PhysicalDisplayId is ignored because SF only emits VSYNC events for the
377// internal display and DisplayEventReceiver::requestNextVsync only allows requesting VSYNC for
378// the internal display implicitly.
Ady Abraham0d28d762020-10-05 17:50:41 -0700379void Choreographer::dispatchVsync(nsecs_t timestamp, PhysicalDisplayId, uint32_t,
380 VsyncEventData vsyncEventData) {
Alec Mouricc445222019-10-22 10:19:17 -0700381 std::vector<FrameCallback> callbacks{};
382 {
Alec Mouri271de042020-04-27 22:38:19 -0700383 std::lock_guard<std::mutex> _l{mLock};
Alec Mouricc445222019-10-22 10:19:17 -0700384 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700385 while (!mFrameCallbacks.empty() && mFrameCallbacks.top().dueTime < now) {
386 callbacks.push_back(mFrameCallbacks.top());
387 mFrameCallbacks.pop();
Alec Mouricc445222019-10-22 10:19:17 -0700388 }
389 }
Ady Abraham0d28d762020-10-05 17:50:41 -0700390 mLastVsyncEventData = vsyncEventData;
Alec Mouricc445222019-10-22 10:19:17 -0700391 for (const auto& cb : callbacks) {
Rachel Lee4879d812021-08-25 11:50:11 -0700392 if (cb.extendedCallback != nullptr) {
393 const ChoreographerFrameCallbackDataImpl frameCallbackData =
394 createFrameCallbackData(timestamp);
395 mInCallback = true;
396 cb.extendedCallback(reinterpret_cast<const AChoreographerFrameCallbackData*>(
397 &frameCallbackData),
398 cb.data);
399 mInCallback = false;
400 } else if (cb.callback64 != nullptr) {
Alec Mouricc445222019-10-22 10:19:17 -0700401 cb.callback64(timestamp, cb.data);
402 } else if (cb.callback != nullptr) {
403 cb.callback(timestamp, cb.data);
404 }
405 }
406}
407
408void Choreographer::dispatchHotplug(nsecs_t, PhysicalDisplayId displayId, bool connected) {
Rachel Lee4879d812021-08-25 11:50:11 -0700409 ALOGV("choreographer %p ~ received hotplug event (displayId=%s, connected=%s), ignoring.", this,
410 to_string(displayId).c_str(), toString(connected));
Alec Mouricc445222019-10-22 10:19:17 -0700411}
412
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100413void Choreographer::dispatchModeChanged(nsecs_t, PhysicalDisplayId, int32_t, nsecs_t) {
414 LOG_ALWAYS_FATAL("dispatchModeChanged was called but was never registered");
Ady Abraham62f216c2020-10-13 19:07:23 -0700415}
416
417void Choreographer::dispatchFrameRateOverrides(nsecs_t, PhysicalDisplayId,
418 std::vector<FrameRateOverride>) {
419 LOG_ALWAYS_FATAL("dispatchFrameRateOverrides was called but was never registered");
Alec Mouri967d5d72020-08-05 12:50:03 -0700420}
Alec Mouri271de042020-04-27 22:38:19 -0700421
Alec Mouri967d5d72020-08-05 12:50:03 -0700422void Choreographer::dispatchNullEvent(nsecs_t, PhysicalDisplayId) {
423 ALOGV("choreographer %p ~ received null event.", this);
424 handleRefreshRateUpdates();
Alec Mouricc445222019-10-22 10:19:17 -0700425}
426
427void Choreographer::handleMessage(const Message& message) {
428 switch (message.what) {
Rachel Lee4879d812021-08-25 11:50:11 -0700429 case MSG_SCHEDULE_CALLBACKS:
430 scheduleCallbacks();
431 break;
432 case MSG_SCHEDULE_VSYNC:
433 scheduleVsync();
434 break;
435 case MSG_HANDLE_REFRESH_RATE_UPDATES:
436 handleRefreshRateUpdates();
437 break;
Alec Mouricc445222019-10-22 10:19:17 -0700438 }
439}
440
Jorim Jaggic0086af2021-02-12 18:18:11 +0100441int64_t Choreographer::getFrameInterval() const {
442 return mLastVsyncEventData.frameInterval;
443}
444
Rachel Lee4879d812021-08-25 11:50:11 -0700445bool Choreographer::inCallback() const {
446 return mInCallback;
447}
448
449ChoreographerFrameCallbackDataImpl Choreographer::createFrameCallbackData(nsecs_t timestamp) const {
Rachel Lee4879d812021-08-25 11:50:11 -0700450 return {.frameTimeNanos = timestamp,
Rachel Leeb9c5a772022-02-04 21:17:37 -0800451 .vsyncEventData = mLastVsyncEventData,
Rachel Lee4879d812021-08-25 11:50:11 -0700452 .choreographer = this};
453}
454
Alec Mouri271de042020-04-27 22:38:19 -0700455} // namespace android
Alec Mouri50a931d2019-11-19 16:23:59 -0800456using namespace android;
Alec Mouricc445222019-10-22 10:19:17 -0700457
458static inline Choreographer* AChoreographer_to_Choreographer(AChoreographer* choreographer) {
459 return reinterpret_cast<Choreographer*>(choreographer);
460}
461
Ady Abraham74e17562020-08-24 18:18:19 -0700462static inline const Choreographer* AChoreographer_to_Choreographer(
463 const AChoreographer* choreographer) {
464 return reinterpret_cast<const Choreographer*>(choreographer);
465}
466
Rachel Lee4879d812021-08-25 11:50:11 -0700467static inline const ChoreographerFrameCallbackDataImpl*
468AChoreographerFrameCallbackData_to_ChoreographerFrameCallbackDataImpl(
469 const AChoreographerFrameCallbackData* data) {
470 return reinterpret_cast<const ChoreographerFrameCallbackDataImpl*>(data);
471}
472
Alec Mouri271de042020-04-27 22:38:19 -0700473// Glue for private C api
474namespace android {
475void AChoreographer_signalRefreshRateCallbacks(nsecs_t vsyncPeriod) EXCLUDES(gChoreographers.lock) {
476 std::lock_guard<std::mutex> _l(gChoreographers.lock);
477 gChoreographers.mLastKnownVsync.store(vsyncPeriod);
478 for (auto c : gChoreographers.ptrs) {
479 c->scheduleLatestConfigRequest();
480 }
481}
482
483void AChoreographer_initJVM(JNIEnv* env) {
484 env->GetJavaVM(&gJni.jvm);
485 // Now we need to find the java classes.
486 jclass dmgClass = env->FindClass("android/hardware/display/DisplayManagerGlobal");
487 gJni.displayManagerGlobal.clazz = static_cast<jclass>(env->NewGlobalRef(dmgClass));
488 gJni.displayManagerGlobal.getInstance =
489 env->GetStaticMethodID(dmgClass, "getInstance",
490 "()Landroid/hardware/display/DisplayManagerGlobal;");
491 gJni.displayManagerGlobal.registerNativeChoreographerForRefreshRateCallbacks =
492 env->GetMethodID(dmgClass, "registerNativeChoreographerForRefreshRateCallbacks", "()V");
493 gJni.displayManagerGlobal.unregisterNativeChoreographerForRefreshRateCallbacks =
494 env->GetMethodID(dmgClass, "unregisterNativeChoreographerForRefreshRateCallbacks",
495 "()V");
496}
497
498AChoreographer* AChoreographer_routeGetInstance() {
499 return AChoreographer_getInstance();
500}
501void AChoreographer_routePostFrameCallback(AChoreographer* choreographer,
502 AChoreographer_frameCallback callback, void* data) {
Jiyong Park8cdf0762020-08-13 20:21:57 +0900503#pragma clang diagnostic push
504#pragma clang diagnostic ignored "-Wdeprecated-declarations"
Alec Mouri271de042020-04-27 22:38:19 -0700505 return AChoreographer_postFrameCallback(choreographer, callback, data);
Jiyong Park8cdf0762020-08-13 20:21:57 +0900506#pragma clang diagnostic pop
Alec Mouri271de042020-04-27 22:38:19 -0700507}
508void AChoreographer_routePostFrameCallbackDelayed(AChoreographer* choreographer,
509 AChoreographer_frameCallback callback, void* data,
510 long delayMillis) {
Jiyong Park8cdf0762020-08-13 20:21:57 +0900511#pragma clang diagnostic push
512#pragma clang diagnostic ignored "-Wdeprecated-declarations"
Alec Mouri271de042020-04-27 22:38:19 -0700513 return AChoreographer_postFrameCallbackDelayed(choreographer, callback, data, delayMillis);
Jiyong Park8cdf0762020-08-13 20:21:57 +0900514#pragma clang diagnostic pop
Alec Mouri271de042020-04-27 22:38:19 -0700515}
516void AChoreographer_routePostFrameCallback64(AChoreographer* choreographer,
517 AChoreographer_frameCallback64 callback, void* data) {
518 return AChoreographer_postFrameCallback64(choreographer, callback, data);
519}
520void AChoreographer_routePostFrameCallbackDelayed64(AChoreographer* choreographer,
521 AChoreographer_frameCallback64 callback,
522 void* data, uint32_t delayMillis) {
523 return AChoreographer_postFrameCallbackDelayed64(choreographer, callback, data, delayMillis);
524}
Rachel Lee4879d812021-08-25 11:50:11 -0700525void AChoreographer_routePostExtendedFrameCallback(AChoreographer* choreographer,
526 AChoreographer_extendedFrameCallback callback,
527 void* data) {
528 return AChoreographer_postExtendedFrameCallback(choreographer, callback, data);
529}
Alec Mouri271de042020-04-27 22:38:19 -0700530void AChoreographer_routeRegisterRefreshRateCallback(AChoreographer* choreographer,
531 AChoreographer_refreshRateCallback callback,
532 void* data) {
533 return AChoreographer_registerRefreshRateCallback(choreographer, callback, data);
534}
535void AChoreographer_routeUnregisterRefreshRateCallback(AChoreographer* choreographer,
536 AChoreographer_refreshRateCallback callback,
537 void* data) {
538 return AChoreographer_unregisterRefreshRateCallback(choreographer, callback, data);
539}
Rachel Lee4879d812021-08-25 11:50:11 -0700540int64_t AChoreographerFrameCallbackData_routeGetFrameTimeNanos(
541 const AChoreographerFrameCallbackData* data) {
542 return AChoreographerFrameCallbackData_getFrameTimeNanos(data);
543}
544size_t AChoreographerFrameCallbackData_routeGetFrameTimelinesLength(
545 const AChoreographerFrameCallbackData* data) {
546 return AChoreographerFrameCallbackData_getFrameTimelinesLength(data);
547}
548size_t AChoreographerFrameCallbackData_routeGetPreferredFrameTimelineIndex(
549 const AChoreographerFrameCallbackData* data) {
550 return AChoreographerFrameCallbackData_getPreferredFrameTimelineIndex(data);
551}
Rachel Lee1fb2ddc2022-01-12 14:33:07 -0800552AVsyncId AChoreographerFrameCallbackData_routeGetFrameTimelineVsyncId(
Rachel Lee4879d812021-08-25 11:50:11 -0700553 const AChoreographerFrameCallbackData* data, size_t index) {
554 return AChoreographerFrameCallbackData_getFrameTimelineVsyncId(data, index);
555}
Rachel Lee2825fa22022-01-12 17:35:16 -0800556int64_t AChoreographerFrameCallbackData_routeGetFrameTimelineExpectedPresentTimeNanos(
Rachel Lee4879d812021-08-25 11:50:11 -0700557 const AChoreographerFrameCallbackData* data, size_t index) {
Rachel Lee2825fa22022-01-12 17:35:16 -0800558 return AChoreographerFrameCallbackData_getFrameTimelineExpectedPresentTimeNanos(data, index);
Rachel Lee4879d812021-08-25 11:50:11 -0700559}
Rachel Lee2825fa22022-01-12 17:35:16 -0800560int64_t AChoreographerFrameCallbackData_routeGetFrameTimelineDeadlineNanos(
Rachel Lee4879d812021-08-25 11:50:11 -0700561 const AChoreographerFrameCallbackData* data, size_t index) {
Rachel Lee2825fa22022-01-12 17:35:16 -0800562 return AChoreographerFrameCallbackData_getFrameTimelineDeadlineNanos(data, index);
Rachel Lee4879d812021-08-25 11:50:11 -0700563}
Alec Mouri271de042020-04-27 22:38:19 -0700564
Jorim Jaggic0086af2021-02-12 18:18:11 +0100565int64_t AChoreographer_getFrameInterval(const AChoreographer* choreographer) {
566 return AChoreographer_to_Choreographer(choreographer)->getFrameInterval();
567}
568
Alec Mouri271de042020-04-27 22:38:19 -0700569} // namespace android
570
571/* Glue for the NDK interface */
572
Alec Mouricc445222019-10-22 10:19:17 -0700573static inline AChoreographer* Choreographer_to_AChoreographer(Choreographer* choreographer) {
574 return reinterpret_cast<AChoreographer*>(choreographer);
575}
576
577AChoreographer* AChoreographer_getInstance() {
578 return Choreographer_to_AChoreographer(Choreographer::getForThread());
579}
580
581void AChoreographer_postFrameCallback(AChoreographer* choreographer,
Rachel Lee4879d812021-08-25 11:50:11 -0700582 AChoreographer_frameCallback callback, void* data) {
583 AChoreographer_to_Choreographer(choreographer)
584 ->postFrameCallbackDelayed(callback, nullptr, nullptr, data, 0);
Alec Mouricc445222019-10-22 10:19:17 -0700585}
586void AChoreographer_postFrameCallbackDelayed(AChoreographer* choreographer,
Rachel Lee4879d812021-08-25 11:50:11 -0700587 AChoreographer_frameCallback callback, void* data,
588 long delayMillis) {
589 AChoreographer_to_Choreographer(choreographer)
590 ->postFrameCallbackDelayed(callback, nullptr, nullptr, data, ms2ns(delayMillis));
591}
592void AChoreographer_postExtendedFrameCallback(AChoreographer* choreographer,
593 AChoreographer_extendedFrameCallback callback,
594 void* data) {
595 AChoreographer_to_Choreographer(choreographer)
596 ->postFrameCallbackDelayed(nullptr, nullptr, callback, data, 0);
Alec Mouricc445222019-10-22 10:19:17 -0700597}
598void AChoreographer_postFrameCallback64(AChoreographer* choreographer,
Rachel Lee4879d812021-08-25 11:50:11 -0700599 AChoreographer_frameCallback64 callback, void* data) {
600 AChoreographer_to_Choreographer(choreographer)
601 ->postFrameCallbackDelayed(nullptr, callback, nullptr, data, 0);
Alec Mouricc445222019-10-22 10:19:17 -0700602}
603void AChoreographer_postFrameCallbackDelayed64(AChoreographer* choreographer,
Rachel Lee4879d812021-08-25 11:50:11 -0700604 AChoreographer_frameCallback64 callback, void* data,
605 uint32_t delayMillis) {
606 AChoreographer_to_Choreographer(choreographer)
607 ->postFrameCallbackDelayed(nullptr, callback, nullptr, data, ms2ns(delayMillis));
Alec Mouricc445222019-10-22 10:19:17 -0700608}
Alec Mouri60aee1c2019-10-28 16:18:59 -0700609void AChoreographer_registerRefreshRateCallback(AChoreographer* choreographer,
610 AChoreographer_refreshRateCallback callback,
611 void* data) {
612 AChoreographer_to_Choreographer(choreographer)->registerRefreshRateCallback(callback, data);
613}
614void AChoreographer_unregisterRefreshRateCallback(AChoreographer* choreographer,
Alec Mouri33682e92020-01-10 15:11:15 -0800615 AChoreographer_refreshRateCallback callback,
616 void* data) {
617 AChoreographer_to_Choreographer(choreographer)->unregisterRefreshRateCallback(callback, data);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700618}
Alec Mouri50a931d2019-11-19 16:23:59 -0800619
Rachel Lee4879d812021-08-25 11:50:11 -0700620int64_t AChoreographerFrameCallbackData_getFrameTimeNanos(
621 const AChoreographerFrameCallbackData* data) {
622 const ChoreographerFrameCallbackDataImpl* frameCallbackData =
623 AChoreographerFrameCallbackData_to_ChoreographerFrameCallbackDataImpl(data);
624 LOG_ALWAYS_FATAL_IF(!frameCallbackData->choreographer->inCallback(),
625 "Data is only valid in callback");
626 return frameCallbackData->frameTimeNanos;
627}
628size_t AChoreographerFrameCallbackData_getFrameTimelinesLength(
629 const AChoreographerFrameCallbackData* data) {
630 const ChoreographerFrameCallbackDataImpl* frameCallbackData =
631 AChoreographerFrameCallbackData_to_ChoreographerFrameCallbackDataImpl(data);
632 LOG_ALWAYS_FATAL_IF(!frameCallbackData->choreographer->inCallback(),
633 "Data is only valid in callback");
Rachel Leeb9c5a772022-02-04 21:17:37 -0800634 return VsyncEventData::kFrameTimelinesLength;
Rachel Lee4879d812021-08-25 11:50:11 -0700635}
636size_t AChoreographerFrameCallbackData_getPreferredFrameTimelineIndex(
637 const AChoreographerFrameCallbackData* data) {
638 const ChoreographerFrameCallbackDataImpl* frameCallbackData =
639 AChoreographerFrameCallbackData_to_ChoreographerFrameCallbackDataImpl(data);
640 LOG_ALWAYS_FATAL_IF(!frameCallbackData->choreographer->inCallback(),
641 "Data is only valid in callback");
Rachel Leeb9c5a772022-02-04 21:17:37 -0800642 return frameCallbackData->vsyncEventData.preferredFrameTimelineIndex;
Rachel Lee4879d812021-08-25 11:50:11 -0700643}
Rachel Lee1fb2ddc2022-01-12 14:33:07 -0800644AVsyncId AChoreographerFrameCallbackData_getFrameTimelineVsyncId(
Rachel Lee4879d812021-08-25 11:50:11 -0700645 const AChoreographerFrameCallbackData* data, size_t index) {
646 const ChoreographerFrameCallbackDataImpl* frameCallbackData =
647 AChoreographerFrameCallbackData_to_ChoreographerFrameCallbackDataImpl(data);
648 LOG_ALWAYS_FATAL_IF(!frameCallbackData->choreographer->inCallback(),
649 "Data is only valid in callback");
Rachel Leeb9c5a772022-02-04 21:17:37 -0800650 LOG_ALWAYS_FATAL_IF(index >= VsyncEventData::kFrameTimelinesLength, "Index out of bounds");
651 return frameCallbackData->vsyncEventData.frameTimelines[index].vsyncId;
Rachel Lee4879d812021-08-25 11:50:11 -0700652}
Rachel Lee2825fa22022-01-12 17:35:16 -0800653int64_t AChoreographerFrameCallbackData_getFrameTimelineExpectedPresentTimeNanos(
Rachel Lee4879d812021-08-25 11:50:11 -0700654 const AChoreographerFrameCallbackData* data, size_t index) {
655 const ChoreographerFrameCallbackDataImpl* frameCallbackData =
656 AChoreographerFrameCallbackData_to_ChoreographerFrameCallbackDataImpl(data);
657 LOG_ALWAYS_FATAL_IF(!frameCallbackData->choreographer->inCallback(),
658 "Data is only valid in callback");
Rachel Leeb9c5a772022-02-04 21:17:37 -0800659 LOG_ALWAYS_FATAL_IF(index >= VsyncEventData::kFrameTimelinesLength, "Index out of bounds");
660 return frameCallbackData->vsyncEventData.frameTimelines[index].expectedPresentationTime;
Rachel Lee4879d812021-08-25 11:50:11 -0700661}
Rachel Lee2825fa22022-01-12 17:35:16 -0800662int64_t AChoreographerFrameCallbackData_getFrameTimelineDeadlineNanos(
Rachel Lee4879d812021-08-25 11:50:11 -0700663 const AChoreographerFrameCallbackData* data, size_t index) {
664 const ChoreographerFrameCallbackDataImpl* frameCallbackData =
665 AChoreographerFrameCallbackData_to_ChoreographerFrameCallbackDataImpl(data);
666 LOG_ALWAYS_FATAL_IF(!frameCallbackData->choreographer->inCallback(),
667 "Data is only valid in callback");
Rachel Leeb9c5a772022-02-04 21:17:37 -0800668 LOG_ALWAYS_FATAL_IF(index >= VsyncEventData::kFrameTimelinesLength, "Index out of bounds");
669 return frameCallbackData->vsyncEventData.frameTimelines[index].deadlineTimestamp;
Rachel Lee4879d812021-08-25 11:50:11 -0700670}
671
Alec Mouri50a931d2019-11-19 16:23:59 -0800672AChoreographer* AChoreographer_create() {
673 Choreographer* choreographer = new Choreographer(nullptr);
674 status_t result = choreographer->initialize();
675 if (result != OK) {
676 ALOGW("Failed to initialize");
677 return nullptr;
678 }
679 return Choreographer_to_AChoreographer(choreographer);
680}
681
682void AChoreographer_destroy(AChoreographer* choreographer) {
683 if (choreographer == nullptr) {
684 return;
685 }
686
687 delete AChoreographer_to_Choreographer(choreographer);
688}
689
Alec Mouri921b2772020-02-05 19:03:28 -0800690int AChoreographer_getFd(const AChoreographer* choreographer) {
Alec Mouri50a931d2019-11-19 16:23:59 -0800691 return AChoreographer_to_Choreographer(choreographer)->getFd();
692}
693
694void AChoreographer_handlePendingEvents(AChoreographer* choreographer, void* data) {
695 // Pass dummy fd and events args to handleEvent, since the underlying
696 // DisplayEventDispatcher doesn't need them outside of validating that a
697 // Looper instance didn't break, but these args circumvent those checks.
Alec Mouri271de042020-04-27 22:38:19 -0700698 Choreographer* impl = AChoreographer_to_Choreographer(choreographer);
699 impl->handleEvent(-1, Looper::EVENT_INPUT, data);
Alec Mouri50a931d2019-11-19 16:23:59 -0800700}