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
3 // found in the LICENSE file.
5 #ifndef CC_RESOURCES_TILE_MANAGER_H_
6 #define CC_RESOURCES_TILE_MANAGER_H_
12 #include "base/containers/hash_tables.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/values.h"
15 #include "cc/base/ref_counted_managed.h"
16 #include "cc/debug/rendering_stats_instrumentation.h"
17 #include "cc/resources/managed_tile_state.h"
18 #include "cc/resources/memory_history.h"
19 #include "cc/resources/picture_pile_impl.h"
20 #include "cc/resources/prioritized_tile_set.h"
21 #include "cc/resources/raster_worker_pool.h"
22 #include "cc/resources/resource_pool.h"
23 #include "cc/resources/tile.h"
26 class ResourceProvider;
28 class CC_EXPORT TileManagerClient {
30 virtual void NotifyReadyToActivate() = 0;
33 virtual ~TileManagerClient() {}
36 struct RasterTaskCompletionStats {
37 RasterTaskCompletionStats();
39 size_t completed_count;
40 size_t canceled_count;
42 scoped_ptr<base::Value> RasterTaskCompletionStatsAsValue(
43 const RasterTaskCompletionStats& stats);
45 // This class manages tiles, deciding which should get rasterized and which
46 // should no longer have any memory assigned to them. Tile objects are "owned"
47 // by layers; they automatically register with the manager when they are
48 // created, and unregister from the manager when they are deleted.
49 class CC_EXPORT TileManager : public RasterWorkerPoolClient,
50 public RefCountedManager<Tile> {
52 static scoped_ptr<TileManager> Create(
53 TileManagerClient* client,
54 ResourceProvider* resource_provider,
55 size_t num_raster_threads,
56 RenderingStatsInstrumentation* rendering_stats_instrumentation,
58 size_t max_transfer_buffer_usage_bytes);
59 virtual ~TileManager();
61 void ManageTiles(const GlobalStateThatImpactsTilePriority& state);
63 // Returns true when visible tiles have been initialized.
64 bool UpdateVisibleTiles();
66 scoped_refptr<Tile> CreateTile(PicturePileImpl* picture_pile,
68 gfx::Rect content_rect,
69 gfx::Rect opaque_rect,
72 int source_frame_number,
73 bool can_use_lcd_text);
75 scoped_ptr<base::Value> BasicStateAsValue() const;
76 scoped_ptr<base::Value> AllTilesAsValue() const;
77 void GetMemoryStats(size_t* memory_required_bytes,
78 size_t* memory_nice_to_have_bytes,
79 size_t* memory_allocated_bytes,
80 size_t* memory_used_bytes) const;
82 const MemoryHistory::Entry& memory_stats_from_last_assign() const {
83 return memory_stats_from_last_assign_;
86 void InitializeTilesWithResourcesForTesting(
87 const std::vector<Tile*>& tiles,
88 ResourceProvider* resource_provider) {
89 for (size_t i = 0; i < tiles.size(); ++i) {
90 ManagedTileState& mts = tiles[i]->managed_state();
91 ManagedTileState::TileVersion& tile_version =
92 mts.tile_versions[HIGH_QUALITY_NO_LCD_RASTER_MODE];
94 tile_version.resource_ = make_scoped_ptr(
95 new ResourcePool::Resource(resource_provider,
97 resource_provider->best_texture_format()));
99 bytes_releasable_ += BytesConsumedIfAllocated(tiles[i]);
100 ++resources_releasable_;
103 RasterWorkerPool* RasterWorkerPoolForTesting() {
104 return raster_worker_pool_.get();
108 TileManager(TileManagerClient* client,
109 ResourceProvider* resource_provider,
110 scoped_ptr<RasterWorkerPool> raster_worker_pool,
111 size_t num_raster_threads,
112 RenderingStatsInstrumentation* rendering_stats_instrumentation);
114 // Methods called by Tile
116 void DidChangeTilePriority(Tile* tile);
118 void CleanUpReleasedTiles();
120 // Overriden from RefCountedManager<Tile>:
121 virtual void Release(Tile* tile) OVERRIDE;
123 // Overriden from RasterWorkerPoolClient:
124 virtual bool ShouldForceTasksRequiredForActivationToComplete() const
126 virtual void DidFinishRunningTasks() OVERRIDE;
127 virtual void DidFinishRunningTasksRequiredForActivation() OVERRIDE;
129 typedef std::vector<Tile*> TileVector;
130 typedef std::set<Tile*> TileSet;
133 virtual void ScheduleTasks(
134 const TileVector& tiles_that_need_to_be_rasterized);
136 void AssignGpuMemoryToTiles(
137 PrioritizedTileSet* tiles,
138 TileVector* tiles_that_need_to_be_rasterized);
139 void GetTilesWithAssignedBins(PrioritizedTileSet* tiles);
142 void OnImageDecodeTaskCompleted(
144 skia::LazyPixelRef* pixel_ref,
146 void OnRasterTaskCompleted(
148 scoped_ptr<ResourcePool::Resource> resource,
149 RasterMode raster_mode,
150 const PicturePileImpl::Analysis& analysis,
153 inline size_t BytesConsumedIfAllocated(const Tile* tile) const {
154 return Resource::MemorySizeBytes(tile->size(),
155 raster_worker_pool_->GetResourceFormat());
158 RasterMode DetermineRasterMode(const Tile* tile) const;
159 void FreeResourceForTile(Tile* tile, RasterMode mode);
160 void FreeResourcesForTile(Tile* tile);
161 void FreeUnusedResourcesForTile(Tile* tile);
162 RasterWorkerPool::Task CreateImageDecodeTask(
163 Tile* tile, skia::LazyPixelRef* pixel_ref);
164 RasterWorkerPool::RasterTask CreateRasterTask(Tile* tile);
165 scoped_ptr<base::Value> GetMemoryRequirementsAsValue() const;
166 void UpdatePrioritizedTileSetIfNeeded();
168 TileManagerClient* client_;
169 scoped_ptr<ResourcePool> resource_pool_;
170 scoped_ptr<RasterWorkerPool> raster_worker_pool_;
171 GlobalStateThatImpactsTilePriority global_state_;
173 typedef base::hash_map<Tile::Id, Tile*> TileMap;
176 PrioritizedTileSet prioritized_tiles_;
177 bool prioritized_tiles_dirty_;
179 bool all_tiles_that_need_to_be_rasterized_have_memory_;
180 bool all_tiles_required_for_activation_have_memory_;
182 size_t memory_required_bytes_;
183 size_t memory_nice_to_have_bytes_;
185 size_t bytes_releasable_;
186 size_t resources_releasable_;
188 bool ever_exceeded_memory_budget_;
189 MemoryHistory::Entry memory_stats_from_last_assign_;
191 RenderingStatsInstrumentation* rendering_stats_instrumentation_;
193 bool did_initialize_visible_tile_;
194 bool did_check_for_completed_tasks_since_last_schedule_tasks_;
196 typedef base::hash_map<uint32_t, RasterWorkerPool::Task> PixelRefTaskMap;
197 typedef base::hash_map<int, PixelRefTaskMap> LayerPixelRefTaskMap;
198 LayerPixelRefTaskMap image_decode_tasks_;
200 typedef base::hash_map<int, int> LayerCountMap;
201 LayerCountMap used_layer_counts_;
203 RasterTaskCompletionStats update_visible_tiles_stats_;
205 std::vector<Tile*> released_tiles_;
207 DISALLOW_COPY_AND_ASSIGN(TileManager);
212 #endif // CC_RESOURCES_TILE_MANAGER_H_