raster_worker_pool.h revision 868fa2fe829687343ffae624259930155e16dbd8
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.
4
5#ifndef CC_RESOURCES_RASTER_WORKER_POOL_H_
6#define CC_RESOURCES_RASTER_WORKER_POOL_H_
7
8#include <vector>
9
10#include "base/hash_tables.h"
11#include "cc/base/worker_pool.h"
12#include "cc/resources/resource_provider.h"
13
14class SkDevice;
15
16namespace cc {
17class PicturePileImpl;
18class PixelBufferRasterWorkerPool;
19class Resource;
20
21namespace internal {
22
23class CC_EXPORT RasterWorkerPoolTask
24    : public base::RefCounted<RasterWorkerPoolTask> {
25 public:
26  typedef std::vector<scoped_refptr<RasterWorkerPoolTask> > TaskVector;
27
28  virtual bool RunOnThread(SkDevice* device, unsigned thread_index) = 0;
29  virtual void DispatchCompletionCallback() = 0;
30
31  void DidRun();
32  bool HasFinishedRunning() const;
33  void DidComplete();
34  bool HasCompleted() const;
35
36  const Resource* resource() const { return resource_; }
37  const WorkerPoolTask::TaskVector& dependencies() const {
38    return dependencies_;
39  }
40
41 protected:
42  friend class base::RefCounted<RasterWorkerPoolTask>;
43
44  RasterWorkerPoolTask(const Resource* resource,
45                       WorkerPoolTask::TaskVector* dependencies);
46  virtual ~RasterWorkerPoolTask();
47
48 private:
49  bool did_run_;
50  bool did_complete_;
51  const Resource* resource_;
52  WorkerPoolTask::TaskVector dependencies_;
53};
54
55}  // namespace internal
56}  // namespace cc
57
58#if defined(COMPILER_GCC)
59namespace BASE_HASH_NAMESPACE {
60template <> struct hash<cc::internal::RasterWorkerPoolTask*> {
61  size_t operator()(cc::internal::RasterWorkerPoolTask* ptr) const {
62    return hash<size_t>()(reinterpret_cast<size_t>(ptr));
63  }
64};
65}  // namespace BASE_HASH_NAMESPACE
66#endif  // COMPILER
67
68namespace cc {
69
70// A worker thread pool that runs raster tasks.
71class CC_EXPORT RasterWorkerPool : public WorkerPool {
72 public:
73  class CC_EXPORT Task {
74   public:
75    class CC_EXPORT Set {
76     public:
77      typedef internal::WorkerPoolTask::TaskVector TaskVector;
78
79      Set();
80      ~Set();
81
82      void Insert(const Task& task);
83
84     private:
85      friend class RasterWorkerPool;
86
87      TaskVector tasks_;
88    };
89
90    Task();
91    Task(const base::Closure& callback, const base::Closure& reply);
92    ~Task();
93
94    // Returns true if Task is null (doesn't refer to anything).
95    bool is_null() const { return !internal_.get(); }
96
97    // Returns the Task into an uninitialized state.
98    void Reset();
99
100   protected:
101    friend class RasterWorkerPool;
102
103    scoped_refptr<internal::WorkerPoolTask> internal_;
104  };
105
106  class CC_EXPORT RasterTask {
107   public:
108    // Returns true if |device| was written to. False indicate that
109    // the content of |device| is undefined and the resource doesn't
110    // need to be initialized.
111    typedef base::Callback<bool(SkDevice* device,
112                                PicturePileImpl* picture_pile)> Callback;
113
114    typedef base::Callback<void(bool was_canceled)> Reply;
115
116    class CC_EXPORT Queue {
117     public:
118      typedef internal::RasterWorkerPoolTask::TaskVector TaskVector;
119
120      Queue();
121      ~Queue();
122
123      void Append(const RasterTask& task);
124
125     private:
126      friend class RasterWorkerPool;
127
128      TaskVector tasks_;
129    };
130
131    RasterTask();
132    RasterTask(PicturePileImpl* picture_pile,
133               const Resource* resource,
134               const Callback& callback,
135               const Reply& reply,
136               Task::Set* dependencies);
137    ~RasterTask();
138
139    // Returns true if Task is null (doesn't refer to anything).
140    bool is_null() const { return !internal_; }
141
142    // Returns the Task into an uninitialized state.
143    void Reset();
144
145   protected:
146    friend class PixelBufferRasterWorkerPool;
147    friend class RasterWorkerPool;
148
149    scoped_refptr<internal::RasterWorkerPoolTask> internal_;
150  };
151
152  virtual ~RasterWorkerPool();
153
154  // Tells the worker pool to shutdown after canceling all previously
155  // scheduled tasks. Reply callbacks are still guaranteed to run.
156  virtual void Shutdown() OVERRIDE;
157
158  // Schedule running of raster tasks in |queue| and all dependencies.
159  // Previously scheduled tasks that are no longer needed to run
160  // raster tasks in |queue| will be canceled unless already running.
161  // Once scheduled, reply callbacks are guaranteed to run for all tasks
162  // even if they later get canceled by another call to ScheduleTasks().
163  virtual void ScheduleTasks(RasterTask::Queue* queue) = 0;
164
165  // Tells the raster worker pool to force any pending uploads for
166  // |raster_task| to complete. Returns true when successful.
167  virtual bool ForceUploadToComplete(const RasterTask& raster_task);
168
169 protected:
170  class RootTask {
171   public:
172    RootTask();
173    explicit RootTask(internal::WorkerPoolTask::TaskVector* dependencies);
174    RootTask(const base::Closure& callback,
175             internal::WorkerPoolTask::TaskVector* dependencies);
176    ~RootTask();
177
178   protected:
179    friend class RasterWorkerPool;
180
181    scoped_refptr<internal::WorkerPoolTask> internal_;
182  };
183
184  typedef internal::RasterWorkerPoolTask* TaskMapKey;
185  typedef base::hash_map<TaskMapKey,
186                         scoped_refptr<internal::WorkerPoolTask> > TaskMap;
187
188  RasterWorkerPool(ResourceProvider* resource_provider, size_t num_threads);
189
190  void SetRasterTasks(RasterTask::Queue* queue);
191  void ScheduleRasterTasks(const RootTask& root);
192
193  ResourceProvider* resource_provider() const { return resource_provider_; }
194  const RasterTask::Queue::TaskVector& raster_tasks() const {
195    return raster_tasks_;
196  }
197
198 private:
199  ResourceProvider* resource_provider_;
200  RasterTask::Queue::TaskVector raster_tasks_;
201};
202
203}  // namespace cc
204
205#endif  // CC_RESOURCES_RASTER_WORKER_POOL_H_
206