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 #include "cc/tiles/picture_layer_tiling.h"
12 #include "base/logging.h"
13 #include "base/trace_event/trace_event.h"
14 #include "base/trace_event/trace_event_argument.h"
15 #include "cc/base/math_util.h"
16 #include "cc/playback/raster_source.h"
17 #include "cc/tiles/prioritized_tile.h"
18 #include "cc/tiles/tile.h"
19 #include "cc/tiles/tile_priority.h"
20 #include "ui/gfx/geometry/point_conversions.h"
21 #include "ui/gfx/geometry/rect_conversions.h"
22 #include "ui/gfx/geometry/safe_integer_conversions.h"
23 #include "ui/gfx/geometry/size_conversions.h"
28 const float kSoonBorderDistanceViewportPercentage
= 0.15f
;
29 const float kMaxSoonBorderDistanceInScreenPixels
= 312.f
;
33 scoped_ptr
<PictureLayerTiling
> PictureLayerTiling::Create(
36 scoped_refptr
<RasterSource
> raster_source
,
37 PictureLayerTilingClient
* client
,
38 float tiling_interest_area_viewport_multiplier
,
39 float skewport_target_time_in_seconds
,
40 int skewport_extrapolation_limit_in_content_pixels
) {
41 return make_scoped_ptr(new PictureLayerTiling(
42 tree
, contents_scale
, raster_source
, client
,
43 tiling_interest_area_viewport_multiplier
, skewport_target_time_in_seconds
,
44 skewport_extrapolation_limit_in_content_pixels
));
47 PictureLayerTiling::PictureLayerTiling(
50 scoped_refptr
<RasterSource
> raster_source
,
51 PictureLayerTilingClient
* client
,
52 float tiling_interest_area_viewport_multiplier
,
53 float skewport_target_time_in_seconds
,
54 int skewport_extrapolation_limit_in_content_pixels
)
55 : tiling_interest_area_viewport_multiplier_(
56 tiling_interest_area_viewport_multiplier
),
57 skewport_target_time_in_seconds_(skewport_target_time_in_seconds
),
58 skewport_extrapolation_limit_in_content_pixels_(
59 skewport_extrapolation_limit_in_content_pixels
),
60 contents_scale_(contents_scale
),
63 raster_source_(raster_source
),
64 resolution_(NON_IDEAL_RESOLUTION
),
65 tiling_data_(gfx::Size(), gfx::Size(), kBorderTexels
),
66 can_require_tiles_for_activation_(false),
67 current_content_to_screen_scale_(0.f
),
68 has_visible_rect_tiles_(false),
69 has_skewport_rect_tiles_(false),
70 has_soon_border_rect_tiles_(false),
71 has_eventually_rect_tiles_(false),
72 all_tiles_done_(true) {
73 DCHECK(!raster_source
->IsSolidColor());
74 gfx::Size content_bounds
= gfx::ToCeiledSize(
75 gfx::ScaleSize(raster_source_
->GetSize(), contents_scale
));
76 gfx::Size tile_size
= client_
->CalculateTileSize(content_bounds
);
78 DCHECK(!gfx::ToFlooredSize(gfx::ScaleSize(raster_source_
->GetSize(),
79 contents_scale
)).IsEmpty())
80 << "Tiling created with scale too small as contents become empty."
81 << " Layer bounds: " << raster_source_
->GetSize().ToString()
82 << " Contents scale: " << contents_scale
;
84 tiling_data_
.SetTilingSize(content_bounds
);
85 tiling_data_
.SetMaxTextureSize(tile_size
);
88 PictureLayerTiling::~PictureLayerTiling() {
92 float PictureLayerTiling::CalculateSoonBorderDistance(
93 const gfx::Rect
& visible_rect_in_content_space
,
94 float content_to_screen_scale
) {
95 float max_dimension
= std::max(visible_rect_in_content_space
.width(),
96 visible_rect_in_content_space
.height());
98 kMaxSoonBorderDistanceInScreenPixels
/ content_to_screen_scale
,
99 max_dimension
* kSoonBorderDistanceViewportPercentage
);
102 Tile
* PictureLayerTiling::CreateTile(int i
, int j
) {
103 TileMapKey
key(i
, j
);
104 DCHECK(tiles_
.find(key
) == tiles_
.end());
106 gfx::Rect paint_rect
= tiling_data_
.TileBoundsWithBorder(i
, j
);
107 gfx::Rect tile_rect
= paint_rect
;
108 tile_rect
.set_size(tiling_data_
.max_texture_size());
110 if (!raster_source_
->CoversRect(tile_rect
, contents_scale_
))
113 all_tiles_done_
= false;
114 ScopedTilePtr tile
= client_
->CreateTile(contents_scale_
, tile_rect
);
115 Tile
* raw_ptr
= tile
.get();
116 tile
->set_tiling_index(i
, j
);
117 tiles_
.add(key
, tile
.Pass());
121 void PictureLayerTiling::CreateMissingTilesInLiveTilesRect() {
122 bool include_borders
= false;
123 for (TilingData::Iterator
iter(&tiling_data_
, live_tiles_rect_
,
126 TileMapKey
key(iter
.index());
127 TileMap::iterator find
= tiles_
.find(key
);
128 if (find
!= tiles_
.end())
131 if (ShouldCreateTileAt(key
.index_x
, key
.index_y
))
132 CreateTile(key
.index_x
, key
.index_y
);
134 VerifyLiveTilesRect(false);
137 void PictureLayerTiling::TakeTilesAndPropertiesFrom(
138 PictureLayerTiling
* pending_twin
,
139 const Region
& layer_invalidation
) {
140 TRACE_EVENT0("cc", "TakeTilesAndPropertiesFrom");
141 SetRasterSourceAndResize(pending_twin
->raster_source_
);
143 RemoveTilesInRegion(layer_invalidation
, false /* recreate tiles */);
145 resolution_
= pending_twin
->resolution_
;
146 bool create_missing_tiles
= false;
147 if (live_tiles_rect_
.IsEmpty()) {
148 live_tiles_rect_
= pending_twin
->live_tiles_rect();
149 create_missing_tiles
= true;
151 SetLiveTilesRect(pending_twin
->live_tiles_rect());
154 if (tiles_
.empty()) {
155 tiles_
.swap(pending_twin
->tiles_
);
156 all_tiles_done_
= pending_twin
->all_tiles_done_
;
158 while (!pending_twin
->tiles_
.empty()) {
159 TileMapKey key
= pending_twin
->tiles_
.begin()->first
;
160 tiles_
.set(key
, pending_twin
->tiles_
.take_and_erase(key
));
162 all_tiles_done_
&= pending_twin
->all_tiles_done_
;
164 DCHECK(pending_twin
->tiles_
.empty());
165 pending_twin
->all_tiles_done_
= true;
167 if (create_missing_tiles
)
168 CreateMissingTilesInLiveTilesRect();
170 VerifyLiveTilesRect(false);
172 SetTilePriorityRects(pending_twin
->current_content_to_screen_scale_
,
173 pending_twin
->current_visible_rect_
,
174 pending_twin
->current_skewport_rect_
,
175 pending_twin
->current_soon_border_rect_
,
176 pending_twin
->current_eventually_rect_
,
177 pending_twin
->current_occlusion_in_layer_space_
);
180 void PictureLayerTiling::SetRasterSourceAndResize(
181 scoped_refptr
<RasterSource
> raster_source
) {
182 DCHECK(!raster_source
->IsSolidColor());
183 gfx::Size old_layer_bounds
= raster_source_
->GetSize();
184 raster_source_
.swap(raster_source
);
185 gfx::Size new_layer_bounds
= raster_source_
->GetSize();
186 gfx::Size content_bounds
=
187 gfx::ToCeiledSize(gfx::ScaleSize(new_layer_bounds
, contents_scale_
));
188 gfx::Size tile_size
= client_
->CalculateTileSize(content_bounds
);
190 if (tile_size
!= tiling_data_
.max_texture_size()) {
191 tiling_data_
.SetTilingSize(content_bounds
);
192 tiling_data_
.SetMaxTextureSize(tile_size
);
193 // When the tile size changes, the TilingData positions no longer work
194 // as valid keys to the TileMap, so just drop all tiles and clear the live
200 if (old_layer_bounds
== new_layer_bounds
)
203 // The SetLiveTilesRect() method would drop tiles outside the new bounds,
204 // but may do so incorrectly if resizing the tiling causes the number of
205 // tiles in the tiling_data_ to change.
206 gfx::Rect
content_rect(content_bounds
);
207 int before_left
= tiling_data_
.TileXIndexFromSrcCoord(live_tiles_rect_
.x());
208 int before_top
= tiling_data_
.TileYIndexFromSrcCoord(live_tiles_rect_
.y());
210 tiling_data_
.TileXIndexFromSrcCoord(live_tiles_rect_
.right() - 1);
212 tiling_data_
.TileYIndexFromSrcCoord(live_tiles_rect_
.bottom() - 1);
214 // The live_tiles_rect_ is clamped to stay within the tiling size as we
216 live_tiles_rect_
.Intersect(content_rect
);
217 tiling_data_
.SetTilingSize(content_bounds
);
219 int after_right
= -1;
220 int after_bottom
= -1;
221 if (!live_tiles_rect_
.IsEmpty()) {
223 tiling_data_
.TileXIndexFromSrcCoord(live_tiles_rect_
.right() - 1);
225 tiling_data_
.TileYIndexFromSrcCoord(live_tiles_rect_
.bottom() - 1);
228 // There is no recycled twin since this is run on the pending tiling
229 // during commit, and on the active tree during activate.
230 // Drop tiles outside the new layer bounds if the layer shrank.
231 for (int i
= after_right
+ 1; i
<= before_right
; ++i
) {
232 for (int j
= before_top
; j
<= before_bottom
; ++j
)
235 for (int i
= before_left
; i
<= after_right
; ++i
) {
236 for (int j
= after_bottom
+ 1; j
<= before_bottom
; ++j
)
240 if (after_right
> before_right
) {
241 DCHECK_EQ(after_right
, before_right
+ 1);
242 for (int j
= before_top
; j
<= after_bottom
; ++j
) {
243 if (ShouldCreateTileAt(after_right
, j
))
244 CreateTile(after_right
, j
);
247 if (after_bottom
> before_bottom
) {
248 DCHECK_EQ(after_bottom
, before_bottom
+ 1);
249 for (int i
= before_left
; i
<= before_right
; ++i
) {
250 if (ShouldCreateTileAt(i
, after_bottom
))
251 CreateTile(i
, after_bottom
);
256 void PictureLayerTiling::Invalidate(const Region
& layer_invalidation
) {
257 DCHECK_IMPLIES(tree_
== ACTIVE_TREE
,
258 !client_
->GetPendingOrActiveTwinTiling(this));
259 RemoveTilesInRegion(layer_invalidation
, true /* recreate tiles */);
262 void PictureLayerTiling::RemoveTilesInRegion(const Region
& layer_invalidation
,
263 bool recreate_tiles
) {
264 // We only invalidate the active tiling when it's orphaned: it has no pending
265 // twin, so it's slated for removal in the future.
266 if (live_tiles_rect_
.IsEmpty())
268 std::vector
<TileMapKey
> new_tile_keys
;
269 gfx::Rect expanded_live_tiles_rect
=
270 tiling_data_
.ExpandRectIgnoringBordersToTileBounds(live_tiles_rect_
);
271 for (Region::Iterator
iter(layer_invalidation
); iter
.has_rect();
273 gfx::Rect layer_rect
= iter
.rect();
274 gfx::Rect content_rect
=
275 gfx::ScaleToEnclosingRect(layer_rect
, contents_scale_
);
276 // Consider tiles inside the live tiles rect even if only their border
277 // pixels intersect the invalidation. But don't consider tiles outside
278 // the live tiles rect with the same conditions, as they won't exist.
279 int border_pixels
= tiling_data_
.border_texels();
280 content_rect
.Inset(-border_pixels
, -border_pixels
);
281 // Avoid needless work by not bothering to invalidate where there aren't
283 content_rect
.Intersect(expanded_live_tiles_rect
);
284 if (content_rect
.IsEmpty())
286 // Since the content_rect includes border pixels already, don't include
287 // borders when iterating to avoid double counting them.
288 bool include_borders
= false;
290 TilingData::Iterator
iter(&tiling_data_
, content_rect
, include_borders
);
292 if (RemoveTileAt(iter
.index_x(), iter
.index_y())) {
294 new_tile_keys
.push_back(TileMapKey(iter
.index()));
299 for (const auto& key
: new_tile_keys
)
300 CreateTile(key
.index_x
, key
.index_y
);
303 bool PictureLayerTiling::ShouldCreateTileAt(int i
, int j
) const {
304 // Active tree should always create a tile. The reason for this is that active
305 // tree represents content that we draw on screen, which means that whenever
306 // we check whether a tile should exist somewhere, the answer is yes. This
307 // doesn't mean it will actually be created (if raster source doesn't cover
308 // the tile for instance). Pending tree, on the other hand, should only be
309 // creating tiles that are different from the current active tree, which is
310 // represented by the logic in the rest of the function.
311 if (tree_
== ACTIVE_TREE
)
314 // If the pending tree has no active twin, then it needs to create all tiles.
315 const PictureLayerTiling
* active_twin
=
316 client_
->GetPendingOrActiveTwinTiling(this);
320 // Pending tree will override the entire active tree if indices don't match.
321 if (!TilingMatchesTileIndices(active_twin
))
324 gfx::Rect paint_rect
= tiling_data_
.TileBoundsWithBorder(i
, j
);
325 gfx::Rect tile_rect
= paint_rect
;
326 tile_rect
.set_size(tiling_data_
.max_texture_size());
328 // If the active tree can't create a tile, because of its raster source, then
329 // the pending tree should create one.
330 if (!active_twin
->raster_source()->CoversRect(tile_rect
, contents_scale()))
333 const Region
* layer_invalidation
= client_
->GetPendingInvalidation();
334 gfx::Rect layer_rect
=
335 gfx::ScaleToEnclosingRect(tile_rect
, 1.f
/ contents_scale());
337 // If this tile is invalidated, then the pending tree should create one.
338 if (layer_invalidation
&& layer_invalidation
->Intersects(layer_rect
))
341 // If the active tree doesn't have a tile here, but it's in the pending tree's
342 // visible rect, then the pending tree should create a tile. This can happen
343 // if the pending visible rect is outside of the active tree's live tiles
344 // rect. In those situations, we need to block activation until we're ready to
345 // display content, which will have to come from the pending tree.
346 if (!active_twin
->TileAt(i
, j
) && current_visible_rect_
.Intersects(tile_rect
))
349 // In all other cases, the pending tree doesn't need to create a tile.
353 bool PictureLayerTiling::TilingMatchesTileIndices(
354 const PictureLayerTiling
* twin
) const {
355 return tiling_data_
.max_texture_size() ==
356 twin
->tiling_data_
.max_texture_size();
359 PictureLayerTiling::CoverageIterator::CoverageIterator()
370 PictureLayerTiling::CoverageIterator::CoverageIterator(
371 const PictureLayerTiling
* tiling
,
373 const gfx::Rect
& dest_rect
)
375 dest_rect_(dest_rect
),
376 dest_to_content_scale_(0),
385 if (dest_rect_
.IsEmpty())
388 dest_to_content_scale_
= tiling_
->contents_scale_
/ dest_scale
;
390 gfx::Rect content_rect
=
391 gfx::ScaleToEnclosingRect(dest_rect_
,
392 dest_to_content_scale_
,
393 dest_to_content_scale_
);
394 // IndexFromSrcCoord clamps to valid tile ranges, so it's necessary to
395 // check for non-intersection first.
396 content_rect
.Intersect(gfx::Rect(tiling_
->tiling_size()));
397 if (content_rect
.IsEmpty())
400 left_
= tiling_
->tiling_data_
.TileXIndexFromSrcCoord(content_rect
.x());
401 top_
= tiling_
->tiling_data_
.TileYIndexFromSrcCoord(content_rect
.y());
402 right_
= tiling_
->tiling_data_
.TileXIndexFromSrcCoord(
403 content_rect
.right() - 1);
404 bottom_
= tiling_
->tiling_data_
.TileYIndexFromSrcCoord(
405 content_rect
.bottom() - 1);
412 PictureLayerTiling::CoverageIterator::~CoverageIterator() {
415 PictureLayerTiling::CoverageIterator
&
416 PictureLayerTiling::CoverageIterator::operator++() {
417 if (tile_j_
> bottom_
)
420 bool first_time
= tile_i_
< left_
;
421 bool new_row
= false;
423 if (tile_i_
> right_
) {
427 if (tile_j_
> bottom_
) {
428 current_tile_
= NULL
;
433 current_tile_
= tiling_
->TileAt(tile_i_
, tile_j_
);
435 // Calculate the current geometry rect. Due to floating point rounding
436 // and ToEnclosingRect, tiles might overlap in destination space on the
438 gfx::Rect last_geometry_rect
= current_geometry_rect_
;
440 gfx::Rect content_rect
= tiling_
->tiling_data_
.TileBounds(tile_i_
, tile_j_
);
442 current_geometry_rect_
=
443 gfx::ScaleToEnclosingRect(content_rect
,
444 1 / dest_to_content_scale_
,
445 1 / dest_to_content_scale_
);
447 current_geometry_rect_
.Intersect(dest_rect_
);
452 // Iteration happens left->right, top->bottom. Running off the bottom-right
453 // edge is handled by the intersection above with dest_rect_. Here we make
454 // sure that the new current geometry rect doesn't overlap with the last.
458 min_left
= dest_rect_
.x();
459 min_top
= last_geometry_rect
.bottom();
461 min_left
= last_geometry_rect
.right();
462 min_top
= last_geometry_rect
.y();
465 int inset_left
= std::max(0, min_left
- current_geometry_rect_
.x());
466 int inset_top
= std::max(0, min_top
- current_geometry_rect_
.y());
467 current_geometry_rect_
.Inset(inset_left
, inset_top
, 0, 0);
470 DCHECK_EQ(last_geometry_rect
.right(), current_geometry_rect_
.x());
471 DCHECK_EQ(last_geometry_rect
.bottom(), current_geometry_rect_
.bottom());
472 DCHECK_EQ(last_geometry_rect
.y(), current_geometry_rect_
.y());
478 gfx::Rect
PictureLayerTiling::CoverageIterator::geometry_rect() const {
479 return current_geometry_rect_
;
482 gfx::RectF
PictureLayerTiling::CoverageIterator::texture_rect() const {
483 gfx::PointF tex_origin
=
484 tiling_
->tiling_data_
.TileBoundsWithBorder(tile_i_
, tile_j_
).origin();
486 // Convert from dest space => content space => texture space.
487 gfx::RectF
texture_rect(current_geometry_rect_
);
488 texture_rect
.Scale(dest_to_content_scale_
,
489 dest_to_content_scale_
);
490 texture_rect
.Intersect(gfx::Rect(tiling_
->tiling_size()));
491 if (texture_rect
.IsEmpty())
493 texture_rect
.Offset(-tex_origin
.OffsetFromOrigin());
498 bool PictureLayerTiling::RemoveTileAt(int i
, int j
) {
499 TileMap::iterator found
= tiles_
.find(TileMapKey(i
, j
));
500 if (found
== tiles_
.end())
506 void PictureLayerTiling::Reset() {
507 live_tiles_rect_
= gfx::Rect();
509 all_tiles_done_
= true;
512 gfx::Rect
PictureLayerTiling::ComputeSkewport(
513 double current_frame_time_in_seconds
,
514 const gfx::Rect
& visible_rect_in_content_space
) const {
515 gfx::Rect skewport
= visible_rect_in_content_space
;
516 if (skewport
.IsEmpty())
519 if (visible_rect_history_
[1].frame_time_in_seconds
== 0.0)
522 double time_delta
= current_frame_time_in_seconds
-
523 visible_rect_history_
[1].frame_time_in_seconds
;
524 if (time_delta
== 0.0)
527 double extrapolation_multiplier
=
528 skewport_target_time_in_seconds_
/ time_delta
;
530 int old_x
= visible_rect_history_
[1].visible_rect_in_content_space
.x();
531 int old_y
= visible_rect_history_
[1].visible_rect_in_content_space
.y();
533 visible_rect_history_
[1].visible_rect_in_content_space
.right();
535 visible_rect_history_
[1].visible_rect_in_content_space
.bottom();
537 int new_x
= visible_rect_in_content_space
.x();
538 int new_y
= visible_rect_in_content_space
.y();
539 int new_right
= visible_rect_in_content_space
.right();
540 int new_bottom
= visible_rect_in_content_space
.bottom();
542 // Compute the maximum skewport based on
543 // |skewport_extrapolation_limit_in_content_pixels_|.
544 gfx::Rect max_skewport
= skewport
;
545 max_skewport
.Inset(-skewport_extrapolation_limit_in_content_pixels_
,
546 -skewport_extrapolation_limit_in_content_pixels_
);
548 // Inset the skewport by the needed adjustment.
549 skewport
.Inset(extrapolation_multiplier
* (new_x
- old_x
),
550 extrapolation_multiplier
* (new_y
- old_y
),
551 extrapolation_multiplier
* (old_right
- new_right
),
552 extrapolation_multiplier
* (old_bottom
- new_bottom
));
554 // Ensure that visible rect is contained in the skewport.
555 skewport
.Union(visible_rect_in_content_space
);
557 // Clip the skewport to |max_skewport|. This needs to happen after the
558 // union in case intersecting would have left the empty rect.
559 skewport
.Intersect(max_skewport
);
561 // Due to limits in int's representation, it is possible that the two
562 // operations above (union and intersect) result in an empty skewport. To
563 // avoid any unpleasant situations like that, union the visible rect again to
564 // ensure that skewport.Contains(visible_rect_in_content_space) is always
566 skewport
.Union(visible_rect_in_content_space
);
571 bool PictureLayerTiling::ComputeTilePriorityRects(
572 const gfx::Rect
& viewport_in_layer_space
,
573 float ideal_contents_scale
,
574 double current_frame_time_in_seconds
,
575 const Occlusion
& occlusion_in_layer_space
) {
576 if (!NeedsUpdateForFrameAtTimeAndViewport(current_frame_time_in_seconds
,
577 viewport_in_layer_space
)) {
578 // This should never be zero for the purposes of has_ever_been_updated().
579 DCHECK_NE(current_frame_time_in_seconds
, 0.0);
582 gfx::Rect visible_rect_in_content_space
=
583 gfx::ScaleToEnclosingRect(viewport_in_layer_space
, contents_scale_
);
585 if (tiling_size().IsEmpty()) {
586 UpdateVisibleRectHistory(current_frame_time_in_seconds
,
587 visible_rect_in_content_space
);
588 last_viewport_in_layer_space_
= viewport_in_layer_space
;
592 // Calculate the skewport.
593 gfx::Rect skewport
= ComputeSkewport(current_frame_time_in_seconds
,
594 visible_rect_in_content_space
);
595 DCHECK(skewport
.Contains(visible_rect_in_content_space
));
597 // Calculate the eventually/live tiles rect.
598 int64 eventually_rect_area
= tiling_interest_area_viewport_multiplier_
*
599 visible_rect_in_content_space
.width() *
600 visible_rect_in_content_space
.height();
602 gfx::Rect eventually_rect
=
603 ExpandRectEquallyToAreaBoundedBy(visible_rect_in_content_space
,
604 eventually_rect_area
,
605 gfx::Rect(tiling_size()),
608 DCHECK(eventually_rect
.IsEmpty() ||
609 gfx::Rect(tiling_size()).Contains(eventually_rect
))
610 << "tiling_size: " << tiling_size().ToString()
611 << " eventually_rect: " << eventually_rect
.ToString();
613 // Calculate the soon border rect.
614 float content_to_screen_scale
= ideal_contents_scale
/ contents_scale_
;
615 gfx::Rect soon_border_rect
= visible_rect_in_content_space
;
616 float border
= CalculateSoonBorderDistance(visible_rect_in_content_space
,
617 content_to_screen_scale
);
618 soon_border_rect
.Inset(-border
, -border
, -border
, -border
);
620 UpdateVisibleRectHistory(current_frame_time_in_seconds
,
621 visible_rect_in_content_space
);
622 last_viewport_in_layer_space_
= viewport_in_layer_space
;
624 SetTilePriorityRects(content_to_screen_scale
, visible_rect_in_content_space
,
625 skewport
, soon_border_rect
, eventually_rect
,
626 occlusion_in_layer_space
);
627 SetLiveTilesRect(eventually_rect
);
631 void PictureLayerTiling::SetTilePriorityRects(
632 float content_to_screen_scale
,
633 const gfx::Rect
& visible_rect_in_content_space
,
634 const gfx::Rect
& skewport
,
635 const gfx::Rect
& soon_border_rect
,
636 const gfx::Rect
& eventually_rect
,
637 const Occlusion
& occlusion_in_layer_space
) {
638 current_visible_rect_
= visible_rect_in_content_space
;
639 current_skewport_rect_
= skewport
;
640 current_soon_border_rect_
= soon_border_rect
;
641 current_eventually_rect_
= eventually_rect
;
642 current_occlusion_in_layer_space_
= occlusion_in_layer_space
;
643 current_content_to_screen_scale_
= content_to_screen_scale
;
645 gfx::Rect
tiling_rect(tiling_size());
646 has_visible_rect_tiles_
= tiling_rect
.Intersects(current_visible_rect_
);
647 has_skewport_rect_tiles_
= tiling_rect
.Intersects(current_skewport_rect_
);
648 has_soon_border_rect_tiles_
=
649 tiling_rect
.Intersects(current_soon_border_rect_
);
650 has_eventually_rect_tiles_
= tiling_rect
.Intersects(current_eventually_rect_
);
653 void PictureLayerTiling::SetLiveTilesRect(
654 const gfx::Rect
& new_live_tiles_rect
) {
655 DCHECK(new_live_tiles_rect
.IsEmpty() ||
656 gfx::Rect(tiling_size()).Contains(new_live_tiles_rect
))
657 << "tiling_size: " << tiling_size().ToString()
658 << " new_live_tiles_rect: " << new_live_tiles_rect
.ToString();
659 if (live_tiles_rect_
== new_live_tiles_rect
)
662 // Iterate to delete all tiles outside of our new live_tiles rect.
663 for (TilingData::DifferenceIterator
iter(&tiling_data_
, live_tiles_rect_
,
664 new_live_tiles_rect
);
666 RemoveTileAt(iter
.index_x(), iter
.index_y());
669 // Iterate to allocate new tiles for all regions with newly exposed area.
670 for (TilingData::DifferenceIterator
iter(&tiling_data_
, new_live_tiles_rect
,
673 TileMapKey
key(iter
.index());
674 if (ShouldCreateTileAt(key
.index_x
, key
.index_y
))
675 CreateTile(key
.index_x
, key
.index_y
);
678 live_tiles_rect_
= new_live_tiles_rect
;
679 VerifyLiveTilesRect(false);
682 void PictureLayerTiling::VerifyLiveTilesRect(bool is_on_recycle_tree
) const {
684 for (auto it
= tiles_
.begin(); it
!= tiles_
.end(); ++it
) {
687 TileMapKey key
= it
->first
;
688 DCHECK(key
.index_x
< tiling_data_
.num_tiles_x())
689 << this << " " << key
.index_x
<< "," << key
.index_y
<< " num_tiles_x "
690 << tiling_data_
.num_tiles_x() << " live_tiles_rect "
691 << live_tiles_rect_
.ToString();
692 DCHECK(key
.index_y
< tiling_data_
.num_tiles_y())
693 << this << " " << key
.index_x
<< "," << key
.index_y
<< " num_tiles_y "
694 << tiling_data_
.num_tiles_y() << " live_tiles_rect "
695 << live_tiles_rect_
.ToString();
696 DCHECK(tiling_data_
.TileBounds(key
.index_x
, key
.index_y
)
697 .Intersects(live_tiles_rect_
))
698 << this << " " << key
.index_x
<< "," << key
.index_y
<< " tile bounds "
699 << tiling_data_
.TileBounds(key
.index_x
, key
.index_y
).ToString()
700 << " live_tiles_rect " << live_tiles_rect_
.ToString();
705 bool PictureLayerTiling::IsTileOccluded(const Tile
* tile
) const {
706 // If this tile is not occluded on this tree, then it is not occluded.
707 if (!IsTileOccludedOnCurrentTree(tile
))
710 // Otherwise, if this is the pending tree, we're done and the tile is
712 if (tree_
== PENDING_TREE
)
715 // On the active tree however, we need to check if this tile will be
716 // unoccluded upon activation, in which case it has to be considered
718 const PictureLayerTiling
* pending_twin
=
719 client_
->GetPendingOrActiveTwinTiling(this);
721 // If there's a pending tile in the same position. Or if the pending twin
722 // would have to be creating all tiles, then we don't need to worry about
723 // occlusion on the twin.
724 if (!TilingMatchesTileIndices(pending_twin
) ||
725 pending_twin
->TileAt(tile
->tiling_i_index(), tile
->tiling_j_index())) {
728 return pending_twin
->IsTileOccludedOnCurrentTree(tile
);
733 bool PictureLayerTiling::IsTileOccludedOnCurrentTree(const Tile
* tile
) const {
734 if (!current_occlusion_in_layer_space_
.HasOcclusion())
736 gfx::Rect tile_query_rect
=
737 gfx::IntersectRects(tile
->content_rect(), current_visible_rect_
);
738 // Explicitly check if the tile is outside the viewport. If so, we need to
739 // return false, since occlusion for this tile is unknown.
740 if (tile_query_rect
.IsEmpty())
743 if (contents_scale_
!= 1.f
) {
745 gfx::ScaleToEnclosingRect(tile_query_rect
, 1.f
/ contents_scale_
);
747 return current_occlusion_in_layer_space_
.IsOccluded(tile_query_rect
);
750 bool PictureLayerTiling::IsTileRequiredForActivation(const Tile
* tile
) const {
751 if (tree_
== PENDING_TREE
) {
752 if (!can_require_tiles_for_activation_
)
755 if (resolution_
!= HIGH_RESOLUTION
)
758 if (IsTileOccluded(tile
))
761 bool tile_is_visible
=
762 tile
->content_rect().Intersects(current_visible_rect_
);
763 if (!tile_is_visible
)
766 if (client_
->RequiresHighResToDraw())
769 const PictureLayerTiling
* active_twin
=
770 client_
->GetPendingOrActiveTwinTiling(this);
771 if (!active_twin
|| !TilingMatchesTileIndices(active_twin
))
774 if (active_twin
->raster_source()->GetSize() != raster_source()->GetSize())
777 if (active_twin
->current_visible_rect_
!= current_visible_rect_
)
781 active_twin
->TileAt(tile
->tiling_i_index(), tile
->tiling_j_index());
787 DCHECK_EQ(tree_
, ACTIVE_TREE
);
788 const PictureLayerTiling
* pending_twin
=
789 client_
->GetPendingOrActiveTwinTiling(this);
790 // If we don't have a pending tree, or the pending tree will overwrite the
791 // given tile, then it is not required for activation.
792 if (!pending_twin
|| !TilingMatchesTileIndices(pending_twin
) ||
793 pending_twin
->TileAt(tile
->tiling_i_index(), tile
->tiling_j_index())) {
796 // Otherwise, ask the pending twin if this tile is required for activation.
797 return pending_twin
->IsTileRequiredForActivation(tile
);
800 bool PictureLayerTiling::IsTileRequiredForDraw(const Tile
* tile
) const {
801 if (tree_
== PENDING_TREE
)
804 if (resolution_
!= HIGH_RESOLUTION
)
807 bool tile_is_visible
= current_visible_rect_
.Intersects(tile
->content_rect());
808 if (!tile_is_visible
)
811 if (IsTileOccludedOnCurrentTree(tile
))
816 void PictureLayerTiling::UpdateRequiredStatesOnTile(Tile
* tile
) const {
818 tile
->set_required_for_activation(IsTileRequiredForActivation(tile
));
819 tile
->set_required_for_draw(IsTileRequiredForDraw(tile
));
822 PrioritizedTile
PictureLayerTiling::MakePrioritizedTile(
824 PriorityRectType priority_rect_type
) const {
827 raster_source()->CoversRect(tile
->content_rect(), tile
->contents_scale()))
828 << "Recording rect: "
829 << gfx::ScaleToEnclosingRect(tile
->content_rect(),
830 1.f
/ tile
->contents_scale()).ToString();
832 return PrioritizedTile(tile
, raster_source(),
833 ComputePriorityForTile(tile
, priority_rect_type
),
834 IsTileOccluded(tile
));
837 std::map
<const Tile
*, PrioritizedTile
>
838 PictureLayerTiling::UpdateAndGetAllPrioritizedTilesForTesting() const {
839 std::map
<const Tile
*, PrioritizedTile
> result
;
840 for (const auto& key_tile_pair
: tiles_
) {
841 Tile
* tile
= key_tile_pair
.second
;
842 UpdateRequiredStatesOnTile(tile
);
843 PrioritizedTile prioritized_tile
=
844 MakePrioritizedTile(tile
, ComputePriorityRectTypeForTile(tile
));
845 result
.insert(std::make_pair(prioritized_tile
.tile(), prioritized_tile
));
850 TilePriority
PictureLayerTiling::ComputePriorityForTile(
852 PriorityRectType priority_rect_type
) const {
853 // TODO(vmpstr): See if this can be moved to iterators.
854 DCHECK_EQ(ComputePriorityRectTypeForTile(tile
), priority_rect_type
);
855 DCHECK_EQ(TileAt(tile
->tiling_i_index(), tile
->tiling_j_index()), tile
);
857 TilePriority::PriorityBin priority_bin
= client_
->GetMaxTilePriorityBin();
859 switch (priority_rect_type
) {
861 return TilePriority(resolution_
, priority_bin
, 0);
862 case PENDING_VISIBLE_RECT
:
863 if (priority_bin
< TilePriority::SOON
)
864 priority_bin
= TilePriority::SOON
;
865 return TilePriority(resolution_
, priority_bin
, 0);
867 case SOON_BORDER_RECT
:
868 if (priority_bin
< TilePriority::SOON
)
869 priority_bin
= TilePriority::SOON
;
871 case EVENTUALLY_RECT
:
872 priority_bin
= TilePriority::EVENTUALLY
;
876 gfx::Rect tile_bounds
=
877 tiling_data_
.TileBounds(tile
->tiling_i_index(), tile
->tiling_j_index());
878 DCHECK_GT(current_content_to_screen_scale_
, 0.f
);
879 float distance_to_visible
=
880 current_visible_rect_
.ManhattanInternalDistance(tile_bounds
) *
881 current_content_to_screen_scale_
;
883 return TilePriority(resolution_
, priority_bin
, distance_to_visible
);
886 PictureLayerTiling::PriorityRectType
887 PictureLayerTiling::ComputePriorityRectTypeForTile(const Tile
* tile
) const {
888 DCHECK_EQ(TileAt(tile
->tiling_i_index(), tile
->tiling_j_index()), tile
);
889 gfx::Rect tile_bounds
=
890 tiling_data_
.TileBounds(tile
->tiling_i_index(), tile
->tiling_j_index());
892 if (current_visible_rect_
.Intersects(tile_bounds
))
895 if (pending_visible_rect().Intersects(tile_bounds
))
896 return PENDING_VISIBLE_RECT
;
898 if (current_skewport_rect_
.Intersects(tile_bounds
))
899 return SKEWPORT_RECT
;
901 if (current_soon_border_rect_
.Intersects(tile_bounds
))
902 return SOON_BORDER_RECT
;
904 DCHECK(current_eventually_rect_
.Intersects(tile_bounds
));
905 return EVENTUALLY_RECT
;
908 void PictureLayerTiling::GetAllPrioritizedTilesForTracing(
909 std::vector
<PrioritizedTile
>* prioritized_tiles
) const {
910 for (const auto& tile_pair
: tiles_
) {
911 Tile
* tile
= tile_pair
.second
;
912 prioritized_tiles
->push_back(
913 MakePrioritizedTile(tile
, ComputePriorityRectTypeForTile(tile
)));
917 void PictureLayerTiling::AsValueInto(
918 base::trace_event::TracedValue
* state
) const {
919 state
->SetInteger("num_tiles", tiles_
.size());
920 state
->SetDouble("content_scale", contents_scale_
);
921 MathUtil::AddToTracedValue("visible_rect", current_visible_rect_
, state
);
922 MathUtil::AddToTracedValue("skewport_rect", current_skewport_rect_
, state
);
923 MathUtil::AddToTracedValue("soon_rect", current_soon_border_rect_
, state
);
924 MathUtil::AddToTracedValue("eventually_rect", current_eventually_rect_
,
926 MathUtil::AddToTracedValue("tiling_size", tiling_size(), state
);
929 size_t PictureLayerTiling::GPUMemoryUsageInBytes() const {
931 for (TileMap::const_iterator it
= tiles_
.begin(); it
!= tiles_
.end(); ++it
) {
932 const Tile
* tile
= it
->second
;
933 amount
+= tile
->GPUMemoryUsageInBytes();
938 PictureLayerTiling::RectExpansionCache::RectExpansionCache()
939 : previous_target(0) {
944 // This struct represents an event at which the expending rect intersects
945 // one of its boundaries. 4 intersection events will occur during expansion.
947 enum { BOTTOM
, TOP
, LEFT
, RIGHT
} edge
;
952 // Compute the delta to expand from edges to cover target_area.
953 int ComputeExpansionDelta(int num_x_edges
, int num_y_edges
,
954 int width
, int height
,
956 // Compute coefficients for the quadratic equation:
957 // a*x^2 + b*x + c = 0
958 int a
= num_y_edges
* num_x_edges
;
959 int b
= num_y_edges
* width
+ num_x_edges
* height
;
960 int64 c
= static_cast<int64
>(width
) * height
- target_area
;
962 // Compute the delta for our edges using the quadratic equation.
964 (a
== 0) ? -c
/ b
: (-b
+ static_cast<int>(std::sqrt(
965 static_cast<int64
>(b
) * b
- 4.0 * a
* c
))) /
967 return std::max(0, delta
);
972 gfx::Rect
PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
973 const gfx::Rect
& starting_rect
,
975 const gfx::Rect
& bounding_rect
,
976 RectExpansionCache
* cache
) {
977 if (starting_rect
.IsEmpty())
978 return starting_rect
;
981 cache
->previous_start
== starting_rect
&&
982 cache
->previous_bounds
== bounding_rect
&&
983 cache
->previous_target
== target_area
)
984 return cache
->previous_result
;
987 cache
->previous_start
= starting_rect
;
988 cache
->previous_bounds
= bounding_rect
;
989 cache
->previous_target
= target_area
;
992 DCHECK(!bounding_rect
.IsEmpty());
993 DCHECK_GT(target_area
, 0);
995 // Expand the starting rect to cover target_area, if it is smaller than it.
996 int delta
= ComputeExpansionDelta(
997 2, 2, starting_rect
.width(), starting_rect
.height(), target_area
);
998 gfx::Rect expanded_starting_rect
= starting_rect
;
1000 expanded_starting_rect
.Inset(-delta
, -delta
);
1002 gfx::Rect rect
= IntersectRects(expanded_starting_rect
, bounding_rect
);
1003 if (rect
.IsEmpty()) {
1004 // The starting_rect and bounding_rect are far away.
1006 cache
->previous_result
= rect
;
1009 if (delta
>= 0 && rect
== expanded_starting_rect
) {
1010 // The starting rect already covers the entire bounding_rect and isn't too
1011 // large for the target_area.
1013 cache
->previous_result
= rect
;
1017 // Continue to expand/shrink rect to let it cover target_area.
1019 // These values will be updated by the loop and uses as the output.
1020 int origin_x
= rect
.x();
1021 int origin_y
= rect
.y();
1022 int width
= rect
.width();
1023 int height
= rect
.height();
1025 // In the beginning we will consider 2 edges in each dimension.
1026 int num_y_edges
= 2;
1027 int num_x_edges
= 2;
1029 // Create an event list.
1030 EdgeEvent events
[] = {
1031 { EdgeEvent::BOTTOM
, &num_y_edges
, rect
.y() - bounding_rect
.y() },
1032 { EdgeEvent::TOP
, &num_y_edges
, bounding_rect
.bottom() - rect
.bottom() },
1033 { EdgeEvent::LEFT
, &num_x_edges
, rect
.x() - bounding_rect
.x() },
1034 { EdgeEvent::RIGHT
, &num_x_edges
, bounding_rect
.right() - rect
.right() }
1037 // Sort the events by distance (closest first).
1038 if (events
[0].distance
> events
[1].distance
) std::swap(events
[0], events
[1]);
1039 if (events
[2].distance
> events
[3].distance
) std::swap(events
[2], events
[3]);
1040 if (events
[0].distance
> events
[2].distance
) std::swap(events
[0], events
[2]);
1041 if (events
[1].distance
> events
[3].distance
) std::swap(events
[1], events
[3]);
1042 if (events
[1].distance
> events
[2].distance
) std::swap(events
[1], events
[2]);
1044 for (int event_index
= 0; event_index
< 4; event_index
++) {
1045 const EdgeEvent
& event
= events
[event_index
];
1047 int delta
= ComputeExpansionDelta(
1048 num_x_edges
, num_y_edges
, width
, height
, target_area
);
1050 // Clamp delta to our event distance.
1051 if (delta
> event
.distance
)
1052 delta
= event
.distance
;
1054 // Adjust the edge count for this kind of edge.
1057 // Apply the delta to the edges and edge events.
1058 for (int i
= event_index
; i
< 4; i
++) {
1059 switch (events
[i
].edge
) {
1060 case EdgeEvent::BOTTOM
:
1064 case EdgeEvent::TOP
:
1067 case EdgeEvent::LEFT
:
1071 case EdgeEvent::RIGHT
:
1075 events
[i
].distance
-= delta
;
1078 // If our delta is less then our event distance, we're done.
1079 if (delta
< event
.distance
)
1083 gfx::Rect
result(origin_x
, origin_y
, width
, height
);
1085 cache
->previous_result
= result
;