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