OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/scheduler/renderer/renderer_scheduler_impl.h" | 5 #include "components/scheduler/renderer/renderer_scheduler_impl.h" |
6 | 6 |
7 #include "base/callback.h" | 7 #include "base/callback.h" |
8 #include "cc/output/begin_frame_args.h" | 8 #include "cc/output/begin_frame_args.h" |
9 #include "cc/test/ordered_simple_task_runner.h" | 9 #include "cc/test/ordered_simple_task_runner.h" |
10 #include "cc/test/test_now_source.h" | 10 #include "cc/test/test_now_source.h" |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 int* run_count, | 71 int* run_count, |
72 base::TimeTicks deadline) { | 72 base::TimeTicks deadline) { |
73 if ((*run_count + 1) < max_idle_task_reposts) { | 73 if ((*run_count + 1) < max_idle_task_reposts) { |
74 idle_task_runner->PostIdleTask( | 74 idle_task_runner->PostIdleTask( |
75 FROM_HERE, base::Bind(&RepostingIdleTestTask, | 75 FROM_HERE, base::Bind(&RepostingIdleTestTask, |
76 base::Unretained(idle_task_runner), run_count)); | 76 base::Unretained(idle_task_runner), run_count)); |
77 } | 77 } |
78 (*run_count)++; | 78 (*run_count)++; |
79 } | 79 } |
80 | 80 |
| 81 void RepostingUpdateClockIdleTestTask( |
| 82 SingleThreadIdleTaskRunner* idle_task_runner, |
| 83 int* run_count, |
| 84 scoped_refptr<cc::TestNowSource> clock, |
| 85 base::TimeDelta advance_time, |
| 86 std::vector<base::TimeTicks>* deadlines, |
| 87 base::TimeTicks deadline) { |
| 88 if ((*run_count + 1) < max_idle_task_reposts) { |
| 89 idle_task_runner->PostIdleTask( |
| 90 FROM_HERE, base::Bind(&RepostingUpdateClockIdleTestTask, |
| 91 base::Unretained(idle_task_runner), run_count, |
| 92 clock, advance_time, deadlines)); |
| 93 } |
| 94 deadlines->push_back(deadline); |
| 95 (*run_count)++; |
| 96 clock->AdvanceNow(advance_time); |
| 97 } |
| 98 |
| 99 void WillBeginFrameIdleTask(RendererScheduler* scheduler, |
| 100 scoped_refptr<cc::TestNowSource> clock, |
| 101 base::TimeTicks deadline) { |
| 102 scheduler->WillBeginFrame(cc::BeginFrameArgs::Create( |
| 103 BEGINFRAME_FROM_HERE, clock->Now(), base::TimeTicks(), |
| 104 base::TimeDelta::FromMilliseconds(1000), cc::BeginFrameArgs::NORMAL)); |
| 105 } |
| 106 |
81 void UpdateClockToDeadlineIdleTestTask( | 107 void UpdateClockToDeadlineIdleTestTask( |
82 cc::TestNowSource* clock, | 108 cc::TestNowSource* clock, |
83 base::SingleThreadTaskRunner* task_runner, | |
84 int* run_count, | 109 int* run_count, |
85 base::TimeTicks deadline) { | 110 base::TimeTicks deadline) { |
86 clock->SetNow(deadline); | 111 clock->SetNow(deadline); |
87 // Due to the way in which OrderedSimpleTestRunner orders tasks and the fact | |
88 // that we updated the time within a task, the delayed pending task to call | |
89 // EndIdlePeriod will not happen until after a TaskQueueManager DoWork, so | |
90 // post a normal task here to ensure it runs before the next idle task. | |
91 task_runner->PostTask(FROM_HERE, base::Bind(NullTask)); | |
92 (*run_count)++; | 112 (*run_count)++; |
93 } | 113 } |
94 | 114 |
95 void PostingYieldingTestTask(RendererSchedulerImpl* scheduler, | 115 void PostingYieldingTestTask(RendererSchedulerImpl* scheduler, |
96 base::SingleThreadTaskRunner* task_runner, | 116 base::SingleThreadTaskRunner* task_runner, |
97 bool simulate_input, | 117 bool simulate_input, |
98 bool* should_yield_before, | 118 bool* should_yield_before, |
99 bool* should_yield_after) { | 119 bool* should_yield_after) { |
100 *should_yield_before = scheduler->ShouldYieldForHighPriorityWork(); | 120 *should_yield_before = scheduler->ShouldYieldForHighPriorityWork(); |
101 task_runner->PostTask(FROM_HERE, base::Bind(NullTask)); | 121 task_runner->PostTask(FROM_HERE, base::Bind(NullTask)); |
(...skipping 287 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
389 RunUntilIdle(); | 409 RunUntilIdle(); |
390 EXPECT_EQ(2, run_count); | 410 EXPECT_EQ(2, run_count); |
391 } | 411 } |
392 | 412 |
393 TEST_F(RendererSchedulerImplTest, TestIdleTaskExceedsDeadline) { | 413 TEST_F(RendererSchedulerImplTest, TestIdleTaskExceedsDeadline) { |
394 mock_task_runner_->SetAutoAdvanceNowToPendingTasks(true); | 414 mock_task_runner_->SetAutoAdvanceNowToPendingTasks(true); |
395 int run_count = 0; | 415 int run_count = 0; |
396 | 416 |
397 // Post two UpdateClockToDeadlineIdleTestTask tasks. | 417 // Post two UpdateClockToDeadlineIdleTestTask tasks. |
398 idle_task_runner_->PostIdleTask( | 418 idle_task_runner_->PostIdleTask( |
399 FROM_HERE, base::Bind(&UpdateClockToDeadlineIdleTestTask, clock_, | 419 FROM_HERE, |
400 default_task_runner_, &run_count)); | 420 base::Bind(&UpdateClockToDeadlineIdleTestTask, clock_, &run_count)); |
401 idle_task_runner_->PostIdleTask( | 421 idle_task_runner_->PostIdleTask( |
402 FROM_HERE, base::Bind(&UpdateClockToDeadlineIdleTestTask, clock_, | 422 FROM_HERE, |
403 default_task_runner_, &run_count)); | 423 base::Bind(&UpdateClockToDeadlineIdleTestTask, clock_, &run_count)); |
404 | 424 |
405 EnableIdleTasks(); | 425 EnableIdleTasks(); |
406 RunUntilIdle(); | 426 RunUntilIdle(); |
407 // Only the first idle task should execute since it's used up the deadline. | 427 // Only the first idle task should execute since it's used up the deadline. |
408 EXPECT_EQ(1, run_count); | 428 EXPECT_EQ(1, run_count); |
409 | 429 |
410 EnableIdleTasks(); | 430 EnableIdleTasks(); |
411 RunUntilIdle(); | 431 RunUntilIdle(); |
412 // Second task should be run on the next idle period. | 432 // Second task should be run on the next idle period. |
413 EXPECT_EQ(2, run_count); | 433 EXPECT_EQ(2, run_count); |
(...skipping 809 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1223 RunUntilIdle(); | 1243 RunUntilIdle(); |
1224 EXPECT_EQ(0, run_count); | 1244 EXPECT_EQ(0, run_count); |
1225 | 1245 |
1226 // After the delayed task has been run we should trigger an idle period. | 1246 // After the delayed task has been run we should trigger an idle period. |
1227 clock_->AdvanceNow(maximum_idle_period_duration()); | 1247 clock_->AdvanceNow(maximum_idle_period_duration()); |
1228 RunUntilIdle(); | 1248 RunUntilIdle(); |
1229 EXPECT_EQ(1, run_count); | 1249 EXPECT_EQ(1, run_count); |
1230 } | 1250 } |
1231 | 1251 |
1232 TEST_F(RendererSchedulerImplTest, TestLongIdlePeriodRepeating) { | 1252 TEST_F(RendererSchedulerImplTest, TestLongIdlePeriodRepeating) { |
| 1253 mock_task_runner_->SetAutoAdvanceNowToPendingTasks(true); |
| 1254 std::vector<base::TimeTicks> actual_deadlines; |
1233 int run_count = 0; | 1255 int run_count = 0; |
1234 | 1256 |
1235 max_idle_task_reposts = 3; | 1257 max_idle_task_reposts = 3; |
| 1258 base::TimeTicks clock_before(clock_->Now()); |
| 1259 base::TimeDelta idle_task_runtime(base::TimeDelta::FromMilliseconds(10)); |
1236 idle_task_runner_->PostIdleTask( | 1260 idle_task_runner_->PostIdleTask( |
1237 FROM_HERE, | 1261 FROM_HERE, |
1238 base::Bind(&RepostingIdleTestTask, idle_task_runner_, &run_count)); | 1262 base::Bind(&RepostingUpdateClockIdleTestTask, idle_task_runner_, |
1239 | 1263 &run_count, clock_, idle_task_runtime, &actual_deadlines)); |
1240 scheduler_->BeginFrameNotExpectedSoon(); | 1264 scheduler_->BeginFrameNotExpectedSoon(); |
1241 RunUntilIdle(); | 1265 RunUntilIdle(); |
1242 EXPECT_EQ(1, run_count); // Should only run once per idle period. | 1266 EXPECT_EQ(3, run_count); |
| 1267 EXPECT_THAT( |
| 1268 actual_deadlines, |
| 1269 testing::ElementsAre( |
| 1270 clock_before + maximum_idle_period_duration(), |
| 1271 clock_before + idle_task_runtime + maximum_idle_period_duration(), |
| 1272 clock_before + (2 * idle_task_runtime) + |
| 1273 maximum_idle_period_duration())); |
1243 | 1274 |
1244 // Advance time to start of next long idle period and check task reposted task | 1275 // Check that idle tasks don't run after the idle period ends with a |
1245 // gets run. | 1276 // new BeginMainFrame. |
1246 clock_->AdvanceNow(maximum_idle_period_duration()); | 1277 max_idle_task_reposts = 5; |
| 1278 idle_task_runner_->PostIdleTask( |
| 1279 FROM_HERE, |
| 1280 base::Bind(&RepostingUpdateClockIdleTestTask, idle_task_runner_, |
| 1281 &run_count, clock_, idle_task_runtime, &actual_deadlines)); |
| 1282 idle_task_runner_->PostIdleTask( |
| 1283 FROM_HERE, base::Bind(&WillBeginFrameIdleTask, |
| 1284 base::Unretained(scheduler_.get()), clock_)); |
1247 RunUntilIdle(); | 1285 RunUntilIdle(); |
1248 EXPECT_EQ(2, run_count); | 1286 EXPECT_EQ(4, run_count); |
1249 | |
1250 // Advance time to start of next long idle period then end idle period with a | |
1251 // new BeginMainFrame and check idle task doesn't run. | |
1252 clock_->AdvanceNow(maximum_idle_period_duration()); | |
1253 scheduler_->WillBeginFrame(cc::BeginFrameArgs::Create( | |
1254 BEGINFRAME_FROM_HERE, clock_->Now(), base::TimeTicks(), | |
1255 base::TimeDelta::FromMilliseconds(1000), cc::BeginFrameArgs::NORMAL)); | |
1256 RunUntilIdle(); | |
1257 EXPECT_EQ(2, run_count); | |
1258 } | 1287 } |
1259 | 1288 |
1260 TEST_F(RendererSchedulerImplTest, TestLongIdlePeriodDoesNotWakeScheduler) { | 1289 TEST_F(RendererSchedulerImplTest, TestLongIdlePeriodDoesNotWakeScheduler) { |
1261 base::TimeTicks deadline_in_task; | 1290 base::TimeTicks deadline_in_task; |
1262 int run_count = 0; | 1291 int run_count = 0; |
1263 | 1292 |
1264 // Start a long idle period and get the time it should end. | 1293 // Start a long idle period and get the time it should end. |
1265 scheduler_->BeginFrameNotExpectedSoon(); | 1294 scheduler_->BeginFrameNotExpectedSoon(); |
1266 // The scheduler should not run the initiate_next_long_idle_period task if | 1295 // The scheduler should not run the initiate_next_long_idle_period task if |
1267 // there are no idle tasks and no other task woke up the scheduler, thus | 1296 // there are no idle tasks and no other task woke up the scheduler, thus |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1368 // CanExceedIdleDeadlineIfRequired should return true. | 1397 // CanExceedIdleDeadlineIfRequired should return true. |
1369 scheduler_->WillBeginFrame(cc::BeginFrameArgs::Create( | 1398 scheduler_->WillBeginFrame(cc::BeginFrameArgs::Create( |
1370 BEGINFRAME_FROM_HERE, clock_->Now(), base::TimeTicks(), | 1399 BEGINFRAME_FROM_HERE, clock_->Now(), base::TimeTicks(), |
1371 base::TimeDelta::FromMilliseconds(1000), cc::BeginFrameArgs::NORMAL)); | 1400 base::TimeDelta::FromMilliseconds(1000), cc::BeginFrameArgs::NORMAL)); |
1372 EXPECT_FALSE(scheduler_->CanExceedIdleDeadlineIfRequired()); | 1401 EXPECT_FALSE(scheduler_->CanExceedIdleDeadlineIfRequired()); |
1373 } | 1402 } |
1374 | 1403 |
1375 TEST_F(RendererSchedulerImplTest, TestRendererHiddenIdlePeriod) { | 1404 TEST_F(RendererSchedulerImplTest, TestRendererHiddenIdlePeriod) { |
1376 int run_count = 0; | 1405 int run_count = 0; |
1377 | 1406 |
| 1407 max_idle_task_reposts = 2; |
1378 idle_task_runner_->PostIdleTask( | 1408 idle_task_runner_->PostIdleTask( |
1379 FROM_HERE, | 1409 FROM_HERE, |
1380 base::Bind(&RepostingIdleTestTask, idle_task_runner_, &run_count)); | 1410 base::Bind(&RepostingIdleTestTask, idle_task_runner_, &run_count)); |
1381 | 1411 |
1382 // Renderer should start in visible state. | 1412 // Renderer should start in visible state. |
1383 RunUntilIdle(); | 1413 RunUntilIdle(); |
1384 EXPECT_EQ(0, run_count); | 1414 EXPECT_EQ(0, run_count); |
1385 | 1415 |
1386 // When we hide the renderer it should start an idle period. | 1416 // When we hide the renderer it should start a max deadline idle period, which |
| 1417 // will run an idle task and then immediately start a new idle period, which |
| 1418 // runs the second idle task. |
1387 scheduler_->OnRendererHidden(); | 1419 scheduler_->OnRendererHidden(); |
1388 RunUntilIdle(); | 1420 RunUntilIdle(); |
1389 EXPECT_EQ(1, run_count); | |
1390 | |
1391 // Advance time to start of next long idle period and check task reposted task | |
1392 // gets run. | |
1393 clock_->AdvanceNow(maximum_idle_period_duration()); | |
1394 RunUntilIdle(); | |
1395 EXPECT_EQ(2, run_count); | 1421 EXPECT_EQ(2, run_count); |
1396 | 1422 |
1397 // Advance time by amount of time by the maximum amount of time we execute | 1423 // Advance time by amount of time by the maximum amount of time we execute |
1398 // idle tasks when hidden (plus some slack) - idle period should have ended. | 1424 // idle tasks when hidden (plus some slack) - idle period should have ended. |
| 1425 max_idle_task_reposts = 3; |
| 1426 idle_task_runner_->PostIdleTask( |
| 1427 FROM_HERE, |
| 1428 base::Bind(&RepostingIdleTestTask, idle_task_runner_, &run_count)); |
1399 clock_->AdvanceNow(end_idle_when_hidden_delay() + | 1429 clock_->AdvanceNow(end_idle_when_hidden_delay() + |
1400 base::TimeDelta::FromMilliseconds(10)); | 1430 base::TimeDelta::FromMilliseconds(10)); |
1401 RunUntilIdle(); | 1431 RunUntilIdle(); |
1402 EXPECT_EQ(2, run_count); | 1432 EXPECT_EQ(2, run_count); |
1403 } | 1433 } |
1404 | 1434 |
1405 TEST_F(RendererSchedulerImplTest, TimerQueueEnabledByDefault) { | 1435 TEST_F(RendererSchedulerImplTest, TimerQueueEnabledByDefault) { |
1406 std::vector<std::string> run_order; | 1436 std::vector<std::string> run_order; |
1407 PostTestTasks(&run_order, "T1 T2"); | 1437 PostTestTasks(&run_order, "T1 T2"); |
1408 RunUntilIdle(); | 1438 RunUntilIdle(); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1454 | 1484 |
1455 TEST_F(RendererSchedulerImplTest, PolicyToString) { | 1485 TEST_F(RendererSchedulerImplTest, PolicyToString) { |
1456 CheckAllTaskQueueIdToString(); | 1486 CheckAllTaskQueueIdToString(); |
1457 } | 1487 } |
1458 | 1488 |
1459 TEST_F(RendererSchedulerImplTest, InputStreamStateToString) { | 1489 TEST_F(RendererSchedulerImplTest, InputStreamStateToString) { |
1460 CheckAllInputStreamStateToString(); | 1490 CheckAllInputStreamStateToString(); |
1461 } | 1491 } |
1462 | 1492 |
1463 } // namespace scheduler | 1493 } // namespace scheduler |
OLD | NEW |