| 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 <deque> | 8 #include <deque> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/containers/hash_tables.h" | 11 #include "base/containers/hash_tables.h" |
| 12 #include "cc/debug/rendering_stats_instrumentation.h" | 12 #include "cc/debug/rendering_stats_instrumentation.h" |
| 13 #include "cc/resources/picture_pile_impl.h" | 13 #include "cc/resources/picture_pile_impl.h" |
| 14 #include "cc/resources/raster_mode.h" | 14 #include "cc/resources/raster_mode.h" |
| 15 #include "cc/resources/resource_format.h" | 15 #include "cc/resources/resource.h" |
| 16 #include "cc/resources/task_graph_runner.h" | 16 #include "cc/resources/resource_provider.h" |
| 17 #include "cc/resources/tile_priority.h" | 17 #include "cc/resources/tile_priority.h" |
| 18 #include "cc/resources/worker_pool.h" |
| 19 #include "third_party/khronos/GLES2/gl2.h" |
| 18 | 20 |
| 19 class SkPixelRef; | 21 class SkPixelRef; |
| 20 | 22 |
| 21 namespace cc { | 23 namespace cc { |
| 22 | |
| 23 class ContextProvider; | |
| 24 class Resource; | |
| 25 class ResourceProvider; | |
| 26 | |
| 27 namespace internal { | 24 namespace internal { |
| 28 | 25 |
| 29 class CC_EXPORT WorkerPoolTask : public Task { | |
| 30 public: | |
| 31 virtual void CompleteOnOriginThread() = 0; | |
| 32 | |
| 33 void WillComplete(); | |
| 34 void DidComplete(); | |
| 35 bool HasCompleted() const; | |
| 36 | |
| 37 protected: | |
| 38 WorkerPoolTask(); | |
| 39 virtual ~WorkerPoolTask(); | |
| 40 | |
| 41 private: | |
| 42 bool did_complete_; | |
| 43 }; | |
| 44 | |
| 45 class CC_EXPORT RasterWorkerPoolTask | 26 class CC_EXPORT RasterWorkerPoolTask |
| 46 : public base::RefCounted<RasterWorkerPoolTask> { | 27 : public base::RefCounted<RasterWorkerPoolTask> { |
| 47 public: | 28 public: |
| 29 typedef std::vector<scoped_refptr<WorkerPoolTask> > TaskVector; |
| 30 |
| 48 // Returns true if |buffer| was written to. False indicate that | 31 // Returns true if |buffer| was written to. False indicate that |
| 49 // the content of |buffer| is undefined and the resource doesn't | 32 // the content of |buffer| is undefined and the resource doesn't |
| 50 // need to be initialized. | 33 // need to be initialized. |
| 51 virtual bool RunOnWorkerThread(unsigned thread_index, | 34 virtual bool RunOnWorkerThread(unsigned thread_index, |
| 52 void* buffer, | 35 void* buffer, |
| 53 gfx::Size size, | 36 gfx::Size size, |
| 54 int stride) = 0; | 37 int stride) = 0; |
| 55 virtual void RunOnOriginThread(ResourceProvider* resource_provider, | 38 virtual void RunOnOriginThread(ResourceProvider* resource_provider, |
| 56 ContextProvider* context_provider) = 0; | 39 ContextProvider* context_provider) = 0; |
| 57 virtual void CompleteOnOriginThread() = 0; | 40 virtual void CompleteOnOriginThread() = 0; |
| 58 | 41 |
| 59 void DidRun(bool was_canceled); | 42 void DidRun(bool was_canceled); |
| 60 bool HasFinishedRunning() const; | 43 bool HasFinishedRunning() const; |
| 61 bool WasCanceled() const; | 44 bool WasCanceled() const; |
| 62 void WillComplete(); | 45 void WillComplete(); |
| 63 void DidComplete(); | 46 void DidComplete(); |
| 64 bool HasCompleted() const; | 47 bool HasCompleted() const; |
| 65 | 48 |
| 66 const Resource* resource() const { return resource_; } | 49 const Resource* resource() const { return resource_; } |
| 67 const internal::Task::Vector& dependencies() const { return dependencies_; } | 50 const TaskVector& dependencies() const { return dependencies_; } |
| 68 bool use_gpu_rasterization() const { return use_gpu_rasterization_; } | 51 bool use_gpu_rasterization() const { return use_gpu_rasterization_; } |
| 69 | 52 |
| 70 protected: | 53 protected: |
| 71 friend class base::RefCounted<RasterWorkerPoolTask>; | 54 friend class base::RefCounted<RasterWorkerPoolTask>; |
| 72 | 55 |
| 73 RasterWorkerPoolTask(const Resource* resource, | 56 RasterWorkerPoolTask(const Resource* resource, |
| 74 internal::Task::Vector* dependencies, | 57 TaskVector* dependencies, |
| 75 bool use_gpu_rasterization); | 58 bool use_gpu_rasterization); |
| 76 virtual ~RasterWorkerPoolTask(); | 59 virtual ~RasterWorkerPoolTask(); |
| 77 | 60 |
| 78 private: | 61 private: |
| 79 bool did_run_; | 62 bool did_run_; |
| 80 bool did_complete_; | 63 bool did_complete_; |
| 81 bool was_canceled_; | 64 bool was_canceled_; |
| 82 const Resource* resource_; | 65 const Resource* resource_; |
| 83 Task::Vector dependencies_; | 66 TaskVector dependencies_; |
| 84 bool use_gpu_rasterization_; | 67 bool use_gpu_rasterization_; |
| 85 }; | 68 }; |
| 86 | 69 |
| 87 } // namespace internal | 70 } // namespace internal |
| 88 } // namespace cc | 71 } // namespace cc |
| 89 | 72 |
| 90 #if defined(COMPILER_GCC) | 73 #if defined(COMPILER_GCC) |
| 91 namespace BASE_HASH_NAMESPACE { | 74 namespace BASE_HASH_NAMESPACE { |
| 92 template <> struct hash<cc::internal::RasterWorkerPoolTask*> { | 75 template <> struct hash<cc::internal::RasterWorkerPoolTask*> { |
| 93 size_t operator()(cc::internal::RasterWorkerPoolTask* ptr) const { | 76 size_t operator()(cc::internal::RasterWorkerPoolTask* ptr) const { |
| 94 return hash<size_t>()(reinterpret_cast<size_t>(ptr)); | 77 return hash<size_t>()(reinterpret_cast<size_t>(ptr)); |
| 95 } | 78 } |
| 96 }; | 79 }; |
| 97 } // namespace BASE_HASH_NAMESPACE | 80 } // namespace BASE_HASH_NAMESPACE |
| 98 #endif // COMPILER | 81 #endif // COMPILER |
| 99 | 82 |
| 100 namespace cc { | 83 namespace cc { |
| 101 | 84 |
| 102 class CC_EXPORT RasterWorkerPoolClient { | 85 class CC_EXPORT RasterWorkerPoolClient { |
| 103 public: | 86 public: |
| 104 virtual bool ShouldForceTasksRequiredForActivationToComplete() const = 0; | 87 virtual bool ShouldForceTasksRequiredForActivationToComplete() const = 0; |
| 105 virtual void DidFinishRunningTasks() = 0; | 88 virtual void DidFinishRunningTasks() = 0; |
| 106 virtual void DidFinishRunningTasksRequiredForActivation() = 0; | 89 virtual void DidFinishRunningTasksRequiredForActivation() = 0; |
| 107 | 90 |
| 108 protected: | 91 protected: |
| 109 virtual ~RasterWorkerPoolClient() {} | 92 virtual ~RasterWorkerPoolClient() {} |
| 110 }; | 93 }; |
| 111 | 94 |
| 112 // A worker thread pool that runs raster tasks. | 95 // A worker thread pool that runs raster tasks. |
| 113 class CC_EXPORT RasterWorkerPool { | 96 class CC_EXPORT RasterWorkerPool : public WorkerPool { |
| 114 public: | 97 public: |
| 115 class CC_EXPORT Task { | 98 class CC_EXPORT Task { |
| 116 public: | 99 public: |
| 117 typedef base::Callback<void(bool was_canceled)> Reply; | 100 typedef base::Callback<void(bool was_canceled)> Reply; |
| 118 | 101 |
| 119 class CC_EXPORT Set { | 102 class CC_EXPORT Set { |
| 120 public: | 103 public: |
| 121 Set(); | 104 Set(); |
| 122 ~Set(); | 105 ~Set(); |
| 123 | 106 |
| 124 void Insert(const Task& task); | 107 void Insert(const Task& task); |
| 125 | 108 |
| 126 private: | 109 private: |
| 127 friend class RasterWorkerPool; | 110 friend class RasterWorkerPool; |
| 128 friend class RasterWorkerPoolTest; | 111 friend class RasterWorkerPoolTest; |
| 129 | 112 |
| 130 internal::Task::Vector tasks_; | 113 typedef internal::RasterWorkerPoolTask::TaskVector TaskVector; |
| 114 TaskVector tasks_; |
| 131 }; | 115 }; |
| 132 | 116 |
| 133 Task(); | 117 Task(); |
| 134 ~Task(); | 118 ~Task(); |
| 135 | 119 |
| 136 // Returns true if Task is null (doesn't refer to anything). | 120 // Returns true if Task is null (doesn't refer to anything). |
| 137 bool is_null() const { return !internal_.get(); } | 121 bool is_null() const { return !internal_.get(); } |
| 138 | 122 |
| 139 // Returns the Task into an uninitialized state. | 123 // Returns the Task into an uninitialized state. |
| 140 void Reset(); | 124 void Reset(); |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 183 friend class RasterWorkerPool; | 167 friend class RasterWorkerPool; |
| 184 friend class RasterWorkerPoolTest; | 168 friend class RasterWorkerPoolTest; |
| 185 | 169 |
| 186 explicit RasterTask(internal::RasterWorkerPoolTask* internal); | 170 explicit RasterTask(internal::RasterWorkerPoolTask* internal); |
| 187 | 171 |
| 188 scoped_refptr<internal::RasterWorkerPoolTask> internal_; | 172 scoped_refptr<internal::RasterWorkerPoolTask> internal_; |
| 189 }; | 173 }; |
| 190 | 174 |
| 191 virtual ~RasterWorkerPool(); | 175 virtual ~RasterWorkerPool(); |
| 192 | 176 |
| 193 static void SetNumRasterThreads(int num_threads); | 177 void SetClient(RasterWorkerPoolClient* client); |
| 194 | 178 |
| 195 static int GetNumRasterThreads(); | 179 // Overidden from WorkerPool: |
| 180 virtual void Shutdown() OVERRIDE; |
| 181 |
| 182 // Schedule running of raster tasks in |queue| and all dependencies. |
| 183 // Previously scheduled tasks that are no longer needed to run |
| 184 // raster tasks in |queue| will be canceled unless already running. |
| 185 // Once scheduled, reply callbacks are guaranteed to run for all tasks |
| 186 // even if they later get canceled by another call to ScheduleTasks(). |
| 187 virtual void ScheduleTasks(RasterTask::Queue* queue) = 0; |
| 188 |
| 189 // Returns the target that needs to be used for raster task resources. |
| 190 virtual GLenum GetResourceTarget() const = 0; |
| 191 |
| 192 // Returns the format that needs to be used for raster task resources. |
| 193 virtual ResourceFormat GetResourceFormat() const = 0; |
| 194 |
| 195 // Force a check for completed raster tasks. |
| 196 // Calls completion callbacks on completed tasks. |
| 197 virtual void CheckForCompletedTasks(); |
| 196 | 198 |
| 197 // TODO(vmpstr): Figure out an elegant way to not pass this many parameters. | 199 // TODO(vmpstr): Figure out an elegant way to not pass this many parameters. |
| 198 static RasterTask CreateRasterTask( | 200 static RasterTask CreateRasterTask( |
| 199 const Resource* resource, | 201 const Resource* resource, |
| 200 PicturePileImpl* picture_pile, | 202 PicturePileImpl* picture_pile, |
| 201 const gfx::Rect& content_rect, | 203 const gfx::Rect& content_rect, |
| 202 float contents_scale, | 204 float contents_scale, |
| 203 RasterMode raster_mode, | 205 RasterMode raster_mode, |
| 204 TileResolution tile_resolution, | 206 TileResolution tile_resolution, |
| 205 int layer_id, | 207 int layer_id, |
| 206 const void* tile_id, | 208 const void* tile_id, |
| 207 int source_frame_number, | 209 int source_frame_number, |
| 208 bool use_gpu_rasterization, | 210 bool use_gpu_rasterization, |
| 209 RenderingStatsInstrumentation* rendering_stats, | 211 RenderingStatsInstrumentation* rendering_stats, |
| 210 const RasterTask::Reply& reply, | 212 const RasterTask::Reply& reply, |
| 211 Task::Set* dependencies); | 213 Task::Set* dependencies); |
| 212 | 214 |
| 213 static Task CreateImageDecodeTask( | 215 static Task CreateImageDecodeTask( |
| 214 SkPixelRef* pixel_ref, | 216 SkPixelRef* pixel_ref, |
| 215 int layer_id, | 217 int layer_id, |
| 216 RenderingStatsInstrumentation* stats_instrumentation, | 218 RenderingStatsInstrumentation* stats_instrumentation, |
| 217 const Task::Reply& reply); | 219 const Task::Reply& reply); |
| 218 | 220 |
| 219 void SetClient(RasterWorkerPoolClient* client); | |
| 220 | |
| 221 // Tells the worker pool to shutdown after canceling all previously | |
| 222 // scheduled tasks. Reply callbacks are still guaranteed to run. | |
| 223 virtual void Shutdown(); | |
| 224 | |
| 225 // Schedule running of raster tasks in |queue| and all dependencies. | |
| 226 // Previously scheduled tasks that are no longer needed to run | |
| 227 // raster tasks in |queue| will be canceled unless already running. | |
| 228 // Once scheduled, reply callbacks are guaranteed to run for all tasks | |
| 229 // even if they later get canceled by another call to ScheduleTasks(). | |
| 230 virtual void ScheduleTasks(RasterTask::Queue* queue) = 0; | |
| 231 | |
| 232 // Force a check for completed tasks. | |
| 233 virtual void CheckForCompletedTasks(); | |
| 234 | |
| 235 // Returns the target that needs to be used for raster task resources. | |
| 236 virtual unsigned GetResourceTarget() const = 0; | |
| 237 | |
| 238 // Returns the format that needs to be used for raster task resources. | |
| 239 virtual ResourceFormat GetResourceFormat() const = 0; | |
| 240 | |
| 241 protected: | 221 protected: |
| 242 typedef internal::TaskGraphRunner::TaskGraph TaskGraph; | |
| 243 typedef std::vector<scoped_refptr<internal::WorkerPoolTask> > TaskVector; | 222 typedef std::vector<scoped_refptr<internal::WorkerPoolTask> > TaskVector; |
| 244 typedef std::vector<scoped_refptr<internal::RasterWorkerPoolTask> > | 223 typedef std::vector<scoped_refptr<internal::RasterWorkerPoolTask> > |
| 245 RasterTaskVector; | 224 RasterTaskVector; |
| 246 typedef std::deque<scoped_refptr<internal::RasterWorkerPoolTask> > | 225 typedef std::deque<scoped_refptr<internal::RasterWorkerPoolTask> > |
| 247 RasterTaskDeque; | 226 RasterTaskDeque; |
| 248 typedef base::hash_set<internal::RasterWorkerPoolTask*> RasterTaskSet; | 227 typedef base::hash_set<internal::RasterWorkerPoolTask*> RasterTaskSet; |
| 249 typedef internal::RasterWorkerPoolTask* TaskMapKey; | 228 typedef internal::RasterWorkerPoolTask* TaskMapKey; |
| 250 typedef base::hash_map<TaskMapKey, scoped_refptr<internal::WorkerPoolTask> > | 229 typedef base::hash_map<TaskMapKey, |
| 251 TaskMap; | 230 scoped_refptr<internal::WorkerPoolTask> > TaskMap; |
| 252 | 231 |
| 253 RasterWorkerPool(ResourceProvider* resource_provider, | 232 RasterWorkerPool(ResourceProvider* resource_provider, |
| 254 ContextProvider* context_provider); | 233 ContextProvider* context_provider); |
| 255 | 234 |
| 256 virtual void OnRasterTasksFinished() = 0; | 235 virtual void OnRasterTasksFinished() = 0; |
| 257 virtual void OnRasterTasksRequiredForActivationFinished() = 0; | 236 virtual void OnRasterTasksRequiredForActivationFinished() = 0; |
| 258 | 237 |
| 259 void CheckForCompletedWorkerPoolTasks(); | |
| 260 void SetTaskGraph(TaskGraph* graph); | |
| 261 | |
| 262 void SetRasterTasks(RasterTask::Queue* queue); | 238 void SetRasterTasks(RasterTask::Queue* queue); |
| 263 bool IsRasterTaskRequiredForActivation( | 239 bool IsRasterTaskRequiredForActivation( |
| 264 internal::RasterWorkerPoolTask* task) const; | 240 internal::RasterWorkerPoolTask* task) const; |
| 265 // Run raster tasks that use GPU on current thread. | 241 // Run raster tasks that use GPU on current thread. |
| 266 void RunGpuRasterTasks(const RasterTaskVector& tasks); | 242 void RunGpuRasterTasks(const RasterTaskVector& tasks); |
| 267 | 243 |
| 268 RasterWorkerPoolClient* client() const { return client_; } | 244 RasterWorkerPoolClient* client() const { return client_; } |
| 269 ResourceProvider* resource_provider() const { return resource_provider_; } | 245 ResourceProvider* resource_provider() const { return resource_provider_; } |
| 270 ContextProvider* context_provider() const { return context_provider_; } | 246 ContextProvider* context_provider() const { return context_provider_; } |
| 271 const RasterTaskVector& raster_tasks() const { return raster_tasks_; } | 247 const RasterTaskVector& raster_tasks() const { return raster_tasks_; } |
| 272 const RasterTaskSet& raster_tasks_required_for_activation() const { | 248 const RasterTaskSet& raster_tasks_required_for_activation() const { |
| 273 return raster_tasks_required_for_activation_; | 249 return raster_tasks_required_for_activation_; |
| 274 } | 250 } |
| 275 void set_raster_finished_task( | 251 void set_raster_finished_task( |
| 276 internal::WorkerPoolTask* raster_finished_task) { | 252 scoped_refptr<internal::WorkerPoolTask> raster_finished_task) { |
| 277 raster_finished_task_ = raster_finished_task; | 253 raster_finished_task_ = raster_finished_task; |
| 278 } | 254 } |
| 279 void set_raster_required_for_activation_finished_task( | 255 void set_raster_required_for_activation_finished_task( |
| 280 internal::WorkerPoolTask* raster_required_for_activation_finished_task) { | 256 scoped_refptr<internal::WorkerPoolTask> |
| 257 raster_required_for_activation_finished_task) { |
| 281 raster_required_for_activation_finished_task_ = | 258 raster_required_for_activation_finished_task_ = |
| 282 raster_required_for_activation_finished_task; | 259 raster_required_for_activation_finished_task; |
| 283 } | 260 } |
| 284 | 261 |
| 285 scoped_refptr<internal::WorkerPoolTask> CreateRasterFinishedTask(); | 262 scoped_refptr<internal::WorkerPoolTask> CreateRasterFinishedTask(); |
| 286 scoped_refptr<internal::WorkerPoolTask> | 263 scoped_refptr<internal::WorkerPoolTask> |
| 287 CreateRasterRequiredForActivationFinishedTask( | 264 CreateRasterRequiredForActivationFinishedTask( |
| 288 size_t tasks_required_for_activation_count); | 265 size_t tasks_required_for_activation_count); |
| 289 | 266 |
| 290 scoped_ptr<base::Value> ScheduledStateAsValue() const; | 267 scoped_ptr<base::Value> ScheduledStateAsValue() const; |
| 291 | 268 |
| 292 static internal::GraphNode* CreateGraphNodeForTask( | 269 static internal::GraphNode* CreateGraphNodeForTask( |
| 293 internal::WorkerPoolTask* task, | 270 internal::WorkerPoolTask* task, |
| 294 unsigned priority, | 271 unsigned priority, |
| 295 TaskGraph* graph); | 272 TaskGraph* graph); |
| 296 | 273 |
| 297 static internal::GraphNode* CreateGraphNodeForRasterTask( | 274 static internal::GraphNode* CreateGraphNodeForRasterTask( |
| 298 internal::WorkerPoolTask* raster_task, | 275 internal::WorkerPoolTask* raster_task, |
| 299 const internal::Task::Vector& decode_tasks, | 276 const TaskVector& decode_tasks, |
| 300 unsigned priority, | 277 unsigned priority, |
| 301 TaskGraph* graph); | 278 TaskGraph* graph); |
| 302 | 279 |
| 303 private: | 280 private: |
| 304 void OnRasterFinished(const internal::WorkerPoolTask* source); | 281 void OnRasterFinished(const internal::WorkerPoolTask* source); |
| 305 void OnRasterRequiredForActivationFinished( | 282 void OnRasterRequiredForActivationFinished( |
| 306 const internal::WorkerPoolTask* source); | 283 const internal::WorkerPoolTask* source); |
| 307 | 284 |
| 308 internal::NamespaceToken namespace_token_; | |
| 309 RasterWorkerPoolClient* client_; | 285 RasterWorkerPoolClient* client_; |
| 310 ResourceProvider* resource_provider_; | 286 ResourceProvider* resource_provider_; |
| 311 ContextProvider* context_provider_; | 287 ContextProvider* context_provider_; |
| 312 RasterTask::Queue::TaskVector raster_tasks_; | 288 RasterTask::Queue::TaskVector raster_tasks_; |
| 313 RasterTask::Queue::TaskSet raster_tasks_required_for_activation_; | 289 RasterTask::Queue::TaskSet raster_tasks_required_for_activation_; |
| 314 RasterTaskDeque completed_gpu_raster_tasks_; | 290 RasterTaskDeque completed_gpu_raster_tasks_; |
| 315 | 291 |
| 316 scoped_refptr<internal::WorkerPoolTask> raster_finished_task_; | 292 scoped_refptr<internal::WorkerPoolTask> raster_finished_task_; |
| 317 scoped_refptr<internal::WorkerPoolTask> | 293 scoped_refptr<internal::WorkerPoolTask> |
| 318 raster_required_for_activation_finished_task_; | 294 raster_required_for_activation_finished_task_; |
| 319 base::WeakPtrFactory<RasterWorkerPool> weak_ptr_factory_; | 295 base::WeakPtrFactory<RasterWorkerPool> weak_ptr_factory_; |
| 320 }; | 296 }; |
| 321 | 297 |
| 322 } // namespace cc | 298 } // namespace cc |
| 323 | 299 |
| 324 #endif // CC_RESOURCES_RASTER_WORKER_POOL_H_ | 300 #endif // CC_RESOURCES_RASTER_WORKER_POOL_H_ |
| OLD | NEW |