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.h"
16 #include "cc/layers/render_surface_impl.h"
17 #include "cc/trees/draw_property_utils.h"
18 #include "cc/trees/layer_tree_host.h"
19 #include "cc/trees/layer_tree_impl.h"
20 #include "ui/gfx/geometry/rect_conversions.h"
21 #include "ui/gfx/geometry/vector2d_conversions.h"
22 #include "ui/gfx/transform.h"
23 #include "ui/gfx/transform_util.h"
27 ScrollAndScaleSet::ScrollAndScaleSet()
28 : page_scale_delta(1.f
), top_controls_delta(0.f
) {
31 ScrollAndScaleSet::~ScrollAndScaleSet() {}
33 template <typename LayerType
>
34 static gfx::Vector2dF
GetEffectiveScrollDelta(LayerType
* layer
) {
35 // Layer's scroll offset can have an integer part and fractional part.
36 // Due to Blink's limitation, it only counter-scrolls the position-fixed
37 // layer using the integer part of Layer's scroll offset.
38 // CC scrolls the layer using the full scroll offset, so we have to
39 // add the ScrollCompensationAdjustment (fractional part of the scroll
40 // offset) to the effective scroll delta which is used to counter-scroll
41 // the position-fixed layer.
42 gfx::Vector2dF scroll_delta
=
43 layer
->ScrollDelta() + layer
->ScrollCompensationAdjustment();
44 // The scroll parent's scroll delta is the amount we've scrolled on the
45 // compositor thread since the commit for this layer tree's source frame.
46 // we last reported to the main thread. I.e., it's the discrepancy between
47 // a scroll parent's scroll delta and offset, so we must add it here.
48 if (layer
->scroll_parent())
49 scroll_delta
+= layer
->scroll_parent()->ScrollDelta() +
50 layer
->ScrollCompensationAdjustment();
54 template <typename LayerType
>
55 static gfx::ScrollOffset
GetEffectiveCurrentScrollOffset(LayerType
* layer
) {
56 gfx::ScrollOffset offset
= layer
->CurrentScrollOffset();
57 // The scroll parent's total scroll offset (scroll offset + scroll delta)
58 // can't be used because its scroll offset has already been applied to the
59 // scroll children's positions by the main thread layer positioning code.
60 if (layer
->scroll_parent())
61 offset
+= gfx::ScrollOffset(layer
->scroll_parent()->ScrollDelta());
65 inline gfx::Rect
CalculateVisibleRectWithCachedLayerRect(
66 const gfx::Rect
& target_surface_rect
,
67 const gfx::Rect
& layer_bound_rect
,
68 const gfx::Rect
& layer_rect_in_target_space
,
69 const gfx::Transform
& transform
) {
70 if (layer_rect_in_target_space
.IsEmpty())
73 // Is this layer fully contained within the target surface?
74 if (target_surface_rect
.Contains(layer_rect_in_target_space
))
75 return layer_bound_rect
;
77 // If the layer doesn't fill up the entire surface, then find the part of
78 // the surface rect where the layer could be visible. This avoids trying to
79 // project surface rect points that are behind the projection point.
80 gfx::Rect minimal_surface_rect
= target_surface_rect
;
81 minimal_surface_rect
.Intersect(layer_rect_in_target_space
);
83 if (minimal_surface_rect
.IsEmpty())
86 // Project the corners of the target surface rect into the layer space.
87 // This bounding rectangle may be larger than it needs to be (being
88 // axis-aligned), but is a reasonable filter on the space to consider.
89 // Non-invertible transforms will create an empty rect here.
91 gfx::Transform
surface_to_layer(gfx::Transform::kSkipInitialization
);
92 if (!transform
.GetInverse(&surface_to_layer
)) {
93 // Because we cannot use the surface bounds to determine what portion of
94 // the layer is visible, we must conservatively assume the full layer is
96 return layer_bound_rect
;
99 gfx::Rect layer_rect
= MathUtil::ProjectEnclosingClippedRect(
100 surface_to_layer
, minimal_surface_rect
);
101 layer_rect
.Intersect(layer_bound_rect
);
105 gfx::Rect
LayerTreeHostCommon::CalculateVisibleRect(
106 const gfx::Rect
& target_surface_rect
,
107 const gfx::Rect
& layer_bound_rect
,
108 const gfx::Transform
& transform
) {
109 gfx::Rect layer_in_surface_space
=
110 MathUtil::MapEnclosingClippedRect(transform
, layer_bound_rect
);
111 return CalculateVisibleRectWithCachedLayerRect(
112 target_surface_rect
, layer_bound_rect
, layer_in_surface_space
, transform
);
115 template <typename LayerType
>
116 static LayerType
* NextTargetSurface(LayerType
* layer
) {
117 return layer
->parent() ? layer
->parent()->render_target() : 0;
120 // Given two layers, this function finds their respective render targets and,
121 // computes a change of basis translation. It does this by accumulating the
122 // translation components of the draw transforms of each target between the
123 // ancestor and descendant. These transforms must be 2D translations, and this
124 // requirement is enforced at every step.
125 template <typename LayerType
>
126 static gfx::Vector2dF
ComputeChangeOfBasisTranslation(
127 const LayerType
& ancestor_layer
,
128 const LayerType
& descendant_layer
) {
129 DCHECK(descendant_layer
.HasAncestor(&ancestor_layer
));
130 const LayerType
* descendant_target
= descendant_layer
.render_target();
131 DCHECK(descendant_target
);
132 const LayerType
* ancestor_target
= ancestor_layer
.render_target();
133 DCHECK(ancestor_target
);
135 gfx::Vector2dF translation
;
136 for (const LayerType
* target
= descendant_target
; target
!= ancestor_target
;
137 target
= NextTargetSurface(target
)) {
138 const gfx::Transform
& trans
= target
->render_surface()->draw_transform();
139 // Ensure that this translation is truly 2d.
140 DCHECK(trans
.IsIdentityOrTranslation());
141 DCHECK_EQ(0.f
, trans
.matrix().get(2, 3));
142 translation
+= trans
.To2dTranslation();
148 enum TranslateRectDirection
{
149 TRANSLATE_RECT_DIRECTION_TO_ANCESTOR
,
150 TRANSLATE_RECT_DIRECTION_TO_DESCENDANT
153 template <typename LayerType
>
154 static gfx::Rect
TranslateRectToTargetSpace(const LayerType
& ancestor_layer
,
155 const LayerType
& descendant_layer
,
156 const gfx::Rect
& rect
,
157 TranslateRectDirection direction
) {
158 gfx::Vector2dF translation
= ComputeChangeOfBasisTranslation
<LayerType
>(
159 ancestor_layer
, descendant_layer
);
160 if (direction
== TRANSLATE_RECT_DIRECTION_TO_DESCENDANT
)
161 translation
.Scale(-1.f
);
162 return gfx::ToEnclosingRect(
163 gfx::RectF(rect
.origin() + translation
, rect
.size()));
166 // Attempts to update the clip rects for the given layer. If the layer has a
167 // clip_parent, it may not inherit its immediate ancestor's clip.
168 template <typename LayerType
>
169 static void UpdateClipRectsForClipChild(
170 const LayerType
* layer
,
171 gfx::Rect
* clip_rect_in_parent_target_space
,
172 bool* subtree_should_be_clipped
) {
173 // If the layer has no clip_parent, or the ancestor is the same as its actual
174 // parent, then we don't need special clip rects. Bail now and leave the out
175 // parameters untouched.
176 const LayerType
* clip_parent
= layer
->scroll_parent();
179 clip_parent
= layer
->clip_parent();
181 if (!clip_parent
|| clip_parent
== layer
->parent())
184 // The root layer is never a clip child.
185 DCHECK(layer
->parent());
187 // Grab the cached values.
188 *clip_rect_in_parent_target_space
= clip_parent
->clip_rect();
189 *subtree_should_be_clipped
= clip_parent
->is_clipped();
191 // We may have to project the clip rect into our parent's target space. Note,
192 // it must be our parent's target space, not ours. For one, we haven't
193 // computed our transforms, so we couldn't put it in our space yet even if we
194 // wanted to. But more importantly, this matches the expectations of
195 // CalculateDrawPropertiesInternal. If we, say, create a render surface, these
196 // clip rects will want to be in its target space, not ours.
197 if (clip_parent
== layer
->clip_parent()) {
198 *clip_rect_in_parent_target_space
= TranslateRectToTargetSpace
<LayerType
>(
199 *clip_parent
, *layer
->parent(), *clip_rect_in_parent_target_space
,
200 TRANSLATE_RECT_DIRECTION_TO_DESCENDANT
);
202 // If we're being clipped by our scroll parent, we must translate through
203 // our common ancestor. This happens to be our parent, so it is sufficent to
204 // translate from our clip parent's space to the space of its ancestor (our
206 *clip_rect_in_parent_target_space
= TranslateRectToTargetSpace
<LayerType
>(
207 *layer
->parent(), *clip_parent
, *clip_rect_in_parent_target_space
,
208 TRANSLATE_RECT_DIRECTION_TO_ANCESTOR
);
212 // We collect an accumulated drawable content rect per render surface.
213 // Typically, a layer will contribute to only one surface, the surface
214 // associated with its render target. Clip children, however, may affect
215 // several surfaces since there may be several surfaces between the clip child
218 // NB: we accumulate the layer's *clipped* drawable content rect.
219 template <typename LayerType
>
220 struct AccumulatedSurfaceState
{
221 explicit AccumulatedSurfaceState(LayerType
* render_target
)
222 : render_target(render_target
) {}
224 // The accumulated drawable content rect for the surface associated with the
225 // given |render_target|.
226 gfx::Rect drawable_content_rect
;
228 // The target owning the surface. (We hang onto the target rather than the
229 // surface so that we can DCHECK that the surface's draw transform is simply
230 // a translation when |render_target| reports that it has no unclipped
232 LayerType
* render_target
;
235 template <typename LayerType
>
236 void UpdateAccumulatedSurfaceState(
238 const gfx::Rect
& drawable_content_rect
,
239 std::vector
<AccumulatedSurfaceState
<LayerType
>>*
240 accumulated_surface_state
) {
241 if (IsRootLayer(layer
))
244 // We will apply our drawable content rect to the accumulated rects for all
245 // surfaces between us and |render_target| (inclusive). This is either our
246 // clip parent's target if we are a clip child, or else simply our parent's
247 // target. We use our parent's target because we're either the owner of a
248 // render surface and we'll want to add our rect to our *surface's* target, or
249 // we're not and our target is the same as our parent's. In both cases, the
250 // parent's target gives us what we want.
251 LayerType
* render_target
= layer
->clip_parent()
252 ? layer
->clip_parent()->render_target()
253 : layer
->parent()->render_target();
255 // If the layer owns a surface, then the content rect is in the wrong space.
256 // Instead, we will use the surface's DrawableContentRect which is in target
257 // space as required.
258 gfx::Rect target_rect
= drawable_content_rect
;
259 if (layer
->render_surface()) {
261 gfx::ToEnclosedRect(layer
->render_surface()->DrawableContentRect());
264 if (render_target
->is_clipped()) {
265 gfx::Rect clip_rect
= render_target
->clip_rect();
266 // If the layer has a clip parent, the clip rect may be in the wrong space,
267 // so we'll need to transform it before it is applied.
268 if (layer
->clip_parent()) {
269 clip_rect
= TranslateRectToTargetSpace
<LayerType
>(
270 *layer
->clip_parent(), *layer
, clip_rect
,
271 TRANSLATE_RECT_DIRECTION_TO_DESCENDANT
);
273 target_rect
.Intersect(clip_rect
);
276 // We must have at least one entry in the vector for the root.
277 DCHECK_LT(0ul, accumulated_surface_state
->size());
279 typedef typename
std::vector
<AccumulatedSurfaceState
<LayerType
>>
280 AccumulatedSurfaceStateVector
;
281 typedef typename
AccumulatedSurfaceStateVector::reverse_iterator
282 AccumulatedSurfaceStateIterator
;
283 AccumulatedSurfaceStateIterator current_state
=
284 accumulated_surface_state
->rbegin();
286 // Add this rect to the accumulated content rect for all surfaces until we
287 // reach the target surface.
288 bool found_render_target
= false;
289 for (; current_state
!= accumulated_surface_state
->rend(); ++current_state
) {
290 current_state
->drawable_content_rect
.Union(target_rect
);
292 // If we've reached |render_target| our work is done and we can bail.
293 if (current_state
->render_target
== render_target
) {
294 found_render_target
= true;
298 // Transform rect from the current target's space to the next.
299 LayerType
* current_target
= current_state
->render_target
;
300 DCHECK(current_target
->render_surface());
301 const gfx::Transform
& current_draw_transform
=
302 current_target
->render_surface()->draw_transform();
304 // If we have unclipped descendants, the draw transform is a translation.
305 DCHECK(current_target
->num_unclipped_descendants() == 0 ||
306 current_draw_transform
.IsIdentityOrTranslation());
308 target_rect
= gfx::ToEnclosingRect(
309 MathUtil::MapClippedRect(current_draw_transform
, target_rect
));
312 // It is an error to not reach |render_target|. If this happens, it means that
313 // either the clip parent is not an ancestor of the clip child or the surface
314 // state vector is empty, both of which should be impossible.
315 DCHECK(found_render_target
);
318 template <typename LayerType
> static inline bool IsRootLayer(LayerType
* layer
) {
319 return !layer
->parent();
322 template <typename LayerType
>
323 static inline bool LayerIsInExisting3DRenderingContext(LayerType
* layer
) {
324 return layer
->Is3dSorted() && layer
->parent() &&
325 layer
->parent()->Is3dSorted() &&
326 (layer
->parent()->sorting_context_id() == layer
->sorting_context_id());
329 template <typename LayerType
>
330 static bool IsRootLayerOfNewRenderingContext(LayerType
* layer
) {
332 return !layer
->parent()->Is3dSorted() && layer
->Is3dSorted();
334 return layer
->Is3dSorted();
337 template <typename LayerType
>
338 static bool IsLayerBackFaceVisible(LayerType
* layer
) {
339 // The current W3C spec on CSS transforms says that backface visibility should
340 // be determined differently depending on whether the layer is in a "3d
341 // rendering context" or not. For Chromium code, we can determine whether we
342 // are in a 3d rendering context by checking if the parent preserves 3d.
344 if (LayerIsInExisting3DRenderingContext(layer
))
345 return layer
->draw_transform().IsBackFaceVisible();
347 // In this case, either the layer establishes a new 3d rendering context, or
348 // is not in a 3d rendering context at all.
349 return layer
->transform().IsBackFaceVisible();
352 template <typename LayerType
>
353 static bool IsSurfaceBackFaceVisible(LayerType
* layer
,
354 const gfx::Transform
& draw_transform
) {
355 if (LayerIsInExisting3DRenderingContext(layer
))
356 return draw_transform
.IsBackFaceVisible();
358 if (IsRootLayerOfNewRenderingContext(layer
))
359 return layer
->transform().IsBackFaceVisible();
361 // If the render_surface is not part of a new or existing rendering context,
362 // then the layers that contribute to this surface will decide back-face
363 // visibility for themselves.
367 template <typename LayerType
>
368 static inline bool LayerClipsSubtree(LayerType
* layer
) {
369 return layer
->masks_to_bounds() || layer
->mask_layer();
372 template <typename LayerType
>
373 static gfx::Rect
CalculateVisibleContentRect(
375 const gfx::Rect
& clip_rect_of_target_surface_in_target_space
,
376 const gfx::Rect
& layer_rect_in_target_space
) {
377 DCHECK(layer
->render_target());
379 // Nothing is visible if the layer bounds are empty.
380 if (!layer
->DrawsContent() || layer
->content_bounds().IsEmpty() ||
381 layer
->drawable_content_rect().IsEmpty())
384 // Compute visible bounds in target surface space.
385 gfx::Rect visible_rect_in_target_surface_space
=
386 layer
->drawable_content_rect();
388 if (layer
->render_target()->render_surface()->is_clipped()) {
389 // The |layer| L has a target T which owns a surface Ts. The surface Ts
392 // In this case the target surface Ts does clip the layer L that contributes
393 // to it. So, we have to convert the clip rect of Ts from the target space
394 // of Ts (that is the space of TsT), to the current render target's space
395 // (that is the space of T). This conversion is done outside this function
396 // so that it can be cached instead of computing it redundantly for every
398 visible_rect_in_target_surface_space
.Intersect(
399 clip_rect_of_target_surface_in_target_space
);
402 if (visible_rect_in_target_surface_space
.IsEmpty())
405 return CalculateVisibleRectWithCachedLayerRect(
406 visible_rect_in_target_surface_space
,
407 gfx::Rect(layer
->content_bounds()),
408 layer_rect_in_target_space
,
409 layer
->draw_transform());
412 static inline bool TransformToParentIsKnown(LayerImpl
* layer
) { return true; }
414 static inline bool TransformToParentIsKnown(Layer
* layer
) {
415 return !layer
->TransformIsAnimating();
418 static inline bool TransformToScreenIsKnown(LayerImpl
* layer
) { return true; }
420 static inline bool TransformToScreenIsKnown(Layer
* layer
) {
421 return !layer
->screen_space_transform_is_animating();
424 template <typename LayerType
>
425 static bool LayerShouldBeSkipped(LayerType
* layer
, bool layer_is_drawn
) {
426 // Layers can be skipped if any of these conditions are met.
427 // - is not drawn due to it or one of its ancestors being hidden (or having
428 // no copy requests).
429 // - does not draw content.
431 // - has empty bounds
432 // - the layer is not double-sided, but its back face is visible.
434 // Some additional conditions need to be computed at a later point after the
435 // recursion is finished.
436 // - the intersection of render_surface content and layer clip_rect is empty
437 // - the visible_content_rect is empty
439 // Note, if the layer should not have been drawn due to being fully
440 // transparent, we would have skipped the entire subtree and never made it
441 // into this function, so it is safe to omit this check here.
446 if (!layer
->DrawsContent() || layer
->bounds().IsEmpty())
449 LayerType
* backface_test_layer
= layer
;
450 if (layer
->use_parent_backface_visibility()) {
451 DCHECK(layer
->parent());
452 DCHECK(!layer
->parent()->use_parent_backface_visibility());
453 backface_test_layer
= layer
->parent();
456 // The layer should not be drawn if (1) it is not double-sided and (2) the
457 // back of the layer is known to be facing the screen.
458 if (!backface_test_layer
->double_sided() &&
459 TransformToScreenIsKnown(backface_test_layer
) &&
460 IsLayerBackFaceVisible(backface_test_layer
))
466 template <typename LayerType
>
467 static bool HasInvertibleOrAnimatedTransform(LayerType
* layer
) {
468 return layer
->transform_is_invertible() || layer
->TransformIsAnimating();
471 static inline bool SubtreeShouldBeSkipped(LayerImpl
* layer
,
472 bool layer_is_drawn
) {
473 // If the layer transform is not invertible, it should not be drawn.
474 // TODO(ajuma): Correctly process subtrees with singular transform for the
475 // case where we may animate to a non-singular transform and wish to
477 if (!HasInvertibleOrAnimatedTransform(layer
))
480 // When we need to do a readback/copy of a layer's output, we can not skip
481 // it or any of its ancestors.
482 if (layer
->draw_properties().layer_or_descendant_has_copy_request
)
485 // We cannot skip the the subtree if a descendant has a wheel or touch handler
486 // or the hit testing code will break (it requires fresh transforms, etc).
487 if (layer
->draw_properties().layer_or_descendant_has_input_handler
)
490 // If the layer is not drawn, then skip it and its subtree.
494 // If layer is on the pending tree and opacity is being animated then
495 // this subtree can't be skipped as we need to create, prioritize and
496 // include tiles for this layer when deciding if tree can be activated.
497 if (layer
->layer_tree_impl()->IsPendingTree() && layer
->OpacityIsAnimating())
500 // The opacity of a layer always applies to its children (either implicitly
501 // via a render surface or explicitly if the parent preserves 3D), so the
502 // entire subtree can be skipped if this layer is fully transparent.
503 return !layer
->opacity();
506 static inline bool SubtreeShouldBeSkipped(Layer
* layer
, bool layer_is_drawn
) {
507 // If the layer transform is not invertible, it should not be drawn.
508 if (!layer
->transform_is_invertible() && !layer
->TransformIsAnimating())
511 // When we need to do a readback/copy of a layer's output, we can not skip
512 // it or any of its ancestors.
513 if (layer
->draw_properties().layer_or_descendant_has_copy_request
)
516 // We cannot skip the the subtree if a descendant has a wheel or touch handler
517 // or the hit testing code will break (it requires fresh transforms, etc).
518 if (layer
->draw_properties().layer_or_descendant_has_input_handler
)
521 // If the layer is not drawn, then skip it and its subtree.
525 // If the opacity is being animated then the opacity on the main thread is
526 // unreliable (since the impl thread may be using a different opacity), so it
527 // should not be trusted.
528 // In particular, it should not cause the subtree to be skipped.
529 // Similarly, for layers that might animate opacity using an impl-only
530 // animation, their subtree should also not be skipped.
531 return !layer
->opacity() && !layer
->OpacityIsAnimating() &&
532 !layer
->OpacityCanAnimateOnImplThread();
535 static inline void SavePaintPropertiesLayer(LayerImpl
* layer
) {}
537 static inline void SavePaintPropertiesLayer(Layer
* layer
) {
538 layer
->SavePaintProperties();
540 if (layer
->mask_layer())
541 layer
->mask_layer()->SavePaintProperties();
542 if (layer
->replica_layer() && layer
->replica_layer()->mask_layer())
543 layer
->replica_layer()->mask_layer()->SavePaintProperties();
546 static bool SubtreeShouldRenderToSeparateSurface(
548 bool axis_aligned_with_respect_to_parent
) {
550 // A layer and its descendants should render onto a new RenderSurfaceImpl if
551 // any of these rules hold:
554 // The root layer owns a render surface, but it never acts as a contributing
555 // surface to another render target. Compositor features that are applied via
556 // a contributing surface can not be applied to the root layer. In order to
557 // use these effects, another child of the root would need to be introduced
558 // in order to act as a contributing surface to the root layer's surface.
559 bool is_root
= IsRootLayer(layer
);
561 // If the layer uses a mask.
562 if (layer
->mask_layer()) {
567 // If the layer has a reflection.
568 if (layer
->replica_layer()) {
573 // If the layer uses a CSS filter.
574 if (!layer
->filters().IsEmpty() || !layer
->background_filters().IsEmpty()) {
579 // If the layer will use a CSS filter. In this case, the animation
580 // will start and add a filter to this layer, so it needs a surface.
581 if (layer
->FilterIsAnimating()) {
586 int num_descendants_that_draw_content
=
587 layer
->NumDescendantsThatDrawContent();
589 // If the layer flattens its subtree, but it is treated as a 3D object by its
590 // parent (i.e. parent participates in a 3D rendering context).
591 if (LayerIsInExisting3DRenderingContext(layer
) &&
592 layer
->should_flatten_transform() &&
593 num_descendants_that_draw_content
> 0) {
594 TRACE_EVENT_INSTANT0(
596 "LayerTreeHostCommon::SubtreeShouldRenderToSeparateSurface flattening",
597 TRACE_EVENT_SCOPE_THREAD
);
602 // If the layer has blending.
603 // TODO(rosca): this is temporary, until blending is implemented for other
604 // types of quads than RenderPassDrawQuad. Layers having descendants that draw
605 // content will still create a separate rendering surface.
606 if (!layer
->uses_default_blend_mode()) {
607 TRACE_EVENT_INSTANT0(
609 "LayerTreeHostCommon::SubtreeShouldRenderToSeparateSurface blending",
610 TRACE_EVENT_SCOPE_THREAD
);
615 // If the layer clips its descendants but it is not axis-aligned with respect
617 bool layer_clips_external_content
=
618 LayerClipsSubtree(layer
) || layer
->HasDelegatedContent();
619 if (layer_clips_external_content
&& !axis_aligned_with_respect_to_parent
&&
620 num_descendants_that_draw_content
> 0) {
621 TRACE_EVENT_INSTANT0(
623 "LayerTreeHostCommon::SubtreeShouldRenderToSeparateSurface clipping",
624 TRACE_EVENT_SCOPE_THREAD
);
629 // If the layer has some translucency and does not have a preserves-3d
630 // transform style. This condition only needs a render surface if two or more
631 // layers in the subtree overlap. But checking layer overlaps is unnecessarily
632 // costly so instead we conservatively create a surface whenever at least two
633 // layers draw content for this subtree.
634 bool at_least_two_layers_in_subtree_draw_content
=
635 num_descendants_that_draw_content
> 0 &&
636 (layer
->DrawsContent() || num_descendants_that_draw_content
> 1);
638 if (layer
->opacity() != 1.f
&& layer
->should_flatten_transform() &&
639 at_least_two_layers_in_subtree_draw_content
) {
640 TRACE_EVENT_INSTANT0(
642 "LayerTreeHostCommon::SubtreeShouldRenderToSeparateSurface opacity",
643 TRACE_EVENT_SCOPE_THREAD
);
648 // The root layer should always have a render_surface.
653 // These are allowed on the root surface, as they don't require the surface to
654 // be used as a contributing surface in order to apply correctly.
657 // If the layer has isolation.
658 // TODO(rosca): to be optimized - create separate rendering surface only when
659 // the blending descendants might have access to the content behind this layer
660 // (layer has transparent background or descendants overflow).
661 // https://code.google.com/p/chromium/issues/detail?id=301738
662 if (layer
->is_root_for_isolated_group()) {
663 TRACE_EVENT_INSTANT0(
665 "LayerTreeHostCommon::SubtreeShouldRenderToSeparateSurface isolation",
666 TRACE_EVENT_SCOPE_THREAD
);
671 if (layer
->force_render_surface())
674 // If we'll make a copy of the layer's contents.
675 if (layer
->HasCopyRequest())
681 // This function returns a translation matrix that can be applied on a vector
682 // that's in the layer's target surface coordinate, while the position offset is
683 // specified in some ancestor layer's coordinate.
684 template <typename LayerType
>
685 gfx::Transform
ComputeSizeDeltaCompensation(
687 LayerType
* container
,
688 const gfx::Vector2dF
& position_offset
) {
689 gfx::Transform result_transform
;
691 // To apply a translate in the container's layer space,
692 // the following steps need to be done:
693 // Step 1a. transform from target surface space to the container's target
695 // Step 1b. transform from container's target surface space to the
696 // container's layer space
697 // Step 2. apply the compensation
698 // Step 3. transform back to target surface space
700 gfx::Transform target_surface_space_to_container_layer_space
;
702 LayerType
* container_target_surface
= container
->render_target();
703 for (LayerType
* current_target_surface
= NextTargetSurface(layer
);
704 current_target_surface
&&
705 current_target_surface
!= container_target_surface
;
706 current_target_surface
= NextTargetSurface(current_target_surface
)) {
707 // Note: Concat is used here to convert the result coordinate space from
708 // current render surface to the next render surface.
709 target_surface_space_to_container_layer_space
.ConcatTransform(
710 current_target_surface
->render_surface()->draw_transform());
713 gfx::Transform container_layer_space_to_container_target_surface_space
=
714 container
->draw_transform();
715 container_layer_space_to_container_target_surface_space
.Scale(
716 container
->contents_scale_x(), container
->contents_scale_y());
718 gfx::Transform container_target_surface_space_to_container_layer_space
;
719 if (container_layer_space_to_container_target_surface_space
.GetInverse(
720 &container_target_surface_space_to_container_layer_space
)) {
721 // Note: Again, Concat is used to conver the result coordinate space from
722 // the container render surface to the container layer.
723 target_surface_space_to_container_layer_space
.ConcatTransform(
724 container_target_surface_space_to_container_layer_space
);
728 gfx::Transform container_layer_space_to_target_surface_space
;
729 if (target_surface_space_to_container_layer_space
.GetInverse(
730 &container_layer_space_to_target_surface_space
)) {
731 result_transform
.PreconcatTransform(
732 container_layer_space_to_target_surface_space
);
734 // TODO(shawnsingh): A non-invertible matrix could still make meaningful
735 // projection. For example ScaleZ(0) is non-invertible but the layer is
737 return gfx::Transform();
741 result_transform
.Translate(position_offset
.x(), position_offset
.y());
744 result_transform
.PreconcatTransform(
745 target_surface_space_to_container_layer_space
);
747 return result_transform
;
750 template <typename LayerType
>
751 void ApplyPositionAdjustment(
753 LayerType
* container
,
754 const gfx::Transform
& scroll_compensation
,
755 gfx::Transform
* combined_transform
) {
756 if (!layer
->position_constraint().is_fixed_position())
759 // Special case: this layer is a composited fixed-position layer; we need to
760 // explicitly compensate for all ancestors' nonzero scroll_deltas to keep
761 // this layer fixed correctly.
762 // Note carefully: this is Concat, not Preconcat
763 // (current_scroll_compensation * combined_transform).
764 combined_transform
->ConcatTransform(scroll_compensation
);
766 // For right-edge or bottom-edge anchored fixed position layers,
767 // the layer should relocate itself if the container changes its size.
768 bool fixed_to_right_edge
=
769 layer
->position_constraint().is_fixed_to_right_edge();
770 bool fixed_to_bottom_edge
=
771 layer
->position_constraint().is_fixed_to_bottom_edge();
772 gfx::Vector2dF position_offset
= container
->FixedContainerSizeDelta();
773 position_offset
.set_x(fixed_to_right_edge
? position_offset
.x() : 0);
774 position_offset
.set_y(fixed_to_bottom_edge
? position_offset
.y() : 0);
775 if (position_offset
.IsZero())
778 // Note: Again, this is Concat. The compensation matrix will be applied on
779 // the vector in target surface space.
780 combined_transform
->ConcatTransform(
781 ComputeSizeDeltaCompensation(layer
, container
, position_offset
));
784 template <typename LayerType
>
785 gfx::Transform
ComputeScrollCompensationForThisLayer(
786 LayerType
* scrolling_layer
,
787 const gfx::Transform
& parent_matrix
,
788 const gfx::Vector2dF
& scroll_delta
) {
789 // For every layer that has non-zero scroll_delta, we have to compute a
790 // transform that can undo the scroll_delta translation. In particular, we
791 // want this matrix to premultiply a fixed-position layer's parent_matrix, so
792 // we design this transform in three steps as follows. The steps described
793 // here apply from right-to-left, so Step 1 would be the right-most matrix:
795 // Step 1. transform from target surface space to the exact space where
796 // scroll_delta is actually applied.
797 // -- this is inverse of parent_matrix
798 // Step 2. undo the scroll_delta
799 // -- this is just a translation by scroll_delta.
800 // Step 3. transform back to target surface space.
801 // -- this transform is the parent_matrix
803 // These steps create a matrix that both start and end in target surface
804 // space. So this matrix can pre-multiply any fixed-position layer's
805 // draw_transform to undo the scroll_deltas -- as long as that fixed position
806 // layer is fixed onto the same render_target as this scrolling_layer.
809 gfx::Transform scroll_compensation_for_this_layer
= parent_matrix
; // Step 3
810 scroll_compensation_for_this_layer
.Translate(
812 scroll_delta
.y()); // Step 2
814 gfx::Transform
inverse_parent_matrix(gfx::Transform::kSkipInitialization
);
815 if (!parent_matrix
.GetInverse(&inverse_parent_matrix
)) {
816 // TODO(shawnsingh): Either we need to handle uninvertible transforms
817 // here, or DCHECK that the transform is invertible.
819 scroll_compensation_for_this_layer
.PreconcatTransform(
820 inverse_parent_matrix
); // Step 1
821 return scroll_compensation_for_this_layer
;
824 template <typename LayerType
>
825 gfx::Transform
ComputeScrollCompensationMatrixForChildren(
827 const gfx::Transform
& parent_matrix
,
828 const gfx::Transform
& current_scroll_compensation_matrix
,
829 const gfx::Vector2dF
& scroll_delta
) {
830 // "Total scroll compensation" is the transform needed to cancel out all
831 // scroll_delta translations that occurred since the nearest container layer,
832 // even if there are render_surfaces in-between.
834 // There are some edge cases to be aware of, that are not explicit in the
836 // - A layer that is both a fixed-position and container should not be its
837 // own container, instead, that means it is fixed to an ancestor, and is a
838 // container for any fixed-position descendants.
839 // - A layer that is a fixed-position container and has a render_surface
840 // should behave the same as a container without a render_surface, the
841 // render_surface is irrelevant in that case.
842 // - A layer that does not have an explicit container is simply fixed to the
843 // viewport. (i.e. the root render_surface.)
844 // - If the fixed-position layer has its own render_surface, then the
845 // render_surface is the one who gets fixed.
847 // This function needs to be called AFTER layers create their own
851 // Scroll compensation restarts from identity under two possible conditions:
852 // - the current layer is a container for fixed-position descendants
853 // - the current layer is fixed-position itself, so any fixed-position
854 // descendants are positioned with respect to this layer. Thus, any
855 // fixed position descendants only need to compensate for scrollDeltas
856 // that occur below this layer.
857 bool current_layer_resets_scroll_compensation_for_descendants
=
858 layer
->IsContainerForFixedPositionLayers() ||
859 layer
->position_constraint().is_fixed_position();
861 // Avoid the overheads (including stack allocation and matrix
862 // initialization/copy) if we know that the scroll compensation doesn't need
863 // to be reset or adjusted.
864 if (!current_layer_resets_scroll_compensation_for_descendants
&&
865 scroll_delta
.IsZero() && !layer
->render_surface())
866 return current_scroll_compensation_matrix
;
868 // Start as identity matrix.
869 gfx::Transform next_scroll_compensation_matrix
;
871 // If this layer does not reset scroll compensation, then it inherits the
872 // existing scroll compensations.
873 if (!current_layer_resets_scroll_compensation_for_descendants
)
874 next_scroll_compensation_matrix
= current_scroll_compensation_matrix
;
876 // If the current layer has a non-zero scroll_delta, then we should compute
877 // its local scroll compensation and accumulate it to the
878 // next_scroll_compensation_matrix.
879 if (!scroll_delta
.IsZero()) {
880 gfx::Transform scroll_compensation_for_this_layer
=
881 ComputeScrollCompensationForThisLayer(
882 layer
, parent_matrix
, scroll_delta
);
883 next_scroll_compensation_matrix
.PreconcatTransform(
884 scroll_compensation_for_this_layer
);
887 // If the layer created its own render_surface, we have to adjust
888 // next_scroll_compensation_matrix. The adjustment allows us to continue
889 // using the scroll compensation on the next surface.
890 // Step 1 (right-most in the math): transform from the new surface to the
891 // original ancestor surface
892 // Step 2: apply the scroll compensation
893 // Step 3: transform back to the new surface.
894 if (layer
->render_surface() &&
895 !next_scroll_compensation_matrix
.IsIdentity()) {
896 gfx::Transform
inverse_surface_draw_transform(
897 gfx::Transform::kSkipInitialization
);
898 if (!layer
->render_surface()->draw_transform().GetInverse(
899 &inverse_surface_draw_transform
)) {
900 // TODO(shawnsingh): Either we need to handle uninvertible transforms
901 // here, or DCHECK that the transform is invertible.
903 next_scroll_compensation_matrix
=
904 inverse_surface_draw_transform
* next_scroll_compensation_matrix
*
905 layer
->render_surface()->draw_transform();
908 return next_scroll_compensation_matrix
;
911 template <typename LayerType
>
912 static inline void UpdateLayerScaleDrawProperties(
914 float ideal_contents_scale
,
915 float maximum_animation_contents_scale
,
916 float starting_animation_contents_scale
,
917 float page_scale_factor
,
918 float device_scale_factor
) {
919 layer
->draw_properties().ideal_contents_scale
= ideal_contents_scale
;
920 layer
->draw_properties().maximum_animation_contents_scale
=
921 maximum_animation_contents_scale
;
922 layer
->draw_properties().starting_animation_contents_scale
=
923 starting_animation_contents_scale
;
924 layer
->draw_properties().page_scale_factor
= page_scale_factor
;
925 layer
->draw_properties().device_scale_factor
= device_scale_factor
;
928 static inline void CalculateContentsScale(LayerImpl
* layer
,
929 float contents_scale
) {
930 // LayerImpl has all of its content scales and bounds pushed from the Main
931 // thread during commit and just uses those values as-is.
934 static inline void CalculateContentsScale(Layer
* layer
, float contents_scale
) {
935 layer
->CalculateContentsScale(contents_scale
,
936 &layer
->draw_properties().contents_scale_x
,
937 &layer
->draw_properties().contents_scale_y
,
938 &layer
->draw_properties().content_bounds
);
940 Layer
* mask_layer
= layer
->mask_layer();
942 mask_layer
->CalculateContentsScale(
944 &mask_layer
->draw_properties().contents_scale_x
,
945 &mask_layer
->draw_properties().contents_scale_y
,
946 &mask_layer
->draw_properties().content_bounds
);
949 Layer
* replica_mask_layer
=
950 layer
->replica_layer() ? layer
->replica_layer()->mask_layer() : NULL
;
951 if (replica_mask_layer
) {
952 replica_mask_layer
->CalculateContentsScale(
954 &replica_mask_layer
->draw_properties().contents_scale_x
,
955 &replica_mask_layer
->draw_properties().contents_scale_y
,
956 &replica_mask_layer
->draw_properties().content_bounds
);
960 static inline void UpdateLayerContentsScale(
962 bool can_adjust_raster_scale
,
963 float ideal_contents_scale
,
964 float device_scale_factor
,
965 float page_scale_factor
,
966 bool animating_transform_to_screen
) {
967 CalculateContentsScale(layer
, ideal_contents_scale
);
970 static inline void UpdateLayerContentsScale(
972 bool can_adjust_raster_scale
,
973 float ideal_contents_scale
,
974 float device_scale_factor
,
975 float page_scale_factor
,
976 bool animating_transform_to_screen
) {
977 if (can_adjust_raster_scale
) {
978 float ideal_raster_scale
=
979 ideal_contents_scale
/ (device_scale_factor
* page_scale_factor
);
981 bool need_to_set_raster_scale
= layer
->raster_scale_is_unknown();
983 // If we've previously saved a raster_scale but the ideal changes, things
984 // are unpredictable and we should just use 1.
985 if (!need_to_set_raster_scale
&& layer
->raster_scale() != 1.f
&&
986 ideal_raster_scale
!= layer
->raster_scale()) {
987 ideal_raster_scale
= 1.f
;
988 need_to_set_raster_scale
= true;
991 if (need_to_set_raster_scale
) {
992 bool use_and_save_ideal_scale
=
993 ideal_raster_scale
>= 1.f
&& !animating_transform_to_screen
;
994 if (use_and_save_ideal_scale
)
995 layer
->set_raster_scale(ideal_raster_scale
);
999 float raster_scale
= 1.f
;
1000 if (!layer
->raster_scale_is_unknown())
1001 raster_scale
= layer
->raster_scale();
1003 gfx::Size old_content_bounds
= layer
->content_bounds();
1004 float old_contents_scale_x
= layer
->contents_scale_x();
1005 float old_contents_scale_y
= layer
->contents_scale_y();
1007 float contents_scale
= raster_scale
* device_scale_factor
* page_scale_factor
;
1008 CalculateContentsScale(layer
, contents_scale
);
1010 if (layer
->content_bounds() != old_content_bounds
||
1011 layer
->contents_scale_x() != old_contents_scale_x
||
1012 layer
->contents_scale_y() != old_contents_scale_y
)
1013 layer
->SetNeedsPushProperties();
1016 static inline void CalculateAnimationContentsScale(
1018 bool ancestor_is_animating_scale
,
1019 float ancestor_maximum_animation_contents_scale
,
1020 const gfx::Transform
& parent_transform
,
1021 const gfx::Transform
& combined_transform
,
1022 bool* combined_is_animating_scale
,
1023 float* combined_maximum_animation_contents_scale
,
1024 float* combined_starting_animation_contents_scale
) {
1025 *combined_is_animating_scale
= false;
1026 *combined_maximum_animation_contents_scale
= 0.f
;
1027 *combined_starting_animation_contents_scale
= 0.f
;
1030 static inline void CalculateAnimationContentsScale(
1032 bool ancestor_is_animating_scale
,
1033 float ancestor_maximum_animation_contents_scale
,
1034 const gfx::Transform
& ancestor_transform
,
1035 const gfx::Transform
& combined_transform
,
1036 bool* combined_is_animating_scale
,
1037 float* combined_maximum_animation_contents_scale
,
1038 float* combined_starting_animation_contents_scale
) {
1039 if (ancestor_is_animating_scale
&&
1040 ancestor_maximum_animation_contents_scale
== 0.f
) {
1041 // We've already failed to compute a maximum animated scale at an
1042 // ancestor, so we'll continue to fail.
1043 *combined_maximum_animation_contents_scale
= 0.f
;
1044 *combined_starting_animation_contents_scale
= 0.f
;
1045 *combined_is_animating_scale
= true;
1049 if (!combined_transform
.IsScaleOrTranslation()) {
1050 // Computing maximum animated scale in the presence of
1051 // non-scale/translation transforms isn't supported.
1052 *combined_maximum_animation_contents_scale
= 0.f
;
1053 *combined_starting_animation_contents_scale
= 0.f
;
1054 *combined_is_animating_scale
= true;
1058 // We currently only support computing maximum scale for combinations of
1059 // scales and translations. We treat all non-translations as potentially
1060 // affecting scale. Animations that include non-translation/scale components
1061 // will cause the computation of MaximumScale below to fail.
1062 bool layer_is_animating_scale
=
1063 !layer
->layer_animation_controller()->HasOnlyTranslationTransforms();
1065 if (!layer_is_animating_scale
&& !ancestor_is_animating_scale
) {
1066 *combined_maximum_animation_contents_scale
= 0.f
;
1067 *combined_starting_animation_contents_scale
= 0.f
;
1068 *combined_is_animating_scale
= false;
1072 // We don't attempt to accumulate animation scale from multiple nodes,
1073 // because of the risk of significant overestimation. For example, one node
1074 // may be increasing scale from 1 to 10 at the same time as a descendant is
1075 // decreasing scale from 10 to 1. Naively combining these scales would produce
1077 if (layer_is_animating_scale
&& ancestor_is_animating_scale
) {
1078 *combined_maximum_animation_contents_scale
= 0.f
;
1079 *combined_starting_animation_contents_scale
= 0.f
;
1080 *combined_is_animating_scale
= true;
1084 // At this point, we know either the layer or an ancestor, but not both,
1085 // is animating scale.
1086 *combined_is_animating_scale
= true;
1087 if (!layer_is_animating_scale
) {
1088 gfx::Vector2dF layer_transform_scales
=
1089 MathUtil::ComputeTransform2dScaleComponents(layer
->transform(), 0.f
);
1090 *combined_maximum_animation_contents_scale
=
1091 ancestor_maximum_animation_contents_scale
*
1092 std::max(layer_transform_scales
.x(), layer_transform_scales
.y());
1093 *combined_starting_animation_contents_scale
=
1094 *combined_maximum_animation_contents_scale
;
1098 float layer_maximum_animated_scale
= 0.f
;
1099 float layer_start_animated_scale
= 0.f
;
1100 if (!layer
->layer_animation_controller()->MaximumTargetScale(
1101 &layer_maximum_animated_scale
)) {
1102 *combined_maximum_animation_contents_scale
= 0.f
;
1105 if (!layer
->layer_animation_controller()->AnimationStartScale(
1106 &layer_start_animated_scale
)) {
1107 *combined_starting_animation_contents_scale
= 0.f
;
1111 gfx::Vector2dF ancestor_transform_scales
=
1112 MathUtil::ComputeTransform2dScaleComponents(ancestor_transform
, 0.f
);
1113 float max_scale_xy
=
1114 std::max(ancestor_transform_scales
.x(), ancestor_transform_scales
.y());
1115 *combined_maximum_animation_contents_scale
=
1116 layer_maximum_animated_scale
* max_scale_xy
;
1117 *combined_starting_animation_contents_scale
=
1118 layer_start_animated_scale
* max_scale_xy
;
1121 template <typename LayerTypePtr
>
1122 static inline void MarkLayerWithRenderSurfaceLayerListId(
1124 int current_render_surface_layer_list_id
) {
1125 layer
->draw_properties().last_drawn_render_surface_layer_list_id
=
1126 current_render_surface_layer_list_id
;
1127 layer
->draw_properties().layer_or_descendant_is_drawn
=
1128 !!current_render_surface_layer_list_id
;
1131 template <typename LayerTypePtr
>
1132 static inline void MarkMasksWithRenderSurfaceLayerListId(
1134 int current_render_surface_layer_list_id
) {
1135 if (layer
->mask_layer()) {
1136 MarkLayerWithRenderSurfaceLayerListId(layer
->mask_layer(),
1137 current_render_surface_layer_list_id
);
1139 if (layer
->replica_layer() && layer
->replica_layer()->mask_layer()) {
1140 MarkLayerWithRenderSurfaceLayerListId(layer
->replica_layer()->mask_layer(),
1141 current_render_surface_layer_list_id
);
1145 template <typename LayerListType
>
1146 static inline void MarkLayerListWithRenderSurfaceLayerListId(
1147 LayerListType
* layer_list
,
1148 int current_render_surface_layer_list_id
) {
1149 for (typename
LayerListType::iterator it
= layer_list
->begin();
1150 it
!= layer_list
->end();
1152 MarkLayerWithRenderSurfaceLayerListId(*it
,
1153 current_render_surface_layer_list_id
);
1154 MarkMasksWithRenderSurfaceLayerListId(*it
,
1155 current_render_surface_layer_list_id
);
1159 template <typename LayerType
>
1160 static inline void RemoveSurfaceForEarlyExit(
1161 LayerType
* layer_to_remove
,
1162 typename
LayerType::RenderSurfaceListType
* render_surface_layer_list
) {
1163 DCHECK(layer_to_remove
->render_surface());
1164 // Technically, we know that the layer we want to remove should be
1165 // at the back of the render_surface_layer_list. However, we have had
1166 // bugs before that added unnecessary layers here
1167 // (https://bugs.webkit.org/show_bug.cgi?id=74147), but that causes
1168 // things to crash. So here we proactively remove any additional
1169 // layers from the end of the list.
1170 while (render_surface_layer_list
->back() != layer_to_remove
) {
1171 MarkLayerListWithRenderSurfaceLayerListId(
1172 &render_surface_layer_list
->back()->render_surface()->layer_list(), 0);
1173 MarkLayerWithRenderSurfaceLayerListId(render_surface_layer_list
->back(), 0);
1175 render_surface_layer_list
->back()->ClearRenderSurfaceLayerList();
1176 render_surface_layer_list
->pop_back();
1178 DCHECK_EQ(render_surface_layer_list
->back(), layer_to_remove
);
1179 MarkLayerListWithRenderSurfaceLayerListId(
1180 &layer_to_remove
->render_surface()->layer_list(), 0);
1181 MarkLayerWithRenderSurfaceLayerListId(layer_to_remove
, 0);
1182 render_surface_layer_list
->pop_back();
1183 layer_to_remove
->ClearRenderSurfaceLayerList();
1186 struct PreCalculateMetaInformationRecursiveData
{
1187 int num_unclipped_descendants
;
1188 int num_layer_or_descendants_with_copy_request
;
1189 int num_layer_or_descendants_with_input_handler
;
1191 PreCalculateMetaInformationRecursiveData()
1192 : num_unclipped_descendants(0),
1193 num_layer_or_descendants_with_copy_request(0),
1194 num_layer_or_descendants_with_input_handler(0) {}
1196 void Merge(const PreCalculateMetaInformationRecursiveData
& data
) {
1197 num_layer_or_descendants_with_copy_request
+=
1198 data
.num_layer_or_descendants_with_copy_request
;
1199 num_layer_or_descendants_with_input_handler
+=
1200 data
.num_layer_or_descendants_with_input_handler
;
1201 num_unclipped_descendants
+= data
.num_unclipped_descendants
;
1205 static void ValidateRenderSurface(LayerImpl
* layer
) {
1206 // This test verifies that there are no cases where a LayerImpl needs
1207 // a render surface, but doesn't have one.
1208 if (layer
->render_surface())
1211 DCHECK(layer
->filters().IsEmpty()) << "layer: " << layer
->id();
1212 DCHECK(layer
->background_filters().IsEmpty()) << "layer: " << layer
->id();
1213 DCHECK(!layer
->mask_layer()) << "layer: " << layer
->id();
1214 DCHECK(!layer
->replica_layer()) << "layer: " << layer
->id();
1215 DCHECK(!IsRootLayer(layer
)) << "layer: " << layer
->id();
1216 DCHECK(!layer
->is_root_for_isolated_group()) << "layer: " << layer
->id();
1217 DCHECK(!layer
->HasCopyRequest()) << "layer: " << layer
->id();
1220 static void ValidateRenderSurface(Layer
* layer
) {
1223 static void ResetDrawProperties(Layer
* layer
) {
1224 layer
->draw_properties().sorted_for_recursion
= false;
1225 layer
->draw_properties().has_child_with_a_scroll_parent
= false;
1226 layer
->draw_properties().layer_or_descendant_is_drawn
= false;
1227 layer
->draw_properties().visited
= false;
1228 if (!HasInvertibleOrAnimatedTransform(layer
)) {
1229 // Layers with singular transforms should not be drawn, the whole subtree
1234 for (size_t i
= 0; i
< layer
->children().size(); ++i
) {
1235 Layer
* child_layer
= layer
->child_at(i
);
1236 if (child_layer
->scroll_parent())
1237 layer
->draw_properties().has_child_with_a_scroll_parent
= true;
1238 ResetDrawProperties(child_layer
);
1242 static void ResetDrawProperties(LayerImpl
* layer
) {
1245 static bool IsMetaInformationRecomputationNeeded(Layer
* layer
) {
1246 return layer
->layer_tree_host()->needs_meta_info_recomputation();
1249 // Recursively walks the layer tree(if needed) to compute any information
1250 // that is needed before doing the main recursion.
1251 static void PreCalculateMetaInformationInternal(
1253 PreCalculateMetaInformationRecursiveData
* recursive_data
) {
1254 ValidateRenderSurface(layer
);
1255 if (!HasInvertibleOrAnimatedTransform(layer
)) {
1256 // Layers with singular transforms should not be drawn, the whole subtree
1261 if (!IsMetaInformationRecomputationNeeded(layer
)) {
1262 DCHECK(IsRootLayer(layer
));
1266 if (layer
->clip_parent())
1267 recursive_data
->num_unclipped_descendants
++;
1269 for (size_t i
= 0; i
< layer
->children().size(); ++i
) {
1270 Layer
* child_layer
= layer
->child_at(i
);
1272 PreCalculateMetaInformationRecursiveData data_for_child
;
1273 PreCalculateMetaInformationInternal(child_layer
, &data_for_child
);
1275 recursive_data
->Merge(data_for_child
);
1278 if (layer
->clip_children()) {
1279 int num_clip_children
= layer
->clip_children()->size();
1280 DCHECK_GE(recursive_data
->num_unclipped_descendants
, num_clip_children
);
1281 recursive_data
->num_unclipped_descendants
-= num_clip_children
;
1284 if (layer
->HasCopyRequest())
1285 recursive_data
->num_layer_or_descendants_with_copy_request
++;
1287 if (!layer
->touch_event_handler_region().IsEmpty() ||
1288 layer
->have_wheel_event_handlers())
1289 recursive_data
->num_layer_or_descendants_with_input_handler
++;
1291 layer
->draw_properties().num_unclipped_descendants
=
1292 recursive_data
->num_unclipped_descendants
;
1293 layer
->draw_properties().layer_or_descendant_has_copy_request
=
1294 (recursive_data
->num_layer_or_descendants_with_copy_request
!= 0);
1295 layer
->draw_properties().layer_or_descendant_has_input_handler
=
1296 (recursive_data
->num_layer_or_descendants_with_input_handler
!= 0);
1297 layer
->set_num_layer_or_descandant_with_copy_request(
1298 recursive_data
->num_layer_or_descendants_with_copy_request
);
1299 layer
->set_num_layer_or_descandant_with_input_handler(
1300 recursive_data
->num_layer_or_descendants_with_input_handler
);
1302 if (IsRootLayer(layer
))
1303 layer
->layer_tree_host()->SetNeedsMetaInfoRecomputation(false);
1306 static void PreCalculateMetaInformationInternal(
1308 PreCalculateMetaInformationRecursiveData
* recursive_data
) {
1309 ValidateRenderSurface(layer
);
1311 layer
->draw_properties().sorted_for_recursion
= false;
1312 layer
->draw_properties().has_child_with_a_scroll_parent
= false;
1313 layer
->draw_properties().layer_or_descendant_is_drawn
= false;
1314 layer
->draw_properties().visited
= false;
1316 if (!HasInvertibleOrAnimatedTransform(layer
)) {
1317 // Layers with singular transforms should not be drawn, the whole subtree
1322 if (layer
->clip_parent())
1323 recursive_data
->num_unclipped_descendants
++;
1325 for (size_t i
= 0; i
< layer
->children().size(); ++i
) {
1326 LayerImpl
* child_layer
= layer
->child_at(i
);
1328 PreCalculateMetaInformationRecursiveData data_for_child
;
1329 PreCalculateMetaInformationInternal(child_layer
, &data_for_child
);
1331 if (child_layer
->scroll_parent())
1332 layer
->draw_properties().has_child_with_a_scroll_parent
= true;
1333 recursive_data
->Merge(data_for_child
);
1336 if (layer
->clip_children()) {
1337 int num_clip_children
= layer
->clip_children()->size();
1338 DCHECK_GE(recursive_data
->num_unclipped_descendants
, num_clip_children
);
1339 recursive_data
->num_unclipped_descendants
-= num_clip_children
;
1342 if (layer
->HasCopyRequest())
1343 recursive_data
->num_layer_or_descendants_with_copy_request
++;
1345 if (!layer
->touch_event_handler_region().IsEmpty() ||
1346 layer
->have_wheel_event_handlers())
1347 recursive_data
->num_layer_or_descendants_with_input_handler
++;
1349 layer
->draw_properties().num_unclipped_descendants
=
1350 recursive_data
->num_unclipped_descendants
;
1351 layer
->draw_properties().layer_or_descendant_has_copy_request
=
1352 (recursive_data
->num_layer_or_descendants_with_copy_request
!= 0);
1353 layer
->draw_properties().layer_or_descendant_has_input_handler
=
1354 (recursive_data
->num_layer_or_descendants_with_input_handler
!= 0);
1357 void LayerTreeHostCommon::PreCalculateMetaInformation(Layer
* root_layer
) {
1358 PreCalculateMetaInformationRecursiveData recursive_data
;
1359 PreCalculateMetaInformationInternal(root_layer
, &recursive_data
);
1362 void LayerTreeHostCommon::PreCalculateMetaInformationForTesting(
1363 LayerImpl
* root_layer
) {
1364 PreCalculateMetaInformationRecursiveData recursive_data
;
1365 PreCalculateMetaInformationInternal(root_layer
, &recursive_data
);
1368 template <typename LayerType
>
1369 struct SubtreeGlobals
{
1370 int max_texture_size
;
1371 float device_scale_factor
;
1372 float page_scale_factor
;
1373 const LayerType
* page_scale_application_layer
;
1374 gfx::Vector2dF elastic_overscroll
;
1375 const LayerType
* elastic_overscroll_application_layer
;
1376 bool can_adjust_raster_scales
;
1377 bool can_render_to_separate_surface
;
1378 bool layers_always_allowed_lcd_text
;
1381 template<typename LayerType
>
1382 struct DataForRecursion
{
1383 // The accumulated sequence of transforms a layer will use to determine its
1384 // own draw transform.
1385 gfx::Transform parent_matrix
;
1387 // The accumulated sequence of transforms a layer will use to determine its
1388 // own screen-space transform.
1389 gfx::Transform full_hierarchy_matrix
;
1391 // The transform that removes all scrolling that may have occurred between a
1392 // fixed-position layer and its container, so that the layer actually does
1394 gfx::Transform scroll_compensation_matrix
;
1396 // The ancestor that would be the container for any fixed-position / sticky
1398 LayerType
* fixed_container
;
1400 // This is the normal clip rect that is propagated from parent to child.
1401 gfx::Rect clip_rect_in_target_space
;
1403 // When the layer's children want to compute their visible content rect, they
1404 // want to know what their target surface's clip rect will be. BUT - they
1405 // want to know this clip rect represented in their own target space. This
1406 // requires inverse-projecting the surface's clip rect from the surface's
1407 // render target space down to the surface's own space. Instead of computing
1408 // this value redundantly for each child layer, it is computed only once
1409 // while dealing with the parent layer, and then this precomputed value is
1410 // passed down the recursion to the children that actually use it.
1411 gfx::Rect clip_rect_of_target_surface_in_target_space
;
1413 // The maximum amount by which this layer will be scaled during the lifetime
1414 // of currently running animations.
1415 float maximum_animation_contents_scale
;
1417 bool ancestor_is_animating_scale
;
1418 bool ancestor_clips_subtree
;
1419 typename
LayerType::RenderSurfaceType
*
1420 nearest_occlusion_immune_ancestor_surface
;
1421 bool in_subtree_of_page_scale_application_layer
;
1422 bool subtree_can_use_lcd_text
;
1423 bool subtree_is_visible_from_ancestor
;
1426 template <typename LayerType
>
1427 static LayerType
* GetChildContainingLayer(const LayerType
& parent
,
1429 for (LayerType
* ancestor
= layer
; ancestor
; ancestor
= ancestor
->parent()) {
1430 if (ancestor
->parent() == &parent
)
1437 template <typename LayerType
>
1438 static void AddScrollParentChain(std::vector
<LayerType
*>* out
,
1439 const LayerType
& parent
,
1441 // At a high level, this function walks up the chain of scroll parents
1442 // recursively, and once we reach the end of the chain, we add the child
1443 // of |parent| containing each scroll ancestor as we unwind. The result is
1444 // an ordering of parent's children that ensures that scroll parents are
1445 // visited before their descendants.
1446 // Take for example this layer tree:
1448 // + stacking_context
1449 // + scroll_child (1)
1450 // + scroll_parent_graphics_layer (*)
1451 // | + scroll_parent_scrolling_layer
1452 // | + scroll_parent_scrolling_content_layer (2)
1453 // + scroll_grandparent_graphics_layer (**)
1454 // + scroll_grandparent_scrolling_layer
1455 // + scroll_grandparent_scrolling_content_layer (3)
1457 // The scroll child is (1), its scroll parent is (2) and its scroll
1458 // grandparent is (3). Note, this doesn't mean that (2)'s scroll parent is
1459 // (3), it means that (*)'s scroll parent is (3). We don't want our list to
1460 // look like [ (3), (2), (1) ], even though that does have the ancestor chain
1461 // in the right order. Instead, we want [ (**), (*), (1) ]. That is, only want
1462 // (1)'s siblings in the list, but we want them to appear in such an order
1463 // that the scroll ancestors get visited in the correct order.
1465 // So our first task at this step of the recursion is to determine the layer
1466 // that we will potentionally add to the list. That is, the child of parent
1467 // containing |layer|.
1468 LayerType
* child
= GetChildContainingLayer(parent
, layer
);
1469 if (child
->draw_properties().sorted_for_recursion
)
1472 if (LayerType
* scroll_parent
= child
->scroll_parent())
1473 AddScrollParentChain(out
, parent
, scroll_parent
);
1475 out
->push_back(child
);
1476 child
->draw_properties().sorted_for_recursion
= true;
1479 template <typename LayerType
>
1480 static bool SortChildrenForRecursion(std::vector
<LayerType
*>* out
,
1481 const LayerType
& parent
) {
1482 out
->reserve(parent
.children().size());
1483 bool order_changed
= false;
1484 for (size_t i
= 0; i
< parent
.children().size(); ++i
) {
1485 LayerType
* current
=
1486 LayerTreeHostCommon::get_layer_as_raw_ptr(parent
.children(), i
);
1488 if (current
->draw_properties().sorted_for_recursion
) {
1489 order_changed
= true;
1493 AddScrollParentChain(out
, parent
, current
);
1496 DCHECK_EQ(parent
.children().size(), out
->size());
1497 return order_changed
;
1500 template <typename LayerType
>
1501 static void GetNewDescendantsStartIndexAndCount(LayerType
* layer
,
1502 size_t* start_index
,
1504 *start_index
= layer
->draw_properties().index_of_first_descendants_addition
;
1505 *count
= layer
->draw_properties().num_descendants_added
;
1508 template <typename LayerType
>
1509 static void GetNewRenderSurfacesStartIndexAndCount(LayerType
* layer
,
1510 size_t* start_index
,
1512 *start_index
= layer
->draw_properties()
1513 .index_of_first_render_surface_layer_list_addition
;
1514 *count
= layer
->draw_properties().num_render_surfaces_added
;
1517 // We need to extract a list from the the two flavors of RenderSurfaceListType
1518 // for use in the sorting function below.
1519 static LayerList
* GetLayerListForSorting(RenderSurfaceLayerList
* rsll
) {
1520 return &rsll
->AsLayerList();
1523 static LayerImplList
* GetLayerListForSorting(LayerImplList
* layer_list
) {
1527 static inline gfx::Vector2d
BoundsDelta(Layer
* layer
) {
1528 return gfx::Vector2d();
1531 static inline gfx::Vector2d
BoundsDelta(LayerImpl
* layer
) {
1532 return gfx::ToCeiledVector2d(layer
->bounds_delta());
1535 template <typename LayerType
, typename GetIndexAndCountType
>
1536 static void SortLayerListContributions(
1537 const LayerType
& parent
,
1538 typename
LayerType::LayerListType
* unsorted
,
1539 size_t start_index_for_all_contributions
,
1540 GetIndexAndCountType get_index_and_count
) {
1541 typename
LayerType::LayerListType buffer
;
1542 for (size_t i
= 0; i
< parent
.children().size(); ++i
) {
1544 LayerTreeHostCommon::get_layer_as_raw_ptr(parent
.children(), i
);
1546 size_t start_index
= 0;
1548 get_index_and_count(child
, &start_index
, &count
);
1549 for (size_t j
= start_index
; j
< start_index
+ count
; ++j
)
1550 buffer
.push_back(unsorted
->at(j
));
1553 DCHECK_EQ(buffer
.size(),
1554 unsorted
->size() - start_index_for_all_contributions
);
1556 for (size_t i
= 0; i
< buffer
.size(); ++i
)
1557 (*unsorted
)[i
+ start_index_for_all_contributions
] = buffer
[i
];
1560 // Recursively walks the layer tree starting at the given node and computes all
1561 // the necessary transformations, clip rects, render surfaces, etc.
1562 template <typename LayerType
>
1563 static void CalculateDrawPropertiesInternal(
1565 const SubtreeGlobals
<LayerType
>& globals
,
1566 const DataForRecursion
<LayerType
>& data_from_ancestor
,
1567 typename
LayerType::RenderSurfaceListType
* render_surface_layer_list
,
1568 typename
LayerType::LayerListType
* layer_list
,
1569 std::vector
<AccumulatedSurfaceState
<LayerType
>>* accumulated_surface_state
,
1570 int current_render_surface_layer_list_id
) {
1571 // This function computes the new matrix transformations recursively for this
1572 // layer and all its descendants. It also computes the appropriate render
1574 // Some important points to remember:
1576 // 0. Here, transforms are notated in Matrix x Vector order, and in words we
1577 // describe what the transform does from left to right.
1579 // 1. In our terminology, the "layer origin" refers to the top-left corner of
1580 // a layer, and the positive Y-axis points downwards. This interpretation is
1581 // valid because the orthographic projection applied at draw time flips the Y
1582 // axis appropriately.
1584 // 2. The anchor point, when given as a PointF object, is specified in "unit
1585 // layer space", where the bounds of the layer map to [0, 1]. However, as a
1586 // Transform object, the transform to the anchor point is specified in "layer
1587 // space", where the bounds of the layer map to [bounds.width(),
1588 // bounds.height()].
1590 // 3. Definition of various transforms used:
1591 // M[parent] is the parent matrix, with respect to the nearest render
1592 // surface, passed down recursively.
1594 // M[root] is the full hierarchy, with respect to the root, passed down
1597 // Tr[origin] is the translation matrix from the parent's origin to
1598 // this layer's origin.
1600 // Tr[origin2anchor] is the translation from the layer's origin to its
1603 // Tr[origin2center] is the translation from the layer's origin to its
1606 // M[layer] is the layer's matrix (applied at the anchor point)
1608 // S[layer2content] is the ratio of a layer's content_bounds() to its
1611 // Some composite transforms can help in understanding the sequence of
1613 // composite_layer_transform = Tr[origin2anchor] * M[layer] *
1614 // Tr[origin2anchor].inverse()
1616 // 4. When a layer (or render surface) is drawn, it is drawn into a "target
1617 // render surface". Therefore the draw transform does not necessarily
1618 // transform from screen space to local layer space. Instead, the draw
1619 // transform is the transform between the "target render surface space" and
1620 // local layer space. Note that render surfaces, except for the root, also
1621 // draw themselves into a different target render surface, and so their draw
1622 // transform and origin transforms are also described with respect to the
1625 // Using these definitions, then:
1627 // The draw transform for the layer is:
1628 // M[draw] = M[parent] * Tr[origin] * composite_layer_transform *
1629 // S[layer2content] = M[parent] * Tr[layer->position() + anchor] *
1630 // M[layer] * Tr[anchor2origin] * S[layer2content]
1632 // Interpreting the math left-to-right, this transforms from the
1633 // layer's render surface to the origin of the layer in content space.
1635 // The screen space transform is:
1636 // M[screenspace] = M[root] * Tr[origin] * composite_layer_transform *
1638 // = M[root] * Tr[layer->position() + anchor] * M[layer]
1639 // * Tr[anchor2origin] * S[layer2content]
1641 // Interpreting the math left-to-right, this transforms from the root
1642 // render surface's content space to the origin of the layer in content
1645 // The transform hierarchy that is passed on to children (i.e. the child's
1646 // parent_matrix) is:
1647 // M[parent]_for_child = M[parent] * Tr[origin] *
1648 // composite_layer_transform
1649 // = M[parent] * Tr[layer->position() + anchor] *
1650 // M[layer] * Tr[anchor2origin]
1652 // and a similar matrix for the full hierarchy with respect to the
1655 // Finally, note that the final matrix used by the shader for the layer is P *
1656 // M[draw] * S . This final product is computed in drawTexturedQuad(), where:
1657 // P is the projection matrix
1658 // S is the scale adjustment (to scale up a canonical quad to the
1661 // When a render surface has a replica layer, that layer's transform is used
1662 // to draw a second copy of the surface. gfx::Transforms named here are
1663 // relative to the surface, unless they specify they are relative to the
1666 // We will denote a scale by device scale S[deviceScale]
1668 // The render surface draw transform to its target surface origin is:
1669 // M[surfaceDraw] = M[owningLayer->Draw]
1671 // The render surface origin transform to its the root (screen space) origin
1673 // M[surface2root] = M[owningLayer->screenspace] *
1674 // S[deviceScale].inverse()
1676 // The replica draw transform to its target surface origin is:
1677 // M[replicaDraw] = S[deviceScale] * M[surfaceDraw] *
1678 // Tr[replica->position() + replica->anchor()] * Tr[replica] *
1679 // Tr[origin2anchor].inverse() * S[contents_scale].inverse()
1681 // The replica draw transform to the root (screen space) origin is:
1682 // M[replica2root] = M[surface2root] * Tr[replica->position()] *
1683 // Tr[replica] * Tr[origin2anchor].inverse()
1686 // It makes no sense to have a non-unit page_scale_factor without specifying
1687 // which layer roots the subtree the scale is applied to.
1688 DCHECK(globals
.page_scale_application_layer
||
1689 (globals
.page_scale_factor
== 1.f
));
1691 CHECK(!layer
->draw_properties().visited
);
1692 layer
->draw_properties().visited
= true;
1694 DataForRecursion
<LayerType
> data_for_children
;
1695 typename
LayerType::RenderSurfaceType
*
1696 nearest_occlusion_immune_ancestor_surface
=
1697 data_from_ancestor
.nearest_occlusion_immune_ancestor_surface
;
1698 data_for_children
.in_subtree_of_page_scale_application_layer
=
1699 data_from_ancestor
.in_subtree_of_page_scale_application_layer
;
1700 data_for_children
.subtree_can_use_lcd_text
=
1701 data_from_ancestor
.subtree_can_use_lcd_text
;
1703 // Layers that are marked as hidden will hide themselves and their subtree.
1704 // Exception: Layers with copy requests, whether hidden or not, must be drawn
1705 // anyway. In this case, we will inform their subtree they are visible to get
1706 // the right results.
1707 const bool layer_is_visible
=
1708 data_from_ancestor
.subtree_is_visible_from_ancestor
&&
1709 !layer
->hide_layer_and_subtree();
1710 const bool layer_is_drawn
= layer_is_visible
|| layer
->HasCopyRequest();
1712 // The root layer cannot skip CalcDrawProperties.
1713 if (!IsRootLayer(layer
) && SubtreeShouldBeSkipped(layer
, layer_is_drawn
)) {
1714 if (layer
->render_surface())
1715 layer
->ClearRenderSurfaceLayerList();
1716 layer
->draw_properties().render_target
= nullptr;
1720 // We need to circumvent the normal recursive flow of information for clip
1721 // children (they don't inherit their direct ancestor's clip information).
1722 // This is unfortunate, and would be unnecessary if we were to formally
1723 // separate the clipping hierarchy from the layer hierarchy.
1724 bool ancestor_clips_subtree
= data_from_ancestor
.ancestor_clips_subtree
;
1725 gfx::Rect ancestor_clip_rect_in_target_space
=
1726 data_from_ancestor
.clip_rect_in_target_space
;
1728 // Update our clipping state. If we have a clip parent we will need to pull
1729 // from the clip state cache rather than using the clip state passed from our
1730 // immediate ancestor.
1731 UpdateClipRectsForClipChild
<LayerType
>(
1732 layer
, &ancestor_clip_rect_in_target_space
, &ancestor_clips_subtree
);
1734 // As this function proceeds, these are the properties for the current
1735 // layer that actually get computed. To avoid unnecessary copies
1736 // (particularly for matrices), we do computations directly on these values
1738 DrawProperties
<LayerType
>& layer_draw_properties
= layer
->draw_properties();
1740 gfx::Rect clip_rect_in_target_space
;
1741 bool layer_or_ancestor_clips_descendants
= false;
1743 // This value is cached on the stack so that we don't have to inverse-project
1744 // the surface's clip rect redundantly for every layer. This value is the
1745 // same as the target surface's clip rect, except that instead of being
1746 // described in the target surface's target's space, it is described in the
1747 // current render target's space.
1748 gfx::Rect clip_rect_of_target_surface_in_target_space
;
1750 float accumulated_draw_opacity
= layer
->opacity();
1751 bool animating_opacity_to_target
= layer
->OpacityIsAnimating();
1752 bool animating_opacity_to_screen
= animating_opacity_to_target
;
1753 if (layer
->parent()) {
1754 accumulated_draw_opacity
*= layer
->parent()->draw_opacity();
1755 animating_opacity_to_target
|= layer
->parent()->draw_opacity_is_animating();
1756 animating_opacity_to_screen
|=
1757 layer
->parent()->screen_space_opacity_is_animating();
1760 bool animating_transform_to_target
= layer
->TransformIsAnimating();
1761 bool animating_transform_to_screen
= animating_transform_to_target
;
1762 if (layer
->parent()) {
1763 animating_transform_to_target
|=
1764 layer
->parent()->draw_transform_is_animating();
1765 animating_transform_to_screen
|=
1766 layer
->parent()->screen_space_transform_is_animating();
1768 gfx::Point3F transform_origin
= layer
->transform_origin();
1769 gfx::ScrollOffset scroll_offset
= GetEffectiveCurrentScrollOffset(layer
);
1770 gfx::PointF position
=
1771 layer
->position() - ScrollOffsetToVector2dF(scroll_offset
);
1772 gfx::Transform combined_transform
= data_from_ancestor
.parent_matrix
;
1773 if (!layer
->transform().IsIdentity()) {
1774 // LT = Tr[origin] * Tr[origin2transformOrigin]
1775 combined_transform
.Translate3d(position
.x() + transform_origin
.x(),
1776 position
.y() + transform_origin
.y(),
1777 transform_origin
.z());
1778 // LT = Tr[origin] * Tr[origin2origin] * M[layer]
1779 combined_transform
.PreconcatTransform(layer
->transform());
1780 // LT = Tr[origin] * Tr[origin2origin] * M[layer] *
1781 // Tr[transformOrigin2origin]
1782 combined_transform
.Translate3d(
1783 -transform_origin
.x(), -transform_origin
.y(), -transform_origin
.z());
1785 combined_transform
.Translate(position
.x(), position
.y());
1788 gfx::Vector2dF effective_scroll_delta
= GetEffectiveScrollDelta(layer
);
1789 if (!animating_transform_to_target
&& layer
->scrollable() &&
1790 combined_transform
.IsScaleOrTranslation()) {
1791 // Align the scrollable layer's position to screen space pixels to avoid
1792 // blurriness. To avoid side-effects, do this only if the transform is
1794 gfx::Vector2dF previous_translation
= combined_transform
.To2dTranslation();
1795 combined_transform
.RoundTranslationComponents();
1796 gfx::Vector2dF current_translation
= combined_transform
.To2dTranslation();
1798 // This rounding changes the scroll delta, and so must be included
1799 // in the scroll compensation matrix. The scaling converts from physical
1800 // coordinates to the scroll delta's CSS coordinates (using the parent
1801 // matrix instead of combined transform since scrolling is applied before
1802 // the layer's transform). For example, if we have a total scale factor of
1803 // 3.0, then 1 physical pixel is only 1/3 of a CSS pixel.
1804 gfx::Vector2dF parent_scales
= MathUtil::ComputeTransform2dScaleComponents(
1805 data_from_ancestor
.parent_matrix
, 1.f
);
1806 effective_scroll_delta
-=
1807 gfx::ScaleVector2d(current_translation
- previous_translation
,
1808 1.f
/ parent_scales
.x(),
1809 1.f
/ parent_scales
.y());
1812 // Apply adjustment from position constraints.
1813 ApplyPositionAdjustment(layer
, data_from_ancestor
.fixed_container
,
1814 data_from_ancestor
.scroll_compensation_matrix
, &combined_transform
);
1816 bool combined_is_animating_scale
= false;
1817 float combined_maximum_animation_contents_scale
= 0.f
;
1818 float combined_starting_animation_contents_scale
= 0.f
;
1819 if (globals
.can_adjust_raster_scales
) {
1820 CalculateAnimationContentsScale(
1821 layer
, data_from_ancestor
.ancestor_is_animating_scale
,
1822 data_from_ancestor
.maximum_animation_contents_scale
,
1823 data_from_ancestor
.parent_matrix
, combined_transform
,
1824 &combined_is_animating_scale
,
1825 &combined_maximum_animation_contents_scale
,
1826 &combined_starting_animation_contents_scale
);
1828 data_for_children
.ancestor_is_animating_scale
= combined_is_animating_scale
;
1829 data_for_children
.maximum_animation_contents_scale
=
1830 combined_maximum_animation_contents_scale
;
1832 // Compute the 2d scale components of the transform hierarchy up to the target
1833 // surface. From there, we can decide on a contents scale for the layer.
1834 float layer_scale_factors
= globals
.device_scale_factor
;
1835 if (data_from_ancestor
.in_subtree_of_page_scale_application_layer
)
1836 layer_scale_factors
*= globals
.page_scale_factor
;
1837 gfx::Vector2dF combined_transform_scales
=
1838 MathUtil::ComputeTransform2dScaleComponents(
1840 layer_scale_factors
);
1842 float ideal_contents_scale
=
1843 globals
.can_adjust_raster_scales
1844 ? std::max(combined_transform_scales
.x(),
1845 combined_transform_scales
.y())
1846 : layer_scale_factors
;
1847 UpdateLayerContentsScale(
1849 globals
.can_adjust_raster_scales
,
1850 ideal_contents_scale
,
1851 globals
.device_scale_factor
,
1852 data_from_ancestor
.in_subtree_of_page_scale_application_layer
1853 ? globals
.page_scale_factor
1855 animating_transform_to_screen
);
1857 UpdateLayerScaleDrawProperties(
1858 layer
, ideal_contents_scale
, combined_maximum_animation_contents_scale
,
1859 combined_starting_animation_contents_scale
,
1860 data_from_ancestor
.in_subtree_of_page_scale_application_layer
1861 ? globals
.page_scale_factor
1863 globals
.device_scale_factor
);
1865 LayerType
* mask_layer
= layer
->mask_layer();
1867 UpdateLayerScaleDrawProperties(
1868 mask_layer
, ideal_contents_scale
,
1869 combined_maximum_animation_contents_scale
,
1870 combined_starting_animation_contents_scale
,
1871 data_from_ancestor
.in_subtree_of_page_scale_application_layer
1872 ? globals
.page_scale_factor
1874 globals
.device_scale_factor
);
1877 LayerType
* replica_mask_layer
=
1878 layer
->replica_layer() ? layer
->replica_layer()->mask_layer() : NULL
;
1879 if (replica_mask_layer
) {
1880 UpdateLayerScaleDrawProperties(
1881 replica_mask_layer
, ideal_contents_scale
,
1882 combined_maximum_animation_contents_scale
,
1883 combined_starting_animation_contents_scale
,
1884 data_from_ancestor
.in_subtree_of_page_scale_application_layer
1885 ? globals
.page_scale_factor
1887 globals
.device_scale_factor
);
1890 // The draw_transform that gets computed below is effectively the layer's
1891 // draw_transform, unless the layer itself creates a render_surface. In that
1892 // case, the render_surface re-parents the transforms.
1893 layer_draw_properties
.target_space_transform
= combined_transform
;
1894 // M[draw] = M[parent] * LT * S[layer2content]
1895 layer_draw_properties
.target_space_transform
.Scale(
1896 SK_MScalar1
/ layer
->contents_scale_x(),
1897 SK_MScalar1
/ layer
->contents_scale_y());
1899 // The layer's screen_space_transform represents the transform between root
1900 // layer's "screen space" and local content space.
1901 layer_draw_properties
.screen_space_transform
=
1902 data_from_ancestor
.full_hierarchy_matrix
;
1903 layer_draw_properties
.screen_space_transform
.PreconcatTransform
1904 (layer_draw_properties
.target_space_transform
);
1906 bool layer_can_use_lcd_text
= true;
1907 bool subtree_can_use_lcd_text
= true;
1908 if (!globals
.layers_always_allowed_lcd_text
) {
1909 // To avoid color fringing, LCD text should only be used on opaque layers
1910 // with just integral translation.
1911 subtree_can_use_lcd_text
= data_from_ancestor
.subtree_can_use_lcd_text
&&
1912 accumulated_draw_opacity
== 1.f
&&
1913 layer_draw_properties
.target_space_transform
1914 .IsIdentityOrIntegerTranslation();
1915 // Also disable LCD text locally for non-opaque content.
1916 layer_can_use_lcd_text
= subtree_can_use_lcd_text
&&
1917 layer
->contents_opaque();
1920 // full_hierarchy_matrix is the matrix that transforms objects between screen
1921 // space (except projection matrix) and the most recent RenderSurfaceImpl's
1922 // space. next_hierarchy_matrix will only change if this layer uses a new
1923 // RenderSurfaceImpl, otherwise remains the same.
1924 data_for_children
.full_hierarchy_matrix
=
1925 data_from_ancestor
.full_hierarchy_matrix
;
1927 bool render_to_separate_surface
=
1928 IsRootLayer(layer
) ||
1929 (globals
.can_render_to_separate_surface
&& layer
->render_surface());
1931 if (render_to_separate_surface
) {
1932 DCHECK(layer
->render_surface());
1933 // Check back-face visibility before continuing with this surface and its
1935 if (!layer
->double_sided() && TransformToParentIsKnown(layer
) &&
1936 IsSurfaceBackFaceVisible(layer
, combined_transform
)) {
1937 layer
->ClearRenderSurfaceLayerList();
1938 layer
->draw_properties().render_target
= nullptr;
1942 typename
LayerType::RenderSurfaceType
* render_surface
=
1943 layer
->render_surface();
1944 layer
->ClearRenderSurfaceLayerList();
1946 layer_draw_properties
.render_target
= layer
;
1947 if (IsRootLayer(layer
)) {
1948 // The root layer's render surface size is predetermined and so the root
1949 // layer can't directly support non-identity transforms. It should just
1950 // forward top-level transforms to the rest of the tree.
1951 data_for_children
.parent_matrix
= combined_transform
;
1953 // The root surface does not contribute to any other surface, it has no
1955 layer
->render_surface()->set_contributes_to_drawn_surface(false);
1957 // The owning layer's draw transform has a scale from content to layer
1958 // space which we do not want; so here we use the combined_transform
1959 // instead of the draw_transform. However, we do need to add a different
1960 // scale factor that accounts for the surface's pixel dimensions.
1961 // Remove the combined_transform scale from the draw transform.
1962 gfx::Transform draw_transform
= combined_transform
;
1963 draw_transform
.Scale(1.0 / combined_transform_scales
.x(),
1964 1.0 / combined_transform_scales
.y());
1965 render_surface
->SetDrawTransform(draw_transform
);
1967 // The owning layer's transform was re-parented by the surface, so the
1968 // layer's new draw_transform only needs to scale the layer to surface
1970 layer_draw_properties
.target_space_transform
.MakeIdentity();
1971 layer_draw_properties
.target_space_transform
.Scale(
1972 combined_transform_scales
.x() / layer
->contents_scale_x(),
1973 combined_transform_scales
.y() / layer
->contents_scale_y());
1975 // Inside the surface's subtree, we scale everything to the owning layer's
1976 // scale. The sublayer matrix transforms layer rects into target surface
1977 // content space. Conceptually, all layers in the subtree inherit the
1978 // scale at the point of the render surface in the transform hierarchy,
1979 // but we apply it explicitly to the owning layer and the remainder of the
1980 // subtree independently.
1981 DCHECK(data_for_children
.parent_matrix
.IsIdentity());
1982 data_for_children
.parent_matrix
.Scale(combined_transform_scales
.x(),
1983 combined_transform_scales
.y());
1985 // Even if the |layer_is_drawn|, it only contributes to a drawn surface
1986 // when the |layer_is_visible|.
1987 layer
->render_surface()->set_contributes_to_drawn_surface(
1991 // The opacity value is moved from the layer to its surface, so that the
1992 // entire subtree properly inherits opacity.
1993 render_surface
->SetDrawOpacity(accumulated_draw_opacity
);
1994 render_surface
->SetDrawOpacityIsAnimating(animating_opacity_to_target
);
1995 animating_opacity_to_target
= false;
1996 layer_draw_properties
.opacity
= 1.f
;
1997 layer_draw_properties
.blend_mode
= SkXfermode::kSrcOver_Mode
;
1998 layer_draw_properties
.opacity_is_animating
= animating_opacity_to_target
;
1999 layer_draw_properties
.screen_space_opacity_is_animating
=
2000 animating_opacity_to_screen
;
2002 render_surface
->SetTargetSurfaceTransformsAreAnimating(
2003 animating_transform_to_target
);
2004 render_surface
->SetScreenSpaceTransformsAreAnimating(
2005 animating_transform_to_screen
);
2006 animating_transform_to_target
= false;
2007 layer_draw_properties
.target_space_transform_is_animating
=
2008 animating_transform_to_target
;
2009 layer_draw_properties
.screen_space_transform_is_animating
=
2010 animating_transform_to_screen
;
2012 // Update the aggregate hierarchy matrix to include the transform of the
2013 // newly created RenderSurfaceImpl.
2014 data_for_children
.full_hierarchy_matrix
.PreconcatTransform(
2015 render_surface
->draw_transform());
2017 // A render surface inherently acts as a flattening point for the content of
2019 data_for_children
.full_hierarchy_matrix
.FlattenTo2d();
2021 if (layer
->mask_layer()) {
2022 DrawProperties
<LayerType
>& mask_layer_draw_properties
=
2023 layer
->mask_layer()->draw_properties();
2024 mask_layer_draw_properties
.render_target
= layer
;
2025 mask_layer_draw_properties
.visible_content_rect
=
2026 gfx::Rect(layer
->content_bounds());
2029 if (layer
->replica_layer() && layer
->replica_layer()->mask_layer()) {
2030 DrawProperties
<LayerType
>& replica_mask_draw_properties
=
2031 layer
->replica_layer()->mask_layer()->draw_properties();
2032 replica_mask_draw_properties
.render_target
= layer
;
2033 replica_mask_draw_properties
.visible_content_rect
=
2034 gfx::Rect(layer
->content_bounds());
2037 // Ignore occlusion from outside the surface when surface contents need to
2038 // be fully drawn. Layers with copy-request need to be complete.
2039 // We could be smarter about layers with replica and exclude regions
2040 // where both layer and the replica are occluded, but this seems like an
2041 // overkill. The same is true for layers with filters that move pixels.
2042 // TODO(senorblanco): make this smarter for the SkImageFilter case (check
2043 // for pixel-moving filters)
2044 if (layer
->HasCopyRequest() ||
2045 layer
->has_replica() ||
2046 layer
->filters().HasReferenceFilter() ||
2047 layer
->filters().HasFilterThatMovesPixels()) {
2048 nearest_occlusion_immune_ancestor_surface
= render_surface
;
2050 render_surface
->SetNearestOcclusionImmuneAncestor(
2051 nearest_occlusion_immune_ancestor_surface
);
2053 layer_or_ancestor_clips_descendants
= false;
2054 bool subtree_is_clipped_by_surface_bounds
= false;
2055 if (ancestor_clips_subtree
) {
2056 // It may be the layer or the surface doing the clipping of the subtree,
2057 // but in either case, we'll be clipping to the projected clip rect of our
2059 gfx::Transform
inverse_surface_draw_transform(
2060 gfx::Transform::kSkipInitialization
);
2061 if (!render_surface
->draw_transform().GetInverse(
2062 &inverse_surface_draw_transform
)) {
2063 // TODO(shawnsingh): Either we need to handle uninvertible transforms
2064 // here, or DCHECK that the transform is invertible.
2067 gfx::Rect surface_clip_rect_in_target_space
= gfx::IntersectRects(
2068 data_from_ancestor
.clip_rect_of_target_surface_in_target_space
,
2069 ancestor_clip_rect_in_target_space
);
2070 gfx::Rect projected_surface_rect
= MathUtil::ProjectEnclosingClippedRect(
2071 inverse_surface_draw_transform
, surface_clip_rect_in_target_space
);
2073 if (layer_draw_properties
.num_unclipped_descendants
> 0) {
2074 // If we have unclipped descendants, we cannot count on the render
2075 // surface's bounds clipping our subtree: the unclipped descendants
2076 // could cause us to expand our bounds. In this case, we must rely on
2077 // layer clipping for correctess. NB: since we can only encounter
2078 // translations between a clip child and its clip parent, clipping is
2079 // guaranteed to be exact in this case.
2080 layer_or_ancestor_clips_descendants
= true;
2081 clip_rect_in_target_space
= projected_surface_rect
;
2083 // The new render_surface here will correctly clip the entire subtree.
2084 // So, we do not need to continue propagating the clipping state further
2085 // down the tree. This way, we can avoid transforming clip rects from
2086 // ancestor target surface space to current target surface space that
2087 // could cause more w < 0 headaches. The render surface clip rect is
2088 // expressed in the space where this surface draws, i.e. the same space
2089 // as clip_rect_from_ancestor_in_ancestor_target_space.
2090 render_surface
->SetClipRect(ancestor_clip_rect_in_target_space
);
2091 clip_rect_of_target_surface_in_target_space
= projected_surface_rect
;
2092 subtree_is_clipped_by_surface_bounds
= true;
2096 DCHECK(layer
->render_surface());
2097 DCHECK(!layer
->parent() || layer
->parent()->render_target() ==
2098 accumulated_surface_state
->back().render_target
);
2100 accumulated_surface_state
->push_back(
2101 AccumulatedSurfaceState
<LayerType
>(layer
));
2103 render_surface
->SetIsClipped(subtree_is_clipped_by_surface_bounds
);
2104 if (!subtree_is_clipped_by_surface_bounds
) {
2105 render_surface
->SetClipRect(gfx::Rect());
2106 clip_rect_of_target_surface_in_target_space
=
2107 data_from_ancestor
.clip_rect_of_target_surface_in_target_space
;
2110 // If the new render surface is drawn translucent or with a non-integral
2111 // translation then the subtree that gets drawn on this render surface
2112 // cannot use LCD text.
2113 data_for_children
.subtree_can_use_lcd_text
= subtree_can_use_lcd_text
;
2115 render_surface_layer_list
->push_back(layer
);
2117 DCHECK(layer
->parent());
2119 // Note: layer_draw_properties.target_space_transform is computed above,
2120 // before this if-else statement.
2121 layer_draw_properties
.target_space_transform_is_animating
=
2122 animating_transform_to_target
;
2123 layer_draw_properties
.screen_space_transform_is_animating
=
2124 animating_transform_to_screen
;
2125 layer_draw_properties
.opacity
= accumulated_draw_opacity
;
2126 layer_draw_properties
.blend_mode
= layer
->blend_mode();
2127 layer_draw_properties
.opacity_is_animating
= animating_opacity_to_target
;
2128 layer_draw_properties
.screen_space_opacity_is_animating
=
2129 animating_opacity_to_screen
;
2130 data_for_children
.parent_matrix
= combined_transform
;
2132 // Layers without render_surfaces directly inherit the ancestor's clip
2134 layer_or_ancestor_clips_descendants
= ancestor_clips_subtree
;
2135 if (ancestor_clips_subtree
) {
2136 clip_rect_in_target_space
=
2137 ancestor_clip_rect_in_target_space
;
2140 // The surface's cached clip rect value propagates regardless of what
2141 // clipping goes on between layers here.
2142 clip_rect_of_target_surface_in_target_space
=
2143 data_from_ancestor
.clip_rect_of_target_surface_in_target_space
;
2145 // Layers that are not their own render_target will render into the target
2146 // of their nearest ancestor.
2147 layer_draw_properties
.render_target
= layer
->parent()->render_target();
2150 layer_draw_properties
.can_use_lcd_text
= layer_can_use_lcd_text
;
2152 gfx::Size
content_size_affected_by_delta(layer
->content_bounds());
2154 // Non-zero BoundsDelta imply the contents_scale of 1.0
2155 // because BoundsDela is only set on Android where
2156 // ContentScalingLayer is never used.
2157 DCHECK_IMPLIES(!BoundsDelta(layer
).IsZero(),
2158 (layer
->contents_scale_x() == 1.0 &&
2159 layer
->contents_scale_y() == 1.0));
2161 // Thus we can omit contents scale in the following calculation.
2162 gfx::Vector2d bounds_delta
= BoundsDelta(layer
);
2163 content_size_affected_by_delta
.Enlarge(bounds_delta
.x(), bounds_delta
.y());
2165 gfx::Rect rect_in_target_space
= MathUtil::MapEnclosingClippedRect(
2166 layer
->draw_transform(),
2167 gfx::Rect(content_size_affected_by_delta
));
2169 if (LayerClipsSubtree(layer
)) {
2170 layer_or_ancestor_clips_descendants
= true;
2171 if (ancestor_clips_subtree
&& !render_to_separate_surface
) {
2172 // A layer without render surface shares the same target as its ancestor.
2173 clip_rect_in_target_space
=
2174 ancestor_clip_rect_in_target_space
;
2175 clip_rect_in_target_space
.Intersect(rect_in_target_space
);
2177 clip_rect_in_target_space
= rect_in_target_space
;
2181 // Tell the layer the rect that it's clipped by. In theory we could use a
2182 // tighter clip rect here (drawable_content_rect), but that actually does not
2183 // reduce how much would be drawn, and instead it would create unnecessary
2184 // changes to scissor state affecting GPU performance. Our clip information
2185 // is used in the recursion below, so we must set it beforehand.
2186 layer_draw_properties
.is_clipped
= layer_or_ancestor_clips_descendants
;
2187 if (layer_or_ancestor_clips_descendants
) {
2188 layer_draw_properties
.clip_rect
= clip_rect_in_target_space
;
2190 // Initialize the clip rect to a safe value that will not clip the
2191 // layer, just in case clipping is still accidentally used.
2192 layer_draw_properties
.clip_rect
= rect_in_target_space
;
2195 typename
LayerType::LayerListType
& descendants
=
2196 (render_to_separate_surface
? layer
->render_surface()->layer_list()
2199 // Any layers that are appended after this point are in the layer's subtree
2200 // and should be included in the sorting process.
2201 size_t sorting_start_index
= descendants
.size();
2203 if (!LayerShouldBeSkipped(layer
, layer_is_drawn
)) {
2204 MarkLayerWithRenderSurfaceLayerListId(layer
,
2205 current_render_surface_layer_list_id
);
2206 descendants
.push_back(layer
);
2209 // Any layers that are appended after this point may need to be sorted if we
2210 // visit the children out of order.
2211 size_t render_surface_layer_list_child_sorting_start_index
=
2212 render_surface_layer_list
->size();
2213 size_t layer_list_child_sorting_start_index
= descendants
.size();
2215 if (!layer
->children().empty()) {
2216 if (layer
== globals
.page_scale_application_layer
) {
2217 data_for_children
.parent_matrix
.Scale(
2218 globals
.page_scale_factor
,
2219 globals
.page_scale_factor
);
2220 data_for_children
.in_subtree_of_page_scale_application_layer
= true;
2222 if (layer
== globals
.elastic_overscroll_application_layer
) {
2223 data_for_children
.parent_matrix
.Translate(
2224 -globals
.elastic_overscroll
.x(), -globals
.elastic_overscroll
.y());
2227 // Flatten to 2D if the layer doesn't preserve 3D.
2228 if (layer
->should_flatten_transform())
2229 data_for_children
.parent_matrix
.FlattenTo2d();
2231 data_for_children
.scroll_compensation_matrix
=
2232 ComputeScrollCompensationMatrixForChildren(
2234 data_from_ancestor
.parent_matrix
,
2235 data_from_ancestor
.scroll_compensation_matrix
,
2236 effective_scroll_delta
);
2237 data_for_children
.fixed_container
=
2238 layer
->IsContainerForFixedPositionLayers() ?
2239 layer
: data_from_ancestor
.fixed_container
;
2241 data_for_children
.clip_rect_in_target_space
= clip_rect_in_target_space
;
2242 data_for_children
.clip_rect_of_target_surface_in_target_space
=
2243 clip_rect_of_target_surface_in_target_space
;
2244 data_for_children
.ancestor_clips_subtree
=
2245 layer_or_ancestor_clips_descendants
;
2246 data_for_children
.nearest_occlusion_immune_ancestor_surface
=
2247 nearest_occlusion_immune_ancestor_surface
;
2248 data_for_children
.subtree_is_visible_from_ancestor
= layer_is_drawn
;
2251 std::vector
<LayerType
*> sorted_children
;
2252 bool child_order_changed
= false;
2253 if (layer_draw_properties
.has_child_with_a_scroll_parent
)
2254 child_order_changed
= SortChildrenForRecursion(&sorted_children
, *layer
);
2256 for (size_t i
= 0; i
< layer
->children().size(); ++i
) {
2257 // If one of layer's children has a scroll parent, then we may have to
2258 // visit the children out of order. The new order is stored in
2259 // sorted_children. Otherwise, we'll grab the child directly from the
2260 // layer's list of children.
2262 layer_draw_properties
.has_child_with_a_scroll_parent
2263 ? sorted_children
[i
]
2264 : LayerTreeHostCommon::get_layer_as_raw_ptr(layer
->children(), i
);
2266 child
->draw_properties().index_of_first_descendants_addition
=
2268 child
->draw_properties().index_of_first_render_surface_layer_list_addition
=
2269 render_surface_layer_list
->size();
2271 CalculateDrawPropertiesInternal
<LayerType
>(
2275 render_surface_layer_list
,
2277 accumulated_surface_state
,
2278 current_render_surface_layer_list_id
);
2279 // If the child is its own render target, then it has a render surface.
2280 if (child
->render_target() == child
&&
2281 !child
->render_surface()->layer_list().empty() &&
2282 !child
->render_surface()->content_rect().IsEmpty()) {
2283 // This child will contribute its render surface, which means
2284 // we need to mark just the mask layer (and replica mask layer)
2286 MarkMasksWithRenderSurfaceLayerListId(
2287 child
, current_render_surface_layer_list_id
);
2288 descendants
.push_back(child
);
2291 child
->draw_properties().num_descendants_added
=
2292 descendants
.size() -
2293 child
->draw_properties().index_of_first_descendants_addition
;
2294 child
->draw_properties().num_render_surfaces_added
=
2295 render_surface_layer_list
->size() -
2296 child
->draw_properties()
2297 .index_of_first_render_surface_layer_list_addition
;
2298 layer_draw_properties
.layer_or_descendant_is_drawn
|=
2299 child
->draw_properties().layer_or_descendant_is_drawn
;
2302 // Add the unsorted layer list contributions, if necessary.
2303 if (child_order_changed
) {
2304 SortLayerListContributions(
2306 GetLayerListForSorting(render_surface_layer_list
),
2307 render_surface_layer_list_child_sorting_start_index
,
2308 &GetNewRenderSurfacesStartIndexAndCount
<LayerType
>);
2310 SortLayerListContributions(
2313 layer_list_child_sorting_start_index
,
2314 &GetNewDescendantsStartIndexAndCount
<LayerType
>);
2317 // Compute the total drawable_content_rect for this subtree (the rect is in
2318 // target surface space).
2319 gfx::Rect local_drawable_content_rect_of_subtree
=
2320 accumulated_surface_state
->back().drawable_content_rect
;
2321 if (render_to_separate_surface
) {
2322 DCHECK(accumulated_surface_state
->back().render_target
== layer
);
2323 accumulated_surface_state
->pop_back();
2326 if (render_to_separate_surface
&& !IsRootLayer(layer
) &&
2327 layer
->render_surface()->layer_list().empty()) {
2328 RemoveSurfaceForEarlyExit(layer
, render_surface_layer_list
);
2332 // Compute the layer's drawable content rect (the rect is in target surface
2334 layer_draw_properties
.drawable_content_rect
= rect_in_target_space
;
2335 if (layer_or_ancestor_clips_descendants
) {
2336 layer_draw_properties
.drawable_content_rect
.Intersect(
2337 clip_rect_in_target_space
);
2339 if (layer
->DrawsContent()) {
2340 local_drawable_content_rect_of_subtree
.Union(
2341 layer_draw_properties
.drawable_content_rect
);
2344 // Compute the layer's visible content rect (the rect is in content space).
2345 layer_draw_properties
.visible_content_rect
= CalculateVisibleContentRect(
2346 layer
, clip_rect_of_target_surface_in_target_space
, rect_in_target_space
);
2348 // Compute the remaining properties for the render surface, if the layer has
2350 if (IsRootLayer(layer
)) {
2351 // The root layer's surface's content_rect is always the entire viewport.
2352 DCHECK(render_to_separate_surface
);
2353 layer
->render_surface()->SetContentRect(
2354 ancestor_clip_rect_in_target_space
);
2355 } else if (render_to_separate_surface
) {
2356 typename
LayerType::RenderSurfaceType
* render_surface
=
2357 layer
->render_surface();
2358 gfx::Rect clipped_content_rect
= local_drawable_content_rect_of_subtree
;
2360 // Don't clip if the layer is reflected as the reflection shouldn't be
2361 // clipped. If the layer is animating, then the surface's transform to
2362 // its target is not known on the main thread, and we should not use it
2364 if (!layer
->replica_layer() && TransformToParentIsKnown(layer
)) {
2365 // Note, it is correct to use data_from_ancestor.ancestor_clips_subtree
2366 // here, because we are looking at this layer's render_surface, not the
2368 if (render_surface
->is_clipped() && !clipped_content_rect
.IsEmpty()) {
2369 gfx::Rect surface_clip_rect
= LayerTreeHostCommon::CalculateVisibleRect(
2370 render_surface
->clip_rect(),
2371 clipped_content_rect
,
2372 render_surface
->draw_transform());
2373 clipped_content_rect
.Intersect(surface_clip_rect
);
2377 // The RenderSurfaceImpl backing texture cannot exceed the maximum supported
2379 clipped_content_rect
.set_width(
2380 std::min(clipped_content_rect
.width(), globals
.max_texture_size
));
2381 clipped_content_rect
.set_height(
2382 std::min(clipped_content_rect
.height(), globals
.max_texture_size
));
2384 if (clipped_content_rect
.IsEmpty()) {
2385 RemoveSurfaceForEarlyExit(layer
, render_surface_layer_list
);
2389 // Layers having a non-default blend mode will blend with the content
2390 // inside its parent's render target. This render target should be
2391 // either root_for_isolated_group, or the root of the layer tree.
2392 // Otherwise, this layer will use an incomplete backdrop, limited to its
2393 // render target and the blending result will be incorrect.
2394 DCHECK(layer
->uses_default_blend_mode() || IsRootLayer(layer
) ||
2395 !layer
->parent()->render_target() ||
2396 IsRootLayer(layer
->parent()->render_target()) ||
2397 layer
->parent()->render_target()->is_root_for_isolated_group());
2399 render_surface
->SetContentRect(clipped_content_rect
);
2401 // The owning layer's screen_space_transform has a scale from content to
2402 // layer space which we need to undo and replace with a scale from the
2403 // surface's subtree into layer space.
2404 gfx::Transform screen_space_transform
= layer
->screen_space_transform();
2405 screen_space_transform
.Scale(
2406 layer
->contents_scale_x() / combined_transform_scales
.x(),
2407 layer
->contents_scale_y() / combined_transform_scales
.y());
2408 render_surface
->SetScreenSpaceTransform(screen_space_transform
);
2410 if (layer
->replica_layer()) {
2411 gfx::Transform surface_origin_to_replica_origin_transform
;
2412 surface_origin_to_replica_origin_transform
.Scale(
2413 combined_transform_scales
.x(), combined_transform_scales
.y());
2414 surface_origin_to_replica_origin_transform
.Translate(
2415 layer
->replica_layer()->position().x() +
2416 layer
->replica_layer()->transform_origin().x(),
2417 layer
->replica_layer()->position().y() +
2418 layer
->replica_layer()->transform_origin().y());
2419 surface_origin_to_replica_origin_transform
.PreconcatTransform(
2420 layer
->replica_layer()->transform());
2421 surface_origin_to_replica_origin_transform
.Translate(
2422 -layer
->replica_layer()->transform_origin().x(),
2423 -layer
->replica_layer()->transform_origin().y());
2424 surface_origin_to_replica_origin_transform
.Scale(
2425 1.0 / combined_transform_scales
.x(),
2426 1.0 / combined_transform_scales
.y());
2428 // Compute the replica's "originTransform" that maps from the replica's
2429 // origin space to the target surface origin space.
2430 gfx::Transform replica_origin_transform
=
2431 layer
->render_surface()->draw_transform() *
2432 surface_origin_to_replica_origin_transform
;
2433 render_surface
->SetReplicaDrawTransform(replica_origin_transform
);
2435 // Compute the replica's "screen_space_transform" that maps from the
2436 // replica's origin space to the screen's origin space.
2437 gfx::Transform replica_screen_space_transform
=
2438 layer
->render_surface()->screen_space_transform() *
2439 surface_origin_to_replica_origin_transform
;
2440 render_surface
->SetReplicaScreenSpaceTransform(
2441 replica_screen_space_transform
);
2445 SavePaintPropertiesLayer(layer
);
2447 // If neither this layer nor any of its children were added, early out.
2448 if (sorting_start_index
== descendants
.size()) {
2449 DCHECK(!render_to_separate_surface
|| IsRootLayer(layer
));
2453 UpdateAccumulatedSurfaceState
<LayerType
>(
2454 layer
, local_drawable_content_rect_of_subtree
, accumulated_surface_state
);
2456 if (layer
->HasContributingDelegatedRenderPasses()) {
2457 layer
->render_target()->render_surface()->
2458 AddContributingDelegatedRenderPassLayer(layer
);
2460 } // NOLINT(readability/fn_size)
2462 template <typename LayerType
, typename RenderSurfaceLayerListType
>
2463 static void ProcessCalcDrawPropsInputs(
2464 const LayerTreeHostCommon::CalcDrawPropsInputs
<LayerType
,
2465 RenderSurfaceLayerListType
>&
2467 SubtreeGlobals
<LayerType
>* globals
,
2468 DataForRecursion
<LayerType
>* data_for_recursion
) {
2469 DCHECK(inputs
.root_layer
);
2470 DCHECK(IsRootLayer(inputs
.root_layer
));
2471 DCHECK(inputs
.render_surface_layer_list
);
2473 gfx::Transform identity_matrix
;
2475 // The root layer's render_surface should receive the device viewport as the
2476 // initial clip rect.
2477 gfx::Rect
device_viewport_rect(inputs
.device_viewport_size
);
2479 gfx::Vector2dF device_transform_scale_components
=
2480 MathUtil::ComputeTransform2dScaleComponents(inputs
.device_transform
, 1.f
);
2481 // Not handling the rare case of different x and y device scale.
2482 float device_transform_scale
=
2483 std::max(device_transform_scale_components
.x(),
2484 device_transform_scale_components
.y());
2486 gfx::Transform scaled_device_transform
= inputs
.device_transform
;
2487 scaled_device_transform
.Scale(inputs
.device_scale_factor
,
2488 inputs
.device_scale_factor
);
2490 globals
->max_texture_size
= inputs
.max_texture_size
;
2491 globals
->device_scale_factor
=
2492 inputs
.device_scale_factor
* device_transform_scale
;
2493 globals
->page_scale_factor
= inputs
.page_scale_factor
;
2494 globals
->page_scale_application_layer
= inputs
.page_scale_application_layer
;
2495 globals
->elastic_overscroll
= inputs
.elastic_overscroll
;
2496 globals
->elastic_overscroll_application_layer
=
2497 inputs
.elastic_overscroll_application_layer
;
2498 globals
->can_render_to_separate_surface
=
2499 inputs
.can_render_to_separate_surface
;
2500 globals
->can_adjust_raster_scales
= inputs
.can_adjust_raster_scales
;
2501 globals
->layers_always_allowed_lcd_text
=
2502 inputs
.layers_always_allowed_lcd_text
;
2504 data_for_recursion
->parent_matrix
= scaled_device_transform
;
2505 data_for_recursion
->full_hierarchy_matrix
= identity_matrix
;
2506 data_for_recursion
->scroll_compensation_matrix
= identity_matrix
;
2507 data_for_recursion
->fixed_container
= inputs
.root_layer
;
2508 data_for_recursion
->clip_rect_in_target_space
= device_viewport_rect
;
2509 data_for_recursion
->clip_rect_of_target_surface_in_target_space
=
2510 device_viewport_rect
;
2511 data_for_recursion
->maximum_animation_contents_scale
= 0.f
;
2512 data_for_recursion
->ancestor_is_animating_scale
= false;
2513 data_for_recursion
->ancestor_clips_subtree
= true;
2514 data_for_recursion
->nearest_occlusion_immune_ancestor_surface
= NULL
;
2515 data_for_recursion
->in_subtree_of_page_scale_application_layer
= false;
2516 data_for_recursion
->subtree_can_use_lcd_text
= inputs
.can_use_lcd_text
;
2517 data_for_recursion
->subtree_is_visible_from_ancestor
= true;
2520 void LayerTreeHostCommon::UpdateRenderSurface(
2522 bool can_render_to_separate_surface
,
2523 gfx::Transform
* transform
,
2524 bool* draw_transform_is_axis_aligned
) {
2525 bool preserves_2d_axis_alignment
=
2526 transform
->Preserves2dAxisAlignment() && *draw_transform_is_axis_aligned
;
2527 if (IsRootLayer(layer
) || (can_render_to_separate_surface
&&
2528 SubtreeShouldRenderToSeparateSurface(
2529 layer
, preserves_2d_axis_alignment
))) {
2530 // We reset the transform here so that any axis-changing transforms
2531 // will now be relative to this RenderSurface.
2532 transform
->MakeIdentity();
2533 *draw_transform_is_axis_aligned
= true;
2534 if (!layer
->render_surface()) {
2535 layer
->CreateRenderSurface();
2537 layer
->SetHasRenderSurface(true);
2540 layer
->SetHasRenderSurface(false);
2541 if (layer
->render_surface())
2542 layer
->ClearRenderSurface();
2545 void LayerTreeHostCommon::UpdateRenderSurfaces(
2547 bool can_render_to_separate_surface
,
2548 const gfx::Transform
& parent_transform
,
2549 bool draw_transform_is_axis_aligned
) {
2550 gfx::Transform transform_for_children
= layer
->transform();
2551 transform_for_children
*= parent_transform
;
2552 draw_transform_is_axis_aligned
&= layer
->AnimationsPreserveAxisAlignment();
2553 UpdateRenderSurface(layer
, can_render_to_separate_surface
,
2554 &transform_for_children
, &draw_transform_is_axis_aligned
);
2556 for (size_t i
= 0; i
< layer
->children().size(); ++i
) {
2557 UpdateRenderSurfaces(layer
->children()[i
].get(),
2558 can_render_to_separate_surface
, transform_for_children
,
2559 draw_transform_is_axis_aligned
);
2563 static bool ApproximatelyEqual(const gfx::Rect
& r1
, const gfx::Rect
& r2
) {
2564 // TODO(vollick): This tolerance should be lower: crbug.com/471786
2565 static const int tolerance
= 3;
2568 return std::min(r2
.width(), r2
.height()) < tolerance
;
2571 return std::min(r1
.width(), r1
.height()) < tolerance
;
2573 return std::abs(r1
.x() - r2
.x()) <= tolerance
&&
2574 std::abs(r1
.y() - r2
.y()) <= tolerance
&&
2575 std::abs(r1
.right() - r2
.right()) <= tolerance
&&
2576 std::abs(r1
.bottom() - r2
.bottom()) <= tolerance
;
2579 static bool ApproximatelyEqual(const gfx::Transform
& a
,
2580 const gfx::Transform
& b
) {
2581 static const float component_tolerance
= 0.1f
;
2583 // We may have a larger discrepancy in the scroll components due to snapping
2584 // (floating point error might round the other way).
2585 static const float translation_tolerance
= 1.f
;
2587 for (int row
= 0; row
< 4; row
++) {
2588 for (int col
= 0; col
< 4; col
++) {
2590 std::abs(a
.matrix().get(row
, col
) - b
.matrix().get(row
, col
));
2591 const float tolerance
=
2592 col
== 3 && row
< 3 ? translation_tolerance
: component_tolerance
;
2593 if (delta
> tolerance
)
2601 template <typename LayerType
>
2602 void VerifyPropertyTreeValuesForLayer(LayerType
* current_layer
,
2603 PropertyTrees
* property_trees
) {
2604 const bool visible_rects_match
=
2605 ApproximatelyEqual(current_layer
->visible_content_rect(),
2606 current_layer
->visible_rect_from_property_trees());
2607 CHECK(visible_rects_match
)
2608 << "expected: " << current_layer
->visible_content_rect().ToString()
2610 << current_layer
->visible_rect_from_property_trees().ToString();
2612 const bool draw_transforms_match
=
2613 ApproximatelyEqual(current_layer
->draw_transform(),
2614 DrawTransformFromPropertyTrees(
2615 current_layer
, property_trees
->transform_tree
));
2616 CHECK(draw_transforms_match
)
2617 << "expected: " << current_layer
->draw_transform().ToString()
2619 << DrawTransformFromPropertyTrees(
2620 current_layer
, property_trees
->transform_tree
).ToString();
2622 const bool draw_opacities_match
=
2623 current_layer
->draw_opacity() ==
2624 DrawOpacityFromPropertyTrees(current_layer
, property_trees
->opacity_tree
);
2625 CHECK(draw_opacities_match
)
2626 << "expected: " << current_layer
->draw_opacity()
2627 << " actual: " << DrawOpacityFromPropertyTrees(
2628 current_layer
, property_trees
->opacity_tree
);
2631 void VerifyPropertyTreeValues(
2632 LayerTreeHostCommon::CalcDrawPropsMainInputs
* inputs
) {
2633 LayerIterator
<Layer
> it
, end
;
2634 for (it
= LayerIterator
<Layer
>::Begin(inputs
->render_surface_layer_list
),
2635 end
= LayerIterator
<Layer
>::End(inputs
->render_surface_layer_list
);
2637 Layer
* current_layer
= *it
;
2638 if (!it
.represents_itself() || !current_layer
->DrawsContent())
2640 VerifyPropertyTreeValuesForLayer(current_layer
, inputs
->property_trees
);
2644 void VerifyPropertyTreeValues(
2645 LayerTreeHostCommon::CalcDrawPropsImplInputs
* inputs
) {
2646 LayerIterator
<LayerImpl
> it
, end
;
2647 for (it
= LayerIterator
<LayerImpl
>::Begin(inputs
->render_surface_layer_list
),
2648 end
= LayerIterator
<LayerImpl
>::End(inputs
->render_surface_layer_list
);
2650 LayerImpl
* current_layer
= *it
;
2651 if (!it
.represents_itself() || !current_layer
->DrawsContent())
2653 VerifyPropertyTreeValuesForLayer(current_layer
, inputs
->property_trees
);
2657 enum PropertyTreeOption
{
2658 BUILD_PROPERTY_TREES_IF_NEEDED
,
2659 DONT_BUILD_PROPERTY_TREES
2662 template <typename LayerType
, typename RenderSurfaceLayerListType
>
2663 void CalculateDrawPropertiesAndVerify(LayerTreeHostCommon::CalcDrawPropsInputs
<
2665 RenderSurfaceLayerListType
>* inputs
,
2666 PropertyTreeOption property_tree_option
) {
2667 typename
LayerType::LayerListType dummy_layer_list
;
2668 SubtreeGlobals
<LayerType
> globals
;
2669 DataForRecursion
<LayerType
> data_for_recursion
;
2671 ProcessCalcDrawPropsInputs(*inputs
, &globals
, &data_for_recursion
);
2672 PreCalculateMetaInformationRecursiveData recursive_data
;
2673 PreCalculateMetaInformationInternal(inputs
->root_layer
, &recursive_data
);
2675 const bool should_measure_property_tree_performance
=
2676 inputs
->verify_property_trees
&&
2677 (property_tree_option
== BUILD_PROPERTY_TREES_IF_NEEDED
);
2679 if (should_measure_property_tree_performance
) {
2680 TRACE_EVENT_BEGIN0(TRACE_DISABLED_BY_DEFAULT("cc.debug.cdp-perf"),
2681 "LayerTreeHostCommon::CalculateDrawProperties");
2683 ResetDrawProperties(inputs
->root_layer
);
2685 std::vector
<AccumulatedSurfaceState
<LayerType
>> accumulated_surface_state
;
2686 CalculateDrawPropertiesInternal
<LayerType
>(
2687 inputs
->root_layer
, globals
, data_for_recursion
,
2688 inputs
->render_surface_layer_list
, &dummy_layer_list
,
2689 &accumulated_surface_state
, inputs
->current_render_surface_layer_list_id
);
2691 if (should_measure_property_tree_performance
) {
2692 TRACE_EVENT_END0(TRACE_DISABLED_BY_DEFAULT("cc.debug.cdp-perf"),
2693 "LayerTreeHostCommon::CalculateDrawProperties");
2696 if (inputs
->verify_property_trees
) {
2697 typename
LayerType::LayerListType update_layer_list
;
2699 // For testing purposes, sometimes property trees need to be built on the
2700 // compositor thread, so this can't just switch on Layer vs LayerImpl,
2701 // even though in practice only the main thread builds property trees.
2702 switch (property_tree_option
) {
2703 case BUILD_PROPERTY_TREES_IF_NEEDED
: {
2704 // The translation from layer to property trees is an intermediate
2705 // state. We will eventually get these data passed directly to the
2707 if (should_measure_property_tree_performance
) {
2709 TRACE_DISABLED_BY_DEFAULT("cc.debug.cdp-perf"),
2710 "LayerTreeHostCommon::ComputeVisibleRectsWithPropertyTrees");
2713 BuildPropertyTreesAndComputeVisibleRects(
2714 inputs
->root_layer
, inputs
->page_scale_application_layer
,
2715 inputs
->page_scale_factor
, inputs
->device_scale_factor
,
2716 gfx::Rect(inputs
->device_viewport_size
), inputs
->device_transform
,
2717 inputs
->property_trees
, &update_layer_list
);
2719 if (should_measure_property_tree_performance
) {
2721 TRACE_DISABLED_BY_DEFAULT("cc.debug.cdp-perf"),
2722 "LayerTreeHostCommon::ComputeVisibleRectsWithPropertyTrees");
2727 case DONT_BUILD_PROPERTY_TREES
: {
2729 TRACE_DISABLED_BY_DEFAULT("cc.debug.cdp-perf"),
2730 "LayerTreeHostCommon::ComputeJustVisibleRectsWithPropertyTrees");
2731 ComputeVisibleRectsUsingPropertyTrees(
2732 inputs
->root_layer
, inputs
->property_trees
, &update_layer_list
);
2737 VerifyPropertyTreeValues(inputs
);
2740 // The dummy layer list should not have been used.
2741 DCHECK_EQ(0u, dummy_layer_list
.size());
2742 // A root layer render_surface should always exist after
2743 // CalculateDrawProperties.
2744 DCHECK(inputs
->root_layer
->render_surface());
2747 void LayerTreeHostCommon::CalculateDrawProperties(
2748 CalcDrawPropsMainInputs
* inputs
) {
2749 UpdateRenderSurfaces(inputs
->root_layer
,
2750 inputs
->can_render_to_separate_surface
, gfx::Transform(),
2752 CalculateDrawPropertiesAndVerify(inputs
, BUILD_PROPERTY_TREES_IF_NEEDED
);
2755 void LayerTreeHostCommon::CalculateDrawProperties(
2756 CalcDrawPropsImplInputs
* inputs
) {
2757 CalculateDrawPropertiesAndVerify(inputs
, DONT_BUILD_PROPERTY_TREES
);
2760 void LayerTreeHostCommon::CalculateDrawProperties(
2761 CalcDrawPropsImplInputsForTesting
* inputs
) {
2762 CalculateDrawPropertiesAndVerify(inputs
, BUILD_PROPERTY_TREES_IF_NEEDED
);
2765 PropertyTrees
* GetPropertyTrees(Layer
* layer
) {
2766 return layer
->layer_tree_host()->property_trees();
2769 PropertyTrees
* GetPropertyTrees(LayerImpl
* layer
) {
2770 return layer
->layer_tree_impl()->property_trees();