MediaCodecPlayer implementation - stage 6 (respect audio permission)
[chromium-blink-merge.git] / cc / tiles / picture_layer_tiling_set.cc
bloba9e16f58a7343ad190431dd4e53f29f7504c680b
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"
7 #include <limits>
8 #include <set>
9 #include <vector>
11 #include "cc/playback/raster_source.h"
13 namespace cc {
15 namespace {
17 class LargestToSmallestScaleFunctor {
18 public:
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;
30 } // namespace
32 // static
33 scoped_ptr<PictureLayerTilingSet> PictureLayerTilingSet::Create(
34 WhichTree tree,
35 PictureLayerTilingClient* client,
36 size_t tiling_interest_area_padding,
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_padding,
41 skewport_target_time_in_seconds,
42 skewport_extrapolation_limit_in_content_pixels));
45 PictureLayerTilingSet::PictureLayerTilingSet(
46 WhichTree tree,
47 PictureLayerTilingClient* client,
48 size_t tiling_interest_area_padding,
49 float skewport_target_time_in_seconds,
50 int skewport_extrapolation_limit_in_content_pixels)
51 : tiling_interest_area_padding_(tiling_interest_area_padding),
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),
55 tree_(tree),
56 client_(client) {}
58 PictureLayerTilingSet::~PictureLayerTilingSet() {
61 void PictureLayerTilingSet::CopyTilingsAndPropertiesFromPendingTwin(
62 const PictureLayerTilingSet* pending_twin_set,
63 const scoped_refptr<RasterSource>& raster_source,
64 const Region& layer_invalidation) {
65 if (pending_twin_set->tilings_.empty()) {
66 // If the twin (pending) tiling set is empty, it was not updated for the
67 // current frame. So we drop tilings from our set as well, instead of
68 // leaving behind unshared tilings that are all non-ideal.
69 RemoveAllTilings();
70 return;
73 bool tiling_sort_required = false;
74 for (PictureLayerTiling* pending_twin_tiling : pending_twin_set->tilings_) {
75 float contents_scale = pending_twin_tiling->contents_scale();
76 PictureLayerTiling* this_tiling = FindTilingWithScale(contents_scale);
77 if (!this_tiling) {
78 scoped_ptr<PictureLayerTiling> new_tiling = PictureLayerTiling::Create(
79 tree_, contents_scale, raster_source, client_,
80 tiling_interest_area_padding_, skewport_target_time_in_seconds_,
81 skewport_extrapolation_limit_in_content_pixels_);
82 tilings_.push_back(new_tiling.Pass());
83 this_tiling = tilings_.back();
84 tiling_sort_required = true;
86 this_tiling->TakeTilesAndPropertiesFrom(pending_twin_tiling,
87 layer_invalidation);
90 if (tiling_sort_required)
91 tilings_.sort(LargestToSmallestScaleFunctor());
94 void PictureLayerTilingSet::UpdateTilingsToCurrentRasterSourceForActivation(
95 scoped_refptr<RasterSource> raster_source,
96 const PictureLayerTilingSet* pending_twin_set,
97 const Region& layer_invalidation,
98 float minimum_contents_scale,
99 float maximum_contents_scale) {
100 RemoveTilingsBelowScale(minimum_contents_scale);
101 RemoveTilingsAboveScale(maximum_contents_scale);
103 // Copy over tilings that are shared with the |pending_twin_set| tiling set.
104 // Also, copy all of the properties from twin tilings.
105 CopyTilingsAndPropertiesFromPendingTwin(pending_twin_set, raster_source,
106 layer_invalidation);
108 // If the tiling is not shared (FindTilingWithScale returns nullptr), then
109 // invalidate tiles and update them to the new raster source.
110 for (PictureLayerTiling* tiling : tilings_) {
111 if (pending_twin_set->FindTilingWithScale(tiling->contents_scale()))
112 continue;
114 tiling->SetRasterSourceAndResize(raster_source);
115 tiling->Invalidate(layer_invalidation);
116 // This is needed for cases where the live tiles rect didn't change but
117 // recordings exist in the raster source that did not exist on the last
118 // raster source.
119 tiling->CreateMissingTilesInLiveTilesRect();
121 // |this| is active set and |tiling| is not in the pending set, which means
122 // it is now NON_IDEAL_RESOLUTION. The exception is for LOW_RESOLUTION
123 // tilings, which are computed and created entirely on the active tree.
124 // Since the pending tree does not have them, we should just leave them as
125 // low resolution to not lose them.
126 if (tiling->resolution() != LOW_RESOLUTION)
127 tiling->set_resolution(NON_IDEAL_RESOLUTION);
130 VerifyTilings(pending_twin_set);
133 void PictureLayerTilingSet::UpdateTilingsToCurrentRasterSourceForCommit(
134 scoped_refptr<RasterSource> raster_source,
135 const Region& layer_invalidation,
136 float minimum_contents_scale,
137 float maximum_contents_scale) {
138 RemoveTilingsBelowScale(minimum_contents_scale);
139 RemoveTilingsAboveScale(maximum_contents_scale);
141 // Invalidate tiles and update them to the new raster source.
142 for (PictureLayerTiling* tiling : tilings_) {
143 DCHECK_IMPLIES(tree_ == PENDING_TREE, !tiling->has_tiles());
144 tiling->SetRasterSourceAndResize(raster_source);
146 // We can commit on either active or pending trees, but only active one can
147 // have tiles at this point.
148 if (tree_ == ACTIVE_TREE)
149 tiling->Invalidate(layer_invalidation);
151 // This is needed for cases where the live tiles rect didn't change but
152 // recordings exist in the raster source that did not exist on the last
153 // raster source.
154 tiling->CreateMissingTilesInLiveTilesRect();
156 VerifyTilings(nullptr /* pending_twin_set */);
159 void PictureLayerTilingSet::UpdateRasterSourceDueToLCDChange(
160 const scoped_refptr<RasterSource>& raster_source,
161 const Region& layer_invalidation) {
162 for (PictureLayerTiling* tiling : tilings_) {
163 tiling->SetRasterSourceAndResize(raster_source);
164 tiling->Invalidate(layer_invalidation);
165 // Since the invalidation changed, we need to create any missing tiles in
166 // the live tiles rect again.
167 tiling->CreateMissingTilesInLiveTilesRect();
171 void PictureLayerTilingSet::VerifyTilings(
172 const PictureLayerTilingSet* pending_twin_set) const {
173 #if DCHECK_IS_ON()
174 for (PictureLayerTiling* tiling : tilings_) {
175 DCHECK(tiling->tile_size() ==
176 client_->CalculateTileSize(tiling->tiling_size()))
177 << "tile_size: " << tiling->tile_size().ToString()
178 << " tiling_size: " << tiling->tiling_size().ToString()
179 << " CalculateTileSize: "
180 << client_->CalculateTileSize(tiling->tiling_size()).ToString();
183 if (!tilings_.empty()) {
184 DCHECK_LE(NumHighResTilings(), 1);
185 // When commiting from the main thread the high res tiling may get dropped,
186 // but when cloning to the active tree, there should always be one.
187 if (pending_twin_set) {
188 DCHECK_EQ(1, NumHighResTilings())
189 << " num tilings on active: " << tilings_.size()
190 << " num tilings on pending: " << pending_twin_set->tilings_.size()
191 << " num high res on pending: "
192 << pending_twin_set->NumHighResTilings()
193 << " are on active tree: " << (tree_ == ACTIVE_TREE);
196 #endif
199 void PictureLayerTilingSet::CleanUpTilings(
200 float min_acceptable_high_res_scale,
201 float max_acceptable_high_res_scale,
202 const std::vector<PictureLayerTiling*>& needed_tilings,
203 PictureLayerTilingSet* twin_set) {
204 std::vector<PictureLayerTiling*> to_remove;
205 for (auto* tiling : tilings_) {
206 // Keep all tilings within the min/max scales.
207 if (tiling->contents_scale() >= min_acceptable_high_res_scale &&
208 tiling->contents_scale() <= max_acceptable_high_res_scale) {
209 continue;
212 // Keep low resolution tilings.
213 if (tiling->resolution() == LOW_RESOLUTION)
214 continue;
216 // Don't remove tilings that are required.
217 if (std::find(needed_tilings.begin(), needed_tilings.end(), tiling) !=
218 needed_tilings.end()) {
219 continue;
222 to_remove.push_back(tiling);
225 for (auto* tiling : to_remove) {
226 DCHECK_NE(HIGH_RESOLUTION, tiling->resolution());
227 Remove(tiling);
231 void PictureLayerTilingSet::RemoveNonIdealTilings() {
232 auto to_remove = tilings_.remove_if([](PictureLayerTiling* t) {
233 return t->resolution() == NON_IDEAL_RESOLUTION;
235 tilings_.erase(to_remove, tilings_.end());
238 void PictureLayerTilingSet::MarkAllTilingsNonIdeal() {
239 for (auto* tiling : tilings_)
240 tiling->set_resolution(NON_IDEAL_RESOLUTION);
243 PictureLayerTiling* PictureLayerTilingSet::AddTiling(
244 float contents_scale,
245 scoped_refptr<RasterSource> raster_source) {
246 for (size_t i = 0; i < tilings_.size(); ++i) {
247 DCHECK_NE(tilings_[i]->contents_scale(), contents_scale);
248 DCHECK_EQ(tilings_[i]->raster_source(), raster_source.get());
251 tilings_.push_back(PictureLayerTiling::Create(
252 tree_, contents_scale, raster_source, client_,
253 tiling_interest_area_padding_, skewport_target_time_in_seconds_,
254 skewport_extrapolation_limit_in_content_pixels_));
255 PictureLayerTiling* appended = tilings_.back();
257 tilings_.sort(LargestToSmallestScaleFunctor());
258 return appended;
261 int PictureLayerTilingSet::NumHighResTilings() const {
262 return std::count_if(tilings_.begin(), tilings_.end(),
263 [](PictureLayerTiling* tiling) {
264 return tiling->resolution() == HIGH_RESOLUTION;
268 PictureLayerTiling* PictureLayerTilingSet::FindTilingWithScale(
269 float scale) const {
270 for (size_t i = 0; i < tilings_.size(); ++i) {
271 if (tilings_[i]->contents_scale() == scale)
272 return tilings_[i];
274 return NULL;
277 PictureLayerTiling* PictureLayerTilingSet::FindTilingWithResolution(
278 TileResolution resolution) const {
279 auto iter = std::find_if(tilings_.begin(), tilings_.end(),
280 [resolution](const PictureLayerTiling* tiling) {
281 return tiling->resolution() == resolution;
283 if (iter == tilings_.end())
284 return NULL;
285 return *iter;
288 void PictureLayerTilingSet::RemoveTilingsBelowScale(float minimum_scale) {
289 auto to_remove =
290 tilings_.remove_if([minimum_scale](PictureLayerTiling* tiling) {
291 return tiling->contents_scale() < minimum_scale;
293 tilings_.erase(to_remove, tilings_.end());
296 void PictureLayerTilingSet::RemoveTilingsAboveScale(float maximum_scale) {
297 auto to_remove =
298 tilings_.remove_if([maximum_scale](PictureLayerTiling* tiling) {
299 return tiling->contents_scale() > maximum_scale;
301 tilings_.erase(to_remove, tilings_.end());
304 void PictureLayerTilingSet::RemoveAllTilings() {
305 tilings_.clear();
308 void PictureLayerTilingSet::Remove(PictureLayerTiling* tiling) {
309 ScopedPtrVector<PictureLayerTiling>::iterator iter =
310 std::find(tilings_.begin(), tilings_.end(), tiling);
311 if (iter == tilings_.end())
312 return;
313 tilings_.erase(iter);
316 void PictureLayerTilingSet::RemoveAllTiles() {
317 for (size_t i = 0; i < tilings_.size(); ++i)
318 tilings_[i]->Reset();
321 float PictureLayerTilingSet::GetSnappedContentsScale(
322 float start_scale,
323 float snap_to_existing_tiling_ratio) const {
324 // If a tiling exists within the max snapping ratio, snap to its scale.
325 float snapped_contents_scale = start_scale;
326 float snapped_ratio = snap_to_existing_tiling_ratio;
327 for (const auto* tiling : tilings_) {
328 float tiling_contents_scale = tiling->contents_scale();
329 float ratio = LargerRatio(tiling_contents_scale, start_scale);
330 if (ratio < snapped_ratio) {
331 snapped_contents_scale = tiling_contents_scale;
332 snapped_ratio = ratio;
335 return snapped_contents_scale;
338 float PictureLayerTilingSet::GetMaximumContentsScale() const {
339 if (tilings_.empty())
340 return 0.f;
341 // The first tiling has the largest contents scale.
342 return tilings_[0]->contents_scale();
345 bool PictureLayerTilingSet::UpdateTilePriorities(
346 const gfx::Rect& required_rect_in_layer_space,
347 float ideal_contents_scale,
348 double current_frame_time_in_seconds,
349 const Occlusion& occlusion_in_layer_space,
350 bool can_require_tiles_for_activation) {
351 bool updated = false;
352 for (auto* tiling : tilings_) {
353 tiling->set_can_require_tiles_for_activation(
354 can_require_tiles_for_activation);
355 updated |= tiling->ComputeTilePriorityRects(
356 required_rect_in_layer_space, ideal_contents_scale,
357 current_frame_time_in_seconds, occlusion_in_layer_space);
359 return updated;
362 void PictureLayerTilingSet::GetAllPrioritizedTilesForTracing(
363 std::vector<PrioritizedTile>* prioritized_tiles) const {
364 for (auto* tiling : tilings_)
365 tiling->GetAllPrioritizedTilesForTracing(prioritized_tiles);
368 PictureLayerTilingSet::CoverageIterator::CoverageIterator(
369 const PictureLayerTilingSet* set,
370 float contents_scale,
371 const gfx::Rect& content_rect,
372 float ideal_contents_scale)
373 : set_(set),
374 contents_scale_(contents_scale),
375 ideal_contents_scale_(ideal_contents_scale),
376 current_tiling_(std::numeric_limits<size_t>::max()) {
377 missing_region_.Union(content_rect);
379 size_t tilings_size = set_->tilings_.size();
380 for (ideal_tiling_ = 0; ideal_tiling_ < tilings_size; ++ideal_tiling_) {
381 PictureLayerTiling* tiling = set_->tilings_[ideal_tiling_];
382 if (tiling->contents_scale() < ideal_contents_scale_) {
383 if (ideal_tiling_ > 0)
384 ideal_tiling_--;
385 break;
389 if (ideal_tiling_ == tilings_size && ideal_tiling_ > 0)
390 ideal_tiling_--;
392 ++(*this);
395 PictureLayerTilingSet::CoverageIterator::~CoverageIterator() {
398 gfx::Rect PictureLayerTilingSet::CoverageIterator::geometry_rect() const {
399 if (!tiling_iter_) {
400 if (!region_iter_.has_rect())
401 return gfx::Rect();
402 return region_iter_.rect();
404 return tiling_iter_.geometry_rect();
407 gfx::RectF PictureLayerTilingSet::CoverageIterator::texture_rect() const {
408 if (!tiling_iter_)
409 return gfx::RectF();
410 return tiling_iter_.texture_rect();
413 Tile* PictureLayerTilingSet::CoverageIterator::operator->() const {
414 if (!tiling_iter_)
415 return NULL;
416 return *tiling_iter_;
419 Tile* PictureLayerTilingSet::CoverageIterator::operator*() const {
420 if (!tiling_iter_)
421 return NULL;
422 return *tiling_iter_;
425 TileResolution PictureLayerTilingSet::CoverageIterator::resolution() const {
426 const PictureLayerTiling* tiling = CurrentTiling();
427 DCHECK(tiling);
428 return tiling->resolution();
431 PictureLayerTiling* PictureLayerTilingSet::CoverageIterator::CurrentTiling()
432 const {
433 if (current_tiling_ == std::numeric_limits<size_t>::max())
434 return NULL;
435 if (current_tiling_ >= set_->tilings_.size())
436 return NULL;
437 return set_->tilings_[current_tiling_];
440 size_t PictureLayerTilingSet::CoverageIterator::NextTiling() const {
441 // Order returned by this method is:
442 // 1. Ideal tiling index
443 // 2. Tiling index < Ideal in decreasing order (higher res than ideal)
444 // 3. Tiling index > Ideal in increasing order (lower res than ideal)
445 // 4. Tiling index > tilings.size() (invalid index)
446 if (current_tiling_ == std::numeric_limits<size_t>::max())
447 return ideal_tiling_;
448 else if (current_tiling_ > ideal_tiling_)
449 return current_tiling_ + 1;
450 else if (current_tiling_)
451 return current_tiling_ - 1;
452 else
453 return ideal_tiling_ + 1;
456 PictureLayerTilingSet::CoverageIterator&
457 PictureLayerTilingSet::CoverageIterator::operator++() {
458 bool first_time = current_tiling_ == std::numeric_limits<size_t>::max();
460 if (!*this && !first_time)
461 return *this;
463 if (tiling_iter_)
464 ++tiling_iter_;
466 // Loop until we find a valid place to stop.
467 while (true) {
468 while (tiling_iter_ &&
469 (!*tiling_iter_ || !tiling_iter_->draw_info().IsReadyToDraw())) {
470 missing_region_.Union(tiling_iter_.geometry_rect());
471 ++tiling_iter_;
473 if (tiling_iter_)
474 return *this;
476 // If the set of current rects for this tiling is done, go to the next
477 // tiling and set up to iterate through all of the remaining holes.
478 // This will also happen the first time through the loop.
479 if (!region_iter_.has_rect()) {
480 current_tiling_ = NextTiling();
481 current_region_.Swap(&missing_region_);
482 missing_region_.Clear();
483 region_iter_ = Region::Iterator(current_region_);
485 // All done and all filled.
486 if (!region_iter_.has_rect()) {
487 current_tiling_ = set_->tilings_.size();
488 return *this;
491 // No more valid tiles, return this checkerboard rect.
492 if (current_tiling_ >= set_->tilings_.size())
493 return *this;
496 // Pop a rect off. If there are no more tilings, then these will be
497 // treated as geometry with null tiles that the caller can checkerboard.
498 gfx::Rect last_rect = region_iter_.rect();
499 region_iter_.next();
501 // Done, found next checkerboard rect to return.
502 if (current_tiling_ >= set_->tilings_.size())
503 return *this;
505 // Construct a new iterator for the next tiling, but we need to loop
506 // again until we get to a valid one.
507 tiling_iter_ = PictureLayerTiling::CoverageIterator(
508 set_->tilings_[current_tiling_],
509 contents_scale_,
510 last_rect);
513 return *this;
516 PictureLayerTilingSet::CoverageIterator::operator bool() const {
517 return current_tiling_ < set_->tilings_.size() || region_iter_.has_rect();
520 void PictureLayerTilingSet::AsValueInto(
521 base::trace_event::TracedValue* state) const {
522 for (size_t i = 0; i < tilings_.size(); ++i) {
523 state->BeginDictionary();
524 tilings_[i]->AsValueInto(state);
525 state->EndDictionary();
529 size_t PictureLayerTilingSet::GPUMemoryUsageInBytes() const {
530 size_t amount = 0;
531 for (size_t i = 0; i < tilings_.size(); ++i)
532 amount += tilings_[i]->GPUMemoryUsageInBytes();
533 return amount;
536 PictureLayerTilingSet::TilingRange PictureLayerTilingSet::GetTilingRange(
537 TilingRangeType type) const {
538 // Doesn't seem to be the case right now but if it ever becomes a performance
539 // problem to compute these ranges each time this function is called, we can
540 // compute them only when the tiling set has changed instead.
541 size_t tilings_size = tilings_.size();
542 TilingRange high_res_range(0, 0);
543 TilingRange low_res_range(tilings_.size(), tilings_.size());
544 for (size_t i = 0; i < tilings_size; ++i) {
545 const PictureLayerTiling* tiling = tilings_[i];
546 if (tiling->resolution() == HIGH_RESOLUTION)
547 high_res_range = TilingRange(i, i + 1);
548 if (tiling->resolution() == LOW_RESOLUTION)
549 low_res_range = TilingRange(i, i + 1);
552 TilingRange range(0, 0);
553 switch (type) {
554 case HIGHER_THAN_HIGH_RES:
555 range = TilingRange(0, high_res_range.start);
556 break;
557 case HIGH_RES:
558 range = high_res_range;
559 break;
560 case BETWEEN_HIGH_AND_LOW_RES:
561 // TODO(vmpstr): This code assumes that high res tiling will come before
562 // low res tiling, however there are cases where this assumption is
563 // violated. As a result, it's better to be safe in these situations,
564 // since otherwise we can end up accessing a tiling that doesn't exist.
565 // See crbug.com/429397 for high res tiling appearing after low res
566 // tiling discussion/fixes.
567 if (high_res_range.start <= low_res_range.start)
568 range = TilingRange(high_res_range.end, low_res_range.start);
569 else
570 range = TilingRange(low_res_range.end, high_res_range.start);
571 break;
572 case LOW_RES:
573 range = low_res_range;
574 break;
575 case LOWER_THAN_LOW_RES:
576 range = TilingRange(low_res_range.end, tilings_size);
577 break;
580 DCHECK_LE(range.start, range.end);
581 return range;
584 } // namespace cc