blob: 0ff33ac74768142f9335d7defeb2132ac7d7c872 [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() {
Alec Mouri134266b2020-03-03 19:22:29 -0800188 const nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
189 nsecs_t dueTime;
190 {
191 AutoMutex _{mLock};
192 // If there are no pending callbacks then don't schedule a vsync
193 if (mFrameCallbacks.empty()) {
194 return;
195 }
196 dueTime = mFrameCallbacks.top().dueTime;
197 }
198
199 if (dueTime <= now) {
Alec Mouricc445222019-10-22 10:19:17 -0700200 ALOGV("choreographer %p ~ scheduling vsync", this);
201 scheduleVsync();
202 return;
203 }
204}
205
206// TODO(b/74619554): The PhysicalDisplayId is ignored because SF only emits VSYNC events for the
207// internal display and DisplayEventReceiver::requestNextVsync only allows requesting VSYNC for
208// the internal display implicitly.
209void Choreographer::dispatchVsync(nsecs_t timestamp, PhysicalDisplayId, uint32_t) {
210 std::vector<FrameCallback> callbacks{};
211 {
212 AutoMutex _l{mLock};
213 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700214 while (!mFrameCallbacks.empty() && mFrameCallbacks.top().dueTime < now) {
215 callbacks.push_back(mFrameCallbacks.top());
216 mFrameCallbacks.pop();
Alec Mouricc445222019-10-22 10:19:17 -0700217 }
218 }
219 for (const auto& cb : callbacks) {
220 if (cb.callback64 != nullptr) {
221 cb.callback64(timestamp, cb.data);
222 } else if (cb.callback != nullptr) {
223 cb.callback(timestamp, cb.data);
224 }
225 }
226}
227
228void Choreographer::dispatchHotplug(nsecs_t, PhysicalDisplayId displayId, bool connected) {
229 ALOGV("choreographer %p ~ received hotplug event (displayId=%"
230 ANDROID_PHYSICAL_DISPLAY_ID_FORMAT ", connected=%s), ignoring.",
231 this, displayId, toString(connected));
232}
233
Alec Mouri60aee1c2019-10-28 16:18:59 -0700234// TODO(b/74619554): The PhysicalDisplayId is ignored because currently
235// Choreographer only supports dispatching VSYNC events for the internal
236// display, so as such Choreographer does not support the notion of multiple
237// displays. When multi-display choreographer is properly supported, then
238// PhysicalDisplayId should no longer be ignored.
239void Choreographer::dispatchConfigChanged(nsecs_t, PhysicalDisplayId, int32_t,
240 nsecs_t vsyncPeriod) {
241 {
242 AutoMutex _l{mLock};
243 for (const auto& cb : mRefreshRateCallbacks) {
244 // Only perform the callback when the old refresh rate is different
245 // from the new refresh rate, so that we don't dispatch the callback
246 // on every single configuration change.
247 if (mVsyncPeriod != vsyncPeriod) {
248 cb.callback(vsyncPeriod, cb.data);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700249 }
250 }
Alec Mouri33682e92020-01-10 15:11:15 -0800251 mVsyncPeriod = vsyncPeriod;
Alec Mouri60aee1c2019-10-28 16:18:59 -0700252 }
Alec Mouricc445222019-10-22 10:19:17 -0700253}
254
255void Choreographer::handleMessage(const Message& message) {
256 switch (message.what) {
257 case MSG_SCHEDULE_CALLBACKS:
258 scheduleCallbacks();
259 break;
260 case MSG_SCHEDULE_VSYNC:
261 scheduleVsync();
262 break;
263 }
264}
265
266}
267
268/* Glue for the NDK interface */
269
Alec Mouri50a931d2019-11-19 16:23:59 -0800270using namespace android;
Alec Mouricc445222019-10-22 10:19:17 -0700271
272static inline Choreographer* AChoreographer_to_Choreographer(AChoreographer* choreographer) {
273 return reinterpret_cast<Choreographer*>(choreographer);
274}
275
Alec Mouri921b2772020-02-05 19:03:28 -0800276static inline const Choreographer* AChoreographer_to_Choreographer(
277 const AChoreographer* choreographer) {
278 return reinterpret_cast<const Choreographer*>(choreographer);
279}
280
Alec Mouricc445222019-10-22 10:19:17 -0700281static inline AChoreographer* Choreographer_to_AChoreographer(Choreographer* choreographer) {
282 return reinterpret_cast<AChoreographer*>(choreographer);
283}
284
285AChoreographer* AChoreographer_getInstance() {
286 return Choreographer_to_AChoreographer(Choreographer::getForThread());
287}
288
289void AChoreographer_postFrameCallback(AChoreographer* choreographer,
290 AChoreographer_frameCallback callback, void* data) {
291 AChoreographer_to_Choreographer(choreographer)->postFrameCallbackDelayed(
292 callback, nullptr, data, 0);
293}
294void AChoreographer_postFrameCallbackDelayed(AChoreographer* choreographer,
295 AChoreographer_frameCallback callback, void* data, long delayMillis) {
296 AChoreographer_to_Choreographer(choreographer)->postFrameCallbackDelayed(
297 callback, nullptr, data, ms2ns(delayMillis));
298}
299void AChoreographer_postFrameCallback64(AChoreographer* choreographer,
300 AChoreographer_frameCallback64 callback, void* data) {
301 AChoreographer_to_Choreographer(choreographer)->postFrameCallbackDelayed(
302 nullptr, callback, data, 0);
303}
304void AChoreographer_postFrameCallbackDelayed64(AChoreographer* choreographer,
305 AChoreographer_frameCallback64 callback, void* data, uint32_t delayMillis) {
306 AChoreographer_to_Choreographer(choreographer)->postFrameCallbackDelayed(
307 nullptr, callback, data, ms2ns(delayMillis));
308}
Alec Mouri60aee1c2019-10-28 16:18:59 -0700309void AChoreographer_registerRefreshRateCallback(AChoreographer* choreographer,
310 AChoreographer_refreshRateCallback callback,
311 void* data) {
312 AChoreographer_to_Choreographer(choreographer)->registerRefreshRateCallback(callback, data);
313}
314void AChoreographer_unregisterRefreshRateCallback(AChoreographer* choreographer,
Alec Mouri33682e92020-01-10 15:11:15 -0800315 AChoreographer_refreshRateCallback callback,
316 void* data) {
317 AChoreographer_to_Choreographer(choreographer)->unregisterRefreshRateCallback(callback, data);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700318}
Alec Mouri50a931d2019-11-19 16:23:59 -0800319
320AChoreographer* AChoreographer_create() {
321 Choreographer* choreographer = new Choreographer(nullptr);
322 status_t result = choreographer->initialize();
323 if (result != OK) {
324 ALOGW("Failed to initialize");
325 return nullptr;
326 }
327 return Choreographer_to_AChoreographer(choreographer);
328}
329
330void AChoreographer_destroy(AChoreographer* choreographer) {
331 if (choreographer == nullptr) {
332 return;
333 }
334
335 delete AChoreographer_to_Choreographer(choreographer);
336}
337
Alec Mouri921b2772020-02-05 19:03:28 -0800338int AChoreographer_getFd(const AChoreographer* choreographer) {
Alec Mouri50a931d2019-11-19 16:23:59 -0800339 return AChoreographer_to_Choreographer(choreographer)->getFd();
340}
341
342void AChoreographer_handlePendingEvents(AChoreographer* choreographer, void* data) {
343 // Pass dummy fd and events args to handleEvent, since the underlying
344 // DisplayEventDispatcher doesn't need them outside of validating that a
345 // Looper instance didn't break, but these args circumvent those checks.
346 AChoreographer_to_Choreographer(choreographer)->handleEvent(-1, Looper::EVENT_INPUT, data);
347}