blob: 79d9b9313cc4af06b8bcbc117b4ecd9c2431056d [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;
Rachel Lee4879d812021-08-25 11:50:11 -070080 AChoreographer_extendedFrameCallback extendedCallback;
Alec Mouricc445222019-10-22 10:19:17 -070081 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
Rachel Lee4879d812021-08-25 11:50:11 -070099/**
100 * Implementation of AChoreographerFrameCallbackData.
101 */
102struct ChoreographerFrameCallbackDataImpl {
Rachel Leecaaa47d2021-10-29 18:03:52 +0000103 struct FrameTimeline {
104 int64_t vsyncId{0};
105 int64_t expectedPresentTimeNanos{0};
106 int64_t deadlineNanos{0};
107 };
108
Rachel Lee4879d812021-08-25 11:50:11 -0700109 int64_t frameTimeNanos{0};
110
Rachel Leecaaa47d2021-10-29 18:03:52 +0000111 size_t frameTimelinesLength;
112
113 std::vector<FrameTimeline> frameTimelines;
Rachel Lee4879d812021-08-25 11:50:11 -0700114
115 size_t preferredFrameTimelineIndex;
116
117 const Choreographer* choreographer;
118};
119
Alec Mouri271de042020-04-27 22:38:19 -0700120struct {
121 std::mutex lock;
122 std::vector<Choreographer*> ptrs GUARDED_BY(lock);
123 bool registeredToDisplayManager GUARDED_BY(lock) = false;
124
125 std::atomic<nsecs_t> mLastKnownVsync = -1;
126} gChoreographers;
127
Alec Mouricc445222019-10-22 10:19:17 -0700128class Choreographer : public DisplayEventDispatcher, public MessageHandler {
129public:
Alec Mouri271de042020-04-27 22:38:19 -0700130 explicit Choreographer(const sp<Looper>& looper) EXCLUDES(gChoreographers.lock);
Alec Mouricc445222019-10-22 10:19:17 -0700131 void postFrameCallbackDelayed(AChoreographer_frameCallback cb,
Rachel Lee4879d812021-08-25 11:50:11 -0700132 AChoreographer_frameCallback64 cb64,
133 AChoreographer_extendedFrameCallback extendedCallback, void* data,
134 nsecs_t delay);
Alec Mouri271de042020-04-27 22:38:19 -0700135 void registerRefreshRateCallback(AChoreographer_refreshRateCallback cb, void* data)
136 EXCLUDES(gChoreographers.lock);
Alec Mouri33682e92020-01-10 15:11:15 -0800137 void unregisterRefreshRateCallback(AChoreographer_refreshRateCallback cb, void* data);
Alec Mouri271de042020-04-27 22:38:19 -0700138 // Drains the queue of pending vsync periods and dispatches refresh rate
139 // updates to callbacks.
140 // The assumption is that this method is only called on a single
141 // processing thread, either by looper or by AChoreographer_handleEvents
142 void handleRefreshRateUpdates();
143 void scheduleLatestConfigRequest();
Alec Mouricc445222019-10-22 10:19:17 -0700144
145 enum {
146 MSG_SCHEDULE_CALLBACKS = 0,
Alec Mouri271de042020-04-27 22:38:19 -0700147 MSG_SCHEDULE_VSYNC = 1,
148 MSG_HANDLE_REFRESH_RATE_UPDATES = 2,
Alec Mouricc445222019-10-22 10:19:17 -0700149 };
150 virtual void handleMessage(const Message& message) override;
151
152 static Choreographer* getForThread();
Alec Mouri271de042020-04-27 22:38:19 -0700153 virtual ~Choreographer() override EXCLUDES(gChoreographers.lock);
Jorim Jaggic0086af2021-02-12 18:18:11 +0100154 int64_t getFrameInterval() const;
Rachel Lee4879d812021-08-25 11:50:11 -0700155 bool inCallback() const;
Alec Mouricc445222019-10-22 10:19:17 -0700156
157private:
Alec Mouricc445222019-10-22 10:19:17 -0700158 Choreographer(const Choreographer&) = delete;
159
Ady Abraham74e17562020-08-24 18:18:19 -0700160 void dispatchVsync(nsecs_t timestamp, PhysicalDisplayId displayId, uint32_t count,
Ady Abraham0d28d762020-10-05 17:50:41 -0700161 VsyncEventData vsyncEventData) override;
Alec Mouricc445222019-10-22 10:19:17 -0700162 void dispatchHotplug(nsecs_t timestamp, PhysicalDisplayId displayId, bool connected) override;
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100163 void dispatchModeChanged(nsecs_t timestamp, PhysicalDisplayId displayId, int32_t modeId,
164 nsecs_t vsyncPeriod) override;
Alec Mouri967d5d72020-08-05 12:50:03 -0700165 void dispatchNullEvent(nsecs_t, PhysicalDisplayId) override;
Ady Abraham62f216c2020-10-13 19:07:23 -0700166 void dispatchFrameRateOverrides(nsecs_t timestamp, PhysicalDisplayId displayId,
167 std::vector<FrameRateOverride> overrides) override;
Alec Mouricc445222019-10-22 10:19:17 -0700168
169 void scheduleCallbacks();
170
Rachel Lee4879d812021-08-25 11:50:11 -0700171 ChoreographerFrameCallbackDataImpl createFrameCallbackData(nsecs_t timestamp) const;
172
Alec Mouri271de042020-04-27 22:38:19 -0700173 std::mutex mLock;
Alec Mouricc445222019-10-22 10:19:17 -0700174 // Protected by mLock
Alec Mouri60aee1c2019-10-28 16:18:59 -0700175 std::priority_queue<FrameCallback> mFrameCallbacks;
Alec Mouri60aee1c2019-10-28 16:18:59 -0700176 std::vector<RefreshRateCallback> mRefreshRateCallbacks;
Alec Mouricc445222019-10-22 10:19:17 -0700177
Alec Mouri271de042020-04-27 22:38:19 -0700178 nsecs_t mLatestVsyncPeriod = -1;
Ady Abraham0d28d762020-10-05 17:50:41 -0700179 VsyncEventData mLastVsyncEventData;
Rachel Lee4879d812021-08-25 11:50:11 -0700180 bool mInCallback = false;
Alec Mouricc445222019-10-22 10:19:17 -0700181
182 const sp<Looper> mLooper;
183 const std::thread::id mThreadId;
184};
185
Alec Mouricc445222019-10-22 10:19:17 -0700186static thread_local Choreographer* gChoreographer;
187Choreographer* Choreographer::getForThread() {
188 if (gChoreographer == nullptr) {
189 sp<Looper> looper = Looper::getForThread();
190 if (!looper.get()) {
191 ALOGW("No looper prepared for thread");
192 return nullptr;
193 }
194 gChoreographer = new Choreographer(looper);
195 status_t result = gChoreographer->initialize();
196 if (result != OK) {
197 ALOGW("Failed to initialize");
198 return nullptr;
199 }
200 }
201 return gChoreographer;
202}
203
Alec Mouri60aee1c2019-10-28 16:18:59 -0700204Choreographer::Choreographer(const sp<Looper>& looper)
Ady Abraham62f216c2020-10-13 19:07:23 -0700205 : DisplayEventDispatcher(looper, ISurfaceComposer::VsyncSource::eVsyncSourceApp),
Alec Mouri60aee1c2019-10-28 16:18:59 -0700206 mLooper(looper),
Alec Mouri271de042020-04-27 22:38:19 -0700207 mThreadId(std::this_thread::get_id()) {
208 std::lock_guard<std::mutex> _l(gChoreographers.lock);
209 gChoreographers.ptrs.push_back(this);
210}
211
212Choreographer::~Choreographer() {
213 std::lock_guard<std::mutex> _l(gChoreographers.lock);
214 gChoreographers.ptrs.erase(std::remove_if(gChoreographers.ptrs.begin(),
215 gChoreographers.ptrs.end(),
216 [=](Choreographer* c) { return c == this; }),
217 gChoreographers.ptrs.end());
218 // Only poke DisplayManagerGlobal to unregister if we previously registered
219 // callbacks.
220 if (gChoreographers.ptrs.empty() && gChoreographers.registeredToDisplayManager) {
Ady Abraham6a8986b2021-08-18 13:44:14 -0700221 gChoreographers.registeredToDisplayManager = false;
Alec Mouri271de042020-04-27 22:38:19 -0700222 JNIEnv* env = getJniEnv();
223 if (env == nullptr) {
224 ALOGW("JNI environment is unavailable, skipping choreographer cleanup");
225 return;
226 }
227 jobject dmg = env->CallStaticObjectMethod(gJni.displayManagerGlobal.clazz,
228 gJni.displayManagerGlobal.getInstance);
229 if (dmg == nullptr) {
230 ALOGW("DMS is not initialized yet, skipping choreographer cleanup");
231 } else {
232 env->CallVoidMethod(dmg,
233 gJni.displayManagerGlobal
234 .unregisterNativeChoreographerForRefreshRateCallbacks);
235 env->DeleteLocalRef(dmg);
236 }
237 }
238}
Alec Mouricc445222019-10-22 10:19:17 -0700239
Rachel Lee4879d812021-08-25 11:50:11 -0700240void Choreographer::postFrameCallbackDelayed(AChoreographer_frameCallback cb,
241 AChoreographer_frameCallback64 cb64,
242 AChoreographer_extendedFrameCallback extendedCallback,
243 void* data, nsecs_t delay) {
Alec Mouricc445222019-10-22 10:19:17 -0700244 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Rachel Lee4879d812021-08-25 11:50:11 -0700245 FrameCallback callback{cb, cb64, extendedCallback, data, now + delay};
Alec Mouricc445222019-10-22 10:19:17 -0700246 {
Alec Mouri271de042020-04-27 22:38:19 -0700247 std::lock_guard<std::mutex> _l{mLock};
Alec Mouri60aee1c2019-10-28 16:18:59 -0700248 mFrameCallbacks.push(callback);
Alec Mouricc445222019-10-22 10:19:17 -0700249 }
250 if (callback.dueTime <= now) {
251 if (std::this_thread::get_id() != mThreadId) {
Alec Mouri50a931d2019-11-19 16:23:59 -0800252 if (mLooper != nullptr) {
253 Message m{MSG_SCHEDULE_VSYNC};
254 mLooper->sendMessage(this, m);
255 } else {
256 scheduleVsync();
257 }
Alec Mouricc445222019-10-22 10:19:17 -0700258 } else {
259 scheduleVsync();
260 }
261 } else {
Alec Mouri50a931d2019-11-19 16:23:59 -0800262 if (mLooper != nullptr) {
263 Message m{MSG_SCHEDULE_CALLBACKS};
264 mLooper->sendMessageDelayed(delay, this, m);
265 } else {
266 scheduleCallbacks();
267 }
Alec Mouricc445222019-10-22 10:19:17 -0700268 }
269}
270
Alec Mouri60aee1c2019-10-28 16:18:59 -0700271void Choreographer::registerRefreshRateCallback(AChoreographer_refreshRateCallback cb, void* data) {
Alec Mouri271de042020-04-27 22:38:19 -0700272 std::lock_guard<std::mutex> _l{mLock};
273 for (const auto& callback : mRefreshRateCallbacks) {
274 // Don't re-add callbacks.
275 if (cb == callback.callback && data == callback.data) {
276 return;
277 }
278 }
279 mRefreshRateCallbacks.emplace_back(
280 RefreshRateCallback{.callback = cb, .data = data, .firstCallbackFired = false});
281 bool needsRegistration = false;
Alec Mouri60aee1c2019-10-28 16:18:59 -0700282 {
Alec Mouri271de042020-04-27 22:38:19 -0700283 std::lock_guard<std::mutex> _l2(gChoreographers.lock);
284 needsRegistration = !gChoreographers.registeredToDisplayManager;
285 }
286 if (needsRegistration) {
287 JNIEnv* env = getJniEnv();
Alec Mouri271de042020-04-27 22:38:19 -0700288 if (env == nullptr) {
Greg Kaiserb66d04b2020-05-11 06:52:15 -0700289 ALOGW("JNI environment is unavailable, skipping registration");
Alec Mouri271de042020-04-27 22:38:19 -0700290 return;
291 }
Greg Kaiserb66d04b2020-05-11 06:52:15 -0700292 jobject dmg = env->CallStaticObjectMethod(gJni.displayManagerGlobal.clazz,
293 gJni.displayManagerGlobal.getInstance);
Alec Mouri271de042020-04-27 22:38:19 -0700294 if (dmg == nullptr) {
295 ALOGW("DMS is not initialized yet: skipping registration");
296 return;
297 } else {
298 env->CallVoidMethod(dmg,
299 gJni.displayManagerGlobal
300 .registerNativeChoreographerForRefreshRateCallbacks,
301 reinterpret_cast<int64_t>(this));
302 env->DeleteLocalRef(dmg);
303 {
304 std::lock_guard<std::mutex> _l2(gChoreographers.lock);
305 gChoreographers.registeredToDisplayManager = true;
Alec Mouri33682e92020-01-10 15:11:15 -0800306 }
307 }
Alec Mouri271de042020-04-27 22:38:19 -0700308 } else {
309 scheduleLatestConfigRequest();
Alec Mouri60aee1c2019-10-28 16:18:59 -0700310 }
311}
312
Alec Mouri33682e92020-01-10 15:11:15 -0800313void Choreographer::unregisterRefreshRateCallback(AChoreographer_refreshRateCallback cb,
314 void* data) {
Alec Mouri271de042020-04-27 22:38:19 -0700315 std::lock_guard<std::mutex> _l{mLock};
316 mRefreshRateCallbacks.erase(std::remove_if(mRefreshRateCallbacks.begin(),
317 mRefreshRateCallbacks.end(),
318 [&](const RefreshRateCallback& callback) {
319 return cb == callback.callback &&
320 data == callback.data;
321 }),
322 mRefreshRateCallbacks.end());
323}
324
325void Choreographer::scheduleLatestConfigRequest() {
326 if (mLooper != nullptr) {
327 Message m{MSG_HANDLE_REFRESH_RATE_UPDATES};
328 mLooper->sendMessage(this, m);
329 } else {
330 // If the looper thread is detached from Choreographer, then refresh rate
331 // changes will be handled in AChoreographer_handlePendingEvents, so we
Alec Mouri967d5d72020-08-05 12:50:03 -0700332 // need to wake up the looper thread by writing to the write-end of the
333 // socket the looper is listening on.
334 // Fortunately, these events are small so sending packets across the
335 // socket should be atomic across processes.
336 DisplayEventReceiver::Event event;
Dominik Laskowskif1833852021-03-23 15:06:50 -0700337 event.header =
338 DisplayEventReceiver::Event::Header{DisplayEventReceiver::DISPLAY_EVENT_NULL,
339 PhysicalDisplayId::fromPort(0), systemTime()};
Alec Mouri967d5d72020-08-05 12:50:03 -0700340 injectEvent(event);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700341 }
342}
343
Alec Mouricc445222019-10-22 10:19:17 -0700344void Choreographer::scheduleCallbacks() {
Alec Mouri134266b2020-03-03 19:22:29 -0800345 const nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
346 nsecs_t dueTime;
347 {
Alec Mouri271de042020-04-27 22:38:19 -0700348 std::lock_guard<std::mutex> _l{mLock};
Alec Mouri134266b2020-03-03 19:22:29 -0800349 // If there are no pending callbacks then don't schedule a vsync
350 if (mFrameCallbacks.empty()) {
351 return;
352 }
353 dueTime = mFrameCallbacks.top().dueTime;
354 }
355
356 if (dueTime <= now) {
Alec Mouricc445222019-10-22 10:19:17 -0700357 ALOGV("choreographer %p ~ scheduling vsync", this);
358 scheduleVsync();
359 return;
360 }
361}
362
Alec Mouri271de042020-04-27 22:38:19 -0700363void Choreographer::handleRefreshRateUpdates() {
364 std::vector<RefreshRateCallback> callbacks{};
365 const nsecs_t pendingPeriod = gChoreographers.mLastKnownVsync.load();
366 const nsecs_t lastPeriod = mLatestVsyncPeriod;
367 if (pendingPeriod > 0) {
368 mLatestVsyncPeriod = pendingPeriod;
369 }
370 {
371 std::lock_guard<std::mutex> _l{mLock};
372 for (auto& cb : mRefreshRateCallbacks) {
373 callbacks.push_back(cb);
374 cb.firstCallbackFired = true;
375 }
376 }
377
378 for (auto& cb : callbacks) {
379 if (!cb.firstCallbackFired || (pendingPeriod > 0 && pendingPeriod != lastPeriod)) {
380 cb.callback(pendingPeriod, cb.data);
381 }
382 }
383}
384
Alec Mouricc445222019-10-22 10:19:17 -0700385// TODO(b/74619554): The PhysicalDisplayId is ignored because SF only emits VSYNC events for the
386// internal display and DisplayEventReceiver::requestNextVsync only allows requesting VSYNC for
387// the internal display implicitly.
Ady Abraham0d28d762020-10-05 17:50:41 -0700388void Choreographer::dispatchVsync(nsecs_t timestamp, PhysicalDisplayId, uint32_t,
389 VsyncEventData vsyncEventData) {
Alec Mouricc445222019-10-22 10:19:17 -0700390 std::vector<FrameCallback> callbacks{};
391 {
Alec Mouri271de042020-04-27 22:38:19 -0700392 std::lock_guard<std::mutex> _l{mLock};
Alec Mouricc445222019-10-22 10:19:17 -0700393 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700394 while (!mFrameCallbacks.empty() && mFrameCallbacks.top().dueTime < now) {
395 callbacks.push_back(mFrameCallbacks.top());
396 mFrameCallbacks.pop();
Alec Mouricc445222019-10-22 10:19:17 -0700397 }
398 }
Ady Abraham0d28d762020-10-05 17:50:41 -0700399 mLastVsyncEventData = vsyncEventData;
Alec Mouricc445222019-10-22 10:19:17 -0700400 for (const auto& cb : callbacks) {
Rachel Lee4879d812021-08-25 11:50:11 -0700401 if (cb.extendedCallback != nullptr) {
402 const ChoreographerFrameCallbackDataImpl frameCallbackData =
403 createFrameCallbackData(timestamp);
404 mInCallback = true;
405 cb.extendedCallback(reinterpret_cast<const AChoreographerFrameCallbackData*>(
406 &frameCallbackData),
407 cb.data);
408 mInCallback = false;
409 } else if (cb.callback64 != nullptr) {
Alec Mouricc445222019-10-22 10:19:17 -0700410 cb.callback64(timestamp, cb.data);
411 } else if (cb.callback != nullptr) {
412 cb.callback(timestamp, cb.data);
413 }
414 }
415}
416
417void Choreographer::dispatchHotplug(nsecs_t, PhysicalDisplayId displayId, bool connected) {
Rachel Lee4879d812021-08-25 11:50:11 -0700418 ALOGV("choreographer %p ~ received hotplug event (displayId=%s, connected=%s), ignoring.", this,
419 to_string(displayId).c_str(), toString(connected));
Alec Mouricc445222019-10-22 10:19:17 -0700420}
421
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100422void Choreographer::dispatchModeChanged(nsecs_t, PhysicalDisplayId, int32_t, nsecs_t) {
423 LOG_ALWAYS_FATAL("dispatchModeChanged was called but was never registered");
Ady Abraham62f216c2020-10-13 19:07:23 -0700424}
425
426void Choreographer::dispatchFrameRateOverrides(nsecs_t, PhysicalDisplayId,
427 std::vector<FrameRateOverride>) {
428 LOG_ALWAYS_FATAL("dispatchFrameRateOverrides was called but was never registered");
Alec Mouri967d5d72020-08-05 12:50:03 -0700429}
Alec Mouri271de042020-04-27 22:38:19 -0700430
Alec Mouri967d5d72020-08-05 12:50:03 -0700431void Choreographer::dispatchNullEvent(nsecs_t, PhysicalDisplayId) {
432 ALOGV("choreographer %p ~ received null event.", this);
433 handleRefreshRateUpdates();
Alec Mouricc445222019-10-22 10:19:17 -0700434}
435
436void Choreographer::handleMessage(const Message& message) {
437 switch (message.what) {
Rachel Lee4879d812021-08-25 11:50:11 -0700438 case MSG_SCHEDULE_CALLBACKS:
439 scheduleCallbacks();
440 break;
441 case MSG_SCHEDULE_VSYNC:
442 scheduleVsync();
443 break;
444 case MSG_HANDLE_REFRESH_RATE_UPDATES:
445 handleRefreshRateUpdates();
446 break;
Alec Mouricc445222019-10-22 10:19:17 -0700447 }
448}
449
Jorim Jaggic0086af2021-02-12 18:18:11 +0100450int64_t Choreographer::getFrameInterval() const {
451 return mLastVsyncEventData.frameInterval;
452}
453
Rachel Lee4879d812021-08-25 11:50:11 -0700454bool Choreographer::inCallback() const {
455 return mInCallback;
456}
457
458ChoreographerFrameCallbackDataImpl Choreographer::createFrameCallbackData(nsecs_t timestamp) const {
Rachel Leecaaa47d2021-10-29 18:03:52 +0000459 std::vector<ChoreographerFrameCallbackDataImpl::FrameTimeline> frameTimelines;
460 frameTimelines.push_back({.vsyncId = mLastVsyncEventData.id,
461 .expectedPresentTimeNanos = mLastVsyncEventData.expectedPresentTime,
462 .deadlineNanos = mLastVsyncEventData.deadlineTimestamp});
Rachel Lee4879d812021-08-25 11:50:11 -0700463 return {.frameTimeNanos = timestamp,
Rachel Leecaaa47d2021-10-29 18:03:52 +0000464 .frameTimelinesLength = 1,
465 .preferredFrameTimelineIndex = 0,
466 .frameTimelines = frameTimelines,
Rachel Lee4879d812021-08-25 11:50:11 -0700467 .choreographer = this};
468}
469
Alec Mouri271de042020-04-27 22:38:19 -0700470} // namespace android
Alec Mouri50a931d2019-11-19 16:23:59 -0800471using namespace android;
Alec Mouricc445222019-10-22 10:19:17 -0700472
473static inline Choreographer* AChoreographer_to_Choreographer(AChoreographer* choreographer) {
474 return reinterpret_cast<Choreographer*>(choreographer);
475}
476
Ady Abraham74e17562020-08-24 18:18:19 -0700477static inline const Choreographer* AChoreographer_to_Choreographer(
478 const AChoreographer* choreographer) {
479 return reinterpret_cast<const Choreographer*>(choreographer);
480}
481
Rachel Lee4879d812021-08-25 11:50:11 -0700482static inline const ChoreographerFrameCallbackDataImpl*
483AChoreographerFrameCallbackData_to_ChoreographerFrameCallbackDataImpl(
484 const AChoreographerFrameCallbackData* data) {
485 return reinterpret_cast<const ChoreographerFrameCallbackDataImpl*>(data);
486}
487
Alec Mouri271de042020-04-27 22:38:19 -0700488// Glue for private C api
489namespace android {
490void AChoreographer_signalRefreshRateCallbacks(nsecs_t vsyncPeriod) EXCLUDES(gChoreographers.lock) {
491 std::lock_guard<std::mutex> _l(gChoreographers.lock);
492 gChoreographers.mLastKnownVsync.store(vsyncPeriod);
493 for (auto c : gChoreographers.ptrs) {
494 c->scheduleLatestConfigRequest();
495 }
496}
497
498void AChoreographer_initJVM(JNIEnv* env) {
499 env->GetJavaVM(&gJni.jvm);
500 // Now we need to find the java classes.
501 jclass dmgClass = env->FindClass("android/hardware/display/DisplayManagerGlobal");
502 gJni.displayManagerGlobal.clazz = static_cast<jclass>(env->NewGlobalRef(dmgClass));
503 gJni.displayManagerGlobal.getInstance =
504 env->GetStaticMethodID(dmgClass, "getInstance",
505 "()Landroid/hardware/display/DisplayManagerGlobal;");
506 gJni.displayManagerGlobal.registerNativeChoreographerForRefreshRateCallbacks =
507 env->GetMethodID(dmgClass, "registerNativeChoreographerForRefreshRateCallbacks", "()V");
508 gJni.displayManagerGlobal.unregisterNativeChoreographerForRefreshRateCallbacks =
509 env->GetMethodID(dmgClass, "unregisterNativeChoreographerForRefreshRateCallbacks",
510 "()V");
511}
512
513AChoreographer* AChoreographer_routeGetInstance() {
514 return AChoreographer_getInstance();
515}
516void AChoreographer_routePostFrameCallback(AChoreographer* choreographer,
517 AChoreographer_frameCallback callback, void* data) {
Jiyong Park8cdf0762020-08-13 20:21:57 +0900518#pragma clang diagnostic push
519#pragma clang diagnostic ignored "-Wdeprecated-declarations"
Alec Mouri271de042020-04-27 22:38:19 -0700520 return AChoreographer_postFrameCallback(choreographer, callback, data);
Jiyong Park8cdf0762020-08-13 20:21:57 +0900521#pragma clang diagnostic pop
Alec Mouri271de042020-04-27 22:38:19 -0700522}
523void AChoreographer_routePostFrameCallbackDelayed(AChoreographer* choreographer,
524 AChoreographer_frameCallback callback, void* data,
525 long delayMillis) {
Jiyong Park8cdf0762020-08-13 20:21:57 +0900526#pragma clang diagnostic push
527#pragma clang diagnostic ignored "-Wdeprecated-declarations"
Alec Mouri271de042020-04-27 22:38:19 -0700528 return AChoreographer_postFrameCallbackDelayed(choreographer, callback, data, delayMillis);
Jiyong Park8cdf0762020-08-13 20:21:57 +0900529#pragma clang diagnostic pop
Alec Mouri271de042020-04-27 22:38:19 -0700530}
531void AChoreographer_routePostFrameCallback64(AChoreographer* choreographer,
532 AChoreographer_frameCallback64 callback, void* data) {
533 return AChoreographer_postFrameCallback64(choreographer, callback, data);
534}
535void AChoreographer_routePostFrameCallbackDelayed64(AChoreographer* choreographer,
536 AChoreographer_frameCallback64 callback,
537 void* data, uint32_t delayMillis) {
538 return AChoreographer_postFrameCallbackDelayed64(choreographer, callback, data, delayMillis);
539}
Rachel Lee4879d812021-08-25 11:50:11 -0700540void AChoreographer_routePostExtendedFrameCallback(AChoreographer* choreographer,
541 AChoreographer_extendedFrameCallback callback,
542 void* data) {
543 return AChoreographer_postExtendedFrameCallback(choreographer, callback, data);
544}
Alec Mouri271de042020-04-27 22:38:19 -0700545void AChoreographer_routeRegisterRefreshRateCallback(AChoreographer* choreographer,
546 AChoreographer_refreshRateCallback callback,
547 void* data) {
548 return AChoreographer_registerRefreshRateCallback(choreographer, callback, data);
549}
550void AChoreographer_routeUnregisterRefreshRateCallback(AChoreographer* choreographer,
551 AChoreographer_refreshRateCallback callback,
552 void* data) {
553 return AChoreographer_unregisterRefreshRateCallback(choreographer, callback, data);
554}
Rachel Lee4879d812021-08-25 11:50:11 -0700555int64_t AChoreographerFrameCallbackData_routeGetFrameTimeNanos(
556 const AChoreographerFrameCallbackData* data) {
557 return AChoreographerFrameCallbackData_getFrameTimeNanos(data);
558}
559size_t AChoreographerFrameCallbackData_routeGetFrameTimelinesLength(
560 const AChoreographerFrameCallbackData* data) {
561 return AChoreographerFrameCallbackData_getFrameTimelinesLength(data);
562}
563size_t AChoreographerFrameCallbackData_routeGetPreferredFrameTimelineIndex(
564 const AChoreographerFrameCallbackData* data) {
565 return AChoreographerFrameCallbackData_getPreferredFrameTimelineIndex(data);
566}
567int64_t AChoreographerFrameCallbackData_routeGetFrameTimelineVsyncId(
568 const AChoreographerFrameCallbackData* data, size_t index) {
569 return AChoreographerFrameCallbackData_getFrameTimelineVsyncId(data, index);
570}
571int64_t AChoreographerFrameCallbackData_routeGetFrameTimelineExpectedPresentTime(
572 const AChoreographerFrameCallbackData* data, size_t index) {
573 return AChoreographerFrameCallbackData_getFrameTimelineExpectedPresentTime(data, index);
574}
575int64_t AChoreographerFrameCallbackData_routeGetFrameTimelineDeadline(
576 const AChoreographerFrameCallbackData* data, size_t index) {
577 return AChoreographerFrameCallbackData_getFrameTimelineDeadline(data, index);
578}
Alec Mouri271de042020-04-27 22:38:19 -0700579
Jorim Jaggic0086af2021-02-12 18:18:11 +0100580int64_t AChoreographer_getFrameInterval(const AChoreographer* choreographer) {
581 return AChoreographer_to_Choreographer(choreographer)->getFrameInterval();
582}
583
Alec Mouri271de042020-04-27 22:38:19 -0700584} // namespace android
585
586/* Glue for the NDK interface */
587
Alec Mouricc445222019-10-22 10:19:17 -0700588static inline AChoreographer* Choreographer_to_AChoreographer(Choreographer* choreographer) {
589 return reinterpret_cast<AChoreographer*>(choreographer);
590}
591
592AChoreographer* AChoreographer_getInstance() {
593 return Choreographer_to_AChoreographer(Choreographer::getForThread());
594}
595
596void AChoreographer_postFrameCallback(AChoreographer* choreographer,
Rachel Lee4879d812021-08-25 11:50:11 -0700597 AChoreographer_frameCallback callback, void* data) {
598 AChoreographer_to_Choreographer(choreographer)
599 ->postFrameCallbackDelayed(callback, nullptr, nullptr, data, 0);
Alec Mouricc445222019-10-22 10:19:17 -0700600}
601void AChoreographer_postFrameCallbackDelayed(AChoreographer* choreographer,
Rachel Lee4879d812021-08-25 11:50:11 -0700602 AChoreographer_frameCallback callback, void* data,
603 long delayMillis) {
604 AChoreographer_to_Choreographer(choreographer)
605 ->postFrameCallbackDelayed(callback, nullptr, nullptr, data, ms2ns(delayMillis));
606}
607void AChoreographer_postExtendedFrameCallback(AChoreographer* choreographer,
608 AChoreographer_extendedFrameCallback callback,
609 void* data) {
610 AChoreographer_to_Choreographer(choreographer)
611 ->postFrameCallbackDelayed(nullptr, nullptr, callback, data, 0);
Alec Mouricc445222019-10-22 10:19:17 -0700612}
613void AChoreographer_postFrameCallback64(AChoreographer* choreographer,
Rachel Lee4879d812021-08-25 11:50:11 -0700614 AChoreographer_frameCallback64 callback, void* data) {
615 AChoreographer_to_Choreographer(choreographer)
616 ->postFrameCallbackDelayed(nullptr, callback, nullptr, data, 0);
Alec Mouricc445222019-10-22 10:19:17 -0700617}
618void AChoreographer_postFrameCallbackDelayed64(AChoreographer* choreographer,
Rachel Lee4879d812021-08-25 11:50:11 -0700619 AChoreographer_frameCallback64 callback, void* data,
620 uint32_t delayMillis) {
621 AChoreographer_to_Choreographer(choreographer)
622 ->postFrameCallbackDelayed(nullptr, callback, nullptr, data, ms2ns(delayMillis));
Alec Mouricc445222019-10-22 10:19:17 -0700623}
Alec Mouri60aee1c2019-10-28 16:18:59 -0700624void AChoreographer_registerRefreshRateCallback(AChoreographer* choreographer,
625 AChoreographer_refreshRateCallback callback,
626 void* data) {
627 AChoreographer_to_Choreographer(choreographer)->registerRefreshRateCallback(callback, data);
628}
629void AChoreographer_unregisterRefreshRateCallback(AChoreographer* choreographer,
Alec Mouri33682e92020-01-10 15:11:15 -0800630 AChoreographer_refreshRateCallback callback,
631 void* data) {
632 AChoreographer_to_Choreographer(choreographer)->unregisterRefreshRateCallback(callback, data);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700633}
Alec Mouri50a931d2019-11-19 16:23:59 -0800634
Rachel Lee4879d812021-08-25 11:50:11 -0700635int64_t AChoreographerFrameCallbackData_getFrameTimeNanos(
636 const AChoreographerFrameCallbackData* data) {
637 const ChoreographerFrameCallbackDataImpl* frameCallbackData =
638 AChoreographerFrameCallbackData_to_ChoreographerFrameCallbackDataImpl(data);
639 LOG_ALWAYS_FATAL_IF(!frameCallbackData->choreographer->inCallback(),
640 "Data is only valid in callback");
641 return frameCallbackData->frameTimeNanos;
642}
643size_t AChoreographerFrameCallbackData_getFrameTimelinesLength(
644 const AChoreographerFrameCallbackData* data) {
645 const ChoreographerFrameCallbackDataImpl* frameCallbackData =
646 AChoreographerFrameCallbackData_to_ChoreographerFrameCallbackDataImpl(data);
647 LOG_ALWAYS_FATAL_IF(!frameCallbackData->choreographer->inCallback(),
648 "Data is only valid in callback");
Rachel Leecaaa47d2021-10-29 18:03:52 +0000649 return frameCallbackData->frameTimelinesLength;
Rachel Lee4879d812021-08-25 11:50:11 -0700650}
651size_t AChoreographerFrameCallbackData_getPreferredFrameTimelineIndex(
652 const AChoreographerFrameCallbackData* data) {
653 const ChoreographerFrameCallbackDataImpl* frameCallbackData =
654 AChoreographerFrameCallbackData_to_ChoreographerFrameCallbackDataImpl(data);
655 LOG_ALWAYS_FATAL_IF(!frameCallbackData->choreographer->inCallback(),
656 "Data is only valid in callback");
657 return frameCallbackData->preferredFrameTimelineIndex;
658}
659int64_t AChoreographerFrameCallbackData_getFrameTimelineVsyncId(
660 const AChoreographerFrameCallbackData* data, size_t index) {
661 const ChoreographerFrameCallbackDataImpl* frameCallbackData =
662 AChoreographerFrameCallbackData_to_ChoreographerFrameCallbackDataImpl(data);
663 LOG_ALWAYS_FATAL_IF(!frameCallbackData->choreographer->inCallback(),
664 "Data is only valid in callback");
Rachel Leecaaa47d2021-10-29 18:03:52 +0000665 LOG_ALWAYS_FATAL_IF(index >= frameCallbackData->frameTimelinesLength, "Index out of bounds");
666 return frameCallbackData->frameTimelines[index].vsyncId;
Rachel Lee4879d812021-08-25 11:50:11 -0700667}
668int64_t AChoreographerFrameCallbackData_getFrameTimelineExpectedPresentTime(
669 const AChoreographerFrameCallbackData* data, size_t index) {
670 const ChoreographerFrameCallbackDataImpl* frameCallbackData =
671 AChoreographerFrameCallbackData_to_ChoreographerFrameCallbackDataImpl(data);
672 LOG_ALWAYS_FATAL_IF(!frameCallbackData->choreographer->inCallback(),
673 "Data is only valid in callback");
Rachel Leecaaa47d2021-10-29 18:03:52 +0000674 LOG_ALWAYS_FATAL_IF(index >= frameCallbackData->frameTimelinesLength, "Index out of bounds");
675 return frameCallbackData->frameTimelines[index].expectedPresentTimeNanos;
Rachel Lee4879d812021-08-25 11:50:11 -0700676}
677int64_t AChoreographerFrameCallbackData_getFrameTimelineDeadline(
678 const AChoreographerFrameCallbackData* data, size_t index) {
679 const ChoreographerFrameCallbackDataImpl* frameCallbackData =
680 AChoreographerFrameCallbackData_to_ChoreographerFrameCallbackDataImpl(data);
681 LOG_ALWAYS_FATAL_IF(!frameCallbackData->choreographer->inCallback(),
682 "Data is only valid in callback");
Rachel Leecaaa47d2021-10-29 18:03:52 +0000683 LOG_ALWAYS_FATAL_IF(index >= frameCallbackData->frameTimelinesLength, "Index out of bounds");
684 return frameCallbackData->frameTimelines[index].deadlineNanos;
Rachel Lee4879d812021-08-25 11:50:11 -0700685}
686
Alec Mouri50a931d2019-11-19 16:23:59 -0800687AChoreographer* AChoreographer_create() {
688 Choreographer* choreographer = new Choreographer(nullptr);
689 status_t result = choreographer->initialize();
690 if (result != OK) {
691 ALOGW("Failed to initialize");
692 return nullptr;
693 }
694 return Choreographer_to_AChoreographer(choreographer);
695}
696
697void AChoreographer_destroy(AChoreographer* choreographer) {
698 if (choreographer == nullptr) {
699 return;
700 }
701
702 delete AChoreographer_to_Choreographer(choreographer);
703}
704
Alec Mouri921b2772020-02-05 19:03:28 -0800705int AChoreographer_getFd(const AChoreographer* choreographer) {
Alec Mouri50a931d2019-11-19 16:23:59 -0800706 return AChoreographer_to_Choreographer(choreographer)->getFd();
707}
708
709void AChoreographer_handlePendingEvents(AChoreographer* choreographer, void* data) {
710 // Pass dummy fd and events args to handleEvent, since the underlying
711 // DisplayEventDispatcher doesn't need them outside of validating that a
712 // Looper instance didn't break, but these args circumvent those checks.
Alec Mouri271de042020-04-27 22:38:19 -0700713 Choreographer* impl = AChoreographer_to_Choreographer(choreographer);
714 impl->handleEvent(-1, Looper::EVENT_INPUT, data);
Alec Mouri50a931d2019-11-19 16:23:59 -0800715}