1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
|
// Copyright 2011 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/viz/common/frame_sinks/delay_based_time_source.h"
#include <stdint.h>
#include "base/test/simple_test_tick_clock.h"
#include "base/test/test_mock_time_task_runner.h"
#include "components/viz/test/fake_delay_based_time_source.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace viz {
namespace {
base::TimeDelta Interval() {
return base::Microseconds(base::Time::kMicrosecondsPerSecond / 60);
}
class DelayBasedTimeSourceTest : public ::testing::Test {
protected:
void SetUp() override {
task_runner_ = base::MakeRefCounted<base::TestMockTimeTaskRunner>();
delay_based_time_source_ = std::make_unique<FakeDelayBasedTimeSource>(
task_runner_->GetMockTickClock(), task_runner_.get());
delay_based_time_source_->SetClient(&client_);
}
void TearDown() override {
delay_based_time_source_.reset();
task_runner_ = nullptr;
}
base::TestMockTimeTaskRunner* task_runner() { return task_runner_.get(); }
FakeDelayBasedTimeSource* timer() { return delay_based_time_source_.get(); }
FakeDelayBasedTimeSourceClient* client() { return &client_; }
FakeDelayBasedTimeSourceClient client_;
scoped_refptr<base::TestMockTimeTaskRunner> task_runner_;
std::unique_ptr<FakeDelayBasedTimeSource> delay_based_time_source_;
};
TEST_F(DelayBasedTimeSourceTest, TaskPostedAndTickCalled) {
timer()->SetTimebaseAndInterval(base::TimeTicks(), Interval());
timer()->SetActive(true);
EXPECT_TRUE(timer()->Active());
EXPECT_TRUE(task_runner()->HasPendingTask());
task_runner()->AdvanceMockTickClock(Interval());
task_runner()->RunUntilIdle();
EXPECT_TRUE(timer()->Active());
EXPECT_TRUE(client()->TickCalled());
}
TEST_F(DelayBasedTimeSourceTest, TickNotCalledWithTaskPosted) {
timer()->SetTimebaseAndInterval(base::TimeTicks(), Interval());
timer()->SetActive(true);
EXPECT_TRUE(task_runner()->HasPendingTask());
timer()->SetActive(false);
task_runner()->RunUntilIdle();
EXPECT_FALSE(client()->TickCalled());
}
TEST_F(DelayBasedTimeSourceTest, StartTwiceEnqueuesOneTask) {
timer()->SetTimebaseAndInterval(base::TimeTicks(), Interval());
timer()->SetActive(true);
EXPECT_TRUE(task_runner()->HasPendingTask());
task_runner()->ClearPendingTasks();
timer()->SetActive(true);
EXPECT_FALSE(task_runner()->HasPendingTask());
}
TEST_F(DelayBasedTimeSourceTest, StartWhenRunningDoesntTick) {
timer()->SetTimebaseAndInterval(base::TimeTicks(), Interval());
timer()->SetActive(true);
EXPECT_TRUE(task_runner()->HasPendingTask());
task_runner()->RunUntilIdle();
task_runner()->ClearPendingTasks();
timer()->SetActive(true);
EXPECT_FALSE(task_runner()->HasPendingTask());
}
// At 60Hz, when the tick returns at exactly the requested next time, make sure
// a 16ms next delay is posted.
TEST_F(DelayBasedTimeSourceTest, NextDelaySaneWhenExactlyOnRequestedTime) {
timer()->SetTimebaseAndInterval(base::TimeTicks(), Interval());
timer()->SetActive(true);
// Run the first tick.
task_runner()->RunUntilIdle();
EXPECT_EQ(16, task_runner()->NextPendingTaskDelay().InMilliseconds());
task_runner()->AdvanceMockTickClock(Interval());
task_runner()->RunUntilIdle();
EXPECT_EQ(16, task_runner()->NextPendingTaskDelay().InMilliseconds());
}
// At 60Hz, when the tick returns at slightly after the requested next time,
// make sure a 16ms next delay is posted.
TEST_F(DelayBasedTimeSourceTest, NextDelaySaneWhenSlightlyAfterRequestedTime) {
timer()->SetTimebaseAndInterval(base::TimeTicks(), Interval());
timer()->SetActive(true);
// Run the first tick.
task_runner()->RunUntilIdle();
EXPECT_EQ(16, task_runner()->NextPendingTaskDelay().InMilliseconds());
task_runner()->AdvanceMockTickClock(Interval() + base::Microseconds(1));
task_runner()->RunUntilIdle();
EXPECT_EQ(16, task_runner()->NextPendingTaskDelay().InMilliseconds());
}
// At 60Hz, when the tick returns at exactly 2*interval after the requested next
// time, make sure we don't tick unnecessarily.
TEST_F(DelayBasedTimeSourceTest,
NextDelaySaneWhenExactlyTwiceAfterRequestedTime) {
timer()->SetTimebaseAndInterval(base::TimeTicks(), Interval());
timer()->SetActive(true);
// Run the first tick.
task_runner()->RunUntilIdle();
EXPECT_EQ(16, task_runner()->NextPendingTaskDelay().InMilliseconds());
task_runner()->AdvanceMockTickClock(2 * Interval());
task_runner()->RunUntilIdle();
EXPECT_EQ(16, task_runner()->NextPendingTaskDelay().InMilliseconds());
}
// At 60Hz, when the tick returns at 2*interval and a bit after the requested
// next time, make sure a 16ms next delay is posted.
TEST_F(DelayBasedTimeSourceTest,
NextDelaySaneWhenSlightlyAfterTwiceRequestedTime) {
timer()->SetTimebaseAndInterval(base::TimeTicks(), Interval());
timer()->SetActive(true);
// Run the first tick.
task_runner()->RunUntilIdle();
EXPECT_EQ(16, task_runner()->NextPendingTaskDelay().InMilliseconds());
task_runner()->AdvanceMockTickClock(2 * Interval() + base::Microseconds(1));
task_runner()->RunUntilIdle();
EXPECT_EQ(16, task_runner()->NextPendingTaskDelay().InMilliseconds());
}
// At 60Hz, when the tick returns halfway to the next frame time, make sure
// a correct next delay value is posted.
TEST_F(DelayBasedTimeSourceTest, NextDelaySaneWhenHalfAfterRequestedTime) {
timer()->SetTimebaseAndInterval(base::TimeTicks(), Interval());
timer()->SetActive(true);
// Run the first tick.
task_runner()->RunUntilIdle();
EXPECT_EQ(16, task_runner()->NextPendingTaskDelay().InMilliseconds());
task_runner()->AdvanceMockTickClock(Interval() + base::Milliseconds(8));
task_runner()->RunUntilIdle();
EXPECT_EQ(8, task_runner()->NextPendingTaskDelay().InMilliseconds());
}
TEST_F(DelayBasedTimeSourceTest, JitteryRuntimeWithFutureTimebases) {
timer()->SetTimebaseAndInterval(base::TimeTicks(), Interval());
timer()->SetActive(true);
// Run the first tick.
task_runner()->RunUntilIdle();
EXPECT_EQ(16, task_runner()->NextPendingTaskDelay().InMilliseconds());
base::TimeTicks future_timebase = timer()->Now() + Interval() * 10;
// 1ms jitter
base::TimeDelta jitter1 = base::Milliseconds(1);
// Tick with +1ms of jitter
future_timebase += Interval();
timer()->SetTimebaseAndInterval(future_timebase, Interval());
task_runner()->AdvanceMockTickClock(Interval() + jitter1);
task_runner()->RunUntilIdle();
EXPECT_EQ(15, task_runner()->NextPendingTaskDelay().InMilliseconds());
// Tick with 0ms of jitter
future_timebase += Interval();
timer()->SetTimebaseAndInterval(future_timebase, Interval());
task_runner()->AdvanceMockTickClock(Interval() - jitter1);
task_runner()->RunUntilIdle();
EXPECT_EQ(16, task_runner()->NextPendingTaskDelay().InMilliseconds());
// Tick with -1ms of jitter
future_timebase += Interval();
timer()->SetTimebaseAndInterval(future_timebase, Interval());
task_runner()->AdvanceMockTickClock(Interval() - jitter1);
task_runner()->RunUntilIdle();
EXPECT_EQ(1, task_runner()->NextPendingTaskDelay().InMilliseconds());
// Tick with 0ms of jitter
future_timebase += Interval();
timer()->SetTimebaseAndInterval(future_timebase, Interval());
task_runner()->AdvanceMockTickClock(Interval() + jitter1);
task_runner()->RunUntilIdle();
EXPECT_EQ(16, task_runner()->NextPendingTaskDelay().InMilliseconds());
// 8 ms jitter
base::TimeDelta jitter8 = base::Milliseconds(8);
// Tick with +8ms of jitter
future_timebase += Interval();
timer()->SetTimebaseAndInterval(future_timebase, Interval());
task_runner()->AdvanceMockTickClock(Interval() + jitter8);
task_runner()->RunUntilIdle();
EXPECT_EQ(8, task_runner()->NextPendingTaskDelay().InMilliseconds());
// Tick with 0ms of jitter
future_timebase += Interval();
timer()->SetTimebaseAndInterval(future_timebase, Interval());
task_runner()->AdvanceMockTickClock(Interval() - jitter8);
task_runner()->RunUntilIdle();
EXPECT_EQ(16, task_runner()->NextPendingTaskDelay().InMilliseconds());
// Tick with -8ms of jitter
future_timebase += Interval();
timer()->SetTimebaseAndInterval(future_timebase, Interval());
task_runner()->AdvanceMockTickClock(Interval() - jitter8);
task_runner()->RunUntilIdle();
EXPECT_EQ(8, task_runner()->NextPendingTaskDelay().InMilliseconds());
// Tick with 0ms of jitter
future_timebase += Interval();
timer()->SetTimebaseAndInterval(future_timebase, Interval());
task_runner()->AdvanceMockTickClock(Interval() + jitter8);
task_runner()->RunUntilIdle();
EXPECT_EQ(16, task_runner()->NextPendingTaskDelay().InMilliseconds());
// 15 ms jitter
base::TimeDelta jitter15 = base::Milliseconds(15);
// Tick with +15ms jitter
future_timebase += Interval();
timer()->SetTimebaseAndInterval(future_timebase, Interval());
task_runner()->AdvanceMockTickClock(Interval() + jitter15);
task_runner()->RunUntilIdle();
EXPECT_EQ(1, task_runner()->NextPendingTaskDelay().InMilliseconds());
// Tick with 0ms of jitter
future_timebase += Interval();
timer()->SetTimebaseAndInterval(future_timebase, Interval());
task_runner()->AdvanceMockTickClock(Interval() - jitter15);
task_runner()->RunUntilIdle();
EXPECT_EQ(16, task_runner()->NextPendingTaskDelay().InMilliseconds());
// Tick with -15ms of jitter
future_timebase += Interval();
timer()->SetTimebaseAndInterval(future_timebase, Interval());
task_runner()->AdvanceMockTickClock(Interval() - jitter15);
task_runner()->RunUntilIdle();
EXPECT_EQ(15, task_runner()->NextPendingTaskDelay().InMilliseconds());
// Tick with 0ms of jitter
future_timebase += Interval();
timer()->SetTimebaseAndInterval(future_timebase, Interval());
task_runner()->AdvanceMockTickClock(Interval() + jitter15);
task_runner()->RunUntilIdle();
EXPECT_EQ(16, task_runner()->NextPendingTaskDelay().InMilliseconds());
}
TEST_F(DelayBasedTimeSourceTest, AchievesTargetRateWithNoNoise) {
int num_iterations = 10;
timer()->SetTimebaseAndInterval(base::TimeTicks(), Interval());
timer()->SetActive(true);
double total_frame_time = 0.0;
for (int i = 0; i < num_iterations; ++i) {
int64_t delay_ms = task_runner()->NextPendingTaskDelay().InMilliseconds();
// accumulate the "delay"
total_frame_time += delay_ms / 1000.0;
// Run the callback exactly when asked
task_runner()->AdvanceMockTickClock(base::Milliseconds(delay_ms));
task_runner()->RunUntilIdle();
}
double average_interval =
total_frame_time / static_cast<double>(num_iterations);
EXPECT_NEAR(1.0 / 60.0, average_interval, 0.1);
}
TEST_F(DelayBasedTimeSourceTest, TestDeactivateWhilePending) {
timer()->SetTimebaseAndInterval(base::TimeTicks(), Interval());
timer()->SetActive(true); // Should post a task.
timer()->SetActive(false);
// Should run the posted task without crashing.
EXPECT_FALSE(task_runner()->HasPendingTask());
task_runner()->RunUntilIdle();
}
TEST_F(DelayBasedTimeSourceTest,
TestDeactivateAndReactivateBeforeNextTickTime) {
timer()->SetTimebaseAndInterval(base::TimeTicks(), Interval());
// Should run the activate task, and pick up a new timebase.
timer()->SetActive(true);
task_runner()->RunUntilIdle();
// Stop the timer()
timer()->SetActive(false);
// Task will be pending anyway, run it
task_runner()->RunUntilIdle();
// Start the timer() again, but before the next tick time the timer()
// previously planned on using. That same tick time should still be targeted.
task_runner()->AdvanceMockTickClock(base::Milliseconds(4));
timer()->SetActive(true);
EXPECT_EQ(12, task_runner()->NextPendingTaskDelay().InMilliseconds());
}
TEST_F(DelayBasedTimeSourceTest, TestDeactivateAndReactivateAfterNextTickTime) {
timer()->SetTimebaseAndInterval(base::TimeTicks(), Interval());
// Should run the activate task, and pick up a new timebase.
timer()->SetActive(true);
task_runner()->RunUntilIdle();
// Stop the timer().
timer()->SetActive(false);
// Task will be pending anyway, run it.
task_runner()->RunUntilIdle();
// Start the timer() again, but before the next tick time the timer()
// previously planned on using. That same tick time should still be targeted.
task_runner()->AdvanceMockTickClock(base::Milliseconds(20));
timer()->SetActive(true);
EXPECT_EQ(13, task_runner()->NextPendingTaskDelay().InMilliseconds());
}
} // namespace
} // namespace viz
|