drm_hwcomposer: Avoid resetting vsync timestamp
last_timestamp_ tracks the most recent timestamp from a vblank event. Do
not reset it when toggling vsync tracking on/off. This allows for
GetPhasedVSync to calculate a more precise approximation of the next
vsync relative to the current time, even if a vsync hasn't happened for
a while.
Reset last_timestamp_ when the vsync period changes, since the phased
vsync calculation is based on the current vsync.
Also change last_timestamp_ to std::optional type.
Change-Id: I101b36f87d6ba8197c321c83ac0bc230fba9cf7e
Signed-off-by: Drew Davenport <ddavenport@google.com>
diff --git a/drm/VSyncWorker.cpp b/drm/VSyncWorker.cpp
index 5234f6b..5b3071d 100644
--- a/drm/VSyncWorker.cpp
+++ b/drm/VSyncWorker.cpp
@@ -55,7 +55,6 @@
{
const std::lock_guard<std::mutex> lock(mutex_);
enabled_ = ShouldEnable();
- last_timestamp_ = -1;
}
cv_.notify_all();
@@ -64,6 +63,7 @@
void VSyncWorker::SetVsyncPeriodNs(uint32_t vsync_period_ns) {
const std::lock_guard<std::mutex> lock(mutex_);
vsync_period_ns_ = vsync_period_ns;
+ last_timestamp_ = std::nullopt;
}
void VSyncWorker::SetVsyncTimestampTracking(bool enabled) {
@@ -121,11 +121,11 @@
* timestamp.
*/
int64_t VSyncWorker::GetPhasedVSync(int64_t frame_ns, int64_t current) const {
- if (last_timestamp_ < 0)
+ if (!last_timestamp_.has_value())
return current + frame_ns;
- return (frame_ns * ((current - last_timestamp_) / frame_ns + 1)) +
- last_timestamp_;
+ return (frame_ns * ((current - *last_timestamp_) / frame_ns + 1)) +
+ *last_timestamp_;
}
static const int64_t kOneSecondNs = 1LL * 1000 * 1000 * 1000;