drm_hwcomposer: Calculate vsync period consistently
Calculating the vsync period can yield different results depending on
floating point precision and ordering of operations.
Calcuate the vsync period in one place for consistency.
Change-Id: I968e4976ec5d546f66ffe3e0f7dd404cd934a60c
Signed-off-by: Drew Davenport <ddavenport@google.com>
diff --git a/drm/DrmMode.h b/drm/DrmMode.h
index c5790a5..5450daf 100644
--- a/drm/DrmMode.h
+++ b/drm/DrmMode.h
@@ -48,6 +48,12 @@
(float)(mode_.vtotal * mode_.htotal) * 1000.0F;
}
+ auto GetVSyncPeriodNs() const {
+ static const int kNanosecondsPerSecond = 1E9;
+ return static_cast<int32_t>(kNanosecondsPerSecond *
+ double(1 / GetVRefresh()));
+ }
+
auto GetName() const {
return std::string(mode_.name) + "@" + std::to_string(GetVRefresh());
}
diff --git a/hwc2_device/HwcDisplay.cpp b/hwc2_device/HwcDisplay.cpp
index 36333d2..9a9761b 100644
--- a/hwc2_device/HwcDisplay.cpp
+++ b/hwc2_device/HwcDisplay.cpp
@@ -320,7 +320,7 @@
break;
case HWC2::Attribute::VsyncPeriod:
// in nanoseconds
- *value = static_cast<int>(1E9 / hwc_config.mode.GetVRefresh());
+ *value = hwc_config.mode.GetVSyncPeriodNs();
break;
case HWC2::Attribute::DpiY:
// ideally this should be vdisplay/mm_heigth, however mm_height
diff --git a/hwc3/ComposerClient.cpp b/hwc3/ComposerClient.cpp
index eb4e683..2efce8f 100644
--- a/hwc3/ComposerClient.cpp
+++ b/hwc3/ComposerClient.cpp
@@ -972,15 +972,13 @@
const HwcDisplayConfigs& configs = display->GetDisplayConfigs();
for (const auto& [id, config] : configs.hwc_configs) {
- static const int kNanosecondsPerSecond = 1E9;
configurations->emplace_back(
DisplayConfiguration{.configId = static_cast<int32_t>(config.id),
.width = config.mode.GetRawMode().hdisplay,
.height = config.mode.GetRawMode().vdisplay,
.configGroup = static_cast<int32_t>(
config.group_id),
- .vsyncPeriod = static_cast<int>(kNanosecondsPerSecond * double(
- 1 / config.mode.GetVRefresh()))});
+ .vsyncPeriod = config.mode.GetVSyncPeriodNs()});
if (configs.mm_width != 0) {
// ideally this should be vdisplay/mm_heigth, however mm_height