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
3 // found in the LICENSE file.
5 #ifndef CC_RESOURCES_RASTER_WORKER_POOL_H_
6 #define CC_RESOURCES_RASTER_WORKER_POOL_H_
11 #include "cc/debug/rendering_stats_instrumentation.h"
12 #include "cc/resources/picture_pile_impl.h"
13 #include "cc/resources/raster_mode.h"
14 #include "cc/resources/resource_format.h"
15 #include "cc/resources/task_graph_runner.h"
16 #include "cc/resources/tile_priority.h"
22 class ContextProvider;
24 class ResourceProvider;
29 class RasterWorkerPoolTask;
31 class CC_EXPORT WorkerPoolTaskClient {
33 virtual SkCanvas* AcquireCanvasForRaster(RasterWorkerPoolTask* task) = 0;
34 virtual void OnRasterCompleted(RasterWorkerPoolTask* task,
35 const PicturePileImpl::Analysis& analysis) = 0;
36 virtual void OnImageDecodeCompleted(WorkerPoolTask* task) = 0;
39 virtual ~WorkerPoolTaskClient() {}
42 class CC_EXPORT WorkerPoolTask : public Task {
44 typedef std::vector<scoped_refptr<WorkerPoolTask> > Vector;
46 virtual void ScheduleOnOriginThread(WorkerPoolTaskClient* client) = 0;
47 virtual void RunOnOriginThread() = 0;
48 virtual void CompleteOnOriginThread(WorkerPoolTaskClient* client) = 0;
49 virtual void RunReplyOnOriginThread() = 0;
53 bool HasBeenScheduled() const;
57 bool HasCompleted() const;
61 virtual ~WorkerPoolTask();
67 class CC_EXPORT RasterWorkerPoolTask : public WorkerPoolTask {
69 const Resource* resource() const { return resource_; }
70 const internal::WorkerPoolTask::Vector& dependencies() const {
75 RasterWorkerPoolTask(const Resource* resource,
76 internal::WorkerPoolTask::Vector* dependencies);
77 virtual ~RasterWorkerPoolTask();
80 const Resource* resource_;
81 WorkerPoolTask::Vector dependencies_;
84 } // namespace internal
86 class CC_EXPORT RasterWorkerPoolClient {
88 virtual bool ShouldForceTasksRequiredForActivationToComplete() const = 0;
89 virtual void DidFinishRunningTasks() = 0;
90 virtual void DidFinishRunningTasksRequiredForActivation() = 0;
93 virtual ~RasterWorkerPoolClient() {}
96 // A worker thread pool that runs raster tasks.
97 class CC_EXPORT RasterWorkerPool : public internal::WorkerPoolTaskClient {
99 class CC_EXPORT Task {
101 typedef base::Callback<void(bool was_canceled)> Reply;
103 class CC_EXPORT Set {
108 void Insert(const Task& task);
111 friend class RasterWorkerPool;
113 internal::WorkerPoolTask::Vector tasks_;
119 // Returns true if Task is null (doesn't refer to anything).
120 bool is_null() const { return !internal_.get(); }
122 // Returns the Task into an uninitialized state.
126 friend class RasterWorkerPool;
128 explicit Task(internal::WorkerPoolTask* internal);
130 scoped_refptr<internal::WorkerPoolTask> internal_;
133 class CC_EXPORT RasterTask {
135 typedef base::Callback<void(const PicturePileImpl::Analysis& analysis,
136 bool was_canceled)> Reply;
138 class CC_EXPORT Queue {
144 void Append(const RasterTask& task, bool required_for_activation);
145 void Swap(Queue* other);
147 size_t count() const { return tasks_.size(); }
148 size_t required_for_activation_count() const {
149 return required_for_activation_count_;
153 friend class RasterWorkerPool;
154 friend class DirectRasterWorkerPool;
157 class TaskComparator {
159 explicit TaskComparator(const internal::RasterWorkerPoolTask* task)
162 bool operator()(const QueuedTask& queued_task) const {
163 return queued_task.task == task_;
167 const internal::RasterWorkerPoolTask* task_;
170 typedef std::vector<QueuedTask> Vector;
172 QueuedTask(internal::RasterWorkerPoolTask* task,
173 bool required_for_activation);
176 scoped_refptr<internal::RasterWorkerPoolTask> task;
177 bool required_for_activation;
180 QueuedTask::Vector tasks_;
181 size_t required_for_activation_count_;
187 // Returns true if Task is null (doesn't refer to anything).
188 bool is_null() const { return !internal_.get(); }
190 // Returns the Task into an uninitialized state.
194 friend class RasterWorkerPool;
196 explicit RasterTask(internal::RasterWorkerPoolTask* internal);
198 scoped_refptr<internal::RasterWorkerPoolTask> internal_;
201 virtual ~RasterWorkerPool();
203 static void SetNumRasterThreads(int num_threads);
204 static int GetNumRasterThreads();
206 static internal::TaskGraphRunner* GetTaskGraphRunner();
208 static unsigned kOnDemandRasterTaskPriority;
209 static unsigned kRasterFinishedTaskPriority;
210 static unsigned kRasterRequiredForActivationFinishedTaskPriority;
211 static unsigned kRasterTaskPriorityBase;
213 // TODO(vmpstr): Figure out an elegant way to not pass this many parameters.
214 static RasterTask CreateRasterTask(
215 const Resource* resource,
216 PicturePileImpl* picture_pile,
217 const gfx::Rect& content_rect,
218 float contents_scale,
219 RasterMode raster_mode,
220 TileResolution tile_resolution,
223 int source_frame_number,
224 RenderingStatsInstrumentation* rendering_stats,
225 const RasterTask::Reply& reply,
226 Task::Set* dependencies,
227 ContextProvider* context_provider);
229 static Task CreateImageDecodeTask(
230 SkPixelRef* pixel_ref,
232 RenderingStatsInstrumentation* rendering_stats,
233 const Task::Reply& reply);
235 void SetClient(RasterWorkerPoolClient* client);
237 // Tells the worker pool to shutdown after canceling all previously
238 // scheduled tasks. Reply callbacks are still guaranteed to run.
239 virtual void Shutdown();
241 // Schedule running of raster tasks in |queue| and all dependencies.
242 // Previously scheduled tasks that are no longer needed to run
243 // raster tasks in |queue| will be canceled unless already running.
244 // Once scheduled, reply callbacks are guaranteed to run for all tasks
245 // even if they later get canceled by another call to ScheduleTasks().
246 virtual void ScheduleTasks(RasterTask::Queue* queue) = 0;
248 // Force a check for completed tasks.
249 virtual void CheckForCompletedTasks() = 0;
251 // Returns the target that needs to be used for raster task resources.
252 virtual unsigned GetResourceTarget() const = 0;
254 // Returns the format that needs to be used for raster task resources.
255 virtual ResourceFormat GetResourceFormat() const = 0;
258 class RasterTaskQueueIterator {
260 explicit RasterTaskQueueIterator(const RasterTask::Queue* queue)
261 : tasks_(&queue->tasks_), current_index_(0u) {}
262 ~RasterTaskQueueIterator() {}
264 bool required_for_activation() const {
265 DCHECK_LT(current_index_, tasks_->size());
266 return (*tasks_)[current_index_].required_for_activation;
269 internal::RasterWorkerPoolTask* operator->() const {
270 DCHECK_LT(current_index_, tasks_->size());
271 return (*tasks_)[current_index_].task.get();
274 internal::RasterWorkerPoolTask* operator*() const {
275 DCHECK_LT(current_index_, tasks_->size());
276 return (*tasks_)[current_index_].task.get();
279 RasterTaskQueueIterator& operator++() {
280 DCHECK_LT(current_index_, tasks_->size());
285 operator bool() const { return current_index_ < tasks_->size(); }
288 const RasterTask::Queue::QueuedTask::Vector* tasks_;
289 size_t current_index_;
291 typedef std::vector<scoped_refptr<internal::WorkerPoolTask> > TaskVector;
292 typedef std::deque<scoped_refptr<internal::WorkerPoolTask> > TaskDeque;
293 typedef std::vector<scoped_refptr<internal::RasterWorkerPoolTask> >
296 RasterWorkerPool(internal::TaskGraphRunner* task_graph_runner,
297 ResourceProvider* resource_provider);
299 virtual void OnRasterTasksFinished() = 0;
300 virtual void OnRasterTasksRequiredForActivationFinished() = 0;
302 void SetTaskGraph(internal::TaskGraph* graph);
303 void CollectCompletedWorkerPoolTasks(internal::Task::Vector* completed_tasks);
305 RasterWorkerPoolClient* client() const { return client_; }
306 ResourceProvider* resource_provider() const { return resource_provider_; }
308 void set_raster_finished_task(
309 internal::WorkerPoolTask* raster_finished_task) {
310 raster_finished_task_ = raster_finished_task;
312 internal::WorkerPoolTask* raster_finished_task() const {
313 return raster_finished_task_.get();
315 void set_raster_required_for_activation_finished_task(
316 internal::WorkerPoolTask* raster_required_for_activation_finished_task) {
317 raster_required_for_activation_finished_task_ =
318 raster_required_for_activation_finished_task;
320 internal::WorkerPoolTask* raster_required_for_activation_finished_task()
322 return raster_required_for_activation_finished_task_.get();
325 scoped_refptr<internal::WorkerPoolTask> CreateRasterFinishedTask();
326 scoped_refptr<internal::WorkerPoolTask>
327 CreateRasterRequiredForActivationFinishedTask(
328 size_t tasks_required_for_activation_count);
330 void RunTaskOnOriginThread(internal::WorkerPoolTask* task);
332 static void InsertNodeForTask(internal::TaskGraph* graph,
333 internal::WorkerPoolTask* task,
335 size_t dependencies);
337 static void InsertNodeForRasterTask(
338 internal::TaskGraph* graph,
339 internal::WorkerPoolTask* task,
340 const internal::WorkerPoolTask::Vector& decode_tasks,
344 void OnRasterFinished(const internal::WorkerPoolTask* source);
345 void OnRasterRequiredForActivationFinished(
346 const internal::WorkerPoolTask* source);
348 internal::TaskGraphRunner* task_graph_runner_;
349 internal::NamespaceToken namespace_token_;
350 RasterWorkerPoolClient* client_;
351 ResourceProvider* resource_provider_;
353 scoped_refptr<internal::WorkerPoolTask> raster_finished_task_;
354 scoped_refptr<internal::WorkerPoolTask>
355 raster_required_for_activation_finished_task_;
356 base::WeakPtrFactory<RasterWorkerPool> weak_ptr_factory_;
361 #endif // CC_RESOURCES_RASTER_WORKER_POOL_H_