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_WORK_QUEUE_H_ | 5 #ifndef CC_RASTER_TASK_GRAPH_WORK_QUEUE_H_ |
6 #define CC_RASTER_TASK_GRAPH_WORK_QUEUE_H_ | 6 #define CC_RASTER_TASK_GRAPH_WORK_QUEUE_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "cc/base/cc_export.h" | 11 #include "cc/base/cc_export.h" |
12 #include "cc/raster/task_graph_runner.h" | 12 #include "cc/raster/task_graph_runner.h" |
13 | 13 |
14 namespace cc { | 14 namespace cc { |
15 | 15 |
16 // Implements a queue of incoming TaskGraph work. Designed for use by | 16 // Implements a queue of incoming TaskGraph work. Designed for use by |
17 // implementations of TaskGraphRunner. Not thread safe, so the caller is | 17 // implementations of TaskGraphRunner. Not thread safe, so the caller is |
18 // responsible for all necessary locking. | 18 // responsible for all necessary locking. |
19 // | |
20 // Tasks in the queue are divided into groups. Tasks from a single graph may | |
21 // be put into different groups, each of which is prioritized independently | |
22 // from the others. It is up to the implementation of TaskGraphRunner to | |
23 // define the meaning of the groups and handle them appropriately. | |
19 class CC_EXPORT TaskGraphWorkQueue { | 24 class CC_EXPORT TaskGraphWorkQueue { |
20 public: | 25 public: |
26 // This value is chosen as the max needed at the moment. | |
27 enum : uint16_t { kMaxTaskGroups = 3 }; | |
reveman
2015/12/04 02:30:54
hm, not a huge fan of this. if this is going to be
ericrk
2015/12/04 19:14:31
ok - heh... I have that code 2 or 3 CLs back in th
| |
28 | |
21 struct TaskNamespace; | 29 struct TaskNamespace; |
22 | 30 |
23 struct PrioritizedTask { | 31 struct PrioritizedTask { |
24 typedef std::vector<PrioritizedTask> Vector; | 32 typedef std::vector<PrioritizedTask> Vector; |
25 | 33 |
26 PrioritizedTask(Task* task, TaskNamespace* task_namespace, size_t priority) | 34 PrioritizedTask(Task* task, |
27 : task(task), task_namespace(task_namespace), priority(priority) {} | 35 TaskNamespace* task_namespace, |
36 uint16_t group, | |
37 uint16_t priority) | |
38 : task(task), | |
39 task_namespace(task_namespace), | |
40 group(group), | |
41 priority(priority) {} | |
28 | 42 |
29 Task* task; | 43 Task* task; |
30 TaskNamespace* task_namespace; | 44 TaskNamespace* task_namespace; |
31 size_t priority; | 45 uint16_t group; |
46 uint16_t priority; | |
32 }; | 47 }; |
33 | 48 |
34 // Helper classes and static methods used by dependent classes. | 49 // Helper classes and static methods used by dependent classes. |
35 struct TaskNamespace { | 50 struct TaskNamespace { |
36 typedef std::vector<TaskNamespace*> Vector; | 51 typedef std::vector<TaskNamespace*> Vector; |
37 | 52 |
38 TaskNamespace(); | 53 TaskNamespace(); |
39 ~TaskNamespace(); | 54 ~TaskNamespace(); |
40 | 55 |
41 // Current task graph. | 56 // Current task graph. |
42 TaskGraph graph; | 57 TaskGraph graph; |
43 | 58 |
44 // Ordered set of tasks that are ready to run. | 59 // One ordered set of tasks that are ready to run for each group. |
45 PrioritizedTask::Vector ready_to_run_tasks; | 60 PrioritizedTask::Vector ready_to_run_tasks[kMaxTaskGroups]; |
46 | 61 |
47 // Completed tasks not yet collected by origin thread. | 62 // Completed tasks not yet collected by origin thread. |
48 Task::Vector completed_tasks; | 63 Task::Vector completed_tasks; |
49 | 64 |
50 // This set contains all currently running tasks. | 65 // This set contains all currently running tasks. |
51 Task::Vector running_tasks; | 66 Task::Vector running_tasks; |
52 }; | 67 }; |
53 | 68 |
54 TaskGraphWorkQueue(); | 69 TaskGraphWorkQueue(); |
55 virtual ~TaskGraphWorkQueue(); | 70 virtual ~TaskGraphWorkQueue(); |
56 | 71 |
57 // Gets a NamespaceToken which is guaranteed to be unique within this | 72 // Gets a NamespaceToken which is guaranteed to be unique within this |
58 // TaskGraphWorkQueue. | 73 // TaskGraphWorkQueue. |
59 NamespaceToken GetNamespaceToken(); | 74 NamespaceToken GetNamespaceToken(); |
60 | 75 |
61 // Updates a TaskNamespace with a new TaskGraph to run. This cancels any | 76 // Updates a TaskNamespace with a new TaskGraph to run. This cancels any |
62 // previous tasks in the graph being replaced. | 77 // previous tasks in the graph being replaced. |
63 void ScheduleTasks(NamespaceToken token, TaskGraph* graph); | 78 void ScheduleTasks(NamespaceToken token, TaskGraph* graph); |
64 | 79 |
65 // Returns the next task to run paired with its namespace. | 80 // Returns the next task to run. Treats group as a secondary priority (group |
81 // 0 runs before group 1). | |
66 PrioritizedTask GetNextTaskToRun(); | 82 PrioritizedTask GetNextTaskToRun(); |
reveman
2015/12/04 02:30:54
Is this the only code in TaskGraphWorkQueue that t
ericrk
2015/12/04 19:14:31
sgtm
| |
67 | 83 |
84 // Returns the next task to run for the given group. | |
85 PrioritizedTask GetNextTaskToRunForGroup(uint16_t group); | |
86 | |
68 // Marks a task as completed, adding it to its namespace's list of completed | 87 // Marks a task as completed, adding it to its namespace's list of completed |
69 // tasks and updating the list of |ready_to_run_namespaces|. | 88 // tasks and updating the list of |ready_to_run_namespaces|. |
70 void CompleteTask(const PrioritizedTask& completed_task); | 89 void CompleteTask(const PrioritizedTask& completed_task); |
71 | 90 |
72 // Helper which populates a vector of completed tasks from the provided | 91 // Helper which populates a vector of completed tasks from the provided |
73 // namespace. | 92 // namespace. |
74 void CollectCompletedTasks(NamespaceToken token, | 93 void CollectCompletedTasks(NamespaceToken token, |
75 Task::Vector* completed_tasks); | 94 Task::Vector* completed_tasks); |
76 | 95 |
77 // Helper which returns the raw TaskNamespace* for the given token. Used to | 96 // Helper which returns the raw TaskNamespace* for the given token. Used to |
78 // allow callers to re-use a TaskNamespace*, reducing the number of lookups | 97 // allow callers to re-use a TaskNamespace*, reducing the number of lookups |
79 // needed. | 98 // needed. |
80 TaskNamespace* GetNamespaceForToken(NamespaceToken token) { | 99 TaskNamespace* GetNamespaceForToken(NamespaceToken token) { |
81 auto it = namespaces_.find(token); | 100 auto it = namespaces_.find(token); |
82 if (it == namespaces_.end()) | 101 if (it == namespaces_.end()) |
83 return nullptr; | 102 return nullptr; |
84 return &it->second; | 103 return &it->second; |
85 } | 104 } |
86 | 105 |
87 static bool HasFinishedRunningTasksInNamespace( | 106 static bool HasFinishedRunningTasksInNamespace( |
88 const TaskNamespace* task_namespace) { | 107 const TaskNamespace* task_namespace); |
89 return task_namespace->running_tasks.empty() && | 108 |
90 task_namespace->ready_to_run_tasks.empty(); | 109 bool HasReadyToRunTasks() const; |
110 | |
111 bool HasReadyToRunTasksForGroup(uint16_t group) const { | |
112 DCHECK(group < kMaxTaskGroups); | |
113 return ready_to_run_namespaces_[group].empty(); | |
91 } | 114 } |
92 | 115 |
93 bool HasReadyToRunTasks() const { return !ready_to_run_namespaces_.empty(); } | |
94 | |
95 bool HasAnyNamespaces() const { return !namespaces_.empty(); } | 116 bool HasAnyNamespaces() const { return !namespaces_.empty(); } |
96 | 117 |
97 bool HasFinishedRunningTasksInAllNamespaces() { | 118 bool HasFinishedRunningTasksInAllNamespaces() { |
98 return std::find_if( | 119 return std::find_if( |
99 namespaces_.begin(), namespaces_.end(), | 120 namespaces_.begin(), namespaces_.end(), |
100 [](const TaskNamespaceMap::value_type& entry) { | 121 [](const TaskNamespaceMap::value_type& entry) { |
101 return !HasFinishedRunningTasksInNamespace(&entry.second); | 122 return !HasFinishedRunningTasksInNamespace(&entry.second); |
102 }) == namespaces_.end(); | 123 }) == namespaces_.end(); |
103 } | 124 } |
104 | 125 |
105 // Helper function which ensures that graph dependencies were correctly | 126 // Helper function which ensures that graph dependencies were correctly |
106 // configured. | 127 // configured. |
107 static bool DependencyMismatch(const TaskGraph* graph); | 128 static bool DependencyMismatch(const TaskGraph* graph); |
108 | 129 |
109 private: | 130 private: |
110 // Helper class used to provide NamespaceToken comparison to TaskNamespaceMap. | 131 // Helper class used to provide NamespaceToken comparison to TaskNamespaceMap. |
111 class CompareToken { | 132 class CompareToken { |
112 public: | 133 public: |
113 bool operator()(const NamespaceToken& lhs, | 134 bool operator()(const NamespaceToken& lhs, |
114 const NamespaceToken& rhs) const { | 135 const NamespaceToken& rhs) const { |
115 return lhs.id_ < rhs.id_; | 136 return lhs.id_ < rhs.id_; |
116 } | 137 } |
117 }; | 138 }; |
118 | 139 |
119 static bool CompareTaskPriority(const PrioritizedTask& a, | 140 // Validates that no group in a graph exceeds |kMaxTaskGroups|. |
120 const PrioritizedTask& b) { | 141 static bool ValidateGroups(const TaskGraph* graph); |
121 // In this system, numerically lower priority is run first. | |
122 return a.priority > b.priority; | |
123 } | |
124 | |
125 static bool CompareTaskNamespacePriority(const TaskNamespace* a, | |
126 const TaskNamespace* b) { | |
127 DCHECK(!a->ready_to_run_tasks.empty()); | |
128 DCHECK(!b->ready_to_run_tasks.empty()); | |
129 | |
130 // Compare based on task priority of the ready_to_run_tasks heap .front() | |
131 // will hold the max element of the heap, except after pop_heap, when max | |
132 // element is moved to .back(). | |
133 return CompareTaskPriority(a->ready_to_run_tasks.front(), | |
134 b->ready_to_run_tasks.front()); | |
135 } | |
136 | 142 |
137 using TaskNamespaceMap = | 143 using TaskNamespaceMap = |
138 std::map<NamespaceToken, TaskNamespace, CompareToken>; | 144 std::map<NamespaceToken, TaskNamespace, CompareToken>; |
139 | 145 |
140 TaskNamespaceMap namespaces_; | 146 TaskNamespaceMap namespaces_; |
141 TaskNamespace::Vector ready_to_run_namespaces_; | 147 |
148 // A vector of ready to run namespaces for each group. | |
149 TaskNamespace::Vector ready_to_run_namespaces_[kMaxTaskGroups]; | |
142 | 150 |
143 // Provides a unique id to each NamespaceToken. | 151 // Provides a unique id to each NamespaceToken. |
144 int next_namespace_id_; | 152 int next_namespace_id_; |
145 }; | 153 }; |
146 | 154 |
147 } // namespace cc | 155 } // namespace cc |
148 | 156 |
149 #endif // CC_RASTER_TASK_GRAPH_WORK_QUEUE_H_ | 157 #endif // CC_RASTER_TASK_GRAPH_WORK_QUEUE_H_ |
OLD | NEW |