blob: 1f77393dd15dfb70959ab171741bc1e0d9ec0723 [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);
63 void unregisterRefreshRateCallback(AChoreographer_refreshRateCallback cb);
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};
155 mRefreshRateCallbacks.emplace_back(RefreshRateCallback{cb, data});
156 toggleConfigEvents(ISurfaceComposer::ConfigChanged::eConfigChangedDispatch);
157 }
158}
159
160void Choreographer::unregisterRefreshRateCallback(AChoreographer_refreshRateCallback cb) {
161 {
162 AutoMutex _l{mLock};
163 std::remove_if(mRefreshRateCallbacks.begin(), mRefreshRateCallbacks.end(),
164 [&](const RefreshRateCallback& callback) {
165 return cb == callback.callback;
166 });
167 if (mRefreshRateCallbacks.empty()) {
168 toggleConfigEvents(ISurfaceComposer::ConfigChanged::eConfigChangedSuppress);
169 }
170 }
171}
172
Alec Mouricc445222019-10-22 10:19:17 -0700173void Choreographer::scheduleCallbacks() {
174 AutoMutex _{mLock};
175 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700176 if (mFrameCallbacks.top().dueTime <= now) {
Alec Mouricc445222019-10-22 10:19:17 -0700177 ALOGV("choreographer %p ~ scheduling vsync", this);
178 scheduleVsync();
179 return;
180 }
181}
182
183// TODO(b/74619554): The PhysicalDisplayId is ignored because SF only emits VSYNC events for the
184// internal display and DisplayEventReceiver::requestNextVsync only allows requesting VSYNC for
185// the internal display implicitly.
186void Choreographer::dispatchVsync(nsecs_t timestamp, PhysicalDisplayId, uint32_t) {
187 std::vector<FrameCallback> callbacks{};
188 {
189 AutoMutex _l{mLock};
190 nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
Alec Mouri60aee1c2019-10-28 16:18:59 -0700191 while (!mFrameCallbacks.empty() && mFrameCallbacks.top().dueTime < now) {
192 callbacks.push_back(mFrameCallbacks.top());
193 mFrameCallbacks.pop();
Alec Mouricc445222019-10-22 10:19:17 -0700194 }
195 }
196 for (const auto& cb : callbacks) {
197 if (cb.callback64 != nullptr) {
198 cb.callback64(timestamp, cb.data);
199 } else if (cb.callback != nullptr) {
200 cb.callback(timestamp, cb.data);
201 }
202 }
203}
204
205void Choreographer::dispatchHotplug(nsecs_t, PhysicalDisplayId displayId, bool connected) {
206 ALOGV("choreographer %p ~ received hotplug event (displayId=%"
207 ANDROID_PHYSICAL_DISPLAY_ID_FORMAT ", connected=%s), ignoring.",
208 this, displayId, toString(connected));
209}
210
Alec Mouri60aee1c2019-10-28 16:18:59 -0700211// TODO(b/74619554): The PhysicalDisplayId is ignored because currently
212// Choreographer only supports dispatching VSYNC events for the internal
213// display, so as such Choreographer does not support the notion of multiple
214// displays. When multi-display choreographer is properly supported, then
215// PhysicalDisplayId should no longer be ignored.
216void Choreographer::dispatchConfigChanged(nsecs_t, PhysicalDisplayId, int32_t,
217 nsecs_t vsyncPeriod) {
218 {
219 AutoMutex _l{mLock};
220 for (const auto& cb : mRefreshRateCallbacks) {
221 // Only perform the callback when the old refresh rate is different
222 // from the new refresh rate, so that we don't dispatch the callback
223 // on every single configuration change.
224 if (mVsyncPeriod != vsyncPeriod) {
225 cb.callback(vsyncPeriod, cb.data);
226 mVsyncPeriod = vsyncPeriod;
227 }
228 }
229 }
Alec Mouricc445222019-10-22 10:19:17 -0700230}
231
232void Choreographer::handleMessage(const Message& message) {
233 switch (message.what) {
234 case MSG_SCHEDULE_CALLBACKS:
235 scheduleCallbacks();
236 break;
237 case MSG_SCHEDULE_VSYNC:
238 scheduleVsync();
239 break;
240 }
241}
242
243}
244
245/* Glue for the NDK interface */
246
Alec Mouri50a931d2019-11-19 16:23:59 -0800247using namespace android;
Alec Mouricc445222019-10-22 10:19:17 -0700248
249static inline Choreographer* AChoreographer_to_Choreographer(AChoreographer* choreographer) {
250 return reinterpret_cast<Choreographer*>(choreographer);
251}
252
253static inline AChoreographer* Choreographer_to_AChoreographer(Choreographer* choreographer) {
254 return reinterpret_cast<AChoreographer*>(choreographer);
255}
256
257AChoreographer* AChoreographer_getInstance() {
258 return Choreographer_to_AChoreographer(Choreographer::getForThread());
259}
260
261void AChoreographer_postFrameCallback(AChoreographer* choreographer,
262 AChoreographer_frameCallback callback, void* data) {
263 AChoreographer_to_Choreographer(choreographer)->postFrameCallbackDelayed(
264 callback, nullptr, data, 0);
265}
266void AChoreographer_postFrameCallbackDelayed(AChoreographer* choreographer,
267 AChoreographer_frameCallback callback, void* data, long delayMillis) {
268 AChoreographer_to_Choreographer(choreographer)->postFrameCallbackDelayed(
269 callback, nullptr, data, ms2ns(delayMillis));
270}
271void AChoreographer_postFrameCallback64(AChoreographer* choreographer,
272 AChoreographer_frameCallback64 callback, void* data) {
273 AChoreographer_to_Choreographer(choreographer)->postFrameCallbackDelayed(
274 nullptr, callback, data, 0);
275}
276void AChoreographer_postFrameCallbackDelayed64(AChoreographer* choreographer,
277 AChoreographer_frameCallback64 callback, void* data, uint32_t delayMillis) {
278 AChoreographer_to_Choreographer(choreographer)->postFrameCallbackDelayed(
279 nullptr, callback, data, ms2ns(delayMillis));
280}
Alec Mouri60aee1c2019-10-28 16:18:59 -0700281void AChoreographer_registerRefreshRateCallback(AChoreographer* choreographer,
282 AChoreographer_refreshRateCallback callback,
283 void* data) {
284 AChoreographer_to_Choreographer(choreographer)->registerRefreshRateCallback(callback, data);
285}
286void AChoreographer_unregisterRefreshRateCallback(AChoreographer* choreographer,
287 AChoreographer_refreshRateCallback callback) {
288 AChoreographer_to_Choreographer(choreographer)->unregisterRefreshRateCallback(callback);
289}
Alec Mouri50a931d2019-11-19 16:23:59 -0800290
291AChoreographer* AChoreographer_create() {
292 Choreographer* choreographer = new Choreographer(nullptr);
293 status_t result = choreographer->initialize();
294 if (result != OK) {
295 ALOGW("Failed to initialize");
296 return nullptr;
297 }
298 return Choreographer_to_AChoreographer(choreographer);
299}
300
301void AChoreographer_destroy(AChoreographer* choreographer) {
302 if (choreographer == nullptr) {
303 return;
304 }
305
306 delete AChoreographer_to_Choreographer(choreographer);
307}
308
309int AChoreographer_getFd(AChoreographer* choreographer) {
310 return AChoreographer_to_Choreographer(choreographer)->getFd();
311}
312
313void AChoreographer_handlePendingEvents(AChoreographer* choreographer, void* data) {
314 // Pass dummy fd and events args to handleEvent, since the underlying
315 // DisplayEventDispatcher doesn't need them outside of validating that a
316 // Looper instance didn't break, but these args circumvent those checks.
317 AChoreographer_to_Choreographer(choreographer)->handleEvent(-1, Looper::EVENT_INPUT, data);
318}