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