OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #ifndef CC_RESOURCES_TILE_MANAGER_H_ | |
6 #define CC_RESOURCES_TILE_MANAGER_H_ | |
7 | |
8 #include <deque> | |
9 #include <queue> | |
10 #include <set> | |
11 #include <utility> | |
12 #include <vector> | |
13 | |
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/resources/eviction_tile_priority_queue.h" | |
19 #include "cc/resources/memory_history.h" | |
20 #include "cc/resources/raster_source.h" | |
21 #include "cc/resources/raster_tile_priority_queue.h" | |
22 #include "cc/resources/resource_pool.h" | |
23 #include "cc/resources/tile.h" | |
24 #include "cc/resources/tile_draw_info.h" | |
25 #include "cc/resources/tile_task_runner.h" | |
26 | |
27 namespace base { | |
28 namespace trace_event { | |
29 class ConvertableToTraceFormat; | |
30 class TracedValue; | |
31 } | |
32 } | |
33 | |
34 namespace cc { | |
35 class PictureLayerImpl; | |
36 class ResourceProvider; | |
37 | |
38 class CC_EXPORT TileManagerClient { | |
39 public: | |
40 // Called when all tiles marked as required for activation are ready to draw. | |
41 virtual void NotifyReadyToActivate() = 0; | |
42 | |
43 // Called when all tiles marked as required for draw are ready to draw. | |
44 virtual void NotifyReadyToDraw() = 0; | |
45 | |
46 // Called when the visible representation of a tile might have changed. Some | |
47 // examples are: | |
48 // - Tile version initialized. | |
49 // - Tile resources freed. | |
50 // - Tile marked for on-demand raster. | |
51 virtual void NotifyTileStateChanged(const Tile* tile) = 0; | |
52 | |
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; | |
59 | |
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; | |
65 | |
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; | |
70 | |
71 protected: | |
72 virtual ~TileManagerClient() {} | |
73 }; | |
74 | |
75 struct RasterTaskCompletionStats { | |
76 RasterTaskCompletionStats(); | |
77 | |
78 size_t completed_count; | |
79 size_t canceled_count; | |
80 }; | |
81 scoped_refptr<base::trace_event::ConvertableToTraceFormat> | |
82 RasterTaskCompletionStatsAsValue(const RasterTaskCompletionStats& stats); | |
83 | |
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 { | |
89 public: | |
90 enum NamedTaskSet { | |
91 REQUIRED_FOR_ACTIVATION, | |
92 REQUIRED_FOR_DRAW, | |
93 // PixelBufferTileTaskWorkerPool depends on ALL being last. | |
94 ALL | |
95 // Adding additional values requires increasing kNumberOfTaskSets in | |
96 // tile_task_runner.h | |
97 }; | |
98 | |
99 static_assert(NamedTaskSet::ALL == (kNumberOfTaskSets - 1), | |
100 "NamedTaskSet::ALL should be equal to kNumberOfTaskSets" | |
101 "minus 1"); | |
102 | |
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; | |
109 | |
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); | |
116 | |
117 void UpdateVisibleTiles(const GlobalStateThatImpactsTilePriority& state); | |
118 | |
119 ScopedTilePtr CreateTile(const gfx::Size& desired_texture_size, | |
120 const gfx::Rect& content_rect, | |
121 float contents_scale, | |
122 int layer_id, | |
123 int source_frame_number, | |
124 int flags); | |
125 | |
126 bool IsReadyToActivate() const; | |
127 bool IsReadyToDraw() const; | |
128 | |
129 scoped_refptr<base::trace_event::ConvertableToTraceFormat> BasicStateAsValue() | |
130 const; | |
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_; | |
134 } | |
135 | |
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()); | |
143 } | |
144 } | |
145 | |
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); | |
150 } | |
151 } | |
152 | |
153 void SetGlobalStateForTesting( | |
154 const GlobalStateThatImpactsTilePriority& state) { | |
155 global_state_ = state; | |
156 } | |
157 | |
158 void SetTileTaskRunnerForTesting(TileTaskRunner* tile_task_runner); | |
159 | |
160 void FreeResourcesAndCleanUpReleasedTilesForTesting() { | |
161 FreeResourcesForReleasedTiles(); | |
162 CleanUpReleasedTiles(); | |
163 } | |
164 | |
165 std::vector<Tile*> AllTilesForTesting() const { | |
166 std::vector<Tile*> tiles; | |
167 for (TileMap::const_iterator it = tiles_.begin(); it != tiles_.end(); | |
168 ++it) { | |
169 tiles.push_back(it->second); | |
170 } | |
171 return tiles; | |
172 } | |
173 | |
174 void SetScheduledRasterTaskLimitForTesting(size_t limit) { | |
175 scheduled_raster_task_limit_ = limit; | |
176 } | |
177 | |
178 void CheckIfMoreTilesNeedToBePreparedForTesting() { | |
179 CheckIfMoreTilesNeedToBePrepared(); | |
180 } | |
181 | |
182 protected: | |
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); | |
188 | |
189 void FreeResourcesForReleasedTiles(); | |
190 void CleanUpReleasedTiles(); | |
191 | |
192 friend class Tile; | |
193 // Virtual for testing. | |
194 virtual void Release(Tile* tile); | |
195 | |
196 // Overriden from TileTaskRunnerClient: | |
197 void DidFinishRunningTileTasks(TaskSet task_set) override; | |
198 TaskSetCollection TasksThatShouldBeForcedToComplete() const override; | |
199 | |
200 typedef std::vector<PrioritizedTile> PrioritizedTileVector; | |
201 typedef std::set<Tile*> TileSet; | |
202 | |
203 // Virtual for test | |
204 virtual void ScheduleTasks( | |
205 const PrioritizedTileVector& tiles_that_need_to_be_rasterized); | |
206 | |
207 void AssignGpuMemoryToTiles( | |
208 RasterTilePriorityQueue* raster_priority_queue, | |
209 size_t scheduled_raser_task_limit, | |
210 PrioritizedTileVector* tiles_that_need_to_be_rasterized); | |
211 | |
212 private: | |
213 class MemoryUsage { | |
214 public: | |
215 MemoryUsage(); | |
216 MemoryUsage(int64 memory_bytes, int resource_count); | |
217 | |
218 static MemoryUsage FromConfig(const gfx::Size& size, ResourceFormat format); | |
219 static MemoryUsage FromTile(const Tile* tile); | |
220 | |
221 MemoryUsage& operator+=(const MemoryUsage& other); | |
222 MemoryUsage& operator-=(const MemoryUsage& other); | |
223 MemoryUsage operator-(const MemoryUsage& other); | |
224 | |
225 bool Exceeds(const MemoryUsage& limit) const; | |
226 int64 memory_bytes() const { return memory_bytes_; } | |
227 | |
228 private: | |
229 int64 memory_bytes_; | |
230 int resource_count_; | |
231 }; | |
232 | |
233 void OnImageDecodeTaskCompleted(int layer_id, | |
234 SkPixelRef* pixel_ref, | |
235 bool was_canceled); | |
236 void OnRasterTaskCompleted(Tile::Id tile, | |
237 scoped_ptr<ScopedResource> resource, | |
238 const RasterSource::SolidColorAnalysis& analysis, | |
239 bool was_canceled); | |
240 void UpdateTileDrawInfo(Tile* tile, | |
241 scoped_ptr<ScopedResource> resource, | |
242 const RasterSource::SolidColorAnalysis& analysis); | |
243 | |
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); | |
250 | |
251 scoped_ptr<EvictionTilePriorityQueue> | |
252 FreeTileResourcesUntilUsageIsWithinLimit( | |
253 scoped_ptr<EvictionTilePriorityQueue> eviction_priority_queue, | |
254 const MemoryUsage& limit, | |
255 MemoryUsage* usage); | |
256 scoped_ptr<EvictionTilePriorityQueue> | |
257 FreeTileResourcesWithLowerPriorityUntilUsageIsWithinLimit( | |
258 scoped_ptr<EvictionTilePriorityQueue> eviction_priority_queue, | |
259 const MemoryUsage& limit, | |
260 const TilePriority& oother_priority, | |
261 MemoryUsage* usage); | |
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(); | |
269 | |
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_; | |
276 | |
277 typedef base::hash_map<Tile::Id, Tile*> TileMap; | |
278 TileMap tiles_; | |
279 | |
280 bool all_tiles_that_need_to_be_rasterized_are_scheduled_; | |
281 MemoryHistory::Entry memory_stats_from_last_assign_; | |
282 | |
283 bool did_check_for_completed_tasks_since_last_schedule_tasks_; | |
284 bool did_oom_on_last_assign_; | |
285 | |
286 typedef base::hash_map<uint32_t, scoped_refptr<ImageDecodeTask>> | |
287 PixelRefTaskMap; | |
288 typedef base::hash_map<int, PixelRefTaskMap> LayerPixelRefTaskMap; | |
289 LayerPixelRefTaskMap image_decode_tasks_; | |
290 | |
291 typedef base::hash_map<int, int> LayerCountMap; | |
292 LayerCountMap used_layer_counts_; | |
293 | |
294 RasterTaskCompletionStats update_visible_tiles_stats_; | |
295 | |
296 std::vector<Tile*> released_tiles_; | |
297 | |
298 // Queue used when scheduling raster tasks. | |
299 TileTaskQueue raster_queue_; | |
300 | |
301 std::vector<scoped_refptr<RasterTask>> orphan_raster_tasks_; | |
302 | |
303 UniqueNotifier ready_to_activate_check_notifier_; | |
304 UniqueNotifier ready_to_draw_check_notifier_; | |
305 UniqueNotifier more_tiles_need_prepare_check_notifier_; | |
306 | |
307 bool did_notify_ready_to_activate_; | |
308 bool did_notify_ready_to_draw_; | |
309 | |
310 DISALLOW_COPY_AND_ASSIGN(TileManager); | |
311 }; | |
312 | |
313 } // namespace cc | |
314 | |
315 #endif // CC_RESOURCES_TILE_MANAGER_H_ | |
OLD | NEW |