Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / cc / layers / picture_layer_impl.cc
blob740555f9f442773bbfe00af2525ebdbd9aa219b5
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/debug/debug_colors.h"
16 #include "cc/debug/micro_benchmark_impl.h"
17 #include "cc/debug/traced_value.h"
18 #include "cc/layers/append_quads_data.h"
19 #include "cc/layers/solid_color_layer_impl.h"
20 #include "cc/output/begin_frame_args.h"
21 #include "cc/quads/debug_border_draw_quad.h"
22 #include "cc/quads/picture_draw_quad.h"
23 #include "cc/quads/solid_color_draw_quad.h"
24 #include "cc/quads/tile_draw_quad.h"
25 #include "cc/tiles/tile_manager.h"
26 #include "cc/tiles/tiling_set_raster_queue_all.h"
27 #include "cc/trees/layer_tree_impl.h"
28 #include "cc/trees/occlusion.h"
29 #include "ui/gfx/geometry/quad_f.h"
30 #include "ui/gfx/geometry/rect_conversions.h"
31 #include "ui/gfx/geometry/size_conversions.h"
33 namespace {
34 // This must be > 1 as we multiply or divide by this to find a new raster
35 // scale during pinch.
36 const float kMaxScaleRatioDuringPinch = 2.0f;
38 // When creating a new tiling during pinch, snap to an existing
39 // tiling's scale if the desired scale is within this ratio.
40 const float kSnapToExistingTilingRatio = 1.2f;
42 // Even for really wide viewports, at some point GPU raster should use
43 // less than 4 tiles to fill the viewport. This is set to 256 as a
44 // sane minimum for now, but we might want to tune this for low-end.
45 const int kMinHeightForGpuRasteredTile = 256;
47 // When making odd-sized tiles, round them up to increase the chances
48 // of using the same tile size.
49 const int kTileRoundUp = 64;
51 } // namespace
53 namespace cc {
55 PictureLayerImpl::PictureLayerImpl(
56 LayerTreeImpl* tree_impl,
57 int id,
58 bool is_mask,
59 scoped_refptr<SyncedScrollOffset> scroll_offset)
60 : LayerImpl(tree_impl, id, scroll_offset),
61 twin_layer_(nullptr),
62 tilings_(CreatePictureLayerTilingSet()),
63 ideal_page_scale_(0.f),
64 ideal_device_scale_(0.f),
65 ideal_source_scale_(0.f),
66 ideal_contents_scale_(0.f),
67 raster_page_scale_(0.f),
68 raster_device_scale_(0.f),
69 raster_source_scale_(0.f),
70 raster_contents_scale_(0.f),
71 low_res_raster_contents_scale_(0.f),
72 raster_source_scale_is_fixed_(false),
73 was_screen_space_transform_animating_(false),
74 only_used_low_res_last_append_quads_(false),
75 is_mask_(is_mask),
76 nearest_neighbor_(false) {
77 layer_tree_impl()->RegisterPictureLayerImpl(this);
80 PictureLayerImpl::~PictureLayerImpl() {
81 if (twin_layer_)
82 twin_layer_->twin_layer_ = nullptr;
83 layer_tree_impl()->UnregisterPictureLayerImpl(this);
86 const char* PictureLayerImpl::LayerTypeAsString() const {
87 return "cc::PictureLayerImpl";
90 scoped_ptr<LayerImpl> PictureLayerImpl::CreateLayerImpl(
91 LayerTreeImpl* tree_impl) {
92 return PictureLayerImpl::Create(tree_impl, id(), is_mask_,
93 synced_scroll_offset());
96 void PictureLayerImpl::PushPropertiesTo(LayerImpl* base_layer) {
97 PictureLayerImpl* layer_impl = static_cast<PictureLayerImpl*>(base_layer);
98 DCHECK_EQ(layer_impl->is_mask_, is_mask_);
100 LayerImpl::PushPropertiesTo(base_layer);
102 // Twin relationships should never change once established.
103 DCHECK_IMPLIES(twin_layer_, twin_layer_ == layer_impl);
104 DCHECK_IMPLIES(twin_layer_, layer_impl->twin_layer_ == this);
105 // The twin relationship does not need to exist before the first
106 // PushPropertiesTo from pending to active layer since before that the active
107 // layer can not have a pile or tilings, it has only been created and inserted
108 // into the tree at that point.
109 twin_layer_ = layer_impl;
110 layer_impl->twin_layer_ = this;
112 layer_impl->SetNearestNeighbor(nearest_neighbor_);
114 // Solid color layers have no tilings.
115 DCHECK_IMPLIES(raster_source_->IsSolidColor(), tilings_->num_tilings() == 0);
116 // The pending tree should only have a high res (and possibly low res) tiling.
117 DCHECK_LE(tilings_->num_tilings(),
118 layer_tree_impl()->create_low_res_tiling() ? 2u : 1u);
120 layer_impl->set_gpu_raster_max_texture_size(gpu_raster_max_texture_size_);
121 layer_impl->UpdateRasterSource(raster_source_, &invalidation_,
122 tilings_.get());
123 DCHECK(invalidation_.IsEmpty());
125 // After syncing a solid color layer, the active layer has no tilings.
126 DCHECK_IMPLIES(raster_source_->IsSolidColor(),
127 layer_impl->tilings_->num_tilings() == 0);
129 layer_impl->raster_page_scale_ = raster_page_scale_;
130 layer_impl->raster_device_scale_ = raster_device_scale_;
131 layer_impl->raster_source_scale_ = raster_source_scale_;
132 layer_impl->raster_contents_scale_ = raster_contents_scale_;
133 layer_impl->low_res_raster_contents_scale_ = low_res_raster_contents_scale_;
135 layer_impl->SanityCheckTilingState();
137 // We always need to push properties.
138 // See http://crbug.com/303943
139 // TODO(danakj): Stop always pushing properties since we don't swap tilings.
140 needs_push_properties_ = true;
143 void PictureLayerImpl::AppendQuads(RenderPass* render_pass,
144 AppendQuadsData* append_quads_data) {
145 // The bounds and the pile size may differ if the pile wasn't updated (ie.
146 // PictureLayer::Update didn't happen). In that case the pile will be empty.
147 DCHECK_IMPLIES(!raster_source_->GetSize().IsEmpty(),
148 bounds() == raster_source_->GetSize())
149 << " bounds " << bounds().ToString() << " pile "
150 << raster_source_->GetSize().ToString();
152 SharedQuadState* shared_quad_state =
153 render_pass->CreateAndAppendSharedQuadState();
155 if (raster_source_->IsSolidColor()) {
156 PopulateSharedQuadState(shared_quad_state);
158 AppendDebugBorderQuad(
159 render_pass, bounds(), shared_quad_state, append_quads_data);
161 SolidColorLayerImpl::AppendSolidQuads(
162 render_pass, draw_properties().occlusion_in_content_space,
163 shared_quad_state, visible_layer_rect(),
164 raster_source_->GetSolidColor(), append_quads_data);
165 return;
168 float max_contents_scale = MaximumTilingContentsScale();
169 PopulateScaledSharedQuadState(shared_quad_state, max_contents_scale);
170 Occlusion scaled_occlusion =
171 draw_properties()
172 .occlusion_in_content_space.GetOcclusionWithGivenDrawTransform(
173 shared_quad_state->quad_to_target_transform);
175 if (current_draw_mode_ == DRAW_MODE_RESOURCELESS_SOFTWARE) {
176 AppendDebugBorderQuad(
177 render_pass, shared_quad_state->quad_layer_bounds, shared_quad_state,
178 append_quads_data, DebugColors::DirectPictureBorderColor(),
179 DebugColors::DirectPictureBorderWidth(layer_tree_impl()));
181 gfx::Rect geometry_rect = shared_quad_state->visible_quad_layer_rect;
182 gfx::Rect opaque_rect = contents_opaque() ? geometry_rect : gfx::Rect();
183 gfx::Rect visible_geometry_rect =
184 scaled_occlusion.GetUnoccludedContentRect(geometry_rect);
186 // The raster source may not be valid over the entire visible rect,
187 // and rastering outside of that may cause incorrect pixels.
188 gfx::Rect scaled_recorded_viewport = gfx::ScaleToEnclosingRect(
189 raster_source_->RecordedViewport(), max_contents_scale);
190 geometry_rect.Intersect(scaled_recorded_viewport);
191 opaque_rect.Intersect(scaled_recorded_viewport);
192 visible_geometry_rect.Intersect(scaled_recorded_viewport);
194 if (visible_geometry_rect.IsEmpty())
195 return;
197 DCHECK(raster_source_->HasRecordings());
198 gfx::Rect quad_content_rect = shared_quad_state->visible_quad_layer_rect;
199 gfx::Size texture_size = quad_content_rect.size();
200 gfx::RectF texture_rect = gfx::RectF(texture_size);
202 PictureDrawQuad* quad =
203 render_pass->CreateAndAppendDrawQuad<PictureDrawQuad>();
204 quad->SetNew(shared_quad_state, geometry_rect, opaque_rect,
205 visible_geometry_rect, texture_rect, texture_size,
206 nearest_neighbor_, RGBA_8888, quad_content_rect,
207 max_contents_scale, raster_source_);
208 ValidateQuadResources(quad);
209 return;
212 AppendDebugBorderQuad(render_pass, shared_quad_state->quad_layer_bounds,
213 shared_quad_state, append_quads_data);
215 if (ShowDebugBorders()) {
216 for (PictureLayerTilingSet::CoverageIterator iter(
217 tilings_.get(), max_contents_scale,
218 shared_quad_state->visible_quad_layer_rect, ideal_contents_scale_);
219 iter; ++iter) {
220 SkColor color;
221 float width;
222 if (*iter && iter->draw_info().IsReadyToDraw()) {
223 TileDrawInfo::Mode mode = iter->draw_info().mode();
224 if (mode == TileDrawInfo::SOLID_COLOR_MODE) {
225 color = DebugColors::SolidColorTileBorderColor();
226 width = DebugColors::SolidColorTileBorderWidth(layer_tree_impl());
227 } else if (mode == TileDrawInfo::OOM_MODE) {
228 color = DebugColors::OOMTileBorderColor();
229 width = DebugColors::OOMTileBorderWidth(layer_tree_impl());
230 } else if (iter.resolution() == HIGH_RESOLUTION) {
231 color = DebugColors::HighResTileBorderColor();
232 width = DebugColors::HighResTileBorderWidth(layer_tree_impl());
233 } else if (iter.resolution() == LOW_RESOLUTION) {
234 color = DebugColors::LowResTileBorderColor();
235 width = DebugColors::LowResTileBorderWidth(layer_tree_impl());
236 } else if (iter->contents_scale() > max_contents_scale) {
237 color = DebugColors::ExtraHighResTileBorderColor();
238 width = DebugColors::ExtraHighResTileBorderWidth(layer_tree_impl());
239 } else {
240 color = DebugColors::ExtraLowResTileBorderColor();
241 width = DebugColors::ExtraLowResTileBorderWidth(layer_tree_impl());
243 } else {
244 color = DebugColors::MissingTileBorderColor();
245 width = DebugColors::MissingTileBorderWidth(layer_tree_impl());
248 DebugBorderDrawQuad* debug_border_quad =
249 render_pass->CreateAndAppendDrawQuad<DebugBorderDrawQuad>();
250 gfx::Rect geometry_rect = iter.geometry_rect();
251 gfx::Rect visible_geometry_rect = geometry_rect;
252 debug_border_quad->SetNew(shared_quad_state,
253 geometry_rect,
254 visible_geometry_rect,
255 color,
256 width);
260 // Keep track of the tilings that were used so that tilings that are
261 // unused can be considered for removal.
262 last_append_quads_tilings_.clear();
264 // Ignore missing tiles outside of viewport for tile priority. This is
265 // normally the same as draw viewport but can be independently overridden by
266 // embedders like Android WebView with SetExternalDrawConstraints.
267 gfx::Rect scaled_viewport_for_tile_priority = gfx::ScaleToEnclosingRect(
268 viewport_rect_for_tile_priority_in_content_space_, max_contents_scale);
270 size_t missing_tile_count = 0u;
271 size_t on_demand_missing_tile_count = 0u;
272 only_used_low_res_last_append_quads_ = true;
273 for (PictureLayerTilingSet::CoverageIterator iter(
274 tilings_.get(), max_contents_scale,
275 shared_quad_state->visible_quad_layer_rect, ideal_contents_scale_);
276 iter; ++iter) {
277 gfx::Rect geometry_rect = iter.geometry_rect();
278 gfx::Rect opaque_rect = contents_opaque() ? geometry_rect : gfx::Rect();
279 gfx::Rect visible_geometry_rect =
280 scaled_occlusion.GetUnoccludedContentRect(geometry_rect);
281 if (visible_geometry_rect.IsEmpty())
282 continue;
284 append_quads_data->visible_layer_area +=
285 visible_geometry_rect.width() * visible_geometry_rect.height();
287 bool has_draw_quad = false;
288 if (*iter && iter->draw_info().IsReadyToDraw()) {
289 const TileDrawInfo& draw_info = iter->draw_info();
290 switch (draw_info.mode()) {
291 case TileDrawInfo::RESOURCE_MODE: {
292 gfx::RectF texture_rect = iter.texture_rect();
294 // The raster_contents_scale_ is the best scale that the layer is
295 // trying to produce, even though it may not be ideal. Since that's
296 // the best the layer can promise in the future, consider those as
297 // complete. But if a tile is ideal scale, we don't want to consider
298 // it incomplete and trying to replace it with a tile at a worse
299 // scale.
300 if (iter->contents_scale() != raster_contents_scale_ &&
301 iter->contents_scale() != ideal_contents_scale_ &&
302 geometry_rect.Intersects(scaled_viewport_for_tile_priority)) {
303 append_quads_data->num_incomplete_tiles++;
306 TileDrawQuad* quad =
307 render_pass->CreateAndAppendDrawQuad<TileDrawQuad>();
308 quad->SetNew(shared_quad_state, geometry_rect, opaque_rect,
309 visible_geometry_rect, draw_info.resource_id(),
310 texture_rect, draw_info.resource_size(),
311 draw_info.contents_swizzled(), nearest_neighbor_);
312 ValidateQuadResources(quad);
313 iter->draw_info().set_was_ever_used_to_draw();
314 has_draw_quad = true;
315 break;
317 case TileDrawInfo::SOLID_COLOR_MODE: {
318 SolidColorDrawQuad* quad =
319 render_pass->CreateAndAppendDrawQuad<SolidColorDrawQuad>();
320 quad->SetNew(shared_quad_state, geometry_rect, visible_geometry_rect,
321 draw_info.solid_color(), false);
322 ValidateQuadResources(quad);
323 iter->draw_info().set_was_ever_used_to_draw();
324 has_draw_quad = true;
325 break;
327 case TileDrawInfo::OOM_MODE:
328 break; // Checkerboard.
332 if (!has_draw_quad) {
333 // Checkerboard.
334 SkColor color = SafeOpaqueBackgroundColor();
335 if (ShowDebugBorders()) {
336 // Fill the whole tile with the missing tile color.
337 color = DebugColors::OOMTileBorderColor();
339 SolidColorDrawQuad* quad =
340 render_pass->CreateAndAppendDrawQuad<SolidColorDrawQuad>();
341 quad->SetNew(shared_quad_state, geometry_rect, visible_geometry_rect,
342 color, false);
343 ValidateQuadResources(quad);
345 if (geometry_rect.Intersects(scaled_viewport_for_tile_priority)) {
346 append_quads_data->num_missing_tiles++;
347 ++missing_tile_count;
349 append_quads_data->checkerboarded_visible_content_area +=
350 visible_geometry_rect.width() * visible_geometry_rect.height();
351 continue;
354 if (iter.resolution() != HIGH_RESOLUTION) {
355 append_quads_data->approximated_visible_content_area +=
356 visible_geometry_rect.width() * visible_geometry_rect.height();
359 // If we have a draw quad, but it's not low resolution, then
360 // mark that we've used something other than low res to draw.
361 if (iter.resolution() != LOW_RESOLUTION)
362 only_used_low_res_last_append_quads_ = false;
364 if (last_append_quads_tilings_.empty() ||
365 last_append_quads_tilings_.back() != iter.CurrentTiling()) {
366 last_append_quads_tilings_.push_back(iter.CurrentTiling());
370 if (missing_tile_count) {
371 TRACE_EVENT_INSTANT2("cc",
372 "PictureLayerImpl::AppendQuads checkerboard",
373 TRACE_EVENT_SCOPE_THREAD,
374 "missing_tile_count",
375 missing_tile_count,
376 "on_demand_missing_tile_count",
377 on_demand_missing_tile_count);
380 // Aggressively remove any tilings that are not seen to save memory. Note
381 // that this is at the expense of doing cause more frequent re-painting. A
382 // better scheme would be to maintain a tighter visible_layer_rect for the
383 // finer tilings.
384 CleanUpTilingsOnActiveLayer(last_append_quads_tilings_);
387 bool PictureLayerImpl::UpdateTiles(bool resourceless_software_draw) {
388 if (!resourceless_software_draw) {
389 visible_rect_for_tile_priority_ = visible_layer_rect();
392 if (!CanHaveTilings()) {
393 ideal_page_scale_ = 0.f;
394 ideal_device_scale_ = 0.f;
395 ideal_contents_scale_ = 0.f;
396 ideal_source_scale_ = 0.f;
397 SanityCheckTilingState();
398 return false;
401 // Remove any non-ideal tilings that were not used last time we generated
402 // quads to save memory and processing time. Note that pending tree should
403 // only have one or two tilings (high and low res), so only clean up the
404 // active layer. This cleans it up here in case AppendQuads didn't run.
405 // If it did run, this would not remove any additional tilings.
406 if (layer_tree_impl()->IsActiveTree())
407 CleanUpTilingsOnActiveLayer(last_append_quads_tilings_);
409 UpdateIdealScales();
411 if (!raster_contents_scale_ || ShouldAdjustRasterScale()) {
412 RecalculateRasterScales();
413 AddTilingsForRasterScale();
416 if (layer_tree_impl()->IsActiveTree())
417 AddLowResolutionTilingIfNeeded();
419 DCHECK(raster_page_scale_);
420 DCHECK(raster_device_scale_);
421 DCHECK(raster_source_scale_);
422 DCHECK(raster_contents_scale_);
423 DCHECK(low_res_raster_contents_scale_);
425 was_screen_space_transform_animating_ =
426 draw_properties().screen_space_transform_is_animating;
428 if (screen_space_transform_is_animating())
429 raster_source_->SetShouldAttemptToUseDistanceFieldText();
431 double current_frame_time_in_seconds =
432 (layer_tree_impl()->CurrentBeginFrameArgs().frame_time -
433 base::TimeTicks()).InSecondsF();
434 UpdateViewportRectForTilePriorityInContentSpace();
436 // The tiling set can require tiles for activation any of the following
437 // conditions are true:
438 // - This layer produced a high-res or non-ideal-res tile last frame.
439 // - We're in requires high res to draw mode.
440 // - We're not in smoothness takes priority mode.
441 // To put different, the tiling set can't require tiles for activation if
442 // we're in smoothness mode and only used low-res or checkerboard to draw last
443 // frame and we don't need high res to draw.
445 // The reason for this is that we should be able to activate sooner and get a
446 // more up to date recording, so we don't run out of recording on the active
447 // tree.
448 bool can_require_tiles_for_activation =
449 !only_used_low_res_last_append_quads_ || RequiresHighResToDraw() ||
450 !layer_tree_impl()->SmoothnessTakesPriority();
452 static const Occlusion kEmptyOcclusion;
453 const Occlusion& occlusion_in_content_space =
454 layer_tree_impl()->settings().use_occlusion_for_tile_prioritization
455 ? draw_properties().occlusion_in_content_space
456 : kEmptyOcclusion;
458 // Pass |occlusion_in_content_space| for |occlusion_in_layer_space| since
459 // they are the same space in picture layer, as contents scale is always 1.
460 bool updated = tilings_->UpdateTilePriorities(
461 viewport_rect_for_tile_priority_in_content_space_, ideal_contents_scale_,
462 current_frame_time_in_seconds, occlusion_in_content_space,
463 can_require_tiles_for_activation);
464 return updated;
467 void PictureLayerImpl::UpdateViewportRectForTilePriorityInContentSpace() {
468 // If visible_rect_for_tile_priority_ is empty or
469 // viewport_rect_for_tile_priority is set to be different from the device
470 // viewport, try to inverse project the viewport into layer space and use
471 // that. Otherwise just use visible_rect_for_tile_priority_
472 gfx::Rect visible_rect_in_content_space = visible_rect_for_tile_priority_;
473 gfx::Rect viewport_rect_for_tile_priority =
474 layer_tree_impl()->ViewportRectForTilePriority();
475 if (visible_rect_in_content_space.IsEmpty() ||
476 layer_tree_impl()->DeviceViewport() != viewport_rect_for_tile_priority) {
477 gfx::Transform view_to_layer(gfx::Transform::kSkipInitialization);
478 if (screen_space_transform().GetInverse(&view_to_layer)) {
479 // Transform from view space to content space.
480 visible_rect_in_content_space = MathUtil::ProjectEnclosingClippedRect(
481 view_to_layer, viewport_rect_for_tile_priority);
483 // We have to allow for a viewport that is outside of the layer bounds in
484 // order to compute tile priorities correctly for offscreen content that
485 // is going to make it on screen. However, we also have to limit the
486 // viewport since it can be very large due to screen_space_transforms. As
487 // a heuristic, we clip to bounds padded by skewport_extrapolation_limit *
488 // maximum tiling scale, since this should allow sufficient room for
489 // skewport calculations.
490 gfx::Rect padded_bounds(bounds());
491 int padding_amount = layer_tree_impl()
492 ->settings()
493 .skewport_extrapolation_limit_in_content_pixels *
494 MaximumTilingContentsScale();
495 padded_bounds.Inset(-padding_amount, -padding_amount);
496 visible_rect_in_content_space.Intersect(padded_bounds);
499 viewport_rect_for_tile_priority_in_content_space_ =
500 visible_rect_in_content_space;
503 PictureLayerImpl* PictureLayerImpl::GetPendingOrActiveTwinLayer() 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::Rect layer_damage_rect = gfx::ScaleToEnclosingRect(
593 tile->content_rect(), 1.f / tile->contents_scale());
594 AddDamageRect(layer_damage_rect);
596 if (tile->draw_info().NeedsRaster()) {
597 PictureLayerTiling* tiling =
598 tilings_->FindTilingWithScale(tile->contents_scale());
599 if (tiling)
600 tiling->set_all_tiles_done(false);
604 void PictureLayerImpl::DidBeginTracing() {
605 raster_source_->DidBeginTracing();
608 void PictureLayerImpl::ReleaseResources() {
609 // Recreate tilings with new settings, since some of those might change when
610 // we release resources.
611 tilings_ = nullptr;
612 ResetRasterScale();
615 void PictureLayerImpl::RecreateResources() {
616 tilings_ = CreatePictureLayerTilingSet();
618 // To avoid an edge case after lost context where the tree is up to date but
619 // the tilings have not been managed, request an update draw properties
620 // to force tilings to get managed.
621 layer_tree_impl()->set_needs_update_draw_properties();
624 skia::RefPtr<SkPicture> PictureLayerImpl::GetPicture() {
625 return raster_source_->GetFlattenedPicture();
628 Region PictureLayerImpl::GetInvalidationRegion() {
629 // |invalidation_| gives the invalidation contained in the source frame, but
630 // is not cleared after drawing from the layer. However, update_rect() is
631 // cleared once the invalidation is drawn, which is useful for debugging
632 // visualizations. This method intersects the two to give a more exact
633 // representation of what was invalidated that is cleared after drawing.
634 return IntersectRegions(invalidation_, update_rect());
637 ScopedTilePtr PictureLayerImpl::CreateTile(const Tile::CreateInfo& info) {
638 int flags = 0;
640 // We don't handle solid color masks, so we shouldn't bother analyzing those.
641 // Otherwise, always analyze to maximize memory savings.
642 if (!is_mask_)
643 flags = Tile::USE_PICTURE_ANALYSIS;
645 return layer_tree_impl()->tile_manager()->CreateTile(
646 info, id(), layer_tree_impl()->source_frame_number(), flags);
649 const Region* PictureLayerImpl::GetPendingInvalidation() {
650 if (layer_tree_impl()->IsPendingTree())
651 return &invalidation_;
652 if (layer_tree_impl()->IsRecycleTree())
653 return nullptr;
654 DCHECK(layer_tree_impl()->IsActiveTree());
655 if (PictureLayerImpl* twin_layer = GetPendingOrActiveTwinLayer())
656 return &twin_layer->invalidation_;
657 return nullptr;
660 const PictureLayerTiling* PictureLayerImpl::GetPendingOrActiveTwinTiling(
661 const PictureLayerTiling* tiling) const {
662 PictureLayerImpl* twin_layer = GetPendingOrActiveTwinLayer();
663 if (!twin_layer)
664 return nullptr;
665 return twin_layer->tilings_->FindTilingWithScale(tiling->contents_scale());
668 bool PictureLayerImpl::RequiresHighResToDraw() const {
669 return layer_tree_impl()->RequiresHighResToDraw();
672 gfx::Rect PictureLayerImpl::GetEnclosingRectInTargetSpace() const {
673 return GetScaledEnclosingRectInTargetSpace(MaximumTilingContentsScale());
676 gfx::Size PictureLayerImpl::CalculateTileSize(
677 const gfx::Size& content_bounds) const {
678 int max_texture_size =
679 layer_tree_impl()->resource_provider()->max_texture_size();
681 if (is_mask_) {
682 // Masks are not tiled, so if we can't cover the whole mask with one tile,
683 // we shouldn't have such a tiling at all.
684 DCHECK_LE(content_bounds.width(), max_texture_size);
685 DCHECK_LE(content_bounds.height(), max_texture_size);
686 return content_bounds;
689 int default_tile_width = 0;
690 int default_tile_height = 0;
691 if (layer_tree_impl()->use_gpu_rasterization()) {
692 // For GPU rasterization, we pick an ideal tile size using the viewport
693 // so we don't need any settings. The current approach uses 4 tiles
694 // to cover the viewport vertically.
695 int viewport_width = gpu_raster_max_texture_size_.width();
696 int viewport_height = gpu_raster_max_texture_size_.height();
697 default_tile_width = viewport_width;
699 // Also, increase the height proportionally as the width decreases, and
700 // pad by our border texels to make the tiles exactly match the viewport.
701 int divisor = 4;
702 if (content_bounds.width() <= viewport_width / 2)
703 divisor = 2;
704 if (content_bounds.width() <= viewport_width / 4)
705 divisor = 1;
706 default_tile_height =
707 MathUtil::UncheckedRoundUp(viewport_height, divisor) / divisor;
709 // Grow default sizes to account for overlapping border texels.
710 default_tile_width += 2 * PictureLayerTiling::kBorderTexels;
711 default_tile_height += 2 * PictureLayerTiling::kBorderTexels;
713 default_tile_height =
714 std::max(default_tile_height, kMinHeightForGpuRasteredTile);
715 } else {
716 // For CPU rasterization we use tile-size settings.
717 const LayerTreeSettings& settings = layer_tree_impl()->settings();
718 int max_untiled_content_width = settings.max_untiled_layer_size.width();
719 int max_untiled_content_height = settings.max_untiled_layer_size.height();
720 default_tile_width = settings.default_tile_size.width();
721 default_tile_height = settings.default_tile_size.height();
723 // If the content width is small, increase tile size vertically.
724 // If the content height is small, increase tile size horizontally.
725 // If both are less than the untiled-size, use a single tile.
726 if (content_bounds.width() < default_tile_width)
727 default_tile_height = max_untiled_content_height;
728 if (content_bounds.height() < default_tile_height)
729 default_tile_width = max_untiled_content_width;
730 if (content_bounds.width() < max_untiled_content_width &&
731 content_bounds.height() < max_untiled_content_height) {
732 default_tile_height = max_untiled_content_height;
733 default_tile_width = max_untiled_content_width;
737 int tile_width = default_tile_width;
738 int tile_height = default_tile_height;
740 // Clamp the tile width/height to the content width/height to save space.
741 if (content_bounds.width() < default_tile_width) {
742 tile_width = std::min(tile_width, content_bounds.width());
743 tile_width = MathUtil::UncheckedRoundUp(tile_width, kTileRoundUp);
744 tile_width = std::min(tile_width, default_tile_width);
746 if (content_bounds.height() < default_tile_height) {
747 tile_height = std::min(tile_height, content_bounds.height());
748 tile_height = MathUtil::UncheckedRoundUp(tile_height, kTileRoundUp);
749 tile_height = std::min(tile_height, default_tile_height);
752 // Under no circumstance should we be larger than the max texture size.
753 tile_width = std::min(tile_width, max_texture_size);
754 tile_height = std::min(tile_height, max_texture_size);
755 return gfx::Size(tile_width, tile_height);
758 void PictureLayerImpl::GetContentsResourceId(ResourceId* resource_id,
759 gfx::Size* resource_size) const {
760 // The bounds and the pile size may differ if the pile wasn't updated (ie.
761 // PictureLayer::Update didn't happen). In that case the pile will be empty.
762 DCHECK_IMPLIES(!raster_source_->GetSize().IsEmpty(),
763 bounds() == raster_source_->GetSize())
764 << " bounds " << bounds().ToString() << " pile "
765 << raster_source_->GetSize().ToString();
766 gfx::Rect content_rect(bounds());
767 PictureLayerTilingSet::CoverageIterator iter(
768 tilings_.get(), 1.f, content_rect, ideal_contents_scale_);
770 // Mask resource not ready yet.
771 if (!iter || !*iter) {
772 *resource_id = 0;
773 return;
776 // Masks only supported if they fit on exactly one tile.
777 DCHECK(iter.geometry_rect() == content_rect)
778 << "iter rect " << iter.geometry_rect().ToString() << " content rect "
779 << content_rect.ToString();
781 const TileDrawInfo& draw_info = iter->draw_info();
782 if (!draw_info.IsReadyToDraw() ||
783 draw_info.mode() != TileDrawInfo::RESOURCE_MODE) {
784 *resource_id = 0;
785 return;
788 *resource_id = draw_info.resource_id();
789 *resource_size = draw_info.resource_size();
792 void PictureLayerImpl::SetNearestNeighbor(bool nearest_neighbor) {
793 if (nearest_neighbor_ == nearest_neighbor)
794 return;
796 nearest_neighbor_ = nearest_neighbor;
797 NoteLayerPropertyChanged();
800 PictureLayerTiling* PictureLayerImpl::AddTiling(float contents_scale) {
801 DCHECK(CanHaveTilings());
802 DCHECK_GE(contents_scale, MinimumContentsScale());
803 DCHECK_LE(contents_scale, MaximumContentsScale());
804 DCHECK(raster_source_->HasRecordings());
805 return tilings_->AddTiling(contents_scale, raster_source_);
808 void PictureLayerImpl::RemoveAllTilings() {
809 tilings_->RemoveAllTilings();
810 // If there are no tilings, then raster scales are no longer meaningful.
811 ResetRasterScale();
814 void PictureLayerImpl::AddTilingsForRasterScale() {
815 // Reset all resolution enums on tilings, we'll be setting new values in this
816 // function.
817 tilings_->MarkAllTilingsNonIdeal();
819 PictureLayerTiling* high_res =
820 tilings_->FindTilingWithScale(raster_contents_scale_);
821 if (!high_res) {
822 // We always need a high res tiling, so create one if it doesn't exist.
823 high_res = AddTiling(raster_contents_scale_);
824 } else if (high_res->may_contain_low_resolution_tiles()) {
825 // If the tiling we find here was LOW_RESOLUTION previously, it may not be
826 // fully rastered, so destroy the old tiles.
827 high_res->Reset();
828 // Reset the flag now that we'll make it high res, it will have fully
829 // rastered content.
830 high_res->reset_may_contain_low_resolution_tiles();
832 high_res->set_resolution(HIGH_RESOLUTION);
834 if (layer_tree_impl()->IsPendingTree()) {
835 // On the pending tree, drop any tilings that are non-ideal since we don't
836 // need them to activate anyway.
837 tilings_->RemoveNonIdealTilings();
840 SanityCheckTilingState();
843 bool PictureLayerImpl::ShouldAdjustRasterScale() const {
844 if (was_screen_space_transform_animating_ !=
845 draw_properties().screen_space_transform_is_animating)
846 return true;
848 if (draw_properties().screen_space_transform_is_animating &&
849 raster_contents_scale_ != ideal_contents_scale_ &&
850 ShouldAdjustRasterScaleDuringScaleAnimations())
851 return true;
853 bool is_pinching = layer_tree_impl()->PinchGestureActive();
854 if (is_pinching && raster_page_scale_) {
855 // We change our raster scale when it is:
856 // - Higher than ideal (need a lower-res tiling available)
857 // - Too far from ideal (need a higher-res tiling available)
858 float ratio = ideal_page_scale_ / raster_page_scale_;
859 if (raster_page_scale_ > ideal_page_scale_ ||
860 ratio > kMaxScaleRatioDuringPinch)
861 return true;
864 if (!is_pinching) {
865 // When not pinching, match the ideal page scale factor.
866 if (raster_page_scale_ != ideal_page_scale_)
867 return true;
870 // Always match the ideal device scale factor.
871 if (raster_device_scale_ != ideal_device_scale_)
872 return true;
874 // When the source scale changes we want to match it, but not when animating
875 // or when we've fixed the scale in place.
876 if (!draw_properties().screen_space_transform_is_animating &&
877 !raster_source_scale_is_fixed_ &&
878 raster_source_scale_ != ideal_source_scale_)
879 return true;
881 if (raster_contents_scale_ > MaximumContentsScale())
882 return true;
883 if (raster_contents_scale_ < MinimumContentsScale())
884 return true;
886 return false;
889 void PictureLayerImpl::AddLowResolutionTilingIfNeeded() {
890 DCHECK(layer_tree_impl()->IsActiveTree());
892 if (!layer_tree_impl()->create_low_res_tiling())
893 return;
895 // We should have a high resolution tiling at raster_contents_scale, so if the
896 // low res one is the same then we shouldn't try to override this tiling by
897 // marking it as a low res.
898 if (raster_contents_scale_ == low_res_raster_contents_scale_)
899 return;
901 PictureLayerTiling* low_res =
902 tilings_->FindTilingWithScale(low_res_raster_contents_scale_);
903 DCHECK_IMPLIES(low_res, low_res->resolution() != HIGH_RESOLUTION);
905 // Only create new low res tilings when the transform is static. This
906 // prevents wastefully creating a paired low res tiling for every new high
907 // res tiling during a pinch or a CSS animation.
908 bool is_pinching = layer_tree_impl()->PinchGestureActive();
909 bool is_animating = draw_properties().screen_space_transform_is_animating;
910 if (!is_pinching && !is_animating) {
911 if (!low_res)
912 low_res = AddTiling(low_res_raster_contents_scale_);
913 low_res->set_resolution(LOW_RESOLUTION);
917 void PictureLayerImpl::RecalculateRasterScales() {
918 float old_raster_contents_scale = raster_contents_scale_;
919 float old_raster_page_scale = raster_page_scale_;
920 float old_raster_source_scale = raster_source_scale_;
922 raster_device_scale_ = ideal_device_scale_;
923 raster_page_scale_ = ideal_page_scale_;
924 raster_source_scale_ = ideal_source_scale_;
925 raster_contents_scale_ = ideal_contents_scale_;
927 // If we're not animating, or leaving an animation, and the
928 // ideal_source_scale_ changes, then things are unpredictable, and we fix
929 // the raster_source_scale_ in place.
930 if (old_raster_source_scale &&
931 !draw_properties().screen_space_transform_is_animating &&
932 !was_screen_space_transform_animating_ &&
933 old_raster_source_scale != ideal_source_scale_)
934 raster_source_scale_is_fixed_ = true;
936 // TODO(danakj): Adjust raster source scale closer to ideal source scale at
937 // a throttled rate. Possibly make use of invalidation_.IsEmpty() on pending
938 // tree. This will allow CSS scale changes to get re-rastered at an
939 // appropriate rate. (crbug.com/413636)
940 if (raster_source_scale_is_fixed_) {
941 raster_contents_scale_ /= raster_source_scale_;
942 raster_source_scale_ = 1.f;
945 // During pinch we completely ignore the current ideal scale, and just use
946 // a multiple of the previous scale.
947 bool is_pinching = layer_tree_impl()->PinchGestureActive();
948 if (is_pinching && old_raster_contents_scale) {
949 // See ShouldAdjustRasterScale:
950 // - When zooming out, preemptively create new tiling at lower resolution.
951 // - When zooming in, approximate ideal using multiple of kMaxScaleRatio.
952 bool zooming_out = old_raster_page_scale > ideal_page_scale_;
953 float desired_contents_scale = old_raster_contents_scale;
954 if (zooming_out) {
955 while (desired_contents_scale > ideal_contents_scale_)
956 desired_contents_scale /= kMaxScaleRatioDuringPinch;
957 } else {
958 while (desired_contents_scale < ideal_contents_scale_)
959 desired_contents_scale *= kMaxScaleRatioDuringPinch;
961 raster_contents_scale_ = tilings_->GetSnappedContentsScale(
962 desired_contents_scale, kSnapToExistingTilingRatio);
963 raster_page_scale_ =
964 raster_contents_scale_ / raster_device_scale_ / raster_source_scale_;
967 // If we're not re-rasterizing during animation, rasterize at the maximum
968 // scale that will occur during the animation, if the maximum scale is
969 // known. However we want to avoid excessive memory use. If the scale is
970 // smaller than what we would choose otherwise, then it's always better off
971 // for us memory-wise. But otherwise, we don't choose a scale at which this
972 // layer's rastered content would become larger than the viewport.
973 if (draw_properties().screen_space_transform_is_animating &&
974 !ShouldAdjustRasterScaleDuringScaleAnimations()) {
975 bool can_raster_at_maximum_scale = false;
976 bool should_raster_at_starting_scale = false;
977 float maximum_scale = draw_properties().maximum_animation_contents_scale;
978 float starting_scale = draw_properties().starting_animation_contents_scale;
979 if (maximum_scale) {
980 gfx::Size bounds_at_maximum_scale = gfx::ToCeiledSize(
981 gfx::ScaleSize(raster_source_->GetSize(), maximum_scale));
982 int64 maximum_area = static_cast<int64>(bounds_at_maximum_scale.width()) *
983 static_cast<int64>(bounds_at_maximum_scale.height());
984 gfx::Size viewport = layer_tree_impl()->device_viewport_size();
985 int64 viewport_area = static_cast<int64>(viewport.width()) *
986 static_cast<int64>(viewport.height());
987 if (maximum_area <= viewport_area)
988 can_raster_at_maximum_scale = true;
990 if (starting_scale && starting_scale > maximum_scale) {
991 gfx::Size bounds_at_starting_scale = gfx::ToCeiledSize(
992 gfx::ScaleSize(raster_source_->GetSize(), starting_scale));
993 int64 start_area = static_cast<int64>(bounds_at_starting_scale.width()) *
994 static_cast<int64>(bounds_at_starting_scale.height());
995 gfx::Size viewport = layer_tree_impl()->device_viewport_size();
996 int64 viewport_area = static_cast<int64>(viewport.width()) *
997 static_cast<int64>(viewport.height());
998 if (start_area <= viewport_area)
999 should_raster_at_starting_scale = true;
1001 // Use the computed scales for the raster scale directly, do not try to use
1002 // the ideal scale here. The current ideal scale may be way too large in the
1003 // case of an animation with scale, and will be constantly changing.
1004 if (should_raster_at_starting_scale)
1005 raster_contents_scale_ = starting_scale;
1006 else if (can_raster_at_maximum_scale)
1007 raster_contents_scale_ = maximum_scale;
1008 else
1009 raster_contents_scale_ = 1.f * ideal_page_scale_ * ideal_device_scale_;
1012 raster_contents_scale_ =
1013 std::max(raster_contents_scale_, MinimumContentsScale());
1014 raster_contents_scale_ =
1015 std::min(raster_contents_scale_, MaximumContentsScale());
1016 DCHECK_GE(raster_contents_scale_, MinimumContentsScale());
1017 DCHECK_LE(raster_contents_scale_, MaximumContentsScale());
1019 // If this layer would create zero or one tiles at this content scale,
1020 // don't create a low res tiling.
1021 gfx::Size raster_bounds = gfx::ToCeiledSize(
1022 gfx::ScaleSize(raster_source_->GetSize(), raster_contents_scale_));
1023 gfx::Size tile_size = CalculateTileSize(raster_bounds);
1024 bool tile_covers_bounds = tile_size.width() >= raster_bounds.width() &&
1025 tile_size.height() >= raster_bounds.height();
1026 if (tile_size.IsEmpty() || tile_covers_bounds) {
1027 low_res_raster_contents_scale_ = raster_contents_scale_;
1028 return;
1031 float low_res_factor =
1032 layer_tree_impl()->settings().low_res_contents_scale_factor;
1033 low_res_raster_contents_scale_ =
1034 std::max(raster_contents_scale_ * low_res_factor, MinimumContentsScale());
1035 DCHECK_LE(low_res_raster_contents_scale_, raster_contents_scale_);
1036 DCHECK_GE(low_res_raster_contents_scale_, MinimumContentsScale());
1037 DCHECK_LE(low_res_raster_contents_scale_, MaximumContentsScale());
1040 void PictureLayerImpl::CleanUpTilingsOnActiveLayer(
1041 const std::vector<PictureLayerTiling*>& used_tilings) {
1042 DCHECK(layer_tree_impl()->IsActiveTree());
1043 if (tilings_->num_tilings() == 0)
1044 return;
1046 float min_acceptable_high_res_scale = std::min(
1047 raster_contents_scale_, ideal_contents_scale_);
1048 float max_acceptable_high_res_scale = std::max(
1049 raster_contents_scale_, ideal_contents_scale_);
1051 PictureLayerImpl* twin = GetPendingOrActiveTwinLayer();
1052 if (twin && twin->CanHaveTilings()) {
1053 min_acceptable_high_res_scale = std::min(
1054 min_acceptable_high_res_scale,
1055 std::min(twin->raster_contents_scale_, twin->ideal_contents_scale_));
1056 max_acceptable_high_res_scale = std::max(
1057 max_acceptable_high_res_scale,
1058 std::max(twin->raster_contents_scale_, twin->ideal_contents_scale_));
1061 PictureLayerTilingSet* twin_set = twin ? twin->tilings_.get() : nullptr;
1062 tilings_->CleanUpTilings(min_acceptable_high_res_scale,
1063 max_acceptable_high_res_scale, used_tilings,
1064 twin_set);
1065 DCHECK_GT(tilings_->num_tilings(), 0u);
1066 SanityCheckTilingState();
1069 float PictureLayerImpl::MinimumContentsScale() const {
1070 float setting_min = layer_tree_impl()->settings().minimum_contents_scale;
1072 // If the contents scale is less than 1 / width (also for height),
1073 // then it will end up having less than one pixel of content in that
1074 // dimension. Bump the minimum contents scale up in this case to prevent
1075 // this from happening.
1076 int min_dimension = std::min(raster_source_->GetSize().width(),
1077 raster_source_->GetSize().height());
1078 if (!min_dimension)
1079 return setting_min;
1081 return std::max(1.f / min_dimension, setting_min);
1084 float PictureLayerImpl::MaximumContentsScale() const {
1085 // Masks can not have tilings that would become larger than the
1086 // max_texture_size since they use a single tile for the entire
1087 // tiling. Other layers can have tilings of any scale.
1088 if (!is_mask_)
1089 return std::numeric_limits<float>::max();
1091 int max_texture_size =
1092 layer_tree_impl()->resource_provider()->max_texture_size();
1093 float max_scale_width =
1094 static_cast<float>(max_texture_size) / bounds().width();
1095 float max_scale_height =
1096 static_cast<float>(max_texture_size) / bounds().height();
1097 float max_scale = std::min(max_scale_width, max_scale_height);
1098 // We require that multiplying the layer size by the contents scale and
1099 // ceiling produces a value <= |max_texture_size|. Because for large layer
1100 // sizes floating point ambiguity may crop up, making the result larger or
1101 // smaller than expected, we use a slightly smaller floating point value for
1102 // the scale, to help ensure that the resulting content bounds will never end
1103 // up larger than |max_texture_size|.
1104 return nextafterf(max_scale, 0.f);
1107 void PictureLayerImpl::ResetRasterScale() {
1108 raster_page_scale_ = 0.f;
1109 raster_device_scale_ = 0.f;
1110 raster_source_scale_ = 0.f;
1111 raster_contents_scale_ = 0.f;
1112 low_res_raster_contents_scale_ = 0.f;
1113 raster_source_scale_is_fixed_ = false;
1116 bool PictureLayerImpl::CanHaveTilings() const {
1117 if (raster_source_->IsSolidColor())
1118 return false;
1119 if (!DrawsContent())
1120 return false;
1121 if (!raster_source_->HasRecordings())
1122 return false;
1123 // If the |raster_source_| has a recording it should have non-empty bounds.
1124 DCHECK(!raster_source_->GetSize().IsEmpty());
1125 if (MaximumContentsScale() < MinimumContentsScale())
1126 return false;
1127 return true;
1130 void PictureLayerImpl::SanityCheckTilingState() const {
1131 #if DCHECK_IS_ON()
1132 if (!CanHaveTilings()) {
1133 DCHECK_EQ(0u, tilings_->num_tilings());
1134 return;
1136 if (tilings_->num_tilings() == 0)
1137 return;
1139 // We should only have one high res tiling.
1140 DCHECK_EQ(1, tilings_->NumHighResTilings());
1141 #endif
1144 bool PictureLayerImpl::ShouldAdjustRasterScaleDuringScaleAnimations() const {
1145 return layer_tree_impl()->use_gpu_rasterization();
1148 float PictureLayerImpl::MaximumTilingContentsScale() const {
1149 float max_contents_scale = tilings_->GetMaximumContentsScale();
1150 return std::max(max_contents_scale, MinimumContentsScale());
1153 scoped_ptr<PictureLayerTilingSet>
1154 PictureLayerImpl::CreatePictureLayerTilingSet() {
1155 const LayerTreeSettings& settings = layer_tree_impl()->settings();
1156 return PictureLayerTilingSet::Create(
1157 GetTree(), this, settings.tiling_interest_area_padding,
1158 layer_tree_impl()->use_gpu_rasterization()
1159 ? settings.gpu_rasterization_skewport_target_time_in_seconds
1160 : settings.skewport_target_time_in_seconds,
1161 settings.skewport_extrapolation_limit_in_content_pixels);
1164 void PictureLayerImpl::UpdateIdealScales() {
1165 DCHECK(CanHaveTilings());
1167 float min_contents_scale = MinimumContentsScale();
1168 DCHECK_GT(min_contents_scale, 0.f);
1170 ideal_page_scale_ = IsAffectedByPageScale()
1171 ? layer_tree_impl()->current_page_scale_factor()
1172 : 1.f;
1173 ideal_device_scale_ = layer_tree_impl()->device_scale_factor();
1174 ideal_contents_scale_ = std::max(GetIdealContentsScale(), min_contents_scale);
1175 ideal_source_scale_ =
1176 ideal_contents_scale_ / ideal_page_scale_ / ideal_device_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_layer_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