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/time/time.h"
11 #include "cc/base/math_util.h"
12 #include "cc/base/util.h"
13 #include "cc/debug/debug_colors.h"
14 #include "cc/debug/micro_benchmark_impl.h"
15 #include "cc/debug/traced_value.h"
16 #include "cc/layers/append_quads_data.h"
17 #include "cc/layers/quad_sink.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 "ui/gfx/quad_f.h"
25 #include "ui/gfx/rect_conversions.h"
26 #include "ui/gfx/size_conversions.h"
29 const float kMaxScaleRatioDuringPinch = 2.0f;
31 // When creating a new tiling during pinch, snap to an existing
32 // tiling's scale if the desired scale is within this ratio.
33 const float kSnapToExistingTilingRatio = 0.2f;
35 // Estimate skewport 60 frames ahead for pre-rasterization on the CPU.
36 const float kCpuSkewportTargetTimeInFrames = 60.0f;
38 // Don't pre-rasterize on the GPU (except for kBackflingGuardDistancePixels in
39 // TileManager::BinFromTilePriority).
40 const float kGpuSkewportTargetTimeInFrames = 0.0f;
45 PictureLayerImpl::PictureLayerImpl(LayerTreeImpl* tree_impl, int id)
46 : LayerImpl(tree_impl, id),
48 pile_(PicturePileImpl::Create()),
50 ideal_page_scale_(0.f),
51 ideal_device_scale_(0.f),
52 ideal_source_scale_(0.f),
53 ideal_contents_scale_(0.f),
54 raster_page_scale_(0.f),
55 raster_device_scale_(0.f),
56 raster_source_scale_(0.f),
57 raster_contents_scale_(0.f),
58 low_res_raster_contents_scale_(0.f),
59 raster_source_scale_is_fixed_(false),
60 was_animating_transform_to_screen_(false),
61 is_using_lcd_text_(tree_impl->settings().can_use_lcd_text),
62 needs_post_commit_initialization_(true),
63 should_update_tile_priorities_(false),
64 should_use_low_res_tiling_(tree_impl->settings().create_low_res_tiling),
65 layer_needs_to_register_itself_(true) {
68 PictureLayerImpl::~PictureLayerImpl() {
69 if (!layer_needs_to_register_itself_)
70 layer_tree_impl()->tile_manager()->UnregisterPictureLayerImpl(this);
73 const char* PictureLayerImpl::LayerTypeAsString() const {
74 return "cc::PictureLayerImpl";
77 scoped_ptr<LayerImpl> PictureLayerImpl::CreateLayerImpl(
78 LayerTreeImpl* tree_impl) {
79 return PictureLayerImpl::Create(tree_impl, id()).PassAs<LayerImpl>();
82 void PictureLayerImpl::PushPropertiesTo(LayerImpl* base_layer) {
83 // It's possible this layer was never drawn or updated (e.g. because it was
84 // a descendant of an opacity 0 layer).
85 DoPostCommitInitializationIfNeeded();
86 PictureLayerImpl* layer_impl = static_cast<PictureLayerImpl*>(base_layer);
88 // We have already synced the important bits from the the active layer, and
89 // we will soon swap out its tilings and use them for recycling. However,
90 // there are now tiles in this layer's tilings that were unref'd and replaced
91 // with new tiles (due to invalidation). This resets all active priorities on
92 // the to-be-recycled tiling to ensure replaced tiles don't linger and take
93 // memory (due to a stale 'active' priority).
94 if (layer_impl->tilings_)
95 layer_impl->tilings_->DidBecomeRecycled();
97 LayerImpl::PushPropertiesTo(base_layer);
99 // When the pending tree pushes to the active tree, the pending twin
101 layer_impl->twin_layer_ = NULL;
104 layer_impl->SetIsMask(is_mask_);
105 layer_impl->pile_ = pile_;
107 // Tilings would be expensive to push, so we swap.
108 layer_impl->tilings_.swap(tilings_);
110 // Ensure that we don't have any tiles that are out of date.
112 tilings_->RemoveTilesInRegion(invalidation_);
114 layer_impl->tilings_->SetClient(layer_impl);
116 tilings_->SetClient(this);
118 layer_impl->raster_page_scale_ = raster_page_scale_;
119 layer_impl->raster_device_scale_ = raster_device_scale_;
120 layer_impl->raster_source_scale_ = raster_source_scale_;
121 layer_impl->raster_contents_scale_ = raster_contents_scale_;
122 layer_impl->low_res_raster_contents_scale_ = low_res_raster_contents_scale_;
124 layer_impl->UpdateLCDTextStatus(is_using_lcd_text_);
125 layer_impl->needs_post_commit_initialization_ = false;
127 // The invalidation on this soon-to-be-recycled layer must be cleared to
128 // mirror clearing the invalidation in PictureLayer's version of this function
129 // in case push properties is skipped.
130 layer_impl->invalidation_.Swap(&invalidation_);
131 invalidation_.Clear();
132 needs_post_commit_initialization_ = true;
134 // We always need to push properties.
135 // See http://crbug.com/303943
136 needs_push_properties_ = true;
139 void PictureLayerImpl::AppendQuads(QuadSink* quad_sink,
140 AppendQuadsData* append_quads_data) {
141 DCHECK(!needs_post_commit_initialization_);
142 gfx::Rect rect(visible_content_rect());
143 gfx::Rect content_rect(content_bounds());
145 SharedQuadState* shared_quad_state = quad_sink->CreateSharedQuadState();
146 PopulateSharedQuadState(shared_quad_state);
148 if (current_draw_mode_ == DRAW_MODE_RESOURCELESS_SOFTWARE) {
149 AppendDebugBorderQuad(
153 DebugColors::DirectPictureBorderColor(),
154 DebugColors::DirectPictureBorderWidth(layer_tree_impl()));
156 gfx::Rect geometry_rect = rect;
157 gfx::Rect opaque_rect = contents_opaque() ? geometry_rect : gfx::Rect();
158 gfx::Rect visible_geometry_rect =
159 quad_sink->UnoccludedContentRect(geometry_rect, draw_transform());
160 if (visible_geometry_rect.IsEmpty())
163 gfx::Size texture_size = rect.size();
164 gfx::RectF texture_rect = gfx::RectF(texture_size);
165 gfx::Rect quad_content_rect = rect;
166 float contents_scale = contents_scale_x();
168 scoped_ptr<PictureDrawQuad> quad = PictureDrawQuad::Create();
169 quad->SetNew(shared_quad_state,
172 visible_geometry_rect,
179 quad_sink->Append(quad.PassAs<DrawQuad>());
180 append_quads_data->num_missing_tiles++;
184 AppendDebugBorderQuad(quad_sink, shared_quad_state, append_quads_data);
186 if (ShowDebugBorders()) {
187 for (PictureLayerTilingSet::CoverageIterator iter(
188 tilings_.get(), contents_scale_x(), rect, ideal_contents_scale_);
193 if (*iter && iter->IsReadyToDraw()) {
194 ManagedTileState::TileVersion::Mode mode =
195 iter->GetTileVersionForDrawing().mode();
196 if (mode == ManagedTileState::TileVersion::SOLID_COLOR_MODE) {
197 color = DebugColors::SolidColorTileBorderColor();
198 width = DebugColors::SolidColorTileBorderWidth(layer_tree_impl());
199 } else if (mode == ManagedTileState::TileVersion::PICTURE_PILE_MODE) {
200 color = DebugColors::PictureTileBorderColor();
201 width = DebugColors::PictureTileBorderWidth(layer_tree_impl());
202 } else if (iter->priority(ACTIVE_TREE).resolution == HIGH_RESOLUTION) {
203 color = DebugColors::HighResTileBorderColor();
204 width = DebugColors::HighResTileBorderWidth(layer_tree_impl());
205 } else if (iter->priority(ACTIVE_TREE).resolution == LOW_RESOLUTION) {
206 color = DebugColors::LowResTileBorderColor();
207 width = DebugColors::LowResTileBorderWidth(layer_tree_impl());
208 } else if (iter->contents_scale() > contents_scale_x()) {
209 color = DebugColors::ExtraHighResTileBorderColor();
210 width = DebugColors::ExtraHighResTileBorderWidth(layer_tree_impl());
212 color = DebugColors::ExtraLowResTileBorderColor();
213 width = DebugColors::ExtraLowResTileBorderWidth(layer_tree_impl());
216 color = DebugColors::MissingTileBorderColor();
217 width = DebugColors::MissingTileBorderWidth(layer_tree_impl());
220 scoped_ptr<DebugBorderDrawQuad> debug_border_quad =
221 DebugBorderDrawQuad::Create();
222 gfx::Rect geometry_rect = iter.geometry_rect();
223 gfx::Rect visible_geometry_rect = geometry_rect;
224 debug_border_quad->SetNew(shared_quad_state,
226 visible_geometry_rect,
229 quad_sink->Append(debug_border_quad.PassAs<DrawQuad>());
233 // Keep track of the tilings that were used so that tilings that are
234 // unused can be considered for removal.
235 std::vector<PictureLayerTiling*> seen_tilings;
237 for (PictureLayerTilingSet::CoverageIterator iter(
238 tilings_.get(), contents_scale_x(), rect, ideal_contents_scale_);
241 gfx::Rect geometry_rect = iter.geometry_rect();
242 gfx::Rect visible_geometry_rect =
243 quad_sink->UnoccludedContentRect(geometry_rect, draw_transform());
244 if (visible_geometry_rect.IsEmpty())
247 append_quads_data->visible_content_area +=
248 visible_geometry_rect.width() * visible_geometry_rect.height();
250 if (!*iter || !iter->IsReadyToDraw()) {
251 if (draw_checkerboard_for_missing_tiles()) {
252 scoped_ptr<CheckerboardDrawQuad> quad = CheckerboardDrawQuad::Create();
253 SkColor color = DebugColors::DefaultCheckerboardColor();
255 shared_quad_state, geometry_rect, visible_geometry_rect, color);
256 quad_sink->Append(quad.PassAs<DrawQuad>());
258 SkColor color = SafeOpaqueBackgroundColor();
259 scoped_ptr<SolidColorDrawQuad> quad = SolidColorDrawQuad::Create();
260 quad->SetNew(shared_quad_state,
262 visible_geometry_rect,
265 quad_sink->Append(quad.PassAs<DrawQuad>());
268 append_quads_data->num_missing_tiles++;
269 append_quads_data->had_incomplete_tile = true;
270 append_quads_data->approximated_visible_content_area +=
271 visible_geometry_rect.width() * visible_geometry_rect.height();
275 const ManagedTileState::TileVersion& tile_version =
276 iter->GetTileVersionForDrawing();
277 scoped_ptr<DrawQuad> draw_quad;
278 switch (tile_version.mode()) {
279 case ManagedTileState::TileVersion::RESOURCE_MODE: {
280 gfx::RectF texture_rect = iter.texture_rect();
281 gfx::Rect opaque_rect = iter->opaque_rect();
282 opaque_rect.Intersect(geometry_rect);
284 if (iter->contents_scale() != ideal_contents_scale_)
285 append_quads_data->had_incomplete_tile = true;
287 scoped_ptr<TileDrawQuad> quad = TileDrawQuad::Create();
288 quad->SetNew(shared_quad_state,
291 visible_geometry_rect,
292 tile_version.get_resource_id(),
295 tile_version.contents_swizzled());
296 draw_quad = quad.PassAs<DrawQuad>();
299 case ManagedTileState::TileVersion::PICTURE_PILE_MODE: {
300 gfx::RectF texture_rect = iter.texture_rect();
301 gfx::Rect opaque_rect = iter->opaque_rect();
302 opaque_rect.Intersect(geometry_rect);
304 ResourceProvider* resource_provider =
305 layer_tree_impl()->resource_provider();
306 ResourceFormat format =
307 resource_provider->memory_efficient_texture_format();
308 scoped_ptr<PictureDrawQuad> quad = PictureDrawQuad::Create();
309 quad->SetNew(shared_quad_state,
312 visible_geometry_rect,
316 iter->content_rect(),
317 iter->contents_scale(),
319 draw_quad = quad.PassAs<DrawQuad>();
322 case ManagedTileState::TileVersion::SOLID_COLOR_MODE: {
323 scoped_ptr<SolidColorDrawQuad> quad = SolidColorDrawQuad::Create();
324 quad->SetNew(shared_quad_state,
326 visible_geometry_rect,
327 tile_version.get_solid_color(),
329 draw_quad = quad.PassAs<DrawQuad>();
335 quad_sink->Append(draw_quad.Pass());
337 if (iter->priority(ACTIVE_TREE).resolution != HIGH_RESOLUTION) {
338 append_quads_data->approximated_visible_content_area +=
339 visible_geometry_rect.width() * visible_geometry_rect.height();
342 if (seen_tilings.empty() || seen_tilings.back() != iter.CurrentTiling())
343 seen_tilings.push_back(iter.CurrentTiling());
346 // Aggressively remove any tilings that are not seen to save memory. Note
347 // that this is at the expense of doing cause more frequent re-painting. A
348 // better scheme would be to maintain a tighter visible_content_rect for the
350 CleanUpTilingsOnActiveLayer(seen_tilings);
353 void PictureLayerImpl::DidUnregisterLayer() {
354 layer_needs_to_register_itself_ = true;
357 void PictureLayerImpl::UpdateTilePriorities() {
358 DCHECK(!needs_post_commit_initialization_);
359 CHECK(should_update_tile_priorities_);
361 if (layer_needs_to_register_itself_) {
362 layer_tree_impl()->tile_manager()->RegisterPictureLayerImpl(this);
363 layer_needs_to_register_itself_ = false;
366 if (layer_tree_impl()->device_viewport_valid_for_tile_management()) {
367 visible_rect_for_tile_priority_ = visible_content_rect();
368 viewport_size_for_tile_priority_ = layer_tree_impl()->DrawViewportSize();
369 screen_space_transform_for_tile_priority_ = screen_space_transform();
372 if (!tilings_->num_tilings())
375 double current_frame_time_in_seconds =
376 (layer_tree_impl()->CurrentFrameTimeTicks() -
377 base::TimeTicks()).InSecondsF();
379 bool tiling_needs_update = false;
380 for (size_t i = 0; i < tilings_->num_tilings(); ++i) {
381 if (tilings_->tiling_at(i)->NeedsUpdateForFrameAtTime(
382 current_frame_time_in_seconds)) {
383 tiling_needs_update = true;
387 if (!tiling_needs_update)
390 UpdateLCDTextStatus(can_use_lcd_text());
392 // Use visible_content_rect, unless it's empty. If it's empty, then
393 // try to inverse project the viewport into layer space and use that.
394 gfx::Rect visible_rect_in_content_space = visible_rect_for_tile_priority_;
395 if (visible_rect_in_content_space.IsEmpty()) {
396 gfx::Transform screen_to_layer(gfx::Transform::kSkipInitialization);
397 if (screen_space_transform_for_tile_priority_.GetInverse(
399 visible_rect_in_content_space =
400 gfx::ToEnclosingRect(MathUtil::ProjectClippedRect(
401 screen_to_layer, gfx::Rect(viewport_size_for_tile_priority_)));
402 visible_rect_in_content_space.Intersect(gfx::Rect(content_bounds()));
407 layer_tree_impl()->IsActiveTree() ? ACTIVE_TREE : PENDING_TREE;
409 tilings_->UpdateTilePriorities(tree,
410 visible_rect_in_content_space,
412 current_frame_time_in_seconds);
414 if (layer_tree_impl()->IsPendingTree())
415 MarkVisibleResourcesAsRequired();
417 // Tile priorities were modified.
418 layer_tree_impl()->DidModifyTilePriorities();
421 void PictureLayerImpl::NotifyTileInitialized(const Tile* tile) {
422 if (layer_tree_impl()->IsActiveTree()) {
423 gfx::RectF layer_damage_rect =
424 gfx::ScaleRect(tile->content_rect(), 1.f / tile->contents_scale());
425 AddDamageRect(layer_damage_rect);
429 void PictureLayerImpl::DidBecomeActive() {
430 LayerImpl::DidBecomeActive();
431 tilings_->DidBecomeActive();
432 layer_tree_impl()->DidModifyTilePriorities();
435 void PictureLayerImpl::DidBeginTracing() {
436 pile_->DidBeginTracing();
439 void PictureLayerImpl::ReleaseResources() {
445 // To avoid an edge case after lost context where the tree is up to date but
446 // the tilings have not been managed, request an update draw properties
447 // to force tilings to get managed.
448 layer_tree_impl()->set_needs_update_draw_properties();
451 void PictureLayerImpl::CalculateContentsScale(
452 float ideal_contents_scale,
453 float device_scale_factor,
454 float page_scale_factor,
455 float maximum_animation_contents_scale,
456 bool animating_transform_to_screen,
457 float* contents_scale_x,
458 float* contents_scale_y,
459 gfx::Size* content_bounds) {
460 DoPostCommitInitializationIfNeeded();
462 // This function sets valid raster scales and manages tilings, so tile
463 // priorities can now be updated.
464 should_update_tile_priorities_ = true;
466 if (!CanHaveTilings()) {
467 ideal_page_scale_ = page_scale_factor;
468 ideal_device_scale_ = device_scale_factor;
469 ideal_contents_scale_ = ideal_contents_scale;
470 ideal_source_scale_ =
471 ideal_contents_scale_ / ideal_page_scale_ / ideal_device_scale_;
472 *contents_scale_x = ideal_contents_scale_;
473 *contents_scale_y = ideal_contents_scale_;
474 *content_bounds = gfx::ToCeiledSize(gfx::ScaleSize(bounds(),
475 ideal_contents_scale_,
476 ideal_contents_scale_));
480 float min_contents_scale = MinimumContentsScale();
481 DCHECK_GT(min_contents_scale, 0.f);
482 float min_page_scale = layer_tree_impl()->min_page_scale_factor();
483 DCHECK_GT(min_page_scale, 0.f);
484 float min_device_scale = 1.f;
485 float min_source_scale =
486 min_contents_scale / min_page_scale / min_device_scale;
488 float ideal_page_scale = page_scale_factor;
489 float ideal_device_scale = device_scale_factor;
490 float ideal_source_scale =
491 ideal_contents_scale / ideal_page_scale / ideal_device_scale;
493 ideal_contents_scale_ = std::max(ideal_contents_scale, min_contents_scale);
494 ideal_page_scale_ = ideal_page_scale;
495 ideal_device_scale_ = ideal_device_scale;
496 ideal_source_scale_ = std::max(ideal_source_scale, min_source_scale);
498 ManageTilings(animating_transform_to_screen,
499 maximum_animation_contents_scale);
501 // The content scale and bounds for a PictureLayerImpl is somewhat fictitious.
502 // There are (usually) several tilings at different scales. However, the
503 // content bounds is the (integer!) space in which quads are generated.
504 // In order to guarantee that we can fill this integer space with any set of
505 // tilings (and then map back to floating point texture coordinates), the
506 // contents scale must be at least as large as the largest of the tilings.
507 float max_contents_scale = min_contents_scale;
508 for (size_t i = 0; i < tilings_->num_tilings(); ++i) {
509 const PictureLayerTiling* tiling = tilings_->tiling_at(i);
510 max_contents_scale = std::max(max_contents_scale, tiling->contents_scale());
513 *contents_scale_x = max_contents_scale;
514 *contents_scale_y = max_contents_scale;
515 *content_bounds = gfx::ToCeiledSize(
516 gfx::ScaleSize(bounds(), max_contents_scale, max_contents_scale));
519 skia::RefPtr<SkPicture> PictureLayerImpl::GetPicture() {
520 return pile_->GetFlattenedPicture();
523 scoped_refptr<Tile> PictureLayerImpl::CreateTile(PictureLayerTiling* tiling,
524 const gfx::Rect& content_rect) {
525 if (!pile_->CanRaster(tiling->contents_scale(), content_rect))
526 return scoped_refptr<Tile>();
529 if (is_using_lcd_text_)
530 flags |= Tile::USE_LCD_TEXT;
531 if (use_gpu_rasterization())
532 flags |= Tile::USE_GPU_RASTERIZATION;
533 return layer_tree_impl()->tile_manager()->CreateTile(
537 contents_opaque() ? content_rect : gfx::Rect(),
538 tiling->contents_scale(),
540 layer_tree_impl()->source_frame_number(),
544 void PictureLayerImpl::UpdatePile(Tile* tile) {
545 tile->set_picture_pile(pile_);
548 const Region* PictureLayerImpl::GetInvalidation() {
549 return &invalidation_;
552 const PictureLayerTiling* PictureLayerImpl::GetTwinTiling(
553 const PictureLayerTiling* tiling) const {
555 twin_layer_->use_gpu_rasterization() != use_gpu_rasterization())
557 for (size_t i = 0; i < twin_layer_->tilings_->num_tilings(); ++i)
558 if (twin_layer_->tilings_->tiling_at(i)->contents_scale() ==
559 tiling->contents_scale())
560 return twin_layer_->tilings_->tiling_at(i);
564 size_t PictureLayerImpl::GetMaxTilesForInterestArea() const {
565 return layer_tree_impl()->settings().max_tiles_for_interest_area;
568 float PictureLayerImpl::GetSkewportTargetTimeInSeconds() const {
569 float skewport_target_time_in_frames = use_gpu_rasterization()
570 ? kGpuSkewportTargetTimeInFrames
571 : kCpuSkewportTargetTimeInFrames;
572 return skewport_target_time_in_frames *
573 layer_tree_impl()->begin_impl_frame_interval().InSecondsF() *
574 layer_tree_impl()->settings().skewport_target_time_multiplier;
577 int PictureLayerImpl::GetSkewportExtrapolationLimitInContentPixels() const {
578 return layer_tree_impl()
580 .skewport_extrapolation_limit_in_content_pixels;
583 gfx::Size PictureLayerImpl::CalculateTileSize(
584 const gfx::Size& content_bounds) const {
586 int max_size = layer_tree_impl()->MaxTextureSize();
588 std::min(max_size, content_bounds.width()),
589 std::min(max_size, content_bounds.height()));
592 int max_texture_size =
593 layer_tree_impl()->resource_provider()->max_texture_size();
595 gfx::Size default_tile_size = layer_tree_impl()->settings().default_tile_size;
596 if (use_gpu_rasterization()) {
597 // TODO(ernstm) crbug.com/365877: We need a unified way to override the
598 // default-tile-size.
600 gfx::Size(layer_tree_impl()->device_viewport_size().width(),
601 layer_tree_impl()->device_viewport_size().height() / 4);
603 default_tile_size.SetToMin(gfx::Size(max_texture_size, max_texture_size));
605 gfx::Size max_untiled_content_size =
606 layer_tree_impl()->settings().max_untiled_layer_size;
607 max_untiled_content_size.SetToMin(
608 gfx::Size(max_texture_size, max_texture_size));
610 bool any_dimension_too_large =
611 content_bounds.width() > max_untiled_content_size.width() ||
612 content_bounds.height() > max_untiled_content_size.height();
614 bool any_dimension_one_tile =
615 content_bounds.width() <= default_tile_size.width() ||
616 content_bounds.height() <= default_tile_size.height();
618 // If long and skinny, tile at the max untiled content size, and clamp
619 // the smaller dimension to the content size, e.g. 1000x12 layer with
620 // 500x500 max untiled size would get 500x12 tiles. Also do this
621 // if the layer is small.
622 if (any_dimension_one_tile || !any_dimension_too_large) {
623 int width = std::min(
624 std::max(max_untiled_content_size.width(), default_tile_size.width()),
625 content_bounds.width());
626 int height = std::min(
627 std::max(max_untiled_content_size.height(), default_tile_size.height()),
628 content_bounds.height());
629 // Round width and height up to the closest multiple of 64, or 56 if
630 // we should avoid power-of-two textures. This helps reduce the number
631 // of different textures sizes to help recycling, and also keeps all
632 // textures multiple-of-eight, which is preferred on some drivers (IMG).
634 layer_tree_impl()->GetRendererCapabilities().avoid_pow2_textures;
635 int round_up_to = avoid_pow2 ? 56 : 64;
636 width = RoundUp(width, round_up_to);
637 height = RoundUp(height, round_up_to);
638 return gfx::Size(width, height);
641 return default_tile_size;
644 void PictureLayerImpl::SyncFromActiveLayer(const PictureLayerImpl* other) {
645 DCHECK(!other->needs_post_commit_initialization_);
646 DCHECK(other->tilings_);
648 UpdateLCDTextStatus(other->is_using_lcd_text_);
650 if (!DrawsContent()) {
655 raster_page_scale_ = other->raster_page_scale_;
656 raster_device_scale_ = other->raster_device_scale_;
657 raster_source_scale_ = other->raster_source_scale_;
658 raster_contents_scale_ = other->raster_contents_scale_;
659 low_res_raster_contents_scale_ = other->low_res_raster_contents_scale_;
661 // Add synthetic invalidations for any recordings that were dropped. As
662 // tiles are updated to point to this new pile, this will force the dropping
663 // of tiles that can no longer be rastered. This is not ideal, but is a
664 // trade-off for memory (use the same pile as much as possible, by switching
665 // during DidBecomeActive) and for time (don't bother checking every tile
666 // during activation to see if the new pile can still raster it).
667 for (int x = 0; x < pile_->num_tiles_x(); ++x) {
668 for (int y = 0; y < pile_->num_tiles_y(); ++y) {
669 bool previously_had = other->pile_->HasRecordingAt(x, y);
670 bool now_has = pile_->HasRecordingAt(x, y);
671 if (now_has || !previously_had)
673 gfx::Rect layer_rect = pile_->tile_bounds(x, y);
674 invalidation_.Union(layer_rect);
678 // Union in the other newly exposed regions as invalid.
679 Region difference_region = Region(gfx::Rect(bounds()));
680 difference_region.Subtract(gfx::Rect(other->bounds()));
681 invalidation_.Union(difference_region);
683 if (CanHaveTilings()) {
684 tilings_->SyncTilings(
685 *other->tilings_, bounds(), invalidation_, MinimumContentsScale());
690 SanityCheckTilingState();
693 void PictureLayerImpl::SyncTiling(
694 const PictureLayerTiling* tiling) {
695 if (!CanHaveTilingWithScale(tiling->contents_scale()))
697 tilings_->AddTiling(tiling->contents_scale());
699 // If this tree needs update draw properties, then the tiling will
700 // get updated prior to drawing or activation. If this tree does not
701 // need update draw properties, then its transforms are up to date and
702 // we can create tiles for this tiling immediately.
703 if (!layer_tree_impl()->needs_update_draw_properties() &&
704 should_update_tile_priorities_) {
705 UpdateTilePriorities();
709 void PictureLayerImpl::SetIsMask(bool is_mask) {
710 if (is_mask_ == is_mask)
714 tilings_->RemoveAllTiles();
717 ResourceProvider::ResourceId PictureLayerImpl::ContentsResourceId() const {
718 gfx::Rect content_rect(content_bounds());
719 float scale = contents_scale_x();
720 PictureLayerTilingSet::CoverageIterator iter(
721 tilings_.get(), scale, content_rect, ideal_contents_scale_);
723 // Mask resource not ready yet.
727 // Masks only supported if they fit on exactly one tile.
728 if (iter.geometry_rect() != content_rect)
731 const ManagedTileState::TileVersion& tile_version =
732 iter->GetTileVersionForDrawing();
733 if (!tile_version.IsReadyToDraw() ||
734 tile_version.mode() != ManagedTileState::TileVersion::RESOURCE_MODE)
737 return tile_version.get_resource_id();
740 void PictureLayerImpl::MarkVisibleResourcesAsRequired() const {
741 DCHECK(layer_tree_impl()->IsPendingTree());
742 DCHECK(!layer_tree_impl()->needs_update_draw_properties());
743 DCHECK(ideal_contents_scale_);
744 DCHECK_GT(tilings_->num_tilings(), 0u);
746 // The goal of this function is to find the minimum set of tiles that need to
747 // be ready to draw in order to activate without flashing content from a
748 // higher res on the active tree to a lower res on the pending tree.
750 gfx::Rect rect(visible_content_rect());
752 float min_acceptable_scale =
753 std::min(raster_contents_scale_, ideal_contents_scale_);
755 if (PictureLayerImpl* twin = twin_layer_) {
756 float twin_min_acceptable_scale =
757 std::min(twin->ideal_contents_scale_, twin->raster_contents_scale_);
758 // Ignore 0 scale in case CalculateContentsScale() has never been
759 // called for active twin.
760 if (twin_min_acceptable_scale != 0.0f) {
761 min_acceptable_scale =
762 std::min(min_acceptable_scale, twin_min_acceptable_scale);
766 PictureLayerTiling* high_res = NULL;
767 PictureLayerTiling* low_res = NULL;
769 // First pass: ready to draw tiles in acceptable but non-ideal tilings are
770 // marked as required for activation so that their textures are not thrown
771 // away; any non-ready tiles are not marked as required.
772 Region missing_region = rect;
773 for (size_t i = 0; i < tilings_->num_tilings(); ++i) {
774 PictureLayerTiling* tiling = tilings_->tiling_at(i);
775 DCHECK(tiling->has_ever_been_updated());
777 if (tiling->resolution() == LOW_RESOLUTION) {
778 DCHECK(!low_res) << "There can only be one low res tiling";
781 if (tiling->contents_scale() < min_acceptable_scale)
783 if (tiling->resolution() == HIGH_RESOLUTION) {
784 DCHECK(!high_res) << "There can only be one high res tiling";
788 for (PictureLayerTiling::CoverageIterator iter(tiling,
793 if (!*iter || !iter->IsReadyToDraw())
796 missing_region.Subtract(iter.geometry_rect());
797 iter->MarkRequiredForActivation();
800 DCHECK(high_res) << "There must be one high res tiling";
802 // If these pointers are null (because no twin, no matching tiling, or the
803 // simpification just below), then high res tiles will be required to fill any
804 // holes left by the first pass above. If the pointers are valid, then this
805 // layer is allowed to skip any tiles that are not ready on its twin.
806 const PictureLayerTiling* twin_high_res = NULL;
807 const PictureLayerTiling* twin_low_res = NULL;
809 // As a simplification, only allow activating to skip twin tiles that the
810 // active layer is also missing when both this layer and its twin have 2
811 // tilings (high and low). This avoids having to iterate/track coverage of
812 // non-ideal tilings during the last draw call on the active layer.
813 if (high_res && low_res && tilings_->num_tilings() == 2 &&
814 twin_layer_ && twin_layer_->tilings_->num_tilings() == 2) {
815 twin_low_res = GetTwinTiling(low_res);
817 twin_high_res = GetTwinTiling(high_res);
819 // If this layer and its twin have different bounds or transforms, then don't
820 // compare them and only allow activating to high res tiles, since tiles on
821 // each layer will occupy different areas of the screen.
822 if (!twin_high_res || !twin_low_res ||
823 twin_layer_->layer_tree_impl()->RequiresHighResToDraw() ||
824 bounds() != twin_layer_->bounds() ||
825 draw_properties().screen_space_transform !=
826 twin_layer_->draw_properties().screen_space_transform) {
827 twin_high_res = NULL;
831 // As a second pass, mark as required any visible high res tiles not filled in
832 // by acceptable non-ideal tiles from the first pass.
833 if (MarkVisibleTilesAsRequired(
834 high_res, twin_high_res, contents_scale_x(), rect, missing_region)) {
835 // As an optional third pass, if a high res tile was skipped because its
836 // twin was also missing, then fall back to mark low res tiles as required
837 // in case the active twin is substituting those for missing high res
839 MarkVisibleTilesAsRequired(
840 low_res, twin_low_res, contents_scale_x(), rect, missing_region);
844 bool PictureLayerImpl::MarkVisibleTilesAsRequired(
845 PictureLayerTiling* tiling,
846 const PictureLayerTiling* optional_twin_tiling,
847 float contents_scale,
848 const gfx::Rect& rect,
849 const Region& missing_region) const {
850 bool twin_had_missing_tile = false;
851 for (PictureLayerTiling::CoverageIterator iter(tiling,
857 // A null tile (i.e. missing recording) can just be skipped.
861 // If the missing region doesn't cover it, this tile is fully
862 // covered by acceptable tiles at other scales.
863 if (!missing_region.Intersects(iter.geometry_rect()))
866 // If the twin tile doesn't exist (i.e. missing recording or so far away
867 // that it is outside the visible tile rect) or this tile is shared between
868 // with the twin, then this tile isn't required to prevent flashing.
869 if (optional_twin_tiling) {
870 Tile* twin_tile = optional_twin_tiling->TileAt(iter.i(), iter.j());
871 if (!twin_tile || twin_tile == tile) {
872 twin_had_missing_tile = true;
877 tile->MarkRequiredForActivation();
879 return twin_had_missing_tile;
882 void PictureLayerImpl::DoPostCommitInitialization() {
883 DCHECK(needs_post_commit_initialization_);
884 DCHECK(layer_tree_impl()->IsPendingTree());
887 tilings_.reset(new PictureLayerTilingSet(this, bounds()));
889 DCHECK(!twin_layer_);
890 twin_layer_ = static_cast<PictureLayerImpl*>(
891 layer_tree_impl()->FindActiveTreeLayerById(id()));
893 DCHECK(!twin_layer_->twin_layer_);
894 twin_layer_->twin_layer_ = this;
895 // If the twin has never been pushed to, do not sync from it.
896 // This can happen if this function is called during activation.
897 if (!twin_layer_->needs_post_commit_initialization_)
898 SyncFromActiveLayer(twin_layer_);
901 needs_post_commit_initialization_ = false;
904 PictureLayerTiling* PictureLayerImpl::AddTiling(float contents_scale) {
905 DCHECK(CanHaveTilingWithScale(contents_scale)) <<
906 "contents_scale: " << contents_scale;
908 PictureLayerTiling* tiling = tilings_->AddTiling(contents_scale);
910 DCHECK(pile_->HasRecordings());
913 twin_layer_->use_gpu_rasterization() == use_gpu_rasterization())
914 twin_layer_->SyncTiling(tiling);
919 void PictureLayerImpl::RemoveTiling(float contents_scale) {
920 for (size_t i = 0; i < tilings_->num_tilings(); ++i) {
921 PictureLayerTiling* tiling = tilings_->tiling_at(i);
922 if (tiling->contents_scale() == contents_scale) {
923 tilings_->Remove(tiling);
927 if (tilings_->num_tilings() == 0)
929 SanityCheckTilingState();
932 void PictureLayerImpl::RemoveAllTilings() {
934 tilings_->RemoveAllTilings();
935 // If there are no tilings, then raster scales are no longer meaningful.
941 inline float PositiveRatio(float float1, float float2) {
942 DCHECK_GT(float1, 0);
943 DCHECK_GT(float2, 0);
944 return float1 > float2 ? float1 / float2 : float2 / float1;
949 void PictureLayerImpl::ManageTilings(bool animating_transform_to_screen,
950 float maximum_animation_contents_scale) {
951 DCHECK(ideal_contents_scale_);
952 DCHECK(ideal_page_scale_);
953 DCHECK(ideal_device_scale_);
954 DCHECK(ideal_source_scale_);
955 DCHECK(CanHaveTilings());
956 DCHECK(!needs_post_commit_initialization_);
958 bool change_target_tiling =
959 raster_page_scale_ == 0.f ||
960 raster_device_scale_ == 0.f ||
961 raster_source_scale_ == 0.f ||
962 raster_contents_scale_ == 0.f ||
963 low_res_raster_contents_scale_ == 0.f ||
964 ShouldAdjustRasterScale(animating_transform_to_screen);
966 if (tilings_->num_tilings() == 0) {
967 DCHECK(change_target_tiling)
968 << "A layer with no tilings shouldn't have valid raster scales";
971 if (change_target_tiling) {
972 RecalculateRasterScales(animating_transform_to_screen,
973 maximum_animation_contents_scale);
976 was_animating_transform_to_screen_ = animating_transform_to_screen;
978 if (!change_target_tiling)
981 PictureLayerTiling* high_res = NULL;
982 PictureLayerTiling* low_res = NULL;
984 PictureLayerTiling* previous_low_res = NULL;
985 for (size_t i = 0; i < tilings_->num_tilings(); ++i) {
986 PictureLayerTiling* tiling = tilings_->tiling_at(i);
987 if (tiling->contents_scale() == raster_contents_scale_)
989 if (tiling->contents_scale() == low_res_raster_contents_scale_)
991 if (tiling->resolution() == LOW_RESOLUTION)
992 previous_low_res = tiling;
994 // Reset all tilings to non-ideal until the end of this function.
995 tiling->set_resolution(NON_IDEAL_RESOLUTION);
999 high_res = AddTiling(raster_contents_scale_);
1000 if (raster_contents_scale_ == low_res_raster_contents_scale_)
1004 // Only create new low res tilings when the transform is static. This
1005 // prevents wastefully creating a paired low res tiling for every new high res
1006 // tiling during a pinch or a CSS animation.
1007 bool is_pinching = layer_tree_impl()->PinchGestureActive();
1008 if (ShouldHaveLowResTiling() && !is_pinching &&
1009 !animating_transform_to_screen &&
1010 !low_res && low_res != high_res)
1011 low_res = AddTiling(low_res_raster_contents_scale_);
1013 // Set low-res if we have one.
1015 low_res = previous_low_res;
1016 if (low_res && low_res != high_res)
1017 low_res->set_resolution(LOW_RESOLUTION);
1019 // Make sure we always have one high-res (even if high == low).
1020 high_res->set_resolution(HIGH_RESOLUTION);
1022 SanityCheckTilingState();
1025 bool PictureLayerImpl::ShouldAdjustRasterScale(
1026 bool animating_transform_to_screen) const {
1027 if (was_animating_transform_to_screen_ != animating_transform_to_screen)
1030 bool is_pinching = layer_tree_impl()->PinchGestureActive();
1031 if (is_pinching && raster_page_scale_) {
1032 // We change our raster scale when it is:
1033 // - Higher than ideal (need a lower-res tiling available)
1034 // - Too far from ideal (need a higher-res tiling available)
1035 float ratio = ideal_page_scale_ / raster_page_scale_;
1036 if (raster_page_scale_ > ideal_page_scale_ ||
1037 ratio > kMaxScaleRatioDuringPinch)
1042 // When not pinching, match the ideal page scale factor.
1043 if (raster_page_scale_ != ideal_page_scale_)
1047 // Always match the ideal device scale factor.
1048 if (raster_device_scale_ != ideal_device_scale_)
1051 // When the source scale changes we want to match it, but not when animating
1052 // or when we've fixed the scale in place.
1053 if (!animating_transform_to_screen && !raster_source_scale_is_fixed_ &&
1054 raster_source_scale_ != ideal_source_scale_)
1060 float PictureLayerImpl::SnappedContentsScale(float scale) {
1061 // If a tiling exists within the max snapping ratio, snap to its scale.
1062 float snapped_contents_scale = scale;
1063 float snapped_ratio = kSnapToExistingTilingRatio;
1064 for (size_t i = 0; i < tilings_->num_tilings(); ++i) {
1065 float tiling_contents_scale = tilings_->tiling_at(i)->contents_scale();
1066 float ratio = PositiveRatio(tiling_contents_scale, scale);
1067 if (ratio < snapped_ratio) {
1068 snapped_contents_scale = tiling_contents_scale;
1069 snapped_ratio = ratio;
1072 return snapped_contents_scale;
1075 void PictureLayerImpl::RecalculateRasterScales(
1076 bool animating_transform_to_screen,
1077 float maximum_animation_contents_scale) {
1078 float old_raster_contents_scale = raster_contents_scale_;
1079 float old_raster_page_scale = raster_page_scale_;
1080 float old_raster_source_scale = raster_source_scale_;
1082 raster_device_scale_ = ideal_device_scale_;
1083 raster_page_scale_ = ideal_page_scale_;
1084 raster_source_scale_ = ideal_source_scale_;
1085 raster_contents_scale_ = ideal_contents_scale_;
1087 // If we're not animating, or leaving an animation, and the
1088 // ideal_source_scale_ changes, then things are unpredictable, and we fix
1089 // the raster_source_scale_ in place.
1090 if (old_raster_source_scale && !animating_transform_to_screen &&
1091 !was_animating_transform_to_screen_ &&
1092 old_raster_source_scale != ideal_source_scale_)
1093 raster_source_scale_is_fixed_ = true;
1095 // TODO(danakj): Adjust raster source scale closer to ideal source scale at
1096 // a throttled rate. Possibly make use of invalidation_.IsEmpty() on pending
1097 // tree. This will allow CSS scale changes to get re-rastered at an
1098 // appropriate rate.
1099 if (raster_source_scale_is_fixed_) {
1100 raster_contents_scale_ /= raster_source_scale_;
1101 raster_source_scale_ = 1.f;
1104 // During pinch we completely ignore the current ideal scale, and just use
1105 // a multiple of the previous scale.
1106 // TODO(danakj): This seems crazy, we should use the current ideal, no?
1107 bool is_pinching = layer_tree_impl()->PinchGestureActive();
1108 if (is_pinching && old_raster_contents_scale) {
1109 // See ShouldAdjustRasterScale:
1110 // - When zooming out, preemptively create new tiling at lower resolution.
1111 // - When zooming in, approximate ideal using multiple of kMaxScaleRatio.
1112 bool zooming_out = old_raster_page_scale > ideal_page_scale_;
1113 float desired_contents_scale =
1114 zooming_out ? old_raster_contents_scale / kMaxScaleRatioDuringPinch
1115 : old_raster_contents_scale * kMaxScaleRatioDuringPinch;
1116 raster_contents_scale_ = SnappedContentsScale(desired_contents_scale);
1117 raster_page_scale_ =
1118 raster_contents_scale_ / raster_device_scale_ / raster_source_scale_;
1121 raster_contents_scale_ =
1122 std::max(raster_contents_scale_, MinimumContentsScale());
1124 // Since we're not re-rasterizing during animation, rasterize at the maximum
1125 // scale that will occur during the animation, if the maximum scale is
1127 if (animating_transform_to_screen) {
1128 if (maximum_animation_contents_scale > 0.f) {
1129 raster_contents_scale_ =
1130 std::max(raster_contents_scale_, maximum_animation_contents_scale);
1132 raster_contents_scale_ =
1133 std::max(raster_contents_scale_,
1134 1.f * ideal_page_scale_ * ideal_device_scale_);
1138 // If this layer would only create one tile at this content scale,
1139 // don't create a low res tiling.
1140 gfx::Size content_bounds =
1141 gfx::ToCeiledSize(gfx::ScaleSize(bounds(), raster_contents_scale_));
1142 gfx::Size tile_size = CalculateTileSize(content_bounds);
1143 if (tile_size.width() >= content_bounds.width() &&
1144 tile_size.height() >= content_bounds.height()) {
1145 low_res_raster_contents_scale_ = raster_contents_scale_;
1149 float low_res_factor =
1150 layer_tree_impl()->settings().low_res_contents_scale_factor;
1151 low_res_raster_contents_scale_ = std::max(
1152 raster_contents_scale_ * low_res_factor,
1153 MinimumContentsScale());
1156 void PictureLayerImpl::CleanUpTilingsOnActiveLayer(
1157 std::vector<PictureLayerTiling*> used_tilings) {
1158 DCHECK(layer_tree_impl()->IsActiveTree());
1159 if (tilings_->num_tilings() == 0)
1162 float min_acceptable_high_res_scale = std::min(
1163 raster_contents_scale_, ideal_contents_scale_);
1164 float max_acceptable_high_res_scale = std::max(
1165 raster_contents_scale_, ideal_contents_scale_);
1167 PictureLayerImpl* twin = twin_layer_;
1169 min_acceptable_high_res_scale = std::min(
1170 min_acceptable_high_res_scale,
1171 std::min(twin->raster_contents_scale_, twin->ideal_contents_scale_));
1172 max_acceptable_high_res_scale = std::max(
1173 max_acceptable_high_res_scale,
1174 std::max(twin->raster_contents_scale_, twin->ideal_contents_scale_));
1177 std::vector<PictureLayerTiling*> to_remove;
1178 for (size_t i = 0; i < tilings_->num_tilings(); ++i) {
1179 PictureLayerTiling* tiling = tilings_->tiling_at(i);
1181 // Keep multiple high resolution tilings even if not used to help
1182 // activate earlier at non-ideal resolutions.
1183 if (tiling->contents_scale() >= min_acceptable_high_res_scale &&
1184 tiling->contents_scale() <= max_acceptable_high_res_scale)
1187 // Keep low resolution tilings, if the layer should have them.
1188 if (tiling->resolution() == LOW_RESOLUTION && ShouldHaveLowResTiling())
1191 // Don't remove tilings that are being used (and thus would cause a flash.)
1192 if (std::find(used_tilings.begin(), used_tilings.end(), tiling) !=
1196 to_remove.push_back(tiling);
1199 for (size_t i = 0; i < to_remove.size(); ++i) {
1200 const PictureLayerTiling* twin_tiling = GetTwinTiling(to_remove[i]);
1201 // Only remove tilings from the twin layer if they have
1202 // NON_IDEAL_RESOLUTION.
1203 if (twin_tiling && twin_tiling->resolution() == NON_IDEAL_RESOLUTION)
1204 twin->RemoveTiling(to_remove[i]->contents_scale());
1205 // TODO(enne): temporary sanity CHECK for http://crbug.com/358350
1206 CHECK_NE(HIGH_RESOLUTION, to_remove[i]->resolution());
1207 tilings_->Remove(to_remove[i]);
1209 DCHECK_GT(tilings_->num_tilings(), 0u);
1211 SanityCheckTilingState();
1214 float PictureLayerImpl::MinimumContentsScale() const {
1215 float setting_min = layer_tree_impl()->settings().minimum_contents_scale;
1217 // If the contents scale is less than 1 / width (also for height),
1218 // then it will end up having less than one pixel of content in that
1219 // dimension. Bump the minimum contents scale up in this case to prevent
1220 // this from happening.
1221 int min_dimension = std::min(bounds().width(), bounds().height());
1225 return std::max(1.f / min_dimension, setting_min);
1228 void PictureLayerImpl::UpdateLCDTextStatus(bool new_status) {
1229 // Once this layer is not using lcd text, don't switch back.
1230 if (!is_using_lcd_text_)
1233 if (is_using_lcd_text_ == new_status)
1236 is_using_lcd_text_ = new_status;
1237 tilings_->SetCanUseLCDText(is_using_lcd_text_);
1240 void PictureLayerImpl::ResetRasterScale() {
1241 raster_page_scale_ = 0.f;
1242 raster_device_scale_ = 0.f;
1243 raster_source_scale_ = 0.f;
1244 raster_contents_scale_ = 0.f;
1245 low_res_raster_contents_scale_ = 0.f;
1246 raster_source_scale_is_fixed_ = false;
1248 // When raster scales aren't valid, don't update tile priorities until
1249 // this layer has been updated via UpdateDrawProperties.
1250 should_update_tile_priorities_ = false;
1253 bool PictureLayerImpl::CanHaveTilings() const {
1254 if (!DrawsContent())
1256 if (!pile_->HasRecordings())
1261 bool PictureLayerImpl::CanHaveTilingWithScale(float contents_scale) const {
1262 if (!CanHaveTilings())
1264 if (contents_scale < MinimumContentsScale())
1269 void PictureLayerImpl::SanityCheckTilingState() const {
1271 if (!CanHaveTilings()) {
1272 DCHECK_EQ(0u, tilings_->num_tilings());
1275 if (tilings_->num_tilings() == 0)
1278 // MarkVisibleResourcesAsRequired depends on having exactly 1 high res
1279 // tiling to mark its tiles as being required for activation.
1280 DCHECK_EQ(1, tilings_->NumHighResTilings());
1284 void PictureLayerImpl::GetDebugBorderProperties(
1286 float* width) const {
1287 *color = DebugColors::TiledContentLayerBorderColor();
1288 *width = DebugColors::TiledContentLayerBorderWidth(layer_tree_impl());
1291 void PictureLayerImpl::AsValueInto(base::DictionaryValue* state) const {
1292 const_cast<PictureLayerImpl*>(this)->DoPostCommitInitializationIfNeeded();
1293 LayerImpl::AsValueInto(state);
1294 state->SetDouble("ideal_contents_scale", ideal_contents_scale_);
1295 state->SetDouble("geometry_contents_scale", contents_scale_x());
1296 state->Set("tilings", tilings_->AsValue().release());
1297 state->Set("pictures", pile_->AsValue().release());
1298 state->Set("invalidation", invalidation_.AsValue().release());
1300 scoped_ptr<base::ListValue> coverage_tiles(new base::ListValue);
1301 for (PictureLayerTilingSet::CoverageIterator iter(tilings_.get(),
1303 gfx::Rect(content_bounds()),
1304 ideal_contents_scale_);
1307 scoped_ptr<base::DictionaryValue> tile_data(new base::DictionaryValue);
1308 tile_data->Set("geometry_rect",
1309 MathUtil::AsValue(iter.geometry_rect()).release());
1311 tile_data->Set("tile", TracedValue::CreateIDRef(*iter).release());
1313 coverage_tiles->Append(tile_data.release());
1315 state->Set("coverage_tiles", coverage_tiles.release());
1316 state->SetBoolean("is_using_lcd_text", is_using_lcd_text_);
1317 state->SetBoolean("using_gpu_rasterization", use_gpu_rasterization());
1320 size_t PictureLayerImpl::GPUMemoryUsageInBytes() const {
1321 const_cast<PictureLayerImpl*>(this)->DoPostCommitInitializationIfNeeded();
1322 return tilings_->GPUMemoryUsageInBytes();
1325 void PictureLayerImpl::RunMicroBenchmark(MicroBenchmarkImpl* benchmark) {
1326 benchmark->RunOnLayer(this);
1329 WhichTree PictureLayerImpl::GetTree() const {
1330 return layer_tree_impl()->IsActiveTree() ? ACTIVE_TREE : PENDING_TREE;
1333 bool PictureLayerImpl::IsOnActiveOrPendingTree() const {
1334 return !layer_tree_impl()->IsRecycleTree();
1337 PictureLayerImpl::LayerRasterTileIterator::LayerRasterTileIterator()
1340 PictureLayerImpl::LayerRasterTileIterator::LayerRasterTileIterator(
1341 PictureLayerImpl* layer,
1342 bool prioritize_low_res)
1343 : layer_(layer), current_stage_(0) {
1345 if (!layer_->tilings_ || !layer_->tilings_->num_tilings()) {
1346 current_stage_ = arraysize(stages_);
1351 layer_->layer_tree_impl()->IsActiveTree() ? ACTIVE_TREE : PENDING_TREE;
1353 // Find high and low res tilings and initialize the iterators.
1354 for (size_t i = 0; i < layer_->tilings_->num_tilings(); ++i) {
1355 PictureLayerTiling* tiling = layer_->tilings_->tiling_at(i);
1356 if (tiling->resolution() == HIGH_RESOLUTION) {
1357 iterators_[HIGH_RES] =
1358 PictureLayerTiling::TilingRasterTileIterator(tiling, tree);
1361 if (tiling->resolution() == LOW_RESOLUTION) {
1362 iterators_[LOW_RES] =
1363 PictureLayerTiling::TilingRasterTileIterator(tiling, tree);
1367 if (prioritize_low_res) {
1368 stages_[0].iterator_type = LOW_RES;
1369 stages_[0].tile_type = TilePriority::NOW;
1371 stages_[1].iterator_type = HIGH_RES;
1372 stages_[1].tile_type = TilePriority::NOW;
1374 stages_[0].iterator_type = HIGH_RES;
1375 stages_[0].tile_type = TilePriority::NOW;
1377 stages_[1].iterator_type = LOW_RES;
1378 stages_[1].tile_type = TilePriority::NOW;
1381 stages_[2].iterator_type = HIGH_RES;
1382 stages_[2].tile_type = TilePriority::SOON;
1384 stages_[3].iterator_type = HIGH_RES;
1385 stages_[3].tile_type = TilePriority::EVENTUALLY;
1387 IteratorType index = stages_[current_stage_].iterator_type;
1388 TilePriority::PriorityBin tile_type = stages_[current_stage_].tile_type;
1389 if (!iterators_[index] || iterators_[index].get_type() != tile_type)
1393 PictureLayerImpl::LayerRasterTileIterator::~LayerRasterTileIterator() {}
1395 PictureLayerImpl::LayerRasterTileIterator::operator bool() const {
1396 return layer_ && static_cast<size_t>(current_stage_) < arraysize(stages_);
1399 PictureLayerImpl::LayerRasterTileIterator&
1400 PictureLayerImpl::LayerRasterTileIterator::
1402 IteratorType index = stages_[current_stage_].iterator_type;
1403 TilePriority::PriorityBin tile_type = stages_[current_stage_].tile_type;
1405 // First advance the iterator.
1406 if (iterators_[index])
1407 ++iterators_[index];
1409 if (iterators_[index] && iterators_[index].get_type() == tile_type)
1412 // Next, advance the stage.
1413 int stage_count = arraysize(stages_);
1415 while (current_stage_ < stage_count) {
1416 index = stages_[current_stage_].iterator_type;
1417 tile_type = stages_[current_stage_].tile_type;
1419 if (iterators_[index] && iterators_[index].get_type() == tile_type)
1426 Tile* PictureLayerImpl::LayerRasterTileIterator::operator*() {
1429 IteratorType index = stages_[current_stage_].iterator_type;
1430 DCHECK(iterators_[index]);
1431 DCHECK(iterators_[index].get_type() == stages_[current_stage_].tile_type);
1433 return *iterators_[index];
1436 PictureLayerImpl::LayerEvictionTileIterator::LayerEvictionTileIterator()
1437 : iterator_index_(0),
1438 iteration_stage_(TilePriority::EVENTUALLY),
1439 required_for_activation_(false),
1442 PictureLayerImpl::LayerEvictionTileIterator::LayerEvictionTileIterator(
1443 PictureLayerImpl* layer,
1444 TreePriority tree_priority)
1445 : iterator_index_(0),
1446 iteration_stage_(TilePriority::EVENTUALLY),
1447 required_for_activation_(false),
1449 if (!layer_->tilings_ || !layer_->tilings_->num_tilings())
1452 size_t high_res_tiling_index = layer_->tilings_->num_tilings();
1453 size_t low_res_tiling_index = layer_->tilings_->num_tilings();
1454 for (size_t i = 0; i < layer_->tilings_->num_tilings(); ++i) {
1455 PictureLayerTiling* tiling = layer_->tilings_->tiling_at(i);
1456 if (tiling->resolution() == HIGH_RESOLUTION)
1457 high_res_tiling_index = i;
1458 else if (tiling->resolution() == LOW_RESOLUTION)
1459 low_res_tiling_index = i;
1462 iterators_.reserve(layer_->tilings_->num_tilings());
1464 // Higher resolution non-ideal goes first.
1465 for (size_t i = 0; i < high_res_tiling_index; ++i) {
1466 iterators_.push_back(PictureLayerTiling::TilingEvictionTileIterator(
1467 layer_->tilings_->tiling_at(i), tree_priority));
1470 // Lower resolution non-ideal goes next.
1471 for (size_t i = layer_->tilings_->num_tilings() - 1;
1472 i > high_res_tiling_index;
1474 PictureLayerTiling* tiling = layer_->tilings_->tiling_at(i);
1475 if (tiling->resolution() == LOW_RESOLUTION)
1478 iterators_.push_back(
1479 PictureLayerTiling::TilingEvictionTileIterator(tiling, tree_priority));
1482 // Now, put the low res tiling if we have one.
1483 if (low_res_tiling_index < layer_->tilings_->num_tilings()) {
1484 iterators_.push_back(PictureLayerTiling::TilingEvictionTileIterator(
1485 layer_->tilings_->tiling_at(low_res_tiling_index), tree_priority));
1488 // Finally, put the high res tiling if we have one.
1489 if (high_res_tiling_index < layer_->tilings_->num_tilings()) {
1490 iterators_.push_back(PictureLayerTiling::TilingEvictionTileIterator(
1491 layer_->tilings_->tiling_at(high_res_tiling_index), tree_priority));
1494 DCHECK_GT(iterators_.size(), 0u);
1496 if (!iterators_[iterator_index_] ||
1497 !IsCorrectType(&iterators_[iterator_index_])) {
1498 AdvanceToNextIterator();
1502 PictureLayerImpl::LayerEvictionTileIterator::~LayerEvictionTileIterator() {}
1504 Tile* PictureLayerImpl::LayerEvictionTileIterator::operator*() {
1506 return *iterators_[iterator_index_];
1509 PictureLayerImpl::LayerEvictionTileIterator&
1510 PictureLayerImpl::LayerEvictionTileIterator::
1513 ++iterators_[iterator_index_];
1514 if (!iterators_[iterator_index_] ||
1515 !IsCorrectType(&iterators_[iterator_index_])) {
1516 AdvanceToNextIterator();
1521 void PictureLayerImpl::LayerEvictionTileIterator::AdvanceToNextIterator() {
1525 while (iterator_index_ < iterators_.size()) {
1526 if (iterators_[iterator_index_] &&
1527 IsCorrectType(&iterators_[iterator_index_])) {
1533 // If we're NOW and required_for_activation, then this was the last pass
1534 // through the iterators.
1535 if (iteration_stage_ == TilePriority::NOW && required_for_activation_)
1538 if (!required_for_activation_) {
1539 required_for_activation_ = true;
1541 required_for_activation_ = false;
1543 static_cast<TilePriority::PriorityBin>(iteration_stage_ - 1);
1545 iterator_index_ = 0;
1549 PictureLayerImpl::LayerEvictionTileIterator::operator bool() const {
1550 return iterator_index_ < iterators_.size();
1553 bool PictureLayerImpl::LayerEvictionTileIterator::IsCorrectType(
1554 PictureLayerTiling::TilingEvictionTileIterator* it) const {
1555 return it->get_type() == iteration_stage_ &&
1556 (**it)->required_for_activation() == required_for_activation_;