Changes to RenderFrameProxy:
[chromium-blink-merge.git] / cc / trees / layer_tree_host_common.cc
blob3bbbf6340b623d5e0dd95258d4cf3e00ec0308d6
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"
7 #include <algorithm>
9 #include "base/debug/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/layer_sorter.h"
18 #include "cc/trees/layer_tree_impl.h"
19 #include "ui/gfx/rect_conversions.h"
20 #include "ui/gfx/transform.h"
22 namespace cc {
24 ScrollAndScaleSet::ScrollAndScaleSet() {}
26 ScrollAndScaleSet::~ScrollAndScaleSet() {}
28 static void SortLayers(LayerList::iterator forst,
29 LayerList::iterator end,
30 void* layer_sorter) {
31 NOTREACHED();
34 static void SortLayers(LayerImplList::iterator first,
35 LayerImplList::iterator end,
36 LayerSorter* layer_sorter) {
37 DCHECK(layer_sorter);
38 TRACE_EVENT0("cc", "LayerTreeHostCommon::SortLayers");
39 layer_sorter->Sort(first, end);
42 template <typename LayerType>
43 static gfx::Vector2dF GetEffectiveScrollDelta(LayerType* layer) {
44 gfx::Vector2dF scroll_delta = layer->ScrollDelta();
45 // The scroll parent's scroll delta is the amount we've scrolled on the
46 // compositor thread since the commit for this layer tree's source frame.
47 // we last reported to the main thread. I.e., it's the discrepancy between
48 // a scroll parent's scroll delta and offset, so we must add it here.
49 if (layer->scroll_parent())
50 scroll_delta += layer->scroll_parent()->ScrollDelta();
51 return scroll_delta;
54 template <typename LayerType>
55 static gfx::Vector2dF GetEffectiveTotalScrollOffset(LayerType* layer) {
56 gfx::Vector2dF offset = layer->TotalScrollOffset();
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 += layer->scroll_parent()->ScrollDelta();
62 return offset;
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())
71 return gfx::Rect();
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())
84 return gfx::Rect();
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
95 // visible.
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);
102 return layer_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();
145 return translation;
148 enum TranslateRectDirection {
149 TranslateRectDirectionToAncestor,
150 TranslateRectDirectionToDescendant
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 == TranslateRectDirectionToDescendant)
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();
178 if (!clip_parent)
179 clip_parent = layer->clip_parent();
181 if (!clip_parent || clip_parent == layer->parent())
182 return;
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,
200 *layer->parent(),
201 *clip_rect_in_parent_target_space,
202 TranslateRectDirectionToDescendant);
203 } else {
204 // If we're being clipped by our scroll parent, we must translate through
205 // our common ancestor. This happens to be our parent, so it is sufficent to
206 // translate from our clip parent's space to the space of its ancestor (our
207 // parent).
208 *clip_rect_in_parent_target_space =
209 TranslateRectToTargetSpace<LayerType>(*layer->parent(),
210 *clip_parent,
211 *clip_rect_in_parent_target_space,
212 TranslateRectDirectionToAncestor);
216 // We collect an accumulated drawable content rect per render surface.
217 // Typically, a layer will contribute to only one surface, the surface
218 // associated with its render target. Clip children, however, may affect
219 // several surfaces since there may be several surfaces between the clip child
220 // and its parent.
222 // NB: we accumulate the layer's *clipped* drawable content rect.
223 template <typename LayerType>
224 struct AccumulatedSurfaceState {
225 explicit AccumulatedSurfaceState(LayerType* render_target)
226 : render_target(render_target) {}
228 // The accumulated drawable content rect for the surface associated with the
229 // given |render_target|.
230 gfx::Rect drawable_content_rect;
232 // The target owning the surface. (We hang onto the target rather than the
233 // surface so that we can DCHECK that the surface's draw transform is simply
234 // a translation when |render_target| reports that it has no unclipped
235 // descendants).
236 LayerType* render_target;
239 template <typename LayerType>
240 void UpdateAccumulatedSurfaceState(
241 LayerType* layer,
242 const gfx::Rect& drawable_content_rect,
243 std::vector<AccumulatedSurfaceState<LayerType> >*
244 accumulated_surface_state) {
245 if (IsRootLayer(layer))
246 return;
248 // We will apply our drawable content rect to the accumulated rects for all
249 // surfaces between us and |render_target| (inclusive). This is either our
250 // clip parent's target if we are a clip child, or else simply our parent's
251 // target. We use our parent's target because we're either the owner of a
252 // render surface and we'll want to add our rect to our *surface's* target, or
253 // we're not and our target is the same as our parent's. In both cases, the
254 // parent's target gives us what we want.
255 LayerType* render_target = layer->clip_parent()
256 ? layer->clip_parent()->render_target()
257 : layer->parent()->render_target();
259 // If the layer owns a surface, then the content rect is in the wrong space.
260 // Instead, we will use the surface's DrawableContentRect which is in target
261 // space as required.
262 gfx::Rect target_rect = drawable_content_rect;
263 if (layer->render_surface()) {
264 target_rect =
265 gfx::ToEnclosedRect(layer->render_surface()->DrawableContentRect());
268 if (render_target->is_clipped()) {
269 gfx::Rect clip_rect = render_target->clip_rect();
270 // If the layer has a clip parent, the clip rect may be in the wrong space,
271 // so we'll need to transform it before it is applied.
272 if (layer->clip_parent()) {
273 clip_rect = TranslateRectToTargetSpace<LayerType>(
274 *layer->clip_parent(),
275 *layer,
276 clip_rect,
277 TranslateRectDirectionToDescendant);
279 target_rect.Intersect(clip_rect);
282 // We must have at least one entry in the vector for the root.
283 DCHECK_LT(0ul, accumulated_surface_state->size());
285 typedef typename std::vector<AccumulatedSurfaceState<LayerType> >
286 AccumulatedSurfaceStateVector;
287 typedef typename AccumulatedSurfaceStateVector::reverse_iterator
288 AccumulatedSurfaceStateIterator;
289 AccumulatedSurfaceStateIterator current_state =
290 accumulated_surface_state->rbegin();
292 // Add this rect to the accumulated content rect for all surfaces until we
293 // reach the target surface.
294 bool found_render_target = false;
295 for (; current_state != accumulated_surface_state->rend(); ++current_state) {
296 current_state->drawable_content_rect.Union(target_rect);
298 // If we've reached |render_target| our work is done and we can bail.
299 if (current_state->render_target == render_target) {
300 found_render_target = true;
301 break;
304 // Transform rect from the current target's space to the next.
305 LayerType* current_target = current_state->render_target;
306 DCHECK(current_target->render_surface());
307 const gfx::Transform& current_draw_transform =
308 current_target->render_surface()->draw_transform();
310 // If we have unclipped descendants, the draw transform is a translation.
311 DCHECK(current_target->num_unclipped_descendants() == 0 ||
312 current_draw_transform.IsIdentityOrTranslation());
314 target_rect = gfx::ToEnclosingRect(
315 MathUtil::MapClippedRect(current_draw_transform, target_rect));
318 // It is an error to not reach |render_target|. If this happens, it means that
319 // either the clip parent is not an ancestor of the clip child or the surface
320 // state vector is empty, both of which should be impossible.
321 DCHECK(found_render_target);
324 template <typename LayerType> static inline bool IsRootLayer(LayerType* layer) {
325 return !layer->parent();
328 template <typename LayerType>
329 static inline bool LayerIsInExisting3DRenderingContext(LayerType* layer) {
330 return layer->Is3dSorted() && layer->parent() &&
331 layer->parent()->Is3dSorted();
334 template <typename LayerType>
335 static bool IsRootLayerOfNewRenderingContext(LayerType* layer) {
336 if (layer->parent())
337 return !layer->parent()->Is3dSorted() && layer->Is3dSorted();
339 return layer->Is3dSorted();
342 template <typename LayerType>
343 static bool IsLayerBackFaceVisible(LayerType* layer) {
344 // The current W3C spec on CSS transforms says that backface visibility should
345 // be determined differently depending on whether the layer is in a "3d
346 // rendering context" or not. For Chromium code, we can determine whether we
347 // are in a 3d rendering context by checking if the parent preserves 3d.
349 if (LayerIsInExisting3DRenderingContext(layer))
350 return layer->draw_transform().IsBackFaceVisible();
352 // In this case, either the layer establishes a new 3d rendering context, or
353 // is not in a 3d rendering context at all.
354 return layer->transform().IsBackFaceVisible();
357 template <typename LayerType>
358 static bool IsSurfaceBackFaceVisible(LayerType* layer,
359 const gfx::Transform& draw_transform) {
360 if (LayerIsInExisting3DRenderingContext(layer))
361 return draw_transform.IsBackFaceVisible();
363 if (IsRootLayerOfNewRenderingContext(layer))
364 return layer->transform().IsBackFaceVisible();
366 // If the render_surface is not part of a new or existing rendering context,
367 // then the layers that contribute to this surface will decide back-face
368 // visibility for themselves.
369 return false;
372 template <typename LayerType>
373 static inline bool LayerClipsSubtree(LayerType* layer) {
374 return layer->masks_to_bounds() || layer->mask_layer();
377 template <typename LayerType>
378 static gfx::Rect CalculateVisibleContentRect(
379 LayerType* layer,
380 const gfx::Rect& clip_rect_of_target_surface_in_target_space,
381 const gfx::Rect& layer_rect_in_target_space) {
382 DCHECK(layer->render_target());
384 // Nothing is visible if the layer bounds are empty.
385 if (!layer->DrawsContent() || layer->content_bounds().IsEmpty() ||
386 layer->drawable_content_rect().IsEmpty())
387 return gfx::Rect();
389 // Compute visible bounds in target surface space.
390 gfx::Rect visible_rect_in_target_surface_space =
391 layer->drawable_content_rect();
393 if (!layer->render_target()->render_surface()->clip_rect().IsEmpty()) {
394 // The |layer| L has a target T which owns a surface Ts. The surface Ts
395 // has a target TsT.
397 // In this case the target surface Ts does clip the layer L that contributes
398 // to it. So, we have to convert the clip rect of Ts from the target space
399 // of Ts (that is the space of TsT), to the current render target's space
400 // (that is the space of T). This conversion is done outside this function
401 // so that it can be cached instead of computing it redundantly for every
402 // layer.
403 visible_rect_in_target_surface_space.Intersect(
404 clip_rect_of_target_surface_in_target_space);
407 if (visible_rect_in_target_surface_space.IsEmpty())
408 return gfx::Rect();
410 return CalculateVisibleRectWithCachedLayerRect(
411 visible_rect_in_target_surface_space,
412 gfx::Rect(layer->content_bounds()),
413 layer_rect_in_target_space,
414 layer->draw_transform());
417 static inline bool TransformToParentIsKnown(LayerImpl* layer) { return true; }
419 static inline bool TransformToParentIsKnown(Layer* layer) {
420 return !layer->TransformIsAnimating();
423 static inline bool TransformToScreenIsKnown(LayerImpl* layer) { return true; }
425 static inline bool TransformToScreenIsKnown(Layer* layer) {
426 return !layer->screen_space_transform_is_animating();
429 template <typename LayerType>
430 static bool LayerShouldBeSkipped(LayerType* layer, bool layer_is_drawn) {
431 // Layers can be skipped if any of these conditions are met.
432 // - is not drawn due to it or one of its ancestors being hidden (or having
433 // no copy requests).
434 // - does not draw content.
435 // - is transparent.
436 // - has empty bounds
437 // - the layer is not double-sided, but its back face is visible.
439 // Some additional conditions need to be computed at a later point after the
440 // recursion is finished.
441 // - the intersection of render_surface content and layer clip_rect is empty
442 // - the visible_content_rect is empty
444 // Note, if the layer should not have been drawn due to being fully
445 // transparent, we would have skipped the entire subtree and never made it
446 // into this function, so it is safe to omit this check here.
448 if (!layer_is_drawn)
449 return true;
451 if (!layer->DrawsContent() || layer->bounds().IsEmpty())
452 return true;
454 LayerType* backface_test_layer = layer;
455 if (layer->use_parent_backface_visibility()) {
456 DCHECK(layer->parent());
457 DCHECK(!layer->parent()->use_parent_backface_visibility());
458 backface_test_layer = layer->parent();
461 // The layer should not be drawn if (1) it is not double-sided and (2) the
462 // back of the layer is known to be facing the screen.
463 if (!backface_test_layer->double_sided() &&
464 TransformToScreenIsKnown(backface_test_layer) &&
465 IsLayerBackFaceVisible(backface_test_layer))
466 return true;
468 return false;
471 template <typename LayerType>
472 static bool HasInvertibleOrAnimatedTransform(LayerType* layer) {
473 return layer->transform_is_invertible() || layer->TransformIsAnimating();
476 static inline bool SubtreeShouldBeSkipped(LayerImpl* layer,
477 bool layer_is_drawn) {
478 // If the layer transform is not invertible, it should not be drawn.
479 // TODO(ajuma): Correctly process subtrees with singular transform for the
480 // case where we may animate to a non-singular transform and wish to
481 // pre-raster.
482 if (!HasInvertibleOrAnimatedTransform(layer))
483 return true;
485 // When we need to do a readback/copy of a layer's output, we can not skip
486 // it or any of its ancestors.
487 if (layer->draw_properties().layer_or_descendant_has_copy_request)
488 return false;
490 // We cannot skip the the subtree if a descendant has a wheel or touch handler
491 // or the hit testing code will break (it requires fresh transforms, etc).
492 if (layer->draw_properties().layer_or_descendant_has_input_handler)
493 return false;
495 // If the layer is not drawn, then skip it and its subtree.
496 if (!layer_is_drawn)
497 return true;
499 // If layer is on the pending tree and opacity is being animated then
500 // this subtree can't be skipped as we need to create, prioritize and
501 // include tiles for this layer when deciding if tree can be activated.
502 if (layer->layer_tree_impl()->IsPendingTree() && layer->OpacityIsAnimating())
503 return false;
505 // The opacity of a layer always applies to its children (either implicitly
506 // via a render surface or explicitly if the parent preserves 3D), so the
507 // entire subtree can be skipped if this layer is fully transparent.
508 return !layer->opacity();
511 static inline bool SubtreeShouldBeSkipped(Layer* layer, bool layer_is_drawn) {
512 // If the layer transform is not invertible, it should not be drawn.
513 if (!layer->transform_is_invertible() && !layer->TransformIsAnimating())
514 return true;
516 // When we need to do a readback/copy of a layer's output, we can not skip
517 // it or any of its ancestors.
518 if (layer->draw_properties().layer_or_descendant_has_copy_request)
519 return false;
521 // We cannot skip the the subtree if a descendant has a wheel or touch handler
522 // or the hit testing code will break (it requires fresh transforms, etc).
523 if (layer->draw_properties().layer_or_descendant_has_input_handler)
524 return false;
526 // If the layer is not drawn, then skip it and its subtree.
527 if (!layer_is_drawn)
528 return true;
530 // If the opacity is being animated then the opacity on the main thread is
531 // unreliable (since the impl thread may be using a different opacity), so it
532 // should not be trusted.
533 // In particular, it should not cause the subtree to be skipped.
534 // Similarly, for layers that might animate opacity using an impl-only
535 // animation, their subtree should also not be skipped.
536 return !layer->opacity() && !layer->OpacityIsAnimating() &&
537 !layer->OpacityCanAnimateOnImplThread();
540 static inline void SavePaintPropertiesLayer(LayerImpl* layer) {}
542 static inline void SavePaintPropertiesLayer(Layer* layer) {
543 layer->SavePaintProperties();
545 if (layer->mask_layer())
546 layer->mask_layer()->SavePaintProperties();
547 if (layer->replica_layer() && layer->replica_layer()->mask_layer())
548 layer->replica_layer()->mask_layer()->SavePaintProperties();
551 template <typename LayerType>
552 static bool SubtreeShouldRenderToSeparateSurface(
553 LayerType* layer,
554 bool axis_aligned_with_respect_to_parent) {
556 // A layer and its descendants should render onto a new RenderSurfaceImpl if
557 // any of these rules hold:
560 // The root layer owns a render surface, but it never acts as a contributing
561 // surface to another render target. Compositor features that are applied via
562 // a contributing surface can not be applied to the root layer. In order to
563 // use these effects, another child of the root would need to be introduced
564 // in order to act as a contributing surface to the root layer's surface.
565 bool is_root = IsRootLayer(layer);
567 // If the layer uses a mask.
568 if (layer->mask_layer()) {
569 DCHECK(!is_root);
570 return true;
573 // If the layer has a reflection.
574 if (layer->replica_layer()) {
575 DCHECK(!is_root);
576 return true;
579 // If the layer uses a CSS filter.
580 if (!layer->filters().IsEmpty() || !layer->background_filters().IsEmpty()) {
581 DCHECK(!is_root);
582 return true;
585 int num_descendants_that_draw_content =
586 layer->draw_properties().num_descendants_that_draw_content;
588 // If the layer flattens its subtree, but it is treated as a 3D object by its
589 // parent (i.e. parent participates in a 3D rendering context).
590 if (LayerIsInExisting3DRenderingContext(layer) &&
591 layer->should_flatten_transform() &&
592 num_descendants_that_draw_content > 0) {
593 TRACE_EVENT_INSTANT0(
594 "cc",
595 "LayerTreeHostCommon::SubtreeShouldRenderToSeparateSurface flattening",
596 TRACE_EVENT_SCOPE_THREAD);
597 DCHECK(!is_root);
598 return true;
601 // If the layer has blending.
602 // TODO(rosca): this is temporary, until blending is implemented for other
603 // types of quads than RenderPassDrawQuad. Layers having descendants that draw
604 // content will still create a separate rendering surface.
605 if (!layer->uses_default_blend_mode()) {
606 TRACE_EVENT_INSTANT0(
607 "cc",
608 "LayerTreeHostCommon::SubtreeShouldRenderToSeparateSurface blending",
609 TRACE_EVENT_SCOPE_THREAD);
610 DCHECK(!is_root);
611 return true;
614 // If the layer clips its descendants but it is not axis-aligned with respect
615 // to its parent.
616 bool layer_clips_external_content =
617 LayerClipsSubtree(layer) || layer->HasDelegatedContent();
618 if (layer_clips_external_content && !axis_aligned_with_respect_to_parent &&
619 num_descendants_that_draw_content > 0) {
620 TRACE_EVENT_INSTANT0(
621 "cc",
622 "LayerTreeHostCommon::SubtreeShouldRenderToSeparateSurface clipping",
623 TRACE_EVENT_SCOPE_THREAD);
624 DCHECK(!is_root);
625 return true;
628 // If the layer has some translucency and does not have a preserves-3d
629 // transform style. This condition only needs a render surface if two or more
630 // layers in the subtree overlap. But checking layer overlaps is unnecessarily
631 // costly so instead we conservatively create a surface whenever at least two
632 // layers draw content for this subtree.
633 bool at_least_two_layers_in_subtree_draw_content =
634 num_descendants_that_draw_content > 0 &&
635 (layer->DrawsContent() || num_descendants_that_draw_content > 1);
637 if (layer->opacity() != 1.f && layer->should_flatten_transform() &&
638 at_least_two_layers_in_subtree_draw_content) {
639 TRACE_EVENT_INSTANT0(
640 "cc",
641 "LayerTreeHostCommon::SubtreeShouldRenderToSeparateSurface opacity",
642 TRACE_EVENT_SCOPE_THREAD);
643 DCHECK(!is_root);
644 return true;
647 // The root layer should always have a render_surface.
648 if (is_root)
649 return true;
652 // These are allowed on the root surface, as they don't require the surface to
653 // be used as a contributing surface in order to apply correctly.
656 // If the layer has isolation.
657 // TODO(rosca): to be optimized - create separate rendering surface only when
658 // the blending descendants might have access to the content behind this layer
659 // (layer has transparent background or descendants overflow).
660 // https://code.google.com/p/chromium/issues/detail?id=301738
661 if (layer->is_root_for_isolated_group()) {
662 TRACE_EVENT_INSTANT0(
663 "cc",
664 "LayerTreeHostCommon::SubtreeShouldRenderToSeparateSurface isolation",
665 TRACE_EVENT_SCOPE_THREAD);
666 return true;
669 // If we force it.
670 if (layer->force_render_surface())
671 return true;
673 // If we'll make a copy of the layer's contents.
674 if (layer->HasCopyRequest())
675 return true;
677 return false;
680 // This function returns a translation matrix that can be applied on a vector
681 // that's in the layer's target surface coordinate, while the position offset is
682 // specified in some ancestor layer's coordinate.
683 gfx::Transform ComputeSizeDeltaCompensation(
684 LayerImpl* layer,
685 LayerImpl* container,
686 const gfx::Vector2dF& position_offset) {
687 gfx::Transform result_transform;
689 // To apply a translate in the container's layer space,
690 // the following steps need to be done:
691 // Step 1a. transform from target surface space to the container's target
692 // surface space
693 // Step 1b. transform from container's target surface space to the
694 // container's layer space
695 // Step 2. apply the compensation
696 // Step 3. transform back to target surface space
698 gfx::Transform target_surface_space_to_container_layer_space;
699 // Calculate step 1a
700 LayerImpl* container_target_surface = container->render_target();
701 for (LayerImpl* current_target_surface = NextTargetSurface(layer);
702 current_target_surface &&
703 current_target_surface != container_target_surface;
704 current_target_surface = NextTargetSurface(current_target_surface)) {
705 // Note: Concat is used here to convert the result coordinate space from
706 // current render surface to the next render surface.
707 target_surface_space_to_container_layer_space.ConcatTransform(
708 current_target_surface->render_surface()->draw_transform());
710 // Calculate step 1b
711 gfx::Transform container_layer_space_to_container_target_surface_space =
712 container->draw_transform();
713 container_layer_space_to_container_target_surface_space.Scale(
714 container->contents_scale_x(), container->contents_scale_y());
716 gfx::Transform container_target_surface_space_to_container_layer_space;
717 if (container_layer_space_to_container_target_surface_space.GetInverse(
718 &container_target_surface_space_to_container_layer_space)) {
719 // Note: Again, Concat is used to conver the result coordinate space from
720 // the container render surface to the container layer.
721 target_surface_space_to_container_layer_space.ConcatTransform(
722 container_target_surface_space_to_container_layer_space);
725 // Apply step 3
726 gfx::Transform container_layer_space_to_target_surface_space;
727 if (target_surface_space_to_container_layer_space.GetInverse(
728 &container_layer_space_to_target_surface_space)) {
729 result_transform.PreconcatTransform(
730 container_layer_space_to_target_surface_space);
731 } else {
732 // TODO(shawnsingh): A non-invertible matrix could still make meaningful
733 // projection. For example ScaleZ(0) is non-invertible but the layer is
734 // still visible.
735 return gfx::Transform();
738 // Apply step 2
739 result_transform.Translate(position_offset.x(), position_offset.y());
741 // Apply step 1
742 result_transform.PreconcatTransform(
743 target_surface_space_to_container_layer_space);
745 return result_transform;
748 void ApplyPositionAdjustment(
749 Layer* layer,
750 Layer* container,
751 const gfx::Transform& scroll_compensation,
752 gfx::Transform* combined_transform) {}
753 void ApplyPositionAdjustment(
754 LayerImpl* layer,
755 LayerImpl* container,
756 const gfx::Transform& scroll_compensation,
757 gfx::Transform* combined_transform) {
758 if (!layer->position_constraint().is_fixed_position())
759 return;
761 // Special case: this layer is a composited fixed-position layer; we need to
762 // explicitly compensate for all ancestors' nonzero scroll_deltas to keep
763 // this layer fixed correctly.
764 // Note carefully: this is Concat, not Preconcat
765 // (current_scroll_compensation * combined_transform).
766 combined_transform->ConcatTransform(scroll_compensation);
768 // For right-edge or bottom-edge anchored fixed position layers,
769 // the layer should relocate itself if the container changes its size.
770 bool fixed_to_right_edge =
771 layer->position_constraint().is_fixed_to_right_edge();
772 bool fixed_to_bottom_edge =
773 layer->position_constraint().is_fixed_to_bottom_edge();
774 gfx::Vector2dF position_offset = container->FixedContainerSizeDelta();
775 position_offset.set_x(fixed_to_right_edge ? position_offset.x() : 0);
776 position_offset.set_y(fixed_to_bottom_edge ? position_offset.y() : 0);
777 if (position_offset.IsZero())
778 return;
780 // Note: Again, this is Concat. The compensation matrix will be applied on
781 // the vector in target surface space.
782 combined_transform->ConcatTransform(
783 ComputeSizeDeltaCompensation(layer, container, position_offset));
786 gfx::Transform ComputeScrollCompensationForThisLayer(
787 LayerImpl* scrolling_layer,
788 const gfx::Transform& parent_matrix,
789 const gfx::Vector2dF& scroll_delta) {
790 // For every layer that has non-zero scroll_delta, we have to compute a
791 // transform that can undo the scroll_delta translation. In particular, we
792 // want this matrix to premultiply a fixed-position layer's parent_matrix, so
793 // we design this transform in three steps as follows. The steps described
794 // here apply from right-to-left, so Step 1 would be the right-most matrix:
796 // Step 1. transform from target surface space to the exact space where
797 // scroll_delta is actually applied.
798 // -- this is inverse of parent_matrix
799 // Step 2. undo the scroll_delta
800 // -- this is just a translation by scroll_delta.
801 // Step 3. transform back to target surface space.
802 // -- this transform is the parent_matrix
804 // These steps create a matrix that both start and end in target surface
805 // space. So this matrix can pre-multiply any fixed-position layer's
806 // draw_transform to undo the scroll_deltas -- as long as that fixed position
807 // layer is fixed onto the same render_target as this scrolling_layer.
810 gfx::Transform scroll_compensation_for_this_layer = parent_matrix; // Step 3
811 scroll_compensation_for_this_layer.Translate(
812 scroll_delta.x(),
813 scroll_delta.y()); // Step 2
815 gfx::Transform inverse_parent_matrix(gfx::Transform::kSkipInitialization);
816 if (!parent_matrix.GetInverse(&inverse_parent_matrix)) {
817 // TODO(shawnsingh): Either we need to handle uninvertible transforms
818 // here, or DCHECK that the transform is invertible.
820 scroll_compensation_for_this_layer.PreconcatTransform(
821 inverse_parent_matrix); // Step 1
822 return scroll_compensation_for_this_layer;
825 gfx::Transform ComputeScrollCompensationMatrixForChildren(
826 Layer* current_layer,
827 const gfx::Transform& current_parent_matrix,
828 const gfx::Transform& current_scroll_compensation,
829 const gfx::Vector2dF& scroll_delta) {
830 // The main thread (i.e. Layer) does not need to worry about scroll
831 // compensation. So we can just return an identity matrix here.
832 return gfx::Transform();
835 gfx::Transform ComputeScrollCompensationMatrixForChildren(
836 LayerImpl* layer,
837 const gfx::Transform& parent_matrix,
838 const gfx::Transform& current_scroll_compensation_matrix,
839 const gfx::Vector2dF& scroll_delta) {
840 // "Total scroll compensation" is the transform needed to cancel out all
841 // scroll_delta translations that occurred since the nearest container layer,
842 // even if there are render_surfaces in-between.
844 // There are some edge cases to be aware of, that are not explicit in the
845 // code:
846 // - A layer that is both a fixed-position and container should not be its
847 // own container, instead, that means it is fixed to an ancestor, and is a
848 // container for any fixed-position descendants.
849 // - A layer that is a fixed-position container and has a render_surface
850 // should behave the same as a container without a render_surface, the
851 // render_surface is irrelevant in that case.
852 // - A layer that does not have an explicit container is simply fixed to the
853 // viewport. (i.e. the root render_surface.)
854 // - If the fixed-position layer has its own render_surface, then the
855 // render_surface is the one who gets fixed.
857 // This function needs to be called AFTER layers create their own
858 // render_surfaces.
861 // Scroll compensation restarts from identity under two possible conditions:
862 // - the current layer is a container for fixed-position descendants
863 // - the current layer is fixed-position itself, so any fixed-position
864 // descendants are positioned with respect to this layer. Thus, any
865 // fixed position descendants only need to compensate for scrollDeltas
866 // that occur below this layer.
867 bool current_layer_resets_scroll_compensation_for_descendants =
868 layer->IsContainerForFixedPositionLayers() ||
869 layer->position_constraint().is_fixed_position();
871 // Avoid the overheads (including stack allocation and matrix
872 // initialization/copy) if we know that the scroll compensation doesn't need
873 // to be reset or adjusted.
874 if (!current_layer_resets_scroll_compensation_for_descendants &&
875 scroll_delta.IsZero() && !layer->render_surface())
876 return current_scroll_compensation_matrix;
878 // Start as identity matrix.
879 gfx::Transform next_scroll_compensation_matrix;
881 // If this layer does not reset scroll compensation, then it inherits the
882 // existing scroll compensations.
883 if (!current_layer_resets_scroll_compensation_for_descendants)
884 next_scroll_compensation_matrix = current_scroll_compensation_matrix;
886 // If the current layer has a non-zero scroll_delta, then we should compute
887 // its local scroll compensation and accumulate it to the
888 // next_scroll_compensation_matrix.
889 if (!scroll_delta.IsZero()) {
890 gfx::Transform scroll_compensation_for_this_layer =
891 ComputeScrollCompensationForThisLayer(
892 layer, parent_matrix, scroll_delta);
893 next_scroll_compensation_matrix.PreconcatTransform(
894 scroll_compensation_for_this_layer);
897 // If the layer created its own render_surface, we have to adjust
898 // next_scroll_compensation_matrix. The adjustment allows us to continue
899 // using the scroll compensation on the next surface.
900 // Step 1 (right-most in the math): transform from the new surface to the
901 // original ancestor surface
902 // Step 2: apply the scroll compensation
903 // Step 3: transform back to the new surface.
904 if (layer->render_surface() &&
905 !next_scroll_compensation_matrix.IsIdentity()) {
906 gfx::Transform inverse_surface_draw_transform(
907 gfx::Transform::kSkipInitialization);
908 if (!layer->render_surface()->draw_transform().GetInverse(
909 &inverse_surface_draw_transform)) {
910 // TODO(shawnsingh): Either we need to handle uninvertible transforms
911 // here, or DCHECK that the transform is invertible.
913 next_scroll_compensation_matrix =
914 inverse_surface_draw_transform * next_scroll_compensation_matrix *
915 layer->render_surface()->draw_transform();
918 return next_scroll_compensation_matrix;
921 template <typename LayerType>
922 static inline void UpdateLayerScaleDrawProperties(
923 LayerType* layer,
924 float ideal_contents_scale,
925 float maximum_animation_contents_scale,
926 float page_scale_factor,
927 float device_scale_factor) {
928 layer->draw_properties().ideal_contents_scale = ideal_contents_scale;
929 layer->draw_properties().maximum_animation_contents_scale =
930 maximum_animation_contents_scale;
931 layer->draw_properties().page_scale_factor = page_scale_factor;
932 layer->draw_properties().device_scale_factor = device_scale_factor;
935 static inline void CalculateContentsScale(
936 LayerImpl* layer,
937 float contents_scale,
938 float device_scale_factor,
939 float page_scale_factor,
940 float maximum_animation_contents_scale,
941 bool animating_transform_to_screen) {
942 // LayerImpl has all of its content scales and bounds pushed from the Main
943 // thread during commit and just uses those values as-is.
946 static inline void CalculateContentsScale(
947 Layer* layer,
948 float contents_scale,
949 float device_scale_factor,
950 float page_scale_factor,
951 float maximum_animation_contents_scale,
952 bool animating_transform_to_screen) {
953 layer->CalculateContentsScale(contents_scale,
954 device_scale_factor,
955 page_scale_factor,
956 maximum_animation_contents_scale,
957 animating_transform_to_screen,
958 &layer->draw_properties().contents_scale_x,
959 &layer->draw_properties().contents_scale_y,
960 &layer->draw_properties().content_bounds);
962 Layer* mask_layer = layer->mask_layer();
963 if (mask_layer) {
964 mask_layer->CalculateContentsScale(
965 contents_scale,
966 device_scale_factor,
967 page_scale_factor,
968 maximum_animation_contents_scale,
969 animating_transform_to_screen,
970 &mask_layer->draw_properties().contents_scale_x,
971 &mask_layer->draw_properties().contents_scale_y,
972 &mask_layer->draw_properties().content_bounds);
975 Layer* replica_mask_layer =
976 layer->replica_layer() ? layer->replica_layer()->mask_layer() : NULL;
977 if (replica_mask_layer) {
978 replica_mask_layer->CalculateContentsScale(
979 contents_scale,
980 device_scale_factor,
981 page_scale_factor,
982 maximum_animation_contents_scale,
983 animating_transform_to_screen,
984 &replica_mask_layer->draw_properties().contents_scale_x,
985 &replica_mask_layer->draw_properties().contents_scale_y,
986 &replica_mask_layer->draw_properties().content_bounds);
990 static inline void UpdateLayerContentsScale(
991 LayerImpl* layer,
992 bool can_adjust_raster_scale,
993 float ideal_contents_scale,
994 float device_scale_factor,
995 float page_scale_factor,
996 float maximum_animation_contents_scale,
997 bool animating_transform_to_screen) {
998 CalculateContentsScale(layer,
999 ideal_contents_scale,
1000 device_scale_factor,
1001 page_scale_factor,
1002 maximum_animation_contents_scale,
1003 animating_transform_to_screen);
1006 static inline void UpdateLayerContentsScale(
1007 Layer* layer,
1008 bool can_adjust_raster_scale,
1009 float ideal_contents_scale,
1010 float device_scale_factor,
1011 float page_scale_factor,
1012 float maximum_animation_contents_scale,
1013 bool animating_transform_to_screen) {
1014 if (can_adjust_raster_scale) {
1015 float ideal_raster_scale =
1016 ideal_contents_scale / (device_scale_factor * page_scale_factor);
1018 bool need_to_set_raster_scale = layer->raster_scale_is_unknown();
1020 // If we've previously saved a raster_scale but the ideal changes, things
1021 // are unpredictable and we should just use 1.
1022 if (!need_to_set_raster_scale && layer->raster_scale() != 1.f &&
1023 ideal_raster_scale != layer->raster_scale()) {
1024 ideal_raster_scale = 1.f;
1025 need_to_set_raster_scale = true;
1028 if (need_to_set_raster_scale) {
1029 bool use_and_save_ideal_scale =
1030 ideal_raster_scale >= 1.f && !animating_transform_to_screen;
1031 if (use_and_save_ideal_scale)
1032 layer->set_raster_scale(ideal_raster_scale);
1036 float raster_scale = 1.f;
1037 if (!layer->raster_scale_is_unknown())
1038 raster_scale = layer->raster_scale();
1040 gfx::Size old_content_bounds = layer->content_bounds();
1041 float old_contents_scale_x = layer->contents_scale_x();
1042 float old_contents_scale_y = layer->contents_scale_y();
1044 float contents_scale = raster_scale * device_scale_factor * page_scale_factor;
1045 CalculateContentsScale(layer,
1046 contents_scale,
1047 device_scale_factor,
1048 page_scale_factor,
1049 maximum_animation_contents_scale,
1050 animating_transform_to_screen);
1052 if (layer->content_bounds() != old_content_bounds ||
1053 layer->contents_scale_x() != old_contents_scale_x ||
1054 layer->contents_scale_y() != old_contents_scale_y)
1055 layer->SetNeedsPushProperties();
1058 static inline void CalculateAnimationContentsScale(
1059 Layer* layer,
1060 bool ancestor_is_animating_scale,
1061 float ancestor_maximum_animation_contents_scale,
1062 const gfx::Transform& parent_transform,
1063 const gfx::Transform& combined_transform,
1064 bool* combined_is_animating_scale,
1065 float* combined_maximum_animation_contents_scale) {
1066 *combined_is_animating_scale = false;
1067 *combined_maximum_animation_contents_scale = 0.f;
1070 static inline void CalculateAnimationContentsScale(
1071 LayerImpl* layer,
1072 bool ancestor_is_animating_scale,
1073 float ancestor_maximum_animation_contents_scale,
1074 const gfx::Transform& ancestor_transform,
1075 const gfx::Transform& combined_transform,
1076 bool* combined_is_animating_scale,
1077 float* combined_maximum_animation_contents_scale) {
1078 if (ancestor_is_animating_scale &&
1079 ancestor_maximum_animation_contents_scale == 0.f) {
1080 // We've already failed to compute a maximum animated scale at an
1081 // ancestor, so we'll continue to fail.
1082 *combined_maximum_animation_contents_scale = 0.f;
1083 *combined_is_animating_scale = true;
1084 return;
1087 if (!combined_transform.IsScaleOrTranslation()) {
1088 // Computing maximum animated scale in the presence of
1089 // non-scale/translation transforms isn't supported.
1090 *combined_maximum_animation_contents_scale = 0.f;
1091 *combined_is_animating_scale = true;
1092 return;
1095 // We currently only support computing maximum scale for combinations of
1096 // scales and translations. We treat all non-translations as potentially
1097 // affecting scale. Animations that include non-translation/scale components
1098 // will cause the computation of MaximumScale below to fail.
1099 bool layer_is_animating_scale =
1100 !layer->layer_animation_controller()->HasOnlyTranslationTransforms();
1102 if (!layer_is_animating_scale && !ancestor_is_animating_scale) {
1103 *combined_maximum_animation_contents_scale = 0.f;
1104 *combined_is_animating_scale = false;
1105 return;
1108 // We don't attempt to accumulate animation scale from multiple nodes,
1109 // because of the risk of significant overestimation. For example, one node
1110 // may be increasing scale from 1 to 10 at the same time as a descendant is
1111 // decreasing scale from 10 to 1. Naively combining these scales would produce
1112 // a scale of 100.
1113 if (layer_is_animating_scale && ancestor_is_animating_scale) {
1114 *combined_maximum_animation_contents_scale = 0.f;
1115 *combined_is_animating_scale = true;
1116 return;
1119 // At this point, we know either the layer or an ancestor, but not both,
1120 // is animating scale.
1121 *combined_is_animating_scale = true;
1122 if (!layer_is_animating_scale) {
1123 gfx::Vector2dF layer_transform_scales =
1124 MathUtil::ComputeTransform2dScaleComponents(layer->transform(), 0.f);
1125 *combined_maximum_animation_contents_scale =
1126 ancestor_maximum_animation_contents_scale *
1127 std::max(layer_transform_scales.x(), layer_transform_scales.y());
1128 return;
1131 float layer_maximum_animated_scale = 0.f;
1132 if (!layer->layer_animation_controller()->MaximumScale(
1133 &layer_maximum_animated_scale)) {
1134 *combined_maximum_animation_contents_scale = 0.f;
1135 return;
1137 gfx::Vector2dF ancestor_transform_scales =
1138 MathUtil::ComputeTransform2dScaleComponents(ancestor_transform, 0.f);
1139 *combined_maximum_animation_contents_scale =
1140 layer_maximum_animated_scale *
1141 std::max(ancestor_transform_scales.x(), ancestor_transform_scales.y());
1144 template <typename LayerType>
1145 static inline typename LayerType::RenderSurfaceType* CreateOrReuseRenderSurface(
1146 LayerType* layer) {
1147 if (!layer->render_surface()) {
1148 layer->CreateRenderSurface();
1149 return layer->render_surface();
1152 layer->render_surface()->ClearLayerLists();
1153 return layer->render_surface();
1156 template <typename LayerTypePtr>
1157 static inline void MarkLayerWithRenderSurfaceLayerListId(
1158 LayerTypePtr layer,
1159 int current_render_surface_layer_list_id) {
1160 layer->draw_properties().last_drawn_render_surface_layer_list_id =
1161 current_render_surface_layer_list_id;
1164 template <typename LayerTypePtr>
1165 static inline void MarkMasksWithRenderSurfaceLayerListId(
1166 LayerTypePtr layer,
1167 int current_render_surface_layer_list_id) {
1168 if (layer->mask_layer()) {
1169 MarkLayerWithRenderSurfaceLayerListId(layer->mask_layer(),
1170 current_render_surface_layer_list_id);
1172 if (layer->replica_layer() && layer->replica_layer()->mask_layer()) {
1173 MarkLayerWithRenderSurfaceLayerListId(layer->replica_layer()->mask_layer(),
1174 current_render_surface_layer_list_id);
1178 template <typename LayerListType>
1179 static inline void MarkLayerListWithRenderSurfaceLayerListId(
1180 LayerListType* layer_list,
1181 int current_render_surface_layer_list_id) {
1182 for (typename LayerListType::iterator it = layer_list->begin();
1183 it != layer_list->end();
1184 ++it) {
1185 MarkLayerWithRenderSurfaceLayerListId(*it,
1186 current_render_surface_layer_list_id);
1187 MarkMasksWithRenderSurfaceLayerListId(*it,
1188 current_render_surface_layer_list_id);
1192 template <typename LayerType>
1193 static inline void RemoveSurfaceForEarlyExit(
1194 LayerType* layer_to_remove,
1195 typename LayerType::RenderSurfaceListType* render_surface_layer_list) {
1196 DCHECK(layer_to_remove->render_surface());
1197 // Technically, we know that the layer we want to remove should be
1198 // at the back of the render_surface_layer_list. However, we have had
1199 // bugs before that added unnecessary layers here
1200 // (https://bugs.webkit.org/show_bug.cgi?id=74147), but that causes
1201 // things to crash. So here we proactively remove any additional
1202 // layers from the end of the list.
1203 while (render_surface_layer_list->back() != layer_to_remove) {
1204 MarkLayerListWithRenderSurfaceLayerListId(
1205 &render_surface_layer_list->back()->render_surface()->layer_list(), 0);
1206 MarkLayerWithRenderSurfaceLayerListId(render_surface_layer_list->back(), 0);
1208 render_surface_layer_list->back()->ClearRenderSurfaceLayerList();
1209 render_surface_layer_list->pop_back();
1211 DCHECK_EQ(render_surface_layer_list->back(), layer_to_remove);
1212 MarkLayerListWithRenderSurfaceLayerListId(
1213 &layer_to_remove->render_surface()->layer_list(), 0);
1214 MarkLayerWithRenderSurfaceLayerListId(layer_to_remove, 0);
1215 render_surface_layer_list->pop_back();
1216 layer_to_remove->ClearRenderSurfaceLayerList();
1219 struct PreCalculateMetaInformationRecursiveData {
1220 bool layer_or_descendant_has_copy_request;
1221 bool layer_or_descendant_has_input_handler;
1222 int num_unclipped_descendants;
1224 PreCalculateMetaInformationRecursiveData()
1225 : layer_or_descendant_has_copy_request(false),
1226 layer_or_descendant_has_input_handler(false),
1227 num_unclipped_descendants(0) {}
1229 void Merge(const PreCalculateMetaInformationRecursiveData& data) {
1230 layer_or_descendant_has_copy_request |=
1231 data.layer_or_descendant_has_copy_request;
1232 layer_or_descendant_has_input_handler |=
1233 data.layer_or_descendant_has_input_handler;
1234 num_unclipped_descendants +=
1235 data.num_unclipped_descendants;
1239 // Recursively walks the layer tree to compute any information that is needed
1240 // before doing the main recursion.
1241 template <typename LayerType>
1242 static void PreCalculateMetaInformation(
1243 LayerType* layer,
1244 PreCalculateMetaInformationRecursiveData* recursive_data) {
1245 bool has_delegated_content = layer->HasDelegatedContent();
1246 int num_descendants_that_draw_content = 0;
1248 layer->draw_properties().sorted_for_recursion = false;
1249 layer->draw_properties().has_child_with_a_scroll_parent = false;
1251 if (!HasInvertibleOrAnimatedTransform(layer)) {
1252 // Layers with singular transforms should not be drawn, the whole subtree
1253 // can be skipped.
1254 return;
1257 if (has_delegated_content) {
1258 // Layers with delegated content need to be treated as if they have as
1259 // many children as the number of layers they own delegated quads for.
1260 // Since we don't know this number right now, we choose one that acts like
1261 // infinity for our purposes.
1262 num_descendants_that_draw_content = 1000;
1265 if (layer->clip_parent())
1266 recursive_data->num_unclipped_descendants++;
1268 for (size_t i = 0; i < layer->children().size(); ++i) {
1269 LayerType* child_layer =
1270 LayerTreeHostCommon::get_layer_as_raw_ptr(layer->children(), i);
1272 PreCalculateMetaInformationRecursiveData data_for_child;
1273 PreCalculateMetaInformation(child_layer, &data_for_child);
1275 num_descendants_that_draw_content += child_layer->DrawsContent() ? 1 : 0;
1276 num_descendants_that_draw_content +=
1277 child_layer->draw_properties().num_descendants_that_draw_content;
1279 if (child_layer->scroll_parent())
1280 layer->draw_properties().has_child_with_a_scroll_parent = true;
1281 recursive_data->Merge(data_for_child);
1284 if (layer->clip_children()) {
1285 int num_clip_children = layer->clip_children()->size();
1286 DCHECK_GE(recursive_data->num_unclipped_descendants, num_clip_children);
1287 recursive_data->num_unclipped_descendants -= num_clip_children;
1290 if (layer->HasCopyRequest())
1291 recursive_data->layer_or_descendant_has_copy_request = true;
1293 if (!layer->touch_event_handler_region().IsEmpty() ||
1294 layer->have_wheel_event_handlers())
1295 recursive_data->layer_or_descendant_has_input_handler = true;
1297 layer->draw_properties().num_descendants_that_draw_content =
1298 num_descendants_that_draw_content;
1299 layer->draw_properties().num_unclipped_descendants =
1300 recursive_data->num_unclipped_descendants;
1301 layer->draw_properties().layer_or_descendant_has_copy_request =
1302 recursive_data->layer_or_descendant_has_copy_request;
1303 layer->draw_properties().layer_or_descendant_has_input_handler =
1304 recursive_data->layer_or_descendant_has_input_handler;
1307 static void RoundTranslationComponents(gfx::Transform* transform) {
1308 transform->matrix().set(0, 3, MathUtil::Round(transform->matrix().get(0, 3)));
1309 transform->matrix().set(1, 3, MathUtil::Round(transform->matrix().get(1, 3)));
1312 template <typename LayerType>
1313 struct SubtreeGlobals {
1314 LayerSorter* layer_sorter;
1315 int max_texture_size;
1316 float device_scale_factor;
1317 float page_scale_factor;
1318 const LayerType* page_scale_application_layer;
1319 bool can_adjust_raster_scales;
1320 bool can_render_to_separate_surface;
1323 template<typename LayerType>
1324 struct DataForRecursion {
1325 // The accumulated sequence of transforms a layer will use to determine its
1326 // own draw transform.
1327 gfx::Transform parent_matrix;
1329 // The accumulated sequence of transforms a layer will use to determine its
1330 // own screen-space transform.
1331 gfx::Transform full_hierarchy_matrix;
1333 // The transform that removes all scrolling that may have occurred between a
1334 // fixed-position layer and its container, so that the layer actually does
1335 // remain fixed.
1336 gfx::Transform scroll_compensation_matrix;
1338 // The ancestor that would be the container for any fixed-position / sticky
1339 // layers.
1340 LayerType* fixed_container;
1342 // This is the normal clip rect that is propagated from parent to child.
1343 gfx::Rect clip_rect_in_target_space;
1345 // When the layer's children want to compute their visible content rect, they
1346 // want to know what their target surface's clip rect will be. BUT - they
1347 // want to know this clip rect represented in their own target space. This
1348 // requires inverse-projecting the surface's clip rect from the surface's
1349 // render target space down to the surface's own space. Instead of computing
1350 // this value redundantly for each child layer, it is computed only once
1351 // while dealing with the parent layer, and then this precomputed value is
1352 // passed down the recursion to the children that actually use it.
1353 gfx::Rect clip_rect_of_target_surface_in_target_space;
1355 // The maximum amount by which this layer will be scaled during the lifetime
1356 // of currently running animations.
1357 float maximum_animation_contents_scale;
1359 bool ancestor_is_animating_scale;
1360 bool ancestor_clips_subtree;
1361 typename LayerType::RenderSurfaceType*
1362 nearest_occlusion_immune_ancestor_surface;
1363 bool in_subtree_of_page_scale_application_layer;
1364 bool subtree_can_use_lcd_text;
1365 bool subtree_is_visible_from_ancestor;
1368 template <typename LayerType>
1369 static LayerType* GetChildContainingLayer(const LayerType& parent,
1370 LayerType* layer) {
1371 for (LayerType* ancestor = layer; ancestor; ancestor = ancestor->parent()) {
1372 if (ancestor->parent() == &parent)
1373 return ancestor;
1375 NOTREACHED();
1376 return 0;
1379 template <typename LayerType>
1380 static void AddScrollParentChain(std::vector<LayerType*>* out,
1381 const LayerType& parent,
1382 LayerType* layer) {
1383 // At a high level, this function walks up the chain of scroll parents
1384 // recursively, and once we reach the end of the chain, we add the child
1385 // of |parent| containing each scroll ancestor as we unwind. The result is
1386 // an ordering of parent's children that ensures that scroll parents are
1387 // visited before their descendants.
1388 // Take for example this layer tree:
1390 // + stacking_context
1391 // + scroll_child (1)
1392 // + scroll_parent_graphics_layer (*)
1393 // | + scroll_parent_scrolling_layer
1394 // | + scroll_parent_scrolling_content_layer (2)
1395 // + scroll_grandparent_graphics_layer (**)
1396 // + scroll_grandparent_scrolling_layer
1397 // + scroll_grandparent_scrolling_content_layer (3)
1399 // The scroll child is (1), its scroll parent is (2) and its scroll
1400 // grandparent is (3). Note, this doesn't mean that (2)'s scroll parent is
1401 // (3), it means that (*)'s scroll parent is (3). We don't want our list to
1402 // look like [ (3), (2), (1) ], even though that does have the ancestor chain
1403 // in the right order. Instead, we want [ (**), (*), (1) ]. That is, only want
1404 // (1)'s siblings in the list, but we want them to appear in such an order
1405 // that the scroll ancestors get visited in the correct order.
1407 // So our first task at this step of the recursion is to determine the layer
1408 // that we will potentionally add to the list. That is, the child of parent
1409 // containing |layer|.
1410 LayerType* child = GetChildContainingLayer(parent, layer);
1411 if (child->draw_properties().sorted_for_recursion)
1412 return;
1414 if (LayerType* scroll_parent = child->scroll_parent())
1415 AddScrollParentChain(out, parent, scroll_parent);
1417 out->push_back(child);
1418 child->draw_properties().sorted_for_recursion = true;
1421 template <typename LayerType>
1422 static bool SortChildrenForRecursion(std::vector<LayerType*>* out,
1423 const LayerType& parent) {
1424 out->reserve(parent.children().size());
1425 bool order_changed = false;
1426 for (size_t i = 0; i < parent.children().size(); ++i) {
1427 LayerType* current =
1428 LayerTreeHostCommon::get_layer_as_raw_ptr(parent.children(), i);
1430 if (current->draw_properties().sorted_for_recursion) {
1431 order_changed = true;
1432 continue;
1435 AddScrollParentChain(out, parent, current);
1438 DCHECK_EQ(parent.children().size(), out->size());
1439 return order_changed;
1442 template <typename LayerType>
1443 static void GetNewDescendantsStartIndexAndCount(LayerType* layer,
1444 size_t* start_index,
1445 size_t* count) {
1446 *start_index = layer->draw_properties().index_of_first_descendants_addition;
1447 *count = layer->draw_properties().num_descendants_added;
1450 template <typename LayerType>
1451 static void GetNewRenderSurfacesStartIndexAndCount(LayerType* layer,
1452 size_t* start_index,
1453 size_t* count) {
1454 *start_index = layer->draw_properties()
1455 .index_of_first_render_surface_layer_list_addition;
1456 *count = layer->draw_properties().num_render_surfaces_added;
1459 // We need to extract a list from the the two flavors of RenderSurfaceListType
1460 // for use in the sorting function below.
1461 static LayerList* GetLayerListForSorting(RenderSurfaceLayerList* rsll) {
1462 return &rsll->AsLayerList();
1465 static LayerImplList* GetLayerListForSorting(LayerImplList* layer_list) {
1466 return layer_list;
1469 template <typename LayerType, typename GetIndexAndCountType>
1470 static void SortLayerListContributions(
1471 const LayerType& parent,
1472 typename LayerType::LayerListType* unsorted,
1473 size_t start_index_for_all_contributions,
1474 GetIndexAndCountType get_index_and_count) {
1475 typename LayerType::LayerListType buffer;
1476 for (size_t i = 0; i < parent.children().size(); ++i) {
1477 LayerType* child =
1478 LayerTreeHostCommon::get_layer_as_raw_ptr(parent.children(), i);
1480 size_t start_index = 0;
1481 size_t count = 0;
1482 get_index_and_count(child, &start_index, &count);
1483 for (size_t j = start_index; j < start_index + count; ++j)
1484 buffer.push_back(unsorted->at(j));
1487 DCHECK_EQ(buffer.size(),
1488 unsorted->size() - start_index_for_all_contributions);
1490 for (size_t i = 0; i < buffer.size(); ++i)
1491 (*unsorted)[i + start_index_for_all_contributions] = buffer[i];
1494 // Recursively walks the layer tree starting at the given node and computes all
1495 // the necessary transformations, clip rects, render surfaces, etc.
1496 template <typename LayerType>
1497 static void CalculateDrawPropertiesInternal(
1498 LayerType* layer,
1499 const SubtreeGlobals<LayerType>& globals,
1500 const DataForRecursion<LayerType>& data_from_ancestor,
1501 typename LayerType::RenderSurfaceListType* render_surface_layer_list,
1502 typename LayerType::LayerListType* layer_list,
1503 std::vector<AccumulatedSurfaceState<LayerType> >* accumulated_surface_state,
1504 int current_render_surface_layer_list_id) {
1505 // This function computes the new matrix transformations recursively for this
1506 // layer and all its descendants. It also computes the appropriate render
1507 // surfaces.
1508 // Some important points to remember:
1510 // 0. Here, transforms are notated in Matrix x Vector order, and in words we
1511 // describe what the transform does from left to right.
1513 // 1. In our terminology, the "layer origin" refers to the top-left corner of
1514 // a layer, and the positive Y-axis points downwards. This interpretation is
1515 // valid because the orthographic projection applied at draw time flips the Y
1516 // axis appropriately.
1518 // 2. The anchor point, when given as a PointF object, is specified in "unit
1519 // layer space", where the bounds of the layer map to [0, 1]. However, as a
1520 // Transform object, the transform to the anchor point is specified in "layer
1521 // space", where the bounds of the layer map to [bounds.width(),
1522 // bounds.height()].
1524 // 3. Definition of various transforms used:
1525 // M[parent] is the parent matrix, with respect to the nearest render
1526 // surface, passed down recursively.
1528 // M[root] is the full hierarchy, with respect to the root, passed down
1529 // recursively.
1531 // Tr[origin] is the translation matrix from the parent's origin to
1532 // this layer's origin.
1534 // Tr[origin2anchor] is the translation from the layer's origin to its
1535 // anchor point
1537 // Tr[origin2center] is the translation from the layer's origin to its
1538 // center
1540 // M[layer] is the layer's matrix (applied at the anchor point)
1542 // S[layer2content] is the ratio of a layer's content_bounds() to its
1543 // Bounds().
1545 // Some composite transforms can help in understanding the sequence of
1546 // transforms:
1547 // composite_layer_transform = Tr[origin2anchor] * M[layer] *
1548 // Tr[origin2anchor].inverse()
1550 // 4. When a layer (or render surface) is drawn, it is drawn into a "target
1551 // render surface". Therefore the draw transform does not necessarily
1552 // transform from screen space to local layer space. Instead, the draw
1553 // transform is the transform between the "target render surface space" and
1554 // local layer space. Note that render surfaces, except for the root, also
1555 // draw themselves into a different target render surface, and so their draw
1556 // transform and origin transforms are also described with respect to the
1557 // target.
1559 // Using these definitions, then:
1561 // The draw transform for the layer is:
1562 // M[draw] = M[parent] * Tr[origin] * composite_layer_transform *
1563 // S[layer2content] = M[parent] * Tr[layer->position() + anchor] *
1564 // M[layer] * Tr[anchor2origin] * S[layer2content]
1566 // Interpreting the math left-to-right, this transforms from the
1567 // layer's render surface to the origin of the layer in content space.
1569 // The screen space transform is:
1570 // M[screenspace] = M[root] * Tr[origin] * composite_layer_transform *
1571 // S[layer2content]
1572 // = M[root] * Tr[layer->position() + anchor] * M[layer]
1573 // * Tr[anchor2origin] * S[layer2content]
1575 // Interpreting the math left-to-right, this transforms from the root
1576 // render surface's content space to the origin of the layer in content
1577 // space.
1579 // The transform hierarchy that is passed on to children (i.e. the child's
1580 // parent_matrix) is:
1581 // M[parent]_for_child = M[parent] * Tr[origin] *
1582 // composite_layer_transform
1583 // = M[parent] * Tr[layer->position() + anchor] *
1584 // M[layer] * Tr[anchor2origin]
1586 // and a similar matrix for the full hierarchy with respect to the
1587 // root.
1589 // Finally, note that the final matrix used by the shader for the layer is P *
1590 // M[draw] * S . This final product is computed in drawTexturedQuad(), where:
1591 // P is the projection matrix
1592 // S is the scale adjustment (to scale up a canonical quad to the
1593 // layer's size)
1595 // When a render surface has a replica layer, that layer's transform is used
1596 // to draw a second copy of the surface. gfx::Transforms named here are
1597 // relative to the surface, unless they specify they are relative to the
1598 // replica layer.
1600 // We will denote a scale by device scale S[deviceScale]
1602 // The render surface draw transform to its target surface origin is:
1603 // M[surfaceDraw] = M[owningLayer->Draw]
1605 // The render surface origin transform to its the root (screen space) origin
1606 // is:
1607 // M[surface2root] = M[owningLayer->screenspace] *
1608 // S[deviceScale].inverse()
1610 // The replica draw transform to its target surface origin is:
1611 // M[replicaDraw] = S[deviceScale] * M[surfaceDraw] *
1612 // Tr[replica->position() + replica->anchor()] * Tr[replica] *
1613 // Tr[origin2anchor].inverse() * S[contents_scale].inverse()
1615 // The replica draw transform to the root (screen space) origin is:
1616 // M[replica2root] = M[surface2root] * Tr[replica->position()] *
1617 // Tr[replica] * Tr[origin2anchor].inverse()
1620 // It makes no sense to have a non-unit page_scale_factor without specifying
1621 // which layer roots the subtree the scale is applied to.
1622 DCHECK(globals.page_scale_application_layer ||
1623 (globals.page_scale_factor == 1.f));
1625 DataForRecursion<LayerType> data_for_children;
1626 typename LayerType::RenderSurfaceType*
1627 nearest_occlusion_immune_ancestor_surface =
1628 data_from_ancestor.nearest_occlusion_immune_ancestor_surface;
1629 data_for_children.in_subtree_of_page_scale_application_layer =
1630 data_from_ancestor.in_subtree_of_page_scale_application_layer;
1631 data_for_children.subtree_can_use_lcd_text =
1632 data_from_ancestor.subtree_can_use_lcd_text;
1634 // Layers that are marked as hidden will hide themselves and their subtree.
1635 // Exception: Layers with copy requests, whether hidden or not, must be drawn
1636 // anyway. In this case, we will inform their subtree they are visible to get
1637 // the right results.
1638 const bool layer_is_visible =
1639 data_from_ancestor.subtree_is_visible_from_ancestor &&
1640 !layer->hide_layer_and_subtree();
1641 const bool layer_is_drawn = layer_is_visible || layer->HasCopyRequest();
1643 // The root layer cannot skip CalcDrawProperties.
1644 if (!IsRootLayer(layer) && SubtreeShouldBeSkipped(layer, layer_is_drawn)) {
1645 if (layer->render_surface())
1646 layer->ClearRenderSurfaceLayerList();
1647 return;
1650 // We need to circumvent the normal recursive flow of information for clip
1651 // children (they don't inherit their direct ancestor's clip information).
1652 // This is unfortunate, and would be unnecessary if we were to formally
1653 // separate the clipping hierarchy from the layer hierarchy.
1654 bool ancestor_clips_subtree = data_from_ancestor.ancestor_clips_subtree;
1655 gfx::Rect ancestor_clip_rect_in_target_space =
1656 data_from_ancestor.clip_rect_in_target_space;
1658 // Update our clipping state. If we have a clip parent we will need to pull
1659 // from the clip state cache rather than using the clip state passed from our
1660 // immediate ancestor.
1661 UpdateClipRectsForClipChild<LayerType>(
1662 layer, &ancestor_clip_rect_in_target_space, &ancestor_clips_subtree);
1664 // As this function proceeds, these are the properties for the current
1665 // layer that actually get computed. To avoid unnecessary copies
1666 // (particularly for matrices), we do computations directly on these values
1667 // when possible.
1668 DrawProperties<LayerType>& layer_draw_properties = layer->draw_properties();
1670 gfx::Rect clip_rect_in_target_space;
1671 bool layer_or_ancestor_clips_descendants = false;
1673 // This value is cached on the stack so that we don't have to inverse-project
1674 // the surface's clip rect redundantly for every layer. This value is the
1675 // same as the target surface's clip rect, except that instead of being
1676 // described in the target surface's target's space, it is described in the
1677 // current render target's space.
1678 gfx::Rect clip_rect_of_target_surface_in_target_space;
1680 float accumulated_draw_opacity = layer->opacity();
1681 bool animating_opacity_to_target = layer->OpacityIsAnimating();
1682 bool animating_opacity_to_screen = animating_opacity_to_target;
1683 if (layer->parent()) {
1684 accumulated_draw_opacity *= layer->parent()->draw_opacity();
1685 animating_opacity_to_target |= layer->parent()->draw_opacity_is_animating();
1686 animating_opacity_to_screen |=
1687 layer->parent()->screen_space_opacity_is_animating();
1690 bool animating_transform_to_target = layer->TransformIsAnimating();
1691 bool animating_transform_to_screen = animating_transform_to_target;
1692 if (layer->parent()) {
1693 animating_transform_to_target |=
1694 layer->parent()->draw_transform_is_animating();
1695 animating_transform_to_screen |=
1696 layer->parent()->screen_space_transform_is_animating();
1698 gfx::Point3F transform_origin = layer->transform_origin();
1699 gfx::Vector2dF scroll_offset = GetEffectiveTotalScrollOffset(layer);
1700 gfx::PointF position = layer->position() - scroll_offset;
1701 gfx::Transform combined_transform = data_from_ancestor.parent_matrix;
1702 if (!layer->transform().IsIdentity()) {
1703 // LT = Tr[origin] * Tr[origin2transformOrigin]
1704 combined_transform.Translate3d(position.x() + transform_origin.x(),
1705 position.y() + transform_origin.y(),
1706 transform_origin.z());
1707 // LT = Tr[origin] * Tr[origin2origin] * M[layer]
1708 combined_transform.PreconcatTransform(layer->transform());
1709 // LT = Tr[origin] * Tr[origin2origin] * M[layer] *
1710 // Tr[transformOrigin2origin]
1711 combined_transform.Translate3d(
1712 -transform_origin.x(), -transform_origin.y(), -transform_origin.z());
1713 } else {
1714 combined_transform.Translate(position.x(), position.y());
1717 gfx::Vector2dF effective_scroll_delta = GetEffectiveScrollDelta(layer);
1718 if (!animating_transform_to_target && layer->scrollable() &&
1719 combined_transform.IsScaleOrTranslation()) {
1720 // Align the scrollable layer's position to screen space pixels to avoid
1721 // blurriness. To avoid side-effects, do this only if the transform is
1722 // simple.
1723 gfx::Vector2dF previous_translation = combined_transform.To2dTranslation();
1724 RoundTranslationComponents(&combined_transform);
1725 gfx::Vector2dF current_translation = combined_transform.To2dTranslation();
1727 // This rounding changes the scroll delta, and so must be included
1728 // in the scroll compensation matrix. The scaling converts from physical
1729 // coordinates to the scroll delta's CSS coordinates (using the parent
1730 // matrix instead of combined transform since scrolling is applied before
1731 // the layer's transform). For example, if we have a total scale factor of
1732 // 3.0, then 1 physical pixel is only 1/3 of a CSS pixel.
1733 gfx::Vector2dF parent_scales = MathUtil::ComputeTransform2dScaleComponents(
1734 data_from_ancestor.parent_matrix, 1.f);
1735 effective_scroll_delta -=
1736 gfx::ScaleVector2d(current_translation - previous_translation,
1737 1.f / parent_scales.x(),
1738 1.f / parent_scales.y());
1741 // Apply adjustment from position constraints.
1742 ApplyPositionAdjustment(layer, data_from_ancestor.fixed_container,
1743 data_from_ancestor.scroll_compensation_matrix, &combined_transform);
1745 bool combined_is_animating_scale = false;
1746 float combined_maximum_animation_contents_scale = 0.f;
1747 if (globals.can_adjust_raster_scales) {
1748 CalculateAnimationContentsScale(
1749 layer,
1750 data_from_ancestor.ancestor_is_animating_scale,
1751 data_from_ancestor.maximum_animation_contents_scale,
1752 data_from_ancestor.parent_matrix,
1753 combined_transform,
1754 &combined_is_animating_scale,
1755 &combined_maximum_animation_contents_scale);
1757 data_for_children.ancestor_is_animating_scale = combined_is_animating_scale;
1758 data_for_children.maximum_animation_contents_scale =
1759 combined_maximum_animation_contents_scale;
1761 // Compute the 2d scale components of the transform hierarchy up to the target
1762 // surface. From there, we can decide on a contents scale for the layer.
1763 float layer_scale_factors = globals.device_scale_factor;
1764 if (data_from_ancestor.in_subtree_of_page_scale_application_layer)
1765 layer_scale_factors *= globals.page_scale_factor;
1766 gfx::Vector2dF combined_transform_scales =
1767 MathUtil::ComputeTransform2dScaleComponents(
1768 combined_transform,
1769 layer_scale_factors);
1771 float ideal_contents_scale =
1772 globals.can_adjust_raster_scales
1773 ? std::max(combined_transform_scales.x(),
1774 combined_transform_scales.y())
1775 : layer_scale_factors;
1776 UpdateLayerContentsScale(
1777 layer,
1778 globals.can_adjust_raster_scales,
1779 ideal_contents_scale,
1780 globals.device_scale_factor,
1781 data_from_ancestor.in_subtree_of_page_scale_application_layer
1782 ? globals.page_scale_factor
1783 : 1.f,
1784 combined_maximum_animation_contents_scale,
1785 animating_transform_to_screen);
1787 UpdateLayerScaleDrawProperties(
1788 layer,
1789 ideal_contents_scale,
1790 combined_maximum_animation_contents_scale,
1791 data_from_ancestor.in_subtree_of_page_scale_application_layer
1792 ? globals.page_scale_factor
1793 : 1.f,
1794 globals.device_scale_factor);
1796 LayerType* mask_layer = layer->mask_layer();
1797 if (mask_layer) {
1798 UpdateLayerScaleDrawProperties(
1799 mask_layer,
1800 ideal_contents_scale,
1801 combined_maximum_animation_contents_scale,
1802 data_from_ancestor.in_subtree_of_page_scale_application_layer
1803 ? globals.page_scale_factor
1804 : 1.f,
1805 globals.device_scale_factor);
1808 LayerType* replica_mask_layer =
1809 layer->replica_layer() ? layer->replica_layer()->mask_layer() : NULL;
1810 if (replica_mask_layer) {
1811 UpdateLayerScaleDrawProperties(
1812 replica_mask_layer,
1813 ideal_contents_scale,
1814 combined_maximum_animation_contents_scale,
1815 data_from_ancestor.in_subtree_of_page_scale_application_layer
1816 ? globals.page_scale_factor
1817 : 1.f,
1818 globals.device_scale_factor);
1821 // The draw_transform that gets computed below is effectively the layer's
1822 // draw_transform, unless the layer itself creates a render_surface. In that
1823 // case, the render_surface re-parents the transforms.
1824 layer_draw_properties.target_space_transform = combined_transform;
1825 // M[draw] = M[parent] * LT * S[layer2content]
1826 layer_draw_properties.target_space_transform.Scale(
1827 SK_MScalar1 / layer->contents_scale_x(),
1828 SK_MScalar1 / layer->contents_scale_y());
1830 // The layer's screen_space_transform represents the transform between root
1831 // layer's "screen space" and local content space.
1832 layer_draw_properties.screen_space_transform =
1833 data_from_ancestor.full_hierarchy_matrix;
1834 if (layer->should_flatten_transform())
1835 layer_draw_properties.screen_space_transform.FlattenTo2d();
1836 layer_draw_properties.screen_space_transform.PreconcatTransform
1837 (layer_draw_properties.target_space_transform);
1839 // Adjusting text AA method during animation may cause repaints, which in-turn
1840 // causes jank.
1841 bool adjust_text_aa =
1842 !animating_opacity_to_screen && !animating_transform_to_screen;
1843 // To avoid color fringing, LCD text should only be used on opaque layers with
1844 // just integral translation.
1845 bool layer_can_use_lcd_text =
1846 data_from_ancestor.subtree_can_use_lcd_text &&
1847 accumulated_draw_opacity == 1.f &&
1848 layer_draw_properties.target_space_transform.
1849 IsIdentityOrIntegerTranslation();
1851 gfx::Rect content_rect(layer->content_bounds());
1853 // full_hierarchy_matrix is the matrix that transforms objects between screen
1854 // space (except projection matrix) and the most recent RenderSurfaceImpl's
1855 // space. next_hierarchy_matrix will only change if this layer uses a new
1856 // RenderSurfaceImpl, otherwise remains the same.
1857 data_for_children.full_hierarchy_matrix =
1858 data_from_ancestor.full_hierarchy_matrix;
1860 // If the subtree will scale layer contents by the transform hierarchy, then
1861 // we should scale things into the render surface by the transform hierarchy
1862 // to take advantage of that.
1863 gfx::Vector2dF render_surface_sublayer_scale =
1864 globals.can_adjust_raster_scales
1865 ? combined_transform_scales
1866 : gfx::Vector2dF(layer_scale_factors, layer_scale_factors);
1868 bool render_to_separate_surface;
1869 if (globals.can_render_to_separate_surface) {
1870 render_to_separate_surface = SubtreeShouldRenderToSeparateSurface(
1871 layer, combined_transform.Preserves2dAxisAlignment());
1872 } else {
1873 render_to_separate_surface = IsRootLayer(layer);
1875 if (render_to_separate_surface) {
1876 // Check back-face visibility before continuing with this surface and its
1877 // subtree
1878 if (!layer->double_sided() && TransformToParentIsKnown(layer) &&
1879 IsSurfaceBackFaceVisible(layer, combined_transform)) {
1880 layer->ClearRenderSurfaceLayerList();
1881 return;
1884 typename LayerType::RenderSurfaceType* render_surface =
1885 CreateOrReuseRenderSurface(layer);
1887 if (IsRootLayer(layer)) {
1888 // The root layer's render surface size is predetermined and so the root
1889 // layer can't directly support non-identity transforms. It should just
1890 // forward top-level transforms to the rest of the tree.
1891 data_for_children.parent_matrix = combined_transform;
1893 // The root surface does not contribute to any other surface, it has no
1894 // target.
1895 layer->render_surface()->set_contributes_to_drawn_surface(false);
1896 } else {
1897 // The owning layer's draw transform has a scale from content to layer
1898 // space which we do not want; so here we use the combined_transform
1899 // instead of the draw_transform. However, we do need to add a different
1900 // scale factor that accounts for the surface's pixel dimensions.
1901 combined_transform.Scale(1.0 / render_surface_sublayer_scale.x(),
1902 1.0 / render_surface_sublayer_scale.y());
1903 render_surface->SetDrawTransform(combined_transform);
1905 // The owning layer's transform was re-parented by the surface, so the
1906 // layer's new draw_transform only needs to scale the layer to surface
1907 // space.
1908 layer_draw_properties.target_space_transform.MakeIdentity();
1909 layer_draw_properties.target_space_transform.
1910 Scale(render_surface_sublayer_scale.x() / layer->contents_scale_x(),
1911 render_surface_sublayer_scale.y() / layer->contents_scale_y());
1913 // Inside the surface's subtree, we scale everything to the owning layer's
1914 // scale. The sublayer matrix transforms layer rects into target surface
1915 // content space. Conceptually, all layers in the subtree inherit the
1916 // scale at the point of the render surface in the transform hierarchy,
1917 // but we apply it explicitly to the owning layer and the remainder of the
1918 // subtree independently.
1919 DCHECK(data_for_children.parent_matrix.IsIdentity());
1920 data_for_children.parent_matrix.Scale(render_surface_sublayer_scale.x(),
1921 render_surface_sublayer_scale.y());
1923 // Even if the |layer_is_drawn|, it only contributes to a drawn surface
1924 // when the |layer_is_visible|.
1925 layer->render_surface()->set_contributes_to_drawn_surface(
1926 layer_is_visible);
1929 // The opacity value is moved from the layer to its surface, so that the
1930 // entire subtree properly inherits opacity.
1931 render_surface->SetDrawOpacity(accumulated_draw_opacity);
1932 render_surface->SetDrawOpacityIsAnimating(animating_opacity_to_target);
1933 animating_opacity_to_target = false;
1934 layer_draw_properties.opacity = 1.f;
1935 layer_draw_properties.opacity_is_animating = animating_opacity_to_target;
1936 layer_draw_properties.screen_space_opacity_is_animating =
1937 animating_opacity_to_screen;
1939 render_surface->SetTargetSurfaceTransformsAreAnimating(
1940 animating_transform_to_target);
1941 render_surface->SetScreenSpaceTransformsAreAnimating(
1942 animating_transform_to_screen);
1943 animating_transform_to_target = false;
1944 layer_draw_properties.target_space_transform_is_animating =
1945 animating_transform_to_target;
1946 layer_draw_properties.screen_space_transform_is_animating =
1947 animating_transform_to_screen;
1949 // Update the aggregate hierarchy matrix to include the transform of the
1950 // newly created RenderSurfaceImpl.
1951 data_for_children.full_hierarchy_matrix.PreconcatTransform(
1952 render_surface->draw_transform());
1954 if (layer->mask_layer()) {
1955 DrawProperties<LayerType>& mask_layer_draw_properties =
1956 layer->mask_layer()->draw_properties();
1957 mask_layer_draw_properties.render_target = layer;
1958 mask_layer_draw_properties.visible_content_rect =
1959 gfx::Rect(layer->content_bounds());
1962 if (layer->replica_layer() && layer->replica_layer()->mask_layer()) {
1963 DrawProperties<LayerType>& replica_mask_draw_properties =
1964 layer->replica_layer()->mask_layer()->draw_properties();
1965 replica_mask_draw_properties.render_target = layer;
1966 replica_mask_draw_properties.visible_content_rect =
1967 gfx::Rect(layer->content_bounds());
1970 // Ignore occlusion from outside the surface when surface contents need to
1971 // be fully drawn. Layers with copy-request need to be complete.
1972 // We could be smarter about layers with replica and exclude regions
1973 // where both layer and the replica are occluded, but this seems like an
1974 // overkill. The same is true for layers with filters that move pixels.
1975 // TODO(senorblanco): make this smarter for the SkImageFilter case (check
1976 // for pixel-moving filters)
1977 if (layer->HasCopyRequest() ||
1978 layer->has_replica() ||
1979 layer->filters().HasReferenceFilter() ||
1980 layer->filters().HasFilterThatMovesPixels()) {
1981 nearest_occlusion_immune_ancestor_surface = render_surface;
1983 render_surface->SetNearestOcclusionImmuneAncestor(
1984 nearest_occlusion_immune_ancestor_surface);
1986 layer_or_ancestor_clips_descendants = false;
1987 bool subtree_is_clipped_by_surface_bounds = false;
1988 if (ancestor_clips_subtree) {
1989 // It may be the layer or the surface doing the clipping of the subtree,
1990 // but in either case, we'll be clipping to the projected clip rect of our
1991 // ancestor.
1992 gfx::Transform inverse_surface_draw_transform(
1993 gfx::Transform::kSkipInitialization);
1994 if (!render_surface->draw_transform().GetInverse(
1995 &inverse_surface_draw_transform)) {
1996 // TODO(shawnsingh): Either we need to handle uninvertible transforms
1997 // here, or DCHECK that the transform is invertible.
2000 gfx::Rect projected_surface_rect = MathUtil::ProjectEnclosingClippedRect(
2001 inverse_surface_draw_transform, ancestor_clip_rect_in_target_space);
2003 if (layer_draw_properties.num_unclipped_descendants > 0) {
2004 // If we have unclipped descendants, we cannot count on the render
2005 // surface's bounds clipping our subtree: the unclipped descendants
2006 // could cause us to expand our bounds. In this case, we must rely on
2007 // layer clipping for correctess. NB: since we can only encounter
2008 // translations between a clip child and its clip parent, clipping is
2009 // guaranteed to be exact in this case.
2010 layer_or_ancestor_clips_descendants = true;
2011 clip_rect_in_target_space = projected_surface_rect;
2012 } else {
2013 // The new render_surface here will correctly clip the entire subtree.
2014 // So, we do not need to continue propagating the clipping state further
2015 // down the tree. This way, we can avoid transforming clip rects from
2016 // ancestor target surface space to current target surface space that
2017 // could cause more w < 0 headaches. The render surface clip rect is
2018 // expressed in the space where this surface draws, i.e. the same space
2019 // as clip_rect_from_ancestor_in_ancestor_target_space.
2020 render_surface->SetClipRect(ancestor_clip_rect_in_target_space);
2021 clip_rect_of_target_surface_in_target_space = projected_surface_rect;
2022 subtree_is_clipped_by_surface_bounds = true;
2026 DCHECK(layer->render_surface());
2027 DCHECK(!layer->parent() || layer->parent()->render_target() ==
2028 accumulated_surface_state->back().render_target);
2030 accumulated_surface_state->push_back(
2031 AccumulatedSurfaceState<LayerType>(layer));
2033 render_surface->SetIsClipped(subtree_is_clipped_by_surface_bounds);
2034 if (!subtree_is_clipped_by_surface_bounds) {
2035 render_surface->SetClipRect(gfx::Rect());
2036 clip_rect_of_target_surface_in_target_space =
2037 data_from_ancestor.clip_rect_of_target_surface_in_target_space;
2040 // If the new render surface is drawn translucent or with a non-integral
2041 // translation then the subtree that gets drawn on this render surface
2042 // cannot use LCD text.
2043 data_for_children.subtree_can_use_lcd_text = layer_can_use_lcd_text;
2045 render_surface_layer_list->push_back(layer);
2046 } else {
2047 DCHECK(layer->parent());
2049 // Note: layer_draw_properties.target_space_transform is computed above,
2050 // before this if-else statement.
2051 layer_draw_properties.target_space_transform_is_animating =
2052 animating_transform_to_target;
2053 layer_draw_properties.screen_space_transform_is_animating =
2054 animating_transform_to_screen;
2055 layer_draw_properties.opacity = accumulated_draw_opacity;
2056 layer_draw_properties.opacity_is_animating = animating_opacity_to_target;
2057 layer_draw_properties.screen_space_opacity_is_animating =
2058 animating_opacity_to_screen;
2059 data_for_children.parent_matrix = combined_transform;
2061 layer->ClearRenderSurface();
2063 // Layers without render_surfaces directly inherit the ancestor's clip
2064 // status.
2065 layer_or_ancestor_clips_descendants = ancestor_clips_subtree;
2066 if (ancestor_clips_subtree) {
2067 clip_rect_in_target_space =
2068 ancestor_clip_rect_in_target_space;
2071 // The surface's cached clip rect value propagates regardless of what
2072 // clipping goes on between layers here.
2073 clip_rect_of_target_surface_in_target_space =
2074 data_from_ancestor.clip_rect_of_target_surface_in_target_space;
2076 // Layers that are not their own render_target will render into the target
2077 // of their nearest ancestor.
2078 layer_draw_properties.render_target = layer->parent()->render_target();
2081 if (adjust_text_aa)
2082 layer_draw_properties.can_use_lcd_text = layer_can_use_lcd_text;
2084 gfx::Rect rect_in_target_space =
2085 MathUtil::MapEnclosingClippedRect(layer->draw_transform(), content_rect);
2087 if (LayerClipsSubtree(layer)) {
2088 layer_or_ancestor_clips_descendants = true;
2089 if (ancestor_clips_subtree && !layer->render_surface()) {
2090 // A layer without render surface shares the same target as its ancestor.
2091 clip_rect_in_target_space =
2092 ancestor_clip_rect_in_target_space;
2093 clip_rect_in_target_space.Intersect(rect_in_target_space);
2094 } else {
2095 clip_rect_in_target_space = rect_in_target_space;
2099 // Tell the layer the rect that it's clipped by. In theory we could use a
2100 // tighter clip rect here (drawable_content_rect), but that actually does not
2101 // reduce how much would be drawn, and instead it would create unnecessary
2102 // changes to scissor state affecting GPU performance. Our clip information
2103 // is used in the recursion below, so we must set it beforehand.
2104 layer_draw_properties.is_clipped = layer_or_ancestor_clips_descendants;
2105 if (layer_or_ancestor_clips_descendants) {
2106 layer_draw_properties.clip_rect = clip_rect_in_target_space;
2107 } else {
2108 // Initialize the clip rect to a safe value that will not clip the
2109 // layer, just in case clipping is still accidentally used.
2110 layer_draw_properties.clip_rect = rect_in_target_space;
2113 typename LayerType::LayerListType& descendants =
2114 (layer->render_surface() ? layer->render_surface()->layer_list()
2115 : *layer_list);
2117 // Any layers that are appended after this point are in the layer's subtree
2118 // and should be included in the sorting process.
2119 size_t sorting_start_index = descendants.size();
2121 if (!LayerShouldBeSkipped(layer, layer_is_drawn)) {
2122 MarkLayerWithRenderSurfaceLayerListId(layer,
2123 current_render_surface_layer_list_id);
2124 descendants.push_back(layer);
2127 // Any layers that are appended after this point may need to be sorted if we
2128 // visit the children out of order.
2129 size_t render_surface_layer_list_child_sorting_start_index =
2130 render_surface_layer_list->size();
2131 size_t layer_list_child_sorting_start_index = descendants.size();
2133 if (!layer->children().empty()) {
2134 if (layer == globals.page_scale_application_layer) {
2135 data_for_children.parent_matrix.Scale(
2136 globals.page_scale_factor,
2137 globals.page_scale_factor);
2138 data_for_children.in_subtree_of_page_scale_application_layer = true;
2141 // Flatten to 2D if the layer doesn't preserve 3D.
2142 if (layer->should_flatten_transform())
2143 data_for_children.parent_matrix.FlattenTo2d();
2145 data_for_children.scroll_compensation_matrix =
2146 ComputeScrollCompensationMatrixForChildren(
2147 layer,
2148 data_from_ancestor.parent_matrix,
2149 data_from_ancestor.scroll_compensation_matrix,
2150 effective_scroll_delta);
2151 data_for_children.fixed_container =
2152 layer->IsContainerForFixedPositionLayers() ?
2153 layer : data_from_ancestor.fixed_container;
2155 data_for_children.clip_rect_in_target_space = clip_rect_in_target_space;
2156 data_for_children.clip_rect_of_target_surface_in_target_space =
2157 clip_rect_of_target_surface_in_target_space;
2158 data_for_children.ancestor_clips_subtree =
2159 layer_or_ancestor_clips_descendants;
2160 data_for_children.nearest_occlusion_immune_ancestor_surface =
2161 nearest_occlusion_immune_ancestor_surface;
2162 data_for_children.subtree_is_visible_from_ancestor = layer_is_drawn;
2165 std::vector<LayerType*> sorted_children;
2166 bool child_order_changed = false;
2167 if (layer_draw_properties.has_child_with_a_scroll_parent)
2168 child_order_changed = SortChildrenForRecursion(&sorted_children, *layer);
2170 for (size_t i = 0; i < layer->children().size(); ++i) {
2171 // If one of layer's children has a scroll parent, then we may have to
2172 // visit the children out of order. The new order is stored in
2173 // sorted_children. Otherwise, we'll grab the child directly from the
2174 // layer's list of children.
2175 LayerType* child =
2176 layer_draw_properties.has_child_with_a_scroll_parent
2177 ? sorted_children[i]
2178 : LayerTreeHostCommon::get_layer_as_raw_ptr(layer->children(), i);
2180 child->draw_properties().index_of_first_descendants_addition =
2181 descendants.size();
2182 child->draw_properties().index_of_first_render_surface_layer_list_addition =
2183 render_surface_layer_list->size();
2185 CalculateDrawPropertiesInternal<LayerType>(
2186 child,
2187 globals,
2188 data_for_children,
2189 render_surface_layer_list,
2190 &descendants,
2191 accumulated_surface_state,
2192 current_render_surface_layer_list_id);
2193 if (child->render_surface() &&
2194 !child->render_surface()->layer_list().empty() &&
2195 !child->render_surface()->content_rect().IsEmpty()) {
2196 // This child will contribute its render surface, which means
2197 // we need to mark just the mask layer (and replica mask layer)
2198 // with the id.
2199 MarkMasksWithRenderSurfaceLayerListId(
2200 child, current_render_surface_layer_list_id);
2201 descendants.push_back(child);
2204 child->draw_properties().num_descendants_added =
2205 descendants.size() -
2206 child->draw_properties().index_of_first_descendants_addition;
2207 child->draw_properties().num_render_surfaces_added =
2208 render_surface_layer_list->size() -
2209 child->draw_properties()
2210 .index_of_first_render_surface_layer_list_addition;
2213 // Add the unsorted layer list contributions, if necessary.
2214 if (child_order_changed) {
2215 SortLayerListContributions(
2216 *layer,
2217 GetLayerListForSorting(render_surface_layer_list),
2218 render_surface_layer_list_child_sorting_start_index,
2219 &GetNewRenderSurfacesStartIndexAndCount<LayerType>);
2221 SortLayerListContributions(
2222 *layer,
2223 &descendants,
2224 layer_list_child_sorting_start_index,
2225 &GetNewDescendantsStartIndexAndCount<LayerType>);
2228 // Compute the total drawable_content_rect for this subtree (the rect is in
2229 // target surface space).
2230 gfx::Rect local_drawable_content_rect_of_subtree =
2231 accumulated_surface_state->back().drawable_content_rect;
2232 if (layer->render_surface()) {
2233 DCHECK(accumulated_surface_state->back().render_target == layer);
2234 accumulated_surface_state->pop_back();
2237 if (layer->render_surface() && !IsRootLayer(layer) &&
2238 layer->render_surface()->layer_list().empty()) {
2239 RemoveSurfaceForEarlyExit(layer, render_surface_layer_list);
2240 return;
2243 // Compute the layer's drawable content rect (the rect is in target surface
2244 // space).
2245 layer_draw_properties.drawable_content_rect = rect_in_target_space;
2246 if (layer_or_ancestor_clips_descendants) {
2247 layer_draw_properties.drawable_content_rect.Intersect(
2248 clip_rect_in_target_space);
2250 if (layer->DrawsContent()) {
2251 local_drawable_content_rect_of_subtree.Union(
2252 layer_draw_properties.drawable_content_rect);
2255 // Compute the layer's visible content rect (the rect is in content space).
2256 layer_draw_properties.visible_content_rect = CalculateVisibleContentRect(
2257 layer, clip_rect_of_target_surface_in_target_space, rect_in_target_space);
2259 // Compute the remaining properties for the render surface, if the layer has
2260 // one.
2261 if (IsRootLayer(layer)) {
2262 // The root layer's surface's content_rect is always the entire viewport.
2263 DCHECK(layer->render_surface());
2264 layer->render_surface()->SetContentRect(
2265 ancestor_clip_rect_in_target_space);
2266 } else if (layer->render_surface()) {
2267 typename LayerType::RenderSurfaceType* render_surface =
2268 layer->render_surface();
2269 gfx::Rect clipped_content_rect = local_drawable_content_rect_of_subtree;
2271 // Don't clip if the layer is reflected as the reflection shouldn't be
2272 // clipped. If the layer is animating, then the surface's transform to
2273 // its target is not known on the main thread, and we should not use it
2274 // to clip.
2275 if (!layer->replica_layer() && TransformToParentIsKnown(layer)) {
2276 // Note, it is correct to use data_from_ancestor.ancestor_clips_subtree
2277 // here, because we are looking at this layer's render_surface, not the
2278 // layer itself.
2279 if (render_surface->is_clipped() && !clipped_content_rect.IsEmpty()) {
2280 gfx::Rect surface_clip_rect = LayerTreeHostCommon::CalculateVisibleRect(
2281 render_surface->clip_rect(),
2282 clipped_content_rect,
2283 render_surface->draw_transform());
2284 clipped_content_rect.Intersect(surface_clip_rect);
2288 // The RenderSurfaceImpl backing texture cannot exceed the maximum supported
2289 // texture size.
2290 clipped_content_rect.set_width(
2291 std::min(clipped_content_rect.width(), globals.max_texture_size));
2292 clipped_content_rect.set_height(
2293 std::min(clipped_content_rect.height(), globals.max_texture_size));
2295 if (clipped_content_rect.IsEmpty()) {
2296 RemoveSurfaceForEarlyExit(layer, render_surface_layer_list);
2297 return;
2300 // Layers having a non-default blend mode will blend with the content
2301 // inside its parent's render target. This render target should be
2302 // either root_for_isolated_group, or the root of the layer tree.
2303 // Otherwise, this layer will use an incomplete backdrop, limited to its
2304 // render target and the blending result will be incorrect.
2305 DCHECK(layer->uses_default_blend_mode() || IsRootLayer(layer) ||
2306 !layer->parent()->render_target() ||
2307 IsRootLayer(layer->parent()->render_target()) ||
2308 layer->parent()->render_target()->is_root_for_isolated_group());
2310 render_surface->SetContentRect(clipped_content_rect);
2312 // The owning layer's screen_space_transform has a scale from content to
2313 // layer space which we need to undo and replace with a scale from the
2314 // surface's subtree into layer space.
2315 gfx::Transform screen_space_transform = layer->screen_space_transform();
2316 screen_space_transform.Scale(
2317 layer->contents_scale_x() / render_surface_sublayer_scale.x(),
2318 layer->contents_scale_y() / render_surface_sublayer_scale.y());
2319 render_surface->SetScreenSpaceTransform(screen_space_transform);
2321 if (layer->replica_layer()) {
2322 gfx::Transform surface_origin_to_replica_origin_transform;
2323 surface_origin_to_replica_origin_transform.Scale(
2324 render_surface_sublayer_scale.x(), render_surface_sublayer_scale.y());
2325 surface_origin_to_replica_origin_transform.Translate(
2326 layer->replica_layer()->position().x() +
2327 layer->replica_layer()->transform_origin().x(),
2328 layer->replica_layer()->position().y() +
2329 layer->replica_layer()->transform_origin().y());
2330 surface_origin_to_replica_origin_transform.PreconcatTransform(
2331 layer->replica_layer()->transform());
2332 surface_origin_to_replica_origin_transform.Translate(
2333 -layer->replica_layer()->transform_origin().x(),
2334 -layer->replica_layer()->transform_origin().y());
2335 surface_origin_to_replica_origin_transform.Scale(
2336 1.0 / render_surface_sublayer_scale.x(),
2337 1.0 / render_surface_sublayer_scale.y());
2339 // Compute the replica's "originTransform" that maps from the replica's
2340 // origin space to the target surface origin space.
2341 gfx::Transform replica_origin_transform =
2342 layer->render_surface()->draw_transform() *
2343 surface_origin_to_replica_origin_transform;
2344 render_surface->SetReplicaDrawTransform(replica_origin_transform);
2346 // Compute the replica's "screen_space_transform" that maps from the
2347 // replica's origin space to the screen's origin space.
2348 gfx::Transform replica_screen_space_transform =
2349 layer->render_surface()->screen_space_transform() *
2350 surface_origin_to_replica_origin_transform;
2351 render_surface->SetReplicaScreenSpaceTransform(
2352 replica_screen_space_transform);
2356 SavePaintPropertiesLayer(layer);
2358 // If neither this layer nor any of its children were added, early out.
2359 if (sorting_start_index == descendants.size()) {
2360 DCHECK(!layer->render_surface() || IsRootLayer(layer));
2361 return;
2364 // If preserves-3d then sort all the descendants in 3D so that they can be
2365 // drawn from back to front. If the preserves-3d property is also set on the
2366 // parent then skip the sorting as the parent will sort all the descendants
2367 // anyway.
2368 if (globals.layer_sorter && descendants.size() && layer->Is3dSorted() &&
2369 !LayerIsInExisting3DRenderingContext(layer)) {
2370 SortLayers(descendants.begin() + sorting_start_index,
2371 descendants.end(),
2372 globals.layer_sorter);
2375 UpdateAccumulatedSurfaceState<LayerType>(
2376 layer, local_drawable_content_rect_of_subtree, accumulated_surface_state);
2378 if (layer->HasContributingDelegatedRenderPasses()) {
2379 layer->render_target()->render_surface()->
2380 AddContributingDelegatedRenderPassLayer(layer);
2384 template <typename LayerType, typename RenderSurfaceLayerListType>
2385 static void ProcessCalcDrawPropsInputs(
2386 const LayerTreeHostCommon::CalcDrawPropsInputs<LayerType,
2387 RenderSurfaceLayerListType>&
2388 inputs,
2389 SubtreeGlobals<LayerType>* globals,
2390 DataForRecursion<LayerType>* data_for_recursion) {
2391 DCHECK(inputs.root_layer);
2392 DCHECK(IsRootLayer(inputs.root_layer));
2393 DCHECK(inputs.render_surface_layer_list);
2395 gfx::Transform identity_matrix;
2397 // The root layer's render_surface should receive the device viewport as the
2398 // initial clip rect.
2399 gfx::Rect device_viewport_rect(inputs.device_viewport_size);
2401 gfx::Vector2dF device_transform_scale_components =
2402 MathUtil::ComputeTransform2dScaleComponents(inputs.device_transform, 1.f);
2403 // Not handling the rare case of different x and y device scale.
2404 float device_transform_scale =
2405 std::max(device_transform_scale_components.x(),
2406 device_transform_scale_components.y());
2408 gfx::Transform scaled_device_transform = inputs.device_transform;
2409 scaled_device_transform.Scale(inputs.device_scale_factor,
2410 inputs.device_scale_factor);
2412 globals->layer_sorter = NULL;
2413 globals->max_texture_size = inputs.max_texture_size;
2414 globals->device_scale_factor =
2415 inputs.device_scale_factor * device_transform_scale;
2416 globals->page_scale_factor = inputs.page_scale_factor;
2417 globals->page_scale_application_layer = inputs.page_scale_application_layer;
2418 globals->can_render_to_separate_surface =
2419 inputs.can_render_to_separate_surface;
2420 globals->can_adjust_raster_scales = inputs.can_adjust_raster_scales;
2422 data_for_recursion->parent_matrix = scaled_device_transform;
2423 data_for_recursion->full_hierarchy_matrix = identity_matrix;
2424 data_for_recursion->scroll_compensation_matrix = identity_matrix;
2425 data_for_recursion->fixed_container = inputs.root_layer;
2426 data_for_recursion->clip_rect_in_target_space = device_viewport_rect;
2427 data_for_recursion->clip_rect_of_target_surface_in_target_space =
2428 device_viewport_rect;
2429 data_for_recursion->maximum_animation_contents_scale = 0.f;
2430 data_for_recursion->ancestor_is_animating_scale = false;
2431 data_for_recursion->ancestor_clips_subtree = true;
2432 data_for_recursion->nearest_occlusion_immune_ancestor_surface = NULL;
2433 data_for_recursion->in_subtree_of_page_scale_application_layer = false;
2434 data_for_recursion->subtree_can_use_lcd_text = inputs.can_use_lcd_text;
2435 data_for_recursion->subtree_is_visible_from_ancestor = true;
2438 void LayerTreeHostCommon::CalculateDrawProperties(
2439 CalcDrawPropsMainInputs* inputs) {
2440 LayerList dummy_layer_list;
2441 SubtreeGlobals<Layer> globals;
2442 DataForRecursion<Layer> data_for_recursion;
2443 ProcessCalcDrawPropsInputs(*inputs, &globals, &data_for_recursion);
2445 PreCalculateMetaInformationRecursiveData recursive_data;
2446 PreCalculateMetaInformation(inputs->root_layer, &recursive_data);
2447 std::vector<AccumulatedSurfaceState<Layer> > accumulated_surface_state;
2448 CalculateDrawPropertiesInternal<Layer>(
2449 inputs->root_layer,
2450 globals,
2451 data_for_recursion,
2452 inputs->render_surface_layer_list,
2453 &dummy_layer_list,
2454 &accumulated_surface_state,
2455 inputs->current_render_surface_layer_list_id);
2457 // The dummy layer list should not have been used.
2458 DCHECK_EQ(0u, dummy_layer_list.size());
2459 // A root layer render_surface should always exist after
2460 // CalculateDrawProperties.
2461 DCHECK(inputs->root_layer->render_surface());
2464 void LayerTreeHostCommon::CalculateDrawProperties(
2465 CalcDrawPropsImplInputs* inputs) {
2466 LayerImplList dummy_layer_list;
2467 SubtreeGlobals<LayerImpl> globals;
2468 DataForRecursion<LayerImpl> data_for_recursion;
2469 ProcessCalcDrawPropsInputs(*inputs, &globals, &data_for_recursion);
2471 LayerSorter layer_sorter;
2472 globals.layer_sorter = &layer_sorter;
2474 PreCalculateMetaInformationRecursiveData recursive_data;
2475 PreCalculateMetaInformation(inputs->root_layer, &recursive_data);
2476 std::vector<AccumulatedSurfaceState<LayerImpl> >
2477 accumulated_surface_state;
2478 CalculateDrawPropertiesInternal<LayerImpl>(
2479 inputs->root_layer,
2480 globals,
2481 data_for_recursion,
2482 inputs->render_surface_layer_list,
2483 &dummy_layer_list,
2484 &accumulated_surface_state,
2485 inputs->current_render_surface_layer_list_id);
2487 // The dummy layer list should not have been used.
2488 DCHECK_EQ(0u, dummy_layer_list.size());
2489 // A root layer render_surface should always exist after
2490 // CalculateDrawProperties.
2491 DCHECK(inputs->root_layer->render_surface());
2494 } // namespace cc