Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(22)

Side by Side Diff: cc/resources/tile_manager.h

Issue 367833003: cc: Start using raster/eviction iterators. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: update Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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" 20 #include "cc/resources/eviction_tile_priority_queue.h"
21 #include "cc/resources/managed_tile_state.h" 21 #include "cc/resources/managed_tile_state.h"
22 #include "cc/resources/memory_history.h" 22 #include "cc/resources/memory_history.h"
23 #include "cc/resources/picture_pile_impl.h" 23 #include "cc/resources/picture_pile_impl.h"
24 #include "cc/resources/prioritized_tile_set.h" 24 #include "cc/resources/raster_tile_priority_queue.h"
25 #include "cc/resources/rasterizer.h" 25 #include "cc/resources/rasterizer.h"
26 #include "cc/resources/resource_pool.h" 26 #include "cc/resources/resource_pool.h"
27 #include "cc/resources/tile.h" 27 #include "cc/resources/tile.h"
28 28
29 namespace cc { 29 namespace cc {
30 class PictureLayerImpl;
30 class ResourceProvider; 31 class ResourceProvider;
31 32
32 class CC_EXPORT TileManagerClient { 33 class CC_EXPORT TileManagerClient {
33 public: 34 public:
34 // Returns the set of layers that the tile manager should consider for raster. 35 // Returns the set of layers that the tile manager should consider for raster.
35 virtual const std::vector<PictureLayerImpl*>& GetPictureLayers() = 0; 36 // TODO(vmpstr): Change the way we determine if we are ready to activate, so
37 // that this can be removed.
38 virtual const std::vector<PictureLayerImpl*>& GetPictureLayers() const = 0;
36 39
37 // Called when all tiles marked as required for activation are ready to draw. 40 // Called when all tiles marked as required for activation are ready to draw.
38 virtual void NotifyReadyToActivate() = 0; 41 virtual void NotifyReadyToActivate() = 0;
39 42
40 // Called when the visible representation of a tile might have changed. Some 43 // Called when the visible representation of a tile might have changed. Some
41 // examples are: 44 // examples are:
42 // - Tile version initialized. 45 // - Tile version initialized.
43 // - Tile resources freed. 46 // - Tile resources freed.
44 // - Tile marked for on-demand raster. 47 // - Tile marked for on-demand raster.
45 virtual void NotifyTileStateChanged(const Tile* tile) = 0; 48 virtual void NotifyTileStateChanged(const Tile* tile) = 0;
46 49
50 virtual void BuildRasterQueue(RasterTilePriorityQueue* queue,
51 TreePriority tree_priority) = 0;
52 virtual void BuildEvictionQueue(EvictionTilePriorityQueue* queue,
53 TreePriority tree_priority) = 0;
reveman 2014/07/15 01:51:10 Looks good. Can you add a short comment above each
vmpstr 2014/07/15 05:36:27 Will do.
54
47 protected: 55 protected:
48 virtual ~TileManagerClient() {} 56 virtual ~TileManagerClient() {}
49 }; 57 };
50 58
51 struct RasterTaskCompletionStats { 59 struct RasterTaskCompletionStats {
52 RasterTaskCompletionStats(); 60 RasterTaskCompletionStats();
53 61
54 size_t completed_count; 62 size_t completed_count;
55 size_t canceled_count; 63 size_t canceled_count;
56 }; 64 };
57 scoped_ptr<base::Value> RasterTaskCompletionStatsAsValue( 65 scoped_ptr<base::Value> RasterTaskCompletionStatsAsValue(
58 const RasterTaskCompletionStats& stats); 66 const RasterTaskCompletionStats& stats);
59 67
60 // This class manages tiles, deciding which should get rasterized and which 68 // 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" 69 // 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 70 // by layers; they automatically register with the manager when they are
63 // created, and unregister from the manager when they are deleted. 71 // created, and unregister from the manager when they are deleted.
64 class CC_EXPORT TileManager : public RasterizerClient, 72 class CC_EXPORT TileManager : public RasterizerClient,
65 public RefCountedManager<Tile> { 73 public RefCountedManager<Tile> {
66 public: 74 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( 75 static scoped_ptr<TileManager> Create(
167 TileManagerClient* client, 76 TileManagerClient* client,
168 base::SequencedTaskRunner* task_runner, 77 base::SequencedTaskRunner* task_runner,
169 ResourcePool* resource_pool, 78 ResourcePool* resource_pool,
170 Rasterizer* rasterizer, 79 Rasterizer* rasterizer,
171 RenderingStatsInstrumentation* rendering_stats_instrumentation); 80 RenderingStatsInstrumentation* rendering_stats_instrumentation);
172 virtual ~TileManager(); 81 virtual ~TileManager();
173 82
174 void ManageTiles(const GlobalStateThatImpactsTilePriority& state); 83 void ManageTiles(const GlobalStateThatImpactsTilePriority& state);
175 84
176 // Returns true when visible tiles have been initialized. 85 // Returns true when visible tiles have been initialized.
177 bool UpdateVisibleTiles(); 86 bool UpdateVisibleTiles();
178 87
179 scoped_refptr<Tile> CreateTile(PicturePileImpl* picture_pile, 88 scoped_refptr<Tile> CreateTile(PicturePileImpl* picture_pile,
180 const gfx::Size& tile_size, 89 const gfx::Size& tile_size,
181 const gfx::Rect& content_rect, 90 const gfx::Rect& content_rect,
182 const gfx::Rect& opaque_rect, 91 const gfx::Rect& opaque_rect,
183 float contents_scale, 92 float contents_scale,
184 int layer_id, 93 int layer_id,
185 int source_frame_number, 94 int source_frame_number,
186 int flags); 95 int flags);
187 96
188 scoped_ptr<base::Value> BasicStateAsValue() const; 97 scoped_ptr<base::Value> BasicStateAsValue() const;
189 scoped_ptr<base::Value> AllTilesAsValue() const; 98 scoped_ptr<base::Value> AllTilesAsValue() const;
190 const MemoryHistory::Entry& memory_stats_from_last_assign() const { 99 const MemoryHistory::Entry& memory_stats_from_last_assign() const {
191 return memory_stats_from_last_assign_; 100 return memory_stats_from_last_assign_;
192 } 101 }
193 102
194 void GetPairedPictureLayers(std::vector<PairedPictureLayer>* layers) const;
195
196 void InitializeTilesWithResourcesForTesting(const std::vector<Tile*>& tiles) { 103 void InitializeTilesWithResourcesForTesting(const std::vector<Tile*>& tiles) {
197 for (size_t i = 0; i < tiles.size(); ++i) { 104 for (size_t i = 0; i < tiles.size(); ++i) {
198 ManagedTileState& mts = tiles[i]->managed_state(); 105 ManagedTileState& mts = tiles[i]->managed_state();
199 ManagedTileState::TileVersion& tile_version = 106 ManagedTileState::TileVersion& tile_version =
200 mts.tile_versions[HIGH_QUALITY_RASTER_MODE]; 107 mts.tile_versions[HIGH_QUALITY_RASTER_MODE];
201 108
202 tile_version.resource_ = resource_pool_->AcquireResource(gfx::Size(1, 1)); 109 tile_version.resource_ =
203 110 resource_pool_->AcquireResource(tiles[i]->size());
204 bytes_releasable_ += BytesConsumedIfAllocated(tiles[i]);
205 ++resources_releasable_;
206 } 111 }
207 } 112 }
208 113
209 void ReleaseTileResourcesForTesting(const std::vector<Tile*>& tiles) { 114 void ReleaseTileResourcesForTesting(const std::vector<Tile*>& tiles) {
210 for (size_t i = 0; i < tiles.size(); ++i) { 115 for (size_t i = 0; i < tiles.size(); ++i) {
211 Tile* tile = tiles[i]; 116 Tile* tile = tiles[i];
212 for (int mode = 0; mode < NUM_RASTER_MODES; ++mode) { 117 for (int mode = 0; mode < NUM_RASTER_MODES; ++mode) {
213 FreeResourceForTile(tile, static_cast<RasterMode>(mode)); 118 FreeResourceForTile(tile, static_cast<RasterMode>(mode));
214 } 119 }
215 } 120 }
216 } 121 }
217 122
218 void SetGlobalStateForTesting( 123 void SetGlobalStateForTesting(
219 const GlobalStateThatImpactsTilePriority& state) { 124 const GlobalStateThatImpactsTilePriority& state) {
220 // Soft limit is used for resource pool such that 125 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 } 126 }
227 127
228 void SetRasterizerForTesting(Rasterizer* rasterizer); 128 void SetRasterizerForTesting(Rasterizer* rasterizer);
229 129
230 void CleanUpReleasedTilesForTesting() { CleanUpReleasedTiles(); } 130 void CleanUpReleasedTilesForTesting() { CleanUpReleasedTiles(); }
231 131
232 protected: 132 protected:
233 TileManager(TileManagerClient* client, 133 TileManager(TileManagerClient* client,
234 base::SequencedTaskRunner* task_runner, 134 base::SequencedTaskRunner* task_runner,
235 ResourcePool* resource_pool, 135 ResourcePool* resource_pool,
236 Rasterizer* rasterizer, 136 Rasterizer* rasterizer,
237 RenderingStatsInstrumentation* rendering_stats_instrumentation); 137 RenderingStatsInstrumentation* rendering_stats_instrumentation);
238 138
239 // Methods called by Tile
240 friend class Tile;
241 void DidChangeTilePriority(Tile* tile);
242
243 void CleanUpReleasedTiles(); 139 void CleanUpReleasedTiles();
244 140
245 // Overriden from RefCountedManager<Tile>: 141 // Overriden from RefCountedManager<Tile>:
142 friend class Tile;
246 virtual void Release(Tile* tile) OVERRIDE; 143 virtual void Release(Tile* tile) OVERRIDE;
247 144
248 // Overriden from RasterizerClient: 145 // Overriden from RasterizerClient:
249 virtual bool ShouldForceTasksRequiredForActivationToComplete() const OVERRIDE; 146 virtual bool ShouldForceTasksRequiredForActivationToComplete() const OVERRIDE;
250 virtual void DidFinishRunningTasks() OVERRIDE; 147 virtual void DidFinishRunningTasks() OVERRIDE;
251 virtual void DidFinishRunningTasksRequiredForActivation() OVERRIDE; 148 virtual void DidFinishRunningTasksRequiredForActivation() OVERRIDE;
252 149
253 typedef std::vector<Tile*> TileVector; 150 typedef std::vector<Tile*> TileVector;
254 typedef std::set<Tile*> TileSet; 151 typedef std::set<Tile*> TileSet;
255 152
256 // Virtual for test 153 // Virtual for test
257 virtual void ScheduleTasks( 154 virtual void ScheduleTasks(
258 const TileVector& tiles_that_need_to_be_rasterized); 155 const TileVector& tiles_that_need_to_be_rasterized);
259 156
260 void AssignGpuMemoryToTiles(PrioritizedTileSet* tiles, 157 void AssignGpuMemoryToTiles(TileVector* tiles_that_need_to_be_rasterized);
261 TileVector* tiles_that_need_to_be_rasterized);
262 void GetTilesWithAssignedBins(PrioritizedTileSet* tiles);
263 158
264 private: 159 private:
160 class MemoryUsage {
161 public:
162 MemoryUsage();
163 MemoryUsage(int64 memory_bytes, int resource_count);
164
165 static MemoryUsage FromConfig(const gfx::Size& size, ResourceFormat format);
166 static MemoryUsage FromTile(const Tile* tile);
167
168 MemoryUsage& operator+=(const MemoryUsage& other);
169 MemoryUsage& operator-=(const MemoryUsage& other);
170 MemoryUsage operator-(const MemoryUsage& other);
171
172 bool Exceeds(const MemoryUsage& limit) const;
173
174 int64 memory_bytes() const { return memory_bytes_; }
175
176 private:
177 int64 memory_bytes_;
178 int resource_count_;
179 };
180
265 void OnImageDecodeTaskCompleted(int layer_id, 181 void OnImageDecodeTaskCompleted(int layer_id,
266 SkPixelRef* pixel_ref, 182 SkPixelRef* pixel_ref,
267 bool was_canceled); 183 bool was_canceled);
268 void OnRasterTaskCompleted(Tile::Id tile, 184 void OnRasterTaskCompleted(Tile::Id tile,
269 scoped_ptr<ScopedResource> resource, 185 scoped_ptr<ScopedResource> resource,
270 RasterMode raster_mode, 186 RasterMode raster_mode,
271 const PicturePileImpl::Analysis& analysis, 187 const PicturePileImpl::Analysis& analysis,
272 bool was_canceled); 188 bool was_canceled);
273 189
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); 190 void FreeResourceForTile(Tile* tile, RasterMode mode);
280 void FreeResourcesForTile(Tile* tile); 191 void FreeResourcesForTile(Tile* tile);
281 void FreeUnusedResourcesForTile(Tile* tile); 192 void FreeUnusedResourcesForTile(Tile* tile);
282 void FreeResourcesForTileAndNotifyClientIfTileWasReadyToDraw(Tile* tile); 193 void FreeResourcesForTileAndNotifyClientIfTileWasReadyToDraw(Tile* tile);
283 scoped_refptr<ImageDecodeTask> CreateImageDecodeTask(Tile* tile, 194 scoped_refptr<ImageDecodeTask> CreateImageDecodeTask(Tile* tile,
284 SkPixelRef* pixel_ref); 195 SkPixelRef* pixel_ref);
285 scoped_refptr<RasterTask> CreateRasterTask(Tile* tile); 196 scoped_refptr<RasterTask> CreateRasterTask(Tile* tile);
286 void UpdatePrioritizedTileSetIfNeeded();
287 197
198 bool FreeTileResourcesUntilUsageIsWithinLimit(TilePriorityQueue* queue,
199 const MemoryUsage& limit,
200 MemoryUsage* usage);
201 bool FreeTileResourcesWithLowerPriorityUntilUsageIsWithinLimit(
202 TilePriorityQueue* queue,
203 const MemoryUsage& limit,
204 const TilePriority& other_priority,
205 MemoryUsage* usage);
206 bool TilePriorityViolatesMemoryPolicy(const TilePriority& priority);
288 bool IsReadyToActivate() const; 207 bool IsReadyToActivate() const;
289 void CheckIfReadyToActivate(); 208 void CheckIfReadyToActivate();
290 209
291 TileManagerClient* client_; 210 TileManagerClient* client_;
292 scoped_refptr<base::SequencedTaskRunner> task_runner_; 211 scoped_refptr<base::SequencedTaskRunner> task_runner_;
293 ResourcePool* resource_pool_; 212 ResourcePool* resource_pool_;
294 Rasterizer* rasterizer_; 213 Rasterizer* rasterizer_;
295 GlobalStateThatImpactsTilePriority global_state_; 214 GlobalStateThatImpactsTilePriority global_state_;
296 215
297 typedef base::hash_map<Tile::Id, Tile*> TileMap; 216 typedef base::hash_map<Tile::Id, Tile*> TileMap;
298 TileMap tiles_; 217 TileMap tiles_;
299 218
300 PrioritizedTileSet prioritized_tiles_; 219 bool all_tiles_that_need_to_be_rasterized_are_scheduled_;
301 bool prioritized_tiles_dirty_;
302 220
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_; 221 MemoryHistory::Entry memory_stats_from_last_assign_;
311 222
312 RenderingStatsInstrumentation* rendering_stats_instrumentation_; 223 RenderingStatsInstrumentation* rendering_stats_instrumentation_;
313 224
314 bool did_initialize_visible_tile_; 225 bool did_initialize_visible_tile_;
315 bool did_check_for_completed_tasks_since_last_schedule_tasks_; 226 bool did_check_for_completed_tasks_since_last_schedule_tasks_;
316 227
317 typedef base::hash_map<uint32_t, scoped_refptr<ImageDecodeTask> > 228 typedef base::hash_map<uint32_t, scoped_refptr<ImageDecodeTask> >
318 PixelRefTaskMap; 229 PixelRefTaskMap;
319 typedef base::hash_map<int, PixelRefTaskMap> LayerPixelRefTaskMap; 230 typedef base::hash_map<int, PixelRefTaskMap> LayerPixelRefTaskMap;
320 LayerPixelRefTaskMap image_decode_tasks_; 231 LayerPixelRefTaskMap image_decode_tasks_;
321 232
322 typedef base::hash_map<int, int> LayerCountMap; 233 typedef base::hash_map<int, int> LayerCountMap;
323 LayerCountMap used_layer_counts_; 234 LayerCountMap used_layer_counts_;
324 235
325 RasterTaskCompletionStats update_visible_tiles_stats_; 236 RasterTaskCompletionStats update_visible_tiles_stats_;
326 237
327 std::vector<Tile*> released_tiles_; 238 std::vector<Tile*> released_tiles_;
328 239
329 ResourceFormat resource_format_; 240 ResourceFormat resource_format_;
330 241
331 // Queue used when scheduling raster tasks. 242 // Queue used when scheduling raster tasks.
332 RasterTaskQueue raster_queue_; 243 RasterTaskQueue raster_queue_;
333 244
334 std::vector<scoped_refptr<RasterTask> > orphan_raster_tasks_; 245 std::vector<scoped_refptr<RasterTask> > orphan_raster_tasks_;
335 246
336 UniqueNotifier ready_to_activate_check_notifier_; 247 UniqueNotifier ready_to_activate_check_notifier_;
337 248
249 RasterTilePriorityQueue raster_priority_queue_;
250 EvictionTilePriorityQueue eviction_priority_queue_;
251
338 DISALLOW_COPY_AND_ASSIGN(TileManager); 252 DISALLOW_COPY_AND_ASSIGN(TileManager);
339 }; 253 };
340 254
341 } // namespace cc 255 } // namespace cc
342 256
343 #endif // CC_RESOURCES_TILE_MANAGER_H_ 257 #endif // CC_RESOURCES_TILE_MANAGER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698