[Android] Add a device-side test HTTP server via ChromeInstrumentationTestRunner.
[chromium-blink-merge.git] / cc / resources / tile_manager.h
blob8b844010d7b3fd6732583cea553ee780a6720569
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_
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/ref_counted_managed.h"
18 #include "cc/base/unique_notifier.h"
19 #include "cc/resources/eviction_tile_priority_queue.h"
20 #include "cc/resources/memory_history.h"
21 #include "cc/resources/raster_source.h"
22 #include "cc/resources/raster_tile_priority_queue.h"
23 #include "cc/resources/resource_pool.h"
24 #include "cc/resources/tile.h"
25 #include "cc/resources/tile_draw_info.h"
26 #include "cc/resources/tile_task_runner.h"
28 namespace base {
29 namespace debug {
30 class ConvertableToTraceFormat;
31 class TracedValue;
35 namespace cc {
36 class PictureLayerImpl;
37 class Rasterizer;
38 class ResourceProvider;
40 class CC_EXPORT TileManagerClient {
41 public:
42 // Returns the set of layers that the tile manager should consider for raster.
43 // TODO(vmpstr): Change the way we determine if we are ready to activate, so
44 // that this can be removed.
45 virtual const std::vector<PictureLayerImpl*>& GetPictureLayers() const = 0;
47 // Called when all tiles marked as required for activation are ready to draw.
48 virtual void NotifyReadyToActivate() = 0;
50 // Called when all tiles marked as required for draw are ready to draw.
51 virtual void NotifyReadyToDraw() = 0;
53 // Called when the visible representation of a tile might have changed. Some
54 // examples are:
55 // - Tile version initialized.
56 // - Tile resources freed.
57 // - Tile marked for on-demand raster.
58 virtual void NotifyTileStateChanged(const Tile* tile) = 0;
60 // Given an empty raster tile priority queue, this will build a priority queue
61 // that will return tiles in order in which they should be rasterized.
62 // Note if the queue was previous built, Reset must be called on it.
63 virtual void BuildRasterQueue(RasterTilePriorityQueue* queue,
64 TreePriority tree_priority) = 0;
66 // Given an empty eviction tile priority queue, this will build a priority
67 // queue that will return tiles in order in which they should be evicted.
68 // Note if the queue was previous built, Reset must be called on it.
69 virtual void BuildEvictionQueue(EvictionTilePriorityQueue* queue,
70 TreePriority tree_priority) = 0;
72 protected:
73 virtual ~TileManagerClient() {}
76 struct RasterTaskCompletionStats {
77 RasterTaskCompletionStats();
79 size_t completed_count;
80 size_t canceled_count;
82 scoped_refptr<base::debug::ConvertableToTraceFormat>
83 RasterTaskCompletionStatsAsValue(const RasterTaskCompletionStats& stats);
85 // This class manages tiles, deciding which should get rasterized and which
86 // should no longer have any memory assigned to them. Tile objects are "owned"
87 // by layers; they automatically register with the manager when they are
88 // created, and unregister from the manager when they are deleted.
89 class CC_EXPORT TileManager : public TileTaskRunnerClient,
90 public RefCountedManager<Tile> {
91 public:
92 enum NamedTaskSet {
93 ALL,
94 REQUIRED_FOR_ACTIVATION,
95 REQUIRED_FOR_DRAW
96 // Adding additional values requires increasing kNumberOfTaskSets in
97 // rasterizer.h
100 static scoped_ptr<TileManager> Create(TileManagerClient* client,
101 base::SequencedTaskRunner* task_runner,
102 ResourcePool* resource_pool,
103 TileTaskRunner* tile_task_runner,
104 Rasterizer* rasterizer,
105 size_t scheduled_raster_task_limit);
106 ~TileManager() override;
108 // Assigns tile memory and schedules work to prepare tiles for drawing.
109 // - Runs client_->NotifyReadyToActivate() when all tiles required for
110 // activation are prepared, or failed to prepare due to OOM.
111 // - Runs client_->NotifyReadyToDraw() when all tiles required draw are
112 // prepared, or failed to prepare due to OOM.
113 void PrepareTiles(const GlobalStateThatImpactsTilePriority& state);
115 void UpdateVisibleTiles(const GlobalStateThatImpactsTilePriority& state);
117 scoped_refptr<Tile> CreateTile(RasterSource* raster_source,
118 const gfx::Size& desired_texture_size,
119 const gfx::Rect& content_rect,
120 float contents_scale,
121 int layer_id,
122 int source_frame_number,
123 int flags);
125 scoped_refptr<base::debug::ConvertableToTraceFormat> BasicStateAsValue()
126 const;
127 void BasicStateAsValueInto(base::debug::TracedValue* dict) const;
128 const MemoryHistory::Entry& memory_stats_from_last_assign() const {
129 return memory_stats_from_last_assign_;
132 void InitializeTilesWithResourcesForTesting(const std::vector<Tile*>& tiles) {
133 for (size_t i = 0; i < tiles.size(); ++i) {
134 TileDrawInfo& draw_info = tiles[i]->draw_info();
135 draw_info.resource_ =
136 resource_pool_->AcquireResource(tiles[i]->desired_texture_size());
140 void ReleaseTileResourcesForTesting(const std::vector<Tile*>& tiles) {
141 for (size_t i = 0; i < tiles.size(); ++i) {
142 Tile* tile = tiles[i];
143 FreeResourcesForTile(tile);
147 void SetGlobalStateForTesting(
148 const GlobalStateThatImpactsTilePriority& state) {
149 global_state_ = state;
152 void SetTileTaskRunnerForTesting(TileTaskRunner* tile_task_runner);
154 void FreeResourcesAndCleanUpReleasedTilesForTesting() {
155 FreeResourcesForReleasedTiles();
156 CleanUpReleasedTiles();
159 std::vector<Tile*> AllTilesForTesting() const {
160 std::vector<Tile*> tiles;
161 for (TileMap::const_iterator it = tiles_.begin(); it != tiles_.end();
162 ++it) {
163 tiles.push_back(it->second);
165 return tiles;
168 void SetScheduledRasterTaskLimitForTesting(size_t limit) {
169 scheduled_raster_task_limit_ = limit;
172 protected:
173 TileManager(TileManagerClient* client,
174 const scoped_refptr<base::SequencedTaskRunner>& task_runner,
175 ResourcePool* resource_pool,
176 TileTaskRunner* tile_task_runner,
177 Rasterizer* rasterizer,
178 size_t scheduled_raster_task_limit);
180 void FreeResourcesForReleasedTiles();
181 void CleanUpReleasedTiles();
183 // Overriden from RefCountedManager<Tile>:
184 friend class Tile;
185 void Release(Tile* tile) override;
187 // Overriden from TileTaskRunnerClient:
188 void DidFinishRunningTileTasks(TaskSet task_set) override;
189 TaskSetCollection TasksThatShouldBeForcedToComplete() const override;
191 typedef std::vector<Tile*> TileVector;
192 typedef std::set<Tile*> TileSet;
194 // Virtual for test
195 virtual void ScheduleTasks(
196 const TileVector& tiles_that_need_to_be_rasterized);
198 void AssignGpuMemoryToTiles(TileVector* tiles_that_need_to_be_rasterized,
199 size_t scheduled_raser_task_limit);
201 void SynchronouslyRasterizeTiles(
202 const GlobalStateThatImpactsTilePriority& state);
204 private:
205 class MemoryUsage {
206 public:
207 MemoryUsage();
208 MemoryUsage(int64 memory_bytes, int resource_count);
210 static MemoryUsage FromConfig(const gfx::Size& size, ResourceFormat format);
211 static MemoryUsage FromTile(const Tile* tile);
213 MemoryUsage& operator+=(const MemoryUsage& other);
214 MemoryUsage& operator-=(const MemoryUsage& other);
215 MemoryUsage operator-(const MemoryUsage& other);
217 bool Exceeds(const MemoryUsage& limit) const;
218 int64 memory_bytes() const { return memory_bytes_; }
220 private:
221 int64 memory_bytes_;
222 int resource_count_;
225 void OnImageDecodeTaskCompleted(int layer_id,
226 SkPixelRef* pixel_ref,
227 bool was_canceled);
228 void OnRasterTaskCompleted(Tile::Id tile,
229 scoped_ptr<ScopedResource> resource,
230 const RasterSource::SolidColorAnalysis& analysis,
231 bool was_canceled);
232 void UpdateTileDrawInfo(Tile* tile,
233 scoped_ptr<ScopedResource> resource,
234 const RasterSource::SolidColorAnalysis& analysis);
236 void FreeResourcesForTile(Tile* tile);
237 void FreeResourcesForTileAndNotifyClientIfTileWasReadyToDraw(Tile* tile);
238 scoped_refptr<ImageDecodeTask> CreateImageDecodeTask(Tile* tile,
239 SkPixelRef* pixel_ref);
240 scoped_refptr<RasterTask> CreateRasterTask(Tile* tile);
242 void RebuildEvictionQueueIfNeeded();
243 bool FreeTileResourcesUntilUsageIsWithinLimit(const MemoryUsage& limit,
244 MemoryUsage* usage);
245 bool FreeTileResourcesWithLowerPriorityUntilUsageIsWithinLimit(
246 const MemoryUsage& limit,
247 const TilePriority& oother_priority,
248 MemoryUsage* usage);
249 bool TilePriorityViolatesMemoryPolicy(const TilePriority& priority);
250 bool IsReadyToActivate() const;
251 bool IsReadyToDraw() const;
252 void NotifyReadyToActivate();
253 void NotifyReadyToDraw();
254 void CheckIfReadyToActivate();
255 void CheckIfReadyToDraw();
256 void CheckIfMoreTilesNeedToBePrepared();
258 TileManagerClient* client_;
259 scoped_refptr<base::SequencedTaskRunner> task_runner_;
260 ResourcePool* resource_pool_;
261 TileTaskRunner* tile_task_runner_;
262 Rasterizer* rasterizer_;
263 GlobalStateThatImpactsTilePriority global_state_;
264 size_t scheduled_raster_task_limit_;
266 typedef base::hash_map<Tile::Id, Tile*> TileMap;
267 TileMap tiles_;
269 bool all_tiles_that_need_to_be_rasterized_are_scheduled_;
270 MemoryHistory::Entry memory_stats_from_last_assign_;
272 bool did_check_for_completed_tasks_since_last_schedule_tasks_;
273 bool did_oom_on_last_assign_;
275 typedef base::hash_map<uint32_t, scoped_refptr<ImageDecodeTask>>
276 PixelRefTaskMap;
277 typedef base::hash_map<int, PixelRefTaskMap> LayerPixelRefTaskMap;
278 LayerPixelRefTaskMap image_decode_tasks_;
280 typedef base::hash_map<int, int> LayerCountMap;
281 LayerCountMap used_layer_counts_;
283 RasterTaskCompletionStats update_visible_tiles_stats_;
285 std::vector<Tile*> released_tiles_;
287 ResourceFormat resource_format_;
289 // Queue used when scheduling raster tasks.
290 TileTaskQueue raster_queue_;
292 std::vector<scoped_refptr<RasterTask>> orphan_raster_tasks_;
294 UniqueNotifier ready_to_activate_notifier_;
295 UniqueNotifier ready_to_draw_notifier_;
296 UniqueNotifier ready_to_activate_check_notifier_;
297 UniqueNotifier ready_to_draw_check_notifier_;
298 UniqueNotifier more_tiles_need_prepare_check_notifier_;
300 RasterTilePriorityQueue raster_priority_queue_;
301 EvictionTilePriorityQueue eviction_priority_queue_;
302 bool eviction_priority_queue_is_up_to_date_;
304 bool did_notify_ready_to_activate_;
305 bool did_notify_ready_to_draw_;
307 DISALLOW_COPY_AND_ASSIGN(TileManager);
310 } // namespace cc
312 #endif // CC_RESOURCES_TILE_MANAGER_H_