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/raster_tile_priority_queue.h"
24 #include "cc/tiles/tile.h"
25 #include "cc/tiles/tile_draw_info.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(const gfx::Size
& desired_texture_size
,
120 const gfx::Rect
& content_rect
,
121 float contents_scale
,
123 int source_frame_number
,
126 bool IsReadyToActivate() const;
127 bool IsReadyToDraw() const;
129 scoped_refptr
<base::trace_event::ConvertableToTraceFormat
> BasicStateAsValue()
131 void BasicStateAsValueInto(base::trace_event::TracedValue
* dict
) const;
132 const MemoryHistory::Entry
& memory_stats_from_last_assign() const {
133 return memory_stats_from_last_assign_
;
136 // Public methods for testing.
137 void InitializeTilesWithResourcesForTesting(const std::vector
<Tile
*>& tiles
) {
138 for (size_t i
= 0; i
< tiles
.size(); ++i
) {
139 TileDrawInfo
& draw_info
= tiles
[i
]->draw_info();
140 draw_info
.resource_
= resource_pool_
->AcquireResource(
141 tiles
[i
]->desired_texture_size(),
142 tile_task_runner_
->GetResourceFormat());
146 void ReleaseTileResourcesForTesting(const std::vector
<Tile
*>& tiles
) {
147 for (size_t i
= 0; i
< tiles
.size(); ++i
) {
148 Tile
* tile
= tiles
[i
];
149 FreeResourcesForTile(tile
);
153 void SetGlobalStateForTesting(
154 const GlobalStateThatImpactsTilePriority
& state
) {
155 global_state_
= state
;
158 void SetTileTaskRunnerForTesting(TileTaskRunner
* tile_task_runner
);
160 void FreeResourcesAndCleanUpReleasedTilesForTesting() {
161 FreeResourcesForReleasedTiles();
162 CleanUpReleasedTiles();
165 std::vector
<Tile
*> AllTilesForTesting() const {
166 std::vector
<Tile
*> tiles
;
167 for (TileMap::const_iterator it
= tiles_
.begin(); it
!= tiles_
.end();
169 tiles
.push_back(it
->second
);
174 void SetScheduledRasterTaskLimitForTesting(size_t limit
) {
175 scheduled_raster_task_limit_
= limit
;
178 void CheckIfMoreTilesNeedToBePreparedForTesting() {
179 CheckIfMoreTilesNeedToBePrepared();
183 TileManager(TileManagerClient
* client
,
184 const scoped_refptr
<base::SequencedTaskRunner
>& task_runner
,
185 ResourcePool
* resource_pool
,
186 TileTaskRunner
* tile_task_runner
,
187 size_t scheduled_raster_task_limit
);
189 void FreeResourcesForReleasedTiles();
190 void CleanUpReleasedTiles();
193 // Virtual for testing.
194 virtual void Release(Tile
* tile
);
196 // Overriden from TileTaskRunnerClient:
197 void DidFinishRunningTileTasks(TaskSet task_set
) override
;
198 TaskSetCollection
TasksThatShouldBeForcedToComplete() const override
;
200 typedef std::vector
<PrioritizedTile
> PrioritizedTileVector
;
201 typedef std::set
<Tile
*> TileSet
;
204 virtual void ScheduleTasks(
205 const PrioritizedTileVector
& tiles_that_need_to_be_rasterized
);
207 void AssignGpuMemoryToTiles(
208 RasterTilePriorityQueue
* raster_priority_queue
,
209 size_t scheduled_raser_task_limit
,
210 PrioritizedTileVector
* 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(
249 const PrioritizedTile
& prioritized_tile
);
251 scoped_ptr
<EvictionTilePriorityQueue
>
252 FreeTileResourcesUntilUsageIsWithinLimit(
253 scoped_ptr
<EvictionTilePriorityQueue
> eviction_priority_queue
,
254 const MemoryUsage
& limit
,
256 scoped_ptr
<EvictionTilePriorityQueue
>
257 FreeTileResourcesWithLowerPriorityUntilUsageIsWithinLimit(
258 scoped_ptr
<EvictionTilePriorityQueue
> eviction_priority_queue
,
259 const MemoryUsage
& limit
,
260 const TilePriority
& oother_priority
,
262 bool TilePriorityViolatesMemoryPolicy(const TilePriority
& priority
);
263 bool AreRequiredTilesReadyToDraw(RasterTilePriorityQueue::Type type
) const;
264 void NotifyReadyToActivate();
265 void NotifyReadyToDraw();
266 void CheckIfReadyToActivate();
267 void CheckIfReadyToDraw();
268 void CheckIfMoreTilesNeedToBePrepared();
270 TileManagerClient
* client_
;
271 scoped_refptr
<base::SequencedTaskRunner
> task_runner_
;
272 ResourcePool
* resource_pool_
;
273 TileTaskRunner
* tile_task_runner_
;
274 GlobalStateThatImpactsTilePriority global_state_
;
275 size_t scheduled_raster_task_limit_
;
277 typedef base::hash_map
<Tile::Id
, Tile
*> TileMap
;
280 bool all_tiles_that_need_to_be_rasterized_are_scheduled_
;
281 MemoryHistory::Entry memory_stats_from_last_assign_
;
283 bool did_check_for_completed_tasks_since_last_schedule_tasks_
;
284 bool did_oom_on_last_assign_
;
286 typedef base::hash_map
<uint32_t, scoped_refptr
<ImageDecodeTask
>>
288 typedef base::hash_map
<int, PixelRefTaskMap
> LayerPixelRefTaskMap
;
289 LayerPixelRefTaskMap image_decode_tasks_
;
291 typedef base::hash_map
<int, int> LayerCountMap
;
292 LayerCountMap used_layer_counts_
;
294 RasterTaskCompletionStats update_visible_tiles_stats_
;
296 std::vector
<Tile
*> released_tiles_
;
298 // Queue used when scheduling raster tasks.
299 TileTaskQueue raster_queue_
;
301 std::vector
<scoped_refptr
<RasterTask
>> orphan_raster_tasks_
;
303 UniqueNotifier ready_to_activate_check_notifier_
;
304 UniqueNotifier ready_to_draw_check_notifier_
;
305 UniqueNotifier more_tiles_need_prepare_check_notifier_
;
307 bool did_notify_ready_to_activate_
;
308 bool did_notify_ready_to_draw_
;
310 DISALLOW_COPY_AND_ASSIGN(TileManager
);
315 #endif // CC_TILES_TILE_MANAGER_H_