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