| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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_RESOURCES_RASTER_WORKER_POOL_H_ | 5 #ifndef CC_RESOURCES_RASTER_WORKER_POOL_H_ |
| 6 #define CC_RESOURCES_RASTER_WORKER_POOL_H_ | 6 #define CC_RESOURCES_RASTER_WORKER_POOL_H_ |
| 7 | 7 |
| 8 #include <vector> | 8 #include "cc/resources/rasterizer.h" |
| 9 | |
| 10 #include "base/callback.h" | |
| 11 #include "cc/resources/resource_format.h" | |
| 12 #include "cc/resources/task_graph_runner.h" | |
| 13 | |
| 14 class SkCanvas; | |
| 15 | 9 |
| 16 namespace base { | 10 namespace base { |
| 17 class SequencedTaskRunner; | 11 class SequencedTaskRunner; |
| 18 } | 12 } |
| 19 | 13 |
| 20 namespace cc { | 14 namespace cc { |
| 21 class Resource; | |
| 22 | 15 |
| 23 namespace internal { | |
| 24 class WorkerPoolTask; | |
| 25 | |
| 26 class CC_EXPORT WorkerPoolTaskClient { | |
| 27 public: | |
| 28 virtual SkCanvas* AcquireCanvasForRaster(WorkerPoolTask* task, | |
| 29 const Resource* resource) = 0; | |
| 30 virtual void ReleaseCanvasForRaster(WorkerPoolTask* task, | |
| 31 const Resource* resource) = 0; | |
| 32 | |
| 33 protected: | |
| 34 virtual ~WorkerPoolTaskClient() {} | |
| 35 }; | |
| 36 | |
| 37 class CC_EXPORT WorkerPoolTask : public Task { | |
| 38 public: | |
| 39 typedef std::vector<scoped_refptr<WorkerPoolTask> > Vector; | |
| 40 | |
| 41 virtual void ScheduleOnOriginThread(WorkerPoolTaskClient* client) = 0; | |
| 42 virtual void RunOnOriginThread() = 0; | |
| 43 virtual void CompleteOnOriginThread(WorkerPoolTaskClient* client) = 0; | |
| 44 virtual void RunReplyOnOriginThread() = 0; | |
| 45 | |
| 46 void WillSchedule(); | |
| 47 void DidSchedule(); | |
| 48 bool HasBeenScheduled() const; | |
| 49 | |
| 50 void WillComplete(); | |
| 51 void DidComplete(); | |
| 52 bool HasCompleted() const; | |
| 53 | |
| 54 protected: | |
| 55 WorkerPoolTask(); | |
| 56 virtual ~WorkerPoolTask(); | |
| 57 | |
| 58 bool did_schedule_; | |
| 59 bool did_complete_; | |
| 60 }; | |
| 61 | |
| 62 class CC_EXPORT RasterWorkerPoolTask : public WorkerPoolTask { | |
| 63 public: | |
| 64 const Resource* resource() const { return resource_; } | |
| 65 const internal::WorkerPoolTask::Vector& dependencies() const { | |
| 66 return dependencies_; | |
| 67 } | |
| 68 | |
| 69 protected: | |
| 70 RasterWorkerPoolTask(const Resource* resource, | |
| 71 internal::WorkerPoolTask::Vector* dependencies); | |
| 72 virtual ~RasterWorkerPoolTask(); | |
| 73 | |
| 74 private: | |
| 75 const Resource* resource_; | |
| 76 WorkerPoolTask::Vector dependencies_; | |
| 77 }; | |
| 78 | |
| 79 } // namespace internal | |
| 80 | |
| 81 class CC_EXPORT RasterWorkerPoolClient { | |
| 82 public: | |
| 83 virtual bool ShouldForceTasksRequiredForActivationToComplete() const = 0; | |
| 84 virtual void DidFinishRunningTasks() = 0; | |
| 85 virtual void DidFinishRunningTasksRequiredForActivation() = 0; | |
| 86 | |
| 87 protected: | |
| 88 virtual ~RasterWorkerPoolClient() {} | |
| 89 }; | |
| 90 | |
| 91 struct CC_EXPORT RasterTaskQueue { | |
| 92 struct CC_EXPORT Item { | |
| 93 class TaskComparator { | |
| 94 public: | |
| 95 explicit TaskComparator(const internal::WorkerPoolTask* task) | |
| 96 : task_(task) {} | |
| 97 | |
| 98 bool operator()(const Item& item) const { return item.task == task_; } | |
| 99 | |
| 100 private: | |
| 101 const internal::WorkerPoolTask* task_; | |
| 102 }; | |
| 103 | |
| 104 typedef std::vector<Item> Vector; | |
| 105 | |
| 106 Item(internal::RasterWorkerPoolTask* task, bool required_for_activation); | |
| 107 ~Item(); | |
| 108 | |
| 109 static bool IsRequiredForActivation(const Item& item) { | |
| 110 return item.required_for_activation; | |
| 111 } | |
| 112 | |
| 113 internal::RasterWorkerPoolTask* task; | |
| 114 bool required_for_activation; | |
| 115 }; | |
| 116 | |
| 117 RasterTaskQueue(); | |
| 118 ~RasterTaskQueue(); | |
| 119 | |
| 120 void Swap(RasterTaskQueue* other); | |
| 121 void Reset(); | |
| 122 | |
| 123 Item::Vector items; | |
| 124 size_t required_for_activation_count; | |
| 125 }; | |
| 126 | |
| 127 // This interface can be used to schedule and run raster tasks. The client will | |
| 128 // be notified asynchronously when the set of tasks marked as "required for | |
| 129 // activation" have finished running and when all scheduled tasks have finished | |
| 130 // running. The client can call CheckForCompletedTasks() at any time to dispatch | |
| 131 // pending completion callbacks for all tasks that have finished running. | |
| 132 class CC_EXPORT RasterWorkerPool { | 16 class CC_EXPORT RasterWorkerPool { |
| 133 public: | 17 public: |
| 134 static unsigned kOnDemandRasterTaskPriority; | 18 static unsigned kOnDemandRasterTaskPriority; |
| 135 static unsigned kBenchmarkRasterTaskPriority; | 19 static unsigned kBenchmarkRasterTaskPriority; |
| 136 static unsigned kRasterFinishedTaskPriority; | 20 static unsigned kRasterFinishedTaskPriority; |
| 137 static unsigned kRasterRequiredForActivationFinishedTaskPriority; | 21 static unsigned kRasterRequiredForActivationFinishedTaskPriority; |
| 138 static unsigned kRasterTaskPriorityBase; | 22 static unsigned kRasterTaskPriorityBase; |
| 139 | 23 |
| 24 RasterWorkerPool(); |
| 25 virtual ~RasterWorkerPool(); |
| 26 |
| 140 // Set the number of threads to use for the global TaskGraphRunner instance. | 27 // Set the number of threads to use for the global TaskGraphRunner instance. |
| 141 // This can only be called once and must be called prior to | 28 // This can only be called once and must be called prior to |
| 142 // GetNumRasterThreads(). | 29 // GetNumRasterThreads(). |
| 143 static void SetNumRasterThreads(int num_threads); | 30 static void SetNumRasterThreads(int num_threads); |
| 144 | 31 |
| 145 // Returns the number of threads used for the global TaskGraphRunner instance. | 32 // Returns the number of threads used for the global TaskGraphRunner instance. |
| 146 static int GetNumRasterThreads(); | 33 static int GetNumRasterThreads(); |
| 147 | 34 |
| 148 // Returns a pointer to the global TaskGraphRunner instance. | 35 // Returns a pointer to the global TaskGraphRunner instance. |
| 149 static internal::TaskGraphRunner* GetTaskGraphRunner(); | 36 static internal::TaskGraphRunner* GetTaskGraphRunner(); |
| 150 | 37 |
| 151 // Returns a unique clone index for the current thread. Guaranteed to be a | 38 // Returns a unique clone index for the current thread. Guaranteed to be a |
| 152 // value between 0 and GetNumRasterThreads() - 1. | 39 // value between 0 and GetNumRasterThreads() - 1. |
| 153 static size_t GetPictureCloneIndexForCurrentThread(); | 40 static size_t GetPictureCloneIndexForCurrentThread(); |
| 154 | 41 |
| 155 // Utility function that can be used by implementations to create a "raster | 42 // Utility function that can be used to create a "raster finished" task that |
| 156 // finished" task that posts |callback| to |task_runner| when run. | 43 // posts |callback| to |task_runner| when run. |
| 157 static scoped_refptr<internal::WorkerPoolTask> CreateRasterFinishedTask( | 44 static scoped_refptr<internal::RasterizerTask> CreateRasterFinishedTask( |
| 158 base::SequencedTaskRunner* task_runner, | 45 base::SequencedTaskRunner* task_runner, |
| 159 const base::Closure& callback); | 46 const base::Closure& callback); |
| 160 | 47 |
| 161 // Utility function that can be used by implementations to create a "raster | 48 // Utility function that can be used to create a "raster required for |
| 162 // required for activation finished" task that posts |callback| to | 49 // activation finished" task that posts |callback| to |task_runner| when run. |
| 163 // |task_runner| when run. | 50 static scoped_refptr<internal::RasterizerTask> |
| 164 static scoped_refptr<internal::WorkerPoolTask> | |
| 165 CreateRasterRequiredForActivationFinishedTask( | 51 CreateRasterRequiredForActivationFinishedTask( |
| 166 size_t tasks_required_for_activation_count, | 52 size_t tasks_required_for_activation_count, |
| 167 base::SequencedTaskRunner* task_runner, | 53 base::SequencedTaskRunner* task_runner, |
| 168 const base::Closure& callback); | 54 const base::Closure& callback); |
| 169 | 55 |
| 170 // Utility function that can be used by implementations to call | 56 // Utility function that can be used to call ::ScheduleOnOriginThread() for |
| 171 // ::ScheduleOnOriginThread() for each task in |graph|. | 57 // each task in |graph|. |
| 172 static void ScheduleTasksOnOriginThread( | 58 static void ScheduleTasksOnOriginThread( |
| 173 internal::WorkerPoolTaskClient* client, | 59 internal::RasterizerTaskClient* client, |
| 174 internal::TaskGraph* graph); | 60 internal::TaskGraph* graph); |
| 175 | 61 |
| 176 // Utility function that can be used by implementations to build a task graph. | 62 // Utility function that can be used to build a task graph. Inserts a node |
| 177 // Inserts a node that represents |task| in |graph|. See TaskGraph definition | 63 // that represents |task| in |graph|. See TaskGraph definition for valid |
| 178 // for valid |priority| values. | 64 // |priority| values. |
| 179 static void InsertNodeForTask(internal::TaskGraph* graph, | 65 static void InsertNodeForTask(internal::TaskGraph* graph, |
| 180 internal::WorkerPoolTask* task, | 66 internal::RasterizerTask* task, |
| 181 unsigned priority, | 67 unsigned priority, |
| 182 size_t dependencies); | 68 size_t dependencies); |
| 183 | 69 |
| 184 // Utility function that can be used by implementations to build a task graph. | 70 // Utility function that can be used to build a task graph. Inserts nodes that |
| 185 // Inserts nodes that represent |task| and all its image decode dependencies | 71 // represent |task| and all its image decode dependencies in |graph|. |
| 186 // in |graph|. | |
| 187 static void InsertNodesForRasterTask( | 72 static void InsertNodesForRasterTask( |
| 188 internal::TaskGraph* graph, | 73 internal::TaskGraph* graph, |
| 189 internal::WorkerPoolTask* task, | 74 internal::RasterTask* task, |
| 190 const internal::WorkerPoolTask::Vector& decode_tasks, | 75 const internal::ImageDecodeTask::Vector& decode_tasks, |
| 191 unsigned priority); | 76 unsigned priority); |
| 192 | 77 |
| 193 // Set the client instance to be notified when finished running tasks. | 78 // Type-checking downcast routine. |
| 194 virtual void SetClient(RasterWorkerPoolClient* client) = 0; | 79 virtual Rasterizer* AsRasterizer() = 0; |
| 195 | |
| 196 // Tells the worker pool to shutdown after canceling all previously scheduled | |
| 197 // tasks. Reply callbacks are still guaranteed to run when | |
| 198 // CheckForCompletedTasks() is called. | |
| 199 virtual void Shutdown() = 0; | |
| 200 | |
| 201 // Schedule running of raster tasks in |queue| and all dependencies. | |
| 202 // Previously scheduled tasks that are not in |queue| will be canceled unless | |
| 203 // already running. Once scheduled, reply callbacks are guaranteed to run for | |
| 204 // all tasks even if they later get canceled by another call to | |
| 205 // ScheduleTasks(). | |
| 206 virtual void ScheduleTasks(RasterTaskQueue* queue) = 0; | |
| 207 | |
| 208 // Check for completed tasks and dispatch reply callbacks. | |
| 209 virtual void CheckForCompletedTasks() = 0; | |
| 210 | |
| 211 // Returns the target that needs to be used for raster task resources. | |
| 212 virtual unsigned GetResourceTarget() const = 0; | |
| 213 | |
| 214 // Returns the format that needs to be used for raster task resources. | |
| 215 virtual ResourceFormat GetResourceFormat() const = 0; | |
| 216 | |
| 217 protected: | |
| 218 virtual ~RasterWorkerPool() {} | |
| 219 }; | 80 }; |
| 220 | 81 |
| 221 } // namespace cc | 82 } // namespace cc |
| 222 | 83 |
| 223 #endif // CC_RESOURCES_RASTER_WORKER_POOL_H_ | 84 #endif // CC_RESOURCES_RASTER_WORKER_POOL_H_ |
| OLD | NEW |