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"
13 class LargestToSmallestScaleFunctor
{
15 bool operator() (PictureLayerTiling
* left
, PictureLayerTiling
* right
) {
16 return left
->contents_scale() > right
->contents_scale();
23 PictureLayerTilingSet::PictureLayerTilingSet(
24 PictureLayerTilingClient
* client
,
25 gfx::Size layer_bounds
)
27 layer_bounds_(layer_bounds
) {
30 PictureLayerTilingSet::~PictureLayerTilingSet() {
33 void PictureLayerTilingSet::SetClient(PictureLayerTilingClient
* client
) {
35 for (size_t i
= 0; i
< tilings_
.size(); ++i
)
36 tilings_
[i
]->SetClient(client_
);
39 void PictureLayerTilingSet::SyncTilings(
40 const PictureLayerTilingSet
& other
,
41 gfx::Size new_layer_bounds
,
42 const Region
& layer_invalidation
,
43 float minimum_contents_scale
) {
44 if (new_layer_bounds
.IsEmpty()) {
46 layer_bounds_
= new_layer_bounds
;
50 tilings_
.reserve(other
.tilings_
.size());
52 // Remove any tilings that aren't in |other| or don't meet the minimum.
53 for (size_t i
= 0; i
< tilings_
.size(); ++i
) {
54 float scale
= tilings_
[i
]->contents_scale();
55 if (scale
>= minimum_contents_scale
&& !!other
.TilingAtScale(scale
))
57 // Swap with the last element and remove it.
58 tilings_
.swap(tilings_
.begin() + i
, tilings_
.end() - 1);
63 // Add any missing tilings from |other| that meet the minimum.
64 for (size_t i
= 0; i
< other
.tilings_
.size(); ++i
) {
65 float contents_scale
= other
.tilings_
[i
]->contents_scale();
66 if (contents_scale
< minimum_contents_scale
)
68 if (PictureLayerTiling
* this_tiling
= TilingAtScale(contents_scale
)) {
69 this_tiling
->set_resolution(other
.tilings_
[i
]->resolution());
71 // These two calls must come before updating the pile, because they may
72 // destroy tiles that the new pile cannot raster.
73 this_tiling
->SetLayerBounds(new_layer_bounds
);
74 this_tiling
->Invalidate(layer_invalidation
);
76 this_tiling
->UpdateTilesToCurrentPile();
77 this_tiling
->CreateMissingTilesInLiveTilesRect();
79 DCHECK(this_tiling
->tile_size() ==
80 client_
->CalculateTileSize(this_tiling
->ContentRect().size()));
83 scoped_ptr
<PictureLayerTiling
> new_tiling
= PictureLayerTiling::Create(
87 new_tiling
->set_resolution(other
.tilings_
[i
]->resolution());
88 tilings_
.push_back(new_tiling
.Pass());
90 tilings_
.sort(LargestToSmallestScaleFunctor());
92 layer_bounds_
= new_layer_bounds
;
95 void PictureLayerTilingSet::SetCanUseLCDText(bool can_use_lcd_text
) {
96 for (size_t i
= 0; i
< tilings_
.size(); ++i
)
97 tilings_
[i
]->SetCanUseLCDText(can_use_lcd_text
);
100 PictureLayerTiling
* PictureLayerTilingSet::AddTiling(float contents_scale
) {
101 for (size_t i
= 0; i
< tilings_
.size(); ++i
)
102 DCHECK_NE(tilings_
[i
]->contents_scale(), contents_scale
);
104 tilings_
.push_back(PictureLayerTiling::Create(contents_scale
,
107 PictureLayerTiling
* appended
= tilings_
.back();
109 tilings_
.sort(LargestToSmallestScaleFunctor());
113 int PictureLayerTilingSet::NumHighResTilings() const {
114 int num_high_res
= 0;
115 for (size_t i
= 0; i
< tilings_
.size(); ++i
) {
116 if (tilings_
[i
]->resolution() == HIGH_RESOLUTION
)
122 PictureLayerTiling
* PictureLayerTilingSet::TilingAtScale(float scale
) const {
123 for (size_t i
= 0; i
< tilings_
.size(); ++i
) {
124 if (tilings_
[i
]->contents_scale() == scale
)
130 void PictureLayerTilingSet::RemoveAllTilings() {
134 void PictureLayerTilingSet::Remove(PictureLayerTiling
* tiling
) {
135 ScopedPtrVector
<PictureLayerTiling
>::iterator iter
=
136 std::find(tilings_
.begin(), tilings_
.end(), tiling
);
137 if (iter
== tilings_
.end())
139 tilings_
.erase(iter
);
142 void PictureLayerTilingSet::RemoveAllTiles() {
143 for (size_t i
= 0; i
< tilings_
.size(); ++i
)
144 tilings_
[i
]->Reset();
147 PictureLayerTilingSet::CoverageIterator::CoverageIterator(
148 const PictureLayerTilingSet
* set
,
149 float contents_scale
,
150 const gfx::Rect
& content_rect
,
151 float ideal_contents_scale
)
153 contents_scale_(contents_scale
),
154 ideal_contents_scale_(ideal_contents_scale
),
155 current_tiling_(-1) {
156 missing_region_
.Union(content_rect
);
158 for (ideal_tiling_
= 0;
159 static_cast<size_t>(ideal_tiling_
) < set_
->tilings_
.size();
161 PictureLayerTiling
* tiling
= set_
->tilings_
[ideal_tiling_
];
162 if (tiling
->contents_scale() < ideal_contents_scale_
) {
163 if (ideal_tiling_
> 0)
169 DCHECK_LE(set_
->tilings_
.size(),
170 static_cast<size_t>(std::numeric_limits
<int>::max()));
172 int num_tilings
= set_
->tilings_
.size();
173 if (ideal_tiling_
== num_tilings
&& ideal_tiling_
> 0)
179 PictureLayerTilingSet::CoverageIterator::~CoverageIterator() {
182 gfx::Rect
PictureLayerTilingSet::CoverageIterator::geometry_rect() const {
184 if (!region_iter_
.has_rect())
186 return region_iter_
.rect();
188 return tiling_iter_
.geometry_rect();
191 gfx::RectF
PictureLayerTilingSet::CoverageIterator::texture_rect() const {
194 return tiling_iter_
.texture_rect();
197 gfx::Size
PictureLayerTilingSet::CoverageIterator::texture_size() const {
200 return tiling_iter_
.texture_size();
203 Tile
* PictureLayerTilingSet::CoverageIterator::operator->() const {
206 return *tiling_iter_
;
209 Tile
* PictureLayerTilingSet::CoverageIterator::operator*() const {
212 return *tiling_iter_
;
215 PictureLayerTiling
* PictureLayerTilingSet::CoverageIterator::CurrentTiling() {
216 if (current_tiling_
< 0)
218 if (static_cast<size_t>(current_tiling_
) >= set_
->tilings_
.size())
220 return set_
->tilings_
[current_tiling_
];
223 int PictureLayerTilingSet::CoverageIterator::NextTiling() const {
224 // Order returned by this method is:
225 // 1. Ideal tiling index
226 // 2. Tiling index < Ideal in decreasing order (higher res than ideal)
227 // 3. Tiling index > Ideal in increasing order (lower res than ideal)
228 // 4. Tiling index > tilings.size() (invalid index)
229 if (current_tiling_
< 0)
230 return ideal_tiling_
;
231 else if (current_tiling_
> ideal_tiling_
)
232 return current_tiling_
+ 1;
233 else if (current_tiling_
)
234 return current_tiling_
- 1;
236 return ideal_tiling_
+ 1;
239 PictureLayerTilingSet::CoverageIterator
&
240 PictureLayerTilingSet::CoverageIterator::operator++() {
241 bool first_time
= current_tiling_
< 0;
243 if (!*this && !first_time
)
249 // Loop until we find a valid place to stop.
251 while (tiling_iter_
&&
252 (!*tiling_iter_
|| !tiling_iter_
->IsReadyToDraw())) {
253 missing_region_
.Union(tiling_iter_
.geometry_rect());
259 // If the set of current rects for this tiling is done, go to the next
260 // tiling and set up to iterate through all of the remaining holes.
261 // This will also happen the first time through the loop.
262 if (!region_iter_
.has_rect()) {
263 current_tiling_
= NextTiling();
264 current_region_
.Swap(&missing_region_
);
265 missing_region_
.Clear();
266 region_iter_
= Region::Iterator(current_region_
);
268 // All done and all filled.
269 if (!region_iter_
.has_rect()) {
270 current_tiling_
= set_
->tilings_
.size();
274 // No more valid tiles, return this checkerboard rect.
275 if (current_tiling_
>= static_cast<int>(set_
->tilings_
.size()))
279 // Pop a rect off. If there are no more tilings, then these will be
280 // treated as geometry with null tiles that the caller can checkerboard.
281 gfx::Rect last_rect
= region_iter_
.rect();
284 // Done, found next checkerboard rect to return.
285 if (current_tiling_
>= static_cast<int>(set_
->tilings_
.size()))
288 // Construct a new iterator for the next tiling, but we need to loop
289 // again until we get to a valid one.
290 tiling_iter_
= PictureLayerTiling::CoverageIterator(
291 set_
->tilings_
[current_tiling_
],
299 PictureLayerTilingSet::CoverageIterator::operator bool() const {
300 return current_tiling_
< static_cast<int>(set_
->tilings_
.size()) ||
301 region_iter_
.has_rect();
304 void PictureLayerTilingSet::UpdateTilePriorities(
306 gfx::Size device_viewport
,
307 const gfx::Rect
& viewport_in_content_space
,
308 const gfx::Rect
& visible_content_rect
,
309 gfx::Size last_layer_bounds
,
310 gfx::Size current_layer_bounds
,
311 float last_layer_contents_scale
,
312 float current_layer_contents_scale
,
313 const gfx::Transform
& last_screen_transform
,
314 const gfx::Transform
& current_screen_transform
,
315 double current_frame_time_in_seconds
,
316 size_t max_tiles_for_interest_area
) {
317 gfx::Rect viewport_in_layer_space
= gfx::ScaleToEnclosingRect(
318 viewport_in_content_space
,
319 1.f
/ current_layer_contents_scale
);
320 gfx::Rect visible_layer_rect
= gfx::ScaleToEnclosingRect(
321 visible_content_rect
,
322 1.f
/ current_layer_contents_scale
);
324 for (size_t i
= 0; i
< tilings_
.size(); ++i
) {
325 tilings_
[i
]->UpdateTilePriorities(
328 viewport_in_layer_space
,
331 current_layer_bounds
,
332 last_layer_contents_scale
,
333 current_layer_contents_scale
,
334 last_screen_transform
,
335 current_screen_transform
,
336 current_frame_time_in_seconds
,
337 max_tiles_for_interest_area
);
341 void PictureLayerTilingSet::DidBecomeActive() {
342 for (size_t i
= 0; i
< tilings_
.size(); ++i
)
343 tilings_
[i
]->DidBecomeActive();
346 void PictureLayerTilingSet::DidBecomeRecycled() {
347 for (size_t i
= 0; i
< tilings_
.size(); ++i
)
348 tilings_
[i
]->DidBecomeRecycled();
351 scoped_ptr
<base::Value
> PictureLayerTilingSet::AsValue() const {
352 scoped_ptr
<base::ListValue
> state(new base::ListValue());
353 for (size_t i
= 0; i
< tilings_
.size(); ++i
)
354 state
->Append(tilings_
[i
]->AsValue().release());
355 return state
.PassAs
<base::Value
>();
358 size_t PictureLayerTilingSet::GPUMemoryUsageInBytes() const {
360 for (size_t i
= 0; i
< tilings_
.size(); ++i
)
361 amount
+= tilings_
[i
]->GPUMemoryUsageInBytes();