1 // Copyright 2011 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/trees/layer_tree_host_common.h"
9 #include "base/trace_event/trace_event.h"
10 #include "cc/base/math_util.h"
11 #include "cc/layers/heads_up_display_layer_impl.h"
12 #include "cc/layers/layer.h"
13 #include "cc/layers/layer_impl.h"
14 #include "cc/layers/layer_iterator.h"
15 #include "cc/layers/render_surface_impl.h"
16 #include "cc/trees/draw_property_utils.h"
17 #include "cc/trees/layer_tree_host.h"
18 #include "cc/trees/layer_tree_impl.h"
19 #include "ui/gfx/geometry/rect_conversions.h"
20 #include "ui/gfx/geometry/vector2d_conversions.h"
21 #include "ui/gfx/transform.h"
22 #include "ui/gfx/transform_util.h"
26 LayerTreeHostCommon::CalcDrawPropsMainInputs::CalcDrawPropsMainInputs(
28 const gfx::Size
& device_viewport_size
,
29 const gfx::Transform
& device_transform
,
30 float device_scale_factor
,
31 float page_scale_factor
,
32 const Layer
* page_scale_layer
,
33 const Layer
* inner_viewport_scroll_layer
,
34 const Layer
* outer_viewport_scroll_layer
)
35 : root_layer(root_layer
),
36 device_viewport_size(device_viewport_size
),
37 device_transform(device_transform
),
38 device_scale_factor(device_scale_factor
),
39 page_scale_factor(page_scale_factor
),
40 page_scale_layer(page_scale_layer
),
41 inner_viewport_scroll_layer(inner_viewport_scroll_layer
),
42 outer_viewport_scroll_layer(outer_viewport_scroll_layer
) {}
44 LayerTreeHostCommon::CalcDrawPropsMainInputs::CalcDrawPropsMainInputs(
46 const gfx::Size
& device_viewport_size
,
47 const gfx::Transform
& device_transform
)
48 : CalcDrawPropsMainInputs(root_layer
,
57 LayerTreeHostCommon::CalcDrawPropsMainInputs::CalcDrawPropsMainInputs(
59 const gfx::Size
& device_viewport_size
)
60 : CalcDrawPropsMainInputs(root_layer
,
64 LayerTreeHostCommon::CalcDrawPropsImplInputs::CalcDrawPropsImplInputs(
65 LayerImpl
* root_layer
,
66 const gfx::Size
& device_viewport_size
,
67 const gfx::Transform
& device_transform
,
68 float device_scale_factor
,
69 float page_scale_factor
,
70 const LayerImpl
* page_scale_layer
,
71 const LayerImpl
* inner_viewport_scroll_layer
,
72 const LayerImpl
* outer_viewport_scroll_layer
,
73 const gfx::Vector2dF
& elastic_overscroll
,
74 const LayerImpl
* elastic_overscroll_application_layer
,
76 bool can_use_lcd_text
,
77 bool layers_always_allowed_lcd_text
,
78 bool can_render_to_separate_surface
,
79 bool can_adjust_raster_scales
,
80 bool verify_property_trees
,
81 LayerImplList
* render_surface_layer_list
,
82 int current_render_surface_layer_list_id
,
83 PropertyTrees
* property_trees
)
84 : root_layer(root_layer
),
85 device_viewport_size(device_viewport_size
),
86 device_transform(device_transform
),
87 device_scale_factor(device_scale_factor
),
88 page_scale_factor(page_scale_factor
),
89 page_scale_layer(page_scale_layer
),
90 inner_viewport_scroll_layer(inner_viewport_scroll_layer
),
91 outer_viewport_scroll_layer(outer_viewport_scroll_layer
),
92 elastic_overscroll(elastic_overscroll
),
93 elastic_overscroll_application_layer(
94 elastic_overscroll_application_layer
),
95 max_texture_size(max_texture_size
),
96 can_use_lcd_text(can_use_lcd_text
),
97 layers_always_allowed_lcd_text(layers_always_allowed_lcd_text
),
98 can_render_to_separate_surface(can_render_to_separate_surface
),
99 can_adjust_raster_scales(can_adjust_raster_scales
),
100 verify_property_trees(verify_property_trees
),
101 render_surface_layer_list(render_surface_layer_list
),
102 current_render_surface_layer_list_id(
103 current_render_surface_layer_list_id
),
104 property_trees(property_trees
) {}
106 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting::
107 CalcDrawPropsImplInputsForTesting(LayerImpl
* root_layer
,
108 const gfx::Size
& device_viewport_size
,
109 const gfx::Transform
& device_transform
,
110 LayerImplList
* render_surface_layer_list
)
111 : CalcDrawPropsImplInputs(root_layer
,
112 device_viewport_size
,
121 std::numeric_limits
<int>::max() / 2,
127 render_surface_layer_list
,
129 GetPropertyTrees(root_layer
)) {
131 DCHECK(render_surface_layer_list
);
134 LayerTreeHostCommon::CalcDrawPropsImplInputsForTesting::
135 CalcDrawPropsImplInputsForTesting(LayerImpl
* root_layer
,
136 const gfx::Size
& device_viewport_size
,
137 LayerImplList
* render_surface_layer_list
)
138 : CalcDrawPropsImplInputsForTesting(root_layer
,
139 device_viewport_size
,
141 render_surface_layer_list
) {}
143 ScrollAndScaleSet::ScrollAndScaleSet()
144 : page_scale_delta(1.f
), top_controls_delta(0.f
) {
147 ScrollAndScaleSet::~ScrollAndScaleSet() {}
149 static gfx::Vector2dF
GetEffectiveScrollDelta(LayerImpl
* layer
) {
150 // Layer's scroll offset can have an integer part and fractional part.
151 // Due to Blink's limitation, it only counter-scrolls the position-fixed
152 // layer using the integer part of Layer's scroll offset.
153 // CC scrolls the layer using the full scroll offset, so we have to
154 // add the ScrollCompensationAdjustment (fractional part of the scroll
155 // offset) to the effective scroll delta which is used to counter-scroll
156 // the position-fixed layer.
157 gfx::Vector2dF scroll_delta
=
158 layer
->ScrollDelta() + layer
->ScrollCompensationAdjustment();
159 // The scroll parent's scroll delta is the amount we've scrolled on the
160 // compositor thread since the commit for this layer tree's source frame.
161 // we last reported to the main thread. I.e., it's the discrepancy between
162 // a scroll parent's scroll delta and offset, so we must add it here.
163 if (layer
->scroll_parent())
164 scroll_delta
+= layer
->scroll_parent()->ScrollDelta() +
165 layer
->ScrollCompensationAdjustment();
169 static gfx::ScrollOffset
GetEffectiveCurrentScrollOffset(LayerImpl
* layer
) {
170 gfx::ScrollOffset offset
= layer
->CurrentScrollOffset();
171 // The scroll parent's total scroll offset (scroll offset + scroll delta)
172 // can't be used because its scroll offset has already been applied to the
173 // scroll children's positions by the main thread layer positioning code.
174 if (layer
->scroll_parent())
175 offset
+= gfx::ScrollOffset(layer
->scroll_parent()->ScrollDelta());
179 inline gfx::Rect
CalculateVisibleRectWithCachedLayerRect(
180 const gfx::Rect
& target_surface_rect
,
181 const gfx::Rect
& layer_bound_rect
,
182 const gfx::Rect
& layer_rect_in_target_space
,
183 const gfx::Transform
& transform
) {
184 if (layer_rect_in_target_space
.IsEmpty())
187 // Is this layer fully contained within the target surface?
188 if (target_surface_rect
.Contains(layer_rect_in_target_space
))
189 return layer_bound_rect
;
191 // If the layer doesn't fill up the entire surface, then find the part of
192 // the surface rect where the layer could be visible. This avoids trying to
193 // project surface rect points that are behind the projection point.
194 gfx::Rect minimal_surface_rect
= target_surface_rect
;
195 minimal_surface_rect
.Intersect(layer_rect_in_target_space
);
197 if (minimal_surface_rect
.IsEmpty())
200 // Project the corners of the target surface rect into the layer space.
201 // This bounding rectangle may be larger than it needs to be (being
202 // axis-aligned), but is a reasonable filter on the space to consider.
203 // Non-invertible transforms will create an empty rect here.
205 gfx::Transform
surface_to_layer(gfx::Transform::kSkipInitialization
);
206 if (!transform
.GetInverse(&surface_to_layer
)) {
207 // Because we cannot use the surface bounds to determine what portion of
208 // the layer is visible, we must conservatively assume the full layer is
210 return layer_bound_rect
;
213 gfx::Rect layer_rect
= MathUtil::ProjectEnclosingClippedRect(
214 surface_to_layer
, minimal_surface_rect
);
215 layer_rect
.Intersect(layer_bound_rect
);
219 gfx::Rect
LayerTreeHostCommon::CalculateVisibleRect(
220 const gfx::Rect
& target_surface_rect
,
221 const gfx::Rect
& layer_bound_rect
,
222 const gfx::Transform
& transform
) {
223 gfx::Rect layer_in_surface_space
=
224 MathUtil::MapEnclosingClippedRect(transform
, layer_bound_rect
);
225 return CalculateVisibleRectWithCachedLayerRect(
226 target_surface_rect
, layer_bound_rect
, layer_in_surface_space
, transform
);
229 static const LayerImpl
* NextTargetSurface(const LayerImpl
* layer
) {
230 return layer
->parent() ? layer
->parent()->render_target() : 0;
233 // Given two layers, this function finds their respective render targets and,
234 // computes a change of basis translation. It does this by accumulating the
235 // translation components of the draw transforms of each target between the
236 // ancestor and descendant. These transforms must be 2D translations, and this
237 // requirement is enforced at every step.
238 static gfx::Vector2dF
ComputeChangeOfBasisTranslation(
239 const LayerImpl
& ancestor_layer
,
240 const LayerImpl
& descendant_layer
) {
241 DCHECK(descendant_layer
.HasAncestor(&ancestor_layer
));
242 const LayerImpl
* descendant_target
= descendant_layer
.render_target();
243 DCHECK(descendant_target
);
244 const LayerImpl
* ancestor_target
= ancestor_layer
.render_target();
245 DCHECK(ancestor_target
);
247 gfx::Vector2dF translation
;
248 for (const LayerImpl
* target
= descendant_target
; target
!= ancestor_target
;
249 target
= NextTargetSurface(target
)) {
250 const gfx::Transform
& trans
= target
->render_surface()->draw_transform();
251 // Ensure that this translation is truly 2d.
252 DCHECK(trans
.IsIdentityOrTranslation());
253 DCHECK_EQ(0.f
, trans
.matrix().get(2, 3));
254 translation
+= trans
.To2dTranslation();
260 enum TranslateRectDirection
{
261 TRANSLATE_RECT_DIRECTION_TO_ANCESTOR
,
262 TRANSLATE_RECT_DIRECTION_TO_DESCENDANT
265 static gfx::Rect
TranslateRectToTargetSpace(const LayerImpl
& ancestor_layer
,
266 const LayerImpl
& descendant_layer
,
267 const gfx::Rect
& rect
,
268 TranslateRectDirection direction
) {
269 gfx::Vector2dF translation
=
270 ComputeChangeOfBasisTranslation(ancestor_layer
, descendant_layer
);
271 if (direction
== TRANSLATE_RECT_DIRECTION_TO_DESCENDANT
)
272 translation
.Scale(-1.f
);
273 return gfx::ToEnclosingRect(
274 gfx::RectF(rect
.origin() + translation
, rect
.size()));
277 // Attempts to update the clip rects for the given layer. If the layer has a
278 // clip_parent, it may not inherit its immediate ancestor's clip.
279 static void UpdateClipRectsForClipChild(
280 const LayerImpl
* layer
,
281 gfx::Rect
* clip_rect_in_parent_target_space
,
282 bool* subtree_should_be_clipped
) {
283 // If the layer has no clip_parent, or the ancestor is the same as its actual
284 // parent, then we don't need special clip rects. Bail now and leave the out
285 // parameters untouched.
286 const LayerImpl
* clip_parent
= layer
->scroll_parent();
289 clip_parent
= layer
->clip_parent();
291 if (!clip_parent
|| clip_parent
== layer
->parent())
294 // The root layer is never a clip child.
295 DCHECK(layer
->parent());
297 // Grab the cached values.
298 *clip_rect_in_parent_target_space
= clip_parent
->clip_rect();
299 *subtree_should_be_clipped
= clip_parent
->is_clipped();
301 // We may have to project the clip rect into our parent's target space. Note,
302 // it must be our parent's target space, not ours. For one, we haven't
303 // computed our transforms, so we couldn't put it in our space yet even if we
304 // wanted to. But more importantly, this matches the expectations of
305 // CalculateDrawPropertiesInternal. If we, say, create a render surface, these
306 // clip rects will want to be in its target space, not ours.
307 if (clip_parent
== layer
->clip_parent()) {
308 *clip_rect_in_parent_target_space
= TranslateRectToTargetSpace(
309 *clip_parent
, *layer
->parent(), *clip_rect_in_parent_target_space
,
310 TRANSLATE_RECT_DIRECTION_TO_DESCENDANT
);
312 // If we're being clipped by our scroll parent, we must translate through
313 // our common ancestor. This happens to be our parent, so it is sufficent to
314 // translate from our clip parent's space to the space of its ancestor (our
316 *clip_rect_in_parent_target_space
= TranslateRectToTargetSpace(
317 *layer
->parent(), *clip_parent
, *clip_rect_in_parent_target_space
,
318 TRANSLATE_RECT_DIRECTION_TO_ANCESTOR
);
322 // We collect an accumulated drawable content rect per render surface.
323 // Typically, a layer will contribute to only one surface, the surface
324 // associated with its render target. Clip children, however, may affect
325 // several surfaces since there may be several surfaces between the clip child
328 // NB: we accumulate the layer's *clipped* drawable content rect.
329 struct AccumulatedSurfaceState
{
330 explicit AccumulatedSurfaceState(LayerImpl
* render_target
)
331 : render_target(render_target
) {}
333 // The accumulated drawable content rect for the surface associated with the
334 // given |render_target|.
335 gfx::Rect drawable_content_rect
;
337 // The target owning the surface. (We hang onto the target rather than the
338 // surface so that we can DCHECK that the surface's draw transform is simply
339 // a translation when |render_target| reports that it has no unclipped
341 LayerImpl
* render_target
;
344 template <typename LayerType
>
345 static inline bool IsRootLayer(LayerType
* layer
) {
346 return !layer
->parent();
349 void UpdateAccumulatedSurfaceState(
351 const gfx::Rect
& drawable_content_rect
,
352 std::vector
<AccumulatedSurfaceState
>* accumulated_surface_state
) {
353 if (IsRootLayer(layer
))
356 // We will apply our drawable content rect to the accumulated rects for all
357 // surfaces between us and |render_target| (inclusive). This is either our
358 // clip parent's target if we are a clip child, or else simply our parent's
359 // target. We use our parent's target because we're either the owner of a
360 // render surface and we'll want to add our rect to our *surface's* target, or
361 // we're not and our target is the same as our parent's. In both cases, the
362 // parent's target gives us what we want.
363 LayerImpl
* render_target
= layer
->clip_parent()
364 ? layer
->clip_parent()->render_target()
365 : layer
->parent()->render_target();
367 // If the layer owns a surface, then the content rect is in the wrong space.
368 // Instead, we will use the surface's DrawableContentRect which is in target
369 // space as required.
370 gfx::Rect target_rect
= drawable_content_rect
;
371 if (layer
->render_surface()) {
373 gfx::ToEnclosedRect(layer
->render_surface()->DrawableContentRect());
376 if (render_target
->is_clipped()) {
377 gfx::Rect clip_rect
= render_target
->clip_rect();
378 // If the layer has a clip parent, the clip rect may be in the wrong space,
379 // so we'll need to transform it before it is applied.
380 if (layer
->clip_parent()) {
382 TranslateRectToTargetSpace(*layer
->clip_parent(), *layer
, clip_rect
,
383 TRANSLATE_RECT_DIRECTION_TO_DESCENDANT
);
385 target_rect
.Intersect(clip_rect
);
388 // We must have at least one entry in the vector for the root.
389 DCHECK_LT(0ul, accumulated_surface_state
->size());
391 typedef std::vector
<AccumulatedSurfaceState
> AccumulatedSurfaceStateVector
;
392 typedef AccumulatedSurfaceStateVector::reverse_iterator
393 AccumulatedSurfaceStateIterator
;
394 AccumulatedSurfaceStateIterator current_state
=
395 accumulated_surface_state
->rbegin();
397 // Add this rect to the accumulated content rect for all surfaces until we
398 // reach the target surface.
399 bool found_render_target
= false;
400 for (; current_state
!= accumulated_surface_state
->rend(); ++current_state
) {
401 current_state
->drawable_content_rect
.Union(target_rect
);
403 // If we've reached |render_target| our work is done and we can bail.
404 if (current_state
->render_target
== render_target
) {
405 found_render_target
= true;
409 // Transform rect from the current target's space to the next.
410 LayerImpl
* current_target
= current_state
->render_target
;
411 DCHECK(current_target
->render_surface());
412 const gfx::Transform
& current_draw_transform
=
413 current_target
->render_surface()->draw_transform();
415 // If we have unclipped descendants, the draw transform is a translation.
416 DCHECK_IMPLIES(current_target
->num_unclipped_descendants(),
417 current_draw_transform
.IsIdentityOrTranslation());
419 target_rect
= gfx::ToEnclosingRect(
420 MathUtil::MapClippedRect(current_draw_transform
, target_rect
));
423 // It is an error to not reach |render_target|. If this happens, it means that
424 // either the clip parent is not an ancestor of the clip child or the surface
425 // state vector is empty, both of which should be impossible.
426 DCHECK(found_render_target
);
429 template <typename LayerType
>
430 static inline bool LayerIsInExisting3DRenderingContext(LayerType
* layer
) {
431 return layer
->Is3dSorted() && layer
->parent() &&
432 layer
->parent()->Is3dSorted() &&
433 (layer
->parent()->sorting_context_id() == layer
->sorting_context_id());
436 static bool IsRootLayerOfNewRenderingContext(LayerImpl
* layer
) {
438 return !layer
->parent()->Is3dSorted() && layer
->Is3dSorted();
440 return layer
->Is3dSorted();
443 static bool IsLayerBackFaceVisible(LayerImpl
* layer
) {
444 // The current W3C spec on CSS transforms says that backface visibility should
445 // be determined differently depending on whether the layer is in a "3d
446 // rendering context" or not. For Chromium code, we can determine whether we
447 // are in a 3d rendering context by checking if the parent preserves 3d.
449 if (LayerIsInExisting3DRenderingContext(layer
))
450 return layer
->draw_transform().IsBackFaceVisible();
452 // In this case, either the layer establishes a new 3d rendering context, or
453 // is not in a 3d rendering context at all.
454 return layer
->transform().IsBackFaceVisible();
457 static bool IsSurfaceBackFaceVisible(LayerImpl
* layer
,
458 const gfx::Transform
& draw_transform
) {
459 if (LayerIsInExisting3DRenderingContext(layer
))
460 return draw_transform
.IsBackFaceVisible();
462 if (IsRootLayerOfNewRenderingContext(layer
))
463 return layer
->transform().IsBackFaceVisible();
465 // If the render_surface is not part of a new or existing rendering context,
466 // then the layers that contribute to this surface will decide back-face
467 // visibility for themselves.
471 template <typename LayerType
>
472 static inline bool LayerClipsSubtree(LayerType
* layer
) {
473 return layer
->masks_to_bounds() || layer
->mask_layer();
476 static gfx::Rect
CalculateVisibleLayerRect(
478 const gfx::Rect
& clip_rect_of_target_surface_in_target_space
,
479 const gfx::Rect
& layer_rect_in_target_space
) {
480 DCHECK(layer
->render_target());
482 // Nothing is visible if the layer bounds are empty.
483 if (!layer
->DrawsContent() || layer
->bounds().IsEmpty() ||
484 layer
->drawable_content_rect().IsEmpty())
487 // Compute visible bounds in target surface space.
488 gfx::Rect visible_rect_in_target_surface_space
=
489 layer
->drawable_content_rect();
491 if (layer
->render_target()->render_surface()->is_clipped()) {
492 // The |layer| L has a target T which owns a surface Ts. The surface Ts
495 // In this case the target surface Ts does clip the layer L that contributes
496 // to it. So, we have to convert the clip rect of Ts from the target space
497 // of Ts (that is the space of TsT), to the current render target's space
498 // (that is the space of T). This conversion is done outside this function
499 // so that it can be cached instead of computing it redundantly for every
501 visible_rect_in_target_surface_space
.Intersect(
502 clip_rect_of_target_surface_in_target_space
);
505 if (visible_rect_in_target_surface_space
.IsEmpty())
508 return CalculateVisibleRectWithCachedLayerRect(
509 visible_rect_in_target_surface_space
, gfx::Rect(layer
->bounds()),
510 layer_rect_in_target_space
, layer
->draw_transform());
513 static bool LayerShouldBeSkipped(LayerImpl
* layer
, bool layer_is_drawn
) {
514 // Layers can be skipped if any of these conditions are met.
515 // - is not drawn due to it or one of its ancestors being hidden (or having
516 // no copy requests).
517 // - does not draw content.
519 // - has empty bounds
520 // - the layer is not double-sided, but its back face is visible.
522 // Some additional conditions need to be computed at a later point after the
523 // recursion is finished.
524 // - the intersection of render_surface content and layer clip_rect is empty
525 // - the visible_layer_rect is empty
527 // Note, if the layer should not have been drawn due to being fully
528 // transparent, we would have skipped the entire subtree and never made it
529 // into this function, so it is safe to omit this check here.
534 if (!layer
->DrawsContent() || layer
->bounds().IsEmpty())
537 LayerImpl
* backface_test_layer
= layer
;
538 if (layer
->use_parent_backface_visibility()) {
539 DCHECK(layer
->parent());
540 DCHECK(!layer
->parent()->use_parent_backface_visibility());
541 backface_test_layer
= layer
->parent();
544 // The layer should not be drawn if (1) it is not double-sided and (2) the
545 // back of the layer is known to be facing the screen.
546 if (!backface_test_layer
->double_sided() &&
547 IsLayerBackFaceVisible(backface_test_layer
))
553 template <typename LayerType
>
554 static bool HasInvertibleOrAnimatedTransform(LayerType
* layer
) {
555 return layer
->transform_is_invertible() ||
556 layer
->HasPotentiallyRunningTransformAnimation();
559 static inline bool SubtreeShouldBeSkipped(LayerImpl
* layer
,
560 bool layer_is_drawn
) {
561 // If the layer transform is not invertible, it should not be drawn.
562 // TODO(ajuma): Correctly process subtrees with singular transform for the
563 // case where we may animate to a non-singular transform and wish to
565 if (!HasInvertibleOrAnimatedTransform(layer
))
568 // When we need to do a readback/copy of a layer's output, we can not skip
569 // it or any of its ancestors.
570 if (layer
->draw_properties().layer_or_descendant_has_copy_request
)
573 // We cannot skip the the subtree if a descendant has a wheel or touch handler
574 // or the hit testing code will break (it requires fresh transforms, etc).
575 if (layer
->draw_properties().layer_or_descendant_has_input_handler
)
578 // If the layer is not drawn, then skip it and its subtree.
582 // If layer is on the pending tree and opacity is being animated then
583 // this subtree can't be skipped as we need to create, prioritize and
584 // include tiles for this layer when deciding if tree can be activated.
585 if (layer
->layer_tree_impl()->IsPendingTree() &&
586 layer
->HasPotentiallyRunningOpacityAnimation())
589 // If layer has a background filter, don't skip the layer, even it the
591 if (!layer
->background_filters().IsEmpty())
594 // The opacity of a layer always applies to its children (either implicitly
595 // via a render surface or explicitly if the parent preserves 3D), so the
596 // entire subtree can be skipped if this layer is fully transparent.
597 return !layer
->opacity();
600 static inline void SavePaintPropertiesLayer(LayerImpl
* layer
) {}
602 static bool SubtreeShouldRenderToSeparateSurface(
604 bool axis_aligned_with_respect_to_parent
) {
606 // A layer and its descendants should render onto a new RenderSurfaceImpl if
607 // any of these rules hold:
610 // The root layer owns a render surface, but it never acts as a contributing
611 // surface to another render target. Compositor features that are applied via
612 // a contributing surface can not be applied to the root layer. In order to
613 // use these effects, another child of the root would need to be introduced
614 // in order to act as a contributing surface to the root layer's surface.
615 bool is_root
= IsRootLayer(layer
);
617 // If the layer uses a mask.
618 if (layer
->mask_layer()) {
623 // If the layer has a reflection.
624 if (layer
->replica_layer()) {
629 // If the layer uses a CSS filter.
630 if (!layer
->filters().IsEmpty() || !layer
->background_filters().IsEmpty()) {
635 // If the layer will use a CSS filter. In this case, the animation
636 // will start and add a filter to this layer, so it needs a surface.
637 if (layer
->HasPotentiallyRunningFilterAnimation()) {
642 int num_descendants_that_draw_content
=
643 layer
->NumDescendantsThatDrawContent();
645 // If the layer flattens its subtree, but it is treated as a 3D object by its
646 // parent (i.e. parent participates in a 3D rendering context).
647 if (LayerIsInExisting3DRenderingContext(layer
) &&
648 layer
->should_flatten_transform() &&
649 num_descendants_that_draw_content
> 0) {
650 TRACE_EVENT_INSTANT0(
652 "LayerTreeHostCommon::SubtreeShouldRenderToSeparateSurface flattening",
653 TRACE_EVENT_SCOPE_THREAD
);
658 // If the layer has blending.
659 // TODO(rosca): this is temporary, until blending is implemented for other
660 // types of quads than RenderPassDrawQuad. Layers having descendants that draw
661 // content will still create a separate rendering surface.
662 if (!layer
->uses_default_blend_mode()) {
663 TRACE_EVENT_INSTANT0(
665 "LayerTreeHostCommon::SubtreeShouldRenderToSeparateSurface blending",
666 TRACE_EVENT_SCOPE_THREAD
);
671 // If the layer clips its descendants but it is not axis-aligned with respect
673 bool layer_clips_external_content
=
674 LayerClipsSubtree(layer
) || layer
->HasDelegatedContent();
675 if (layer_clips_external_content
&& !axis_aligned_with_respect_to_parent
&&
676 num_descendants_that_draw_content
> 0) {
677 TRACE_EVENT_INSTANT0(
679 "LayerTreeHostCommon::SubtreeShouldRenderToSeparateSurface clipping",
680 TRACE_EVENT_SCOPE_THREAD
);
685 // If the layer has some translucency and does not have a preserves-3d
686 // transform style. This condition only needs a render surface if two or more
687 // layers in the subtree overlap. But checking layer overlaps is unnecessarily
688 // costly so instead we conservatively create a surface whenever at least two
689 // layers draw content for this subtree.
690 bool at_least_two_layers_in_subtree_draw_content
=
691 num_descendants_that_draw_content
> 0 &&
692 (layer
->DrawsContent() || num_descendants_that_draw_content
> 1);
694 if (layer
->opacity() != 1.f
&& layer
->should_flatten_transform() &&
695 at_least_two_layers_in_subtree_draw_content
) {
696 TRACE_EVENT_INSTANT0(
698 "LayerTreeHostCommon::SubtreeShouldRenderToSeparateSurface opacity",
699 TRACE_EVENT_SCOPE_THREAD
);
704 // The root layer should always have a render_surface.
709 // These are allowed on the root surface, as they don't require the surface to
710 // be used as a contributing surface in order to apply correctly.
713 // If the layer has isolation.
714 // TODO(rosca): to be optimized - create separate rendering surface only when
715 // the blending descendants might have access to the content behind this layer
716 // (layer has transparent background or descendants overflow).
717 // https://code.google.com/p/chromium/issues/detail?id=301738
718 if (layer
->is_root_for_isolated_group()) {
719 TRACE_EVENT_INSTANT0(
721 "LayerTreeHostCommon::SubtreeShouldRenderToSeparateSurface isolation",
722 TRACE_EVENT_SCOPE_THREAD
);
727 if (layer
->force_render_surface())
730 // If we'll make a copy of the layer's contents.
731 if (layer
->HasCopyRequest())
737 // This function returns a translation matrix that can be applied on a vector
738 // that's in the layer's target surface coordinate, while the position offset is
739 // specified in some ancestor layer's coordinate.
740 gfx::Transform
ComputeSizeDeltaCompensation(
742 LayerImpl
* container
,
743 const gfx::Vector2dF
& position_offset
) {
744 gfx::Transform result_transform
;
746 // To apply a translate in the container's layer space,
747 // the following steps need to be done:
748 // Step 1a. transform from target surface space to the container's target
750 // Step 1b. transform from container's target surface space to the
751 // container's layer space
752 // Step 2. apply the compensation
753 // Step 3. transform back to target surface space
755 gfx::Transform target_surface_space_to_container_layer_space
;
757 LayerImpl
* container_target_surface
= container
->render_target();
758 for (const LayerImpl
* current_target_surface
= NextTargetSurface(layer
);
759 current_target_surface
&&
760 current_target_surface
!= container_target_surface
;
761 current_target_surface
= NextTargetSurface(current_target_surface
)) {
762 // Note: Concat is used here to convert the result coordinate space from
763 // current render surface to the next render surface.
764 target_surface_space_to_container_layer_space
.ConcatTransform(
765 current_target_surface
->render_surface()->draw_transform());
768 gfx::Transform container_layer_space_to_container_target_surface_space
=
769 container
->draw_transform();
770 gfx::Transform container_target_surface_space_to_container_layer_space
;
771 if (container_layer_space_to_container_target_surface_space
.GetInverse(
772 &container_target_surface_space_to_container_layer_space
)) {
773 // Note: Again, Concat is used to conver the result coordinate space from
774 // the container render surface to the container layer.
775 target_surface_space_to_container_layer_space
.ConcatTransform(
776 container_target_surface_space_to_container_layer_space
);
780 gfx::Transform container_layer_space_to_target_surface_space
;
781 if (target_surface_space_to_container_layer_space
.GetInverse(
782 &container_layer_space_to_target_surface_space
)) {
783 result_transform
.PreconcatTransform(
784 container_layer_space_to_target_surface_space
);
786 // TODO(shawnsingh): A non-invertible matrix could still make meaningful
787 // projection. For example ScaleZ(0) is non-invertible but the layer is
789 return gfx::Transform();
793 result_transform
.Translate(position_offset
.x(), position_offset
.y());
796 result_transform
.PreconcatTransform(
797 target_surface_space_to_container_layer_space
);
799 return result_transform
;
802 void ApplyPositionAdjustment(LayerImpl
* layer
,
803 LayerImpl
* container
,
804 const gfx::Transform
& scroll_compensation
,
805 gfx::Transform
* combined_transform
) {
806 if (!layer
->position_constraint().is_fixed_position())
809 // Special case: this layer is a composited fixed-position layer; we need to
810 // explicitly compensate for all ancestors' nonzero scroll_deltas to keep
811 // this layer fixed correctly.
812 // Note carefully: this is Concat, not Preconcat
813 // (current_scroll_compensation * combined_transform).
814 combined_transform
->ConcatTransform(scroll_compensation
);
816 // For right-edge or bottom-edge anchored fixed position layers,
817 // the layer should relocate itself if the container changes its size.
818 bool fixed_to_right_edge
=
819 layer
->position_constraint().is_fixed_to_right_edge();
820 bool fixed_to_bottom_edge
=
821 layer
->position_constraint().is_fixed_to_bottom_edge();
822 gfx::Vector2dF position_offset
= container
->FixedContainerSizeDelta();
823 position_offset
.set_x(fixed_to_right_edge
? position_offset
.x() : 0);
824 position_offset
.set_y(fixed_to_bottom_edge
? position_offset
.y() : 0);
825 if (position_offset
.IsZero())
828 // Note: Again, this is Concat. The compensation matrix will be applied on
829 // the vector in target surface space.
830 combined_transform
->ConcatTransform(
831 ComputeSizeDeltaCompensation(layer
, container
, position_offset
));
834 gfx::Transform
ComputeScrollCompensationForThisLayer(
835 LayerImpl
* scrolling_layer
,
836 const gfx::Transform
& parent_matrix
,
837 const gfx::Vector2dF
& scroll_delta
) {
838 // For every layer that has non-zero scroll_delta, we have to compute a
839 // transform that can undo the scroll_delta translation. In particular, we
840 // want this matrix to premultiply a fixed-position layer's parent_matrix, so
841 // we design this transform in three steps as follows. The steps described
842 // here apply from right-to-left, so Step 1 would be the right-most matrix:
844 // Step 1. transform from target surface space to the exact space where
845 // scroll_delta is actually applied.
846 // -- this is inverse of parent_matrix
847 // Step 2. undo the scroll_delta
848 // -- this is just a translation by scroll_delta.
849 // Step 3. transform back to target surface space.
850 // -- this transform is the parent_matrix
852 // These steps create a matrix that both start and end in target surface
853 // space. So this matrix can pre-multiply any fixed-position layer's
854 // draw_transform to undo the scroll_deltas -- as long as that fixed position
855 // layer is fixed onto the same render_target as this scrolling_layer.
858 gfx::Transform scroll_compensation_for_this_layer
= parent_matrix
; // Step 3
859 scroll_compensation_for_this_layer
.Translate(
861 scroll_delta
.y()); // Step 2
863 gfx::Transform
inverse_parent_matrix(gfx::Transform::kSkipInitialization
);
864 if (!parent_matrix
.GetInverse(&inverse_parent_matrix
)) {
865 // TODO(shawnsingh): Either we need to handle uninvertible transforms
866 // here, or DCHECK that the transform is invertible.
868 scroll_compensation_for_this_layer
.PreconcatTransform(
869 inverse_parent_matrix
); // Step 1
870 return scroll_compensation_for_this_layer
;
873 gfx::Transform
ComputeScrollCompensationMatrixForChildren(
875 const gfx::Transform
& parent_matrix
,
876 const gfx::Transform
& current_scroll_compensation_matrix
,
877 const gfx::Vector2dF
& scroll_delta
) {
878 // "Total scroll compensation" is the transform needed to cancel out all
879 // scroll_delta translations that occurred since the nearest container layer,
880 // even if there are render_surfaces in-between.
882 // There are some edge cases to be aware of, that are not explicit in the
884 // - A layer that is both a fixed-position and container should not be its
885 // own container, instead, that means it is fixed to an ancestor, and is a
886 // container for any fixed-position descendants.
887 // - A layer that is a fixed-position container and has a render_surface
888 // should behave the same as a container without a render_surface, the
889 // render_surface is irrelevant in that case.
890 // - A layer that does not have an explicit container is simply fixed to the
891 // viewport. (i.e. the root render_surface.)
892 // - If the fixed-position layer has its own render_surface, then the
893 // render_surface is the one who gets fixed.
895 // This function needs to be called AFTER layers create their own
899 // Scroll compensation restarts from identity under two possible conditions:
900 // - the current layer is a container for fixed-position descendants
901 // - the current layer is fixed-position itself, so any fixed-position
902 // descendants are positioned with respect to this layer. Thus, any
903 // fixed position descendants only need to compensate for scrollDeltas
904 // that occur below this layer.
905 bool current_layer_resets_scroll_compensation_for_descendants
=
906 layer
->IsContainerForFixedPositionLayers() ||
907 layer
->position_constraint().is_fixed_position();
909 // Avoid the overheads (including stack allocation and matrix
910 // initialization/copy) if we know that the scroll compensation doesn't need
911 // to be reset or adjusted.
912 if (!current_layer_resets_scroll_compensation_for_descendants
&&
913 scroll_delta
.IsZero() && !layer
->render_surface())
914 return current_scroll_compensation_matrix
;
916 // Start as identity matrix.
917 gfx::Transform next_scroll_compensation_matrix
;
919 // If this layer does not reset scroll compensation, then it inherits the
920 // existing scroll compensations.
921 if (!current_layer_resets_scroll_compensation_for_descendants
)
922 next_scroll_compensation_matrix
= current_scroll_compensation_matrix
;
924 // If the current layer has a non-zero scroll_delta, then we should compute
925 // its local scroll compensation and accumulate it to the
926 // next_scroll_compensation_matrix.
927 if (!scroll_delta
.IsZero()) {
928 gfx::Transform scroll_compensation_for_this_layer
=
929 ComputeScrollCompensationForThisLayer(
930 layer
, parent_matrix
, scroll_delta
);
931 next_scroll_compensation_matrix
.PreconcatTransform(
932 scroll_compensation_for_this_layer
);
935 // If the layer created its own render_surface, we have to adjust
936 // next_scroll_compensation_matrix. The adjustment allows us to continue
937 // using the scroll compensation on the next surface.
938 // Step 1 (right-most in the math): transform from the new surface to the
939 // original ancestor surface
940 // Step 2: apply the scroll compensation
941 // Step 3: transform back to the new surface.
942 if (layer
->render_surface() &&
943 !next_scroll_compensation_matrix
.IsIdentity()) {
944 gfx::Transform
inverse_surface_draw_transform(
945 gfx::Transform::kSkipInitialization
);
946 if (!layer
->render_surface()->draw_transform().GetInverse(
947 &inverse_surface_draw_transform
)) {
948 // TODO(shawnsingh): Either we need to handle uninvertible transforms
949 // here, or DCHECK that the transform is invertible.
951 next_scroll_compensation_matrix
=
952 inverse_surface_draw_transform
* next_scroll_compensation_matrix
*
953 layer
->render_surface()->draw_transform();
956 return next_scroll_compensation_matrix
;
959 static inline void UpdateLayerScaleDrawProperties(
961 float maximum_animation_contents_scale
,
962 float starting_animation_contents_scale
) {
963 layer
->draw_properties().maximum_animation_contents_scale
=
964 maximum_animation_contents_scale
;
965 layer
->draw_properties().starting_animation_contents_scale
=
966 starting_animation_contents_scale
;
969 static inline void CalculateAnimationContentsScale(
971 bool ancestor_is_animating_scale
,
972 float ancestor_maximum_animation_contents_scale
,
973 float ancestor_starting_animation_contents_scale
,
974 const gfx::Transform
& ancestor_transform
,
975 const gfx::Transform
& combined_transform
,
976 bool* combined_is_animating_scale
,
977 float* combined_maximum_animation_contents_scale
,
978 float* combined_starting_animation_contents_scale
) {
979 if (ancestor_is_animating_scale
&&
980 ancestor_maximum_animation_contents_scale
== 0.f
) {
981 // We've already failed to compute a maximum animated scale at an
982 // ancestor, so we'll continue to fail.
983 *combined_maximum_animation_contents_scale
= 0.f
;
984 *combined_starting_animation_contents_scale
= 0.f
;
985 *combined_is_animating_scale
= true;
989 if (!combined_transform
.IsScaleOrTranslation()) {
990 // Computing maximum animated scale in the presence of
991 // non-scale/translation transforms isn't supported.
992 *combined_maximum_animation_contents_scale
= 0.f
;
993 *combined_starting_animation_contents_scale
= 0.f
;
994 *combined_is_animating_scale
= true;
998 // We currently only support computing maximum scale for combinations of
999 // scales and translations. We treat all non-translations as potentially
1000 // affecting scale. Animations that include non-translation/scale components
1001 // will cause the computation of MaximumScale below to fail.
1002 bool layer_is_animating_scale
= !layer
->HasOnlyTranslationTransforms();
1004 if (!layer_is_animating_scale
&& !ancestor_is_animating_scale
) {
1005 *combined_maximum_animation_contents_scale
= 0.f
;
1006 *combined_starting_animation_contents_scale
= 0.f
;
1007 *combined_is_animating_scale
= false;
1011 // We don't attempt to accumulate animation scale from multiple nodes,
1012 // because of the risk of significant overestimation. For example, one node
1013 // may be increasing scale from 1 to 10 at the same time as a descendant is
1014 // decreasing scale from 10 to 1. Naively combining these scales would produce
1016 if (layer_is_animating_scale
&& ancestor_is_animating_scale
) {
1017 *combined_maximum_animation_contents_scale
= 0.f
;
1018 *combined_starting_animation_contents_scale
= 0.f
;
1019 *combined_is_animating_scale
= true;
1023 // At this point, we know either the layer or an ancestor, but not both,
1024 // is animating scale.
1025 *combined_is_animating_scale
= true;
1026 if (!layer_is_animating_scale
) {
1027 gfx::Vector2dF layer_transform_scales
=
1028 MathUtil::ComputeTransform2dScaleComponents(layer
->transform(), 0.f
);
1029 float max_layer_scale
=
1030 std::max(layer_transform_scales
.x(), layer_transform_scales
.y());
1031 *combined_maximum_animation_contents_scale
=
1032 ancestor_maximum_animation_contents_scale
* max_layer_scale
;
1033 *combined_starting_animation_contents_scale
=
1034 ancestor_starting_animation_contents_scale
* max_layer_scale
;
1038 float layer_maximum_animated_scale
= 0.f
;
1039 float layer_start_animated_scale
= 0.f
;
1040 if (!layer
->MaximumTargetScale(&layer_maximum_animated_scale
)) {
1041 *combined_maximum_animation_contents_scale
= 0.f
;
1044 if (!layer
->AnimationStartScale(&layer_start_animated_scale
)) {
1045 *combined_starting_animation_contents_scale
= 0.f
;
1049 gfx::Vector2dF ancestor_transform_scales
=
1050 MathUtil::ComputeTransform2dScaleComponents(ancestor_transform
, 0.f
);
1051 float max_scale_xy
=
1052 std::max(ancestor_transform_scales
.x(), ancestor_transform_scales
.y());
1053 *combined_maximum_animation_contents_scale
=
1054 layer_maximum_animated_scale
* max_scale_xy
;
1055 *combined_starting_animation_contents_scale
=
1056 layer_start_animated_scale
* max_scale_xy
;
1059 static inline void MarkLayerWithRenderSurfaceLayerListId(
1061 int current_render_surface_layer_list_id
) {
1062 layer
->draw_properties().last_drawn_render_surface_layer_list_id
=
1063 current_render_surface_layer_list_id
;
1064 layer
->set_layer_or_descendant_is_drawn(
1065 !!current_render_surface_layer_list_id
);
1068 static inline void MarkMasksWithRenderSurfaceLayerListId(
1070 int current_render_surface_layer_list_id
) {
1071 if (layer
->mask_layer()) {
1072 MarkLayerWithRenderSurfaceLayerListId(layer
->mask_layer(),
1073 current_render_surface_layer_list_id
);
1075 if (layer
->replica_layer() && layer
->replica_layer()->mask_layer()) {
1076 MarkLayerWithRenderSurfaceLayerListId(layer
->replica_layer()->mask_layer(),
1077 current_render_surface_layer_list_id
);
1081 static inline void MarkLayerListWithRenderSurfaceLayerListId(
1082 LayerImplList
* layer_list
,
1083 int current_render_surface_layer_list_id
) {
1084 for (LayerImplList::iterator it
= layer_list
->begin();
1085 it
!= layer_list
->end(); ++it
) {
1086 MarkLayerWithRenderSurfaceLayerListId(*it
,
1087 current_render_surface_layer_list_id
);
1088 MarkMasksWithRenderSurfaceLayerListId(*it
,
1089 current_render_surface_layer_list_id
);
1093 static inline void RemoveSurfaceForEarlyExit(
1094 LayerImpl
* layer_to_remove
,
1095 LayerImplList
* render_surface_layer_list
) {
1096 DCHECK(layer_to_remove
->render_surface());
1097 // Technically, we know that the layer we want to remove should be
1098 // at the back of the render_surface_layer_list. However, we have had
1099 // bugs before that added unnecessary layers here
1100 // (https://bugs.webkit.org/show_bug.cgi?id=74147), but that causes
1101 // things to crash. So here we proactively remove any additional
1102 // layers from the end of the list.
1103 while (render_surface_layer_list
->back() != layer_to_remove
) {
1104 MarkLayerListWithRenderSurfaceLayerListId(
1105 &render_surface_layer_list
->back()->render_surface()->layer_list(), 0);
1106 MarkLayerWithRenderSurfaceLayerListId(render_surface_layer_list
->back(), 0);
1108 render_surface_layer_list
->back()->ClearRenderSurfaceLayerList();
1109 render_surface_layer_list
->pop_back();
1111 DCHECK_EQ(render_surface_layer_list
->back(), layer_to_remove
);
1112 MarkLayerListWithRenderSurfaceLayerListId(
1113 &layer_to_remove
->render_surface()->layer_list(), 0);
1114 MarkLayerWithRenderSurfaceLayerListId(layer_to_remove
, 0);
1115 render_surface_layer_list
->pop_back();
1116 layer_to_remove
->ClearRenderSurfaceLayerList();
1119 struct PreCalculateMetaInformationRecursiveData
{
1120 size_t num_unclipped_descendants
;
1121 int num_layer_or_descendants_with_copy_request
;
1122 int num_layer_or_descendants_with_input_handler
;
1124 PreCalculateMetaInformationRecursiveData()
1125 : num_unclipped_descendants(0),
1126 num_layer_or_descendants_with_copy_request(0),
1127 num_layer_or_descendants_with_input_handler(0) {}
1129 void Merge(const PreCalculateMetaInformationRecursiveData
& data
) {
1130 num_layer_or_descendants_with_copy_request
+=
1131 data
.num_layer_or_descendants_with_copy_request
;
1132 num_layer_or_descendants_with_input_handler
+=
1133 data
.num_layer_or_descendants_with_input_handler
;
1134 num_unclipped_descendants
+= data
.num_unclipped_descendants
;
1138 static void ValidateRenderSurface(LayerImpl
* layer
) {
1139 // This test verifies that there are no cases where a LayerImpl needs
1140 // a render surface, but doesn't have one.
1141 if (layer
->render_surface())
1144 DCHECK(layer
->filters().IsEmpty()) << "layer: " << layer
->id();
1145 DCHECK(layer
->background_filters().IsEmpty()) << "layer: " << layer
->id();
1146 DCHECK(!layer
->mask_layer()) << "layer: " << layer
->id();
1147 DCHECK(!layer
->replica_layer()) << "layer: " << layer
->id();
1148 DCHECK(!IsRootLayer(layer
)) << "layer: " << layer
->id();
1149 DCHECK(!layer
->is_root_for_isolated_group()) << "layer: " << layer
->id();
1150 DCHECK(!layer
->HasCopyRequest()) << "layer: " << layer
->id();
1153 static void ValidateRenderSurface(Layer
* layer
) {
1156 static bool IsMetaInformationRecomputationNeeded(Layer
* layer
) {
1157 return layer
->layer_tree_host()->needs_meta_info_recomputation();
1160 static void UpdateMetaInformationSequenceNumber(Layer
* root_layer
) {
1161 root_layer
->layer_tree_host()->IncrementMetaInformationSequenceNumber();
1164 static void UpdateMetaInformationSequenceNumber(LayerImpl
* root_layer
) {
1167 // Recursively walks the layer tree(if needed) to compute any information
1168 // that is needed before doing the main recursion.
1169 static void PreCalculateMetaInformationInternal(
1171 PreCalculateMetaInformationRecursiveData
* recursive_data
) {
1172 ValidateRenderSurface(layer
);
1174 if (!IsMetaInformationRecomputationNeeded(layer
)) {
1175 DCHECK(IsRootLayer(layer
));
1179 layer
->set_sorted_for_recursion(false);
1180 layer
->set_layer_or_descendant_is_drawn(false);
1181 layer
->set_visited(false);
1183 if (!HasInvertibleOrAnimatedTransform(layer
)) {
1184 // Layers with singular transforms should not be drawn, the whole subtree
1189 if (layer
->clip_parent())
1190 recursive_data
->num_unclipped_descendants
++;
1192 layer
->set_num_children_with_scroll_parent(0);
1193 for (size_t i
= 0; i
< layer
->children().size(); ++i
) {
1194 Layer
* child_layer
= layer
->child_at(i
);
1196 PreCalculateMetaInformationRecursiveData data_for_child
;
1197 PreCalculateMetaInformationInternal(child_layer
, &data_for_child
);
1199 if (child_layer
->scroll_parent()) {
1200 layer
->set_num_children_with_scroll_parent(
1201 layer
->num_children_with_scroll_parent() + 1);
1203 recursive_data
->Merge(data_for_child
);
1206 if (layer
->clip_children()) {
1207 size_t num_clip_children
= layer
->clip_children()->size();
1208 DCHECK_GE(recursive_data
->num_unclipped_descendants
, num_clip_children
);
1209 recursive_data
->num_unclipped_descendants
-= num_clip_children
;
1212 if (layer
->HasCopyRequest())
1213 recursive_data
->num_layer_or_descendants_with_copy_request
++;
1215 if (!layer
->touch_event_handler_region().IsEmpty() ||
1216 layer
->have_wheel_event_handlers())
1217 recursive_data
->num_layer_or_descendants_with_input_handler
++;
1219 layer
->set_num_unclipped_descendants(
1220 recursive_data
->num_unclipped_descendants
);
1221 layer
->set_num_layer_or_descendant_with_copy_request(
1222 recursive_data
->num_layer_or_descendants_with_copy_request
);
1224 if (IsRootLayer(layer
))
1225 layer
->layer_tree_host()->SetNeedsMetaInfoRecomputation(false);
1228 static void PreCalculateMetaInformationInternal(
1230 PreCalculateMetaInformationRecursiveData
* recursive_data
) {
1231 ValidateRenderSurface(layer
);
1233 layer
->set_sorted_for_recursion(false);
1234 layer
->draw_properties().has_child_with_a_scroll_parent
= false;
1235 layer
->set_layer_or_descendant_is_drawn(false);
1236 layer
->set_visited(false);
1238 if (!HasInvertibleOrAnimatedTransform(layer
)) {
1239 // Layers with singular transforms should not be drawn, the whole subtree
1244 if (layer
->clip_parent())
1245 recursive_data
->num_unclipped_descendants
++;
1247 for (size_t i
= 0; i
< layer
->children().size(); ++i
) {
1248 LayerImpl
* child_layer
= layer
->child_at(i
);
1250 PreCalculateMetaInformationRecursiveData data_for_child
;
1251 PreCalculateMetaInformationInternal(child_layer
, &data_for_child
);
1253 if (child_layer
->scroll_parent())
1254 layer
->draw_properties().has_child_with_a_scroll_parent
= true;
1255 recursive_data
->Merge(data_for_child
);
1258 if (layer
->clip_children()) {
1259 size_t num_clip_children
= layer
->clip_children()->size();
1260 DCHECK_GE(recursive_data
->num_unclipped_descendants
, num_clip_children
);
1261 recursive_data
->num_unclipped_descendants
-= num_clip_children
;
1264 if (layer
->HasCopyRequest())
1265 recursive_data
->num_layer_or_descendants_with_copy_request
++;
1267 if (!layer
->touch_event_handler_region().IsEmpty() ||
1268 layer
->have_wheel_event_handlers())
1269 recursive_data
->num_layer_or_descendants_with_input_handler
++;
1271 layer
->draw_properties().num_unclipped_descendants
=
1272 recursive_data
->num_unclipped_descendants
;
1273 layer
->draw_properties().layer_or_descendant_has_copy_request
=
1274 (recursive_data
->num_layer_or_descendants_with_copy_request
!= 0);
1275 layer
->draw_properties().layer_or_descendant_has_input_handler
=
1276 (recursive_data
->num_layer_or_descendants_with_input_handler
!= 0);
1279 void LayerTreeHostCommon::PreCalculateMetaInformation(Layer
* root_layer
) {
1280 PreCalculateMetaInformationRecursiveData recursive_data
;
1281 PreCalculateMetaInformationInternal(root_layer
, &recursive_data
);
1284 void LayerTreeHostCommon::PreCalculateMetaInformationForTesting(
1285 LayerImpl
* root_layer
) {
1286 PreCalculateMetaInformationRecursiveData recursive_data
;
1287 PreCalculateMetaInformationInternal(root_layer
, &recursive_data
);
1290 void LayerTreeHostCommon::PreCalculateMetaInformationForTesting(
1291 Layer
* root_layer
) {
1292 UpdateMetaInformationSequenceNumber(root_layer
);
1293 PreCalculateMetaInformationRecursiveData recursive_data
;
1294 PreCalculateMetaInformationInternal(root_layer
, &recursive_data
);
1297 struct SubtreeGlobals
{
1298 int max_texture_size
;
1299 float device_scale_factor
;
1300 float page_scale_factor
;
1301 const LayerImpl
* page_scale_layer
;
1302 gfx::Vector2dF elastic_overscroll
;
1303 const LayerImpl
* elastic_overscroll_application_layer
;
1304 bool can_adjust_raster_scales
;
1305 bool can_render_to_separate_surface
;
1306 bool layers_always_allowed_lcd_text
;
1309 struct DataForRecursion
{
1310 // The accumulated sequence of transforms a layer will use to determine its
1311 // own draw transform.
1312 gfx::Transform parent_matrix
;
1314 // The accumulated sequence of transforms a layer will use to determine its
1315 // own screen-space transform.
1316 gfx::Transform full_hierarchy_matrix
;
1318 // The transform that removes all scrolling that may have occurred between a
1319 // fixed-position layer and its container, so that the layer actually does
1321 gfx::Transform scroll_compensation_matrix
;
1323 // The ancestor that would be the container for any fixed-position / sticky
1325 LayerImpl
* fixed_container
;
1327 // This is the normal clip rect that is propagated from parent to child.
1328 gfx::Rect clip_rect_in_target_space
;
1330 // When the layer's children want to compute their visible content rect, they
1331 // want to know what their target surface's clip rect will be. BUT - they
1332 // want to know this clip rect represented in their own target space. This
1333 // requires inverse-projecting the surface's clip rect from the surface's
1334 // render target space down to the surface's own space. Instead of computing
1335 // this value redundantly for each child layer, it is computed only once
1336 // while dealing with the parent layer, and then this precomputed value is
1337 // passed down the recursion to the children that actually use it.
1338 gfx::Rect clip_rect_of_target_surface_in_target_space
;
1340 // The maximum amount by which this layer will be scaled during the lifetime
1341 // of currently running animations, considering only scales at keyframes not
1342 // including the starting keyframe of each animation.
1343 float maximum_animation_contents_scale
;
1345 // The maximum amout by which this layer will be scaled during the lifetime of
1346 // currently running animations, consdering only the starting scale of each
1348 float starting_animation_contents_scale
;
1350 bool ancestor_is_animating_scale
;
1351 bool ancestor_clips_subtree
;
1352 bool in_subtree_of_page_scale_layer
;
1353 bool subtree_can_use_lcd_text
;
1354 bool subtree_is_visible_from_ancestor
;
1357 static LayerImpl
* GetChildContainingLayer(const LayerImpl
& parent
,
1359 for (LayerImpl
* ancestor
= layer
; ancestor
; ancestor
= ancestor
->parent()) {
1360 if (ancestor
->parent() == &parent
)
1367 static void AddScrollParentChain(std::vector
<LayerImpl
*>* out
,
1368 const LayerImpl
& parent
,
1370 // At a high level, this function walks up the chain of scroll parents
1371 // recursively, and once we reach the end of the chain, we add the child
1372 // of |parent| containing each scroll ancestor as we unwind. The result is
1373 // an ordering of parent's children that ensures that scroll parents are
1374 // visited before their descendants.
1375 // Take for example this layer tree:
1377 // + stacking_context
1378 // + scroll_child (1)
1379 // + scroll_parent_graphics_layer (*)
1380 // | + scroll_parent_scrolling_layer
1381 // | + scroll_parent_scrolling_content_layer (2)
1382 // + scroll_grandparent_graphics_layer (**)
1383 // + scroll_grandparent_scrolling_layer
1384 // + scroll_grandparent_scrolling_content_layer (3)
1386 // The scroll child is (1), its scroll parent is (2) and its scroll
1387 // grandparent is (3). Note, this doesn't mean that (2)'s scroll parent is
1388 // (3), it means that (*)'s scroll parent is (3). We don't want our list to
1389 // look like [ (3), (2), (1) ], even though that does have the ancestor chain
1390 // in the right order. Instead, we want [ (**), (*), (1) ]. That is, only want
1391 // (1)'s siblings in the list, but we want them to appear in such an order
1392 // that the scroll ancestors get visited in the correct order.
1394 // So our first task at this step of the recursion is to determine the layer
1395 // that we will potentionally add to the list. That is, the child of parent
1396 // containing |layer|.
1397 LayerImpl
* child
= GetChildContainingLayer(parent
, layer
);
1398 if (child
->sorted_for_recursion())
1401 if (LayerImpl
* scroll_parent
= child
->scroll_parent())
1402 AddScrollParentChain(out
, parent
, scroll_parent
);
1404 out
->push_back(child
);
1405 bool sorted_for_recursion
= true;
1406 child
->set_sorted_for_recursion(sorted_for_recursion
);
1409 static bool SortChildrenForRecursion(std::vector
<LayerImpl
*>* out
,
1410 const LayerImpl
& parent
) {
1411 out
->reserve(parent
.children().size());
1412 bool order_changed
= false;
1413 for (size_t i
= 0; i
< parent
.children().size(); ++i
) {
1414 LayerImpl
* current
=
1415 LayerTreeHostCommon::get_layer_as_raw_ptr(parent
.children(), i
);
1417 if (current
->sorted_for_recursion()) {
1418 order_changed
= true;
1422 AddScrollParentChain(out
, parent
, current
);
1425 DCHECK_EQ(parent
.children().size(), out
->size());
1426 return order_changed
;
1429 // Recursively walks the layer tree starting at the given node and computes all
1430 // the necessary transformations, clip rects, render surfaces, etc.
1431 static void CalculateDrawPropertiesInternal(
1433 const SubtreeGlobals
& globals
,
1434 const DataForRecursion
& data_from_ancestor
,
1435 std::vector
<AccumulatedSurfaceState
>* accumulated_surface_state
) {
1436 // This function computes the new matrix transformations recursively for this
1437 // layer and all its descendants. It also computes the appropriate render
1439 // Some important points to remember:
1441 // 0. Here, transforms are notated in Matrix x Vector order, and in words we
1442 // describe what the transform does from left to right.
1444 // 1. In our terminology, the "layer origin" refers to the top-left corner of
1445 // a layer, and the positive Y-axis points downwards. This interpretation is
1446 // valid because the orthographic projection applied at draw time flips the Y
1447 // axis appropriately.
1449 // 2. The anchor point, when given as a PointF object, is specified in "unit
1450 // layer space", where the bounds of the layer map to [0, 1]. However, as a
1451 // Transform object, the transform to the anchor point is specified in "layer
1452 // space", where the bounds of the layer map to [bounds.width(),
1453 // bounds.height()].
1455 // 3. Definition of various transforms used:
1456 // M[parent] is the parent matrix, with respect to the nearest render
1457 // surface, passed down recursively.
1459 // M[root] is the full hierarchy, with respect to the root, passed down
1462 // Tr[origin] is the translation matrix from the parent's origin to
1463 // this layer's origin.
1465 // Tr[origin2anchor] is the translation from the layer's origin to its
1468 // Tr[origin2center] is the translation from the layer's origin to its
1471 // M[layer] is the layer's matrix (applied at the anchor point)
1473 // S[layer2content] is the ratio of a layer's content_bounds() to its
1476 // Some composite transforms can help in understanding the sequence of
1478 // composite_layer_transform = Tr[origin2anchor] * M[layer] *
1479 // Tr[origin2anchor].inverse()
1481 // 4. When a layer (or render surface) is drawn, it is drawn into a "target
1482 // render surface". Therefore the draw transform does not necessarily
1483 // transform from screen space to local layer space. Instead, the draw
1484 // transform is the transform between the "target render surface space" and
1485 // local layer space. Note that render surfaces, except for the root, also
1486 // draw themselves into a different target render surface, and so their draw
1487 // transform and origin transforms are also described with respect to the
1490 // Using these definitions, then:
1492 // The draw transform for the layer is:
1493 // M[draw] = M[parent] * Tr[origin] * composite_layer_transform *
1494 // S[layer2content] = M[parent] * Tr[layer->position() + anchor] *
1495 // M[layer] * Tr[anchor2origin] * S[layer2content]
1497 // Interpreting the math left-to-right, this transforms from the
1498 // layer's render surface to the origin of the layer in content space.
1500 // The screen space transform is:
1501 // M[screenspace] = M[root] * Tr[origin] * composite_layer_transform *
1503 // = M[root] * Tr[layer->position() + anchor] * M[layer]
1504 // * Tr[anchor2origin] * S[layer2content]
1506 // Interpreting the math left-to-right, this transforms from the root
1507 // render surface's content space to the origin of the layer in content
1510 // The transform hierarchy that is passed on to children (i.e. the child's
1511 // parent_matrix) is:
1512 // M[parent]_for_child = M[parent] * Tr[origin] *
1513 // composite_layer_transform
1514 // = M[parent] * Tr[layer->position() + anchor] *
1515 // M[layer] * Tr[anchor2origin]
1517 // and a similar matrix for the full hierarchy with respect to the
1520 // Finally, note that the final matrix used by the shader for the layer is P *
1521 // M[draw] * S . This final product is computed in drawTexturedQuad(), where:
1522 // P is the projection matrix
1523 // S is the scale adjustment (to scale up a canonical quad to the
1526 // When a render surface has a replica layer, that layer's transform is used
1527 // to draw a second copy of the surface. gfx::Transforms named here are
1528 // relative to the surface, unless they specify they are relative to the
1531 // We will denote a scale by device scale S[deviceScale]
1533 // The render surface draw transform to its target surface origin is:
1534 // M[surfaceDraw] = M[owningLayer->Draw]
1536 // The render surface origin transform to its the root (screen space) origin
1538 // M[surface2root] = M[owningLayer->screenspace] *
1539 // S[deviceScale].inverse()
1541 // The replica draw transform to its target surface origin is:
1542 // M[replicaDraw] = S[deviceScale] * M[surfaceDraw] *
1543 // Tr[replica->position() + replica->anchor()] * Tr[replica] *
1544 // Tr[origin2anchor].inverse() * S[contents_scale].inverse()
1546 // The replica draw transform to the root (screen space) origin is:
1547 // M[replica2root] = M[surface2root] * Tr[replica->position()] *
1548 // Tr[replica] * Tr[origin2anchor].inverse()
1551 // It makes no sense to have a non-unit page_scale_factor without specifying
1552 // which layer roots the subtree the scale is applied to.
1553 DCHECK(globals
.page_scale_layer
|| (globals
.page_scale_factor
== 1.f
));
1555 CHECK(!layer
->visited());
1556 bool visited
= true;
1557 layer
->set_visited(visited
);
1559 DataForRecursion data_for_children
;
1560 data_for_children
.in_subtree_of_page_scale_layer
=
1561 data_from_ancestor
.in_subtree_of_page_scale_layer
;
1562 data_for_children
.subtree_can_use_lcd_text
=
1563 data_from_ancestor
.subtree_can_use_lcd_text
;
1565 // Layers that are marked as hidden will hide themselves and their subtree.
1566 // Exception: Layers with copy requests, whether hidden or not, must be drawn
1567 // anyway. In this case, we will inform their subtree they are visible to get
1568 // the right results.
1569 const bool layer_is_visible
=
1570 data_from_ancestor
.subtree_is_visible_from_ancestor
&&
1571 !layer
->hide_layer_and_subtree();
1572 const bool layer_is_drawn
= layer_is_visible
|| layer
->HasCopyRequest();
1574 // The root layer cannot skip CalcDrawProperties.
1575 if (!IsRootLayer(layer
) && SubtreeShouldBeSkipped(layer
, layer_is_drawn
)) {
1579 // We need to circumvent the normal recursive flow of information for clip
1580 // children (they don't inherit their direct ancestor's clip information).
1581 // This is unfortunate, and would be unnecessary if we were to formally
1582 // separate the clipping hierarchy from the layer hierarchy.
1583 bool ancestor_clips_subtree
= data_from_ancestor
.ancestor_clips_subtree
;
1584 gfx::Rect ancestor_clip_rect_in_target_space
=
1585 data_from_ancestor
.clip_rect_in_target_space
;
1587 // Update our clipping state. If we have a clip parent we will need to pull
1588 // from the clip state cache rather than using the clip state passed from our
1589 // immediate ancestor.
1590 UpdateClipRectsForClipChild(layer
, &ancestor_clip_rect_in_target_space
,
1591 &ancestor_clips_subtree
);
1593 // As this function proceeds, these are the properties for the current
1594 // layer that actually get computed. To avoid unnecessary copies
1595 // (particularly for matrices), we do computations directly on these values
1597 DrawProperties
& layer_draw_properties
= layer
->draw_properties();
1599 gfx::Rect clip_rect_in_target_space
;
1600 bool layer_or_ancestor_clips_descendants
= false;
1602 // This value is cached on the stack so that we don't have to inverse-project
1603 // the surface's clip rect redundantly for every layer. This value is the
1604 // same as the target surface's clip rect, except that instead of being
1605 // described in the target surface's target's space, it is described in the
1606 // current render target's space.
1607 gfx::Rect clip_rect_of_target_surface_in_target_space
;
1609 float accumulated_draw_opacity
= layer
->opacity();
1610 if (layer
->parent())
1611 accumulated_draw_opacity
*= layer
->parent()->draw_opacity();
1613 bool animating_transform_to_screen
=
1614 layer
->HasPotentiallyRunningTransformAnimation();
1615 if (layer
->parent()) {
1616 animating_transform_to_screen
|=
1617 layer
->parent()->screen_space_transform_is_animating();
1619 gfx::Point3F transform_origin
= layer
->transform_origin();
1620 gfx::ScrollOffset scroll_offset
= GetEffectiveCurrentScrollOffset(layer
);
1621 gfx::PointF position
=
1622 layer
->position() - ScrollOffsetToVector2dF(scroll_offset
);
1623 gfx::Transform combined_transform
= data_from_ancestor
.parent_matrix
;
1624 if (!layer
->transform().IsIdentity()) {
1625 // LT = Tr[origin] * Tr[origin2transformOrigin]
1626 combined_transform
.Translate3d(position
.x() + transform_origin
.x(),
1627 position
.y() + transform_origin
.y(),
1628 transform_origin
.z());
1629 // LT = Tr[origin] * Tr[origin2origin] * M[layer]
1630 combined_transform
.PreconcatTransform(layer
->transform());
1631 // LT = Tr[origin] * Tr[origin2origin] * M[layer] *
1632 // Tr[transformOrigin2origin]
1633 combined_transform
.Translate3d(
1634 -transform_origin
.x(), -transform_origin
.y(), -transform_origin
.z());
1636 combined_transform
.Translate(position
.x(), position
.y());
1639 gfx::Vector2dF effective_scroll_delta
= GetEffectiveScrollDelta(layer
);
1640 if (!animating_transform_to_screen
&& layer
->scrollable() &&
1641 combined_transform
.IsScaleOrTranslation()) {
1642 // Align the scrollable layer's position to screen space pixels to avoid
1643 // blurriness. To avoid side-effects, do this only if the transform is
1645 gfx::Vector2dF previous_translation
= combined_transform
.To2dTranslation();
1646 combined_transform
.RoundTranslationComponents();
1647 gfx::Vector2dF current_translation
= combined_transform
.To2dTranslation();
1649 // This rounding changes the scroll delta, and so must be included
1650 // in the scroll compensation matrix. The scaling converts from physical
1651 // coordinates to the scroll delta's CSS coordinates (using the parent
1652 // matrix instead of combined transform since scrolling is applied before
1653 // the layer's transform). For example, if we have a total scale factor of
1654 // 3.0, then 1 physical pixel is only 1/3 of a CSS pixel.
1655 gfx::Vector2dF parent_scales
= MathUtil::ComputeTransform2dScaleComponents(
1656 data_from_ancestor
.parent_matrix
, 1.f
);
1657 effective_scroll_delta
-=
1658 gfx::ScaleVector2d(current_translation
- previous_translation
,
1659 1.f
/ parent_scales
.x(),
1660 1.f
/ parent_scales
.y());
1663 // Apply adjustment from position constraints.
1664 ApplyPositionAdjustment(layer
, data_from_ancestor
.fixed_container
,
1665 data_from_ancestor
.scroll_compensation_matrix
, &combined_transform
);
1667 bool combined_is_animating_scale
= false;
1668 float combined_maximum_animation_contents_scale
= 0.f
;
1669 float combined_starting_animation_contents_scale
= 0.f
;
1670 if (globals
.can_adjust_raster_scales
) {
1671 CalculateAnimationContentsScale(
1672 layer
, data_from_ancestor
.ancestor_is_animating_scale
,
1673 data_from_ancestor
.maximum_animation_contents_scale
,
1674 data_from_ancestor
.starting_animation_contents_scale
,
1675 data_from_ancestor
.parent_matrix
, combined_transform
,
1676 &combined_is_animating_scale
,
1677 &combined_maximum_animation_contents_scale
,
1678 &combined_starting_animation_contents_scale
);
1680 data_for_children
.ancestor_is_animating_scale
= combined_is_animating_scale
;
1681 data_for_children
.maximum_animation_contents_scale
=
1682 combined_maximum_animation_contents_scale
;
1683 data_for_children
.starting_animation_contents_scale
=
1684 combined_starting_animation_contents_scale
;
1686 // Compute the 2d scale components of the transform hierarchy up to the target
1687 // surface. From there, we can decide on a contents scale for the layer.
1688 float layer_scale_factors
= globals
.device_scale_factor
;
1689 if (data_from_ancestor
.in_subtree_of_page_scale_layer
)
1690 layer_scale_factors
*= globals
.page_scale_factor
;
1691 gfx::Vector2dF combined_transform_scales
=
1692 MathUtil::ComputeTransform2dScaleComponents(
1694 layer_scale_factors
);
1696 UpdateLayerScaleDrawProperties(layer
,
1697 combined_maximum_animation_contents_scale
,
1698 combined_starting_animation_contents_scale
);
1700 LayerImpl
* mask_layer
= layer
->mask_layer();
1702 UpdateLayerScaleDrawProperties(mask_layer
,
1703 combined_maximum_animation_contents_scale
,
1704 combined_starting_animation_contents_scale
);
1707 LayerImpl
* replica_mask_layer
=
1708 layer
->replica_layer() ? layer
->replica_layer()->mask_layer() : NULL
;
1709 if (replica_mask_layer
) {
1710 UpdateLayerScaleDrawProperties(replica_mask_layer
,
1711 combined_maximum_animation_contents_scale
,
1712 combined_starting_animation_contents_scale
);
1715 if (layer
== globals
.page_scale_layer
) {
1716 combined_transform
.Scale(globals
.page_scale_factor
,
1717 globals
.page_scale_factor
);
1718 data_for_children
.in_subtree_of_page_scale_layer
= true;
1721 // The draw_transform that gets computed below is effectively the layer's
1722 // draw_transform, unless the layer itself creates a render_surface. In that
1723 // case, the render_surface re-parents the transforms.
1724 layer_draw_properties
.target_space_transform
= combined_transform
;
1726 // The layer's screen_space_transform represents the transform between root
1727 // layer's "screen space" and local content space.
1728 layer_draw_properties
.screen_space_transform
=
1729 data_from_ancestor
.full_hierarchy_matrix
;
1730 layer_draw_properties
.screen_space_transform
.PreconcatTransform
1731 (layer_draw_properties
.target_space_transform
);
1733 bool layer_can_use_lcd_text
= true;
1734 bool subtree_can_use_lcd_text
= true;
1735 if (!globals
.layers_always_allowed_lcd_text
) {
1736 // To avoid color fringing, LCD text should only be used on opaque layers
1737 // with just integral translation.
1738 subtree_can_use_lcd_text
= data_from_ancestor
.subtree_can_use_lcd_text
&&
1739 accumulated_draw_opacity
== 1.f
&&
1740 layer_draw_properties
.target_space_transform
1741 .IsIdentityOrIntegerTranslation();
1742 // Also disable LCD text locally for non-opaque content.
1743 layer_can_use_lcd_text
= subtree_can_use_lcd_text
&&
1744 layer
->contents_opaque();
1747 // full_hierarchy_matrix is the matrix that transforms objects between screen
1748 // space (except projection matrix) and the most recent RenderSurfaceImpl's
1749 // space. next_hierarchy_matrix will only change if this layer uses a new
1750 // RenderSurfaceImpl, otherwise remains the same.
1751 data_for_children
.full_hierarchy_matrix
=
1752 data_from_ancestor
.full_hierarchy_matrix
;
1754 bool render_to_separate_surface
=
1755 IsRootLayer(layer
) ||
1756 (globals
.can_render_to_separate_surface
&& layer
->render_surface());
1758 if (render_to_separate_surface
) {
1759 DCHECK(layer
->render_surface());
1760 // Check back-face visibility before continuing with this surface and its
1762 if (!layer
->double_sided() &&
1763 IsSurfaceBackFaceVisible(layer
, combined_transform
)) {
1767 RenderSurfaceImpl
* render_surface
= layer
->render_surface();
1769 if (IsRootLayer(layer
)) {
1770 // The root layer's render surface size is predetermined and so the root
1771 // layer can't directly support non-identity transforms. It should just
1772 // forward top-level transforms to the rest of the tree.
1773 data_for_children
.parent_matrix
= combined_transform
;
1775 // The owning layer's draw transform has a scale from content to layer
1776 // space which we do not want; so here we use the combined_transform
1777 // instead of the draw_transform. However, we do need to add a different
1778 // scale factor that accounts for the surface's pixel dimensions.
1779 // Remove the combined_transform scale from the draw transform.
1780 gfx::Transform draw_transform
= combined_transform
;
1781 draw_transform
.Scale(1.0 / combined_transform_scales
.x(),
1782 1.0 / combined_transform_scales
.y());
1783 render_surface
->SetDrawTransform(draw_transform
);
1785 // The owning layer's transform was re-parented by the surface, so the
1786 // layer's new draw_transform only needs to scale the layer to surface
1788 layer_draw_properties
.target_space_transform
.MakeIdentity();
1789 layer_draw_properties
.target_space_transform
.Scale(
1790 combined_transform_scales
.x(), combined_transform_scales
.y());
1792 // Inside the surface's subtree, we scale everything to the owning layer's
1793 // scale. The sublayer matrix transforms layer rects into target surface
1794 // content space. Conceptually, all layers in the subtree inherit the
1795 // scale at the point of the render surface in the transform hierarchy,
1796 // but we apply it explicitly to the owning layer and the remainder of the
1797 // subtree independently.
1798 DCHECK(data_for_children
.parent_matrix
.IsIdentity());
1799 data_for_children
.parent_matrix
.Scale(combined_transform_scales
.x(),
1800 combined_transform_scales
.y());
1803 // The opacity value is moved from the layer to its surface, so that the
1804 // entire subtree properly inherits opacity.
1805 render_surface
->SetDrawOpacity(accumulated_draw_opacity
);
1806 layer_draw_properties
.opacity
= 1.f
;
1807 DCHECK_EQ(layer
->draw_blend_mode(), SkXfermode::kSrcOver_Mode
);
1809 layer_draw_properties
.screen_space_transform_is_animating
=
1810 animating_transform_to_screen
;
1812 // Update the aggregate hierarchy matrix to include the transform of the
1813 // newly created RenderSurfaceImpl.
1814 data_for_children
.full_hierarchy_matrix
.PreconcatTransform(
1815 render_surface
->draw_transform());
1817 // A render surface inherently acts as a flattening point for the content of
1819 data_for_children
.full_hierarchy_matrix
.FlattenTo2d();
1821 if (layer
->mask_layer()) {
1822 DrawProperties
& mask_layer_draw_properties
=
1823 layer
->mask_layer()->draw_properties();
1824 mask_layer_draw_properties
.visible_layer_rect
=
1825 gfx::Rect(layer
->bounds());
1826 // Temporarily copy the draw transform of the mask's owning layer into the
1827 // mask layer draw properties. This won't actually get used for drawing
1828 // (the render surface uses the mask texture directly), but will get used
1829 // to get the correct contents scale.
1830 // TODO(enne): do something similar for property trees.
1831 mask_layer_draw_properties
.target_space_transform
=
1832 layer_draw_properties
.target_space_transform
;
1835 if (layer
->replica_layer() && layer
->replica_layer()->mask_layer()) {
1836 DrawProperties
& replica_mask_draw_properties
=
1837 layer
->replica_layer()->mask_layer()->draw_properties();
1838 replica_mask_draw_properties
.visible_layer_rect
=
1839 gfx::Rect(layer
->bounds());
1840 replica_mask_draw_properties
.target_space_transform
=
1841 layer_draw_properties
.target_space_transform
;
1844 layer_or_ancestor_clips_descendants
= false;
1845 bool subtree_is_clipped_by_surface_bounds
= false;
1846 if (ancestor_clips_subtree
) {
1847 // It may be the layer or the surface doing the clipping of the subtree,
1848 // but in either case, we'll be clipping to the projected clip rect of our
1850 gfx::Transform
inverse_surface_draw_transform(
1851 gfx::Transform::kSkipInitialization
);
1852 if (!render_surface
->draw_transform().GetInverse(
1853 &inverse_surface_draw_transform
)) {
1854 // TODO(shawnsingh): Either we need to handle uninvertible transforms
1855 // here, or DCHECK that the transform is invertible.
1858 gfx::Rect surface_clip_rect_in_target_space
= gfx::IntersectRects(
1859 data_from_ancestor
.clip_rect_of_target_surface_in_target_space
,
1860 ancestor_clip_rect_in_target_space
);
1861 gfx::Rect projected_surface_rect
= MathUtil::ProjectEnclosingClippedRect(
1862 inverse_surface_draw_transform
, surface_clip_rect_in_target_space
);
1864 if (layer_draw_properties
.num_unclipped_descendants
> 0u) {
1865 // If we have unclipped descendants, we cannot count on the render
1866 // surface's bounds clipping our subtree: the unclipped descendants
1867 // could cause us to expand our bounds. In this case, we must rely on
1868 // layer clipping for correctess. NB: since we can only encounter
1869 // translations between a clip child and its clip parent, clipping is
1870 // guaranteed to be exact in this case.
1871 layer_or_ancestor_clips_descendants
= true;
1872 clip_rect_in_target_space
= projected_surface_rect
;
1874 // The new render_surface here will correctly clip the entire subtree.
1875 // So, we do not need to continue propagating the clipping state further
1876 // down the tree. This way, we can avoid transforming clip rects from
1877 // ancestor target surface space to current target surface space that
1878 // could cause more w < 0 headaches. The render surface clip rect is
1879 // expressed in the space where this surface draws, i.e. the same space
1880 // as clip_rect_from_ancestor_in_ancestor_target_space.
1881 render_surface
->SetClipRect(ancestor_clip_rect_in_target_space
);
1882 clip_rect_of_target_surface_in_target_space
= projected_surface_rect
;
1883 subtree_is_clipped_by_surface_bounds
= true;
1887 DCHECK(layer
->render_surface());
1888 DCHECK(!layer
->parent() || layer
->parent()->render_target() ==
1889 accumulated_surface_state
->back().render_target
);
1891 accumulated_surface_state
->push_back(AccumulatedSurfaceState(layer
));
1893 render_surface
->SetIsClipped(subtree_is_clipped_by_surface_bounds
);
1894 if (!subtree_is_clipped_by_surface_bounds
) {
1895 render_surface
->SetClipRect(gfx::Rect());
1896 clip_rect_of_target_surface_in_target_space
=
1897 data_from_ancestor
.clip_rect_of_target_surface_in_target_space
;
1900 // If the new render surface is drawn translucent or with a non-integral
1901 // translation then the subtree that gets drawn on this render surface
1902 // cannot use LCD text.
1903 data_for_children
.subtree_can_use_lcd_text
= subtree_can_use_lcd_text
;
1906 DCHECK(layer
->parent());
1908 // Note: layer_draw_properties.target_space_transform is computed above,
1909 // before this if-else statement.
1910 layer_draw_properties
.screen_space_transform_is_animating
=
1911 animating_transform_to_screen
;
1912 layer_draw_properties
.opacity
= accumulated_draw_opacity
;
1913 DCHECK_EQ(layer
->draw_blend_mode(), layer
->blend_mode());
1914 data_for_children
.parent_matrix
= combined_transform
;
1916 // Layers without render_surfaces directly inherit the ancestor's clip
1918 layer_or_ancestor_clips_descendants
= ancestor_clips_subtree
;
1919 if (ancestor_clips_subtree
) {
1920 clip_rect_in_target_space
=
1921 ancestor_clip_rect_in_target_space
;
1924 // The surface's cached clip rect value propagates regardless of what
1925 // clipping goes on between layers here.
1926 clip_rect_of_target_surface_in_target_space
=
1927 data_from_ancestor
.clip_rect_of_target_surface_in_target_space
;
1930 layer_draw_properties
.can_use_lcd_text
= layer_can_use_lcd_text
;
1932 // The layer bounds() includes the layer's bounds_delta() which we want
1933 // for the clip rect.
1934 gfx::Rect rect_in_target_space
= MathUtil::MapEnclosingClippedRect(
1935 layer
->draw_transform(), gfx::Rect(layer
->bounds()));
1937 if (LayerClipsSubtree(layer
)) {
1938 layer_or_ancestor_clips_descendants
= true;
1939 if (ancestor_clips_subtree
&& !render_to_separate_surface
) {
1940 // A layer without render surface shares the same target as its ancestor.
1941 clip_rect_in_target_space
=
1942 ancestor_clip_rect_in_target_space
;
1943 clip_rect_in_target_space
.Intersect(rect_in_target_space
);
1945 clip_rect_in_target_space
= rect_in_target_space
;
1949 // Tell the layer the rect that it's clipped by. In theory we could use a
1950 // tighter clip rect here (drawable_content_rect), but that actually does not
1951 // reduce how much would be drawn, and instead it would create unnecessary
1952 // changes to scissor state affecting GPU performance. Our clip information
1953 // is used in the recursion below, so we must set it beforehand.
1954 DCHECK_EQ(layer_or_ancestor_clips_descendants
, layer
->is_clipped());
1955 if (layer_or_ancestor_clips_descendants
) {
1956 layer_draw_properties
.clip_rect
= clip_rect_in_target_space
;
1958 // Initialize the clip rect to a safe value that will not clip the
1959 // layer, just in case clipping is still accidentally used.
1960 layer_draw_properties
.clip_rect
= rect_in_target_space
;
1963 if (!layer
->children().empty()) {
1964 if (layer
== globals
.elastic_overscroll_application_layer
) {
1965 data_for_children
.parent_matrix
.Translate(
1966 -globals
.elastic_overscroll
.x(), -globals
.elastic_overscroll
.y());
1969 // Flatten to 2D if the layer doesn't preserve 3D.
1970 if (layer
->should_flatten_transform())
1971 data_for_children
.parent_matrix
.FlattenTo2d();
1973 data_for_children
.scroll_compensation_matrix
=
1974 ComputeScrollCompensationMatrixForChildren(
1976 data_from_ancestor
.parent_matrix
,
1977 data_from_ancestor
.scroll_compensation_matrix
,
1978 effective_scroll_delta
);
1979 data_for_children
.fixed_container
=
1980 layer
->IsContainerForFixedPositionLayers() ?
1981 layer
: data_from_ancestor
.fixed_container
;
1983 data_for_children
.clip_rect_in_target_space
= clip_rect_in_target_space
;
1984 data_for_children
.clip_rect_of_target_surface_in_target_space
=
1985 clip_rect_of_target_surface_in_target_space
;
1986 data_for_children
.ancestor_clips_subtree
=
1987 layer_or_ancestor_clips_descendants
;
1988 data_for_children
.subtree_is_visible_from_ancestor
= layer_is_drawn
;
1991 std::vector
<LayerImpl
*> sorted_children
;
1992 if (layer_draw_properties
.has_child_with_a_scroll_parent
)
1993 SortChildrenForRecursion(&sorted_children
, *layer
);
1995 for (size_t i
= 0; i
< layer
->children().size(); ++i
) {
1996 // If one of layer's children has a scroll parent, then we may have to
1997 // visit the children out of order. The new order is stored in
1998 // sorted_children. Otherwise, we'll grab the child directly from the
1999 // layer's list of children.
2002 layer_draw_properties
.has_child_with_a_scroll_parent
2003 ? sorted_children
[i
]
2004 : LayerTreeHostCommon::get_layer_as_raw_ptr(layer
->children(), i
);
2006 CalculateDrawPropertiesInternal(child
, globals
, data_for_children
,
2007 accumulated_surface_state
);
2009 if (child
->layer_or_descendant_is_drawn()) {
2010 bool layer_or_descendant_is_drawn
= true;
2011 layer
->set_layer_or_descendant_is_drawn(layer_or_descendant_is_drawn
);
2015 // Compute the total drawable_content_rect for this subtree (the rect is in
2016 // target surface space).
2017 gfx::Rect local_drawable_content_rect_of_subtree
=
2018 accumulated_surface_state
->back().drawable_content_rect
;
2019 if (render_to_separate_surface
) {
2020 DCHECK(accumulated_surface_state
->back().render_target
== layer
);
2021 accumulated_surface_state
->pop_back();
2024 // Compute the layer's drawable content rect (the rect is in target surface
2026 layer_draw_properties
.drawable_content_rect
= rect_in_target_space
;
2027 if (layer_or_ancestor_clips_descendants
) {
2028 layer_draw_properties
.drawable_content_rect
.Intersect(
2029 clip_rect_in_target_space
);
2031 if (layer
->DrawsContent()) {
2032 local_drawable_content_rect_of_subtree
.Union(
2033 layer_draw_properties
.drawable_content_rect
);
2036 // Compute the layer's visible content rect (the rect is in content space).
2037 layer_draw_properties
.visible_layer_rect
= CalculateVisibleLayerRect(
2038 layer
, clip_rect_of_target_surface_in_target_space
, rect_in_target_space
);
2040 // Compute the remaining properties for the render surface, if the layer has
2042 if (IsRootLayer(layer
)) {
2043 // The root layer's surface's content_rect is always the entire viewport.
2044 DCHECK(render_to_separate_surface
);
2045 layer
->render_surface()->SetContentRect(
2046 ancestor_clip_rect_in_target_space
);
2047 } else if (render_to_separate_surface
) {
2048 RenderSurfaceImpl
* render_surface
= layer
->render_surface();
2049 gfx::Rect clipped_content_rect
= local_drawable_content_rect_of_subtree
;
2051 // Don't clip if the layer is reflected as the reflection shouldn't be
2053 if (!layer
->replica_layer()) {
2054 // Note, it is correct to use data_from_ancestor.ancestor_clips_subtree
2055 // here, because we are looking at this layer's render_surface, not the
2057 if (render_surface
->is_clipped() && !clipped_content_rect
.IsEmpty()) {
2058 gfx::Rect surface_clip_rect
= LayerTreeHostCommon::CalculateVisibleRect(
2059 render_surface
->clip_rect(),
2060 clipped_content_rect
,
2061 render_surface
->draw_transform());
2062 clipped_content_rect
.Intersect(surface_clip_rect
);
2066 // The RenderSurfaceImpl backing texture cannot exceed the maximum supported
2068 clipped_content_rect
.set_width(
2069 std::min(clipped_content_rect
.width(), globals
.max_texture_size
));
2070 clipped_content_rect
.set_height(
2071 std::min(clipped_content_rect
.height(), globals
.max_texture_size
));
2073 // Layers having a non-default blend mode will blend with the content
2074 // inside its parent's render target. This render target should be
2075 // either root_for_isolated_group, or the root of the layer tree.
2076 // Otherwise, this layer will use an incomplete backdrop, limited to its
2077 // render target and the blending result will be incorrect.
2078 DCHECK(layer
->uses_default_blend_mode() || IsRootLayer(layer
) ||
2079 !layer
->parent()->render_target() ||
2080 IsRootLayer(layer
->parent()->render_target()) ||
2081 layer
->parent()->render_target()->is_root_for_isolated_group());
2083 render_surface
->SetContentRect(clipped_content_rect
);
2085 if (clipped_content_rect
.IsEmpty()) {
2089 // The owning layer's screen_space_transform has a scale from content to
2090 // layer space which we need to undo and replace with a scale from the
2091 // surface's subtree into layer space.
2092 gfx::Transform screen_space_transform
= layer
->screen_space_transform();
2093 screen_space_transform
.Scale(1.0 / combined_transform_scales
.x(),
2094 1.0 / combined_transform_scales
.y());
2095 render_surface
->SetScreenSpaceTransform(screen_space_transform
);
2097 if (layer
->replica_layer()) {
2098 gfx::Transform surface_origin_to_replica_origin_transform
;
2099 surface_origin_to_replica_origin_transform
.Scale(
2100 combined_transform_scales
.x(), combined_transform_scales
.y());
2101 surface_origin_to_replica_origin_transform
.Translate(
2102 layer
->replica_layer()->position().x() +
2103 layer
->replica_layer()->transform_origin().x(),
2104 layer
->replica_layer()->position().y() +
2105 layer
->replica_layer()->transform_origin().y());
2106 surface_origin_to_replica_origin_transform
.PreconcatTransform(
2107 layer
->replica_layer()->transform());
2108 surface_origin_to_replica_origin_transform
.Translate(
2109 -layer
->replica_layer()->transform_origin().x(),
2110 -layer
->replica_layer()->transform_origin().y());
2111 surface_origin_to_replica_origin_transform
.Scale(
2112 1.0 / combined_transform_scales
.x(),
2113 1.0 / combined_transform_scales
.y());
2115 // Compute the replica's "originTransform" that maps from the replica's
2116 // origin space to the target surface origin space.
2117 gfx::Transform replica_origin_transform
=
2118 layer
->render_surface()->draw_transform() *
2119 surface_origin_to_replica_origin_transform
;
2120 render_surface
->SetReplicaDrawTransform(replica_origin_transform
);
2122 // Compute the replica's "screen_space_transform" that maps from the
2123 // replica's origin space to the screen's origin space.
2124 gfx::Transform replica_screen_space_transform
=
2125 layer
->render_surface()->screen_space_transform() *
2126 surface_origin_to_replica_origin_transform
;
2127 render_surface
->SetReplicaScreenSpaceTransform(
2128 replica_screen_space_transform
);
2132 SavePaintPropertiesLayer(layer
);
2134 UpdateAccumulatedSurfaceState(layer
, local_drawable_content_rect_of_subtree
,
2135 accumulated_surface_state
);
2136 } // NOLINT(readability/fn_size)
2138 static void ProcessCalcDrawPropsInputs(
2139 const LayerTreeHostCommon::CalcDrawPropsImplInputs
& inputs
,
2140 SubtreeGlobals
* globals
,
2141 DataForRecursion
* data_for_recursion
) {
2142 DCHECK(inputs
.root_layer
);
2143 DCHECK(IsRootLayer(inputs
.root_layer
));
2144 DCHECK(inputs
.render_surface_layer_list
);
2146 gfx::Transform identity_matrix
;
2148 // The root layer's render_surface should receive the device viewport as the
2149 // initial clip rect.
2150 gfx::Rect
device_viewport_rect(inputs
.device_viewport_size
);
2152 gfx::Vector2dF device_transform_scale_components
=
2153 MathUtil::ComputeTransform2dScaleComponents(inputs
.device_transform
, 1.f
);
2154 // Not handling the rare case of different x and y device scale.
2155 float device_transform_scale
=
2156 std::max(device_transform_scale_components
.x(),
2157 device_transform_scale_components
.y());
2159 gfx::Transform scaled_device_transform
= inputs
.device_transform
;
2160 scaled_device_transform
.Scale(inputs
.device_scale_factor
,
2161 inputs
.device_scale_factor
);
2163 globals
->max_texture_size
= inputs
.max_texture_size
;
2164 globals
->device_scale_factor
=
2165 inputs
.device_scale_factor
* device_transform_scale
;
2166 globals
->page_scale_factor
= inputs
.page_scale_factor
;
2167 globals
->page_scale_layer
= inputs
.page_scale_layer
;
2168 globals
->elastic_overscroll
= inputs
.elastic_overscroll
;
2169 globals
->elastic_overscroll_application_layer
=
2170 inputs
.elastic_overscroll_application_layer
;
2171 globals
->can_render_to_separate_surface
=
2172 inputs
.can_render_to_separate_surface
;
2173 globals
->can_adjust_raster_scales
= inputs
.can_adjust_raster_scales
;
2174 globals
->layers_always_allowed_lcd_text
=
2175 inputs
.layers_always_allowed_lcd_text
;
2177 data_for_recursion
->parent_matrix
= scaled_device_transform
;
2178 data_for_recursion
->full_hierarchy_matrix
= identity_matrix
;
2179 data_for_recursion
->scroll_compensation_matrix
= identity_matrix
;
2180 data_for_recursion
->fixed_container
= inputs
.root_layer
;
2181 data_for_recursion
->clip_rect_in_target_space
= device_viewport_rect
;
2182 data_for_recursion
->clip_rect_of_target_surface_in_target_space
=
2183 device_viewport_rect
;
2184 data_for_recursion
->maximum_animation_contents_scale
= 0.f
;
2185 data_for_recursion
->starting_animation_contents_scale
= 0.f
;
2186 data_for_recursion
->ancestor_is_animating_scale
= false;
2187 data_for_recursion
->ancestor_clips_subtree
= true;
2188 data_for_recursion
->in_subtree_of_page_scale_layer
= false;
2189 data_for_recursion
->subtree_can_use_lcd_text
= inputs
.can_use_lcd_text
;
2190 data_for_recursion
->subtree_is_visible_from_ancestor
= true;
2193 void LayerTreeHostCommon::UpdateRenderSurface(
2195 bool can_render_to_separate_surface
,
2196 gfx::Transform
* transform
,
2197 bool* draw_transform_is_axis_aligned
) {
2198 bool preserves_2d_axis_alignment
=
2199 transform
->Preserves2dAxisAlignment() && *draw_transform_is_axis_aligned
;
2200 if (IsRootLayer(layer
) || (can_render_to_separate_surface
&&
2201 SubtreeShouldRenderToSeparateSurface(
2202 layer
, preserves_2d_axis_alignment
))) {
2203 // We reset the transform here so that any axis-changing transforms
2204 // will now be relative to this RenderSurface.
2205 transform
->MakeIdentity();
2206 *draw_transform_is_axis_aligned
= true;
2207 layer
->SetHasRenderSurface(true);
2210 layer
->SetHasRenderSurface(false);
2213 void LayerTreeHostCommon::UpdateRenderSurfaces(
2215 bool can_render_to_separate_surface
,
2216 const gfx::Transform
& parent_transform
,
2217 bool draw_transform_is_axis_aligned
) {
2218 gfx::Transform transform_for_children
= layer
->transform();
2219 transform_for_children
*= parent_transform
;
2220 draw_transform_is_axis_aligned
&= layer
->AnimationsPreserveAxisAlignment();
2221 UpdateRenderSurface(layer
, can_render_to_separate_surface
,
2222 &transform_for_children
, &draw_transform_is_axis_aligned
);
2224 for (size_t i
= 0; i
< layer
->children().size(); ++i
) {
2225 UpdateRenderSurfaces(layer
->children()[i
].get(),
2226 can_render_to_separate_surface
, transform_for_children
,
2227 draw_transform_is_axis_aligned
);
2231 static bool ApproximatelyEqual(const gfx::Rect
& r1
, const gfx::Rect
& r2
) {
2232 // TODO(vollick): This tolerance should be lower: crbug.com/471786
2233 static const int tolerance
= 1;
2236 return std::min(r2
.width(), r2
.height()) < tolerance
;
2239 return std::min(r1
.width(), r1
.height()) < tolerance
;
2241 return std::abs(r1
.x() - r2
.x()) <= tolerance
&&
2242 std::abs(r1
.y() - r2
.y()) <= tolerance
&&
2243 std::abs(r1
.right() - r2
.right()) <= tolerance
&&
2244 std::abs(r1
.bottom() - r2
.bottom()) <= tolerance
;
2247 static bool ApproximatelyEqual(const gfx::Transform
& a
,
2248 const gfx::Transform
& b
) {
2249 static const float component_tolerance
= 0.1f
;
2251 // We may have a larger discrepancy in the scroll components due to snapping
2252 // (floating point error might round the other way).
2253 static const float translation_tolerance
= 1.f
;
2255 for (int row
= 0; row
< 4; row
++) {
2256 for (int col
= 0; col
< 4; col
++) {
2258 std::abs(a
.matrix().get(row
, col
) - b
.matrix().get(row
, col
));
2259 const float tolerance
=
2260 col
== 3 && row
< 3 ? translation_tolerance
: component_tolerance
;
2261 if (delta
> tolerance
)
2269 void VerifyPropertyTreeValuesForSurface(RenderSurfaceImpl
* render_surface
,
2270 PropertyTrees
* property_trees
) {
2271 const bool render_surface_draw_transforms_match
=
2272 ApproximatelyEqual(render_surface
->draw_transform(),
2273 DrawTransformOfRenderSurfaceFromPropertyTrees(
2274 render_surface
, property_trees
->transform_tree
));
2275 CHECK(render_surface_draw_transforms_match
)
2276 << "expected: " << render_surface
->draw_transform().ToString()
2278 << DrawTransformOfRenderSurfaceFromPropertyTrees(
2279 render_surface
, property_trees
->transform_tree
)
2282 const bool render_surface_screen_space_transform_match
=
2283 ApproximatelyEqual(render_surface
->screen_space_transform(),
2284 ScreenSpaceTransformOfRenderSurfaceFromPropertyTrees(
2285 render_surface
, property_trees
->transform_tree
));
2286 CHECK(render_surface_screen_space_transform_match
)
2287 << "expected: " << render_surface
->screen_space_transform().ToString()
2289 << ScreenSpaceTransformOfRenderSurfaceFromPropertyTrees(
2290 render_surface
, property_trees
->transform_tree
)
2293 const bool render_surface_replica_draw_transforms_match
=
2294 ApproximatelyEqual(render_surface
->replica_draw_transform(),
2295 DrawTransformOfRenderSurfaceReplicaFromPropertyTrees(
2296 render_surface
, property_trees
->transform_tree
));
2297 CHECK(render_surface_replica_draw_transforms_match
)
2298 << "expected: " << render_surface
->replica_draw_transform().ToString()
2300 << DrawTransformOfRenderSurfaceReplicaFromPropertyTrees(
2301 render_surface
, property_trees
->transform_tree
)
2304 const bool render_surface_replica_screen_space_transforms_match
=
2306 render_surface
->replica_screen_space_transform(),
2307 ScreenSpaceTransformOfRenderSurfaceReplicaFromPropertyTrees(
2308 render_surface
, property_trees
->transform_tree
));
2309 CHECK(render_surface_replica_screen_space_transforms_match
)
2311 << render_surface
->replica_screen_space_transform().ToString()
2313 << ScreenSpaceTransformOfRenderSurfaceReplicaFromPropertyTrees(
2314 render_surface
, property_trees
->transform_tree
)
2317 CHECK_EQ(render_surface
->is_clipped(),
2318 RenderSurfaceIsClippedFromPropertyTrees(render_surface
,
2319 property_trees
->clip_tree
));
2321 const bool render_surface_clip_rects_match
=
2322 ApproximatelyEqual(render_surface
->clip_rect(),
2323 ClipRectOfRenderSurfaceFromPropertyTrees(
2324 render_surface
, property_trees
->clip_tree
));
2325 CHECK(render_surface_clip_rects_match
)
2326 << "expected: " << render_surface
->clip_rect().ToString() << " actual: "
2327 << ClipRectOfRenderSurfaceFromPropertyTrees(render_surface
,
2328 property_trees
->clip_tree
)
2331 const bool render_surface_content_rects_match
=
2332 ApproximatelyEqual(render_surface
->content_rect(),
2333 render_surface
->content_rect_from_property_trees());
2334 CHECK(render_surface_content_rects_match
)
2335 << "expected: " << render_surface
->content_rect().ToString()
2337 << render_surface
->content_rect_from_property_trees().ToString();
2339 CHECK_EQ(render_surface
->draw_opacity(),
2340 DrawOpacityOfRenderSurfaceFromPropertyTrees(
2341 render_surface
, property_trees
->effect_tree
));
2344 void VerifyPropertyTreeValuesForLayer(LayerImpl
* current_layer
,
2345 PropertyTrees
* property_trees
,
2346 bool layers_always_allowed_lcd_text
,
2347 bool can_use_lcd_text
) {
2348 const bool visible_rects_match
=
2349 ApproximatelyEqual(current_layer
->visible_layer_rect(),
2350 current_layer
->visible_rect_from_property_trees());
2351 CHECK(visible_rects_match
)
2352 << "expected: " << current_layer
->visible_layer_rect().ToString()
2354 << current_layer
->visible_rect_from_property_trees().ToString();
2356 const bool draw_transforms_match
=
2357 ApproximatelyEqual(current_layer
->draw_transform(),
2358 DrawTransformFromPropertyTrees(
2359 current_layer
, property_trees
->transform_tree
));
2360 CHECK(draw_transforms_match
)
2361 << "expected: " << current_layer
->draw_transform().ToString()
2363 << DrawTransformFromPropertyTrees(
2364 current_layer
, property_trees
->transform_tree
).ToString();
2366 const bool draw_opacities_match
=
2367 current_layer
->draw_opacity() ==
2368 DrawOpacityFromPropertyTrees(current_layer
, property_trees
->effect_tree
);
2369 CHECK(draw_opacities_match
)
2370 << "expected: " << current_layer
->draw_opacity()
2371 << " actual: " << DrawOpacityFromPropertyTrees(
2372 current_layer
, property_trees
->effect_tree
);
2374 const bool can_use_lcd_text_match
=
2375 CanUseLcdTextFromPropertyTrees(
2376 current_layer
, layers_always_allowed_lcd_text
, can_use_lcd_text
,
2377 property_trees
) == current_layer
->can_use_lcd_text();
2378 CHECK(can_use_lcd_text_match
);
2380 CHECK_EQ(current_layer
->screen_space_transform_is_animating(),
2381 ScreenSpaceTransformIsAnimatingFromPropertyTrees(
2382 current_layer
, property_trees
->transform_tree
));
2384 const bool drawable_content_rects_match
=
2385 ApproximatelyEqual(current_layer
->drawable_content_rect(),
2386 DrawableContentRectFromPropertyTrees(
2387 current_layer
, property_trees
->transform_tree
));
2388 CHECK(drawable_content_rects_match
)
2389 << "expected: " << current_layer
->drawable_content_rect().ToString()
2391 << DrawableContentRectFromPropertyTrees(current_layer
,
2392 property_trees
->transform_tree
)
2395 const bool clip_rects_match
= ApproximatelyEqual(
2396 current_layer
->clip_rect(),
2397 ClipRectFromPropertyTrees(current_layer
, property_trees
->transform_tree
));
2398 CHECK(clip_rects_match
) << "expected: "
2399 << current_layer
->clip_rect().ToString()
2401 << ClipRectFromPropertyTrees(
2402 current_layer
, property_trees
->transform_tree
)
2405 CHECK_EQ(current_layer
->draw_properties().maximum_animation_contents_scale
,
2406 MaximumAnimationTargetScaleFromPropertyTrees(
2407 current_layer
, property_trees
->transform_tree
));
2408 CHECK_EQ(current_layer
->draw_properties().starting_animation_contents_scale
,
2409 StartingAnimationScaleFromPropertyTrees(
2410 current_layer
, property_trees
->transform_tree
));
2413 void VerifyPropertyTreeValues(
2414 LayerTreeHostCommon::CalcDrawPropsImplInputs
* inputs
) {
2415 LayerIterator it
, end
;
2416 for (it
= LayerIterator::Begin(inputs
->render_surface_layer_list
),
2417 end
= LayerIterator::End(inputs
->render_surface_layer_list
);
2419 LayerImpl
* current_layer
= *it
;
2420 if (it
.represents_target_render_surface())
2421 VerifyPropertyTreeValuesForSurface(current_layer
->render_surface(),
2422 inputs
->property_trees
);
2423 if (!it
.represents_itself() || !current_layer
->DrawsContent())
2425 VerifyPropertyTreeValuesForLayer(current_layer
, inputs
->property_trees
,
2426 inputs
->layers_always_allowed_lcd_text
,
2427 inputs
->can_use_lcd_text
);
2431 enum PropertyTreeOption
{
2432 BUILD_PROPERTY_TREES_IF_NEEDED
,
2433 DONT_BUILD_PROPERTY_TREES
2436 void CalculateRenderTargetInternal(LayerImpl
* layer
,
2437 bool subtree_visible_from_ancestor
,
2438 bool can_render_to_separate_surface
) {
2439 const bool layer_is_visible
=
2440 subtree_visible_from_ancestor
&& !layer
->hide_layer_and_subtree();
2441 const bool layer_is_drawn
= layer_is_visible
|| layer
->HasCopyRequest();
2443 // The root layer cannot be skipped.
2444 if (!IsRootLayer(layer
) && SubtreeShouldBeSkipped(layer
, layer_is_drawn
)) {
2445 layer
->draw_properties().render_target
= nullptr;
2449 bool render_to_separate_surface
=
2450 IsRootLayer(layer
) ||
2451 (can_render_to_separate_surface
&& layer
->render_surface());
2453 if (render_to_separate_surface
) {
2454 DCHECK(layer
->render_surface());
2455 layer
->draw_properties().render_target
= layer
;
2457 if (layer
->mask_layer())
2458 layer
->mask_layer()->draw_properties().render_target
= layer
;
2460 if (layer
->replica_layer() && layer
->replica_layer()->mask_layer())
2461 layer
->replica_layer()->mask_layer()->draw_properties().render_target
=
2465 DCHECK(layer
->parent());
2466 layer
->draw_properties().render_target
= layer
->parent()->render_target();
2469 for (size_t i
= 0; i
< layer
->children().size(); ++i
) {
2470 CalculateRenderTargetInternal(
2471 LayerTreeHostCommon::get_layer_as_raw_ptr(layer
->children(), i
),
2472 layer_is_drawn
, can_render_to_separate_surface
);
2476 void CalculateRenderSurfaceLayerListInternal(
2478 PropertyTrees
* property_trees
,
2479 LayerImplList
* render_surface_layer_list
,
2480 LayerImplList
* descendants
,
2481 RenderSurfaceImpl
* nearest_occlusion_immune_ancestor
,
2482 bool subtree_visible_from_ancestor
,
2483 const bool can_render_to_separate_surface
,
2484 const int current_render_surface_layer_list_id
,
2485 const bool verify_property_trees
) {
2486 // This calculates top level Render Surface Layer List, and Layer List for all
2489 // |layer| is current layer.
2491 // |render_surface_layer_list| is the top level RenderSurfaceLayerList.
2493 // |descendants| is used to determine what's in current layer's render
2494 // surface's layer list.
2496 // |subtree_visible_from_ancestor| is set during recursion to affect current
2499 // |can_render_to_separate_surface| and |current_render_surface_layer_list_id|
2500 // are settings that should stay the same during recursion.
2502 // Layers that are marked as hidden will hide themselves and their subtree.
2503 // Exception: Layers with copy requests, whether hidden or not, must be drawn
2504 // anyway. In this case, we will inform their subtree they are visible to get
2505 // the right results.
2506 const bool layer_is_visible
=
2507 subtree_visible_from_ancestor
&& !layer
->hide_layer_and_subtree();
2508 const bool layer_is_drawn
= layer_is_visible
|| layer
->HasCopyRequest();
2510 // The root layer cannot be skipped.
2511 if (!IsRootLayer(layer
) && SubtreeShouldBeSkipped(layer
, layer_is_drawn
)) {
2512 if (layer
->render_surface())
2513 layer
->ClearRenderSurfaceLayerList();
2514 layer
->draw_properties().render_target
= nullptr;
2518 bool render_to_separate_surface
=
2519 IsRootLayer(layer
) ||
2520 (can_render_to_separate_surface
&& layer
->render_surface());
2522 if (render_to_separate_surface
) {
2523 DCHECK(layer
->render_surface());
2524 if (!layer
->double_sided() &&
2525 IsSurfaceBackFaceVisible(layer
, layer
->draw_transform())) {
2526 layer
->ClearRenderSurfaceLayerList();
2527 layer
->draw_properties().render_target
= nullptr;
2530 if (IsRootLayer(layer
)) {
2531 // The root surface does not contribute to any other surface, it has no
2533 layer
->render_surface()->set_contributes_to_drawn_surface(false);
2535 // Even if the |layer_is_drawn|, it only contributes to a drawn surface
2536 // when the |layer_is_visible|.
2537 layer
->render_surface()->set_contributes_to_drawn_surface(
2541 // Ignore occlusion from outside the surface when surface contents need to
2542 // be fully drawn. Layers with copy-request need to be complete.
2543 // We could be smarter about layers with replica and exclude regions
2544 // where both layer and the replica are occluded, but this seems like an
2545 // overkill. The same is true for layers with filters that move pixels.
2546 // TODO(senorblanco): make this smarter for the SkImageFilter case (check
2547 // for pixel-moving filters)
2548 if (layer
->HasCopyRequest() || layer
->has_replica() ||
2549 layer
->filters().HasReferenceFilter() ||
2550 layer
->filters().HasFilterThatMovesPixels()) {
2551 nearest_occlusion_immune_ancestor
= layer
->render_surface();
2553 layer
->render_surface()->SetNearestOcclusionImmuneAncestor(
2554 nearest_occlusion_immune_ancestor
);
2555 layer
->ClearRenderSurfaceLayerList();
2557 render_surface_layer_list
->push_back(layer
);
2559 descendants
= &(layer
->render_surface()->layer_list());
2562 size_t descendants_size
= descendants
->size();
2564 bool layer_should_be_skipped
= LayerShouldBeSkipped(layer
, layer_is_drawn
);
2565 if (!layer_should_be_skipped
) {
2566 MarkLayerWithRenderSurfaceLayerListId(layer
,
2567 current_render_surface_layer_list_id
);
2568 descendants
->push_back(layer
);
2571 // The render surface's content rect is the union of drawable content rects
2572 // of the layers that draw into the surface. If the render surface is clipped,
2573 // it is also intersected with the render's surface clip rect.
2574 if (verify_property_trees
) {
2575 if (render_to_separate_surface
) {
2576 if (IsRootLayer(layer
)) {
2577 // The root layer's surface content rect is always the entire viewport.
2578 gfx::Rect viewport
=
2579 gfx::ToEnclosingRect(property_trees
->clip_tree
.ViewportClip());
2580 layer
->render_surface()->SetAccumulatedContentRect(viewport
);
2582 // If the owning layer of a render surface draws content, the content
2583 // rect of the render surface is initialized to the drawable content
2584 // rect of the layer.
2585 gfx::Rect content_rect
=
2586 layer
->DrawsContent() ? DrawableContentRectFromPropertyTrees(
2587 layer
, property_trees
->transform_tree
)
2589 layer
->render_surface()->SetAccumulatedContentRect(content_rect
);
2591 } else if (!layer_should_be_skipped
&&
2592 !IsRootLayer(layer
->render_target())) {
2593 // In this case, the layer's drawable content rect can expand the
2594 // content rect of the render surface it is drawing into.
2595 gfx::Rect surface_content_rect
=
2596 layer
->render_target()->render_surface()->accumulated_content_rect();
2597 surface_content_rect
.Union(DrawableContentRectFromPropertyTrees(
2598 layer
, property_trees
->transform_tree
));
2599 layer
->render_target()->render_surface()->SetAccumulatedContentRect(
2600 surface_content_rect
);
2604 for (auto& child_layer
: layer
->children()) {
2605 CalculateRenderSurfaceLayerListInternal(
2606 child_layer
, property_trees
, render_surface_layer_list
, descendants
,
2607 nearest_occlusion_immune_ancestor
, layer_is_drawn
,
2608 can_render_to_separate_surface
, current_render_surface_layer_list_id
,
2609 verify_property_trees
);
2611 // If the child is its own render target, then it has a render surface.
2612 if (child_layer
->render_target() == child_layer
&&
2613 !child_layer
->render_surface()->layer_list().empty() &&
2614 !child_layer
->render_surface()->content_rect().IsEmpty()) {
2615 // This child will contribute its render surface, which means
2616 // we need to mark just the mask layer (and replica mask layer)
2618 MarkMasksWithRenderSurfaceLayerListId(
2619 child_layer
, current_render_surface_layer_list_id
);
2620 descendants
->push_back(child_layer
);
2623 if (child_layer
->layer_or_descendant_is_drawn()) {
2624 bool layer_or_descendant_is_drawn
= true;
2625 layer
->set_layer_or_descendant_is_drawn(layer_or_descendant_is_drawn
);
2629 if (render_to_separate_surface
&& !IsRootLayer(layer
) &&
2630 layer
->render_surface()->layer_list().empty()) {
2631 RemoveSurfaceForEarlyExit(layer
, render_surface_layer_list
);
2635 if (verify_property_trees
&& render_to_separate_surface
&&
2636 !IsRootLayer(layer
)) {
2637 if (!layer
->replica_layer() &&
2638 RenderSurfaceIsClippedFromPropertyTrees(layer
->render_surface(),
2639 property_trees
->clip_tree
)) {
2640 // Here, we clip the render surface's content rect with its clip rect.
2641 // As the clip rect of render surface is in the surface's target space,
2642 // we first map the content rect into the target space, intersect it with
2643 // clip rect and project back the result to the surface space.
2644 gfx::Rect surface_content_rect
=
2645 layer
->render_surface()->accumulated_content_rect();
2647 if (!surface_content_rect
.IsEmpty()) {
2648 gfx::Rect surface_clip_rect
= LayerTreeHostCommon::CalculateVisibleRect(
2649 ClipRectOfRenderSurfaceFromPropertyTrees(layer
->render_surface(),
2650 property_trees
->clip_tree
),
2651 surface_content_rect
,
2652 DrawTransformOfRenderSurfaceFromPropertyTrees(
2653 layer
->render_surface(), property_trees
->transform_tree
));
2654 surface_content_rect
.Intersect(surface_clip_rect
);
2655 layer
->render_surface()->SetAccumulatedContentRect(
2656 surface_content_rect
);
2659 layer
->render_surface()->SetContentRectFromPropertyTrees(
2660 layer
->render_surface()->accumulated_content_rect());
2661 if (!IsRootLayer(layer
->parent()->render_target())) {
2662 // The surface's drawable content rect may expand the content rect
2663 // of its target's surface(surface's target's surface).
2664 gfx::Rect surface_target_rect
= layer
->parent()
2667 ->accumulated_content_rect();
2668 surface_target_rect
.Union(DrawableContentRectOfSurfaceFromPropertyTrees(
2669 layer
->render_surface(), property_trees
->transform_tree
));
2673 ->SetAccumulatedContentRect(surface_target_rect
);
2677 if (verify_property_trees
&& IsRootLayer(layer
))
2678 layer
->render_surface()->SetContentRectFromPropertyTrees(
2679 layer
->render_surface()->accumulated_content_rect());
2681 if (render_to_separate_surface
&& !IsRootLayer(layer
) &&
2682 layer
->render_surface()->content_rect().IsEmpty()) {
2683 RemoveSurfaceForEarlyExit(layer
, render_surface_layer_list
);
2687 // If neither this layer nor any of its children were added, early out.
2688 if (descendants_size
== descendants
->size()) {
2689 DCHECK(!render_to_separate_surface
|| IsRootLayer(layer
));
2693 if (layer
->HasContributingDelegatedRenderPasses()) {
2694 layer
->render_target()
2696 ->AddContributingDelegatedRenderPassLayer(layer
);
2700 void CalculateRenderTarget(
2701 LayerTreeHostCommon::CalcDrawPropsImplInputs
* inputs
) {
2702 CalculateRenderTargetInternal(inputs
->root_layer
, true,
2703 inputs
->can_render_to_separate_surface
);
2706 void CalculateRenderSurfaceLayerList(
2707 LayerTreeHostCommon::CalcDrawPropsImplInputs
* inputs
) {
2708 const bool subtree_visible_from_ancestor
= true;
2709 CalculateRenderSurfaceLayerListInternal(
2710 inputs
->root_layer
, inputs
->property_trees
,
2711 inputs
->render_surface_layer_list
, nullptr, nullptr,
2712 subtree_visible_from_ancestor
, inputs
->can_render_to_separate_surface
,
2713 inputs
->current_render_surface_layer_list_id
,
2714 inputs
->verify_property_trees
);
2717 void CalculateDrawPropertiesAndVerify(
2718 LayerTreeHostCommon::CalcDrawPropsImplInputs
* inputs
,
2719 PropertyTreeOption property_tree_option
) {
2720 SubtreeGlobals globals
;
2721 DataForRecursion data_for_recursion
;
2722 inputs
->render_surface_layer_list
->clear();
2724 ProcessCalcDrawPropsInputs(*inputs
, &globals
, &data_for_recursion
);
2725 UpdateMetaInformationSequenceNumber(inputs
->root_layer
);
2726 PreCalculateMetaInformationRecursiveData recursive_data
;
2727 PreCalculateMetaInformationInternal(inputs
->root_layer
, &recursive_data
);
2729 const bool should_measure_property_tree_performance
=
2730 inputs
->verify_property_trees
&&
2731 (property_tree_option
== BUILD_PROPERTY_TREES_IF_NEEDED
);
2733 if (inputs
->verify_property_trees
) {
2734 LayerImplList update_layer_list
;
2736 switch (property_tree_option
) {
2737 case BUILD_PROPERTY_TREES_IF_NEEDED
: {
2738 // The translation from layer to property trees is an intermediate
2739 // state. We will eventually get these data passed directly to the
2741 if (should_measure_property_tree_performance
) {
2743 TRACE_DISABLED_BY_DEFAULT("cc.debug.cdp-perf"),
2744 "LayerTreeHostCommon::ComputeVisibleRectsWithPropertyTrees");
2747 BuildPropertyTreesAndComputeVisibleRects(
2748 inputs
->root_layer
, inputs
->page_scale_layer
,
2749 inputs
->inner_viewport_scroll_layer
,
2750 inputs
->outer_viewport_scroll_layer
, inputs
->page_scale_factor
,
2751 inputs
->device_scale_factor
,
2752 gfx::Rect(inputs
->device_viewport_size
), inputs
->device_transform
,
2753 inputs
->property_trees
, &update_layer_list
);
2755 if (should_measure_property_tree_performance
) {
2757 TRACE_DISABLED_BY_DEFAULT("cc.debug.cdp-perf"),
2758 "LayerTreeHostCommon::ComputeVisibleRectsWithPropertyTrees");
2763 case DONT_BUILD_PROPERTY_TREES
: {
2765 TRACE_DISABLED_BY_DEFAULT("cc.debug.cdp-perf"),
2766 "LayerTreeHostCommon::ComputeJustVisibleRectsWithPropertyTrees");
2767 ComputeVisibleRectsUsingPropertyTrees(
2768 inputs
->root_layer
, inputs
->property_trees
, &update_layer_list
);
2774 if (should_measure_property_tree_performance
) {
2775 TRACE_EVENT_BEGIN0(TRACE_DISABLED_BY_DEFAULT("cc.debug.cdp-perf"),
2776 "LayerTreeHostCommon::CalculateDrawProperties");
2779 std::vector
<AccumulatedSurfaceState
> accumulated_surface_state
;
2780 CalculateRenderTarget(inputs
);
2781 CalculateDrawPropertiesInternal(inputs
->root_layer
, globals
,
2783 &accumulated_surface_state
);
2784 CalculateRenderSurfaceLayerList(inputs
);
2786 if (should_measure_property_tree_performance
) {
2787 TRACE_EVENT_END0(TRACE_DISABLED_BY_DEFAULT("cc.debug.cdp-perf"),
2788 "LayerTreeHostCommon::CalculateDrawProperties");
2791 if (inputs
->verify_property_trees
)
2792 VerifyPropertyTreeValues(inputs
);
2794 // A root layer render_surface should always exist after
2795 // CalculateDrawProperties.
2796 DCHECK(inputs
->root_layer
->render_surface());
2799 void LayerTreeHostCommon::CalculateDrawProperties(
2800 CalcDrawPropsMainInputs
* inputs
) {
2801 LayerList update_layer_list
;
2802 bool can_render_to_separate_surface
= true;
2803 UpdateRenderSurfaces(inputs
->root_layer
, can_render_to_separate_surface
,
2804 gfx::Transform(), false);
2805 PropertyTrees
* property_trees
=
2806 inputs
->root_layer
->layer_tree_host()->property_trees();
2807 BuildPropertyTreesAndComputeVisibleRects(
2808 inputs
->root_layer
, inputs
->page_scale_layer
,
2809 inputs
->inner_viewport_scroll_layer
, inputs
->outer_viewport_scroll_layer
,
2810 inputs
->page_scale_factor
, inputs
->device_scale_factor
,
2811 gfx::Rect(inputs
->device_viewport_size
), inputs
->device_transform
,
2812 property_trees
, &update_layer_list
);
2815 void LayerTreeHostCommon::CalculateDrawProperties(
2816 CalcDrawPropsImplInputs
* inputs
) {
2817 CalculateDrawPropertiesAndVerify(inputs
, DONT_BUILD_PROPERTY_TREES
);
2820 void LayerTreeHostCommon::CalculateDrawProperties(
2821 CalcDrawPropsImplInputsForTesting
* inputs
) {
2822 CalculateDrawPropertiesAndVerify(inputs
, BUILD_PROPERTY_TREES_IF_NEEDED
);
2825 PropertyTrees
* GetPropertyTrees(Layer
* layer
) {
2826 return layer
->layer_tree_host()->property_trees();
2829 PropertyTrees
* GetPropertyTrees(LayerImpl
* layer
) {
2830 return layer
->layer_tree_impl()->property_trees();