Revert 268405 "Make sure that ScratchBuffer::Allocate() always r..."
[chromium-blink-merge.git] / cc / resources / picture_layer_tiling.h
blob92c28797a6ed59998517faa6f904a7d5f1a1757a
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_PICTURE_LAYER_TILING_H_
6 #define CC_RESOURCES_PICTURE_LAYER_TILING_H_
8 #include <utility>
9 #include <vector>
11 #include "base/basictypes.h"
12 #include "base/containers/hash_tables.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "cc/base/cc_export.h"
15 #include "cc/base/region.h"
16 #include "cc/base/tiling_data.h"
17 #include "cc/resources/tile.h"
18 #include "cc/resources/tile_priority.h"
19 #include "ui/gfx/rect.h"
21 namespace cc {
23 class PictureLayerTiling;
25 class CC_EXPORT PictureLayerTilingClient {
26 public:
27 // Create a tile at the given content_rect (in the contents scale of the
28 // tiling) This might return null if the client cannot create such a tile.
29 virtual scoped_refptr<Tile> CreateTile(
30 PictureLayerTiling* tiling,
31 const gfx::Rect& content_rect) = 0;
32 virtual void UpdatePile(Tile* tile) = 0;
33 virtual gfx::Size CalculateTileSize(
34 const gfx::Size& content_bounds) const = 0;
35 virtual const Region* GetInvalidation() = 0;
36 virtual const PictureLayerTiling* GetTwinTiling(
37 const PictureLayerTiling* tiling) const = 0;
38 virtual size_t GetMaxTilesForInterestArea() const = 0;
39 virtual float GetSkewportTargetTimeInSeconds() const = 0;
40 virtual int GetSkewportExtrapolationLimitInContentPixels() const = 0;
42 protected:
43 virtual ~PictureLayerTilingClient() {}
46 class CC_EXPORT PictureLayerTiling {
47 public:
48 class CC_EXPORT TilingRasterTileIterator {
49 public:
50 TilingRasterTileIterator();
51 TilingRasterTileIterator(PictureLayerTiling* tiling, WhichTree tree);
52 ~TilingRasterTileIterator();
54 operator bool() const {
55 return current_tile_ && TileNeedsRaster(current_tile_);
57 Tile* operator*() { return current_tile_; }
58 TilePriority::PriorityBin get_type() const { return type_; }
60 TilingRasterTileIterator& operator++();
62 gfx::Rect TileBounds() const {
63 DCHECK(*this);
64 if (type_ == TilePriority::NOW) {
65 return tiling_->tiling_data_.TileBounds(visible_iterator_.index_x(),
66 visible_iterator_.index_y());
68 return tiling_->tiling_data_.TileBounds(spiral_iterator_.index_x(),
69 spiral_iterator_.index_y());
72 private:
73 void AdvancePhase();
74 bool TileNeedsRaster(Tile* tile) const {
75 RasterMode mode = tile->DetermineRasterModeForTree(tree_);
76 return tile->NeedsRasterForMode(mode);
79 PictureLayerTiling* tiling_;
81 TilePriority::PriorityBin type_;
82 gfx::Rect visible_rect_in_content_space_;
83 gfx::Rect skewport_in_content_space_;
84 gfx::Rect eventually_rect_in_content_space_;
85 WhichTree tree_;
87 Tile* current_tile_;
88 TilingData::Iterator visible_iterator_;
89 TilingData::SpiralDifferenceIterator spiral_iterator_;
92 class CC_EXPORT TilingEvictionTileIterator {
93 public:
94 TilingEvictionTileIterator();
95 TilingEvictionTileIterator(PictureLayerTiling* tiling,
96 TreePriority tree_priority);
97 ~TilingEvictionTileIterator();
99 operator bool();
100 Tile* operator*();
101 TilingEvictionTileIterator& operator++();
102 TilePriority::PriorityBin get_type() {
103 DCHECK(*this);
104 const TilePriority& priority =
105 (*tile_iterator_)->priority_for_tree_priority(tree_priority_);
106 return priority.priority_bin;
109 private:
110 void Initialize();
111 bool IsValid() const { return is_valid_; }
113 bool is_valid_;
114 PictureLayerTiling* tiling_;
115 TreePriority tree_priority_;
116 std::vector<Tile*>::iterator tile_iterator_;
119 ~PictureLayerTiling();
121 // Create a tiling with no tiles. CreateTiles must be called to add some.
122 static scoped_ptr<PictureLayerTiling> Create(
123 float contents_scale,
124 const gfx::Size& layer_bounds,
125 PictureLayerTilingClient* client);
126 gfx::Size layer_bounds() const { return layer_bounds_; }
127 void SetLayerBounds(const gfx::Size& layer_bounds);
128 void Invalidate(const Region& layer_region);
129 void RemoveTilesInRegion(const Region& layer_region);
130 void CreateMissingTilesInLiveTilesRect();
132 void SetCanUseLCDText(bool can_use_lcd_text);
134 void SetClient(PictureLayerTilingClient* client);
135 void set_resolution(TileResolution resolution) { resolution_ = resolution; }
136 TileResolution resolution() const { return resolution_; }
138 gfx::Rect TilingRect() const;
139 gfx::Rect live_tiles_rect() const { return live_tiles_rect_; }
140 gfx::Size tile_size() const { return tiling_data_.max_texture_size(); }
141 float contents_scale() const { return contents_scale_; }
143 Tile* TileAt(int i, int j) const {
144 TileMap::const_iterator iter = tiles_.find(TileMapKey(i, j));
145 return (iter == tiles_.end()) ? NULL : iter->second.get();
148 void CreateAllTilesForTesting() {
149 SetLiveTilesRect(tiling_data_.tiling_rect());
152 std::vector<Tile*> AllTilesForTesting() const {
153 std::vector<Tile*> all_tiles;
154 for (TileMap::const_iterator it = tiles_.begin();
155 it != tiles_.end(); ++it)
156 all_tiles.push_back(it->second.get());
157 return all_tiles;
160 // Iterate over all tiles to fill content_rect. Even if tiles are invalid
161 // (i.e. no valid resource) this tiling should still iterate over them.
162 // The union of all geometry_rect calls for each element iterated over should
163 // exactly equal content_rect and no two geometry_rects should intersect.
164 class CC_EXPORT CoverageIterator {
165 public:
166 CoverageIterator();
167 CoverageIterator(const PictureLayerTiling* tiling,
168 float dest_scale,
169 const gfx::Rect& rect);
170 ~CoverageIterator();
172 // Visible rect (no borders), always in the space of content_rect,
173 // regardless of the contents scale of the tiling.
174 gfx::Rect geometry_rect() const;
175 // Texture rect (in texels) for geometry_rect
176 gfx::RectF texture_rect() const;
177 gfx::Size texture_size() const;
179 // Full rect (including borders) of the current tile, always in the space
180 // of content_rect, regardless of the contents scale of the tiling.
181 gfx::Rect full_tile_geometry_rect() const;
183 Tile* operator->() const { return current_tile_; }
184 Tile* operator*() const { return current_tile_; }
186 CoverageIterator& operator++();
187 operator bool() const { return tile_j_ <= bottom_; }
189 int i() const { return tile_i_; }
190 int j() const { return tile_j_; }
192 private:
193 const PictureLayerTiling* tiling_;
194 gfx::Rect dest_rect_;
195 float dest_to_content_scale_;
197 Tile* current_tile_;
198 gfx::Rect current_geometry_rect_;
199 int tile_i_;
200 int tile_j_;
201 int left_;
202 int top_;
203 int right_;
204 int bottom_;
206 friend class PictureLayerTiling;
209 Region OpaqueRegionInContentRect(const gfx::Rect& content_rect) const;
211 void Reset();
213 void UpdateTilePriorities(WhichTree tree,
214 const gfx::Rect& visible_layer_rect,
215 float layer_contents_scale,
216 double current_frame_time_in_seconds);
218 // Copies the src_tree priority into the dst_tree priority for all tiles.
219 // The src_tree priority is reset to the lowest priority possible. This
220 // also updates the pile on each tile to be the current client's pile.
221 void DidBecomeActive();
223 // Resets the active priority for all tiles in a tiling, when an active
224 // tiling is becoming recycled. This may include some tiles which are
225 // not in the the pending tiling (due to invalidations). This must
226 // be called before DidBecomeActive, as it resets the active priority
227 // while DidBecomeActive promotes pending priority on a similar set of tiles.
228 void DidBecomeRecycled();
230 void UpdateTilesToCurrentPile();
232 bool NeedsUpdateForFrameAtTime(double frame_time_in_seconds) {
233 return frame_time_in_seconds != last_impl_frame_time_in_seconds_;
236 scoped_ptr<base::Value> AsValue() const;
237 size_t GPUMemoryUsageInBytes() const;
239 struct RectExpansionCache {
240 RectExpansionCache();
242 gfx::Rect previous_start;
243 gfx::Rect previous_bounds;
244 gfx::Rect previous_result;
245 int64 previous_target;
248 static
249 gfx::Rect ExpandRectEquallyToAreaBoundedBy(
250 const gfx::Rect& starting_rect,
251 int64 target_area,
252 const gfx::Rect& bounding_rect,
253 RectExpansionCache* cache);
255 bool has_ever_been_updated() const {
256 return last_impl_frame_time_in_seconds_ != 0.0;
259 protected:
260 friend class CoverageIterator;
261 friend class TilingRasterTileIterator;
262 friend class TilingEvictionTileIterator;
264 typedef std::pair<int, int> TileMapKey;
265 typedef base::hash_map<TileMapKey, scoped_refptr<Tile> > TileMap;
267 PictureLayerTiling(float contents_scale,
268 const gfx::Size& layer_bounds,
269 PictureLayerTilingClient* client);
270 void SetLiveTilesRect(const gfx::Rect& live_tiles_rect);
271 Tile* CreateTile(int i, int j, const PictureLayerTiling* twin_tiling);
273 // Computes a skewport. The calculation extrapolates the last visible
274 // rect and the current visible rect to expand the skewport to where it
275 // would be in |skewport_target_time| seconds. Note that the skewport
276 // is guaranteed to contain the current visible rect.
277 gfx::Rect ComputeSkewport(double current_frame_time_in_seconds,
278 const gfx::Rect& visible_rect_in_content_space)
279 const;
281 void UpdateEvictionCacheIfNeeded(TreePriority tree_priority);
282 void DoInvalidate(const Region& layer_region, bool recreate_tiles);
284 // Given properties.
285 float contents_scale_;
286 gfx::Size layer_bounds_;
287 TileResolution resolution_;
288 PictureLayerTilingClient* client_;
290 // Internal data.
291 TilingData tiling_data_;
292 TileMap tiles_; // It is not legal to have a NULL tile in the tiles_ map.
293 gfx::Rect live_tiles_rect_;
295 // State saved for computing velocities based upon finite differences.
296 double last_impl_frame_time_in_seconds_;
297 gfx::Rect last_visible_rect_in_content_space_;
299 gfx::Rect current_visible_rect_in_content_space_;
300 gfx::Rect current_skewport_;
301 gfx::Rect current_eventually_rect_;
303 std::vector<Tile*> eviction_tiles_cache_;
304 bool eviction_tiles_cache_valid_;
305 TreePriority eviction_cache_tree_priority_;
307 private:
308 DISALLOW_ASSIGN(PictureLayerTiling);
310 RectExpansionCache expansion_cache_;
313 } // namespace cc
315 #endif // CC_RESOURCES_PICTURE_LAYER_TILING_H_