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 bool HasValidTilePriorities() 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 size_t tiling_interest_area_padding
,
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
) {
115 resolution_
= resolution
;
116 may_contain_low_resolution_tiles_
|= resolution
== LOW_RESOLUTION
;
118 TileResolution
resolution() const { return resolution_
; }
119 bool may_contain_low_resolution_tiles() const {
120 return may_contain_low_resolution_tiles_
;
122 void reset_may_contain_low_resolution_tiles() {
123 may_contain_low_resolution_tiles_
= false;
125 void set_can_require_tiles_for_activation(bool can_require_tiles
) {
126 can_require_tiles_for_activation_
= can_require_tiles
;
129 RasterSource
* raster_source() const { return raster_source_
.get(); }
130 gfx::Size
tiling_size() const { return tiling_data_
.tiling_size(); }
131 gfx::Rect
live_tiles_rect() const { return live_tiles_rect_
; }
132 gfx::Size
tile_size() const { return tiling_data_
.max_texture_size(); }
133 float contents_scale() const { return contents_scale_
; }
134 const TilingData
* tiling_data() const { return &tiling_data_
; }
136 Tile
* TileAt(int i
, int j
) const {
137 TileMap::const_iterator iter
= tiles_
.find(TileMapKey(i
, j
));
138 return iter
== tiles_
.end() ? nullptr : iter
->second
;
141 bool has_tiles() const { return !tiles_
.empty(); }
142 // all_tiles_done() can return false negatives.
143 bool all_tiles_done() const { return all_tiles_done_
; }
144 void set_all_tiles_done(bool all_tiles_done
) {
145 all_tiles_done_
= all_tiles_done
;
148 void VerifyNoTileNeedsRaster() const {
150 for (const auto tile_pair
: tiles_
) {
151 DCHECK(!tile_pair
.second
->draw_info().NeedsRaster() ||
152 IsTileOccluded(tile_pair
.second
));
154 #endif // DCHECK_IS_ON()
157 // For testing functionality.
158 void CreateAllTilesForTesting() {
159 SetLiveTilesRect(gfx::Rect(tiling_data_
.tiling_size()));
161 const TilingData
& TilingDataForTesting() const { return tiling_data_
; }
162 std::vector
<Tile
*> AllTilesForTesting() const {
163 std::vector
<Tile
*> all_tiles
;
164 for (TileMap::const_iterator it
= tiles_
.begin(); it
!= tiles_
.end(); ++it
)
165 all_tiles
.push_back(it
->second
);
169 void UpdateAllRequiredStateForTesting() {
170 for (const auto& key_tile_pair
: tiles_
)
171 UpdateRequiredStatesOnTile(key_tile_pair
.second
);
173 std::map
<const Tile
*, PrioritizedTile
>
174 UpdateAndGetAllPrioritizedTilesForTesting() const;
176 void SetAllTilesOccludedForTesting() {
177 gfx::Rect viewport_in_layer_space
=
178 ScaleToEnclosingRect(current_visible_rect_
, 1.0f
/ contents_scale_
);
179 current_occlusion_in_layer_space_
=
180 Occlusion(gfx::Transform(),
181 SimpleEnclosedRegion(viewport_in_layer_space
),
182 SimpleEnclosedRegion(viewport_in_layer_space
));
184 const gfx::Rect
& GetCurrentVisibleRectForTesting() const {
185 return current_visible_rect_
;
187 void SetTilePriorityRectsForTesting(
188 const gfx::Rect
& visible_rect_in_content_space
,
189 const gfx::Rect
& skewport
,
190 const gfx::Rect
& soon_border_rect
,
191 const gfx::Rect
& eventually_rect
) {
192 SetTilePriorityRects(1.0f
, visible_rect_in_content_space
, skewport
,
193 soon_border_rect
, eventually_rect
, Occlusion());
196 // Iterate over all tiles to fill content_rect. Even if tiles are invalid
197 // (i.e. no valid resource) this tiling should still iterate over them.
198 // The union of all geometry_rect calls for each element iterated over should
199 // exactly equal content_rect and no two geometry_rects should intersect.
200 class CC_EXPORT CoverageIterator
{
203 CoverageIterator(const PictureLayerTiling
* tiling
,
205 const gfx::Rect
& rect
);
208 // Visible rect (no borders), always in the space of content_rect,
209 // regardless of the contents scale of the tiling.
210 gfx::Rect
geometry_rect() const;
211 // Texture rect (in texels) for geometry_rect
212 gfx::RectF
texture_rect() const;
214 Tile
* operator->() const { return current_tile_
; }
215 Tile
* operator*() const { return current_tile_
; }
217 CoverageIterator
& operator++();
218 operator bool() const { return tile_j_
<= bottom_
; }
220 int i() const { return tile_i_
; }
221 int j() const { return tile_j_
; }
224 const PictureLayerTiling
* tiling_
;
225 gfx::Rect dest_rect_
;
226 float dest_to_content_scale_
;
229 gfx::Rect current_geometry_rect_
;
237 friend class PictureLayerTiling
;
242 bool ComputeTilePriorityRects(const gfx::Rect
& viewport_in_layer_space
,
243 float ideal_contents_scale
,
244 double current_frame_time_in_seconds
,
245 const Occlusion
& occlusion_in_layer_space
);
247 void GetAllPrioritizedTilesForTracing(
248 std::vector
<PrioritizedTile
>* prioritized_tiles
) const;
249 void AsValueInto(base::trace_event::TracedValue
* array
) const;
250 size_t GPUMemoryUsageInBytes() const;
253 friend class CoverageIterator
;
254 friend class PrioritizedTile
;
255 friend class TilingSetRasterQueueAll
;
256 friend class TilingSetRasterQueueRequired
;
257 friend class TilingSetEvictionQueue
;
259 // PENDING VISIBLE RECT refers to the visible rect that will become current
260 // upon activation (ie, the pending tree's visible rect). Tiles in this
261 // region that are not part of the current visible rect are all handled
262 // here. Note that when processing a pending tree, this rect is the same as
263 // the visible rect so no tiles are processed in this case.
264 enum PriorityRectType
{
266 PENDING_VISIBLE_RECT
,
272 using TileMap
= base::ScopedPtrHashMap
<TileMapKey
, ScopedTilePtr
>;
274 struct FrameVisibleRect
{
275 gfx::Rect visible_rect_in_content_space
;
276 double frame_time_in_seconds
= 0.0;
279 PictureLayerTiling(WhichTree tree
,
280 float contents_scale
,
281 scoped_refptr
<RasterSource
> raster_source
,
282 PictureLayerTilingClient
* client
,
283 size_t tiling_interest_area_padding
,
284 float skewport_target_time_in_seconds
,
285 int skewport_extrapolation_limit_in_content_pixels
);
286 void SetLiveTilesRect(const gfx::Rect
& live_tiles_rect
);
287 void VerifyLiveTilesRect(bool is_on_recycle_tree
) const;
288 Tile
* CreateTile(int i
, int j
);
289 ScopedTilePtr
TakeTileAt(int i
, int j
);
290 // Returns true if the Tile existed and was removed from the tiling.
291 bool RemoveTileAt(int i
, int j
);
292 bool TilingMatchesTileIndices(const PictureLayerTiling
* twin
) const;
294 // Computes a skewport. The calculation extrapolates the last visible
295 // rect and the current visible rect to expand the skewport to where it
296 // would be in |skewport_target_time| seconds. Note that the skewport
297 // is guaranteed to contain the current visible rect.
298 gfx::Rect
ComputeSkewport(double current_frame_time_in_seconds
,
299 const gfx::Rect
& visible_rect_in_content_space
)
302 // Save the required data for computing tile priorities later.
303 void SetTilePriorityRects(float content_to_screen_scale_
,
304 const gfx::Rect
& visible_rect_in_content_space
,
305 const gfx::Rect
& skewport
,
306 const gfx::Rect
& soon_border_rect
,
307 const gfx::Rect
& eventually_rect
,
308 const Occlusion
& occlusion_in_layer_space
);
310 bool NeedsUpdateForFrameAtTimeAndViewport(
311 double frame_time_in_seconds
,
312 const gfx::Rect
& viewport_in_layer_space
) {
313 return frame_time_in_seconds
!=
314 visible_rect_history_
[0].frame_time_in_seconds
||
315 viewport_in_layer_space
!= last_viewport_in_layer_space_
;
317 void UpdateVisibleRectHistory(
318 double frame_time_in_seconds
,
319 const gfx::Rect
& visible_rect_in_content_space
) {
320 visible_rect_history_
[1] = visible_rect_history_
[0];
321 visible_rect_history_
[0].frame_time_in_seconds
= frame_time_in_seconds
;
322 visible_rect_history_
[0].visible_rect_in_content_space
=
323 visible_rect_in_content_space
;
324 // If we don't have a second history item, set it to the most recent one.
325 if (visible_rect_history_
[1].frame_time_in_seconds
== 0.0)
326 visible_rect_history_
[1] = visible_rect_history_
[0];
328 bool IsTileOccludedOnCurrentTree(const Tile
* tile
) const;
329 bool ShouldCreateTileAt(int i
, int j
) const;
330 bool IsTileOccluded(const Tile
* tile
) const;
331 void UpdateRequiredStatesOnTile(Tile
* tile
) const;
332 PrioritizedTile
MakePrioritizedTile(
334 PriorityRectType priority_rect_type
) const;
335 TilePriority
ComputePriorityForTile(
337 PriorityRectType priority_rect_type
) const;
338 PriorityRectType
ComputePriorityRectTypeForTile(const Tile
* tile
) const;
339 bool has_visible_rect_tiles() const { return has_visible_rect_tiles_
; }
340 bool has_skewport_rect_tiles() const { return has_skewport_rect_tiles_
; }
341 bool has_soon_border_rect_tiles() const {
342 return has_soon_border_rect_tiles_
;
344 bool has_eventually_rect_tiles() const { return has_eventually_rect_tiles_
; }
346 const gfx::Rect
& current_visible_rect() const {
347 return current_visible_rect_
;
349 gfx::Rect
pending_visible_rect() const {
350 const PictureLayerTiling
* pending_tiling
=
351 tree_
== ACTIVE_TREE
? client_
->GetPendingOrActiveTwinTiling(this)
354 return pending_tiling
->current_visible_rect();
357 const gfx::Rect
& current_skewport_rect() const {
358 return current_skewport_rect_
;
360 const gfx::Rect
& current_soon_border_rect() const {
361 return current_soon_border_rect_
;
363 const gfx::Rect
& current_eventually_rect() const {
364 return current_eventually_rect_
;
366 bool has_ever_been_updated() const {
367 return visible_rect_history_
[0].frame_time_in_seconds
!= 0.0;
369 void RemoveTilesInRegion(const Region
& layer_region
, bool recreate_tiles
);
371 const size_t tiling_interest_area_padding_
;
372 const float skewport_target_time_in_seconds_
;
373 const int skewport_extrapolation_limit_in_content_pixels_
;
376 const float contents_scale_
;
377 PictureLayerTilingClient
* const client_
;
378 const WhichTree tree_
;
379 scoped_refptr
<RasterSource
> raster_source_
;
380 TileResolution resolution_
;
381 bool may_contain_low_resolution_tiles_
;
384 TilingData tiling_data_
;
385 TileMap tiles_
; // It is not legal to have a NULL tile in the tiles_ map.
386 gfx::Rect live_tiles_rect_
;
388 gfx::Rect last_viewport_in_layer_space_
;
389 // State saved for computing velocities based upon finite differences.
390 FrameVisibleRect visible_rect_history_
[2];
392 bool can_require_tiles_for_activation_
;
394 // Iteration rects in content space.
395 gfx::Rect current_visible_rect_
;
396 gfx::Rect current_skewport_rect_
;
397 gfx::Rect current_soon_border_rect_
;
398 gfx::Rect current_eventually_rect_
;
399 // Other properties used for tile iteration and prioritization.
400 float current_content_to_screen_scale_
;
401 Occlusion current_occlusion_in_layer_space_
;
403 bool has_visible_rect_tiles_
;
404 bool has_skewport_rect_tiles_
;
405 bool has_soon_border_rect_tiles_
;
406 bool has_eventually_rect_tiles_
;
407 bool all_tiles_done_
;
410 DISALLOW_ASSIGN(PictureLayerTiling
);
415 #endif // CC_TILES_PICTURE_LAYER_TILING_H_