1 // Copyright 2014 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_RASTERIZER_H_ 6 #define CC_RESOURCES_RASTERIZER_H_ 7 8 #include <vector> 9 10 #include "base/callback.h" 11 #include "cc/resources/resource_format.h" 12 #include "cc/resources/task_graph_runner.h" 13 14 class SkCanvas; 15 16 namespace cc { 17 class ImageDecodeTask; 18 class RasterTask; 19 class Resource; 20 21 class CC_EXPORT RasterizerTaskClient { 22 public: 23 virtual SkCanvas* AcquireCanvasForRaster(RasterTask* task) = 0; 24 virtual void ReleaseCanvasForRaster(RasterTask* task) = 0; 25 26 protected: ~RasterizerTaskClient()27 virtual ~RasterizerTaskClient() {} 28 }; 29 30 class CC_EXPORT RasterizerTask : public Task { 31 public: 32 typedef std::vector<scoped_refptr<RasterizerTask> > Vector; 33 34 virtual void ScheduleOnOriginThread(RasterizerTaskClient* client) = 0; 35 virtual void CompleteOnOriginThread(RasterizerTaskClient* client) = 0; 36 virtual void RunReplyOnOriginThread() = 0; 37 38 // Type-checking downcast routines. 39 virtual ImageDecodeTask* AsImageDecodeTask(); 40 virtual RasterTask* AsRasterTask(); 41 42 void WillSchedule(); 43 void DidSchedule(); 44 bool HasBeenScheduled() const; 45 46 void WillComplete(); 47 void DidComplete(); 48 bool HasCompleted() const; 49 50 protected: 51 RasterizerTask(); 52 virtual ~RasterizerTask(); 53 54 bool did_schedule_; 55 bool did_complete_; 56 }; 57 58 class CC_EXPORT ImageDecodeTask : public RasterizerTask { 59 public: 60 typedef std::vector<scoped_refptr<ImageDecodeTask> > Vector; 61 62 // Overridden from RasterizerTask: 63 virtual ImageDecodeTask* AsImageDecodeTask() OVERRIDE; 64 65 protected: 66 ImageDecodeTask(); 67 virtual ~ImageDecodeTask(); 68 }; 69 70 class CC_EXPORT RasterTask : public RasterizerTask { 71 public: 72 typedef std::vector<scoped_refptr<RasterTask> > Vector; 73 74 // Overridden from RasterizerTask: 75 virtual RasterTask* AsRasterTask() OVERRIDE; 76 resource()77 const Resource* resource() const { return resource_; } dependencies()78 const ImageDecodeTask::Vector& dependencies() const { return dependencies_; } 79 80 protected: 81 RasterTask(const Resource* resource, ImageDecodeTask::Vector* dependencies); 82 virtual ~RasterTask(); 83 84 private: 85 const Resource* resource_; 86 ImageDecodeTask::Vector dependencies_; 87 }; 88 89 class CC_EXPORT RasterizerClient { 90 public: 91 virtual bool ShouldForceTasksRequiredForActivationToComplete() const = 0; 92 virtual void DidFinishRunningTasks() = 0; 93 virtual void DidFinishRunningTasksRequiredForActivation() = 0; 94 95 protected: ~RasterizerClient()96 virtual ~RasterizerClient() {} 97 }; 98 99 struct CC_EXPORT RasterTaskQueue { 100 struct CC_EXPORT Item { 101 class TaskComparator { 102 public: TaskComparatorRasterTaskQueue::Item103 explicit TaskComparator(const RasterTask* task) : task_(task) {} 104 operatorRasterTaskQueue::Item105 bool operator()(const Item& item) const { return item.task == task_; } 106 107 private: 108 const RasterTask* task_; 109 }; 110 111 typedef std::vector<Item> Vector; 112 113 Item(RasterTask* task, bool required_for_activation); 114 ~Item(); 115 IsRequiredForActivationRasterTaskQueue::Item116 static bool IsRequiredForActivation(const Item& item) { 117 return item.required_for_activation; 118 } 119 120 RasterTask* task; 121 bool required_for_activation; 122 }; 123 124 RasterTaskQueue(); 125 ~RasterTaskQueue(); 126 127 void Swap(RasterTaskQueue* other); 128 void Reset(); 129 130 Item::Vector items; 131 size_t required_for_activation_count; 132 }; 133 134 // This interface can be used to schedule and run raster tasks. The client will 135 // be notified asynchronously when the set of tasks marked as "required for 136 // activation" have finished running and when all scheduled tasks have finished 137 // running. The client can call CheckForCompletedTasks() at any time to dispatch 138 // pending completion callbacks for all tasks that have finished running. 139 class CC_EXPORT Rasterizer { 140 public: 141 // Set the client instance to be notified when finished running tasks. 142 virtual void SetClient(RasterizerClient* client) = 0; 143 144 // Tells the worker pool to shutdown after canceling all previously scheduled 145 // tasks. Reply callbacks are still guaranteed to run when 146 // CheckForCompletedTasks() is called. 147 virtual void Shutdown() = 0; 148 149 // Schedule running of raster tasks in |queue| and all dependencies. 150 // Previously scheduled tasks that are not in |queue| will be canceled unless 151 // already running. Once scheduled, reply callbacks are guaranteed to run for 152 // all tasks even if they later get canceled by another call to 153 // ScheduleTasks(). 154 virtual void ScheduleTasks(RasterTaskQueue* queue) = 0; 155 156 // Check for completed tasks and dispatch reply callbacks. 157 virtual void CheckForCompletedTasks() = 0; 158 159 protected: ~Rasterizer()160 virtual ~Rasterizer() {} 161 }; 162 163 } // namespace cc 164 165 #endif // CC_RESOURCES_RASTERIZER_H_ 166