blob: b182a4a6f1ac222b231c24af27f65e016e6d270b [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 Lee18c34372022-01-20 13:57:18 -0800106 std::array<VsyncEventData::FrameTimeline, VsyncEventData::kFrameTimelinesLength> frameTimelines;
Rachel Lee4879d812021-08-25 11:50:11 -0700107
108 size_t preferredFrameTimelineIndex;
109
110 const Choreographer* choreographer;
111};
112
Alec Mouri271de042020-04-27 22:38:19 -0700113struct {
114 std::mutex lock;
115 std::vector<Choreographer*> ptrs GUARDED_BY(lock);
116 bool registeredToDisplayManager GUARDED_BY(lock) = false;
117
118 std::atomic<nsecs_t> mLastKnownVsync = -1;
119} gChoreographers;
120
Alec Mouricc445222019-10-22 10:19:17 -0700121class Choreographer : public DisplayEventDispatcher, public MessageHandler {
122public:
Alec Mouri271de042020-04-27 22:38:19 -0700123 explicit Choreographer(const sp<Looper>& looper) EXCLUDES(gChoreographers.lock);
Alec Mouricc445222019-10-22 10:19:17 -0700124 void postFrameCallbackDelayed(AChoreographer_frameCallback cb,
Rachel Lee4879d812021-08-25 11:50:11 -0700125 AChoreographer_frameCallback64 cb64,
126 AChoreographer_extendedFrameCallback extendedCallback, void* data,
127 nsecs_t delay);
Alec Mouri271de042020-04-27 22:38:19 -0700128 void registerRefreshRateCallback(AChoreographer_refreshRateCallback cb, void* data)
129 EXCLUDES(gChoreographers.lock);
Alec Mouri33682e92020-01-10 15:11:15 -0800130 void unregisterRefreshRateCallback(AChoreographer_refreshRateCallback cb, void* data);
Alec Mouri271de042020-04-27 22:38:19 -0700131 // Drains the queue of pending vsync periods and dispatches refresh rate
132 // updates to callbacks.
133 // The assumption is that this method is only called on a single
134 // processing thread, either by looper or by AChoreographer_handleEvents
135 void handleRefreshRateUpdates();
136 void scheduleLatestConfigRequest();
Alec Mouricc445222019-10-22 10:19:17 -0700137
138 enum {
139 MSG_SCHEDULE_CALLBACKS = 0,
Alec Mouri271de042020-04-27 22:38:19 -0700140 MSG_SCHEDULE_VSYNC = 1,
141 MSG_HANDLE_REFRESH_RATE_UPDATES = 2,
Alec Mouricc445222019-10-22 10:19:17 -0700142 };
143 virtual void handleMessage(const Message& message) override;
144
145 static Choreographer* getForThread();
Alec Mouri271de042020-04-27 22:38:19 -0700146 virtual ~Choreographer() override EXCLUDES(gChoreographers.lock);
Jorim Jaggic0086af2021-02-12 18:18:11 +0100147 int64_t getFrameInterval() const;
Rachel Lee4879d812021-08-25 11:50:11 -0700148 bool inCallback() const;
Alec Mouricc445222019-10-22 10:19:17 -0700149
150private:
Alec Mouricc445222019-10-22 10:19:17 -0700151 Choreographer(const Choreographer&) = delete;
152
Ady Abraham74e17562020-08-24 18:18:19 -0700153 void dispatchVsync(nsecs_t timestamp, PhysicalDisplayId displayId, uint32_t count,
Ady Abraham0d28d762020-10-05 17:50:41 -0700154 VsyncEventData vsyncEventData) override;
Alec Mouricc445222019-10-22 10:19:17 -0700155 void dispatchHotplug(nsecs_t timestamp, PhysicalDisplayId displayId, bool connected) override;
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100156 void dispatchModeChanged(nsecs_t timestamp, PhysicalDisplayId displayId, int32_t modeId,
157 nsecs_t vsyncPeriod) override;
Alec Mouri967d5d72020-08-05 12:50:03 -0700158 void dispatchNullEvent(nsecs_t, PhysicalDisplayId) override;
Ady Abraham62f216c2020-10-13 19:07:23 -0700159 void dispatchFrameRateOverrides(nsecs_t timestamp, PhysicalDisplayId displayId,
160 std::vector<FrameRateOverride> overrides) override;
Alec Mouricc445222019-10-22 10:19:17 -0700161
162 void scheduleCallbacks();
163
Rachel Lee4879d812021-08-25 11:50:11 -0700164 ChoreographerFrameCallbackDataImpl createFrameCallbackData(nsecs_t timestamp) const;
165
Alec Mouri271de042020-04-27 22:38:19 -0700166 std::mutex mLock;
Alec Mouricc445222019-10-22 10:19:17 -0700167 // Protected by mLock
Alec Mouri60aee1c2019-10-28 16:18:59 -0700168 std::priority_queue<FrameCallback> mFrameCallbacks;
Alec Mouri60aee1c2019-10-28 16:18:59 -0700169 std::vector<RefreshRateCallback> mRefreshRateCallbacks;
Alec Mouricc445222019-10-22 10:19:17 -0700170
Alec Mouri271de042020-04-27 22:38:19 -0700171 nsecs_t mLatestVsyncPeriod = -1;
Ady Abraham0d28d762020-10-05 17:50:41 -0700172 VsyncEventData mLastVsyncEventData;
Rachel Lee4879d812021-08-25 11:50:11 -0700173 bool mInCallback = false;
Alec Mouricc445222019-10-22 10:19:17 -0700174
175 const sp<Looper> mLooper;
176 const std::thread::id mThreadId;
177};
178
Alec Mouricc445222019-10-22 10:19:17 -0700179static thread_local Choreographer* gChoreographer;
180Choreographer* Choreographer::getForThread() {
181 if (gChoreographer == nullptr) {
182 sp<Looper> looper = Looper::getForThread();
183 if (!looper.get()) {
184 ALOGW("No looper prepared for thread");
185 return nullptr;
186 }
187 gChoreographer = new Choreographer(looper);
188 status_t result = gChoreographer->initialize();
189 if (result != OK) {
190 ALOGW("Failed to initialize");
191 return nullptr;
192 }
193 }
194 return gChoreographer;
195}
196
Alec Mouri60aee1c2019-10-28 16:18:59 -0700197Choreographer::Choreographer(const sp<Looper>& looper)
Ady Abraham62f216c2020-10-13 19:07:23 -0700198 : DisplayEventDispatcher(looper, ISurfaceComposer::VsyncSource::eVsyncSourceApp),
Alec Mouri60aee1c2019-10-28 16:18:59 -0700199 mLooper(looper),
Alec Mouri271de042020-04-27 22:38:19 -0700200 mThreadId(std::this_thread::get_id()) {
201 std::lock_guard<std::mutex> _l(gChoreographers.lock);
202 gChoreographers.ptrs.push_back(this);
203}
204
205Choreographer::~Choreographer() {
206 std::lock_guard<std::mutex> _l(gChoreographers.lock);
207 gChoreographers.ptrs.erase(std::remove_if(gChoreographers.ptrs.begin(),
208 gChoreographers.ptrs.end(),
209 [=](Choreographer* c) { return c == this; }),
210 gChoreographers.ptrs.end());
211 // Only poke DisplayManagerGlobal to unregister if we previously registered
212 // callbacks.
213 if (gChoreographers.ptrs.empty() && gChoreographers.registeredToDisplayManager) {
Ady Abraham6a8986b2021-08-18 13:44:14 -0700214 gChoreographers.registeredToDisplayManager = false;
Alec Mouri271de042020-04-27 22:38:19 -0700215 JNIEnv* env = getJniEnv();
216 if (env == nullptr) {
217 ALOGW("JNI environment is unavailable, skipping choreographer cleanup");
218 return;
219 }
220 jobject dmg = env->CallStaticObjectMethod(gJni.displayManagerGlobal.clazz,
221 gJni.displayManagerGlobal.getInstance);
222 if (dmg == nullptr) {
223 ALOGW("DMS is not initialized yet, skipping choreographer cleanup");
224 } else {
225 env->CallVoidMethod(dmg,
226 gJni.displayManagerGlobal
227 .unregisterNativeChoreographerForRefreshRateCallbacks);
228 env->DeleteLocalRef(dmg);
229 }
230 }
231}
Alec Mouricc445222019-10-22 10:19:17 -0700232
Rachel Lee4879d812021-08-25 11:50:11 -0700233void Choreographer::postFrameCallbackDelayed(AChoreographer_frameCallback cb,
234 AChoreographer_frameCallback64 cb64,
235 AChoreographer_extendedFrameCallback extendedCallback,
236 void* data, nsecs_t delay) {
Alec Mouricc445222019-10-22 10:19:17 -0700237 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Rachel Lee4879d812021-08-25 11:50:11 -0700238 FrameCallback callback{cb, cb64, extendedCallback, data, now + delay};
Alec Mouricc445222019-10-22 10:19:17 -0700239 {
Alec Mouri271de042020-04-27 22:38:19 -0700240 std::lock_guard<std::mutex> _l{mLock};
Alec Mouri60aee1c2019-10-28 16:18:59 -0700241 mFrameCallbacks.push(callback);
Alec Mouricc445222019-10-22 10:19:17 -0700242 }
243 if (callback.dueTime <= now) {
244 if (std::this_thread::get_id() != mThreadId) {
Alec Mouri50a931d2019-11-19 16:23:59 -0800245 if (mLooper != nullptr) {
246 Message m{MSG_SCHEDULE_VSYNC};
247 mLooper->sendMessage(this, m);
248 } else {
249 scheduleVsync();
250 }
Alec Mouricc445222019-10-22 10:19:17 -0700251 } else {
252 scheduleVsync();
253 }
254 } else {
Alec Mouri50a931d2019-11-19 16:23:59 -0800255 if (mLooper != nullptr) {
256 Message m{MSG_SCHEDULE_CALLBACKS};
257 mLooper->sendMessageDelayed(delay, this, m);
258 } else {
259 scheduleCallbacks();
260 }
Alec Mouricc445222019-10-22 10:19:17 -0700261 }
262}
263
Alec Mouri60aee1c2019-10-28 16:18:59 -0700264void Choreographer::registerRefreshRateCallback(AChoreographer_refreshRateCallback cb, void* data) {
Alec Mouri271de042020-04-27 22:38:19 -0700265 std::lock_guard<std::mutex> _l{mLock};
266 for (const auto& callback : mRefreshRateCallbacks) {
267 // Don't re-add callbacks.
268 if (cb == callback.callback && data == callback.data) {
269 return;
270 }
271 }
272 mRefreshRateCallbacks.emplace_back(
273 RefreshRateCallback{.callback = cb, .data = data, .firstCallbackFired = false});
274 bool needsRegistration = false;
Alec Mouri60aee1c2019-10-28 16:18:59 -0700275 {
Alec Mouri271de042020-04-27 22:38:19 -0700276 std::lock_guard<std::mutex> _l2(gChoreographers.lock);
277 needsRegistration = !gChoreographers.registeredToDisplayManager;
278 }
279 if (needsRegistration) {
280 JNIEnv* env = getJniEnv();
Alec Mouri271de042020-04-27 22:38:19 -0700281 if (env == nullptr) {
Greg Kaiserb66d04b2020-05-11 06:52:15 -0700282 ALOGW("JNI environment is unavailable, skipping registration");
Alec Mouri271de042020-04-27 22:38:19 -0700283 return;
284 }
Greg Kaiserb66d04b2020-05-11 06:52:15 -0700285 jobject dmg = env->CallStaticObjectMethod(gJni.displayManagerGlobal.clazz,
286 gJni.displayManagerGlobal.getInstance);
Alec Mouri271de042020-04-27 22:38:19 -0700287 if (dmg == nullptr) {
288 ALOGW("DMS is not initialized yet: skipping registration");
289 return;
290 } else {
291 env->CallVoidMethod(dmg,
292 gJni.displayManagerGlobal
293 .registerNativeChoreographerForRefreshRateCallbacks,
294 reinterpret_cast<int64_t>(this));
295 env->DeleteLocalRef(dmg);
296 {
297 std::lock_guard<std::mutex> _l2(gChoreographers.lock);
298 gChoreographers.registeredToDisplayManager = true;
Alec Mouri33682e92020-01-10 15:11:15 -0800299 }
300 }
Alec Mouri271de042020-04-27 22:38:19 -0700301 } else {
302 scheduleLatestConfigRequest();
Alec Mouri60aee1c2019-10-28 16:18:59 -0700303 }
304}
305
Alec Mouri33682e92020-01-10 15:11:15 -0800306void Choreographer::unregisterRefreshRateCallback(AChoreographer_refreshRateCallback cb,
307 void* data) {
Alec Mouri271de042020-04-27 22:38:19 -0700308 std::lock_guard<std::mutex> _l{mLock};
309 mRefreshRateCallbacks.erase(std::remove_if(mRefreshRateCallbacks.begin(),
310 mRefreshRateCallbacks.end(),
311 [&](const RefreshRateCallback& callback) {
312 return cb == callback.callback &&
313 data == callback.data;
314 }),
315 mRefreshRateCallbacks.end());
316}
317
318void Choreographer::scheduleLatestConfigRequest() {
319 if (mLooper != nullptr) {
320 Message m{MSG_HANDLE_REFRESH_RATE_UPDATES};
321 mLooper->sendMessage(this, m);
322 } else {
323 // If the looper thread is detached from Choreographer, then refresh rate
324 // changes will be handled in AChoreographer_handlePendingEvents, so we
Alec Mouri967d5d72020-08-05 12:50:03 -0700325 // need to wake up the looper thread by writing to the write-end of the
326 // socket the looper is listening on.
327 // Fortunately, these events are small so sending packets across the
328 // socket should be atomic across processes.
329 DisplayEventReceiver::Event event;
Dominik Laskowskif1833852021-03-23 15:06:50 -0700330 event.header =
331 DisplayEventReceiver::Event::Header{DisplayEventReceiver::DISPLAY_EVENT_NULL,
332 PhysicalDisplayId::fromPort(0), systemTime()};
Alec Mouri967d5d72020-08-05 12:50:03 -0700333 injectEvent(event);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700334 }
335}
336
Alec Mouricc445222019-10-22 10:19:17 -0700337void Choreographer::scheduleCallbacks() {
Alec Mouri134266b2020-03-03 19:22:29 -0800338 const nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
339 nsecs_t dueTime;
340 {
Alec Mouri271de042020-04-27 22:38:19 -0700341 std::lock_guard<std::mutex> _l{mLock};
Alec Mouri134266b2020-03-03 19:22:29 -0800342 // If there are no pending callbacks then don't schedule a vsync
343 if (mFrameCallbacks.empty()) {
344 return;
345 }
346 dueTime = mFrameCallbacks.top().dueTime;
347 }
348
349 if (dueTime <= now) {
Alec Mouricc445222019-10-22 10:19:17 -0700350 ALOGV("choreographer %p ~ scheduling vsync", this);
351 scheduleVsync();
352 return;
353 }
354}
355
Alec Mouri271de042020-04-27 22:38:19 -0700356void Choreographer::handleRefreshRateUpdates() {
357 std::vector<RefreshRateCallback> callbacks{};
358 const nsecs_t pendingPeriod = gChoreographers.mLastKnownVsync.load();
359 const nsecs_t lastPeriod = mLatestVsyncPeriod;
360 if (pendingPeriod > 0) {
361 mLatestVsyncPeriod = pendingPeriod;
362 }
363 {
364 std::lock_guard<std::mutex> _l{mLock};
365 for (auto& cb : mRefreshRateCallbacks) {
366 callbacks.push_back(cb);
367 cb.firstCallbackFired = true;
368 }
369 }
370
371 for (auto& cb : callbacks) {
372 if (!cb.firstCallbackFired || (pendingPeriod > 0 && pendingPeriod != lastPeriod)) {
373 cb.callback(pendingPeriod, cb.data);
374 }
375 }
376}
377
Alec Mouricc445222019-10-22 10:19:17 -0700378// TODO(b/74619554): The PhysicalDisplayId is ignored because SF only emits VSYNC events for the
379// internal display and DisplayEventReceiver::requestNextVsync only allows requesting VSYNC for
380// the internal display implicitly.
Ady Abraham0d28d762020-10-05 17:50:41 -0700381void Choreographer::dispatchVsync(nsecs_t timestamp, PhysicalDisplayId, uint32_t,
382 VsyncEventData vsyncEventData) {
Alec Mouricc445222019-10-22 10:19:17 -0700383 std::vector<FrameCallback> callbacks{};
384 {
Alec Mouri271de042020-04-27 22:38:19 -0700385 std::lock_guard<std::mutex> _l{mLock};
Alec Mouricc445222019-10-22 10:19:17 -0700386 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700387 while (!mFrameCallbacks.empty() && mFrameCallbacks.top().dueTime < now) {
388 callbacks.push_back(mFrameCallbacks.top());
389 mFrameCallbacks.pop();
Alec Mouricc445222019-10-22 10:19:17 -0700390 }
391 }
Ady Abraham0d28d762020-10-05 17:50:41 -0700392 mLastVsyncEventData = vsyncEventData;
Alec Mouricc445222019-10-22 10:19:17 -0700393 for (const auto& cb : callbacks) {
Rachel Lee4879d812021-08-25 11:50:11 -0700394 if (cb.extendedCallback != nullptr) {
395 const ChoreographerFrameCallbackDataImpl frameCallbackData =
396 createFrameCallbackData(timestamp);
397 mInCallback = true;
398 cb.extendedCallback(reinterpret_cast<const AChoreographerFrameCallbackData*>(
399 &frameCallbackData),
400 cb.data);
401 mInCallback = false;
402 } else if (cb.callback64 != nullptr) {
Alec Mouricc445222019-10-22 10:19:17 -0700403 cb.callback64(timestamp, cb.data);
404 } else if (cb.callback != nullptr) {
405 cb.callback(timestamp, cb.data);
406 }
407 }
408}
409
410void Choreographer::dispatchHotplug(nsecs_t, PhysicalDisplayId displayId, bool connected) {
Rachel Lee4879d812021-08-25 11:50:11 -0700411 ALOGV("choreographer %p ~ received hotplug event (displayId=%s, connected=%s), ignoring.", this,
412 to_string(displayId).c_str(), toString(connected));
Alec Mouricc445222019-10-22 10:19:17 -0700413}
414
Marin Shalamanova7fe3042021-01-29 21:02:08 +0100415void Choreographer::dispatchModeChanged(nsecs_t, PhysicalDisplayId, int32_t, nsecs_t) {
416 LOG_ALWAYS_FATAL("dispatchModeChanged was called but was never registered");
Ady Abraham62f216c2020-10-13 19:07:23 -0700417}
418
419void Choreographer::dispatchFrameRateOverrides(nsecs_t, PhysicalDisplayId,
420 std::vector<FrameRateOverride>) {
421 LOG_ALWAYS_FATAL("dispatchFrameRateOverrides was called but was never registered");
Alec Mouri967d5d72020-08-05 12:50:03 -0700422}
Alec Mouri271de042020-04-27 22:38:19 -0700423
Alec Mouri967d5d72020-08-05 12:50:03 -0700424void Choreographer::dispatchNullEvent(nsecs_t, PhysicalDisplayId) {
425 ALOGV("choreographer %p ~ received null event.", this);
426 handleRefreshRateUpdates();
Alec Mouricc445222019-10-22 10:19:17 -0700427}
428
429void Choreographer::handleMessage(const Message& message) {
430 switch (message.what) {
Rachel Lee4879d812021-08-25 11:50:11 -0700431 case MSG_SCHEDULE_CALLBACKS:
432 scheduleCallbacks();
433 break;
434 case MSG_SCHEDULE_VSYNC:
435 scheduleVsync();
436 break;
437 case MSG_HANDLE_REFRESH_RATE_UPDATES:
438 handleRefreshRateUpdates();
439 break;
Alec Mouricc445222019-10-22 10:19:17 -0700440 }
441}
442
Jorim Jaggic0086af2021-02-12 18:18:11 +0100443int64_t Choreographer::getFrameInterval() const {
444 return mLastVsyncEventData.frameInterval;
445}
446
Rachel Lee4879d812021-08-25 11:50:11 -0700447bool Choreographer::inCallback() const {
448 return mInCallback;
449}
450
451ChoreographerFrameCallbackDataImpl Choreographer::createFrameCallbackData(nsecs_t timestamp) const {
Rachel Lee4879d812021-08-25 11:50:11 -0700452 return {.frameTimeNanos = timestamp,
Rachel Lee3f028662021-11-04 19:32:24 +0000453 .preferredFrameTimelineIndex = mLastVsyncEventData.preferredFrameTimelineIndex,
454 .frameTimelines = mLastVsyncEventData.frameTimelines,
Rachel Lee4879d812021-08-25 11:50:11 -0700455 .choreographer = this};
456}
457
Alec Mouri271de042020-04-27 22:38:19 -0700458} // namespace android
Alec Mouri50a931d2019-11-19 16:23:59 -0800459using namespace android;
Alec Mouricc445222019-10-22 10:19:17 -0700460
461static inline Choreographer* AChoreographer_to_Choreographer(AChoreographer* choreographer) {
462 return reinterpret_cast<Choreographer*>(choreographer);
463}
464
Ady Abraham74e17562020-08-24 18:18:19 -0700465static inline const Choreographer* AChoreographer_to_Choreographer(
466 const AChoreographer* choreographer) {
467 return reinterpret_cast<const Choreographer*>(choreographer);
468}
469
Rachel Lee4879d812021-08-25 11:50:11 -0700470static inline const ChoreographerFrameCallbackDataImpl*
471AChoreographerFrameCallbackData_to_ChoreographerFrameCallbackDataImpl(
472 const AChoreographerFrameCallbackData* data) {
473 return reinterpret_cast<const ChoreographerFrameCallbackDataImpl*>(data);
474}
475
Alec Mouri271de042020-04-27 22:38:19 -0700476// Glue for private C api
477namespace android {
478void AChoreographer_signalRefreshRateCallbacks(nsecs_t vsyncPeriod) EXCLUDES(gChoreographers.lock) {
479 std::lock_guard<std::mutex> _l(gChoreographers.lock);
480 gChoreographers.mLastKnownVsync.store(vsyncPeriod);
481 for (auto c : gChoreographers.ptrs) {
482 c->scheduleLatestConfigRequest();
483 }
484}
485
486void AChoreographer_initJVM(JNIEnv* env) {
487 env->GetJavaVM(&gJni.jvm);
488 // Now we need to find the java classes.
489 jclass dmgClass = env->FindClass("android/hardware/display/DisplayManagerGlobal");
490 gJni.displayManagerGlobal.clazz = static_cast<jclass>(env->NewGlobalRef(dmgClass));
491 gJni.displayManagerGlobal.getInstance =
492 env->GetStaticMethodID(dmgClass, "getInstance",
493 "()Landroid/hardware/display/DisplayManagerGlobal;");
494 gJni.displayManagerGlobal.registerNativeChoreographerForRefreshRateCallbacks =
495 env->GetMethodID(dmgClass, "registerNativeChoreographerForRefreshRateCallbacks", "()V");
496 gJni.displayManagerGlobal.unregisterNativeChoreographerForRefreshRateCallbacks =
497 env->GetMethodID(dmgClass, "unregisterNativeChoreographerForRefreshRateCallbacks",
498 "()V");
499}
500
501AChoreographer* AChoreographer_routeGetInstance() {
502 return AChoreographer_getInstance();
503}
504void AChoreographer_routePostFrameCallback(AChoreographer* choreographer,
505 AChoreographer_frameCallback callback, void* data) {
Jiyong Park8cdf0762020-08-13 20:21:57 +0900506#pragma clang diagnostic push
507#pragma clang diagnostic ignored "-Wdeprecated-declarations"
Alec Mouri271de042020-04-27 22:38:19 -0700508 return AChoreographer_postFrameCallback(choreographer, callback, data);
Jiyong Park8cdf0762020-08-13 20:21:57 +0900509#pragma clang diagnostic pop
Alec Mouri271de042020-04-27 22:38:19 -0700510}
511void AChoreographer_routePostFrameCallbackDelayed(AChoreographer* choreographer,
512 AChoreographer_frameCallback callback, void* data,
513 long delayMillis) {
Jiyong Park8cdf0762020-08-13 20:21:57 +0900514#pragma clang diagnostic push
515#pragma clang diagnostic ignored "-Wdeprecated-declarations"
Alec Mouri271de042020-04-27 22:38:19 -0700516 return AChoreographer_postFrameCallbackDelayed(choreographer, callback, data, delayMillis);
Jiyong Park8cdf0762020-08-13 20:21:57 +0900517#pragma clang diagnostic pop
Alec Mouri271de042020-04-27 22:38:19 -0700518}
519void AChoreographer_routePostFrameCallback64(AChoreographer* choreographer,
520 AChoreographer_frameCallback64 callback, void* data) {
521 return AChoreographer_postFrameCallback64(choreographer, callback, data);
522}
523void AChoreographer_routePostFrameCallbackDelayed64(AChoreographer* choreographer,
524 AChoreographer_frameCallback64 callback,
525 void* data, uint32_t delayMillis) {
526 return AChoreographer_postFrameCallbackDelayed64(choreographer, callback, data, delayMillis);
527}
Rachel Lee4879d812021-08-25 11:50:11 -0700528void AChoreographer_routePostExtendedFrameCallback(AChoreographer* choreographer,
529 AChoreographer_extendedFrameCallback callback,
530 void* data) {
531 return AChoreographer_postExtendedFrameCallback(choreographer, callback, data);
532}
Alec Mouri271de042020-04-27 22:38:19 -0700533void AChoreographer_routeRegisterRefreshRateCallback(AChoreographer* choreographer,
534 AChoreographer_refreshRateCallback callback,
535 void* data) {
536 return AChoreographer_registerRefreshRateCallback(choreographer, callback, data);
537}
538void AChoreographer_routeUnregisterRefreshRateCallback(AChoreographer* choreographer,
539 AChoreographer_refreshRateCallback callback,
540 void* data) {
541 return AChoreographer_unregisterRefreshRateCallback(choreographer, callback, data);
542}
Rachel Lee4879d812021-08-25 11:50:11 -0700543int64_t AChoreographerFrameCallbackData_routeGetFrameTimeNanos(
544 const AChoreographerFrameCallbackData* data) {
545 return AChoreographerFrameCallbackData_getFrameTimeNanos(data);
546}
547size_t AChoreographerFrameCallbackData_routeGetFrameTimelinesLength(
548 const AChoreographerFrameCallbackData* data) {
549 return AChoreographerFrameCallbackData_getFrameTimelinesLength(data);
550}
551size_t AChoreographerFrameCallbackData_routeGetPreferredFrameTimelineIndex(
552 const AChoreographerFrameCallbackData* data) {
553 return AChoreographerFrameCallbackData_getPreferredFrameTimelineIndex(data);
554}
Rachel Lee1fb2ddc2022-01-12 14:33:07 -0800555AVsyncId AChoreographerFrameCallbackData_routeGetFrameTimelineVsyncId(
Rachel Lee4879d812021-08-25 11:50:11 -0700556 const AChoreographerFrameCallbackData* data, size_t index) {
557 return AChoreographerFrameCallbackData_getFrameTimelineVsyncId(data, index);
558}
Rachel Lee2825fa22022-01-12 17:35:16 -0800559int64_t AChoreographerFrameCallbackData_routeGetFrameTimelineExpectedPresentTimeNanos(
Rachel Lee4879d812021-08-25 11:50:11 -0700560 const AChoreographerFrameCallbackData* data, size_t index) {
Rachel Lee2825fa22022-01-12 17:35:16 -0800561 return AChoreographerFrameCallbackData_getFrameTimelineExpectedPresentTimeNanos(data, index);
Rachel Lee4879d812021-08-25 11:50:11 -0700562}
Rachel Lee2825fa22022-01-12 17:35:16 -0800563int64_t AChoreographerFrameCallbackData_routeGetFrameTimelineDeadlineNanos(
Rachel Lee4879d812021-08-25 11:50:11 -0700564 const AChoreographerFrameCallbackData* data, size_t index) {
Rachel Lee2825fa22022-01-12 17:35:16 -0800565 return AChoreographerFrameCallbackData_getFrameTimelineDeadlineNanos(data, index);
Rachel Lee4879d812021-08-25 11:50:11 -0700566}
Alec Mouri271de042020-04-27 22:38:19 -0700567
Jorim Jaggic0086af2021-02-12 18:18:11 +0100568int64_t AChoreographer_getFrameInterval(const AChoreographer* choreographer) {
569 return AChoreographer_to_Choreographer(choreographer)->getFrameInterval();
570}
571
Alec Mouri271de042020-04-27 22:38:19 -0700572} // namespace android
573
574/* Glue for the NDK interface */
575
Alec Mouricc445222019-10-22 10:19:17 -0700576static inline AChoreographer* Choreographer_to_AChoreographer(Choreographer* choreographer) {
577 return reinterpret_cast<AChoreographer*>(choreographer);
578}
579
580AChoreographer* AChoreographer_getInstance() {
581 return Choreographer_to_AChoreographer(Choreographer::getForThread());
582}
583
584void AChoreographer_postFrameCallback(AChoreographer* choreographer,
Rachel Lee4879d812021-08-25 11:50:11 -0700585 AChoreographer_frameCallback callback, void* data) {
586 AChoreographer_to_Choreographer(choreographer)
587 ->postFrameCallbackDelayed(callback, nullptr, nullptr, data, 0);
Alec Mouricc445222019-10-22 10:19:17 -0700588}
589void AChoreographer_postFrameCallbackDelayed(AChoreographer* choreographer,
Rachel Lee4879d812021-08-25 11:50:11 -0700590 AChoreographer_frameCallback callback, void* data,
591 long delayMillis) {
592 AChoreographer_to_Choreographer(choreographer)
593 ->postFrameCallbackDelayed(callback, nullptr, nullptr, data, ms2ns(delayMillis));
594}
595void AChoreographer_postExtendedFrameCallback(AChoreographer* choreographer,
596 AChoreographer_extendedFrameCallback callback,
597 void* data) {
598 AChoreographer_to_Choreographer(choreographer)
599 ->postFrameCallbackDelayed(nullptr, nullptr, callback, data, 0);
Alec Mouricc445222019-10-22 10:19:17 -0700600}
601void AChoreographer_postFrameCallback64(AChoreographer* choreographer,
Rachel Lee4879d812021-08-25 11:50:11 -0700602 AChoreographer_frameCallback64 callback, void* data) {
603 AChoreographer_to_Choreographer(choreographer)
604 ->postFrameCallbackDelayed(nullptr, callback, nullptr, data, 0);
Alec Mouricc445222019-10-22 10:19:17 -0700605}
606void AChoreographer_postFrameCallbackDelayed64(AChoreographer* choreographer,
Rachel Lee4879d812021-08-25 11:50:11 -0700607 AChoreographer_frameCallback64 callback, void* data,
608 uint32_t delayMillis) {
609 AChoreographer_to_Choreographer(choreographer)
610 ->postFrameCallbackDelayed(nullptr, callback, nullptr, data, ms2ns(delayMillis));
Alec Mouricc445222019-10-22 10:19:17 -0700611}
Alec Mouri60aee1c2019-10-28 16:18:59 -0700612void AChoreographer_registerRefreshRateCallback(AChoreographer* choreographer,
613 AChoreographer_refreshRateCallback callback,
614 void* data) {
615 AChoreographer_to_Choreographer(choreographer)->registerRefreshRateCallback(callback, data);
616}
617void AChoreographer_unregisterRefreshRateCallback(AChoreographer* choreographer,
Alec Mouri33682e92020-01-10 15:11:15 -0800618 AChoreographer_refreshRateCallback callback,
619 void* data) {
620 AChoreographer_to_Choreographer(choreographer)->unregisterRefreshRateCallback(callback, data);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700621}
Alec Mouri50a931d2019-11-19 16:23:59 -0800622
Rachel Lee4879d812021-08-25 11:50:11 -0700623int64_t AChoreographerFrameCallbackData_getFrameTimeNanos(
624 const AChoreographerFrameCallbackData* data) {
625 const ChoreographerFrameCallbackDataImpl* frameCallbackData =
626 AChoreographerFrameCallbackData_to_ChoreographerFrameCallbackDataImpl(data);
627 LOG_ALWAYS_FATAL_IF(!frameCallbackData->choreographer->inCallback(),
628 "Data is only valid in callback");
629 return frameCallbackData->frameTimeNanos;
630}
631size_t AChoreographerFrameCallbackData_getFrameTimelinesLength(
632 const AChoreographerFrameCallbackData* data) {
633 const ChoreographerFrameCallbackDataImpl* frameCallbackData =
634 AChoreographerFrameCallbackData_to_ChoreographerFrameCallbackDataImpl(data);
635 LOG_ALWAYS_FATAL_IF(!frameCallbackData->choreographer->inCallback(),
636 "Data is only valid in callback");
Rachel Lee3f028662021-11-04 19:32:24 +0000637 return frameCallbackData->frameTimelines.size();
Rachel Lee4879d812021-08-25 11:50:11 -0700638}
639size_t AChoreographerFrameCallbackData_getPreferredFrameTimelineIndex(
640 const AChoreographerFrameCallbackData* data) {
641 const ChoreographerFrameCallbackDataImpl* frameCallbackData =
642 AChoreographerFrameCallbackData_to_ChoreographerFrameCallbackDataImpl(data);
643 LOG_ALWAYS_FATAL_IF(!frameCallbackData->choreographer->inCallback(),
644 "Data is only valid in callback");
645 return frameCallbackData->preferredFrameTimelineIndex;
646}
Rachel Lee1fb2ddc2022-01-12 14:33:07 -0800647AVsyncId AChoreographerFrameCallbackData_getFrameTimelineVsyncId(
Rachel Lee4879d812021-08-25 11:50:11 -0700648 const AChoreographerFrameCallbackData* data, size_t index) {
649 const ChoreographerFrameCallbackDataImpl* frameCallbackData =
650 AChoreographerFrameCallbackData_to_ChoreographerFrameCallbackDataImpl(data);
651 LOG_ALWAYS_FATAL_IF(!frameCallbackData->choreographer->inCallback(),
652 "Data is only valid in callback");
Rachel Lee3f028662021-11-04 19:32:24 +0000653 LOG_ALWAYS_FATAL_IF(index >= frameCallbackData->frameTimelines.size(), "Index out of bounds");
654 return frameCallbackData->frameTimelines[index].id;
Rachel Lee4879d812021-08-25 11:50:11 -0700655}
Rachel Lee2825fa22022-01-12 17:35:16 -0800656int64_t AChoreographerFrameCallbackData_getFrameTimelineExpectedPresentTimeNanos(
Rachel Lee4879d812021-08-25 11:50:11 -0700657 const AChoreographerFrameCallbackData* data, size_t index) {
658 const ChoreographerFrameCallbackDataImpl* frameCallbackData =
659 AChoreographerFrameCallbackData_to_ChoreographerFrameCallbackDataImpl(data);
660 LOG_ALWAYS_FATAL_IF(!frameCallbackData->choreographer->inCallback(),
661 "Data is only valid in callback");
Rachel Lee3f028662021-11-04 19:32:24 +0000662 LOG_ALWAYS_FATAL_IF(index >= frameCallbackData->frameTimelines.size(), "Index out of bounds");
663 return frameCallbackData->frameTimelines[index].expectedPresentTime;
Rachel Lee4879d812021-08-25 11:50:11 -0700664}
Rachel Lee2825fa22022-01-12 17:35:16 -0800665int64_t AChoreographerFrameCallbackData_getFrameTimelineDeadlineNanos(
Rachel Lee4879d812021-08-25 11:50:11 -0700666 const AChoreographerFrameCallbackData* data, size_t index) {
667 const ChoreographerFrameCallbackDataImpl* frameCallbackData =
668 AChoreographerFrameCallbackData_to_ChoreographerFrameCallbackDataImpl(data);
669 LOG_ALWAYS_FATAL_IF(!frameCallbackData->choreographer->inCallback(),
670 "Data is only valid in callback");
Rachel Lee3f028662021-11-04 19:32:24 +0000671 LOG_ALWAYS_FATAL_IF(index >= frameCallbackData->frameTimelines.size(), "Index out of bounds");
672 return frameCallbackData->frameTimelines[index].deadlineTimestamp;
Rachel Lee4879d812021-08-25 11:50:11 -0700673}
674
Alec Mouri50a931d2019-11-19 16:23:59 -0800675AChoreographer* AChoreographer_create() {
676 Choreographer* choreographer = new Choreographer(nullptr);
677 status_t result = choreographer->initialize();
678 if (result != OK) {
679 ALOGW("Failed to initialize");
680 return nullptr;
681 }
682 return Choreographer_to_AChoreographer(choreographer);
683}
684
685void AChoreographer_destroy(AChoreographer* choreographer) {
686 if (choreographer == nullptr) {
687 return;
688 }
689
690 delete AChoreographer_to_Choreographer(choreographer);
691}
692
Alec Mouri921b2772020-02-05 19:03:28 -0800693int AChoreographer_getFd(const AChoreographer* choreographer) {
Alec Mouri50a931d2019-11-19 16:23:59 -0800694 return AChoreographer_to_Choreographer(choreographer)->getFd();
695}
696
697void AChoreographer_handlePendingEvents(AChoreographer* choreographer, void* data) {
698 // Pass dummy fd and events args to handleEvent, since the underlying
699 // DisplayEventDispatcher doesn't need them outside of validating that a
700 // Looper instance didn't break, but these args circumvent those checks.
Alec Mouri271de042020-04-27 22:38:19 -0700701 Choreographer* impl = AChoreographer_to_Choreographer(choreographer);
702 impl->handleEvent(-1, Looper::EVENT_INPUT, data);
Alec Mouri50a931d2019-11-19 16:23:59 -0800703}