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