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"
10 #include "base/debug/trace_event_argument.h"
11 #include "base/time/time.h"
12 #include "cc/base/math_util.h"
13 #include "cc/base/util.h"
14 #include "cc/debug/debug_colors.h"
15 #include "cc/debug/micro_benchmark_impl.h"
16 #include "cc/debug/traced_value.h"
17 #include "cc/layers/append_quads_data.h"
18 #include "cc/quads/checkerboard_draw_quad.h"
19 #include "cc/quads/debug_border_draw_quad.h"
20 #include "cc/quads/picture_draw_quad.h"
21 #include "cc/quads/solid_color_draw_quad.h"
22 #include "cc/quads/tile_draw_quad.h"
23 #include "cc/resources/tile_manager.h"
24 #include "cc/trees/layer_tree_impl.h"
25 #include "cc/trees/occlusion_tracker.h"
26 #include "ui/gfx/quad_f.h"
27 #include "ui/gfx/rect_conversions.h"
28 #include "ui/gfx/size_conversions.h"
31 const float kMaxScaleRatioDuringPinch = 2.0f;
33 // When creating a new tiling during pinch, snap to an existing
34 // tiling's scale if the desired scale is within this ratio.
35 const float kSnapToExistingTilingRatio = 1.2f;
37 // Estimate skewport 60 frames ahead for pre-rasterization on the CPU.
38 const float kCpuSkewportTargetTimeInFrames = 60.0f;
40 // Don't pre-rasterize on the GPU (except for kBackflingGuardDistancePixels in
41 // TileManager::BinFromTilePriority).
42 const float kGpuSkewportTargetTimeInFrames = 0.0f;
48 PictureLayerImpl::Pair::Pair() : active(NULL), pending(NULL) {
51 PictureLayerImpl::Pair::Pair(PictureLayerImpl* active_layer,
52 PictureLayerImpl* pending_layer)
53 : active(active_layer), pending(pending_layer) {
56 PictureLayerImpl::Pair::~Pair() {
59 PictureLayerImpl::PictureLayerImpl(LayerTreeImpl* tree_impl, int id)
60 : LayerImpl(tree_impl, id),
62 pile_(PicturePileImpl::Create()),
63 ideal_page_scale_(0.f),
64 ideal_device_scale_(0.f),
65 ideal_source_scale_(0.f),
66 ideal_contents_scale_(0.f),
67 raster_page_scale_(0.f),
68 raster_device_scale_(0.f),
69 raster_source_scale_(0.f),
70 raster_contents_scale_(0.f),
71 low_res_raster_contents_scale_(0.f),
72 raster_source_scale_is_fixed_(false),
73 was_screen_space_transform_animating_(false),
74 needs_post_commit_initialization_(true),
75 should_update_tile_priorities_(false) {
76 layer_tree_impl()->RegisterPictureLayerImpl(this);
79 PictureLayerImpl::~PictureLayerImpl() {
80 layer_tree_impl()->UnregisterPictureLayerImpl(this);
83 const char* PictureLayerImpl::LayerTypeAsString() const {
84 return "cc::PictureLayerImpl";
87 scoped_ptr<LayerImpl> PictureLayerImpl::CreateLayerImpl(
88 LayerTreeImpl* tree_impl) {
89 return PictureLayerImpl::Create(tree_impl, id()).PassAs<LayerImpl>();
92 void PictureLayerImpl::PushPropertiesTo(LayerImpl* base_layer) {
93 // It's possible this layer was never drawn or updated (e.g. because it was
94 // a descendant of an opacity 0 layer).
95 DoPostCommitInitializationIfNeeded();
96 PictureLayerImpl* layer_impl = static_cast<PictureLayerImpl*>(base_layer);
98 // We have already synced the important bits from the the active layer, and
99 // we will soon swap out its tilings and use them for recycling. However,
100 // there are now tiles in this layer's tilings that were unref'd and replaced
101 // with new tiles (due to invalidation). This resets all active priorities on
102 // the to-be-recycled tiling to ensure replaced tiles don't linger and take
103 // memory (due to a stale 'active' priority).
104 if (layer_impl->tilings_)
105 layer_impl->tilings_->DidBecomeRecycled();
107 LayerImpl::PushPropertiesTo(base_layer);
109 // When the pending tree pushes to the active tree, the pending twin
111 layer_impl->twin_layer_ = NULL;
114 layer_impl->pile_ = pile_;
116 // Tilings would be expensive to push, so we swap.
117 layer_impl->tilings_.swap(tilings_);
119 // Remove invalidated tiles from what will become a recycle tree.
121 tilings_->RemoveTilesInRegion(invalidation_);
123 layer_impl->tilings_->SetClient(layer_impl);
125 tilings_->SetClient(this);
127 layer_impl->raster_page_scale_ = raster_page_scale_;
128 layer_impl->raster_device_scale_ = raster_device_scale_;
129 layer_impl->raster_source_scale_ = raster_source_scale_;
130 layer_impl->raster_contents_scale_ = raster_contents_scale_;
131 layer_impl->low_res_raster_contents_scale_ = low_res_raster_contents_scale_;
132 layer_impl->needs_post_commit_initialization_ = false;
134 // The invalidation on this soon-to-be-recycled layer must be cleared to
135 // mirror clearing the invalidation in PictureLayer's version of this function
136 // in case push properties is skipped.
137 layer_impl->invalidation_.Swap(&invalidation_);
138 invalidation_.Clear();
139 needs_post_commit_initialization_ = true;
141 // We always need to push properties.
142 // See http://crbug.com/303943
143 needs_push_properties_ = true;
146 void PictureLayerImpl::AppendQuads(
147 RenderPass* render_pass,
148 const OcclusionTracker<LayerImpl>& occlusion_tracker,
149 AppendQuadsData* append_quads_data) {
150 DCHECK(!needs_post_commit_initialization_);
152 float max_contents_scale = MaximumTilingContentsScale();
153 gfx::Transform scaled_draw_transform = draw_transform();
154 scaled_draw_transform.Scale(SK_MScalar1 / max_contents_scale,
155 SK_MScalar1 / max_contents_scale);
156 gfx::Size scaled_content_bounds =
157 gfx::ToCeiledSize(gfx::ScaleSize(content_bounds(), max_contents_scale));
159 gfx::Rect scaled_visible_content_rect =
160 gfx::ScaleToEnclosingRect(visible_content_rect(), max_contents_scale);
161 scaled_visible_content_rect.Intersect(gfx::Rect(scaled_content_bounds));
163 SharedQuadState* shared_quad_state =
164 render_pass->CreateAndAppendSharedQuadState();
165 shared_quad_state->SetAll(scaled_draw_transform,
166 scaled_content_bounds,
167 scaled_visible_content_rect,
168 draw_properties().clip_rect,
169 draw_properties().is_clipped,
170 draw_properties().opacity,
172 sorting_context_id_);
174 if (current_draw_mode_ == DRAW_MODE_RESOURCELESS_SOFTWARE) {
175 AppendDebugBorderQuad(
177 scaled_content_bounds,
180 DebugColors::DirectPictureBorderColor(),
181 DebugColors::DirectPictureBorderWidth(layer_tree_impl()));
183 gfx::Rect geometry_rect = scaled_visible_content_rect;
184 gfx::Rect opaque_rect = contents_opaque() ? geometry_rect : gfx::Rect();
185 gfx::Rect visible_geometry_rect = occlusion_tracker.UnoccludedContentRect(
186 geometry_rect, scaled_draw_transform);
187 if (visible_geometry_rect.IsEmpty())
190 gfx::Size texture_size = scaled_visible_content_rect.size();
191 gfx::RectF texture_rect = gfx::RectF(texture_size);
192 gfx::Rect quad_content_rect = scaled_visible_content_rect;
194 PictureDrawQuad* quad =
195 render_pass->CreateAndAppendDrawQuad<PictureDrawQuad>();
196 quad->SetNew(shared_quad_state,
199 visible_geometry_rect,
209 AppendDebugBorderQuad(
210 render_pass, scaled_content_bounds, shared_quad_state, append_quads_data);
212 if (ShowDebugBorders()) {
213 for (PictureLayerTilingSet::CoverageIterator iter(
216 scaled_visible_content_rect,
217 ideal_contents_scale_);
222 if (*iter && iter->IsReadyToDraw()) {
223 ManagedTileState::TileVersion::Mode mode =
224 iter->GetTileVersionForDrawing().mode();
225 if (mode == ManagedTileState::TileVersion::SOLID_COLOR_MODE) {
226 color = DebugColors::SolidColorTileBorderColor();
227 width = DebugColors::SolidColorTileBorderWidth(layer_tree_impl());
228 } else if (mode == ManagedTileState::TileVersion::PICTURE_PILE_MODE) {
229 color = DebugColors::PictureTileBorderColor();
230 width = DebugColors::PictureTileBorderWidth(layer_tree_impl());
231 } else if (iter->priority(ACTIVE_TREE).resolution == HIGH_RESOLUTION) {
232 color = DebugColors::HighResTileBorderColor();
233 width = DebugColors::HighResTileBorderWidth(layer_tree_impl());
234 } else if (iter->priority(ACTIVE_TREE).resolution == LOW_RESOLUTION) {
235 color = DebugColors::LowResTileBorderColor();
236 width = DebugColors::LowResTileBorderWidth(layer_tree_impl());
237 } else if (iter->contents_scale() > max_contents_scale) {
238 color = DebugColors::ExtraHighResTileBorderColor();
239 width = DebugColors::ExtraHighResTileBorderWidth(layer_tree_impl());
241 color = DebugColors::ExtraLowResTileBorderColor();
242 width = DebugColors::ExtraLowResTileBorderWidth(layer_tree_impl());
245 color = DebugColors::MissingTileBorderColor();
246 width = DebugColors::MissingTileBorderWidth(layer_tree_impl());
249 DebugBorderDrawQuad* debug_border_quad =
250 render_pass->CreateAndAppendDrawQuad<DebugBorderDrawQuad>();
251 gfx::Rect geometry_rect = iter.geometry_rect();
252 gfx::Rect visible_geometry_rect = geometry_rect;
253 debug_border_quad->SetNew(shared_quad_state,
255 visible_geometry_rect,
261 // Keep track of the tilings that were used so that tilings that are
262 // unused can be considered for removal.
263 std::vector<PictureLayerTiling*> seen_tilings;
265 // Ignore missing tiles outside of viewport for tile priority. This is
266 // normally the same as draw viewport but can be independently overridden by
267 // embedders like Android WebView with SetExternalDrawConstraints.
268 gfx::Rect scaled_viewport_for_tile_priority = gfx::ScaleToEnclosingRect(
269 GetViewportForTilePriorityInContentSpace(), max_contents_scale);
271 size_t missing_tile_count = 0u;
272 size_t on_demand_missing_tile_count = 0u;
273 for (PictureLayerTilingSet::CoverageIterator iter(tilings_.get(),
275 scaled_visible_content_rect,
276 ideal_contents_scale_);
279 gfx::Rect geometry_rect = iter.geometry_rect();
280 gfx::Rect visible_geometry_rect = occlusion_tracker.UnoccludedContentRect(
281 geometry_rect, scaled_draw_transform);
282 if (visible_geometry_rect.IsEmpty())
285 append_quads_data->visible_content_area +=
286 visible_geometry_rect.width() * visible_geometry_rect.height();
288 bool has_draw_quad = false;
289 if (*iter && iter->IsReadyToDraw()) {
290 const ManagedTileState::TileVersion& tile_version =
291 iter->GetTileVersionForDrawing();
292 switch (tile_version.mode()) {
293 case ManagedTileState::TileVersion::RESOURCE_MODE: {
294 gfx::RectF texture_rect = iter.texture_rect();
295 gfx::Rect opaque_rect = iter->opaque_rect();
296 opaque_rect.Intersect(geometry_rect);
298 if (iter->contents_scale() != ideal_contents_scale_ &&
299 geometry_rect.Intersects(scaled_viewport_for_tile_priority)) {
300 append_quads_data->num_incomplete_tiles++;
304 render_pass->CreateAndAppendDrawQuad<TileDrawQuad>();
305 quad->SetNew(shared_quad_state,
308 visible_geometry_rect,
309 tile_version.get_resource_id(),
312 tile_version.contents_swizzled());
313 has_draw_quad = true;
316 case ManagedTileState::TileVersion::PICTURE_PILE_MODE: {
317 if (!layer_tree_impl()
318 ->GetRendererCapabilities()
319 .allow_rasterize_on_demand) {
320 ++on_demand_missing_tile_count;
324 gfx::RectF texture_rect = iter.texture_rect();
325 gfx::Rect opaque_rect = iter->opaque_rect();
326 opaque_rect.Intersect(geometry_rect);
328 ResourceProvider* resource_provider =
329 layer_tree_impl()->resource_provider();
330 ResourceFormat format =
331 resource_provider->memory_efficient_texture_format();
332 PictureDrawQuad* quad =
333 render_pass->CreateAndAppendDrawQuad<PictureDrawQuad>();
334 quad->SetNew(shared_quad_state,
337 visible_geometry_rect,
341 iter->content_rect(),
342 iter->contents_scale(),
344 has_draw_quad = true;
347 case ManagedTileState::TileVersion::SOLID_COLOR_MODE: {
348 SolidColorDrawQuad* quad =
349 render_pass->CreateAndAppendDrawQuad<SolidColorDrawQuad>();
350 quad->SetNew(shared_quad_state,
352 visible_geometry_rect,
353 tile_version.get_solid_color(),
355 has_draw_quad = true;
361 if (!has_draw_quad) {
362 if (draw_checkerboard_for_missing_tiles()) {
363 CheckerboardDrawQuad* quad =
364 render_pass->CreateAndAppendDrawQuad<CheckerboardDrawQuad>();
365 SkColor color = DebugColors::DefaultCheckerboardColor();
367 shared_quad_state, geometry_rect, visible_geometry_rect, color);
369 SkColor color = SafeOpaqueBackgroundColor();
370 SolidColorDrawQuad* quad =
371 render_pass->CreateAndAppendDrawQuad<SolidColorDrawQuad>();
372 quad->SetNew(shared_quad_state,
374 visible_geometry_rect,
379 if (geometry_rect.Intersects(scaled_viewport_for_tile_priority)) {
380 append_quads_data->num_missing_tiles++;
381 ++missing_tile_count;
383 append_quads_data->approximated_visible_content_area +=
384 visible_geometry_rect.width() * visible_geometry_rect.height();
388 if (iter->priority(ACTIVE_TREE).resolution != HIGH_RESOLUTION) {
389 append_quads_data->approximated_visible_content_area +=
390 visible_geometry_rect.width() * visible_geometry_rect.height();
393 if (seen_tilings.empty() || seen_tilings.back() != iter.CurrentTiling())
394 seen_tilings.push_back(iter.CurrentTiling());
397 if (missing_tile_count) {
398 TRACE_EVENT_INSTANT2("cc",
399 "PictureLayerImpl::AppendQuads checkerboard",
400 TRACE_EVENT_SCOPE_THREAD,
401 "missing_tile_count",
403 "on_demand_missing_tile_count",
404 on_demand_missing_tile_count);
407 // Aggressively remove any tilings that are not seen to save memory. Note
408 // that this is at the expense of doing cause more frequent re-painting. A
409 // better scheme would be to maintain a tighter visible_content_rect for the
411 CleanUpTilingsOnActiveLayer(seen_tilings);
414 void PictureLayerImpl::UpdateTiles(
415 const OcclusionTracker<LayerImpl>* occlusion_tracker) {
416 TRACE_EVENT0("cc", "PictureLayerImpl::UpdateTiles");
418 DoPostCommitInitializationIfNeeded();
420 // TODO(danakj): We should always get an occlusion tracker when we are using
421 // occlusion, so update this check when we don't use a pending tree in the
422 // browser compositor.
423 DCHECK(!occlusion_tracker ||
424 layer_tree_impl()->settings().use_occlusion_for_tile_prioritization);
426 // Transforms and viewport are invalid for tile management inside a
427 // resourceless software draw, so don't update them.
428 if (!layer_tree_impl()->resourceless_software_draw()) {
429 visible_rect_for_tile_priority_ = visible_content_rect();
430 viewport_rect_for_tile_priority_ =
431 layer_tree_impl()->ViewportRectForTilePriority();
432 screen_space_transform_for_tile_priority_ = screen_space_transform();
435 if (!CanHaveTilings()) {
436 ideal_page_scale_ = 0.f;
437 ideal_device_scale_ = 0.f;
438 ideal_contents_scale_ = 0.f;
439 ideal_source_scale_ = 0.f;
440 SanityCheckTilingState();
446 DCHECK(tilings_->num_tilings() > 0 || raster_contents_scale_ == 0.f)
447 << "A layer with no tilings shouldn't have valid raster scales";
448 if (!raster_contents_scale_ || ShouldAdjustRasterScale()) {
449 RecalculateRasterScales();
450 AddTilingsForRasterScale();
453 DCHECK(raster_page_scale_);
454 DCHECK(raster_device_scale_);
455 DCHECK(raster_source_scale_);
456 DCHECK(raster_contents_scale_);
457 DCHECK(low_res_raster_contents_scale_);
459 was_screen_space_transform_animating_ =
460 draw_properties().screen_space_transform_is_animating;
462 should_update_tile_priorities_ = true;
464 UpdateTilePriorities(occlusion_tracker);
466 if (layer_tree_impl()->IsPendingTree())
467 MarkVisibleResourcesAsRequired();
470 void PictureLayerImpl::UpdateTilePriorities(
471 const OcclusionTracker<LayerImpl>* occlusion_tracker) {
472 TRACE_EVENT0("cc", "PictureLayerImpl::UpdateTilePriorities");
474 double current_frame_time_in_seconds =
475 (layer_tree_impl()->CurrentFrameTimeTicks() -
476 base::TimeTicks()).InSecondsF();
478 bool tiling_needs_update = false;
479 for (size_t i = 0; i < tilings_->num_tilings(); ++i) {
480 if (tilings_->tiling_at(i)->NeedsUpdateForFrameAtTime(
481 current_frame_time_in_seconds)) {
482 tiling_needs_update = true;
486 if (!tiling_needs_update)
489 gfx::Rect visible_rect_in_content_space(
490 GetViewportForTilePriorityInContentSpace());
491 visible_rect_in_content_space.Intersect(visible_content_rect());
492 gfx::Rect visible_layer_rect = gfx::ScaleToEnclosingRect(
493 visible_rect_in_content_space, 1.f / contents_scale_x());
495 layer_tree_impl()->IsActiveTree() ? ACTIVE_TREE : PENDING_TREE;
496 for (size_t i = 0; i < tilings_->num_tilings(); ++i) {
497 tilings_->tiling_at(i)->UpdateTilePriorities(tree,
499 ideal_contents_scale_,
500 current_frame_time_in_seconds,
506 // Tile priorities were modified.
507 layer_tree_impl()->DidModifyTilePriorities();
510 gfx::Rect PictureLayerImpl::GetViewportForTilePriorityInContentSpace() const {
511 // If visible_rect_for_tile_priority_ is empty or
512 // viewport_rect_for_tile_priority_ is set to be different from the device
513 // viewport, try to inverse project the viewport into layer space and use
514 // that. Otherwise just use visible_rect_for_tile_priority_
515 gfx::Rect visible_rect_in_content_space = visible_rect_for_tile_priority_;
517 if (visible_rect_in_content_space.IsEmpty() ||
518 layer_tree_impl()->DeviceViewport() != viewport_rect_for_tile_priority_) {
519 gfx::Transform view_to_layer(gfx::Transform::kSkipInitialization);
521 if (screen_space_transform_for_tile_priority_.GetInverse(&view_to_layer)) {
522 // Transform from view space to content space.
523 visible_rect_in_content_space =
524 gfx::ToEnclosingRect(MathUtil::ProjectClippedRect(
525 view_to_layer, viewport_rect_for_tile_priority_));
527 visible_rect_in_content_space.Intersect(gfx::Rect(content_bounds()));
531 return visible_rect_in_content_space;
534 PictureLayerImpl* PictureLayerImpl::GetRecycledTwinLayer() {
535 // TODO(vmpstr): Maintain recycled twin as a member. crbug.com/407418
536 return static_cast<PictureLayerImpl*>(
537 layer_tree_impl()->FindRecycleTreeLayerById(id()));
540 void PictureLayerImpl::NotifyTileStateChanged(const Tile* tile) {
541 if (layer_tree_impl()->IsActiveTree()) {
542 gfx::RectF layer_damage_rect =
543 gfx::ScaleRect(tile->content_rect(), 1.f / tile->contents_scale());
544 AddDamageRect(layer_damage_rect);
548 void PictureLayerImpl::DidBecomeActive() {
549 LayerImpl::DidBecomeActive();
550 tilings_->DidBecomeActive();
551 layer_tree_impl()->DidModifyTilePriorities();
554 void PictureLayerImpl::DidBeginTracing() {
555 pile_->DidBeginTracing();
558 void PictureLayerImpl::ReleaseResources() {
564 // To avoid an edge case after lost context where the tree is up to date but
565 // the tilings have not been managed, request an update draw properties
566 // to force tilings to get managed.
567 layer_tree_impl()->set_needs_update_draw_properties();
570 skia::RefPtr<SkPicture> PictureLayerImpl::GetPicture() {
571 return pile_->GetFlattenedPicture();
574 scoped_refptr<Tile> PictureLayerImpl::CreateTile(PictureLayerTiling* tiling,
575 const gfx::Rect& content_rect) {
576 if (!pile_->CanRaster(tiling->contents_scale(), content_rect))
577 return scoped_refptr<Tile>();
579 // TODO(vmpstr): Revisit this. For now, enabling analysis means that we get as
580 // much savings on memory as we can. However, for some cases like ganesh or
581 // small layers, the amount of time we spend analyzing might not justify
582 // memory savings that we can get. Note that we don't handle solid color
583 // masks, so we shouldn't bother analyzing those.
584 // Bugs: crbug.com/397198, crbug.com/396908
586 if (!pile_->is_mask())
587 flags = Tile::USE_PICTURE_ANALYSIS;
589 return layer_tree_impl()->tile_manager()->CreateTile(
593 contents_opaque() ? content_rect : gfx::Rect(),
594 tiling->contents_scale(),
596 layer_tree_impl()->source_frame_number(),
600 PicturePileImpl* PictureLayerImpl::GetPile() {
604 const Region* PictureLayerImpl::GetInvalidation() {
605 return &invalidation_;
608 const PictureLayerTiling* PictureLayerImpl::GetTwinTiling(
609 const PictureLayerTiling* tiling) const {
612 for (size_t i = 0; i < twin_layer_->tilings_->num_tilings(); ++i)
613 if (twin_layer_->tilings_->tiling_at(i)->contents_scale() ==
614 tiling->contents_scale())
615 return twin_layer_->tilings_->tiling_at(i);
619 PictureLayerTiling* PictureLayerImpl::GetRecycledTwinTiling(
620 const PictureLayerTiling* tiling) {
621 PictureLayerImpl* recycled_twin = GetRecycledTwinLayer();
622 if (!recycled_twin || !recycled_twin->tilings_)
624 return recycled_twin->tilings_->TilingAtScale(tiling->contents_scale());
627 size_t PictureLayerImpl::GetMaxTilesForInterestArea() const {
628 return layer_tree_impl()->settings().max_tiles_for_interest_area;
631 float PictureLayerImpl::GetSkewportTargetTimeInSeconds() const {
632 float skewport_target_time_in_frames =
633 layer_tree_impl()->use_gpu_rasterization()
634 ? kGpuSkewportTargetTimeInFrames
635 : kCpuSkewportTargetTimeInFrames;
636 return skewport_target_time_in_frames *
637 layer_tree_impl()->begin_impl_frame_interval().InSecondsF() *
638 layer_tree_impl()->settings().skewport_target_time_multiplier;
641 int PictureLayerImpl::GetSkewportExtrapolationLimitInContentPixels() const {
642 return layer_tree_impl()
644 .skewport_extrapolation_limit_in_content_pixels;
647 gfx::Size PictureLayerImpl::CalculateTileSize(
648 const gfx::Size& content_bounds) const {
649 int max_texture_size =
650 layer_tree_impl()->resource_provider()->max_texture_size();
652 if (pile_->is_mask()) {
653 // Masks are not tiled, so if we can't cover the whole mask with one tile,
654 // don't make any tiles at all. Returning an empty size signals this.
655 if (content_bounds.width() > max_texture_size ||
656 content_bounds.height() > max_texture_size)
658 return content_bounds;
661 gfx::Size default_tile_size = layer_tree_impl()->settings().default_tile_size;
662 if (layer_tree_impl()->use_gpu_rasterization()) {
663 // TODO(ernstm) crbug.com/365877: We need a unified way to override the
664 // default-tile-size.
666 gfx::Size(layer_tree_impl()->device_viewport_size().width(),
667 layer_tree_impl()->device_viewport_size().height() / 4);
669 default_tile_size.SetToMin(gfx::Size(max_texture_size, max_texture_size));
671 gfx::Size max_untiled_content_size =
672 layer_tree_impl()->settings().max_untiled_layer_size;
673 max_untiled_content_size.SetToMin(
674 gfx::Size(max_texture_size, max_texture_size));
676 bool any_dimension_too_large =
677 content_bounds.width() > max_untiled_content_size.width() ||
678 content_bounds.height() > max_untiled_content_size.height();
680 bool any_dimension_one_tile =
681 content_bounds.width() <= default_tile_size.width() ||
682 content_bounds.height() <= default_tile_size.height();
684 // If long and skinny, tile at the max untiled content size, and clamp
685 // the smaller dimension to the content size, e.g. 1000x12 layer with
686 // 500x500 max untiled size would get 500x12 tiles. Also do this
687 // if the layer is small.
688 if (any_dimension_one_tile || !any_dimension_too_large) {
689 int width = std::min(
690 std::max(max_untiled_content_size.width(), default_tile_size.width()),
691 content_bounds.width());
692 int height = std::min(
693 std::max(max_untiled_content_size.height(), default_tile_size.height()),
694 content_bounds.height());
695 // Round up to the closest multiple of 64. This improves recycling and
696 // avoids odd texture sizes.
697 width = RoundUp(width, 64);
698 height = RoundUp(height, 64);
699 return gfx::Size(width, height);
702 return default_tile_size;
705 void PictureLayerImpl::SyncFromActiveLayer(const PictureLayerImpl* other) {
706 TRACE_EVENT0("cc", "SyncFromActiveLayer");
707 DCHECK(!other->needs_post_commit_initialization_);
708 DCHECK(other->tilings_);
710 if (!DrawsContent()) {
715 raster_page_scale_ = other->raster_page_scale_;
716 raster_device_scale_ = other->raster_device_scale_;
717 raster_source_scale_ = other->raster_source_scale_;
718 raster_contents_scale_ = other->raster_contents_scale_;
719 low_res_raster_contents_scale_ = other->low_res_raster_contents_scale_;
721 bool synced_high_res_tiling = false;
722 if (CanHaveTilings()) {
723 synced_high_res_tiling = tilings_->SyncTilings(
724 *other->tilings_, bounds(), invalidation_, MinimumContentsScale());
729 // If our MinimumContentsScale has changed to prevent the twin's high res
730 // tiling from being synced, we should reset the raster scale and let it be
731 // recalculated (1) again. This can happen if our bounds shrink to the point
732 // where min contents scale grows.
733 // (1) - TODO(vmpstr) Instead of hoping that this will be recalculated, we
734 // should refactor this code a little bit and actually recalculate this.
735 // However, this is a larger undertaking, so this will work for now.
736 if (!synced_high_res_tiling)
739 SanityCheckTilingState();
742 void PictureLayerImpl::SyncTiling(
743 const PictureLayerTiling* tiling) {
744 if (!CanHaveTilingWithScale(tiling->contents_scale()))
746 tilings_->AddTiling(tiling->contents_scale());
748 // If this tree needs update draw properties, then the tiling will
749 // get updated prior to drawing or activation. If this tree does not
750 // need update draw properties, then its transforms are up to date and
751 // we can create tiles for this tiling immediately.
752 if (!layer_tree_impl()->needs_update_draw_properties() &&
753 should_update_tile_priorities_) {
754 // TODO(danakj): Add a DCHECK() that we are not using occlusion tracking
755 // when we stop using the pending tree in the browser compositor. If we want
756 // to support occlusion tracking here, we need to dirty the draw properties
757 // or save occlusion as a draw property.
758 UpdateTilePriorities(NULL);
762 ResourceProvider::ResourceId PictureLayerImpl::ContentsResourceId() const {
763 gfx::Rect content_rect(content_bounds());
764 float scale = MaximumTilingContentsScale();
765 PictureLayerTilingSet::CoverageIterator iter(
766 tilings_.get(), scale, content_rect, ideal_contents_scale_);
768 // Mask resource not ready yet.
772 // Masks only supported if they fit on exactly one tile.
773 DCHECK(iter.geometry_rect() == content_rect)
774 << "iter rect " << iter.geometry_rect().ToString() << " content rect "
775 << content_rect.ToString();
777 const ManagedTileState::TileVersion& tile_version =
778 iter->GetTileVersionForDrawing();
779 if (!tile_version.IsReadyToDraw() ||
780 tile_version.mode() != ManagedTileState::TileVersion::RESOURCE_MODE)
783 return tile_version.get_resource_id();
786 void PictureLayerImpl::MarkVisibleResourcesAsRequired() const {
787 DCHECK(layer_tree_impl()->IsPendingTree());
788 DCHECK(ideal_contents_scale_);
789 DCHECK_GT(tilings_->num_tilings(), 0u);
791 // The goal of this function is to find the minimum set of tiles that need to
792 // be ready to draw in order to activate without flashing content from a
793 // higher res on the active tree to a lower res on the pending tree.
795 // First, early out for layers with no visible content.
796 if (visible_content_rect().IsEmpty())
799 gfx::Rect rect(visible_content_rect());
801 // Only mark tiles inside the viewport for tile priority as required for
802 // activation. This viewport is normally the same as the draw viewport but
803 // can be independently overridden by embedders like Android WebView with
804 // SetExternalDrawConstraints.
805 rect.Intersect(GetViewportForTilePriorityInContentSpace());
807 float min_acceptable_scale =
808 std::min(raster_contents_scale_, ideal_contents_scale_);
810 if (PictureLayerImpl* twin = twin_layer_) {
811 float twin_min_acceptable_scale =
812 std::min(twin->ideal_contents_scale_, twin->raster_contents_scale_);
813 // Ignore 0 scale in case CalculateContentsScale() has never been
814 // called for active twin.
815 if (twin_min_acceptable_scale != 0.0f) {
816 min_acceptable_scale =
817 std::min(min_acceptable_scale, twin_min_acceptable_scale);
821 PictureLayerTiling* high_res = NULL;
822 PictureLayerTiling* low_res = NULL;
824 // First pass: ready to draw tiles in acceptable but non-ideal tilings are
825 // marked as required for activation so that their textures are not thrown
826 // away; any non-ready tiles are not marked as required.
827 Region missing_region = rect;
828 for (size_t i = 0; i < tilings_->num_tilings(); ++i) {
829 PictureLayerTiling* tiling = tilings_->tiling_at(i);
830 DCHECK(tiling->has_ever_been_updated());
832 if (tiling->resolution() == LOW_RESOLUTION) {
833 DCHECK(!low_res) << "There can only be one low res tiling";
836 if (tiling->contents_scale() < min_acceptable_scale)
838 if (tiling->resolution() == HIGH_RESOLUTION) {
839 DCHECK(!high_res) << "There can only be one high res tiling";
843 for (PictureLayerTiling::CoverageIterator iter(tiling,
848 if (!*iter || !iter->IsReadyToDraw())
851 missing_region.Subtract(iter.geometry_rect());
852 iter->MarkRequiredForActivation();
855 DCHECK(high_res) << "There must be one high res tiling";
857 // If these pointers are null (because no twin, no matching tiling, or the
858 // simpification just below), then high res tiles will be required to fill any
859 // holes left by the first pass above. If the pointers are valid, then this
860 // layer is allowed to skip any tiles that are not ready on its twin.
861 const PictureLayerTiling* twin_high_res = NULL;
862 const PictureLayerTiling* twin_low_res = NULL;
865 // As a simplification, only allow activating to skip twin tiles that the
866 // active layer is also missing when both this layer and its twin have
867 // "simple" sets of tilings: only 2 tilings (high and low) or only 1 high
868 // res tiling. This avoids having to iterate/track coverage of non-ideal
869 // tilings during the last draw call on the active layer.
870 if (tilings_->num_tilings() <= 2 &&
871 twin_layer_->tilings_->num_tilings() <= tilings_->num_tilings()) {
872 twin_low_res = low_res ? GetTwinTiling(low_res) : NULL;
873 twin_high_res = high_res ? GetTwinTiling(high_res) : NULL;
876 // If this layer and its twin have different transforms, then don't compare
877 // them and only allow activating to high res tiles, since tiles on each
878 // layer will be in different places on screen.
879 if (twin_layer_->layer_tree_impl()->RequiresHighResToDraw() ||
880 bounds() != twin_layer_->bounds() ||
881 draw_properties().screen_space_transform !=
882 twin_layer_->draw_properties().screen_space_transform) {
883 twin_high_res = NULL;
888 // As a second pass, mark as required any visible high res tiles not filled in
889 // by acceptable non-ideal tiles from the first pass.
890 if (MarkVisibleTilesAsRequired(
891 high_res, twin_high_res, contents_scale_x(), rect, missing_region)) {
892 // As an optional third pass, if a high res tile was skipped because its
893 // twin was also missing, then fall back to mark low res tiles as required
894 // in case the active twin is substituting those for missing high res
895 // content. Only suitable, when low res is enabled.
897 MarkVisibleTilesAsRequired(
898 low_res, twin_low_res, contents_scale_x(), rect, missing_region);
903 bool PictureLayerImpl::MarkVisibleTilesAsRequired(
904 PictureLayerTiling* tiling,
905 const PictureLayerTiling* optional_twin_tiling,
906 float contents_scale,
907 const gfx::Rect& rect,
908 const Region& missing_region) const {
909 bool twin_had_missing_tile = false;
910 for (PictureLayerTiling::CoverageIterator iter(tiling,
916 // A null tile (i.e. missing recording) can just be skipped.
920 // If the tile is occluded, don't mark it as required for activation.
921 if (tile->is_occluded(PENDING_TREE))
924 // If the missing region doesn't cover it, this tile is fully
925 // covered by acceptable tiles at other scales.
926 if (!missing_region.Intersects(iter.geometry_rect()))
929 // If the twin tile doesn't exist (i.e. missing recording or so far away
930 // that it is outside the visible tile rect) or this tile is shared between
931 // with the twin, then this tile isn't required to prevent flashing.
932 if (optional_twin_tiling) {
933 Tile* twin_tile = optional_twin_tiling->TileAt(iter.i(), iter.j());
934 if (!twin_tile || twin_tile == tile) {
935 twin_had_missing_tile = true;
940 tile->MarkRequiredForActivation();
942 return twin_had_missing_tile;
945 void PictureLayerImpl::DoPostCommitInitialization() {
946 DCHECK(needs_post_commit_initialization_);
947 DCHECK(layer_tree_impl()->IsPendingTree());
950 tilings_.reset(new PictureLayerTilingSet(this, bounds()));
952 DCHECK(!twin_layer_);
953 twin_layer_ = static_cast<PictureLayerImpl*>(
954 layer_tree_impl()->FindActiveTreeLayerById(id()));
956 DCHECK(!twin_layer_->twin_layer_);
957 twin_layer_->twin_layer_ = this;
958 // If the twin has never been pushed to, do not sync from it.
959 // This can happen if this function is called during activation.
960 if (!twin_layer_->needs_post_commit_initialization_)
961 SyncFromActiveLayer(twin_layer_);
964 needs_post_commit_initialization_ = false;
967 PictureLayerTiling* PictureLayerImpl::AddTiling(float contents_scale) {
968 DCHECK(CanHaveTilingWithScale(contents_scale)) <<
969 "contents_scale: " << contents_scale;
971 PictureLayerTiling* tiling = tilings_->AddTiling(contents_scale);
973 DCHECK(pile_->HasRecordings());
976 twin_layer_->SyncTiling(tiling);
981 void PictureLayerImpl::RemoveTiling(float contents_scale) {
982 if (!tilings_ || tilings_->num_tilings() == 0)
985 for (size_t i = 0; i < tilings_->num_tilings(); ++i) {
986 PictureLayerTiling* tiling = tilings_->tiling_at(i);
987 if (tiling->contents_scale() == contents_scale) {
988 tilings_->Remove(tiling);
992 if (tilings_->num_tilings() == 0)
994 SanityCheckTilingState();
997 void PictureLayerImpl::RemoveAllTilings() {
999 tilings_->RemoveAllTilings();
1000 // If there are no tilings, then raster scales are no longer meaningful.
1006 inline float PositiveRatio(float float1, float float2) {
1007 DCHECK_GT(float1, 0);
1008 DCHECK_GT(float2, 0);
1009 return float1 > float2 ? float1 / float2 : float2 / float1;
1014 void PictureLayerImpl::AddTilingsForRasterScale() {
1015 PictureLayerTiling* high_res = NULL;
1016 PictureLayerTiling* low_res = NULL;
1018 PictureLayerTiling* previous_low_res = NULL;
1019 for (size_t i = 0; i < tilings_->num_tilings(); ++i) {
1020 PictureLayerTiling* tiling = tilings_->tiling_at(i);
1021 if (tiling->contents_scale() == raster_contents_scale_)
1023 if (tiling->contents_scale() == low_res_raster_contents_scale_)
1025 if (tiling->resolution() == LOW_RESOLUTION)
1026 previous_low_res = tiling;
1028 // Reset all tilings to non-ideal until the end of this function.
1029 tiling->set_resolution(NON_IDEAL_RESOLUTION);
1033 high_res = AddTiling(raster_contents_scale_);
1034 if (raster_contents_scale_ == low_res_raster_contents_scale_)
1038 // Only create new low res tilings when the transform is static. This
1039 // prevents wastefully creating a paired low res tiling for every new high res
1040 // tiling during a pinch or a CSS animation.
1041 bool is_pinching = layer_tree_impl()->PinchGestureActive();
1042 if (layer_tree_impl()->create_low_res_tiling() && !is_pinching &&
1043 !draw_properties().screen_space_transform_is_animating && !low_res &&
1044 low_res != high_res)
1045 low_res = AddTiling(low_res_raster_contents_scale_);
1047 // Set low-res if we have one.
1049 low_res = previous_low_res;
1050 if (low_res && low_res != high_res)
1051 low_res->set_resolution(LOW_RESOLUTION);
1053 // Make sure we always have one high-res (even if high == low).
1054 high_res->set_resolution(HIGH_RESOLUTION);
1056 SanityCheckTilingState();
1059 bool PictureLayerImpl::ShouldAdjustRasterScale() const {
1060 if (was_screen_space_transform_animating_ !=
1061 draw_properties().screen_space_transform_is_animating)
1064 bool is_pinching = layer_tree_impl()->PinchGestureActive();
1065 if (is_pinching && raster_page_scale_) {
1066 // We change our raster scale when it is:
1067 // - Higher than ideal (need a lower-res tiling available)
1068 // - Too far from ideal (need a higher-res tiling available)
1069 float ratio = ideal_page_scale_ / raster_page_scale_;
1070 if (raster_page_scale_ > ideal_page_scale_ ||
1071 ratio > kMaxScaleRatioDuringPinch)
1076 // When not pinching, match the ideal page scale factor.
1077 if (raster_page_scale_ != ideal_page_scale_)
1081 // Always match the ideal device scale factor.
1082 if (raster_device_scale_ != ideal_device_scale_)
1085 // When the source scale changes we want to match it, but not when animating
1086 // or when we've fixed the scale in place.
1087 if (!draw_properties().screen_space_transform_is_animating &&
1088 !raster_source_scale_is_fixed_ &&
1089 raster_source_scale_ != ideal_source_scale_)
1095 float PictureLayerImpl::SnappedContentsScale(float scale) {
1096 // If a tiling exists within the max snapping ratio, snap to its scale.
1097 float snapped_contents_scale = scale;
1098 float snapped_ratio = kSnapToExistingTilingRatio;
1099 for (size_t i = 0; i < tilings_->num_tilings(); ++i) {
1100 float tiling_contents_scale = tilings_->tiling_at(i)->contents_scale();
1101 float ratio = PositiveRatio(tiling_contents_scale, scale);
1102 if (ratio < snapped_ratio) {
1103 snapped_contents_scale = tiling_contents_scale;
1104 snapped_ratio = ratio;
1107 return snapped_contents_scale;
1110 void PictureLayerImpl::RecalculateRasterScales() {
1111 float old_raster_contents_scale = raster_contents_scale_;
1112 float old_raster_page_scale = raster_page_scale_;
1113 float old_raster_source_scale = raster_source_scale_;
1115 raster_device_scale_ = ideal_device_scale_;
1116 raster_page_scale_ = ideal_page_scale_;
1117 raster_source_scale_ = ideal_source_scale_;
1118 raster_contents_scale_ = ideal_contents_scale_;
1120 // If we're not animating, or leaving an animation, and the
1121 // ideal_source_scale_ changes, then things are unpredictable, and we fix
1122 // the raster_source_scale_ in place.
1123 if (old_raster_source_scale &&
1124 !draw_properties().screen_space_transform_is_animating &&
1125 !was_screen_space_transform_animating_ &&
1126 old_raster_source_scale != ideal_source_scale_)
1127 raster_source_scale_is_fixed_ = true;
1129 // TODO(danakj): Adjust raster source scale closer to ideal source scale at
1130 // a throttled rate. Possibly make use of invalidation_.IsEmpty() on pending
1131 // tree. This will allow CSS scale changes to get re-rastered at an
1132 // appropriate rate.
1133 if (raster_source_scale_is_fixed_) {
1134 raster_contents_scale_ /= raster_source_scale_;
1135 raster_source_scale_ = 1.f;
1138 // During pinch we completely ignore the current ideal scale, and just use
1139 // a multiple of the previous scale.
1140 // TODO(danakj): This seems crazy, we should use the current ideal, no?
1141 bool is_pinching = layer_tree_impl()->PinchGestureActive();
1142 if (is_pinching && old_raster_contents_scale) {
1143 // See ShouldAdjustRasterScale:
1144 // - When zooming out, preemptively create new tiling at lower resolution.
1145 // - When zooming in, approximate ideal using multiple of kMaxScaleRatio.
1146 bool zooming_out = old_raster_page_scale > ideal_page_scale_;
1147 float desired_contents_scale =
1148 zooming_out ? old_raster_contents_scale / kMaxScaleRatioDuringPinch
1149 : old_raster_contents_scale * kMaxScaleRatioDuringPinch;
1150 raster_contents_scale_ = SnappedContentsScale(desired_contents_scale);
1151 raster_page_scale_ =
1152 raster_contents_scale_ / raster_device_scale_ / raster_source_scale_;
1155 raster_contents_scale_ =
1156 std::max(raster_contents_scale_, MinimumContentsScale());
1158 // Since we're not re-rasterizing during animation, rasterize at the maximum
1159 // scale that will occur during the animation, if the maximum scale is
1160 // known. However, to avoid excessive memory use, don't rasterize at a scale
1161 // at which this layer would become larger than the viewport.
1162 if (draw_properties().screen_space_transform_is_animating) {
1163 bool can_raster_at_maximum_scale = false;
1164 if (draw_properties().maximum_animation_contents_scale > 0.f) {
1165 gfx::Size bounds_at_maximum_scale = gfx::ToCeiledSize(gfx::ScaleSize(
1166 bounds(), draw_properties().maximum_animation_contents_scale));
1167 if (bounds_at_maximum_scale.GetArea() <=
1168 layer_tree_impl()->device_viewport_size().GetArea())
1169 can_raster_at_maximum_scale = true;
1171 if (can_raster_at_maximum_scale) {
1172 raster_contents_scale_ =
1173 std::max(raster_contents_scale_,
1174 draw_properties().maximum_animation_contents_scale);
1176 raster_contents_scale_ =
1177 std::max(raster_contents_scale_,
1178 1.f * ideal_page_scale_ * ideal_device_scale_);
1182 // If this layer would create zero or one tiles at this content scale,
1183 // don't create a low res tiling.
1184 gfx::Size content_bounds =
1185 gfx::ToCeiledSize(gfx::ScaleSize(bounds(), raster_contents_scale_));
1186 gfx::Size tile_size = CalculateTileSize(content_bounds);
1187 bool tile_covers_bounds = tile_size.width() >= content_bounds.width() &&
1188 tile_size.height() >= content_bounds.height();
1189 if (tile_size.IsEmpty() || tile_covers_bounds) {
1190 low_res_raster_contents_scale_ = raster_contents_scale_;
1194 float low_res_factor =
1195 layer_tree_impl()->settings().low_res_contents_scale_factor;
1196 low_res_raster_contents_scale_ = std::max(
1197 raster_contents_scale_ * low_res_factor,
1198 MinimumContentsScale());
1201 void PictureLayerImpl::CleanUpTilingsOnActiveLayer(
1202 std::vector<PictureLayerTiling*> used_tilings) {
1203 DCHECK(layer_tree_impl()->IsActiveTree());
1204 if (tilings_->num_tilings() == 0)
1207 float min_acceptable_high_res_scale = std::min(
1208 raster_contents_scale_, ideal_contents_scale_);
1209 float max_acceptable_high_res_scale = std::max(
1210 raster_contents_scale_, ideal_contents_scale_);
1211 float twin_low_res_scale = 0.f;
1213 PictureLayerImpl* twin = twin_layer_;
1214 if (twin && twin->CanHaveTilings()) {
1215 min_acceptable_high_res_scale = std::min(
1216 min_acceptable_high_res_scale,
1217 std::min(twin->raster_contents_scale_, twin->ideal_contents_scale_));
1218 max_acceptable_high_res_scale = std::max(
1219 max_acceptable_high_res_scale,
1220 std::max(twin->raster_contents_scale_, twin->ideal_contents_scale_));
1222 for (size_t i = 0; i < twin->tilings_->num_tilings(); ++i) {
1223 PictureLayerTiling* tiling = twin->tilings_->tiling_at(i);
1224 if (tiling->resolution() == LOW_RESOLUTION)
1225 twin_low_res_scale = tiling->contents_scale();
1229 std::vector<PictureLayerTiling*> to_remove;
1230 for (size_t i = 0; i < tilings_->num_tilings(); ++i) {
1231 PictureLayerTiling* tiling = tilings_->tiling_at(i);
1233 // Keep multiple high resolution tilings even if not used to help
1234 // activate earlier at non-ideal resolutions.
1235 if (tiling->contents_scale() >= min_acceptable_high_res_scale &&
1236 tiling->contents_scale() <= max_acceptable_high_res_scale)
1239 // Keep low resolution tilings, if the layer should have them.
1240 if (layer_tree_impl()->create_low_res_tiling()) {
1241 if (tiling->resolution() == LOW_RESOLUTION ||
1242 tiling->contents_scale() == twin_low_res_scale)
1246 // Don't remove tilings that are being used (and thus would cause a flash.)
1247 if (std::find(used_tilings.begin(), used_tilings.end(), tiling) !=
1251 to_remove.push_back(tiling);
1254 if (to_remove.empty())
1257 PictureLayerImpl* recycled_twin = GetRecycledTwinLayer();
1258 // Remove tilings on this tree and the twin tree.
1259 for (size_t i = 0; i < to_remove.size(); ++i) {
1260 const PictureLayerTiling* twin_tiling = GetTwinTiling(to_remove[i]);
1261 // Only remove tilings from the twin layer if they have
1262 // NON_IDEAL_RESOLUTION.
1263 if (twin_tiling && twin_tiling->resolution() == NON_IDEAL_RESOLUTION)
1264 twin->RemoveTiling(to_remove[i]->contents_scale());
1265 // Remove the tiling from the recycle tree. Note that we ignore resolution,
1266 // since we don't need to maintain high/low res on the recycle tree.
1268 recycled_twin->RemoveTiling(to_remove[i]->contents_scale());
1269 // TODO(enne): temporary sanity CHECK for http://crbug.com/358350
1270 CHECK_NE(HIGH_RESOLUTION, to_remove[i]->resolution());
1271 tilings_->Remove(to_remove[i]);
1274 DCHECK_GT(tilings_->num_tilings(), 0u);
1275 SanityCheckTilingState();
1278 float PictureLayerImpl::MinimumContentsScale() const {
1279 float setting_min = layer_tree_impl()->settings().minimum_contents_scale;
1281 // If the contents scale is less than 1 / width (also for height),
1282 // then it will end up having less than one pixel of content in that
1283 // dimension. Bump the minimum contents scale up in this case to prevent
1284 // this from happening.
1285 int min_dimension = std::min(bounds().width(), bounds().height());
1289 return std::max(1.f / min_dimension, setting_min);
1292 void PictureLayerImpl::ResetRasterScale() {
1293 raster_page_scale_ = 0.f;
1294 raster_device_scale_ = 0.f;
1295 raster_source_scale_ = 0.f;
1296 raster_contents_scale_ = 0.f;
1297 low_res_raster_contents_scale_ = 0.f;
1298 raster_source_scale_is_fixed_ = false;
1300 // When raster scales aren't valid, don't update tile priorities until
1301 // this layer has been updated via UpdateDrawProperties.
1302 should_update_tile_priorities_ = false;
1305 bool PictureLayerImpl::CanHaveTilings() const {
1306 if (!DrawsContent())
1308 if (!pile_->HasRecordings())
1313 bool PictureLayerImpl::CanHaveTilingWithScale(float contents_scale) const {
1314 if (!CanHaveTilings())
1316 if (contents_scale < MinimumContentsScale())
1321 void PictureLayerImpl::SanityCheckTilingState() const {
1323 // Recycle tree doesn't have any restrictions.
1324 if (layer_tree_impl()->IsRecycleTree())
1327 if (!CanHaveTilings()) {
1328 DCHECK_EQ(0u, tilings_->num_tilings());
1331 if (tilings_->num_tilings() == 0)
1334 // MarkVisibleResourcesAsRequired depends on having exactly 1 high res
1335 // tiling to mark its tiles as being required for activation.
1336 DCHECK_EQ(1, tilings_->NumHighResTilings());
1340 float PictureLayerImpl::MaximumTilingContentsScale() const {
1341 float max_contents_scale = MinimumContentsScale();
1342 for (size_t i = 0; i < tilings_->num_tilings(); ++i) {
1343 const PictureLayerTiling* tiling = tilings_->tiling_at(i);
1344 max_contents_scale = std::max(max_contents_scale, tiling->contents_scale());
1346 return max_contents_scale;
1349 void PictureLayerImpl::UpdateIdealScales() {
1350 DCHECK(CanHaveTilings());
1352 float min_contents_scale = MinimumContentsScale();
1353 DCHECK_GT(min_contents_scale, 0.f);
1354 float min_page_scale = layer_tree_impl()->min_page_scale_factor();
1355 DCHECK_GT(min_page_scale, 0.f);
1356 float min_device_scale = 1.f;
1357 float min_source_scale =
1358 min_contents_scale / min_page_scale / min_device_scale;
1360 float ideal_page_scale = draw_properties().page_scale_factor;
1361 float ideal_device_scale = draw_properties().device_scale_factor;
1362 float ideal_source_scale = draw_properties().ideal_contents_scale /
1363 ideal_page_scale / ideal_device_scale;
1364 ideal_contents_scale_ =
1365 std::max(draw_properties().ideal_contents_scale, min_contents_scale);
1366 ideal_page_scale_ = draw_properties().page_scale_factor;
1367 ideal_device_scale_ = draw_properties().device_scale_factor;
1368 ideal_source_scale_ = std::max(ideal_source_scale, min_source_scale);
1371 void PictureLayerImpl::GetDebugBorderProperties(
1373 float* width) const {
1374 *color = DebugColors::TiledContentLayerBorderColor();
1375 *width = DebugColors::TiledContentLayerBorderWidth(layer_tree_impl());
1378 void PictureLayerImpl::AsValueInto(base::debug::TracedValue* state) const {
1379 const_cast<PictureLayerImpl*>(this)->DoPostCommitInitializationIfNeeded();
1380 LayerImpl::AsValueInto(state);
1381 state->SetDouble("ideal_contents_scale", ideal_contents_scale_);
1382 state->SetDouble("geometry_contents_scale", MaximumTilingContentsScale());
1383 state->BeginArray("tilings");
1384 tilings_->AsValueInto(state);
1387 state->BeginArray("pictures");
1388 pile_->AsValueInto(state);
1391 state->BeginArray("invalidation");
1392 invalidation_.AsValueInto(state);
1395 state->BeginArray("coverage_tiles");
1396 for (PictureLayerTilingSet::CoverageIterator iter(tilings_.get(),
1398 gfx::Rect(content_bounds()),
1399 ideal_contents_scale_);
1402 state->BeginDictionary();
1404 state->BeginArray("geometry_rect");
1405 MathUtil::AddToTracedValue(iter.geometry_rect(), state);
1409 TracedValue::SetIDRef(*iter, state, "tile");
1411 state->EndDictionary();
1416 size_t PictureLayerImpl::GPUMemoryUsageInBytes() const {
1417 const_cast<PictureLayerImpl*>(this)->DoPostCommitInitializationIfNeeded();
1418 return tilings_->GPUMemoryUsageInBytes();
1421 void PictureLayerImpl::RunMicroBenchmark(MicroBenchmarkImpl* benchmark) {
1422 benchmark->RunOnLayer(this);
1425 WhichTree PictureLayerImpl::GetTree() const {
1426 return layer_tree_impl()->IsActiveTree() ? ACTIVE_TREE : PENDING_TREE;
1429 bool PictureLayerImpl::IsOnActiveOrPendingTree() const {
1430 return !layer_tree_impl()->IsRecycleTree();
1433 bool PictureLayerImpl::HasValidTilePriorities() const {
1434 return IsOnActiveOrPendingTree() && IsDrawnRenderSurfaceLayerListMember();
1437 bool PictureLayerImpl::AllTilesRequiredForActivationAreReadyToDraw() const {
1438 if (!layer_tree_impl()->IsPendingTree())
1441 if (!HasValidTilePriorities())
1447 if (visible_content_rect().IsEmpty())
1450 for (size_t i = 0; i < tilings_->num_tilings(); ++i) {
1451 PictureLayerTiling* tiling = tilings_->tiling_at(i);
1452 if (tiling->resolution() != HIGH_RESOLUTION &&
1453 tiling->resolution() != LOW_RESOLUTION)
1456 gfx::Rect rect(visible_content_rect());
1457 for (PictureLayerTiling::CoverageIterator iter(
1458 tiling, contents_scale_x(), rect);
1461 const Tile* tile = *iter;
1462 // A null tile (i.e. missing recording) can just be skipped.
1466 if (tile->required_for_activation() && !tile->IsReadyToDraw())
1474 PictureLayerImpl::LayerRasterTileIterator::LayerRasterTileIterator()
1475 : layer_(NULL), current_stage_(arraysize(stages_)) {
1478 PictureLayerImpl::LayerRasterTileIterator::LayerRasterTileIterator(
1479 PictureLayerImpl* layer,
1480 bool prioritize_low_res)
1481 : layer_(layer), current_stage_(0) {
1484 // Early out if the layer has no tilings.
1485 if (!layer_->tilings_ || !layer_->tilings_->num_tilings()) {
1486 current_stage_ = arraysize(stages_);
1490 // Tiles without valid priority are treated as having lowest priority and
1491 // never considered for raster.
1492 if (!layer_->HasValidTilePriorities()) {
1493 current_stage_ = arraysize(stages_);
1497 WhichTree tree = layer_->GetTree();
1499 // Find high and low res tilings and initialize the iterators.
1500 for (size_t i = 0; i < layer_->tilings_->num_tilings(); ++i) {
1501 PictureLayerTiling* tiling = layer_->tilings_->tiling_at(i);
1502 if (tiling->resolution() == HIGH_RESOLUTION) {
1503 iterators_[HIGH_RES] =
1504 PictureLayerTiling::TilingRasterTileIterator(tiling, tree);
1507 if (tiling->resolution() == LOW_RESOLUTION) {
1508 iterators_[LOW_RES] =
1509 PictureLayerTiling::TilingRasterTileIterator(tiling, tree);
1513 if (prioritize_low_res) {
1514 stages_[0].iterator_type = LOW_RES;
1515 stages_[0].tile_type = TilePriority::NOW;
1517 stages_[1].iterator_type = HIGH_RES;
1518 stages_[1].tile_type = TilePriority::NOW;
1520 stages_[0].iterator_type = HIGH_RES;
1521 stages_[0].tile_type = TilePriority::NOW;
1523 stages_[1].iterator_type = LOW_RES;
1524 stages_[1].tile_type = TilePriority::NOW;
1527 stages_[2].iterator_type = HIGH_RES;
1528 stages_[2].tile_type = TilePriority::SOON;
1530 stages_[3].iterator_type = HIGH_RES;
1531 stages_[3].tile_type = TilePriority::EVENTUALLY;
1533 IteratorType index = stages_[current_stage_].iterator_type;
1534 TilePriority::PriorityBin tile_type = stages_[current_stage_].tile_type;
1535 if (!iterators_[index] || iterators_[index].get_type() != tile_type)
1539 PictureLayerImpl::LayerRasterTileIterator::~LayerRasterTileIterator() {}
1541 PictureLayerImpl::LayerRasterTileIterator::operator bool() const {
1542 return current_stage_ < arraysize(stages_);
1545 PictureLayerImpl::LayerRasterTileIterator&
1546 PictureLayerImpl::LayerRasterTileIterator::
1548 IteratorType index = stages_[current_stage_].iterator_type;
1549 TilePriority::PriorityBin tile_type = stages_[current_stage_].tile_type;
1551 // First advance the iterator.
1552 if (iterators_[index])
1553 ++iterators_[index];
1555 if (iterators_[index] && iterators_[index].get_type() == tile_type)
1558 // Next, advance the stage.
1560 while (current_stage_ < arraysize(stages_)) {
1561 index = stages_[current_stage_].iterator_type;
1562 tile_type = stages_[current_stage_].tile_type;
1564 if (iterators_[index] && iterators_[index].get_type() == tile_type)
1571 Tile* PictureLayerImpl::LayerRasterTileIterator::operator*() {
1574 IteratorType index = stages_[current_stage_].iterator_type;
1575 DCHECK(iterators_[index]);
1576 DCHECK(iterators_[index].get_type() == stages_[current_stage_].tile_type);
1578 return *iterators_[index];
1581 const Tile* PictureLayerImpl::LayerRasterTileIterator::operator*() const {
1584 IteratorType index = stages_[current_stage_].iterator_type;
1585 DCHECK(iterators_[index]);
1586 DCHECK(iterators_[index].get_type() == stages_[current_stage_].tile_type);
1588 return *iterators_[index];
1591 PictureLayerImpl::LayerEvictionTileIterator::LayerEvictionTileIterator()
1593 tree_priority_(SAME_PRIORITY_FOR_BOTH_TREES),
1594 current_category_(PictureLayerTiling::EVENTUALLY),
1595 current_tiling_range_type_(PictureLayerTilingSet::HIGHER_THAN_HIGH_RES),
1596 current_tiling_(0u) {
1599 PictureLayerImpl::LayerEvictionTileIterator::LayerEvictionTileIterator(
1600 PictureLayerImpl* layer,
1601 TreePriority tree_priority)
1603 tree_priority_(tree_priority),
1604 current_category_(PictureLayerTiling::EVENTUALLY),
1605 current_tiling_range_type_(PictureLayerTilingSet::HIGHER_THAN_HIGH_RES),
1606 current_tiling_(CurrentTilingRange().start - 1u) {
1607 // TODO(vmpstr): Once tile priorities are determined by the iterators, ensure
1608 // that layers that don't have valid tile priorities have lowest priorities so
1609 // they evict their tiles first (crbug.com/381704)
1610 DCHECK(layer_->tilings_);
1612 if (!AdvanceToNextTiling())
1615 current_iterator_ = PictureLayerTiling::TilingEvictionTileIterator(
1616 layer_->tilings_->tiling_at(CurrentTilingIndex()),
1619 } while (!current_iterator_);
1622 PictureLayerImpl::LayerEvictionTileIterator::~LayerEvictionTileIterator() {
1625 Tile* PictureLayerImpl::LayerEvictionTileIterator::operator*() {
1627 return *current_iterator_;
1630 const Tile* PictureLayerImpl::LayerEvictionTileIterator::operator*() const {
1632 return *current_iterator_;
1635 PictureLayerImpl::LayerEvictionTileIterator&
1636 PictureLayerImpl::LayerEvictionTileIterator::
1639 ++current_iterator_;
1640 while (!current_iterator_) {
1641 if (!AdvanceToNextTiling())
1644 current_iterator_ = PictureLayerTiling::TilingEvictionTileIterator(
1645 layer_->tilings_->tiling_at(CurrentTilingIndex()),
1652 PictureLayerImpl::LayerEvictionTileIterator::operator bool() const {
1653 return !!current_iterator_;
1656 bool PictureLayerImpl::LayerEvictionTileIterator::AdvanceToNextCategory() {
1657 switch (current_category_) {
1658 case PictureLayerTiling::EVENTUALLY:
1660 PictureLayerTiling::EVENTUALLY_AND_REQUIRED_FOR_ACTIVATION;
1662 case PictureLayerTiling::EVENTUALLY_AND_REQUIRED_FOR_ACTIVATION:
1663 current_category_ = PictureLayerTiling::SOON;
1665 case PictureLayerTiling::SOON:
1666 current_category_ = PictureLayerTiling::SOON_AND_REQUIRED_FOR_ACTIVATION;
1668 case PictureLayerTiling::SOON_AND_REQUIRED_FOR_ACTIVATION:
1669 current_category_ = PictureLayerTiling::NOW;
1671 case PictureLayerTiling::NOW:
1672 current_category_ = PictureLayerTiling::NOW_AND_REQUIRED_FOR_ACTIVATION;
1674 case PictureLayerTiling::NOW_AND_REQUIRED_FOR_ACTIVATION:
1682 PictureLayerImpl::LayerEvictionTileIterator::AdvanceToNextTilingRangeType() {
1683 switch (current_tiling_range_type_) {
1684 case PictureLayerTilingSet::HIGHER_THAN_HIGH_RES:
1685 current_tiling_range_type_ = PictureLayerTilingSet::LOWER_THAN_LOW_RES;
1687 case PictureLayerTilingSet::LOWER_THAN_LOW_RES:
1688 current_tiling_range_type_ =
1689 PictureLayerTilingSet::BETWEEN_HIGH_AND_LOW_RES;
1691 case PictureLayerTilingSet::BETWEEN_HIGH_AND_LOW_RES:
1692 current_tiling_range_type_ = PictureLayerTilingSet::LOW_RES;
1694 case PictureLayerTilingSet::LOW_RES:
1695 current_tiling_range_type_ = PictureLayerTilingSet::HIGH_RES;
1697 case PictureLayerTilingSet::HIGH_RES:
1698 if (!AdvanceToNextCategory())
1701 current_tiling_range_type_ = PictureLayerTilingSet::HIGHER_THAN_HIGH_RES;
1708 bool PictureLayerImpl::LayerEvictionTileIterator::AdvanceToNextTiling() {
1709 DCHECK_NE(current_tiling_, CurrentTilingRange().end);
1711 while (current_tiling_ == CurrentTilingRange().end) {
1712 if (!AdvanceToNextTilingRangeType())
1715 current_tiling_ = CurrentTilingRange().start;
1720 PictureLayerTilingSet::TilingRange
1721 PictureLayerImpl::LayerEvictionTileIterator::CurrentTilingRange() const {
1722 return layer_->tilings_->GetTilingRange(current_tiling_range_type_);
1725 size_t PictureLayerImpl::LayerEvictionTileIterator::CurrentTilingIndex() const {
1726 DCHECK_NE(current_tiling_, CurrentTilingRange().end);
1727 switch (current_tiling_range_type_) {
1728 case PictureLayerTilingSet::HIGHER_THAN_HIGH_RES:
1729 case PictureLayerTilingSet::LOW_RES:
1730 case PictureLayerTilingSet::HIGH_RES:
1731 return current_tiling_;
1732 // Tilings in the following ranges are accessed in reverse order.
1733 case PictureLayerTilingSet::BETWEEN_HIGH_AND_LOW_RES:
1734 case PictureLayerTilingSet::LOWER_THAN_LOW_RES: {
1735 PictureLayerTilingSet::TilingRange tiling_range = CurrentTilingRange();
1736 size_t current_tiling_range_offset = current_tiling_ - tiling_range.start;
1737 return tiling_range.end - 1 - current_tiling_range_offset;