Change three SPDY tests to use port 8080 instead of 443.
[chromium-blink-merge.git] / cc / layers / picture_layer_impl.cc
blobeb90e955d9e699b613001cf3552de27af76fdc81
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"
7 #include <algorithm>
8 #include <limits>
9 #include <set>
11 #include "base/debug/trace_event_argument.h"
12 #include "base/time/time.h"
13 #include "cc/base/math_util.h"
14 #include "cc/base/util.h"
15 #include "cc/debug/debug_colors.h"
16 #include "cc/debug/micro_benchmark_impl.h"
17 #include "cc/debug/traced_value.h"
18 #include "cc/layers/append_quads_data.h"
19 #include "cc/layers/solid_color_layer_impl.h"
20 #include "cc/output/begin_frame_args.h"
21 #include "cc/quads/checkerboard_draw_quad.h"
22 #include "cc/quads/debug_border_draw_quad.h"
23 #include "cc/quads/picture_draw_quad.h"
24 #include "cc/quads/solid_color_draw_quad.h"
25 #include "cc/quads/tile_draw_quad.h"
26 #include "cc/resources/tile_manager.h"
27 #include "cc/trees/layer_tree_impl.h"
28 #include "cc/trees/occlusion.h"
29 #include "ui/gfx/geometry/quad_f.h"
30 #include "ui/gfx/geometry/rect_conversions.h"
31 #include "ui/gfx/geometry/size_conversions.h"
33 namespace {
34 // This must be > 1 as we multiply or divide by this to find a new raster
35 // scale during pinch.
36 const float kMaxScaleRatioDuringPinch = 2.0f;
38 // When creating a new tiling during pinch, snap to an existing
39 // tiling's scale if the desired scale is within this ratio.
40 const float kSnapToExistingTilingRatio = 1.2f;
42 // Estimate skewport 60 frames ahead for pre-rasterization on the CPU.
43 const float kCpuSkewportTargetTimeInFrames = 60.0f;
45 // Don't pre-rasterize on the GPU (except for kBackflingGuardDistancePixels in
46 // TileManager::BinFromTilePriority).
47 const float kGpuSkewportTargetTimeInFrames = 0.0f;
49 // Even for really wide viewports, at some point GPU raster should use
50 // less than 4 tiles to fill the viewport. This is set to 256 as a
51 // sane minimum for now, but we might want to tune this for low-end.
52 const int kMinHeightForGpuRasteredTile = 256;
54 // When making odd-sized tiles, round them up to increase the chances
55 // of using the same tile size.
56 const int kTileRoundUp = 64;
58 } // namespace
60 namespace cc {
62 PictureLayerImpl::Pair::Pair() : active(nullptr), pending(nullptr) {
65 PictureLayerImpl::Pair::Pair(PictureLayerImpl* active_layer,
66 PictureLayerImpl* pending_layer)
67 : active(active_layer), pending(pending_layer) {
70 PictureLayerImpl::Pair::~Pair() {
73 PictureLayerImpl::PictureLayerImpl(LayerTreeImpl* tree_impl, int id)
74 : LayerImpl(tree_impl, id),
75 twin_layer_(nullptr),
76 raster_source_(PicturePileImpl::Create()),
77 ideal_page_scale_(0.f),
78 ideal_device_scale_(0.f),
79 ideal_source_scale_(0.f),
80 ideal_contents_scale_(0.f),
81 raster_page_scale_(0.f),
82 raster_device_scale_(0.f),
83 raster_source_scale_(0.f),
84 raster_contents_scale_(0.f),
85 low_res_raster_contents_scale_(0.f),
86 raster_source_scale_is_fixed_(false),
87 was_screen_space_transform_animating_(false),
88 needs_post_commit_initialization_(true),
89 should_update_tile_priorities_(false),
90 only_used_low_res_last_append_quads_(false),
91 is_mask_(false) {
92 layer_tree_impl()->RegisterPictureLayerImpl(this);
95 PictureLayerImpl::~PictureLayerImpl() {
96 if (twin_layer_)
97 twin_layer_->twin_layer_ = nullptr;
98 layer_tree_impl()->UnregisterPictureLayerImpl(this);
101 scoped_ptr<TilingSetEvictionQueue> PictureLayerImpl::CreateEvictionQueue(
102 TreePriority tree_priority) {
103 if (!tilings_)
104 return make_scoped_ptr(new TilingSetEvictionQueue());
105 return make_scoped_ptr(
106 new TilingSetEvictionQueue(tilings_.get(), tree_priority));
109 const char* PictureLayerImpl::LayerTypeAsString() const {
110 return "cc::PictureLayerImpl";
113 scoped_ptr<LayerImpl> PictureLayerImpl::CreateLayerImpl(
114 LayerTreeImpl* tree_impl) {
115 return PictureLayerImpl::Create(tree_impl, id());
118 void PictureLayerImpl::PushPropertiesTo(LayerImpl* base_layer) {
119 // It's possible this layer was never drawn or updated (e.g. because it was
120 // a descendant of an opacity 0 layer).
121 DoPostCommitInitializationIfNeeded();
122 PictureLayerImpl* layer_impl = static_cast<PictureLayerImpl*>(base_layer);
124 LayerImpl::PushPropertiesTo(base_layer);
126 // Twin relationships should never change once established.
127 DCHECK_IMPLIES(twin_layer_, twin_layer_ == layer_impl);
128 DCHECK_IMPLIES(twin_layer_, layer_impl->twin_layer_ == this);
129 // The twin relationship does not need to exist before the first
130 // PushPropertiesTo from pending to active layer since before that the active
131 // layer can not have a pile or tilings, it has only been created and inserted
132 // into the tree at that point.
133 twin_layer_ = layer_impl;
134 layer_impl->twin_layer_ = this;
136 layer_impl->set_is_mask(is_mask_);
137 layer_impl->UpdateRasterSource(raster_source_);
139 DCHECK_IMPLIES(raster_source_->IsSolidColor(), tilings_->num_tilings() == 0);
140 // Tilings would be expensive to push, so we swap.
141 layer_impl->tilings_.swap(tilings_);
142 layer_impl->tilings_->SetClient(layer_impl);
143 if (tilings_)
144 tilings_->SetClient(this);
146 // Ensure that the recycle tree doesn't have any unshared tiles.
147 if (tilings_ && raster_source_->IsSolidColor())
148 tilings_->RemoveAllTilings();
150 // Remove invalidated tiles from what will become a recycle tree.
151 if (tilings_)
152 tilings_->RemoveTilesInRegion(invalidation_);
154 layer_impl->raster_page_scale_ = raster_page_scale_;
155 layer_impl->raster_device_scale_ = raster_device_scale_;
156 layer_impl->raster_source_scale_ = raster_source_scale_;
157 layer_impl->raster_contents_scale_ = raster_contents_scale_;
158 layer_impl->low_res_raster_contents_scale_ = low_res_raster_contents_scale_;
159 layer_impl->needs_post_commit_initialization_ = false;
161 // The invalidation on this soon-to-be-recycled layer must be cleared to
162 // mirror clearing the invalidation in PictureLayer's version of this function
163 // in case push properties is skipped.
164 layer_impl->invalidation_.Swap(&invalidation_);
165 invalidation_.Clear();
166 needs_post_commit_initialization_ = true;
168 // We always need to push properties.
169 // See http://crbug.com/303943
170 needs_push_properties_ = true;
173 void PictureLayerImpl::UpdateRasterSource(
174 scoped_refptr<RasterSource> raster_source) {
175 bool could_have_tilings = CanHaveTilings();
176 raster_source_.swap(raster_source);
178 // Need to call UpdateTiles again if CanHaveTilings changed.
179 if (could_have_tilings != CanHaveTilings()) {
180 layer_tree_impl()->set_needs_update_draw_properties();
184 void PictureLayerImpl::AppendQuads(RenderPass* render_pass,
185 const Occlusion& occlusion_in_content_space,
186 AppendQuadsData* append_quads_data) {
187 DCHECK(!needs_post_commit_initialization_);
188 // The bounds and the pile size may differ if the pile wasn't updated (ie.
189 // PictureLayer::Update didn't happen). In that case the pile will be empty.
190 DCHECK_IMPLIES(!raster_source_->GetSize().IsEmpty(),
191 bounds() == raster_source_->GetSize())
192 << " bounds " << bounds().ToString() << " pile "
193 << raster_source_->GetSize().ToString();
195 SharedQuadState* shared_quad_state =
196 render_pass->CreateAndAppendSharedQuadState();
198 if (raster_source_->IsSolidColor()) {
199 PopulateSharedQuadState(shared_quad_state);
201 AppendDebugBorderQuad(
202 render_pass, bounds(), shared_quad_state, append_quads_data);
204 SolidColorLayerImpl::AppendSolidQuads(
205 render_pass, occlusion_in_content_space, shared_quad_state,
206 visible_content_rect(), raster_source_->GetSolidColor(),
207 append_quads_data);
208 return;
211 float max_contents_scale = MaximumTilingContentsScale();
212 gfx::Transform scaled_draw_transform = draw_transform();
213 scaled_draw_transform.Scale(SK_MScalar1 / max_contents_scale,
214 SK_MScalar1 / max_contents_scale);
215 gfx::Size scaled_content_bounds =
216 gfx::ToCeiledSize(gfx::ScaleSize(bounds(), max_contents_scale));
217 gfx::Rect scaled_visible_content_rect =
218 gfx::ScaleToEnclosingRect(visible_content_rect(), max_contents_scale);
219 scaled_visible_content_rect.Intersect(gfx::Rect(scaled_content_bounds));
220 Occlusion scaled_occlusion =
221 occlusion_in_content_space.GetOcclusionWithGivenDrawTransform(
222 scaled_draw_transform);
224 shared_quad_state->SetAll(
225 scaled_draw_transform, scaled_content_bounds, scaled_visible_content_rect,
226 draw_properties().clip_rect, draw_properties().is_clipped,
227 draw_properties().opacity, draw_properties().blend_mode,
228 sorting_context_id_);
230 if (current_draw_mode_ == DRAW_MODE_RESOURCELESS_SOFTWARE) {
231 AppendDebugBorderQuad(
232 render_pass,
233 scaled_content_bounds,
234 shared_quad_state,
235 append_quads_data,
236 DebugColors::DirectPictureBorderColor(),
237 DebugColors::DirectPictureBorderWidth(layer_tree_impl()));
239 gfx::Rect geometry_rect = scaled_visible_content_rect;
240 gfx::Rect opaque_rect = contents_opaque() ? geometry_rect : gfx::Rect();
241 gfx::Rect visible_geometry_rect =
242 scaled_occlusion.GetUnoccludedContentRect(geometry_rect);
243 if (visible_geometry_rect.IsEmpty())
244 return;
246 gfx::Size texture_size = scaled_visible_content_rect.size();
247 gfx::RectF texture_rect = gfx::RectF(texture_size);
248 gfx::Rect quad_content_rect = scaled_visible_content_rect;
250 PictureDrawQuad* quad =
251 render_pass->CreateAndAppendDrawQuad<PictureDrawQuad>();
252 quad->SetNew(shared_quad_state, geometry_rect, opaque_rect,
253 visible_geometry_rect, texture_rect, texture_size, RGBA_8888,
254 quad_content_rect, max_contents_scale, raster_source_);
255 return;
258 AppendDebugBorderQuad(
259 render_pass, scaled_content_bounds, shared_quad_state, append_quads_data);
261 if (ShowDebugBorders()) {
262 for (PictureLayerTilingSet::CoverageIterator iter(
263 tilings_.get(),
264 max_contents_scale,
265 scaled_visible_content_rect,
266 ideal_contents_scale_);
267 iter;
268 ++iter) {
269 SkColor color;
270 float width;
271 if (*iter && iter->IsReadyToDraw()) {
272 ManagedTileState::DrawInfo::Mode mode = iter->draw_info().mode();
273 if (mode == ManagedTileState::DrawInfo::SOLID_COLOR_MODE) {
274 color = DebugColors::SolidColorTileBorderColor();
275 width = DebugColors::SolidColorTileBorderWidth(layer_tree_impl());
276 } else if (mode == ManagedTileState::DrawInfo::PICTURE_PILE_MODE) {
277 color = DebugColors::PictureTileBorderColor();
278 width = DebugColors::PictureTileBorderWidth(layer_tree_impl());
279 } else if (iter.resolution() == HIGH_RESOLUTION) {
280 color = DebugColors::HighResTileBorderColor();
281 width = DebugColors::HighResTileBorderWidth(layer_tree_impl());
282 } else if (iter.resolution() == LOW_RESOLUTION) {
283 color = DebugColors::LowResTileBorderColor();
284 width = DebugColors::LowResTileBorderWidth(layer_tree_impl());
285 } else if (iter->contents_scale() > max_contents_scale) {
286 color = DebugColors::ExtraHighResTileBorderColor();
287 width = DebugColors::ExtraHighResTileBorderWidth(layer_tree_impl());
288 } else {
289 color = DebugColors::ExtraLowResTileBorderColor();
290 width = DebugColors::ExtraLowResTileBorderWidth(layer_tree_impl());
292 } else {
293 color = DebugColors::MissingTileBorderColor();
294 width = DebugColors::MissingTileBorderWidth(layer_tree_impl());
297 DebugBorderDrawQuad* debug_border_quad =
298 render_pass->CreateAndAppendDrawQuad<DebugBorderDrawQuad>();
299 gfx::Rect geometry_rect = iter.geometry_rect();
300 gfx::Rect visible_geometry_rect = geometry_rect;
301 debug_border_quad->SetNew(shared_quad_state,
302 geometry_rect,
303 visible_geometry_rect,
304 color,
305 width);
309 // Keep track of the tilings that were used so that tilings that are
310 // unused can be considered for removal.
311 std::vector<PictureLayerTiling*> seen_tilings;
313 // Ignore missing tiles outside of viewport for tile priority. This is
314 // normally the same as draw viewport but can be independently overridden by
315 // embedders like Android WebView with SetExternalDrawConstraints.
316 gfx::Rect scaled_viewport_for_tile_priority = gfx::ScaleToEnclosingRect(
317 GetViewportForTilePriorityInContentSpace(), max_contents_scale);
319 size_t missing_tile_count = 0u;
320 size_t on_demand_missing_tile_count = 0u;
321 only_used_low_res_last_append_quads_ = true;
322 for (PictureLayerTilingSet::CoverageIterator iter(tilings_.get(),
323 max_contents_scale,
324 scaled_visible_content_rect,
325 ideal_contents_scale_);
326 iter;
327 ++iter) {
328 gfx::Rect geometry_rect = iter.geometry_rect();
329 gfx::Rect opaque_rect = contents_opaque() ? geometry_rect : gfx::Rect();
330 gfx::Rect visible_geometry_rect =
331 scaled_occlusion.GetUnoccludedContentRect(geometry_rect);
332 if (visible_geometry_rect.IsEmpty())
333 continue;
335 append_quads_data->visible_content_area +=
336 visible_geometry_rect.width() * visible_geometry_rect.height();
338 bool has_draw_quad = false;
339 if (*iter && iter->IsReadyToDraw()) {
340 const ManagedTileState::DrawInfo& draw_info = iter->draw_info();
341 switch (draw_info.mode()) {
342 case ManagedTileState::DrawInfo::RESOURCE_MODE: {
343 gfx::RectF texture_rect = iter.texture_rect();
345 // The raster_contents_scale_ is the best scale that the layer is
346 // trying to produce, even though it may not be ideal. Since that's
347 // the best the layer can promise in the future, consider those as
348 // complete. But if a tile is ideal scale, we don't want to consider
349 // it incomplete and trying to replace it with a tile at a worse
350 // scale.
351 if (iter->contents_scale() != raster_contents_scale_ &&
352 iter->contents_scale() != ideal_contents_scale_ &&
353 geometry_rect.Intersects(scaled_viewport_for_tile_priority)) {
354 append_quads_data->num_incomplete_tiles++;
357 TileDrawQuad* quad =
358 render_pass->CreateAndAppendDrawQuad<TileDrawQuad>();
359 quad->SetNew(shared_quad_state,
360 geometry_rect,
361 opaque_rect,
362 visible_geometry_rect,
363 draw_info.get_resource_id(),
364 texture_rect,
365 iter.texture_size(),
366 draw_info.contents_swizzled());
367 has_draw_quad = true;
368 break;
370 case ManagedTileState::DrawInfo::PICTURE_PILE_MODE: {
371 if (!layer_tree_impl()
372 ->GetRendererCapabilities()
373 .allow_rasterize_on_demand) {
374 ++on_demand_missing_tile_count;
375 break;
378 gfx::RectF texture_rect = iter.texture_rect();
380 ResourceProvider* resource_provider =
381 layer_tree_impl()->resource_provider();
382 ResourceFormat format =
383 resource_provider->memory_efficient_texture_format();
384 PictureDrawQuad* quad =
385 render_pass->CreateAndAppendDrawQuad<PictureDrawQuad>();
386 quad->SetNew(shared_quad_state, geometry_rect, opaque_rect,
387 visible_geometry_rect, texture_rect, iter.texture_size(),
388 format, iter->content_rect(), iter->contents_scale(),
389 raster_source_);
390 has_draw_quad = true;
391 break;
393 case ManagedTileState::DrawInfo::SOLID_COLOR_MODE: {
394 SolidColorDrawQuad* quad =
395 render_pass->CreateAndAppendDrawQuad<SolidColorDrawQuad>();
396 quad->SetNew(shared_quad_state,
397 geometry_rect,
398 visible_geometry_rect,
399 draw_info.get_solid_color(),
400 false);
401 has_draw_quad = true;
402 break;
407 if (!has_draw_quad) {
408 if (draw_checkerboard_for_missing_tiles()) {
409 CheckerboardDrawQuad* quad =
410 render_pass->CreateAndAppendDrawQuad<CheckerboardDrawQuad>();
411 SkColor color = DebugColors::DefaultCheckerboardColor();
412 quad->SetNew(
413 shared_quad_state, geometry_rect, visible_geometry_rect, color);
414 } else {
415 SkColor color = SafeOpaqueBackgroundColor();
416 SolidColorDrawQuad* quad =
417 render_pass->CreateAndAppendDrawQuad<SolidColorDrawQuad>();
418 quad->SetNew(shared_quad_state,
419 geometry_rect,
420 visible_geometry_rect,
421 color,
422 false);
425 if (geometry_rect.Intersects(scaled_viewport_for_tile_priority)) {
426 append_quads_data->num_missing_tiles++;
427 ++missing_tile_count;
429 append_quads_data->approximated_visible_content_area +=
430 visible_geometry_rect.width() * visible_geometry_rect.height();
431 continue;
434 if (iter.resolution() != HIGH_RESOLUTION) {
435 append_quads_data->approximated_visible_content_area +=
436 visible_geometry_rect.width() * visible_geometry_rect.height();
439 // If we have a draw quad, but it's not low resolution, then
440 // mark that we've used something other than low res to draw.
441 if (iter.resolution() != LOW_RESOLUTION)
442 only_used_low_res_last_append_quads_ = false;
444 if (seen_tilings.empty() || seen_tilings.back() != iter.CurrentTiling())
445 seen_tilings.push_back(iter.CurrentTiling());
448 if (missing_tile_count) {
449 TRACE_EVENT_INSTANT2("cc",
450 "PictureLayerImpl::AppendQuads checkerboard",
451 TRACE_EVENT_SCOPE_THREAD,
452 "missing_tile_count",
453 missing_tile_count,
454 "on_demand_missing_tile_count",
455 on_demand_missing_tile_count);
458 // Aggressively remove any tilings that are not seen to save memory. Note
459 // that this is at the expense of doing cause more frequent re-painting. A
460 // better scheme would be to maintain a tighter visible_content_rect for the
461 // finer tilings.
462 CleanUpTilingsOnActiveLayer(seen_tilings);
465 void PictureLayerImpl::UpdateTiles(const Occlusion& occlusion_in_content_space,
466 bool resourceless_software_draw) {
467 DCHECK_EQ(1.f, contents_scale_x());
468 DCHECK_EQ(1.f, contents_scale_y());
470 DoPostCommitInitializationIfNeeded();
472 if (!resourceless_software_draw) {
473 visible_rect_for_tile_priority_ = visible_content_rect();
476 if (!CanHaveTilings()) {
477 ideal_page_scale_ = 0.f;
478 ideal_device_scale_ = 0.f;
479 ideal_contents_scale_ = 0.f;
480 ideal_source_scale_ = 0.f;
481 SanityCheckTilingState();
482 return;
485 UpdateIdealScales();
487 DCHECK_IMPLIES(tilings_->num_tilings() == 0, raster_contents_scale_ == 0.f)
488 << "A layer with no tilings shouldn't have valid raster scales";
489 if (!raster_contents_scale_ || ShouldAdjustRasterScale()) {
490 RecalculateRasterScales();
491 AddTilingsForRasterScale();
494 DCHECK(raster_page_scale_);
495 DCHECK(raster_device_scale_);
496 DCHECK(raster_source_scale_);
497 DCHECK(raster_contents_scale_);
498 DCHECK(low_res_raster_contents_scale_);
500 was_screen_space_transform_animating_ =
501 draw_properties().screen_space_transform_is_animating;
503 if (draw_transform_is_animating())
504 raster_source_->SetShouldAttemptToUseDistanceFieldText();
506 should_update_tile_priorities_ = true;
508 UpdateTilePriorities(occlusion_in_content_space);
511 void PictureLayerImpl::UpdateTilePriorities(
512 const Occlusion& occlusion_in_content_space) {
513 DCHECK_IMPLIES(raster_source_->IsSolidColor(), tilings_->num_tilings() == 0);
515 double current_frame_time_in_seconds =
516 (layer_tree_impl()->CurrentBeginFrameArgs().frame_time -
517 base::TimeTicks()).InSecondsF();
518 gfx::Rect viewport_rect_in_layer_space =
519 GetViewportForTilePriorityInContentSpace();
521 // The tiling set can require tiles for activation any of the following
522 // conditions are true:
523 // - This layer produced a high-res or non-ideal-res tile last frame.
524 // - We're in requires high res to draw mode.
525 // - We're not in smoothness takes priority mode.
526 // To put different, the tiling set can't require tiles for activation if
527 // we're in smoothness mode and only used low-res or checkerboard to draw last
528 // frame and we don't need high res to draw.
530 // The reason for this is that we should be able to activate sooner and get a
531 // more up to date recording, so we don't run out of recording on the active
532 // tree.
533 bool can_require_tiles_for_activation =
534 !only_used_low_res_last_append_quads_ || RequiresHighResToDraw() ||
535 !layer_tree_impl()->SmoothnessTakesPriority();
537 // Pass |occlusion_in_content_space| for |occlusion_in_layer_space| since
538 // they are the same space in picture layer, as contents scale is always 1.
539 bool updated = tilings_->UpdateTilePriorities(
540 viewport_rect_in_layer_space, ideal_contents_scale_,
541 current_frame_time_in_seconds, occlusion_in_content_space,
542 can_require_tiles_for_activation);
544 // TODO(vmpstr): See if this can be removed in favour of calling it from LTHI
545 if (updated)
546 layer_tree_impl()->DidModifyTilePriorities();
549 gfx::Rect PictureLayerImpl::GetViewportForTilePriorityInContentSpace() const {
550 // If visible_rect_for_tile_priority_ is empty or
551 // viewport_rect_for_tile_priority is set to be different from the device
552 // viewport, try to inverse project the viewport into layer space and use
553 // that. Otherwise just use visible_rect_for_tile_priority_
554 gfx::Rect visible_rect_in_content_space = visible_rect_for_tile_priority_;
555 gfx::Rect viewport_rect_for_tile_priority =
556 layer_tree_impl()->ViewportRectForTilePriority();
558 if (visible_rect_in_content_space.IsEmpty() ||
559 layer_tree_impl()->DeviceViewport() != viewport_rect_for_tile_priority) {
560 gfx::Transform view_to_layer(gfx::Transform::kSkipInitialization);
561 if (screen_space_transform().GetInverse(&view_to_layer)) {
562 // Transform from view space to content space.
563 visible_rect_in_content_space =
564 gfx::ToEnclosingRect(MathUtil::ProjectClippedRect(
565 view_to_layer, viewport_rect_for_tile_priority));
568 return visible_rect_in_content_space;
571 PictureLayerImpl* PictureLayerImpl::GetPendingOrActiveTwinLayer() const {
572 if (!twin_layer_ || !twin_layer_->IsOnActiveOrPendingTree())
573 return nullptr;
574 return twin_layer_;
577 PictureLayerImpl* PictureLayerImpl::GetRecycledTwinLayer() const {
578 if (!twin_layer_ || twin_layer_->IsOnActiveOrPendingTree())
579 return nullptr;
580 return twin_layer_;
583 void PictureLayerImpl::NotifyTileStateChanged(const Tile* tile) {
584 if (layer_tree_impl()->IsActiveTree()) {
585 gfx::RectF layer_damage_rect =
586 gfx::ScaleRect(tile->content_rect(), 1.f / tile->contents_scale());
587 AddDamageRect(layer_damage_rect);
591 void PictureLayerImpl::DidBecomeActive() {
592 LayerImpl::DidBecomeActive();
593 // TODO(vmpstr): See if this can be removed in favour of calling it from LTHI
594 layer_tree_impl()->DidModifyTilePriorities();
597 void PictureLayerImpl::DidBeginTracing() {
598 raster_source_->DidBeginTracing();
601 void PictureLayerImpl::ReleaseResources() {
602 if (tilings_)
603 RemoveAllTilings();
605 ResetRasterScale();
607 // To avoid an edge case after lost context where the tree is up to date but
608 // the tilings have not been managed, request an update draw properties
609 // to force tilings to get managed.
610 layer_tree_impl()->set_needs_update_draw_properties();
613 skia::RefPtr<SkPicture> PictureLayerImpl::GetPicture() {
614 return raster_source_->GetFlattenedPicture();
617 scoped_refptr<Tile> PictureLayerImpl::CreateTile(PictureLayerTiling* tiling,
618 const gfx::Rect& content_rect) {
619 DCHECK(!raster_source_->IsSolidColor());
620 if (!raster_source_->CoversRect(content_rect, tiling->contents_scale()))
621 return scoped_refptr<Tile>();
623 int flags = 0;
625 // TODO(vmpstr): Revisit this. For now, enabling analysis means that we get as
626 // much savings on memory as we can. However, for some cases like ganesh or
627 // small layers, the amount of time we spend analyzing might not justify
628 // memory savings that we can get. Note that we don't handle solid color
629 // masks, so we shouldn't bother analyzing those.
630 // Bugs: crbug.com/397198, crbug.com/396908
631 if (!is_mask_)
632 flags = Tile::USE_PICTURE_ANALYSIS;
634 return layer_tree_impl()->tile_manager()->CreateTile(
635 raster_source_.get(), content_rect.size(), content_rect,
636 tiling->contents_scale(), id(), layer_tree_impl()->source_frame_number(),
637 flags);
640 const Region* PictureLayerImpl::GetPendingInvalidation() {
641 if (layer_tree_impl()->IsPendingTree())
642 return &invalidation_;
643 DCHECK(layer_tree_impl()->IsActiveTree());
644 if (PictureLayerImpl* twin_layer = GetPendingOrActiveTwinLayer())
645 return &twin_layer->invalidation_;
646 return nullptr;
649 const PictureLayerTiling* PictureLayerImpl::GetPendingOrActiveTwinTiling(
650 const PictureLayerTiling* tiling) const {
651 PictureLayerImpl* twin_layer = GetPendingOrActiveTwinLayer();
652 if (!twin_layer)
653 return nullptr;
654 // TODO(danakj): Remove this when no longer swapping tilings.
655 if (!twin_layer->tilings_)
656 return nullptr;
657 return twin_layer->tilings_->FindTilingWithScale(tiling->contents_scale());
660 PictureLayerTiling* PictureLayerImpl::GetRecycledTwinTiling(
661 const PictureLayerTiling* tiling) {
662 PictureLayerImpl* recycled_twin = GetRecycledTwinLayer();
663 if (!recycled_twin || !recycled_twin->tilings_)
664 return nullptr;
665 return recycled_twin->tilings_->FindTilingWithScale(tiling->contents_scale());
668 TilePriority::PriorityBin PictureLayerImpl::GetMaxTilePriorityBin() const {
669 if (!HasValidTilePriorities())
670 return TilePriority::EVENTUALLY;
671 return TilePriority::NOW;
674 size_t PictureLayerImpl::GetMaxTilesForInterestArea() const {
675 return layer_tree_impl()->settings().max_tiles_for_interest_area;
678 float PictureLayerImpl::GetSkewportTargetTimeInSeconds() const {
679 float skewport_target_time_in_frames =
680 layer_tree_impl()->use_gpu_rasterization()
681 ? kGpuSkewportTargetTimeInFrames
682 : kCpuSkewportTargetTimeInFrames;
683 return skewport_target_time_in_frames *
684 layer_tree_impl()->begin_impl_frame_interval().InSecondsF() *
685 layer_tree_impl()->settings().skewport_target_time_multiplier;
688 int PictureLayerImpl::GetSkewportExtrapolationLimitInContentPixels() const {
689 return layer_tree_impl()
690 ->settings()
691 .skewport_extrapolation_limit_in_content_pixels;
694 bool PictureLayerImpl::RequiresHighResToDraw() const {
695 return layer_tree_impl()->RequiresHighResToDraw();
698 gfx::Size PictureLayerImpl::CalculateTileSize(
699 const gfx::Size& content_bounds) const {
700 int max_texture_size =
701 layer_tree_impl()->resource_provider()->max_texture_size();
703 if (is_mask_) {
704 // Masks are not tiled, so if we can't cover the whole mask with one tile,
705 // don't make any tiles at all. Returning an empty size signals this.
706 if (content_bounds.width() > max_texture_size ||
707 content_bounds.height() > max_texture_size)
708 return gfx::Size();
709 return content_bounds;
712 int default_tile_width = 0;
713 int default_tile_height = 0;
714 if (layer_tree_impl()->use_gpu_rasterization()) {
715 // For GPU rasterization, we pick an ideal tile size using the viewport
716 // so we don't need any settings. The current approach uses 4 tiles
717 // to cover the viewport vertically.
718 int viewport_width = layer_tree_impl()->device_viewport_size().width();
719 int viewport_height = layer_tree_impl()->device_viewport_size().height();
720 default_tile_width = viewport_width;
721 // Also, increase the height proportionally as the width decreases, and
722 // pad by our border texels to make the tiles exactly match the viewport.
723 int divisor = 4;
724 if (content_bounds.width() <= viewport_width / 2)
725 divisor = 2;
726 if (content_bounds.width() <= viewport_width / 4)
727 divisor = 1;
728 default_tile_height = RoundUp(viewport_height, divisor) / divisor;
729 default_tile_height += 2 * PictureLayerTiling::kBorderTexels;
730 default_tile_height =
731 std::max(default_tile_height, kMinHeightForGpuRasteredTile);
732 } else {
733 // For CPU rasterization we use tile-size settings.
734 const LayerTreeSettings& settings = layer_tree_impl()->settings();
735 int max_untiled_content_width = settings.max_untiled_layer_size.width();
736 int max_untiled_content_height = settings.max_untiled_layer_size.height();
737 default_tile_width = settings.default_tile_size.width();
738 default_tile_height = settings.default_tile_size.height();
740 // If the content width is small, increase tile size vertically.
741 // If the content height is small, increase tile size horizontally.
742 // If both are less than the untiled-size, use a single tile.
743 if (content_bounds.width() < default_tile_width)
744 default_tile_height = max_untiled_content_height;
745 if (content_bounds.height() < default_tile_height)
746 default_tile_width = max_untiled_content_width;
747 if (content_bounds.width() < max_untiled_content_width &&
748 content_bounds.height() < max_untiled_content_height) {
749 default_tile_height = max_untiled_content_height;
750 default_tile_width = max_untiled_content_width;
754 int tile_width = default_tile_width;
755 int tile_height = default_tile_height;
757 // Clamp the tile width/height to the content width/height to save space.
758 if (content_bounds.width() < default_tile_width) {
759 tile_width = std::min(tile_width, content_bounds.width());
760 tile_width = RoundUp(tile_width, kTileRoundUp);
761 tile_width = std::min(tile_width, default_tile_width);
763 if (content_bounds.height() < default_tile_height) {
764 tile_height = std::min(tile_height, content_bounds.height());
765 tile_height = RoundUp(tile_height, kTileRoundUp);
766 tile_height = std::min(tile_height, default_tile_height);
769 // Under no circumstance should we be larger than the max texture size.
770 tile_width = std::min(tile_width, max_texture_size);
771 tile_height = std::min(tile_height, max_texture_size);
772 return gfx::Size(tile_width, tile_height);
775 void PictureLayerImpl::SyncFromActiveLayer(const PictureLayerImpl* other) {
776 DCHECK(!other->needs_post_commit_initialization_);
777 DCHECK(other->tilings_);
779 if (!DrawsContent()) {
780 RemoveAllTilings();
781 return;
784 raster_page_scale_ = other->raster_page_scale_;
785 raster_device_scale_ = other->raster_device_scale_;
786 raster_source_scale_ = other->raster_source_scale_;
787 raster_contents_scale_ = other->raster_contents_scale_;
788 low_res_raster_contents_scale_ = other->low_res_raster_contents_scale_;
790 bool synced_high_res_tiling = false;
791 if (CanHaveTilings()) {
792 synced_high_res_tiling = tilings_->SyncTilings(
793 *other->tilings_, raster_source_->GetSize(), invalidation_,
794 MinimumContentsScale(), raster_source_.get());
795 } else {
796 RemoveAllTilings();
799 // If our MinimumContentsScale has changed to prevent the twin's high res
800 // tiling from being synced, we should reset the raster scale and let it be
801 // recalculated (1) again. This can happen if our bounds shrink to the point
802 // where min contents scale grows.
803 // (1) - TODO(vmpstr) Instead of hoping that this will be recalculated, we
804 // should refactor this code a little bit and actually recalculate this.
805 // However, this is a larger undertaking, so this will work for now.
806 if (!synced_high_res_tiling)
807 ResetRasterScale();
808 else
809 SanityCheckTilingState();
812 void PictureLayerImpl::SyncTiling(
813 const PictureLayerTiling* tiling) {
814 if (!tilings_)
815 return;
816 if (!CanHaveTilingWithScale(tiling->contents_scale()))
817 return;
818 tilings_->AddTiling(tiling->contents_scale(), raster_source_->GetSize());
820 // If this tree needs update draw properties, then the tiling will
821 // get updated prior to drawing or activation. If this tree does not
822 // need update draw properties, then its transforms are up to date and
823 // we can create tiles for this tiling immediately.
824 if (!layer_tree_impl()->needs_update_draw_properties() &&
825 should_update_tile_priorities_) {
826 // TODO(danakj): Add a DCHECK() that we are not using occlusion tracking
827 // when we stop using the pending tree in the browser compositor. If we want
828 // to support occlusion tracking here, we need to dirty the draw properties
829 // or save occlusion as a draw property.
830 UpdateTilePriorities(Occlusion());
834 void PictureLayerImpl::GetContentsResourceId(
835 ResourceProvider::ResourceId* resource_id,
836 gfx::Size* resource_size) const {
837 DCHECK_EQ(bounds().ToString(), raster_source_->GetSize().ToString());
838 gfx::Rect content_rect(bounds());
839 PictureLayerTilingSet::CoverageIterator iter(
840 tilings_.get(), 1.f, content_rect, ideal_contents_scale_);
842 // Mask resource not ready yet.
843 if (!iter || !*iter) {
844 *resource_id = 0;
845 return;
848 // Masks only supported if they fit on exactly one tile.
849 DCHECK(iter.geometry_rect() == content_rect)
850 << "iter rect " << iter.geometry_rect().ToString() << " content rect "
851 << content_rect.ToString();
853 const ManagedTileState::DrawInfo& draw_info = iter->draw_info();
854 if (!draw_info.IsReadyToDraw() ||
855 draw_info.mode() != ManagedTileState::DrawInfo::RESOURCE_MODE) {
856 *resource_id = 0;
857 return;
860 *resource_id = draw_info.get_resource_id();
861 *resource_size = iter.texture_size();
864 void PictureLayerImpl::DoPostCommitInitialization() {
865 DCHECK(needs_post_commit_initialization_);
866 DCHECK(layer_tree_impl()->IsPendingTree());
868 if (!tilings_)
869 tilings_ = PictureLayerTilingSet::Create(this);
871 PictureLayerImpl* twin_layer = GetPendingOrActiveTwinLayer();
872 if (twin_layer) {
873 // If the twin has never been pushed to, do not sync from it.
874 // This can happen if this function is called during activation.
875 if (!twin_layer->needs_post_commit_initialization_)
876 SyncFromActiveLayer(twin_layer);
879 needs_post_commit_initialization_ = false;
882 PictureLayerTiling* PictureLayerImpl::AddTiling(float contents_scale) {
883 DCHECK(CanHaveTilingWithScale(contents_scale)) <<
884 "contents_scale: " << contents_scale;
886 PictureLayerTiling* tiling =
887 tilings_->AddTiling(contents_scale, raster_source_->GetSize());
889 DCHECK(raster_source_->HasRecordings());
891 if (PictureLayerImpl* twin_layer = GetPendingOrActiveTwinLayer())
892 twin_layer->SyncTiling(tiling);
894 return tiling;
897 void PictureLayerImpl::RemoveAllTilings() {
898 if (tilings_)
899 tilings_->RemoveAllTilings();
900 // If there are no tilings, then raster scales are no longer meaningful.
901 ResetRasterScale();
904 void PictureLayerImpl::AddTilingsForRasterScale() {
905 // Reset all resolution enums on tilings, we'll be setting new values in this
906 // function.
907 tilings_->MarkAllTilingsNonIdeal();
909 PictureLayerTiling* high_res =
910 tilings_->FindTilingWithScale(raster_contents_scale_);
911 // We always need a high res tiling, so create one if it doesn't exist.
912 if (!high_res)
913 high_res = AddTiling(raster_contents_scale_);
915 // Try and find a low res tiling.
916 PictureLayerTiling* low_res = nullptr;
917 if (raster_contents_scale_ == low_res_raster_contents_scale_)
918 low_res = high_res;
919 else
920 low_res = tilings_->FindTilingWithScale(low_res_raster_contents_scale_);
922 // Only create new low res tilings when the transform is static. This
923 // prevents wastefully creating a paired low res tiling for every new high res
924 // tiling during a pinch or a CSS animation.
925 bool can_have_low_res = layer_tree_impl()->create_low_res_tiling();
926 bool needs_low_res = !low_res;
927 bool is_pinching = layer_tree_impl()->PinchGestureActive();
928 bool is_animating = draw_properties().screen_space_transform_is_animating;
929 if (can_have_low_res && needs_low_res && !is_pinching && !is_animating)
930 low_res = AddTiling(low_res_raster_contents_scale_);
932 // Set low-res if we have one.
933 if (low_res && low_res != high_res)
934 low_res->set_resolution(LOW_RESOLUTION);
936 // Make sure we always have one high-res (even if high == low).
937 high_res->set_resolution(HIGH_RESOLUTION);
939 SanityCheckTilingState();
942 bool PictureLayerImpl::ShouldAdjustRasterScale() const {
943 if (was_screen_space_transform_animating_ !=
944 draw_properties().screen_space_transform_is_animating)
945 return true;
947 if (draw_properties().screen_space_transform_is_animating &&
948 raster_contents_scale_ != ideal_contents_scale_ &&
949 ShouldAdjustRasterScaleDuringScaleAnimations())
950 return true;
952 bool is_pinching = layer_tree_impl()->PinchGestureActive();
953 if (is_pinching && raster_page_scale_) {
954 // We change our raster scale when it is:
955 // - Higher than ideal (need a lower-res tiling available)
956 // - Too far from ideal (need a higher-res tiling available)
957 float ratio = ideal_page_scale_ / raster_page_scale_;
958 if (raster_page_scale_ > ideal_page_scale_ ||
959 ratio > kMaxScaleRatioDuringPinch)
960 return true;
963 if (!is_pinching) {
964 // When not pinching, match the ideal page scale factor.
965 if (raster_page_scale_ != ideal_page_scale_)
966 return true;
969 // Always match the ideal device scale factor.
970 if (raster_device_scale_ != ideal_device_scale_)
971 return true;
973 // When the source scale changes we want to match it, but not when animating
974 // or when we've fixed the scale in place.
975 if (!draw_properties().screen_space_transform_is_animating &&
976 !raster_source_scale_is_fixed_ &&
977 raster_source_scale_ != ideal_source_scale_)
978 return true;
980 return false;
983 void PictureLayerImpl::RecalculateRasterScales() {
984 float old_raster_contents_scale = raster_contents_scale_;
985 float old_raster_page_scale = raster_page_scale_;
986 float old_raster_source_scale = raster_source_scale_;
988 raster_device_scale_ = ideal_device_scale_;
989 raster_page_scale_ = ideal_page_scale_;
990 raster_source_scale_ = ideal_source_scale_;
991 raster_contents_scale_ = ideal_contents_scale_;
993 // If we're not animating, or leaving an animation, and the
994 // ideal_source_scale_ changes, then things are unpredictable, and we fix
995 // the raster_source_scale_ in place.
996 if (old_raster_source_scale &&
997 !draw_properties().screen_space_transform_is_animating &&
998 !was_screen_space_transform_animating_ &&
999 old_raster_source_scale != ideal_source_scale_)
1000 raster_source_scale_is_fixed_ = true;
1002 // TODO(danakj): Adjust raster source scale closer to ideal source scale at
1003 // a throttled rate. Possibly make use of invalidation_.IsEmpty() on pending
1004 // tree. This will allow CSS scale changes to get re-rastered at an
1005 // appropriate rate. (crbug.com/413636)
1006 if (raster_source_scale_is_fixed_) {
1007 raster_contents_scale_ /= raster_source_scale_;
1008 raster_source_scale_ = 1.f;
1011 // During pinch we completely ignore the current ideal scale, and just use
1012 // a multiple of the previous scale.
1013 bool is_pinching = layer_tree_impl()->PinchGestureActive();
1014 if (is_pinching && old_raster_contents_scale) {
1015 // See ShouldAdjustRasterScale:
1016 // - When zooming out, preemptively create new tiling at lower resolution.
1017 // - When zooming in, approximate ideal using multiple of kMaxScaleRatio.
1018 bool zooming_out = old_raster_page_scale > ideal_page_scale_;
1019 float desired_contents_scale = old_raster_contents_scale;
1020 if (zooming_out) {
1021 while (desired_contents_scale > ideal_contents_scale_)
1022 desired_contents_scale /= kMaxScaleRatioDuringPinch;
1023 } else {
1024 while (desired_contents_scale < ideal_contents_scale_)
1025 desired_contents_scale *= kMaxScaleRatioDuringPinch;
1027 raster_contents_scale_ = tilings_->GetSnappedContentsScale(
1028 desired_contents_scale, kSnapToExistingTilingRatio);
1029 raster_page_scale_ =
1030 raster_contents_scale_ / raster_device_scale_ / raster_source_scale_;
1033 // If we're not re-rasterizing during animation, rasterize at the maximum
1034 // scale that will occur during the animation, if the maximum scale is
1035 // known. However we want to avoid excessive memory use. If the scale is
1036 // smaller than what we would choose otherwise, then it's always better off
1037 // for us memory-wise. But otherwise, we don't choose a scale at which this
1038 // layer's rastered content would become larger than the viewport.
1039 if (draw_properties().screen_space_transform_is_animating &&
1040 !ShouldAdjustRasterScaleDuringScaleAnimations()) {
1041 bool can_raster_at_maximum_scale = false;
1042 // TODO(ajuma): If we need to deal with scale-down animations starting right
1043 // as a layer gets promoted, then we'd want to have the
1044 // |starting_animation_contents_scale| passed in here as a separate draw
1045 // property so we could try use that when the max is too large.
1046 // See crbug.com/422341.
1047 float maximum_scale = draw_properties().maximum_animation_contents_scale;
1048 if (maximum_scale) {
1049 gfx::Size bounds_at_maximum_scale = gfx::ToCeiledSize(
1050 gfx::ScaleSize(raster_source_->GetSize(), maximum_scale));
1051 int64 maximum_area = static_cast<int64>(bounds_at_maximum_scale.width()) *
1052 static_cast<int64>(bounds_at_maximum_scale.height());
1053 gfx::Size viewport = layer_tree_impl()->device_viewport_size();
1054 int64 viewport_area = static_cast<int64>(viewport.width()) *
1055 static_cast<int64>(viewport.height());
1056 if (maximum_area <= viewport_area)
1057 can_raster_at_maximum_scale = true;
1059 // Use the computed scales for the raster scale directly, do not try to use
1060 // the ideal scale here. The current ideal scale may be way too large in the
1061 // case of an animation with scale, and will be constantly changing.
1062 if (can_raster_at_maximum_scale)
1063 raster_contents_scale_ = maximum_scale;
1064 else
1065 raster_contents_scale_ = 1.f * ideal_page_scale_ * ideal_device_scale_;
1068 raster_contents_scale_ =
1069 std::max(raster_contents_scale_, MinimumContentsScale());
1071 // If this layer would create zero or one tiles at this content scale,
1072 // don't create a low res tiling.
1073 gfx::Size raster_bounds = gfx::ToCeiledSize(
1074 gfx::ScaleSize(raster_source_->GetSize(), raster_contents_scale_));
1075 gfx::Size tile_size = CalculateTileSize(raster_bounds);
1076 bool tile_covers_bounds = tile_size.width() >= raster_bounds.width() &&
1077 tile_size.height() >= raster_bounds.height();
1078 if (tile_size.IsEmpty() || tile_covers_bounds) {
1079 low_res_raster_contents_scale_ = raster_contents_scale_;
1080 return;
1083 float low_res_factor =
1084 layer_tree_impl()->settings().low_res_contents_scale_factor;
1085 low_res_raster_contents_scale_ = std::max(
1086 raster_contents_scale_ * low_res_factor,
1087 MinimumContentsScale());
1088 DCHECK_LE(low_res_raster_contents_scale_, raster_contents_scale_);
1089 DCHECK_GE(low_res_raster_contents_scale_, MinimumContentsScale());
1092 void PictureLayerImpl::CleanUpTilingsOnActiveLayer(
1093 std::vector<PictureLayerTiling*> used_tilings) {
1094 DCHECK(layer_tree_impl()->IsActiveTree());
1095 if (tilings_->num_tilings() == 0)
1096 return;
1098 float min_acceptable_high_res_scale = std::min(
1099 raster_contents_scale_, ideal_contents_scale_);
1100 float max_acceptable_high_res_scale = std::max(
1101 raster_contents_scale_, ideal_contents_scale_);
1103 PictureLayerImpl* twin = GetPendingOrActiveTwinLayer();
1104 if (twin && twin->CanHaveTilings()) {
1105 min_acceptable_high_res_scale = std::min(
1106 min_acceptable_high_res_scale,
1107 std::min(twin->raster_contents_scale_, twin->ideal_contents_scale_));
1108 max_acceptable_high_res_scale = std::max(
1109 max_acceptable_high_res_scale,
1110 std::max(twin->raster_contents_scale_, twin->ideal_contents_scale_));
1113 PictureLayerTilingSet* twin_set = twin ? twin->tilings_.get() : nullptr;
1114 PictureLayerImpl* recycled_twin = GetRecycledTwinLayer();
1115 PictureLayerTilingSet* recycled_twin_set =
1116 recycled_twin ? recycled_twin->tilings_.get() : nullptr;
1118 tilings_->CleanUpTilings(min_acceptable_high_res_scale,
1119 max_acceptable_high_res_scale, used_tilings,
1120 layer_tree_impl()->create_low_res_tiling(), twin_set,
1121 recycled_twin_set);
1123 if (twin_set && twin_set->num_tilings() == 0)
1124 twin->ResetRasterScale();
1126 if (recycled_twin_set && recycled_twin_set->num_tilings() == 0)
1127 recycled_twin->ResetRasterScale();
1129 DCHECK_GT(tilings_->num_tilings(), 0u);
1130 SanityCheckTilingState();
1133 float PictureLayerImpl::MinimumContentsScale() const {
1134 float setting_min = layer_tree_impl()->settings().minimum_contents_scale;
1136 // If the contents scale is less than 1 / width (also for height),
1137 // then it will end up having less than one pixel of content in that
1138 // dimension. Bump the minimum contents scale up in this case to prevent
1139 // this from happening.
1140 int min_dimension = std::min(raster_source_->GetSize().width(),
1141 raster_source_->GetSize().height());
1142 if (!min_dimension)
1143 return setting_min;
1145 return std::max(1.f / min_dimension, setting_min);
1148 void PictureLayerImpl::ResetRasterScale() {
1149 raster_page_scale_ = 0.f;
1150 raster_device_scale_ = 0.f;
1151 raster_source_scale_ = 0.f;
1152 raster_contents_scale_ = 0.f;
1153 low_res_raster_contents_scale_ = 0.f;
1154 raster_source_scale_is_fixed_ = false;
1156 // When raster scales aren't valid, don't update tile priorities until
1157 // this layer has been updated via UpdateDrawProperties.
1158 should_update_tile_priorities_ = false;
1161 bool PictureLayerImpl::CanHaveTilings() const {
1162 if (raster_source_->IsSolidColor())
1163 return false;
1164 if (!DrawsContent())
1165 return false;
1166 if (!raster_source_->HasRecordings())
1167 return false;
1168 return true;
1171 bool PictureLayerImpl::CanHaveTilingWithScale(float contents_scale) const {
1172 if (!CanHaveTilings())
1173 return false;
1174 if (contents_scale < MinimumContentsScale())
1175 return false;
1176 return true;
1179 void PictureLayerImpl::SanityCheckTilingState() const {
1180 #if DCHECK_IS_ON
1181 // Recycle tree doesn't have any restrictions.
1182 if (layer_tree_impl()->IsRecycleTree())
1183 return;
1185 if (!CanHaveTilings()) {
1186 DCHECK_EQ(0u, tilings_->num_tilings());
1187 return;
1189 if (tilings_->num_tilings() == 0)
1190 return;
1192 // We should only have one high res tiling.
1193 DCHECK_EQ(1, tilings_->NumHighResTilings());
1194 #endif
1197 bool PictureLayerImpl::ShouldAdjustRasterScaleDuringScaleAnimations() const {
1198 return layer_tree_impl()->use_gpu_rasterization();
1201 float PictureLayerImpl::MaximumTilingContentsScale() const {
1202 float max_contents_scale = tilings_->GetMaximumContentsScale();
1203 return std::max(max_contents_scale, MinimumContentsScale());
1206 void PictureLayerImpl::UpdateIdealScales() {
1207 DCHECK(CanHaveTilings());
1209 float min_contents_scale = MinimumContentsScale();
1210 DCHECK_GT(min_contents_scale, 0.f);
1211 float min_page_scale = layer_tree_impl()->min_page_scale_factor();
1212 DCHECK_GT(min_page_scale, 0.f);
1213 float min_device_scale = 1.f;
1214 float min_source_scale =
1215 min_contents_scale / min_page_scale / min_device_scale;
1217 float ideal_page_scale = draw_properties().page_scale_factor;
1218 float ideal_device_scale = draw_properties().device_scale_factor;
1219 float ideal_source_scale = draw_properties().ideal_contents_scale /
1220 ideal_page_scale / ideal_device_scale;
1221 ideal_contents_scale_ =
1222 std::max(draw_properties().ideal_contents_scale, min_contents_scale);
1223 ideal_page_scale_ = draw_properties().page_scale_factor;
1224 ideal_device_scale_ = draw_properties().device_scale_factor;
1225 ideal_source_scale_ = std::max(ideal_source_scale, min_source_scale);
1228 void PictureLayerImpl::GetDebugBorderProperties(
1229 SkColor* color,
1230 float* width) const {
1231 *color = DebugColors::TiledContentLayerBorderColor();
1232 *width = DebugColors::TiledContentLayerBorderWidth(layer_tree_impl());
1235 void PictureLayerImpl::GetAllTilesForTracing(
1236 std::set<const Tile*>* tiles) const {
1237 if (!tilings_)
1238 return;
1239 tilings_->GetAllTilesForTracing(tiles);
1242 void PictureLayerImpl::AsValueInto(base::debug::TracedValue* state) const {
1243 const_cast<PictureLayerImpl*>(this)->DoPostCommitInitializationIfNeeded();
1244 LayerImpl::AsValueInto(state);
1245 state->SetDouble("ideal_contents_scale", ideal_contents_scale_);
1246 state->SetDouble("geometry_contents_scale", MaximumTilingContentsScale());
1247 state->BeginArray("tilings");
1248 tilings_->AsValueInto(state);
1249 state->EndArray();
1251 state->BeginArray("tile_priority_rect");
1252 MathUtil::AddToTracedValue(GetViewportForTilePriorityInContentSpace(), state);
1253 state->EndArray();
1255 state->BeginArray("visible_rect");
1256 MathUtil::AddToTracedValue(visible_content_rect(), state);
1257 state->EndArray();
1259 state->BeginArray("pictures");
1260 raster_source_->AsValueInto(state);
1261 state->EndArray();
1263 state->BeginArray("invalidation");
1264 invalidation_.AsValueInto(state);
1265 state->EndArray();
1267 state->BeginArray("coverage_tiles");
1268 for (PictureLayerTilingSet::CoverageIterator iter(
1269 tilings_.get(), 1.f, gfx::Rect(raster_source_->GetSize()),
1270 ideal_contents_scale_);
1271 iter; ++iter) {
1272 state->BeginDictionary();
1274 state->BeginArray("geometry_rect");
1275 MathUtil::AddToTracedValue(iter.geometry_rect(), state);
1276 state->EndArray();
1278 if (*iter)
1279 TracedValue::SetIDRef(*iter, state, "tile");
1281 state->EndDictionary();
1283 state->EndArray();
1286 size_t PictureLayerImpl::GPUMemoryUsageInBytes() const {
1287 const_cast<PictureLayerImpl*>(this)->DoPostCommitInitializationIfNeeded();
1288 return tilings_->GPUMemoryUsageInBytes();
1291 void PictureLayerImpl::RunMicroBenchmark(MicroBenchmarkImpl* benchmark) {
1292 benchmark->RunOnLayer(this);
1295 WhichTree PictureLayerImpl::GetTree() const {
1296 return layer_tree_impl()->IsActiveTree() ? ACTIVE_TREE : PENDING_TREE;
1299 bool PictureLayerImpl::IsOnActiveOrPendingTree() const {
1300 return !layer_tree_impl()->IsRecycleTree();
1303 bool PictureLayerImpl::HasValidTilePriorities() const {
1304 return IsOnActiveOrPendingTree() && IsDrawnRenderSurfaceLayerListMember();
1307 bool PictureLayerImpl::AllTilesRequiredAreReadyToDraw(
1308 TileRequirementCheck is_tile_required_callback) const {
1309 if (!HasValidTilePriorities())
1310 return true;
1312 if (!tilings_)
1313 return true;
1315 if (visible_rect_for_tile_priority_.IsEmpty())
1316 return true;
1318 gfx::Rect rect = GetViewportForTilePriorityInContentSpace();
1319 rect.Intersect(visible_rect_for_tile_priority_);
1321 // The high resolution tiling is the only tiling that can mark tiles as
1322 // requiring either draw or activation. There is an explicit check in those
1323 // callbacks to return false if they are not high resolution tilings. This
1324 // check needs to remain since there are other callers of that function that
1325 // rely on it. However, for the purposes of this function, we don't have to
1326 // check other tilings.
1327 PictureLayerTiling* tiling =
1328 tilings_->FindTilingWithResolution(HIGH_RESOLUTION);
1329 if (!tiling)
1330 return true;
1332 for (PictureLayerTiling::CoverageIterator iter(tiling, 1.f, rect); iter;
1333 ++iter) {
1334 const Tile* tile = *iter;
1335 // A null tile (i.e. missing recording) can just be skipped.
1336 // TODO(vmpstr): Verify this is true if we create tiles in raster
1337 // iterators.
1338 if (!tile)
1339 continue;
1341 // We can't check tile->required_for_activation, because that value might
1342 // be out of date. It is updated in the raster/eviction iterators.
1343 // TODO(vmpstr): Remove the comment once you can't access this information
1344 // from the tile.
1345 if ((tiling->*is_tile_required_callback)(tile) && !tile->IsReadyToDraw()) {
1346 TRACE_EVENT_INSTANT0("cc", "Tile required, but not ready to draw.",
1347 TRACE_EVENT_SCOPE_THREAD);
1348 return false;
1352 return true;
1355 bool PictureLayerImpl::AllTilesRequiredForActivationAreReadyToDraw() const {
1356 if (!layer_tree_impl()->IsPendingTree())
1357 return true;
1359 return AllTilesRequiredAreReadyToDraw(
1360 &PictureLayerTiling::IsTileRequiredForActivationIfVisible);
1363 bool PictureLayerImpl::AllTilesRequiredForDrawAreReadyToDraw() const {
1364 if (!layer_tree_impl()->IsActiveTree())
1365 return true;
1367 return AllTilesRequiredAreReadyToDraw(
1368 &PictureLayerTiling::IsTileRequiredForDrawIfVisible);
1371 PictureLayerImpl::LayerRasterTileIterator::LayerRasterTileIterator()
1372 : layer_(nullptr), current_stage_(arraysize(stages_)) {
1375 PictureLayerImpl::LayerRasterTileIterator::LayerRasterTileIterator(
1376 PictureLayerImpl* layer,
1377 bool prioritize_low_res)
1378 : layer_(layer), current_stage_(0) {
1379 DCHECK(layer_);
1381 // Early out if the layer has no tilings.
1382 if (!layer_->tilings_ || !layer_->tilings_->num_tilings()) {
1383 current_stage_ = arraysize(stages_);
1384 return;
1387 // Tiles without valid priority are treated as having lowest priority and
1388 // never considered for raster.
1389 if (!layer_->HasValidTilePriorities()) {
1390 current_stage_ = arraysize(stages_);
1391 return;
1394 // Find high and low res tilings and initialize the iterators.
1395 for (size_t i = 0; i < layer_->tilings_->num_tilings(); ++i) {
1396 PictureLayerTiling* tiling = layer_->tilings_->tiling_at(i);
1397 if (tiling->resolution() == HIGH_RESOLUTION) {
1398 iterators_[HIGH_RES] =
1399 PictureLayerTiling::TilingRasterTileIterator(tiling);
1402 if (prioritize_low_res && tiling->resolution() == LOW_RESOLUTION) {
1403 iterators_[LOW_RES] =
1404 PictureLayerTiling::TilingRasterTileIterator(tiling);
1408 if (prioritize_low_res) {
1409 stages_[0].iterator_type = LOW_RES;
1410 stages_[0].tile_type = TilePriority::NOW;
1412 stages_[1].iterator_type = HIGH_RES;
1413 stages_[1].tile_type = TilePriority::NOW;
1414 } else {
1415 stages_[0].iterator_type = HIGH_RES;
1416 stages_[0].tile_type = TilePriority::NOW;
1418 stages_[1].iterator_type = LOW_RES;
1419 stages_[1].tile_type = TilePriority::NOW;
1422 stages_[2].iterator_type = HIGH_RES;
1423 stages_[2].tile_type = TilePriority::SOON;
1425 stages_[3].iterator_type = HIGH_RES;
1426 stages_[3].tile_type = TilePriority::EVENTUALLY;
1428 IteratorType index = stages_[current_stage_].iterator_type;
1429 TilePriority::PriorityBin tile_type = stages_[current_stage_].tile_type;
1430 if (!iterators_[index] || iterators_[index].get_type() != tile_type)
1431 AdvanceToNextStage();
1434 PictureLayerImpl::LayerRasterTileIterator::~LayerRasterTileIterator() {}
1436 PictureLayerImpl::LayerRasterTileIterator::operator bool() const {
1437 return current_stage_ < arraysize(stages_);
1440 PictureLayerImpl::LayerRasterTileIterator&
1441 PictureLayerImpl::LayerRasterTileIterator::
1442 operator++() {
1443 IteratorType index = stages_[current_stage_].iterator_type;
1444 TilePriority::PriorityBin tile_type = stages_[current_stage_].tile_type;
1446 // First advance the iterator.
1447 DCHECK(iterators_[index]);
1448 DCHECK(iterators_[index].get_type() == tile_type);
1449 ++iterators_[index];
1451 if (!iterators_[index] || iterators_[index].get_type() != tile_type)
1452 AdvanceToNextStage();
1454 return *this;
1457 Tile* PictureLayerImpl::LayerRasterTileIterator::operator*() {
1458 DCHECK(*this);
1460 IteratorType index = stages_[current_stage_].iterator_type;
1461 DCHECK(iterators_[index]);
1462 DCHECK(iterators_[index].get_type() == stages_[current_stage_].tile_type);
1464 return *iterators_[index];
1467 const Tile* PictureLayerImpl::LayerRasterTileIterator::operator*() const {
1468 DCHECK(*this);
1470 IteratorType index = stages_[current_stage_].iterator_type;
1471 DCHECK(iterators_[index]);
1472 DCHECK(iterators_[index].get_type() == stages_[current_stage_].tile_type);
1474 return *iterators_[index];
1477 void PictureLayerImpl::LayerRasterTileIterator::AdvanceToNextStage() {
1478 DCHECK_LT(current_stage_, arraysize(stages_));
1479 ++current_stage_;
1480 while (current_stage_ < arraysize(stages_)) {
1481 IteratorType index = stages_[current_stage_].iterator_type;
1482 TilePriority::PriorityBin tile_type = stages_[current_stage_].tile_type;
1484 if (iterators_[index] && iterators_[index].get_type() == tile_type)
1485 break;
1486 ++current_stage_;
1490 } // namespace cc