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_
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"
23 template <typename LayerType
>
24 class OcclusionTracker
;
25 class PictureLayerTiling
;
26 class PicturePileImpl
;
28 class CC_EXPORT PictureLayerTilingClient
{
30 // Create a tile at the given content_rect (in the contents scale of the
31 // tiling) This might return null if the client cannot create such a tile.
32 virtual scoped_refptr
<Tile
> CreateTile(
33 PictureLayerTiling
* tiling
,
34 const gfx::Rect
& content_rect
) = 0;
35 virtual PicturePileImpl
* GetPile() = 0;
36 virtual gfx::Size
CalculateTileSize(
37 const gfx::Size
& content_bounds
) const = 0;
38 virtual const Region
* GetInvalidation() = 0;
39 virtual const PictureLayerTiling
* GetTwinTiling(
40 const PictureLayerTiling
* tiling
) const = 0;
41 virtual size_t GetMaxTilesForInterestArea() const = 0;
42 virtual float GetSkewportTargetTimeInSeconds() const = 0;
43 virtual int GetSkewportExtrapolationLimitInContentPixels() const = 0;
46 virtual ~PictureLayerTilingClient() {}
49 class CC_EXPORT PictureLayerTiling
{
51 class CC_EXPORT TilingRasterTileIterator
{
53 TilingRasterTileIterator();
54 TilingRasterTileIterator(PictureLayerTiling
* tiling
, WhichTree tree
);
55 ~TilingRasterTileIterator();
57 operator bool() const { return !!current_tile_
; }
58 const Tile
* operator*() const { return current_tile_
; }
59 Tile
* operator*() { return current_tile_
; }
60 TilePriority::PriorityBin
get_type() const { return type_
; }
62 TilingRasterTileIterator
& operator++();
64 gfx::Rect
TileBounds() const {
66 if (type_
== TilePriority::NOW
) {
67 return tiling_
->tiling_data_
.TileBounds(visible_iterator_
.index_x(),
68 visible_iterator_
.index_y());
70 return tiling_
->tiling_data_
.TileBounds(spiral_iterator_
.index_x(),
71 spiral_iterator_
.index_y());
76 bool TileNeedsRaster(Tile
* tile
) const {
77 RasterMode mode
= tile
->DetermineRasterModeForTree(tree_
);
78 return tile
->NeedsRasterForMode(mode
);
81 PictureLayerTiling
* tiling_
;
83 TilePriority::PriorityBin type_
;
84 gfx::Rect visible_rect_in_content_space_
;
85 gfx::Rect skewport_in_content_space_
;
86 gfx::Rect eventually_rect_in_content_space_
;
87 gfx::Rect soon_border_rect_in_content_space_
;
91 TilingData::Iterator visible_iterator_
;
92 TilingData::SpiralDifferenceIterator spiral_iterator_
;
93 bool skewport_processed_
;
96 class CC_EXPORT TilingEvictionTileIterator
{
98 TilingEvictionTileIterator();
99 TilingEvictionTileIterator(PictureLayerTiling
* tiling
,
100 TreePriority tree_priority
);
101 ~TilingEvictionTileIterator();
103 operator bool() const;
104 const Tile
* operator*() const;
106 TilingEvictionTileIterator
& operator++();
107 TilePriority::PriorityBin
get_type() {
109 const TilePriority
& priority
=
110 (*tile_iterator_
)->priority_for_tree_priority(tree_priority_
);
111 return priority
.priority_bin
;
115 PictureLayerTiling
* tiling_
;
116 TreePriority tree_priority_
;
117 std::vector
<Tile
*>::iterator tile_iterator_
;
120 ~PictureLayerTiling();
122 // Create a tiling with no tiles. CreateTiles must be called to add some.
123 static scoped_ptr
<PictureLayerTiling
> Create(
124 float contents_scale
,
125 const gfx::Size
& layer_bounds
,
126 PictureLayerTilingClient
* client
);
127 gfx::Size
layer_bounds() const { return layer_bounds_
; }
128 void UpdateTilesToCurrentPile(const Region
& layer_invalidation
,
129 const gfx::Size
& new_layer_bounds
);
130 void CreateMissingTilesInLiveTilesRect();
131 void RemoveTilesInRegion(const Region
& layer_region
);
133 void SetClient(PictureLayerTilingClient
* client
);
134 void set_resolution(TileResolution resolution
) { resolution_
= resolution
; }
135 TileResolution
resolution() const { return resolution_
; }
137 gfx::Size
tiling_size() const { return tiling_data_
.tiling_size(); }
138 gfx::Rect
live_tiles_rect() const { return live_tiles_rect_
; }
139 gfx::Size
tile_size() const { return tiling_data_
.max_texture_size(); }
140 float contents_scale() const { return contents_scale_
; }
142 Tile
* TileAt(int i
, int j
) const {
143 TileMap::const_iterator iter
= tiles_
.find(TileMapKey(i
, j
));
144 return (iter
== tiles_
.end()) ? NULL
: iter
->second
.get();
147 void CreateAllTilesForTesting() {
148 SetLiveTilesRect(gfx::Rect(tiling_data_
.tiling_size()));
151 const TilingData
& TilingDataForTesting() const { return tiling_data_
; }
153 std::vector
<Tile
*> AllTilesForTesting() const {
154 std::vector
<Tile
*> all_tiles
;
155 for (TileMap::const_iterator it
= tiles_
.begin();
156 it
!= tiles_
.end(); ++it
)
157 all_tiles
.push_back(it
->second
.get());
161 // Iterate over all tiles to fill content_rect. Even if tiles are invalid
162 // (i.e. no valid resource) this tiling should still iterate over them.
163 // The union of all geometry_rect calls for each element iterated over should
164 // exactly equal content_rect and no two geometry_rects should intersect.
165 class CC_EXPORT CoverageIterator
{
168 CoverageIterator(const PictureLayerTiling
* tiling
,
170 const gfx::Rect
& rect
);
173 // Visible rect (no borders), always in the space of content_rect,
174 // regardless of the contents scale of the tiling.
175 gfx::Rect
geometry_rect() const;
176 // Texture rect (in texels) for geometry_rect
177 gfx::RectF
texture_rect() const;
178 gfx::Size
texture_size() const;
180 // Full rect (including borders) of the current tile, always in the space
181 // of content_rect, regardless of the contents scale of the tiling.
182 gfx::Rect
full_tile_geometry_rect() const;
184 Tile
* operator->() const { return current_tile_
; }
185 Tile
* operator*() const { return current_tile_
; }
187 CoverageIterator
& operator++();
188 operator bool() const { return tile_j_
<= bottom_
; }
190 int i() const { return tile_i_
; }
191 int j() const { return tile_j_
; }
194 const PictureLayerTiling
* tiling_
;
195 gfx::Rect dest_rect_
;
196 float dest_to_content_scale_
;
199 gfx::Rect current_geometry_rect_
;
207 friend class PictureLayerTiling
;
212 void UpdateTilePriorities(
214 const gfx::Rect
& visible_layer_rect
,
215 float ideal_contents_scale
,
216 double current_frame_time_in_seconds
,
217 const OcclusionTracker
<LayerImpl
>* occlusion_tracker
,
218 const LayerImpl
* render_target
,
219 const gfx::Transform
& draw_transform
);
221 // Copies the src_tree priority into the dst_tree priority for all tiles.
222 // The src_tree priority is reset to the lowest priority possible. This
223 // also updates the pile on each tile to be the current client's pile.
224 void DidBecomeActive();
226 // Resets the active priority for all tiles in a tiling, when an active
227 // tiling is becoming recycled. This may include some tiles which are
228 // not in the the pending tiling (due to invalidations). This must
229 // be called before DidBecomeActive, as it resets the active priority
230 // while DidBecomeActive promotes pending priority on a similar set of tiles.
231 void DidBecomeRecycled();
233 bool NeedsUpdateForFrameAtTime(double frame_time_in_seconds
) {
234 return frame_time_in_seconds
!= last_impl_frame_time_in_seconds_
;
237 scoped_ptr
<base::Value
> AsValue() const;
238 size_t GPUMemoryUsageInBytes() const;
240 struct RectExpansionCache
{
241 RectExpansionCache();
243 gfx::Rect previous_start
;
244 gfx::Rect previous_bounds
;
245 gfx::Rect previous_result
;
246 int64 previous_target
;
250 gfx::Rect
ExpandRectEquallyToAreaBoundedBy(
251 const gfx::Rect
& starting_rect
,
253 const gfx::Rect
& bounding_rect
,
254 RectExpansionCache
* cache
);
256 bool has_ever_been_updated() const {
257 return last_impl_frame_time_in_seconds_
!= 0.0;
261 friend class CoverageIterator
;
262 friend class TilingRasterTileIterator
;
263 friend class TilingEvictionTileIterator
;
265 typedef std::pair
<int, int> TileMapKey
;
266 typedef base::hash_map
<TileMapKey
, scoped_refptr
<Tile
> > TileMap
;
268 PictureLayerTiling(float contents_scale
,
269 const gfx::Size
& layer_bounds
,
270 PictureLayerTilingClient
* client
);
271 void SetLiveTilesRect(const gfx::Rect
& live_tiles_rect
);
272 Tile
* CreateTile(int i
, int j
, const PictureLayerTiling
* twin_tiling
);
274 // Computes a skewport. The calculation extrapolates the last visible
275 // rect and the current visible rect to expand the skewport to where it
276 // would be in |skewport_target_time| seconds. Note that the skewport
277 // is guaranteed to contain the current visible rect.
278 gfx::Rect
ComputeSkewport(double current_frame_time_in_seconds
,
279 const gfx::Rect
& visible_rect_in_content_space
)
282 void UpdateEvictionCacheIfNeeded(TreePriority tree_priority
);
283 void Invalidate(const Region
& layer_region
);
285 void DoInvalidate(const Region
& layer_region
,
286 bool recreate_invalidated_tiles
);
289 float contents_scale_
;
290 gfx::Size layer_bounds_
;
291 TileResolution resolution_
;
292 PictureLayerTilingClient
* client_
;
295 TilingData tiling_data_
;
296 TileMap tiles_
; // It is not legal to have a NULL tile in the tiles_ map.
297 gfx::Rect live_tiles_rect_
;
299 // State saved for computing velocities based upon finite differences.
300 double last_impl_frame_time_in_seconds_
;
301 gfx::Rect last_visible_rect_in_content_space_
;
303 // Iteration rects in content space
304 gfx::Rect current_visible_rect_
;
305 gfx::Rect current_skewport_rect_
;
306 gfx::Rect current_soon_border_rect_
;
307 gfx::Rect current_eventually_rect_
;
309 std::vector
<Tile
*> eviction_tiles_cache_
;
310 bool eviction_tiles_cache_valid_
;
311 TreePriority eviction_cache_tree_priority_
;
314 DISALLOW_ASSIGN(PictureLayerTiling
);
316 RectExpansionCache expansion_cache_
;
321 #endif // CC_RESOURCES_PICTURE_LAYER_TILING_H_