Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 1 | /* |
| 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 Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 20 | #include <apex/choreographer.h> |
Alec Mouri | 77a5387 | 2019-10-28 16:44:36 -0700 | [diff] [blame] | 21 | #include <gui/DisplayEventDispatcher.h> |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 22 | #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 Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 28 | #include <cinttypes> |
| 29 | #include <optional> |
| 30 | #include <queue> |
| 31 | #include <thread> |
| 32 | |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 33 | namespace android { |
| 34 | |
| 35 | static inline const char* toString(bool value) { |
| 36 | return value ? "true" : "false"; |
| 37 | } |
| 38 | |
| 39 | struct 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 Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 52 | struct RefreshRateCallback { |
| 53 | AChoreographer_refreshRateCallback callback; |
| 54 | void* data; |
| 55 | }; |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 56 | |
| 57 | class Choreographer : public DisplayEventDispatcher, public MessageHandler { |
| 58 | public: |
Alec Mouri | 50a931d | 2019-11-19 16:23:59 -0800 | [diff] [blame] | 59 | explicit Choreographer(const sp<Looper>& looper); |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 60 | void postFrameCallbackDelayed(AChoreographer_frameCallback cb, |
| 61 | AChoreographer_frameCallback64 cb64, void* data, nsecs_t delay); |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 62 | void registerRefreshRateCallback(AChoreographer_refreshRateCallback cb, void* data); |
Alec Mouri | 33682e9 | 2020-01-10 15:11:15 -0800 | [diff] [blame^] | 63 | void unregisterRefreshRateCallback(AChoreographer_refreshRateCallback cb, void* data); |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 64 | |
| 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 Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 72 | virtual ~Choreographer() = default; |
| 73 | |
| 74 | private: |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 75 | 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 Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 79 | void dispatchConfigChanged(nsecs_t timestamp, PhysicalDisplayId displayId, int32_t configId, |
| 80 | nsecs_t vsyncPeriod) override; |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 81 | |
| 82 | void scheduleCallbacks(); |
| 83 | |
| 84 | // Protected by mLock |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 85 | std::priority_queue<FrameCallback> mFrameCallbacks; |
| 86 | |
| 87 | // Protected by mLock |
| 88 | std::vector<RefreshRateCallback> mRefreshRateCallbacks; |
| 89 | nsecs_t mVsyncPeriod = 0; |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 90 | |
| 91 | mutable Mutex mLock; |
| 92 | |
| 93 | const sp<Looper> mLooper; |
| 94 | const std::thread::id mThreadId; |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 95 | const std::optional<PhysicalDisplayId> mInternalDisplayId; |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | |
| 99 | static thread_local Choreographer* gChoreographer; |
| 100 | Choreographer* 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 Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 117 | Choreographer::Choreographer(const sp<Looper>& looper) |
| 118 | : DisplayEventDispatcher(looper), |
| 119 | mLooper(looper), |
| 120 | mThreadId(std::this_thread::get_id()), |
| 121 | mInternalDisplayId(SurfaceComposerClient::getInternalDisplayId()) {} |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 122 | |
| 123 | void 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 Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 129 | mFrameCallbacks.push(callback); |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 130 | } |
| 131 | if (callback.dueTime <= now) { |
| 132 | if (std::this_thread::get_id() != mThreadId) { |
Alec Mouri | 50a931d | 2019-11-19 16:23:59 -0800 | [diff] [blame] | 133 | if (mLooper != nullptr) { |
| 134 | Message m{MSG_SCHEDULE_VSYNC}; |
| 135 | mLooper->sendMessage(this, m); |
| 136 | } else { |
| 137 | scheduleVsync(); |
| 138 | } |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 139 | } else { |
| 140 | scheduleVsync(); |
| 141 | } |
| 142 | } else { |
Alec Mouri | 50a931d | 2019-11-19 16:23:59 -0800 | [diff] [blame] | 143 | if (mLooper != nullptr) { |
| 144 | Message m{MSG_SCHEDULE_CALLBACKS}; |
| 145 | mLooper->sendMessageDelayed(delay, this, m); |
| 146 | } else { |
| 147 | scheduleCallbacks(); |
| 148 | } |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 152 | void Choreographer::registerRefreshRateCallback(AChoreographer_refreshRateCallback cb, void* data) { |
| 153 | { |
| 154 | AutoMutex _l{mLock}; |
Alec Mouri | 33682e9 | 2020-01-10 15:11:15 -0800 | [diff] [blame^] | 155 | for (const auto& callback : mRefreshRateCallbacks) { |
| 156 | // Don't re-add callbacks. |
| 157 | if (cb == callback.callback && data == callback.data) { |
| 158 | return; |
| 159 | } |
| 160 | } |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 161 | mRefreshRateCallbacks.emplace_back(RefreshRateCallback{cb, data}); |
| 162 | toggleConfigEvents(ISurfaceComposer::ConfigChanged::eConfigChangedDispatch); |
| 163 | } |
| 164 | } |
| 165 | |
Alec Mouri | 33682e9 | 2020-01-10 15:11:15 -0800 | [diff] [blame^] | 166 | void Choreographer::unregisterRefreshRateCallback(AChoreographer_refreshRateCallback cb, |
| 167 | void* data) { |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 168 | { |
| 169 | AutoMutex _l{mLock}; |
Alec Mouri | b7ebad6 | 2020-01-08 08:12:01 -0800 | [diff] [blame] | 170 | mRefreshRateCallbacks.erase(std::remove_if(mRefreshRateCallbacks.begin(), |
| 171 | mRefreshRateCallbacks.end(), |
| 172 | [&](const RefreshRateCallback& callback) { |
Alec Mouri | 33682e9 | 2020-01-10 15:11:15 -0800 | [diff] [blame^] | 173 | return cb == callback.callback && |
| 174 | data == callback.data; |
| 175 | }), |
| 176 | mRefreshRateCallbacks.end()); |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 177 | if (mRefreshRateCallbacks.empty()) { |
| 178 | toggleConfigEvents(ISurfaceComposer::ConfigChanged::eConfigChangedSuppress); |
Alec Mouri | 33682e9 | 2020-01-10 15:11:15 -0800 | [diff] [blame^] | 179 | // 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 Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 183 | } |
| 184 | } |
| 185 | } |
| 186 | |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 187 | void Choreographer::scheduleCallbacks() { |
| 188 | AutoMutex _{mLock}; |
| 189 | nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC); |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 190 | if (mFrameCallbacks.top().dueTime <= now) { |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 191 | 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. |
| 200 | void 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 Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 205 | while (!mFrameCallbacks.empty() && mFrameCallbacks.top().dueTime < now) { |
| 206 | callbacks.push_back(mFrameCallbacks.top()); |
| 207 | mFrameCallbacks.pop(); |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 208 | } |
| 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 | |
| 219 | void 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 Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 225 | // 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. |
| 230 | void 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 Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 240 | } |
| 241 | } |
Alec Mouri | 33682e9 | 2020-01-10 15:11:15 -0800 | [diff] [blame^] | 242 | mVsyncPeriod = vsyncPeriod; |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 243 | } |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | void 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 Mouri | 50a931d | 2019-11-19 16:23:59 -0800 | [diff] [blame] | 261 | using namespace android; |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 262 | |
| 263 | static inline Choreographer* AChoreographer_to_Choreographer(AChoreographer* choreographer) { |
| 264 | return reinterpret_cast<Choreographer*>(choreographer); |
| 265 | } |
| 266 | |
| 267 | static inline AChoreographer* Choreographer_to_AChoreographer(Choreographer* choreographer) { |
| 268 | return reinterpret_cast<AChoreographer*>(choreographer); |
| 269 | } |
| 270 | |
| 271 | AChoreographer* AChoreographer_getInstance() { |
| 272 | return Choreographer_to_AChoreographer(Choreographer::getForThread()); |
| 273 | } |
| 274 | |
| 275 | void AChoreographer_postFrameCallback(AChoreographer* choreographer, |
| 276 | AChoreographer_frameCallback callback, void* data) { |
| 277 | AChoreographer_to_Choreographer(choreographer)->postFrameCallbackDelayed( |
| 278 | callback, nullptr, data, 0); |
| 279 | } |
| 280 | void AChoreographer_postFrameCallbackDelayed(AChoreographer* choreographer, |
| 281 | AChoreographer_frameCallback callback, void* data, long delayMillis) { |
| 282 | AChoreographer_to_Choreographer(choreographer)->postFrameCallbackDelayed( |
| 283 | callback, nullptr, data, ms2ns(delayMillis)); |
| 284 | } |
| 285 | void AChoreographer_postFrameCallback64(AChoreographer* choreographer, |
| 286 | AChoreographer_frameCallback64 callback, void* data) { |
| 287 | AChoreographer_to_Choreographer(choreographer)->postFrameCallbackDelayed( |
| 288 | nullptr, callback, data, 0); |
| 289 | } |
| 290 | void AChoreographer_postFrameCallbackDelayed64(AChoreographer* choreographer, |
| 291 | AChoreographer_frameCallback64 callback, void* data, uint32_t delayMillis) { |
| 292 | AChoreographer_to_Choreographer(choreographer)->postFrameCallbackDelayed( |
| 293 | nullptr, callback, data, ms2ns(delayMillis)); |
| 294 | } |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 295 | void AChoreographer_registerRefreshRateCallback(AChoreographer* choreographer, |
| 296 | AChoreographer_refreshRateCallback callback, |
| 297 | void* data) { |
| 298 | AChoreographer_to_Choreographer(choreographer)->registerRefreshRateCallback(callback, data); |
| 299 | } |
| 300 | void AChoreographer_unregisterRefreshRateCallback(AChoreographer* choreographer, |
Alec Mouri | 33682e9 | 2020-01-10 15:11:15 -0800 | [diff] [blame^] | 301 | AChoreographer_refreshRateCallback callback, |
| 302 | void* data) { |
| 303 | AChoreographer_to_Choreographer(choreographer)->unregisterRefreshRateCallback(callback, data); |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 304 | } |
Alec Mouri | 50a931d | 2019-11-19 16:23:59 -0800 | [diff] [blame] | 305 | |
| 306 | AChoreographer* AChoreographer_create() { |
| 307 | Choreographer* choreographer = new Choreographer(nullptr); |
| 308 | status_t result = choreographer->initialize(); |
| 309 | if (result != OK) { |
| 310 | ALOGW("Failed to initialize"); |
| 311 | return nullptr; |
| 312 | } |
| 313 | return Choreographer_to_AChoreographer(choreographer); |
| 314 | } |
| 315 | |
| 316 | void AChoreographer_destroy(AChoreographer* choreographer) { |
| 317 | if (choreographer == nullptr) { |
| 318 | return; |
| 319 | } |
| 320 | |
| 321 | delete AChoreographer_to_Choreographer(choreographer); |
| 322 | } |
| 323 | |
| 324 | int AChoreographer_getFd(AChoreographer* choreographer) { |
| 325 | return AChoreographer_to_Choreographer(choreographer)->getFd(); |
| 326 | } |
| 327 | |
| 328 | void AChoreographer_handlePendingEvents(AChoreographer* choreographer, void* data) { |
| 329 | // Pass dummy fd and events args to handleEvent, since the underlying |
| 330 | // DisplayEventDispatcher doesn't need them outside of validating that a |
| 331 | // Looper instance didn't break, but these args circumvent those checks. |
| 332 | AChoreographer_to_Choreographer(choreographer)->handleEvent(-1, Looper::EVENT_INPUT, data); |
| 333 | } |