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