1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
|
// Copyright 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CC_RESOURCES_TILE_MANAGER_H_
#define CC_RESOURCES_TILE_MANAGER_H_
#include <queue>
#include <set>
#include <utility>
#include <vector>
#include "base/containers/hash_tables.h"
#include "base/memory/scoped_ptr.h"
#include "base/values.h"
#include "cc/base/ref_counted_managed.h"
#include "cc/debug/rendering_stats_instrumentation.h"
#include "cc/layers/picture_layer_impl.h"
#include "cc/resources/managed_tile_state.h"
#include "cc/resources/memory_history.h"
#include "cc/resources/picture_pile_impl.h"
#include "cc/resources/prioritized_tile_set.h"
#include "cc/resources/raster_worker_pool.h"
#include "cc/resources/resource_pool.h"
#include "cc/resources/tile.h"
namespace cc {
class RasterWorkerPoolDelegate;
class ResourceProvider;
class CC_EXPORT TileManagerClient {
public:
virtual void NotifyReadyToActivate() = 0;
protected:
virtual ~TileManagerClient() {}
};
struct RasterTaskCompletionStats {
RasterTaskCompletionStats();
size_t completed_count;
size_t canceled_count;
};
scoped_ptr<base::Value> RasterTaskCompletionStatsAsValue(
const RasterTaskCompletionStats& stats);
// This class manages tiles, deciding which should get rasterized and which
// should no longer have any memory assigned to them. Tile objects are "owned"
// by layers; they automatically register with the manager when they are
// created, and unregister from the manager when they are deleted.
class CC_EXPORT TileManager : public RasterWorkerPoolClient,
public RefCountedManager<Tile> {
public:
struct CC_EXPORT PairedPictureLayer {
PairedPictureLayer();
~PairedPictureLayer();
PictureLayerImpl* active_layer;
PictureLayerImpl* pending_layer;
};
class CC_EXPORT RasterTileIterator {
public:
RasterTileIterator(TileManager* tile_manager, TreePriority tree_priority);
~RasterTileIterator();
RasterTileIterator& operator++();
operator bool() const;
Tile* operator*();
private:
struct PairedPictureLayerIterator {
PairedPictureLayerIterator();
~PairedPictureLayerIterator();
Tile* PeekTile(TreePriority tree_priority);
void PopTile(TreePriority tree_priority);
std::pair<PictureLayerImpl::LayerRasterTileIterator*, WhichTree>
NextTileIterator(TreePriority tree_priority);
PictureLayerImpl::LayerRasterTileIterator active_iterator;
PictureLayerImpl::LayerRasterTileIterator pending_iterator;
std::vector<Tile*> returned_shared_tiles;
};
class RasterOrderComparator {
public:
explicit RasterOrderComparator(TreePriority tree_priority);
bool operator()(PairedPictureLayerIterator* a,
PairedPictureLayerIterator* b) const;
private:
bool ComparePriorities(const TilePriority& a_priority,
const TilePriority& b_priority,
bool prioritize_low_res) const;
TreePriority tree_priority_;
};
std::vector<PairedPictureLayerIterator> paired_iterators_;
std::vector<PairedPictureLayerIterator*> iterator_heap_;
TreePriority tree_priority_;
RasterOrderComparator comparator_;
};
static scoped_ptr<TileManager> Create(
TileManagerClient* client,
base::SequencedTaskRunner* task_runner,
ResourceProvider* resource_provider,
ContextProvider* context_provider,
RenderingStatsInstrumentation* rendering_stats_instrumentation,
bool use_map_image,
bool use_rasterize_on_demand,
size_t max_transfer_buffer_usage_bytes,
size_t max_raster_usage_bytes,
unsigned map_image_texture_target);
virtual ~TileManager();
void ManageTiles(const GlobalStateThatImpactsTilePriority& state);
// Returns true when visible tiles have been initialized.
bool UpdateVisibleTiles();
scoped_refptr<Tile> CreateTile(PicturePileImpl* picture_pile,
const gfx::Size& tile_size,
const gfx::Rect& content_rect,
const gfx::Rect& opaque_rect,
float contents_scale,
int layer_id,
int source_frame_number,
int flags);
void RegisterPictureLayerImpl(PictureLayerImpl* layer);
void UnregisterPictureLayerImpl(PictureLayerImpl* layer);
scoped_ptr<base::Value> BasicStateAsValue() const;
scoped_ptr<base::Value> AllTilesAsValue() const;
void GetMemoryStats(size_t* memory_required_bytes,
size_t* memory_nice_to_have_bytes,
size_t* memory_allocated_bytes,
size_t* memory_used_bytes) const;
const MemoryHistory::Entry& memory_stats_from_last_assign() const {
return memory_stats_from_last_assign_;
}
void GetPairedPictureLayers(std::vector<PairedPictureLayer>* layers) const;
ResourcePool* resource_pool() { return resource_pool_.get(); }
void InitializeTilesWithResourcesForTesting(const std::vector<Tile*>& tiles) {
for (size_t i = 0; i < tiles.size(); ++i) {
ManagedTileState& mts = tiles[i]->managed_state();
ManagedTileState::TileVersion& tile_version =
mts.tile_versions[HIGH_QUALITY_NO_LCD_RASTER_MODE];
tile_version.resource_ = resource_pool_->AcquireResource(gfx::Size(1, 1));
bytes_releasable_ += BytesConsumedIfAllocated(tiles[i]);
++resources_releasable_;
}
}
void SetGlobalStateForTesting(
const GlobalStateThatImpactsTilePriority& state) {
// Soft limit is used for resource pool such that
// memory returns to soft limit after going over.
if (state != global_state_) {
global_state_ = state;
prioritized_tiles_dirty_ = true;
resource_pool_->SetResourceUsageLimits(
global_state_.soft_memory_limit_in_bytes,
global_state_.unused_memory_limit_in_bytes,
global_state_.num_resources_limit);
}
}
protected:
TileManager(TileManagerClient* client,
base::SequencedTaskRunner* task_runner,
ResourceProvider* resource_provider,
ContextProvider* context_provider,
scoped_ptr<RasterWorkerPool> raster_worker_pool,
scoped_ptr<RasterWorkerPool> direct_raster_worker_pool,
size_t max_raster_usage_bytes,
RenderingStatsInstrumentation* rendering_stats_instrumentation,
bool use_rasterize_on_demand);
// Methods called by Tile
friend class Tile;
void DidChangeTilePriority(Tile* tile);
void CleanUpReleasedTiles();
// Overriden from RefCountedManager<Tile>:
virtual void Release(Tile* tile) OVERRIDE;
// Overriden from RasterWorkerPoolClient:
virtual bool ShouldForceTasksRequiredForActivationToComplete() const OVERRIDE;
virtual void DidFinishRunningTasks() OVERRIDE;
virtual void DidFinishRunningTasksRequiredForActivation() OVERRIDE;
typedef std::vector<Tile*> TileVector;
typedef std::set<Tile*> TileSet;
// Virtual for test
virtual void ScheduleTasks(
const TileVector& tiles_that_need_to_be_rasterized);
void AssignGpuMemoryToTiles(PrioritizedTileSet* tiles,
TileVector* tiles_that_need_to_be_rasterized);
void GetTilesWithAssignedBins(PrioritizedTileSet* tiles);
private:
enum RasterWorkerPoolType {
RASTER_WORKER_POOL_TYPE_DEFAULT,
RASTER_WORKER_POOL_TYPE_DIRECT,
NUM_RASTER_WORKER_POOL_TYPES
};
void OnImageDecodeTaskCompleted(int layer_id,
SkPixelRef* pixel_ref,
bool was_canceled);
void OnRasterTaskCompleted(Tile::Id tile,
scoped_ptr<ScopedResource> resource,
RasterMode raster_mode,
const PicturePileImpl::Analysis& analysis,
bool was_canceled);
inline size_t BytesConsumedIfAllocated(const Tile* tile) const {
return Resource::MemorySizeBytes(tile->size(),
raster_worker_pool_->GetResourceFormat());
}
void FreeResourceForTile(Tile* tile, RasterMode mode);
void FreeResourcesForTile(Tile* tile);
void FreeUnusedResourcesForTile(Tile* tile);
scoped_refptr<internal::WorkerPoolTask> CreateImageDecodeTask(
Tile* tile,
SkPixelRef* pixel_ref);
scoped_refptr<internal::RasterWorkerPoolTask> CreateRasterTask(Tile* tile);
scoped_ptr<base::Value> GetMemoryRequirementsAsValue() const;
void UpdatePrioritizedTileSetIfNeeded();
TileManagerClient* client_;
ContextProvider* context_provider_;
scoped_ptr<ResourcePool> resource_pool_;
scoped_ptr<RasterWorkerPool> raster_worker_pool_;
scoped_ptr<RasterWorkerPool> direct_raster_worker_pool_;
scoped_ptr<RasterWorkerPoolDelegate> raster_worker_pool_delegate_;
GlobalStateThatImpactsTilePriority global_state_;
typedef base::hash_map<Tile::Id, Tile*> TileMap;
TileMap tiles_;
PrioritizedTileSet prioritized_tiles_;
bool prioritized_tiles_dirty_;
bool all_tiles_that_need_to_be_rasterized_have_memory_;
bool all_tiles_required_for_activation_have_memory_;
size_t memory_required_bytes_;
size_t memory_nice_to_have_bytes_;
size_t bytes_releasable_;
size_t resources_releasable_;
size_t max_raster_usage_bytes_;
bool ever_exceeded_memory_budget_;
MemoryHistory::Entry memory_stats_from_last_assign_;
RenderingStatsInstrumentation* rendering_stats_instrumentation_;
bool did_initialize_visible_tile_;
bool did_check_for_completed_tasks_since_last_schedule_tasks_;
typedef base::hash_map<uint32_t, scoped_refptr<internal::WorkerPoolTask> >
PixelRefTaskMap;
typedef base::hash_map<int, PixelRefTaskMap> LayerPixelRefTaskMap;
LayerPixelRefTaskMap image_decode_tasks_;
typedef base::hash_map<int, int> LayerCountMap;
LayerCountMap used_layer_counts_;
RasterTaskCompletionStats update_visible_tiles_stats_;
std::vector<Tile*> released_tiles_;
bool use_rasterize_on_demand_;
// Queues used when scheduling raster tasks.
RasterTaskQueue raster_queue_[NUM_RASTER_WORKER_POOL_TYPES];
std::vector<scoped_refptr<internal::Task> > orphan_raster_tasks_;
std::vector<PictureLayerImpl*> layers_;
DISALLOW_COPY_AND_ASSIGN(TileManager);
};
} // namespace cc
#endif // CC_RESOURCES_TILE_MANAGER_H_
|