OLD | NEW |
---|---|
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 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_TILE_MANAGER_H_ | 5 #ifndef CC_RESOURCES_TILE_MANAGER_H_ |
6 #define CC_RESOURCES_TILE_MANAGER_H_ | 6 #define CC_RESOURCES_TILE_MANAGER_H_ |
7 | 7 |
8 #include <deque> | 8 #include <deque> |
9 #include <queue> | 9 #include <queue> |
10 #include <set> | 10 #include <set> |
11 #include <utility> | 11 #include <utility> |
12 #include <vector> | 12 #include <vector> |
13 | 13 |
14 #include "base/containers/hash_tables.h" | 14 #include "base/containers/hash_tables.h" |
15 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
16 #include "base/values.h" | 16 #include "base/values.h" |
17 #include "cc/base/ref_counted_managed.h" | 17 #include "cc/base/ref_counted_managed.h" |
18 #include "cc/base/unique_notifier.h" | 18 #include "cc/base/unique_notifier.h" |
19 #include "cc/debug/rendering_stats_instrumentation.h" | 19 #include "cc/debug/rendering_stats_instrumentation.h" |
20 #include "cc/layers/picture_layer_impl.h" | |
21 #include "cc/resources/managed_tile_state.h" | 20 #include "cc/resources/managed_tile_state.h" |
22 #include "cc/resources/memory_history.h" | 21 #include "cc/resources/memory_history.h" |
23 #include "cc/resources/picture_pile_impl.h" | 22 #include "cc/resources/picture_pile_impl.h" |
24 #include "cc/resources/prioritized_tile_set.h" | |
25 #include "cc/resources/rasterizer.h" | 23 #include "cc/resources/rasterizer.h" |
26 #include "cc/resources/resource_pool.h" | 24 #include "cc/resources/resource_pool.h" |
27 #include "cc/resources/tile.h" | 25 #include "cc/resources/tile.h" |
26 #include "cc/resources/tile_priority_queue.h" | |
28 | 27 |
29 namespace cc { | 28 namespace cc { |
29 class PictureLayerImpl; | |
30 class ResourceProvider; | 30 class ResourceProvider; |
31 | 31 |
32 class CC_EXPORT TileManagerClient { | 32 class CC_EXPORT TileManagerClient { |
33 public: | 33 public: |
34 // Returns the set of layers that the tile manager should consider for raster. | 34 // Returns the set of layers that the tile manager should consider for raster. |
35 virtual const std::vector<PictureLayerImpl*>& GetPictureLayers() = 0; | 35 // TODO(vmpstr): Change the way we determine if we are ready to activate, so |
36 // that this can be removed. | |
37 virtual const std::vector<PictureLayerImpl*>& GetPictureLayers() const = 0; | |
36 | 38 |
37 // Called when all tiles marked as required for activation are ready to draw. | 39 // Called when all tiles marked as required for activation are ready to draw. |
38 virtual void NotifyReadyToActivate() = 0; | 40 virtual void NotifyReadyToActivate() = 0; |
39 | 41 |
40 // Called when the visible representation of a tile might have changed. Some | 42 // Called when the visible representation of a tile might have changed. Some |
41 // examples are: | 43 // examples are: |
42 // - Tile version initialized. | 44 // - Tile version initialized. |
43 // - Tile resources freed. | 45 // - Tile resources freed. |
44 // - Tile marked for on-demand raster. | 46 // - Tile marked for on-demand raster. |
45 virtual void NotifyTileStateChanged(const Tile* tile) = 0; | 47 virtual void NotifyTileStateChanged(const Tile* tile) = 0; |
46 | 48 |
49 virtual TilePriorityQueue* RebuildRasterQueue(TreePriority tree_priority) = 0; | |
50 virtual TilePriorityQueue* RebuildEvictionQueue( | |
51 TreePriority tree_priority) = 0; | |
reveman
2014/07/14 15:52:38
This API is not perfectly clear. What happens if I
vmpstr
2014/07/14 18:10:44
Yes, I'd like to avoid allocations of a new queue.
reveman
2014/07/14 19:09:46
We can't maintain a persistent queue with the curr
| |
52 | |
47 protected: | 53 protected: |
48 virtual ~TileManagerClient() {} | 54 virtual ~TileManagerClient() {} |
49 }; | 55 }; |
50 | 56 |
51 struct RasterTaskCompletionStats { | 57 struct RasterTaskCompletionStats { |
52 RasterTaskCompletionStats(); | 58 RasterTaskCompletionStats(); |
53 | 59 |
54 size_t completed_count; | 60 size_t completed_count; |
55 size_t canceled_count; | 61 size_t canceled_count; |
56 }; | 62 }; |
57 scoped_ptr<base::Value> RasterTaskCompletionStatsAsValue( | 63 scoped_ptr<base::Value> RasterTaskCompletionStatsAsValue( |
58 const RasterTaskCompletionStats& stats); | 64 const RasterTaskCompletionStats& stats); |
59 | 65 |
60 // This class manages tiles, deciding which should get rasterized and which | 66 // This class manages tiles, deciding which should get rasterized and which |
61 // should no longer have any memory assigned to them. Tile objects are "owned" | 67 // should no longer have any memory assigned to them. Tile objects are "owned" |
62 // by layers; they automatically register with the manager when they are | 68 // by layers; they automatically register with the manager when they are |
63 // created, and unregister from the manager when they are deleted. | 69 // created, and unregister from the manager when they are deleted. |
64 class CC_EXPORT TileManager : public RasterizerClient, | 70 class CC_EXPORT TileManager : public RasterizerClient, |
65 public RefCountedManager<Tile> { | 71 public RefCountedManager<Tile> { |
66 public: | 72 public: |
67 struct CC_EXPORT PairedPictureLayer { | |
68 PairedPictureLayer(); | |
69 ~PairedPictureLayer(); | |
70 | |
71 PictureLayerImpl* active_layer; | |
72 PictureLayerImpl* pending_layer; | |
73 }; | |
74 | |
75 class CC_EXPORT RasterTileIterator { | |
76 public: | |
77 RasterTileIterator(TileManager* tile_manager, TreePriority tree_priority); | |
78 ~RasterTileIterator(); | |
79 | |
80 RasterTileIterator& operator++(); | |
81 operator bool() const; | |
82 Tile* operator*(); | |
83 | |
84 private: | |
85 struct PairedPictureLayerIterator { | |
86 PairedPictureLayerIterator(); | |
87 ~PairedPictureLayerIterator(); | |
88 | |
89 Tile* PeekTile(TreePriority tree_priority); | |
90 void PopTile(TreePriority tree_priority); | |
91 | |
92 std::pair<PictureLayerImpl::LayerRasterTileIterator*, WhichTree> | |
93 NextTileIterator(TreePriority tree_priority); | |
94 | |
95 PictureLayerImpl::LayerRasterTileIterator active_iterator; | |
96 PictureLayerImpl::LayerRasterTileIterator pending_iterator; | |
97 | |
98 std::vector<Tile*> returned_shared_tiles; | |
99 }; | |
100 | |
101 class RasterOrderComparator { | |
102 public: | |
103 explicit RasterOrderComparator(TreePriority tree_priority); | |
104 | |
105 bool operator()(PairedPictureLayerIterator* a, | |
106 PairedPictureLayerIterator* b) const; | |
107 | |
108 private: | |
109 TreePriority tree_priority_; | |
110 }; | |
111 | |
112 std::vector<PairedPictureLayerIterator> paired_iterators_; | |
113 std::vector<PairedPictureLayerIterator*> iterator_heap_; | |
114 TreePriority tree_priority_; | |
115 RasterOrderComparator comparator_; | |
116 | |
117 DISALLOW_COPY_AND_ASSIGN(RasterTileIterator); | |
118 }; | |
119 | |
120 struct CC_EXPORT EvictionTileIterator { | |
121 public: | |
122 EvictionTileIterator(); | |
123 EvictionTileIterator(TileManager* tile_manager, TreePriority tree_priority); | |
124 ~EvictionTileIterator(); | |
125 | |
126 EvictionTileIterator& operator++(); | |
127 operator bool() const; | |
128 Tile* operator*(); | |
129 | |
130 private: | |
131 struct PairedPictureLayerIterator { | |
132 PairedPictureLayerIterator(); | |
133 ~PairedPictureLayerIterator(); | |
134 | |
135 Tile* PeekTile(TreePriority tree_priority); | |
136 void PopTile(TreePriority tree_priority); | |
137 | |
138 PictureLayerImpl::LayerEvictionTileIterator* NextTileIterator( | |
139 TreePriority tree_priority); | |
140 | |
141 PictureLayerImpl::LayerEvictionTileIterator active_iterator; | |
142 PictureLayerImpl::LayerEvictionTileIterator pending_iterator; | |
143 | |
144 std::vector<Tile*> returned_shared_tiles; | |
145 }; | |
146 | |
147 class EvictionOrderComparator { | |
148 public: | |
149 explicit EvictionOrderComparator(TreePriority tree_priority); | |
150 | |
151 bool operator()(PairedPictureLayerIterator* a, | |
152 PairedPictureLayerIterator* b) const; | |
153 | |
154 private: | |
155 TreePriority tree_priority_; | |
156 }; | |
157 | |
158 std::vector<PairedPictureLayerIterator> paired_iterators_; | |
159 std::vector<PairedPictureLayerIterator*> iterator_heap_; | |
160 TreePriority tree_priority_; | |
161 EvictionOrderComparator comparator_; | |
162 | |
163 DISALLOW_COPY_AND_ASSIGN(EvictionTileIterator); | |
164 }; | |
165 | |
166 static scoped_ptr<TileManager> Create( | 73 static scoped_ptr<TileManager> Create( |
167 TileManagerClient* client, | 74 TileManagerClient* client, |
168 base::SequencedTaskRunner* task_runner, | 75 base::SequencedTaskRunner* task_runner, |
169 ResourcePool* resource_pool, | 76 ResourcePool* resource_pool, |
170 Rasterizer* rasterizer, | 77 Rasterizer* rasterizer, |
171 RenderingStatsInstrumentation* rendering_stats_instrumentation); | 78 RenderingStatsInstrumentation* rendering_stats_instrumentation); |
172 virtual ~TileManager(); | 79 virtual ~TileManager(); |
173 | 80 |
174 void ManageTiles(const GlobalStateThatImpactsTilePriority& state); | 81 void ManageTiles(const GlobalStateThatImpactsTilePriority& state); |
175 | 82 |
176 // Returns true when visible tiles have been initialized. | 83 // Returns true when visible tiles have been initialized. |
177 bool UpdateVisibleTiles(); | 84 bool UpdateVisibleTiles(); |
178 | 85 |
179 scoped_refptr<Tile> CreateTile(PicturePileImpl* picture_pile, | 86 scoped_refptr<Tile> CreateTile(PicturePileImpl* picture_pile, |
180 const gfx::Size& tile_size, | 87 const gfx::Size& tile_size, |
181 const gfx::Rect& content_rect, | 88 const gfx::Rect& content_rect, |
182 const gfx::Rect& opaque_rect, | 89 const gfx::Rect& opaque_rect, |
183 float contents_scale, | 90 float contents_scale, |
184 int layer_id, | 91 int layer_id, |
185 int source_frame_number, | 92 int source_frame_number, |
186 int flags); | 93 int flags); |
187 | 94 |
188 scoped_ptr<base::Value> BasicStateAsValue() const; | 95 scoped_ptr<base::Value> BasicStateAsValue() const; |
189 scoped_ptr<base::Value> AllTilesAsValue() const; | 96 scoped_ptr<base::Value> AllTilesAsValue() const; |
190 const MemoryHistory::Entry& memory_stats_from_last_assign() const { | 97 const MemoryHistory::Entry& memory_stats_from_last_assign() const { |
191 return memory_stats_from_last_assign_; | 98 return memory_stats_from_last_assign_; |
192 } | 99 } |
193 | 100 |
194 void GetPairedPictureLayers(std::vector<PairedPictureLayer>* layers) const; | |
195 | |
196 void InitializeTilesWithResourcesForTesting(const std::vector<Tile*>& tiles) { | 101 void InitializeTilesWithResourcesForTesting(const std::vector<Tile*>& tiles) { |
197 for (size_t i = 0; i < tiles.size(); ++i) { | 102 for (size_t i = 0; i < tiles.size(); ++i) { |
198 ManagedTileState& mts = tiles[i]->managed_state(); | 103 ManagedTileState& mts = tiles[i]->managed_state(); |
199 ManagedTileState::TileVersion& tile_version = | 104 ManagedTileState::TileVersion& tile_version = |
200 mts.tile_versions[HIGH_QUALITY_RASTER_MODE]; | 105 mts.tile_versions[HIGH_QUALITY_RASTER_MODE]; |
201 | 106 |
202 tile_version.resource_ = resource_pool_->AcquireResource(gfx::Size(1, 1)); | 107 tile_version.resource_ = |
203 | 108 resource_pool_->AcquireResource(tiles[i]->size()); |
204 bytes_releasable_ += BytesConsumedIfAllocated(tiles[i]); | |
205 ++resources_releasable_; | |
206 } | 109 } |
207 } | 110 } |
208 | 111 |
209 void ReleaseTileResourcesForTesting(const std::vector<Tile*>& tiles) { | 112 void ReleaseTileResourcesForTesting(const std::vector<Tile*>& tiles) { |
210 for (size_t i = 0; i < tiles.size(); ++i) { | 113 for (size_t i = 0; i < tiles.size(); ++i) { |
211 Tile* tile = tiles[i]; | 114 Tile* tile = tiles[i]; |
212 for (int mode = 0; mode < NUM_RASTER_MODES; ++mode) { | 115 for (int mode = 0; mode < NUM_RASTER_MODES; ++mode) { |
213 FreeResourceForTile(tile, static_cast<RasterMode>(mode)); | 116 FreeResourceForTile(tile, static_cast<RasterMode>(mode)); |
214 } | 117 } |
215 } | 118 } |
216 } | 119 } |
217 | 120 |
218 void SetGlobalStateForTesting( | 121 void SetGlobalStateForTesting( |
219 const GlobalStateThatImpactsTilePriority& state) { | 122 const GlobalStateThatImpactsTilePriority& state) { |
220 // Soft limit is used for resource pool such that | 123 global_state_ = state; |
221 // memory returns to soft limit after going over. | |
222 if (state != global_state_) { | |
223 global_state_ = state; | |
224 prioritized_tiles_dirty_ = true; | |
225 } | |
226 } | 124 } |
227 | 125 |
228 void SetRasterizerForTesting(Rasterizer* rasterizer); | 126 void SetRasterizerForTesting(Rasterizer* rasterizer); |
229 | 127 |
230 void CleanUpReleasedTilesForTesting() { CleanUpReleasedTiles(); } | 128 void CleanUpReleasedTilesForTesting() { CleanUpReleasedTiles(); } |
231 | 129 |
232 protected: | 130 protected: |
233 TileManager(TileManagerClient* client, | 131 TileManager(TileManagerClient* client, |
234 base::SequencedTaskRunner* task_runner, | 132 base::SequencedTaskRunner* task_runner, |
235 ResourcePool* resource_pool, | 133 ResourcePool* resource_pool, |
236 Rasterizer* rasterizer, | 134 Rasterizer* rasterizer, |
237 RenderingStatsInstrumentation* rendering_stats_instrumentation); | 135 RenderingStatsInstrumentation* rendering_stats_instrumentation); |
238 | 136 |
239 // Methods called by Tile | |
240 friend class Tile; | |
241 void DidChangeTilePriority(Tile* tile); | |
242 | |
243 void CleanUpReleasedTiles(); | 137 void CleanUpReleasedTiles(); |
244 | 138 |
245 // Overriden from RefCountedManager<Tile>: | 139 // Overriden from RefCountedManager<Tile>: |
140 friend class Tile; | |
246 virtual void Release(Tile* tile) OVERRIDE; | 141 virtual void Release(Tile* tile) OVERRIDE; |
247 | 142 |
248 // Overriden from RasterizerClient: | 143 // Overriden from RasterizerClient: |
249 virtual bool ShouldForceTasksRequiredForActivationToComplete() const OVERRIDE; | 144 virtual bool ShouldForceTasksRequiredForActivationToComplete() const OVERRIDE; |
250 virtual void DidFinishRunningTasks() OVERRIDE; | 145 virtual void DidFinishRunningTasks() OVERRIDE; |
251 virtual void DidFinishRunningTasksRequiredForActivation() OVERRIDE; | 146 virtual void DidFinishRunningTasksRequiredForActivation() OVERRIDE; |
252 | 147 |
253 typedef std::vector<Tile*> TileVector; | 148 typedef std::vector<Tile*> TileVector; |
254 typedef std::set<Tile*> TileSet; | 149 typedef std::set<Tile*> TileSet; |
255 | 150 |
256 // Virtual for test | 151 // Virtual for test |
257 virtual void ScheduleTasks( | 152 virtual void ScheduleTasks( |
258 const TileVector& tiles_that_need_to_be_rasterized); | 153 const TileVector& tiles_that_need_to_be_rasterized); |
259 | 154 |
260 void AssignGpuMemoryToTiles(PrioritizedTileSet* tiles, | 155 void AssignGpuMemoryToTiles(TileVector* tiles_that_need_to_be_rasterized); |
261 TileVector* tiles_that_need_to_be_rasterized); | |
262 void GetTilesWithAssignedBins(PrioritizedTileSet* tiles); | |
263 | 156 |
264 private: | 157 private: |
158 class MemoryUsage { | |
159 public: | |
160 MemoryUsage(); | |
161 MemoryUsage(int64 memory_bytes, int resource_count); | |
162 | |
163 static MemoryUsage FromConfig(const gfx::Size& size, ResourceFormat format); | |
164 static MemoryUsage FromTile(const Tile* tile); | |
165 | |
166 MemoryUsage& operator+=(const MemoryUsage& other); | |
167 MemoryUsage& operator-=(const MemoryUsage& other); | |
168 MemoryUsage operator-(const MemoryUsage& other); | |
169 | |
170 bool Exceeds(const MemoryUsage& limit) const; | |
171 | |
172 int64 memory_bytes() const { return memory_bytes_; } | |
173 | |
174 private: | |
175 int64 memory_bytes_; | |
176 int resource_count_; | |
177 }; | |
178 | |
265 void OnImageDecodeTaskCompleted(int layer_id, | 179 void OnImageDecodeTaskCompleted(int layer_id, |
266 SkPixelRef* pixel_ref, | 180 SkPixelRef* pixel_ref, |
267 bool was_canceled); | 181 bool was_canceled); |
268 void OnRasterTaskCompleted(Tile::Id tile, | 182 void OnRasterTaskCompleted(Tile::Id tile, |
269 scoped_ptr<ScopedResource> resource, | 183 scoped_ptr<ScopedResource> resource, |
270 RasterMode raster_mode, | 184 RasterMode raster_mode, |
271 const PicturePileImpl::Analysis& analysis, | 185 const PicturePileImpl::Analysis& analysis, |
272 bool was_canceled); | 186 bool was_canceled); |
273 | 187 |
274 inline size_t BytesConsumedIfAllocated(const Tile* tile) const { | |
275 return Resource::MemorySizeBytes(tile->size(), | |
276 resource_pool_->resource_format()); | |
277 } | |
278 | |
279 void FreeResourceForTile(Tile* tile, RasterMode mode); | 188 void FreeResourceForTile(Tile* tile, RasterMode mode); |
280 void FreeResourcesForTile(Tile* tile); | 189 void FreeResourcesForTile(Tile* tile); |
281 void FreeUnusedResourcesForTile(Tile* tile); | 190 void FreeUnusedResourcesForTile(Tile* tile); |
282 void FreeResourcesForTileAndNotifyClientIfTileWasReadyToDraw(Tile* tile); | 191 void FreeResourcesForTileAndNotifyClientIfTileWasReadyToDraw(Tile* tile); |
283 scoped_refptr<ImageDecodeTask> CreateImageDecodeTask(Tile* tile, | 192 scoped_refptr<ImageDecodeTask> CreateImageDecodeTask(Tile* tile, |
284 SkPixelRef* pixel_ref); | 193 SkPixelRef* pixel_ref); |
285 scoped_refptr<RasterTask> CreateRasterTask(Tile* tile); | 194 scoped_refptr<RasterTask> CreateRasterTask(Tile* tile); |
286 void UpdatePrioritizedTileSetIfNeeded(); | |
287 | 195 |
196 bool FreeTileResourcesUntilUsageIsWithinLimit(TilePriorityQueue* queue, | |
197 const MemoryUsage& limit, | |
198 MemoryUsage* usage); | |
199 bool FreeTileResourcesWithLowerPriorityUntilUsageIsWithinLimit( | |
200 TilePriorityQueue* queue, | |
201 const MemoryUsage& limit, | |
202 const TilePriority& other_priority, | |
203 MemoryUsage* usage); | |
204 bool TilePriorityViolatesMemoryPolicy(const TilePriority& priority); | |
288 bool IsReadyToActivate() const; | 205 bool IsReadyToActivate() const; |
289 void CheckIfReadyToActivate(); | 206 void CheckIfReadyToActivate(); |
290 | 207 |
291 TileManagerClient* client_; | 208 TileManagerClient* client_; |
292 scoped_refptr<base::SequencedTaskRunner> task_runner_; | 209 scoped_refptr<base::SequencedTaskRunner> task_runner_; |
293 ResourcePool* resource_pool_; | 210 ResourcePool* resource_pool_; |
294 Rasterizer* rasterizer_; | 211 Rasterizer* rasterizer_; |
295 GlobalStateThatImpactsTilePriority global_state_; | 212 GlobalStateThatImpactsTilePriority global_state_; |
296 | 213 |
297 typedef base::hash_map<Tile::Id, Tile*> TileMap; | 214 typedef base::hash_map<Tile::Id, Tile*> TileMap; |
298 TileMap tiles_; | 215 TileMap tiles_; |
299 | 216 |
300 PrioritizedTileSet prioritized_tiles_; | 217 bool all_tiles_that_need_to_be_rasterized_are_scheduled_; |
301 bool prioritized_tiles_dirty_; | |
302 | 218 |
303 bool all_tiles_that_need_to_be_rasterized_have_memory_; | |
304 bool all_tiles_required_for_activation_have_memory_; | |
305 | |
306 size_t bytes_releasable_; | |
307 size_t resources_releasable_; | |
308 | |
309 bool ever_exceeded_memory_budget_; | |
310 MemoryHistory::Entry memory_stats_from_last_assign_; | 219 MemoryHistory::Entry memory_stats_from_last_assign_; |
311 | 220 |
312 RenderingStatsInstrumentation* rendering_stats_instrumentation_; | 221 RenderingStatsInstrumentation* rendering_stats_instrumentation_; |
313 | 222 |
314 bool did_initialize_visible_tile_; | 223 bool did_initialize_visible_tile_; |
315 bool did_check_for_completed_tasks_since_last_schedule_tasks_; | 224 bool did_check_for_completed_tasks_since_last_schedule_tasks_; |
316 | 225 |
317 typedef base::hash_map<uint32_t, scoped_refptr<ImageDecodeTask> > | 226 typedef base::hash_map<uint32_t, scoped_refptr<ImageDecodeTask> > |
318 PixelRefTaskMap; | 227 PixelRefTaskMap; |
319 typedef base::hash_map<int, PixelRefTaskMap> LayerPixelRefTaskMap; | 228 typedef base::hash_map<int, PixelRefTaskMap> LayerPixelRefTaskMap; |
(...skipping 14 matching lines...) Expand all Loading... | |
334 std::vector<scoped_refptr<RasterTask> > orphan_raster_tasks_; | 243 std::vector<scoped_refptr<RasterTask> > orphan_raster_tasks_; |
335 | 244 |
336 UniqueNotifier ready_to_activate_check_notifier_; | 245 UniqueNotifier ready_to_activate_check_notifier_; |
337 | 246 |
338 DISALLOW_COPY_AND_ASSIGN(TileManager); | 247 DISALLOW_COPY_AND_ASSIGN(TileManager); |
339 }; | 248 }; |
340 | 249 |
341 } // namespace cc | 250 } // namespace cc |
342 | 251 |
343 #endif // CC_RESOURCES_TILE_MANAGER_H_ | 252 #endif // CC_RESOURCES_TILE_MANAGER_H_ |
OLD | NEW |