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/resources/eviction_tile_priority_queue.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" | |
25 #include "cc/resources/raster_tile_priority_queue.h" | 24 #include "cc/resources/raster_tile_priority_queue.h" |
26 #include "cc/resources/rasterizer.h" | 25 #include "cc/resources/rasterizer.h" |
27 #include "cc/resources/resource_pool.h" | 26 #include "cc/resources/resource_pool.h" |
28 #include "cc/resources/tile.h" | 27 #include "cc/resources/tile.h" |
29 | 28 |
30 namespace base { | 29 namespace base { |
31 namespace debug { | 30 namespace debug { |
32 class ConvertableToTraceFormat; | 31 class ConvertableToTraceFormat; |
33 class TracedValue; | 32 class TracedValue; |
34 } | 33 } |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 } | 116 } |
118 | 117 |
119 void InitializeTilesWithResourcesForTesting(const std::vector<Tile*>& tiles) { | 118 void InitializeTilesWithResourcesForTesting(const std::vector<Tile*>& tiles) { |
120 for (size_t i = 0; i < tiles.size(); ++i) { | 119 for (size_t i = 0; i < tiles.size(); ++i) { |
121 ManagedTileState& mts = tiles[i]->managed_state(); | 120 ManagedTileState& mts = tiles[i]->managed_state(); |
122 ManagedTileState::TileVersion& tile_version = | 121 ManagedTileState::TileVersion& tile_version = |
123 mts.tile_versions[HIGH_QUALITY_RASTER_MODE]; | 122 mts.tile_versions[HIGH_QUALITY_RASTER_MODE]; |
124 | 123 |
125 tile_version.resource_ = | 124 tile_version.resource_ = |
126 resource_pool_->AcquireResource(tiles[i]->size()); | 125 resource_pool_->AcquireResource(tiles[i]->size()); |
127 | |
128 bytes_releasable_ += BytesConsumedIfAllocated(tiles[i]); | |
129 ++resources_releasable_; | |
130 } | 126 } |
131 } | 127 } |
132 | 128 |
133 void ReleaseTileResourcesForTesting(const std::vector<Tile*>& tiles) { | 129 void ReleaseTileResourcesForTesting(const std::vector<Tile*>& tiles) { |
134 for (size_t i = 0; i < tiles.size(); ++i) { | 130 for (size_t i = 0; i < tiles.size(); ++i) { |
135 Tile* tile = tiles[i]; | 131 Tile* tile = tiles[i]; |
136 for (int mode = 0; mode < NUM_RASTER_MODES; ++mode) { | 132 for (int mode = 0; mode < NUM_RASTER_MODES; ++mode) { |
137 FreeResourceForTile(tile, static_cast<RasterMode>(mode)); | 133 FreeResourceForTile(tile, static_cast<RasterMode>(mode)); |
138 } | 134 } |
139 } | 135 } |
140 } | 136 } |
141 | 137 |
142 void SetGlobalStateForTesting( | 138 void SetGlobalStateForTesting( |
143 const GlobalStateThatImpactsTilePriority& state) { | 139 const GlobalStateThatImpactsTilePriority& state) { |
144 // Soft limit is used for resource pool such that | 140 global_state_ = state; |
145 // memory returns to soft limit after going over. | |
146 if (state != global_state_) { | |
147 global_state_ = state; | |
148 prioritized_tiles_dirty_ = true; | |
149 } | |
150 } | 141 } |
151 | 142 |
152 void SetRasterizerForTesting(Rasterizer* rasterizer); | 143 void SetRasterizerForTesting(Rasterizer* rasterizer); |
153 | 144 |
154 void FreeResourcesAndCleanUpReleasedTilesForTesting() { | 145 void FreeResourcesAndCleanUpReleasedTilesForTesting() { |
155 prioritized_tiles_.Clear(); | |
156 FreeResourcesForReleasedTiles(); | 146 FreeResourcesForReleasedTiles(); |
157 CleanUpReleasedTiles(); | 147 CleanUpReleasedTiles(); |
158 } | 148 } |
159 | 149 |
160 protected: | 150 protected: |
161 TileManager(TileManagerClient* client, | 151 TileManager(TileManagerClient* client, |
162 const scoped_refptr<base::SequencedTaskRunner>& task_runner, | 152 const scoped_refptr<base::SequencedTaskRunner>& task_runner, |
163 ResourcePool* resource_pool, | 153 ResourcePool* resource_pool, |
164 Rasterizer* rasterizer, | 154 Rasterizer* rasterizer, |
165 RenderingStatsInstrumentation* rendering_stats_instrumentation); | 155 RenderingStatsInstrumentation* rendering_stats_instrumentation); |
166 | 156 |
167 // Methods called by Tile | |
168 friend class Tile; | |
169 void DidChangeTilePriority(Tile* tile); | |
170 | |
171 void FreeResourcesForReleasedTiles(); | 157 void FreeResourcesForReleasedTiles(); |
172 void CleanUpReleasedTiles(); | 158 void CleanUpReleasedTiles(); |
173 | 159 |
174 // Overriden from RefCountedManager<Tile>: | 160 // Overriden from RefCountedManager<Tile>: |
| 161 friend class Tile; |
175 virtual void Release(Tile* tile) OVERRIDE; | 162 virtual void Release(Tile* tile) OVERRIDE; |
176 | 163 |
177 // Overriden from RasterizerClient: | 164 // Overriden from RasterizerClient: |
178 virtual bool ShouldForceTasksRequiredForActivationToComplete() const OVERRIDE; | 165 virtual bool ShouldForceTasksRequiredForActivationToComplete() const OVERRIDE; |
179 virtual void DidFinishRunningTasks() OVERRIDE; | 166 virtual void DidFinishRunningTasks() OVERRIDE; |
180 virtual void DidFinishRunningTasksRequiredForActivation() OVERRIDE; | 167 virtual void DidFinishRunningTasksRequiredForActivation() OVERRIDE; |
181 | 168 |
182 typedef std::vector<Tile*> TileVector; | 169 typedef std::vector<Tile*> TileVector; |
183 typedef std::set<Tile*> TileSet; | 170 typedef std::set<Tile*> TileSet; |
184 | 171 |
185 // Virtual for test | 172 // Virtual for test |
186 virtual void ScheduleTasks( | 173 virtual void ScheduleTasks( |
187 const TileVector& tiles_that_need_to_be_rasterized); | 174 const TileVector& tiles_that_need_to_be_rasterized); |
188 | 175 |
189 void AssignGpuMemoryToTiles(PrioritizedTileSet* tiles, | 176 void AssignGpuMemoryToTiles(TileVector* tiles_that_need_to_be_rasterized); |
190 TileVector* tiles_that_need_to_be_rasterized); | |
191 void GetTilesWithAssignedBins(PrioritizedTileSet* tiles); | |
192 | 177 |
193 private: | 178 private: |
| 179 class MemoryUsage { |
| 180 public: |
| 181 MemoryUsage(); |
| 182 MemoryUsage(int64 memory_bytes, int resource_count); |
| 183 |
| 184 static MemoryUsage FromConfig(const gfx::Size& size, ResourceFormat format); |
| 185 static MemoryUsage FromTile(const Tile* tile); |
| 186 |
| 187 MemoryUsage& operator+=(const MemoryUsage& other); |
| 188 MemoryUsage& operator-=(const MemoryUsage& other); |
| 189 MemoryUsage operator-(const MemoryUsage& other); |
| 190 |
| 191 bool Exceeds(const MemoryUsage& limit) const; |
| 192 int64 memory_bytes() const { return memory_bytes_; } |
| 193 |
| 194 private: |
| 195 int64 memory_bytes_; |
| 196 int resource_count_; |
| 197 }; |
| 198 |
194 void OnImageDecodeTaskCompleted(int layer_id, | 199 void OnImageDecodeTaskCompleted(int layer_id, |
195 SkPixelRef* pixel_ref, | 200 SkPixelRef* pixel_ref, |
196 bool was_canceled); | 201 bool was_canceled); |
197 void OnRasterTaskCompleted(Tile::Id tile, | 202 void OnRasterTaskCompleted(Tile::Id tile, |
198 scoped_ptr<ScopedResource> resource, | 203 scoped_ptr<ScopedResource> resource, |
199 RasterMode raster_mode, | 204 RasterMode raster_mode, |
200 const PicturePileImpl::Analysis& analysis, | 205 const PicturePileImpl::Analysis& analysis, |
201 bool was_canceled); | 206 bool was_canceled); |
202 | 207 |
203 inline size_t BytesConsumedIfAllocated(const Tile* tile) const { | |
204 return Resource::MemorySizeBytes(tile->size(), | |
205 resource_pool_->resource_format()); | |
206 } | |
207 | |
208 void FreeResourceForTile(Tile* tile, RasterMode mode); | 208 void FreeResourceForTile(Tile* tile, RasterMode mode); |
209 void FreeResourcesForTile(Tile* tile); | 209 void FreeResourcesForTile(Tile* tile); |
210 void FreeUnusedResourcesForTile(Tile* tile); | 210 void FreeUnusedResourcesForTile(Tile* tile); |
211 void FreeResourcesForTileAndNotifyClientIfTileWasReadyToDraw(Tile* tile); | 211 void FreeResourcesForTileAndNotifyClientIfTileWasReadyToDraw(Tile* tile); |
212 scoped_refptr<ImageDecodeTask> CreateImageDecodeTask(Tile* tile, | 212 scoped_refptr<ImageDecodeTask> CreateImageDecodeTask(Tile* tile, |
213 SkPixelRef* pixel_ref); | 213 SkPixelRef* pixel_ref); |
214 scoped_refptr<RasterTask> CreateRasterTask(Tile* tile); | 214 scoped_refptr<RasterTask> CreateRasterTask(Tile* tile); |
215 void UpdatePrioritizedTileSetIfNeeded(); | |
216 | 215 |
| 216 void RebuildEvictionQueueIfNeeded(); |
| 217 bool FreeTileResourcesUntilUsageIsWithinLimit(const MemoryUsage& limit, |
| 218 MemoryUsage* usage); |
| 219 bool FreeTileResourcesWithLowerPriorityUntilUsageIsWithinLimit( |
| 220 const MemoryUsage& limit, |
| 221 const TilePriority& oother_priority, |
| 222 MemoryUsage* usage); |
| 223 bool TilePriorityViolatesMemoryPolicy(const TilePriority& priority); |
217 bool IsReadyToActivate() const; | 224 bool IsReadyToActivate() const; |
218 void CheckIfReadyToActivate(); | 225 void CheckIfReadyToActivate(); |
219 | 226 |
220 TileManagerClient* client_; | 227 TileManagerClient* client_; |
221 scoped_refptr<base::SequencedTaskRunner> task_runner_; | 228 scoped_refptr<base::SequencedTaskRunner> task_runner_; |
222 ResourcePool* resource_pool_; | 229 ResourcePool* resource_pool_; |
223 Rasterizer* rasterizer_; | 230 Rasterizer* rasterizer_; |
224 GlobalStateThatImpactsTilePriority global_state_; | 231 GlobalStateThatImpactsTilePriority global_state_; |
225 | 232 |
226 typedef base::hash_map<Tile::Id, Tile*> TileMap; | 233 typedef base::hash_map<Tile::Id, Tile*> TileMap; |
227 TileMap tiles_; | 234 TileMap tiles_; |
228 | 235 |
229 PrioritizedTileSet prioritized_tiles_; | 236 bool all_tiles_that_need_to_be_rasterized_are_scheduled_; |
230 bool prioritized_tiles_dirty_; | |
231 | |
232 bool all_tiles_that_need_to_be_rasterized_have_memory_; | |
233 bool all_tiles_required_for_activation_have_memory_; | |
234 | |
235 size_t bytes_releasable_; | |
236 size_t resources_releasable_; | |
237 | |
238 bool ever_exceeded_memory_budget_; | |
239 MemoryHistory::Entry memory_stats_from_last_assign_; | 237 MemoryHistory::Entry memory_stats_from_last_assign_; |
240 | 238 |
241 RenderingStatsInstrumentation* rendering_stats_instrumentation_; | 239 RenderingStatsInstrumentation* rendering_stats_instrumentation_; |
242 | 240 |
243 bool did_initialize_visible_tile_; | 241 bool did_initialize_visible_tile_; |
244 bool did_check_for_completed_tasks_since_last_schedule_tasks_; | 242 bool did_check_for_completed_tasks_since_last_schedule_tasks_; |
245 bool did_oom_on_last_assign_; | 243 bool did_oom_on_last_assign_; |
246 | 244 |
247 typedef base::hash_map<uint32_t, scoped_refptr<ImageDecodeTask> > | 245 typedef base::hash_map<uint32_t, scoped_refptr<ImageDecodeTask> > |
248 PixelRefTaskMap; | 246 PixelRefTaskMap; |
249 typedef base::hash_map<int, PixelRefTaskMap> LayerPixelRefTaskMap; | 247 typedef base::hash_map<int, PixelRefTaskMap> LayerPixelRefTaskMap; |
250 LayerPixelRefTaskMap image_decode_tasks_; | 248 LayerPixelRefTaskMap image_decode_tasks_; |
251 | 249 |
252 typedef base::hash_map<int, int> LayerCountMap; | 250 typedef base::hash_map<int, int> LayerCountMap; |
253 LayerCountMap used_layer_counts_; | 251 LayerCountMap used_layer_counts_; |
254 | 252 |
255 RasterTaskCompletionStats update_visible_tiles_stats_; | 253 RasterTaskCompletionStats update_visible_tiles_stats_; |
256 | 254 |
257 std::vector<Tile*> released_tiles_; | 255 std::vector<Tile*> released_tiles_; |
258 | 256 |
259 ResourceFormat resource_format_; | 257 ResourceFormat resource_format_; |
260 | 258 |
261 // Queue used when scheduling raster tasks. | 259 // Queue used when scheduling raster tasks. |
262 RasterTaskQueue raster_queue_; | 260 RasterTaskQueue raster_queue_; |
263 | 261 |
264 std::vector<scoped_refptr<RasterTask> > orphan_raster_tasks_; | 262 std::vector<scoped_refptr<RasterTask> > orphan_raster_tasks_; |
265 | 263 |
266 UniqueNotifier ready_to_activate_check_notifier_; | 264 UniqueNotifier ready_to_activate_check_notifier_; |
267 | 265 |
| 266 RasterTilePriorityQueue raster_priority_queue_; |
| 267 EvictionTilePriorityQueue eviction_priority_queue_; |
| 268 bool eviction_priority_queue_is_up_to_date_; |
| 269 |
268 DISALLOW_COPY_AND_ASSIGN(TileManager); | 270 DISALLOW_COPY_AND_ASSIGN(TileManager); |
269 }; | 271 }; |
270 | 272 |
271 } // namespace cc | 273 } // namespace cc |
272 | 274 |
273 #endif // CC_RESOURCES_TILE_MANAGER_H_ | 275 #endif // CC_RESOURCES_TILE_MANAGER_H_ |
OLD | NEW |