| 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/layers/picture_layer_impl.h" | 20 #include "cc/layers/picture_layer_impl.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/rasterizer.h" | 24 #include "cc/resources/rasterizer.h" |
| 26 #include "cc/resources/resource_pool.h" | 25 #include "cc/resources/resource_pool.h" |
| 27 #include "cc/resources/tile.h" | 26 #include "cc/resources/tile.h" |
| 28 | 27 |
| 29 namespace cc { | 28 namespace cc { |
| 30 class ResourceProvider; | 29 class ResourceProvider; |
| 31 | 30 |
| 32 class CC_EXPORT TileManagerClient { | 31 class CC_EXPORT TileManagerClient { |
| 33 public: | 32 public: |
| 34 // Returns the set of layers that the tile manager should consider for raster. | 33 // Returns the set of layers that the tile manager should consider for raster. |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 197 } | 196 } |
| 198 | 197 |
| 199 void GetPairedPictureLayers(std::vector<PairedPictureLayer>* layers) const; | 198 void GetPairedPictureLayers(std::vector<PairedPictureLayer>* layers) const; |
| 200 | 199 |
| 201 void InitializeTilesWithResourcesForTesting(const std::vector<Tile*>& tiles) { | 200 void InitializeTilesWithResourcesForTesting(const std::vector<Tile*>& tiles) { |
| 202 for (size_t i = 0; i < tiles.size(); ++i) { | 201 for (size_t i = 0; i < tiles.size(); ++i) { |
| 203 ManagedTileState& mts = tiles[i]->managed_state(); | 202 ManagedTileState& mts = tiles[i]->managed_state(); |
| 204 ManagedTileState::TileVersion& tile_version = | 203 ManagedTileState::TileVersion& tile_version = |
| 205 mts.tile_versions[HIGH_QUALITY_RASTER_MODE]; | 204 mts.tile_versions[HIGH_QUALITY_RASTER_MODE]; |
| 206 | 205 |
| 207 tile_version.resource_ = resource_pool_->AcquireResource(gfx::Size(1, 1)); | 206 tile_version.resource_ = |
| 207 resource_pool_->AcquireResource(tiles[i]->size()); |
| 208 | 208 |
| 209 bytes_releasable_ += BytesConsumedIfAllocated(tiles[i]); | 209 releasable_usage_ += MemoryUsage::FromConfig( |
| 210 ++resources_releasable_; | 210 tiles[i]->size(), tile_version.resource_->format()); |
| 211 } | 211 } |
| 212 } | 212 } |
| 213 | 213 |
| 214 void ReleaseTileResourcesForTesting(const std::vector<Tile*>& tiles) { | 214 void ReleaseTileResourcesForTesting(const std::vector<Tile*>& tiles) { |
| 215 for (size_t i = 0; i < tiles.size(); ++i) { | 215 for (size_t i = 0; i < tiles.size(); ++i) { |
| 216 Tile* tile = tiles[i]; | 216 Tile* tile = tiles[i]; |
| 217 for (int mode = 0; mode < NUM_RASTER_MODES; ++mode) { | 217 for (int mode = 0; mode < NUM_RASTER_MODES; ++mode) { |
| 218 FreeResourceForTile(tile, static_cast<RasterMode>(mode)); | 218 FreeResourceForTile(tile, static_cast<RasterMode>(mode)); |
| 219 } | 219 } |
| 220 } | 220 } |
| 221 } | 221 } |
| 222 | 222 |
| 223 void SetGlobalStateForTesting( | 223 void SetGlobalStateForTesting( |
| 224 const GlobalStateThatImpactsTilePriority& state) { | 224 const GlobalStateThatImpactsTilePriority& state) { |
| 225 // Soft limit is used for resource pool such that | 225 global_state_ = state; |
| 226 // memory returns to soft limit after going over. | |
| 227 if (state != global_state_) { | |
| 228 global_state_ = state; | |
| 229 prioritized_tiles_dirty_ = true; | |
| 230 } | |
| 231 } | 226 } |
| 232 | 227 |
| 233 void SetRasterizerForTesting(Rasterizer* rasterizer); | 228 void SetRasterizerForTesting(Rasterizer* rasterizer); |
| 234 | 229 |
| 235 void CleanUpReleasedTilesForTesting() { CleanUpReleasedTiles(); } | 230 void CleanUpReleasedTilesForTesting() { CleanUpReleasedTiles(); } |
| 236 | 231 |
| 237 protected: | 232 protected: |
| 238 TileManager(TileManagerClient* client, | 233 TileManager(TileManagerClient* client, |
| 239 base::SequencedTaskRunner* task_runner, | 234 base::SequencedTaskRunner* task_runner, |
| 240 ResourcePool* resource_pool, | 235 ResourcePool* resource_pool, |
| 241 Rasterizer* rasterizer, | 236 Rasterizer* rasterizer, |
| 242 RenderingStatsInstrumentation* rendering_stats_instrumentation); | 237 RenderingStatsInstrumentation* rendering_stats_instrumentation); |
| 243 | 238 |
| 244 // Methods called by Tile | |
| 245 friend class Tile; | |
| 246 void DidChangeTilePriority(Tile* tile); | |
| 247 | |
| 248 void CleanUpReleasedTiles(); | 239 void CleanUpReleasedTiles(); |
| 249 | 240 |
| 250 // Overriden from RefCountedManager<Tile>: | 241 // Overriden from RefCountedManager<Tile>: |
| 242 friend class Tile; |
| 251 virtual void Release(Tile* tile) OVERRIDE; | 243 virtual void Release(Tile* tile) OVERRIDE; |
| 252 | 244 |
| 253 // Overriden from RasterizerClient: | 245 // Overriden from RasterizerClient: |
| 254 virtual bool ShouldForceTasksRequiredForActivationToComplete() const OVERRIDE; | 246 virtual bool ShouldForceTasksRequiredForActivationToComplete() const OVERRIDE; |
| 255 virtual void DidFinishRunningTasks() OVERRIDE; | 247 virtual void DidFinishRunningTasks() OVERRIDE; |
| 256 virtual void DidFinishRunningTasksRequiredForActivation() OVERRIDE; | 248 virtual void DidFinishRunningTasksRequiredForActivation() OVERRIDE; |
| 257 | 249 |
| 258 typedef std::vector<Tile*> TileVector; | 250 typedef std::vector<Tile*> TileVector; |
| 259 typedef std::set<Tile*> TileSet; | 251 typedef std::set<Tile*> TileSet; |
| 260 | 252 |
| 261 // Virtual for test | 253 // Virtual for test |
| 262 virtual void ScheduleTasks( | 254 virtual void ScheduleTasks( |
| 263 const TileVector& tiles_that_need_to_be_rasterized); | 255 const TileVector& tiles_that_need_to_be_rasterized); |
| 264 | 256 |
| 265 void AssignGpuMemoryToTiles(PrioritizedTileSet* tiles, | 257 void AssignGpuMemoryToTiles(TileVector* tiles_that_need_to_be_rasterized); |
| 266 TileVector* tiles_that_need_to_be_rasterized); | |
| 267 void GetTilesWithAssignedBins(PrioritizedTileSet* tiles); | |
| 268 | 258 |
| 269 private: | 259 private: |
| 260 class MemoryUsage { |
| 261 public: |
| 262 MemoryUsage(); |
| 263 MemoryUsage(int64 memory_bytes, int resource_count); |
| 264 |
| 265 static MemoryUsage FromConfig(const gfx::Size& size, ResourceFormat format); |
| 266 static MemoryUsage FromTile(const Tile* tile); |
| 267 |
| 268 MemoryUsage& operator+=(const MemoryUsage& other); |
| 269 MemoryUsage& operator-=(const MemoryUsage& other); |
| 270 MemoryUsage operator-(const MemoryUsage& other); |
| 271 |
| 272 bool Exceeds(const MemoryUsage& limit) const; |
| 273 |
| 274 int64 memory_bytes() const { return memory_bytes_; } |
| 275 |
| 276 private: |
| 277 int64 memory_bytes_; |
| 278 int resource_count_; |
| 279 }; |
| 280 |
| 270 void OnImageDecodeTaskCompleted(int layer_id, | 281 void OnImageDecodeTaskCompleted(int layer_id, |
| 271 SkPixelRef* pixel_ref, | 282 SkPixelRef* pixel_ref, |
| 272 bool was_canceled); | 283 bool was_canceled); |
| 273 void OnRasterTaskCompleted(Tile::Id tile, | 284 void OnRasterTaskCompleted(Tile::Id tile, |
| 274 scoped_ptr<ScopedResource> resource, | 285 scoped_ptr<ScopedResource> resource, |
| 275 RasterMode raster_mode, | 286 RasterMode raster_mode, |
| 276 const PicturePileImpl::Analysis& analysis, | 287 const PicturePileImpl::Analysis& analysis, |
| 277 bool was_canceled); | 288 bool was_canceled); |
| 278 | 289 |
| 279 inline size_t BytesConsumedIfAllocated(const Tile* tile) const { | |
| 280 return Resource::MemorySizeBytes(tile->size(), | |
| 281 resource_pool_->resource_format()); | |
| 282 } | |
| 283 | |
| 284 void FreeResourceForTile(Tile* tile, RasterMode mode); | 290 void FreeResourceForTile(Tile* tile, RasterMode mode); |
| 285 void FreeResourcesForTile(Tile* tile); | 291 void FreeResourcesForTile(Tile* tile); |
| 286 void FreeUnusedResourcesForTile(Tile* tile); | 292 void FreeUnusedResourcesForTile(Tile* tile); |
| 287 void FreeResourcesForTileAndNotifyClientIfTileWasReadyToDraw(Tile* tile); | 293 void FreeResourcesForTileAndNotifyClientIfTileWasReadyToDraw(Tile* tile); |
| 288 scoped_refptr<ImageDecodeTask> CreateImageDecodeTask(Tile* tile, | 294 scoped_refptr<ImageDecodeTask> CreateImageDecodeTask(Tile* tile, |
| 289 SkPixelRef* pixel_ref); | 295 SkPixelRef* pixel_ref); |
| 290 scoped_refptr<RasterTask> CreateRasterTask(Tile* tile); | 296 scoped_refptr<RasterTask> CreateRasterTask(Tile* tile); |
| 291 scoped_ptr<base::Value> GetMemoryRequirementsAsValue() const; | 297 scoped_ptr<base::Value> GetMemoryRequirementsAsValue() const; |
| 292 void UpdatePrioritizedTileSetIfNeeded(); | |
| 293 | 298 |
| 299 bool FreeTileResourcesUntilUsageIsWithinLimit(EvictionTileIterator* iterator, |
| 300 const MemoryUsage& limit, |
| 301 MemoryUsage* usage); |
| 302 bool FreeTileResourcesWithLowerPriorityUntilUsageIsWithinLimit( |
| 303 EvictionTileIterator* iterator, |
| 304 const MemoryUsage& limit, |
| 305 const TilePriority& max_priority, |
| 306 MemoryUsage* usage); |
| 307 bool TilePriorityViolatesMemoryPolicy(const TilePriority& priority); |
| 294 bool IsReadyToActivate() const; | 308 bool IsReadyToActivate() const; |
| 295 void CheckIfReadyToActivate(); | 309 void CheckIfReadyToActivate(); |
| 296 | 310 |
| 297 TileManagerClient* client_; | 311 TileManagerClient* client_; |
| 298 scoped_refptr<base::SequencedTaskRunner> task_runner_; | 312 scoped_refptr<base::SequencedTaskRunner> task_runner_; |
| 299 ResourcePool* resource_pool_; | 313 ResourcePool* resource_pool_; |
| 300 Rasterizer* rasterizer_; | 314 Rasterizer* rasterizer_; |
| 301 GlobalStateThatImpactsTilePriority global_state_; | 315 GlobalStateThatImpactsTilePriority global_state_; |
| 302 | 316 |
| 303 typedef base::hash_map<Tile::Id, Tile*> TileMap; | 317 typedef base::hash_map<Tile::Id, Tile*> TileMap; |
| 304 TileMap tiles_; | 318 TileMap tiles_; |
| 305 | 319 |
| 306 PrioritizedTileSet prioritized_tiles_; | |
| 307 bool prioritized_tiles_dirty_; | |
| 308 | |
| 309 bool all_tiles_that_need_to_be_rasterized_have_memory_; | 320 bool all_tiles_that_need_to_be_rasterized_have_memory_; |
| 310 bool all_tiles_required_for_activation_have_memory_; | 321 bool all_tiles_required_for_activation_have_memory_; |
| 311 | 322 |
| 312 size_t memory_required_bytes_; | 323 MemoryUsage releasable_usage_; |
| 313 size_t memory_nice_to_have_bytes_; | 324 size_t bytes_required_but_not_allocated_; |
| 314 | |
| 315 size_t bytes_releasable_; | |
| 316 size_t resources_releasable_; | |
| 317 | 325 |
| 318 bool ever_exceeded_memory_budget_; | 326 bool ever_exceeded_memory_budget_; |
| 319 MemoryHistory::Entry memory_stats_from_last_assign_; | 327 MemoryHistory::Entry memory_stats_from_last_assign_; |
| 320 | 328 |
| 321 RenderingStatsInstrumentation* rendering_stats_instrumentation_; | 329 RenderingStatsInstrumentation* rendering_stats_instrumentation_; |
| 322 | 330 |
| 323 bool did_initialize_visible_tile_; | 331 bool did_initialize_visible_tile_; |
| 324 bool did_check_for_completed_tasks_since_last_schedule_tasks_; | 332 bool did_check_for_completed_tasks_since_last_schedule_tasks_; |
| 325 | 333 |
| 326 typedef base::hash_map<uint32_t, scoped_refptr<ImageDecodeTask> > | 334 typedef base::hash_map<uint32_t, scoped_refptr<ImageDecodeTask> > |
| (...skipping 16 matching lines...) Expand all Loading... |
| 343 std::vector<scoped_refptr<RasterTask> > orphan_raster_tasks_; | 351 std::vector<scoped_refptr<RasterTask> > orphan_raster_tasks_; |
| 344 | 352 |
| 345 UniqueNotifier ready_to_activate_check_notifier_; | 353 UniqueNotifier ready_to_activate_check_notifier_; |
| 346 | 354 |
| 347 DISALLOW_COPY_AND_ASSIGN(TileManager); | 355 DISALLOW_COPY_AND_ASSIGN(TileManager); |
| 348 }; | 356 }; |
| 349 | 357 |
| 350 } // namespace cc | 358 } // namespace cc |
| 351 | 359 |
| 352 #endif // CC_RESOURCES_TILE_MANAGER_H_ | 360 #endif // CC_RESOURCES_TILE_MANAGER_H_ |
| OLD | NEW |