Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / cc / resources / tile_manager.h
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/ref_counted_managed.h"
18 #include "cc/base/unique_notifier.h"
19 #include "cc/debug/rendering_stats_instrumentation.h"
20 #include "cc/resources/eviction_tile_priority_queue.h"
21 #include "cc/resources/managed_tile_state.h"
22 #include "cc/resources/memory_history.h"
23 #include "cc/resources/raster_source.h"
24 #include "cc/resources/raster_tile_priority_queue.h"
25 #include "cc/resources/rasterizer.h"
26 #include "cc/resources/resource_pool.h"
27 #include "cc/resources/tile.h"
28
29 namespace base {
30 namespace debug {
31 class ConvertableToTraceFormat;
32 class TracedValue;
33 }
34 }
35
36 namespace cc {
37 class PictureLayerImpl;
38 class ResourceProvider;
39
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;
46
47   // Called when all tiles marked as required for activation are ready to draw.
48   virtual void NotifyReadyToActivate() = 0;
49
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;
56
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 void BuildRasterQueue(RasterTilePriorityQueue* queue,
61                                 TreePriority tree_priority) = 0;
62
63   // Given an empty eviction tile priority queue, this will build a priority
64   // queue that will return tiles in order in which they should be evicted.
65   // Note if the queue was previous built, Reset must be called on it.
66   virtual void BuildEvictionQueue(EvictionTilePriorityQueue* queue,
67                                   TreePriority tree_priority) = 0;
68
69  protected:
70   virtual ~TileManagerClient() {}
71 };
72
73 struct RasterTaskCompletionStats {
74   RasterTaskCompletionStats();
75
76   size_t completed_count;
77   size_t canceled_count;
78 };
79 scoped_refptr<base::debug::ConvertableToTraceFormat>
80     RasterTaskCompletionStatsAsValue(const RasterTaskCompletionStats& stats);
81
82 // This class manages tiles, deciding which should get rasterized and which
83 // should no longer have any memory assigned to them. Tile objects are "owned"
84 // by layers; they automatically register with the manager when they are
85 // created, and unregister from the manager when they are deleted.
86 class CC_EXPORT TileManager : public RasterizerClient,
87                               public RefCountedManager<Tile> {
88  public:
89   enum NamedTaskSet {
90     REQUIRED_FOR_ACTIVATION = 0,
91     ALL = 1,
92     // Adding additional values requires increasing kNumberOfTaskSets in
93     // rasterizer.h
94   };
95
96   static scoped_ptr<TileManager> Create(
97       TileManagerClient* client,
98       base::SequencedTaskRunner* task_runner,
99       ResourcePool* resource_pool,
100       Rasterizer* rasterizer,
101       RenderingStatsInstrumentation* rendering_stats_instrumentation,
102       size_t scheduled_raster_task_limit);
103   ~TileManager() override;
104
105   void ManageTiles(const GlobalStateThatImpactsTilePriority& state);
106
107   // Returns true when visible tiles have been initialized.
108   bool UpdateVisibleTiles();
109
110   scoped_refptr<Tile> CreateTile(RasterSource* raster_source,
111                                  const gfx::Size& tile_size,
112                                  const gfx::Rect& content_rect,
113                                  float contents_scale,
114                                  int layer_id,
115                                  int source_frame_number,
116                                  int flags);
117
118   scoped_refptr<base::debug::ConvertableToTraceFormat> BasicStateAsValue()
119       const;
120   void BasicStateAsValueInto(base::debug::TracedValue* dict) const;
121   const MemoryHistory::Entry& memory_stats_from_last_assign() const {
122     return memory_stats_from_last_assign_;
123   }
124
125   void InitializeTilesWithResourcesForTesting(const std::vector<Tile*>& tiles) {
126     for (size_t i = 0; i < tiles.size(); ++i) {
127       ManagedTileState& mts = tiles[i]->managed_state();
128
129       mts.draw_info.resource_ =
130           resource_pool_->AcquireResource(tiles[i]->size());
131     }
132   }
133
134   void ReleaseTileResourcesForTesting(const std::vector<Tile*>& tiles) {
135     for (size_t i = 0; i < tiles.size(); ++i) {
136       Tile* tile = tiles[i];
137       FreeResourcesForTile(tile);
138     }
139   }
140
141   void SetGlobalStateForTesting(
142       const GlobalStateThatImpactsTilePriority& state) {
143     global_state_ = state;
144   }
145
146   void SetRasterizerForTesting(Rasterizer* rasterizer);
147
148   void FreeResourcesAndCleanUpReleasedTilesForTesting() {
149     FreeResourcesForReleasedTiles();
150     CleanUpReleasedTiles();
151   }
152
153   std::vector<Tile*> AllTilesForTesting() const {
154     std::vector<Tile*> tiles;
155     for (TileMap::const_iterator it = tiles_.begin(); it != tiles_.end();
156          ++it) {
157       tiles.push_back(it->second);
158     }
159     return tiles;
160   }
161
162  protected:
163   TileManager(TileManagerClient* client,
164               const scoped_refptr<base::SequencedTaskRunner>& task_runner,
165               ResourcePool* resource_pool,
166               Rasterizer* rasterizer,
167               RenderingStatsInstrumentation* rendering_stats_instrumentation,
168               size_t scheduled_raster_task_limit);
169
170   void FreeResourcesForReleasedTiles();
171   void CleanUpReleasedTiles();
172
173   // Overriden from RefCountedManager<Tile>:
174   friend class Tile;
175   void Release(Tile* tile) override;
176
177   // Overriden from RasterizerClient:
178   void DidFinishRunningTasks(TaskSet task_set) override;
179   TaskSetCollection TasksThatShouldBeForcedToComplete() const override;
180
181   typedef std::vector<Tile*> TileVector;
182   typedef std::set<Tile*> TileSet;
183
184   // Virtual for test
185   virtual void ScheduleTasks(
186       const TileVector& tiles_that_need_to_be_rasterized);
187
188   void AssignGpuMemoryToTiles(TileVector* tiles_that_need_to_be_rasterized);
189
190  private:
191   class MemoryUsage {
192    public:
193     MemoryUsage();
194     MemoryUsage(int64 memory_bytes, int resource_count);
195
196     static MemoryUsage FromConfig(const gfx::Size& size, ResourceFormat format);
197     static MemoryUsage FromTile(const Tile* tile);
198
199     MemoryUsage& operator+=(const MemoryUsage& other);
200     MemoryUsage& operator-=(const MemoryUsage& other);
201     MemoryUsage operator-(const MemoryUsage& other);
202
203     bool Exceeds(const MemoryUsage& limit) const;
204     int64 memory_bytes() const { return memory_bytes_; }
205
206    private:
207     int64 memory_bytes_;
208     int resource_count_;
209   };
210
211   void OnImageDecodeTaskCompleted(int layer_id,
212                                   SkPixelRef* pixel_ref,
213                                   bool was_canceled);
214   void OnRasterTaskCompleted(Tile::Id tile,
215                              scoped_ptr<ScopedResource> resource,
216                              const RasterSource::SolidColorAnalysis& analysis,
217                              bool was_canceled);
218
219   void FreeResourcesForTile(Tile* tile);
220   void FreeResourcesForTileAndNotifyClientIfTileWasReadyToDraw(Tile* tile);
221   scoped_refptr<ImageDecodeTask> CreateImageDecodeTask(Tile* tile,
222                                                        SkPixelRef* pixel_ref);
223   scoped_refptr<RasterTask> CreateRasterTask(Tile* tile);
224
225   void RebuildEvictionQueueIfNeeded();
226   bool FreeTileResourcesUntilUsageIsWithinLimit(const MemoryUsage& limit,
227                                                 MemoryUsage* usage);
228   bool FreeTileResourcesWithLowerPriorityUntilUsageIsWithinLimit(
229       const MemoryUsage& limit,
230       const TilePriority& oother_priority,
231       MemoryUsage* usage);
232   bool TilePriorityViolatesMemoryPolicy(const TilePriority& priority);
233   bool IsReadyToActivate() const;
234   void CheckIfReadyToActivate();
235
236   TileManagerClient* client_;
237   scoped_refptr<base::SequencedTaskRunner> task_runner_;
238   ResourcePool* resource_pool_;
239   Rasterizer* rasterizer_;
240   GlobalStateThatImpactsTilePriority global_state_;
241   const size_t scheduled_raster_task_limit_;
242
243   typedef base::hash_map<Tile::Id, Tile*> TileMap;
244   TileMap tiles_;
245
246   bool all_tiles_that_need_to_be_rasterized_are_scheduled_;
247   MemoryHistory::Entry memory_stats_from_last_assign_;
248
249   RenderingStatsInstrumentation* rendering_stats_instrumentation_;
250
251   bool did_initialize_visible_tile_;
252   bool did_check_for_completed_tasks_since_last_schedule_tasks_;
253   bool did_oom_on_last_assign_;
254
255   typedef base::hash_map<uint32_t, scoped_refptr<ImageDecodeTask>>
256       PixelRefTaskMap;
257   typedef base::hash_map<int, PixelRefTaskMap> LayerPixelRefTaskMap;
258   LayerPixelRefTaskMap image_decode_tasks_;
259
260   typedef base::hash_map<int, int> LayerCountMap;
261   LayerCountMap used_layer_counts_;
262
263   RasterTaskCompletionStats update_visible_tiles_stats_;
264
265   std::vector<Tile*> released_tiles_;
266
267   ResourceFormat resource_format_;
268
269   // Queue used when scheduling raster tasks.
270   RasterTaskQueue raster_queue_;
271
272   std::vector<scoped_refptr<RasterTask>> orphan_raster_tasks_;
273
274   UniqueNotifier ready_to_activate_check_notifier_;
275
276   RasterTilePriorityQueue raster_priority_queue_;
277   EvictionTilePriorityQueue eviction_priority_queue_;
278   bool eviction_priority_queue_is_up_to_date_;
279
280   DISALLOW_COPY_AND_ASSIGN(TileManager);
281 };
282
283 }  // namespace cc
284
285 #endif  // CC_RESOURCES_TILE_MANAGER_H_