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: |
| 59 | void postFrameCallbackDelayed(AChoreographer_frameCallback cb, |
| 60 | AChoreographer_frameCallback64 cb64, void* data, nsecs_t delay); |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 61 | void registerRefreshRateCallback(AChoreographer_refreshRateCallback cb, void* data); |
| 62 | void unregisterRefreshRateCallback(AChoreographer_refreshRateCallback cb); |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 63 | |
| 64 | enum { |
| 65 | MSG_SCHEDULE_CALLBACKS = 0, |
| 66 | MSG_SCHEDULE_VSYNC = 1 |
| 67 | }; |
| 68 | virtual void handleMessage(const Message& message) override; |
| 69 | |
| 70 | static Choreographer* getForThread(); |
| 71 | |
| 72 | protected: |
| 73 | virtual ~Choreographer() = default; |
| 74 | |
| 75 | private: |
| 76 | explicit Choreographer(const sp<Looper>& looper); |
| 77 | Choreographer(const Choreographer&) = delete; |
| 78 | |
| 79 | void dispatchVsync(nsecs_t timestamp, PhysicalDisplayId displayId, uint32_t count) override; |
| 80 | void dispatchHotplug(nsecs_t timestamp, PhysicalDisplayId displayId, bool connected) override; |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 81 | void dispatchConfigChanged(nsecs_t timestamp, PhysicalDisplayId displayId, int32_t configId, |
| 82 | nsecs_t vsyncPeriod) override; |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 83 | |
| 84 | void scheduleCallbacks(); |
| 85 | |
| 86 | // Protected by mLock |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 87 | std::priority_queue<FrameCallback> mFrameCallbacks; |
| 88 | |
| 89 | // Protected by mLock |
| 90 | std::vector<RefreshRateCallback> mRefreshRateCallbacks; |
| 91 | nsecs_t mVsyncPeriod = 0; |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 92 | |
| 93 | mutable Mutex mLock; |
| 94 | |
| 95 | const sp<Looper> mLooper; |
| 96 | const std::thread::id mThreadId; |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 97 | const std::optional<PhysicalDisplayId> mInternalDisplayId; |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 98 | }; |
| 99 | |
| 100 | |
| 101 | static thread_local Choreographer* gChoreographer; |
| 102 | Choreographer* Choreographer::getForThread() { |
| 103 | if (gChoreographer == nullptr) { |
| 104 | sp<Looper> looper = Looper::getForThread(); |
| 105 | if (!looper.get()) { |
| 106 | ALOGW("No looper prepared for thread"); |
| 107 | return nullptr; |
| 108 | } |
| 109 | gChoreographer = new Choreographer(looper); |
| 110 | status_t result = gChoreographer->initialize(); |
| 111 | if (result != OK) { |
| 112 | ALOGW("Failed to initialize"); |
| 113 | return nullptr; |
| 114 | } |
| 115 | } |
| 116 | return gChoreographer; |
| 117 | } |
| 118 | |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 119 | Choreographer::Choreographer(const sp<Looper>& looper) |
| 120 | : DisplayEventDispatcher(looper), |
| 121 | mLooper(looper), |
| 122 | mThreadId(std::this_thread::get_id()), |
| 123 | mInternalDisplayId(SurfaceComposerClient::getInternalDisplayId()) {} |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 124 | |
| 125 | void Choreographer::postFrameCallbackDelayed( |
| 126 | AChoreographer_frameCallback cb, AChoreographer_frameCallback64 cb64, void* data, nsecs_t delay) { |
| 127 | nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC); |
| 128 | FrameCallback callback{cb, cb64, data, now + delay}; |
| 129 | { |
| 130 | AutoMutex _l{mLock}; |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 131 | mFrameCallbacks.push(callback); |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 132 | } |
| 133 | if (callback.dueTime <= now) { |
| 134 | if (std::this_thread::get_id() != mThreadId) { |
| 135 | Message m{MSG_SCHEDULE_VSYNC}; |
| 136 | mLooper->sendMessage(this, m); |
| 137 | } else { |
| 138 | scheduleVsync(); |
| 139 | } |
| 140 | } else { |
| 141 | Message m{MSG_SCHEDULE_CALLBACKS}; |
| 142 | mLooper->sendMessageDelayed(delay, this, m); |
| 143 | } |
| 144 | } |
| 145 | |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 146 | void Choreographer::registerRefreshRateCallback(AChoreographer_refreshRateCallback cb, void* data) { |
| 147 | { |
| 148 | AutoMutex _l{mLock}; |
| 149 | mRefreshRateCallbacks.emplace_back(RefreshRateCallback{cb, data}); |
| 150 | toggleConfigEvents(ISurfaceComposer::ConfigChanged::eConfigChangedDispatch); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | void Choreographer::unregisterRefreshRateCallback(AChoreographer_refreshRateCallback cb) { |
| 155 | { |
| 156 | AutoMutex _l{mLock}; |
Alec Mouri | b7ebad6 | 2020-01-08 08:12:01 -0800 | [diff] [blame^] | 157 | mRefreshRateCallbacks.erase(std::remove_if(mRefreshRateCallbacks.begin(), |
| 158 | mRefreshRateCallbacks.end(), |
| 159 | [&](const RefreshRateCallback& callback) { |
| 160 | return cb == callback.callback; |
| 161 | })); |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 162 | if (mRefreshRateCallbacks.empty()) { |
| 163 | toggleConfigEvents(ISurfaceComposer::ConfigChanged::eConfigChangedSuppress); |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 168 | void Choreographer::scheduleCallbacks() { |
| 169 | AutoMutex _{mLock}; |
| 170 | nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC); |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 171 | if (mFrameCallbacks.top().dueTime <= now) { |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 172 | ALOGV("choreographer %p ~ scheduling vsync", this); |
| 173 | scheduleVsync(); |
| 174 | return; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | // TODO(b/74619554): The PhysicalDisplayId is ignored because SF only emits VSYNC events for the |
| 179 | // internal display and DisplayEventReceiver::requestNextVsync only allows requesting VSYNC for |
| 180 | // the internal display implicitly. |
| 181 | void Choreographer::dispatchVsync(nsecs_t timestamp, PhysicalDisplayId, uint32_t) { |
| 182 | std::vector<FrameCallback> callbacks{}; |
| 183 | { |
| 184 | AutoMutex _l{mLock}; |
| 185 | nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC); |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 186 | while (!mFrameCallbacks.empty() && mFrameCallbacks.top().dueTime < now) { |
| 187 | callbacks.push_back(mFrameCallbacks.top()); |
| 188 | mFrameCallbacks.pop(); |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | for (const auto& cb : callbacks) { |
| 192 | if (cb.callback64 != nullptr) { |
| 193 | cb.callback64(timestamp, cb.data); |
| 194 | } else if (cb.callback != nullptr) { |
| 195 | cb.callback(timestamp, cb.data); |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | void Choreographer::dispatchHotplug(nsecs_t, PhysicalDisplayId displayId, bool connected) { |
| 201 | ALOGV("choreographer %p ~ received hotplug event (displayId=%" |
| 202 | ANDROID_PHYSICAL_DISPLAY_ID_FORMAT ", connected=%s), ignoring.", |
| 203 | this, displayId, toString(connected)); |
| 204 | } |
| 205 | |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 206 | // TODO(b/74619554): The PhysicalDisplayId is ignored because currently |
| 207 | // Choreographer only supports dispatching VSYNC events for the internal |
| 208 | // display, so as such Choreographer does not support the notion of multiple |
| 209 | // displays. When multi-display choreographer is properly supported, then |
| 210 | // PhysicalDisplayId should no longer be ignored. |
| 211 | void Choreographer::dispatchConfigChanged(nsecs_t, PhysicalDisplayId, int32_t, |
| 212 | nsecs_t vsyncPeriod) { |
| 213 | { |
| 214 | AutoMutex _l{mLock}; |
| 215 | for (const auto& cb : mRefreshRateCallbacks) { |
| 216 | // Only perform the callback when the old refresh rate is different |
| 217 | // from the new refresh rate, so that we don't dispatch the callback |
| 218 | // on every single configuration change. |
| 219 | if (mVsyncPeriod != vsyncPeriod) { |
| 220 | cb.callback(vsyncPeriod, cb.data); |
| 221 | mVsyncPeriod = vsyncPeriod; |
| 222 | } |
| 223 | } |
| 224 | } |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | void Choreographer::handleMessage(const Message& message) { |
| 228 | switch (message.what) { |
| 229 | case MSG_SCHEDULE_CALLBACKS: |
| 230 | scheduleCallbacks(); |
| 231 | break; |
| 232 | case MSG_SCHEDULE_VSYNC: |
| 233 | scheduleVsync(); |
| 234 | break; |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | } |
| 239 | |
| 240 | /* Glue for the NDK interface */ |
| 241 | |
| 242 | using android::Choreographer; |
| 243 | |
| 244 | static inline Choreographer* AChoreographer_to_Choreographer(AChoreographer* choreographer) { |
| 245 | return reinterpret_cast<Choreographer*>(choreographer); |
| 246 | } |
| 247 | |
| 248 | static inline AChoreographer* Choreographer_to_AChoreographer(Choreographer* choreographer) { |
| 249 | return reinterpret_cast<AChoreographer*>(choreographer); |
| 250 | } |
| 251 | |
| 252 | AChoreographer* AChoreographer_getInstance() { |
| 253 | return Choreographer_to_AChoreographer(Choreographer::getForThread()); |
| 254 | } |
| 255 | |
| 256 | void AChoreographer_postFrameCallback(AChoreographer* choreographer, |
| 257 | AChoreographer_frameCallback callback, void* data) { |
| 258 | AChoreographer_to_Choreographer(choreographer)->postFrameCallbackDelayed( |
| 259 | callback, nullptr, data, 0); |
| 260 | } |
| 261 | void AChoreographer_postFrameCallbackDelayed(AChoreographer* choreographer, |
| 262 | AChoreographer_frameCallback callback, void* data, long delayMillis) { |
| 263 | AChoreographer_to_Choreographer(choreographer)->postFrameCallbackDelayed( |
| 264 | callback, nullptr, data, ms2ns(delayMillis)); |
| 265 | } |
| 266 | void AChoreographer_postFrameCallback64(AChoreographer* choreographer, |
| 267 | AChoreographer_frameCallback64 callback, void* data) { |
| 268 | AChoreographer_to_Choreographer(choreographer)->postFrameCallbackDelayed( |
| 269 | nullptr, callback, data, 0); |
| 270 | } |
| 271 | void AChoreographer_postFrameCallbackDelayed64(AChoreographer* choreographer, |
| 272 | AChoreographer_frameCallback64 callback, void* data, uint32_t delayMillis) { |
| 273 | AChoreographer_to_Choreographer(choreographer)->postFrameCallbackDelayed( |
| 274 | nullptr, callback, data, ms2ns(delayMillis)); |
| 275 | } |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 276 | void AChoreographer_registerRefreshRateCallback(AChoreographer* choreographer, |
| 277 | AChoreographer_refreshRateCallback callback, |
| 278 | void* data) { |
| 279 | AChoreographer_to_Choreographer(choreographer)->registerRefreshRateCallback(callback, data); |
| 280 | } |
| 281 | void AChoreographer_unregisterRefreshRateCallback(AChoreographer* choreographer, |
| 282 | AChoreographer_refreshRateCallback callback) { |
| 283 | AChoreographer_to_Choreographer(choreographer)->unregisterRefreshRateCallback(callback); |
| 284 | } |