Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / cc / resources / picture_layer_tiling.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_PICTURE_LAYER_TILING_H_
6 #define CC_RESOURCES_PICTURE_LAYER_TILING_H_
7
8 #include <set>
9 #include <utility>
10 #include <vector>
11
12 #include "base/basictypes.h"
13 #include "base/containers/hash_tables.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "cc/base/cc_export.h"
16 #include "cc/base/region.h"
17 #include "cc/base/tiling_data.h"
18 #include "cc/resources/tile.h"
19 #include "cc/resources/tile_priority.h"
20 #include "cc/trees/occlusion.h"
21 #include "ui/gfx/geometry/rect.h"
22
23 namespace base {
24 namespace debug {
25 class TracedValue;
26 }
27 }
28
29 namespace cc {
30
31 class PictureLayerTiling;
32 class PicturePileImpl;
33
34 class CC_EXPORT PictureLayerTilingClient {
35  public:
36   // Create a tile at the given content_rect (in the contents scale of the
37   // tiling) This might return null if the client cannot create such a tile.
38   virtual scoped_refptr<Tile> CreateTile(
39     PictureLayerTiling* tiling,
40     const gfx::Rect& content_rect) = 0;
41   virtual RasterSource* GetRasterSource() = 0;
42   virtual gfx::Size CalculateTileSize(
43     const gfx::Size& content_bounds) const = 0;
44   // This invalidation region defines the area (if any, it can by null) that
45   // tiles can not be shared between pending and active trees.
46   virtual const Region* GetPendingInvalidation() = 0;
47   virtual const PictureLayerTiling* GetPendingOrActiveTwinTiling(
48       const PictureLayerTiling* tiling) const = 0;
49   virtual PictureLayerTiling* GetRecycledTwinTiling(
50       const PictureLayerTiling* tiling) = 0;
51   virtual size_t GetMaxTilesForInterestArea() const = 0;
52   virtual float GetSkewportTargetTimeInSeconds() const = 0;
53   virtual int GetSkewportExtrapolationLimitInContentPixels() const = 0;
54   virtual WhichTree GetTree() const = 0;
55   virtual bool RequiresHighResToDraw() const = 0;
56
57  protected:
58   virtual ~PictureLayerTilingClient() {}
59 };
60
61 class CC_EXPORT PictureLayerTiling {
62  public:
63   static const int kBorderTexels = 1;
64
65   enum EvictionCategory {
66     EVENTUALLY,
67     EVENTUALLY_AND_REQUIRED_FOR_ACTIVATION,
68     SOON,
69     SOON_AND_REQUIRED_FOR_ACTIVATION,
70     NOW,
71     NOW_AND_REQUIRED_FOR_ACTIVATION
72   };
73
74   class CC_EXPORT TilingRasterTileIterator {
75    public:
76     TilingRasterTileIterator();
77     explicit TilingRasterTileIterator(PictureLayerTiling* tiling);
78     ~TilingRasterTileIterator();
79
80     operator bool() const { return !!current_tile_; }
81     const Tile* operator*() const { return current_tile_; }
82     Tile* operator*() { return current_tile_; }
83     TilePriority::PriorityBin get_type() const {
84       switch (phase_) {
85         case VISIBLE_RECT:
86           return TilePriority::NOW;
87         case SKEWPORT_RECT:
88         case SOON_BORDER_RECT:
89           return TilePriority::SOON;
90         case EVENTUALLY_RECT:
91           return TilePriority::EVENTUALLY;
92       }
93       NOTREACHED();
94       return TilePriority::EVENTUALLY;
95     }
96
97     TilingRasterTileIterator& operator++();
98
99    private:
100     enum Phase {
101       VISIBLE_RECT,
102       SKEWPORT_RECT,
103       SOON_BORDER_RECT,
104       EVENTUALLY_RECT
105     };
106
107     void AdvancePhase();
108     bool TileNeedsRaster(Tile* tile) const {
109       return tile->NeedsRaster() && !tiling_->IsTileOccluded(tile);
110     }
111
112     PictureLayerTiling* tiling_;
113
114     Phase phase_;
115
116     Tile* current_tile_;
117     TilingData::Iterator visible_iterator_;
118     TilingData::SpiralDifferenceIterator spiral_iterator_;
119   };
120
121   class CC_EXPORT TilingEvictionTileIterator {
122    public:
123     TilingEvictionTileIterator();
124     TilingEvictionTileIterator(PictureLayerTiling* tiling,
125                                TreePriority tree_priority,
126                                EvictionCategory category);
127     ~TilingEvictionTileIterator();
128
129     operator bool() const;
130     const Tile* operator*() const;
131     Tile* operator*();
132     TilingEvictionTileIterator& operator++();
133
134    private:
135     const std::vector<Tile*>* eviction_tiles_;
136     size_t current_eviction_tiles_index_;
137   };
138
139   ~PictureLayerTiling();
140
141   // Create a tiling with no tiles.  CreateTiles must be called to add some.
142   static scoped_ptr<PictureLayerTiling> Create(
143       float contents_scale,
144       const gfx::Size& layer_bounds,
145       PictureLayerTilingClient* client);
146   gfx::Size layer_bounds() const { return layer_bounds_; }
147   void UpdateTilesToCurrentPile(const Region& layer_invalidation,
148                                 const gfx::Size& new_layer_bounds);
149   void CreateMissingTilesInLiveTilesRect();
150   void RemoveTilesInRegion(const Region& layer_region);
151
152   void SetClient(PictureLayerTilingClient* client);
153   void set_resolution(TileResolution resolution) { resolution_ = resolution; }
154   TileResolution resolution() const { return resolution_; }
155   void set_can_require_tiles_for_activation(bool can_require_tiles) {
156     can_require_tiles_for_activation_ = can_require_tiles;
157   }
158
159   gfx::Size tiling_size() const { return tiling_data_.tiling_size(); }
160   gfx::Rect live_tiles_rect() const { return live_tiles_rect_; }
161   gfx::Size tile_size() const { return tiling_data_.max_texture_size(); }
162   float contents_scale() const { return contents_scale_; }
163
164   Tile* TileAt(int i, int j) const {
165     TileMap::const_iterator iter = tiles_.find(TileMapKey(i, j));
166     return (iter == tiles_.end()) ? NULL : iter->second.get();
167   }
168
169   void CreateAllTilesForTesting() {
170     SetLiveTilesRect(gfx::Rect(tiling_data_.tiling_size()));
171   }
172
173   const TilingData& TilingDataForTesting() const { return tiling_data_; }
174
175   std::vector<Tile*> AllTilesForTesting() const {
176     std::vector<Tile*> all_tiles;
177     for (TileMap::const_iterator it = tiles_.begin(); it != tiles_.end(); ++it)
178       all_tiles.push_back(it->second.get());
179     return all_tiles;
180   }
181
182   void UpdateAllTilePrioritiesForTesting() {
183     for (TileMap::const_iterator it = tiles_.begin(); it != tiles_.end(); ++it)
184       UpdateTileAndTwinPriority(it->second.get());
185   }
186
187   std::vector<scoped_refptr<Tile>> AllRefTilesForTesting() const {
188     std::vector<scoped_refptr<Tile>> all_tiles;
189     for (TileMap::const_iterator it = tiles_.begin(); it != tiles_.end(); ++it)
190       all_tiles.push_back(it->second);
191     return all_tiles;
192   }
193
194   void SetAllTilesOccludedForTesting() {
195     gfx::Rect viewport_in_layer_space =
196         ScaleToEnclosingRect(current_visible_rect_, 1.0f / contents_scale_);
197     current_occlusion_in_layer_space_ =
198         Occlusion(gfx::Transform(),
199                   SimpleEnclosedRegion(viewport_in_layer_space),
200                   SimpleEnclosedRegion(viewport_in_layer_space));
201   }
202
203   const gfx::Rect& GetCurrentVisibleRectForTesting() const {
204     return current_visible_rect_;
205   }
206
207   bool IsTileOccluded(const Tile* tile) const;
208   bool IsTileRequiredForActivation(const Tile* tile) const;
209
210   // Iterate over all tiles to fill content_rect.  Even if tiles are invalid
211   // (i.e. no valid resource) this tiling should still iterate over them.
212   // The union of all geometry_rect calls for each element iterated over should
213   // exactly equal content_rect and no two geometry_rects should intersect.
214   class CC_EXPORT CoverageIterator {
215    public:
216     CoverageIterator();
217     CoverageIterator(const PictureLayerTiling* tiling,
218         float dest_scale,
219         const gfx::Rect& rect);
220     ~CoverageIterator();
221
222     // Visible rect (no borders), always in the space of content_rect,
223     // regardless of the contents scale of the tiling.
224     gfx::Rect geometry_rect() const;
225     // Texture rect (in texels) for geometry_rect
226     gfx::RectF texture_rect() const;
227     gfx::Size texture_size() const;
228
229     // Full rect (including borders) of the current tile, always in the space
230     // of content_rect, regardless of the contents scale of the tiling.
231     gfx::Rect full_tile_geometry_rect() const;
232
233     Tile* operator->() const { return current_tile_; }
234     Tile* operator*() const { return current_tile_; }
235
236     CoverageIterator& operator++();
237     operator bool() const { return tile_j_ <= bottom_; }
238
239     int i() const { return tile_i_; }
240     int j() const { return tile_j_; }
241
242    private:
243     const PictureLayerTiling* tiling_;
244     gfx::Rect dest_rect_;
245     float dest_to_content_scale_;
246
247     Tile* current_tile_;
248     gfx::Rect current_geometry_rect_;
249     int tile_i_;
250     int tile_j_;
251     int left_;
252     int top_;
253     int right_;
254     int bottom_;
255
256     friend class PictureLayerTiling;
257   };
258
259   void Reset();
260
261   void ComputeTilePriorityRects(WhichTree tree,
262                                 const gfx::Rect& viewport_in_layer_space,
263                                 float ideal_contents_scale,
264                                 double current_frame_time_in_seconds,
265                                 const Occlusion& occlusion_in_layer_space);
266
267   bool NeedsUpdateForFrameAtTimeAndViewport(
268       double frame_time_in_seconds,
269       const gfx::Rect& viewport_in_layer_space) {
270     return frame_time_in_seconds != last_impl_frame_time_in_seconds_ ||
271            viewport_in_layer_space != last_viewport_in_layer_space_;
272   }
273
274   void GetAllTilesForTracing(std::set<const Tile*>* tiles) const;
275   void AsValueInto(base::debug::TracedValue* array) const;
276   size_t GPUMemoryUsageInBytes() const;
277
278   struct RectExpansionCache {
279     RectExpansionCache();
280
281     gfx::Rect previous_start;
282     gfx::Rect previous_bounds;
283     gfx::Rect previous_result;
284     int64 previous_target;
285   };
286
287   static
288   gfx::Rect ExpandRectEquallyToAreaBoundedBy(
289       const gfx::Rect& starting_rect,
290       int64 target_area,
291       const gfx::Rect& bounding_rect,
292       RectExpansionCache* cache);
293
294   bool has_ever_been_updated() const {
295     return last_impl_frame_time_in_seconds_ != 0.0;
296   }
297
298  protected:
299   friend class CoverageIterator;
300   friend class TilingRasterTileIterator;
301   friend class TilingEvictionTileIterator;
302
303   typedef std::pair<int, int> TileMapKey;
304   typedef base::hash_map<TileMapKey, scoped_refptr<Tile>> TileMap;
305
306   PictureLayerTiling(float contents_scale,
307                      const gfx::Size& layer_bounds,
308                      PictureLayerTilingClient* client);
309   void SetLiveTilesRect(const gfx::Rect& live_tiles_rect);
310   void VerifyLiveTilesRect();
311   Tile* CreateTile(int i, int j, const PictureLayerTiling* twin_tiling);
312   // Returns true if the Tile existed and was removed from the tiling.
313   bool RemoveTileAt(int i, int j, PictureLayerTiling* recycled_twin);
314
315   // Computes a skewport. The calculation extrapolates the last visible
316   // rect and the current visible rect to expand the skewport to where it
317   // would be in |skewport_target_time| seconds. Note that the skewport
318   // is guaranteed to contain the current visible rect.
319   gfx::Rect ComputeSkewport(double current_frame_time_in_seconds,
320                             const gfx::Rect& visible_rect_in_content_space)
321       const;
322
323   void UpdateEvictionCacheIfNeeded(TreePriority tree_priority);
324   const std::vector<Tile*>* GetEvictionTiles(TreePriority tree_priority,
325                                              EvictionCategory category);
326
327   void Invalidate(const Region& layer_region);
328
329   void DoInvalidate(const Region& layer_region,
330                     bool recreate_invalidated_tiles);
331
332   void UpdateTileAndTwinPriority(Tile* tile) const;
333   void UpdateTilePriority(Tile* tile) const;
334
335   // Given properties.
336   float contents_scale_;
337   gfx::Size layer_bounds_;
338   TileResolution resolution_;
339   PictureLayerTilingClient* client_;
340
341   // Internal data.
342   TilingData tiling_data_;
343   TileMap tiles_;  // It is not legal to have a NULL tile in the tiles_ map.
344   gfx::Rect live_tiles_rect_;
345
346   // State saved for computing velocities based upon finite differences.
347   double last_impl_frame_time_in_seconds_;
348   gfx::Rect last_viewport_in_layer_space_;
349   gfx::Rect last_visible_rect_in_content_space_;
350   float content_to_screen_scale_;
351
352   bool can_require_tiles_for_activation_;
353
354   // Iteration rects in content space
355   gfx::Rect current_visible_rect_;
356   gfx::Rect current_skewport_rect_;
357   gfx::Rect current_soon_border_rect_;
358   gfx::Rect current_eventually_rect_;
359
360   bool has_visible_rect_tiles_;
361   bool has_skewport_rect_tiles_;
362   bool has_soon_border_rect_tiles_;
363   bool has_eventually_rect_tiles_;
364
365   Occlusion current_occlusion_in_layer_space_;
366
367   // TODO(reveman): Remove this in favour of an array of eviction_tiles_ when we
368   // change all enums to have a consistent way of getting the count/last
369   // element.
370   std::vector<Tile*> eviction_tiles_now_;
371   std::vector<Tile*> eviction_tiles_now_and_required_for_activation_;
372   std::vector<Tile*> eviction_tiles_soon_;
373   std::vector<Tile*> eviction_tiles_soon_and_required_for_activation_;
374   std::vector<Tile*> eviction_tiles_eventually_;
375   std::vector<Tile*> eviction_tiles_eventually_and_required_for_activation_;
376
377   bool eviction_tiles_cache_valid_;
378   TreePriority eviction_cache_tree_priority_;
379
380  private:
381   DISALLOW_ASSIGN(PictureLayerTiling);
382
383   RectExpansionCache expansion_cache_;
384 };
385
386 }  // namespace cc
387
388 #endif  // CC_RESOURCES_PICTURE_LAYER_TILING_H_