SF: avoid dispatching close vsyncs
When the vsync period changes, the next vsync callback might be targeting
the same vsync event, that now has a newer time based on the current
period. This means that the client might be woken up for 2 frames within
the same vsync. This change tries to avoid that by making sure that the
vsync callbacks are at least a vsync period apart minus a safe distance.
Test: new unit test
Bug: 235566681
Change-Id: Ifb9b3f8b726976452d5131c8b758d1d5ca0e3639
diff --git a/services/surfaceflinger/Scheduler/VSyncDispatchTimerQueue.cpp b/services/surfaceflinger/Scheduler/VSyncDispatchTimerQueue.cpp
index 27f4311..cc9f7cf 100644
--- a/services/surfaceflinger/Scheduler/VSyncDispatchTimerQueue.cpp
+++ b/services/surfaceflinger/Scheduler/VSyncDispatchTimerQueue.cpp
@@ -100,14 +100,8 @@
return getExpectedCallbackTime(nextVsyncTime, timing);
}
- bool const alreadyDispatchedForVsync = mLastDispatchTime &&
- ((*mLastDispatchTime + mMinVsyncDistance) >= nextVsyncTime &&
- (*mLastDispatchTime - mMinVsyncDistance) <= nextVsyncTime);
- if (alreadyDispatchedForVsync) {
- nextVsyncTime =
- tracker.nextAnticipatedVSyncTimeFrom(*mLastDispatchTime + mMinVsyncDistance);
- nextWakeupTime = nextVsyncTime - timing.workDuration - timing.readyDuration;
- }
+ nextVsyncTime = adjustVsyncIfNeeded(tracker, nextVsyncTime);
+ nextWakeupTime = nextVsyncTime - timing.workDuration - timing.readyDuration;
auto const nextReadyTime = nextVsyncTime - timing.readyDuration;
mScheduleTiming = timing;
@@ -123,6 +117,25 @@
return mWorkloadUpdateInfo.has_value();
}
+nsecs_t VSyncDispatchTimerQueueEntry::adjustVsyncIfNeeded(VSyncTracker& tracker,
+ nsecs_t nextVsyncTime) const {
+ bool const alreadyDispatchedForVsync = mLastDispatchTime &&
+ ((*mLastDispatchTime + mMinVsyncDistance) >= nextVsyncTime &&
+ (*mLastDispatchTime - mMinVsyncDistance) <= nextVsyncTime);
+ const nsecs_t currentPeriod = tracker.currentPeriod();
+ bool const nextVsyncTooClose = mLastDispatchTime &&
+ (nextVsyncTime - *mLastDispatchTime + mMinVsyncDistance) <= currentPeriod;
+ if (alreadyDispatchedForVsync) {
+ return tracker.nextAnticipatedVSyncTimeFrom(*mLastDispatchTime + mMinVsyncDistance);
+ }
+
+ if (nextVsyncTooClose) {
+ return tracker.nextAnticipatedVSyncTimeFrom(*mLastDispatchTime + currentPeriod);
+ }
+
+ return nextVsyncTime;
+}
+
void VSyncDispatchTimerQueueEntry::update(VSyncTracker& tracker, nsecs_t now) {
if (!mArmedInfo && !mWorkloadUpdateInfo) {
return;
@@ -136,7 +149,9 @@
const auto earliestReadyBy = now + mScheduleTiming.workDuration + mScheduleTiming.readyDuration;
const auto earliestVsync = std::max(earliestReadyBy, mScheduleTiming.earliestVsync);
- const auto nextVsyncTime = tracker.nextAnticipatedVSyncTimeFrom(earliestVsync);
+ const auto nextVsyncTime =
+ adjustVsyncIfNeeded(tracker, /*nextVsyncTime*/
+ tracker.nextAnticipatedVSyncTimeFrom(earliestVsync));
const auto nextReadyTime = nextVsyncTime - mScheduleTiming.readyDuration;
const auto nextWakeupTime = nextReadyTime - mScheduleTiming.workDuration;