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 float tiling_interest_area_viewport_multiplier
,
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
, tiling_interest_area_viewport_multiplier
,
41 skewport_target_time_in_seconds
,
42 skewport_extrapolation_limit_in_content_pixels
));
45 PictureLayerTilingSet::PictureLayerTilingSet(
47 PictureLayerTilingClient
* client
,
48 float tiling_interest_area_viewport_multiplier
,
49 float skewport_target_time_in_seconds
,
50 int skewport_extrapolation_limit_in_content_pixels
)
51 : tiling_interest_area_viewport_multiplier_(
52 tiling_interest_area_viewport_multiplier
),
53 skewport_target_time_in_seconds_(skewport_target_time_in_seconds
),
54 skewport_extrapolation_limit_in_content_pixels_(
55 skewport_extrapolation_limit_in_content_pixels
),
60 PictureLayerTilingSet::~PictureLayerTilingSet() {
63 void PictureLayerTilingSet::CopyTilingsAndPropertiesFromPendingTwin(
64 const PictureLayerTilingSet
* pending_twin_set
,
65 const scoped_refptr
<RasterSource
>& raster_source
,
66 const Region
& layer_invalidation
) {
67 if (pending_twin_set
->tilings_
.empty()) {
68 // If the twin (pending) tiling set is empty, it was not updated for the
69 // current frame. So we drop tilings from our set as well, instead of
70 // leaving behind unshared tilings that are all non-ideal.
75 bool tiling_sort_required
= false;
76 for (PictureLayerTiling
* pending_twin_tiling
: pending_twin_set
->tilings_
) {
77 float contents_scale
= pending_twin_tiling
->contents_scale();
78 PictureLayerTiling
* this_tiling
= FindTilingWithScale(contents_scale
);
80 scoped_ptr
<PictureLayerTiling
> new_tiling
= PictureLayerTiling::Create(
81 tree_
, contents_scale
, raster_source
, client_
,
82 tiling_interest_area_viewport_multiplier_
,
83 skewport_target_time_in_seconds_
,
84 skewport_extrapolation_limit_in_content_pixels_
);
85 tilings_
.push_back(new_tiling
.Pass());
86 this_tiling
= tilings_
.back();
87 tiling_sort_required
= true;
89 this_tiling
->TakeTilesAndPropertiesFrom(pending_twin_tiling
,
93 if (tiling_sort_required
)
94 tilings_
.sort(LargestToSmallestScaleFunctor());
97 void PictureLayerTilingSet::UpdateTilingsToCurrentRasterSourceForActivation(
98 scoped_refptr
<RasterSource
> raster_source
,
99 const PictureLayerTilingSet
* pending_twin_set
,
100 const Region
& layer_invalidation
,
101 float minimum_contents_scale
,
102 float maximum_contents_scale
) {
103 RemoveTilingsBelowScale(minimum_contents_scale
);
104 RemoveTilingsAboveScale(maximum_contents_scale
);
106 // Copy over tilings that are shared with the |pending_twin_set| tiling set.
107 // Also, copy all of the properties from twin tilings.
108 CopyTilingsAndPropertiesFromPendingTwin(pending_twin_set
, raster_source
,
111 // If the tiling is not shared (FindTilingWithScale returns nullptr), then
112 // invalidate tiles and update them to the new raster source.
113 for (PictureLayerTiling
* tiling
: tilings_
) {
114 if (pending_twin_set
->FindTilingWithScale(tiling
->contents_scale()))
117 tiling
->SetRasterSourceAndResize(raster_source
);
118 tiling
->Invalidate(layer_invalidation
);
119 // This is needed for cases where the live tiles rect didn't change but
120 // recordings exist in the raster source that did not exist on the last
122 tiling
->CreateMissingTilesInLiveTilesRect();
124 // |this| is active set and |tiling| is not in the pending set, which means
125 // it is now NON_IDEAL_RESOLUTION.
126 tiling
->set_resolution(NON_IDEAL_RESOLUTION
);
129 VerifyTilings(pending_twin_set
);
132 void PictureLayerTilingSet::UpdateTilingsToCurrentRasterSourceForCommit(
133 scoped_refptr
<RasterSource
> raster_source
,
134 const Region
& layer_invalidation
,
135 float minimum_contents_scale
,
136 float maximum_contents_scale
) {
137 RemoveTilingsBelowScale(minimum_contents_scale
);
138 RemoveTilingsAboveScale(maximum_contents_scale
);
140 // Invalidate tiles and update them to the new raster source.
141 for (PictureLayerTiling
* tiling
: tilings_
) {
142 DCHECK_IMPLIES(tree_
== PENDING_TREE
, !tiling
->has_tiles());
143 tiling
->SetRasterSourceAndResize(raster_source
);
145 // We can commit on either active or pending trees, but only active one can
146 // have tiles at this point.
147 if (tree_
== ACTIVE_TREE
)
148 tiling
->Invalidate(layer_invalidation
);
150 // This is needed for cases where the live tiles rect didn't change but
151 // recordings exist in the raster source that did not exist on the last
153 tiling
->CreateMissingTilesInLiveTilesRect();
155 VerifyTilings(nullptr /* pending_twin_set */);
158 void PictureLayerTilingSet::UpdateRasterSourceDueToLCDChange(
159 const scoped_refptr
<RasterSource
>& raster_source
,
160 const Region
& layer_invalidation
) {
161 for (PictureLayerTiling
* tiling
: tilings_
) {
162 tiling
->SetRasterSourceAndResize(raster_source
);
163 tiling
->Invalidate(layer_invalidation
);
164 // Since the invalidation changed, we need to create any missing tiles in
165 // the live tiles rect again.
166 tiling
->CreateMissingTilesInLiveTilesRect();
170 void PictureLayerTilingSet::VerifyTilings(
171 const PictureLayerTilingSet
* pending_twin_set
) const {
173 for (PictureLayerTiling
* tiling
: tilings_
) {
174 DCHECK(tiling
->tile_size() ==
175 client_
->CalculateTileSize(tiling
->tiling_size()))
176 << "tile_size: " << tiling
->tile_size().ToString()
177 << " tiling_size: " << tiling
->tiling_size().ToString()
178 << " CalculateTileSize: "
179 << client_
->CalculateTileSize(tiling
->tiling_size()).ToString();
182 if (!tilings_
.empty()) {
183 DCHECK_LE(NumHighResTilings(), 1);
184 // When commiting from the main thread the high res tiling may get dropped,
185 // but when cloning to the active tree, there should always be one.
186 if (pending_twin_set
) {
187 DCHECK_EQ(1, NumHighResTilings())
188 << " num tilings on active: " << tilings_
.size()
189 << " num tilings on pending: " << pending_twin_set
->tilings_
.size()
190 << " num high res on pending: "
191 << pending_twin_set
->NumHighResTilings()
192 << " are on active tree: " << (tree_
== ACTIVE_TREE
);
198 void PictureLayerTilingSet::CleanUpTilings(
199 float min_acceptable_high_res_scale
,
200 float max_acceptable_high_res_scale
,
201 const std::vector
<PictureLayerTiling
*>& needed_tilings
,
202 bool should_have_low_res
,
203 PictureLayerTilingSet
* twin_set
) {
204 float twin_low_res_scale
= 0.f
;
206 PictureLayerTiling
* tiling
=
207 twin_set
->FindTilingWithResolution(LOW_RESOLUTION
);
209 twin_low_res_scale
= tiling
->contents_scale();
212 std::vector
<PictureLayerTiling
*> to_remove
;
213 for (auto* tiling
: tilings_
) {
214 // Keep all tilings within the min/max scales.
215 if (tiling
->contents_scale() >= min_acceptable_high_res_scale
&&
216 tiling
->contents_scale() <= max_acceptable_high_res_scale
) {
220 // Keep low resolution tilings, if the tiling set should have them.
221 if (should_have_low_res
&&
222 (tiling
->resolution() == LOW_RESOLUTION
||
223 tiling
->contents_scale() == twin_low_res_scale
)) {
227 // Don't remove tilings that are required.
228 if (std::find(needed_tilings
.begin(), needed_tilings
.end(), tiling
) !=
229 needed_tilings
.end()) {
233 to_remove
.push_back(tiling
);
236 for (auto* tiling
: to_remove
) {
237 DCHECK_NE(HIGH_RESOLUTION
, tiling
->resolution());
242 void PictureLayerTilingSet::RemoveNonIdealTilings() {
243 auto to_remove
= tilings_
.remove_if([](PictureLayerTiling
* t
) {
244 return t
->resolution() == NON_IDEAL_RESOLUTION
;
246 tilings_
.erase(to_remove
, tilings_
.end());
249 void PictureLayerTilingSet::MarkAllTilingsNonIdeal() {
250 for (auto* tiling
: tilings_
)
251 tiling
->set_resolution(NON_IDEAL_RESOLUTION
);
254 PictureLayerTiling
* PictureLayerTilingSet::AddTiling(
255 float contents_scale
,
256 scoped_refptr
<RasterSource
> raster_source
) {
257 for (size_t i
= 0; i
< tilings_
.size(); ++i
) {
258 DCHECK_NE(tilings_
[i
]->contents_scale(), contents_scale
);
259 DCHECK_EQ(tilings_
[i
]->raster_source(), raster_source
.get());
262 tilings_
.push_back(PictureLayerTiling::Create(
263 tree_
, contents_scale
, raster_source
, client_
,
264 tiling_interest_area_viewport_multiplier_
,
265 skewport_target_time_in_seconds_
,
266 skewport_extrapolation_limit_in_content_pixels_
));
267 PictureLayerTiling
* appended
= tilings_
.back();
269 tilings_
.sort(LargestToSmallestScaleFunctor());
273 int PictureLayerTilingSet::NumHighResTilings() const {
274 return std::count_if(tilings_
.begin(), tilings_
.end(),
275 [](PictureLayerTiling
* tiling
) {
276 return tiling
->resolution() == HIGH_RESOLUTION
;
280 PictureLayerTiling
* PictureLayerTilingSet::FindTilingWithScale(
282 for (size_t i
= 0; i
< tilings_
.size(); ++i
) {
283 if (tilings_
[i
]->contents_scale() == scale
)
289 PictureLayerTiling
* PictureLayerTilingSet::FindTilingWithResolution(
290 TileResolution resolution
) const {
291 auto iter
= std::find_if(tilings_
.begin(), tilings_
.end(),
292 [resolution
](const PictureLayerTiling
* tiling
) {
293 return tiling
->resolution() == resolution
;
295 if (iter
== tilings_
.end())
300 void PictureLayerTilingSet::RemoveTilingsBelowScale(float minimum_scale
) {
302 tilings_
.remove_if([minimum_scale
](PictureLayerTiling
* tiling
) {
303 return tiling
->contents_scale() < minimum_scale
;
305 tilings_
.erase(to_remove
, tilings_
.end());
308 void PictureLayerTilingSet::RemoveTilingsAboveScale(float maximum_scale
) {
310 tilings_
.remove_if([maximum_scale
](PictureLayerTiling
* tiling
) {
311 return tiling
->contents_scale() > maximum_scale
;
313 tilings_
.erase(to_remove
, tilings_
.end());
316 void PictureLayerTilingSet::RemoveAllTilings() {
320 void PictureLayerTilingSet::Remove(PictureLayerTiling
* tiling
) {
321 ScopedPtrVector
<PictureLayerTiling
>::iterator iter
=
322 std::find(tilings_
.begin(), tilings_
.end(), tiling
);
323 if (iter
== tilings_
.end())
325 tilings_
.erase(iter
);
328 void PictureLayerTilingSet::RemoveAllTiles() {
329 for (size_t i
= 0; i
< tilings_
.size(); ++i
)
330 tilings_
[i
]->Reset();
333 float PictureLayerTilingSet::GetSnappedContentsScale(
335 float snap_to_existing_tiling_ratio
) const {
336 // If a tiling exists within the max snapping ratio, snap to its scale.
337 float snapped_contents_scale
= start_scale
;
338 float snapped_ratio
= snap_to_existing_tiling_ratio
;
339 for (const auto* tiling
: tilings_
) {
340 float tiling_contents_scale
= tiling
->contents_scale();
341 float ratio
= LargerRatio(tiling_contents_scale
, start_scale
);
342 if (ratio
< snapped_ratio
) {
343 snapped_contents_scale
= tiling_contents_scale
;
344 snapped_ratio
= ratio
;
347 return snapped_contents_scale
;
350 float PictureLayerTilingSet::GetMaximumContentsScale() const {
351 if (tilings_
.empty())
353 // The first tiling has the largest contents scale.
354 return tilings_
[0]->contents_scale();
357 bool PictureLayerTilingSet::UpdateTilePriorities(
358 const gfx::Rect
& required_rect_in_layer_space
,
359 float ideal_contents_scale
,
360 double current_frame_time_in_seconds
,
361 const Occlusion
& occlusion_in_layer_space
,
362 bool can_require_tiles_for_activation
) {
363 bool updated
= false;
364 for (auto* tiling
: tilings_
) {
365 tiling
->set_can_require_tiles_for_activation(
366 can_require_tiles_for_activation
);
367 updated
|= tiling
->ComputeTilePriorityRects(
368 required_rect_in_layer_space
, ideal_contents_scale
,
369 current_frame_time_in_seconds
, occlusion_in_layer_space
);
374 void PictureLayerTilingSet::GetAllPrioritizedTilesForTracing(
375 std::vector
<PrioritizedTile
>* prioritized_tiles
) const {
376 for (auto* tiling
: tilings_
)
377 tiling
->GetAllPrioritizedTilesForTracing(prioritized_tiles
);
380 PictureLayerTilingSet::CoverageIterator::CoverageIterator(
381 const PictureLayerTilingSet
* set
,
382 float contents_scale
,
383 const gfx::Rect
& content_rect
,
384 float ideal_contents_scale
)
386 contents_scale_(contents_scale
),
387 ideal_contents_scale_(ideal_contents_scale
),
388 current_tiling_(-1) {
389 missing_region_
.Union(content_rect
);
391 for (ideal_tiling_
= 0;
392 static_cast<size_t>(ideal_tiling_
) < set_
->tilings_
.size();
394 PictureLayerTiling
* tiling
= set_
->tilings_
[ideal_tiling_
];
395 if (tiling
->contents_scale() < ideal_contents_scale_
) {
396 if (ideal_tiling_
> 0)
402 DCHECK_LE(set_
->tilings_
.size(),
403 static_cast<size_t>(std::numeric_limits
<int>::max()));
405 int num_tilings
= set_
->tilings_
.size();
406 if (ideal_tiling_
== num_tilings
&& ideal_tiling_
> 0)
412 PictureLayerTilingSet::CoverageIterator::~CoverageIterator() {
415 gfx::Rect
PictureLayerTilingSet::CoverageIterator::geometry_rect() const {
417 if (!region_iter_
.has_rect())
419 return region_iter_
.rect();
421 return tiling_iter_
.geometry_rect();
424 gfx::RectF
PictureLayerTilingSet::CoverageIterator::texture_rect() const {
427 return tiling_iter_
.texture_rect();
430 Tile
* PictureLayerTilingSet::CoverageIterator::operator->() const {
433 return *tiling_iter_
;
436 Tile
* PictureLayerTilingSet::CoverageIterator::operator*() const {
439 return *tiling_iter_
;
442 TileResolution
PictureLayerTilingSet::CoverageIterator::resolution() const {
443 const PictureLayerTiling
* tiling
= CurrentTiling();
445 return tiling
->resolution();
448 PictureLayerTiling
* PictureLayerTilingSet::CoverageIterator::CurrentTiling()
450 if (current_tiling_
< 0)
452 if (static_cast<size_t>(current_tiling_
) >= set_
->tilings_
.size())
454 return set_
->tilings_
[current_tiling_
];
457 int PictureLayerTilingSet::CoverageIterator::NextTiling() const {
458 // Order returned by this method is:
459 // 1. Ideal tiling index
460 // 2. Tiling index < Ideal in decreasing order (higher res than ideal)
461 // 3. Tiling index > Ideal in increasing order (lower res than ideal)
462 // 4. Tiling index > tilings.size() (invalid index)
463 if (current_tiling_
< 0)
464 return ideal_tiling_
;
465 else if (current_tiling_
> ideal_tiling_
)
466 return current_tiling_
+ 1;
467 else if (current_tiling_
)
468 return current_tiling_
- 1;
470 return ideal_tiling_
+ 1;
473 PictureLayerTilingSet::CoverageIterator
&
474 PictureLayerTilingSet::CoverageIterator::operator++() {
475 bool first_time
= current_tiling_
< 0;
477 if (!*this && !first_time
)
483 // Loop until we find a valid place to stop.
485 while (tiling_iter_
&&
486 (!*tiling_iter_
|| !tiling_iter_
->draw_info().IsReadyToDraw())) {
487 missing_region_
.Union(tiling_iter_
.geometry_rect());
493 // If the set of current rects for this tiling is done, go to the next
494 // tiling and set up to iterate through all of the remaining holes.
495 // This will also happen the first time through the loop.
496 if (!region_iter_
.has_rect()) {
497 current_tiling_
= NextTiling();
498 current_region_
.Swap(&missing_region_
);
499 missing_region_
.Clear();
500 region_iter_
= Region::Iterator(current_region_
);
502 // All done and all filled.
503 if (!region_iter_
.has_rect()) {
504 current_tiling_
= set_
->tilings_
.size();
508 // No more valid tiles, return this checkerboard rect.
509 if (current_tiling_
>= static_cast<int>(set_
->tilings_
.size()))
513 // Pop a rect off. If there are no more tilings, then these will be
514 // treated as geometry with null tiles that the caller can checkerboard.
515 gfx::Rect last_rect
= region_iter_
.rect();
518 // Done, found next checkerboard rect to return.
519 if (current_tiling_
>= static_cast<int>(set_
->tilings_
.size()))
522 // Construct a new iterator for the next tiling, but we need to loop
523 // again until we get to a valid one.
524 tiling_iter_
= PictureLayerTiling::CoverageIterator(
525 set_
->tilings_
[current_tiling_
],
533 PictureLayerTilingSet::CoverageIterator::operator bool() const {
534 return current_tiling_
< static_cast<int>(set_
->tilings_
.size()) ||
535 region_iter_
.has_rect();
538 void PictureLayerTilingSet::AsValueInto(
539 base::trace_event::TracedValue
* state
) const {
540 for (size_t i
= 0; i
< tilings_
.size(); ++i
) {
541 state
->BeginDictionary();
542 tilings_
[i
]->AsValueInto(state
);
543 state
->EndDictionary();
547 size_t PictureLayerTilingSet::GPUMemoryUsageInBytes() const {
549 for (size_t i
= 0; i
< tilings_
.size(); ++i
)
550 amount
+= tilings_
[i
]->GPUMemoryUsageInBytes();
554 PictureLayerTilingSet::TilingRange
PictureLayerTilingSet::GetTilingRange(
555 TilingRangeType type
) const {
556 // Doesn't seem to be the case right now but if it ever becomes a performance
557 // problem to compute these ranges each time this function is called, we can
558 // compute them only when the tiling set has changed instead.
559 TilingRange
high_res_range(0, 0);
560 TilingRange
low_res_range(tilings_
.size(), tilings_
.size());
561 for (size_t i
= 0; i
< tilings_
.size(); ++i
) {
562 const PictureLayerTiling
* tiling
= tilings_
[i
];
563 if (tiling
->resolution() == HIGH_RESOLUTION
)
564 high_res_range
= TilingRange(i
, i
+ 1);
565 if (tiling
->resolution() == LOW_RESOLUTION
)
566 low_res_range
= TilingRange(i
, i
+ 1);
569 TilingRange
range(0, 0);
571 case HIGHER_THAN_HIGH_RES
:
572 range
= TilingRange(0, high_res_range
.start
);
575 range
= high_res_range
;
577 case BETWEEN_HIGH_AND_LOW_RES
:
578 // TODO(vmpstr): This code assumes that high res tiling will come before
579 // low res tiling, however there are cases where this assumption is
580 // violated. As a result, it's better to be safe in these situations,
581 // since otherwise we can end up accessing a tiling that doesn't exist.
582 // See crbug.com/429397 for high res tiling appearing after low res
583 // tiling discussion/fixes.
584 if (high_res_range
.start
<= low_res_range
.start
)
585 range
= TilingRange(high_res_range
.end
, low_res_range
.start
);
587 range
= TilingRange(low_res_range
.end
, high_res_range
.start
);
590 range
= low_res_range
;
592 case LOWER_THAN_LOW_RES
:
593 range
= TilingRange(low_res_range
.end
, tilings_
.size());
597 DCHECK_LE(range
.start
, range
.end
);