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/resources/picture_layer_tiling_set.h"
15 class LargestToSmallestScaleFunctor
{
17 bool operator() (PictureLayerTiling
* left
, PictureLayerTiling
* right
) {
18 return left
->contents_scale() > right
->contents_scale();
22 inline float LargerRatio(float float1
, float float2
) {
23 DCHECK_GT(float1
, 0.f
);
24 DCHECK_GT(float2
, 0.f
);
25 return float1
> float2
? float1
/ float2
: float2
/ float1
;
31 scoped_ptr
<PictureLayerTilingSet
> PictureLayerTilingSet::Create(
32 PictureLayerTilingClient
* client
,
33 size_t max_tiles_for_interest_area
,
34 float skewport_target_time_in_seconds
,
35 int skewport_extrapolation_limit_in_content_pixels
) {
36 return make_scoped_ptr(new PictureLayerTilingSet(
37 client
, max_tiles_for_interest_area
, skewport_target_time_in_seconds
,
38 skewport_extrapolation_limit_in_content_pixels
));
41 PictureLayerTilingSet::PictureLayerTilingSet(
42 PictureLayerTilingClient
* client
,
43 size_t max_tiles_for_interest_area
,
44 float skewport_target_time_in_seconds
,
45 int skewport_extrapolation_limit_in_content_pixels
)
46 : max_tiles_for_interest_area_(max_tiles_for_interest_area
),
47 skewport_target_time_in_seconds_(skewport_target_time_in_seconds
),
48 skewport_extrapolation_limit_in_content_pixels_(
49 skewport_extrapolation_limit_in_content_pixels
),
53 PictureLayerTilingSet::~PictureLayerTilingSet() {
56 void PictureLayerTilingSet::UpdateTilingsToCurrentRasterSource(
57 scoped_refptr
<RasterSource
> raster_source
,
58 const PictureLayerTilingSet
* twin_set
,
59 const Region
& layer_invalidation
,
60 float minimum_contents_scale
,
61 float maximum_contents_scale
) {
62 RemoveTilingsBelowScale(minimum_contents_scale
);
63 RemoveTilingsAboveScale(maximum_contents_scale
);
65 // Copy over tilings that are shared with the |twin_set| tiling set (if it
68 for (PictureLayerTiling
* twin_tiling
: twin_set
->tilings_
) {
69 float contents_scale
= twin_tiling
->contents_scale();
70 DCHECK_GE(contents_scale
, minimum_contents_scale
);
71 DCHECK_LE(contents_scale
, maximum_contents_scale
);
73 PictureLayerTiling
* this_tiling
= FindTilingWithScale(contents_scale
);
75 scoped_ptr
<PictureLayerTiling
> new_tiling
= PictureLayerTiling::Create(
76 contents_scale
, raster_source
, client_
,
77 max_tiles_for_interest_area_
, skewport_target_time_in_seconds_
,
78 skewport_extrapolation_limit_in_content_pixels_
);
79 tilings_
.push_back(new_tiling
.Pass());
80 this_tiling
= tilings_
.back();
82 this_tiling
->CloneTilesAndPropertiesFrom(*twin_tiling
);
86 // For unshared tilings, invalidate tiles and update them to the new raster
88 for (PictureLayerTiling
* tiling
: tilings_
) {
89 if (twin_set
&& twin_set
->FindTilingWithScale(tiling
->contents_scale()))
92 tiling
->SetRasterSourceAndResize(raster_source
);
93 tiling
->Invalidate(layer_invalidation
);
94 tiling
->SetRasterSourceOnTiles();
95 // This is needed for cases where the live tiles rect didn't change but
96 // recordings exist in the raster source that did not exist on the last
98 tiling
->CreateMissingTilesInLiveTilesRect();
100 // If |twin_set| is present, use the resolutions from there. Otherwise leave
101 // all resolutions as they are.
103 tiling
->set_resolution(NON_IDEAL_RESOLUTION
);
106 tilings_
.sort(LargestToSmallestScaleFunctor());
109 for (PictureLayerTiling
* tiling
: tilings_
) {
110 DCHECK(tiling
->tile_size() ==
111 client_
->CalculateTileSize(tiling
->tiling_size()))
112 << "tile_size: " << tiling
->tile_size().ToString()
113 << " tiling_size: " << tiling
->tiling_size().ToString()
114 << " CalculateTileSize: "
115 << client_
->CalculateTileSize(tiling
->tiling_size()).ToString();
118 if (!tilings_
.empty()) {
119 DCHECK_LE(NumHighResTilings(), 1);
120 // When commiting from the main thread the high res tiling may get dropped,
121 // but when cloning to the active tree, there should always be one.
123 DCHECK_EQ(1, NumHighResTilings())
124 << " num tilings on active: " << tilings_
.size()
125 << " num tilings on pending: " << twin_set
->tilings_
.size()
126 << " num high res on pending: " << twin_set
->NumHighResTilings()
127 << " are on active tree: " << (client_
->GetTree() == ACTIVE_TREE
);
133 void PictureLayerTilingSet::CleanUpTilings(
134 float min_acceptable_high_res_scale
,
135 float max_acceptable_high_res_scale
,
136 const std::vector
<PictureLayerTiling
*>& needed_tilings
,
137 bool should_have_low_res
,
138 PictureLayerTilingSet
* twin_set
,
139 PictureLayerTilingSet
* recycled_twin_set
) {
140 float twin_low_res_scale
= 0.f
;
142 PictureLayerTiling
* tiling
=
143 twin_set
->FindTilingWithResolution(LOW_RESOLUTION
);
145 twin_low_res_scale
= tiling
->contents_scale();
148 std::vector
<PictureLayerTiling
*> to_remove
;
149 for (auto* tiling
: tilings_
) {
150 // Keep all tilings within the min/max scales.
151 if (tiling
->contents_scale() >= min_acceptable_high_res_scale
&&
152 tiling
->contents_scale() <= max_acceptable_high_res_scale
) {
156 // Keep low resolution tilings, if the tiling set should have them.
157 if (should_have_low_res
&&
158 (tiling
->resolution() == LOW_RESOLUTION
||
159 tiling
->contents_scale() == twin_low_res_scale
)) {
163 // Don't remove tilings that are required.
164 if (std::find(needed_tilings
.begin(), needed_tilings
.end(), tiling
) !=
165 needed_tilings
.end()) {
169 to_remove
.push_back(tiling
);
172 for (auto* tiling
: to_remove
) {
173 PictureLayerTiling
* recycled_twin_tiling
=
175 ? recycled_twin_set
->FindTilingWithScale(tiling
->contents_scale())
177 // Remove the tiling from the recycle tree. Note that we ignore resolution,
178 // since we don't need to maintain high/low res on the recycle set.
179 if (recycled_twin_tiling
)
180 recycled_twin_set
->Remove(recycled_twin_tiling
);
182 DCHECK_NE(HIGH_RESOLUTION
, tiling
->resolution());
187 void PictureLayerTilingSet::RemoveNonIdealTilings() {
188 auto to_remove
= tilings_
.remove_if([](PictureLayerTiling
* t
) {
189 return t
->resolution() == NON_IDEAL_RESOLUTION
;
191 tilings_
.erase(to_remove
, tilings_
.end());
194 void PictureLayerTilingSet::MarkAllTilingsNonIdeal() {
195 for (auto* tiling
: tilings_
)
196 tiling
->set_resolution(NON_IDEAL_RESOLUTION
);
199 PictureLayerTiling
* PictureLayerTilingSet::AddTiling(
200 float contents_scale
,
201 scoped_refptr
<RasterSource
> raster_source
) {
202 for (size_t i
= 0; i
< tilings_
.size(); ++i
) {
203 DCHECK_NE(tilings_
[i
]->contents_scale(), contents_scale
);
204 DCHECK_EQ(tilings_
[i
]->raster_source(), raster_source
.get());
207 tilings_
.push_back(PictureLayerTiling::Create(
208 contents_scale
, raster_source
, client_
, max_tiles_for_interest_area_
,
209 skewport_target_time_in_seconds_
,
210 skewport_extrapolation_limit_in_content_pixels_
));
211 PictureLayerTiling
* appended
= tilings_
.back();
213 tilings_
.sort(LargestToSmallestScaleFunctor());
217 int PictureLayerTilingSet::NumHighResTilings() const {
218 return std::count_if(tilings_
.begin(), tilings_
.end(),
219 [](PictureLayerTiling
* tiling
) {
220 return tiling
->resolution() == HIGH_RESOLUTION
;
224 PictureLayerTiling
* PictureLayerTilingSet::FindTilingWithScale(
226 for (size_t i
= 0; i
< tilings_
.size(); ++i
) {
227 if (tilings_
[i
]->contents_scale() == scale
)
233 PictureLayerTiling
* PictureLayerTilingSet::FindTilingWithResolution(
234 TileResolution resolution
) const {
235 auto iter
= std::find_if(tilings_
.begin(), tilings_
.end(),
236 [resolution
](const PictureLayerTiling
* tiling
) {
237 return tiling
->resolution() == resolution
;
239 if (iter
== tilings_
.end())
244 void PictureLayerTilingSet::RemoveTilingsBelowScale(float minimum_scale
) {
246 tilings_
.remove_if([minimum_scale
](PictureLayerTiling
* tiling
) {
247 return tiling
->contents_scale() < minimum_scale
;
249 tilings_
.erase(to_remove
, tilings_
.end());
252 void PictureLayerTilingSet::RemoveTilingsAboveScale(float maximum_scale
) {
254 tilings_
.remove_if([maximum_scale
](PictureLayerTiling
* tiling
) {
255 return tiling
->contents_scale() > maximum_scale
;
257 tilings_
.erase(to_remove
, tilings_
.end());
260 void PictureLayerTilingSet::RemoveAllTilings() {
264 void PictureLayerTilingSet::Remove(PictureLayerTiling
* tiling
) {
265 ScopedPtrVector
<PictureLayerTiling
>::iterator iter
=
266 std::find(tilings_
.begin(), tilings_
.end(), tiling
);
267 if (iter
== tilings_
.end())
269 tilings_
.erase(iter
);
272 void PictureLayerTilingSet::RemoveAllTiles() {
273 for (size_t i
= 0; i
< tilings_
.size(); ++i
)
274 tilings_
[i
]->Reset();
277 float PictureLayerTilingSet::GetSnappedContentsScale(
279 float snap_to_existing_tiling_ratio
) const {
280 // If a tiling exists within the max snapping ratio, snap to its scale.
281 float snapped_contents_scale
= start_scale
;
282 float snapped_ratio
= snap_to_existing_tiling_ratio
;
283 for (const auto* tiling
: tilings_
) {
284 float tiling_contents_scale
= tiling
->contents_scale();
285 float ratio
= LargerRatio(tiling_contents_scale
, start_scale
);
286 if (ratio
< snapped_ratio
) {
287 snapped_contents_scale
= tiling_contents_scale
;
288 snapped_ratio
= ratio
;
291 return snapped_contents_scale
;
294 float PictureLayerTilingSet::GetMaximumContentsScale() const {
295 if (tilings_
.empty())
297 // The first tiling has the largest contents scale.
298 return tilings_
[0]->contents_scale();
301 bool PictureLayerTilingSet::UpdateTilePriorities(
302 const gfx::Rect
& required_rect_in_layer_space
,
303 float ideal_contents_scale
,
304 double current_frame_time_in_seconds
,
305 const Occlusion
& occlusion_in_layer_space
,
306 bool can_require_tiles_for_activation
) {
307 bool updated
= false;
308 for (auto* tiling
: tilings_
) {
309 tiling
->set_can_require_tiles_for_activation(
310 can_require_tiles_for_activation
);
311 updated
|= tiling
->ComputeTilePriorityRects(
312 required_rect_in_layer_space
, ideal_contents_scale
,
313 current_frame_time_in_seconds
, occlusion_in_layer_space
);
318 void PictureLayerTilingSet::GetAllTilesForTracing(
319 std::set
<const Tile
*>* tiles
) const {
320 for (auto* tiling
: tilings_
)
321 tiling
->GetAllTilesForTracing(tiles
);
324 PictureLayerTilingSet::CoverageIterator::CoverageIterator(
325 const PictureLayerTilingSet
* set
,
326 float contents_scale
,
327 const gfx::Rect
& content_rect
,
328 float ideal_contents_scale
)
330 contents_scale_(contents_scale
),
331 ideal_contents_scale_(ideal_contents_scale
),
332 current_tiling_(-1) {
333 missing_region_
.Union(content_rect
);
335 for (ideal_tiling_
= 0;
336 static_cast<size_t>(ideal_tiling_
) < set_
->tilings_
.size();
338 PictureLayerTiling
* tiling
= set_
->tilings_
[ideal_tiling_
];
339 if (tiling
->contents_scale() < ideal_contents_scale_
) {
340 if (ideal_tiling_
> 0)
346 DCHECK_LE(set_
->tilings_
.size(),
347 static_cast<size_t>(std::numeric_limits
<int>::max()));
349 int num_tilings
= set_
->tilings_
.size();
350 if (ideal_tiling_
== num_tilings
&& ideal_tiling_
> 0)
356 PictureLayerTilingSet::CoverageIterator::~CoverageIterator() {
359 gfx::Rect
PictureLayerTilingSet::CoverageIterator::geometry_rect() const {
361 if (!region_iter_
.has_rect())
363 return region_iter_
.rect();
365 return tiling_iter_
.geometry_rect();
368 gfx::RectF
PictureLayerTilingSet::CoverageIterator::texture_rect() const {
371 return tiling_iter_
.texture_rect();
374 Tile
* PictureLayerTilingSet::CoverageIterator::operator->() const {
377 return *tiling_iter_
;
380 Tile
* PictureLayerTilingSet::CoverageIterator::operator*() const {
383 return *tiling_iter_
;
386 TileResolution
PictureLayerTilingSet::CoverageIterator::resolution() const {
387 const PictureLayerTiling
* tiling
= CurrentTiling();
389 return tiling
->resolution();
392 PictureLayerTiling
* PictureLayerTilingSet::CoverageIterator::CurrentTiling()
394 if (current_tiling_
< 0)
396 if (static_cast<size_t>(current_tiling_
) >= set_
->tilings_
.size())
398 return set_
->tilings_
[current_tiling_
];
401 int PictureLayerTilingSet::CoverageIterator::NextTiling() const {
402 // Order returned by this method is:
403 // 1. Ideal tiling index
404 // 2. Tiling index < Ideal in decreasing order (higher res than ideal)
405 // 3. Tiling index > Ideal in increasing order (lower res than ideal)
406 // 4. Tiling index > tilings.size() (invalid index)
407 if (current_tiling_
< 0)
408 return ideal_tiling_
;
409 else if (current_tiling_
> ideal_tiling_
)
410 return current_tiling_
+ 1;
411 else if (current_tiling_
)
412 return current_tiling_
- 1;
414 return ideal_tiling_
+ 1;
417 PictureLayerTilingSet::CoverageIterator
&
418 PictureLayerTilingSet::CoverageIterator::operator++() {
419 bool first_time
= current_tiling_
< 0;
421 if (!*this && !first_time
)
427 // Loop until we find a valid place to stop.
429 while (tiling_iter_
&&
430 (!*tiling_iter_
|| !tiling_iter_
->IsReadyToDraw())) {
431 missing_region_
.Union(tiling_iter_
.geometry_rect());
437 // If the set of current rects for this tiling is done, go to the next
438 // tiling and set up to iterate through all of the remaining holes.
439 // This will also happen the first time through the loop.
440 if (!region_iter_
.has_rect()) {
441 current_tiling_
= NextTiling();
442 current_region_
.Swap(&missing_region_
);
443 missing_region_
.Clear();
444 region_iter_
= Region::Iterator(current_region_
);
446 // All done and all filled.
447 if (!region_iter_
.has_rect()) {
448 current_tiling_
= set_
->tilings_
.size();
452 // No more valid tiles, return this checkerboard rect.
453 if (current_tiling_
>= static_cast<int>(set_
->tilings_
.size()))
457 // Pop a rect off. If there are no more tilings, then these will be
458 // treated as geometry with null tiles that the caller can checkerboard.
459 gfx::Rect last_rect
= region_iter_
.rect();
462 // Done, found next checkerboard rect to return.
463 if (current_tiling_
>= static_cast<int>(set_
->tilings_
.size()))
466 // Construct a new iterator for the next tiling, but we need to loop
467 // again until we get to a valid one.
468 tiling_iter_
= PictureLayerTiling::CoverageIterator(
469 set_
->tilings_
[current_tiling_
],
477 PictureLayerTilingSet::CoverageIterator::operator bool() const {
478 return current_tiling_
< static_cast<int>(set_
->tilings_
.size()) ||
479 region_iter_
.has_rect();
482 void PictureLayerTilingSet::AsValueInto(
483 base::trace_event::TracedValue
* state
) const {
484 for (size_t i
= 0; i
< tilings_
.size(); ++i
) {
485 state
->BeginDictionary();
486 tilings_
[i
]->AsValueInto(state
);
487 state
->EndDictionary();
491 size_t PictureLayerTilingSet::GPUMemoryUsageInBytes() const {
493 for (size_t i
= 0; i
< tilings_
.size(); ++i
)
494 amount
+= tilings_
[i
]->GPUMemoryUsageInBytes();
498 PictureLayerTilingSet::TilingRange
PictureLayerTilingSet::GetTilingRange(
499 TilingRangeType type
) const {
500 // Doesn't seem to be the case right now but if it ever becomes a performance
501 // problem to compute these ranges each time this function is called, we can
502 // compute them only when the tiling set has changed instead.
503 TilingRange
high_res_range(0, 0);
504 TilingRange
low_res_range(tilings_
.size(), tilings_
.size());
505 for (size_t i
= 0; i
< tilings_
.size(); ++i
) {
506 const PictureLayerTiling
* tiling
= tilings_
[i
];
507 if (tiling
->resolution() == HIGH_RESOLUTION
)
508 high_res_range
= TilingRange(i
, i
+ 1);
509 if (tiling
->resolution() == LOW_RESOLUTION
)
510 low_res_range
= TilingRange(i
, i
+ 1);
513 TilingRange
range(0, 0);
515 case HIGHER_THAN_HIGH_RES
:
516 range
= TilingRange(0, high_res_range
.start
);
519 range
= high_res_range
;
521 case BETWEEN_HIGH_AND_LOW_RES
:
522 // TODO(vmpstr): This code assumes that high res tiling will come before
523 // low res tiling, however there are cases where this assumption is
524 // violated. As a result, it's better to be safe in these situations,
525 // since otherwise we can end up accessing a tiling that doesn't exist.
526 // See crbug.com/429397 for high res tiling appearing after low res
527 // tiling discussion/fixes.
528 if (high_res_range
.start
<= low_res_range
.start
)
529 range
= TilingRange(high_res_range
.end
, low_res_range
.start
);
531 range
= TilingRange(low_res_range
.end
, high_res_range
.start
);
534 range
= low_res_range
;
536 case LOWER_THAN_LOW_RES
:
537 range
= TilingRange(low_res_range
.end
, tilings_
.size());
541 DCHECK_LE(range
.start
, range
.end
);