Adding Peter Thatcher to the owners file.
[chromium-blink-merge.git] / cc / layers / picture_layer_impl.cc
blobe9188ec678fbefb736b8a5db9d70934fc354392d
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;
709 // Also, increase the height proportionally as the width decreases, and
710 // pad by our border texels to make the tiles exactly match the viewport.
711 int divisor = 4;
712 if (content_bounds.width() <= viewport_width / 2)
713 divisor = 2;
714 if (content_bounds.width() <= viewport_width / 4)
715 divisor = 1;
716 default_tile_height = RoundUp(viewport_height, divisor) / divisor;
717 default_tile_height += 2 * PictureLayerTiling::kBorderTexels;
718 default_tile_height =
719 std::max(default_tile_height, kMinHeightForGpuRasteredTile);
720 } else {
721 // For CPU rasterization we use tile-size settings.
722 const LayerTreeSettings& settings = layer_tree_impl()->settings();
723 int max_untiled_content_width = settings.max_untiled_layer_size.width();
724 int max_untiled_content_height = settings.max_untiled_layer_size.height();
725 default_tile_width = settings.default_tile_size.width();
726 default_tile_height = settings.default_tile_size.height();
728 // If the content width is small, increase tile size vertically.
729 // If the content height is small, increase tile size horizontally.
730 // If both are less than the untiled-size, use a single tile.
731 if (content_bounds.width() < default_tile_width)
732 default_tile_height = max_untiled_content_height;
733 if (content_bounds.height() < default_tile_height)
734 default_tile_width = max_untiled_content_width;
735 if (content_bounds.width() < max_untiled_content_width &&
736 content_bounds.height() < max_untiled_content_height) {
737 default_tile_height = max_untiled_content_height;
738 default_tile_width = max_untiled_content_width;
742 int tile_width = default_tile_width;
743 int tile_height = default_tile_height;
745 // Clamp the tile width/height to the content width/height to save space.
746 if (content_bounds.width() < default_tile_width) {
747 tile_width = std::min(tile_width, content_bounds.width());
748 tile_width = RoundUp(tile_width, kTileRoundUp);
749 tile_width = std::min(tile_width, default_tile_width);
751 if (content_bounds.height() < default_tile_height) {
752 tile_height = std::min(tile_height, content_bounds.height());
753 tile_height = RoundUp(tile_height, kTileRoundUp);
754 tile_height = std::min(tile_height, default_tile_height);
757 // Under no circumstance should we be larger than the max texture size.
758 tile_width = std::min(tile_width, max_texture_size);
759 tile_height = std::min(tile_height, max_texture_size);
760 return gfx::Size(tile_width, tile_height);
763 void PictureLayerImpl::GetContentsResourceId(
764 ResourceProvider::ResourceId* resource_id,
765 gfx::Size* resource_size) const {
766 // The bounds and the pile size may differ if the pile wasn't updated (ie.
767 // PictureLayer::Update didn't happen). In that case the pile will be empty.
768 DCHECK_IMPLIES(!raster_source_->GetSize().IsEmpty(),
769 bounds() == raster_source_->GetSize())
770 << " bounds " << bounds().ToString() << " pile "
771 << raster_source_->GetSize().ToString();
772 gfx::Rect content_rect(bounds());
773 PictureLayerTilingSet::CoverageIterator iter(
774 tilings_.get(), 1.f, content_rect, ideal_contents_scale_);
776 // Mask resource not ready yet.
777 if (!iter || !*iter) {
778 *resource_id = 0;
779 return;
782 // Masks only supported if they fit on exactly one tile.
783 DCHECK(iter.geometry_rect() == content_rect)
784 << "iter rect " << iter.geometry_rect().ToString() << " content rect "
785 << content_rect.ToString();
787 const TileDrawInfo& draw_info = iter->draw_info();
788 if (!draw_info.IsReadyToDraw() ||
789 draw_info.mode() != TileDrawInfo::RESOURCE_MODE) {
790 *resource_id = 0;
791 return;
794 *resource_id = draw_info.resource_id();
795 *resource_size = draw_info.resource_size();
798 void PictureLayerImpl::SetNearestNeighbor(bool nearest_neighbor) {
799 if (nearest_neighbor_ == nearest_neighbor)
800 return;
802 nearest_neighbor_ = nearest_neighbor;
803 NoteLayerPropertyChanged();
806 PictureLayerTiling* PictureLayerImpl::AddTiling(float contents_scale) {
807 DCHECK(CanHaveTilings());
808 DCHECK_GE(contents_scale, MinimumContentsScale());
809 DCHECK_LE(contents_scale, MaximumContentsScale());
810 DCHECK(raster_source_->HasRecordings());
811 return tilings_->AddTiling(contents_scale, raster_source_);
814 void PictureLayerImpl::RemoveAllTilings() {
815 tilings_->RemoveAllTilings();
816 // If there are no tilings, then raster scales are no longer meaningful.
817 ResetRasterScale();
820 void PictureLayerImpl::AddTilingsForRasterScale() {
821 // Reset all resolution enums on tilings, we'll be setting new values in this
822 // function.
823 tilings_->MarkAllTilingsNonIdeal();
825 PictureLayerTiling* high_res =
826 tilings_->FindTilingWithScale(raster_contents_scale_);
827 // We always need a high res tiling, so create one if it doesn't exist.
828 if (!high_res)
829 high_res = AddTiling(raster_contents_scale_);
831 // Try and find a low res tiling.
832 PictureLayerTiling* low_res = nullptr;
833 if (raster_contents_scale_ == low_res_raster_contents_scale_)
834 low_res = high_res;
835 else
836 low_res = tilings_->FindTilingWithScale(low_res_raster_contents_scale_);
838 // Only create new low res tilings when the transform is static. This
839 // prevents wastefully creating a paired low res tiling for every new high res
840 // tiling during a pinch or a CSS animation.
841 bool can_have_low_res = layer_tree_impl()->create_low_res_tiling();
842 bool needs_low_res = !low_res;
843 bool is_pinching = layer_tree_impl()->PinchGestureActive();
844 bool is_animating = draw_properties().screen_space_transform_is_animating;
845 if (can_have_low_res && needs_low_res && !is_pinching && !is_animating)
846 low_res = AddTiling(low_res_raster_contents_scale_);
848 // Set low-res if we have one.
849 if (low_res && low_res != high_res)
850 low_res->set_resolution(LOW_RESOLUTION);
852 // Make sure we always have one high-res (even if high == low).
853 high_res->set_resolution(HIGH_RESOLUTION);
855 if (layer_tree_impl()->IsPendingTree()) {
856 // On the pending tree, drop any tilings that are non-ideal since we don't
857 // need them to activate anyway.
858 tilings_->RemoveNonIdealTilings();
861 SanityCheckTilingState();
864 bool PictureLayerImpl::ShouldAdjustRasterScale() const {
865 if (was_screen_space_transform_animating_ !=
866 draw_properties().screen_space_transform_is_animating)
867 return true;
869 if (draw_properties().screen_space_transform_is_animating &&
870 raster_contents_scale_ != ideal_contents_scale_ &&
871 ShouldAdjustRasterScaleDuringScaleAnimations())
872 return true;
874 bool is_pinching = layer_tree_impl()->PinchGestureActive();
875 if (is_pinching && raster_page_scale_) {
876 // We change our raster scale when it is:
877 // - Higher than ideal (need a lower-res tiling available)
878 // - Too far from ideal (need a higher-res tiling available)
879 float ratio = ideal_page_scale_ / raster_page_scale_;
880 if (raster_page_scale_ > ideal_page_scale_ ||
881 ratio > kMaxScaleRatioDuringPinch)
882 return true;
885 if (!is_pinching) {
886 // When not pinching, match the ideal page scale factor.
887 if (raster_page_scale_ != ideal_page_scale_)
888 return true;
891 // Always match the ideal device scale factor.
892 if (raster_device_scale_ != ideal_device_scale_)
893 return true;
895 // When the source scale changes we want to match it, but not when animating
896 // or when we've fixed the scale in place.
897 if (!draw_properties().screen_space_transform_is_animating &&
898 !raster_source_scale_is_fixed_ &&
899 raster_source_scale_ != ideal_source_scale_)
900 return true;
902 if (raster_contents_scale_ > MaximumContentsScale())
903 return true;
904 if (raster_contents_scale_ < MinimumContentsScale())
905 return true;
907 return false;
910 void PictureLayerImpl::RecalculateRasterScales() {
911 float old_raster_contents_scale = raster_contents_scale_;
912 float old_raster_page_scale = raster_page_scale_;
913 float old_raster_source_scale = raster_source_scale_;
915 raster_device_scale_ = ideal_device_scale_;
916 raster_page_scale_ = ideal_page_scale_;
917 raster_source_scale_ = ideal_source_scale_;
918 raster_contents_scale_ = ideal_contents_scale_;
920 // If we're not animating, or leaving an animation, and the
921 // ideal_source_scale_ changes, then things are unpredictable, and we fix
922 // the raster_source_scale_ in place.
923 if (old_raster_source_scale &&
924 !draw_properties().screen_space_transform_is_animating &&
925 !was_screen_space_transform_animating_ &&
926 old_raster_source_scale != ideal_source_scale_)
927 raster_source_scale_is_fixed_ = true;
929 // TODO(danakj): Adjust raster source scale closer to ideal source scale at
930 // a throttled rate. Possibly make use of invalidation_.IsEmpty() on pending
931 // tree. This will allow CSS scale changes to get re-rastered at an
932 // appropriate rate. (crbug.com/413636)
933 if (raster_source_scale_is_fixed_) {
934 raster_contents_scale_ /= raster_source_scale_;
935 raster_source_scale_ = 1.f;
938 // During pinch we completely ignore the current ideal scale, and just use
939 // a multiple of the previous scale.
940 bool is_pinching = layer_tree_impl()->PinchGestureActive();
941 if (is_pinching && old_raster_contents_scale) {
942 // See ShouldAdjustRasterScale:
943 // - When zooming out, preemptively create new tiling at lower resolution.
944 // - When zooming in, approximate ideal using multiple of kMaxScaleRatio.
945 bool zooming_out = old_raster_page_scale > ideal_page_scale_;
946 float desired_contents_scale = old_raster_contents_scale;
947 if (zooming_out) {
948 while (desired_contents_scale > ideal_contents_scale_)
949 desired_contents_scale /= kMaxScaleRatioDuringPinch;
950 } else {
951 while (desired_contents_scale < ideal_contents_scale_)
952 desired_contents_scale *= kMaxScaleRatioDuringPinch;
954 raster_contents_scale_ = tilings_->GetSnappedContentsScale(
955 desired_contents_scale, kSnapToExistingTilingRatio);
956 raster_page_scale_ =
957 raster_contents_scale_ / raster_device_scale_ / raster_source_scale_;
960 // If we're not re-rasterizing during animation, rasterize at the maximum
961 // scale that will occur during the animation, if the maximum scale is
962 // known. However we want to avoid excessive memory use. If the scale is
963 // smaller than what we would choose otherwise, then it's always better off
964 // for us memory-wise. But otherwise, we don't choose a scale at which this
965 // layer's rastered content would become larger than the viewport.
966 if (draw_properties().screen_space_transform_is_animating &&
967 !ShouldAdjustRasterScaleDuringScaleAnimations()) {
968 bool can_raster_at_maximum_scale = false;
969 // TODO(ajuma): If we need to deal with scale-down animations starting right
970 // as a layer gets promoted, then we'd want to have the
971 // |starting_animation_contents_scale| passed in here as a separate draw
972 // property so we could try use that when the max is too large.
973 // See crbug.com/422341.
974 float maximum_scale = draw_properties().maximum_animation_contents_scale;
975 if (maximum_scale) {
976 gfx::Size bounds_at_maximum_scale = gfx::ToCeiledSize(
977 gfx::ScaleSize(raster_source_->GetSize(), maximum_scale));
978 int64 maximum_area = static_cast<int64>(bounds_at_maximum_scale.width()) *
979 static_cast<int64>(bounds_at_maximum_scale.height());
980 gfx::Size viewport = layer_tree_impl()->device_viewport_size();
981 int64 viewport_area = static_cast<int64>(viewport.width()) *
982 static_cast<int64>(viewport.height());
983 if (maximum_area <= viewport_area)
984 can_raster_at_maximum_scale = true;
986 // Use the computed scales for the raster scale directly, do not try to use
987 // the ideal scale here. The current ideal scale may be way too large in the
988 // case of an animation with scale, and will be constantly changing.
989 if (can_raster_at_maximum_scale)
990 raster_contents_scale_ = maximum_scale;
991 else
992 raster_contents_scale_ = 1.f * ideal_page_scale_ * ideal_device_scale_;
995 raster_contents_scale_ =
996 std::max(raster_contents_scale_, MinimumContentsScale());
997 raster_contents_scale_ =
998 std::min(raster_contents_scale_, MaximumContentsScale());
999 DCHECK_GE(raster_contents_scale_, MinimumContentsScale());
1000 DCHECK_LE(raster_contents_scale_, MaximumContentsScale());
1002 // If this layer would create zero or one tiles at this content scale,
1003 // don't create a low res tiling.
1004 gfx::Size raster_bounds = gfx::ToCeiledSize(
1005 gfx::ScaleSize(raster_source_->GetSize(), raster_contents_scale_));
1006 gfx::Size tile_size = CalculateTileSize(raster_bounds);
1007 bool tile_covers_bounds = tile_size.width() >= raster_bounds.width() &&
1008 tile_size.height() >= raster_bounds.height();
1009 if (tile_size.IsEmpty() || tile_covers_bounds) {
1010 low_res_raster_contents_scale_ = raster_contents_scale_;
1011 return;
1014 float low_res_factor =
1015 layer_tree_impl()->settings().low_res_contents_scale_factor;
1016 low_res_raster_contents_scale_ =
1017 std::max(raster_contents_scale_ * low_res_factor, MinimumContentsScale());
1018 DCHECK_LE(low_res_raster_contents_scale_, raster_contents_scale_);
1019 DCHECK_GE(low_res_raster_contents_scale_, MinimumContentsScale());
1020 DCHECK_LE(low_res_raster_contents_scale_, MaximumContentsScale());
1023 void PictureLayerImpl::CleanUpTilingsOnActiveLayer(
1024 const std::vector<PictureLayerTiling*>& used_tilings) {
1025 DCHECK(layer_tree_impl()->IsActiveTree());
1026 if (tilings_->num_tilings() == 0)
1027 return;
1029 float min_acceptable_high_res_scale = std::min(
1030 raster_contents_scale_, ideal_contents_scale_);
1031 float max_acceptable_high_res_scale = std::max(
1032 raster_contents_scale_, ideal_contents_scale_);
1034 PictureLayerImpl* twin = GetPendingOrActiveTwinLayer();
1035 if (twin && twin->CanHaveTilings()) {
1036 min_acceptable_high_res_scale = std::min(
1037 min_acceptable_high_res_scale,
1038 std::min(twin->raster_contents_scale_, twin->ideal_contents_scale_));
1039 max_acceptable_high_res_scale = std::max(
1040 max_acceptable_high_res_scale,
1041 std::max(twin->raster_contents_scale_, twin->ideal_contents_scale_));
1044 PictureLayerTilingSet* twin_set = twin ? twin->tilings_.get() : nullptr;
1045 PictureLayerImpl* recycled_twin = GetRecycledTwinLayer();
1046 PictureLayerTilingSet* recycled_twin_set =
1047 recycled_twin ? recycled_twin->tilings_.get() : nullptr;
1049 tilings_->CleanUpTilings(min_acceptable_high_res_scale,
1050 max_acceptable_high_res_scale, used_tilings,
1051 layer_tree_impl()->create_low_res_tiling(), twin_set,
1052 recycled_twin_set);
1054 if (recycled_twin_set && recycled_twin_set->num_tilings() == 0)
1055 recycled_twin->ResetRasterScale();
1057 DCHECK_GT(tilings_->num_tilings(), 0u);
1058 SanityCheckTilingState();
1061 float PictureLayerImpl::MinimumContentsScale() const {
1062 float setting_min = layer_tree_impl()->settings().minimum_contents_scale;
1064 // If the contents scale is less than 1 / width (also for height),
1065 // then it will end up having less than one pixel of content in that
1066 // dimension. Bump the minimum contents scale up in this case to prevent
1067 // this from happening.
1068 int min_dimension = std::min(raster_source_->GetSize().width(),
1069 raster_source_->GetSize().height());
1070 if (!min_dimension)
1071 return setting_min;
1073 return std::max(1.f / min_dimension, setting_min);
1076 float PictureLayerImpl::MaximumContentsScale() const {
1077 // Masks can not have tilings that would become larger than the
1078 // max_texture_size since they use a single tile for the entire
1079 // tiling. Other layers can have tilings of any scale.
1080 if (!is_mask_)
1081 return std::numeric_limits<float>::max();
1083 int max_texture_size =
1084 layer_tree_impl()->resource_provider()->max_texture_size();
1085 float max_scale_width =
1086 static_cast<float>(max_texture_size) / bounds().width();
1087 float max_scale_height =
1088 static_cast<float>(max_texture_size) / bounds().height();
1089 float max_scale = std::min(max_scale_width, max_scale_height);
1090 // We require that multiplying the layer size by the contents scale and
1091 // ceiling produces a value <= |max_texture_size|. Because for large layer
1092 // sizes floating point ambiguity may crop up, making the result larger or
1093 // smaller than expected, we use a slightly smaller floating point value for
1094 // the scale, to help ensure that the resulting content bounds will never end
1095 // up larger than |max_texture_size|.
1096 return nextafterf(max_scale, 0.f);
1099 void PictureLayerImpl::ResetRasterScale() {
1100 raster_page_scale_ = 0.f;
1101 raster_device_scale_ = 0.f;
1102 raster_source_scale_ = 0.f;
1103 raster_contents_scale_ = 0.f;
1104 low_res_raster_contents_scale_ = 0.f;
1105 raster_source_scale_is_fixed_ = false;
1108 bool PictureLayerImpl::CanHaveTilings() const {
1109 if (raster_source_->IsSolidColor())
1110 return false;
1111 if (!DrawsContent())
1112 return false;
1113 if (!raster_source_->HasRecordings())
1114 return false;
1115 // If the |raster_source_| has a recording it should have non-empty bounds.
1116 DCHECK(!raster_source_->GetSize().IsEmpty());
1117 if (MaximumContentsScale() < MinimumContentsScale())
1118 return false;
1119 return true;
1122 void PictureLayerImpl::SanityCheckTilingState() const {
1123 #if DCHECK_IS_ON()
1124 // Recycle tree doesn't have any restrictions.
1125 if (layer_tree_impl()->IsRecycleTree())
1126 return;
1128 if (!CanHaveTilings()) {
1129 DCHECK_EQ(0u, tilings_->num_tilings());
1130 return;
1132 if (tilings_->num_tilings() == 0)
1133 return;
1135 // We should only have one high res tiling.
1136 DCHECK_EQ(1, tilings_->NumHighResTilings());
1137 #endif
1140 bool PictureLayerImpl::ShouldAdjustRasterScaleDuringScaleAnimations() const {
1141 return layer_tree_impl()->use_gpu_rasterization();
1144 float PictureLayerImpl::MaximumTilingContentsScale() const {
1145 float max_contents_scale = tilings_->GetMaximumContentsScale();
1146 return std::max(max_contents_scale, MinimumContentsScale());
1149 scoped_ptr<PictureLayerTilingSet>
1150 PictureLayerImpl::CreatePictureLayerTilingSet() {
1151 const LayerTreeSettings& settings = layer_tree_impl()->settings();
1152 return PictureLayerTilingSet::Create(
1153 this, settings.max_tiles_for_interest_area,
1154 layer_tree_impl()->use_gpu_rasterization()
1155 ? settings.gpu_rasterization_skewport_target_time_in_seconds
1156 : settings.skewport_target_time_in_seconds,
1157 settings.skewport_extrapolation_limit_in_content_pixels);
1160 void PictureLayerImpl::UpdateIdealScales() {
1161 DCHECK(CanHaveTilings());
1163 float min_contents_scale = MinimumContentsScale();
1164 DCHECK_GT(min_contents_scale, 0.f);
1165 float min_page_scale = layer_tree_impl()->min_page_scale_factor();
1166 DCHECK_GT(min_page_scale, 0.f);
1167 float min_device_scale = 1.f;
1168 float min_source_scale =
1169 min_contents_scale / min_page_scale / min_device_scale;
1171 float ideal_page_scale = draw_properties().page_scale_factor;
1172 float ideal_device_scale = draw_properties().device_scale_factor;
1173 float ideal_source_scale = draw_properties().ideal_contents_scale /
1174 ideal_page_scale / ideal_device_scale;
1175 ideal_contents_scale_ =
1176 std::max(draw_properties().ideal_contents_scale, min_contents_scale);
1177 ideal_page_scale_ = draw_properties().page_scale_factor;
1178 ideal_device_scale_ = draw_properties().device_scale_factor;
1179 ideal_source_scale_ = std::max(ideal_source_scale, min_source_scale);
1182 void PictureLayerImpl::GetDebugBorderProperties(
1183 SkColor* color,
1184 float* width) const {
1185 *color = DebugColors::TiledContentLayerBorderColor();
1186 *width = DebugColors::TiledContentLayerBorderWidth(layer_tree_impl());
1189 void PictureLayerImpl::GetAllTilesAndPrioritiesForTracing(
1190 std::map<const Tile*, TilePriority>* tile_map) const {
1191 if (!tilings_)
1192 return;
1193 tilings_->GetAllTilesAndPrioritiesForTracing(tile_map);
1196 void PictureLayerImpl::AsValueInto(
1197 base::trace_event::TracedValue* state) const {
1198 LayerImpl::AsValueInto(state);
1199 state->SetDouble("ideal_contents_scale", ideal_contents_scale_);
1200 state->SetDouble("geometry_contents_scale", MaximumTilingContentsScale());
1201 state->BeginArray("tilings");
1202 tilings_->AsValueInto(state);
1203 state->EndArray();
1205 MathUtil::AddToTracedValue("tile_priority_rect",
1206 viewport_rect_for_tile_priority_in_content_space_,
1207 state);
1208 MathUtil::AddToTracedValue("visible_rect", visible_content_rect(), state);
1210 state->BeginArray("pictures");
1211 raster_source_->AsValueInto(state);
1212 state->EndArray();
1214 state->BeginArray("invalidation");
1215 invalidation_.AsValueInto(state);
1216 state->EndArray();
1218 state->BeginArray("coverage_tiles");
1219 for (PictureLayerTilingSet::CoverageIterator iter(
1220 tilings_.get(), 1.f, gfx::Rect(raster_source_->GetSize()),
1221 ideal_contents_scale_);
1222 iter; ++iter) {
1223 state->BeginDictionary();
1225 MathUtil::AddToTracedValue("geometry_rect", iter.geometry_rect(), state);
1227 if (*iter)
1228 TracedValue::SetIDRef(*iter, state, "tile");
1230 state->EndDictionary();
1232 state->EndArray();
1235 size_t PictureLayerImpl::GPUMemoryUsageInBytes() const {
1236 return tilings_->GPUMemoryUsageInBytes();
1239 void PictureLayerImpl::RunMicroBenchmark(MicroBenchmarkImpl* benchmark) {
1240 benchmark->RunOnLayer(this);
1243 WhichTree PictureLayerImpl::GetTree() const {
1244 return layer_tree_impl()->IsActiveTree() ? ACTIVE_TREE : PENDING_TREE;
1247 bool PictureLayerImpl::IsOnActiveOrPendingTree() const {
1248 return !layer_tree_impl()->IsRecycleTree();
1251 bool PictureLayerImpl::HasValidTilePriorities() const {
1252 return IsOnActiveOrPendingTree() && IsDrawnRenderSurfaceLayerListMember();
1255 } // namespace cc