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 PictureLayerImpl;
27 class RasterWorkerPoolDelegate;
28 class ResourceProvider;
30 class CC_EXPORT TileManagerClient {
32 virtual void NotifyReadyToActivate() = 0;
35 virtual ~TileManagerClient() {}
38 struct RasterTaskCompletionStats {
39 RasterTaskCompletionStats();
41 size_t completed_count;
42 size_t canceled_count;
44 scoped_ptr<base::Value> RasterTaskCompletionStatsAsValue(
45 const RasterTaskCompletionStats& stats);
47 // This class manages tiles, deciding which should get rasterized and which
48 // should no longer have any memory assigned to them. Tile objects are "owned"
49 // by layers; they automatically register with the manager when they are
50 // created, and unregister from the manager when they are deleted.
51 class CC_EXPORT TileManager : public RasterWorkerPoolClient,
52 public RefCountedManager<Tile> {
54 struct CC_EXPORT PairedPictureLayer {
56 ~PairedPictureLayer();
58 PictureLayerImpl* active_layer;
59 PictureLayerImpl* pending_layer;
62 static scoped_ptr<TileManager> Create(
63 TileManagerClient* client,
64 base::SequencedTaskRunner* task_runner,
65 ResourceProvider* resource_provider,
66 ContextProvider* context_provider,
67 RenderingStatsInstrumentation* rendering_stats_instrumentation,
69 bool use_rasterize_on_demand,
70 size_t max_transfer_buffer_usage_bytes,
71 size_t max_raster_usage_bytes,
72 unsigned map_image_texture_target);
73 virtual ~TileManager();
75 void ManageTiles(const GlobalStateThatImpactsTilePriority& state);
77 // Returns true when visible tiles have been initialized.
78 bool UpdateVisibleTiles();
80 scoped_refptr<Tile> CreateTile(PicturePileImpl* picture_pile,
81 const gfx::Size& tile_size,
82 const gfx::Rect& content_rect,
83 const gfx::Rect& opaque_rect,
86 int source_frame_number,
89 void RegisterPictureLayerImpl(PictureLayerImpl* layer);
90 void UnregisterPictureLayerImpl(PictureLayerImpl* layer);
92 scoped_ptr<base::Value> BasicStateAsValue() const;
93 scoped_ptr<base::Value> AllTilesAsValue() const;
94 void GetMemoryStats(size_t* memory_required_bytes,
95 size_t* memory_nice_to_have_bytes,
96 size_t* memory_allocated_bytes,
97 size_t* memory_used_bytes) const;
99 const MemoryHistory::Entry& memory_stats_from_last_assign() const {
100 return memory_stats_from_last_assign_;
103 void GetPairedPictureLayers(std::vector<PairedPictureLayer>* layers) const;
105 void InitializeTilesWithResourcesForTesting(
106 const std::vector<Tile*>& tiles,
107 ResourceProvider* resource_provider) {
108 for (size_t i = 0; i < tiles.size(); ++i) {
109 ManagedTileState& mts = tiles[i]->managed_state();
110 ManagedTileState::TileVersion& tile_version =
111 mts.tile_versions[HIGH_QUALITY_NO_LCD_RASTER_MODE];
113 tile_version.resource_ = resource_pool_->AcquireResource(gfx::Size(1, 1));
115 bytes_releasable_ += BytesConsumedIfAllocated(tiles[i]);
116 ++resources_releasable_;
120 void SetGlobalStateForTesting(
121 const GlobalStateThatImpactsTilePriority& state) {
122 // Soft limit is used for resource pool such that
123 // memory returns to soft limit after going over.
124 if (state != global_state_) {
125 global_state_ = state;
126 prioritized_tiles_dirty_ = true;
127 resource_pool_->SetResourceUsageLimits(
128 global_state_.soft_memory_limit_in_bytes,
129 global_state_.unused_memory_limit_in_bytes,
130 global_state_.num_resources_limit);
135 TileManager(TileManagerClient* client,
136 base::SequencedTaskRunner* task_runner,
137 ResourceProvider* resource_provider,
138 ContextProvider* context_provider,
139 scoped_ptr<RasterWorkerPool> raster_worker_pool,
140 scoped_ptr<RasterWorkerPool> direct_raster_worker_pool,
141 size_t max_raster_usage_bytes,
142 RenderingStatsInstrumentation* rendering_stats_instrumentation,
143 bool use_rasterize_on_demand);
145 // Methods called by Tile
147 void DidChangeTilePriority(Tile* tile);
149 void CleanUpReleasedTiles();
151 // Overriden from RefCountedManager<Tile>:
152 virtual void Release(Tile* tile) OVERRIDE;
154 // Overriden from RasterWorkerPoolClient:
155 virtual bool ShouldForceTasksRequiredForActivationToComplete() const OVERRIDE;
156 virtual void DidFinishRunningTasks() OVERRIDE;
157 virtual void DidFinishRunningTasksRequiredForActivation() OVERRIDE;
159 typedef std::vector<Tile*> TileVector;
160 typedef std::set<Tile*> TileSet;
163 virtual void ScheduleTasks(
164 const TileVector& tiles_that_need_to_be_rasterized);
166 void AssignGpuMemoryToTiles(PrioritizedTileSet* tiles,
167 TileVector* tiles_that_need_to_be_rasterized);
168 void GetTilesWithAssignedBins(PrioritizedTileSet* tiles);
171 enum RasterWorkerPoolType {
172 RASTER_WORKER_POOL_TYPE_DEFAULT,
173 RASTER_WORKER_POOL_TYPE_DIRECT,
174 NUM_RASTER_WORKER_POOL_TYPES
177 void OnImageDecodeTaskCompleted(int layer_id,
178 SkPixelRef* pixel_ref,
180 void OnRasterTaskCompleted(Tile::Id tile,
181 scoped_ptr<ScopedResource> resource,
182 RasterMode raster_mode,
183 const PicturePileImpl::Analysis& analysis,
186 inline size_t BytesConsumedIfAllocated(const Tile* tile) const {
187 return Resource::MemorySizeBytes(tile->size(),
188 raster_worker_pool_->GetResourceFormat());
191 void FreeResourceForTile(Tile* tile, RasterMode mode);
192 void FreeResourcesForTile(Tile* tile);
193 void FreeUnusedResourcesForTile(Tile* tile);
194 scoped_refptr<internal::WorkerPoolTask> CreateImageDecodeTask(
196 SkPixelRef* pixel_ref);
197 scoped_refptr<internal::RasterWorkerPoolTask> CreateRasterTask(Tile* tile);
198 scoped_ptr<base::Value> GetMemoryRequirementsAsValue() const;
199 void UpdatePrioritizedTileSetIfNeeded();
201 TileManagerClient* client_;
202 ContextProvider* context_provider_;
203 scoped_ptr<ResourcePool> resource_pool_;
204 scoped_ptr<RasterWorkerPool> raster_worker_pool_;
205 scoped_ptr<RasterWorkerPool> direct_raster_worker_pool_;
206 scoped_ptr<RasterWorkerPoolDelegate> raster_worker_pool_delegate_;
207 GlobalStateThatImpactsTilePriority global_state_;
209 typedef base::hash_map<Tile::Id, Tile*> TileMap;
212 PrioritizedTileSet prioritized_tiles_;
213 bool prioritized_tiles_dirty_;
215 bool all_tiles_that_need_to_be_rasterized_have_memory_;
216 bool all_tiles_required_for_activation_have_memory_;
218 size_t memory_required_bytes_;
219 size_t memory_nice_to_have_bytes_;
221 size_t bytes_releasable_;
222 size_t resources_releasable_;
223 size_t max_raster_usage_bytes_;
225 bool ever_exceeded_memory_budget_;
226 MemoryHistory::Entry memory_stats_from_last_assign_;
228 RenderingStatsInstrumentation* rendering_stats_instrumentation_;
230 bool did_initialize_visible_tile_;
231 bool did_check_for_completed_tasks_since_last_schedule_tasks_;
233 typedef base::hash_map<uint32_t, scoped_refptr<internal::WorkerPoolTask> >
235 typedef base::hash_map<int, PixelRefTaskMap> LayerPixelRefTaskMap;
236 LayerPixelRefTaskMap image_decode_tasks_;
238 typedef base::hash_map<int, int> LayerCountMap;
239 LayerCountMap used_layer_counts_;
241 RasterTaskCompletionStats update_visible_tiles_stats_;
243 std::vector<Tile*> released_tiles_;
245 bool use_rasterize_on_demand_;
247 // Queues used when scheduling raster tasks.
248 RasterTaskQueue raster_queue_[NUM_RASTER_WORKER_POOL_TYPES];
250 std::vector<scoped_refptr<internal::Task> > orphan_raster_tasks_;
252 std::vector<PictureLayerImpl*> layers_;
254 DISALLOW_COPY_AND_ASSIGN(TileManager);
259 #endif // CC_RESOURCES_TILE_MANAGER_H_