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/layers/picture_layer_impl.h"
11 #include "base/debug/trace_event_argument.h"
12 #include "base/time/time.h"
13 #include "cc/base/math_util.h"
14 #include "cc/base/util.h"
15 #include "cc/debug/debug_colors.h"
16 #include "cc/debug/micro_benchmark_impl.h"
17 #include "cc/debug/traced_value.h"
18 #include "cc/layers/append_quads_data.h"
19 #include "cc/layers/solid_color_layer_impl.h"
20 #include "cc/output/begin_frame_args.h"
21 #include "cc/quads/checkerboard_draw_quad.h"
22 #include "cc/quads/debug_border_draw_quad.h"
23 #include "cc/quads/picture_draw_quad.h"
24 #include "cc/quads/solid_color_draw_quad.h"
25 #include "cc/quads/tile_draw_quad.h"
26 #include "cc/resources/tile_manager.h"
27 #include "cc/trees/layer_tree_impl.h"
28 #include "cc/trees/occlusion_tracker.h"
29 #include "ui/gfx/quad_f.h"
30 #include "ui/gfx/rect_conversions.h"
31 #include "ui/gfx/size_conversions.h"
34 const float kMaxScaleRatioDuringPinch = 2.0f;
36 // When creating a new tiling during pinch, snap to an existing
37 // tiling's scale if the desired scale is within this ratio.
38 const float kSnapToExistingTilingRatio = 1.2f;
40 // Estimate skewport 60 frames ahead for pre-rasterization on the CPU.
41 const float kCpuSkewportTargetTimeInFrames = 60.0f;
43 // Don't pre-rasterize on the GPU (except for kBackflingGuardDistancePixels in
44 // TileManager::BinFromTilePriority).
45 const float kGpuSkewportTargetTimeInFrames = 0.0f;
51 PictureLayerImpl::Pair::Pair() : active(NULL), pending(NULL) {
54 PictureLayerImpl::Pair::Pair(PictureLayerImpl* active_layer,
55 PictureLayerImpl* pending_layer)
56 : active(active_layer), pending(pending_layer) {
59 PictureLayerImpl::Pair::~Pair() {
62 PictureLayerImpl::PictureLayerImpl(LayerTreeImpl* tree_impl, int id)
63 : LayerImpl(tree_impl, id),
65 pile_(PicturePileImpl::Create()),
66 ideal_page_scale_(0.f),
67 ideal_device_scale_(0.f),
68 ideal_source_scale_(0.f),
69 ideal_contents_scale_(0.f),
70 raster_page_scale_(0.f),
71 raster_device_scale_(0.f),
72 raster_source_scale_(0.f),
73 raster_contents_scale_(0.f),
74 low_res_raster_contents_scale_(0.f),
75 raster_source_scale_is_fixed_(false),
76 was_screen_space_transform_animating_(false),
77 needs_post_commit_initialization_(true),
78 should_update_tile_priorities_(false) {
79 layer_tree_impl()->RegisterPictureLayerImpl(this);
82 PictureLayerImpl::~PictureLayerImpl() {
83 layer_tree_impl()->UnregisterPictureLayerImpl(this);
86 const char* PictureLayerImpl::LayerTypeAsString() const {
87 return "cc::PictureLayerImpl";
90 scoped_ptr<LayerImpl> PictureLayerImpl::CreateLayerImpl(
91 LayerTreeImpl* tree_impl) {
92 return PictureLayerImpl::Create(tree_impl, id()).PassAs<LayerImpl>();
95 void PictureLayerImpl::PushPropertiesTo(LayerImpl* base_layer) {
96 // It's possible this layer was never drawn or updated (e.g. because it was
97 // a descendant of an opacity 0 layer).
98 DoPostCommitInitializationIfNeeded();
99 PictureLayerImpl* layer_impl = static_cast<PictureLayerImpl*>(base_layer);
101 // We have already synced the important bits from the the active layer, and
102 // we will soon swap out its tilings and use them for recycling. However,
103 // there are now tiles in this layer's tilings that were unref'd and replaced
104 // with new tiles (due to invalidation). This resets all active priorities on
105 // the to-be-recycled tiling to ensure replaced tiles don't linger and take
106 // memory (due to a stale 'active' priority).
107 if (layer_impl->tilings_)
108 layer_impl->tilings_->DidBecomeRecycled();
110 LayerImpl::PushPropertiesTo(base_layer);
112 // When the pending tree pushes to the active tree, the pending twin
114 layer_impl->twin_layer_ = NULL;
117 layer_impl->pile_ = pile_;
119 DCHECK(!pile_->is_solid_color() || !tilings_->num_tilings());
120 // Tilings would be expensive to push, so we swap.
121 layer_impl->tilings_.swap(tilings_);
122 layer_impl->tilings_->SetClient(layer_impl);
124 tilings_->SetClient(this);
126 // Ensure that the recycle tree doesn't have any unshared tiles.
127 if (tilings_ && pile_->is_solid_color())
128 tilings_->RemoveAllTilings();
130 // Remove invalidated tiles from what will become a recycle tree.
132 tilings_->RemoveTilesInRegion(invalidation_);
134 layer_impl->raster_page_scale_ = raster_page_scale_;
135 layer_impl->raster_device_scale_ = raster_device_scale_;
136 layer_impl->raster_source_scale_ = raster_source_scale_;
137 layer_impl->raster_contents_scale_ = raster_contents_scale_;
138 layer_impl->low_res_raster_contents_scale_ = low_res_raster_contents_scale_;
139 layer_impl->needs_post_commit_initialization_ = false;
141 // The invalidation on this soon-to-be-recycled layer must be cleared to
142 // mirror clearing the invalidation in PictureLayer's version of this function
143 // in case push properties is skipped.
144 layer_impl->invalidation_.Swap(&invalidation_);
145 invalidation_.Clear();
146 needs_post_commit_initialization_ = true;
148 // We always need to push properties.
149 // See http://crbug.com/303943
150 needs_push_properties_ = true;
153 void PictureLayerImpl::AppendQuads(
154 RenderPass* render_pass,
155 const OcclusionTracker<LayerImpl>& occlusion_tracker,
156 AppendQuadsData* append_quads_data) {
157 DCHECK(!needs_post_commit_initialization_);
159 SharedQuadState* shared_quad_state =
160 render_pass->CreateAndAppendSharedQuadState();
162 if (pile_->is_solid_color()) {
163 PopulateSharedQuadState(shared_quad_state);
165 AppendDebugBorderQuad(
166 render_pass, content_bounds(), shared_quad_state, append_quads_data);
168 SolidColorLayerImpl::AppendSolidQuads(
173 draw_properties().target_space_transform,
174 pile_->solid_color());
178 float max_contents_scale = MaximumTilingContentsScale();
179 gfx::Transform scaled_draw_transform = draw_transform();
180 scaled_draw_transform.Scale(SK_MScalar1 / max_contents_scale,
181 SK_MScalar1 / max_contents_scale);
182 gfx::Size scaled_content_bounds =
183 gfx::ToCeiledSize(gfx::ScaleSize(content_bounds(), max_contents_scale));
185 gfx::Rect scaled_visible_content_rect =
186 gfx::ScaleToEnclosingRect(visible_content_rect(), max_contents_scale);
187 scaled_visible_content_rect.Intersect(gfx::Rect(scaled_content_bounds));
189 Occlusion occlusion =
190 occlusion_tracker.GetCurrentOcclusionForLayer(scaled_draw_transform);
192 shared_quad_state->SetAll(scaled_draw_transform,
193 scaled_content_bounds,
194 scaled_visible_content_rect,
195 draw_properties().clip_rect,
196 draw_properties().is_clipped,
197 draw_properties().opacity,
199 sorting_context_id_);
201 if (current_draw_mode_ == DRAW_MODE_RESOURCELESS_SOFTWARE) {
202 AppendDebugBorderQuad(
204 scaled_content_bounds,
207 DebugColors::DirectPictureBorderColor(),
208 DebugColors::DirectPictureBorderWidth(layer_tree_impl()));
210 gfx::Rect geometry_rect = scaled_visible_content_rect;
211 gfx::Rect opaque_rect = contents_opaque() ? geometry_rect : gfx::Rect();
212 gfx::Rect visible_geometry_rect =
213 occlusion.GetUnoccludedContentRect(geometry_rect);
214 if (visible_geometry_rect.IsEmpty())
217 gfx::Size texture_size = scaled_visible_content_rect.size();
218 gfx::RectF texture_rect = gfx::RectF(texture_size);
219 gfx::Rect quad_content_rect = scaled_visible_content_rect;
221 PictureDrawQuad* quad =
222 render_pass->CreateAndAppendDrawQuad<PictureDrawQuad>();
223 quad->SetNew(shared_quad_state,
226 visible_geometry_rect,
236 AppendDebugBorderQuad(
237 render_pass, scaled_content_bounds, shared_quad_state, append_quads_data);
239 if (ShowDebugBorders()) {
240 for (PictureLayerTilingSet::CoverageIterator iter(
243 scaled_visible_content_rect,
244 ideal_contents_scale_);
249 if (*iter && iter->IsReadyToDraw()) {
250 ManagedTileState::TileVersion::Mode mode =
251 iter->GetTileVersionForDrawing().mode();
252 if (mode == ManagedTileState::TileVersion::SOLID_COLOR_MODE) {
253 color = DebugColors::SolidColorTileBorderColor();
254 width = DebugColors::SolidColorTileBorderWidth(layer_tree_impl());
255 } else if (mode == ManagedTileState::TileVersion::PICTURE_PILE_MODE) {
256 color = DebugColors::PictureTileBorderColor();
257 width = DebugColors::PictureTileBorderWidth(layer_tree_impl());
258 } else if (iter->priority(ACTIVE_TREE).resolution == HIGH_RESOLUTION) {
259 color = DebugColors::HighResTileBorderColor();
260 width = DebugColors::HighResTileBorderWidth(layer_tree_impl());
261 } else if (iter->priority(ACTIVE_TREE).resolution == LOW_RESOLUTION) {
262 color = DebugColors::LowResTileBorderColor();
263 width = DebugColors::LowResTileBorderWidth(layer_tree_impl());
264 } else if (iter->contents_scale() > max_contents_scale) {
265 color = DebugColors::ExtraHighResTileBorderColor();
266 width = DebugColors::ExtraHighResTileBorderWidth(layer_tree_impl());
268 color = DebugColors::ExtraLowResTileBorderColor();
269 width = DebugColors::ExtraLowResTileBorderWidth(layer_tree_impl());
272 color = DebugColors::MissingTileBorderColor();
273 width = DebugColors::MissingTileBorderWidth(layer_tree_impl());
276 DebugBorderDrawQuad* debug_border_quad =
277 render_pass->CreateAndAppendDrawQuad<DebugBorderDrawQuad>();
278 gfx::Rect geometry_rect = iter.geometry_rect();
279 gfx::Rect visible_geometry_rect = geometry_rect;
280 debug_border_quad->SetNew(shared_quad_state,
282 visible_geometry_rect,
288 // Keep track of the tilings that were used so that tilings that are
289 // unused can be considered for removal.
290 std::vector<PictureLayerTiling*> seen_tilings;
292 // Ignore missing tiles outside of viewport for tile priority. This is
293 // normally the same as draw viewport but can be independently overridden by
294 // embedders like Android WebView with SetExternalDrawConstraints.
295 gfx::Rect scaled_viewport_for_tile_priority = gfx::ScaleToEnclosingRect(
296 GetViewportForTilePriorityInContentSpace(), max_contents_scale);
298 size_t missing_tile_count = 0u;
299 size_t on_demand_missing_tile_count = 0u;
300 for (PictureLayerTilingSet::CoverageIterator iter(tilings_.get(),
302 scaled_visible_content_rect,
303 ideal_contents_scale_);
306 gfx::Rect geometry_rect = iter.geometry_rect();
307 gfx::Rect opaque_rect = contents_opaque() ? geometry_rect : gfx::Rect();
308 gfx::Rect visible_geometry_rect =
309 occlusion.GetUnoccludedContentRect(geometry_rect);
310 if (visible_geometry_rect.IsEmpty())
313 append_quads_data->visible_content_area +=
314 visible_geometry_rect.width() * visible_geometry_rect.height();
316 bool has_draw_quad = false;
317 if (*iter && iter->IsReadyToDraw()) {
318 const ManagedTileState::TileVersion& tile_version =
319 iter->GetTileVersionForDrawing();
320 switch (tile_version.mode()) {
321 case ManagedTileState::TileVersion::RESOURCE_MODE: {
322 gfx::RectF texture_rect = iter.texture_rect();
324 // The raster_contents_scale_ is the best scale that the layer is
325 // trying to produce, even though it may not be ideal. Since that's
326 // the best the layer can promise in the future, consider those as
327 // complete. But if a tile is ideal scale, we don't want to consider
328 // it incomplete and trying to replace it with a tile at a worse
330 if (iter->contents_scale() != raster_contents_scale_ &&
331 iter->contents_scale() != ideal_contents_scale_ &&
332 geometry_rect.Intersects(scaled_viewport_for_tile_priority)) {
333 append_quads_data->num_incomplete_tiles++;
337 render_pass->CreateAndAppendDrawQuad<TileDrawQuad>();
338 quad->SetNew(shared_quad_state,
341 visible_geometry_rect,
342 tile_version.get_resource_id(),
345 tile_version.contents_swizzled());
346 has_draw_quad = true;
349 case ManagedTileState::TileVersion::PICTURE_PILE_MODE: {
350 if (!layer_tree_impl()
351 ->GetRendererCapabilities()
352 .allow_rasterize_on_demand) {
353 ++on_demand_missing_tile_count;
357 gfx::RectF texture_rect = iter.texture_rect();
359 ResourceProvider* resource_provider =
360 layer_tree_impl()->resource_provider();
361 ResourceFormat format =
362 resource_provider->memory_efficient_texture_format();
363 PictureDrawQuad* quad =
364 render_pass->CreateAndAppendDrawQuad<PictureDrawQuad>();
365 quad->SetNew(shared_quad_state,
368 visible_geometry_rect,
372 iter->content_rect(),
373 iter->contents_scale(),
375 has_draw_quad = true;
378 case ManagedTileState::TileVersion::SOLID_COLOR_MODE: {
379 SolidColorDrawQuad* quad =
380 render_pass->CreateAndAppendDrawQuad<SolidColorDrawQuad>();
381 quad->SetNew(shared_quad_state,
383 visible_geometry_rect,
384 tile_version.get_solid_color(),
386 has_draw_quad = true;
392 if (!has_draw_quad) {
393 if (draw_checkerboard_for_missing_tiles()) {
394 CheckerboardDrawQuad* quad =
395 render_pass->CreateAndAppendDrawQuad<CheckerboardDrawQuad>();
396 SkColor color = DebugColors::DefaultCheckerboardColor();
398 shared_quad_state, geometry_rect, visible_geometry_rect, color);
400 SkColor color = SafeOpaqueBackgroundColor();
401 SolidColorDrawQuad* quad =
402 render_pass->CreateAndAppendDrawQuad<SolidColorDrawQuad>();
403 quad->SetNew(shared_quad_state,
405 visible_geometry_rect,
410 if (geometry_rect.Intersects(scaled_viewport_for_tile_priority)) {
411 append_quads_data->num_missing_tiles++;
412 ++missing_tile_count;
414 append_quads_data->approximated_visible_content_area +=
415 visible_geometry_rect.width() * visible_geometry_rect.height();
419 if (iter->priority(ACTIVE_TREE).resolution != HIGH_RESOLUTION) {
420 append_quads_data->approximated_visible_content_area +=
421 visible_geometry_rect.width() * visible_geometry_rect.height();
424 if (seen_tilings.empty() || seen_tilings.back() != iter.CurrentTiling())
425 seen_tilings.push_back(iter.CurrentTiling());
428 if (missing_tile_count) {
429 TRACE_EVENT_INSTANT2("cc",
430 "PictureLayerImpl::AppendQuads checkerboard",
431 TRACE_EVENT_SCOPE_THREAD,
432 "missing_tile_count",
434 "on_demand_missing_tile_count",
435 on_demand_missing_tile_count);
438 // Aggressively remove any tilings that are not seen to save memory. Note
439 // that this is at the expense of doing cause more frequent re-painting. A
440 // better scheme would be to maintain a tighter visible_content_rect for the
442 CleanUpTilingsOnActiveLayer(seen_tilings);
445 void PictureLayerImpl::UpdateTiles(
446 const Occlusion& occlusion_in_content_space) {
447 TRACE_EVENT0("cc", "PictureLayerImpl::UpdateTiles");
448 DCHECK_EQ(1.f, contents_scale_x());
449 DCHECK_EQ(1.f, contents_scale_y());
451 DoPostCommitInitializationIfNeeded();
453 visible_rect_for_tile_priority_ = visible_content_rect();
454 viewport_rect_for_tile_priority_ =
455 layer_tree_impl()->ViewportRectForTilePriority();
456 screen_space_transform_for_tile_priority_ = screen_space_transform();
458 if (!CanHaveTilings()) {
459 ideal_page_scale_ = 0.f;
460 ideal_device_scale_ = 0.f;
461 ideal_contents_scale_ = 0.f;
462 ideal_source_scale_ = 0.f;
463 SanityCheckTilingState();
469 DCHECK(tilings_->num_tilings() > 0 || raster_contents_scale_ == 0.f)
470 << "A layer with no tilings shouldn't have valid raster scales";
471 if (!raster_contents_scale_ || ShouldAdjustRasterScale()) {
472 RecalculateRasterScales();
473 AddTilingsForRasterScale();
476 DCHECK(raster_page_scale_);
477 DCHECK(raster_device_scale_);
478 DCHECK(raster_source_scale_);
479 DCHECK(raster_contents_scale_);
480 DCHECK(low_res_raster_contents_scale_);
482 was_screen_space_transform_animating_ =
483 draw_properties().screen_space_transform_is_animating;
485 should_update_tile_priorities_ = true;
487 UpdateTilePriorities(occlusion_in_content_space);
489 if (layer_tree_impl()->IsPendingTree())
490 MarkVisibleResourcesAsRequired();
493 void PictureLayerImpl::UpdateTilePriorities(
494 const Occlusion& occlusion_in_content_space) {
495 DCHECK(!pile_->is_solid_color() || !tilings_->num_tilings());
497 TRACE_EVENT0("cc", "PictureLayerImpl::UpdateTilePriorities");
499 double current_frame_time_in_seconds =
500 (layer_tree_impl()->CurrentBeginFrameArgs().frame_time -
501 base::TimeTicks()).InSecondsF();
503 bool tiling_needs_update = false;
504 for (size_t i = 0; i < tilings_->num_tilings(); ++i) {
505 if (tilings_->tiling_at(i)->NeedsUpdateForFrameAtTime(
506 current_frame_time_in_seconds)) {
507 tiling_needs_update = true;
511 if (!tiling_needs_update)
514 gfx::Rect viewport_rect_in_layer_space =
515 GetViewportForTilePriorityInContentSpace();
517 layer_tree_impl()->IsActiveTree() ? ACTIVE_TREE : PENDING_TREE;
518 for (size_t i = 0; i < tilings_->num_tilings(); ++i) {
519 // Pass |occlusion_in_content_space| for |occlusion_in_layer_space| since
520 // they are the same space in picture lbayer, as contents scale is always 1.
521 tilings_->tiling_at(i)->UpdateTilePriorities(tree,
522 viewport_rect_in_layer_space,
523 ideal_contents_scale_,
524 current_frame_time_in_seconds,
525 occlusion_in_content_space);
528 // Tile priorities were modified.
529 layer_tree_impl()->DidModifyTilePriorities();
532 gfx::Rect PictureLayerImpl::GetViewportForTilePriorityInContentSpace() const {
533 // If visible_rect_for_tile_priority_ is empty or
534 // viewport_rect_for_tile_priority_ is set to be different from the device
535 // viewport, try to inverse project the viewport into layer space and use
536 // that. Otherwise just use visible_rect_for_tile_priority_
537 gfx::Rect visible_rect_in_content_space = visible_rect_for_tile_priority_;
539 if (visible_rect_in_content_space.IsEmpty() ||
540 layer_tree_impl()->DeviceViewport() != viewport_rect_for_tile_priority_) {
541 gfx::Transform view_to_layer(gfx::Transform::kSkipInitialization);
543 if (screen_space_transform_for_tile_priority_.GetInverse(&view_to_layer)) {
544 // Transform from view space to content space.
545 visible_rect_in_content_space =
546 gfx::ToEnclosingRect(MathUtil::ProjectClippedRect(
547 view_to_layer, viewport_rect_for_tile_priority_));
550 return visible_rect_in_content_space;
553 PictureLayerImpl* PictureLayerImpl::GetRecycledTwinLayer() {
554 // TODO(vmpstr): Maintain recycled twin as a member. crbug.com/407418
555 return static_cast<PictureLayerImpl*>(
556 layer_tree_impl()->FindRecycleTreeLayerById(id()));
559 void PictureLayerImpl::NotifyTileStateChanged(const Tile* tile) {
560 if (layer_tree_impl()->IsActiveTree()) {
561 gfx::RectF layer_damage_rect =
562 gfx::ScaleRect(tile->content_rect(), 1.f / tile->contents_scale());
563 AddDamageRect(layer_damage_rect);
567 void PictureLayerImpl::DidBecomeActive() {
568 LayerImpl::DidBecomeActive();
569 tilings_->DidBecomeActive();
570 layer_tree_impl()->DidModifyTilePriorities();
573 void PictureLayerImpl::DidBeginTracing() {
574 pile_->DidBeginTracing();
577 void PictureLayerImpl::ReleaseResources() {
583 // To avoid an edge case after lost context where the tree is up to date but
584 // the tilings have not been managed, request an update draw properties
585 // to force tilings to get managed.
586 layer_tree_impl()->set_needs_update_draw_properties();
589 skia::RefPtr<SkPicture> PictureLayerImpl::GetPicture() {
590 return pile_->GetFlattenedPicture();
593 scoped_refptr<Tile> PictureLayerImpl::CreateTile(PictureLayerTiling* tiling,
594 const gfx::Rect& content_rect) {
595 DCHECK(!pile_->is_solid_color());
596 if (!pile_->CanRaster(tiling->contents_scale(), content_rect))
597 return scoped_refptr<Tile>();
601 // TODO(vmpstr): Revisit this. For now, enabling analysis means that we get as
602 // much savings on memory as we can. However, for some cases like ganesh or
603 // small layers, the amount of time we spend analyzing might not justify
604 // memory savings that we can get. Note that we don't handle solid color
605 // masks, so we shouldn't bother analyzing those.
606 // Bugs: crbug.com/397198, crbug.com/396908
607 if (!pile_->is_mask())
608 flags = Tile::USE_PICTURE_ANALYSIS;
610 return layer_tree_impl()->tile_manager()->CreateTile(
614 tiling->contents_scale(),
616 layer_tree_impl()->source_frame_number(),
620 PicturePileImpl* PictureLayerImpl::GetPile() {
624 const Region* PictureLayerImpl::GetInvalidation() {
625 return &invalidation_;
628 const PictureLayerTiling* PictureLayerImpl::GetTwinTiling(
629 const PictureLayerTiling* tiling) const {
632 return twin_layer_->tilings_->TilingAtScale(tiling->contents_scale());
635 PictureLayerTiling* PictureLayerImpl::GetRecycledTwinTiling(
636 const PictureLayerTiling* tiling) {
637 PictureLayerImpl* recycled_twin = GetRecycledTwinLayer();
638 if (!recycled_twin || !recycled_twin->tilings_)
640 return recycled_twin->tilings_->TilingAtScale(tiling->contents_scale());
643 size_t PictureLayerImpl::GetMaxTilesForInterestArea() const {
644 return layer_tree_impl()->settings().max_tiles_for_interest_area;
647 float PictureLayerImpl::GetSkewportTargetTimeInSeconds() const {
648 float skewport_target_time_in_frames =
649 layer_tree_impl()->use_gpu_rasterization()
650 ? kGpuSkewportTargetTimeInFrames
651 : kCpuSkewportTargetTimeInFrames;
652 return skewport_target_time_in_frames *
653 layer_tree_impl()->begin_impl_frame_interval().InSecondsF() *
654 layer_tree_impl()->settings().skewport_target_time_multiplier;
657 int PictureLayerImpl::GetSkewportExtrapolationLimitInContentPixels() const {
658 return layer_tree_impl()
660 .skewport_extrapolation_limit_in_content_pixels;
663 gfx::Size PictureLayerImpl::CalculateTileSize(
664 const gfx::Size& content_bounds) const {
665 int max_texture_size =
666 layer_tree_impl()->resource_provider()->max_texture_size();
668 if (pile_->is_mask()) {
669 // Masks are not tiled, so if we can't cover the whole mask with one tile,
670 // don't make any tiles at all. Returning an empty size signals this.
671 if (content_bounds.width() > max_texture_size ||
672 content_bounds.height() > max_texture_size)
674 return content_bounds;
677 gfx::Size default_tile_size = layer_tree_impl()->settings().default_tile_size;
678 if (layer_tree_impl()->use_gpu_rasterization()) {
679 // TODO(ernstm) crbug.com/365877: We need a unified way to override the
680 // default-tile-size.
682 gfx::Size(layer_tree_impl()->device_viewport_size().width(),
683 layer_tree_impl()->device_viewport_size().height() / 4);
685 default_tile_size.SetToMin(gfx::Size(max_texture_size, max_texture_size));
687 gfx::Size max_untiled_content_size =
688 layer_tree_impl()->settings().max_untiled_layer_size;
689 max_untiled_content_size.SetToMin(
690 gfx::Size(max_texture_size, max_texture_size));
692 bool any_dimension_too_large =
693 content_bounds.width() > max_untiled_content_size.width() ||
694 content_bounds.height() > max_untiled_content_size.height();
696 bool any_dimension_one_tile =
697 content_bounds.width() <= default_tile_size.width() ||
698 content_bounds.height() <= default_tile_size.height();
700 // If long and skinny, tile at the max untiled content size, and clamp
701 // the smaller dimension to the content size, e.g. 1000x12 layer with
702 // 500x500 max untiled size would get 500x12 tiles. Also do this
703 // if the layer is small.
704 if (any_dimension_one_tile || !any_dimension_too_large) {
705 int width = std::min(
706 std::max(max_untiled_content_size.width(), default_tile_size.width()),
707 content_bounds.width());
708 int height = std::min(
709 std::max(max_untiled_content_size.height(), default_tile_size.height()),
710 content_bounds.height());
711 // Round up to the closest multiple of 64. This improves recycling and
712 // avoids odd texture sizes.
713 width = RoundUp(width, 64);
714 height = RoundUp(height, 64);
715 return gfx::Size(width, height);
718 return default_tile_size;
721 void PictureLayerImpl::SyncFromActiveLayer(const PictureLayerImpl* other) {
722 TRACE_EVENT0("cc", "SyncFromActiveLayer");
723 DCHECK(!other->needs_post_commit_initialization_);
724 DCHECK(other->tilings_);
726 if (!DrawsContent()) {
731 raster_page_scale_ = other->raster_page_scale_;
732 raster_device_scale_ = other->raster_device_scale_;
733 raster_source_scale_ = other->raster_source_scale_;
734 raster_contents_scale_ = other->raster_contents_scale_;
735 low_res_raster_contents_scale_ = other->low_res_raster_contents_scale_;
737 bool synced_high_res_tiling = false;
738 if (CanHaveTilings()) {
739 synced_high_res_tiling = tilings_->SyncTilings(
740 *other->tilings_, bounds(), invalidation_, MinimumContentsScale());
745 // If our MinimumContentsScale has changed to prevent the twin's high res
746 // tiling from being synced, we should reset the raster scale and let it be
747 // recalculated (1) again. This can happen if our bounds shrink to the point
748 // where min contents scale grows.
749 // (1) - TODO(vmpstr) Instead of hoping that this will be recalculated, we
750 // should refactor this code a little bit and actually recalculate this.
751 // However, this is a larger undertaking, so this will work for now.
752 if (!synced_high_res_tiling)
755 SanityCheckTilingState();
758 void PictureLayerImpl::SyncTiling(
759 const PictureLayerTiling* tiling) {
760 if (!CanHaveTilingWithScale(tiling->contents_scale()))
762 tilings_->AddTiling(tiling->contents_scale());
764 // If this tree needs update draw properties, then the tiling will
765 // get updated prior to drawing or activation. If this tree does not
766 // need update draw properties, then its transforms are up to date and
767 // we can create tiles for this tiling immediately.
768 if (!layer_tree_impl()->needs_update_draw_properties() &&
769 should_update_tile_priorities_) {
770 // TODO(danakj): Add a DCHECK() that we are not using occlusion tracking
771 // when we stop using the pending tree in the browser compositor. If we want
772 // to support occlusion tracking here, we need to dirty the draw properties
773 // or save occlusion as a draw property.
774 UpdateTilePriorities(Occlusion());
778 ResourceProvider::ResourceId PictureLayerImpl::ContentsResourceId() const {
779 gfx::Rect content_rect(content_bounds());
780 PictureLayerTilingSet::CoverageIterator iter(
781 tilings_.get(), 1.f, content_rect, ideal_contents_scale_);
783 // Mask resource not ready yet.
787 // Masks only supported if they fit on exactly one tile.
788 DCHECK(iter.geometry_rect() == content_rect)
789 << "iter rect " << iter.geometry_rect().ToString() << " content rect "
790 << content_rect.ToString();
792 const ManagedTileState::TileVersion& tile_version =
793 iter->GetTileVersionForDrawing();
794 if (!tile_version.IsReadyToDraw() ||
795 tile_version.mode() != ManagedTileState::TileVersion::RESOURCE_MODE)
798 return tile_version.get_resource_id();
801 void PictureLayerImpl::MarkVisibleResourcesAsRequired() const {
802 DCHECK(layer_tree_impl()->IsPendingTree());
803 DCHECK(ideal_contents_scale_);
804 DCHECK_GT(tilings_->num_tilings(), 0u);
806 // The goal of this function is to find the minimum set of tiles that need to
807 // be ready to draw in order to activate without flashing content from a
808 // higher res on the active tree to a lower res on the pending tree.
810 // First, early out for layers with no visible content.
811 if (visible_content_rect().IsEmpty())
814 // Only mark tiles inside the viewport for tile priority as required for
815 // activation. This viewport is normally the same as the draw viewport but
816 // can be independently overridden by embedders like Android WebView with
817 // SetExternalDrawConstraints.
818 gfx::Rect rect = GetViewportForTilePriorityInContentSpace();
819 rect.Intersect(visible_content_rect());
821 float min_acceptable_scale =
822 std::min(raster_contents_scale_, ideal_contents_scale_);
824 if (PictureLayerImpl* twin = twin_layer_) {
825 float twin_min_acceptable_scale =
826 std::min(twin->ideal_contents_scale_, twin->raster_contents_scale_);
827 // Ignore 0 scale in case CalculateContentsScale() has never been
828 // called for active twin.
829 if (twin_min_acceptable_scale != 0.0f) {
830 min_acceptable_scale =
831 std::min(min_acceptable_scale, twin_min_acceptable_scale);
835 PictureLayerTiling* high_res = NULL;
836 PictureLayerTiling* low_res = NULL;
838 // First pass: ready to draw tiles in acceptable but non-ideal tilings are
839 // marked as required for activation so that their textures are not thrown
840 // away; any non-ready tiles are not marked as required.
841 Region missing_region = rect;
842 for (size_t i = 0; i < tilings_->num_tilings(); ++i) {
843 PictureLayerTiling* tiling = tilings_->tiling_at(i);
844 DCHECK(tiling->has_ever_been_updated());
846 if (tiling->resolution() == LOW_RESOLUTION) {
847 DCHECK(!low_res) << "There can only be one low res tiling";
850 if (tiling->contents_scale() < min_acceptable_scale)
852 if (tiling->resolution() == HIGH_RESOLUTION) {
853 DCHECK(!high_res) << "There can only be one high res tiling";
857 for (PictureLayerTiling::CoverageIterator iter(tiling, 1.f, rect); iter;
859 if (!*iter || !iter->IsReadyToDraw())
862 missing_region.Subtract(iter.geometry_rect());
863 iter->MarkRequiredForActivation();
866 DCHECK(high_res) << "There must be one high res tiling";
868 // If these pointers are null (because no twin, no matching tiling, or the
869 // simpification just below), then high res tiles will be required to fill any
870 // holes left by the first pass above. If the pointers are valid, then this
871 // layer is allowed to skip any tiles that are not ready on its twin.
872 const PictureLayerTiling* twin_high_res = NULL;
873 const PictureLayerTiling* twin_low_res = NULL;
876 // As a simplification, only allow activating to skip twin tiles that the
877 // active layer is also missing when both this layer and its twin have
878 // "simple" sets of tilings: only 2 tilings (high and low) or only 1 high
879 // res tiling. This avoids having to iterate/track coverage of non-ideal
880 // tilings during the last draw call on the active layer.
881 if (tilings_->num_tilings() <= 2 &&
882 twin_layer_->tilings_->num_tilings() <= tilings_->num_tilings()) {
883 twin_low_res = low_res ? GetTwinTiling(low_res) : NULL;
884 twin_high_res = high_res ? GetTwinTiling(high_res) : NULL;
887 // If this layer and its twin have different transforms, then don't compare
888 // them and only allow activating to high res tiles, since tiles on each
889 // layer will be in different places on screen.
890 if (twin_layer_->layer_tree_impl()->RequiresHighResToDraw() ||
891 bounds() != twin_layer_->bounds() ||
892 draw_properties().screen_space_transform !=
893 twin_layer_->draw_properties().screen_space_transform) {
894 twin_high_res = NULL;
899 // As a second pass, mark as required any visible high res tiles not filled in
900 // by acceptable non-ideal tiles from the first pass.
901 if (MarkVisibleTilesAsRequired(
902 high_res, twin_high_res, rect, missing_region)) {
903 // As an optional third pass, if a high res tile was skipped because its
904 // twin was also missing, then fall back to mark low res tiles as required
905 // in case the active twin is substituting those for missing high res
906 // content. Only suitable, when low res is enabled.
908 MarkVisibleTilesAsRequired(low_res, twin_low_res, rect, missing_region);
913 bool PictureLayerImpl::MarkVisibleTilesAsRequired(
914 PictureLayerTiling* tiling,
915 const PictureLayerTiling* optional_twin_tiling,
916 const gfx::Rect& rect,
917 const Region& missing_region) const {
918 bool twin_had_missing_tile = false;
919 for (PictureLayerTiling::CoverageIterator iter(tiling, 1.f, rect); iter;
922 // A null tile (i.e. missing recording) can just be skipped.
926 // If the tile is occluded, don't mark it as required for activation.
927 if (tile->is_occluded(PENDING_TREE))
930 // If the missing region doesn't cover it, this tile is fully
931 // covered by acceptable tiles at other scales.
932 if (!missing_region.Intersects(iter.geometry_rect()))
935 // If the twin tile doesn't exist (i.e. missing recording or so far away
936 // that it is outside the visible tile rect) or this tile is shared between
937 // with the twin, then this tile isn't required to prevent flashing.
938 if (optional_twin_tiling) {
939 Tile* twin_tile = optional_twin_tiling->TileAt(iter.i(), iter.j());
940 if (!twin_tile || twin_tile == tile) {
941 // However if the shared tile is being used on the active tree, then
942 // there's no missing content in this place, and low res is not needed.
943 if (!twin_tile || !twin_tile->IsReadyToDraw())
944 twin_had_missing_tile = true;
949 tile->MarkRequiredForActivation();
951 return twin_had_missing_tile;
954 void PictureLayerImpl::DoPostCommitInitialization() {
955 DCHECK(needs_post_commit_initialization_);
956 DCHECK(layer_tree_impl()->IsPendingTree());
959 tilings_.reset(new PictureLayerTilingSet(this, bounds()));
961 DCHECK(!twin_layer_);
962 twin_layer_ = static_cast<PictureLayerImpl*>(
963 layer_tree_impl()->FindActiveTreeLayerById(id()));
965 DCHECK(!twin_layer_->twin_layer_);
966 twin_layer_->twin_layer_ = this;
967 // If the twin has never been pushed to, do not sync from it.
968 // This can happen if this function is called during activation.
969 if (!twin_layer_->needs_post_commit_initialization_)
970 SyncFromActiveLayer(twin_layer_);
973 needs_post_commit_initialization_ = false;
976 PictureLayerTiling* PictureLayerImpl::AddTiling(float contents_scale) {
977 DCHECK(CanHaveTilingWithScale(contents_scale)) <<
978 "contents_scale: " << contents_scale;
980 PictureLayerTiling* tiling = tilings_->AddTiling(contents_scale);
982 DCHECK(pile_->HasRecordings());
985 twin_layer_->SyncTiling(tiling);
990 void PictureLayerImpl::RemoveTiling(float contents_scale) {
991 if (!tilings_ || tilings_->num_tilings() == 0)
994 for (size_t i = 0; i < tilings_->num_tilings(); ++i) {
995 PictureLayerTiling* tiling = tilings_->tiling_at(i);
996 if (tiling->contents_scale() == contents_scale) {
997 tilings_->Remove(tiling);
1001 if (tilings_->num_tilings() == 0)
1003 SanityCheckTilingState();
1006 void PictureLayerImpl::RemoveAllTilings() {
1008 tilings_->RemoveAllTilings();
1009 // If there are no tilings, then raster scales are no longer meaningful.
1015 inline float PositiveRatio(float float1, float float2) {
1016 DCHECK_GT(float1, 0);
1017 DCHECK_GT(float2, 0);
1018 return float1 > float2 ? float1 / float2 : float2 / float1;
1023 void PictureLayerImpl::AddTilingsForRasterScale() {
1024 PictureLayerTiling* high_res = NULL;
1025 PictureLayerTiling* low_res = NULL;
1027 PictureLayerTiling* previous_low_res = NULL;
1028 for (size_t i = 0; i < tilings_->num_tilings(); ++i) {
1029 PictureLayerTiling* tiling = tilings_->tiling_at(i);
1030 if (tiling->contents_scale() == raster_contents_scale_)
1032 if (tiling->contents_scale() == low_res_raster_contents_scale_)
1034 if (tiling->resolution() == LOW_RESOLUTION)
1035 previous_low_res = tiling;
1037 // Reset all tilings to non-ideal until the end of this function.
1038 tiling->set_resolution(NON_IDEAL_RESOLUTION);
1042 high_res = AddTiling(raster_contents_scale_);
1043 if (raster_contents_scale_ == low_res_raster_contents_scale_)
1047 // Only create new low res tilings when the transform is static. This
1048 // prevents wastefully creating a paired low res tiling for every new high res
1049 // tiling during a pinch or a CSS animation.
1050 bool is_pinching = layer_tree_impl()->PinchGestureActive();
1051 if (layer_tree_impl()->create_low_res_tiling() && !is_pinching &&
1052 !draw_properties().screen_space_transform_is_animating && !low_res &&
1053 low_res != high_res)
1054 low_res = AddTiling(low_res_raster_contents_scale_);
1056 // Set low-res if we have one.
1058 low_res = previous_low_res;
1059 if (low_res && low_res != high_res)
1060 low_res->set_resolution(LOW_RESOLUTION);
1062 // Make sure we always have one high-res (even if high == low).
1063 high_res->set_resolution(HIGH_RESOLUTION);
1065 SanityCheckTilingState();
1068 bool PictureLayerImpl::ShouldAdjustRasterScale() const {
1069 if (was_screen_space_transform_animating_ !=
1070 draw_properties().screen_space_transform_is_animating)
1073 if (draw_properties().screen_space_transform_is_animating &&
1074 raster_contents_scale_ != ideal_contents_scale_ &&
1075 ShouldAdjustRasterScaleDuringScaleAnimations())
1078 bool is_pinching = layer_tree_impl()->PinchGestureActive();
1079 if (is_pinching && raster_page_scale_) {
1080 // We change our raster scale when it is:
1081 // - Higher than ideal (need a lower-res tiling available)
1082 // - Too far from ideal (need a higher-res tiling available)
1083 float ratio = ideal_page_scale_ / raster_page_scale_;
1084 if (raster_page_scale_ > ideal_page_scale_ ||
1085 ratio > kMaxScaleRatioDuringPinch)
1090 // When not pinching, match the ideal page scale factor.
1091 if (raster_page_scale_ != ideal_page_scale_)
1095 // Always match the ideal device scale factor.
1096 if (raster_device_scale_ != ideal_device_scale_)
1099 // When the source scale changes we want to match it, but not when animating
1100 // or when we've fixed the scale in place.
1101 if (!draw_properties().screen_space_transform_is_animating &&
1102 !raster_source_scale_is_fixed_ &&
1103 raster_source_scale_ != ideal_source_scale_)
1109 float PictureLayerImpl::SnappedContentsScale(float scale) {
1110 // If a tiling exists within the max snapping ratio, snap to its scale.
1111 float snapped_contents_scale = scale;
1112 float snapped_ratio = kSnapToExistingTilingRatio;
1113 for (size_t i = 0; i < tilings_->num_tilings(); ++i) {
1114 float tiling_contents_scale = tilings_->tiling_at(i)->contents_scale();
1115 float ratio = PositiveRatio(tiling_contents_scale, scale);
1116 if (ratio < snapped_ratio) {
1117 snapped_contents_scale = tiling_contents_scale;
1118 snapped_ratio = ratio;
1121 return snapped_contents_scale;
1124 void PictureLayerImpl::RecalculateRasterScales() {
1125 float old_raster_contents_scale = raster_contents_scale_;
1126 float old_raster_page_scale = raster_page_scale_;
1127 float old_raster_source_scale = raster_source_scale_;
1129 raster_device_scale_ = ideal_device_scale_;
1130 raster_page_scale_ = ideal_page_scale_;
1131 raster_source_scale_ = ideal_source_scale_;
1132 raster_contents_scale_ = ideal_contents_scale_;
1134 // If we're not animating, or leaving an animation, and the
1135 // ideal_source_scale_ changes, then things are unpredictable, and we fix
1136 // the raster_source_scale_ in place.
1137 if (old_raster_source_scale &&
1138 !draw_properties().screen_space_transform_is_animating &&
1139 !was_screen_space_transform_animating_ &&
1140 old_raster_source_scale != ideal_source_scale_)
1141 raster_source_scale_is_fixed_ = true;
1143 // TODO(danakj): Adjust raster source scale closer to ideal source scale at
1144 // a throttled rate. Possibly make use of invalidation_.IsEmpty() on pending
1145 // tree. This will allow CSS scale changes to get re-rastered at an
1146 // appropriate rate. (crbug.com/413636)
1147 if (raster_source_scale_is_fixed_) {
1148 raster_contents_scale_ /= raster_source_scale_;
1149 raster_source_scale_ = 1.f;
1152 // During pinch we completely ignore the current ideal scale, and just use
1153 // a multiple of the previous scale.
1154 // TODO(danakj): This seems crazy, we should use the current ideal, no?
1155 bool is_pinching = layer_tree_impl()->PinchGestureActive();
1156 if (is_pinching && old_raster_contents_scale) {
1157 // See ShouldAdjustRasterScale:
1158 // - When zooming out, preemptively create new tiling at lower resolution.
1159 // - When zooming in, approximate ideal using multiple of kMaxScaleRatio.
1160 bool zooming_out = old_raster_page_scale > ideal_page_scale_;
1161 float desired_contents_scale =
1162 zooming_out ? old_raster_contents_scale / kMaxScaleRatioDuringPinch
1163 : old_raster_contents_scale * kMaxScaleRatioDuringPinch;
1164 raster_contents_scale_ = SnappedContentsScale(desired_contents_scale);
1165 raster_page_scale_ =
1166 raster_contents_scale_ / raster_device_scale_ / raster_source_scale_;
1169 raster_contents_scale_ =
1170 std::max(raster_contents_scale_, MinimumContentsScale());
1172 // If we're not re-rasterizing during animation, rasterize at the maximum
1173 // scale that will occur during the animation, if the maximum scale is
1174 // known. However, to avoid excessive memory use, don't rasterize at a scale
1175 // at which this layer would become larger than the viewport.
1176 if (draw_properties().screen_space_transform_is_animating &&
1177 !ShouldAdjustRasterScaleDuringScaleAnimations()) {
1178 bool can_raster_at_maximum_scale = false;
1179 if (draw_properties().maximum_animation_contents_scale > 0.f) {
1180 gfx::Size bounds_at_maximum_scale = gfx::ToCeiledSize(gfx::ScaleSize(
1181 bounds(), draw_properties().maximum_animation_contents_scale));
1182 if (bounds_at_maximum_scale.GetArea() <=
1183 layer_tree_impl()->device_viewport_size().GetArea())
1184 can_raster_at_maximum_scale = true;
1186 if (can_raster_at_maximum_scale) {
1187 raster_contents_scale_ =
1188 std::max(raster_contents_scale_,
1189 draw_properties().maximum_animation_contents_scale);
1191 raster_contents_scale_ =
1192 std::max(raster_contents_scale_,
1193 1.f * ideal_page_scale_ * ideal_device_scale_);
1197 // If this layer would create zero or one tiles at this content scale,
1198 // don't create a low res tiling.
1199 gfx::Size content_bounds =
1200 gfx::ToCeiledSize(gfx::ScaleSize(bounds(), raster_contents_scale_));
1201 gfx::Size tile_size = CalculateTileSize(content_bounds);
1202 bool tile_covers_bounds = tile_size.width() >= content_bounds.width() &&
1203 tile_size.height() >= content_bounds.height();
1204 if (tile_size.IsEmpty() || tile_covers_bounds) {
1205 low_res_raster_contents_scale_ = raster_contents_scale_;
1209 float low_res_factor =
1210 layer_tree_impl()->settings().low_res_contents_scale_factor;
1211 low_res_raster_contents_scale_ = std::max(
1212 raster_contents_scale_ * low_res_factor,
1213 MinimumContentsScale());
1216 void PictureLayerImpl::CleanUpTilingsOnActiveLayer(
1217 std::vector<PictureLayerTiling*> used_tilings) {
1218 DCHECK(layer_tree_impl()->IsActiveTree());
1219 if (tilings_->num_tilings() == 0)
1222 float min_acceptable_high_res_scale = std::min(
1223 raster_contents_scale_, ideal_contents_scale_);
1224 float max_acceptable_high_res_scale = std::max(
1225 raster_contents_scale_, ideal_contents_scale_);
1226 float twin_low_res_scale = 0.f;
1228 PictureLayerImpl* twin = twin_layer_;
1229 if (twin && twin->CanHaveTilings()) {
1230 min_acceptable_high_res_scale = std::min(
1231 min_acceptable_high_res_scale,
1232 std::min(twin->raster_contents_scale_, twin->ideal_contents_scale_));
1233 max_acceptable_high_res_scale = std::max(
1234 max_acceptable_high_res_scale,
1235 std::max(twin->raster_contents_scale_, twin->ideal_contents_scale_));
1237 for (size_t i = 0; i < twin->tilings_->num_tilings(); ++i) {
1238 PictureLayerTiling* tiling = twin->tilings_->tiling_at(i);
1239 if (tiling->resolution() == LOW_RESOLUTION)
1240 twin_low_res_scale = tiling->contents_scale();
1244 std::vector<PictureLayerTiling*> to_remove;
1245 for (size_t i = 0; i < tilings_->num_tilings(); ++i) {
1246 PictureLayerTiling* tiling = tilings_->tiling_at(i);
1248 // Keep multiple high resolution tilings even if not used to help
1249 // activate earlier at non-ideal resolutions.
1250 if (tiling->contents_scale() >= min_acceptable_high_res_scale &&
1251 tiling->contents_scale() <= max_acceptable_high_res_scale)
1254 // Keep low resolution tilings, if the layer should have them.
1255 if (layer_tree_impl()->create_low_res_tiling()) {
1256 if (tiling->resolution() == LOW_RESOLUTION ||
1257 tiling->contents_scale() == twin_low_res_scale)
1261 // Don't remove tilings that are being used (and thus would cause a flash.)
1262 if (std::find(used_tilings.begin(), used_tilings.end(), tiling) !=
1266 to_remove.push_back(tiling);
1269 if (to_remove.empty())
1272 PictureLayerImpl* recycled_twin = GetRecycledTwinLayer();
1273 // Remove tilings on this tree and the twin tree.
1274 for (size_t i = 0; i < to_remove.size(); ++i) {
1275 const PictureLayerTiling* twin_tiling = GetTwinTiling(to_remove[i]);
1276 // Only remove tilings from the twin layer if they have
1277 // NON_IDEAL_RESOLUTION.
1278 if (twin_tiling && twin_tiling->resolution() == NON_IDEAL_RESOLUTION)
1279 twin->RemoveTiling(to_remove[i]->contents_scale());
1280 // Remove the tiling from the recycle tree. Note that we ignore resolution,
1281 // since we don't need to maintain high/low res on the recycle tree.
1283 recycled_twin->RemoveTiling(to_remove[i]->contents_scale());
1284 // TODO(enne): temporary sanity CHECK for http://crbug.com/358350
1285 CHECK_NE(HIGH_RESOLUTION, to_remove[i]->resolution());
1286 tilings_->Remove(to_remove[i]);
1289 DCHECK_GT(tilings_->num_tilings(), 0u);
1290 SanityCheckTilingState();
1293 float PictureLayerImpl::MinimumContentsScale() const {
1294 float setting_min = layer_tree_impl()->settings().minimum_contents_scale;
1296 // If the contents scale is less than 1 / width (also for height),
1297 // then it will end up having less than one pixel of content in that
1298 // dimension. Bump the minimum contents scale up in this case to prevent
1299 // this from happening.
1300 int min_dimension = std::min(bounds().width(), bounds().height());
1304 return std::max(1.f / min_dimension, setting_min);
1307 void PictureLayerImpl::ResetRasterScale() {
1308 raster_page_scale_ = 0.f;
1309 raster_device_scale_ = 0.f;
1310 raster_source_scale_ = 0.f;
1311 raster_contents_scale_ = 0.f;
1312 low_res_raster_contents_scale_ = 0.f;
1313 raster_source_scale_is_fixed_ = false;
1315 // When raster scales aren't valid, don't update tile priorities until
1316 // this layer has been updated via UpdateDrawProperties.
1317 should_update_tile_priorities_ = false;
1320 bool PictureLayerImpl::CanHaveTilings() const {
1321 if (pile_->is_solid_color())
1323 if (!DrawsContent())
1325 if (!pile_->HasRecordings())
1330 bool PictureLayerImpl::CanHaveTilingWithScale(float contents_scale) const {
1331 if (!CanHaveTilings())
1333 if (contents_scale < MinimumContentsScale())
1338 void PictureLayerImpl::SanityCheckTilingState() const {
1340 // Recycle tree doesn't have any restrictions.
1341 if (layer_tree_impl()->IsRecycleTree())
1344 if (!CanHaveTilings()) {
1345 DCHECK_EQ(0u, tilings_->num_tilings());
1348 if (tilings_->num_tilings() == 0)
1351 // MarkVisibleResourcesAsRequired depends on having exactly 1 high res
1352 // tiling to mark its tiles as being required for activation.
1353 DCHECK_EQ(1, tilings_->NumHighResTilings());
1357 bool PictureLayerImpl::ShouldAdjustRasterScaleDuringScaleAnimations() const {
1358 if (!layer_tree_impl()->use_gpu_rasterization())
1361 // Re-rastering text at different scales using GPU rasterization causes
1362 // texture uploads for glyphs at each scale (see crbug.com/366225). To
1363 // workaround this performance issue, we don't re-rasterize layers with
1364 // text during scale animations.
1365 // TODO(ajuma): Remove this workaround once text can be efficiently
1366 // re-rastered at different scales (e.g. by using distance-field fonts).
1367 if (pile_->has_text())
1373 float PictureLayerImpl::MaximumTilingContentsScale() const {
1374 float max_contents_scale = MinimumContentsScale();
1375 for (size_t i = 0; i < tilings_->num_tilings(); ++i) {
1376 const PictureLayerTiling* tiling = tilings_->tiling_at(i);
1377 max_contents_scale = std::max(max_contents_scale, tiling->contents_scale());
1379 return max_contents_scale;
1382 void PictureLayerImpl::UpdateIdealScales() {
1383 DCHECK(CanHaveTilings());
1385 float min_contents_scale = MinimumContentsScale();
1386 DCHECK_GT(min_contents_scale, 0.f);
1387 float min_page_scale = layer_tree_impl()->min_page_scale_factor();
1388 DCHECK_GT(min_page_scale, 0.f);
1389 float min_device_scale = 1.f;
1390 float min_source_scale =
1391 min_contents_scale / min_page_scale / min_device_scale;
1393 float ideal_page_scale = draw_properties().page_scale_factor;
1394 float ideal_device_scale = draw_properties().device_scale_factor;
1395 float ideal_source_scale = draw_properties().ideal_contents_scale /
1396 ideal_page_scale / ideal_device_scale;
1397 ideal_contents_scale_ =
1398 std::max(draw_properties().ideal_contents_scale, min_contents_scale);
1399 ideal_page_scale_ = draw_properties().page_scale_factor;
1400 ideal_device_scale_ = draw_properties().device_scale_factor;
1401 ideal_source_scale_ = std::max(ideal_source_scale, min_source_scale);
1404 void PictureLayerImpl::GetDebugBorderProperties(
1406 float* width) const {
1407 *color = DebugColors::TiledContentLayerBorderColor();
1408 *width = DebugColors::TiledContentLayerBorderWidth(layer_tree_impl());
1411 void PictureLayerImpl::GetAllTilesForTracing(
1412 std::set<const Tile*>* tiles) const {
1416 for (size_t i = 0; i < tilings_->num_tilings(); ++i)
1417 tilings_->tiling_at(i)->GetAllTilesForTracing(tiles);
1420 void PictureLayerImpl::AsValueInto(base::debug::TracedValue* state) const {
1421 const_cast<PictureLayerImpl*>(this)->DoPostCommitInitializationIfNeeded();
1422 LayerImpl::AsValueInto(state);
1423 state->SetDouble("ideal_contents_scale", ideal_contents_scale_);
1424 state->SetDouble("geometry_contents_scale", MaximumTilingContentsScale());
1425 state->BeginArray("tilings");
1426 tilings_->AsValueInto(state);
1429 state->BeginArray("tile_priority_rect");
1430 MathUtil::AddToTracedValue(GetViewportForTilePriorityInContentSpace(), state);
1433 state->BeginArray("visible_rect");
1434 MathUtil::AddToTracedValue(visible_content_rect(), state);
1437 state->BeginArray("pictures");
1438 pile_->AsValueInto(state);
1441 state->BeginArray("invalidation");
1442 invalidation_.AsValueInto(state);
1445 state->BeginArray("coverage_tiles");
1446 for (PictureLayerTilingSet::CoverageIterator iter(tilings_.get(),
1448 gfx::Rect(content_bounds()),
1449 ideal_contents_scale_);
1452 state->BeginDictionary();
1454 state->BeginArray("geometry_rect");
1455 MathUtil::AddToTracedValue(iter.geometry_rect(), state);
1459 TracedValue::SetIDRef(*iter, state, "tile");
1461 state->EndDictionary();
1466 size_t PictureLayerImpl::GPUMemoryUsageInBytes() const {
1467 const_cast<PictureLayerImpl*>(this)->DoPostCommitInitializationIfNeeded();
1468 return tilings_->GPUMemoryUsageInBytes();
1471 void PictureLayerImpl::RunMicroBenchmark(MicroBenchmarkImpl* benchmark) {
1472 benchmark->RunOnLayer(this);
1475 WhichTree PictureLayerImpl::GetTree() const {
1476 return layer_tree_impl()->IsActiveTree() ? ACTIVE_TREE : PENDING_TREE;
1479 bool PictureLayerImpl::IsOnActiveOrPendingTree() const {
1480 return !layer_tree_impl()->IsRecycleTree();
1483 bool PictureLayerImpl::HasValidTilePriorities() const {
1484 return IsOnActiveOrPendingTree() && IsDrawnRenderSurfaceLayerListMember();
1487 bool PictureLayerImpl::AllTilesRequiredForActivationAreReadyToDraw() const {
1488 if (!layer_tree_impl()->IsPendingTree())
1491 if (!HasValidTilePriorities())
1497 if (visible_content_rect().IsEmpty())
1500 for (size_t i = 0; i < tilings_->num_tilings(); ++i) {
1501 PictureLayerTiling* tiling = tilings_->tiling_at(i);
1502 if (tiling->resolution() != HIGH_RESOLUTION &&
1503 tiling->resolution() != LOW_RESOLUTION)
1506 gfx::Rect rect(visible_content_rect());
1507 for (PictureLayerTiling::CoverageIterator iter(tiling, 1.f, rect); iter;
1509 const Tile* tile = *iter;
1510 // A null tile (i.e. missing recording) can just be skipped.
1514 if (tile->required_for_activation() && !tile->IsReadyToDraw())
1522 PictureLayerImpl::LayerRasterTileIterator::LayerRasterTileIterator()
1523 : layer_(NULL), current_stage_(arraysize(stages_)) {
1526 PictureLayerImpl::LayerRasterTileIterator::LayerRasterTileIterator(
1527 PictureLayerImpl* layer,
1528 bool prioritize_low_res)
1529 : layer_(layer), current_stage_(0) {
1532 // Early out if the layer has no tilings.
1533 if (!layer_->tilings_ || !layer_->tilings_->num_tilings()) {
1534 current_stage_ = arraysize(stages_);
1538 // Tiles without valid priority are treated as having lowest priority and
1539 // never considered for raster.
1540 if (!layer_->HasValidTilePriorities()) {
1541 current_stage_ = arraysize(stages_);
1545 WhichTree tree = layer_->GetTree();
1547 // Find high and low res tilings and initialize the iterators.
1548 for (size_t i = 0; i < layer_->tilings_->num_tilings(); ++i) {
1549 PictureLayerTiling* tiling = layer_->tilings_->tiling_at(i);
1550 if (tiling->resolution() == HIGH_RESOLUTION) {
1551 iterators_[HIGH_RES] =
1552 PictureLayerTiling::TilingRasterTileIterator(tiling, tree);
1555 if (tiling->resolution() == LOW_RESOLUTION) {
1556 iterators_[LOW_RES] =
1557 PictureLayerTiling::TilingRasterTileIterator(tiling, tree);
1561 if (prioritize_low_res) {
1562 stages_[0].iterator_type = LOW_RES;
1563 stages_[0].tile_type = TilePriority::NOW;
1565 stages_[1].iterator_type = HIGH_RES;
1566 stages_[1].tile_type = TilePriority::NOW;
1568 stages_[0].iterator_type = HIGH_RES;
1569 stages_[0].tile_type = TilePriority::NOW;
1571 stages_[1].iterator_type = LOW_RES;
1572 stages_[1].tile_type = TilePriority::NOW;
1575 stages_[2].iterator_type = HIGH_RES;
1576 stages_[2].tile_type = TilePriority::SOON;
1578 stages_[3].iterator_type = HIGH_RES;
1579 stages_[3].tile_type = TilePriority::EVENTUALLY;
1581 IteratorType index = stages_[current_stage_].iterator_type;
1582 TilePriority::PriorityBin tile_type = stages_[current_stage_].tile_type;
1583 if (!iterators_[index] || iterators_[index].get_type() != tile_type)
1584 AdvanceToNextStage();
1587 PictureLayerImpl::LayerRasterTileIterator::~LayerRasterTileIterator() {}
1589 PictureLayerImpl::LayerRasterTileIterator::operator bool() const {
1590 return current_stage_ < arraysize(stages_);
1593 PictureLayerImpl::LayerRasterTileIterator&
1594 PictureLayerImpl::LayerRasterTileIterator::
1596 IteratorType index = stages_[current_stage_].iterator_type;
1597 TilePriority::PriorityBin tile_type = stages_[current_stage_].tile_type;
1599 // First advance the iterator.
1600 DCHECK(iterators_[index]);
1601 DCHECK(iterators_[index].get_type() == tile_type);
1602 ++iterators_[index];
1604 if (!iterators_[index] || iterators_[index].get_type() != tile_type)
1605 AdvanceToNextStage();
1610 Tile* PictureLayerImpl::LayerRasterTileIterator::operator*() {
1613 IteratorType index = stages_[current_stage_].iterator_type;
1614 DCHECK(iterators_[index]);
1615 DCHECK(iterators_[index].get_type() == stages_[current_stage_].tile_type);
1617 return *iterators_[index];
1620 const Tile* PictureLayerImpl::LayerRasterTileIterator::operator*() const {
1623 IteratorType index = stages_[current_stage_].iterator_type;
1624 DCHECK(iterators_[index]);
1625 DCHECK(iterators_[index].get_type() == stages_[current_stage_].tile_type);
1627 return *iterators_[index];
1630 void PictureLayerImpl::LayerRasterTileIterator::AdvanceToNextStage() {
1631 DCHECK_LT(current_stage_, arraysize(stages_));
1633 while (current_stage_ < arraysize(stages_)) {
1634 IteratorType index = stages_[current_stage_].iterator_type;
1635 TilePriority::PriorityBin tile_type = stages_[current_stage_].tile_type;
1637 if (iterators_[index] && iterators_[index].get_type() == tile_type)
1643 PictureLayerImpl::LayerEvictionTileIterator::LayerEvictionTileIterator()
1645 tree_priority_(SAME_PRIORITY_FOR_BOTH_TREES),
1646 current_category_(PictureLayerTiling::EVENTUALLY),
1647 current_tiling_range_type_(PictureLayerTilingSet::HIGHER_THAN_HIGH_RES),
1648 current_tiling_(0u) {
1651 PictureLayerImpl::LayerEvictionTileIterator::LayerEvictionTileIterator(
1652 PictureLayerImpl* layer,
1653 TreePriority tree_priority)
1655 tree_priority_(tree_priority),
1656 current_category_(PictureLayerTiling::EVENTUALLY),
1657 current_tiling_range_type_(PictureLayerTilingSet::HIGHER_THAN_HIGH_RES),
1658 current_tiling_(CurrentTilingRange().start - 1u) {
1659 // TODO(vmpstr): Once tile priorities are determined by the iterators, ensure
1660 // that layers that don't have valid tile priorities have lowest priorities so
1661 // they evict their tiles first (crbug.com/381704)
1662 DCHECK(layer_->tilings_);
1664 if (!AdvanceToNextTiling())
1667 current_iterator_ = PictureLayerTiling::TilingEvictionTileIterator(
1668 layer_->tilings_->tiling_at(CurrentTilingIndex()),
1671 } while (!current_iterator_);
1674 PictureLayerImpl::LayerEvictionTileIterator::~LayerEvictionTileIterator() {
1677 Tile* PictureLayerImpl::LayerEvictionTileIterator::operator*() {
1679 return *current_iterator_;
1682 const Tile* PictureLayerImpl::LayerEvictionTileIterator::operator*() const {
1684 return *current_iterator_;
1687 PictureLayerImpl::LayerEvictionTileIterator&
1688 PictureLayerImpl::LayerEvictionTileIterator::
1691 ++current_iterator_;
1692 while (!current_iterator_) {
1693 if (!AdvanceToNextTiling())
1696 current_iterator_ = PictureLayerTiling::TilingEvictionTileIterator(
1697 layer_->tilings_->tiling_at(CurrentTilingIndex()),
1704 PictureLayerImpl::LayerEvictionTileIterator::operator bool() const {
1705 return !!current_iterator_;
1708 bool PictureLayerImpl::LayerEvictionTileIterator::AdvanceToNextCategory() {
1709 switch (current_category_) {
1710 case PictureLayerTiling::EVENTUALLY:
1712 PictureLayerTiling::EVENTUALLY_AND_REQUIRED_FOR_ACTIVATION;
1714 case PictureLayerTiling::EVENTUALLY_AND_REQUIRED_FOR_ACTIVATION:
1715 current_category_ = PictureLayerTiling::SOON;
1717 case PictureLayerTiling::SOON:
1718 current_category_ = PictureLayerTiling::SOON_AND_REQUIRED_FOR_ACTIVATION;
1720 case PictureLayerTiling::SOON_AND_REQUIRED_FOR_ACTIVATION:
1721 current_category_ = PictureLayerTiling::NOW;
1723 case PictureLayerTiling::NOW:
1724 current_category_ = PictureLayerTiling::NOW_AND_REQUIRED_FOR_ACTIVATION;
1726 case PictureLayerTiling::NOW_AND_REQUIRED_FOR_ACTIVATION:
1734 PictureLayerImpl::LayerEvictionTileIterator::AdvanceToNextTilingRangeType() {
1735 switch (current_tiling_range_type_) {
1736 case PictureLayerTilingSet::HIGHER_THAN_HIGH_RES:
1737 current_tiling_range_type_ = PictureLayerTilingSet::LOWER_THAN_LOW_RES;
1739 case PictureLayerTilingSet::LOWER_THAN_LOW_RES:
1740 current_tiling_range_type_ =
1741 PictureLayerTilingSet::BETWEEN_HIGH_AND_LOW_RES;
1743 case PictureLayerTilingSet::BETWEEN_HIGH_AND_LOW_RES:
1744 current_tiling_range_type_ = PictureLayerTilingSet::LOW_RES;
1746 case PictureLayerTilingSet::LOW_RES:
1747 current_tiling_range_type_ = PictureLayerTilingSet::HIGH_RES;
1749 case PictureLayerTilingSet::HIGH_RES:
1750 if (!AdvanceToNextCategory())
1753 current_tiling_range_type_ = PictureLayerTilingSet::HIGHER_THAN_HIGH_RES;
1760 bool PictureLayerImpl::LayerEvictionTileIterator::AdvanceToNextTiling() {
1761 DCHECK_NE(current_tiling_, CurrentTilingRange().end);
1763 while (current_tiling_ == CurrentTilingRange().end) {
1764 if (!AdvanceToNextTilingRangeType())
1767 current_tiling_ = CurrentTilingRange().start;
1772 PictureLayerTilingSet::TilingRange
1773 PictureLayerImpl::LayerEvictionTileIterator::CurrentTilingRange() const {
1774 return layer_->tilings_->GetTilingRange(current_tiling_range_type_);
1777 size_t PictureLayerImpl::LayerEvictionTileIterator::CurrentTilingIndex() const {
1778 DCHECK_NE(current_tiling_, CurrentTilingRange().end);
1779 switch (current_tiling_range_type_) {
1780 case PictureLayerTilingSet::HIGHER_THAN_HIGH_RES:
1781 case PictureLayerTilingSet::LOW_RES:
1782 case PictureLayerTilingSet::HIGH_RES:
1783 return current_tiling_;
1784 // Tilings in the following ranges are accessed in reverse order.
1785 case PictureLayerTilingSet::BETWEEN_HIGH_AND_LOW_RES:
1786 case PictureLayerTilingSet::LOWER_THAN_LOW_RES: {
1787 PictureLayerTilingSet::TilingRange tiling_range = CurrentTilingRange();
1788 size_t current_tiling_range_offset = current_tiling_ - tiling_range.start;
1789 return tiling_range.end - 1 - current_tiling_range_offset;