OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef CC_RASTER_TASK_GRAPH_WORK_QUEUE_H_ |
| 6 #define CC_RASTER_TASK_GRAPH_WORK_QUEUE_H_ |
| 7 |
| 8 #include <map> |
| 9 #include <vector> |
| 10 |
| 11 #include "cc/base/cc_export.h" |
| 12 #include "cc/raster/task_graph_runner.h" |
| 13 |
| 14 namespace cc { |
| 15 |
| 16 // Implements a queue of incoming TaskGraph work. Designed for use by |
| 17 // implementations of TaskGraphRunner. Not thread safe, so the caller is |
| 18 // responsible for all necessary locking. |
| 19 class CC_EXPORT TaskGraphWorkQueue { |
| 20 public: |
| 21 struct TaskNamespace; |
| 22 |
| 23 struct PrioritizedTask { |
| 24 typedef std::vector<PrioritizedTask> Vector; |
| 25 |
| 26 PrioritizedTask(Task* task, TaskNamespace* task_namespace, size_t priority) |
| 27 : task(task), task_namespace(task_namespace), priority(priority) {} |
| 28 |
| 29 Task* task; |
| 30 TaskNamespace* task_namespace; |
| 31 size_t priority; |
| 32 }; |
| 33 |
| 34 // Helper classes and static methods used by dependent classes. |
| 35 struct TaskNamespace { |
| 36 typedef std::vector<TaskNamespace*> Vector; |
| 37 |
| 38 TaskNamespace(); |
| 39 ~TaskNamespace(); |
| 40 |
| 41 // Current task graph. |
| 42 TaskGraph graph; |
| 43 |
| 44 // Ordered set of tasks that are ready to run. |
| 45 PrioritizedTask::Vector ready_to_run_tasks; |
| 46 |
| 47 // Completed tasks not yet collected by origin thread. |
| 48 Task::Vector completed_tasks; |
| 49 |
| 50 // This set contains all currently running tasks. Don't use Task::Vector, |
| 51 // as we want raw pointers. |
| 52 std::vector<const Task*> running_tasks; |
| 53 }; |
| 54 |
| 55 TaskGraphWorkQueue(); |
| 56 virtual ~TaskGraphWorkQueue(); |
| 57 |
| 58 static bool HasFinishedRunningTasksInNamespace( |
| 59 const TaskNamespace* task_namespace) { |
| 60 return task_namespace->running_tasks.empty() && |
| 61 task_namespace->ready_to_run_tasks.empty(); |
| 62 } |
| 63 |
| 64 bool HasReadyToRunTasks() { return !ready_to_run_namespaces_.empty(); } |
| 65 |
| 66 bool HasFinishedRunningTasksInAllNamespaces() { |
| 67 return std::find_if( |
| 68 namespaces_.begin(), namespaces_.end(), |
| 69 [](const TaskNamespaceMap::value_type& entry) { |
| 70 return !HasFinishedRunningTasksInNamespace(&entry.second); |
| 71 }) == namespaces_.end(); |
| 72 } |
| 73 |
| 74 // Updates a TaskNamespace with a new TaskGraph to run. This cancels any |
| 75 // previous tasks in the graph being replaced. |
| 76 void ScheduleTasks(NamespaceToken token, TaskGraph* graph); |
| 77 |
| 78 // Returns the next task to run paired with its namespace. |
| 79 PrioritizedTask GetNextTaskToRun(); |
| 80 |
| 81 // Marks a task as completed, adding it to its namespace's list of completed |
| 82 // tasks and updating the list of |ready_to_run_namespaces|. |
| 83 void CompleteTask(const PrioritizedTask& completed_task); |
| 84 |
| 85 // Helper which populates a vector of completed tasks from the provided |
| 86 // namespace. |
| 87 void CollectCompletedTasks(NamespaceToken token, |
| 88 Task::Vector* completed_tasks); |
| 89 |
| 90 TaskNamespace* GetNamespaceForToken(NamespaceToken token) { |
| 91 auto it = namespaces_.find(token); |
| 92 if (it == namespaces_.end()) |
| 93 return nullptr; |
| 94 return &it->second; |
| 95 } |
| 96 |
| 97 private: |
| 98 // Helper class used to provide NamespaceToken comparison to TaskNamespaceMap. |
| 99 class CompareToken { |
| 100 public: |
| 101 bool operator()(const NamespaceToken& lhs, |
| 102 const NamespaceToken& rhs) const { |
| 103 return lhs.id_ < rhs.id_; |
| 104 } |
| 105 }; |
| 106 |
| 107 static bool CompareTaskPriority(const PrioritizedTask& a, |
| 108 const PrioritizedTask& b) { |
| 109 // In this system, numerically lower priority is run first. |
| 110 return a.priority > b.priority; |
| 111 } |
| 112 |
| 113 static bool CompareTaskNamespacePriority(const TaskNamespace* a, |
| 114 const TaskNamespace* b) { |
| 115 DCHECK(!a->ready_to_run_tasks.empty()); |
| 116 DCHECK(!b->ready_to_run_tasks.empty()); |
| 117 |
| 118 // Compare based on task priority of the ready_to_run_tasks heap .front() |
| 119 // will hold the max element of the heap, except after pop_heap, when max |
| 120 // element is moved to .back(). |
| 121 return CompareTaskPriority(a->ready_to_run_tasks.front(), |
| 122 b->ready_to_run_tasks.front()); |
| 123 } |
| 124 |
| 125 using TaskNamespaceMap = |
| 126 std::map<NamespaceToken, TaskNamespace, CompareToken>; |
| 127 |
| 128 TaskNamespaceMap namespaces_; |
| 129 TaskNamespace::Vector ready_to_run_namespaces_; |
| 130 }; |
| 131 |
| 132 } // namespace cc |
| 133 |
| 134 #endif // CC_RASTER_TASK_GRAPH_WORK_QUEUE_H_ |
OLD | NEW |