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
;
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 the visible representation of a tile might have changed. Some
49 // - Tile version initialized.
50 // - Tile resources freed.
51 // - Tile marked for on-demand raster.
52 virtual void NotifyTileStateChanged(const Tile
* tile
) = 0;
54 // Given an empty raster tile priority queue, this will build a priority queue
55 // that will return tiles in order in which they should be rasterized.
56 // Note if the queue was previous built, Reset must be called on it.
57 virtual scoped_ptr
<RasterTilePriorityQueue
> BuildRasterQueue(
58 TreePriority tree_priority
,
59 RasterTilePriorityQueue::Type type
) = 0;
61 // Given an empty eviction tile priority queue, this will build a priority
62 // queue that will return tiles in order in which they should be evicted.
63 // Note if the queue was previous built, Reset must be called on it.
64 virtual scoped_ptr
<EvictionTilePriorityQueue
> BuildEvictionQueue(
65 TreePriority tree_priority
) = 0;
67 // Informs the client that due to the currently rasterizing (or scheduled to
68 // be rasterized) tiles, we will be in a position that will likely require a
69 // draw. This can be used to preemptively start a frame.
70 virtual void SetIsLikelyToRequireADraw(bool is_likely_to_require_a_draw
) = 0;
73 virtual ~TileManagerClient() {}
76 struct RasterTaskCompletionStats
{
77 RasterTaskCompletionStats();
79 size_t completed_count
;
80 size_t canceled_count
;
82 scoped_refptr
<base::trace_event::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
> {
93 REQUIRED_FOR_ACTIVATION
,
95 // PixelBufferTileTaskWorkerPool depends on ALL being last.
97 // Adding additional values requires increasing kNumberOfTaskSets in
101 static_assert(NamedTaskSet::ALL
== (kNumberOfTaskSets
- 1),
102 "NamedTaskSet::ALL should be equal to kNumberOfTaskSets"
105 static scoped_ptr
<TileManager
> Create(TileManagerClient
* client
,
106 base::SequencedTaskRunner
* task_runner
,
107 ResourcePool
* resource_pool
,
108 TileTaskRunner
* tile_task_runner
,
109 size_t scheduled_raster_task_limit
);
110 ~TileManager() override
;
112 // Assigns tile memory and schedules work to prepare tiles for drawing.
113 // - Runs client_->NotifyReadyToActivate() when all tiles required for
114 // activation are prepared, or failed to prepare due to OOM.
115 // - Runs client_->NotifyReadyToDraw() when all tiles required draw are
116 // prepared, or failed to prepare due to OOM.
117 void PrepareTiles(const GlobalStateThatImpactsTilePriority
& state
);
119 void UpdateVisibleTiles(const GlobalStateThatImpactsTilePriority
& state
);
121 scoped_refptr
<Tile
> CreateTile(RasterSource
* raster_source
,
122 const gfx::Size
& desired_texture_size
,
123 const gfx::Rect
& content_rect
,
124 float contents_scale
,
126 int source_frame_number
,
129 bool IsReadyToActivate() const;
130 bool IsReadyToDraw() const;
132 scoped_refptr
<base::trace_event::ConvertableToTraceFormat
> BasicStateAsValue()
134 void BasicStateAsValueInto(base::trace_event::TracedValue
* dict
) const;
135 const MemoryHistory::Entry
& memory_stats_from_last_assign() const {
136 return memory_stats_from_last_assign_
;
139 // Public methods for testing.
140 void InitializeTilesWithResourcesForTesting(const std::vector
<Tile
*>& tiles
) {
141 for (size_t i
= 0; i
< tiles
.size(); ++i
) {
142 TileDrawInfo
& draw_info
= tiles
[i
]->draw_info();
143 draw_info
.resource_
= resource_pool_
->AcquireResource(
144 tiles
[i
]->desired_texture_size(),
145 tile_task_runner_
->GetResourceFormat());
149 void ReleaseTileResourcesForTesting(const std::vector
<Tile
*>& tiles
) {
150 for (size_t i
= 0; i
< tiles
.size(); ++i
) {
151 Tile
* tile
= tiles
[i
];
152 FreeResourcesForTile(tile
);
156 void SetGlobalStateForTesting(
157 const GlobalStateThatImpactsTilePriority
& state
) {
158 global_state_
= state
;
161 void SetTileTaskRunnerForTesting(TileTaskRunner
* tile_task_runner
);
163 void FreeResourcesAndCleanUpReleasedTilesForTesting() {
164 FreeResourcesForReleasedTiles();
165 CleanUpReleasedTiles();
168 std::vector
<Tile
*> AllTilesForTesting() const {
169 std::vector
<Tile
*> tiles
;
170 for (TileMap::const_iterator it
= tiles_
.begin(); it
!= tiles_
.end();
172 tiles
.push_back(it
->second
);
177 void SetScheduledRasterTaskLimitForTesting(size_t limit
) {
178 scheduled_raster_task_limit_
= limit
;
181 void CheckIfMoreTilesNeedToBePreparedForTesting() {
182 CheckIfMoreTilesNeedToBePrepared();
186 TileManager(TileManagerClient
* client
,
187 const scoped_refptr
<base::SequencedTaskRunner
>& task_runner
,
188 ResourcePool
* resource_pool
,
189 TileTaskRunner
* tile_task_runner
,
190 size_t scheduled_raster_task_limit
);
192 void FreeResourcesForReleasedTiles();
193 void CleanUpReleasedTiles();
195 // Overriden from RefCountedManager<Tile>:
197 void Release(Tile
* tile
) override
;
199 // Overriden from TileTaskRunnerClient:
200 void DidFinishRunningTileTasks(TaskSet task_set
) override
;
201 TaskSetCollection
TasksThatShouldBeForcedToComplete() const override
;
203 typedef std::vector
<Tile
*> TileVector
;
204 typedef std::set
<Tile
*> TileSet
;
207 virtual void ScheduleTasks(
208 const TileVector
& tiles_that_need_to_be_rasterized
);
210 void AssignGpuMemoryToTiles(RasterTilePriorityQueue
* raster_priority_queue
,
211 size_t scheduled_raser_task_limit
,
212 TileVector
* tiles_that_need_to_be_rasterized
);
218 MemoryUsage(int64 memory_bytes
, int resource_count
);
220 static MemoryUsage
FromConfig(const gfx::Size
& size
, ResourceFormat format
);
221 static MemoryUsage
FromTile(const Tile
* tile
);
223 MemoryUsage
& operator+=(const MemoryUsage
& other
);
224 MemoryUsage
& operator-=(const MemoryUsage
& other
);
225 MemoryUsage
operator-(const MemoryUsage
& other
);
227 bool Exceeds(const MemoryUsage
& limit
) const;
228 int64
memory_bytes() const { return memory_bytes_
; }
235 void OnImageDecodeTaskCompleted(int layer_id
,
236 SkPixelRef
* pixel_ref
,
238 void OnRasterTaskCompleted(Tile::Id tile
,
239 scoped_ptr
<ScopedResource
> resource
,
240 const RasterSource::SolidColorAnalysis
& analysis
,
242 void UpdateTileDrawInfo(Tile
* tile
,
243 scoped_ptr
<ScopedResource
> resource
,
244 const RasterSource::SolidColorAnalysis
& analysis
);
246 void FreeResourcesForTile(Tile
* tile
);
247 void FreeResourcesForTileAndNotifyClientIfTileWasReadyToDraw(Tile
* tile
);
248 scoped_refptr
<ImageDecodeTask
> CreateImageDecodeTask(Tile
* tile
,
249 SkPixelRef
* pixel_ref
);
250 scoped_refptr
<RasterTask
> CreateRasterTask(Tile
* tile
);
252 scoped_ptr
<EvictionTilePriorityQueue
>
253 FreeTileResourcesUntilUsageIsWithinLimit(
254 scoped_ptr
<EvictionTilePriorityQueue
> eviction_priority_queue
,
255 const MemoryUsage
& limit
,
257 scoped_ptr
<EvictionTilePriorityQueue
>
258 FreeTileResourcesWithLowerPriorityUntilUsageIsWithinLimit(
259 scoped_ptr
<EvictionTilePriorityQueue
> eviction_priority_queue
,
260 const MemoryUsage
& limit
,
261 const TilePriority
& oother_priority
,
263 bool TilePriorityViolatesMemoryPolicy(const TilePriority
& priority
);
264 bool AreRequiredTilesReadyToDraw(RasterTilePriorityQueue::Type type
) const;
265 void NotifyReadyToActivate();
266 void NotifyReadyToDraw();
267 void CheckIfReadyToActivate();
268 void CheckIfReadyToDraw();
269 void CheckIfMoreTilesNeedToBePrepared();
271 TileManagerClient
* client_
;
272 scoped_refptr
<base::SequencedTaskRunner
> task_runner_
;
273 ResourcePool
* resource_pool_
;
274 TileTaskRunner
* tile_task_runner_
;
275 GlobalStateThatImpactsTilePriority global_state_
;
276 size_t scheduled_raster_task_limit_
;
278 typedef base::hash_map
<Tile::Id
, Tile
*> TileMap
;
281 bool all_tiles_that_need_to_be_rasterized_are_scheduled_
;
282 MemoryHistory::Entry memory_stats_from_last_assign_
;
284 bool did_check_for_completed_tasks_since_last_schedule_tasks_
;
285 bool did_oom_on_last_assign_
;
287 typedef base::hash_map
<uint32_t, scoped_refptr
<ImageDecodeTask
>>
289 typedef base::hash_map
<int, PixelRefTaskMap
> LayerPixelRefTaskMap
;
290 LayerPixelRefTaskMap image_decode_tasks_
;
292 typedef base::hash_map
<int, int> LayerCountMap
;
293 LayerCountMap used_layer_counts_
;
295 RasterTaskCompletionStats update_visible_tiles_stats_
;
297 std::vector
<Tile
*> released_tiles_
;
299 // Queue used when scheduling raster tasks.
300 TileTaskQueue raster_queue_
;
302 std::vector
<scoped_refptr
<RasterTask
>> orphan_raster_tasks_
;
304 UniqueNotifier ready_to_activate_check_notifier_
;
305 UniqueNotifier ready_to_draw_check_notifier_
;
306 UniqueNotifier more_tiles_need_prepare_check_notifier_
;
308 bool did_notify_ready_to_activate_
;
309 bool did_notify_ready_to_draw_
;
311 DISALLOW_COPY_AND_ASSIGN(TileManager
);
316 #endif // CC_RESOURCES_TILE_MANAGER_H_