| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "cc/tiles/image_controller.h" | 5 #include "cc/tiles/image_controller.h" |
| 6 #include "base/bind.h" | 6 #include "base/bind.h" |
| 7 #include "base/optional.h" | 7 #include "base/optional.h" |
| 8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
| 9 #include "base/test/test_simple_task_runner.h" | 9 #include "base/test/test_simple_task_runner.h" |
| 10 #include "base/threading/sequenced_task_runner_handle.h" | 10 #include "base/threading/sequenced_task_runner_handle.h" |
| (...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 259 | 259 |
| 260 // Convenience method to run the run loop with a timeout. | 260 // Convenience method to run the run loop with a timeout. |
| 261 void RunOrTimeout(base::RunLoop* run_loop) { | 261 void RunOrTimeout(base::RunLoop* run_loop) { |
| 262 task_runner_->PostDelayedTask( | 262 task_runner_->PostDelayedTask( |
| 263 FROM_HERE, | 263 FROM_HERE, |
| 264 base::Bind(&ImageControllerTest::Timeout, base::Unretained(run_loop)), | 264 base::Bind(&ImageControllerTest::Timeout, base::Unretained(run_loop)), |
| 265 base::TimeDelta::FromSeconds(kDefaultTimeoutSeconds)); | 265 base::TimeDelta::FromSeconds(kDefaultTimeoutSeconds)); |
| 266 run_loop->Run(); | 266 run_loop->Run(); |
| 267 } | 267 } |
| 268 | 268 |
| 269 void ResetController() { controller_.reset(); } |
| 270 |
| 269 private: | 271 private: |
| 270 scoped_refptr<base::SequencedTaskRunner> task_runner_; | 272 scoped_refptr<base::SequencedTaskRunner> task_runner_; |
| 271 scoped_refptr<WorkerTaskRunner> worker_task_runner_; | 273 scoped_refptr<WorkerTaskRunner> worker_task_runner_; |
| 272 TestableCache cache_; | 274 TestableCache cache_; |
| 273 std::unique_ptr<ImageController> controller_; | 275 std::unique_ptr<ImageController> controller_; |
| 274 sk_sp<const SkImage> image_; | 276 sk_sp<const SkImage> image_; |
| 275 }; | 277 }; |
| 276 | 278 |
| 277 TEST_F(ImageControllerTest, NullControllerUnrefsImages) { | 279 TEST_F(ImageControllerTest, NullControllerUnrefsImages) { |
| 278 std::vector<DrawImage> images(10); | 280 std::vector<DrawImage> images(10); |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 442 DecodeClient decode_client2; | 444 DecodeClient decode_client2; |
| 443 ImageController::ImageDecodeRequestId expected_id2 = | 445 ImageController::ImageDecodeRequestId expected_id2 = |
| 444 controller()->QueueImageDecode( | 446 controller()->QueueImageDecode( |
| 445 image(), | 447 image(), |
| 446 base::Bind(&DecodeClient::Callback, base::Unretained(&decode_client2), | 448 base::Bind(&DecodeClient::Callback, base::Unretained(&decode_client2), |
| 447 run_loop.QuitClosure())); | 449 run_loop.QuitClosure())); |
| 448 | 450 |
| 449 task_one->AllowToRun(); | 451 task_one->AllowToRun(); |
| 450 task_two->AllowToRun(); | 452 task_two->AllowToRun(); |
| 451 controller()->SetImageDecodeCache(nullptr); | 453 controller()->SetImageDecodeCache(nullptr); |
| 454 ResetController(); |
| 452 | 455 |
| 453 RunOrTimeout(&run_loop); | 456 RunOrTimeout(&run_loop); |
| 454 | 457 |
| 455 EXPECT_TRUE(task_one->state().IsCanceled() || task_one->HasCompleted()); | 458 EXPECT_TRUE(task_one->state().IsCanceled() || task_one->HasCompleted()); |
| 456 EXPECT_TRUE(task_two->state().IsCanceled() || task_two->HasCompleted()); | 459 EXPECT_TRUE(task_two->state().IsCanceled() || task_two->HasCompleted()); |
| 457 EXPECT_EQ(expected_id1, decode_client1.id()); | 460 EXPECT_EQ(expected_id1, decode_client1.id()); |
| 458 EXPECT_EQ(expected_id2, decode_client2.id()); | 461 EXPECT_EQ(expected_id2, decode_client2.id()); |
| 459 } | 462 } |
| 460 | 463 |
| 461 TEST_F(ImageControllerTest, QueueImageDecodeImageAlreadyLocked) { | 464 TEST_F(ImageControllerTest, QueueImageDecodeImageAlreadyLocked) { |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 500 run_loop1.QuitClosure())); | 503 run_loop1.QuitClosure())); |
| 501 RunOrTimeout(&run_loop1); | 504 RunOrTimeout(&run_loop1); |
| 502 EXPECT_EQ(expected_id1, decode_client1.id()); | 505 EXPECT_EQ(expected_id1, decode_client1.id()); |
| 503 EXPECT_TRUE(task->has_run()); | 506 EXPECT_TRUE(task->has_run()); |
| 504 EXPECT_EQ(1, cache()->number_of_refs()); | 507 EXPECT_EQ(1, cache()->number_of_refs()); |
| 505 | 508 |
| 506 controller()->SetImageDecodeCache(nullptr); | 509 controller()->SetImageDecodeCache(nullptr); |
| 507 EXPECT_EQ(0, cache()->number_of_refs()); | 510 EXPECT_EQ(0, cache()->number_of_refs()); |
| 508 } | 511 } |
| 509 | 512 |
| 510 TEST_F(ImageControllerTest, DispatchesDecodeCallbacksAfterCacheChanged) { | 513 TEST_F(ImageControllerTest, DispatchesDecodeCallbacksAfterCacheReset) { |
| 511 scoped_refptr<SimpleTask> task(new SimpleTask); | 514 scoped_refptr<SimpleTask> task(new SimpleTask); |
| 512 cache()->SetTaskToUse(task); | 515 cache()->SetTaskToUse(task); |
| 513 | 516 |
| 514 base::RunLoop run_loop1; | 517 base::RunLoop run_loop1; |
| 515 DecodeClient decode_client1; | 518 DecodeClient decode_client1; |
| 516 base::RunLoop run_loop2; | 519 base::RunLoop run_loop2; |
| 517 DecodeClient decode_client2; | 520 DecodeClient decode_client2; |
| 518 | 521 |
| 519 controller()->QueueImageDecode( | 522 controller()->QueueImageDecode( |
| 520 image(), | 523 image(), |
| 521 base::Bind(&DecodeClient::Callback, base::Unretained(&decode_client1), | 524 base::Bind(&DecodeClient::Callback, base::Unretained(&decode_client1), |
| 522 run_loop1.QuitClosure())); | 525 run_loop1.QuitClosure())); |
| 523 controller()->QueueImageDecode( | 526 controller()->QueueImageDecode( |
| 524 image(), | 527 image(), |
| 525 base::Bind(&DecodeClient::Callback, base::Unretained(&decode_client2), | 528 base::Bind(&DecodeClient::Callback, base::Unretained(&decode_client2), |
| 526 run_loop2.QuitClosure())); | 529 run_loop2.QuitClosure())); |
| 527 | 530 |
| 528 // Now reset the image cache before decode completed callbacks are posted to | 531 // Now reset the image cache before decode completed callbacks are posted to |
| 529 // the compositor thread. Ensure that the completion callbacks for the decode | 532 // the compositor thread. Ensure that the completion callbacks for the decode |
| 530 // is still run. | 533 // is still run. |
| 531 controller()->SetImageDecodeCache(nullptr); | 534 controller()->SetImageDecodeCache(nullptr); |
| 535 ResetController(); |
| 536 |
| 532 RunOrTimeout(&run_loop1); | 537 RunOrTimeout(&run_loop1); |
| 533 RunOrTimeout(&run_loop2); | 538 RunOrTimeout(&run_loop2); |
| 539 |
| 540 EXPECT_EQ(ImageController::ImageDecodeResult::FAILURE, |
| 541 decode_client1.result()); |
| 542 EXPECT_EQ(ImageController::ImageDecodeResult::FAILURE, |
| 543 decode_client2.result()); |
| 544 } |
| 545 |
| 546 TEST_F(ImageControllerTest, DispatchesDecodeCallbacksAfterCacheChanged) { |
| 547 scoped_refptr<SimpleTask> task(new SimpleTask); |
| 548 cache()->SetTaskToUse(task); |
| 549 |
| 550 base::RunLoop run_loop1; |
| 551 DecodeClient decode_client1; |
| 552 base::RunLoop run_loop2; |
| 553 DecodeClient decode_client2; |
| 554 |
| 555 controller()->QueueImageDecode( |
| 556 image(), |
| 557 base::Bind(&DecodeClient::Callback, base::Unretained(&decode_client1), |
| 558 run_loop1.QuitClosure())); |
| 559 controller()->QueueImageDecode( |
| 560 image(), |
| 561 base::Bind(&DecodeClient::Callback, base::Unretained(&decode_client2), |
| 562 run_loop2.QuitClosure())); |
| 563 |
| 564 // Now reset the image cache before decode completed callbacks are posted to |
| 565 // the compositor thread. This should orphan the requests. |
| 566 controller()->SetImageDecodeCache(nullptr); |
| 567 |
| 568 EXPECT_EQ(0, cache()->number_of_refs()); |
| 569 |
| 570 TestableCache other_cache; |
| 571 other_cache.SetTaskToUse(task); |
| 572 |
| 573 controller()->SetImageDecodeCache(&other_cache); |
| 574 |
| 575 RunOrTimeout(&run_loop1); |
| 576 RunOrTimeout(&run_loop2); |
| 577 |
| 578 EXPECT_EQ(2, other_cache.number_of_refs()); |
| 579 EXPECT_EQ(ImageController::ImageDecodeResult::SUCCESS, |
| 580 decode_client1.result()); |
| 581 EXPECT_EQ(ImageController::ImageDecodeResult::SUCCESS, |
| 582 decode_client2.result()); |
| 583 |
| 584 // Reset the controller since the order of destruction is wrong in this test |
| 585 // (|other_cache| should outlive the controller. This is normally done via |
| 586 // SetImageDecodeCache(nullptr) or it can be done in the dtor of the cache.) |
| 587 ResetController(); |
| 534 } | 588 } |
| 535 | 589 |
| 536 } // namespace | 590 } // namespace |
| 537 } // namespace cc | 591 } // namespace cc |
| OLD | NEW |