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_
12 #include "base/containers/hash_tables.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/values.h"
15 #include "cc/base/ref_counted_managed.h"
16 #include "cc/debug/rendering_stats_instrumentation.h"
17 #include "cc/resources/managed_tile_state.h"
18 #include "cc/resources/memory_history.h"
19 #include "cc/resources/picture_pile_impl.h"
20 #include "cc/resources/prioritized_tile_set.h"
21 #include "cc/resources/raster_worker_pool.h"
22 #include "cc/resources/resource_pool.h"
23 #include "cc/resources/tile.h"
26 class RasterWorkerPoolDelegate
;
27 class ResourceProvider
;
29 class CC_EXPORT TileManagerClient
{
31 virtual void NotifyReadyToActivate() = 0;
34 virtual ~TileManagerClient() {}
37 struct RasterTaskCompletionStats
{
38 RasterTaskCompletionStats();
40 size_t completed_count
;
41 size_t canceled_count
;
43 scoped_ptr
<base::Value
> RasterTaskCompletionStatsAsValue(
44 const RasterTaskCompletionStats
& stats
);
46 // This class manages tiles, deciding which should get rasterized and which
47 // should no longer have any memory assigned to them. Tile objects are "owned"
48 // by layers; they automatically register with the manager when they are
49 // created, and unregister from the manager when they are deleted.
50 class CC_EXPORT TileManager
: public RasterWorkerPoolClient
,
51 public RefCountedManager
<Tile
> {
53 static scoped_ptr
<TileManager
> Create(
54 TileManagerClient
* client
,
55 ResourceProvider
* resource_provider
,
56 ContextProvider
* context_provider
,
57 RenderingStatsInstrumentation
* rendering_stats_instrumentation
,
59 bool use_rasterize_on_demand
,
60 size_t max_transfer_buffer_usage_bytes
,
61 size_t max_raster_usage_bytes
,
62 unsigned map_image_texture_target
);
63 virtual ~TileManager();
65 void ManageTiles(const GlobalStateThatImpactsTilePriority
& state
);
67 // Returns true when visible tiles have been initialized.
68 bool UpdateVisibleTiles();
70 scoped_refptr
<Tile
> CreateTile(PicturePileImpl
* picture_pile
,
71 const gfx::Size
& tile_size
,
72 const gfx::Rect
& content_rect
,
73 const gfx::Rect
& opaque_rect
,
76 int source_frame_number
,
79 scoped_ptr
<base::Value
> BasicStateAsValue() const;
80 scoped_ptr
<base::Value
> AllTilesAsValue() const;
81 void GetMemoryStats(size_t* memory_required_bytes
,
82 size_t* memory_nice_to_have_bytes
,
83 size_t* memory_allocated_bytes
,
84 size_t* memory_used_bytes
) const;
86 const MemoryHistory::Entry
& memory_stats_from_last_assign() const {
87 return memory_stats_from_last_assign_
;
90 void InitializeTilesWithResourcesForTesting(
91 const std::vector
<Tile
*>& tiles
,
92 ResourceProvider
* resource_provider
) {
93 for (size_t i
= 0; i
< tiles
.size(); ++i
) {
94 ManagedTileState
& mts
= tiles
[i
]->managed_state();
95 ManagedTileState::TileVersion
& tile_version
=
96 mts
.tile_versions
[HIGH_QUALITY_NO_LCD_RASTER_MODE
];
98 tile_version
.resource_
= resource_pool_
->AcquireResource(gfx::Size(1, 1));
100 bytes_releasable_
+= BytesConsumedIfAllocated(tiles
[i
]);
101 ++resources_releasable_
;
105 void SetGlobalStateForTesting(
106 const GlobalStateThatImpactsTilePriority
& state
) {
107 // Soft limit is used for resource pool such that
108 // memory returns to soft limit after going over.
109 if (state
!= global_state_
) {
110 global_state_
= state
;
111 prioritized_tiles_dirty_
= true;
112 resource_pool_
->SetResourceUsageLimits(
113 global_state_
.soft_memory_limit_in_bytes
,
114 global_state_
.unused_memory_limit_in_bytes
,
115 global_state_
.num_resources_limit
);
120 TileManager(TileManagerClient
* client
,
121 ResourceProvider
* resource_provider
,
122 ContextProvider
* context_provider
,
123 scoped_ptr
<RasterWorkerPool
> raster_worker_pool
,
124 scoped_ptr
<RasterWorkerPool
> direct_raster_worker_pool
,
125 size_t max_raster_usage_bytes
,
126 RenderingStatsInstrumentation
* rendering_stats_instrumentation
,
127 bool use_rasterize_on_demand
);
129 // Methods called by Tile
131 void DidChangeTilePriority(Tile
* tile
);
133 void CleanUpReleasedTiles();
135 // Overriden from RefCountedManager<Tile>:
136 virtual void Release(Tile
* tile
) OVERRIDE
;
138 // Overriden from RasterWorkerPoolClient:
139 virtual bool ShouldForceTasksRequiredForActivationToComplete() const OVERRIDE
;
140 virtual void DidFinishRunningTasks() OVERRIDE
;
141 virtual void DidFinishRunningTasksRequiredForActivation() OVERRIDE
;
143 typedef std::vector
<Tile
*> TileVector
;
144 typedef std::set
<Tile
*> TileSet
;
147 virtual void ScheduleTasks(
148 const TileVector
& tiles_that_need_to_be_rasterized
);
150 void AssignGpuMemoryToTiles(PrioritizedTileSet
* tiles
,
151 TileVector
* tiles_that_need_to_be_rasterized
);
152 void GetTilesWithAssignedBins(PrioritizedTileSet
* tiles
);
155 enum RasterWorkerPoolType
{
156 RASTER_WORKER_POOL_TYPE_DEFAULT
,
157 RASTER_WORKER_POOL_TYPE_DIRECT
,
158 NUM_RASTER_WORKER_POOL_TYPES
161 void OnImageDecodeTaskCompleted(int layer_id
,
162 SkPixelRef
* pixel_ref
,
164 void OnRasterTaskCompleted(Tile::Id tile
,
165 scoped_ptr
<ScopedResource
> resource
,
166 RasterMode raster_mode
,
167 const PicturePileImpl::Analysis
& analysis
,
170 inline size_t BytesConsumedIfAllocated(const Tile
* tile
) const {
171 return Resource::MemorySizeBytes(tile
->size(),
172 raster_worker_pool_
->GetResourceFormat());
175 RasterMode
DetermineRasterMode(const Tile
* tile
) const;
176 void FreeResourceForTile(Tile
* tile
, RasterMode mode
);
177 void FreeResourcesForTile(Tile
* tile
);
178 void FreeUnusedResourcesForTile(Tile
* tile
);
179 scoped_refptr
<internal::WorkerPoolTask
> CreateImageDecodeTask(
181 SkPixelRef
* pixel_ref
);
182 scoped_refptr
<internal::RasterWorkerPoolTask
> CreateRasterTask(Tile
* tile
);
183 scoped_ptr
<base::Value
> GetMemoryRequirementsAsValue() const;
184 void UpdatePrioritizedTileSetIfNeeded();
186 TileManagerClient
* client_
;
187 ContextProvider
* context_provider_
;
188 scoped_ptr
<ResourcePool
> resource_pool_
;
189 scoped_ptr
<RasterWorkerPool
> raster_worker_pool_
;
190 scoped_ptr
<RasterWorkerPool
> direct_raster_worker_pool_
;
191 scoped_ptr
<RasterWorkerPoolDelegate
> raster_worker_pool_delegate_
;
192 GlobalStateThatImpactsTilePriority global_state_
;
194 typedef base::hash_map
<Tile::Id
, Tile
*> TileMap
;
197 PrioritizedTileSet prioritized_tiles_
;
198 bool prioritized_tiles_dirty_
;
200 bool all_tiles_that_need_to_be_rasterized_have_memory_
;
201 bool all_tiles_required_for_activation_have_memory_
;
203 size_t memory_required_bytes_
;
204 size_t memory_nice_to_have_bytes_
;
206 size_t bytes_releasable_
;
207 size_t resources_releasable_
;
208 size_t max_raster_usage_bytes_
;
210 bool ever_exceeded_memory_budget_
;
211 MemoryHistory::Entry memory_stats_from_last_assign_
;
213 RenderingStatsInstrumentation
* rendering_stats_instrumentation_
;
215 bool did_initialize_visible_tile_
;
216 bool did_check_for_completed_tasks_since_last_schedule_tasks_
;
218 typedef base::hash_map
<uint32_t, scoped_refptr
<internal::WorkerPoolTask
> >
220 typedef base::hash_map
<int, PixelRefTaskMap
> LayerPixelRefTaskMap
;
221 LayerPixelRefTaskMap image_decode_tasks_
;
223 typedef base::hash_map
<int, int> LayerCountMap
;
224 LayerCountMap used_layer_counts_
;
226 RasterTaskCompletionStats update_visible_tiles_stats_
;
228 std::vector
<Tile
*> released_tiles_
;
230 bool use_rasterize_on_demand_
;
232 // Queues used when scheduling raster tasks.
233 RasterTaskQueue raster_queue_
[NUM_RASTER_WORKER_POOL_TYPES
];
235 std::vector
<scoped_refptr
<internal::Task
> > orphan_raster_tasks_
;
237 DISALLOW_COPY_AND_ASSIGN(TileManager
);
242 #endif // CC_RESOURCES_TILE_MANAGER_H_