| 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 <vector> |
| 9 |
| 10 #include "base/hash_tables.h" |
| 8 #include "cc/base/worker_pool.h" | 11 #include "cc/base/worker_pool.h" |
| 12 #include "cc/resources/resource_provider.h" |
| 9 | 13 |
| 10 namespace cc { | 14 namespace cc { |
| 11 class PicturePileImpl; | 15 class PicturePileImpl; |
| 16 class PixelBufferRasterWorkerPool; |
| 17 class Resource; |
| 18 |
| 19 namespace internal { |
| 20 |
| 21 class CC_EXPORT RasterWorkerPoolTask |
| 22 : public base::RefCounted<RasterWorkerPoolTask> { |
| 23 public: |
| 24 typedef std::vector<scoped_refptr<RasterWorkerPoolTask> > TaskVector; |
| 25 |
| 26 virtual bool RunOnThread(uint8_t* buffer, unsigned thread_index) = 0; |
| 27 virtual void DispatchCompletionCallback() = 0; |
| 28 |
| 29 void DidRun(); |
| 30 bool HasFinishedRunning() const; |
| 31 void DidComplete(); |
| 32 bool HasCompleted() const; |
| 33 |
| 34 const Resource* resource() const { return resource_; } |
| 35 const WorkerPoolTask::TaskVector& dependencies() const { |
| 36 return dependencies_; |
| 37 } |
| 38 |
| 39 protected: |
| 40 friend class base::RefCounted<RasterWorkerPoolTask>; |
| 41 |
| 42 RasterWorkerPoolTask(const Resource* resource, |
| 43 WorkerPoolTask::TaskVector* dependencies); |
| 44 virtual ~RasterWorkerPoolTask(); |
| 45 |
| 46 private: |
| 47 bool did_run_; |
| 48 bool did_complete_; |
| 49 const Resource* resource_; |
| 50 WorkerPoolTask::TaskVector dependencies_; |
| 51 }; |
| 52 |
| 53 } // namespace internal |
| 54 } // namespace cc |
| 55 |
| 56 #if defined(COMPILER_GCC) |
| 57 namespace BASE_HASH_NAMESPACE { |
| 58 template <> struct hash<cc::internal::RasterWorkerPoolTask*> { |
| 59 size_t operator()(cc::internal::RasterWorkerPoolTask* ptr) const { |
| 60 return hash<size_t>()(reinterpret_cast<size_t>(ptr)); |
| 61 } |
| 62 }; |
| 63 } // namespace BASE_HASH_NAMESPACE |
| 64 #endif // COMPILER |
| 65 |
| 66 namespace cc { |
| 12 | 67 |
| 13 // A worker thread pool that runs raster tasks. | 68 // A worker thread pool that runs raster tasks. |
| 14 class CC_EXPORT RasterWorkerPool : public WorkerPool { | 69 class CC_EXPORT RasterWorkerPool : public WorkerPool { |
| 15 public: | 70 public: |
| 16 class Task { | 71 class Task { |
| 17 public: | 72 public: |
| 18 typedef base::Callback<void(bool)> Reply; | 73 class Set { |
| 74 public: |
| 75 typedef internal::WorkerPoolTask::TaskVector TaskVector; |
| 19 | 76 |
| 20 // Highest priority task first. Order of execution is not guaranteed. | 77 Set(); |
| 21 class Queue { | 78 ~Set(); |
| 22 public: | |
| 23 Queue(); | |
| 24 ~Queue(); | |
| 25 | 79 |
| 26 bool empty() const { return tasks_.empty(); } | 80 void Insert(const Task& task); |
| 27 | |
| 28 // Add task to the back of the queue. | |
| 29 void Append(const Task& task); | |
| 30 | 81 |
| 31 private: | 82 private: |
| 32 friend class RasterWorkerPool; | 83 friend class RasterWorkerPool; |
| 33 | 84 |
| 34 internal::WorkerPoolTask::TaskVector tasks_; | 85 TaskVector tasks_; |
| 35 }; | 86 }; |
| 36 | 87 |
| 37 Task(); | 88 Task(); |
| 38 Task(const base::Closure& callback, const Reply& reply); | 89 Task(const base::Closure& callback, const base::Closure& reply); |
| 39 explicit Task(Queue* dependencies); | |
| 40 ~Task(); | 90 ~Task(); |
| 41 | 91 |
| 42 // Returns true if Task is null (doesn't refer to anything). | 92 // Returns true if Task is null (doesn't refer to anything). |
| 43 bool is_null() const { return !internal_; } | 93 bool is_null() const { return !internal_; } |
| 44 | 94 |
| 45 // Returns the Task into an uninitialized state. | 95 // Returns the Task into an uninitialized state. |
| 46 void Reset(); | 96 void Reset(); |
| 47 | 97 |
| 48 protected: | 98 protected: |
| 49 friend class RasterWorkerPool; | 99 friend class RasterWorkerPool; |
| 50 | 100 |
| 51 explicit Task(scoped_refptr<internal::WorkerPoolTask> internal); | |
| 52 | |
| 53 scoped_refptr<internal::WorkerPoolTask> internal_; | 101 scoped_refptr<internal::WorkerPoolTask> internal_; |
| 54 }; | 102 }; |
| 55 | 103 |
| 56 class PictureTask : public Task { | 104 class RasterTask { |
| 57 public: | 105 public: |
| 58 typedef base::Callback<void(PicturePileImpl*)> Callback; | 106 // Returns true if buffer was written to. False indicate that |
| 107 // buffer is not needed to initialize the resource. A ResourceInitializer |
| 108 // instance can use this to determine if an upload is necessary. |
| 109 typedef base::Callback<bool(uint8_t*, PicturePileImpl*)> Callback; |
| 59 | 110 |
| 60 PictureTask(PicturePileImpl* picture_pile, | 111 // First callback parameter is true when task was canceled. |
| 61 const Callback& callback, | 112 typedef base::Callback<void(bool)> Reply; |
| 62 const Reply& reply, | 113 |
| 63 Queue* dependencies); | 114 class Queue { |
| 115 public: |
| 116 typedef internal::RasterWorkerPoolTask::TaskVector TaskVector; |
| 117 |
| 118 Queue(); |
| 119 ~Queue(); |
| 120 |
| 121 void Append(const RasterTask& task); |
| 122 |
| 123 private: |
| 124 friend class RasterWorkerPool; |
| 125 |
| 126 TaskVector tasks_; |
| 127 }; |
| 128 |
| 129 RasterTask(); |
| 130 RasterTask(PicturePileImpl* picture_pile, |
| 131 const Resource* resource, |
| 132 const Callback& callback, |
| 133 const Reply& reply, |
| 134 Task::Set* dependencies); |
| 135 ~RasterTask(); |
| 136 |
| 137 // Returns true if Task is null (doesn't refer to anything). |
| 138 bool is_null() const { return !internal_; } |
| 139 |
| 140 // Returns the Task into an uninitialized state. |
| 141 void Reset(); |
| 142 |
| 143 protected: |
| 144 friend class PixelBufferRasterWorkerPool; |
| 145 friend class RasterWorkerPool; |
| 146 |
| 147 scoped_refptr<internal::RasterWorkerPoolTask> internal_; |
| 64 }; | 148 }; |
| 65 | 149 |
| 66 virtual ~RasterWorkerPool(); | 150 virtual ~RasterWorkerPool(); |
| 67 | 151 |
| 68 static scoped_ptr<RasterWorkerPool> Create(size_t num_threads) { | |
| 69 return make_scoped_ptr(new RasterWorkerPool(num_threads)); | |
| 70 } | |
| 71 | |
| 72 // Tells the worker pool to shutdown after canceling all previously | 152 // Tells the worker pool to shutdown after canceling all previously |
| 73 // scheduled tasks. Reply callbacks are still guaranteed to run. | 153 // scheduled tasks. Reply callbacks are still guaranteed to run. |
| 74 virtual void Shutdown() OVERRIDE; | 154 virtual void Shutdown() OVERRIDE; |
| 75 | 155 |
| 76 // Schedule running of |root| task and all its dependencies. Tasks | 156 // Schedule running of raster tasks in |queue| and all dependencies. |
| 77 // previously scheduled but no longer needed to run |root| will be | 157 // Previously scheduled tasks that are no longer needed to run |
| 78 // canceled unless already running. Once scheduled, reply callbacks | 158 // raster tasks in |queue| will be canceled unless already running. |
| 79 // are guaranteed to run for all tasks even if they later get | 159 // Once scheduled, reply callbacks are guaranteed to run for all tasks |
| 80 // canceled by another call to ScheduleTasks(). | 160 // even if they later get canceled by another call to ScheduleTasks(). |
| 81 void ScheduleTasks(Task* root); | 161 virtual void ScheduleTasks(RasterTask::Queue* queue); |
| 82 | 162 |
| 83 private: | 163 // Tells the raster worker pool to force any pending uploads for |
| 84 explicit RasterWorkerPool(size_t num_threads); | 164 // |raster_task| to complete. Returns true when successful. |
| 165 virtual bool ForceUploadToComplete(const RasterTask& raster_task); |
| 85 | 166 |
| 86 DISALLOW_COPY_AND_ASSIGN(RasterWorkerPool); | 167 protected: |
| 168 typedef internal::RasterWorkerPoolTask* TaskMapKey; |
| 169 typedef base::hash_map<TaskMapKey, |
| 170 scoped_refptr<internal::WorkerPoolTask> > TaskMap; |
| 171 |
| 172 RasterWorkerPool(ResourceProvider* resource_provider, size_t num_threads); |
| 173 |
| 174 void SetRasterTasks(RasterTask::Queue* queue); |
| 175 void ScheduleRasterTasks(internal::WorkerPoolTask::TaskVector* raster_tasks); |
| 176 |
| 177 ResourceProvider* resource_provider_; |
| 178 RasterTask::Queue::TaskVector raster_tasks_; |
| 87 }; | 179 }; |
| 88 | 180 |
| 89 } // namespace cc | 181 } // namespace cc |
| 90 | 182 |
| 91 #endif // CC_RESOURCES_RASTER_WORKER_POOL_H_ | 183 #endif // CC_RESOURCES_RASTER_WORKER_POOL_H_ |
| OLD | NEW |