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