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