service_resolver_64: Correctly check all the bytes of the service code.
[chromium-blink-merge.git] / cc / tiles / tile_manager.h
blob56ec94b58b3114e6460ee94b32ffe331a4192535
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/raster_tile_priority_queue.h"
24 #include "cc/tiles/tile.h"
25 #include "cc/tiles/tile_draw_info.h"
27 namespace base {
28 namespace trace_event {
29 class ConvertableToTraceFormat;
30 class TracedValue;
34 namespace cc {
35 class PictureLayerImpl;
36 class ResourceProvider;
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;
43 // Called when all tiles marked as required for draw are ready to draw.
44 virtual void NotifyReadyToDraw() = 0;
46 // Called when all tile tasks started by the most recent call to PrepareTiles
47 // are completed.
48 virtual void NotifyAllTileTasksCompleted() = 0;
50 // Called when the visible representation of a tile might have changed. Some
51 // examples are:
52 // - Tile version initialized.
53 // - Tile resources freed.
54 // - Tile marked for on-demand raster.
55 virtual void NotifyTileStateChanged(const Tile* tile) = 0;
57 // Given an empty raster tile priority queue, this will build a priority queue
58 // that will return tiles in order in which they should be rasterized.
59 // Note if the queue was previous built, Reset must be called on it.
60 virtual scoped_ptr<RasterTilePriorityQueue> BuildRasterQueue(
61 TreePriority tree_priority,
62 RasterTilePriorityQueue::Type type) = 0;
64 // Given an empty eviction tile priority queue, this will build a priority
65 // queue that will return tiles in order in which they should be evicted.
66 // Note if the queue was previous built, Reset must be called on it.
67 virtual scoped_ptr<EvictionTilePriorityQueue> BuildEvictionQueue(
68 TreePriority tree_priority) = 0;
70 // Informs the client that due to the currently rasterizing (or scheduled to
71 // be rasterized) tiles, we will be in a position that will likely require a
72 // draw. This can be used to preemptively start a frame.
73 virtual void SetIsLikelyToRequireADraw(bool is_likely_to_require_a_draw) = 0;
75 protected:
76 virtual ~TileManagerClient() {}
79 struct RasterTaskCompletionStats {
80 RasterTaskCompletionStats();
82 size_t completed_count;
83 size_t canceled_count;
85 scoped_refptr<base::trace_event::ConvertableToTraceFormat>
86 RasterTaskCompletionStatsAsValue(const RasterTaskCompletionStats& stats);
88 // This class manages tiles, deciding which should get rasterized and which
89 // should no longer have any memory assigned to them. Tile objects are "owned"
90 // by layers; they automatically register with the manager when they are
91 // created, and unregister from the manager when they are deleted.
92 class CC_EXPORT TileManager : public TileTaskRunnerClient {
93 public:
94 enum NamedTaskSet {
95 REQUIRED_FOR_ACTIVATION,
96 REQUIRED_FOR_DRAW,
97 // PixelBufferTileTaskWorkerPool depends on ALL being last.
98 ALL
99 // Adding additional values requires increasing kNumberOfTaskSets in
100 // tile_task_runner.h
103 static_assert(NamedTaskSet::ALL == (kNumberOfTaskSets - 1),
104 "NamedTaskSet::ALL should be equal to kNumberOfTaskSets"
105 "minus 1");
107 static scoped_ptr<TileManager> Create(TileManagerClient* client,
108 base::SequencedTaskRunner* task_runner,
109 ResourcePool* resource_pool,
110 TileTaskRunner* tile_task_runner,
111 size_t scheduled_raster_task_limit);
112 ~TileManager() override;
114 // Assigns tile memory and schedules work to prepare tiles for drawing.
115 // - Runs client_->NotifyReadyToActivate() when all tiles required for
116 // activation are prepared, or failed to prepare due to OOM.
117 // - Runs client_->NotifyReadyToDraw() when all tiles required draw are
118 // prepared, or failed to prepare due to OOM.
119 void PrepareTiles(const GlobalStateThatImpactsTilePriority& state);
121 // This causes any completed raster work to finalize, so that tiles get up to
122 // date draw information.
123 void Flush();
125 ScopedTilePtr CreateTile(const gfx::Size& desired_texture_size,
126 const gfx::Rect& content_rect,
127 float contents_scale,
128 int layer_id,
129 int source_frame_number,
130 int flags);
132 bool IsReadyToActivate() const;
133 bool IsReadyToDraw() const;
135 scoped_refptr<base::trace_event::ConvertableToTraceFormat> BasicStateAsValue()
136 const;
137 void BasicStateAsValueInto(base::trace_event::TracedValue* dict) const;
138 const MemoryHistory::Entry& memory_stats_from_last_assign() const {
139 return memory_stats_from_last_assign_;
142 // Public methods for testing.
143 void InitializeTilesWithResourcesForTesting(const std::vector<Tile*>& tiles) {
144 for (size_t i = 0; i < tiles.size(); ++i) {
145 TileDrawInfo& draw_info = tiles[i]->draw_info();
146 draw_info.resource_ = resource_pool_->AcquireResource(
147 tiles[i]->desired_texture_size(),
148 tile_task_runner_->GetResourceFormat());
152 void ReleaseTileResourcesForTesting(const std::vector<Tile*>& tiles) {
153 for (size_t i = 0; i < tiles.size(); ++i) {
154 Tile* tile = tiles[i];
155 FreeResourcesForTile(tile);
159 void SetGlobalStateForTesting(
160 const GlobalStateThatImpactsTilePriority& state) {
161 global_state_ = state;
164 void SetTileTaskRunnerForTesting(TileTaskRunner* tile_task_runner);
166 void FreeResourcesAndCleanUpReleasedTilesForTesting() {
167 FreeResourcesForReleasedTiles();
168 CleanUpReleasedTiles();
171 std::vector<Tile*> AllTilesForTesting() const {
172 std::vector<Tile*> tiles;
173 for (TileMap::const_iterator it = tiles_.begin(); it != tiles_.end();
174 ++it) {
175 tiles.push_back(it->second);
177 return tiles;
180 void SetScheduledRasterTaskLimitForTesting(size_t limit) {
181 scheduled_raster_task_limit_ = limit;
184 void CheckIfMoreTilesNeedToBePreparedForTesting() {
185 CheckIfMoreTilesNeedToBePrepared();
188 void SetMoreTilesNeedToBeRasterizedForTesting() {
189 all_tiles_that_need_to_be_rasterized_are_scheduled_ = false;
192 bool HasScheduledTileTasksForTesting() const {
193 return has_scheduled_tile_tasks_;
196 protected:
197 TileManager(TileManagerClient* client,
198 const scoped_refptr<base::SequencedTaskRunner>& task_runner,
199 ResourcePool* resource_pool,
200 TileTaskRunner* tile_task_runner,
201 size_t scheduled_raster_task_limit);
203 void FreeResourcesForReleasedTiles();
204 void CleanUpReleasedTiles();
206 friend class Tile;
207 // Virtual for testing.
208 virtual void Release(Tile* tile);
210 // Overriden from TileTaskRunnerClient:
211 void DidFinishRunningTileTasks(TaskSet task_set) override;
212 TaskSetCollection TasksThatShouldBeForcedToComplete() const override;
214 typedef std::vector<PrioritizedTile> PrioritizedTileVector;
215 typedef std::set<Tile*> TileSet;
217 // Virtual for test
218 virtual void ScheduleTasks(
219 const PrioritizedTileVector& tiles_that_need_to_be_rasterized);
221 void AssignGpuMemoryToTiles(
222 RasterTilePriorityQueue* raster_priority_queue,
223 size_t scheduled_raser_task_limit,
224 PrioritizedTileVector* tiles_that_need_to_be_rasterized);
226 private:
227 class MemoryUsage {
228 public:
229 MemoryUsage();
230 MemoryUsage(size_t memory_bytes, size_t resource_count);
232 static MemoryUsage FromConfig(const gfx::Size& size, ResourceFormat format);
233 static MemoryUsage FromTile(const Tile* tile);
235 MemoryUsage& operator+=(const MemoryUsage& other);
236 MemoryUsage& operator-=(const MemoryUsage& other);
237 MemoryUsage operator-(const MemoryUsage& other);
239 bool Exceeds(const MemoryUsage& limit) const;
240 int64 memory_bytes() const { return memory_bytes_; }
242 private:
243 int64 memory_bytes_;
244 int resource_count_;
247 void OnImageDecodeTaskCompleted(int layer_id,
248 SkPixelRef* pixel_ref,
249 bool was_canceled);
250 void OnRasterTaskCompleted(Tile::Id tile,
251 scoped_ptr<ScopedResource> resource,
252 const RasterSource::SolidColorAnalysis& analysis,
253 bool was_canceled);
254 void UpdateTileDrawInfo(Tile* tile,
255 scoped_ptr<ScopedResource> resource,
256 const RasterSource::SolidColorAnalysis& analysis);
258 void FreeResourcesForTile(Tile* tile);
259 void FreeResourcesForTileAndNotifyClientIfTileWasReadyToDraw(Tile* tile);
260 scoped_refptr<ImageDecodeTask> CreateImageDecodeTask(Tile* tile,
261 SkPixelRef* pixel_ref);
262 scoped_refptr<RasterTask> CreateRasterTask(
263 const PrioritizedTile& prioritized_tile);
265 scoped_ptr<EvictionTilePriorityQueue>
266 FreeTileResourcesUntilUsageIsWithinLimit(
267 scoped_ptr<EvictionTilePriorityQueue> eviction_priority_queue,
268 const MemoryUsage& limit,
269 MemoryUsage* usage);
270 scoped_ptr<EvictionTilePriorityQueue>
271 FreeTileResourcesWithLowerPriorityUntilUsageIsWithinLimit(
272 scoped_ptr<EvictionTilePriorityQueue> eviction_priority_queue,
273 const MemoryUsage& limit,
274 const TilePriority& oother_priority,
275 MemoryUsage* usage);
276 bool TilePriorityViolatesMemoryPolicy(const TilePriority& priority);
277 bool AreRequiredTilesReadyToDraw(RasterTilePriorityQueue::Type type) const;
278 void CheckIfMoreTilesNeedToBePrepared();
279 void CheckAndIssueSignals();
281 TileManagerClient* client_;
282 scoped_refptr<base::SequencedTaskRunner> task_runner_;
283 ResourcePool* resource_pool_;
284 TileTaskRunner* tile_task_runner_;
285 GlobalStateThatImpactsTilePriority global_state_;
286 size_t scheduled_raster_task_limit_;
288 typedef base::hash_map<Tile::Id, Tile*> TileMap;
289 TileMap tiles_;
291 bool all_tiles_that_need_to_be_rasterized_are_scheduled_;
292 MemoryHistory::Entry memory_stats_from_last_assign_;
294 bool did_check_for_completed_tasks_since_last_schedule_tasks_;
295 bool did_oom_on_last_assign_;
297 typedef base::hash_map<uint32_t, scoped_refptr<ImageDecodeTask>>
298 PixelRefTaskMap;
299 typedef base::hash_map<int, PixelRefTaskMap> LayerPixelRefTaskMap;
300 LayerPixelRefTaskMap image_decode_tasks_;
302 typedef base::hash_map<int, int> LayerCountMap;
303 LayerCountMap used_layer_counts_;
305 RasterTaskCompletionStats flush_stats_;
307 std::vector<Tile*> released_tiles_;
309 // Queue used when scheduling raster tasks.
310 TileTaskQueue raster_queue_;
312 std::vector<scoped_refptr<RasterTask>> orphan_raster_tasks_;
314 UniqueNotifier more_tiles_need_prepare_check_notifier_;
316 struct Signals {
317 Signals();
319 void reset();
321 bool ready_to_activate;
322 bool did_notify_ready_to_activate;
323 bool ready_to_draw;
324 bool did_notify_ready_to_draw;
325 bool all_tile_tasks_completed;
326 bool did_notify_all_tile_tasks_completed;
327 } signals_;
329 UniqueNotifier signals_check_notifier_;
331 bool has_scheduled_tile_tasks_;
333 uint64_t prepare_tiles_count_;
335 DISALLOW_COPY_AND_ASSIGN(TileManager);
338 } // namespace cc
340 #endif // CC_TILES_TILE_MANAGER_H_