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