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_
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"
31 class PictureLayerTiling;
32 class PicturePileImpl;
34 class CC_EXPORT PictureLayerTilingClient {
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;
58 virtual ~PictureLayerTilingClient() {}
61 class CC_EXPORT PictureLayerTiling {
63 static const int kBorderTexels = 1;
65 enum EvictionCategory {
67 EVENTUALLY_AND_REQUIRED_FOR_ACTIVATION,
69 SOON_AND_REQUIRED_FOR_ACTIVATION,
71 NOW_AND_REQUIRED_FOR_ACTIVATION
74 class CC_EXPORT TilingRasterTileIterator {
76 TilingRasterTileIterator();
77 explicit TilingRasterTileIterator(PictureLayerTiling* tiling);
78 ~TilingRasterTileIterator();
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 {
86 return TilePriority::NOW;
88 case SOON_BORDER_RECT:
89 return TilePriority::SOON;
91 return TilePriority::EVENTUALLY;
94 return TilePriority::EVENTUALLY;
97 TilingRasterTileIterator& operator++();
108 bool TileNeedsRaster(Tile* tile) const {
109 return tile->NeedsRaster() && !tiling_->IsTileOccluded(tile);
112 PictureLayerTiling* tiling_;
117 TilingData::Iterator visible_iterator_;
118 TilingData::SpiralDifferenceIterator spiral_iterator_;
121 class CC_EXPORT TilingEvictionTileIterator {
123 TilingEvictionTileIterator();
124 TilingEvictionTileIterator(PictureLayerTiling* tiling,
125 TreePriority tree_priority,
126 EvictionCategory category);
127 ~TilingEvictionTileIterator();
129 operator bool() const;
130 const Tile* operator*() const;
132 TilingEvictionTileIterator& operator++();
135 const std::vector<Tile*>* eviction_tiles_;
136 size_t current_eviction_tiles_index_;
139 ~PictureLayerTiling();
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);
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;
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_; }
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();
169 void CreateAllTilesForTesting() {
170 SetLiveTilesRect(gfx::Rect(tiling_data_.tiling_size()));
173 const TilingData& TilingDataForTesting() const { return tiling_data_; }
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());
182 void UpdateAllTilePrioritiesForTesting() {
183 for (TileMap::const_iterator it = tiles_.begin(); it != tiles_.end(); ++it)
184 UpdateTileAndTwinPriority(it->second.get());
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);
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));
203 const gfx::Rect& GetCurrentVisibleRectForTesting() const {
204 return current_visible_rect_;
207 bool IsTileOccluded(const Tile* tile) const;
208 bool IsTileRequiredForActivation(const Tile* tile) const;
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 {
217 CoverageIterator(const PictureLayerTiling* tiling,
219 const gfx::Rect& rect);
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;
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;
233 Tile* operator->() const { return current_tile_; }
234 Tile* operator*() const { return current_tile_; }
236 CoverageIterator& operator++();
237 operator bool() const { return tile_j_ <= bottom_; }
239 int i() const { return tile_i_; }
240 int j() const { return tile_j_; }
243 const PictureLayerTiling* tiling_;
244 gfx::Rect dest_rect_;
245 float dest_to_content_scale_;
248 gfx::Rect current_geometry_rect_;
256 friend class PictureLayerTiling;
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);
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_;
274 void GetAllTilesForTracing(std::set<const Tile*>* tiles) const;
275 void AsValueInto(base::debug::TracedValue* array) const;
276 size_t GPUMemoryUsageInBytes() const;
278 struct RectExpansionCache {
279 RectExpansionCache();
281 gfx::Rect previous_start;
282 gfx::Rect previous_bounds;
283 gfx::Rect previous_result;
284 int64 previous_target;
288 gfx::Rect ExpandRectEquallyToAreaBoundedBy(
289 const gfx::Rect& starting_rect,
291 const gfx::Rect& bounding_rect,
292 RectExpansionCache* cache);
294 bool has_ever_been_updated() const {
295 return last_impl_frame_time_in_seconds_ != 0.0;
299 friend class CoverageIterator;
300 friend class TilingRasterTileIterator;
301 friend class TilingEvictionTileIterator;
303 typedef std::pair<int, int> TileMapKey;
304 typedef base::hash_map<TileMapKey, scoped_refptr<Tile>> TileMap;
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);
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)
323 void UpdateEvictionCacheIfNeeded(TreePriority tree_priority);
324 const std::vector<Tile*>* GetEvictionTiles(TreePriority tree_priority,
325 EvictionCategory category);
327 void Invalidate(const Region& layer_region);
329 void DoInvalidate(const Region& layer_region,
330 bool recreate_invalidated_tiles);
332 void UpdateTileAndTwinPriority(Tile* tile) const;
333 void UpdateTilePriority(Tile* tile) const;
336 float contents_scale_;
337 gfx::Size layer_bounds_;
338 TileResolution resolution_;
339 PictureLayerTilingClient* client_;
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_;
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_;
352 bool can_require_tiles_for_activation_;
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_;
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_;
365 Occlusion current_occlusion_in_layer_space_;
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
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_;
377 bool eviction_tiles_cache_valid_;
378 TreePriority eviction_cache_tree_priority_;
381 DISALLOW_ASSIGN(PictureLayerTiling);
383 RectExpansionCache expansion_cache_;
388 #endif // CC_RESOURCES_PICTURE_LAYER_TILING_H_