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/scoped_ptr_hash_map.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 ScopedTilePtr
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 TilePriority::PriorityBin
GetMaxTilePriorityBin() const = 0;
48 virtual bool RequiresHighResToDraw() const = 0;
51 virtual ~PictureLayerTilingClient() {}
54 class CC_EXPORT PictureLayerTiling
{
56 static const int kBorderTexels
= 1;
58 PictureLayerTilingClient
* client() const { return client_
; }
59 ~PictureLayerTiling();
61 static float CalculateSoonBorderDistance(
62 const gfx::Rect
& visible_rect_in_content_space
,
63 float content_to_screen_scale
);
65 // Create a tiling with no tiles. CreateTile() must be called to add some.
66 static scoped_ptr
<PictureLayerTiling
> Create(
69 scoped_refptr
<RasterSource
> raster_source
,
70 PictureLayerTilingClient
* client
,
71 size_t max_tiles_for_interest_area
,
72 float skewport_target_time_in_seconds
,
73 int skewport_extrapolation_limit_in_content_pixels
);
75 void SetRasterSourceAndResize(scoped_refptr
<RasterSource
> raster_source
);
76 void Invalidate(const Region
& layer_invalidation
);
77 void SetRasterSourceOnTiles();
78 void CreateMissingTilesInLiveTilesRect();
79 void TakeTilesAndPropertiesFrom(PictureLayerTiling
* pending_twin
,
80 const Region
& layer_invalidation
);
82 bool IsTileRequiredForActivation(const Tile
* tile
) const;
83 bool IsTileRequiredForDraw(const Tile
* tile
) const;
85 void set_resolution(TileResolution resolution
) { resolution_
= resolution
; }
86 TileResolution
resolution() const { return resolution_
; }
87 void set_can_require_tiles_for_activation(bool can_require_tiles
) {
88 can_require_tiles_for_activation_
= can_require_tiles
;
91 RasterSource
* raster_source() const { return raster_source_
.get(); }
92 gfx::Size
tiling_size() const { return tiling_data_
.tiling_size(); }
93 gfx::Rect
live_tiles_rect() const { return live_tiles_rect_
; }
94 gfx::Size
tile_size() const { return tiling_data_
.max_texture_size(); }
95 float contents_scale() const { return contents_scale_
; }
96 const TilingData
* tiling_data() const { return &tiling_data_
; }
98 Tile
* TileAt(int i
, int j
) const {
99 TileMap::const_iterator iter
= tiles_
.find(TileMapKey(i
, j
));
100 return iter
== tiles_
.end() ? nullptr : iter
->second
;
103 bool has_tiles() const { return !tiles_
.empty(); }
105 // For testing functionality.
106 void CreateAllTilesForTesting() {
107 SetLiveTilesRect(gfx::Rect(tiling_data_
.tiling_size()));
109 const TilingData
& TilingDataForTesting() const { return tiling_data_
; }
110 std::vector
<Tile
*> AllTilesForTesting() const {
111 std::vector
<Tile
*> all_tiles
;
112 for (TileMap::const_iterator it
= tiles_
.begin(); it
!= tiles_
.end(); ++it
)
113 all_tiles
.push_back(it
->second
);
116 void UpdateAllTilePrioritiesForTesting() {
117 for (TileMap::const_iterator it
= tiles_
.begin(); it
!= tiles_
.end(); ++it
)
118 UpdateTilePriority(it
->second
);
120 void SetAllTilesOccludedForTesting() {
121 gfx::Rect viewport_in_layer_space
=
122 ScaleToEnclosingRect(current_visible_rect_
, 1.0f
/ contents_scale_
);
123 current_occlusion_in_layer_space_
=
124 Occlusion(gfx::Transform(),
125 SimpleEnclosedRegion(viewport_in_layer_space
),
126 SimpleEnclosedRegion(viewport_in_layer_space
));
128 const gfx::Rect
& GetCurrentVisibleRectForTesting() const {
129 return current_visible_rect_
;
132 void VerifyAllTilesHaveCurrentRasterSource() const;
134 // Iterate over all tiles to fill content_rect. Even if tiles are invalid
135 // (i.e. no valid resource) this tiling should still iterate over them.
136 // The union of all geometry_rect calls for each element iterated over should
137 // exactly equal content_rect and no two geometry_rects should intersect.
138 class CC_EXPORT CoverageIterator
{
141 CoverageIterator(const PictureLayerTiling
* tiling
,
143 const gfx::Rect
& rect
);
146 // Visible rect (no borders), always in the space of content_rect,
147 // regardless of the contents scale of the tiling.
148 gfx::Rect
geometry_rect() const;
149 // Texture rect (in texels) for geometry_rect
150 gfx::RectF
texture_rect() const;
152 Tile
* operator->() const { return current_tile_
; }
153 Tile
* operator*() const { return current_tile_
; }
155 CoverageIterator
& operator++();
156 operator bool() const { return tile_j_
<= bottom_
; }
158 int i() const { return tile_i_
; }
159 int j() const { return tile_j_
; }
162 const PictureLayerTiling
* tiling_
;
163 gfx::Rect dest_rect_
;
164 float dest_to_content_scale_
;
167 gfx::Rect current_geometry_rect_
;
175 friend class PictureLayerTiling
;
180 bool ComputeTilePriorityRects(const gfx::Rect
& viewport_in_layer_space
,
181 float ideal_contents_scale
,
182 double current_frame_time_in_seconds
,
183 const Occlusion
& occlusion_in_layer_space
);
185 void GetAllTilesAndPrioritiesForTracing(
186 std::map
<const Tile
*, TilePriority
>* tile_map
) const;
187 void AsValueInto(base::trace_event::TracedValue
* array
) const;
188 size_t GPUMemoryUsageInBytes() const;
190 struct RectExpansionCache
{
191 RectExpansionCache();
193 gfx::Rect previous_start
;
194 gfx::Rect previous_bounds
;
195 gfx::Rect previous_result
;
196 int64 previous_target
;
200 gfx::Rect
ExpandRectEquallyToAreaBoundedBy(
201 const gfx::Rect
& starting_rect
,
203 const gfx::Rect
& bounding_rect
,
204 RectExpansionCache
* cache
);
207 friend class CoverageIterator
;
208 friend class TilingSetRasterQueueAll
;
209 friend class TilingSetRasterQueueRequired
;
210 friend class TilingSetEvictionQueue
;
212 using TileMapKey
= std::pair
<int, int>;
213 using TileMap
= base::ScopedPtrHashMap
<TileMapKey
, ScopedTilePtr
>;
215 struct FrameVisibleRect
{
216 gfx::Rect visible_rect_in_content_space
;
217 double frame_time_in_seconds
= 0.0;
220 PictureLayerTiling(WhichTree tree
,
221 float contents_scale
,
222 scoped_refptr
<RasterSource
> raster_source
,
223 PictureLayerTilingClient
* client
,
224 size_t max_tiles_for_interest_area
,
225 float skewport_target_time_in_seconds
,
226 int skewport_extrapolation_limit_in_content_pixels
);
227 void SetLiveTilesRect(const gfx::Rect
& live_tiles_rect
);
228 void VerifyLiveTilesRect(bool is_on_recycle_tree
) const;
229 Tile
* CreateTile(int i
, int j
);
230 // Returns true if the Tile existed and was removed from the tiling.
231 bool RemoveTileAt(int i
, int j
);
232 bool TilingMatchesTileIndices(const PictureLayerTiling
* twin
) const;
234 // Computes a skewport. The calculation extrapolates the last visible
235 // rect and the current visible rect to expand the skewport to where it
236 // would be in |skewport_target_time| seconds. Note that the skewport
237 // is guaranteed to contain the current visible rect.
238 gfx::Rect
ComputeSkewport(double current_frame_time_in_seconds
,
239 const gfx::Rect
& visible_rect_in_content_space
)
242 // Save the required data for computing tile priorities later.
243 void SetTilePriorityRects(float content_to_screen_scale_
,
244 const gfx::Rect
& visible_rect_in_content_space
,
245 const gfx::Rect
& skewport
,
246 const gfx::Rect
& soon_border_rect
,
247 const gfx::Rect
& eventually_rect
,
248 const Occlusion
& occlusion_in_layer_space
);
250 bool NeedsUpdateForFrameAtTimeAndViewport(
251 double frame_time_in_seconds
,
252 const gfx::Rect
& viewport_in_layer_space
) {
253 return frame_time_in_seconds
!=
254 visible_rect_history_
[0].frame_time_in_seconds
||
255 viewport_in_layer_space
!= last_viewport_in_layer_space_
;
257 void UpdateVisibleRectHistory(
258 double frame_time_in_seconds
,
259 const gfx::Rect
& visible_rect_in_content_space
) {
260 visible_rect_history_
[1] = visible_rect_history_
[0];
261 visible_rect_history_
[0].frame_time_in_seconds
= frame_time_in_seconds
;
262 visible_rect_history_
[0].visible_rect_in_content_space
=
263 visible_rect_in_content_space
;
264 // If we don't have a second history item, set it to the most recent one.
265 if (visible_rect_history_
[1].frame_time_in_seconds
== 0.0)
266 visible_rect_history_
[1] = visible_rect_history_
[0];
268 bool IsTileOccludedOnCurrentTree(const Tile
* tile
) const;
269 bool ShouldCreateTileAt(int i
, int j
) const;
270 bool IsTileOccluded(const Tile
* tile
) const;
271 void UpdateTilePriority(Tile
* tile
) const;
272 TilePriority
ComputePriorityForTile(const Tile
* tile
) const;
273 bool has_visible_rect_tiles() const { return has_visible_rect_tiles_
; }
274 bool has_skewport_rect_tiles() const { return has_skewport_rect_tiles_
; }
275 bool has_soon_border_rect_tiles() const {
276 return has_soon_border_rect_tiles_
;
278 bool has_eventually_rect_tiles() const { return has_eventually_rect_tiles_
; }
280 const gfx::Rect
& current_visible_rect() const {
281 return current_visible_rect_
;
283 gfx::Rect
pending_visible_rect() const {
284 const PictureLayerTiling
* pending_tiling
=
285 tree_
== ACTIVE_TREE
? client_
->GetPendingOrActiveTwinTiling(this)
288 return pending_tiling
->current_visible_rect();
291 const gfx::Rect
& current_skewport_rect() const {
292 return current_skewport_rect_
;
294 const gfx::Rect
& current_soon_border_rect() const {
295 return current_soon_border_rect_
;
297 const gfx::Rect
& current_eventually_rect() const {
298 return current_eventually_rect_
;
300 bool has_ever_been_updated() const {
301 return visible_rect_history_
[0].frame_time_in_seconds
!= 0.0;
303 void RemoveTilesInRegion(const Region
& layer_region
, bool recreate_tiles
);
305 const size_t max_tiles_for_interest_area_
;
306 const float skewport_target_time_in_seconds_
;
307 const int skewport_extrapolation_limit_in_content_pixels_
;
310 const float contents_scale_
;
311 PictureLayerTilingClient
* const client_
;
312 const WhichTree tree_
;
313 scoped_refptr
<RasterSource
> raster_source_
;
314 TileResolution resolution_
;
317 TilingData tiling_data_
;
318 TileMap tiles_
; // It is not legal to have a NULL tile in the tiles_ map.
319 gfx::Rect live_tiles_rect_
;
321 gfx::Rect last_viewport_in_layer_space_
;
322 // State saved for computing velocities based upon finite differences.
323 FrameVisibleRect visible_rect_history_
[2];
325 bool can_require_tiles_for_activation_
;
327 // Iteration rects in content space.
328 gfx::Rect current_visible_rect_
;
329 gfx::Rect current_skewport_rect_
;
330 gfx::Rect current_soon_border_rect_
;
331 gfx::Rect current_eventually_rect_
;
332 // Other properties used for tile iteration and prioritization.
333 float current_content_to_screen_scale_
;
334 Occlusion current_occlusion_in_layer_space_
;
336 bool has_visible_rect_tiles_
;
337 bool has_skewport_rect_tiles_
;
338 bool has_soon_border_rect_tiles_
;
339 bool has_eventually_rect_tiles_
;
342 DISALLOW_ASSIGN(PictureLayerTiling
);
344 RectExpansionCache expansion_cache_
;
349 #endif // CC_RESOURCES_PICTURE_LAYER_TILING_H_