• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/containers/hash_tables.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.h"
15 #include "cc/resources/resource_provider.h"
16 #include "cc/resources/tile_priority.h"
17 #include "cc/resources/worker_pool.h"
18 #include "third_party/khronos/GLES2/gl2.h"
19 
20 namespace skia {
21 class LazyPixelRef;
22 }
23 
24 namespace cc {
25 namespace internal {
26 
27 class CC_EXPORT RasterWorkerPoolTask
28     : public base::RefCounted<RasterWorkerPoolTask> {
29  public:
30   typedef std::vector<scoped_refptr<WorkerPoolTask> > TaskVector;
31 
32   // Returns true if |buffer| was written to. False indicate that
33   // the content of |buffer| is undefined and the resource doesn't
34   // need to be initialized.
35   virtual bool RunOnWorkerThread(unsigned thread_index,
36                                  void* buffer,
37                                  gfx::Size size,
38                                  int stride) = 0;
39   virtual void CompleteOnOriginThread() = 0;
40 
41   void DidRun(bool was_canceled);
42   bool HasFinishedRunning() const;
43   bool WasCanceled() const;
44   void WillComplete();
45   void DidComplete();
46   bool HasCompleted() const;
47 
resource()48   const Resource* resource() const { return resource_; }
dependencies()49   const TaskVector& dependencies() const { return dependencies_; }
50 
51  protected:
52   friend class base::RefCounted<RasterWorkerPoolTask>;
53 
54   RasterWorkerPoolTask(const Resource* resource, TaskVector* dependencies);
55   virtual ~RasterWorkerPoolTask();
56 
57  private:
58   bool did_run_;
59   bool did_complete_;
60   bool was_canceled_;
61   const Resource* resource_;
62   TaskVector dependencies_;
63 };
64 
65 }  // namespace internal
66 }  // namespace cc
67 
68 #if defined(COMPILER_GCC)
69 namespace BASE_HASH_NAMESPACE {
70 template <> struct hash<cc::internal::RasterWorkerPoolTask*> {
71   size_t operator()(cc::internal::RasterWorkerPoolTask* ptr) const {
72     return hash<size_t>()(reinterpret_cast<size_t>(ptr));
73   }
74 };
75 }  // namespace BASE_HASH_NAMESPACE
76 #endif  // COMPILER
77 
78 namespace cc {
79 
80 class CC_EXPORT RasterWorkerPoolClient {
81  public:
82   virtual bool ShouldForceTasksRequiredForActivationToComplete() const = 0;
83   virtual void DidFinishRunningTasks() = 0;
84   virtual void DidFinishRunningTasksRequiredForActivation() = 0;
85 
86  protected:
87   virtual ~RasterWorkerPoolClient() {}
88 };
89 
90 // A worker thread pool that runs raster tasks.
91 class CC_EXPORT RasterWorkerPool : public WorkerPool {
92  public:
93   class CC_EXPORT Task {
94    public:
95     typedef base::Callback<void(bool was_canceled)> Reply;
96 
97     class CC_EXPORT Set {
98      public:
99       Set();
100       ~Set();
101 
102       void Insert(const Task& task);
103 
104      private:
105       friend class RasterWorkerPool;
106       friend class RasterWorkerPoolTest;
107 
108       typedef internal::RasterWorkerPoolTask::TaskVector TaskVector;
109       TaskVector tasks_;
110     };
111 
112     Task();
113     ~Task();
114 
115     // Returns true if Task is null (doesn't refer to anything).
116     bool is_null() const { return !internal_.get(); }
117 
118     // Returns the Task into an uninitialized state.
119     void Reset();
120 
121    protected:
122     friend class RasterWorkerPool;
123     friend class RasterWorkerPoolTest;
124 
125     explicit Task(internal::WorkerPoolTask* internal);
126 
127     scoped_refptr<internal::WorkerPoolTask> internal_;
128   };
129 
130   class CC_EXPORT RasterTask {
131    public:
132     typedef base::Callback<void(const PicturePileImpl::Analysis& analysis,
133                                 bool was_canceled)> Reply;
134 
135     class CC_EXPORT Queue {
136      public:
137       Queue();
138       ~Queue();
139 
140       void Append(const RasterTask& task, bool required_for_activation);
141 
142      private:
143       friend class RasterWorkerPool;
144 
145       typedef std::vector<scoped_refptr<internal::RasterWorkerPoolTask> >
146           TaskVector;
147       TaskVector tasks_;
148       typedef base::hash_set<internal::RasterWorkerPoolTask*> TaskSet;
149       TaskSet tasks_required_for_activation_;
150     };
151 
152     RasterTask();
153     ~RasterTask();
154 
155     // Returns true if Task is null (doesn't refer to anything).
156     bool is_null() const { return !internal_.get(); }
157 
158     // Returns the Task into an uninitialized state.
159     void Reset();
160 
161    protected:
162     friend class RasterWorkerPool;
163     friend class RasterWorkerPoolTest;
164 
165     explicit RasterTask(internal::RasterWorkerPoolTask* internal);
166 
167     scoped_refptr<internal::RasterWorkerPoolTask> internal_;
168   };
169 
170   virtual ~RasterWorkerPool();
171 
172   void SetClient(RasterWorkerPoolClient* client);
173 
174   // Tells the worker pool to shutdown after canceling all previously
175   // scheduled tasks. Reply callbacks are still guaranteed to run.
176   virtual void Shutdown() OVERRIDE;
177 
178   // Schedule running of raster tasks in |queue| and all dependencies.
179   // Previously scheduled tasks that are no longer needed to run
180   // raster tasks in |queue| will be canceled unless already running.
181   // Once scheduled, reply callbacks are guaranteed to run for all tasks
182   // even if they later get canceled by another call to ScheduleTasks().
183   virtual void ScheduleTasks(RasterTask::Queue* queue) = 0;
184 
185   // Returns the target that needs to be used for raster task resources.
186   virtual GLenum GetResourceTarget() const = 0;
187 
188   // Returns the format that needs to be used for raster task resources.
189   virtual ResourceFormat GetResourceFormat() const = 0;
190 
191   // TODO(vmpstr): Figure out an elegant way to not pass this many parameters.
192   static RasterTask CreateRasterTask(
193       const Resource* resource,
194       PicturePileImpl* picture_pile,
195       gfx::Rect content_rect,
196       float contents_scale,
197       RasterMode raster_mode,
198       TileResolution tile_resolution,
199       int layer_id,
200       const void* tile_id,
201       int source_frame_number,
202       RenderingStatsInstrumentation* rendering_stats,
203       const RasterTask::Reply& reply,
204       Task::Set* dependencies);
205 
206   static Task CreateImageDecodeTask(
207       skia::LazyPixelRef* pixel_ref,
208       int layer_id,
209       RenderingStatsInstrumentation* stats_instrumentation,
210       const Task::Reply& reply);
211 
212  protected:
213   typedef std::vector<scoped_refptr<internal::WorkerPoolTask> > TaskVector;
214   typedef std::vector<scoped_refptr<internal::RasterWorkerPoolTask> >
215       RasterTaskVector;
216   typedef base::hash_set<internal::RasterWorkerPoolTask*> RasterTaskSet;
217   typedef internal::RasterWorkerPoolTask* TaskMapKey;
218   typedef base::hash_map<TaskMapKey,
219                          scoped_refptr<internal::WorkerPoolTask> > TaskMap;
220 
221   RasterWorkerPool(ResourceProvider* resource_provider, size_t num_threads);
222 
223   virtual void OnRasterTasksFinished() = 0;
224   virtual void OnRasterTasksRequiredForActivationFinished() = 0;
225 
226   void SetRasterTasks(RasterTask::Queue* queue);
227   bool IsRasterTaskRequiredForActivation(
228       internal::RasterWorkerPoolTask* task) const;
229 
230   RasterWorkerPoolClient* client() const { return client_; }
231   ResourceProvider* resource_provider() const { return resource_provider_; }
232   const RasterTaskVector& raster_tasks() const { return raster_tasks_; }
233   const RasterTaskSet& raster_tasks_required_for_activation() const {
234     return raster_tasks_required_for_activation_;
235   }
236   void set_raster_finished_task(
237       scoped_refptr<internal::WorkerPoolTask> raster_finished_task) {
238     raster_finished_task_ = raster_finished_task;
239   }
240   void set_raster_required_for_activation_finished_task(
241       scoped_refptr<internal::WorkerPoolTask>
242           raster_required_for_activation_finished_task) {
243     raster_required_for_activation_finished_task_ =
244         raster_required_for_activation_finished_task;
245   }
246 
247   scoped_refptr<internal::WorkerPoolTask> CreateRasterFinishedTask();
248   scoped_refptr<internal::WorkerPoolTask>
249       CreateRasterRequiredForActivationFinishedTask();
250 
251   scoped_ptr<base::Value> ScheduledStateAsValue() const;
252 
253   static internal::GraphNode* CreateGraphNodeForTask(
254       internal::WorkerPoolTask* task,
255       unsigned priority,
256       TaskGraph* graph);
257 
258   static internal::GraphNode* CreateGraphNodeForRasterTask(
259       internal::WorkerPoolTask* raster_task,
260       const TaskVector& decode_tasks,
261       unsigned priority,
262       TaskGraph* graph);
263 
264  private:
265   void OnRasterFinished(const internal::WorkerPoolTask* source);
266   void OnRasterRequiredForActivationFinished(
267       const internal::WorkerPoolTask* source);
268 
269   RasterWorkerPoolClient* client_;
270   ResourceProvider* resource_provider_;
271   RasterTask::Queue::TaskVector raster_tasks_;
272   RasterTask::Queue::TaskSet raster_tasks_required_for_activation_;
273 
274   scoped_refptr<internal::WorkerPoolTask> raster_finished_task_;
275   scoped_refptr<internal::WorkerPoolTask>
276       raster_required_for_activation_finished_task_;
277   base::WeakPtrFactory<RasterWorkerPool> weak_ptr_factory_;
278 };
279 
280 }  // namespace cc
281 
282 #endif  // CC_RESOURCES_RASTER_WORKER_POOL_H_
283