Move VISUAL_STATE promise to activation
[chromium-blink-merge.git] / cc / layers / picture_layer_impl.cc
blobcb41e7a2031c38a8a3536bb785b5e740ae741913
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::PictureLayerImpl(
58 LayerTreeImpl* tree_impl,
59 int id,
60 bool is_mask,
61 scoped_refptr<SyncedScrollOffset> scroll_offset)
62 : LayerImpl(tree_impl, id, scroll_offset),
63 twin_layer_(nullptr),
64 tilings_(CreatePictureLayerTilingSet()),
65 ideal_page_scale_(0.f),
66 ideal_device_scale_(0.f),
67 ideal_source_scale_(0.f),
68 ideal_contents_scale_(0.f),
69 raster_page_scale_(0.f),
70 raster_device_scale_(0.f),
71 raster_source_scale_(0.f),
72 raster_contents_scale_(0.f),
73 low_res_raster_contents_scale_(0.f),
74 raster_source_scale_is_fixed_(false),
75 was_screen_space_transform_animating_(false),
76 only_used_low_res_last_append_quads_(false),
77 is_mask_(is_mask),
78 nearest_neighbor_(false) {
79 layer_tree_impl()->RegisterPictureLayerImpl(this);
82 PictureLayerImpl::~PictureLayerImpl() {
83 if (twin_layer_)
84 twin_layer_->twin_layer_ = nullptr;
85 layer_tree_impl()->UnregisterPictureLayerImpl(this);
88 const char* PictureLayerImpl::LayerTypeAsString() const {
89 return "cc::PictureLayerImpl";
92 scoped_ptr<LayerImpl> PictureLayerImpl::CreateLayerImpl(
93 LayerTreeImpl* tree_impl) {
94 return PictureLayerImpl::Create(tree_impl, id(), is_mask_,
95 synced_scroll_offset());
98 void PictureLayerImpl::PushPropertiesTo(LayerImpl* base_layer) {
99 PictureLayerImpl* layer_impl = static_cast<PictureLayerImpl*>(base_layer);
100 DCHECK_EQ(layer_impl->is_mask_, is_mask_);
102 LayerImpl::PushPropertiesTo(base_layer);
104 // Twin relationships should never change once established.
105 DCHECK_IMPLIES(twin_layer_, twin_layer_ == layer_impl);
106 DCHECK_IMPLIES(twin_layer_, layer_impl->twin_layer_ == this);
107 // The twin relationship does not need to exist before the first
108 // PushPropertiesTo from pending to active layer since before that the active
109 // layer can not have a pile or tilings, it has only been created and inserted
110 // into the tree at that point.
111 twin_layer_ = layer_impl;
112 layer_impl->twin_layer_ = this;
114 layer_impl->SetNearestNeighbor(nearest_neighbor_);
116 // Solid color layers have no tilings.
117 DCHECK_IMPLIES(raster_source_->IsSolidColor(), tilings_->num_tilings() == 0);
118 // The pending tree should only have a high res (and possibly low res) tiling.
119 DCHECK_LE(tilings_->num_tilings(),
120 layer_tree_impl()->create_low_res_tiling() ? 2u : 1u);
122 layer_impl->set_gpu_raster_max_texture_size(gpu_raster_max_texture_size_);
123 layer_impl->UpdateRasterSource(raster_source_, &invalidation_,
124 tilings_.get());
125 DCHECK(invalidation_.IsEmpty());
127 // After syncing a solid color layer, the active layer has no tilings.
128 DCHECK_IMPLIES(raster_source_->IsSolidColor(),
129 layer_impl->tilings_->num_tilings() == 0);
131 layer_impl->raster_page_scale_ = raster_page_scale_;
132 layer_impl->raster_device_scale_ = raster_device_scale_;
133 layer_impl->raster_source_scale_ = raster_source_scale_;
134 layer_impl->raster_contents_scale_ = raster_contents_scale_;
135 layer_impl->low_res_raster_contents_scale_ = low_res_raster_contents_scale_;
137 layer_impl->SanityCheckTilingState();
139 // We always need to push properties.
140 // See http://crbug.com/303943
141 // TODO(danakj): Stop always pushing properties since we don't swap tilings.
142 needs_push_properties_ = true;
145 void PictureLayerImpl::AppendQuads(RenderPass* render_pass,
146 AppendQuadsData* append_quads_data) {
147 // The bounds and the pile size may differ if the pile wasn't updated (ie.
148 // PictureLayer::Update didn't happen). In that case the pile will be empty.
149 DCHECK_IMPLIES(!raster_source_->GetSize().IsEmpty(),
150 bounds() == raster_source_->GetSize())
151 << " bounds " << bounds().ToString() << " pile "
152 << raster_source_->GetSize().ToString();
154 SharedQuadState* shared_quad_state =
155 render_pass->CreateAndAppendSharedQuadState();
157 if (raster_source_->IsSolidColor()) {
158 PopulateSharedQuadState(shared_quad_state);
160 AppendDebugBorderQuad(
161 render_pass, bounds(), shared_quad_state, append_quads_data);
163 SolidColorLayerImpl::AppendSolidQuads(
164 render_pass, draw_properties().occlusion_in_content_space,
165 shared_quad_state, visible_content_rect(),
166 raster_source_->GetSolidColor(), append_quads_data);
167 return;
170 float max_contents_scale = MaximumTilingContentsScale();
171 PopulateScaledSharedQuadState(shared_quad_state, max_contents_scale);
172 Occlusion scaled_occlusion =
173 draw_properties()
174 .occlusion_in_content_space.GetOcclusionWithGivenDrawTransform(
175 shared_quad_state->content_to_target_transform);
177 if (current_draw_mode_ == DRAW_MODE_RESOURCELESS_SOFTWARE) {
178 AppendDebugBorderQuad(
179 render_pass, shared_quad_state->content_bounds, shared_quad_state,
180 append_quads_data, DebugColors::DirectPictureBorderColor(),
181 DebugColors::DirectPictureBorderWidth(layer_tree_impl()));
183 gfx::Rect geometry_rect = shared_quad_state->visible_content_rect;
184 gfx::Rect opaque_rect = contents_opaque() ? geometry_rect : gfx::Rect();
185 gfx::Rect visible_geometry_rect =
186 scaled_occlusion.GetUnoccludedContentRect(geometry_rect);
187 if (visible_geometry_rect.IsEmpty())
188 return;
190 gfx::Rect quad_content_rect = shared_quad_state->visible_content_rect;
191 gfx::Size texture_size = quad_content_rect.size();
192 gfx::RectF texture_rect = gfx::RectF(texture_size);
194 PictureDrawQuad* quad =
195 render_pass->CreateAndAppendDrawQuad<PictureDrawQuad>();
196 quad->SetNew(shared_quad_state, geometry_rect, opaque_rect,
197 visible_geometry_rect, texture_rect, texture_size,
198 nearest_neighbor_, RGBA_8888, quad_content_rect,
199 max_contents_scale, raster_source_);
200 ValidateQuadResources(quad);
201 return;
204 AppendDebugBorderQuad(render_pass, shared_quad_state->content_bounds,
205 shared_quad_state, append_quads_data);
207 if (ShowDebugBorders()) {
208 for (PictureLayerTilingSet::CoverageIterator iter(
209 tilings_.get(), max_contents_scale,
210 shared_quad_state->visible_content_rect, ideal_contents_scale_);
211 iter; ++iter) {
212 SkColor color;
213 float width;
214 if (*iter && iter->draw_info().IsReadyToDraw()) {
215 TileDrawInfo::Mode mode = iter->draw_info().mode();
216 if (mode == TileDrawInfo::SOLID_COLOR_MODE) {
217 color = DebugColors::SolidColorTileBorderColor();
218 width = DebugColors::SolidColorTileBorderWidth(layer_tree_impl());
219 } else if (mode == TileDrawInfo::OOM_MODE) {
220 color = DebugColors::OOMTileBorderColor();
221 width = DebugColors::OOMTileBorderWidth(layer_tree_impl());
222 } else if (iter.resolution() == HIGH_RESOLUTION) {
223 color = DebugColors::HighResTileBorderColor();
224 width = DebugColors::HighResTileBorderWidth(layer_tree_impl());
225 } else if (iter.resolution() == LOW_RESOLUTION) {
226 color = DebugColors::LowResTileBorderColor();
227 width = DebugColors::LowResTileBorderWidth(layer_tree_impl());
228 } else if (iter->contents_scale() > max_contents_scale) {
229 color = DebugColors::ExtraHighResTileBorderColor();
230 width = DebugColors::ExtraHighResTileBorderWidth(layer_tree_impl());
231 } else {
232 color = DebugColors::ExtraLowResTileBorderColor();
233 width = DebugColors::ExtraLowResTileBorderWidth(layer_tree_impl());
235 } else {
236 color = DebugColors::MissingTileBorderColor();
237 width = DebugColors::MissingTileBorderWidth(layer_tree_impl());
240 DebugBorderDrawQuad* debug_border_quad =
241 render_pass->CreateAndAppendDrawQuad<DebugBorderDrawQuad>();
242 gfx::Rect geometry_rect = iter.geometry_rect();
243 gfx::Rect visible_geometry_rect = geometry_rect;
244 debug_border_quad->SetNew(shared_quad_state,
245 geometry_rect,
246 visible_geometry_rect,
247 color,
248 width);
252 // Keep track of the tilings that were used so that tilings that are
253 // unused can be considered for removal.
254 last_append_quads_tilings_.clear();
256 // Ignore missing tiles outside of viewport for tile priority. This is
257 // normally the same as draw viewport but can be independently overridden by
258 // embedders like Android WebView with SetExternalDrawConstraints.
259 gfx::Rect scaled_viewport_for_tile_priority = gfx::ScaleToEnclosingRect(
260 viewport_rect_for_tile_priority_in_content_space_, max_contents_scale);
262 size_t missing_tile_count = 0u;
263 size_t on_demand_missing_tile_count = 0u;
264 only_used_low_res_last_append_quads_ = true;
265 for (PictureLayerTilingSet::CoverageIterator iter(
266 tilings_.get(), max_contents_scale,
267 shared_quad_state->visible_content_rect, ideal_contents_scale_);
268 iter; ++iter) {
269 gfx::Rect geometry_rect = iter.geometry_rect();
270 gfx::Rect opaque_rect = contents_opaque() ? geometry_rect : gfx::Rect();
271 gfx::Rect visible_geometry_rect =
272 scaled_occlusion.GetUnoccludedContentRect(geometry_rect);
273 if (visible_geometry_rect.IsEmpty())
274 continue;
276 append_quads_data->visible_content_area +=
277 visible_geometry_rect.width() * visible_geometry_rect.height();
279 bool has_draw_quad = false;
280 if (*iter && iter->draw_info().IsReadyToDraw()) {
281 const TileDrawInfo& draw_info = iter->draw_info();
282 switch (draw_info.mode()) {
283 case TileDrawInfo::RESOURCE_MODE: {
284 gfx::RectF texture_rect = iter.texture_rect();
286 // The raster_contents_scale_ is the best scale that the layer is
287 // trying to produce, even though it may not be ideal. Since that's
288 // the best the layer can promise in the future, consider those as
289 // complete. But if a tile is ideal scale, we don't want to consider
290 // it incomplete and trying to replace it with a tile at a worse
291 // scale.
292 if (iter->contents_scale() != raster_contents_scale_ &&
293 iter->contents_scale() != ideal_contents_scale_ &&
294 geometry_rect.Intersects(scaled_viewport_for_tile_priority)) {
295 append_quads_data->num_incomplete_tiles++;
298 TileDrawQuad* quad =
299 render_pass->CreateAndAppendDrawQuad<TileDrawQuad>();
300 quad->SetNew(shared_quad_state, geometry_rect, opaque_rect,
301 visible_geometry_rect, draw_info.resource_id(),
302 texture_rect, draw_info.resource_size(),
303 draw_info.contents_swizzled(), nearest_neighbor_);
304 ValidateQuadResources(quad);
305 has_draw_quad = true;
306 break;
308 case TileDrawInfo::SOLID_COLOR_MODE: {
309 SolidColorDrawQuad* quad =
310 render_pass->CreateAndAppendDrawQuad<SolidColorDrawQuad>();
311 quad->SetNew(shared_quad_state, geometry_rect, visible_geometry_rect,
312 draw_info.solid_color(), false);
313 ValidateQuadResources(quad);
314 has_draw_quad = true;
315 break;
317 case TileDrawInfo::OOM_MODE:
318 break; // Checkerboard.
322 if (!has_draw_quad) {
323 if (draw_checkerboard_for_missing_tiles()) {
324 CheckerboardDrawQuad* quad =
325 render_pass->CreateAndAppendDrawQuad<CheckerboardDrawQuad>();
326 SkColor color = DebugColors::DefaultCheckerboardColor();
327 quad->SetNew(shared_quad_state, geometry_rect, visible_geometry_rect,
328 color, draw_properties().device_scale_factor);
329 } else {
330 SkColor color = SafeOpaqueBackgroundColor();
331 SolidColorDrawQuad* quad =
332 render_pass->CreateAndAppendDrawQuad<SolidColorDrawQuad>();
333 quad->SetNew(shared_quad_state,
334 geometry_rect,
335 visible_geometry_rect,
336 color,
337 false);
338 ValidateQuadResources(quad);
341 if (geometry_rect.Intersects(scaled_viewport_for_tile_priority)) {
342 append_quads_data->num_missing_tiles++;
343 ++missing_tile_count;
345 append_quads_data->approximated_visible_content_area +=
346 visible_geometry_rect.width() * visible_geometry_rect.height();
347 append_quads_data->checkerboarded_visible_content_area +=
348 visible_geometry_rect.width() * visible_geometry_rect.height();
349 continue;
352 if (iter.resolution() != HIGH_RESOLUTION) {
353 append_quads_data->approximated_visible_content_area +=
354 visible_geometry_rect.width() * visible_geometry_rect.height();
357 // If we have a draw quad, but it's not low resolution, then
358 // mark that we've used something other than low res to draw.
359 if (iter.resolution() != LOW_RESOLUTION)
360 only_used_low_res_last_append_quads_ = false;
362 if (last_append_quads_tilings_.empty() ||
363 last_append_quads_tilings_.back() != iter.CurrentTiling()) {
364 last_append_quads_tilings_.push_back(iter.CurrentTiling());
368 if (missing_tile_count) {
369 TRACE_EVENT_INSTANT2("cc",
370 "PictureLayerImpl::AppendQuads checkerboard",
371 TRACE_EVENT_SCOPE_THREAD,
372 "missing_tile_count",
373 missing_tile_count,
374 "on_demand_missing_tile_count",
375 on_demand_missing_tile_count);
378 // Aggressively remove any tilings that are not seen to save memory. Note
379 // that this is at the expense of doing cause more frequent re-painting. A
380 // better scheme would be to maintain a tighter visible_content_rect for the
381 // finer tilings.
382 CleanUpTilingsOnActiveLayer(last_append_quads_tilings_);
385 bool PictureLayerImpl::UpdateTiles(bool resourceless_software_draw) {
386 DCHECK_EQ(1.f, contents_scale_x());
387 DCHECK_EQ(1.f, contents_scale_y());
389 if (!resourceless_software_draw) {
390 visible_rect_for_tile_priority_ = visible_content_rect();
393 if (!CanHaveTilings()) {
394 ideal_page_scale_ = 0.f;
395 ideal_device_scale_ = 0.f;
396 ideal_contents_scale_ = 0.f;
397 ideal_source_scale_ = 0.f;
398 SanityCheckTilingState();
399 return false;
402 // Remove any non-ideal tilings that were not used last time we generated
403 // quads to save memory and processing time. Note that pending tree should
404 // only have one or two tilings (high and low res), so only clean up the
405 // active layer. This cleans it up here in case AppendQuads didn't run.
406 // If it did run, this would not remove any additional tilings.
407 if (layer_tree_impl()->IsActiveTree())
408 CleanUpTilingsOnActiveLayer(last_append_quads_tilings_);
410 UpdateIdealScales();
412 if (!raster_contents_scale_ || ShouldAdjustRasterScale()) {
413 RecalculateRasterScales();
414 AddTilingsForRasterScale();
417 DCHECK(raster_page_scale_);
418 DCHECK(raster_device_scale_);
419 DCHECK(raster_source_scale_);
420 DCHECK(raster_contents_scale_);
421 DCHECK(low_res_raster_contents_scale_);
423 was_screen_space_transform_animating_ =
424 draw_properties().screen_space_transform_is_animating;
426 if (draw_transform_is_animating())
427 raster_source_->SetShouldAttemptToUseDistanceFieldText();
429 double current_frame_time_in_seconds =
430 (layer_tree_impl()->CurrentBeginFrameArgs().frame_time -
431 base::TimeTicks()).InSecondsF();
432 UpdateViewportRectForTilePriorityInContentSpace();
434 // The tiling set can require tiles for activation any of the following
435 // conditions are true:
436 // - This layer produced a high-res or non-ideal-res tile last frame.
437 // - We're in requires high res to draw mode.
438 // - We're not in smoothness takes priority mode.
439 // To put different, the tiling set can't require tiles for activation if
440 // we're in smoothness mode and only used low-res or checkerboard to draw last
441 // frame and we don't need high res to draw.
443 // The reason for this is that we should be able to activate sooner and get a
444 // more up to date recording, so we don't run out of recording on the active
445 // tree.
446 bool can_require_tiles_for_activation =
447 !only_used_low_res_last_append_quads_ || RequiresHighResToDraw() ||
448 !layer_tree_impl()->SmoothnessTakesPriority();
450 static const Occlusion kEmptyOcclusion;
451 const Occlusion& occlusion_in_content_space =
452 layer_tree_impl()->settings().use_occlusion_for_tile_prioritization
453 ? draw_properties().occlusion_in_content_space
454 : kEmptyOcclusion;
456 // Pass |occlusion_in_content_space| for |occlusion_in_layer_space| since
457 // they are the same space in picture layer, as contents scale is always 1.
458 bool updated = tilings_->UpdateTilePriorities(
459 viewport_rect_for_tile_priority_in_content_space_, ideal_contents_scale_,
460 current_frame_time_in_seconds, occlusion_in_content_space,
461 can_require_tiles_for_activation);
462 return updated;
465 void PictureLayerImpl::UpdateViewportRectForTilePriorityInContentSpace() {
466 // If visible_rect_for_tile_priority_ is empty or
467 // viewport_rect_for_tile_priority is set to be different from the device
468 // viewport, try to inverse project the viewport into layer space and use
469 // that. Otherwise just use visible_rect_for_tile_priority_
470 gfx::Rect visible_rect_in_content_space = visible_rect_for_tile_priority_;
471 gfx::Rect viewport_rect_for_tile_priority =
472 layer_tree_impl()->ViewportRectForTilePriority();
473 if (visible_rect_in_content_space.IsEmpty() ||
474 layer_tree_impl()->DeviceViewport() != viewport_rect_for_tile_priority) {
475 gfx::Transform view_to_layer(gfx::Transform::kSkipInitialization);
476 if (screen_space_transform().GetInverse(&view_to_layer)) {
477 // Transform from view space to content space.
478 visible_rect_in_content_space =
479 gfx::ToEnclosingRect(MathUtil::ProjectClippedRect(
480 view_to_layer, viewport_rect_for_tile_priority));
483 viewport_rect_for_tile_priority_in_content_space_ =
484 visible_rect_in_content_space;
487 PictureLayerImpl* PictureLayerImpl::GetPendingOrActiveTwinLayer() const {
488 if (!twin_layer_ || !twin_layer_->IsOnActiveOrPendingTree())
489 return nullptr;
490 return twin_layer_;
493 PictureLayerImpl* PictureLayerImpl::GetRecycledTwinLayer() const {
494 if (!twin_layer_ || twin_layer_->IsOnActiveOrPendingTree())
495 return nullptr;
496 return twin_layer_;
499 void PictureLayerImpl::UpdateRasterSource(
500 scoped_refptr<RasterSource> raster_source,
501 Region* new_invalidation,
502 const PictureLayerTilingSet* pending_set) {
503 // The bounds and the pile size may differ if the pile wasn't updated (ie.
504 // PictureLayer::Update didn't happen). In that case the pile will be empty.
505 DCHECK_IMPLIES(!raster_source->GetSize().IsEmpty(),
506 bounds() == raster_source->GetSize())
507 << " bounds " << bounds().ToString() << " pile "
508 << raster_source->GetSize().ToString();
510 // The |raster_source_| is initially null, so have to check for that for the
511 // first frame.
512 bool could_have_tilings = raster_source_.get() && CanHaveTilings();
513 raster_source_.swap(raster_source);
515 // The |new_invalidation| must be cleared before updating tilings since they
516 // access the invalidation through the PictureLayerTilingClient interface.
517 invalidation_.Clear();
518 invalidation_.Swap(new_invalidation);
520 bool can_have_tilings = CanHaveTilings();
521 DCHECK_IMPLIES(
522 pending_set,
523 can_have_tilings == GetPendingOrActiveTwinLayer()->CanHaveTilings());
525 // Need to call UpdateTiles again if CanHaveTilings changed.
526 if (could_have_tilings != can_have_tilings)
527 layer_tree_impl()->set_needs_update_draw_properties();
529 if (!can_have_tilings) {
530 RemoveAllTilings();
531 return;
534 // We could do this after doing UpdateTiles, which would avoid doing this for
535 // tilings that are going to disappear on the pending tree (if scale changed).
536 // But that would also be more complicated, so we just do it here for now.
537 if (pending_set) {
538 tilings_->UpdateTilingsToCurrentRasterSourceForActivation(
539 raster_source_, pending_set, invalidation_, MinimumContentsScale(),
540 MaximumContentsScale());
541 } else {
542 tilings_->UpdateTilingsToCurrentRasterSourceForCommit(
543 raster_source_, invalidation_, MinimumContentsScale(),
544 MaximumContentsScale());
548 void PictureLayerImpl::UpdateCanUseLCDTextAfterCommit() {
549 // This function is only allowed to be called after commit, due to it not
550 // being smart about sharing tiles and because otherwise it would cause
551 // flashes by switching out tiles in place that may be currently on screen.
552 DCHECK(layer_tree_impl()->IsSyncTree());
554 // Don't allow the LCD text state to change once disabled.
555 if (!RasterSourceUsesLCDText())
556 return;
557 if (can_use_lcd_text() == RasterSourceUsesLCDText())
558 return;
560 // Raster sources are considered const, so in order to update the state
561 // a new one must be created and all tiles recreated.
562 scoped_refptr<RasterSource> new_raster_source =
563 raster_source_->CreateCloneWithoutLCDText();
564 raster_source_.swap(new_raster_source);
566 // Synthetically invalidate everything.
567 gfx::Rect bounds_rect(bounds());
568 invalidation_ = Region(bounds_rect);
569 tilings_->UpdateRasterSourceDueToLCDChange(raster_source_, invalidation_);
570 SetUpdateRect(bounds_rect);
572 DCHECK(!RasterSourceUsesLCDText());
575 bool PictureLayerImpl::RasterSourceUsesLCDText() const {
576 return raster_source_ ? raster_source_->CanUseLCDText()
577 : layer_tree_impl()->settings().can_use_lcd_text;
580 void PictureLayerImpl::NotifyTileStateChanged(const Tile* tile) {
581 if (layer_tree_impl()->IsActiveTree()) {
582 gfx::RectF layer_damage_rect =
583 gfx::ScaleRect(tile->content_rect(), 1.f / tile->contents_scale());
584 AddDamageRect(layer_damage_rect);
588 void PictureLayerImpl::DidBeginTracing() {
589 raster_source_->DidBeginTracing();
592 void PictureLayerImpl::ReleaseResources() {
593 // Recreate tilings with new settings, since some of those might change when
594 // we release resources.
595 tilings_ = nullptr;
596 ResetRasterScale();
599 void PictureLayerImpl::RecreateResources() {
600 tilings_ = CreatePictureLayerTilingSet();
602 // To avoid an edge case after lost context where the tree is up to date but
603 // the tilings have not been managed, request an update draw properties
604 // to force tilings to get managed.
605 layer_tree_impl()->set_needs_update_draw_properties();
608 skia::RefPtr<SkPicture> PictureLayerImpl::GetPicture() {
609 return raster_source_->GetFlattenedPicture();
612 Region PictureLayerImpl::GetInvalidationRegion() {
613 // |invalidation_| gives the invalidation contained in the source frame, but
614 // is not cleared after drawing from the layer. However, update_rect() is
615 // cleared once the invalidation is drawn, which is useful for debugging
616 // visualizations. This method intersects the two to give a more exact
617 // representation of what was invalidated that is cleared after drawing.
618 return IntersectRegions(invalidation_, update_rect());
621 ScopedTilePtr PictureLayerImpl::CreateTile(float contents_scale,
622 const gfx::Rect& content_rect) {
623 int flags = 0;
625 // We don't handle solid color masks, so we shouldn't bother analyzing those.
626 // Otherwise, always analyze to maximize memory savings.
627 if (!is_mask_)
628 flags = Tile::USE_PICTURE_ANALYSIS;
630 return layer_tree_impl()->tile_manager()->CreateTile(
631 content_rect.size(), content_rect, contents_scale, id(),
632 layer_tree_impl()->source_frame_number(), flags);
635 const Region* PictureLayerImpl::GetPendingInvalidation() {
636 if (layer_tree_impl()->IsPendingTree())
637 return &invalidation_;
638 if (layer_tree_impl()->IsRecycleTree())
639 return nullptr;
640 DCHECK(layer_tree_impl()->IsActiveTree());
641 if (PictureLayerImpl* twin_layer = GetPendingOrActiveTwinLayer())
642 return &twin_layer->invalidation_;
643 return nullptr;
646 const PictureLayerTiling* PictureLayerImpl::GetPendingOrActiveTwinTiling(
647 const PictureLayerTiling* tiling) const {
648 PictureLayerImpl* twin_layer = GetPendingOrActiveTwinLayer();
649 if (!twin_layer)
650 return nullptr;
651 return twin_layer->tilings_->FindTilingWithScale(tiling->contents_scale());
654 TilePriority::PriorityBin PictureLayerImpl::GetMaxTilePriorityBin() const {
655 if (!HasValidTilePriorities())
656 return TilePriority::EVENTUALLY;
657 return TilePriority::NOW;
660 bool PictureLayerImpl::RequiresHighResToDraw() const {
661 return layer_tree_impl()->RequiresHighResToDraw();
664 gfx::Rect PictureLayerImpl::GetEnclosingRectInTargetSpace() const {
665 return GetScaledEnclosingRectInTargetSpace(MaximumTilingContentsScale());
668 gfx::Size PictureLayerImpl::CalculateTileSize(
669 const gfx::Size& content_bounds) const {
670 int max_texture_size =
671 layer_tree_impl()->resource_provider()->max_texture_size();
673 if (is_mask_) {
674 // Masks are not tiled, so if we can't cover the whole mask with one tile,
675 // we shouldn't have such a tiling at all.
676 DCHECK_LE(content_bounds.width(), max_texture_size);
677 DCHECK_LE(content_bounds.height(), max_texture_size);
678 return content_bounds;
681 int default_tile_width = 0;
682 int default_tile_height = 0;
683 if (layer_tree_impl()->use_gpu_rasterization()) {
684 // For GPU rasterization, we pick an ideal tile size using the viewport
685 // so we don't need any settings. The current approach uses 4 tiles
686 // to cover the viewport vertically.
687 int viewport_width = gpu_raster_max_texture_size_.width();
688 int viewport_height = gpu_raster_max_texture_size_.height();
689 default_tile_width = viewport_width;
691 // Also, increase the height proportionally as the width decreases, and
692 // pad by our border texels to make the tiles exactly match the viewport.
693 int divisor = 4;
694 if (content_bounds.width() <= viewport_width / 2)
695 divisor = 2;
696 if (content_bounds.width() <= viewport_width / 4)
697 divisor = 1;
698 default_tile_height = RoundUp(viewport_height, divisor) / divisor;
700 // Grow default sizes to account for overlapping border texels.
701 default_tile_width += 2 * PictureLayerTiling::kBorderTexels;
702 default_tile_height += 2 * PictureLayerTiling::kBorderTexels;
704 default_tile_height =
705 std::max(default_tile_height, kMinHeightForGpuRasteredTile);
706 } else {
707 // For CPU rasterization we use tile-size settings.
708 const LayerTreeSettings& settings = layer_tree_impl()->settings();
709 int max_untiled_content_width = settings.max_untiled_layer_size.width();
710 int max_untiled_content_height = settings.max_untiled_layer_size.height();
711 default_tile_width = settings.default_tile_size.width();
712 default_tile_height = settings.default_tile_size.height();
714 // If the content width is small, increase tile size vertically.
715 // If the content height is small, increase tile size horizontally.
716 // If both are less than the untiled-size, use a single tile.
717 if (content_bounds.width() < default_tile_width)
718 default_tile_height = max_untiled_content_height;
719 if (content_bounds.height() < default_tile_height)
720 default_tile_width = max_untiled_content_width;
721 if (content_bounds.width() < max_untiled_content_width &&
722 content_bounds.height() < max_untiled_content_height) {
723 default_tile_height = max_untiled_content_height;
724 default_tile_width = max_untiled_content_width;
728 int tile_width = default_tile_width;
729 int tile_height = default_tile_height;
731 // Clamp the tile width/height to the content width/height to save space.
732 if (content_bounds.width() < default_tile_width) {
733 tile_width = std::min(tile_width, content_bounds.width());
734 tile_width = RoundUp(tile_width, kTileRoundUp);
735 tile_width = std::min(tile_width, default_tile_width);
737 if (content_bounds.height() < default_tile_height) {
738 tile_height = std::min(tile_height, content_bounds.height());
739 tile_height = RoundUp(tile_height, kTileRoundUp);
740 tile_height = std::min(tile_height, default_tile_height);
743 // Under no circumstance should we be larger than the max texture size.
744 tile_width = std::min(tile_width, max_texture_size);
745 tile_height = std::min(tile_height, max_texture_size);
746 return gfx::Size(tile_width, tile_height);
749 void PictureLayerImpl::GetContentsResourceId(
750 ResourceProvider::ResourceId* resource_id,
751 gfx::Size* resource_size) const {
752 // The bounds and the pile size may differ if the pile wasn't updated (ie.
753 // PictureLayer::Update didn't happen). In that case the pile will be empty.
754 DCHECK_IMPLIES(!raster_source_->GetSize().IsEmpty(),
755 bounds() == raster_source_->GetSize())
756 << " bounds " << bounds().ToString() << " pile "
757 << raster_source_->GetSize().ToString();
758 gfx::Rect content_rect(bounds());
759 PictureLayerTilingSet::CoverageIterator iter(
760 tilings_.get(), 1.f, content_rect, ideal_contents_scale_);
762 // Mask resource not ready yet.
763 if (!iter || !*iter) {
764 *resource_id = 0;
765 return;
768 // Masks only supported if they fit on exactly one tile.
769 DCHECK(iter.geometry_rect() == content_rect)
770 << "iter rect " << iter.geometry_rect().ToString() << " content rect "
771 << content_rect.ToString();
773 const TileDrawInfo& draw_info = iter->draw_info();
774 if (!draw_info.IsReadyToDraw() ||
775 draw_info.mode() != TileDrawInfo::RESOURCE_MODE) {
776 *resource_id = 0;
777 return;
780 *resource_id = draw_info.resource_id();
781 *resource_size = draw_info.resource_size();
784 void PictureLayerImpl::SetNearestNeighbor(bool nearest_neighbor) {
785 if (nearest_neighbor_ == nearest_neighbor)
786 return;
788 nearest_neighbor_ = nearest_neighbor;
789 NoteLayerPropertyChanged();
792 PictureLayerTiling* PictureLayerImpl::AddTiling(float contents_scale) {
793 DCHECK(CanHaveTilings());
794 DCHECK_GE(contents_scale, MinimumContentsScale());
795 DCHECK_LE(contents_scale, MaximumContentsScale());
796 DCHECK(raster_source_->HasRecordings());
797 return tilings_->AddTiling(contents_scale, raster_source_);
800 void PictureLayerImpl::RemoveAllTilings() {
801 tilings_->RemoveAllTilings();
802 // If there are no tilings, then raster scales are no longer meaningful.
803 ResetRasterScale();
806 void PictureLayerImpl::AddTilingsForRasterScale() {
807 // Reset all resolution enums on tilings, we'll be setting new values in this
808 // function.
809 tilings_->MarkAllTilingsNonIdeal();
811 PictureLayerTiling* high_res =
812 tilings_->FindTilingWithScale(raster_contents_scale_);
813 // We always need a high res tiling, so create one if it doesn't exist.
814 if (!high_res)
815 high_res = AddTiling(raster_contents_scale_);
817 // Try and find a low res tiling.
818 PictureLayerTiling* low_res = nullptr;
819 if (raster_contents_scale_ == low_res_raster_contents_scale_)
820 low_res = high_res;
821 else
822 low_res = tilings_->FindTilingWithScale(low_res_raster_contents_scale_);
824 // Only create new low res tilings when the transform is static. This
825 // prevents wastefully creating a paired low res tiling for every new high res
826 // tiling during a pinch or a CSS animation.
827 bool can_have_low_res = layer_tree_impl()->create_low_res_tiling();
828 bool needs_low_res = !low_res;
829 bool is_pinching = layer_tree_impl()->PinchGestureActive();
830 bool is_animating = draw_properties().screen_space_transform_is_animating;
831 if (can_have_low_res && needs_low_res && !is_pinching && !is_animating)
832 low_res = AddTiling(low_res_raster_contents_scale_);
834 // Set low-res if we have one.
835 if (low_res && low_res != high_res)
836 low_res->set_resolution(LOW_RESOLUTION);
838 // Make sure we always have one high-res (even if high == low).
839 high_res->set_resolution(HIGH_RESOLUTION);
841 if (layer_tree_impl()->IsPendingTree()) {
842 // On the pending tree, drop any tilings that are non-ideal since we don't
843 // need them to activate anyway.
844 tilings_->RemoveNonIdealTilings();
847 SanityCheckTilingState();
850 bool PictureLayerImpl::ShouldAdjustRasterScale() const {
851 if (was_screen_space_transform_animating_ !=
852 draw_properties().screen_space_transform_is_animating)
853 return true;
855 if (draw_properties().screen_space_transform_is_animating &&
856 raster_contents_scale_ != ideal_contents_scale_ &&
857 ShouldAdjustRasterScaleDuringScaleAnimations())
858 return true;
860 bool is_pinching = layer_tree_impl()->PinchGestureActive();
861 if (is_pinching && raster_page_scale_) {
862 // We change our raster scale when it is:
863 // - Higher than ideal (need a lower-res tiling available)
864 // - Too far from ideal (need a higher-res tiling available)
865 float ratio = ideal_page_scale_ / raster_page_scale_;
866 if (raster_page_scale_ > ideal_page_scale_ ||
867 ratio > kMaxScaleRatioDuringPinch)
868 return true;
871 if (!is_pinching) {
872 // When not pinching, match the ideal page scale factor.
873 if (raster_page_scale_ != ideal_page_scale_)
874 return true;
877 // Always match the ideal device scale factor.
878 if (raster_device_scale_ != ideal_device_scale_)
879 return true;
881 // When the source scale changes we want to match it, but not when animating
882 // or when we've fixed the scale in place.
883 if (!draw_properties().screen_space_transform_is_animating &&
884 !raster_source_scale_is_fixed_ &&
885 raster_source_scale_ != ideal_source_scale_)
886 return true;
888 if (raster_contents_scale_ > MaximumContentsScale())
889 return true;
890 if (raster_contents_scale_ < MinimumContentsScale())
891 return true;
893 return false;
896 void PictureLayerImpl::RecalculateRasterScales() {
897 float old_raster_contents_scale = raster_contents_scale_;
898 float old_raster_page_scale = raster_page_scale_;
899 float old_raster_source_scale = raster_source_scale_;
901 raster_device_scale_ = ideal_device_scale_;
902 raster_page_scale_ = ideal_page_scale_;
903 raster_source_scale_ = ideal_source_scale_;
904 raster_contents_scale_ = ideal_contents_scale_;
906 // If we're not animating, or leaving an animation, and the
907 // ideal_source_scale_ changes, then things are unpredictable, and we fix
908 // the raster_source_scale_ in place.
909 if (old_raster_source_scale &&
910 !draw_properties().screen_space_transform_is_animating &&
911 !was_screen_space_transform_animating_ &&
912 old_raster_source_scale != ideal_source_scale_)
913 raster_source_scale_is_fixed_ = true;
915 // TODO(danakj): Adjust raster source scale closer to ideal source scale at
916 // a throttled rate. Possibly make use of invalidation_.IsEmpty() on pending
917 // tree. This will allow CSS scale changes to get re-rastered at an
918 // appropriate rate. (crbug.com/413636)
919 if (raster_source_scale_is_fixed_) {
920 raster_contents_scale_ /= raster_source_scale_;
921 raster_source_scale_ = 1.f;
924 // During pinch we completely ignore the current ideal scale, and just use
925 // a multiple of the previous scale.
926 bool is_pinching = layer_tree_impl()->PinchGestureActive();
927 if (is_pinching && old_raster_contents_scale) {
928 // See ShouldAdjustRasterScale:
929 // - When zooming out, preemptively create new tiling at lower resolution.
930 // - When zooming in, approximate ideal using multiple of kMaxScaleRatio.
931 bool zooming_out = old_raster_page_scale > ideal_page_scale_;
932 float desired_contents_scale = old_raster_contents_scale;
933 if (zooming_out) {
934 while (desired_contents_scale > ideal_contents_scale_)
935 desired_contents_scale /= kMaxScaleRatioDuringPinch;
936 } else {
937 while (desired_contents_scale < ideal_contents_scale_)
938 desired_contents_scale *= kMaxScaleRatioDuringPinch;
940 raster_contents_scale_ = tilings_->GetSnappedContentsScale(
941 desired_contents_scale, kSnapToExistingTilingRatio);
942 raster_page_scale_ =
943 raster_contents_scale_ / raster_device_scale_ / raster_source_scale_;
946 // If we're not re-rasterizing during animation, rasterize at the maximum
947 // scale that will occur during the animation, if the maximum scale is
948 // known. However we want to avoid excessive memory use. If the scale is
949 // smaller than what we would choose otherwise, then it's always better off
950 // for us memory-wise. But otherwise, we don't choose a scale at which this
951 // layer's rastered content would become larger than the viewport.
952 if (draw_properties().screen_space_transform_is_animating &&
953 !ShouldAdjustRasterScaleDuringScaleAnimations()) {
954 bool can_raster_at_maximum_scale = false;
955 bool should_raster_at_starting_scale = false;
956 float maximum_scale = draw_properties().maximum_animation_contents_scale;
957 float starting_scale = draw_properties().starting_animation_contents_scale;
958 if (maximum_scale) {
959 gfx::Size bounds_at_maximum_scale = gfx::ToCeiledSize(
960 gfx::ScaleSize(raster_source_->GetSize(), maximum_scale));
961 int64 maximum_area = static_cast<int64>(bounds_at_maximum_scale.width()) *
962 static_cast<int64>(bounds_at_maximum_scale.height());
963 gfx::Size viewport = layer_tree_impl()->device_viewport_size();
964 int64 viewport_area = static_cast<int64>(viewport.width()) *
965 static_cast<int64>(viewport.height());
966 if (maximum_area <= viewport_area)
967 can_raster_at_maximum_scale = true;
969 if (starting_scale && starting_scale > maximum_scale) {
970 gfx::Size bounds_at_starting_scale = gfx::ToCeiledSize(
971 gfx::ScaleSize(raster_source_->GetSize(), starting_scale));
972 int64 start_area = static_cast<int64>(bounds_at_starting_scale.width()) *
973 static_cast<int64>(bounds_at_starting_scale.height());
974 gfx::Size viewport = layer_tree_impl()->device_viewport_size();
975 int64 viewport_area = static_cast<int64>(viewport.width()) *
976 static_cast<int64>(viewport.height());
977 if (start_area <= viewport_area)
978 should_raster_at_starting_scale = true;
980 // Use the computed scales for the raster scale directly, do not try to use
981 // the ideal scale here. The current ideal scale may be way too large in the
982 // case of an animation with scale, and will be constantly changing.
983 if (should_raster_at_starting_scale)
984 raster_contents_scale_ = starting_scale;
985 else if (can_raster_at_maximum_scale)
986 raster_contents_scale_ = maximum_scale;
987 else
988 raster_contents_scale_ = 1.f * ideal_page_scale_ * ideal_device_scale_;
991 raster_contents_scale_ =
992 std::max(raster_contents_scale_, MinimumContentsScale());
993 raster_contents_scale_ =
994 std::min(raster_contents_scale_, MaximumContentsScale());
995 DCHECK_GE(raster_contents_scale_, MinimumContentsScale());
996 DCHECK_LE(raster_contents_scale_, MaximumContentsScale());
998 // If this layer would create zero or one tiles at this content scale,
999 // don't create a low res tiling.
1000 gfx::Size raster_bounds = gfx::ToCeiledSize(
1001 gfx::ScaleSize(raster_source_->GetSize(), raster_contents_scale_));
1002 gfx::Size tile_size = CalculateTileSize(raster_bounds);
1003 bool tile_covers_bounds = tile_size.width() >= raster_bounds.width() &&
1004 tile_size.height() >= raster_bounds.height();
1005 if (tile_size.IsEmpty() || tile_covers_bounds) {
1006 low_res_raster_contents_scale_ = raster_contents_scale_;
1007 return;
1010 float low_res_factor =
1011 layer_tree_impl()->settings().low_res_contents_scale_factor;
1012 low_res_raster_contents_scale_ =
1013 std::max(raster_contents_scale_ * low_res_factor, MinimumContentsScale());
1014 DCHECK_LE(low_res_raster_contents_scale_, raster_contents_scale_);
1015 DCHECK_GE(low_res_raster_contents_scale_, MinimumContentsScale());
1016 DCHECK_LE(low_res_raster_contents_scale_, MaximumContentsScale());
1019 void PictureLayerImpl::CleanUpTilingsOnActiveLayer(
1020 const std::vector<PictureLayerTiling*>& used_tilings) {
1021 DCHECK(layer_tree_impl()->IsActiveTree());
1022 if (tilings_->num_tilings() == 0)
1023 return;
1025 float min_acceptable_high_res_scale = std::min(
1026 raster_contents_scale_, ideal_contents_scale_);
1027 float max_acceptable_high_res_scale = std::max(
1028 raster_contents_scale_, ideal_contents_scale_);
1030 PictureLayerImpl* twin = GetPendingOrActiveTwinLayer();
1031 if (twin && twin->CanHaveTilings()) {
1032 min_acceptable_high_res_scale = std::min(
1033 min_acceptable_high_res_scale,
1034 std::min(twin->raster_contents_scale_, twin->ideal_contents_scale_));
1035 max_acceptable_high_res_scale = std::max(
1036 max_acceptable_high_res_scale,
1037 std::max(twin->raster_contents_scale_, twin->ideal_contents_scale_));
1040 PictureLayerTilingSet* twin_set = twin ? twin->tilings_.get() : nullptr;
1041 // TODO(vmpstr): See if this step is required without tile sharing.
1042 PictureLayerImpl* recycled_twin = GetRecycledTwinLayer();
1043 PictureLayerTilingSet* recycled_twin_set =
1044 recycled_twin ? recycled_twin->tilings_.get() : nullptr;
1046 tilings_->CleanUpTilings(min_acceptable_high_res_scale,
1047 max_acceptable_high_res_scale, used_tilings,
1048 layer_tree_impl()->create_low_res_tiling(), twin_set,
1049 recycled_twin_set);
1051 if (recycled_twin_set && recycled_twin_set->num_tilings() == 0)
1052 recycled_twin->ResetRasterScale();
1054 DCHECK_GT(tilings_->num_tilings(), 0u);
1055 SanityCheckTilingState();
1058 float PictureLayerImpl::MinimumContentsScale() const {
1059 float setting_min = layer_tree_impl()->settings().minimum_contents_scale;
1061 // If the contents scale is less than 1 / width (also for height),
1062 // then it will end up having less than one pixel of content in that
1063 // dimension. Bump the minimum contents scale up in this case to prevent
1064 // this from happening.
1065 int min_dimension = std::min(raster_source_->GetSize().width(),
1066 raster_source_->GetSize().height());
1067 if (!min_dimension)
1068 return setting_min;
1070 return std::max(1.f / min_dimension, setting_min);
1073 float PictureLayerImpl::MaximumContentsScale() const {
1074 // Masks can not have tilings that would become larger than the
1075 // max_texture_size since they use a single tile for the entire
1076 // tiling. Other layers can have tilings of any scale.
1077 if (!is_mask_)
1078 return std::numeric_limits<float>::max();
1080 int max_texture_size =
1081 layer_tree_impl()->resource_provider()->max_texture_size();
1082 float max_scale_width =
1083 static_cast<float>(max_texture_size) / bounds().width();
1084 float max_scale_height =
1085 static_cast<float>(max_texture_size) / bounds().height();
1086 float max_scale = std::min(max_scale_width, max_scale_height);
1087 // We require that multiplying the layer size by the contents scale and
1088 // ceiling produces a value <= |max_texture_size|. Because for large layer
1089 // sizes floating point ambiguity may crop up, making the result larger or
1090 // smaller than expected, we use a slightly smaller floating point value for
1091 // the scale, to help ensure that the resulting content bounds will never end
1092 // up larger than |max_texture_size|.
1093 return nextafterf(max_scale, 0.f);
1096 void PictureLayerImpl::ResetRasterScale() {
1097 raster_page_scale_ = 0.f;
1098 raster_device_scale_ = 0.f;
1099 raster_source_scale_ = 0.f;
1100 raster_contents_scale_ = 0.f;
1101 low_res_raster_contents_scale_ = 0.f;
1102 raster_source_scale_is_fixed_ = false;
1105 bool PictureLayerImpl::CanHaveTilings() const {
1106 if (raster_source_->IsSolidColor())
1107 return false;
1108 if (!DrawsContent())
1109 return false;
1110 if (!raster_source_->HasRecordings())
1111 return false;
1112 // If the |raster_source_| has a recording it should have non-empty bounds.
1113 DCHECK(!raster_source_->GetSize().IsEmpty());
1114 if (MaximumContentsScale() < MinimumContentsScale())
1115 return false;
1116 return true;
1119 void PictureLayerImpl::SanityCheckTilingState() const {
1120 #if DCHECK_IS_ON()
1121 // Recycle tree doesn't have any restrictions.
1122 if (layer_tree_impl()->IsRecycleTree())
1123 return;
1125 if (!CanHaveTilings()) {
1126 DCHECK_EQ(0u, tilings_->num_tilings());
1127 return;
1129 if (tilings_->num_tilings() == 0)
1130 return;
1132 // We should only have one high res tiling.
1133 DCHECK_EQ(1, tilings_->NumHighResTilings());
1134 #endif
1137 bool PictureLayerImpl::ShouldAdjustRasterScaleDuringScaleAnimations() const {
1138 return layer_tree_impl()->use_gpu_rasterization();
1141 float PictureLayerImpl::MaximumTilingContentsScale() const {
1142 float max_contents_scale = tilings_->GetMaximumContentsScale();
1143 return std::max(max_contents_scale, MinimumContentsScale());
1146 scoped_ptr<PictureLayerTilingSet>
1147 PictureLayerImpl::CreatePictureLayerTilingSet() {
1148 const LayerTreeSettings& settings = layer_tree_impl()->settings();
1149 return PictureLayerTilingSet::Create(
1150 GetTree(), this, settings.tiling_interest_area_viewport_multiplier,
1151 layer_tree_impl()->use_gpu_rasterization()
1152 ? settings.gpu_rasterization_skewport_target_time_in_seconds
1153 : settings.skewport_target_time_in_seconds,
1154 settings.skewport_extrapolation_limit_in_content_pixels);
1157 void PictureLayerImpl::UpdateIdealScales() {
1158 DCHECK(CanHaveTilings());
1160 float min_contents_scale = MinimumContentsScale();
1161 DCHECK_GT(min_contents_scale, 0.f);
1162 float min_page_scale = layer_tree_impl()->min_page_scale_factor();
1163 DCHECK_GT(min_page_scale, 0.f);
1164 float min_device_scale = 1.f;
1165 float min_source_scale =
1166 min_contents_scale / min_page_scale / min_device_scale;
1168 float ideal_page_scale = draw_properties().page_scale_factor;
1169 float ideal_device_scale = draw_properties().device_scale_factor;
1170 float ideal_source_scale = draw_properties().ideal_contents_scale /
1171 ideal_page_scale / ideal_device_scale;
1172 ideal_contents_scale_ =
1173 std::max(draw_properties().ideal_contents_scale, min_contents_scale);
1174 ideal_page_scale_ = draw_properties().page_scale_factor;
1175 ideal_device_scale_ = draw_properties().device_scale_factor;
1176 ideal_source_scale_ = std::max(ideal_source_scale, min_source_scale);
1179 void PictureLayerImpl::GetDebugBorderProperties(
1180 SkColor* color,
1181 float* width) const {
1182 *color = DebugColors::TiledContentLayerBorderColor();
1183 *width = DebugColors::TiledContentLayerBorderWidth(layer_tree_impl());
1186 void PictureLayerImpl::GetAllPrioritizedTilesForTracing(
1187 std::vector<PrioritizedTile>* prioritized_tiles) const {
1188 if (!tilings_)
1189 return;
1190 tilings_->GetAllPrioritizedTilesForTracing(prioritized_tiles);
1193 void PictureLayerImpl::AsValueInto(
1194 base::trace_event::TracedValue* state) const {
1195 LayerImpl::AsValueInto(state);
1196 state->SetDouble("ideal_contents_scale", ideal_contents_scale_);
1197 state->SetDouble("geometry_contents_scale", MaximumTilingContentsScale());
1198 state->BeginArray("tilings");
1199 tilings_->AsValueInto(state);
1200 state->EndArray();
1202 MathUtil::AddToTracedValue("tile_priority_rect",
1203 viewport_rect_for_tile_priority_in_content_space_,
1204 state);
1205 MathUtil::AddToTracedValue("visible_rect", visible_content_rect(), state);
1207 state->BeginArray("pictures");
1208 raster_source_->AsValueInto(state);
1209 state->EndArray();
1211 state->BeginArray("invalidation");
1212 invalidation_.AsValueInto(state);
1213 state->EndArray();
1215 state->BeginArray("coverage_tiles");
1216 for (PictureLayerTilingSet::CoverageIterator iter(
1217 tilings_.get(), 1.f, gfx::Rect(raster_source_->GetSize()),
1218 ideal_contents_scale_);
1219 iter; ++iter) {
1220 state->BeginDictionary();
1222 MathUtil::AddToTracedValue("geometry_rect", iter.geometry_rect(), state);
1224 if (*iter)
1225 TracedValue::SetIDRef(*iter, state, "tile");
1227 state->EndDictionary();
1229 state->EndArray();
1232 size_t PictureLayerImpl::GPUMemoryUsageInBytes() const {
1233 return tilings_->GPUMemoryUsageInBytes();
1236 void PictureLayerImpl::RunMicroBenchmark(MicroBenchmarkImpl* benchmark) {
1237 benchmark->RunOnLayer(this);
1240 WhichTree PictureLayerImpl::GetTree() const {
1241 return layer_tree_impl()->IsActiveTree() ? ACTIVE_TREE : PENDING_TREE;
1244 bool PictureLayerImpl::IsOnActiveOrPendingTree() const {
1245 return !layer_tree_impl()->IsRecycleTree();
1248 bool PictureLayerImpl::HasValidTilePriorities() const {
1249 return IsOnActiveOrPendingTree() && IsDrawnRenderSurfaceLayerListMember();
1252 } // namespace cc