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