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