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
{
28 // TODO(ssid): remove these aliases after the tracing clients are moved to the
29 // new trace_event namespace. See crbug.com/451032. ETA: March 2015
31 using ::base::trace_event::TracedValue
;
37 class PictureLayerTiling
;
40 class CC_EXPORT PictureLayerTilingClient
{
42 // Create a tile at the given content_rect (in the contents scale of the
43 // tiling) This might return null if the client cannot create such a tile.
44 virtual scoped_refptr
<Tile
> CreateTile(float contents_scale
,
45 const gfx::Rect
& content_rect
) = 0;
46 virtual gfx::Size
CalculateTileSize(
47 const gfx::Size
& content_bounds
) const = 0;
48 // This invalidation region defines the area (if any, it can by null) that
49 // tiles can not be shared between pending and active trees.
50 virtual const Region
* GetPendingInvalidation() = 0;
51 virtual const PictureLayerTiling
* GetPendingOrActiveTwinTiling(
52 const PictureLayerTiling
* tiling
) const = 0;
53 virtual PictureLayerTiling
* GetRecycledTwinTiling(
54 const PictureLayerTiling
* tiling
) = 0;
55 virtual TilePriority::PriorityBin
GetMaxTilePriorityBin() const = 0;
56 virtual WhichTree
GetTree() const = 0;
57 virtual bool RequiresHighResToDraw() const = 0;
60 virtual ~PictureLayerTilingClient() {}
63 class CC_EXPORT PictureLayerTiling
{
65 static const int kBorderTexels
= 1;
67 ~PictureLayerTiling();
69 // Create a tiling with no tiles. CreateTile() must be called to add some.
70 static scoped_ptr
<PictureLayerTiling
> Create(
72 scoped_refptr
<RasterSource
> raster_source
,
73 PictureLayerTilingClient
* client
,
74 size_t max_tiles_for_interest_area
,
75 float skewport_target_time_in_seconds
,
76 int skewport_extrapolation_limit_in_content_pixels
);
78 void SetRasterSourceAndResize(scoped_refptr
<RasterSource
> raster_source
);
79 void Invalidate(const Region
& layer_invalidation
);
80 void SetRasterSourceOnTiles();
81 void CreateMissingTilesInLiveTilesRect();
83 void CloneTilesAndPropertiesFrom(const PictureLayerTiling
& twin_tiling
);
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_
; }
97 Tile
* TileAt(int i
, int j
) const {
98 TileMap::const_iterator iter
= tiles_
.find(TileMapKey(i
, j
));
99 return (iter
== tiles_
.end()) ? NULL
: iter
->second
.get();
102 void CreateAllTilesForTesting() {
103 SetLiveTilesRect(gfx::Rect(tiling_data_
.tiling_size()));
106 const TilingData
& TilingDataForTesting() const { return tiling_data_
; }
108 std::vector
<Tile
*> AllTilesForTesting() const {
109 std::vector
<Tile
*> all_tiles
;
110 for (TileMap::const_iterator it
= tiles_
.begin(); it
!= tiles_
.end(); ++it
)
111 all_tiles
.push_back(it
->second
.get());
115 void UpdateAllTilePrioritiesForTesting() {
116 for (TileMap::const_iterator it
= tiles_
.begin(); it
!= tiles_
.end(); ++it
)
117 UpdateTileAndTwinPriority(it
->second
.get());
120 std::vector
<scoped_refptr
<Tile
>> AllRefTilesForTesting() const {
121 std::vector
<scoped_refptr
<Tile
>> all_tiles
;
122 for (TileMap::const_iterator it
= tiles_
.begin(); it
!= tiles_
.end(); ++it
)
123 all_tiles
.push_back(it
->second
);
127 void SetAllTilesOccludedForTesting() {
128 gfx::Rect viewport_in_layer_space
=
129 ScaleToEnclosingRect(current_visible_rect_
, 1.0f
/ contents_scale_
);
130 current_occlusion_in_layer_space_
=
131 Occlusion(gfx::Transform(),
132 SimpleEnclosedRegion(viewport_in_layer_space
),
133 SimpleEnclosedRegion(viewport_in_layer_space
));
136 const gfx::Rect
& GetCurrentVisibleRectForTesting() const {
137 return current_visible_rect_
;
140 bool IsTileOccluded(const Tile
* tile
) const;
141 bool IsTileRequiredForActivationIfVisible(const Tile
* tile
) const;
142 bool IsTileRequiredForDrawIfVisible(const Tile
* tile
) const;
144 void UpdateTileAndTwinPriority(Tile
* tile
) 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_
;
165 // Iterate over all tiles to fill content_rect. Even if tiles are invalid
166 // (i.e. no valid resource) this tiling should still iterate over them.
167 // The union of all geometry_rect calls for each element iterated over should
168 // exactly equal content_rect and no two geometry_rects should intersect.
169 class CC_EXPORT CoverageIterator
{
172 CoverageIterator(const PictureLayerTiling
* tiling
,
174 const gfx::Rect
& rect
);
177 // Visible rect (no borders), always in the space of content_rect,
178 // regardless of the contents scale of the tiling.
179 gfx::Rect
geometry_rect() const;
180 // Texture rect (in texels) for geometry_rect
181 gfx::RectF
texture_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_
; }
193 const PictureLayerTiling
* tiling_
;
194 gfx::Rect dest_rect_
;
195 float dest_to_content_scale_
;
198 gfx::Rect current_geometry_rect_
;
206 friend class PictureLayerTiling
;
211 bool ComputeTilePriorityRects(const gfx::Rect
& viewport_in_layer_space
,
212 float ideal_contents_scale
,
213 double current_frame_time_in_seconds
,
214 const Occlusion
& occlusion_in_layer_space
);
216 void GetAllTilesForTracing(std::set
<const Tile
*>* tiles
) const;
217 void AsValueInto(base::debug::TracedValue
* array
) const;
218 size_t GPUMemoryUsageInBytes() const;
220 struct RectExpansionCache
{
221 RectExpansionCache();
223 gfx::Rect previous_start
;
224 gfx::Rect previous_bounds
;
225 gfx::Rect previous_result
;
226 int64 previous_target
;
230 gfx::Rect
ExpandRectEquallyToAreaBoundedBy(
231 const gfx::Rect
& starting_rect
,
233 const gfx::Rect
& bounding_rect
,
234 RectExpansionCache
* cache
);
236 bool has_ever_been_updated() const {
237 return last_impl_frame_time_in_seconds_
!= 0.0;
241 friend class CoverageIterator
;
242 friend class TilingSetRasterQueueAll
;
243 friend class TilingSetRasterQueueRequired
;
244 friend class TilingSetEvictionQueue
;
246 typedef std::pair
<int, int> TileMapKey
;
247 typedef base::hash_map
<TileMapKey
, scoped_refptr
<Tile
>> TileMap
;
249 PictureLayerTiling(float contents_scale
,
250 scoped_refptr
<RasterSource
> raster_source
,
251 PictureLayerTilingClient
* client
,
252 size_t max_tiles_for_interest_area
,
253 float skewport_target_time_in_seconds
,
254 int skewport_extrapolation_limit_in_content_pixels
);
255 void SetLiveTilesRect(const gfx::Rect
& live_tiles_rect
);
256 void VerifyLiveTilesRect(bool is_on_recycle_tree
) const;
257 Tile
* CreateTile(int i
,
259 const PictureLayerTiling
* twin_tiling
,
260 PictureLayerTiling
* recycled_twin
);
261 // Returns true if the Tile existed and was removed from the tiling.
262 bool RemoveTileAt(int i
, int j
, PictureLayerTiling
* recycled_twin
);
264 // Computes a skewport. The calculation extrapolates the last visible
265 // rect and the current visible rect to expand the skewport to where it
266 // would be in |skewport_target_time| seconds. Note that the skewport
267 // is guaranteed to contain the current visible rect.
268 gfx::Rect
ComputeSkewport(double current_frame_time_in_seconds
,
269 const gfx::Rect
& visible_rect_in_content_space
)
272 // Save the required data for computing tile priorities later.
273 void UpdateTilePriorityRects(float content_to_screen_scale_
,
274 const gfx::Rect
& visible_rect_in_content_space
,
275 const gfx::Rect
& skewport
,
276 const gfx::Rect
& soon_border_rect
,
277 const gfx::Rect
& eventually_rect
,
278 const Occlusion
& occlusion_in_layer_space
);
280 void UpdateTilePriorityForTree(Tile
* tile
, WhichTree tree
) const;
281 bool NeedsUpdateForFrameAtTimeAndViewport(
282 double frame_time_in_seconds
,
283 const gfx::Rect
& viewport_in_layer_space
) {
284 return frame_time_in_seconds
!= last_impl_frame_time_in_seconds_
||
285 viewport_in_layer_space
!= last_viewport_in_layer_space_
;
288 const size_t max_tiles_for_interest_area_
;
289 const float skewport_target_time_in_seconds_
;
290 const int skewport_extrapolation_limit_in_content_pixels_
;
293 const float contents_scale_
;
294 PictureLayerTilingClient
* const client_
;
295 scoped_refptr
<RasterSource
> raster_source_
;
296 TileResolution resolution_
;
299 TilingData tiling_data_
;
300 TileMap tiles_
; // It is not legal to have a NULL tile in the tiles_ map.
301 gfx::Rect live_tiles_rect_
;
303 // State saved for computing velocities based upon finite differences.
304 double last_impl_frame_time_in_seconds_
;
305 gfx::Rect last_viewport_in_layer_space_
;
306 gfx::Rect last_visible_rect_in_content_space_
;
308 bool can_require_tiles_for_activation_
;
310 // Iteration rects in content space.
311 gfx::Rect current_visible_rect_
;
312 gfx::Rect current_skewport_rect_
;
313 gfx::Rect current_soon_border_rect_
;
314 gfx::Rect current_eventually_rect_
;
315 // Other properties used for tile iteration and prioritization.
316 float current_content_to_screen_scale_
;
317 Occlusion current_occlusion_in_layer_space_
;
319 bool has_visible_rect_tiles_
;
320 bool has_skewport_rect_tiles_
;
321 bool has_soon_border_rect_tiles_
;
322 bool has_eventually_rect_tiles_
;
325 DISALLOW_ASSIGN(PictureLayerTiling
);
327 RectExpansionCache expansion_cache_
;
332 #endif // CC_RESOURCES_PICTURE_LAYER_TILING_H_