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 categories. Tasks from a single graph may | |
21 // be put into different categories, 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 categories and handle them appropriately. | |
19 class CC_EXPORT TaskGraphWorkQueue { | 24 class CC_EXPORT TaskGraphWorkQueue { |
20 public: | 25 public: |
21 struct TaskNamespace; | 26 struct TaskNamespace; |
22 | 27 |
23 struct PrioritizedTask { | 28 struct PrioritizedTask { |
24 typedef std::vector<PrioritizedTask> Vector; | 29 typedef std::vector<PrioritizedTask> Vector; |
25 | 30 |
26 PrioritizedTask(Task* task, TaskNamespace* task_namespace, size_t priority) | 31 PrioritizedTask(Task* task, |
27 : task(task), task_namespace(task_namespace), priority(priority) {} | 32 TaskNamespace* task_namespace, |
33 uint16_t category, | |
34 uint16_t priority) | |
35 : task(task), | |
36 task_namespace(task_namespace), | |
37 category(category), | |
38 priority(priority) {} | |
28 | 39 |
29 Task* task; | 40 Task* task; |
30 TaskNamespace* task_namespace; | 41 TaskNamespace* task_namespace; |
31 size_t priority; | 42 uint16_t category; |
43 uint16_t priority; | |
32 }; | 44 }; |
33 | 45 |
34 // Helper classes and static methods used by dependent classes. | 46 // Helper classes and static methods used by dependent classes. |
35 struct TaskNamespace { | 47 struct TaskNamespace { |
36 typedef std::vector<TaskNamespace*> Vector; | 48 typedef std::vector<TaskNamespace*> Vector; |
37 | 49 |
38 TaskNamespace(); | 50 TaskNamespace(); |
39 ~TaskNamespace(); | 51 ~TaskNamespace(); |
40 | 52 |
41 // Current task graph. | 53 // Current task graph. |
42 TaskGraph graph; | 54 TaskGraph graph; |
43 | 55 |
44 // Ordered set of tasks that are ready to run. | 56 // Map from category to a vector of tasks that are ready to run for that |
45 PrioritizedTask::Vector ready_to_run_tasks; | 57 // category. |
58 std::map<uint16_t, PrioritizedTask::Vector> ready_to_run_tasks; | |
46 | 59 |
47 // Completed tasks not yet collected by origin thread. | 60 // Completed tasks not yet collected by origin thread. |
48 Task::Vector completed_tasks; | 61 Task::Vector completed_tasks; |
49 | 62 |
50 // This set contains all currently running tasks. | 63 // This set contains all currently running tasks. |
51 Task::Vector running_tasks; | 64 Task::Vector running_tasks; |
52 }; | 65 }; |
53 | 66 |
54 TaskGraphWorkQueue(); | 67 TaskGraphWorkQueue(); |
55 virtual ~TaskGraphWorkQueue(); | 68 virtual ~TaskGraphWorkQueue(); |
56 | 69 |
57 // Gets a NamespaceToken which is guaranteed to be unique within this | 70 // Gets a NamespaceToken which is guaranteed to be unique within this |
58 // TaskGraphWorkQueue. | 71 // TaskGraphWorkQueue. |
59 NamespaceToken GetNamespaceToken(); | 72 NamespaceToken GetNamespaceToken(); |
60 | 73 |
61 // Updates a TaskNamespace with a new TaskGraph to run. This cancels any | 74 // Updates a TaskNamespace with a new TaskGraph to run. This cancels any |
62 // previous tasks in the graph being replaced. | 75 // previous tasks in the graph being replaced. |
63 void ScheduleTasks(NamespaceToken token, TaskGraph* graph); | 76 void ScheduleTasks(NamespaceToken token, TaskGraph* graph); |
64 | 77 |
65 // Returns the next task to run paired with its namespace. | 78 // Returns the next task to run for the given category. |
66 PrioritizedTask GetNextTaskToRun(); | 79 PrioritizedTask GetNextTaskToRun(uint16_t category); |
67 | 80 |
68 // Marks a task as completed, adding it to its namespace's list of completed | 81 // 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|. | 82 // tasks and updating the list of |ready_to_run_namespaces|. |
70 void CompleteTask(const PrioritizedTask& completed_task); | 83 void CompleteTask(const PrioritizedTask& completed_task); |
71 | 84 |
72 // Helper which populates a vector of completed tasks from the provided | 85 // Helper which populates a vector of completed tasks from the provided |
73 // namespace. | 86 // namespace. |
74 void CollectCompletedTasks(NamespaceToken token, | 87 void CollectCompletedTasks(NamespaceToken token, |
75 Task::Vector* completed_tasks); | 88 Task::Vector* completed_tasks); |
76 | 89 |
77 // Helper which returns the raw TaskNamespace* for the given token. Used to | 90 // 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 | 91 // allow callers to re-use a TaskNamespace*, reducing the number of lookups |
79 // needed. | 92 // needed. |
80 TaskNamespace* GetNamespaceForToken(NamespaceToken token) { | 93 TaskNamespace* GetNamespaceForToken(NamespaceToken token) { |
81 auto it = namespaces_.find(token); | 94 auto it = namespaces_.find(token); |
82 if (it == namespaces_.end()) | 95 if (it == namespaces_.end()) |
83 return nullptr; | 96 return nullptr; |
84 return &it->second; | 97 return &it->second; |
85 } | 98 } |
86 | 99 |
87 static bool HasFinishedRunningTasksInNamespace( | 100 static bool HasFinishedRunningTasksInNamespace( |
88 const TaskNamespace* task_namespace) { | 101 const TaskNamespace* task_namespace) { |
89 return task_namespace->running_tasks.empty() && | 102 return task_namespace->running_tasks.empty() && |
90 task_namespace->ready_to_run_tasks.empty(); | 103 task_namespace->ready_to_run_tasks.empty(); |
91 } | 104 } |
92 | 105 |
93 bool HasReadyToRunTasks() const { return !ready_to_run_namespaces_.empty(); } | 106 bool HasReadyToRunTasks() const { return !ready_to_run_namespaces_.empty(); } |
94 | 107 |
108 bool HasReadyToRunTasksForCategory(uint16_t category) const { | |
109 return ready_to_run_namespaces_.count(category) > 0; | |
reveman
2015/12/04 20:55:51
nit: ready_to_run_namespaces_.find(category)?
ericrk
2015/12/09 22:56:44
now that a category can be empty (but still exist)
| |
110 } | |
111 | |
95 bool HasAnyNamespaces() const { return !namespaces_.empty(); } | 112 bool HasAnyNamespaces() const { return !namespaces_.empty(); } |
96 | 113 |
97 bool HasFinishedRunningTasksInAllNamespaces() { | 114 bool HasFinishedRunningTasksInAllNamespaces() { |
98 return std::find_if( | 115 return std::find_if( |
99 namespaces_.begin(), namespaces_.end(), | 116 namespaces_.begin(), namespaces_.end(), |
100 [](const TaskNamespaceMap::value_type& entry) { | 117 [](const TaskNamespaceMap::value_type& entry) { |
101 return !HasFinishedRunningTasksInNamespace(&entry.second); | 118 return !HasFinishedRunningTasksInNamespace(&entry.second); |
102 }) == namespaces_.end(); | 119 }) == namespaces_.end(); |
103 } | 120 } |
104 | 121 |
105 // Helper function which ensures that graph dependencies were correctly | 122 // Helper function which ensures that graph dependencies were correctly |
106 // configured. | 123 // configured. |
107 static bool DependencyMismatch(const TaskGraph* graph); | 124 static bool DependencyMismatch(const TaskGraph* graph); |
108 | 125 |
126 // Certain TaskGraphRunners may wish to ignore categories - in these cases, we | |
127 // provide a utility function which strips categories from a TaskGraph, | |
128 // assigning all tasks to group 0. | |
129 static void UncategorizeTaskGraph(TaskGraph* graph); | |
reveman
2015/12/04 20:55:51
I don't think we should modify the task graph in a
ericrk
2015/12/09 22:56:43
Re-worked things per our discussion on fri.
| |
130 | |
109 private: | 131 private: |
110 // Helper class used to provide NamespaceToken comparison to TaskNamespaceMap. | 132 // Helper class used to provide NamespaceToken comparison to TaskNamespaceMap. |
111 class CompareToken { | 133 class CompareToken { |
112 public: | 134 public: |
113 bool operator()(const NamespaceToken& lhs, | 135 bool operator()(const NamespaceToken& lhs, |
114 const NamespaceToken& rhs) const { | 136 const NamespaceToken& rhs) const { |
115 return lhs.id_ < rhs.id_; | 137 return lhs.id_ < rhs.id_; |
116 } | 138 } |
117 }; | 139 }; |
118 | 140 |
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 = | 141 using TaskNamespaceMap = |
138 std::map<NamespaceToken, TaskNamespace, CompareToken>; | 142 std::map<NamespaceToken, TaskNamespace, CompareToken>; |
139 | 143 |
140 TaskNamespaceMap namespaces_; | 144 TaskNamespaceMap namespaces_; |
141 TaskNamespace::Vector ready_to_run_namespaces_; | 145 |
146 // Map from category to a vector of ready to run namespaces for that category. | |
147 std::map<uint16_t, TaskNamespace::Vector> ready_to_run_namespaces_; | |
142 | 148 |
143 // Provides a unique id to each NamespaceToken. | 149 // Provides a unique id to each NamespaceToken. |
144 int next_namespace_id_; | 150 int next_namespace_id_; |
145 }; | 151 }; |
146 | 152 |
147 } // namespace cc | 153 } // namespace cc |
148 | 154 |
149 #endif // CC_RASTER_TASK_GRAPH_WORK_QUEUE_H_ | 155 #endif // CC_RASTER_TASK_GRAPH_WORK_QUEUE_H_ |
OLD | NEW |