Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / cc / layers / picture_layer_impl.cc
blob3fb0256a7b20d13d8f77e9e94b948a275cee9031
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 <cmath>
9 #include <limits>
10 #include <set>
12 #include "base/time/time.h"
13 #include "base/trace_event/trace_event_argument.h"
14 #include "cc/base/math_util.h"
15 #include "cc/base/util.h"
16 #include "cc/debug/debug_colors.h"
17 #include "cc/debug/micro_benchmark_impl.h"
18 #include "cc/debug/traced_value.h"
19 #include "cc/layers/append_quads_data.h"
20 #include "cc/layers/solid_color_layer_impl.h"
21 #include "cc/output/begin_frame_args.h"
22 #include "cc/quads/checkerboard_draw_quad.h"
23 #include "cc/quads/debug_border_draw_quad.h"
24 #include "cc/quads/picture_draw_quad.h"
25 #include "cc/quads/solid_color_draw_quad.h"
26 #include "cc/quads/tile_draw_quad.h"
27 #include "cc/resources/tile_manager.h"
28 #include "cc/resources/tiling_set_raster_queue_all.h"
29 #include "cc/trees/layer_tree_impl.h"
30 #include "cc/trees/occlusion.h"
31 #include "ui/gfx/geometry/quad_f.h"
32 #include "ui/gfx/geometry/rect_conversions.h"
33 #include "ui/gfx/geometry/size_conversions.h"
35 namespace {
36 // This must be > 1 as we multiply or divide by this to find a new raster
37 // scale during pinch.
38 const float kMaxScaleRatioDuringPinch = 2.0f;
40 // When creating a new tiling during pinch, snap to an existing
41 // tiling's scale if the desired scale is within this ratio.
42 const float kSnapToExistingTilingRatio = 1.2f;
44 // Even for really wide viewports, at some point GPU raster should use
45 // less than 4 tiles to fill the viewport. This is set to 256 as a
46 // sane minimum for now, but we might want to tune this for low-end.
47 const int kMinHeightForGpuRasteredTile = 256;
49 // When making odd-sized tiles, round them up to increase the chances
50 // of using the same tile size.
51 const int kTileRoundUp = 64;
53 } // namespace
55 namespace cc {
57 PictureLayerImpl::Pair::Pair() : active(nullptr), pending(nullptr) {
60 PictureLayerImpl::Pair::Pair(PictureLayerImpl* active_layer,
61 PictureLayerImpl* pending_layer)
62 : active(active_layer), pending(pending_layer) {
65 PictureLayerImpl::Pair::~Pair() {
68 PictureLayerImpl::PictureLayerImpl(
69 LayerTreeImpl* tree_impl,
70 int id,
71 bool is_mask,
72 scoped_refptr<SyncedScrollOffset> scroll_offset)
73 : LayerImpl(tree_impl, id, scroll_offset),
74 twin_layer_(nullptr),
75 tilings_(CreatePictureLayerTilingSet()),
76 ideal_page_scale_(0.f),
77 ideal_device_scale_(0.f),
78 ideal_source_scale_(0.f),
79 ideal_contents_scale_(0.f),
80 raster_page_scale_(0.f),
81 raster_device_scale_(0.f),
82 raster_source_scale_(0.f),
83 raster_contents_scale_(0.f),
84 low_res_raster_contents_scale_(0.f),
85 raster_source_scale_is_fixed_(false),
86 was_screen_space_transform_animating_(false),
87 only_used_low_res_last_append_quads_(false),
88 is_mask_(is_mask),
89 nearest_neighbor_(false) {
90 layer_tree_impl()->RegisterPictureLayerImpl(this);
93 PictureLayerImpl::~PictureLayerImpl() {
94 if (twin_layer_)
95 twin_layer_->twin_layer_ = nullptr;
96 layer_tree_impl()->UnregisterPictureLayerImpl(this);
99 const char* PictureLayerImpl::LayerTypeAsString() const {
100 return "cc::PictureLayerImpl";
103 scoped_ptr<LayerImpl> PictureLayerImpl::CreateLayerImpl(
104 LayerTreeImpl* tree_impl) {
105 return PictureLayerImpl::Create(tree_impl, id(), is_mask_,
106 synced_scroll_offset());
109 void PictureLayerImpl::PushPropertiesTo(LayerImpl* base_layer) {
110 PictureLayerImpl* layer_impl = static_cast<PictureLayerImpl*>(base_layer);
111 DCHECK_EQ(layer_impl->is_mask_, is_mask_);
113 LayerImpl::PushPropertiesTo(base_layer);
115 // Twin relationships should never change once established.
116 DCHECK_IMPLIES(twin_layer_, twin_layer_ == layer_impl);
117 DCHECK_IMPLIES(twin_layer_, layer_impl->twin_layer_ == this);
118 // The twin relationship does not need to exist before the first
119 // PushPropertiesTo from pending to active layer since before that the active
120 // layer can not have a pile or tilings, it has only been created and inserted
121 // into the tree at that point.
122 twin_layer_ = layer_impl;
123 layer_impl->twin_layer_ = this;
125 layer_impl->SetNearestNeighbor(nearest_neighbor_);
127 // Solid color layers have no tilings.
128 DCHECK_IMPLIES(raster_source_->IsSolidColor(), tilings_->num_tilings() == 0);
129 // The pending tree should only have a high res (and possibly low res) tiling.
130 DCHECK_LE(tilings_->num_tilings(),
131 layer_tree_impl()->create_low_res_tiling() ? 2u : 1u);
133 layer_impl->set_gpu_raster_max_texture_size(gpu_raster_max_texture_size_);
134 layer_impl->UpdateRasterSource(raster_source_, &invalidation_,
135 tilings_.get());
136 DCHECK(invalidation_.IsEmpty());
138 // After syncing a solid color layer, the active layer has no tilings.
139 DCHECK_IMPLIES(raster_source_->IsSolidColor(),
140 layer_impl->tilings_->num_tilings() == 0);
142 layer_impl->raster_page_scale_ = raster_page_scale_;
143 layer_impl->raster_device_scale_ = raster_device_scale_;
144 layer_impl->raster_source_scale_ = raster_source_scale_;
145 layer_impl->raster_contents_scale_ = raster_contents_scale_;
146 layer_impl->low_res_raster_contents_scale_ = low_res_raster_contents_scale_;
148 layer_impl->SanityCheckTilingState();
150 // We always need to push properties.
151 // See http://crbug.com/303943
152 // TODO(danakj): Stop always pushing properties since we don't swap tilings.
153 needs_push_properties_ = true;
156 void PictureLayerImpl::AppendQuads(RenderPass* render_pass,
157 AppendQuadsData* append_quads_data) {
158 // The bounds and the pile size may differ if the pile wasn't updated (ie.
159 // PictureLayer::Update didn't happen). In that case the pile will be empty.
160 DCHECK_IMPLIES(!raster_source_->GetSize().IsEmpty(),
161 bounds() == raster_source_->GetSize())
162 << " bounds " << bounds().ToString() << " pile "
163 << raster_source_->GetSize().ToString();
165 SharedQuadState* shared_quad_state =
166 render_pass->CreateAndAppendSharedQuadState();
168 if (raster_source_->IsSolidColor()) {
169 PopulateSharedQuadState(shared_quad_state);
171 AppendDebugBorderQuad(
172 render_pass, bounds(), shared_quad_state, append_quads_data);
174 SolidColorLayerImpl::AppendSolidQuads(
175 render_pass, draw_properties().occlusion_in_content_space,
176 shared_quad_state, visible_content_rect(),
177 raster_source_->GetSolidColor(), append_quads_data);
178 return;
181 float max_contents_scale = MaximumTilingContentsScale();
182 PopulateScaledSharedQuadState(shared_quad_state, max_contents_scale);
183 Occlusion scaled_occlusion =
184 draw_properties()
185 .occlusion_in_content_space.GetOcclusionWithGivenDrawTransform(
186 shared_quad_state->content_to_target_transform);
188 if (current_draw_mode_ == DRAW_MODE_RESOURCELESS_SOFTWARE) {
189 AppendDebugBorderQuad(
190 render_pass, shared_quad_state->content_bounds, shared_quad_state,
191 append_quads_data, DebugColors::DirectPictureBorderColor(),
192 DebugColors::DirectPictureBorderWidth(layer_tree_impl()));
194 gfx::Rect geometry_rect = shared_quad_state->visible_content_rect;
195 gfx::Rect opaque_rect = contents_opaque() ? geometry_rect : gfx::Rect();
196 gfx::Rect visible_geometry_rect =
197 scaled_occlusion.GetUnoccludedContentRect(geometry_rect);
198 if (visible_geometry_rect.IsEmpty())
199 return;
201 gfx::Rect quad_content_rect = shared_quad_state->visible_content_rect;
202 gfx::Size texture_size = quad_content_rect.size();
203 gfx::RectF texture_rect = gfx::RectF(texture_size);
205 PictureDrawQuad* quad =
206 render_pass->CreateAndAppendDrawQuad<PictureDrawQuad>();
207 quad->SetNew(shared_quad_state, geometry_rect, opaque_rect,
208 visible_geometry_rect, texture_rect, texture_size,
209 nearest_neighbor_, RGBA_8888, quad_content_rect,
210 max_contents_scale, raster_source_);
211 return;
214 AppendDebugBorderQuad(render_pass, shared_quad_state->content_bounds,
215 shared_quad_state, append_quads_data);
217 if (ShowDebugBorders()) {
218 for (PictureLayerTilingSet::CoverageIterator iter(
219 tilings_.get(), max_contents_scale,
220 shared_quad_state->visible_content_rect, ideal_contents_scale_);
221 iter; ++iter) {
222 SkColor color;
223 float width;
224 if (*iter && iter->IsReadyToDraw()) {
225 TileDrawInfo::Mode mode = iter->draw_info().mode();
226 if (mode == TileDrawInfo::SOLID_COLOR_MODE) {
227 color = DebugColors::SolidColorTileBorderColor();
228 width = DebugColors::SolidColorTileBorderWidth(layer_tree_impl());
229 } else if (mode == TileDrawInfo::OOM_MODE) {
230 color = DebugColors::OOMTileBorderColor();
231 width = DebugColors::OOMTileBorderWidth(layer_tree_impl());
232 } else if (iter.resolution() == HIGH_RESOLUTION) {
233 color = DebugColors::HighResTileBorderColor();
234 width = DebugColors::HighResTileBorderWidth(layer_tree_impl());
235 } else if (iter.resolution() == LOW_RESOLUTION) {
236 color = DebugColors::LowResTileBorderColor();
237 width = DebugColors::LowResTileBorderWidth(layer_tree_impl());
238 } else if (iter->contents_scale() > max_contents_scale) {
239 color = DebugColors::ExtraHighResTileBorderColor();
240 width = DebugColors::ExtraHighResTileBorderWidth(layer_tree_impl());
241 } else {
242 color = DebugColors::ExtraLowResTileBorderColor();
243 width = DebugColors::ExtraLowResTileBorderWidth(layer_tree_impl());
245 } else {
246 color = DebugColors::MissingTileBorderColor();
247 width = DebugColors::MissingTileBorderWidth(layer_tree_impl());
250 DebugBorderDrawQuad* debug_border_quad =
251 render_pass->CreateAndAppendDrawQuad<DebugBorderDrawQuad>();
252 gfx::Rect geometry_rect = iter.geometry_rect();
253 gfx::Rect visible_geometry_rect = geometry_rect;
254 debug_border_quad->SetNew(shared_quad_state,
255 geometry_rect,
256 visible_geometry_rect,
257 color,
258 width);
262 // Keep track of the tilings that were used so that tilings that are
263 // unused can be considered for removal.
264 last_append_quads_tilings_.clear();
266 // Ignore missing tiles outside of viewport for tile priority. This is
267 // normally the same as draw viewport but can be independently overridden by
268 // embedders like Android WebView with SetExternalDrawConstraints.
269 gfx::Rect scaled_viewport_for_tile_priority = gfx::ScaleToEnclosingRect(
270 viewport_rect_for_tile_priority_in_content_space_, max_contents_scale);
272 size_t missing_tile_count = 0u;
273 size_t on_demand_missing_tile_count = 0u;
274 only_used_low_res_last_append_quads_ = true;
275 for (PictureLayerTilingSet::CoverageIterator iter(
276 tilings_.get(), max_contents_scale,
277 shared_quad_state->visible_content_rect, ideal_contents_scale_);
278 iter; ++iter) {
279 gfx::Rect geometry_rect = iter.geometry_rect();
280 gfx::Rect opaque_rect = contents_opaque() ? geometry_rect : gfx::Rect();
281 gfx::Rect visible_geometry_rect =
282 scaled_occlusion.GetUnoccludedContentRect(geometry_rect);
283 if (visible_geometry_rect.IsEmpty())
284 continue;
286 append_quads_data->visible_content_area +=
287 visible_geometry_rect.width() * visible_geometry_rect.height();
289 bool has_draw_quad = false;
290 if (*iter && iter->IsReadyToDraw()) {
291 const TileDrawInfo& draw_info = iter->draw_info();
292 switch (draw_info.mode()) {
293 case TileDrawInfo::RESOURCE_MODE: {
294 gfx::RectF texture_rect = iter.texture_rect();
296 // The raster_contents_scale_ is the best scale that the layer is
297 // trying to produce, even though it may not be ideal. Since that's
298 // the best the layer can promise in the future, consider those as
299 // complete. But if a tile is ideal scale, we don't want to consider
300 // it incomplete and trying to replace it with a tile at a worse
301 // scale.
302 if (iter->contents_scale() != raster_contents_scale_ &&
303 iter->contents_scale() != ideal_contents_scale_ &&
304 geometry_rect.Intersects(scaled_viewport_for_tile_priority)) {
305 append_quads_data->num_incomplete_tiles++;
308 // TODO(danakj): crbug.com/455931
309 layer_tree_impl()->resource_provider()->ValidateResource(
310 draw_info.resource_id());
311 TileDrawQuad* quad =
312 render_pass->CreateAndAppendDrawQuad<TileDrawQuad>();
313 quad->SetNew(shared_quad_state, geometry_rect, opaque_rect,
314 visible_geometry_rect, draw_info.resource_id(),
315 texture_rect, draw_info.resource_size(),
316 draw_info.contents_swizzled(), nearest_neighbor_);
317 has_draw_quad = true;
318 break;
320 case TileDrawInfo::SOLID_COLOR_MODE: {
321 SolidColorDrawQuad* quad =
322 render_pass->CreateAndAppendDrawQuad<SolidColorDrawQuad>();
323 quad->SetNew(shared_quad_state, geometry_rect, visible_geometry_rect,
324 draw_info.solid_color(), false);
325 has_draw_quad = true;
326 break;
328 case TileDrawInfo::OOM_MODE:
329 break; // Checkerboard.
333 if (!has_draw_quad) {
334 if (draw_checkerboard_for_missing_tiles()) {
335 CheckerboardDrawQuad* quad =
336 render_pass->CreateAndAppendDrawQuad<CheckerboardDrawQuad>();
337 SkColor color = DebugColors::DefaultCheckerboardColor();
338 quad->SetNew(shared_quad_state, geometry_rect, visible_geometry_rect,
339 color, draw_properties().device_scale_factor);
340 } else {
341 SkColor color = SafeOpaqueBackgroundColor();
342 SolidColorDrawQuad* quad =
343 render_pass->CreateAndAppendDrawQuad<SolidColorDrawQuad>();
344 quad->SetNew(shared_quad_state,
345 geometry_rect,
346 visible_geometry_rect,
347 color,
348 false);
351 if (geometry_rect.Intersects(scaled_viewport_for_tile_priority)) {
352 append_quads_data->num_missing_tiles++;
353 ++missing_tile_count;
355 append_quads_data->approximated_visible_content_area +=
356 visible_geometry_rect.width() * visible_geometry_rect.height();
357 append_quads_data->checkerboarded_visible_content_area +=
358 visible_geometry_rect.width() * visible_geometry_rect.height();
359 continue;
362 if (iter.resolution() != HIGH_RESOLUTION) {
363 append_quads_data->approximated_visible_content_area +=
364 visible_geometry_rect.width() * visible_geometry_rect.height();
367 // If we have a draw quad, but it's not low resolution, then
368 // mark that we've used something other than low res to draw.
369 if (iter.resolution() != LOW_RESOLUTION)
370 only_used_low_res_last_append_quads_ = false;
372 if (last_append_quads_tilings_.empty() ||
373 last_append_quads_tilings_.back() != iter.CurrentTiling()) {
374 last_append_quads_tilings_.push_back(iter.CurrentTiling());
378 if (missing_tile_count) {
379 TRACE_EVENT_INSTANT2("cc",
380 "PictureLayerImpl::AppendQuads checkerboard",
381 TRACE_EVENT_SCOPE_THREAD,
382 "missing_tile_count",
383 missing_tile_count,
384 "on_demand_missing_tile_count",
385 on_demand_missing_tile_count);
388 // Aggressively remove any tilings that are not seen to save memory. Note
389 // that this is at the expense of doing cause more frequent re-painting. A
390 // better scheme would be to maintain a tighter visible_content_rect for the
391 // finer tilings.
392 CleanUpTilingsOnActiveLayer(last_append_quads_tilings_);
395 bool PictureLayerImpl::UpdateTiles(bool resourceless_software_draw) {
396 DCHECK_EQ(1.f, contents_scale_x());
397 DCHECK_EQ(1.f, contents_scale_y());
399 if (!resourceless_software_draw) {
400 visible_rect_for_tile_priority_ = visible_content_rect();
403 if (!CanHaveTilings()) {
404 ideal_page_scale_ = 0.f;
405 ideal_device_scale_ = 0.f;
406 ideal_contents_scale_ = 0.f;
407 ideal_source_scale_ = 0.f;
408 SanityCheckTilingState();
409 return false;
412 // Remove any non-ideal tilings that were not used last time we generated
413 // quads to save memory and processing time. Note that pending tree should
414 // only have one or two tilings (high and low res), so only clean up the
415 // active layer. This cleans it up here in case AppendQuads didn't run.
416 // If it did run, this would not remove any additional tilings.
417 if (layer_tree_impl()->IsActiveTree())
418 CleanUpTilingsOnActiveLayer(last_append_quads_tilings_);
420 UpdateIdealScales();
422 if (!raster_contents_scale_ || ShouldAdjustRasterScale()) {
423 RecalculateRasterScales();
424 AddTilingsForRasterScale();
427 DCHECK(raster_page_scale_);
428 DCHECK(raster_device_scale_);
429 DCHECK(raster_source_scale_);
430 DCHECK(raster_contents_scale_);
431 DCHECK(low_res_raster_contents_scale_);
433 was_screen_space_transform_animating_ =
434 draw_properties().screen_space_transform_is_animating;
436 if (draw_transform_is_animating())
437 raster_source_->SetShouldAttemptToUseDistanceFieldText();
439 double current_frame_time_in_seconds =
440 (layer_tree_impl()->CurrentBeginFrameArgs().frame_time -
441 base::TimeTicks()).InSecondsF();
442 UpdateViewportRectForTilePriorityInContentSpace();
444 // The tiling set can require tiles for activation any of the following
445 // conditions are true:
446 // - This layer produced a high-res or non-ideal-res tile last frame.
447 // - We're in requires high res to draw mode.
448 // - We're not in smoothness takes priority mode.
449 // To put different, the tiling set can't require tiles for activation if
450 // we're in smoothness mode and only used low-res or checkerboard to draw last
451 // frame and we don't need high res to draw.
453 // The reason for this is that we should be able to activate sooner and get a
454 // more up to date recording, so we don't run out of recording on the active
455 // tree.
456 bool can_require_tiles_for_activation =
457 !only_used_low_res_last_append_quads_ || RequiresHighResToDraw() ||
458 !layer_tree_impl()->SmoothnessTakesPriority();
460 static const Occlusion kEmptyOcclusion;
461 const Occlusion& occlusion_in_content_space =
462 layer_tree_impl()->settings().use_occlusion_for_tile_prioritization
463 ? draw_properties().occlusion_in_content_space
464 : kEmptyOcclusion;
466 // Pass |occlusion_in_content_space| for |occlusion_in_layer_space| since
467 // they are the same space in picture layer, as contents scale is always 1.
468 bool updated = tilings_->UpdateTilePriorities(
469 viewport_rect_for_tile_priority_in_content_space_, ideal_contents_scale_,
470 current_frame_time_in_seconds, occlusion_in_content_space,
471 can_require_tiles_for_activation);
472 return updated;
475 void PictureLayerImpl::UpdateViewportRectForTilePriorityInContentSpace() {
476 // If visible_rect_for_tile_priority_ is empty or
477 // viewport_rect_for_tile_priority is set to be different from the device
478 // viewport, try to inverse project the viewport into layer space and use
479 // that. Otherwise just use visible_rect_for_tile_priority_
480 gfx::Rect visible_rect_in_content_space = visible_rect_for_tile_priority_;
481 gfx::Rect viewport_rect_for_tile_priority =
482 layer_tree_impl()->ViewportRectForTilePriority();
483 if (visible_rect_in_content_space.IsEmpty() ||
484 layer_tree_impl()->DeviceViewport() != viewport_rect_for_tile_priority) {
485 gfx::Transform view_to_layer(gfx::Transform::kSkipInitialization);
486 if (screen_space_transform().GetInverse(&view_to_layer)) {
487 // Transform from view space to content space.
488 visible_rect_in_content_space =
489 gfx::ToEnclosingRect(MathUtil::ProjectClippedRect(
490 view_to_layer, viewport_rect_for_tile_priority));
493 viewport_rect_for_tile_priority_in_content_space_ =
494 visible_rect_in_content_space;
497 PictureLayerImpl* PictureLayerImpl::GetPendingOrActiveTwinLayer() const {
498 if (!twin_layer_ || !twin_layer_->IsOnActiveOrPendingTree())
499 return nullptr;
500 return twin_layer_;
503 PictureLayerImpl* PictureLayerImpl::GetRecycledTwinLayer() const {
504 if (!twin_layer_ || twin_layer_->IsOnActiveOrPendingTree())
505 return nullptr;
506 return twin_layer_;
509 void PictureLayerImpl::UpdateRasterSource(
510 scoped_refptr<RasterSource> raster_source,
511 Region* new_invalidation,
512 const PictureLayerTilingSet* pending_set) {
513 // The bounds and the pile size may differ if the pile wasn't updated (ie.
514 // PictureLayer::Update didn't happen). In that case the pile will be empty.
515 DCHECK_IMPLIES(!raster_source->GetSize().IsEmpty(),
516 bounds() == raster_source->GetSize())
517 << " bounds " << bounds().ToString() << " pile "
518 << raster_source->GetSize().ToString();
520 // The |raster_source_| is initially null, so have to check for that for the
521 // first frame.
522 bool could_have_tilings = raster_source_.get() && CanHaveTilings();
523 raster_source_.swap(raster_source);
525 // The |new_invalidation| must be cleared before updating tilings since they
526 // access the invalidation through the PictureLayerTilingClient interface.
527 invalidation_.Clear();
528 invalidation_.Swap(new_invalidation);
530 bool can_have_tilings = CanHaveTilings();
531 DCHECK_IMPLIES(
532 pending_set,
533 can_have_tilings == GetPendingOrActiveTwinLayer()->CanHaveTilings());
535 // Need to call UpdateTiles again if CanHaveTilings changed.
536 if (could_have_tilings != can_have_tilings)
537 layer_tree_impl()->set_needs_update_draw_properties();
539 if (!can_have_tilings) {
540 RemoveAllTilings();
541 return;
544 // We could do this after doing UpdateTiles, which would avoid doing this for
545 // tilings that are going to disappear on the pending tree (if scale changed).
546 // But that would also be more complicated, so we just do it here for now.
547 if (pending_set) {
548 tilings_->UpdateTilingsToCurrentRasterSourceForActivation(
549 raster_source_, pending_set, invalidation_, MinimumContentsScale(),
550 MaximumContentsScale());
551 } else {
552 tilings_->UpdateTilingsToCurrentRasterSourceForCommit(
553 raster_source_, invalidation_, MinimumContentsScale(),
554 MaximumContentsScale());
558 void PictureLayerImpl::UpdateCanUseLCDTextAfterCommit() {
559 // This function is only allowed to be called after commit, due to it not
560 // being smart about sharing tiles and because otherwise it would cause
561 // flashes by switching out tiles in place that may be currently on screen.
562 DCHECK(layer_tree_impl()->IsSyncTree());
564 // Don't allow the LCD text state to change once disabled.
565 if (!RasterSourceUsesLCDText())
566 return;
567 if (can_use_lcd_text() == RasterSourceUsesLCDText())
568 return;
570 // Raster sources are considered const, so in order to update the state
571 // a new one must be created and all tiles recreated.
572 scoped_refptr<RasterSource> new_raster_source =
573 raster_source_->CreateCloneWithoutLCDText();
574 raster_source_.swap(new_raster_source);
576 // Synthetically invalidate everything.
577 gfx::Rect bounds_rect(bounds());
578 invalidation_ = Region(bounds_rect);
579 tilings_->UpdateRasterSourceDueToLCDChange(raster_source_, invalidation_);
580 SetUpdateRect(bounds_rect);
582 DCHECK(!RasterSourceUsesLCDText());
585 bool PictureLayerImpl::RasterSourceUsesLCDText() const {
586 return raster_source_ ? raster_source_->CanUseLCDText()
587 : layer_tree_impl()->settings().can_use_lcd_text;
590 void PictureLayerImpl::NotifyTileStateChanged(const Tile* tile) {
591 if (layer_tree_impl()->IsActiveTree()) {
592 gfx::RectF layer_damage_rect =
593 gfx::ScaleRect(tile->content_rect(), 1.f / tile->contents_scale());
594 AddDamageRect(layer_damage_rect);
598 void PictureLayerImpl::DidBeginTracing() {
599 raster_source_->DidBeginTracing();
602 void PictureLayerImpl::ReleaseResources() {
603 // Recreate tilings with new settings, since some of those might change when
604 // we release resources.
605 tilings_ = nullptr;
606 ResetRasterScale();
609 void PictureLayerImpl::RecreateResources() {
610 tilings_ = CreatePictureLayerTilingSet();
612 // To avoid an edge case after lost context where the tree is up to date but
613 // the tilings have not been managed, request an update draw properties
614 // to force tilings to get managed.
615 layer_tree_impl()->set_needs_update_draw_properties();
618 skia::RefPtr<SkPicture> PictureLayerImpl::GetPicture() {
619 return raster_source_->GetFlattenedPicture();
622 Region PictureLayerImpl::GetInvalidationRegion() {
623 // |invalidation_| gives the invalidation contained in the source frame, but
624 // is not cleared after drawing from the layer. However, update_rect() is
625 // cleared once the invalidation is drawn, which is useful for debugging
626 // visualizations. This method intersects the two to give a more exact
627 // representation of what was invalidated that is cleared after drawing.
628 return IntersectRegions(invalidation_, update_rect());
631 scoped_refptr<Tile> PictureLayerImpl::CreateTile(
632 float contents_scale,
633 const gfx::Rect& content_rect) {
634 int flags = 0;
636 // We don't handle solid color masks, so we shouldn't bother analyzing those.
637 // Otherwise, always analyze to maximize memory savings.
638 if (!is_mask_)
639 flags = Tile::USE_PICTURE_ANALYSIS;
641 return layer_tree_impl()->tile_manager()->CreateTile(
642 raster_source_.get(), content_rect.size(), content_rect, contents_scale,
643 id(), layer_tree_impl()->source_frame_number(), flags);
646 const Region* PictureLayerImpl::GetPendingInvalidation() {
647 if (layer_tree_impl()->IsPendingTree())
648 return &invalidation_;
649 if (layer_tree_impl()->IsRecycleTree())
650 return nullptr;
651 DCHECK(layer_tree_impl()->IsActiveTree());
652 if (PictureLayerImpl* twin_layer = GetPendingOrActiveTwinLayer())
653 return &twin_layer->invalidation_;
654 return nullptr;
657 const PictureLayerTiling* PictureLayerImpl::GetPendingOrActiveTwinTiling(
658 const PictureLayerTiling* tiling) const {
659 PictureLayerImpl* twin_layer = GetPendingOrActiveTwinLayer();
660 if (!twin_layer)
661 return nullptr;
662 return twin_layer->tilings_->FindTilingWithScale(tiling->contents_scale());
665 PictureLayerTiling* PictureLayerImpl::GetRecycledTwinTiling(
666 const PictureLayerTiling* tiling) {
667 PictureLayerImpl* recycled_twin = GetRecycledTwinLayer();
668 if (!recycled_twin || !recycled_twin->tilings_)
669 return nullptr;
670 return recycled_twin->tilings_->FindTilingWithScale(tiling->contents_scale());
673 TilePriority::PriorityBin PictureLayerImpl::GetMaxTilePriorityBin() const {
674 if (!HasValidTilePriorities())
675 return TilePriority::EVENTUALLY;
676 return TilePriority::NOW;
679 bool PictureLayerImpl::RequiresHighResToDraw() const {
680 return layer_tree_impl()->RequiresHighResToDraw();
683 gfx::Rect PictureLayerImpl::GetEnclosingRectInTargetSpace() const {
684 return GetScaledEnclosingRectInTargetSpace(MaximumTilingContentsScale());
687 gfx::Size PictureLayerImpl::CalculateTileSize(
688 const gfx::Size& content_bounds) const {
689 int max_texture_size =
690 layer_tree_impl()->resource_provider()->max_texture_size();
692 if (is_mask_) {
693 // Masks are not tiled, so if we can't cover the whole mask with one tile,
694 // we shouldn't have such a tiling at all.
695 DCHECK_LE(content_bounds.width(), max_texture_size);
696 DCHECK_LE(content_bounds.height(), max_texture_size);
697 return content_bounds;
700 int default_tile_width = 0;
701 int default_tile_height = 0;
702 if (layer_tree_impl()->use_gpu_rasterization()) {
703 // For GPU rasterization, we pick an ideal tile size using the viewport
704 // so we don't need any settings. The current approach uses 4 tiles
705 // to cover the viewport vertically.
706 int viewport_width = gpu_raster_max_texture_size_.width();
707 int viewport_height = gpu_raster_max_texture_size_.height();
708 default_tile_width = viewport_width;
710 // Also, increase the height proportionally as the width decreases, and
711 // pad by our border texels to make the tiles exactly match the viewport.
712 int divisor = 4;
713 if (content_bounds.width() <= viewport_width / 2)
714 divisor = 2;
715 if (content_bounds.width() <= viewport_width / 4)
716 divisor = 1;
717 default_tile_height = RoundUp(viewport_height, divisor) / divisor;
719 // Grow default sizes to account for overlapping border texels.
720 default_tile_width += 2 * PictureLayerTiling::kBorderTexels;
721 default_tile_height += 2 * PictureLayerTiling::kBorderTexels;
723 default_tile_height =
724 std::max(default_tile_height, kMinHeightForGpuRasteredTile);
725 } else {
726 // For CPU rasterization we use tile-size settings.
727 const LayerTreeSettings& settings = layer_tree_impl()->settings();
728 int max_untiled_content_width = settings.max_untiled_layer_size.width();
729 int max_untiled_content_height = settings.max_untiled_layer_size.height();
730 default_tile_width = settings.default_tile_size.width();
731 default_tile_height = settings.default_tile_size.height();
733 // If the content width is small, increase tile size vertically.
734 // If the content height is small, increase tile size horizontally.
735 // If both are less than the untiled-size, use a single tile.
736 if (content_bounds.width() < default_tile_width)
737 default_tile_height = max_untiled_content_height;
738 if (content_bounds.height() < default_tile_height)
739 default_tile_width = max_untiled_content_width;
740 if (content_bounds.width() < max_untiled_content_width &&
741 content_bounds.height() < max_untiled_content_height) {
742 default_tile_height = max_untiled_content_height;
743 default_tile_width = max_untiled_content_width;
747 int tile_width = default_tile_width;
748 int tile_height = default_tile_height;
750 // Clamp the tile width/height to the content width/height to save space.
751 if (content_bounds.width() < default_tile_width) {
752 tile_width = std::min(tile_width, content_bounds.width());
753 tile_width = RoundUp(tile_width, kTileRoundUp);
754 tile_width = std::min(tile_width, default_tile_width);
756 if (content_bounds.height() < default_tile_height) {
757 tile_height = std::min(tile_height, content_bounds.height());
758 tile_height = RoundUp(tile_height, kTileRoundUp);
759 tile_height = std::min(tile_height, default_tile_height);
762 // Under no circumstance should we be larger than the max texture size.
763 tile_width = std::min(tile_width, max_texture_size);
764 tile_height = std::min(tile_height, max_texture_size);
765 return gfx::Size(tile_width, tile_height);
768 void PictureLayerImpl::GetContentsResourceId(
769 ResourceProvider::ResourceId* resource_id,
770 gfx::Size* resource_size) const {
771 // The bounds and the pile size may differ if the pile wasn't updated (ie.
772 // PictureLayer::Update didn't happen). In that case the pile will be empty.
773 DCHECK_IMPLIES(!raster_source_->GetSize().IsEmpty(),
774 bounds() == raster_source_->GetSize())
775 << " bounds " << bounds().ToString() << " pile "
776 << raster_source_->GetSize().ToString();
777 gfx::Rect content_rect(bounds());
778 PictureLayerTilingSet::CoverageIterator iter(
779 tilings_.get(), 1.f, content_rect, ideal_contents_scale_);
781 // Mask resource not ready yet.
782 if (!iter || !*iter) {
783 *resource_id = 0;
784 return;
787 // Masks only supported if they fit on exactly one tile.
788 DCHECK(iter.geometry_rect() == content_rect)
789 << "iter rect " << iter.geometry_rect().ToString() << " content rect "
790 << content_rect.ToString();
792 const TileDrawInfo& draw_info = iter->draw_info();
793 if (!draw_info.IsReadyToDraw() ||
794 draw_info.mode() != TileDrawInfo::RESOURCE_MODE) {
795 *resource_id = 0;
796 return;
799 *resource_id = draw_info.resource_id();
800 *resource_size = draw_info.resource_size();
803 void PictureLayerImpl::SetNearestNeighbor(bool nearest_neighbor) {
804 if (nearest_neighbor_ == nearest_neighbor)
805 return;
807 nearest_neighbor_ = nearest_neighbor;
808 NoteLayerPropertyChanged();
811 PictureLayerTiling* PictureLayerImpl::AddTiling(float contents_scale) {
812 DCHECK(CanHaveTilings());
813 DCHECK_GE(contents_scale, MinimumContentsScale());
814 DCHECK_LE(contents_scale, MaximumContentsScale());
815 DCHECK(raster_source_->HasRecordings());
816 return tilings_->AddTiling(contents_scale, raster_source_);
819 void PictureLayerImpl::RemoveAllTilings() {
820 tilings_->RemoveAllTilings();
821 // If there are no tilings, then raster scales are no longer meaningful.
822 ResetRasterScale();
825 void PictureLayerImpl::AddTilingsForRasterScale() {
826 // Reset all resolution enums on tilings, we'll be setting new values in this
827 // function.
828 tilings_->MarkAllTilingsNonIdeal();
830 PictureLayerTiling* high_res =
831 tilings_->FindTilingWithScale(raster_contents_scale_);
832 // We always need a high res tiling, so create one if it doesn't exist.
833 if (!high_res)
834 high_res = AddTiling(raster_contents_scale_);
836 // Try and find a low res tiling.
837 PictureLayerTiling* low_res = nullptr;
838 if (raster_contents_scale_ == low_res_raster_contents_scale_)
839 low_res = high_res;
840 else
841 low_res = tilings_->FindTilingWithScale(low_res_raster_contents_scale_);
843 // Only create new low res tilings when the transform is static. This
844 // prevents wastefully creating a paired low res tiling for every new high res
845 // tiling during a pinch or a CSS animation.
846 bool can_have_low_res = layer_tree_impl()->create_low_res_tiling();
847 bool needs_low_res = !low_res;
848 bool is_pinching = layer_tree_impl()->PinchGestureActive();
849 bool is_animating = draw_properties().screen_space_transform_is_animating;
850 if (can_have_low_res && needs_low_res && !is_pinching && !is_animating)
851 low_res = AddTiling(low_res_raster_contents_scale_);
853 // Set low-res if we have one.
854 if (low_res && low_res != high_res)
855 low_res->set_resolution(LOW_RESOLUTION);
857 // Make sure we always have one high-res (even if high == low).
858 high_res->set_resolution(HIGH_RESOLUTION);
860 if (layer_tree_impl()->IsPendingTree()) {
861 // On the pending tree, drop any tilings that are non-ideal since we don't
862 // need them to activate anyway.
863 tilings_->RemoveNonIdealTilings();
866 SanityCheckTilingState();
869 bool PictureLayerImpl::ShouldAdjustRasterScale() const {
870 if (was_screen_space_transform_animating_ !=
871 draw_properties().screen_space_transform_is_animating)
872 return true;
874 if (draw_properties().screen_space_transform_is_animating &&
875 raster_contents_scale_ != ideal_contents_scale_ &&
876 ShouldAdjustRasterScaleDuringScaleAnimations())
877 return true;
879 bool is_pinching = layer_tree_impl()->PinchGestureActive();
880 if (is_pinching && raster_page_scale_) {
881 // We change our raster scale when it is:
882 // - Higher than ideal (need a lower-res tiling available)
883 // - Too far from ideal (need a higher-res tiling available)
884 float ratio = ideal_page_scale_ / raster_page_scale_;
885 if (raster_page_scale_ > ideal_page_scale_ ||
886 ratio > kMaxScaleRatioDuringPinch)
887 return true;
890 if (!is_pinching) {
891 // When not pinching, match the ideal page scale factor.
892 if (raster_page_scale_ != ideal_page_scale_)
893 return true;
896 // Always match the ideal device scale factor.
897 if (raster_device_scale_ != ideal_device_scale_)
898 return true;
900 // When the source scale changes we want to match it, but not when animating
901 // or when we've fixed the scale in place.
902 if (!draw_properties().screen_space_transform_is_animating &&
903 !raster_source_scale_is_fixed_ &&
904 raster_source_scale_ != ideal_source_scale_)
905 return true;
907 if (raster_contents_scale_ > MaximumContentsScale())
908 return true;
909 if (raster_contents_scale_ < MinimumContentsScale())
910 return true;
912 return false;
915 void PictureLayerImpl::RecalculateRasterScales() {
916 float old_raster_contents_scale = raster_contents_scale_;
917 float old_raster_page_scale = raster_page_scale_;
918 float old_raster_source_scale = raster_source_scale_;
920 raster_device_scale_ = ideal_device_scale_;
921 raster_page_scale_ = ideal_page_scale_;
922 raster_source_scale_ = ideal_source_scale_;
923 raster_contents_scale_ = ideal_contents_scale_;
925 // If we're not animating, or leaving an animation, and the
926 // ideal_source_scale_ changes, then things are unpredictable, and we fix
927 // the raster_source_scale_ in place.
928 if (old_raster_source_scale &&
929 !draw_properties().screen_space_transform_is_animating &&
930 !was_screen_space_transform_animating_ &&
931 old_raster_source_scale != ideal_source_scale_)
932 raster_source_scale_is_fixed_ = true;
934 // TODO(danakj): Adjust raster source scale closer to ideal source scale at
935 // a throttled rate. Possibly make use of invalidation_.IsEmpty() on pending
936 // tree. This will allow CSS scale changes to get re-rastered at an
937 // appropriate rate. (crbug.com/413636)
938 if (raster_source_scale_is_fixed_) {
939 raster_contents_scale_ /= raster_source_scale_;
940 raster_source_scale_ = 1.f;
943 // During pinch we completely ignore the current ideal scale, and just use
944 // a multiple of the previous scale.
945 bool is_pinching = layer_tree_impl()->PinchGestureActive();
946 if (is_pinching && old_raster_contents_scale) {
947 // See ShouldAdjustRasterScale:
948 // - When zooming out, preemptively create new tiling at lower resolution.
949 // - When zooming in, approximate ideal using multiple of kMaxScaleRatio.
950 bool zooming_out = old_raster_page_scale > ideal_page_scale_;
951 float desired_contents_scale = old_raster_contents_scale;
952 if (zooming_out) {
953 while (desired_contents_scale > ideal_contents_scale_)
954 desired_contents_scale /= kMaxScaleRatioDuringPinch;
955 } else {
956 while (desired_contents_scale < ideal_contents_scale_)
957 desired_contents_scale *= kMaxScaleRatioDuringPinch;
959 raster_contents_scale_ = tilings_->GetSnappedContentsScale(
960 desired_contents_scale, kSnapToExistingTilingRatio);
961 raster_page_scale_ =
962 raster_contents_scale_ / raster_device_scale_ / raster_source_scale_;
965 // If we're not re-rasterizing during animation, rasterize at the maximum
966 // scale that will occur during the animation, if the maximum scale is
967 // known. However we want to avoid excessive memory use. If the scale is
968 // smaller than what we would choose otherwise, then it's always better off
969 // for us memory-wise. But otherwise, we don't choose a scale at which this
970 // layer's rastered content would become larger than the viewport.
971 if (draw_properties().screen_space_transform_is_animating &&
972 !ShouldAdjustRasterScaleDuringScaleAnimations()) {
973 bool can_raster_at_maximum_scale = false;
974 // TODO(ajuma): If we need to deal with scale-down animations starting right
975 // as a layer gets promoted, then we'd want to have the
976 // |starting_animation_contents_scale| passed in here as a separate draw
977 // property so we could try use that when the max is too large.
978 // See crbug.com/422341.
979 float maximum_scale = draw_properties().maximum_animation_contents_scale;
980 if (maximum_scale) {
981 gfx::Size bounds_at_maximum_scale = gfx::ToCeiledSize(
982 gfx::ScaleSize(raster_source_->GetSize(), maximum_scale));
983 int64 maximum_area = static_cast<int64>(bounds_at_maximum_scale.width()) *
984 static_cast<int64>(bounds_at_maximum_scale.height());
985 gfx::Size viewport = layer_tree_impl()->device_viewport_size();
986 int64 viewport_area = static_cast<int64>(viewport.width()) *
987 static_cast<int64>(viewport.height());
988 if (maximum_area <= viewport_area)
989 can_raster_at_maximum_scale = true;
991 // Use the computed scales for the raster scale directly, do not try to use
992 // the ideal scale here. The current ideal scale may be way too large in the
993 // case of an animation with scale, and will be constantly changing.
994 if (can_raster_at_maximum_scale)
995 raster_contents_scale_ = maximum_scale;
996 else
997 raster_contents_scale_ = 1.f * ideal_page_scale_ * ideal_device_scale_;
1000 raster_contents_scale_ =
1001 std::max(raster_contents_scale_, MinimumContentsScale());
1002 raster_contents_scale_ =
1003 std::min(raster_contents_scale_, MaximumContentsScale());
1004 DCHECK_GE(raster_contents_scale_, MinimumContentsScale());
1005 DCHECK_LE(raster_contents_scale_, MaximumContentsScale());
1007 // If this layer would create zero or one tiles at this content scale,
1008 // don't create a low res tiling.
1009 gfx::Size raster_bounds = gfx::ToCeiledSize(
1010 gfx::ScaleSize(raster_source_->GetSize(), raster_contents_scale_));
1011 gfx::Size tile_size = CalculateTileSize(raster_bounds);
1012 bool tile_covers_bounds = tile_size.width() >= raster_bounds.width() &&
1013 tile_size.height() >= raster_bounds.height();
1014 if (tile_size.IsEmpty() || tile_covers_bounds) {
1015 low_res_raster_contents_scale_ = raster_contents_scale_;
1016 return;
1019 float low_res_factor =
1020 layer_tree_impl()->settings().low_res_contents_scale_factor;
1021 low_res_raster_contents_scale_ =
1022 std::max(raster_contents_scale_ * low_res_factor, MinimumContentsScale());
1023 DCHECK_LE(low_res_raster_contents_scale_, raster_contents_scale_);
1024 DCHECK_GE(low_res_raster_contents_scale_, MinimumContentsScale());
1025 DCHECK_LE(low_res_raster_contents_scale_, MaximumContentsScale());
1028 void PictureLayerImpl::CleanUpTilingsOnActiveLayer(
1029 const std::vector<PictureLayerTiling*>& used_tilings) {
1030 DCHECK(layer_tree_impl()->IsActiveTree());
1031 if (tilings_->num_tilings() == 0)
1032 return;
1034 float min_acceptable_high_res_scale = std::min(
1035 raster_contents_scale_, ideal_contents_scale_);
1036 float max_acceptable_high_res_scale = std::max(
1037 raster_contents_scale_, ideal_contents_scale_);
1039 PictureLayerImpl* twin = GetPendingOrActiveTwinLayer();
1040 if (twin && twin->CanHaveTilings()) {
1041 min_acceptable_high_res_scale = std::min(
1042 min_acceptable_high_res_scale,
1043 std::min(twin->raster_contents_scale_, twin->ideal_contents_scale_));
1044 max_acceptable_high_res_scale = std::max(
1045 max_acceptable_high_res_scale,
1046 std::max(twin->raster_contents_scale_, twin->ideal_contents_scale_));
1049 PictureLayerTilingSet* twin_set = twin ? twin->tilings_.get() : nullptr;
1050 PictureLayerImpl* recycled_twin = GetRecycledTwinLayer();
1051 PictureLayerTilingSet* recycled_twin_set =
1052 recycled_twin ? recycled_twin->tilings_.get() : nullptr;
1054 tilings_->CleanUpTilings(min_acceptable_high_res_scale,
1055 max_acceptable_high_res_scale, used_tilings,
1056 layer_tree_impl()->create_low_res_tiling(), twin_set,
1057 recycled_twin_set);
1059 if (recycled_twin_set && recycled_twin_set->num_tilings() == 0)
1060 recycled_twin->ResetRasterScale();
1062 DCHECK_GT(tilings_->num_tilings(), 0u);
1063 SanityCheckTilingState();
1066 float PictureLayerImpl::MinimumContentsScale() const {
1067 float setting_min = layer_tree_impl()->settings().minimum_contents_scale;
1069 // If the contents scale is less than 1 / width (also for height),
1070 // then it will end up having less than one pixel of content in that
1071 // dimension. Bump the minimum contents scale up in this case to prevent
1072 // this from happening.
1073 int min_dimension = std::min(raster_source_->GetSize().width(),
1074 raster_source_->GetSize().height());
1075 if (!min_dimension)
1076 return setting_min;
1078 return std::max(1.f / min_dimension, setting_min);
1081 float PictureLayerImpl::MaximumContentsScale() const {
1082 // Masks can not have tilings that would become larger than the
1083 // max_texture_size since they use a single tile for the entire
1084 // tiling. Other layers can have tilings of any scale.
1085 if (!is_mask_)
1086 return std::numeric_limits<float>::max();
1088 int max_texture_size =
1089 layer_tree_impl()->resource_provider()->max_texture_size();
1090 float max_scale_width =
1091 static_cast<float>(max_texture_size) / bounds().width();
1092 float max_scale_height =
1093 static_cast<float>(max_texture_size) / bounds().height();
1094 float max_scale = std::min(max_scale_width, max_scale_height);
1095 // We require that multiplying the layer size by the contents scale and
1096 // ceiling produces a value <= |max_texture_size|. Because for large layer
1097 // sizes floating point ambiguity may crop up, making the result larger or
1098 // smaller than expected, we use a slightly smaller floating point value for
1099 // the scale, to help ensure that the resulting content bounds will never end
1100 // up larger than |max_texture_size|.
1101 return nextafterf(max_scale, 0.f);
1104 void PictureLayerImpl::ResetRasterScale() {
1105 raster_page_scale_ = 0.f;
1106 raster_device_scale_ = 0.f;
1107 raster_source_scale_ = 0.f;
1108 raster_contents_scale_ = 0.f;
1109 low_res_raster_contents_scale_ = 0.f;
1110 raster_source_scale_is_fixed_ = false;
1113 bool PictureLayerImpl::CanHaveTilings() const {
1114 if (raster_source_->IsSolidColor())
1115 return false;
1116 if (!DrawsContent())
1117 return false;
1118 if (!raster_source_->HasRecordings())
1119 return false;
1120 // If the |raster_source_| has a recording it should have non-empty bounds.
1121 DCHECK(!raster_source_->GetSize().IsEmpty());
1122 if (MaximumContentsScale() < MinimumContentsScale())
1123 return false;
1124 return true;
1127 void PictureLayerImpl::SanityCheckTilingState() const {
1128 #if DCHECK_IS_ON()
1129 // Recycle tree doesn't have any restrictions.
1130 if (layer_tree_impl()->IsRecycleTree())
1131 return;
1133 if (!CanHaveTilings()) {
1134 DCHECK_EQ(0u, tilings_->num_tilings());
1135 return;
1137 if (tilings_->num_tilings() == 0)
1138 return;
1140 // We should only have one high res tiling.
1141 DCHECK_EQ(1, tilings_->NumHighResTilings());
1142 #endif
1145 bool PictureLayerImpl::ShouldAdjustRasterScaleDuringScaleAnimations() const {
1146 return layer_tree_impl()->use_gpu_rasterization();
1149 float PictureLayerImpl::MaximumTilingContentsScale() const {
1150 float max_contents_scale = tilings_->GetMaximumContentsScale();
1151 return std::max(max_contents_scale, MinimumContentsScale());
1154 scoped_ptr<PictureLayerTilingSet>
1155 PictureLayerImpl::CreatePictureLayerTilingSet() {
1156 const LayerTreeSettings& settings = layer_tree_impl()->settings();
1157 return PictureLayerTilingSet::Create(
1158 this, settings.max_tiles_for_interest_area,
1159 layer_tree_impl()->use_gpu_rasterization()
1160 ? settings.gpu_rasterization_skewport_target_time_in_seconds
1161 : settings.skewport_target_time_in_seconds,
1162 settings.skewport_extrapolation_limit_in_content_pixels);
1165 void PictureLayerImpl::UpdateIdealScales() {
1166 DCHECK(CanHaveTilings());
1168 float min_contents_scale = MinimumContentsScale();
1169 DCHECK_GT(min_contents_scale, 0.f);
1170 float min_page_scale = layer_tree_impl()->min_page_scale_factor();
1171 DCHECK_GT(min_page_scale, 0.f);
1172 float min_device_scale = 1.f;
1173 float min_source_scale =
1174 min_contents_scale / min_page_scale / min_device_scale;
1176 float ideal_page_scale = draw_properties().page_scale_factor;
1177 float ideal_device_scale = draw_properties().device_scale_factor;
1178 float ideal_source_scale = draw_properties().ideal_contents_scale /
1179 ideal_page_scale / ideal_device_scale;
1180 ideal_contents_scale_ =
1181 std::max(draw_properties().ideal_contents_scale, min_contents_scale);
1182 ideal_page_scale_ = draw_properties().page_scale_factor;
1183 ideal_device_scale_ = draw_properties().device_scale_factor;
1184 ideal_source_scale_ = std::max(ideal_source_scale, min_source_scale);
1187 void PictureLayerImpl::GetDebugBorderProperties(
1188 SkColor* color,
1189 float* width) const {
1190 *color = DebugColors::TiledContentLayerBorderColor();
1191 *width = DebugColors::TiledContentLayerBorderWidth(layer_tree_impl());
1194 void PictureLayerImpl::GetAllTilesAndPrioritiesForTracing(
1195 std::map<const Tile*, TilePriority>* tile_map) const {
1196 if (!tilings_)
1197 return;
1198 tilings_->GetAllTilesAndPrioritiesForTracing(tile_map);
1201 void PictureLayerImpl::AsValueInto(
1202 base::trace_event::TracedValue* state) const {
1203 LayerImpl::AsValueInto(state);
1204 state->SetDouble("ideal_contents_scale", ideal_contents_scale_);
1205 state->SetDouble("geometry_contents_scale", MaximumTilingContentsScale());
1206 state->BeginArray("tilings");
1207 tilings_->AsValueInto(state);
1208 state->EndArray();
1210 MathUtil::AddToTracedValue("tile_priority_rect",
1211 viewport_rect_for_tile_priority_in_content_space_,
1212 state);
1213 MathUtil::AddToTracedValue("visible_rect", visible_content_rect(), state);
1215 state->BeginArray("pictures");
1216 raster_source_->AsValueInto(state);
1217 state->EndArray();
1219 state->BeginArray("invalidation");
1220 invalidation_.AsValueInto(state);
1221 state->EndArray();
1223 state->BeginArray("coverage_tiles");
1224 for (PictureLayerTilingSet::CoverageIterator iter(
1225 tilings_.get(), 1.f, gfx::Rect(raster_source_->GetSize()),
1226 ideal_contents_scale_);
1227 iter; ++iter) {
1228 state->BeginDictionary();
1230 MathUtil::AddToTracedValue("geometry_rect", iter.geometry_rect(), state);
1232 if (*iter)
1233 TracedValue::SetIDRef(*iter, state, "tile");
1235 state->EndDictionary();
1237 state->EndArray();
1240 size_t PictureLayerImpl::GPUMemoryUsageInBytes() const {
1241 return tilings_->GPUMemoryUsageInBytes();
1244 void PictureLayerImpl::RunMicroBenchmark(MicroBenchmarkImpl* benchmark) {
1245 benchmark->RunOnLayer(this);
1248 WhichTree PictureLayerImpl::GetTree() const {
1249 return layer_tree_impl()->IsActiveTree() ? ACTIVE_TREE : PENDING_TREE;
1252 bool PictureLayerImpl::IsOnActiveOrPendingTree() const {
1253 return !layer_tree_impl()->IsRecycleTree();
1256 bool PictureLayerImpl::HasValidTilePriorities() const {
1257 return IsOnActiveOrPendingTree() && IsDrawnRenderSurfaceLayerListMember();
1260 } // namespace cc