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/unique_notifier.h"
18 #include "cc/resources/eviction_tile_priority_queue.h"
19 #include "cc/resources/memory_history.h"
20 #include "cc/resources/raster_source.h"
21 #include "cc/resources/raster_tile_priority_queue.h"
22 #include "cc/resources/resource_pool.h"
23 #include "cc/resources/tile.h"
24 #include "cc/resources/tile_draw_info.h"
25 #include "cc/resources/tile_task_runner.h"
28 namespace trace_event
{
29 class ConvertableToTraceFormat
;
35 class PictureLayerImpl
;
36 class ResourceProvider
;
38 class CC_EXPORT TileManagerClient
{
40 // Called when all tiles marked as required for activation are ready to draw.
41 virtual void NotifyReadyToActivate() = 0;
43 // Called when all tiles marked as required for draw are ready to draw.
44 virtual void NotifyReadyToDraw() = 0;
46 // Called when the visible representation of a tile might have changed. Some
48 // - Tile version initialized.
49 // - Tile resources freed.
50 // - Tile marked for on-demand raster.
51 virtual void NotifyTileStateChanged(const Tile
* tile
) = 0;
53 // Given an empty raster tile priority queue, this will build a priority queue
54 // that will return tiles in order in which they should be rasterized.
55 // Note if the queue was previous built, Reset must be called on it.
56 virtual scoped_ptr
<RasterTilePriorityQueue
> BuildRasterQueue(
57 TreePriority tree_priority
,
58 RasterTilePriorityQueue::Type type
) = 0;
60 // Given an empty eviction tile priority queue, this will build a priority
61 // queue that will return tiles in order in which they should be evicted.
62 // Note if the queue was previous built, Reset must be called on it.
63 virtual scoped_ptr
<EvictionTilePriorityQueue
> BuildEvictionQueue(
64 TreePriority tree_priority
) = 0;
66 // Informs the client that due to the currently rasterizing (or scheduled to
67 // be rasterized) tiles, we will be in a position that will likely require a
68 // draw. This can be used to preemptively start a frame.
69 virtual void SetIsLikelyToRequireADraw(bool is_likely_to_require_a_draw
) = 0;
72 virtual ~TileManagerClient() {}
75 struct RasterTaskCompletionStats
{
76 RasterTaskCompletionStats();
78 size_t completed_count
;
79 size_t canceled_count
;
81 scoped_refptr
<base::trace_event::ConvertableToTraceFormat
>
82 RasterTaskCompletionStatsAsValue(const RasterTaskCompletionStats
& stats
);
84 // This class manages tiles, deciding which should get rasterized and which
85 // should no longer have any memory assigned to them. Tile objects are "owned"
86 // by layers; they automatically register with the manager when they are
87 // created, and unregister from the manager when they are deleted.
88 class CC_EXPORT TileManager
: public TileTaskRunnerClient
{
91 REQUIRED_FOR_ACTIVATION
,
93 // PixelBufferTileTaskWorkerPool depends on ALL being last.
95 // Adding additional values requires increasing kNumberOfTaskSets in
99 static_assert(NamedTaskSet::ALL
== (kNumberOfTaskSets
- 1),
100 "NamedTaskSet::ALL should be equal to kNumberOfTaskSets"
103 static scoped_ptr
<TileManager
> Create(TileManagerClient
* client
,
104 base::SequencedTaskRunner
* task_runner
,
105 ResourcePool
* resource_pool
,
106 TileTaskRunner
* tile_task_runner
,
107 size_t scheduled_raster_task_limit
);
108 ~TileManager() override
;
110 // Assigns tile memory and schedules work to prepare tiles for drawing.
111 // - Runs client_->NotifyReadyToActivate() when all tiles required for
112 // activation are prepared, or failed to prepare due to OOM.
113 // - Runs client_->NotifyReadyToDraw() when all tiles required draw are
114 // prepared, or failed to prepare due to OOM.
115 void PrepareTiles(const GlobalStateThatImpactsTilePriority
& state
);
117 void UpdateVisibleTiles(const GlobalStateThatImpactsTilePriority
& state
);
119 ScopedTilePtr
CreateTile(RasterSource
* raster_source
,
120 const gfx::Size
& desired_texture_size
,
121 const gfx::Rect
& content_rect
,
122 float contents_scale
,
124 int source_frame_number
,
127 bool IsReadyToActivate() const;
128 bool IsReadyToDraw() const;
130 scoped_refptr
<base::trace_event::ConvertableToTraceFormat
> BasicStateAsValue()
132 void BasicStateAsValueInto(base::trace_event::TracedValue
* dict
) const;
133 const MemoryHistory::Entry
& memory_stats_from_last_assign() const {
134 return memory_stats_from_last_assign_
;
137 // Public methods for testing.
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 void CheckIfMoreTilesNeedToBePreparedForTesting() {
180 CheckIfMoreTilesNeedToBePrepared();
184 TileManager(TileManagerClient
* client
,
185 const scoped_refptr
<base::SequencedTaskRunner
>& task_runner
,
186 ResourcePool
* resource_pool
,
187 TileTaskRunner
* tile_task_runner
,
188 size_t scheduled_raster_task_limit
);
190 void FreeResourcesForReleasedTiles();
191 void CleanUpReleasedTiles();
194 // Virtual for testing.
195 virtual void Release(Tile
* tile
);
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
);
216 MemoryUsage(int64 memory_bytes
, int resource_count
);
218 static MemoryUsage
FromConfig(const gfx::Size
& size
, ResourceFormat format
);
219 static MemoryUsage
FromTile(const Tile
* tile
);
221 MemoryUsage
& operator+=(const MemoryUsage
& other
);
222 MemoryUsage
& operator-=(const MemoryUsage
& other
);
223 MemoryUsage
operator-(const MemoryUsage
& other
);
225 bool Exceeds(const MemoryUsage
& limit
) const;
226 int64
memory_bytes() const { return memory_bytes_
; }
233 void OnImageDecodeTaskCompleted(int layer_id
,
234 SkPixelRef
* pixel_ref
,
236 void OnRasterTaskCompleted(Tile::Id tile
,
237 scoped_ptr
<ScopedResource
> resource
,
238 const RasterSource::SolidColorAnalysis
& analysis
,
240 void UpdateTileDrawInfo(Tile
* tile
,
241 scoped_ptr
<ScopedResource
> resource
,
242 const RasterSource::SolidColorAnalysis
& analysis
);
244 void FreeResourcesForTile(Tile
* tile
);
245 void FreeResourcesForTileAndNotifyClientIfTileWasReadyToDraw(Tile
* tile
);
246 scoped_refptr
<ImageDecodeTask
> CreateImageDecodeTask(Tile
* tile
,
247 SkPixelRef
* pixel_ref
);
248 scoped_refptr
<RasterTask
> CreateRasterTask(Tile
* tile
);
250 scoped_ptr
<EvictionTilePriorityQueue
>
251 FreeTileResourcesUntilUsageIsWithinLimit(
252 scoped_ptr
<EvictionTilePriorityQueue
> eviction_priority_queue
,
253 const MemoryUsage
& limit
,
255 scoped_ptr
<EvictionTilePriorityQueue
>
256 FreeTileResourcesWithLowerPriorityUntilUsageIsWithinLimit(
257 scoped_ptr
<EvictionTilePriorityQueue
> eviction_priority_queue
,
258 const MemoryUsage
& limit
,
259 const TilePriority
& oother_priority
,
261 bool TilePriorityViolatesMemoryPolicy(const TilePriority
& priority
);
262 bool AreRequiredTilesReadyToDraw(RasterTilePriorityQueue::Type type
) const;
263 void NotifyReadyToActivate();
264 void NotifyReadyToDraw();
265 void CheckIfReadyToActivate();
266 void CheckIfReadyToDraw();
267 void CheckIfMoreTilesNeedToBePrepared();
269 TileManagerClient
* client_
;
270 scoped_refptr
<base::SequencedTaskRunner
> task_runner_
;
271 ResourcePool
* resource_pool_
;
272 TileTaskRunner
* tile_task_runner_
;
273 GlobalStateThatImpactsTilePriority global_state_
;
274 size_t scheduled_raster_task_limit_
;
276 typedef base::hash_map
<Tile::Id
, Tile
*> TileMap
;
279 bool all_tiles_that_need_to_be_rasterized_are_scheduled_
;
280 MemoryHistory::Entry memory_stats_from_last_assign_
;
282 bool did_check_for_completed_tasks_since_last_schedule_tasks_
;
283 bool did_oom_on_last_assign_
;
285 typedef base::hash_map
<uint32_t, scoped_refptr
<ImageDecodeTask
>>
287 typedef base::hash_map
<int, PixelRefTaskMap
> LayerPixelRefTaskMap
;
288 LayerPixelRefTaskMap image_decode_tasks_
;
290 typedef base::hash_map
<int, int> LayerCountMap
;
291 LayerCountMap used_layer_counts_
;
293 RasterTaskCompletionStats update_visible_tiles_stats_
;
295 std::vector
<Tile
*> released_tiles_
;
297 // Queue used when scheduling raster tasks.
298 TileTaskQueue raster_queue_
;
300 std::vector
<scoped_refptr
<RasterTask
>> orphan_raster_tasks_
;
302 UniqueNotifier ready_to_activate_check_notifier_
;
303 UniqueNotifier ready_to_draw_check_notifier_
;
304 UniqueNotifier more_tiles_need_prepare_check_notifier_
;
306 bool did_notify_ready_to_activate_
;
307 bool did_notify_ready_to_draw_
;
309 DISALLOW_COPY_AND_ASSIGN(TileManager
);
314 #endif // CC_RESOURCES_TILE_MANAGER_H_