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"
24 namespace trace_event
{
31 class PictureLayerTiling
;
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(float contents_scale
,
39 const gfx::Rect
& content_rect
) = 0;
40 virtual gfx::Size
CalculateTileSize(
41 const gfx::Size
& content_bounds
) const = 0;
42 // This invalidation region defines the area (if any, it can by null) that
43 // tiles can not be shared between pending and active trees.
44 virtual const Region
* GetPendingInvalidation() = 0;
45 virtual const PictureLayerTiling
* GetPendingOrActiveTwinTiling(
46 const PictureLayerTiling
* tiling
) const = 0;
47 virtual PictureLayerTiling
* GetRecycledTwinTiling(
48 const PictureLayerTiling
* tiling
) = 0;
49 virtual TilePriority::PriorityBin
GetMaxTilePriorityBin() const = 0;
50 virtual WhichTree
GetTree() const = 0;
51 virtual bool RequiresHighResToDraw() const = 0;
54 virtual ~PictureLayerTilingClient() {}
57 class CC_EXPORT PictureLayerTiling
{
59 static const int kBorderTexels
= 1;
61 ~PictureLayerTiling();
63 static float CalculateSoonBorderDistance(
64 const gfx::Rect
& visible_rect_in_content_space
,
65 float content_to_screen_scale
);
67 // Create a tiling with no tiles. CreateTile() must be called to add some.
68 static scoped_ptr
<PictureLayerTiling
> Create(
70 scoped_refptr
<RasterSource
> raster_source
,
71 PictureLayerTilingClient
* client
,
72 size_t max_tiles_for_interest_area
,
73 float skewport_target_time_in_seconds
,
74 int skewport_extrapolation_limit_in_content_pixels
);
76 void SetRasterSourceAndResize(scoped_refptr
<RasterSource
> raster_source
);
77 void Invalidate(const Region
& layer_invalidation
);
78 void SetRasterSourceOnTiles();
79 void CreateMissingTilesInLiveTilesRect();
81 void CloneTilesAndPropertiesFrom(const PictureLayerTiling
& twin_tiling
);
83 void set_resolution(TileResolution resolution
) { resolution_
= resolution
; }
84 TileResolution
resolution() const { return resolution_
; }
85 void set_can_require_tiles_for_activation(bool can_require_tiles
) {
86 can_require_tiles_for_activation_
= can_require_tiles
;
89 RasterSource
* raster_source() const { return raster_source_
.get(); }
90 gfx::Size
tiling_size() const { return tiling_data_
.tiling_size(); }
91 gfx::Rect
live_tiles_rect() const { return live_tiles_rect_
; }
92 gfx::Size
tile_size() const { return tiling_data_
.max_texture_size(); }
93 float contents_scale() const { return contents_scale_
; }
95 Tile
* TileAt(int i
, int j
) const {
96 TileMap::const_iterator iter
= tiles_
.find(TileMapKey(i
, j
));
97 return (iter
== tiles_
.end()) ? NULL
: iter
->second
.get();
100 void CreateAllTilesForTesting() {
101 SetLiveTilesRect(gfx::Rect(tiling_data_
.tiling_size()));
104 const TilingData
& TilingDataForTesting() const { return tiling_data_
; }
106 std::vector
<Tile
*> AllTilesForTesting() const {
107 std::vector
<Tile
*> all_tiles
;
108 for (TileMap::const_iterator it
= tiles_
.begin(); it
!= tiles_
.end(); ++it
)
109 all_tiles
.push_back(it
->second
.get());
113 void UpdateAllTilePrioritiesForTesting() {
114 for (TileMap::const_iterator it
= tiles_
.begin(); it
!= tiles_
.end(); ++it
)
115 UpdateTileAndTwinPriority(it
->second
.get());
118 std::vector
<scoped_refptr
<Tile
>> AllRefTilesForTesting() const {
119 std::vector
<scoped_refptr
<Tile
>> all_tiles
;
120 for (TileMap::const_iterator it
= tiles_
.begin(); it
!= tiles_
.end(); ++it
)
121 all_tiles
.push_back(it
->second
);
125 void SetAllTilesOccludedForTesting() {
126 gfx::Rect viewport_in_layer_space
=
127 ScaleToEnclosingRect(current_visible_rect_
, 1.0f
/ contents_scale_
);
128 current_occlusion_in_layer_space_
=
129 Occlusion(gfx::Transform(),
130 SimpleEnclosedRegion(viewport_in_layer_space
),
131 SimpleEnclosedRegion(viewport_in_layer_space
));
134 const gfx::Rect
& GetCurrentVisibleRectForTesting() const {
135 return current_visible_rect_
;
138 bool IsTileOccluded(const Tile
* tile
) const;
139 bool IsTileRequiredForActivationIfVisible(const Tile
* tile
) const;
140 bool IsTileRequiredForDrawIfVisible(const Tile
* tile
) const;
142 void UpdateTileAndTwinPriority(Tile
* tile
) const;
143 TilePriority
ComputePriorityForTile(const Tile
* tile
) const;
144 void UpdateRequiredStateForTile(Tile
* tile
, WhichTree tree
) const;
145 bool has_visible_rect_tiles() const { return has_visible_rect_tiles_
; }
146 bool has_skewport_rect_tiles() const { return has_skewport_rect_tiles_
; }
147 bool has_soon_border_rect_tiles() const {
148 return has_soon_border_rect_tiles_
;
150 bool has_eventually_rect_tiles() const { return has_eventually_rect_tiles_
; }
152 const gfx::Rect
& current_visible_rect() const {
153 return current_visible_rect_
;
155 const gfx::Rect
& current_skewport_rect() const {
156 return current_skewport_rect_
;
158 const gfx::Rect
& current_soon_border_rect() const {
159 return current_soon_border_rect_
;
161 const gfx::Rect
& current_eventually_rect() const {
162 return current_eventually_rect_
;
164 void VerifyAllTilesHaveCurrentRasterSource() const;
166 // Iterate over all tiles to fill content_rect. Even if tiles are invalid
167 // (i.e. no valid resource) this tiling should still iterate over them.
168 // The union of all geometry_rect calls for each element iterated over should
169 // exactly equal content_rect and no two geometry_rects should intersect.
170 class CC_EXPORT CoverageIterator
{
173 CoverageIterator(const PictureLayerTiling
* tiling
,
175 const gfx::Rect
& rect
);
178 // Visible rect (no borders), always in the space of content_rect,
179 // regardless of the contents scale of the tiling.
180 gfx::Rect
geometry_rect() const;
181 // Texture rect (in texels) for geometry_rect
182 gfx::RectF
texture_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 bool ComputeTilePriorityRects(const gfx::Rect
& viewport_in_layer_space
,
213 float ideal_contents_scale
,
214 double current_frame_time_in_seconds
,
215 const Occlusion
& occlusion_in_layer_space
);
217 void GetAllTilesAndPrioritiesForTracing(
218 std::map
<const Tile
*, TilePriority
>* tile_map
) const;
219 void AsValueInto(base::trace_event::TracedValue
* array
) const;
220 size_t GPUMemoryUsageInBytes() const;
222 struct RectExpansionCache
{
223 RectExpansionCache();
225 gfx::Rect previous_start
;
226 gfx::Rect previous_bounds
;
227 gfx::Rect previous_result
;
228 int64 previous_target
;
232 gfx::Rect
ExpandRectEquallyToAreaBoundedBy(
233 const gfx::Rect
& starting_rect
,
235 const gfx::Rect
& bounding_rect
,
236 RectExpansionCache
* cache
);
238 bool has_ever_been_updated() const {
239 return visible_rect_history_
[0].frame_time_in_seconds
!= 0.0;
243 friend class CoverageIterator
;
244 friend class TilingSetRasterQueueAll
;
245 friend class TilingSetRasterQueueRequired
;
246 friend class TilingSetEvictionQueue
;
248 typedef std::pair
<int, int> TileMapKey
;
249 typedef base::hash_map
<TileMapKey
, scoped_refptr
<Tile
>> TileMap
;
251 struct FrameVisibleRect
{
252 gfx::Rect visible_rect_in_content_space
;
253 double frame_time_in_seconds
= 0.0;
256 PictureLayerTiling(float contents_scale
,
257 scoped_refptr
<RasterSource
> raster_source
,
258 PictureLayerTilingClient
* client
,
259 size_t max_tiles_for_interest_area
,
260 float skewport_target_time_in_seconds
,
261 int skewport_extrapolation_limit_in_content_pixels
);
262 void SetLiveTilesRect(const gfx::Rect
& live_tiles_rect
);
263 void VerifyLiveTilesRect(bool is_on_recycle_tree
) const;
264 Tile
* CreateTile(int i
,
266 const PictureLayerTiling
* twin_tiling
,
267 PictureLayerTiling
* recycled_twin
);
268 // Returns true if the Tile existed and was removed from the tiling.
269 bool RemoveTileAt(int i
, int j
, PictureLayerTiling
* recycled_twin
);
271 // Computes a skewport. The calculation extrapolates the last visible
272 // rect and the current visible rect to expand the skewport to where it
273 // would be in |skewport_target_time| seconds. Note that the skewport
274 // is guaranteed to contain the current visible rect.
275 gfx::Rect
ComputeSkewport(double current_frame_time_in_seconds
,
276 const gfx::Rect
& visible_rect_in_content_space
)
279 // Save the required data for computing tile priorities later.
280 void UpdateTilePriorityRects(float content_to_screen_scale_
,
281 const gfx::Rect
& visible_rect_in_content_space
,
282 const gfx::Rect
& skewport
,
283 const gfx::Rect
& soon_border_rect
,
284 const gfx::Rect
& eventually_rect
,
285 const Occlusion
& occlusion_in_layer_space
);
287 void UpdateTilePriorityForTree(Tile
* tile
, WhichTree tree
) const;
288 bool NeedsUpdateForFrameAtTimeAndViewport(
289 double frame_time_in_seconds
,
290 const gfx::Rect
& viewport_in_layer_space
) {
291 return frame_time_in_seconds
!=
292 visible_rect_history_
[0].frame_time_in_seconds
||
293 viewport_in_layer_space
!= last_viewport_in_layer_space_
;
295 void UpdateVisibleRectHistory(
296 double frame_time_in_seconds
,
297 const gfx::Rect
& visible_rect_in_content_space
) {
298 visible_rect_history_
[1] = visible_rect_history_
[0];
299 visible_rect_history_
[0].frame_time_in_seconds
= frame_time_in_seconds
;
300 visible_rect_history_
[0].visible_rect_in_content_space
=
301 visible_rect_in_content_space
;
302 // If we don't have a second history item, set it to the most recent one.
303 if (visible_rect_history_
[1].frame_time_in_seconds
== 0.0)
304 visible_rect_history_
[1] = visible_rect_history_
[0];
307 const size_t max_tiles_for_interest_area_
;
308 const float skewport_target_time_in_seconds_
;
309 const int skewport_extrapolation_limit_in_content_pixels_
;
312 const float contents_scale_
;
313 PictureLayerTilingClient
* const client_
;
314 scoped_refptr
<RasterSource
> raster_source_
;
315 TileResolution resolution_
;
318 TilingData tiling_data_
;
319 TileMap tiles_
; // It is not legal to have a NULL tile in the tiles_ map.
320 gfx::Rect live_tiles_rect_
;
322 gfx::Rect last_viewport_in_layer_space_
;
323 // State saved for computing velocities based upon finite differences.
324 FrameVisibleRect visible_rect_history_
[2];
326 bool can_require_tiles_for_activation_
;
328 // Iteration rects in content space.
329 gfx::Rect current_visible_rect_
;
330 gfx::Rect current_skewport_rect_
;
331 gfx::Rect current_soon_border_rect_
;
332 gfx::Rect current_eventually_rect_
;
333 // Other properties used for tile iteration and prioritization.
334 float current_content_to_screen_scale_
;
335 Occlusion current_occlusion_in_layer_space_
;
337 bool has_visible_rect_tiles_
;
338 bool has_skewport_rect_tiles_
;
339 bool has_soon_border_rect_tiles_
;
340 bool has_eventually_rect_tiles_
;
343 DISALLOW_ASSIGN(PictureLayerTiling
);
345 RectExpansionCache expansion_cache_
;
350 #endif // CC_RESOURCES_PICTURE_LAYER_TILING_H_