[refactor] More post-NSS WebCrypto cleanups (utility functions).
[chromium-blink-merge.git] / cc / tiles / tile_manager.h
blob607d4d66f1b7509d260a2d04a9f3c99bc2669e78
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_
8 #include <deque>
9 #include <queue>
10 #include <set>
11 #include <utility>
12 #include <vector>
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/image_decode_controller.h"
24 #include "cc/tiles/raster_tile_priority_queue.h"
25 #include "cc/tiles/tile.h"
26 #include "cc/tiles/tile_draw_info.h"
28 namespace base {
29 namespace trace_event {
30 class ConvertableToTraceFormat;
31 class TracedValue;
35 namespace cc {
36 class PictureLayerImpl;
37 class ResourceProvider;
39 class CC_EXPORT TileManagerClient {
40 public:
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 all tile tasks started by the most recent call to PrepareTiles
48 // are completed.
49 virtual void NotifyAllTileTasksCompleted() = 0;
51 // Called when the visible representation of a tile might have changed. Some
52 // examples are:
53 // - Tile version initialized.
54 // - Tile resources freed.
55 // - Tile marked for on-demand raster.
56 virtual void NotifyTileStateChanged(const Tile* tile) = 0;
58 // Given an empty raster tile priority queue, this will build a priority queue
59 // that will return tiles in order in which they should be rasterized.
60 // Note if the queue was previous built, Reset must be called on it.
61 virtual scoped_ptr<RasterTilePriorityQueue> BuildRasterQueue(
62 TreePriority tree_priority,
63 RasterTilePriorityQueue::Type type) = 0;
65 // Given an empty eviction tile priority queue, this will build a priority
66 // queue that will return tiles in order in which they should be evicted.
67 // Note if the queue was previous built, Reset must be called on it.
68 virtual scoped_ptr<EvictionTilePriorityQueue> BuildEvictionQueue(
69 TreePriority tree_priority) = 0;
71 // Informs the client that due to the currently rasterizing (or scheduled to
72 // be rasterized) tiles, we will be in a position that will likely require a
73 // draw. This can be used to preemptively start a frame.
74 virtual void SetIsLikelyToRequireADraw(bool is_likely_to_require_a_draw) = 0;
76 protected:
77 virtual ~TileManagerClient() {}
80 struct RasterTaskCompletionStats {
81 RasterTaskCompletionStats();
83 size_t completed_count;
84 size_t canceled_count;
86 scoped_refptr<base::trace_event::ConvertableToTraceFormat>
87 RasterTaskCompletionStatsAsValue(const RasterTaskCompletionStats& stats);
89 // This class manages tiles, deciding which should get rasterized and which
90 // should no longer have any memory assigned to them. Tile objects are "owned"
91 // by layers; they automatically register with the manager when they are
92 // created, and unregister from the manager when they are deleted.
93 class CC_EXPORT TileManager : public TileTaskRunnerClient {
94 public:
95 enum NamedTaskSet {
96 REQUIRED_FOR_ACTIVATION,
97 REQUIRED_FOR_DRAW,
98 // PixelBufferTileTaskWorkerPool depends on ALL being last.
99 ALL
100 // Adding additional values requires increasing kNumberOfTaskSets in
101 // tile_task_runner.h
104 static_assert(NamedTaskSet::ALL == (kNumberOfTaskSets - 1),
105 "NamedTaskSet::ALL should be equal to kNumberOfTaskSets"
106 "minus 1");
108 static scoped_ptr<TileManager> Create(TileManagerClient* client,
109 base::SequencedTaskRunner* task_runner,
110 size_t scheduled_raster_task_limit);
111 ~TileManager() override;
113 // Assigns tile memory and schedules work to prepare tiles for drawing.
114 // - Runs client_->NotifyReadyToActivate() when all tiles required for
115 // activation are prepared, or failed to prepare due to OOM.
116 // - Runs client_->NotifyReadyToDraw() when all tiles required draw are
117 // prepared, or failed to prepare due to OOM.
118 bool PrepareTiles(const GlobalStateThatImpactsTilePriority& state);
120 // Synchronously finish any in progress work, cancel the rest, and clean up as
121 // much resources as possible. Also, prevents any future work until a
122 // SetResources call.
123 void FinishTasksAndCleanUp();
125 // Set the new given resource pool and tile task runner. Note that
126 // FinishTasksAndCleanUp must be called in between consecutive calls to
127 // SetResources.
128 void SetResources(ResourcePool* resource_pool,
129 TileTaskRunner* tile_task_runner,
130 size_t scheduled_raster_task_limit);
132 // This causes any completed raster work to finalize, so that tiles get up to
133 // date draw information.
134 void Flush();
136 ScopedTilePtr CreateTile(const Tile::CreateInfo& info,
137 int layer_id,
138 int source_frame_number,
139 int flags);
141 bool IsReadyToActivate() const;
142 bool IsReadyToDraw() const;
144 scoped_refptr<base::trace_event::ConvertableToTraceFormat> BasicStateAsValue()
145 const;
146 void BasicStateAsValueInto(base::trace_event::TracedValue* dict) const;
147 const MemoryHistory::Entry& memory_stats_from_last_assign() const {
148 return memory_stats_from_last_assign_;
151 // Public methods for testing.
152 void InitializeTilesWithResourcesForTesting(const std::vector<Tile*>& tiles) {
153 for (size_t i = 0; i < tiles.size(); ++i) {
154 TileDrawInfo& draw_info = tiles[i]->draw_info();
155 draw_info.resource_ = resource_pool_->AcquireResource(
156 tiles[i]->desired_texture_size(),
157 tile_task_runner_->GetResourceFormat());
161 void ReleaseTileResourcesForTesting(const std::vector<Tile*>& tiles) {
162 for (size_t i = 0; i < tiles.size(); ++i) {
163 Tile* tile = tiles[i];
164 FreeResourcesForTile(tile);
168 void SetGlobalStateForTesting(
169 const GlobalStateThatImpactsTilePriority& state) {
170 global_state_ = state;
173 void SetTileTaskRunnerForTesting(TileTaskRunner* tile_task_runner);
175 void FreeResourcesAndCleanUpReleasedTilesForTesting() {
176 FreeResourcesForReleasedTiles();
177 CleanUpReleasedTiles();
180 std::vector<Tile*> AllTilesForTesting() const {
181 std::vector<Tile*> tiles;
182 for (TileMap::const_iterator it = tiles_.begin(); it != tiles_.end();
183 ++it) {
184 tiles.push_back(it->second);
186 return tiles;
189 void SetScheduledRasterTaskLimitForTesting(size_t limit) {
190 scheduled_raster_task_limit_ = limit;
193 void CheckIfMoreTilesNeedToBePreparedForTesting() {
194 CheckIfMoreTilesNeedToBePrepared();
197 void SetMoreTilesNeedToBeRasterizedForTesting() {
198 all_tiles_that_need_to_be_rasterized_are_scheduled_ = false;
201 bool HasScheduledTileTasksForTesting() const {
202 return has_scheduled_tile_tasks_;
205 protected:
206 TileManager(TileManagerClient* client,
207 const scoped_refptr<base::SequencedTaskRunner>& task_runner,
208 size_t scheduled_raster_task_limit);
210 void FreeResourcesForReleasedTiles();
211 void CleanUpReleasedTiles();
213 friend class Tile;
214 // Virtual for testing.
215 virtual void Release(Tile* tile);
216 Tile::Id GetUniqueTileId() { return ++next_tile_id_; }
218 // Overriden from TileTaskRunnerClient:
219 void DidFinishRunningTileTasks(TaskSet task_set) override;
221 typedef std::vector<PrioritizedTile> PrioritizedTileVector;
222 typedef std::set<Tile*> TileSet;
224 // Virtual for test
225 virtual void ScheduleTasks(
226 const PrioritizedTileVector& tiles_that_need_to_be_rasterized);
228 void AssignGpuMemoryToTiles(
229 RasterTilePriorityQueue* raster_priority_queue,
230 size_t scheduled_raser_task_limit,
231 PrioritizedTileVector* tiles_that_need_to_be_rasterized);
233 private:
234 class MemoryUsage {
235 public:
236 MemoryUsage();
237 MemoryUsage(size_t memory_bytes, size_t resource_count);
239 static MemoryUsage FromConfig(const gfx::Size& size, ResourceFormat format);
240 static MemoryUsage FromTile(const Tile* tile);
242 MemoryUsage& operator+=(const MemoryUsage& other);
243 MemoryUsage& operator-=(const MemoryUsage& other);
244 MemoryUsage operator-(const MemoryUsage& other);
246 bool Exceeds(const MemoryUsage& limit) const;
247 int64 memory_bytes() const { return memory_bytes_; }
249 private:
250 int64 memory_bytes_;
251 int resource_count_;
254 void OnRasterTaskCompleted(Tile::Id tile,
255 Resource* resource,
256 const RasterSource::SolidColorAnalysis& analysis,
257 bool was_canceled);
258 void UpdateTileDrawInfo(Tile* tile,
259 Resource* resource,
260 const RasterSource::SolidColorAnalysis& analysis);
262 void FreeResourcesForTile(Tile* tile);
263 void FreeResourcesForTileAndNotifyClientIfTileWasReadyToDraw(Tile* tile);
264 scoped_refptr<RasterTask> CreateRasterTask(
265 const PrioritizedTile& prioritized_tile);
267 scoped_ptr<EvictionTilePriorityQueue>
268 FreeTileResourcesUntilUsageIsWithinLimit(
269 scoped_ptr<EvictionTilePriorityQueue> eviction_priority_queue,
270 const MemoryUsage& limit,
271 MemoryUsage* usage);
272 scoped_ptr<EvictionTilePriorityQueue>
273 FreeTileResourcesWithLowerPriorityUntilUsageIsWithinLimit(
274 scoped_ptr<EvictionTilePriorityQueue> eviction_priority_queue,
275 const MemoryUsage& limit,
276 const TilePriority& oother_priority,
277 MemoryUsage* usage);
278 bool TilePriorityViolatesMemoryPolicy(const TilePriority& priority);
279 bool AreRequiredTilesReadyToDraw(RasterTilePriorityQueue::Type type) const;
280 void CheckIfMoreTilesNeedToBePrepared();
281 void CheckAndIssueSignals();
283 TileManagerClient* client_;
284 scoped_refptr<base::SequencedTaskRunner> task_runner_;
285 ResourcePool* resource_pool_;
286 TileTaskRunner* tile_task_runner_;
287 GlobalStateThatImpactsTilePriority global_state_;
288 size_t scheduled_raster_task_limit_;
290 typedef base::hash_map<Tile::Id, Tile*> TileMap;
291 TileMap tiles_;
293 bool all_tiles_that_need_to_be_rasterized_are_scheduled_;
294 MemoryHistory::Entry memory_stats_from_last_assign_;
296 bool did_check_for_completed_tasks_since_last_schedule_tasks_;
297 bool did_oom_on_last_assign_;
299 ImageDecodeController image_decode_controller_;
301 RasterTaskCompletionStats flush_stats_;
303 std::vector<Tile*> released_tiles_;
305 // Queue used when scheduling raster tasks.
306 TileTaskQueue raster_queue_;
308 std::vector<scoped_refptr<RasterTask>> orphan_raster_tasks_;
310 UniqueNotifier more_tiles_need_prepare_check_notifier_;
312 struct Signals {
313 Signals();
315 void reset();
317 bool ready_to_activate;
318 bool did_notify_ready_to_activate;
319 bool ready_to_draw;
320 bool did_notify_ready_to_draw;
321 bool all_tile_tasks_completed;
322 bool did_notify_all_tile_tasks_completed;
323 } signals_;
325 UniqueNotifier signals_check_notifier_;
327 bool has_scheduled_tile_tasks_;
329 uint64_t prepare_tiles_count_;
330 uint64_t next_tile_id_;
332 DISALLOW_COPY_AND_ASSIGN(TileManager);
335 } // namespace cc
337 #endif // CC_TILES_TILE_MANAGER_H_