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"
29 namespace trace_event
{
30 class ConvertableToTraceFormat
;
36 class PictureLayerImpl
;
38 class ResourceProvider
;
40 class CC_EXPORT TileManagerClient
{
42 // Called when all tiles marked as required for activation are ready to draw.
43 virtual void NotifyReadyToActivate() = 0;
45 // Called when all tiles marked as required for draw are ready to draw.
46 virtual void NotifyReadyToDraw() = 0;
48 // Called when the visible representation of a tile might have changed. Some
50 // - Tile version initialized.
51 // - Tile resources freed.
52 // - Tile marked for on-demand raster.
53 virtual void NotifyTileStateChanged(const Tile
* tile
) = 0;
55 // Given an empty raster tile priority queue, this will build a priority queue
56 // that will return tiles in order in which they should be rasterized.
57 // Note if the queue was previous built, Reset must be called on it.
58 virtual scoped_ptr
<RasterTilePriorityQueue
> BuildRasterQueue(
59 TreePriority tree_priority
,
60 RasterTilePriorityQueue::Type type
) = 0;
62 // Given an empty eviction tile priority queue, this will build a priority
63 // queue that will return tiles in order in which they should be evicted.
64 // Note if the queue was previous built, Reset must be called on it.
65 virtual scoped_ptr
<EvictionTilePriorityQueue
> BuildEvictionQueue(
66 TreePriority tree_priority
) = 0;
68 // Informs the client that due to the currently rasterizing (or scheduled to
69 // be rasterized) tiles, we will be in a position that will likely require a
70 // draw. This can be used to preemptively start a frame.
71 virtual void SetIsLikelyToRequireADraw(bool is_likely_to_require_a_draw
) = 0;
74 virtual ~TileManagerClient() {}
77 struct RasterTaskCompletionStats
{
78 RasterTaskCompletionStats();
80 size_t completed_count
;
81 size_t canceled_count
;
83 scoped_refptr
<base::trace_event::ConvertableToTraceFormat
>
84 RasterTaskCompletionStatsAsValue(const RasterTaskCompletionStats
& stats
);
86 // This class manages tiles, deciding which should get rasterized and which
87 // should no longer have any memory assigned to them. Tile objects are "owned"
88 // by layers; they automatically register with the manager when they are
89 // created, and unregister from the manager when they are deleted.
90 class CC_EXPORT TileManager
: public TileTaskRunnerClient
,
91 public RefCountedManager
<Tile
> {
94 REQUIRED_FOR_ACTIVATION
,
96 // PixelBufferTileTaskWorkerPool depends on ALL being last.
98 // Adding additional values requires increasing kNumberOfTaskSets in
102 static_assert(NamedTaskSet::ALL
== (kNumberOfTaskSets
- 1),
103 "NamedTaskSet::ALL should be equal to kNumberOfTaskSets"
106 static scoped_ptr
<TileManager
> Create(TileManagerClient
* client
,
107 base::SequencedTaskRunner
* task_runner
,
108 ResourcePool
* resource_pool
,
109 TileTaskRunner
* tile_task_runner
,
110 Rasterizer
* rasterizer
,
111 size_t scheduled_raster_task_limit
);
112 ~TileManager() override
;
114 // Assigns tile memory and schedules work to prepare tiles for drawing.
115 // - Runs client_->NotifyReadyToActivate() when all tiles required for
116 // activation are prepared, or failed to prepare due to OOM.
117 // - Runs client_->NotifyReadyToDraw() when all tiles required draw are
118 // prepared, or failed to prepare due to OOM.
119 void PrepareTiles(const GlobalStateThatImpactsTilePriority
& state
);
121 void UpdateVisibleTiles(const GlobalStateThatImpactsTilePriority
& state
);
123 scoped_refptr
<Tile
> CreateTile(RasterSource
* raster_source
,
124 const gfx::Size
& desired_texture_size
,
125 const gfx::Rect
& content_rect
,
126 float contents_scale
,
128 int source_frame_number
,
131 scoped_refptr
<base::trace_event::ConvertableToTraceFormat
> BasicStateAsValue()
133 void BasicStateAsValueInto(base::trace_event::TracedValue
* dict
) const;
134 const MemoryHistory::Entry
& memory_stats_from_last_assign() const {
135 return memory_stats_from_last_assign_
;
138 void InitializeTilesWithResourcesForTesting(const std::vector
<Tile
*>& tiles
) {
139 for (size_t i
= 0; i
< tiles
.size(); ++i
) {
140 TileDrawInfo
& draw_info
= tiles
[i
]->draw_info();
141 draw_info
.resource_
= resource_pool_
->AcquireResource(
142 tiles
[i
]->desired_texture_size(),
143 tile_task_runner_
->GetResourceFormat());
147 void ReleaseTileResourcesForTesting(const std::vector
<Tile
*>& tiles
) {
148 for (size_t i
= 0; i
< tiles
.size(); ++i
) {
149 Tile
* tile
= tiles
[i
];
150 FreeResourcesForTile(tile
);
154 void SetGlobalStateForTesting(
155 const GlobalStateThatImpactsTilePriority
& state
) {
156 global_state_
= state
;
159 void SetTileTaskRunnerForTesting(TileTaskRunner
* tile_task_runner
);
161 void FreeResourcesAndCleanUpReleasedTilesForTesting() {
162 FreeResourcesForReleasedTiles();
163 CleanUpReleasedTiles();
166 std::vector
<Tile
*> AllTilesForTesting() const {
167 std::vector
<Tile
*> tiles
;
168 for (TileMap::const_iterator it
= tiles_
.begin(); it
!= tiles_
.end();
170 tiles
.push_back(it
->second
);
175 void SetScheduledRasterTaskLimitForTesting(size_t limit
) {
176 scheduled_raster_task_limit_
= limit
;
179 bool IsReadyToActivate() const;
180 bool IsReadyToDraw() const;
183 TileManager(TileManagerClient
* client
,
184 const scoped_refptr
<base::SequencedTaskRunner
>& task_runner
,
185 ResourcePool
* resource_pool
,
186 TileTaskRunner
* tile_task_runner
,
187 Rasterizer
* rasterizer
,
188 size_t scheduled_raster_task_limit
);
190 void FreeResourcesForReleasedTiles();
191 void CleanUpReleasedTiles();
193 // Overriden from RefCountedManager<Tile>:
195 void Release(Tile
* tile
) override
;
197 // Overriden from TileTaskRunnerClient:
198 void DidFinishRunningTileTasks(TaskSet task_set
) override
;
199 TaskSetCollection
TasksThatShouldBeForcedToComplete() const override
;
201 typedef std::vector
<Tile
*> TileVector
;
202 typedef std::set
<Tile
*> TileSet
;
205 virtual void ScheduleTasks(
206 const TileVector
& tiles_that_need_to_be_rasterized
);
208 void AssignGpuMemoryToTiles(RasterTilePriorityQueue
* raster_priority_queue
,
209 size_t scheduled_raser_task_limit
,
210 TileVector
* tiles_that_need_to_be_rasterized
);
212 void SynchronouslyRasterizeTiles(
213 const GlobalStateThatImpactsTilePriority
& state
);
219 MemoryUsage(int64 memory_bytes
, int resource_count
);
221 static MemoryUsage
FromConfig(const gfx::Size
& size
, ResourceFormat format
);
222 static MemoryUsage
FromTile(const Tile
* tile
);
224 MemoryUsage
& operator+=(const MemoryUsage
& other
);
225 MemoryUsage
& operator-=(const MemoryUsage
& other
);
226 MemoryUsage
operator-(const MemoryUsage
& other
);
228 bool Exceeds(const MemoryUsage
& limit
) const;
229 int64
memory_bytes() const { return memory_bytes_
; }
236 void OnImageDecodeTaskCompleted(int layer_id
,
237 SkPixelRef
* pixel_ref
,
239 void OnRasterTaskCompleted(Tile::Id tile
,
240 scoped_ptr
<ScopedResource
> resource
,
241 const RasterSource::SolidColorAnalysis
& analysis
,
243 void UpdateTileDrawInfo(Tile
* tile
,
244 scoped_ptr
<ScopedResource
> resource
,
245 const RasterSource::SolidColorAnalysis
& analysis
);
247 void FreeResourcesForTile(Tile
* tile
);
248 void FreeResourcesForTileAndNotifyClientIfTileWasReadyToDraw(Tile
* tile
);
249 scoped_refptr
<ImageDecodeTask
> CreateImageDecodeTask(Tile
* tile
,
250 SkPixelRef
* pixel_ref
);
251 scoped_refptr
<RasterTask
> CreateRasterTask(Tile
* tile
);
253 scoped_ptr
<EvictionTilePriorityQueue
>
254 FreeTileResourcesUntilUsageIsWithinLimit(
255 scoped_ptr
<EvictionTilePriorityQueue
> eviction_priority_queue
,
256 const MemoryUsage
& limit
,
258 scoped_ptr
<EvictionTilePriorityQueue
>
259 FreeTileResourcesWithLowerPriorityUntilUsageIsWithinLimit(
260 scoped_ptr
<EvictionTilePriorityQueue
> eviction_priority_queue
,
261 const MemoryUsage
& limit
,
262 const TilePriority
& oother_priority
,
264 bool TilePriorityViolatesMemoryPolicy(const TilePriority
& priority
);
265 bool AreRequiredTilesReadyToDraw(RasterTilePriorityQueue::Type type
) const;
266 void NotifyReadyToActivate();
267 void NotifyReadyToDraw();
268 void CheckIfReadyToActivate();
269 void CheckIfReadyToDraw();
270 void CheckIfMoreTilesNeedToBePrepared();
272 TileManagerClient
* client_
;
273 scoped_refptr
<base::SequencedTaskRunner
> task_runner_
;
274 ResourcePool
* resource_pool_
;
275 TileTaskRunner
* tile_task_runner_
;
276 Rasterizer
* rasterizer_
;
277 GlobalStateThatImpactsTilePriority global_state_
;
278 size_t scheduled_raster_task_limit_
;
280 typedef base::hash_map
<Tile::Id
, Tile
*> TileMap
;
283 bool all_tiles_that_need_to_be_rasterized_are_scheduled_
;
284 MemoryHistory::Entry memory_stats_from_last_assign_
;
286 bool did_check_for_completed_tasks_since_last_schedule_tasks_
;
287 bool did_oom_on_last_assign_
;
289 typedef base::hash_map
<uint32_t, scoped_refptr
<ImageDecodeTask
>>
291 typedef base::hash_map
<int, PixelRefTaskMap
> LayerPixelRefTaskMap
;
292 LayerPixelRefTaskMap image_decode_tasks_
;
294 typedef base::hash_map
<int, int> LayerCountMap
;
295 LayerCountMap used_layer_counts_
;
297 RasterTaskCompletionStats update_visible_tiles_stats_
;
299 std::vector
<Tile
*> released_tiles_
;
301 // Queue used when scheduling raster tasks.
302 TileTaskQueue raster_queue_
;
304 std::vector
<scoped_refptr
<RasterTask
>> orphan_raster_tasks_
;
306 UniqueNotifier ready_to_activate_notifier_
;
307 UniqueNotifier ready_to_draw_notifier_
;
308 UniqueNotifier ready_to_activate_check_notifier_
;
309 UniqueNotifier ready_to_draw_check_notifier_
;
310 UniqueNotifier more_tiles_need_prepare_check_notifier_
;
312 bool did_notify_ready_to_activate_
;
313 bool did_notify_ready_to_draw_
;
315 DISALLOW_COPY_AND_ASSIGN(TileManager
);
320 #endif // CC_RESOURCES_TILE_MANAGER_H_