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 "config.h" | 5 #include "config.h" |
6 #include "platform/scheduler/Scheduler.h" | 6 #include "platform/scheduler/Scheduler.h" |
7 | 7 |
8 #include "platform/TestingPlatformSupport.h" | 8 #include "platform/TestingPlatformSupport.h" |
9 #include "platform/TraceLocation.h" | 9 #include "platform/TraceLocation.h" |
10 #include "public/platform/Platform.h" | 10 #include "public/platform/Platform.h" |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
181 // If the Scheduler hasn't been shut down then explicitly flush the task
s. | 181 // If the Scheduler hasn't been shut down then explicitly flush the task
s. |
182 if (Scheduler::shared()) | 182 if (Scheduler::shared()) |
183 runPendingTasks(); | 183 runPendingTasks(); |
184 } | 184 } |
185 | 185 |
186 void runPendingTasks() | 186 void runPendingTasks() |
187 { | 187 { |
188 m_platformSupport.runPendingTasks(); | 188 m_platformSupport.runPendingTasks(); |
189 } | 189 } |
190 | 190 |
| 191 void enableIdleTasks() |
| 192 { |
| 193 m_platformSupport.setMonotonicTimeForTest(0); |
| 194 m_scheduler->willBeginFrame(1); |
| 195 m_scheduler->didCommitFrameToCompositor(); |
| 196 } |
| 197 |
191 void appendToVector(std::string value) | 198 void appendToVector(std::string value) |
192 { | 199 { |
193 m_order.push_back(value); | 200 m_order.push_back(value); |
194 } | 201 } |
195 | 202 |
| 203 void appendToVectorIdleTask(std::string value, double deadline) |
| 204 { |
| 205 appendToVector(value); |
| 206 } |
| 207 |
196 void appendToVectorReentrantTask() | 208 void appendToVectorReentrantTask() |
197 { | 209 { |
198 m_reentrantOrder.push_back(m_reentrantCount++); | 210 m_reentrantOrder.push_back(m_reentrantCount++); |
199 | 211 |
200 if (m_reentrantCount > m_maxRecursion) | 212 if (m_reentrantCount > m_maxRecursion) |
201 return; | 213 return; |
202 Scheduler::shared()->postTask(FROM_HERE, WTF::bind(&SchedulerTest::appen
dToVectorReentrantTask, this)); | 214 Scheduler::shared()->postTask(FROM_HERE, WTF::bind(&SchedulerTest::appen
dToVectorReentrantTask, this)); |
203 } | 215 } |
204 | 216 |
205 void appendToVectorReentrantInputTask() | 217 void appendToVectorReentrantInputTask() |
206 { | 218 { |
207 m_reentrantOrder.push_back(m_reentrantCount++); | 219 m_reentrantOrder.push_back(m_reentrantCount++); |
208 | 220 |
209 if (m_reentrantCount > m_maxRecursion) | 221 if (m_reentrantCount > m_maxRecursion) |
210 return; | 222 return; |
211 m_scheduler->postInputTask(FROM_HERE, WTF::bind(&SchedulerTest::appendTo
VectorReentrantInputTask, this)); | 223 m_scheduler->postInputTask(FROM_HERE, WTF::bind(&SchedulerTest::appendTo
VectorReentrantInputTask, this)); |
212 } | 224 } |
213 | 225 |
214 void appendToVectorReentrantCompositorTask() | 226 void appendToVectorReentrantCompositorTask() |
215 { | 227 { |
216 m_reentrantOrder.push_back(m_reentrantCount++); | 228 m_reentrantOrder.push_back(m_reentrantCount++); |
217 | 229 |
218 if (m_reentrantCount > m_maxRecursion) | 230 if (m_reentrantCount > m_maxRecursion) |
219 return; | 231 return; |
220 m_scheduler->postCompositorTask(FROM_HERE, WTF::bind(&SchedulerTest::app
endToVectorReentrantCompositorTask, this)); | 232 m_scheduler->postCompositorTask(FROM_HERE, WTF::bind(&SchedulerTest::app
endToVectorReentrantCompositorTask, this)); |
221 } | 233 } |
222 | |
223 protected: | 234 protected: |
224 SchedulerTestingPlatformSupport m_platformSupport; | 235 SchedulerTestingPlatformSupport m_platformSupport; |
225 SchedulerForTest* m_scheduler; | 236 SchedulerForTest* m_scheduler; |
226 std::vector<std::string> m_order; | 237 std::vector<std::string> m_order; |
227 std::vector<int> m_reentrantOrder; | 238 std::vector<int> m_reentrantOrder; |
228 int m_reentrantCount; | 239 int m_reentrantCount; |
229 int m_maxRecursion; | 240 int m_maxRecursion; |
230 }; | 241 }; |
231 | 242 |
232 void orderedTestTask(int value, int* result) | 243 void orderedTestTask(int value, int* result) |
233 { | 244 { |
234 *result = (*result << 4) | value; | 245 *result = (*result << 4) | value; |
235 } | 246 } |
236 | 247 |
237 void unorderedTestTask(int value, int* result) | 248 void unorderedTestTask(int value, int* result) |
238 { | 249 { |
239 *result += value; | 250 *result += value; |
240 } | 251 } |
241 | 252 |
242 void idleTestTask(int value, int* result, double allottedTime) | 253 void idleTestTask(bool* taskRun, double expectedDeadline, double deadlineSeconds
) |
243 { | 254 { |
244 *result += value; | 255 EXPECT_FALSE(*taskRun); |
| 256 EXPECT_EQ(expectedDeadline, deadlineSeconds); |
| 257 *taskRun = true; |
245 } | 258 } |
246 | 259 |
247 TEST_F(SchedulerTest, TestPostTask) | 260 TEST_F(SchedulerTest, TestPostTask) |
248 { | 261 { |
249 int result = 0; | 262 int result = 0; |
250 m_scheduler->postTask(FROM_HERE, WTF::bind(&orderedTestTask, 1, &result)); | 263 m_scheduler->postTask(FROM_HERE, WTF::bind(&orderedTestTask, 1, &result)); |
251 m_scheduler->postTask(FROM_HERE, WTF::bind(&orderedTestTask, 2, &result)); | 264 m_scheduler->postTask(FROM_HERE, WTF::bind(&orderedTestTask, 2, &result)); |
252 m_scheduler->postTask(FROM_HERE, WTF::bind(&orderedTestTask, 3, &result)); | 265 m_scheduler->postTask(FROM_HERE, WTF::bind(&orderedTestTask, 3, &result)); |
253 m_scheduler->postTask(FROM_HERE, WTF::bind(&orderedTestTask, 4, &result)); | 266 m_scheduler->postTask(FROM_HERE, WTF::bind(&orderedTestTask, 4, &result)); |
254 runPendingTasks(); | 267 runPendingTasks(); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
286 | 299 |
287 m_scheduler->stopSharedTimer(); | 300 m_scheduler->stopSharedTimer(); |
288 EXPECT_FALSE(m_platformSupport.sharedTimerRunning()); | 301 EXPECT_FALSE(m_platformSupport.sharedTimerRunning()); |
289 | 302 |
290 m_scheduler->setSharedTimerFiredFunction(nullptr); | 303 m_scheduler->setSharedTimerFiredFunction(nullptr); |
291 EXPECT_FALSE(m_platformSupport.sharedTimerRunning()); | 304 EXPECT_FALSE(m_platformSupport.sharedTimerRunning()); |
292 } | 305 } |
293 | 306 |
294 TEST_F(SchedulerTest, TestIdleTask) | 307 TEST_F(SchedulerTest, TestIdleTask) |
295 { | 308 { |
296 // TODO: Check task allottedTime when implemented in the scheduler. | 309 bool taskRun = false; |
297 int result = 0; | 310 double firstDeadline = 1.1; |
298 m_scheduler->postIdleTask(FROM_HERE, WTF::bind<double>(&idleTestTask, 1, &re
sult)); | 311 double secondDeadline = 2.3; |
299 m_scheduler->postIdleTask(FROM_HERE, WTF::bind<double>(&idleTestTask, 1, &re
sult)); | 312 m_platformSupport.setMonotonicTimeForTest(0.1); |
300 m_scheduler->postIdleTask(FROM_HERE, WTF::bind<double>(&idleTestTask, 1, &re
sult)); | 313 |
301 m_scheduler->postIdleTask(FROM_HERE, WTF::bind<double>(&idleTestTask, 1, &re
sult)); | 314 m_scheduler->postIdleTask(FROM_HERE, WTF::bind<double>(&idleTestTask, &taskR
un, secondDeadline)); |
| 315 |
302 runPendingTasks(); | 316 runPendingTasks(); |
303 EXPECT_EQ(4, result); | 317 EXPECT_FALSE(taskRun); // Shouldn't run yet as no willBeginFrame. |
| 318 |
| 319 m_scheduler->willBeginFrame(firstDeadline); |
| 320 runPendingTasks(); |
| 321 EXPECT_FALSE(taskRun); // Shouldn't run yet as no didCommitFrameToCompositor
. |
| 322 |
| 323 m_platformSupport.setMonotonicTimeForTest(firstDeadline + 0.1); |
| 324 m_scheduler->didCommitFrameToCompositor(); |
| 325 runPendingTasks(); |
| 326 EXPECT_FALSE(taskRun); // We missed the deadline. |
| 327 |
| 328 m_scheduler->willBeginFrame(secondDeadline); |
| 329 m_platformSupport.setMonotonicTimeForTest(secondDeadline - 0.1); |
| 330 m_scheduler->didCommitFrameToCompositor(); |
| 331 runPendingTasks(); |
| 332 EXPECT_TRUE(taskRun); |
304 } | 333 } |
305 | 334 |
306 TEST_F(SchedulerTest, TestTaskPrioritization_normalPolicy) | 335 TEST_F(SchedulerTest, TestTaskPrioritization_normalPolicy) |
307 { | 336 { |
308 m_scheduler->postTask(FROM_HERE, WTF::bind(&SchedulerTest::appendToVector, t
his, std::string("L1"))); | 337 m_scheduler->postTask(FROM_HERE, WTF::bind(&SchedulerTest::appendToVector, t
his, std::string("L1"))); |
309 m_scheduler->postTask(FROM_HERE, WTF::bind(&SchedulerTest::appendToVector, t
his, std::string("L2"))); | 338 m_scheduler->postTask(FROM_HERE, WTF::bind(&SchedulerTest::appendToVector, t
his, std::string("L2"))); |
310 m_scheduler->postInputTask(FROM_HERE, WTF::bind(&SchedulerTest::appendToVect
or, this, std::string("I1"))); | 339 m_scheduler->postInputTask(FROM_HERE, WTF::bind(&SchedulerTest::appendToVect
or, this, std::string("I1"))); |
311 m_scheduler->postCompositorTask(FROM_HERE, WTF::bind(&SchedulerTest::appendT
oVector, this, std::string("C1"))); | 340 m_scheduler->postCompositorTask(FROM_HERE, WTF::bind(&SchedulerTest::appendT
oVector, this, std::string("C1"))); |
312 m_scheduler->postInputTask(FROM_HERE, WTF::bind(&SchedulerTest::appendToVect
or, this, std::string("I2"))); | 341 m_scheduler->postInputTask(FROM_HERE, WTF::bind(&SchedulerTest::appendToVect
or, this, std::string("I2"))); |
313 m_scheduler->postCompositorTask(FROM_HERE, WTF::bind(&SchedulerTest::appendT
oVector, this, std::string("C2"))); | 342 m_scheduler->postCompositorTask(FROM_HERE, WTF::bind(&SchedulerTest::appendT
oVector, this, std::string("C2"))); |
(...skipping 12 matching lines...) Expand all Loading... |
326 | 355 |
327 EXPECT_THAT(m_reentrantOrder, testing::ElementsAre(0, 1, 2, 3, 4)); | 356 EXPECT_THAT(m_reentrantOrder, testing::ElementsAre(0, 1, 2, 3, 4)); |
328 } | 357 } |
329 | 358 |
330 TEST_F(SchedulerTest, TestTasksRunAfterShutdown) | 359 TEST_F(SchedulerTest, TestTasksRunAfterShutdown) |
331 { | 360 { |
332 m_scheduler->postTask(FROM_HERE, WTF::bind(&SchedulerTest::appendToVector, t
his, std::string("1"))); | 361 m_scheduler->postTask(FROM_HERE, WTF::bind(&SchedulerTest::appendToVector, t
his, std::string("1"))); |
333 m_scheduler->postInputTask(FROM_HERE, WTF::bind(&SchedulerTest::appendToVect
or, this, std::string("2"))); | 362 m_scheduler->postInputTask(FROM_HERE, WTF::bind(&SchedulerTest::appendToVect
or, this, std::string("2"))); |
334 m_scheduler->postCompositorTask(FROM_HERE, WTF::bind(&SchedulerTest::appendT
oVector, this, std::string("3"))); | 363 m_scheduler->postCompositorTask(FROM_HERE, WTF::bind(&SchedulerTest::appendT
oVector, this, std::string("3"))); |
335 m_scheduler->postIpcTask(FROM_HERE, WTF::bind(&SchedulerTest::appendToVector
, this, std::string("4"))); | 364 m_scheduler->postIpcTask(FROM_HERE, WTF::bind(&SchedulerTest::appendToVector
, this, std::string("4"))); |
| 365 // Idle task should not be run if scheduler is shutdown, but should not cras
h when flushed. |
| 366 m_scheduler->postIdleTask(FROM_HERE, WTF::bind<double>(&SchedulerTest::appen
dToVectorIdleTask, this, std::string("Not Run"))); |
| 367 enableIdleTasks(); |
336 | 368 |
337 Scheduler::shutdown(); | 369 Scheduler::shutdown(); |
338 EXPECT_TRUE(m_order.empty()); | 370 EXPECT_TRUE(m_order.empty()); |
339 | 371 |
340 runPendingTasks(); | 372 runPendingTasks(); |
341 EXPECT_THAT(m_order, testing::ElementsAre( | 373 EXPECT_THAT(m_order, testing::ElementsAre( |
342 std::string("1"), std::string("2"), std::string("3"), std::string("4")))
; | 374 std::string("1"), std::string("2"), std::string("3"), std::string("4")))
; |
343 } | 375 } |
344 | 376 |
345 bool s_shouldContinue; | 377 bool s_shouldContinue; |
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
498 m_platformSupport.setMonotonicTimeForTest(1000.5); | 530 m_platformSupport.setMonotonicTimeForTest(1000.5); |
499 runPendingTasks(); | 531 runPendingTasks(); |
500 | 532 |
501 ASSERT_FALSE(m_scheduler->shouldYieldForHighPriorityWork()); | 533 ASSERT_FALSE(m_scheduler->shouldYieldForHighPriorityWork()); |
502 m_scheduler->postCompositorTask(FROM_HERE, WTF::bind(&dummyTask)); | 534 m_scheduler->postCompositorTask(FROM_HERE, WTF::bind(&dummyTask)); |
503 | 535 |
504 EXPECT_FALSE(m_scheduler->shouldYieldForHighPriorityWork()); | 536 EXPECT_FALSE(m_scheduler->shouldYieldForHighPriorityWork()); |
505 } | 537 } |
506 | 538 |
507 } // namespace | 539 } // namespace |
OLD | NEW |