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_
14 #include "base/containers/hash_tables.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/values.h"
17 #include "cc/base/ref_counted_managed.h"
18 #include "cc/base/unique_notifier.h"
19 #include "cc/debug/rendering_stats_instrumentation.h"
20 #include "cc/resources/eviction_tile_priority_queue.h"
21 #include "cc/resources/managed_tile_state.h"
22 #include "cc/resources/memory_history.h"
23 #include "cc/resources/raster_source.h"
24 #include "cc/resources/raster_tile_priority_queue.h"
25 #include "cc/resources/rasterizer.h"
26 #include "cc/resources/resource_pool.h"
27 #include "cc/resources/tile.h"
31 class ConvertableToTraceFormat;
37 class PictureLayerImpl;
38 class ResourceProvider;
40 class CC_EXPORT TileManagerClient {
42 // Returns the set of layers that the tile manager should consider for raster.
43 // TODO(vmpstr): Change the way we determine if we are ready to activate, so
44 // that this can be removed.
45 virtual const std::vector<PictureLayerImpl*>& GetPictureLayers() const = 0;
47 // Called when all tiles marked as required for activation are ready to draw.
48 virtual void NotifyReadyToActivate() = 0;
50 // Called when the visible representation of a tile might have changed. Some
52 // - Tile version initialized.
53 // - Tile resources freed.
54 // - Tile marked for on-demand raster.
55 virtual void NotifyTileStateChanged(const Tile* tile) = 0;
57 // Given an empty raster tile priority queue, this will build a priority queue
58 // that will return tiles in order in which they should be rasterized.
59 // Note if the queue was previous built, Reset must be called on it.
60 virtual void BuildRasterQueue(RasterTilePriorityQueue* queue,
61 TreePriority tree_priority) = 0;
63 // Given an empty eviction tile priority queue, this will build a priority
64 // queue that will return tiles in order in which they should be evicted.
65 // Note if the queue was previous built, Reset must be called on it.
66 virtual void BuildEvictionQueue(EvictionTilePriorityQueue* queue,
67 TreePriority tree_priority) = 0;
70 virtual ~TileManagerClient() {}
73 struct RasterTaskCompletionStats {
74 RasterTaskCompletionStats();
76 size_t completed_count;
77 size_t canceled_count;
79 scoped_refptr<base::debug::ConvertableToTraceFormat>
80 RasterTaskCompletionStatsAsValue(const RasterTaskCompletionStats& stats);
82 // This class manages tiles, deciding which should get rasterized and which
83 // should no longer have any memory assigned to them. Tile objects are "owned"
84 // by layers; they automatically register with the manager when they are
85 // created, and unregister from the manager when they are deleted.
86 class CC_EXPORT TileManager : public RasterizerClient,
87 public RefCountedManager<Tile> {
90 REQUIRED_FOR_ACTIVATION = 0,
92 // Adding additional values requires increasing kNumberOfTaskSets in
96 static scoped_ptr<TileManager> Create(
97 TileManagerClient* client,
98 base::SequencedTaskRunner* task_runner,
99 ResourcePool* resource_pool,
100 Rasterizer* rasterizer,
101 RenderingStatsInstrumentation* rendering_stats_instrumentation,
102 size_t scheduled_raster_task_limit);
103 ~TileManager() override;
105 void ManageTiles(const GlobalStateThatImpactsTilePriority& state);
107 // Returns true when visible tiles have been initialized.
108 bool UpdateVisibleTiles();
110 scoped_refptr<Tile> CreateTile(RasterSource* raster_source,
111 const gfx::Size& tile_size,
112 const gfx::Rect& content_rect,
113 float contents_scale,
115 int source_frame_number,
118 scoped_refptr<base::debug::ConvertableToTraceFormat> BasicStateAsValue()
120 void BasicStateAsValueInto(base::debug::TracedValue* dict) const;
121 const MemoryHistory::Entry& memory_stats_from_last_assign() const {
122 return memory_stats_from_last_assign_;
125 void InitializeTilesWithResourcesForTesting(const std::vector<Tile*>& tiles) {
126 for (size_t i = 0; i < tiles.size(); ++i) {
127 ManagedTileState& mts = tiles[i]->managed_state();
129 mts.draw_info.resource_ =
130 resource_pool_->AcquireResource(tiles[i]->size());
134 void ReleaseTileResourcesForTesting(const std::vector<Tile*>& tiles) {
135 for (size_t i = 0; i < tiles.size(); ++i) {
136 Tile* tile = tiles[i];
137 FreeResourcesForTile(tile);
141 void SetGlobalStateForTesting(
142 const GlobalStateThatImpactsTilePriority& state) {
143 global_state_ = state;
146 void SetRasterizerForTesting(Rasterizer* rasterizer);
148 void FreeResourcesAndCleanUpReleasedTilesForTesting() {
149 FreeResourcesForReleasedTiles();
150 CleanUpReleasedTiles();
153 std::vector<Tile*> AllTilesForTesting() const {
154 std::vector<Tile*> tiles;
155 for (TileMap::const_iterator it = tiles_.begin(); it != tiles_.end();
157 tiles.push_back(it->second);
163 TileManager(TileManagerClient* client,
164 const scoped_refptr<base::SequencedTaskRunner>& task_runner,
165 ResourcePool* resource_pool,
166 Rasterizer* rasterizer,
167 RenderingStatsInstrumentation* rendering_stats_instrumentation,
168 size_t scheduled_raster_task_limit);
170 void FreeResourcesForReleasedTiles();
171 void CleanUpReleasedTiles();
173 // Overriden from RefCountedManager<Tile>:
175 void Release(Tile* tile) override;
177 // Overriden from RasterizerClient:
178 void DidFinishRunningTasks(TaskSet task_set) override;
179 TaskSetCollection TasksThatShouldBeForcedToComplete() const override;
181 typedef std::vector<Tile*> TileVector;
182 typedef std::set<Tile*> TileSet;
185 virtual void ScheduleTasks(
186 const TileVector& tiles_that_need_to_be_rasterized);
188 void AssignGpuMemoryToTiles(TileVector* tiles_that_need_to_be_rasterized);
194 MemoryUsage(int64 memory_bytes, int resource_count);
196 static MemoryUsage FromConfig(const gfx::Size& size, ResourceFormat format);
197 static MemoryUsage FromTile(const Tile* tile);
199 MemoryUsage& operator+=(const MemoryUsage& other);
200 MemoryUsage& operator-=(const MemoryUsage& other);
201 MemoryUsage operator-(const MemoryUsage& other);
203 bool Exceeds(const MemoryUsage& limit) const;
204 int64 memory_bytes() const { return memory_bytes_; }
211 void OnImageDecodeTaskCompleted(int layer_id,
212 SkPixelRef* pixel_ref,
214 void OnRasterTaskCompleted(Tile::Id tile,
215 scoped_ptr<ScopedResource> resource,
216 const RasterSource::SolidColorAnalysis& analysis,
219 void FreeResourcesForTile(Tile* tile);
220 void FreeResourcesForTileAndNotifyClientIfTileWasReadyToDraw(Tile* tile);
221 scoped_refptr<ImageDecodeTask> CreateImageDecodeTask(Tile* tile,
222 SkPixelRef* pixel_ref);
223 scoped_refptr<RasterTask> CreateRasterTask(Tile* tile);
225 void RebuildEvictionQueueIfNeeded();
226 bool FreeTileResourcesUntilUsageIsWithinLimit(const MemoryUsage& limit,
228 bool FreeTileResourcesWithLowerPriorityUntilUsageIsWithinLimit(
229 const MemoryUsage& limit,
230 const TilePriority& oother_priority,
232 bool TilePriorityViolatesMemoryPolicy(const TilePriority& priority);
233 bool IsReadyToActivate() const;
234 void CheckIfReadyToActivate();
236 TileManagerClient* client_;
237 scoped_refptr<base::SequencedTaskRunner> task_runner_;
238 ResourcePool* resource_pool_;
239 Rasterizer* rasterizer_;
240 GlobalStateThatImpactsTilePriority global_state_;
241 const size_t scheduled_raster_task_limit_;
243 typedef base::hash_map<Tile::Id, Tile*> TileMap;
246 bool all_tiles_that_need_to_be_rasterized_are_scheduled_;
247 MemoryHistory::Entry memory_stats_from_last_assign_;
249 RenderingStatsInstrumentation* rendering_stats_instrumentation_;
251 bool did_initialize_visible_tile_;
252 bool did_check_for_completed_tasks_since_last_schedule_tasks_;
253 bool did_oom_on_last_assign_;
255 typedef base::hash_map<uint32_t, scoped_refptr<ImageDecodeTask>>
257 typedef base::hash_map<int, PixelRefTaskMap> LayerPixelRefTaskMap;
258 LayerPixelRefTaskMap image_decode_tasks_;
260 typedef base::hash_map<int, int> LayerCountMap;
261 LayerCountMap used_layer_counts_;
263 RasterTaskCompletionStats update_visible_tiles_stats_;
265 std::vector<Tile*> released_tiles_;
267 ResourceFormat resource_format_;
269 // Queue used when scheduling raster tasks.
270 RasterTaskQueue raster_queue_;
272 std::vector<scoped_refptr<RasterTask>> orphan_raster_tasks_;
274 UniqueNotifier ready_to_activate_check_notifier_;
276 RasterTilePriorityQueue raster_priority_queue_;
277 EvictionTilePriorityQueue eviction_priority_queue_;
278 bool eviction_priority_queue_is_up_to_date_;
280 DISALLOW_COPY_AND_ASSIGN(TileManager);
285 #endif // CC_RESOURCES_TILE_MANAGER_H_