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