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/resources/eviction_tile_priority_queue.h"
20 #include "cc/resources/memory_history.h"
21 #include "cc/resources/raster_source.h"
22 #include "cc/resources/raster_tile_priority_queue.h"
23 #include "cc/resources/resource_pool.h"
24 #include "cc/resources/tile.h"
25 #include "cc/resources/tile_draw_info.h"
26 #include "cc/resources/tile_task_runner.h"
30 class ConvertableToTraceFormat
;
36 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 all tiles marked as required for draw are ready to draw.
51 virtual void NotifyReadyToDraw() = 0;
53 // Called when the visible representation of a tile might have changed. Some
55 // - Tile version initialized.
56 // - Tile resources freed.
57 // - Tile marked for on-demand raster.
58 virtual void NotifyTileStateChanged(const Tile
* tile
) = 0;
60 // Given an empty raster tile priority queue, this will build a priority queue
61 // that will return tiles in order in which they should be rasterized.
62 // Note if the queue was previous built, Reset must be called on it.
63 virtual void BuildRasterQueue(RasterTilePriorityQueue
* queue
,
64 TreePriority tree_priority
) = 0;
66 // Given an empty eviction tile priority queue, this will build a priority
67 // queue that will return tiles in order in which they should be evicted.
68 // Note if the queue was previous built, Reset must be called on it.
69 virtual void BuildEvictionQueue(EvictionTilePriorityQueue
* queue
,
70 TreePriority tree_priority
) = 0;
73 virtual ~TileManagerClient() {}
76 struct RasterTaskCompletionStats
{
77 RasterTaskCompletionStats();
79 size_t completed_count
;
80 size_t canceled_count
;
82 scoped_refptr
<base::debug::ConvertableToTraceFormat
>
83 RasterTaskCompletionStatsAsValue(const RasterTaskCompletionStats
& stats
);
85 // This class manages tiles, deciding which should get rasterized and which
86 // should no longer have any memory assigned to them. Tile objects are "owned"
87 // by layers; they automatically register with the manager when they are
88 // created, and unregister from the manager when they are deleted.
89 class CC_EXPORT TileManager
: public TileTaskRunnerClient
,
90 public RefCountedManager
<Tile
> {
94 REQUIRED_FOR_ACTIVATION
,
96 // Adding additional values requires increasing kNumberOfTaskSets in
100 static scoped_ptr
<TileManager
> Create(TileManagerClient
* client
,
101 base::SequencedTaskRunner
* task_runner
,
102 ResourcePool
* resource_pool
,
103 TileTaskRunner
* tile_task_runner
,
104 Rasterizer
* rasterizer
,
105 size_t scheduled_raster_task_limit
);
106 ~TileManager() override
;
108 // Assigns tile memory and schedules work to prepare tiles for drawing.
109 // - Runs client_->NotifyReadyToActivate() when all tiles required for
110 // activation are prepared, or failed to prepare due to OOM.
111 // - Runs client_->NotifyReadyToDraw() when all tiles required draw are
112 // prepared, or failed to prepare due to OOM.
113 void PrepareTiles(const GlobalStateThatImpactsTilePriority
& state
);
115 void UpdateVisibleTiles(const GlobalStateThatImpactsTilePriority
& state
);
117 scoped_refptr
<Tile
> CreateTile(RasterSource
* raster_source
,
118 const gfx::Size
& desired_texture_size
,
119 const gfx::Rect
& content_rect
,
120 float contents_scale
,
122 int source_frame_number
,
125 scoped_refptr
<base::debug::ConvertableToTraceFormat
> BasicStateAsValue()
127 void BasicStateAsValueInto(base::debug::TracedValue
* dict
) const;
128 const MemoryHistory::Entry
& memory_stats_from_last_assign() const {
129 return memory_stats_from_last_assign_
;
132 void InitializeTilesWithResourcesForTesting(const std::vector
<Tile
*>& tiles
) {
133 for (size_t i
= 0; i
< tiles
.size(); ++i
) {
134 TileDrawInfo
& draw_info
= tiles
[i
]->draw_info();
135 draw_info
.resource_
=
136 resource_pool_
->AcquireResource(tiles
[i
]->desired_texture_size());
140 void ReleaseTileResourcesForTesting(const std::vector
<Tile
*>& tiles
) {
141 for (size_t i
= 0; i
< tiles
.size(); ++i
) {
142 Tile
* tile
= tiles
[i
];
143 FreeResourcesForTile(tile
);
147 void SetGlobalStateForTesting(
148 const GlobalStateThatImpactsTilePriority
& state
) {
149 global_state_
= state
;
152 void SetTileTaskRunnerForTesting(TileTaskRunner
* tile_task_runner
);
154 void FreeResourcesAndCleanUpReleasedTilesForTesting() {
155 FreeResourcesForReleasedTiles();
156 CleanUpReleasedTiles();
159 std::vector
<Tile
*> AllTilesForTesting() const {
160 std::vector
<Tile
*> tiles
;
161 for (TileMap::const_iterator it
= tiles_
.begin(); it
!= tiles_
.end();
163 tiles
.push_back(it
->second
);
168 void SetScheduledRasterTaskLimitForTesting(size_t limit
) {
169 scheduled_raster_task_limit_
= limit
;
173 TileManager(TileManagerClient
* client
,
174 const scoped_refptr
<base::SequencedTaskRunner
>& task_runner
,
175 ResourcePool
* resource_pool
,
176 TileTaskRunner
* tile_task_runner
,
177 Rasterizer
* rasterizer
,
178 size_t scheduled_raster_task_limit
);
180 void FreeResourcesForReleasedTiles();
181 void CleanUpReleasedTiles();
183 // Overriden from RefCountedManager<Tile>:
185 void Release(Tile
* tile
) override
;
187 // Overriden from TileTaskRunnerClient:
188 void DidFinishRunningTileTasks(TaskSet task_set
) override
;
189 TaskSetCollection
TasksThatShouldBeForcedToComplete() const override
;
191 typedef std::vector
<Tile
*> TileVector
;
192 typedef std::set
<Tile
*> TileSet
;
195 virtual void ScheduleTasks(
196 const TileVector
& tiles_that_need_to_be_rasterized
);
198 void AssignGpuMemoryToTiles(TileVector
* tiles_that_need_to_be_rasterized
,
199 size_t scheduled_raser_task_limit
);
201 void SynchronouslyRasterizeTiles(
202 const GlobalStateThatImpactsTilePriority
& state
);
208 MemoryUsage(int64 memory_bytes
, int resource_count
);
210 static MemoryUsage
FromConfig(const gfx::Size
& size
, ResourceFormat format
);
211 static MemoryUsage
FromTile(const Tile
* tile
);
213 MemoryUsage
& operator+=(const MemoryUsage
& other
);
214 MemoryUsage
& operator-=(const MemoryUsage
& other
);
215 MemoryUsage
operator-(const MemoryUsage
& other
);
217 bool Exceeds(const MemoryUsage
& limit
) const;
218 int64
memory_bytes() const { return memory_bytes_
; }
225 void OnImageDecodeTaskCompleted(int layer_id
,
226 SkPixelRef
* pixel_ref
,
228 void OnRasterTaskCompleted(Tile::Id tile
,
229 scoped_ptr
<ScopedResource
> resource
,
230 const RasterSource::SolidColorAnalysis
& analysis
,
232 void UpdateTileDrawInfo(Tile
* tile
,
233 scoped_ptr
<ScopedResource
> resource
,
234 const RasterSource::SolidColorAnalysis
& analysis
);
236 void FreeResourcesForTile(Tile
* tile
);
237 void FreeResourcesForTileAndNotifyClientIfTileWasReadyToDraw(Tile
* tile
);
238 scoped_refptr
<ImageDecodeTask
> CreateImageDecodeTask(Tile
* tile
,
239 SkPixelRef
* pixel_ref
);
240 scoped_refptr
<RasterTask
> CreateRasterTask(Tile
* tile
);
242 void RebuildEvictionQueueIfNeeded();
243 bool FreeTileResourcesUntilUsageIsWithinLimit(const MemoryUsage
& limit
,
245 bool FreeTileResourcesWithLowerPriorityUntilUsageIsWithinLimit(
246 const MemoryUsage
& limit
,
247 const TilePriority
& oother_priority
,
249 bool TilePriorityViolatesMemoryPolicy(const TilePriority
& priority
);
250 bool IsReadyToActivate() const;
251 bool IsReadyToDraw() const;
252 void NotifyReadyToActivate();
253 void NotifyReadyToDraw();
254 void CheckIfReadyToActivate();
255 void CheckIfReadyToDraw();
256 void CheckIfMoreTilesNeedToBePrepared();
258 TileManagerClient
* client_
;
259 scoped_refptr
<base::SequencedTaskRunner
> task_runner_
;
260 ResourcePool
* resource_pool_
;
261 TileTaskRunner
* tile_task_runner_
;
262 Rasterizer
* rasterizer_
;
263 GlobalStateThatImpactsTilePriority global_state_
;
264 size_t scheduled_raster_task_limit_
;
266 typedef base::hash_map
<Tile::Id
, Tile
*> TileMap
;
269 bool all_tiles_that_need_to_be_rasterized_are_scheduled_
;
270 MemoryHistory::Entry memory_stats_from_last_assign_
;
272 bool did_check_for_completed_tasks_since_last_schedule_tasks_
;
273 bool did_oom_on_last_assign_
;
275 typedef base::hash_map
<uint32_t, scoped_refptr
<ImageDecodeTask
>>
277 typedef base::hash_map
<int, PixelRefTaskMap
> LayerPixelRefTaskMap
;
278 LayerPixelRefTaskMap image_decode_tasks_
;
280 typedef base::hash_map
<int, int> LayerCountMap
;
281 LayerCountMap used_layer_counts_
;
283 RasterTaskCompletionStats update_visible_tiles_stats_
;
285 std::vector
<Tile
*> released_tiles_
;
287 ResourceFormat resource_format_
;
289 // Queue used when scheduling raster tasks.
290 TileTaskQueue raster_queue_
;
292 std::vector
<scoped_refptr
<RasterTask
>> orphan_raster_tasks_
;
294 UniqueNotifier ready_to_activate_notifier_
;
295 UniqueNotifier ready_to_draw_notifier_
;
296 UniqueNotifier ready_to_activate_check_notifier_
;
297 UniqueNotifier ready_to_draw_check_notifier_
;
298 UniqueNotifier more_tiles_need_prepare_check_notifier_
;
300 RasterTilePriorityQueue raster_priority_queue_
;
301 EvictionTilePriorityQueue eviction_priority_queue_
;
302 bool eviction_priority_queue_is_up_to_date_
;
304 bool did_notify_ready_to_activate_
;
305 bool did_notify_ready_to_draw_
;
307 DISALLOW_COPY_AND_ASSIGN(TileManager
);
312 #endif // CC_RESOURCES_TILE_MANAGER_H_