a4eddb9becc5e51f0aa22687499111f45c5560e3
[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 <queue>
9 #include <set>
10 #include <vector>
11
12 #include "base/containers/hash_tables.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/values.h"
15 #include "cc/base/ref_counted_managed.h"
16 #include "cc/debug/rendering_stats_instrumentation.h"
17 #include "cc/resources/managed_tile_state.h"
18 #include "cc/resources/memory_history.h"
19 #include "cc/resources/picture_pile_impl.h"
20 #include "cc/resources/prioritized_tile_set.h"
21 #include "cc/resources/raster_worker_pool.h"
22 #include "cc/resources/resource_pool.h"
23 #include "cc/resources/tile.h"
24
25 namespace cc {
26 class PictureLayerImpl;
27 class RasterWorkerPoolDelegate;
28 class ResourceProvider;
29
30 class CC_EXPORT TileManagerClient {
31  public:
32   virtual void NotifyReadyToActivate() = 0;
33
34  protected:
35   virtual ~TileManagerClient() {}
36 };
37
38 struct RasterTaskCompletionStats {
39   RasterTaskCompletionStats();
40
41   size_t completed_count;
42   size_t canceled_count;
43 };
44 scoped_ptr<base::Value> RasterTaskCompletionStatsAsValue(
45     const RasterTaskCompletionStats& stats);
46
47 // This class manages tiles, deciding which should get rasterized and which
48 // should no longer have any memory assigned to them. Tile objects are "owned"
49 // by layers; they automatically register with the manager when they are
50 // created, and unregister from the manager when they are deleted.
51 class CC_EXPORT TileManager : public RasterWorkerPoolClient,
52                               public RefCountedManager<Tile> {
53  public:
54   struct CC_EXPORT PairedPictureLayer {
55     PairedPictureLayer();
56     ~PairedPictureLayer();
57
58     PictureLayerImpl* active_layer;
59     PictureLayerImpl* pending_layer;
60   };
61
62   static scoped_ptr<TileManager> Create(
63       TileManagerClient* client,
64       base::SequencedTaskRunner* task_runner,
65       ResourceProvider* resource_provider,
66       ContextProvider* context_provider,
67       RenderingStatsInstrumentation* rendering_stats_instrumentation,
68       bool use_map_image,
69       bool use_rasterize_on_demand,
70       size_t max_transfer_buffer_usage_bytes,
71       size_t max_raster_usage_bytes,
72       unsigned map_image_texture_target);
73   virtual ~TileManager();
74
75   void ManageTiles(const GlobalStateThatImpactsTilePriority& state);
76
77   // Returns true when visible tiles have been initialized.
78   bool UpdateVisibleTiles();
79
80   scoped_refptr<Tile> CreateTile(PicturePileImpl* picture_pile,
81                                  const gfx::Size& tile_size,
82                                  const gfx::Rect& content_rect,
83                                  const gfx::Rect& opaque_rect,
84                                  float contents_scale,
85                                  int layer_id,
86                                  int source_frame_number,
87                                  int flags);
88
89   void RegisterPictureLayerImpl(PictureLayerImpl* layer);
90   void UnregisterPictureLayerImpl(PictureLayerImpl* layer);
91
92   scoped_ptr<base::Value> BasicStateAsValue() const;
93   scoped_ptr<base::Value> AllTilesAsValue() const;
94   void GetMemoryStats(size_t* memory_required_bytes,
95                       size_t* memory_nice_to_have_bytes,
96                       size_t* memory_allocated_bytes,
97                       size_t* memory_used_bytes) const;
98
99   const MemoryHistory::Entry& memory_stats_from_last_assign() const {
100     return memory_stats_from_last_assign_;
101   }
102
103   void GetPairedPictureLayers(std::vector<PairedPictureLayer>* layers) const;
104
105   void InitializeTilesWithResourcesForTesting(
106       const std::vector<Tile*>& tiles,
107       ResourceProvider* resource_provider) {
108     for (size_t i = 0; i < tiles.size(); ++i) {
109       ManagedTileState& mts = tiles[i]->managed_state();
110       ManagedTileState::TileVersion& tile_version =
111           mts.tile_versions[HIGH_QUALITY_NO_LCD_RASTER_MODE];
112
113       tile_version.resource_ = resource_pool_->AcquireResource(gfx::Size(1, 1));
114
115       bytes_releasable_ += BytesConsumedIfAllocated(tiles[i]);
116       ++resources_releasable_;
117     }
118   }
119
120   void SetGlobalStateForTesting(
121       const GlobalStateThatImpactsTilePriority& state) {
122     // Soft limit is used for resource pool such that
123     // memory returns to soft limit after going over.
124     if (state != global_state_) {
125       global_state_ = state;
126       prioritized_tiles_dirty_ = true;
127       resource_pool_->SetResourceUsageLimits(
128           global_state_.soft_memory_limit_in_bytes,
129           global_state_.unused_memory_limit_in_bytes,
130           global_state_.num_resources_limit);
131     }
132   }
133
134  protected:
135   TileManager(TileManagerClient* client,
136               base::SequencedTaskRunner* task_runner,
137               ResourceProvider* resource_provider,
138               ContextProvider* context_provider,
139               scoped_ptr<RasterWorkerPool> raster_worker_pool,
140               scoped_ptr<RasterWorkerPool> direct_raster_worker_pool,
141               size_t max_raster_usage_bytes,
142               RenderingStatsInstrumentation* rendering_stats_instrumentation,
143               bool use_rasterize_on_demand);
144
145   // Methods called by Tile
146   friend class Tile;
147   void DidChangeTilePriority(Tile* tile);
148
149   void CleanUpReleasedTiles();
150
151   // Overriden from RefCountedManager<Tile>:
152   virtual void Release(Tile* tile) OVERRIDE;
153
154   // Overriden from RasterWorkerPoolClient:
155   virtual bool ShouldForceTasksRequiredForActivationToComplete() const OVERRIDE;
156   virtual void DidFinishRunningTasks() OVERRIDE;
157   virtual void DidFinishRunningTasksRequiredForActivation() OVERRIDE;
158
159   typedef std::vector<Tile*> TileVector;
160   typedef std::set<Tile*> TileSet;
161
162   // Virtual for test
163   virtual void ScheduleTasks(
164       const TileVector& tiles_that_need_to_be_rasterized);
165
166   void AssignGpuMemoryToTiles(PrioritizedTileSet* tiles,
167                               TileVector* tiles_that_need_to_be_rasterized);
168   void GetTilesWithAssignedBins(PrioritizedTileSet* tiles);
169
170  private:
171   enum RasterWorkerPoolType {
172     RASTER_WORKER_POOL_TYPE_DEFAULT,
173     RASTER_WORKER_POOL_TYPE_DIRECT,
174     NUM_RASTER_WORKER_POOL_TYPES
175   };
176
177   void OnImageDecodeTaskCompleted(int layer_id,
178                                   SkPixelRef* pixel_ref,
179                                   bool was_canceled);
180   void OnRasterTaskCompleted(Tile::Id tile,
181                              scoped_ptr<ScopedResource> resource,
182                              RasterMode raster_mode,
183                              const PicturePileImpl::Analysis& analysis,
184                              bool was_canceled);
185
186   inline size_t BytesConsumedIfAllocated(const Tile* tile) const {
187     return Resource::MemorySizeBytes(tile->size(),
188                                      raster_worker_pool_->GetResourceFormat());
189   }
190
191   void FreeResourceForTile(Tile* tile, RasterMode mode);
192   void FreeResourcesForTile(Tile* tile);
193   void FreeUnusedResourcesForTile(Tile* tile);
194   scoped_refptr<internal::WorkerPoolTask> CreateImageDecodeTask(
195       Tile* tile,
196       SkPixelRef* pixel_ref);
197   scoped_refptr<internal::RasterWorkerPoolTask> CreateRasterTask(Tile* tile);
198   scoped_ptr<base::Value> GetMemoryRequirementsAsValue() const;
199   void UpdatePrioritizedTileSetIfNeeded();
200
201   TileManagerClient* client_;
202   ContextProvider* context_provider_;
203   scoped_ptr<ResourcePool> resource_pool_;
204   scoped_ptr<RasterWorkerPool> raster_worker_pool_;
205   scoped_ptr<RasterWorkerPool> direct_raster_worker_pool_;
206   scoped_ptr<RasterWorkerPoolDelegate> raster_worker_pool_delegate_;
207   GlobalStateThatImpactsTilePriority global_state_;
208
209   typedef base::hash_map<Tile::Id, Tile*> TileMap;
210   TileMap tiles_;
211
212   PrioritizedTileSet prioritized_tiles_;
213   bool prioritized_tiles_dirty_;
214
215   bool all_tiles_that_need_to_be_rasterized_have_memory_;
216   bool all_tiles_required_for_activation_have_memory_;
217
218   size_t memory_required_bytes_;
219   size_t memory_nice_to_have_bytes_;
220
221   size_t bytes_releasable_;
222   size_t resources_releasable_;
223   size_t max_raster_usage_bytes_;
224
225   bool ever_exceeded_memory_budget_;
226   MemoryHistory::Entry memory_stats_from_last_assign_;
227
228   RenderingStatsInstrumentation* rendering_stats_instrumentation_;
229
230   bool did_initialize_visible_tile_;
231   bool did_check_for_completed_tasks_since_last_schedule_tasks_;
232
233   typedef base::hash_map<uint32_t, scoped_refptr<internal::WorkerPoolTask> >
234       PixelRefTaskMap;
235   typedef base::hash_map<int, PixelRefTaskMap> LayerPixelRefTaskMap;
236   LayerPixelRefTaskMap image_decode_tasks_;
237
238   typedef base::hash_map<int, int> LayerCountMap;
239   LayerCountMap used_layer_counts_;
240
241   RasterTaskCompletionStats update_visible_tiles_stats_;
242
243   std::vector<Tile*> released_tiles_;
244
245   bool use_rasterize_on_demand_;
246
247   // Queues used when scheduling raster tasks.
248   RasterTaskQueue raster_queue_[NUM_RASTER_WORKER_POOL_TYPES];
249
250   std::vector<scoped_refptr<internal::Task> > orphan_raster_tasks_;
251
252   std::vector<PictureLayerImpl*> layers_;
253
254   DISALLOW_COPY_AND_ASSIGN(TileManager);
255 };
256
257 }  // namespace cc
258
259 #endif  // CC_RESOURCES_TILE_MANAGER_H_