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_TILES_TILE_MANAGER_H_
6 #define CC_TILES_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/playback/raster_source.h"
19 #include "cc/raster/tile_task_runner.h"
20 #include "cc/resources/memory_history.h"
21 #include "cc/resources/resource_pool.h"
22 #include "cc/tiles/eviction_tile_priority_queue.h"
23 #include "cc/tiles/image_decode_controller.h"
24 #include "cc/tiles/raster_tile_priority_queue.h"
25 #include "cc/tiles/tile.h"
26 #include "cc/tiles/tile_draw_info.h"
29 namespace trace_event
{
30 class ConvertableToTraceFormat
;
36 class PictureLayerImpl
;
37 class ResourceProvider
;
39 class CC_EXPORT TileManagerClient
{
41 // Called when all tiles marked as required for activation are ready to draw.
42 virtual void NotifyReadyToActivate() = 0;
44 // Called when all tiles marked as required for draw are ready to draw.
45 virtual void NotifyReadyToDraw() = 0;
47 // Called when all tile tasks started by the most recent call to PrepareTiles
49 virtual void NotifyAllTileTasksCompleted() = 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 scoped_ptr
<RasterTilePriorityQueue
> BuildRasterQueue(
62 TreePriority tree_priority
,
63 RasterTilePriorityQueue::Type type
) = 0;
65 // Given an empty eviction tile priority queue, this will build a priority
66 // queue that will return tiles in order in which they should be evicted.
67 // Note if the queue was previous built, Reset must be called on it.
68 virtual scoped_ptr
<EvictionTilePriorityQueue
> BuildEvictionQueue(
69 TreePriority tree_priority
) = 0;
71 // Informs the client that due to the currently rasterizing (or scheduled to
72 // be rasterized) tiles, we will be in a position that will likely require a
73 // draw. This can be used to preemptively start a frame.
74 virtual void SetIsLikelyToRequireADraw(bool is_likely_to_require_a_draw
) = 0;
77 virtual ~TileManagerClient() {}
80 struct RasterTaskCompletionStats
{
81 RasterTaskCompletionStats();
83 size_t completed_count
;
84 size_t canceled_count
;
86 scoped_refptr
<base::trace_event::ConvertableToTraceFormat
>
87 RasterTaskCompletionStatsAsValue(const RasterTaskCompletionStats
& stats
);
89 // This class manages tiles, deciding which should get rasterized and which
90 // should no longer have any memory assigned to them. Tile objects are "owned"
91 // by layers; they automatically register with the manager when they are
92 // created, and unregister from the manager when they are deleted.
93 class CC_EXPORT TileManager
: public TileTaskRunnerClient
{
96 REQUIRED_FOR_ACTIVATION
,
98 // PixelBufferTileTaskWorkerPool depends on ALL being last.
100 // Adding additional values requires increasing kNumberOfTaskSets in
101 // tile_task_runner.h
104 static_assert(NamedTaskSet::ALL
== (kNumberOfTaskSets
- 1),
105 "NamedTaskSet::ALL should be equal to kNumberOfTaskSets"
108 static scoped_ptr
<TileManager
> Create(TileManagerClient
* client
,
109 base::SequencedTaskRunner
* task_runner
,
110 size_t scheduled_raster_task_limit
);
111 ~TileManager() override
;
113 // Assigns tile memory and schedules work to prepare tiles for drawing.
114 // - Runs client_->NotifyReadyToActivate() when all tiles required for
115 // activation are prepared, or failed to prepare due to OOM.
116 // - Runs client_->NotifyReadyToDraw() when all tiles required draw are
117 // prepared, or failed to prepare due to OOM.
118 bool PrepareTiles(const GlobalStateThatImpactsTilePriority
& state
);
120 // Synchronously finish any in progress work, cancel the rest, and clean up as
121 // much resources as possible. Also, prevents any future work until a
122 // SetResources call.
123 void FinishTasksAndCleanUp();
125 // Set the new given resource pool and tile task runner. Note that
126 // FinishTasksAndCleanUp must be called in between consecutive calls to
128 void SetResources(ResourcePool
* resource_pool
,
129 TileTaskRunner
* tile_task_runner
,
130 size_t scheduled_raster_task_limit
);
132 // This causes any completed raster work to finalize, so that tiles get up to
133 // date draw information.
136 ScopedTilePtr
CreateTile(const gfx::Size
& desired_texture_size
,
137 const gfx::Rect
& content_rect
,
138 float contents_scale
,
140 int source_frame_number
,
143 bool IsReadyToActivate() const;
144 bool IsReadyToDraw() const;
146 scoped_refptr
<base::trace_event::ConvertableToTraceFormat
> BasicStateAsValue()
148 void BasicStateAsValueInto(base::trace_event::TracedValue
* dict
) const;
149 const MemoryHistory::Entry
& memory_stats_from_last_assign() const {
150 return memory_stats_from_last_assign_
;
153 // Public methods for testing.
154 void InitializeTilesWithResourcesForTesting(const std::vector
<Tile
*>& tiles
) {
155 for (size_t i
= 0; i
< tiles
.size(); ++i
) {
156 TileDrawInfo
& draw_info
= tiles
[i
]->draw_info();
157 draw_info
.resource_
= resource_pool_
->AcquireResource(
158 tiles
[i
]->desired_texture_size(),
159 tile_task_runner_
->GetResourceFormat());
163 void ReleaseTileResourcesForTesting(const std::vector
<Tile
*>& tiles
) {
164 for (size_t i
= 0; i
< tiles
.size(); ++i
) {
165 Tile
* tile
= tiles
[i
];
166 FreeResourcesForTile(tile
);
170 void SetGlobalStateForTesting(
171 const GlobalStateThatImpactsTilePriority
& state
) {
172 global_state_
= state
;
175 void SetTileTaskRunnerForTesting(TileTaskRunner
* tile_task_runner
);
177 void FreeResourcesAndCleanUpReleasedTilesForTesting() {
178 FreeResourcesForReleasedTiles();
179 CleanUpReleasedTiles();
182 std::vector
<Tile
*> AllTilesForTesting() const {
183 std::vector
<Tile
*> tiles
;
184 for (TileMap::const_iterator it
= tiles_
.begin(); it
!= tiles_
.end();
186 tiles
.push_back(it
->second
);
191 void SetScheduledRasterTaskLimitForTesting(size_t limit
) {
192 scheduled_raster_task_limit_
= limit
;
195 void CheckIfMoreTilesNeedToBePreparedForTesting() {
196 CheckIfMoreTilesNeedToBePrepared();
199 void SetMoreTilesNeedToBeRasterizedForTesting() {
200 all_tiles_that_need_to_be_rasterized_are_scheduled_
= false;
203 bool HasScheduledTileTasksForTesting() const {
204 return has_scheduled_tile_tasks_
;
208 TileManager(TileManagerClient
* client
,
209 const scoped_refptr
<base::SequencedTaskRunner
>& task_runner
,
210 size_t scheduled_raster_task_limit
);
212 void FreeResourcesForReleasedTiles();
213 void CleanUpReleasedTiles();
216 // Virtual for testing.
217 virtual void Release(Tile
* tile
);
219 // Overriden from TileTaskRunnerClient:
220 void DidFinishRunningTileTasks(TaskSet task_set
) override
;
221 TaskSetCollection
TasksThatShouldBeForcedToComplete() const override
;
223 typedef std::vector
<PrioritizedTile
> PrioritizedTileVector
;
224 typedef std::set
<Tile
*> TileSet
;
227 virtual void ScheduleTasks(
228 const PrioritizedTileVector
& tiles_that_need_to_be_rasterized
);
230 void AssignGpuMemoryToTiles(
231 RasterTilePriorityQueue
* raster_priority_queue
,
232 size_t scheduled_raser_task_limit
,
233 PrioritizedTileVector
* tiles_that_need_to_be_rasterized
);
239 MemoryUsage(size_t memory_bytes
, size_t resource_count
);
241 static MemoryUsage
FromConfig(const gfx::Size
& size
, ResourceFormat format
);
242 static MemoryUsage
FromTile(const Tile
* tile
);
244 MemoryUsage
& operator+=(const MemoryUsage
& other
);
245 MemoryUsage
& operator-=(const MemoryUsage
& other
);
246 MemoryUsage
operator-(const MemoryUsage
& other
);
248 bool Exceeds(const MemoryUsage
& limit
) const;
249 int64
memory_bytes() const { return memory_bytes_
; }
256 void OnRasterTaskCompleted(Tile::Id tile
,
258 const RasterSource::SolidColorAnalysis
& analysis
,
260 void UpdateTileDrawInfo(Tile
* tile
,
262 const RasterSource::SolidColorAnalysis
& analysis
);
264 void FreeResourcesForTile(Tile
* tile
);
265 void FreeResourcesForTileAndNotifyClientIfTileWasReadyToDraw(Tile
* tile
);
266 scoped_refptr
<RasterTask
> CreateRasterTask(
267 const PrioritizedTile
& prioritized_tile
);
269 scoped_ptr
<EvictionTilePriorityQueue
>
270 FreeTileResourcesUntilUsageIsWithinLimit(
271 scoped_ptr
<EvictionTilePriorityQueue
> eviction_priority_queue
,
272 const MemoryUsage
& limit
,
274 scoped_ptr
<EvictionTilePriorityQueue
>
275 FreeTileResourcesWithLowerPriorityUntilUsageIsWithinLimit(
276 scoped_ptr
<EvictionTilePriorityQueue
> eviction_priority_queue
,
277 const MemoryUsage
& limit
,
278 const TilePriority
& oother_priority
,
280 bool TilePriorityViolatesMemoryPolicy(const TilePriority
& priority
);
281 bool AreRequiredTilesReadyToDraw(RasterTilePriorityQueue::Type type
) const;
282 void CheckIfMoreTilesNeedToBePrepared();
283 void CheckAndIssueSignals();
285 TileManagerClient
* client_
;
286 scoped_refptr
<base::SequencedTaskRunner
> task_runner_
;
287 ResourcePool
* resource_pool_
;
288 TileTaskRunner
* tile_task_runner_
;
289 GlobalStateThatImpactsTilePriority global_state_
;
290 size_t scheduled_raster_task_limit_
;
292 typedef base::hash_map
<Tile::Id
, Tile
*> TileMap
;
295 bool all_tiles_that_need_to_be_rasterized_are_scheduled_
;
296 MemoryHistory::Entry memory_stats_from_last_assign_
;
298 bool did_check_for_completed_tasks_since_last_schedule_tasks_
;
299 bool did_oom_on_last_assign_
;
301 ImageDecodeController image_decode_controller_
;
303 RasterTaskCompletionStats flush_stats_
;
305 std::vector
<Tile
*> released_tiles_
;
307 // Queue used when scheduling raster tasks.
308 TileTaskQueue raster_queue_
;
310 std::vector
<scoped_refptr
<RasterTask
>> orphan_raster_tasks_
;
312 UniqueNotifier more_tiles_need_prepare_check_notifier_
;
319 bool ready_to_activate
;
320 bool did_notify_ready_to_activate
;
322 bool did_notify_ready_to_draw
;
323 bool all_tile_tasks_completed
;
324 bool did_notify_all_tile_tasks_completed
;
327 UniqueNotifier signals_check_notifier_
;
329 bool has_scheduled_tile_tasks_
;
331 uint64_t prepare_tiles_count_
;
333 DISALLOW_COPY_AND_ASSIGN(TileManager
);
338 #endif // CC_TILES_TILE_MANAGER_H_