blob: 0f7e2fb9362a7c056ae894406a06d4d3c2cb160f [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 Mouri60aee1c2019-10-28 16:18:59 -070020#include <apex/choreographer.h>
Alec Mouri77a53872019-10-28 16:44:36 -070021#include <gui/DisplayEventDispatcher.h>
Alec Mouricc445222019-10-22 10:19:17 -070022#include <gui/ISurfaceComposer.h>
23#include <gui/SurfaceComposerClient.h>
24#include <utils/Looper.h>
25#include <utils/Mutex.h>
26#include <utils/Timers.h>
27
Alec Mouri60aee1c2019-10-28 16:18:59 -070028#include <cinttypes>
29#include <optional>
30#include <queue>
31#include <thread>
32
Alec Mouricc445222019-10-22 10:19:17 -070033namespace android {
34
35static inline const char* toString(bool value) {
36 return value ? "true" : "false";
37}
38
39struct FrameCallback {
40 AChoreographer_frameCallback callback;
41 AChoreographer_frameCallback64 callback64;
42 void* data;
43 nsecs_t dueTime;
44
45 inline bool operator<(const FrameCallback& rhs) const {
46 // Note that this is intentionally flipped because we want callbacks due sooner to be at
47 // the head of the queue
48 return dueTime > rhs.dueTime;
49 }
50};
51
Alec Mouri60aee1c2019-10-28 16:18:59 -070052struct RefreshRateCallback {
53 AChoreographer_refreshRateCallback callback;
54 void* data;
55};
Alec Mouricc445222019-10-22 10:19:17 -070056
57class Choreographer : public DisplayEventDispatcher, public MessageHandler {
58public:
Alec Mouri50a931d2019-11-19 16:23:59 -080059 explicit Choreographer(const sp<Looper>& looper);
Alec Mouricc445222019-10-22 10:19:17 -070060 void postFrameCallbackDelayed(AChoreographer_frameCallback cb,
61 AChoreographer_frameCallback64 cb64, void* data, nsecs_t delay);
Alec Mouri60aee1c2019-10-28 16:18:59 -070062 void registerRefreshRateCallback(AChoreographer_refreshRateCallback cb, void* data);
Alec Mouri33682e92020-01-10 15:11:15 -080063 void unregisterRefreshRateCallback(AChoreographer_refreshRateCallback cb, void* data);
Alec Mouricc445222019-10-22 10:19:17 -070064
65 enum {
66 MSG_SCHEDULE_CALLBACKS = 0,
67 MSG_SCHEDULE_VSYNC = 1
68 };
69 virtual void handleMessage(const Message& message) override;
70
71 static Choreographer* getForThread();
Alec Mouricc445222019-10-22 10:19:17 -070072 virtual ~Choreographer() = default;
73
74private:
Alec Mouricc445222019-10-22 10:19:17 -070075 Choreographer(const Choreographer&) = delete;
76
77 void dispatchVsync(nsecs_t timestamp, PhysicalDisplayId displayId, uint32_t count) override;
78 void dispatchHotplug(nsecs_t timestamp, PhysicalDisplayId displayId, bool connected) override;
Alec Mouri60aee1c2019-10-28 16:18:59 -070079 void dispatchConfigChanged(nsecs_t timestamp, PhysicalDisplayId displayId, int32_t configId,
80 nsecs_t vsyncPeriod) override;
Alec Mouricc445222019-10-22 10:19:17 -070081
82 void scheduleCallbacks();
83
84 // Protected by mLock
Alec Mouri60aee1c2019-10-28 16:18:59 -070085 std::priority_queue<FrameCallback> mFrameCallbacks;
86
87 // Protected by mLock
88 std::vector<RefreshRateCallback> mRefreshRateCallbacks;
89 nsecs_t mVsyncPeriod = 0;
Alec Mouricc445222019-10-22 10:19:17 -070090
91 mutable Mutex mLock;
92
93 const sp<Looper> mLooper;
94 const std::thread::id mThreadId;
Alec Mouri60aee1c2019-10-28 16:18:59 -070095 const std::optional<PhysicalDisplayId> mInternalDisplayId;
Alec Mouricc445222019-10-22 10:19:17 -070096};
97
98
99static thread_local Choreographer* gChoreographer;
100Choreographer* Choreographer::getForThread() {
101 if (gChoreographer == nullptr) {
102 sp<Looper> looper = Looper::getForThread();
103 if (!looper.get()) {
104 ALOGW("No looper prepared for thread");
105 return nullptr;
106 }
107 gChoreographer = new Choreographer(looper);
108 status_t result = gChoreographer->initialize();
109 if (result != OK) {
110 ALOGW("Failed to initialize");
111 return nullptr;
112 }
113 }
114 return gChoreographer;
115}
116
Alec Mouri60aee1c2019-10-28 16:18:59 -0700117Choreographer::Choreographer(const sp<Looper>& looper)
118 : DisplayEventDispatcher(looper),
119 mLooper(looper),
120 mThreadId(std::this_thread::get_id()),
121 mInternalDisplayId(SurfaceComposerClient::getInternalDisplayId()) {}
Alec Mouricc445222019-10-22 10:19:17 -0700122
123void Choreographer::postFrameCallbackDelayed(
124 AChoreographer_frameCallback cb, AChoreographer_frameCallback64 cb64, void* data, nsecs_t delay) {
125 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
126 FrameCallback callback{cb, cb64, data, now + delay};
127 {
128 AutoMutex _l{mLock};
Alec Mouri60aee1c2019-10-28 16:18:59 -0700129 mFrameCallbacks.push(callback);
Alec Mouricc445222019-10-22 10:19:17 -0700130 }
131 if (callback.dueTime <= now) {
132 if (std::this_thread::get_id() != mThreadId) {
Alec Mouri50a931d2019-11-19 16:23:59 -0800133 if (mLooper != nullptr) {
134 Message m{MSG_SCHEDULE_VSYNC};
135 mLooper->sendMessage(this, m);
136 } else {
137 scheduleVsync();
138 }
Alec Mouricc445222019-10-22 10:19:17 -0700139 } else {
140 scheduleVsync();
141 }
142 } else {
Alec Mouri50a931d2019-11-19 16:23:59 -0800143 if (mLooper != nullptr) {
144 Message m{MSG_SCHEDULE_CALLBACKS};
145 mLooper->sendMessageDelayed(delay, this, m);
146 } else {
147 scheduleCallbacks();
148 }
Alec Mouricc445222019-10-22 10:19:17 -0700149 }
150}
151
Alec Mouri60aee1c2019-10-28 16:18:59 -0700152void Choreographer::registerRefreshRateCallback(AChoreographer_refreshRateCallback cb, void* data) {
153 {
154 AutoMutex _l{mLock};
Alec Mouri33682e92020-01-10 15:11:15 -0800155 for (const auto& callback : mRefreshRateCallbacks) {
156 // Don't re-add callbacks.
157 if (cb == callback.callback && data == callback.data) {
158 return;
159 }
160 }
Alec Mouri60aee1c2019-10-28 16:18:59 -0700161 mRefreshRateCallbacks.emplace_back(RefreshRateCallback{cb, data});
162 toggleConfigEvents(ISurfaceComposer::ConfigChanged::eConfigChangedDispatch);
163 }
164}
165
Alec Mouri33682e92020-01-10 15:11:15 -0800166void Choreographer::unregisterRefreshRateCallback(AChoreographer_refreshRateCallback cb,
167 void* data) {
Alec Mouri60aee1c2019-10-28 16:18:59 -0700168 {
169 AutoMutex _l{mLock};
Alec Mourib7ebad62020-01-08 08:12:01 -0800170 mRefreshRateCallbacks.erase(std::remove_if(mRefreshRateCallbacks.begin(),
171 mRefreshRateCallbacks.end(),
172 [&](const RefreshRateCallback& callback) {
Alec Mouri33682e92020-01-10 15:11:15 -0800173 return cb == callback.callback &&
174 data == callback.data;
175 }),
176 mRefreshRateCallbacks.end());
Alec Mouri60aee1c2019-10-28 16:18:59 -0700177 if (mRefreshRateCallbacks.empty()) {
178 toggleConfigEvents(ISurfaceComposer::ConfigChanged::eConfigChangedSuppress);
Alec Mouri33682e92020-01-10 15:11:15 -0800179 // If callbacks are empty then clear out the most recently seen
180 // vsync period so that when another callback is registered then the
181 // up-to-date refresh rate can be communicated to the app again.
182 mVsyncPeriod = 0;
Alec Mouri60aee1c2019-10-28 16:18:59 -0700183 }
184 }
185}
186
Alec Mouricc445222019-10-22 10:19:17 -0700187void Choreographer::scheduleCallbacks() {
188 AutoMutex _{mLock};
189 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700190 if (mFrameCallbacks.top().dueTime <= now) {
Alec Mouricc445222019-10-22 10:19:17 -0700191 ALOGV("choreographer %p ~ scheduling vsync", this);
192 scheduleVsync();
193 return;
194 }
195}
196
197// TODO(b/74619554): The PhysicalDisplayId is ignored because SF only emits VSYNC events for the
198// internal display and DisplayEventReceiver::requestNextVsync only allows requesting VSYNC for
199// the internal display implicitly.
200void Choreographer::dispatchVsync(nsecs_t timestamp, PhysicalDisplayId, uint32_t) {
201 std::vector<FrameCallback> callbacks{};
202 {
203 AutoMutex _l{mLock};
204 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700205 while (!mFrameCallbacks.empty() && mFrameCallbacks.top().dueTime < now) {
206 callbacks.push_back(mFrameCallbacks.top());
207 mFrameCallbacks.pop();
Alec Mouricc445222019-10-22 10:19:17 -0700208 }
209 }
210 for (const auto& cb : callbacks) {
211 if (cb.callback64 != nullptr) {
212 cb.callback64(timestamp, cb.data);
213 } else if (cb.callback != nullptr) {
214 cb.callback(timestamp, cb.data);
215 }
216 }
217}
218
219void Choreographer::dispatchHotplug(nsecs_t, PhysicalDisplayId displayId, bool connected) {
220 ALOGV("choreographer %p ~ received hotplug event (displayId=%"
221 ANDROID_PHYSICAL_DISPLAY_ID_FORMAT ", connected=%s), ignoring.",
222 this, displayId, toString(connected));
223}
224
Alec Mouri60aee1c2019-10-28 16:18:59 -0700225// TODO(b/74619554): The PhysicalDisplayId is ignored because currently
226// Choreographer only supports dispatching VSYNC events for the internal
227// display, so as such Choreographer does not support the notion of multiple
228// displays. When multi-display choreographer is properly supported, then
229// PhysicalDisplayId should no longer be ignored.
230void Choreographer::dispatchConfigChanged(nsecs_t, PhysicalDisplayId, int32_t,
231 nsecs_t vsyncPeriod) {
232 {
233 AutoMutex _l{mLock};
234 for (const auto& cb : mRefreshRateCallbacks) {
235 // Only perform the callback when the old refresh rate is different
236 // from the new refresh rate, so that we don't dispatch the callback
237 // on every single configuration change.
238 if (mVsyncPeriod != vsyncPeriod) {
239 cb.callback(vsyncPeriod, cb.data);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700240 }
241 }
Alec Mouri33682e92020-01-10 15:11:15 -0800242 mVsyncPeriod = vsyncPeriod;
Alec Mouri60aee1c2019-10-28 16:18:59 -0700243 }
Alec Mouricc445222019-10-22 10:19:17 -0700244}
245
246void Choreographer::handleMessage(const Message& message) {
247 switch (message.what) {
248 case MSG_SCHEDULE_CALLBACKS:
249 scheduleCallbacks();
250 break;
251 case MSG_SCHEDULE_VSYNC:
252 scheduleVsync();
253 break;
254 }
255}
256
257}
258
259/* Glue for the NDK interface */
260
Alec Mouri50a931d2019-11-19 16:23:59 -0800261using namespace android;
Alec Mouricc445222019-10-22 10:19:17 -0700262
263static inline Choreographer* AChoreographer_to_Choreographer(AChoreographer* choreographer) {
264 return reinterpret_cast<Choreographer*>(choreographer);
265}
266
Alec Mouri921b2772020-02-05 19:03:28 -0800267static inline const Choreographer* AChoreographer_to_Choreographer(
268 const AChoreographer* choreographer) {
269 return reinterpret_cast<const Choreographer*>(choreographer);
270}
271
Alec Mouricc445222019-10-22 10:19:17 -0700272static inline AChoreographer* Choreographer_to_AChoreographer(Choreographer* choreographer) {
273 return reinterpret_cast<AChoreographer*>(choreographer);
274}
275
276AChoreographer* AChoreographer_getInstance() {
277 return Choreographer_to_AChoreographer(Choreographer::getForThread());
278}
279
280void AChoreographer_postFrameCallback(AChoreographer* choreographer,
281 AChoreographer_frameCallback callback, void* data) {
282 AChoreographer_to_Choreographer(choreographer)->postFrameCallbackDelayed(
283 callback, nullptr, data, 0);
284}
285void AChoreographer_postFrameCallbackDelayed(AChoreographer* choreographer,
286 AChoreographer_frameCallback callback, void* data, long delayMillis) {
287 AChoreographer_to_Choreographer(choreographer)->postFrameCallbackDelayed(
288 callback, nullptr, data, ms2ns(delayMillis));
289}
290void AChoreographer_postFrameCallback64(AChoreographer* choreographer,
291 AChoreographer_frameCallback64 callback, void* data) {
292 AChoreographer_to_Choreographer(choreographer)->postFrameCallbackDelayed(
293 nullptr, callback, data, 0);
294}
295void AChoreographer_postFrameCallbackDelayed64(AChoreographer* choreographer,
296 AChoreographer_frameCallback64 callback, void* data, uint32_t delayMillis) {
297 AChoreographer_to_Choreographer(choreographer)->postFrameCallbackDelayed(
298 nullptr, callback, data, ms2ns(delayMillis));
299}
Alec Mouri60aee1c2019-10-28 16:18:59 -0700300void AChoreographer_registerRefreshRateCallback(AChoreographer* choreographer,
301 AChoreographer_refreshRateCallback callback,
302 void* data) {
303 AChoreographer_to_Choreographer(choreographer)->registerRefreshRateCallback(callback, data);
304}
305void AChoreographer_unregisterRefreshRateCallback(AChoreographer* choreographer,
Alec Mouri33682e92020-01-10 15:11:15 -0800306 AChoreographer_refreshRateCallback callback,
307 void* data) {
308 AChoreographer_to_Choreographer(choreographer)->unregisterRefreshRateCallback(callback, data);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700309}
Alec Mouri50a931d2019-11-19 16:23:59 -0800310
311AChoreographer* AChoreographer_create() {
312 Choreographer* choreographer = new Choreographer(nullptr);
313 status_t result = choreographer->initialize();
314 if (result != OK) {
315 ALOGW("Failed to initialize");
316 return nullptr;
317 }
318 return Choreographer_to_AChoreographer(choreographer);
319}
320
321void AChoreographer_destroy(AChoreographer* choreographer) {
322 if (choreographer == nullptr) {
323 return;
324 }
325
326 delete AChoreographer_to_Choreographer(choreographer);
327}
328
Alec Mouri921b2772020-02-05 19:03:28 -0800329int AChoreographer_getFd(const AChoreographer* choreographer) {
Alec Mouri50a931d2019-11-19 16:23:59 -0800330 return AChoreographer_to_Choreographer(choreographer)->getFd();
331}
332
333void AChoreographer_handlePendingEvents(AChoreographer* choreographer, void* data) {
334 // Pass dummy fd and events args to handleEvent, since the underlying
335 // DisplayEventDispatcher doesn't need them outside of validating that a
336 // Looper instance didn't break, but these args circumvent those checks.
337 AChoreographer_to_Choreographer(choreographer)->handleEvent(-1, Looper::EVENT_INPUT, data);
338}