SF: Polish Fps class
For consistent comparison (which will later be changed to check ULP
distance instead of the magnitude-dependent threshold), express the
inequality operators in terms of equality, and avoid hashing floats.
Add Hz literals and (namespaced) operators.
Bug: 129481165
Bug: 185535769
Test: libsurfaceflinger_unittest
Test: dumpsys SurfaceFlinger --vsync
Change-Id: I79be5d2dd031218c4054774d2645efb337211538
diff --git a/services/surfaceflinger/tests/unittests/LayerInfoTest.cpp b/services/surfaceflinger/tests/unittests/LayerInfoTest.cpp
index d6ce5e2..f25994e 100644
--- a/services/surfaceflinger/tests/unittests/LayerInfoTest.cpp
+++ b/services/surfaceflinger/tests/unittests/LayerInfoTest.cpp
@@ -20,6 +20,7 @@
#include <gtest/gtest.h>
#include "Fps.h"
+#include "FpsOps.h"
#include "Scheduler/LayerHistory.h"
#include "Scheduler/LayerInfo.h"
@@ -47,7 +48,7 @@
TEST_F(LayerInfoTest, prefersPresentTime) {
std::deque<FrameTimeData> frameTimes;
- constexpr auto kExpectedFps = Fps(50.0f);
+ constexpr auto kExpectedFps = 50_Hz;
constexpr auto kPeriod = kExpectedFps.getPeriodNsecs();
constexpr int kNumFrames = 10;
for (int i = 1; i <= kNumFrames; i++) {
@@ -58,14 +59,12 @@
setFrameTimes(frameTimes);
const auto averageFrameTime = calculateAverageFrameTime();
ASSERT_TRUE(averageFrameTime.has_value());
- const auto averageFps = Fps::fromPeriodNsecs(*averageFrameTime);
- ASSERT_TRUE(kExpectedFps.equalsWithMargin(averageFps))
- << "Expected " << averageFps << " to be equal to " << kExpectedFps;
+ ASSERT_EQ(kExpectedFps, Fps::fromPeriodNsecs(*averageFrameTime));
}
TEST_F(LayerInfoTest, fallbacksToQueueTimeIfNoPresentTime) {
std::deque<FrameTimeData> frameTimes;
- constexpr auto kExpectedFps = Fps(50.0f);
+ constexpr auto kExpectedFps = 50_Hz;
constexpr auto kPeriod = kExpectedFps.getPeriodNsecs();
constexpr int kNumFrames = 10;
for (int i = 1; i <= kNumFrames; i++) {
@@ -74,17 +73,15 @@
.pendingModeChange = false});
}
setFrameTimes(frameTimes);
- setLastRefreshRate(Fps(20.0f)); // Set to some valid value
+ setLastRefreshRate(20_Hz); // Set to some valid value.
const auto averageFrameTime = calculateAverageFrameTime();
ASSERT_TRUE(averageFrameTime.has_value());
- const auto averageFps = Fps::fromPeriodNsecs(*averageFrameTime);
- ASSERT_TRUE(kExpectedFps.equalsWithMargin(averageFps))
- << "Expected " << averageFps << " to be equal to " << kExpectedFps;
+ ASSERT_EQ(kExpectedFps, Fps::fromPeriodNsecs(*averageFrameTime));
}
TEST_F(LayerInfoTest, returnsNulloptIfThereWasConfigChange) {
std::deque<FrameTimeData> frameTimesWithoutConfigChange;
- const auto period = Fps(50.0f).getPeriodNsecs();
+ const auto period = (50_Hz).getPeriodNsecs();
constexpr int kNumFrames = 10;
for (int i = 1; i <= kNumFrames; i++) {
frameTimesWithoutConfigChange.push_back(FrameTimeData{.presentTime = period * i,
@@ -124,9 +121,9 @@
// Make sure that this doesn't influence the calculated average FPS.
TEST_F(LayerInfoTest, ignoresSmallPeriods) {
std::deque<FrameTimeData> frameTimes;
- constexpr auto kExpectedFps = Fps(50.0f);
+ constexpr auto kExpectedFps = 50_Hz;
constexpr auto kExpectedPeriod = kExpectedFps.getPeriodNsecs();
- constexpr auto kSmallPeriod = Fps(250.0f).getPeriodNsecs();
+ constexpr auto kSmallPeriod = (250_Hz).getPeriodNsecs();
constexpr int kNumIterations = 10;
for (int i = 1; i <= kNumIterations; i++) {
frameTimes.push_back(FrameTimeData{.presentTime = kExpectedPeriod * i,
@@ -141,18 +138,16 @@
setFrameTimes(frameTimes);
const auto averageFrameTime = calculateAverageFrameTime();
ASSERT_TRUE(averageFrameTime.has_value());
- const auto averageFps = Fps::fromPeriodNsecs(*averageFrameTime);
- ASSERT_TRUE(kExpectedFps.equalsWithMargin(averageFps))
- << "Expected " << averageFps << " to be equal to " << kExpectedFps;
+ ASSERT_EQ(kExpectedFps, Fps::fromPeriodNsecs(*averageFrameTime));
}
// There may be a big period of time between two frames. Make sure that
// this doesn't influence the calculated average FPS.
TEST_F(LayerInfoTest, ignoresLargePeriods) {
std::deque<FrameTimeData> frameTimes;
- constexpr auto kExpectedFps = Fps(50.0f);
+ constexpr auto kExpectedFps = 50_Hz;
constexpr auto kExpectedPeriod = kExpectedFps.getPeriodNsecs();
- constexpr auto kLargePeriod = Fps(9.0f).getPeriodNsecs();
+ constexpr auto kLargePeriod = (9_Hz).getPeriodNsecs();
auto record = [&](nsecs_t time) {
frameTimes.push_back(
@@ -172,9 +167,7 @@
setFrameTimes(frameTimes);
const auto averageFrameTime = calculateAverageFrameTime();
ASSERT_TRUE(averageFrameTime.has_value());
- const auto averageFps = Fps::fromPeriodNsecs(*averageFrameTime);
- ASSERT_TRUE(kExpectedFps.equalsWithMargin(averageFps))
- << "Expected " << averageFps << " to be equal to " << kExpectedFps;
+ ASSERT_EQ(kExpectedFps, Fps::fromPeriodNsecs(*averageFrameTime));
}
} // namespace