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 <algorithm> | |
9 #include <map> | 8 #include <map> |
10 #include <vector> | 9 #include <vector> |
11 | 10 |
12 #include "cc/base/cc_export.h" | 11 #include "cc/base/cc_export.h" |
13 #include "cc/raster/task_graph_runner.h" | 12 #include "cc/raster/task_graph_runner.h" |
14 | 13 |
15 namespace cc { | 14 namespace cc { |
16 | 15 |
17 // Implements a queue of incoming TaskGraph work. Designed for use by | 16 // Implements a queue of incoming TaskGraph work. Designed for use by |
18 // implementations of TaskGraphRunner. Not thread safe, so the caller is | 17 // implementations of TaskGraphRunner. Not thread safe, so the caller is |
19 // responsible for all necessary locking. | 18 // responsible for all necessary locking. |
20 // | |
21 // Tasks in the queue are divided into categories. Tasks from a single graph may | |
22 // be put into different categories, each of which is prioritized independently | |
23 // from the others. It is up to the implementation of TaskGraphRunner to | |
24 // define the meaning of the categories and handle them appropriately. | |
25 class CC_EXPORT TaskGraphWorkQueue { | 19 class CC_EXPORT TaskGraphWorkQueue { |
26 public: | 20 public: |
27 struct TaskNamespace; | 21 struct TaskNamespace; |
28 | 22 |
29 struct PrioritizedTask { | 23 struct PrioritizedTask { |
30 typedef std::vector<PrioritizedTask> Vector; | 24 typedef std::vector<PrioritizedTask> Vector; |
31 | 25 |
32 PrioritizedTask(Task* task, | 26 PrioritizedTask(Task* task, TaskNamespace* task_namespace, size_t priority) |
33 TaskNamespace* task_namespace, | 27 : task(task), task_namespace(task_namespace), priority(priority) {} |
34 uint16_t category, | |
35 uint16_t priority) | |
36 : task(task), | |
37 task_namespace(task_namespace), | |
38 category(category), | |
39 priority(priority) {} | |
40 | 28 |
41 Task* task; | 29 Task* task; |
42 TaskNamespace* task_namespace; | 30 TaskNamespace* task_namespace; |
43 uint16_t category; | 31 size_t priority; |
44 uint16_t priority; | |
45 }; | 32 }; |
46 | 33 |
47 // Helper classes and static methods used by dependent classes. | 34 // Helper classes and static methods used by dependent classes. |
48 struct TaskNamespace { | 35 struct TaskNamespace { |
49 typedef std::vector<TaskNamespace*> Vector; | 36 typedef std::vector<TaskNamespace*> Vector; |
50 | 37 |
51 TaskNamespace(); | 38 TaskNamespace(); |
52 ~TaskNamespace(); | 39 ~TaskNamespace(); |
53 | 40 |
54 // Current task graph. | 41 // Current task graph. |
55 TaskGraph graph; | 42 TaskGraph graph; |
56 | 43 |
57 // Map from category to a vector of tasks that are ready to run for that | 44 // Ordered set of tasks that are ready to run. |
58 // category. | 45 PrioritizedTask::Vector ready_to_run_tasks; |
59 std::map<uint16_t, PrioritizedTask::Vector> ready_to_run_tasks; | |
60 | 46 |
61 // Completed tasks not yet collected by origin thread. | 47 // Completed tasks not yet collected by origin thread. |
62 Task::Vector completed_tasks; | 48 Task::Vector completed_tasks; |
63 | 49 |
64 // This set contains all currently running tasks. | 50 // This set contains all currently running tasks. |
65 Task::Vector running_tasks; | 51 Task::Vector running_tasks; |
66 }; | 52 }; |
67 | 53 |
68 TaskGraphWorkQueue(); | 54 TaskGraphWorkQueue(); |
69 virtual ~TaskGraphWorkQueue(); | 55 virtual ~TaskGraphWorkQueue(); |
70 | 56 |
71 // Gets a NamespaceToken which is guaranteed to be unique within this | 57 // Gets a NamespaceToken which is guaranteed to be unique within this |
72 // TaskGraphWorkQueue. | 58 // TaskGraphWorkQueue. |
73 NamespaceToken GetNamespaceToken(); | 59 NamespaceToken GetNamespaceToken(); |
74 | 60 |
75 // Updates a TaskNamespace with a new TaskGraph to run. This cancels any | 61 // Updates a TaskNamespace with a new TaskGraph to run. This cancels any |
76 // previous tasks in the graph being replaced. | 62 // previous tasks in the graph being replaced. |
77 void ScheduleTasks(NamespaceToken token, TaskGraph* graph); | 63 void ScheduleTasks(NamespaceToken token, TaskGraph* graph); |
78 | 64 |
79 // Returns the next task to run for the given category. | 65 // Returns the next task to run paired with its namespace. |
80 PrioritizedTask GetNextTaskToRun(uint16_t category); | 66 PrioritizedTask GetNextTaskToRun(); |
81 | 67 |
82 // Marks a task as completed, adding it to its namespace's list of completed | 68 // Marks a task as completed, adding it to its namespace's list of completed |
83 // tasks and updating the list of |ready_to_run_namespaces|. | 69 // tasks and updating the list of |ready_to_run_namespaces|. |
84 void CompleteTask(const PrioritizedTask& completed_task); | 70 void CompleteTask(const PrioritizedTask& completed_task); |
85 | 71 |
86 // Helper which populates a vector of completed tasks from the provided | 72 // Helper which populates a vector of completed tasks from the provided |
87 // namespace. | 73 // namespace. |
88 void CollectCompletedTasks(NamespaceToken token, | 74 void CollectCompletedTasks(NamespaceToken token, |
89 Task::Vector* completed_tasks); | 75 Task::Vector* completed_tasks); |
90 | 76 |
91 // Helper which returns the raw TaskNamespace* for the given token. Used to | 77 // Helper which returns the raw TaskNamespace* for the given token. Used to |
92 // allow callers to re-use a TaskNamespace*, reducing the number of lookups | 78 // allow callers to re-use a TaskNamespace*, reducing the number of lookups |
93 // needed. | 79 // needed. |
94 TaskNamespace* GetNamespaceForToken(NamespaceToken token) { | 80 TaskNamespace* GetNamespaceForToken(NamespaceToken token) { |
95 auto it = namespaces_.find(token); | 81 auto it = namespaces_.find(token); |
96 if (it == namespaces_.end()) | 82 if (it == namespaces_.end()) |
97 return nullptr; | 83 return nullptr; |
98 return &it->second; | 84 return &it->second; |
99 } | 85 } |
100 | 86 |
101 static bool HasReadyToRunTasksInNamespace( | |
102 const TaskNamespace* task_namespace) { | |
103 return std::find_if(task_namespace->ready_to_run_tasks.begin(), | |
104 task_namespace->ready_to_run_tasks.end(), | |
105 [](const std::pair<uint16_t, PrioritizedTask::Vector>& | |
106 ready_to_run_tasks) { | |
107 return !ready_to_run_tasks.second.empty(); | |
108 }) != task_namespace->ready_to_run_tasks.end(); | |
109 } | |
110 | |
111 static bool HasFinishedRunningTasksInNamespace( | 87 static bool HasFinishedRunningTasksInNamespace( |
112 const TaskNamespace* task_namespace) { | 88 const TaskNamespace* task_namespace) { |
113 return task_namespace->running_tasks.empty() && | 89 return task_namespace->running_tasks.empty() && |
114 !HasReadyToRunTasksInNamespace(task_namespace); | 90 task_namespace->ready_to_run_tasks.empty(); |
115 } | 91 } |
116 | 92 |
117 bool HasReadyToRunTasks() const { | 93 bool HasReadyToRunTasks() const { return !ready_to_run_namespaces_.empty(); } |
118 return std::find_if(ready_to_run_namespaces_.begin(), | |
119 ready_to_run_namespaces_.end(), | |
120 [](const std::pair<uint16_t, TaskNamespace::Vector>& | |
121 ready_to_run_namespaces) { | |
122 return !ready_to_run_namespaces.second.empty(); | |
123 }) != ready_to_run_namespaces_.end(); | |
124 } | |
125 | |
126 bool HasReadyToRunTasksForCategory(uint16_t category) const { | |
127 auto found = ready_to_run_namespaces_.find(category); | |
128 return found != ready_to_run_namespaces_.end() && !found->second.empty(); | |
129 } | |
130 | 94 |
131 bool HasAnyNamespaces() const { return !namespaces_.empty(); } | 95 bool HasAnyNamespaces() const { return !namespaces_.empty(); } |
132 | 96 |
133 bool HasFinishedRunningTasksInAllNamespaces() { | 97 bool HasFinishedRunningTasksInAllNamespaces() { |
134 return std::find_if( | 98 return std::find_if( |
135 namespaces_.begin(), namespaces_.end(), | 99 namespaces_.begin(), namespaces_.end(), |
136 [](const TaskNamespaceMap::value_type& entry) { | 100 [](const TaskNamespaceMap::value_type& entry) { |
137 return !HasFinishedRunningTasksInNamespace(&entry.second); | 101 return !HasFinishedRunningTasksInNamespace(&entry.second); |
138 }) == namespaces_.end(); | 102 }) == namespaces_.end(); |
139 } | 103 } |
140 | 104 |
141 const std::map<uint16_t, TaskNamespace::Vector>& ready_to_run_namespaces() | |
142 const { | |
143 return ready_to_run_namespaces_; | |
144 } | |
145 | |
146 // Helper function which ensures that graph dependencies were correctly | 105 // Helper function which ensures that graph dependencies were correctly |
147 // configured. | 106 // configured. |
148 static bool DependencyMismatch(const TaskGraph* graph); | 107 static bool DependencyMismatch(const TaskGraph* graph); |
149 | 108 |
150 private: | 109 private: |
151 // Helper class used to provide NamespaceToken comparison to TaskNamespaceMap. | 110 // Helper class used to provide NamespaceToken comparison to TaskNamespaceMap. |
152 class CompareToken { | 111 class CompareToken { |
153 public: | 112 public: |
154 bool operator()(const NamespaceToken& lhs, | 113 bool operator()(const NamespaceToken& lhs, |
155 const NamespaceToken& rhs) const { | 114 const NamespaceToken& rhs) const { |
156 return lhs.id_ < rhs.id_; | 115 return lhs.id_ < rhs.id_; |
157 } | 116 } |
158 }; | 117 }; |
159 | 118 |
| 119 static bool CompareTaskPriority(const PrioritizedTask& a, |
| 120 const PrioritizedTask& b) { |
| 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 |
160 using TaskNamespaceMap = | 137 using TaskNamespaceMap = |
161 std::map<NamespaceToken, TaskNamespace, CompareToken>; | 138 std::map<NamespaceToken, TaskNamespace, CompareToken>; |
162 | 139 |
163 TaskNamespaceMap namespaces_; | 140 TaskNamespaceMap namespaces_; |
164 | 141 TaskNamespace::Vector ready_to_run_namespaces_; |
165 // Map from category to a vector of ready to run namespaces for that category. | |
166 std::map<uint16_t, TaskNamespace::Vector> ready_to_run_namespaces_; | |
167 | 142 |
168 // Provides a unique id to each NamespaceToken. | 143 // Provides a unique id to each NamespaceToken. |
169 int next_namespace_id_; | 144 int next_namespace_id_; |
170 }; | 145 }; |
171 | 146 |
172 } // namespace cc | 147 } // namespace cc |
173 | 148 |
174 #endif // CC_RASTER_TASK_GRAPH_WORK_QUEUE_H_ | 149 #endif // CC_RASTER_TASK_GRAPH_WORK_QUEUE_H_ |
OLD | NEW |