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); |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 131 | |
| 132 | private: |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 133 | Choreographer(const Choreographer&) = delete; |
| 134 | |
| 135 | void dispatchVsync(nsecs_t timestamp, PhysicalDisplayId displayId, uint32_t count) override; |
| 136 | void dispatchHotplug(nsecs_t timestamp, PhysicalDisplayId displayId, bool connected) override; |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 137 | void dispatchConfigChanged(nsecs_t timestamp, PhysicalDisplayId displayId, int32_t configId, |
| 138 | nsecs_t vsyncPeriod) override; |
Alec Mouri | 967d5d7 | 2020-08-05 12:50:03 -0700 | [diff] [blame^] | 139 | void dispatchNullEvent(nsecs_t, PhysicalDisplayId) override; |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 140 | |
| 141 | void scheduleCallbacks(); |
| 142 | |
Alec Mouri | 271de04 | 2020-04-27 22:38:19 -0700 | [diff] [blame] | 143 | std::mutex mLock; |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 144 | // Protected by mLock |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 145 | std::priority_queue<FrameCallback> mFrameCallbacks; |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 146 | std::vector<RefreshRateCallback> mRefreshRateCallbacks; |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 147 | |
Alec Mouri | 271de04 | 2020-04-27 22:38:19 -0700 | [diff] [blame] | 148 | nsecs_t mLatestVsyncPeriod = -1; |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 149 | |
| 150 | const sp<Looper> mLooper; |
| 151 | const std::thread::id mThreadId; |
| 152 | }; |
| 153 | |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 154 | static thread_local Choreographer* gChoreographer; |
| 155 | Choreographer* Choreographer::getForThread() { |
| 156 | if (gChoreographer == nullptr) { |
| 157 | sp<Looper> looper = Looper::getForThread(); |
| 158 | if (!looper.get()) { |
| 159 | ALOGW("No looper prepared for thread"); |
| 160 | return nullptr; |
| 161 | } |
| 162 | gChoreographer = new Choreographer(looper); |
| 163 | status_t result = gChoreographer->initialize(); |
| 164 | if (result != OK) { |
| 165 | ALOGW("Failed to initialize"); |
| 166 | return nullptr; |
| 167 | } |
| 168 | } |
| 169 | return gChoreographer; |
| 170 | } |
| 171 | |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 172 | Choreographer::Choreographer(const sp<Looper>& looper) |
Alec Mouri | 271de04 | 2020-04-27 22:38:19 -0700 | [diff] [blame] | 173 | : DisplayEventDispatcher(looper, ISurfaceComposer::VsyncSource::eVsyncSourceApp, |
Alec Mouri | 967d5d7 | 2020-08-05 12:50:03 -0700 | [diff] [blame^] | 174 | ISurfaceComposer::ConfigChanged::eConfigChangedSuppress), |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 175 | mLooper(looper), |
Alec Mouri | 271de04 | 2020-04-27 22:38:19 -0700 | [diff] [blame] | 176 | mThreadId(std::this_thread::get_id()) { |
| 177 | std::lock_guard<std::mutex> _l(gChoreographers.lock); |
| 178 | gChoreographers.ptrs.push_back(this); |
| 179 | } |
| 180 | |
| 181 | Choreographer::~Choreographer() { |
| 182 | std::lock_guard<std::mutex> _l(gChoreographers.lock); |
| 183 | gChoreographers.ptrs.erase(std::remove_if(gChoreographers.ptrs.begin(), |
| 184 | gChoreographers.ptrs.end(), |
| 185 | [=](Choreographer* c) { return c == this; }), |
| 186 | gChoreographers.ptrs.end()); |
| 187 | // Only poke DisplayManagerGlobal to unregister if we previously registered |
| 188 | // callbacks. |
| 189 | if (gChoreographers.ptrs.empty() && gChoreographers.registeredToDisplayManager) { |
| 190 | JNIEnv* env = getJniEnv(); |
| 191 | if (env == nullptr) { |
| 192 | ALOGW("JNI environment is unavailable, skipping choreographer cleanup"); |
| 193 | return; |
| 194 | } |
| 195 | jobject dmg = env->CallStaticObjectMethod(gJni.displayManagerGlobal.clazz, |
| 196 | gJni.displayManagerGlobal.getInstance); |
| 197 | if (dmg == nullptr) { |
| 198 | ALOGW("DMS is not initialized yet, skipping choreographer cleanup"); |
| 199 | } else { |
| 200 | env->CallVoidMethod(dmg, |
| 201 | gJni.displayManagerGlobal |
| 202 | .unregisterNativeChoreographerForRefreshRateCallbacks); |
| 203 | env->DeleteLocalRef(dmg); |
| 204 | } |
| 205 | } |
| 206 | } |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 207 | |
| 208 | void Choreographer::postFrameCallbackDelayed( |
| 209 | AChoreographer_frameCallback cb, AChoreographer_frameCallback64 cb64, void* data, nsecs_t delay) { |
| 210 | nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC); |
| 211 | FrameCallback callback{cb, cb64, data, now + delay}; |
| 212 | { |
Alec Mouri | 271de04 | 2020-04-27 22:38:19 -0700 | [diff] [blame] | 213 | std::lock_guard<std::mutex> _l{mLock}; |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 214 | mFrameCallbacks.push(callback); |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 215 | } |
| 216 | if (callback.dueTime <= now) { |
| 217 | if (std::this_thread::get_id() != mThreadId) { |
Alec Mouri | 50a931d | 2019-11-19 16:23:59 -0800 | [diff] [blame] | 218 | if (mLooper != nullptr) { |
| 219 | Message m{MSG_SCHEDULE_VSYNC}; |
| 220 | mLooper->sendMessage(this, m); |
| 221 | } else { |
| 222 | scheduleVsync(); |
| 223 | } |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 224 | } else { |
| 225 | scheduleVsync(); |
| 226 | } |
| 227 | } else { |
Alec Mouri | 50a931d | 2019-11-19 16:23:59 -0800 | [diff] [blame] | 228 | if (mLooper != nullptr) { |
| 229 | Message m{MSG_SCHEDULE_CALLBACKS}; |
| 230 | mLooper->sendMessageDelayed(delay, this, m); |
| 231 | } else { |
| 232 | scheduleCallbacks(); |
| 233 | } |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 234 | } |
| 235 | } |
| 236 | |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 237 | void Choreographer::registerRefreshRateCallback(AChoreographer_refreshRateCallback cb, void* data) { |
Alec Mouri | 271de04 | 2020-04-27 22:38:19 -0700 | [diff] [blame] | 238 | std::lock_guard<std::mutex> _l{mLock}; |
| 239 | for (const auto& callback : mRefreshRateCallbacks) { |
| 240 | // Don't re-add callbacks. |
| 241 | if (cb == callback.callback && data == callback.data) { |
| 242 | return; |
| 243 | } |
| 244 | } |
| 245 | mRefreshRateCallbacks.emplace_back( |
| 246 | RefreshRateCallback{.callback = cb, .data = data, .firstCallbackFired = false}); |
| 247 | bool needsRegistration = false; |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 248 | { |
Alec Mouri | 271de04 | 2020-04-27 22:38:19 -0700 | [diff] [blame] | 249 | std::lock_guard<std::mutex> _l2(gChoreographers.lock); |
| 250 | needsRegistration = !gChoreographers.registeredToDisplayManager; |
| 251 | } |
| 252 | if (needsRegistration) { |
| 253 | JNIEnv* env = getJniEnv(); |
Alec Mouri | 271de04 | 2020-04-27 22:38:19 -0700 | [diff] [blame] | 254 | if (env == nullptr) { |
Greg Kaiser | b66d04b | 2020-05-11 06:52:15 -0700 | [diff] [blame] | 255 | ALOGW("JNI environment is unavailable, skipping registration"); |
Alec Mouri | 271de04 | 2020-04-27 22:38:19 -0700 | [diff] [blame] | 256 | return; |
| 257 | } |
Greg Kaiser | b66d04b | 2020-05-11 06:52:15 -0700 | [diff] [blame] | 258 | jobject dmg = env->CallStaticObjectMethod(gJni.displayManagerGlobal.clazz, |
| 259 | gJni.displayManagerGlobal.getInstance); |
Alec Mouri | 271de04 | 2020-04-27 22:38:19 -0700 | [diff] [blame] | 260 | if (dmg == nullptr) { |
| 261 | ALOGW("DMS is not initialized yet: skipping registration"); |
| 262 | return; |
| 263 | } else { |
| 264 | env->CallVoidMethod(dmg, |
| 265 | gJni.displayManagerGlobal |
| 266 | .registerNativeChoreographerForRefreshRateCallbacks, |
| 267 | reinterpret_cast<int64_t>(this)); |
| 268 | env->DeleteLocalRef(dmg); |
| 269 | { |
| 270 | std::lock_guard<std::mutex> _l2(gChoreographers.lock); |
| 271 | gChoreographers.registeredToDisplayManager = true; |
Alec Mouri | 33682e9 | 2020-01-10 15:11:15 -0800 | [diff] [blame] | 272 | } |
| 273 | } |
Alec Mouri | 271de04 | 2020-04-27 22:38:19 -0700 | [diff] [blame] | 274 | } else { |
| 275 | scheduleLatestConfigRequest(); |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 276 | } |
| 277 | } |
| 278 | |
Alec Mouri | 33682e9 | 2020-01-10 15:11:15 -0800 | [diff] [blame] | 279 | void Choreographer::unregisterRefreshRateCallback(AChoreographer_refreshRateCallback cb, |
| 280 | void* data) { |
Alec Mouri | 271de04 | 2020-04-27 22:38:19 -0700 | [diff] [blame] | 281 | std::lock_guard<std::mutex> _l{mLock}; |
| 282 | mRefreshRateCallbacks.erase(std::remove_if(mRefreshRateCallbacks.begin(), |
| 283 | mRefreshRateCallbacks.end(), |
| 284 | [&](const RefreshRateCallback& callback) { |
| 285 | return cb == callback.callback && |
| 286 | data == callback.data; |
| 287 | }), |
| 288 | mRefreshRateCallbacks.end()); |
| 289 | } |
| 290 | |
| 291 | void Choreographer::scheduleLatestConfigRequest() { |
| 292 | if (mLooper != nullptr) { |
| 293 | Message m{MSG_HANDLE_REFRESH_RATE_UPDATES}; |
| 294 | mLooper->sendMessage(this, m); |
| 295 | } else { |
| 296 | // If the looper thread is detached from Choreographer, then refresh rate |
| 297 | // changes will be handled in AChoreographer_handlePendingEvents, so we |
Alec Mouri | 967d5d7 | 2020-08-05 12:50:03 -0700 | [diff] [blame^] | 298 | // need to wake up the looper thread by writing to the write-end of the |
| 299 | // socket the looper is listening on. |
| 300 | // Fortunately, these events are small so sending packets across the |
| 301 | // socket should be atomic across processes. |
| 302 | DisplayEventReceiver::Event event; |
| 303 | event.header = DisplayEventReceiver::Event::Header{DisplayEventReceiver::DISPLAY_EVENT_NULL, |
| 304 | PhysicalDisplayId(0), systemTime()}; |
| 305 | injectEvent(event); |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 306 | } |
| 307 | } |
| 308 | |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 309 | void Choreographer::scheduleCallbacks() { |
Alec Mouri | 134266b | 2020-03-03 19:22:29 -0800 | [diff] [blame] | 310 | const nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC); |
| 311 | nsecs_t dueTime; |
| 312 | { |
Alec Mouri | 271de04 | 2020-04-27 22:38:19 -0700 | [diff] [blame] | 313 | std::lock_guard<std::mutex> _l{mLock}; |
Alec Mouri | 134266b | 2020-03-03 19:22:29 -0800 | [diff] [blame] | 314 | // If there are no pending callbacks then don't schedule a vsync |
| 315 | if (mFrameCallbacks.empty()) { |
| 316 | return; |
| 317 | } |
| 318 | dueTime = mFrameCallbacks.top().dueTime; |
| 319 | } |
| 320 | |
| 321 | if (dueTime <= now) { |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 322 | ALOGV("choreographer %p ~ scheduling vsync", this); |
| 323 | scheduleVsync(); |
| 324 | return; |
| 325 | } |
| 326 | } |
| 327 | |
Alec Mouri | 271de04 | 2020-04-27 22:38:19 -0700 | [diff] [blame] | 328 | void Choreographer::handleRefreshRateUpdates() { |
| 329 | std::vector<RefreshRateCallback> callbacks{}; |
| 330 | const nsecs_t pendingPeriod = gChoreographers.mLastKnownVsync.load(); |
| 331 | const nsecs_t lastPeriod = mLatestVsyncPeriod; |
| 332 | if (pendingPeriod > 0) { |
| 333 | mLatestVsyncPeriod = pendingPeriod; |
| 334 | } |
| 335 | { |
| 336 | std::lock_guard<std::mutex> _l{mLock}; |
| 337 | for (auto& cb : mRefreshRateCallbacks) { |
| 338 | callbacks.push_back(cb); |
| 339 | cb.firstCallbackFired = true; |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | for (auto& cb : callbacks) { |
| 344 | if (!cb.firstCallbackFired || (pendingPeriod > 0 && pendingPeriod != lastPeriod)) { |
| 345 | cb.callback(pendingPeriod, cb.data); |
| 346 | } |
| 347 | } |
| 348 | } |
| 349 | |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 350 | // TODO(b/74619554): The PhysicalDisplayId is ignored because SF only emits VSYNC events for the |
| 351 | // internal display and DisplayEventReceiver::requestNextVsync only allows requesting VSYNC for |
| 352 | // the internal display implicitly. |
| 353 | void Choreographer::dispatchVsync(nsecs_t timestamp, PhysicalDisplayId, uint32_t) { |
| 354 | std::vector<FrameCallback> callbacks{}; |
| 355 | { |
Alec Mouri | 271de04 | 2020-04-27 22:38:19 -0700 | [diff] [blame] | 356 | std::lock_guard<std::mutex> _l{mLock}; |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 357 | nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC); |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 358 | while (!mFrameCallbacks.empty() && mFrameCallbacks.top().dueTime < now) { |
| 359 | callbacks.push_back(mFrameCallbacks.top()); |
| 360 | mFrameCallbacks.pop(); |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 361 | } |
| 362 | } |
| 363 | for (const auto& cb : callbacks) { |
| 364 | if (cb.callback64 != nullptr) { |
| 365 | cb.callback64(timestamp, cb.data); |
| 366 | } else if (cb.callback != nullptr) { |
| 367 | cb.callback(timestamp, cb.data); |
| 368 | } |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | void Choreographer::dispatchHotplug(nsecs_t, PhysicalDisplayId displayId, bool connected) { |
Marin Shalamanov | a524a09 | 2020-07-27 21:39:55 +0200 | [diff] [blame] | 373 | ALOGV("choreographer %p ~ received hotplug event (displayId=%s, connected=%s), ignoring.", |
| 374 | this, to_string(displayId).c_str(), toString(connected)); |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 375 | } |
| 376 | |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 377 | // TODO(b/74619554): The PhysicalDisplayId is ignored because currently |
| 378 | // Choreographer only supports dispatching VSYNC events for the internal |
| 379 | // display, so as such Choreographer does not support the notion of multiple |
| 380 | // displays. When multi-display choreographer is properly supported, then |
| 381 | // PhysicalDisplayId should no longer be ignored. |
Alec Mouri | 271de04 | 2020-04-27 22:38:19 -0700 | [diff] [blame] | 382 | void Choreographer::dispatchConfigChanged(nsecs_t, PhysicalDisplayId displayId, int32_t configId, |
Alec Mouri | 967d5d7 | 2020-08-05 12:50:03 -0700 | [diff] [blame^] | 383 | nsecs_t) { |
Marin Shalamanov | a524a09 | 2020-07-27 21:39:55 +0200 | [diff] [blame] | 384 | ALOGV("choreographer %p ~ received config change event (displayId=%s, configId=%d).", |
| 385 | this, to_string(displayId).c_str(), configId); |
Alec Mouri | 967d5d7 | 2020-08-05 12:50:03 -0700 | [diff] [blame^] | 386 | } |
Alec Mouri | 271de04 | 2020-04-27 22:38:19 -0700 | [diff] [blame] | 387 | |
Alec Mouri | 967d5d7 | 2020-08-05 12:50:03 -0700 | [diff] [blame^] | 388 | void Choreographer::dispatchNullEvent(nsecs_t, PhysicalDisplayId) { |
| 389 | ALOGV("choreographer %p ~ received null event.", this); |
| 390 | handleRefreshRateUpdates(); |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | void Choreographer::handleMessage(const Message& message) { |
| 394 | switch (message.what) { |
| 395 | case MSG_SCHEDULE_CALLBACKS: |
| 396 | scheduleCallbacks(); |
| 397 | break; |
| 398 | case MSG_SCHEDULE_VSYNC: |
| 399 | scheduleVsync(); |
| 400 | break; |
Alec Mouri | 271de04 | 2020-04-27 22:38:19 -0700 | [diff] [blame] | 401 | case MSG_HANDLE_REFRESH_RATE_UPDATES: |
| 402 | handleRefreshRateUpdates(); |
| 403 | break; |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 404 | } |
| 405 | } |
| 406 | |
Alec Mouri | 271de04 | 2020-04-27 22:38:19 -0700 | [diff] [blame] | 407 | } // namespace android |
Alec Mouri | 50a931d | 2019-11-19 16:23:59 -0800 | [diff] [blame] | 408 | using namespace android; |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 409 | |
| 410 | static inline Choreographer* AChoreographer_to_Choreographer(AChoreographer* choreographer) { |
| 411 | return reinterpret_cast<Choreographer*>(choreographer); |
| 412 | } |
| 413 | |
Alec Mouri | 271de04 | 2020-04-27 22:38:19 -0700 | [diff] [blame] | 414 | // Glue for private C api |
| 415 | namespace android { |
| 416 | void AChoreographer_signalRefreshRateCallbacks(nsecs_t vsyncPeriod) EXCLUDES(gChoreographers.lock) { |
| 417 | std::lock_guard<std::mutex> _l(gChoreographers.lock); |
| 418 | gChoreographers.mLastKnownVsync.store(vsyncPeriod); |
| 419 | for (auto c : gChoreographers.ptrs) { |
| 420 | c->scheduleLatestConfigRequest(); |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | void AChoreographer_initJVM(JNIEnv* env) { |
| 425 | env->GetJavaVM(&gJni.jvm); |
| 426 | // Now we need to find the java classes. |
| 427 | jclass dmgClass = env->FindClass("android/hardware/display/DisplayManagerGlobal"); |
| 428 | gJni.displayManagerGlobal.clazz = static_cast<jclass>(env->NewGlobalRef(dmgClass)); |
| 429 | gJni.displayManagerGlobal.getInstance = |
| 430 | env->GetStaticMethodID(dmgClass, "getInstance", |
| 431 | "()Landroid/hardware/display/DisplayManagerGlobal;"); |
| 432 | gJni.displayManagerGlobal.registerNativeChoreographerForRefreshRateCallbacks = |
| 433 | env->GetMethodID(dmgClass, "registerNativeChoreographerForRefreshRateCallbacks", "()V"); |
| 434 | gJni.displayManagerGlobal.unregisterNativeChoreographerForRefreshRateCallbacks = |
| 435 | env->GetMethodID(dmgClass, "unregisterNativeChoreographerForRefreshRateCallbacks", |
| 436 | "()V"); |
| 437 | } |
| 438 | |
| 439 | AChoreographer* AChoreographer_routeGetInstance() { |
| 440 | return AChoreographer_getInstance(); |
| 441 | } |
| 442 | void AChoreographer_routePostFrameCallback(AChoreographer* choreographer, |
| 443 | AChoreographer_frameCallback callback, void* data) { |
| 444 | return AChoreographer_postFrameCallback(choreographer, callback, data); |
| 445 | } |
| 446 | void AChoreographer_routePostFrameCallbackDelayed(AChoreographer* choreographer, |
| 447 | AChoreographer_frameCallback callback, void* data, |
| 448 | long delayMillis) { |
| 449 | return AChoreographer_postFrameCallbackDelayed(choreographer, callback, data, delayMillis); |
| 450 | } |
| 451 | void AChoreographer_routePostFrameCallback64(AChoreographer* choreographer, |
| 452 | AChoreographer_frameCallback64 callback, void* data) { |
| 453 | return AChoreographer_postFrameCallback64(choreographer, callback, data); |
| 454 | } |
| 455 | void AChoreographer_routePostFrameCallbackDelayed64(AChoreographer* choreographer, |
| 456 | AChoreographer_frameCallback64 callback, |
| 457 | void* data, uint32_t delayMillis) { |
| 458 | return AChoreographer_postFrameCallbackDelayed64(choreographer, callback, data, delayMillis); |
| 459 | } |
| 460 | void AChoreographer_routeRegisterRefreshRateCallback(AChoreographer* choreographer, |
| 461 | AChoreographer_refreshRateCallback callback, |
| 462 | void* data) { |
| 463 | return AChoreographer_registerRefreshRateCallback(choreographer, callback, data); |
| 464 | } |
| 465 | void AChoreographer_routeUnregisterRefreshRateCallback(AChoreographer* choreographer, |
| 466 | AChoreographer_refreshRateCallback callback, |
| 467 | void* data) { |
| 468 | return AChoreographer_unregisterRefreshRateCallback(choreographer, callback, data); |
| 469 | } |
| 470 | |
| 471 | } // namespace android |
| 472 | |
| 473 | /* Glue for the NDK interface */ |
| 474 | |
Alec Mouri | 921b277 | 2020-02-05 19:03:28 -0800 | [diff] [blame] | 475 | static inline const Choreographer* AChoreographer_to_Choreographer( |
| 476 | const AChoreographer* choreographer) { |
| 477 | return reinterpret_cast<const Choreographer*>(choreographer); |
| 478 | } |
| 479 | |
Alec Mouri | cc44522 | 2019-10-22 10:19:17 -0700 | [diff] [blame] | 480 | static inline AChoreographer* Choreographer_to_AChoreographer(Choreographer* choreographer) { |
| 481 | return reinterpret_cast<AChoreographer*>(choreographer); |
| 482 | } |
| 483 | |
| 484 | AChoreographer* AChoreographer_getInstance() { |
| 485 | return Choreographer_to_AChoreographer(Choreographer::getForThread()); |
| 486 | } |
| 487 | |
| 488 | void AChoreographer_postFrameCallback(AChoreographer* choreographer, |
| 489 | AChoreographer_frameCallback callback, void* data) { |
| 490 | AChoreographer_to_Choreographer(choreographer)->postFrameCallbackDelayed( |
| 491 | callback, nullptr, data, 0); |
| 492 | } |
| 493 | void AChoreographer_postFrameCallbackDelayed(AChoreographer* choreographer, |
| 494 | AChoreographer_frameCallback callback, void* data, long delayMillis) { |
| 495 | AChoreographer_to_Choreographer(choreographer)->postFrameCallbackDelayed( |
| 496 | callback, nullptr, data, ms2ns(delayMillis)); |
| 497 | } |
| 498 | void AChoreographer_postFrameCallback64(AChoreographer* choreographer, |
| 499 | AChoreographer_frameCallback64 callback, void* data) { |
| 500 | AChoreographer_to_Choreographer(choreographer)->postFrameCallbackDelayed( |
| 501 | nullptr, callback, data, 0); |
| 502 | } |
| 503 | void AChoreographer_postFrameCallbackDelayed64(AChoreographer* choreographer, |
| 504 | AChoreographer_frameCallback64 callback, void* data, uint32_t delayMillis) { |
| 505 | AChoreographer_to_Choreographer(choreographer)->postFrameCallbackDelayed( |
| 506 | nullptr, callback, data, ms2ns(delayMillis)); |
| 507 | } |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 508 | void AChoreographer_registerRefreshRateCallback(AChoreographer* choreographer, |
| 509 | AChoreographer_refreshRateCallback callback, |
| 510 | void* data) { |
| 511 | AChoreographer_to_Choreographer(choreographer)->registerRefreshRateCallback(callback, data); |
| 512 | } |
| 513 | void AChoreographer_unregisterRefreshRateCallback(AChoreographer* choreographer, |
Alec Mouri | 33682e9 | 2020-01-10 15:11:15 -0800 | [diff] [blame] | 514 | AChoreographer_refreshRateCallback callback, |
| 515 | void* data) { |
| 516 | AChoreographer_to_Choreographer(choreographer)->unregisterRefreshRateCallback(callback, data); |
Alec Mouri | 60aee1c | 2019-10-28 16:18:59 -0700 | [diff] [blame] | 517 | } |
Alec Mouri | 50a931d | 2019-11-19 16:23:59 -0800 | [diff] [blame] | 518 | |
| 519 | AChoreographer* AChoreographer_create() { |
| 520 | Choreographer* choreographer = new Choreographer(nullptr); |
| 521 | status_t result = choreographer->initialize(); |
| 522 | if (result != OK) { |
| 523 | ALOGW("Failed to initialize"); |
| 524 | return nullptr; |
| 525 | } |
| 526 | return Choreographer_to_AChoreographer(choreographer); |
| 527 | } |
| 528 | |
| 529 | void AChoreographer_destroy(AChoreographer* choreographer) { |
| 530 | if (choreographer == nullptr) { |
| 531 | return; |
| 532 | } |
| 533 | |
| 534 | delete AChoreographer_to_Choreographer(choreographer); |
| 535 | } |
| 536 | |
Alec Mouri | 921b277 | 2020-02-05 19:03:28 -0800 | [diff] [blame] | 537 | int AChoreographer_getFd(const AChoreographer* choreographer) { |
Alec Mouri | 50a931d | 2019-11-19 16:23:59 -0800 | [diff] [blame] | 538 | return AChoreographer_to_Choreographer(choreographer)->getFd(); |
| 539 | } |
| 540 | |
| 541 | void AChoreographer_handlePendingEvents(AChoreographer* choreographer, void* data) { |
| 542 | // Pass dummy fd and events args to handleEvent, since the underlying |
| 543 | // DisplayEventDispatcher doesn't need them outside of validating that a |
| 544 | // Looper instance didn't break, but these args circumvent those checks. |
Alec Mouri | 271de04 | 2020-04-27 22:38:19 -0700 | [diff] [blame] | 545 | Choreographer* impl = AChoreographer_to_Choreographer(choreographer); |
| 546 | impl->handleEvent(-1, Looper::EVENT_INPUT, data); |
Alec Mouri | 50a931d | 2019-11-19 16:23:59 -0800 | [diff] [blame] | 547 | } |