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_set.h"
11 #include "cc/playback/raster_source.h"
17 class LargestToSmallestScaleFunctor
{
19 bool operator() (PictureLayerTiling
* left
, PictureLayerTiling
* right
) {
20 return left
->contents_scale() > right
->contents_scale();
24 inline float LargerRatio(float float1
, float float2
) {
25 DCHECK_GT(float1
, 0.f
);
26 DCHECK_GT(float2
, 0.f
);
27 return float1
> float2
? float1
/ float2
: float2
/ float1
;
33 scoped_ptr
<PictureLayerTilingSet
> PictureLayerTilingSet::Create(
35 PictureLayerTilingClient
* client
,
36 size_t max_tiles_for_interest_area
,
37 float skewport_target_time_in_seconds
,
38 int skewport_extrapolation_limit_in_content_pixels
) {
39 return make_scoped_ptr(new PictureLayerTilingSet(
40 tree
, client
, max_tiles_for_interest_area
,
41 skewport_target_time_in_seconds
,
42 skewport_extrapolation_limit_in_content_pixels
));
45 PictureLayerTilingSet::PictureLayerTilingSet(
47 PictureLayerTilingClient
* client
,
48 size_t max_tiles_for_interest_area
,
49 float skewport_target_time_in_seconds
,
50 int skewport_extrapolation_limit_in_content_pixels
)
51 : max_tiles_for_interest_area_(max_tiles_for_interest_area
),
52 skewport_target_time_in_seconds_(skewport_target_time_in_seconds
),
53 skewport_extrapolation_limit_in_content_pixels_(
54 skewport_extrapolation_limit_in_content_pixels
),
59 PictureLayerTilingSet::~PictureLayerTilingSet() {
62 void PictureLayerTilingSet::CopyTilingsAndPropertiesFromPendingTwin(
63 const PictureLayerTilingSet
* pending_twin_set
,
64 const scoped_refptr
<RasterSource
>& raster_source
,
65 const Region
& layer_invalidation
) {
66 if (pending_twin_set
->tilings_
.empty()) {
67 // If the twin (pending) tiling set is empty, it was not updated for the
68 // current frame. So we drop tilings from our set as well, instead of
69 // leaving behind unshared tilings that are all non-ideal.
74 bool tiling_sort_required
= false;
75 for (PictureLayerTiling
* pending_twin_tiling
: pending_twin_set
->tilings_
) {
76 float contents_scale
= pending_twin_tiling
->contents_scale();
77 PictureLayerTiling
* this_tiling
= FindTilingWithScale(contents_scale
);
79 scoped_ptr
<PictureLayerTiling
> new_tiling
= PictureLayerTiling::Create(
80 tree_
, contents_scale
, raster_source
, client_
,
81 max_tiles_for_interest_area_
, skewport_target_time_in_seconds_
,
82 skewport_extrapolation_limit_in_content_pixels_
);
83 tilings_
.push_back(new_tiling
.Pass());
84 this_tiling
= tilings_
.back();
85 tiling_sort_required
= true;
87 this_tiling
->TakeTilesAndPropertiesFrom(pending_twin_tiling
,
91 if (tiling_sort_required
)
92 tilings_
.sort(LargestToSmallestScaleFunctor());
95 void PictureLayerTilingSet::UpdateTilingsToCurrentRasterSourceForActivation(
96 scoped_refptr
<RasterSource
> raster_source
,
97 const PictureLayerTilingSet
* pending_twin_set
,
98 const Region
& layer_invalidation
,
99 float minimum_contents_scale
,
100 float maximum_contents_scale
) {
101 RemoveTilingsBelowScale(minimum_contents_scale
);
102 RemoveTilingsAboveScale(maximum_contents_scale
);
104 // Copy over tilings that are shared with the |pending_twin_set| tiling set.
105 // Also, copy all of the properties from twin tilings.
106 CopyTilingsAndPropertiesFromPendingTwin(pending_twin_set
, raster_source
,
109 // If the tiling is not shared (FindTilingWithScale returns nullptr), then
110 // invalidate tiles and update them to the new raster source.
111 for (PictureLayerTiling
* tiling
: tilings_
) {
112 if (pending_twin_set
->FindTilingWithScale(tiling
->contents_scale()))
115 tiling
->SetRasterSourceAndResize(raster_source
);
116 tiling
->Invalidate(layer_invalidation
);
117 // This is needed for cases where the live tiles rect didn't change but
118 // recordings exist in the raster source that did not exist on the last
120 tiling
->CreateMissingTilesInLiveTilesRect();
122 // |this| is active set and |tiling| is not in the pending set, which means
123 // it is now NON_IDEAL_RESOLUTION.
124 tiling
->set_resolution(NON_IDEAL_RESOLUTION
);
127 VerifyTilings(pending_twin_set
);
130 void PictureLayerTilingSet::UpdateTilingsToCurrentRasterSourceForCommit(
131 scoped_refptr
<RasterSource
> raster_source
,
132 const Region
& layer_invalidation
,
133 float minimum_contents_scale
,
134 float maximum_contents_scale
) {
135 RemoveTilingsBelowScale(minimum_contents_scale
);
136 RemoveTilingsAboveScale(maximum_contents_scale
);
138 // Invalidate tiles and update them to the new raster source.
139 for (PictureLayerTiling
* tiling
: tilings_
) {
140 DCHECK_IMPLIES(tree_
== PENDING_TREE
, !tiling
->has_tiles());
141 tiling
->SetRasterSourceAndResize(raster_source
);
143 // We can commit on either active or pending trees, but only active one can
144 // have tiles at this point.
145 if (tree_
== ACTIVE_TREE
)
146 tiling
->Invalidate(layer_invalidation
);
148 // This is needed for cases where the live tiles rect didn't change but
149 // recordings exist in the raster source that did not exist on the last
151 tiling
->CreateMissingTilesInLiveTilesRect();
153 VerifyTilings(nullptr /* pending_twin_set */);
156 void PictureLayerTilingSet::UpdateRasterSourceDueToLCDChange(
157 const scoped_refptr
<RasterSource
>& raster_source
,
158 const Region
& layer_invalidation
) {
159 for (PictureLayerTiling
* tiling
: tilings_
) {
160 tiling
->SetRasterSourceAndResize(raster_source
);
161 tiling
->Invalidate(layer_invalidation
);
162 // Since the invalidation changed, we need to create any missing tiles in
163 // the live tiles rect again.
164 tiling
->CreateMissingTilesInLiveTilesRect();
168 void PictureLayerTilingSet::VerifyTilings(
169 const PictureLayerTilingSet
* pending_twin_set
) const {
171 for (PictureLayerTiling
* tiling
: tilings_
) {
172 DCHECK(tiling
->tile_size() ==
173 client_
->CalculateTileSize(tiling
->tiling_size()))
174 << "tile_size: " << tiling
->tile_size().ToString()
175 << " tiling_size: " << tiling
->tiling_size().ToString()
176 << " CalculateTileSize: "
177 << client_
->CalculateTileSize(tiling
->tiling_size()).ToString();
180 if (!tilings_
.empty()) {
181 DCHECK_LE(NumHighResTilings(), 1);
182 // When commiting from the main thread the high res tiling may get dropped,
183 // but when cloning to the active tree, there should always be one.
184 if (pending_twin_set
) {
185 DCHECK_EQ(1, NumHighResTilings())
186 << " num tilings on active: " << tilings_
.size()
187 << " num tilings on pending: " << pending_twin_set
->tilings_
.size()
188 << " num high res on pending: "
189 << pending_twin_set
->NumHighResTilings()
190 << " are on active tree: " << (tree_
== ACTIVE_TREE
);
196 void PictureLayerTilingSet::CleanUpTilings(
197 float min_acceptable_high_res_scale
,
198 float max_acceptable_high_res_scale
,
199 const std::vector
<PictureLayerTiling
*>& needed_tilings
,
200 bool should_have_low_res
,
201 PictureLayerTilingSet
* twin_set
) {
202 float twin_low_res_scale
= 0.f
;
204 PictureLayerTiling
* tiling
=
205 twin_set
->FindTilingWithResolution(LOW_RESOLUTION
);
207 twin_low_res_scale
= tiling
->contents_scale();
210 std::vector
<PictureLayerTiling
*> to_remove
;
211 for (auto* tiling
: tilings_
) {
212 // Keep all tilings within the min/max scales.
213 if (tiling
->contents_scale() >= min_acceptable_high_res_scale
&&
214 tiling
->contents_scale() <= max_acceptable_high_res_scale
) {
218 // Keep low resolution tilings, if the tiling set should have them.
219 if (should_have_low_res
&&
220 (tiling
->resolution() == LOW_RESOLUTION
||
221 tiling
->contents_scale() == twin_low_res_scale
)) {
225 // Don't remove tilings that are required.
226 if (std::find(needed_tilings
.begin(), needed_tilings
.end(), tiling
) !=
227 needed_tilings
.end()) {
231 to_remove
.push_back(tiling
);
234 for (auto* tiling
: to_remove
) {
235 DCHECK_NE(HIGH_RESOLUTION
, tiling
->resolution());
240 void PictureLayerTilingSet::RemoveNonIdealTilings() {
241 auto to_remove
= tilings_
.remove_if([](PictureLayerTiling
* t
) {
242 return t
->resolution() == NON_IDEAL_RESOLUTION
;
244 tilings_
.erase(to_remove
, tilings_
.end());
247 void PictureLayerTilingSet::MarkAllTilingsNonIdeal() {
248 for (auto* tiling
: tilings_
)
249 tiling
->set_resolution(NON_IDEAL_RESOLUTION
);
252 PictureLayerTiling
* PictureLayerTilingSet::AddTiling(
253 float contents_scale
,
254 scoped_refptr
<RasterSource
> raster_source
) {
255 for (size_t i
= 0; i
< tilings_
.size(); ++i
) {
256 DCHECK_NE(tilings_
[i
]->contents_scale(), contents_scale
);
257 DCHECK_EQ(tilings_
[i
]->raster_source(), raster_source
.get());
260 tilings_
.push_back(PictureLayerTiling::Create(
261 tree_
, contents_scale
, raster_source
, client_
,
262 max_tiles_for_interest_area_
, skewport_target_time_in_seconds_
,
263 skewport_extrapolation_limit_in_content_pixels_
));
264 PictureLayerTiling
* appended
= tilings_
.back();
266 tilings_
.sort(LargestToSmallestScaleFunctor());
270 int PictureLayerTilingSet::NumHighResTilings() const {
271 return std::count_if(tilings_
.begin(), tilings_
.end(),
272 [](PictureLayerTiling
* tiling
) {
273 return tiling
->resolution() == HIGH_RESOLUTION
;
277 PictureLayerTiling
* PictureLayerTilingSet::FindTilingWithScale(
279 for (size_t i
= 0; i
< tilings_
.size(); ++i
) {
280 if (tilings_
[i
]->contents_scale() == scale
)
286 PictureLayerTiling
* PictureLayerTilingSet::FindTilingWithResolution(
287 TileResolution resolution
) const {
288 auto iter
= std::find_if(tilings_
.begin(), tilings_
.end(),
289 [resolution
](const PictureLayerTiling
* tiling
) {
290 return tiling
->resolution() == resolution
;
292 if (iter
== tilings_
.end())
297 void PictureLayerTilingSet::RemoveTilingsBelowScale(float minimum_scale
) {
299 tilings_
.remove_if([minimum_scale
](PictureLayerTiling
* tiling
) {
300 return tiling
->contents_scale() < minimum_scale
;
302 tilings_
.erase(to_remove
, tilings_
.end());
305 void PictureLayerTilingSet::RemoveTilingsAboveScale(float maximum_scale
) {
307 tilings_
.remove_if([maximum_scale
](PictureLayerTiling
* tiling
) {
308 return tiling
->contents_scale() > maximum_scale
;
310 tilings_
.erase(to_remove
, tilings_
.end());
313 void PictureLayerTilingSet::RemoveAllTilings() {
317 void PictureLayerTilingSet::Remove(PictureLayerTiling
* tiling
) {
318 ScopedPtrVector
<PictureLayerTiling
>::iterator iter
=
319 std::find(tilings_
.begin(), tilings_
.end(), tiling
);
320 if (iter
== tilings_
.end())
322 tilings_
.erase(iter
);
325 void PictureLayerTilingSet::RemoveAllTiles() {
326 for (size_t i
= 0; i
< tilings_
.size(); ++i
)
327 tilings_
[i
]->Reset();
330 float PictureLayerTilingSet::GetSnappedContentsScale(
332 float snap_to_existing_tiling_ratio
) const {
333 // If a tiling exists within the max snapping ratio, snap to its scale.
334 float snapped_contents_scale
= start_scale
;
335 float snapped_ratio
= snap_to_existing_tiling_ratio
;
336 for (const auto* tiling
: tilings_
) {
337 float tiling_contents_scale
= tiling
->contents_scale();
338 float ratio
= LargerRatio(tiling_contents_scale
, start_scale
);
339 if (ratio
< snapped_ratio
) {
340 snapped_contents_scale
= tiling_contents_scale
;
341 snapped_ratio
= ratio
;
344 return snapped_contents_scale
;
347 float PictureLayerTilingSet::GetMaximumContentsScale() const {
348 if (tilings_
.empty())
350 // The first tiling has the largest contents scale.
351 return tilings_
[0]->contents_scale();
354 bool PictureLayerTilingSet::UpdateTilePriorities(
355 const gfx::Rect
& required_rect_in_layer_space
,
356 float ideal_contents_scale
,
357 double current_frame_time_in_seconds
,
358 const Occlusion
& occlusion_in_layer_space
,
359 bool can_require_tiles_for_activation
) {
360 bool updated
= false;
361 for (auto* tiling
: tilings_
) {
362 tiling
->set_can_require_tiles_for_activation(
363 can_require_tiles_for_activation
);
364 updated
|= tiling
->ComputeTilePriorityRects(
365 required_rect_in_layer_space
, ideal_contents_scale
,
366 current_frame_time_in_seconds
, occlusion_in_layer_space
);
371 void PictureLayerTilingSet::GetAllPrioritizedTilesForTracing(
372 std::vector
<PrioritizedTile
>* prioritized_tiles
) const {
373 for (auto* tiling
: tilings_
)
374 tiling
->GetAllPrioritizedTilesForTracing(prioritized_tiles
);
377 PictureLayerTilingSet::CoverageIterator::CoverageIterator(
378 const PictureLayerTilingSet
* set
,
379 float contents_scale
,
380 const gfx::Rect
& content_rect
,
381 float ideal_contents_scale
)
383 contents_scale_(contents_scale
),
384 ideal_contents_scale_(ideal_contents_scale
),
385 current_tiling_(std::numeric_limits
<size_t>::max()) {
386 missing_region_
.Union(content_rect
);
388 size_t tilings_size
= set_
->tilings_
.size();
389 for (ideal_tiling_
= 0; ideal_tiling_
< tilings_size
; ++ideal_tiling_
) {
390 PictureLayerTiling
* tiling
= set_
->tilings_
[ideal_tiling_
];
391 if (tiling
->contents_scale() < ideal_contents_scale_
) {
392 if (ideal_tiling_
> 0)
398 if (ideal_tiling_
== tilings_size
&& ideal_tiling_
> 0)
404 PictureLayerTilingSet::CoverageIterator::~CoverageIterator() {
407 gfx::Rect
PictureLayerTilingSet::CoverageIterator::geometry_rect() const {
409 if (!region_iter_
.has_rect())
411 return region_iter_
.rect();
413 return tiling_iter_
.geometry_rect();
416 gfx::RectF
PictureLayerTilingSet::CoverageIterator::texture_rect() const {
419 return tiling_iter_
.texture_rect();
422 Tile
* PictureLayerTilingSet::CoverageIterator::operator->() const {
425 return *tiling_iter_
;
428 Tile
* PictureLayerTilingSet::CoverageIterator::operator*() const {
431 return *tiling_iter_
;
434 TileResolution
PictureLayerTilingSet::CoverageIterator::resolution() const {
435 const PictureLayerTiling
* tiling
= CurrentTiling();
437 return tiling
->resolution();
440 PictureLayerTiling
* PictureLayerTilingSet::CoverageIterator::CurrentTiling()
442 if (current_tiling_
== std::numeric_limits
<size_t>::max())
444 if (current_tiling_
>= set_
->tilings_
.size())
446 return set_
->tilings_
[current_tiling_
];
449 size_t PictureLayerTilingSet::CoverageIterator::NextTiling() const {
450 // Order returned by this method is:
451 // 1. Ideal tiling index
452 // 2. Tiling index < Ideal in decreasing order (higher res than ideal)
453 // 3. Tiling index > Ideal in increasing order (lower res than ideal)
454 // 4. Tiling index > tilings.size() (invalid index)
455 if (current_tiling_
== std::numeric_limits
<size_t>::max())
456 return ideal_tiling_
;
457 else if (current_tiling_
> ideal_tiling_
)
458 return current_tiling_
+ 1;
459 else if (current_tiling_
)
460 return current_tiling_
- 1;
462 return ideal_tiling_
+ 1;
465 PictureLayerTilingSet::CoverageIterator
&
466 PictureLayerTilingSet::CoverageIterator::operator++() {
467 bool first_time
= current_tiling_
== std::numeric_limits
<size_t>::max();
469 if (!*this && !first_time
)
475 // Loop until we find a valid place to stop.
477 while (tiling_iter_
&&
478 (!*tiling_iter_
|| !tiling_iter_
->draw_info().IsReadyToDraw())) {
479 missing_region_
.Union(tiling_iter_
.geometry_rect());
485 // If the set of current rects for this tiling is done, go to the next
486 // tiling and set up to iterate through all of the remaining holes.
487 // This will also happen the first time through the loop.
488 if (!region_iter_
.has_rect()) {
489 current_tiling_
= NextTiling();
490 current_region_
.Swap(&missing_region_
);
491 missing_region_
.Clear();
492 region_iter_
= Region::Iterator(current_region_
);
494 // All done and all filled.
495 if (!region_iter_
.has_rect()) {
496 current_tiling_
= set_
->tilings_
.size();
500 // No more valid tiles, return this checkerboard rect.
501 if (current_tiling_
>= set_
->tilings_
.size())
505 // Pop a rect off. If there are no more tilings, then these will be
506 // treated as geometry with null tiles that the caller can checkerboard.
507 gfx::Rect last_rect
= region_iter_
.rect();
510 // Done, found next checkerboard rect to return.
511 if (current_tiling_
>= set_
->tilings_
.size())
514 // Construct a new iterator for the next tiling, but we need to loop
515 // again until we get to a valid one.
516 tiling_iter_
= PictureLayerTiling::CoverageIterator(
517 set_
->tilings_
[current_tiling_
],
525 PictureLayerTilingSet::CoverageIterator::operator bool() const {
526 return current_tiling_
< set_
->tilings_
.size() || region_iter_
.has_rect();
529 void PictureLayerTilingSet::AsValueInto(
530 base::trace_event::TracedValue
* state
) const {
531 for (size_t i
= 0; i
< tilings_
.size(); ++i
) {
532 state
->BeginDictionary();
533 tilings_
[i
]->AsValueInto(state
);
534 state
->EndDictionary();
538 size_t PictureLayerTilingSet::GPUMemoryUsageInBytes() const {
540 for (size_t i
= 0; i
< tilings_
.size(); ++i
)
541 amount
+= tilings_
[i
]->GPUMemoryUsageInBytes();
545 PictureLayerTilingSet::TilingRange
PictureLayerTilingSet::GetTilingRange(
546 TilingRangeType type
) const {
547 // Doesn't seem to be the case right now but if it ever becomes a performance
548 // problem to compute these ranges each time this function is called, we can
549 // compute them only when the tiling set has changed instead.
550 size_t tilings_size
= tilings_
.size();
551 TilingRange
high_res_range(0, 0);
552 TilingRange
low_res_range(tilings_
.size(), tilings_
.size());
553 for (size_t i
= 0; i
< tilings_size
; ++i
) {
554 const PictureLayerTiling
* tiling
= tilings_
[i
];
555 if (tiling
->resolution() == HIGH_RESOLUTION
)
556 high_res_range
= TilingRange(i
, i
+ 1);
557 if (tiling
->resolution() == LOW_RESOLUTION
)
558 low_res_range
= TilingRange(i
, i
+ 1);
561 TilingRange
range(0, 0);
563 case HIGHER_THAN_HIGH_RES
:
564 range
= TilingRange(0, high_res_range
.start
);
567 range
= high_res_range
;
569 case BETWEEN_HIGH_AND_LOW_RES
:
570 // TODO(vmpstr): This code assumes that high res tiling will come before
571 // low res tiling, however there are cases where this assumption is
572 // violated. As a result, it's better to be safe in these situations,
573 // since otherwise we can end up accessing a tiling that doesn't exist.
574 // See crbug.com/429397 for high res tiling appearing after low res
575 // tiling discussion/fixes.
576 if (high_res_range
.start
<= low_res_range
.start
)
577 range
= TilingRange(high_res_range
.end
, low_res_range
.start
);
579 range
= TilingRange(low_res_range
.end
, high_res_range
.start
);
582 range
= low_res_range
;
584 case LOWER_THAN_LOW_RES
:
585 range
= TilingRange(low_res_range
.end
, tilings_size
);
589 DCHECK_LE(range
.start
, range
.end
);