Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(901)

Side by Side Diff: cc/tiles/image_controller_unittest.cc

Issue 2726523002: Pass Callback to TaskRunner by value and consume it on invocation (1) (Closed)
Patch Set: rebase Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
7 #include <utility>
8
6 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/callback_helpers.h"
7 #include "base/optional.h" 11 #include "base/optional.h"
8 #include "base/run_loop.h" 12 #include "base/run_loop.h"
9 #include "base/test/test_simple_task_runner.h" 13 #include "base/test/test_simple_task_runner.h"
10 #include "base/threading/sequenced_task_runner_handle.h" 14 #include "base/threading/sequenced_task_runner_handle.h"
11 #include "cc/test/skia_common.h" 15 #include "cc/test/skia_common.h"
12 #include "cc/tiles/image_decode_cache.h" 16 #include "cc/tiles/image_decode_cache.h"
13 #include "testing/gtest/include/gtest/gtest.h" 17 #include "testing/gtest/include/gtest/gtest.h"
14 18
15 namespace cc { 19 namespace cc {
16 namespace { 20 namespace {
17 21
18 class TestWorkerThread : public base::SimpleThread { 22 class TestWorkerThread : public base::SimpleThread {
19 public: 23 public:
20 TestWorkerThread() 24 TestWorkerThread()
21 : base::SimpleThread("test_worker_thread"), condition_(&lock_) {} 25 : base::SimpleThread("test_worker_thread"), condition_(&lock_) {}
22 26
23 void Run() override { 27 void Run() override {
24 for (;;) { 28 for (;;) {
25 base::Closure task; 29 base::Closure task;
26 { 30 {
27 base::AutoLock hold(lock_); 31 base::AutoLock hold(lock_);
28 if (shutdown_) 32 if (shutdown_)
29 break; 33 break;
30 34
31 if (queue_.empty()) { 35 if (queue_.empty()) {
32 condition_.Wait(); 36 condition_.Wait();
33 continue; 37 continue;
34 } 38 }
35 39
36 task = queue_.front(); 40 task = std::move(queue_.front());
37 queue_.erase(queue_.begin()); 41 queue_.erase(queue_.begin());
38 } 42 }
39 task.Run(); 43 base::ResetAndReturn(&task).Run();
40 } 44 }
41 } 45 }
42 46
43 void Shutdown() { 47 void Shutdown() {
44 base::AutoLock hold(lock_); 48 base::AutoLock hold(lock_);
45 shutdown_ = true; 49 shutdown_ = true;
46 condition_.Signal(); 50 condition_.Signal();
47 } 51 }
48 52
49 void PostTask(const base::Closure& task) { 53 void PostTask(base::Closure task) {
50 base::AutoLock hold(lock_); 54 base::AutoLock hold(lock_);
51 queue_.push_back(task); 55 queue_.push_back(std::move(task));
52 condition_.Signal(); 56 condition_.Signal();
53 } 57 }
54 58
55 private: 59 private:
56 base::Lock lock_; 60 base::Lock lock_;
57 base::ConditionVariable condition_; 61 base::ConditionVariable condition_;
58 std::vector<base::Closure> queue_; 62 std::vector<base::Closure> queue_;
59 bool shutdown_ = false; 63 bool shutdown_ = false;
60 }; 64 };
61 65
62 class WorkerTaskRunner : public base::SequencedTaskRunner { 66 class WorkerTaskRunner : public base::SequencedTaskRunner {
63 public: 67 public:
64 WorkerTaskRunner() { thread_.Start(); } 68 WorkerTaskRunner() { thread_.Start(); }
65 69
66 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here, 70 bool PostNonNestableDelayedTask(const tracked_objects::Location& from_here,
67 const base::Closure& task, 71 base::Closure task,
68 base::TimeDelta delay) override { 72 base::TimeDelta delay) override {
69 return PostDelayedTask(from_here, task, delay); 73 return PostDelayedTask(from_here, std::move(task), delay);
70 } 74 }
71 75
72 bool PostDelayedTask(const tracked_objects::Location& from_here, 76 bool PostDelayedTask(const tracked_objects::Location& from_here,
73 const base::Closure& task, 77 base::Closure task,
74 base::TimeDelta delay) override { 78 base::TimeDelta delay) override {
75 thread_.PostTask(task); 79 thread_.PostTask(std::move(task));
76 return true; 80 return true;
77 } 81 }
78 82
79 bool RunsTasksOnCurrentThread() const override { return false; } 83 bool RunsTasksOnCurrentThread() const override { return false; }
80 84
81 protected: 85 protected:
82 ~WorkerTaskRunner() override { 86 ~WorkerTaskRunner() override {
83 thread_.Shutdown(); 87 thread_.Shutdown();
84 thread_.Join(); 88 thread_.Join();
85 } 89 }
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
130 134
131 private: 135 private:
132 int number_of_refs_ = 0; 136 int number_of_refs_ = 0;
133 scoped_refptr<TileTask> task_to_use_; 137 scoped_refptr<TileTask> task_to_use_;
134 }; 138 };
135 139
136 // A simple class that can receive decode callbacks. 140 // A simple class that can receive decode callbacks.
137 class DecodeClient { 141 class DecodeClient {
138 public: 142 public:
139 DecodeClient() {} 143 DecodeClient() {}
140 void Callback(const base::Closure& quit_closure, 144 void Callback(base::Closure quit_closure,
141 ImageController::ImageDecodeRequestId id, 145 ImageController::ImageDecodeRequestId id,
142 ImageController::ImageDecodeResult result) { 146 ImageController::ImageDecodeResult result) {
143 id_ = id; 147 id_ = id;
144 result_ = result; 148 result_ = result;
145 quit_closure.Run(); 149 base::ResetAndReturn(&quit_closure).Run();
146 } 150 }
147 151
148 ImageController::ImageDecodeRequestId id() { return id_; } 152 ImageController::ImageDecodeRequestId id() { return id_; }
149 ImageController::ImageDecodeResult result() { return result_; } 153 ImageController::ImageDecodeResult result() { return result_; }
150 154
151 private: 155 private:
152 ImageController::ImageDecodeRequestId id_ = 0; 156 ImageController::ImageDecodeRequestId id_ = 0;
153 ImageController::ImageDecodeResult result_ = 157 ImageController::ImageDecodeResult result_ =
154 ImageController::ImageDecodeResult::FAILURE; 158 ImageController::ImageDecodeResult::FAILURE;
155 }; 159 };
(...skipping 426 matching lines...) Expand 10 before | Expand all | Expand 10 after
582 decode_client2.result()); 586 decode_client2.result());
583 587
584 // Reset the controller since the order of destruction is wrong in this test 588 // 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 589 // (|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.) 590 // SetImageDecodeCache(nullptr) or it can be done in the dtor of the cache.)
587 ResetController(); 591 ResetController();
588 } 592 }
589 593
590 } // namespace 594 } // namespace
591 } // namespace cc 595 } // namespace cc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698