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 | 271de04 | 2020-04-27 22:38:19 -0700 | [diff] [blame] | 20 | #include <android-base/thread_annotations.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> |
Orion Hodson | e53587b | 2021-02-02 15:33:33 +0000 | [diff] [blame] | 23 | #include <jni.h> |
Alec Mouri | 271de04 | 2020-04-27 22:38:19 -0700 | [diff] [blame] | 24 | #include <private/android/choreographer.h> |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 25 | #include <utils/Looper.h> |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 26 | #include <utils/Timers.h> |
| 27 | |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 28 | #include <cinttypes> |
Alec Mouri | 271de04 | 2020-04-27 22:38:19 -0700 | [diff] [blame] | 29 | #include <mutex> |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 30 | #include <optional> |
| 31 | #include <queue> |
| 32 | #include <thread> |
| 33 | |
Alec Mouri | 271de04 | 2020-04-27 22:38:19 -0700 | [diff] [blame] | 34 | namespace { |
| 35 | struct { |
| 36 | // Global JVM that is provided by zygote |
| 37 | JavaVM* jvm = nullptr; |
| 38 | struct { |
| 39 | jclass clazz; |
| 40 | jmethodID getInstance; |
| 41 | jmethodID registerNativeChoreographerForRefreshRateCallbacks; |
| 42 | jmethodID unregisterNativeChoreographerForRefreshRateCallbacks; |
| 43 | } displayManagerGlobal; |
| 44 | } gJni; |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 45 | |
Alec Mouri | 271de04 | 2020-04-27 22:38:19 -0700 | [diff] [blame] | 46 | // Gets the JNIEnv* for this thread, and performs one-off initialization if we |
| 47 | // have never retrieved a JNIEnv* pointer before. |
| 48 | JNIEnv* getJniEnv() { |
| 49 | if (gJni.jvm == nullptr) { |
| 50 | ALOGW("AChoreographer: No JVM provided!"); |
| 51 | return nullptr; |
| 52 | } |
| 53 | |
| 54 | JNIEnv* env = nullptr; |
| 55 | if (gJni.jvm->GetEnv((void**)&env, JNI_VERSION_1_4) != JNI_OK) { |
| 56 | ALOGD("Attaching thread to JVM for AChoreographer"); |
| 57 | JavaVMAttachArgs args = {JNI_VERSION_1_4, "AChoreographer_env", NULL}; |
| 58 | jint attachResult = gJni.jvm->AttachCurrentThreadAsDaemon(&env, (void*)&args); |
| 59 | if (attachResult != JNI_OK) { |
| 60 | ALOGE("Unable to attach thread. Error: %d", attachResult); |
| 61 | return nullptr; |
| 62 | } |
| 63 | } |
| 64 | if (env == nullptr) { |
| 65 | ALOGW("AChoreographer: No JNI env available!"); |
| 66 | } |
| 67 | return env; |
| 68 | } |
| 69 | |
| 70 | inline const char* toString(bool value) { |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 71 | return value ? "true" : "false"; |
| 72 | } |
Alec Mouri | 271de04 | 2020-04-27 22:38:19 -0700 | [diff] [blame] | 73 | } // namespace |
| 74 | |
| 75 | namespace android { |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 76 | |
| 77 | struct FrameCallback { |
| 78 | AChoreographer_frameCallback callback; |
| 79 | AChoreographer_frameCallback64 callback64; |
| 80 | void* data; |
| 81 | nsecs_t dueTime; |
| 82 | |
| 83 | inline bool operator<(const FrameCallback& rhs) const { |
| 84 | // Note that this is intentionally flipped because we want callbacks due sooner to be at |
| 85 | // the head of the queue |
| 86 | return dueTime > rhs.dueTime; |
| 87 | } |
| 88 | }; |
| 89 | |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 90 | struct RefreshRateCallback { |
| 91 | AChoreographer_refreshRateCallback callback; |
| 92 | void* data; |
Alec Mouri | 271de04 | 2020-04-27 22:38:19 -0700 | [diff] [blame] | 93 | bool firstCallbackFired = false; |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 94 | }; |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 95 | |
Alec Mouri | 271de04 | 2020-04-27 22:38:19 -0700 | [diff] [blame] | 96 | class Choreographer; |
| 97 | |
| 98 | struct { |
| 99 | std::mutex lock; |
| 100 | std::vector<Choreographer*> ptrs GUARDED_BY(lock); |
| 101 | bool registeredToDisplayManager GUARDED_BY(lock) = false; |
| 102 | |
| 103 | std::atomic<nsecs_t> mLastKnownVsync = -1; |
| 104 | } gChoreographers; |
| 105 | |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 106 | class Choreographer : public DisplayEventDispatcher, public MessageHandler { |
| 107 | public: |
Alec Mouri | 271de04 | 2020-04-27 22:38:19 -0700 | [diff] [blame] | 108 | explicit Choreographer(const sp<Looper>& looper) EXCLUDES(gChoreographers.lock); |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 109 | void postFrameCallbackDelayed(AChoreographer_frameCallback cb, |
| 110 | AChoreographer_frameCallback64 cb64, void* data, nsecs_t delay); |
Alec Mouri | 271de04 | 2020-04-27 22:38:19 -0700 | [diff] [blame] | 111 | void registerRefreshRateCallback(AChoreographer_refreshRateCallback cb, void* data) |
| 112 | EXCLUDES(gChoreographers.lock); |
Alec Mouri | 33682e9 | 2020-01-10 15:11:15 -0800 | [diff] [blame] | 113 | void unregisterRefreshRateCallback(AChoreographer_refreshRateCallback cb, void* data); |
Alec Mouri | 271de04 | 2020-04-27 22:38:19 -0700 | [diff] [blame] | 114 | // Drains the queue of pending vsync periods and dispatches refresh rate |
| 115 | // updates to callbacks. |
| 116 | // The assumption is that this method is only called on a single |
| 117 | // processing thread, either by looper or by AChoreographer_handleEvents |
| 118 | void handleRefreshRateUpdates(); |
| 119 | void scheduleLatestConfigRequest(); |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 120 | |
| 121 | enum { |
| 122 | MSG_SCHEDULE_CALLBACKS = 0, |
Alec Mouri | 271de04 | 2020-04-27 22:38:19 -0700 | [diff] [blame] | 123 | MSG_SCHEDULE_VSYNC = 1, |
| 124 | MSG_HANDLE_REFRESH_RATE_UPDATES = 2, |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 125 | }; |
| 126 | virtual void handleMessage(const Message& message) override; |
| 127 | |
| 128 | static Choreographer* getForThread(); |
Alec Mouri | 271de04 | 2020-04-27 22:38:19 -0700 | [diff] [blame] | 129 | virtual ~Choreographer() override EXCLUDES(gChoreographers.lock); |
Ady Abraham | 74e1756 | 2020-08-24 18:18:19 -0700 | [diff] [blame] | 130 | int64_t getVsyncId() const; |
Ady Abraham | 0d28d76 | 2020-10-05 17:50:41 -0700 | [diff] [blame] | 131 | int64_t getFrameDeadline() const; |
Jorim Jaggi | c0086af | 2021-02-12 18:18:11 +0100 | [diff] [blame] | 132 | int64_t getFrameInterval() const; |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 133 | |
| 134 | private: |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 135 | Choreographer(const Choreographer&) = delete; |
| 136 | |
Ady Abraham | 74e1756 | 2020-08-24 18:18:19 -0700 | [diff] [blame] | 137 | void dispatchVsync(nsecs_t timestamp, PhysicalDisplayId displayId, uint32_t count, |
Ady Abraham | 0d28d76 | 2020-10-05 17:50:41 -0700 | [diff] [blame] | 138 | VsyncEventData vsyncEventData) override; |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 139 | void dispatchHotplug(nsecs_t timestamp, PhysicalDisplayId displayId, bool connected) override; |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 140 | void dispatchModeChanged(nsecs_t timestamp, PhysicalDisplayId displayId, int32_t modeId, |
| 141 | nsecs_t vsyncPeriod) override; |
Alec Mouri | 967d5d7 | 2020-08-05 12:50:03 -0700 | [diff] [blame] | 142 | void dispatchNullEvent(nsecs_t, PhysicalDisplayId) override; |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 143 | void dispatchFrameRateOverrides(nsecs_t timestamp, PhysicalDisplayId displayId, |
| 144 | std::vector<FrameRateOverride> overrides) override; |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 145 | |
| 146 | void scheduleCallbacks(); |
| 147 | |
Alec Mouri | 271de04 | 2020-04-27 22:38:19 -0700 | [diff] [blame] | 148 | std::mutex mLock; |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 149 | // Protected by mLock |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 150 | std::priority_queue<FrameCallback> mFrameCallbacks; |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 151 | std::vector<RefreshRateCallback> mRefreshRateCallbacks; |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 152 | |
Alec Mouri | 271de04 | 2020-04-27 22:38:19 -0700 | [diff] [blame] | 153 | nsecs_t mLatestVsyncPeriod = -1; |
Ady Abraham | 0d28d76 | 2020-10-05 17:50:41 -0700 | [diff] [blame] | 154 | VsyncEventData mLastVsyncEventData; |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 155 | |
| 156 | const sp<Looper> mLooper; |
| 157 | const std::thread::id mThreadId; |
| 158 | }; |
| 159 | |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 160 | static thread_local Choreographer* gChoreographer; |
| 161 | Choreographer* Choreographer::getForThread() { |
| 162 | if (gChoreographer == nullptr) { |
| 163 | sp<Looper> looper = Looper::getForThread(); |
| 164 | if (!looper.get()) { |
| 165 | ALOGW("No looper prepared for thread"); |
| 166 | return nullptr; |
| 167 | } |
| 168 | gChoreographer = new Choreographer(looper); |
| 169 | status_t result = gChoreographer->initialize(); |
| 170 | if (result != OK) { |
| 171 | ALOGW("Failed to initialize"); |
| 172 | return nullptr; |
| 173 | } |
| 174 | } |
| 175 | return gChoreographer; |
| 176 | } |
| 177 | |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 178 | Choreographer::Choreographer(const sp<Looper>& looper) |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 179 | : DisplayEventDispatcher(looper, ISurfaceComposer::VsyncSource::eVsyncSourceApp), |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 180 | mLooper(looper), |
Alec Mouri | 271de04 | 2020-04-27 22:38:19 -0700 | [diff] [blame] | 181 | mThreadId(std::this_thread::get_id()) { |
| 182 | std::lock_guard<std::mutex> _l(gChoreographers.lock); |
| 183 | gChoreographers.ptrs.push_back(this); |
| 184 | } |
| 185 | |
| 186 | Choreographer::~Choreographer() { |
| 187 | std::lock_guard<std::mutex> _l(gChoreographers.lock); |
| 188 | gChoreographers.ptrs.erase(std::remove_if(gChoreographers.ptrs.begin(), |
| 189 | gChoreographers.ptrs.end(), |
| 190 | [=](Choreographer* c) { return c == this; }), |
| 191 | gChoreographers.ptrs.end()); |
| 192 | // Only poke DisplayManagerGlobal to unregister if we previously registered |
| 193 | // callbacks. |
| 194 | if (gChoreographers.ptrs.empty() && gChoreographers.registeredToDisplayManager) { |
Ady Abraham | 6a8986b | 2021-08-18 13:44:14 -0700 | [diff] [blame^] | 195 | gChoreographers.registeredToDisplayManager = false; |
Alec Mouri | 271de04 | 2020-04-27 22:38:19 -0700 | [diff] [blame] | 196 | JNIEnv* env = getJniEnv(); |
| 197 | if (env == nullptr) { |
| 198 | ALOGW("JNI environment is unavailable, skipping choreographer cleanup"); |
| 199 | return; |
| 200 | } |
| 201 | jobject dmg = env->CallStaticObjectMethod(gJni.displayManagerGlobal.clazz, |
| 202 | gJni.displayManagerGlobal.getInstance); |
| 203 | if (dmg == nullptr) { |
| 204 | ALOGW("DMS is not initialized yet, skipping choreographer cleanup"); |
| 205 | } else { |
| 206 | env->CallVoidMethod(dmg, |
| 207 | gJni.displayManagerGlobal |
| 208 | .unregisterNativeChoreographerForRefreshRateCallbacks); |
| 209 | env->DeleteLocalRef(dmg); |
| 210 | } |
| 211 | } |
| 212 | } |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 213 | |
| 214 | void Choreographer::postFrameCallbackDelayed( |
| 215 | AChoreographer_frameCallback cb, AChoreographer_frameCallback64 cb64, void* data, nsecs_t delay) { |
| 216 | nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC); |
| 217 | FrameCallback callback{cb, cb64, data, now + delay}; |
| 218 | { |
Alec Mouri | 271de04 | 2020-04-27 22:38:19 -0700 | [diff] [blame] | 219 | std::lock_guard<std::mutex> _l{mLock}; |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 220 | mFrameCallbacks.push(callback); |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 221 | } |
| 222 | if (callback.dueTime <= now) { |
| 223 | if (std::this_thread::get_id() != mThreadId) { |
Alec Mouri | 50a931d | 2019-11-19 16:23:59 -0800 | [diff] [blame] | 224 | if (mLooper != nullptr) { |
| 225 | Message m{MSG_SCHEDULE_VSYNC}; |
| 226 | mLooper->sendMessage(this, m); |
| 227 | } else { |
| 228 | scheduleVsync(); |
| 229 | } |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 230 | } else { |
| 231 | scheduleVsync(); |
| 232 | } |
| 233 | } else { |
Alec Mouri | 50a931d | 2019-11-19 16:23:59 -0800 | [diff] [blame] | 234 | if (mLooper != nullptr) { |
| 235 | Message m{MSG_SCHEDULE_CALLBACKS}; |
| 236 | mLooper->sendMessageDelayed(delay, this, m); |
| 237 | } else { |
| 238 | scheduleCallbacks(); |
| 239 | } |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 240 | } |
| 241 | } |
| 242 | |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 243 | void Choreographer::registerRefreshRateCallback(AChoreographer_refreshRateCallback cb, void* data) { |
Alec Mouri | 271de04 | 2020-04-27 22:38:19 -0700 | [diff] [blame] | 244 | std::lock_guard<std::mutex> _l{mLock}; |
| 245 | for (const auto& callback : mRefreshRateCallbacks) { |
| 246 | // Don't re-add callbacks. |
| 247 | if (cb == callback.callback && data == callback.data) { |
| 248 | return; |
| 249 | } |
| 250 | } |
| 251 | mRefreshRateCallbacks.emplace_back( |
| 252 | RefreshRateCallback{.callback = cb, .data = data, .firstCallbackFired = false}); |
| 253 | bool needsRegistration = false; |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 254 | { |
Alec Mouri | 271de04 | 2020-04-27 22:38:19 -0700 | [diff] [blame] | 255 | std::lock_guard<std::mutex> _l2(gChoreographers.lock); |
| 256 | needsRegistration = !gChoreographers.registeredToDisplayManager; |
| 257 | } |
| 258 | if (needsRegistration) { |
| 259 | JNIEnv* env = getJniEnv(); |
Alec Mouri | 271de04 | 2020-04-27 22:38:19 -0700 | [diff] [blame] | 260 | if (env == nullptr) { |
Greg Kaiser | b66d04b | 2020-05-11 06:52:15 -0700 | [diff] [blame] | 261 | ALOGW("JNI environment is unavailable, skipping registration"); |
Alec Mouri | 271de04 | 2020-04-27 22:38:19 -0700 | [diff] [blame] | 262 | return; |
| 263 | } |
Greg Kaiser | b66d04b | 2020-05-11 06:52:15 -0700 | [diff] [blame] | 264 | jobject dmg = env->CallStaticObjectMethod(gJni.displayManagerGlobal.clazz, |
| 265 | gJni.displayManagerGlobal.getInstance); |
Alec Mouri | 271de04 | 2020-04-27 22:38:19 -0700 | [diff] [blame] | 266 | if (dmg == nullptr) { |
| 267 | ALOGW("DMS is not initialized yet: skipping registration"); |
| 268 | return; |
| 269 | } else { |
| 270 | env->CallVoidMethod(dmg, |
| 271 | gJni.displayManagerGlobal |
| 272 | .registerNativeChoreographerForRefreshRateCallbacks, |
| 273 | reinterpret_cast<int64_t>(this)); |
| 274 | env->DeleteLocalRef(dmg); |
| 275 | { |
| 276 | std::lock_guard<std::mutex> _l2(gChoreographers.lock); |
| 277 | gChoreographers.registeredToDisplayManager = true; |
Alec Mouri | 33682e9 | 2020-01-10 15:11:15 -0800 | [diff] [blame] | 278 | } |
| 279 | } |
Alec Mouri | 271de04 | 2020-04-27 22:38:19 -0700 | [diff] [blame] | 280 | } else { |
| 281 | scheduleLatestConfigRequest(); |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 282 | } |
| 283 | } |
| 284 | |
Alec Mouri | 33682e9 | 2020-01-10 15:11:15 -0800 | [diff] [blame] | 285 | void Choreographer::unregisterRefreshRateCallback(AChoreographer_refreshRateCallback cb, |
| 286 | void* data) { |
Alec Mouri | 271de04 | 2020-04-27 22:38:19 -0700 | [diff] [blame] | 287 | std::lock_guard<std::mutex> _l{mLock}; |
| 288 | mRefreshRateCallbacks.erase(std::remove_if(mRefreshRateCallbacks.begin(), |
| 289 | mRefreshRateCallbacks.end(), |
| 290 | [&](const RefreshRateCallback& callback) { |
| 291 | return cb == callback.callback && |
| 292 | data == callback.data; |
| 293 | }), |
| 294 | mRefreshRateCallbacks.end()); |
| 295 | } |
| 296 | |
| 297 | void Choreographer::scheduleLatestConfigRequest() { |
| 298 | if (mLooper != nullptr) { |
| 299 | Message m{MSG_HANDLE_REFRESH_RATE_UPDATES}; |
| 300 | mLooper->sendMessage(this, m); |
| 301 | } else { |
| 302 | // If the looper thread is detached from Choreographer, then refresh rate |
| 303 | // changes will be handled in AChoreographer_handlePendingEvents, so we |
Alec Mouri | 967d5d7 | 2020-08-05 12:50:03 -0700 | [diff] [blame] | 304 | // need to wake up the looper thread by writing to the write-end of the |
| 305 | // socket the looper is listening on. |
| 306 | // Fortunately, these events are small so sending packets across the |
| 307 | // socket should be atomic across processes. |
| 308 | DisplayEventReceiver::Event event; |
Dominik Laskowski | f183385 | 2021-03-23 15:06:50 -0700 | [diff] [blame] | 309 | event.header = |
| 310 | DisplayEventReceiver::Event::Header{DisplayEventReceiver::DISPLAY_EVENT_NULL, |
| 311 | PhysicalDisplayId::fromPort(0), systemTime()}; |
Alec Mouri | 967d5d7 | 2020-08-05 12:50:03 -0700 | [diff] [blame] | 312 | injectEvent(event); |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 313 | } |
| 314 | } |
| 315 | |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 316 | void Choreographer::scheduleCallbacks() { |
Alec Mouri | 134266b | 2020-03-03 19:22:29 -0800 | [diff] [blame] | 317 | const nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC); |
| 318 | nsecs_t dueTime; |
| 319 | { |
Alec Mouri | 271de04 | 2020-04-27 22:38:19 -0700 | [diff] [blame] | 320 | std::lock_guard<std::mutex> _l{mLock}; |
Alec Mouri | 134266b | 2020-03-03 19:22:29 -0800 | [diff] [blame] | 321 | // If there are no pending callbacks then don't schedule a vsync |
| 322 | if (mFrameCallbacks.empty()) { |
| 323 | return; |
| 324 | } |
| 325 | dueTime = mFrameCallbacks.top().dueTime; |
| 326 | } |
| 327 | |
| 328 | if (dueTime <= now) { |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 329 | ALOGV("choreographer %p ~ scheduling vsync", this); |
| 330 | scheduleVsync(); |
| 331 | return; |
| 332 | } |
| 333 | } |
| 334 | |
Alec Mouri | 271de04 | 2020-04-27 22:38:19 -0700 | [diff] [blame] | 335 | void Choreographer::handleRefreshRateUpdates() { |
| 336 | std::vector<RefreshRateCallback> callbacks{}; |
| 337 | const nsecs_t pendingPeriod = gChoreographers.mLastKnownVsync.load(); |
| 338 | const nsecs_t lastPeriod = mLatestVsyncPeriod; |
| 339 | if (pendingPeriod > 0) { |
| 340 | mLatestVsyncPeriod = pendingPeriod; |
| 341 | } |
| 342 | { |
| 343 | std::lock_guard<std::mutex> _l{mLock}; |
| 344 | for (auto& cb : mRefreshRateCallbacks) { |
| 345 | callbacks.push_back(cb); |
| 346 | cb.firstCallbackFired = true; |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | for (auto& cb : callbacks) { |
| 351 | if (!cb.firstCallbackFired || (pendingPeriod > 0 && pendingPeriod != lastPeriod)) { |
| 352 | cb.callback(pendingPeriod, cb.data); |
| 353 | } |
| 354 | } |
| 355 | } |
| 356 | |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 357 | // TODO(b/74619554): The PhysicalDisplayId is ignored because SF only emits VSYNC events for the |
| 358 | // internal display and DisplayEventReceiver::requestNextVsync only allows requesting VSYNC for |
| 359 | // the internal display implicitly. |
Ady Abraham | 0d28d76 | 2020-10-05 17:50:41 -0700 | [diff] [blame] | 360 | void Choreographer::dispatchVsync(nsecs_t timestamp, PhysicalDisplayId, uint32_t, |
| 361 | VsyncEventData vsyncEventData) { |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 362 | std::vector<FrameCallback> callbacks{}; |
| 363 | { |
Alec Mouri | 271de04 | 2020-04-27 22:38:19 -0700 | [diff] [blame] | 364 | std::lock_guard<std::mutex> _l{mLock}; |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 365 | nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC); |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 366 | while (!mFrameCallbacks.empty() && mFrameCallbacks.top().dueTime < now) { |
| 367 | callbacks.push_back(mFrameCallbacks.top()); |
| 368 | mFrameCallbacks.pop(); |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 369 | } |
| 370 | } |
Ady Abraham | 0d28d76 | 2020-10-05 17:50:41 -0700 | [diff] [blame] | 371 | mLastVsyncEventData = vsyncEventData; |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 372 | for (const auto& cb : callbacks) { |
| 373 | if (cb.callback64 != nullptr) { |
| 374 | cb.callback64(timestamp, cb.data); |
| 375 | } else if (cb.callback != nullptr) { |
| 376 | cb.callback(timestamp, cb.data); |
| 377 | } |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | void Choreographer::dispatchHotplug(nsecs_t, PhysicalDisplayId displayId, bool connected) { |
Marin Shalamanov | a524a09 | 2020-07-27 21:39:55 +0200 | [diff] [blame] | 382 | ALOGV("choreographer %p ~ received hotplug event (displayId=%s, connected=%s), ignoring.", |
| 383 | this, to_string(displayId).c_str(), toString(connected)); |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 384 | } |
| 385 | |
Marin Shalamanov | a7fe304 | 2021-01-29 21:02:08 +0100 | [diff] [blame] | 386 | void Choreographer::dispatchModeChanged(nsecs_t, PhysicalDisplayId, int32_t, nsecs_t) { |
| 387 | LOG_ALWAYS_FATAL("dispatchModeChanged was called but was never registered"); |
Ady Abraham | 62f216c | 2020-10-13 19:07:23 -0700 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | void Choreographer::dispatchFrameRateOverrides(nsecs_t, PhysicalDisplayId, |
| 391 | std::vector<FrameRateOverride>) { |
| 392 | LOG_ALWAYS_FATAL("dispatchFrameRateOverrides was called but was never registered"); |
Alec Mouri | 967d5d7 | 2020-08-05 12:50:03 -0700 | [diff] [blame] | 393 | } |
Alec Mouri | 271de04 | 2020-04-27 22:38:19 -0700 | [diff] [blame] | 394 | |
Alec Mouri | 967d5d7 | 2020-08-05 12:50:03 -0700 | [diff] [blame] | 395 | void Choreographer::dispatchNullEvent(nsecs_t, PhysicalDisplayId) { |
| 396 | ALOGV("choreographer %p ~ received null event.", this); |
| 397 | handleRefreshRateUpdates(); |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | void Choreographer::handleMessage(const Message& message) { |
| 401 | switch (message.what) { |
| 402 | case MSG_SCHEDULE_CALLBACKS: |
| 403 | scheduleCallbacks(); |
| 404 | break; |
| 405 | case MSG_SCHEDULE_VSYNC: |
| 406 | scheduleVsync(); |
| 407 | break; |
Alec Mouri | 271de04 | 2020-04-27 22:38:19 -0700 | [diff] [blame] | 408 | case MSG_HANDLE_REFRESH_RATE_UPDATES: |
| 409 | handleRefreshRateUpdates(); |
| 410 | break; |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 411 | } |
| 412 | } |
| 413 | |
Ady Abraham | 74e1756 | 2020-08-24 18:18:19 -0700 | [diff] [blame] | 414 | int64_t Choreographer::getVsyncId() const { |
Ady Abraham | 0d28d76 | 2020-10-05 17:50:41 -0700 | [diff] [blame] | 415 | return mLastVsyncEventData.id; |
| 416 | } |
| 417 | |
| 418 | int64_t Choreographer::getFrameDeadline() const { |
| 419 | return mLastVsyncEventData.deadlineTimestamp; |
Ady Abraham | 74e1756 | 2020-08-24 18:18:19 -0700 | [diff] [blame] | 420 | } |
| 421 | |
Jorim Jaggi | c0086af | 2021-02-12 18:18:11 +0100 | [diff] [blame] | 422 | int64_t Choreographer::getFrameInterval() const { |
| 423 | return mLastVsyncEventData.frameInterval; |
| 424 | } |
| 425 | |
Alec Mouri | 271de04 | 2020-04-27 22:38:19 -0700 | [diff] [blame] | 426 | } // namespace android |
Alec Mouri | 50a931d | 2019-11-19 16:23:59 -0800 | [diff] [blame] | 427 | using namespace android; |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 428 | |
| 429 | static inline Choreographer* AChoreographer_to_Choreographer(AChoreographer* choreographer) { |
| 430 | return reinterpret_cast<Choreographer*>(choreographer); |
| 431 | } |
| 432 | |
Ady Abraham | 74e1756 | 2020-08-24 18:18:19 -0700 | [diff] [blame] | 433 | static inline const Choreographer* AChoreographer_to_Choreographer( |
| 434 | const AChoreographer* choreographer) { |
| 435 | return reinterpret_cast<const Choreographer*>(choreographer); |
| 436 | } |
| 437 | |
Alec Mouri | 271de04 | 2020-04-27 22:38:19 -0700 | [diff] [blame] | 438 | // Glue for private C api |
| 439 | namespace android { |
| 440 | void AChoreographer_signalRefreshRateCallbacks(nsecs_t vsyncPeriod) EXCLUDES(gChoreographers.lock) { |
| 441 | std::lock_guard<std::mutex> _l(gChoreographers.lock); |
| 442 | gChoreographers.mLastKnownVsync.store(vsyncPeriod); |
| 443 | for (auto c : gChoreographers.ptrs) { |
| 444 | c->scheduleLatestConfigRequest(); |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | void AChoreographer_initJVM(JNIEnv* env) { |
| 449 | env->GetJavaVM(&gJni.jvm); |
| 450 | // Now we need to find the java classes. |
| 451 | jclass dmgClass = env->FindClass("android/hardware/display/DisplayManagerGlobal"); |
| 452 | gJni.displayManagerGlobal.clazz = static_cast<jclass>(env->NewGlobalRef(dmgClass)); |
| 453 | gJni.displayManagerGlobal.getInstance = |
| 454 | env->GetStaticMethodID(dmgClass, "getInstance", |
| 455 | "()Landroid/hardware/display/DisplayManagerGlobal;"); |
| 456 | gJni.displayManagerGlobal.registerNativeChoreographerForRefreshRateCallbacks = |
| 457 | env->GetMethodID(dmgClass, "registerNativeChoreographerForRefreshRateCallbacks", "()V"); |
| 458 | gJni.displayManagerGlobal.unregisterNativeChoreographerForRefreshRateCallbacks = |
| 459 | env->GetMethodID(dmgClass, "unregisterNativeChoreographerForRefreshRateCallbacks", |
| 460 | "()V"); |
| 461 | } |
| 462 | |
| 463 | AChoreographer* AChoreographer_routeGetInstance() { |
| 464 | return AChoreographer_getInstance(); |
| 465 | } |
| 466 | void AChoreographer_routePostFrameCallback(AChoreographer* choreographer, |
| 467 | AChoreographer_frameCallback callback, void* data) { |
Jiyong Park | 8cdf076 | 2020-08-13 20:21:57 +0900 | [diff] [blame] | 468 | #pragma clang diagnostic push |
| 469 | #pragma clang diagnostic ignored "-Wdeprecated-declarations" |
Alec Mouri | 271de04 | 2020-04-27 22:38:19 -0700 | [diff] [blame] | 470 | return AChoreographer_postFrameCallback(choreographer, callback, data); |
Jiyong Park | 8cdf076 | 2020-08-13 20:21:57 +0900 | [diff] [blame] | 471 | #pragma clang diagnostic pop |
Alec Mouri | 271de04 | 2020-04-27 22:38:19 -0700 | [diff] [blame] | 472 | } |
| 473 | void AChoreographer_routePostFrameCallbackDelayed(AChoreographer* choreographer, |
| 474 | AChoreographer_frameCallback callback, void* data, |
| 475 | long delayMillis) { |
Jiyong Park | 8cdf076 | 2020-08-13 20:21:57 +0900 | [diff] [blame] | 476 | #pragma clang diagnostic push |
| 477 | #pragma clang diagnostic ignored "-Wdeprecated-declarations" |
Alec Mouri | 271de04 | 2020-04-27 22:38:19 -0700 | [diff] [blame] | 478 | return AChoreographer_postFrameCallbackDelayed(choreographer, callback, data, delayMillis); |
Jiyong Park | 8cdf076 | 2020-08-13 20:21:57 +0900 | [diff] [blame] | 479 | #pragma clang diagnostic pop |
Alec Mouri | 271de04 | 2020-04-27 22:38:19 -0700 | [diff] [blame] | 480 | } |
| 481 | void AChoreographer_routePostFrameCallback64(AChoreographer* choreographer, |
| 482 | AChoreographer_frameCallback64 callback, void* data) { |
| 483 | return AChoreographer_postFrameCallback64(choreographer, callback, data); |
| 484 | } |
| 485 | void AChoreographer_routePostFrameCallbackDelayed64(AChoreographer* choreographer, |
| 486 | AChoreographer_frameCallback64 callback, |
| 487 | void* data, uint32_t delayMillis) { |
| 488 | return AChoreographer_postFrameCallbackDelayed64(choreographer, callback, data, delayMillis); |
| 489 | } |
| 490 | void AChoreographer_routeRegisterRefreshRateCallback(AChoreographer* choreographer, |
| 491 | AChoreographer_refreshRateCallback callback, |
| 492 | void* data) { |
| 493 | return AChoreographer_registerRefreshRateCallback(choreographer, callback, data); |
| 494 | } |
| 495 | void AChoreographer_routeUnregisterRefreshRateCallback(AChoreographer* choreographer, |
| 496 | AChoreographer_refreshRateCallback callback, |
| 497 | void* data) { |
| 498 | return AChoreographer_unregisterRefreshRateCallback(choreographer, callback, data); |
| 499 | } |
| 500 | |
Ady Abraham | 74e1756 | 2020-08-24 18:18:19 -0700 | [diff] [blame] | 501 | int64_t AChoreographer_getVsyncId(const AChoreographer* choreographer) { |
| 502 | return AChoreographer_to_Choreographer(choreographer)->getVsyncId(); |
| 503 | } |
| 504 | |
Ady Abraham | 0d28d76 | 2020-10-05 17:50:41 -0700 | [diff] [blame] | 505 | int64_t AChoreographer_getFrameDeadline(const AChoreographer* choreographer) { |
| 506 | return AChoreographer_to_Choreographer(choreographer)->getFrameDeadline(); |
| 507 | } |
| 508 | |
Jorim Jaggi | c0086af | 2021-02-12 18:18:11 +0100 | [diff] [blame] | 509 | int64_t AChoreographer_getFrameInterval(const AChoreographer* choreographer) { |
| 510 | return AChoreographer_to_Choreographer(choreographer)->getFrameInterval(); |
| 511 | } |
| 512 | |
Alec Mouri | 271de04 | 2020-04-27 22:38:19 -0700 | [diff] [blame] | 513 | } // namespace android |
| 514 | |
| 515 | /* Glue for the NDK interface */ |
| 516 | |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 517 | static inline AChoreographer* Choreographer_to_AChoreographer(Choreographer* choreographer) { |
| 518 | return reinterpret_cast<AChoreographer*>(choreographer); |
| 519 | } |
| 520 | |
| 521 | AChoreographer* AChoreographer_getInstance() { |
| 522 | return Choreographer_to_AChoreographer(Choreographer::getForThread()); |
| 523 | } |
| 524 | |
| 525 | void AChoreographer_postFrameCallback(AChoreographer* choreographer, |
| 526 | AChoreographer_frameCallback callback, void* data) { |
| 527 | AChoreographer_to_Choreographer(choreographer)->postFrameCallbackDelayed( |
| 528 | callback, nullptr, data, 0); |
| 529 | } |
| 530 | void AChoreographer_postFrameCallbackDelayed(AChoreographer* choreographer, |
| 531 | AChoreographer_frameCallback callback, void* data, long delayMillis) { |
| 532 | AChoreographer_to_Choreographer(choreographer)->postFrameCallbackDelayed( |
| 533 | callback, nullptr, data, ms2ns(delayMillis)); |
| 534 | } |
| 535 | void AChoreographer_postFrameCallback64(AChoreographer* choreographer, |
| 536 | AChoreographer_frameCallback64 callback, void* data) { |
| 537 | AChoreographer_to_Choreographer(choreographer)->postFrameCallbackDelayed( |
| 538 | nullptr, callback, data, 0); |
| 539 | } |
| 540 | void AChoreographer_postFrameCallbackDelayed64(AChoreographer* choreographer, |
| 541 | AChoreographer_frameCallback64 callback, void* data, uint32_t delayMillis) { |
| 542 | AChoreographer_to_Choreographer(choreographer)->postFrameCallbackDelayed( |
| 543 | nullptr, callback, data, ms2ns(delayMillis)); |
| 544 | } |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 545 | void AChoreographer_registerRefreshRateCallback(AChoreographer* choreographer, |
| 546 | AChoreographer_refreshRateCallback callback, |
| 547 | void* data) { |
| 548 | AChoreographer_to_Choreographer(choreographer)->registerRefreshRateCallback(callback, data); |
| 549 | } |
| 550 | void AChoreographer_unregisterRefreshRateCallback(AChoreographer* choreographer, |
Alec Mouri | 33682e9 | 2020-01-10 15:11:15 -0800 | [diff] [blame] | 551 | AChoreographer_refreshRateCallback callback, |
| 552 | void* data) { |
| 553 | AChoreographer_to_Choreographer(choreographer)->unregisterRefreshRateCallback(callback, data); |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 554 | } |
Alec Mouri | 50a931d | 2019-11-19 16:23:59 -0800 | [diff] [blame] | 555 | |
| 556 | AChoreographer* AChoreographer_create() { |
| 557 | Choreographer* choreographer = new Choreographer(nullptr); |
| 558 | status_t result = choreographer->initialize(); |
| 559 | if (result != OK) { |
| 560 | ALOGW("Failed to initialize"); |
| 561 | return nullptr; |
| 562 | } |
| 563 | return Choreographer_to_AChoreographer(choreographer); |
| 564 | } |
| 565 | |
| 566 | void AChoreographer_destroy(AChoreographer* choreographer) { |
| 567 | if (choreographer == nullptr) { |
| 568 | return; |
| 569 | } |
| 570 | |
| 571 | delete AChoreographer_to_Choreographer(choreographer); |
| 572 | } |
| 573 | |
Alec Mouri | 921b277 | 2020-02-05 19:03:28 -0800 | [diff] [blame] | 574 | int AChoreographer_getFd(const AChoreographer* choreographer) { |
Alec Mouri | 50a931d | 2019-11-19 16:23:59 -0800 | [diff] [blame] | 575 | return AChoreographer_to_Choreographer(choreographer)->getFd(); |
| 576 | } |
| 577 | |
| 578 | void AChoreographer_handlePendingEvents(AChoreographer* choreographer, void* data) { |
| 579 | // Pass dummy fd and events args to handleEvent, since the underlying |
| 580 | // DisplayEventDispatcher doesn't need them outside of validating that a |
| 581 | // Looper instance didn't break, but these args circumvent those checks. |
Alec Mouri | 271de04 | 2020-04-27 22:38:19 -0700 | [diff] [blame] | 582 | Choreographer* impl = AChoreographer_to_Choreographer(choreographer); |
| 583 | impl->handleEvent(-1, Looper::EVENT_INPUT, data); |
Alec Mouri | 50a931d | 2019-11-19 16:23:59 -0800 | [diff] [blame] | 584 | } |