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 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/optional.h" | 10 #include "base/optional.h" |
11 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
12 #include "base/test/test_simple_task_runner.h" | 12 #include "base/test/test_simple_task_runner.h" |
13 #include "base/threading/sequenced_task_runner_handle.h" | 13 #include "base/threading/sequenced_task_runner_handle.h" |
14 #include "base/threading/thread_checker_impl.h" | 14 #include "base/threading/thread_checker_impl.h" |
15 #include "cc/test/skia_common.h" | 15 #include "cc/test/skia_common.h" |
16 #include "cc/tiles/image_decode_cache.h" | 16 #include "cc/tiles/image_decode_cache.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
18 | 18 |
19 namespace cc { | 19 namespace cc { |
20 namespace { | 20 namespace { |
21 | 21 |
22 class TestWorkerThread : public base::SimpleThread { | 22 class TestWorkerThread : public base::SimpleThread { |
23 public: | 23 public: |
24 TestWorkerThread() | 24 TestWorkerThread() |
25 : base::SimpleThread("test_worker_thread"), condition_(&lock_) {} | 25 : base::SimpleThread("test_worker_thread"), condition_(&lock_) {} |
26 | 26 |
27 void Run() override { | 27 void Run() override { |
28 for (;;) { | 28 for (;;) { |
29 base::Closure task; | 29 base::OnceClosure task; |
30 { | 30 { |
31 base::AutoLock hold(lock_); | 31 base::AutoLock hold(lock_); |
32 if (shutdown_) | 32 if (shutdown_) |
33 break; | 33 break; |
34 | 34 |
35 if (queue_.empty()) { | 35 if (queue_.empty()) { |
36 condition_.Wait(); | 36 condition_.Wait(); |
37 continue; | 37 continue; |
38 } | 38 } |
39 | 39 |
40 task = std::move(queue_.front()); | 40 task = std::move(queue_.front()); |
41 queue_.erase(queue_.begin()); | 41 queue_.erase(queue_.begin()); |
42 } | 42 } |
43 std::move(task).Run(); | 43 std::move(task).Run(); |
44 } | 44 } |
45 } | 45 } |
46 | 46 |
47 void Shutdown() { | 47 void Shutdown() { |
48 base::AutoLock hold(lock_); | 48 base::AutoLock hold(lock_); |
49 shutdown_ = true; | 49 shutdown_ = true; |
50 condition_.Signal(); | 50 condition_.Signal(); |
51 } | 51 } |
52 | 52 |
53 void PostTask(base::Closure task) { | 53 void PostTask(base::OnceClosure task) { |
54 base::AutoLock hold(lock_); | 54 base::AutoLock hold(lock_); |
55 queue_.push_back(std::move(task)); | 55 queue_.push_back(std::move(task)); |
56 condition_.Signal(); | 56 condition_.Signal(); |
57 } | 57 } |
58 | 58 |
59 private: | 59 private: |
60 base::Lock lock_; | 60 base::Lock lock_; |
61 base::ConditionVariable condition_; | 61 base::ConditionVariable condition_; |
62 std::vector<base::Closure> queue_; | 62 std::vector<base::OnceClosure> queue_; |
63 bool shutdown_ = false; | 63 bool shutdown_ = false; |
64 }; | 64 }; |
65 | 65 |
66 class WorkerTaskRunner : public base::SequencedTaskRunner { | 66 class WorkerTaskRunner : public base::SequencedTaskRunner { |
67 public: | 67 public: |
68 WorkerTaskRunner() { thread_.Start(); } | 68 WorkerTaskRunner() { thread_.Start(); } |
69 | 69 |
70 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here, | 70 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here, |
71 base::Closure task, | 71 base::OnceClosure task, |
72 base::TimeDelta delay) override { | 72 base::TimeDelta delay) override { |
73 return PostDelayedTask(from_here, std::move(task), delay); | 73 return PostDelayedTask(from_here, std::move(task), delay); |
74 } | 74 } |
75 | 75 |
76 bool PostDelayedTask(const tracked_objects::Location& from_here, | 76 bool PostDelayedTask(const tracked_objects::Location& from_here, |
77 base::Closure task, | 77 base::OnceClosure task, |
78 base::TimeDelta delay) override { | 78 base::TimeDelta delay) override { |
79 thread_.PostTask(std::move(task)); | 79 thread_.PostTask(std::move(task)); |
80 return true; | 80 return true; |
81 } | 81 } |
82 | 82 |
83 bool RunsTasksOnCurrentThread() const override { return false; } | 83 bool RunsTasksOnCurrentThread() const override { return false; } |
84 | 84 |
85 protected: | 85 protected: |
86 ~WorkerTaskRunner() override { | 86 ~WorkerTaskRunner() override { |
87 thread_.Shutdown(); | 87 thread_.Shutdown(); |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
135 | 135 |
136 private: | 136 private: |
137 int number_of_refs_ = 0; | 137 int number_of_refs_ = 0; |
138 scoped_refptr<TileTask> task_to_use_; | 138 scoped_refptr<TileTask> task_to_use_; |
139 }; | 139 }; |
140 | 140 |
141 // A simple class that can receive decode callbacks. | 141 // A simple class that can receive decode callbacks. |
142 class DecodeClient { | 142 class DecodeClient { |
143 public: | 143 public: |
144 DecodeClient() {} | 144 DecodeClient() {} |
145 void Callback(base::Closure quit_closure, | 145 void Callback(base::OnceClosure quit_closure, |
146 ImageController::ImageDecodeRequestId id, | 146 ImageController::ImageDecodeRequestId id, |
147 ImageController::ImageDecodeResult result) { | 147 ImageController::ImageDecodeResult result) { |
148 id_ = id; | 148 id_ = id; |
149 result_ = result; | 149 result_ = result; |
150 std::move(quit_closure).Run(); | 150 std::move(quit_closure).Run(); |
151 } | 151 } |
152 | 152 |
153 ImageController::ImageDecodeRequestId id() { return id_; } | 153 ImageController::ImageDecodeRequestId id() { return id_; } |
154 ImageController::ImageDecodeResult result() { return result_; } | 154 ImageController::ImageDecodeResult result() { return result_; } |
155 | 155 |
(...skipping 431 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
587 decode_client2.result()); | 587 decode_client2.result()); |
588 | 588 |
589 // Reset the controller since the order of destruction is wrong in this test | 589 // Reset the controller since the order of destruction is wrong in this test |
590 // (|other_cache| should outlive the controller. This is normally done via | 590 // (|other_cache| should outlive the controller. This is normally done via |
591 // SetImageDecodeCache(nullptr) or it can be done in the dtor of the cache.) | 591 // SetImageDecodeCache(nullptr) or it can be done in the dtor of the cache.) |
592 ResetController(); | 592 ResetController(); |
593 } | 593 } |
594 | 594 |
595 } // namespace | 595 } // namespace |
596 } // namespace cc | 596 } // namespace cc |
OLD | NEW |