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 #ifndef CC_RASTER_TASK_GRAPH_RUNNER_H_ | 5 #ifndef CC_RASTER_TASK_GRAPH_RUNNER_H_ |
6 #define CC_RASTER_TASK_GRAPH_RUNNER_H_ | 6 #define CC_RASTER_TASK_GRAPH_RUNNER_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 | 10 |
11 #include <algorithm> | 11 #include <algorithm> |
12 #include <map> | 12 #include <map> |
13 #include <memory> | 13 #include <memory> |
14 #include <vector> | 14 #include <vector> |
15 | 15 |
16 #include "base/logging.h" | 16 #include "base/logging.h" |
17 #include "base/memory/ref_counted.h" | 17 #include "base/memory/ref_counted.h" |
18 #include "cc/base/cc_export.h" | 18 #include "cc/base/cc_export.h" |
| 19 #include "cc/raster/task.h" |
19 | 20 |
20 namespace cc { | 21 namespace cc { |
21 | 22 |
22 class TaskGraphRunner; | |
23 | |
24 // A task which can be run by a TaskGraphRunner. To run a Task, it should be | |
25 // inserted into a TaskGraph, which can then be scheduled on the | |
26 // TaskGraphRunner. | |
27 class CC_EXPORT Task : public base::RefCountedThreadSafe<Task> { | |
28 public: | |
29 typedef std::vector<scoped_refptr<Task>> Vector; | |
30 | |
31 // Subclasses should implement this method. RunOnWorkerThread may be called | |
32 // on any thread, and subclasses are responsible for locking and thread | |
33 // safety. | |
34 virtual void RunOnWorkerThread() = 0; | |
35 | |
36 void WillRun(); | |
37 void DidRun(); | |
38 bool HasFinishedRunning() const; | |
39 | |
40 protected: | |
41 friend class base::RefCountedThreadSafe<Task>; | |
42 | |
43 Task(); | |
44 virtual ~Task(); | |
45 | |
46 bool will_run_; | |
47 bool did_run_; | |
48 }; | |
49 | |
50 // A task dependency graph describes the order in which to execute a set | |
51 // of tasks. Dependencies are represented as edges. Each node is assigned | |
52 // a category, a priority and a run count that matches the number of | |
53 // dependencies. Priority range from 0 (most favorable scheduling) to UINT16_MAX | |
54 // (least favorable). Categories range from 0 to UINT16_MAX. It is up to the | |
55 // implementation and its consumer to determine the meaning (if any) of a | |
56 // category. A TaskGraphRunner implementation may chose to prioritize certain | |
57 // categories over others, regardless of the individual priorities of tasks. | |
58 struct CC_EXPORT TaskGraph { | |
59 struct Node { | |
60 typedef std::vector<Node> Vector; | |
61 | |
62 Node(Task* task, | |
63 uint16_t category, | |
64 uint16_t priority, | |
65 uint32_t dependencies) | |
66 : task(task), | |
67 category(category), | |
68 priority(priority), | |
69 dependencies(dependencies) {} | |
70 | |
71 Task* task; | |
72 uint16_t category; | |
73 uint16_t priority; | |
74 uint32_t dependencies; | |
75 }; | |
76 | |
77 struct Edge { | |
78 typedef std::vector<Edge> Vector; | |
79 | |
80 Edge(const Task* task, Task* dependent) | |
81 : task(task), dependent(dependent) {} | |
82 | |
83 const Task* task; | |
84 Task* dependent; | |
85 }; | |
86 | |
87 TaskGraph(); | |
88 TaskGraph(const TaskGraph& other); | |
89 ~TaskGraph(); | |
90 | |
91 void Swap(TaskGraph* other); | |
92 void Reset(); | |
93 | |
94 Node::Vector nodes; | |
95 Edge::Vector edges; | |
96 }; | |
97 | |
98 // Opaque identifier that defines a namespace of tasks. | 23 // Opaque identifier that defines a namespace of tasks. |
99 class CC_EXPORT NamespaceToken { | 24 class CC_EXPORT NamespaceToken { |
100 public: | 25 public: |
101 NamespaceToken() : id_(0) {} | 26 NamespaceToken() : id_(0) {} |
102 ~NamespaceToken() {} | 27 ~NamespaceToken() {} |
103 | 28 |
104 bool IsValid() const { return id_ != 0; } | 29 bool IsValid() const { return id_ != 0; } |
105 | 30 |
106 private: | 31 private: |
107 friend class TaskGraphWorkQueue; | 32 friend class TaskGraphWorkQueue; |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 virtual void WaitForTasksToFinishRunning(NamespaceToken token) = 0; | 84 virtual void WaitForTasksToFinishRunning(NamespaceToken token) = 0; |
160 | 85 |
161 // Collect all completed tasks in |completed_tasks|. | 86 // Collect all completed tasks in |completed_tasks|. |
162 virtual void CollectCompletedTasks(NamespaceToken token, | 87 virtual void CollectCompletedTasks(NamespaceToken token, |
163 Task::Vector* completed_tasks) = 0; | 88 Task::Vector* completed_tasks) = 0; |
164 | 89 |
165 protected: | 90 protected: |
166 virtual ~TaskGraphRunner() {} | 91 virtual ~TaskGraphRunner() {} |
167 }; | 92 }; |
168 | 93 |
169 // Helper class for iterating over all dependents of a task. | |
170 class DependentIterator { | |
171 public: | |
172 DependentIterator(TaskGraph* graph, const Task* task) | |
173 : graph_(graph), | |
174 task_(task), | |
175 current_index_(static_cast<size_t>(-1)), | |
176 current_node_(NULL) { | |
177 ++(*this); | |
178 } | |
179 | |
180 TaskGraph::Node& operator->() const { | |
181 DCHECK_LT(current_index_, graph_->edges.size()); | |
182 DCHECK_EQ(graph_->edges[current_index_].task, task_); | |
183 DCHECK(current_node_); | |
184 return *current_node_; | |
185 } | |
186 | |
187 TaskGraph::Node& operator*() const { | |
188 DCHECK_LT(current_index_, graph_->edges.size()); | |
189 DCHECK_EQ(graph_->edges[current_index_].task, task_); | |
190 DCHECK(current_node_); | |
191 return *current_node_; | |
192 } | |
193 | |
194 // Note: Performance can be improved by keeping edges sorted. | |
195 DependentIterator& operator++() { | |
196 // Find next dependency edge for |task_|. | |
197 do { | |
198 ++current_index_; | |
199 if (current_index_ == graph_->edges.size()) | |
200 return *this; | |
201 } while (graph_->edges[current_index_].task != task_); | |
202 | |
203 // Now find the node for the dependent of this edge. | |
204 TaskGraph::Node::Vector::iterator it = std::find_if( | |
205 graph_->nodes.begin(), graph_->nodes.end(), | |
206 [this](const TaskGraph::Node& node) { | |
207 return node.task == graph_->edges[current_index_].dependent; | |
208 }); | |
209 DCHECK(it != graph_->nodes.end()); | |
210 current_node_ = &(*it); | |
211 | |
212 return *this; | |
213 } | |
214 | |
215 operator bool() const { return current_index_ < graph_->edges.size(); } | |
216 | |
217 private: | |
218 TaskGraph* graph_; | |
219 const Task* task_; | |
220 size_t current_index_; | |
221 TaskGraph::Node* current_node_; | |
222 }; | |
223 | |
224 } // namespace cc | 94 } // namespace cc |
225 | 95 |
226 #endif // CC_RASTER_TASK_GRAPH_RUNNER_H_ | 96 #endif // CC_RASTER_TASK_GRAPH_RUNNER_H_ |
OLD | NEW |