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

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 // Given an empty raster tile priority queue, this will build a priority queue
51 // that will return tiles in order in which they should be rasterized. Note
52 // that if the queue is an object that was built before,
53 // RasterTilePriorityQueue::Reset must be called on it before this function.
54 virtual void BuildRasterQueue(RasterTilePriorityQueue* queue,
55 TreePriority tree_priority) = 0;
56
57 // Given an empty eviction tile priority queue, this will build a priority
58 // queue that will return tiles in order in which they should be evicted. Note
59 // that if the queue is an object that was built before,
60 // EvictionTilePriorityQueue::Reset must be called on it before this function.
61 virtual void BuildEvictionQueue(EvictionTilePriorityQueue* queue,
62 TreePriority tree_priority) = 0;
63
47 protected: 64 protected:
48 virtual ~TileManagerClient() {} 65 virtual ~TileManagerClient() {}
49 }; 66 };
50 67
51 struct RasterTaskCompletionStats { 68 struct RasterTaskCompletionStats {
52 RasterTaskCompletionStats(); 69 RasterTaskCompletionStats();
53 70
54 size_t completed_count; 71 size_t completed_count;
55 size_t canceled_count; 72 size_t canceled_count;
56 }; 73 };
57 scoped_ptr<base::Value> RasterTaskCompletionStatsAsValue( 74 scoped_ptr<base::Value> RasterTaskCompletionStatsAsValue(
58 const RasterTaskCompletionStats& stats); 75 const RasterTaskCompletionStats& stats);
59 76
60 // This class manages tiles, deciding which should get rasterized and which 77 // 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" 78 // 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 79 // by layers; they automatically register with the manager when they are
63 // created, and unregister from the manager when they are deleted. 80 // created, and unregister from the manager when they are deleted.
64 class CC_EXPORT TileManager : public RasterizerClient, 81 class CC_EXPORT TileManager : public RasterizerClient,
65 public RefCountedManager<Tile> { 82 public RefCountedManager<Tile> {
66 public: 83 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( 84 static scoped_ptr<TileManager> Create(
167 TileManagerClient* client, 85 TileManagerClient* client,
168 base::SequencedTaskRunner* task_runner, 86 base::SequencedTaskRunner* task_runner,
169 ResourcePool* resource_pool, 87 ResourcePool* resource_pool,
170 Rasterizer* rasterizer, 88 Rasterizer* rasterizer,
171 RenderingStatsInstrumentation* rendering_stats_instrumentation); 89 RenderingStatsInstrumentation* rendering_stats_instrumentation);
172 virtual ~TileManager(); 90 virtual ~TileManager();
173 91
174 void ManageTiles(const GlobalStateThatImpactsTilePriority& state); 92 void ManageTiles(const GlobalStateThatImpactsTilePriority& state);
175 93
176 // Returns true when visible tiles have been initialized. 94 // Returns true when visible tiles have been initialized.
177 bool UpdateVisibleTiles(); 95 bool UpdateVisibleTiles();
178 96
179 scoped_refptr<Tile> CreateTile(PicturePileImpl* picture_pile, 97 scoped_refptr<Tile> CreateTile(PicturePileImpl* picture_pile,
180 const gfx::Size& tile_size, 98 const gfx::Size& tile_size,
181 const gfx::Rect& content_rect, 99 const gfx::Rect& content_rect,
182 const gfx::Rect& opaque_rect, 100 const gfx::Rect& opaque_rect,
183 float contents_scale, 101 float contents_scale,
184 int layer_id, 102 int layer_id,
185 int source_frame_number, 103 int source_frame_number,
186 int flags); 104 int flags);
187 105
188 scoped_ptr<base::Value> BasicStateAsValue() const; 106 scoped_ptr<base::Value> BasicStateAsValue() const;
189 scoped_ptr<base::Value> AllTilesAsValue() const; 107 scoped_ptr<base::Value> AllTilesAsValue() const;
190 const MemoryHistory::Entry& memory_stats_from_last_assign() const { 108 const MemoryHistory::Entry& memory_stats_from_last_assign() const {
191 return memory_stats_from_last_assign_; 109 return memory_stats_from_last_assign_;
192 } 110 }
193 111
194 void GetPairedPictureLayers(std::vector<PairedPictureLayer>* layers) const;
195
196 void InitializeTilesWithResourcesForTesting(const std::vector<Tile*>& tiles) { 112 void InitializeTilesWithResourcesForTesting(const std::vector<Tile*>& tiles) {
197 for (size_t i = 0; i < tiles.size(); ++i) { 113 for (size_t i = 0; i < tiles.size(); ++i) {
198 ManagedTileState& mts = tiles[i]->managed_state(); 114 ManagedTileState& mts = tiles[i]->managed_state();
199 ManagedTileState::TileVersion& tile_version = 115 ManagedTileState::TileVersion& tile_version =
200 mts.tile_versions[HIGH_QUALITY_RASTER_MODE]; 116 mts.tile_versions[HIGH_QUALITY_RASTER_MODE];
201 117
202 tile_version.resource_ = resource_pool_->AcquireResource(gfx::Size(1, 1)); 118 tile_version.resource_ =
203 119 resource_pool_->AcquireResource(tiles[i]->size());
204 bytes_releasable_ += BytesConsumedIfAllocated(tiles[i]);
205 ++resources_releasable_;
206 } 120 }
207 } 121 }
208 122
209 void ReleaseTileResourcesForTesting(const std::vector<Tile*>& tiles) { 123 void ReleaseTileResourcesForTesting(const std::vector<Tile*>& tiles) {
210 for (size_t i = 0; i < tiles.size(); ++i) { 124 for (size_t i = 0; i < tiles.size(); ++i) {
211 Tile* tile = tiles[i]; 125 Tile* tile = tiles[i];
212 for (int mode = 0; mode < NUM_RASTER_MODES; ++mode) { 126 for (int mode = 0; mode < NUM_RASTER_MODES; ++mode) {
213 FreeResourceForTile(tile, static_cast<RasterMode>(mode)); 127 FreeResourceForTile(tile, static_cast<RasterMode>(mode));
214 } 128 }
215 } 129 }
216 } 130 }
217 131
218 void SetGlobalStateForTesting( 132 void SetGlobalStateForTesting(
219 const GlobalStateThatImpactsTilePriority& state) { 133 const GlobalStateThatImpactsTilePriority& state) {
220 // Soft limit is used for resource pool such that 134 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 } 135 }
227 136
228 void SetRasterizerForTesting(Rasterizer* rasterizer); 137 void SetRasterizerForTesting(Rasterizer* rasterizer);
229 138
230 void CleanUpReleasedTilesForTesting() { CleanUpReleasedTiles(); } 139 void CleanUpReleasedTilesForTesting() { CleanUpReleasedTiles(); }
231 140
232 protected: 141 protected:
233 TileManager(TileManagerClient* client, 142 TileManager(TileManagerClient* client,
234 base::SequencedTaskRunner* task_runner, 143 base::SequencedTaskRunner* task_runner,
235 ResourcePool* resource_pool, 144 ResourcePool* resource_pool,
236 Rasterizer* rasterizer, 145 Rasterizer* rasterizer,
237 RenderingStatsInstrumentation* rendering_stats_instrumentation); 146 RenderingStatsInstrumentation* rendering_stats_instrumentation);
238 147
239 // Methods called by Tile
240 friend class Tile;
241 void DidChangeTilePriority(Tile* tile);
242
243 void CleanUpReleasedTiles(); 148 void CleanUpReleasedTiles();
244 149
245 // Overriden from RefCountedManager<Tile>: 150 // Overriden from RefCountedManager<Tile>:
151 friend class Tile;
246 virtual void Release(Tile* tile) OVERRIDE; 152 virtual void Release(Tile* tile) OVERRIDE;
247 153
248 // Overriden from RasterizerClient: 154 // Overriden from RasterizerClient:
249 virtual bool ShouldForceTasksRequiredForActivationToComplete() const OVERRIDE; 155 virtual bool ShouldForceTasksRequiredForActivationToComplete() const OVERRIDE;
250 virtual void DidFinishRunningTasks() OVERRIDE; 156 virtual void DidFinishRunningTasks() OVERRIDE;
251 virtual void DidFinishRunningTasksRequiredForActivation() OVERRIDE; 157 virtual void DidFinishRunningTasksRequiredForActivation() OVERRIDE;
252 158
253 typedef std::vector<Tile*> TileVector; 159 typedef std::vector<Tile*> TileVector;
254 typedef std::set<Tile*> TileSet; 160 typedef std::set<Tile*> TileSet;
255 161
256 // Virtual for test 162 // Virtual for test
257 virtual void ScheduleTasks( 163 virtual void ScheduleTasks(
258 const TileVector& tiles_that_need_to_be_rasterized); 164 const TileVector& tiles_that_need_to_be_rasterized);
259 165
260 void AssignGpuMemoryToTiles(PrioritizedTileSet* tiles, 166 void AssignGpuMemoryToTiles(TileVector* tiles_that_need_to_be_rasterized);
261 TileVector* tiles_that_need_to_be_rasterized);
262 void GetTilesWithAssignedBins(PrioritizedTileSet* tiles);
263 167
264 private: 168 private:
169 class MemoryUsage {
170 public:
171 MemoryUsage();
172 MemoryUsage(int64 memory_bytes, int resource_count);
173
174 static MemoryUsage FromConfig(const gfx::Size& size, ResourceFormat format);
175 static MemoryUsage FromTile(const Tile* tile);
176
177 MemoryUsage& operator+=(const MemoryUsage& other);
178 MemoryUsage& operator-=(const MemoryUsage& other);
179 MemoryUsage operator-(const MemoryUsage& other);
180
181 bool Exceeds(const MemoryUsage& limit) const;
182
183 int64 memory_bytes() const { return memory_bytes_; }
184
185 private:
186 int64 memory_bytes_;
187 int resource_count_;
188 };
189
265 void OnImageDecodeTaskCompleted(int layer_id, 190 void OnImageDecodeTaskCompleted(int layer_id,
266 SkPixelRef* pixel_ref, 191 SkPixelRef* pixel_ref,
267 bool was_canceled); 192 bool was_canceled);
268 void OnRasterTaskCompleted(Tile::Id tile, 193 void OnRasterTaskCompleted(Tile::Id tile,
269 scoped_ptr<ScopedResource> resource, 194 scoped_ptr<ScopedResource> resource,
270 RasterMode raster_mode, 195 RasterMode raster_mode,
271 const PicturePileImpl::Analysis& analysis, 196 const PicturePileImpl::Analysis& analysis,
272 bool was_canceled); 197 bool was_canceled);
273 198
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); 199 void FreeResourceForTile(Tile* tile, RasterMode mode);
280 void FreeResourcesForTile(Tile* tile); 200 void FreeResourcesForTile(Tile* tile);
281 void FreeUnusedResourcesForTile(Tile* tile); 201 void FreeUnusedResourcesForTile(Tile* tile);
282 void FreeResourcesForTileAndNotifyClientIfTileWasReadyToDraw(Tile* tile); 202 void FreeResourcesForTileAndNotifyClientIfTileWasReadyToDraw(Tile* tile);
283 scoped_refptr<ImageDecodeTask> CreateImageDecodeTask(Tile* tile, 203 scoped_refptr<ImageDecodeTask> CreateImageDecodeTask(Tile* tile,
284 SkPixelRef* pixel_ref); 204 SkPixelRef* pixel_ref);
285 scoped_refptr<RasterTask> CreateRasterTask(Tile* tile); 205 scoped_refptr<RasterTask> CreateRasterTask(Tile* tile);
286 void UpdatePrioritizedTileSetIfNeeded();
287 206
207 bool FreeTileResourcesUntilUsageIsWithinLimit(TilePriorityQueue* queue,
208 const MemoryUsage& limit,
209 MemoryUsage* usage);
210 bool FreeTileResourcesWithLowerPriorityUntilUsageIsWithinLimit(
211 TilePriorityQueue* queue,
212 const MemoryUsage& limit,
213 const TilePriority& other_priority,
214 MemoryUsage* usage);
215 bool TilePriorityViolatesMemoryPolicy(const TilePriority& priority);
288 bool IsReadyToActivate() const; 216 bool IsReadyToActivate() const;
289 void CheckIfReadyToActivate(); 217 void CheckIfReadyToActivate();
290 218
291 TileManagerClient* client_; 219 TileManagerClient* client_;
292 scoped_refptr<base::SequencedTaskRunner> task_runner_; 220 scoped_refptr<base::SequencedTaskRunner> task_runner_;
293 ResourcePool* resource_pool_; 221 ResourcePool* resource_pool_;
294 Rasterizer* rasterizer_; 222 Rasterizer* rasterizer_;
295 GlobalStateThatImpactsTilePriority global_state_; 223 GlobalStateThatImpactsTilePriority global_state_;
296 224
297 typedef base::hash_map<Tile::Id, Tile*> TileMap; 225 typedef base::hash_map<Tile::Id, Tile*> TileMap;
298 TileMap tiles_; 226 TileMap tiles_;
299 227
300 PrioritizedTileSet prioritized_tiles_; 228 bool all_tiles_that_need_to_be_rasterized_are_scheduled_;
301 bool prioritized_tiles_dirty_;
302 229
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_; 230 MemoryHistory::Entry memory_stats_from_last_assign_;
311 231
312 RenderingStatsInstrumentation* rendering_stats_instrumentation_; 232 RenderingStatsInstrumentation* rendering_stats_instrumentation_;
313 233
314 bool did_initialize_visible_tile_; 234 bool did_initialize_visible_tile_;
315 bool did_check_for_completed_tasks_since_last_schedule_tasks_; 235 bool did_check_for_completed_tasks_since_last_schedule_tasks_;
316 236
317 typedef base::hash_map<uint32_t, scoped_refptr<ImageDecodeTask> > 237 typedef base::hash_map<uint32_t, scoped_refptr<ImageDecodeTask> >
318 PixelRefTaskMap; 238 PixelRefTaskMap;
319 typedef base::hash_map<int, PixelRefTaskMap> LayerPixelRefTaskMap; 239 typedef base::hash_map<int, PixelRefTaskMap> LayerPixelRefTaskMap;
320 LayerPixelRefTaskMap image_decode_tasks_; 240 LayerPixelRefTaskMap image_decode_tasks_;
321 241
322 typedef base::hash_map<int, int> LayerCountMap; 242 typedef base::hash_map<int, int> LayerCountMap;
323 LayerCountMap used_layer_counts_; 243 LayerCountMap used_layer_counts_;
324 244
325 RasterTaskCompletionStats update_visible_tiles_stats_; 245 RasterTaskCompletionStats update_visible_tiles_stats_;
326 246
327 std::vector<Tile*> released_tiles_; 247 std::vector<Tile*> released_tiles_;
328 248
329 ResourceFormat resource_format_; 249 ResourceFormat resource_format_;
330 250
331 // Queue used when scheduling raster tasks. 251 // Queue used when scheduling raster tasks.
332 RasterTaskQueue raster_queue_; 252 RasterTaskQueue raster_queue_;
333 253
334 std::vector<scoped_refptr<RasterTask> > orphan_raster_tasks_; 254 std::vector<scoped_refptr<RasterTask> > orphan_raster_tasks_;
335 255
336 UniqueNotifier ready_to_activate_check_notifier_; 256 UniqueNotifier ready_to_activate_check_notifier_;
337 257
258 RasterTilePriorityQueue raster_priority_queue_;
259 EvictionTilePriorityQueue eviction_priority_queue_;
260
338 DISALLOW_COPY_AND_ASSIGN(TileManager); 261 DISALLOW_COPY_AND_ASSIGN(TileManager);
339 }; 262 };
340 263
341 } // namespace cc 264 } // namespace cc
342 265
343 #endif // CC_RESOURCES_TILE_MANAGER_H_ 266 #endif // CC_RESOURCES_TILE_MANAGER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698