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_TILES_PICTURE_LAYER_TILING_H_
6 #define CC_TILES_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/tiles/tile.h"
19 #include "cc/tiles/tile_priority.h"
20 #include "cc/trees/occlusion.h"
21 #include "ui/gfx/geometry/rect.h"
24 namespace trace_event
{
31 class PictureLayerTiling
;
32 class PrioritizedTile
;
35 class CC_EXPORT PictureLayerTilingClient
{
37 // Create a tile at the given content_rect (in the contents scale of the
38 // tiling) This might return null if the client cannot create such a tile.
39 virtual ScopedTilePtr
CreateTile(float contents_scale
,
40 const gfx::Rect
& content_rect
) = 0;
41 virtual gfx::Size
CalculateTileSize(
42 const gfx::Size
& content_bounds
) const = 0;
43 // This invalidation region defines the area (if any, it can by null) that
44 // tiles can not be shared between pending and active trees.
45 virtual const Region
* GetPendingInvalidation() = 0;
46 virtual const PictureLayerTiling
* GetPendingOrActiveTwinTiling(
47 const PictureLayerTiling
* tiling
) const = 0;
48 virtual TilePriority::PriorityBin
GetMaxTilePriorityBin() const = 0;
49 virtual bool RequiresHighResToDraw() const = 0;
52 virtual ~PictureLayerTilingClient() {}
56 TileMapKey(int x
, int y
) : index_x(x
), index_y(y
) {}
57 explicit TileMapKey(const std::pair
<int, int>& index
)
58 : index_x(index
.first
), index_y(index
.second
) {}
60 bool operator==(const TileMapKey
& other
) const {
61 return index_x
== other
.index_x
&& index_y
== other
.index_y
;
70 namespace BASE_HASH_NAMESPACE
{
72 struct hash
<cc::TileMapKey
> {
73 size_t operator()(const cc::TileMapKey
& key
) const {
74 uint16 value1
= static_cast<uint16
>(key
.index_x
);
75 uint16 value2
= static_cast<uint16
>(key
.index_y
);
76 uint32 value1_32
= value1
;
77 return (value1_32
<< 16) | value2
;
80 } // namespace BASE_HASH_NAMESPACE
84 class CC_EXPORT PictureLayerTiling
{
86 static const int kBorderTexels
= 1;
88 PictureLayerTilingClient
* client() const { return client_
; }
89 ~PictureLayerTiling();
91 static float CalculateSoonBorderDistance(
92 const gfx::Rect
& visible_rect_in_content_space
,
93 float content_to_screen_scale
);
95 // Create a tiling with no tiles. CreateTile() must be called to add some.
96 static scoped_ptr
<PictureLayerTiling
> Create(
99 scoped_refptr
<RasterSource
> raster_source
,
100 PictureLayerTilingClient
* client
,
101 float tiling_interest_area_viewport_multiplier
,
102 float skewport_target_time_in_seconds
,
103 int skewport_extrapolation_limit_in_content_pixels
);
105 void SetRasterSourceAndResize(scoped_refptr
<RasterSource
> raster_source
);
106 void Invalidate(const Region
& layer_invalidation
);
107 void CreateMissingTilesInLiveTilesRect();
108 void TakeTilesAndPropertiesFrom(PictureLayerTiling
* pending_twin
,
109 const Region
& layer_invalidation
);
111 bool IsTileRequiredForActivation(const Tile
* tile
) const;
112 bool IsTileRequiredForDraw(const Tile
* tile
) const;
114 void set_resolution(TileResolution resolution
) { resolution_
= resolution
; }
115 TileResolution
resolution() const { return resolution_
; }
116 void set_can_require_tiles_for_activation(bool can_require_tiles
) {
117 can_require_tiles_for_activation_
= can_require_tiles
;
120 RasterSource
* raster_source() const { return raster_source_
.get(); }
121 gfx::Size
tiling_size() const { return tiling_data_
.tiling_size(); }
122 gfx::Rect
live_tiles_rect() const { return live_tiles_rect_
; }
123 gfx::Size
tile_size() const { return tiling_data_
.max_texture_size(); }
124 float contents_scale() const { return contents_scale_
; }
125 const TilingData
* tiling_data() const { return &tiling_data_
; }
127 Tile
* TileAt(int i
, int j
) const {
128 TileMap::const_iterator iter
= tiles_
.find(TileMapKey(i
, j
));
129 return iter
== tiles_
.end() ? nullptr : iter
->second
;
132 bool has_tiles() const { return !tiles_
.empty(); }
133 // all_tiles_done() can return false negatives.
134 bool all_tiles_done() const { return all_tiles_done_
; }
135 void set_all_tiles_done(bool all_tiles_done
) {
136 all_tiles_done_
= all_tiles_done
;
139 // For testing functionality.
140 void CreateAllTilesForTesting() {
141 SetLiveTilesRect(gfx::Rect(tiling_data_
.tiling_size()));
143 const TilingData
& TilingDataForTesting() const { return tiling_data_
; }
144 std::vector
<Tile
*> AllTilesForTesting() const {
145 std::vector
<Tile
*> all_tiles
;
146 for (TileMap::const_iterator it
= tiles_
.begin(); it
!= tiles_
.end(); ++it
)
147 all_tiles
.push_back(it
->second
);
151 void UpdateAllRequiredStateForTesting() {
152 for (const auto& key_tile_pair
: tiles_
)
153 UpdateRequiredStatesOnTile(key_tile_pair
.second
);
155 std::map
<const Tile
*, PrioritizedTile
>
156 UpdateAndGetAllPrioritizedTilesForTesting() const;
158 void SetAllTilesOccludedForTesting() {
159 gfx::Rect viewport_in_layer_space
=
160 ScaleToEnclosingRect(current_visible_rect_
, 1.0f
/ contents_scale_
);
161 current_occlusion_in_layer_space_
=
162 Occlusion(gfx::Transform(),
163 SimpleEnclosedRegion(viewport_in_layer_space
),
164 SimpleEnclosedRegion(viewport_in_layer_space
));
166 const gfx::Rect
& GetCurrentVisibleRectForTesting() const {
167 return current_visible_rect_
;
170 // Iterate over all tiles to fill content_rect. Even if tiles are invalid
171 // (i.e. no valid resource) this tiling should still iterate over them.
172 // The union of all geometry_rect calls for each element iterated over should
173 // exactly equal content_rect and no two geometry_rects should intersect.
174 class CC_EXPORT CoverageIterator
{
177 CoverageIterator(const PictureLayerTiling
* tiling
,
179 const gfx::Rect
& rect
);
182 // Visible rect (no borders), always in the space of content_rect,
183 // regardless of the contents scale of the tiling.
184 gfx::Rect
geometry_rect() const;
185 // Texture rect (in texels) for geometry_rect
186 gfx::RectF
texture_rect() const;
188 Tile
* operator->() const { return current_tile_
; }
189 Tile
* operator*() const { return current_tile_
; }
191 CoverageIterator
& operator++();
192 operator bool() const { return tile_j_
<= bottom_
; }
194 int i() const { return tile_i_
; }
195 int j() const { return tile_j_
; }
198 const PictureLayerTiling
* tiling_
;
199 gfx::Rect dest_rect_
;
200 float dest_to_content_scale_
;
203 gfx::Rect current_geometry_rect_
;
211 friend class PictureLayerTiling
;
216 bool ComputeTilePriorityRects(const gfx::Rect
& viewport_in_layer_space
,
217 float ideal_contents_scale
,
218 double current_frame_time_in_seconds
,
219 const Occlusion
& occlusion_in_layer_space
);
221 void GetAllPrioritizedTilesForTracing(
222 std::vector
<PrioritizedTile
>* prioritized_tiles
) const;
223 void AsValueInto(base::trace_event::TracedValue
* array
) const;
224 size_t GPUMemoryUsageInBytes() const;
226 struct RectExpansionCache
{
227 RectExpansionCache();
229 gfx::Rect previous_start
;
230 gfx::Rect previous_bounds
;
231 gfx::Rect previous_result
;
232 int64 previous_target
;
236 gfx::Rect
ExpandRectEquallyToAreaBoundedBy(
237 const gfx::Rect
& starting_rect
,
239 const gfx::Rect
& bounding_rect
,
240 RectExpansionCache
* cache
);
243 friend class CoverageIterator
;
244 friend class PrioritizedTile
;
245 friend class TilingSetRasterQueueAll
;
246 friend class TilingSetRasterQueueRequired
;
247 friend class TilingSetEvictionQueue
;
249 // PENDING VISIBLE RECT refers to the visible rect that will become current
250 // upon activation (ie, the pending tree's visible rect). Tiles in this
251 // region that are not part of the current visible rect are all handled
252 // here. Note that when processing a pending tree, this rect is the same as
253 // the visible rect so no tiles are processed in this case.
254 enum PriorityRectType
{
256 PENDING_VISIBLE_RECT
,
262 using TileMap
= base::ScopedPtrHashMap
<TileMapKey
, ScopedTilePtr
>;
264 struct FrameVisibleRect
{
265 gfx::Rect visible_rect_in_content_space
;
266 double frame_time_in_seconds
= 0.0;
269 PictureLayerTiling(WhichTree tree
,
270 float contents_scale
,
271 scoped_refptr
<RasterSource
> raster_source
,
272 PictureLayerTilingClient
* client
,
273 float tiling_interest_area_viewport_multiplier
,
274 float skewport_target_time_in_seconds
,
275 int skewport_extrapolation_limit_in_content_pixels
);
276 void SetLiveTilesRect(const gfx::Rect
& live_tiles_rect
);
277 void VerifyLiveTilesRect(bool is_on_recycle_tree
) const;
278 Tile
* CreateTile(int i
, int j
);
279 // Returns true if the Tile existed and was removed from the tiling.
280 bool RemoveTileAt(int i
, int j
);
281 bool TilingMatchesTileIndices(const PictureLayerTiling
* twin
) const;
283 // Computes a skewport. The calculation extrapolates the last visible
284 // rect and the current visible rect to expand the skewport to where it
285 // would be in |skewport_target_time| seconds. Note that the skewport
286 // is guaranteed to contain the current visible rect.
287 gfx::Rect
ComputeSkewport(double current_frame_time_in_seconds
,
288 const gfx::Rect
& visible_rect_in_content_space
)
291 // Save the required data for computing tile priorities later.
292 void SetTilePriorityRects(float content_to_screen_scale_
,
293 const gfx::Rect
& visible_rect_in_content_space
,
294 const gfx::Rect
& skewport
,
295 const gfx::Rect
& soon_border_rect
,
296 const gfx::Rect
& eventually_rect
,
297 const Occlusion
& occlusion_in_layer_space
);
299 bool NeedsUpdateForFrameAtTimeAndViewport(
300 double frame_time_in_seconds
,
301 const gfx::Rect
& viewport_in_layer_space
) {
302 return frame_time_in_seconds
!=
303 visible_rect_history_
[0].frame_time_in_seconds
||
304 viewport_in_layer_space
!= last_viewport_in_layer_space_
;
306 void UpdateVisibleRectHistory(
307 double frame_time_in_seconds
,
308 const gfx::Rect
& visible_rect_in_content_space
) {
309 visible_rect_history_
[1] = visible_rect_history_
[0];
310 visible_rect_history_
[0].frame_time_in_seconds
= frame_time_in_seconds
;
311 visible_rect_history_
[0].visible_rect_in_content_space
=
312 visible_rect_in_content_space
;
313 // If we don't have a second history item, set it to the most recent one.
314 if (visible_rect_history_
[1].frame_time_in_seconds
== 0.0)
315 visible_rect_history_
[1] = visible_rect_history_
[0];
317 bool IsTileOccludedOnCurrentTree(const Tile
* tile
) const;
318 bool ShouldCreateTileAt(int i
, int j
) const;
319 bool IsTileOccluded(const Tile
* tile
) const;
320 void UpdateRequiredStatesOnTile(Tile
* tile
) const;
321 PrioritizedTile
MakePrioritizedTile(
323 PriorityRectType priority_rect_type
) const;
324 TilePriority
ComputePriorityForTile(
326 PriorityRectType priority_rect_type
) const;
327 PriorityRectType
ComputePriorityRectTypeForTile(const Tile
* tile
) const;
328 bool has_visible_rect_tiles() const { return has_visible_rect_tiles_
; }
329 bool has_skewport_rect_tiles() const { return has_skewport_rect_tiles_
; }
330 bool has_soon_border_rect_tiles() const {
331 return has_soon_border_rect_tiles_
;
333 bool has_eventually_rect_tiles() const { return has_eventually_rect_tiles_
; }
335 const gfx::Rect
& current_visible_rect() const {
336 return current_visible_rect_
;
338 gfx::Rect
pending_visible_rect() const {
339 const PictureLayerTiling
* pending_tiling
=
340 tree_
== ACTIVE_TREE
? client_
->GetPendingOrActiveTwinTiling(this)
343 return pending_tiling
->current_visible_rect();
346 const gfx::Rect
& current_skewport_rect() const {
347 return current_skewport_rect_
;
349 const gfx::Rect
& current_soon_border_rect() const {
350 return current_soon_border_rect_
;
352 const gfx::Rect
& current_eventually_rect() const {
353 return current_eventually_rect_
;
355 bool has_ever_been_updated() const {
356 return visible_rect_history_
[0].frame_time_in_seconds
!= 0.0;
358 void RemoveTilesInRegion(const Region
& layer_region
, bool recreate_tiles
);
360 const float tiling_interest_area_viewport_multiplier_
;
361 const float skewport_target_time_in_seconds_
;
362 const int skewport_extrapolation_limit_in_content_pixels_
;
365 const float contents_scale_
;
366 PictureLayerTilingClient
* const client_
;
367 const WhichTree tree_
;
368 scoped_refptr
<RasterSource
> raster_source_
;
369 TileResolution resolution_
;
372 TilingData tiling_data_
;
373 TileMap tiles_
; // It is not legal to have a NULL tile in the tiles_ map.
374 gfx::Rect live_tiles_rect_
;
376 gfx::Rect last_viewport_in_layer_space_
;
377 // State saved for computing velocities based upon finite differences.
378 FrameVisibleRect visible_rect_history_
[2];
380 bool can_require_tiles_for_activation_
;
382 // Iteration rects in content space.
383 gfx::Rect current_visible_rect_
;
384 gfx::Rect current_skewport_rect_
;
385 gfx::Rect current_soon_border_rect_
;
386 gfx::Rect current_eventually_rect_
;
387 // Other properties used for tile iteration and prioritization.
388 float current_content_to_screen_scale_
;
389 Occlusion current_occlusion_in_layer_space_
;
391 bool has_visible_rect_tiles_
;
392 bool has_skewport_rect_tiles_
;
393 bool has_soon_border_rect_tiles_
;
394 bool has_eventually_rect_tiles_
;
395 bool all_tiles_done_
;
398 DISALLOW_ASSIGN(PictureLayerTiling
);
400 RectExpansionCache expansion_cache_
;
405 #endif // CC_TILES_PICTURE_LAYER_TILING_H_