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/picture_pile_impl.h"
24 #include "cc/resources/prioritized_tile_set.h"
25 #include "cc/resources/raster_tile_priority_queue.h"
26 #include "cc/resources/rasterizer.h"
27 #include "cc/resources/resource_pool.h"
28 #include "cc/resources/tile.h"
32 class ConvertableToTraceFormat
;
38 class PictureLayerImpl
;
39 class ResourceProvider
;
41 class CC_EXPORT TileManagerClient
{
43 // Returns the set of layers that the tile manager should consider for raster.
44 // TODO(vmpstr): Change the way we determine if we are ready to activate, so
45 // that this can be removed.
46 virtual const std::vector
<PictureLayerImpl
*>& GetPictureLayers() const = 0;
48 // Called when all tiles marked as required for activation are ready to draw.
49 virtual void NotifyReadyToActivate() = 0;
51 // Called when the visible representation of a tile might have changed. Some
53 // - Tile version initialized.
54 // - Tile resources freed.
55 // - Tile marked for on-demand raster.
56 virtual void NotifyTileStateChanged(const Tile
* tile
) = 0;
58 // Given an empty raster tile priority queue, this will build a priority queue
59 // that will return tiles in order in which they should be rasterized.
60 // Note if the queue was previous built, Reset must be called on it.
61 virtual void BuildRasterQueue(RasterTilePriorityQueue
* queue
,
62 TreePriority tree_priority
) = 0;
64 // Given an empty eviction tile priority queue, this will build a priority
65 // queue that will return tiles in order in which they should be evicted.
66 // Note if the queue was previous built, Reset must be called on it.
67 virtual void BuildEvictionQueue(EvictionTilePriorityQueue
* queue
,
68 TreePriority tree_priority
) = 0;
71 virtual ~TileManagerClient() {}
74 struct RasterTaskCompletionStats
{
75 RasterTaskCompletionStats();
77 size_t completed_count
;
78 size_t canceled_count
;
80 scoped_refptr
<base::debug::ConvertableToTraceFormat
>
81 RasterTaskCompletionStatsAsValue(const RasterTaskCompletionStats
& stats
);
83 // This class manages tiles, deciding which should get rasterized and which
84 // should no longer have any memory assigned to them. Tile objects are "owned"
85 // by layers; they automatically register with the manager when they are
86 // created, and unregister from the manager when they are deleted.
87 class CC_EXPORT TileManager
: public RasterizerClient
,
88 public RefCountedManager
<Tile
> {
90 static scoped_ptr
<TileManager
> Create(
91 TileManagerClient
* client
,
92 base::SequencedTaskRunner
* task_runner
,
93 ResourcePool
* resource_pool
,
94 Rasterizer
* rasterizer
,
95 RenderingStatsInstrumentation
* rendering_stats_instrumentation
);
96 virtual ~TileManager();
98 void ManageTiles(const GlobalStateThatImpactsTilePriority
& state
);
100 // Returns true when visible tiles have been initialized.
101 bool UpdateVisibleTiles();
103 scoped_refptr
<Tile
> CreateTile(PicturePileImpl
* picture_pile
,
104 const gfx::Size
& tile_size
,
105 const gfx::Rect
& content_rect
,
106 const gfx::Rect
& opaque_rect
,
107 float contents_scale
,
109 int source_frame_number
,
112 scoped_refptr
<base::debug::ConvertableToTraceFormat
> BasicStateAsValue()
114 void BasicStateAsValueInto(base::debug::TracedValue
* dict
) const;
115 void AllTilesAsValueInto(base::debug::TracedValue
* array
) const;
116 const MemoryHistory::Entry
& memory_stats_from_last_assign() const {
117 return memory_stats_from_last_assign_
;
120 void InitializeTilesWithResourcesForTesting(const std::vector
<Tile
*>& tiles
) {
121 for (size_t i
= 0; i
< tiles
.size(); ++i
) {
122 ManagedTileState
& mts
= tiles
[i
]->managed_state();
123 ManagedTileState::TileVersion
& tile_version
=
124 mts
.tile_versions
[HIGH_QUALITY_RASTER_MODE
];
126 tile_version
.resource_
=
127 resource_pool_
->AcquireResource(tiles
[i
]->size());
129 bytes_releasable_
+= BytesConsumedIfAllocated(tiles
[i
]);
130 ++resources_releasable_
;
134 void ReleaseTileResourcesForTesting(const std::vector
<Tile
*>& tiles
) {
135 for (size_t i
= 0; i
< tiles
.size(); ++i
) {
136 Tile
* tile
= tiles
[i
];
137 for (int mode
= 0; mode
< NUM_RASTER_MODES
; ++mode
) {
138 FreeResourceForTile(tile
, static_cast<RasterMode
>(mode
));
143 void SetGlobalStateForTesting(
144 const GlobalStateThatImpactsTilePriority
& state
) {
145 // Soft limit is used for resource pool such that
146 // memory returns to soft limit after going over.
147 if (state
!= global_state_
) {
148 global_state_
= state
;
149 prioritized_tiles_dirty_
= true;
153 void SetRasterizerForTesting(Rasterizer
* rasterizer
);
155 void FreeResourcesAndCleanUpReleasedTilesForTesting() {
156 prioritized_tiles_
.Clear();
157 FreeResourcesForReleasedTiles();
158 CleanUpReleasedTiles();
162 TileManager(TileManagerClient
* client
,
163 base::SequencedTaskRunner
* task_runner
,
164 ResourcePool
* resource_pool
,
165 Rasterizer
* rasterizer
,
166 RenderingStatsInstrumentation
* rendering_stats_instrumentation
);
168 // Methods called by Tile
170 void DidChangeTilePriority(Tile
* tile
);
172 void FreeResourcesForReleasedTiles();
173 void CleanUpReleasedTiles();
175 // Overriden from RefCountedManager<Tile>:
176 virtual void Release(Tile
* tile
) OVERRIDE
;
178 // Overriden from RasterizerClient:
179 virtual bool ShouldForceTasksRequiredForActivationToComplete() const OVERRIDE
;
180 virtual void DidFinishRunningTasks() OVERRIDE
;
181 virtual void DidFinishRunningTasksRequiredForActivation() OVERRIDE
;
183 typedef std::vector
<Tile
*> TileVector
;
184 typedef std::set
<Tile
*> TileSet
;
187 virtual void ScheduleTasks(
188 const TileVector
& tiles_that_need_to_be_rasterized
);
190 void AssignGpuMemoryToTiles(PrioritizedTileSet
* tiles
,
191 TileVector
* tiles_that_need_to_be_rasterized
);
192 void GetTilesWithAssignedBins(PrioritizedTileSet
* tiles
);
195 void OnImageDecodeTaskCompleted(int layer_id
,
196 SkPixelRef
* pixel_ref
,
198 void OnRasterTaskCompleted(Tile::Id tile
,
199 scoped_ptr
<ScopedResource
> resource
,
200 RasterMode raster_mode
,
201 const PicturePileImpl::Analysis
& analysis
,
204 inline size_t BytesConsumedIfAllocated(const Tile
* tile
) const {
205 return Resource::MemorySizeBytes(tile
->size(),
206 resource_pool_
->resource_format());
209 void FreeResourceForTile(Tile
* tile
, RasterMode mode
);
210 void FreeResourcesForTile(Tile
* tile
);
211 void FreeUnusedResourcesForTile(Tile
* tile
);
212 void FreeResourcesForTileAndNotifyClientIfTileWasReadyToDraw(Tile
* tile
);
213 scoped_refptr
<ImageDecodeTask
> CreateImageDecodeTask(Tile
* tile
,
214 SkPixelRef
* pixel_ref
);
215 scoped_refptr
<RasterTask
> CreateRasterTask(Tile
* tile
);
216 void UpdatePrioritizedTileSetIfNeeded();
218 bool IsReadyToActivate() const;
219 void CheckIfReadyToActivate();
221 TileManagerClient
* client_
;
222 scoped_refptr
<base::SequencedTaskRunner
> task_runner_
;
223 ResourcePool
* resource_pool_
;
224 Rasterizer
* rasterizer_
;
225 GlobalStateThatImpactsTilePriority global_state_
;
227 typedef base::hash_map
<Tile::Id
, Tile
*> TileMap
;
230 PrioritizedTileSet prioritized_tiles_
;
231 bool prioritized_tiles_dirty_
;
233 bool all_tiles_that_need_to_be_rasterized_have_memory_
;
234 bool all_tiles_required_for_activation_have_memory_
;
236 size_t bytes_releasable_
;
237 size_t resources_releasable_
;
239 bool ever_exceeded_memory_budget_
;
240 MemoryHistory::Entry memory_stats_from_last_assign_
;
242 RenderingStatsInstrumentation
* rendering_stats_instrumentation_
;
244 bool did_initialize_visible_tile_
;
245 bool did_check_for_completed_tasks_since_last_schedule_tasks_
;
247 typedef base::hash_map
<uint32_t, scoped_refptr
<ImageDecodeTask
> >
249 typedef base::hash_map
<int, PixelRefTaskMap
> LayerPixelRefTaskMap
;
250 LayerPixelRefTaskMap image_decode_tasks_
;
252 typedef base::hash_map
<int, int> LayerCountMap
;
253 LayerCountMap used_layer_counts_
;
255 RasterTaskCompletionStats update_visible_tiles_stats_
;
257 std::vector
<Tile
*> released_tiles_
;
259 ResourceFormat resource_format_
;
261 // Queue used when scheduling raster tasks.
262 RasterTaskQueue raster_queue_
;
264 std::vector
<scoped_refptr
<RasterTask
> > orphan_raster_tasks_
;
266 UniqueNotifier ready_to_activate_check_notifier_
;
268 DISALLOW_COPY_AND_ASSIGN(TileManager
);
273 #endif // CC_RESOURCES_TILE_MANAGER_H_