layer_tree_host_common.cc revision f2477e01787aa58f445919b809d89e252beef54f
12a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Copyright 2011 The Chromium Authors. All rights reserved.
22a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
32a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// found in the LICENSE file.
42a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
52a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "cc/trees/layer_tree_host_common.h"
62a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <algorithm>
82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/debug/trace_event.h"
102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "cc/base/math_util.h"
112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "cc/layers/heads_up_display_layer_impl.h"
122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "cc/layers/layer.h"
132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "cc/layers/layer_impl.h"
142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "cc/layers/layer_iterator.h"
152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "cc/layers/render_surface.h"
162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "cc/layers/render_surface_impl.h"
172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "cc/trees/layer_sorter.h"
182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "cc/trees/layer_tree_impl.h"
192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ui/gfx/point_conversions.h"
202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ui/gfx/rect_conversions.h"
212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ui/gfx/transform.h"
222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace cc {
242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)ScrollAndScaleSet::ScrollAndScaleSet() {}
262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)ScrollAndScaleSet::~ScrollAndScaleSet() {}
282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
29c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)static void SortLayers(LayerList::iterator forst,
30c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                       LayerList::iterator end,
312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                       void* layer_sorter) {
322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  NOTREACHED();
332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
35c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)static void SortLayers(LayerImplList::iterator first,
36c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                       LayerImplList::iterator end,
372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                       LayerSorter* layer_sorter) {
382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(layer_sorter);
392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  TRACE_EVENT0("cc", "LayerTreeHostCommon::SortLayers");
402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  layer_sorter->Sort(first, end);
412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
43424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)template <typename LayerType>
44424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)static gfx::Vector2dF GetEffectiveScrollDelta(LayerType* layer) {
45424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  gfx::Vector2dF scroll_delta = layer->ScrollDelta();
46424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // The scroll parent's scroll delta is the amount we've scrolled on the
47424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // compositor thread since the commit for this layer tree's source frame.
48424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // we last reported to the main thread. I.e., it's the discrepancy between
49424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // a scroll parent's scroll delta and offset, so we must add it here.
50424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  if (layer->scroll_parent())
51424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    scroll_delta += layer->scroll_parent()->ScrollDelta();
52424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  return scroll_delta;
53424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}
54424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
55424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)template <typename LayerType>
56424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)static gfx::Vector2dF GetEffectiveTotalScrollOffset(LayerType* layer) {
57424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  gfx::Vector2dF offset = layer->TotalScrollOffset();
58424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // The scroll parent's total scroll offset (scroll offset + scroll delta)
59424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // can't be used because its scroll offset has already been applied to the
60424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // scroll children's positions by the main thread layer positioning code.
61424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  if (layer->scroll_parent())
62424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    offset += layer->scroll_parent()->ScrollDelta();
63424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  return offset;
64424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}
65424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)inline gfx::Rect CalculateVisibleRectWithCachedLayerRect(
672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    gfx::Rect target_surface_rect,
682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    gfx::Rect layer_bound_rect,
692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    gfx::Rect layer_rect_in_target_space,
702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const gfx::Transform& transform) {
71868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (layer_rect_in_target_space.IsEmpty())
72868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    return gfx::Rect();
73868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Is this layer fully contained within the target surface?
752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (target_surface_rect.Contains(layer_rect_in_target_space))
762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return layer_bound_rect;
772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // If the layer doesn't fill up the entire surface, then find the part of
792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // the surface rect where the layer could be visible. This avoids trying to
802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // project surface rect points that are behind the projection point.
812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  gfx::Rect minimal_surface_rect = target_surface_rect;
822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  minimal_surface_rect.Intersect(layer_rect_in_target_space);
832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
843551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  if (minimal_surface_rect.IsEmpty())
853551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      return gfx::Rect();
863551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Project the corners of the target surface rect into the layer space.
882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // This bounding rectangle may be larger than it needs to be (being
892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // axis-aligned), but is a reasonable filter on the space to consider.
902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Non-invertible transforms will create an empty rect here.
912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  gfx::Transform surface_to_layer(gfx::Transform::kSkipInitialization);
932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!transform.GetInverse(&surface_to_layer)) {
943551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    // Because we cannot use the surface bounds to determine what portion of
953551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    // the layer is visible, we must conservatively assume the full layer is
963551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    // visible.
973551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    return layer_bound_rect;
982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
99c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  gfx::Rect layer_rect = gfx::ToEnclosingRect(MathUtil::ProjectClippedRect(
1012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      surface_to_layer, gfx::RectF(minimal_surface_rect)));
1022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  layer_rect.Intersect(layer_bound_rect);
1032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return layer_rect;
1042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)gfx::Rect LayerTreeHostCommon::CalculateVisibleRect(
1072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    gfx::Rect target_surface_rect,
1082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    gfx::Rect layer_bound_rect,
1092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const gfx::Transform& transform) {
1102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  gfx::Rect layer_in_surface_space =
1112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      MathUtil::MapClippedRect(transform, layer_bound_rect);
1122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return CalculateVisibleRectWithCachedLayerRect(
1132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      target_surface_rect, layer_bound_rect, layer_in_surface_space, transform);
1142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
116424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)template <typename LayerType>
117424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)static LayerType* NextTargetSurface(LayerType* layer) {
118424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  return layer->parent() ? layer->parent()->render_target() : 0;
119424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}
120424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
121424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// Given two layers, this function finds their respective render targets and,
122424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// computes a change of basis translation. It does this by accumulating the
123424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// translation components of the draw transforms of each target between the
124424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// ancestor and descendant. These transforms must be 2D translations, and this
125424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// requirement is enforced at every step.
1268bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)template <typename LayerType>
127424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)static gfx::Vector2dF ComputeChangeOfBasisTranslation(
128424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    const LayerType& ancestor_layer,
129424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    const LayerType& descendant_layer) {
130424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  DCHECK(descendant_layer.HasAncestor(&ancestor_layer));
131424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  const LayerType* descendant_target = descendant_layer.render_target();
132424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  DCHECK(descendant_target);
133424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  const LayerType* ancestor_target = ancestor_layer.render_target();
134424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  DCHECK(ancestor_target);
135424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
136424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  gfx::Vector2dF translation;
137424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  for (const LayerType* target = descendant_target; target != ancestor_target;
1381e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)       target = NextTargetSurface(target)) {
1391e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    const gfx::Transform& trans = target->render_surface()->draw_transform();
1401e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    // Ensure that this translation is truly 2d.
1411e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    DCHECK(trans.IsIdentityOrTranslation());
1421e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    DCHECK_EQ(0.f, trans.matrix().get(2, 3));
1431e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    translation += trans.To2dTranslation();
1441e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  }
145424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
146424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  return translation;
147424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}
148424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
149424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)enum TranslateRectDirection {
150424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  TranslateRectDirectionToAncestor,
151424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  TranslateRectDirectionToDescendant
152424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)};
153424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
1548bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)template <typename LayerType>
155424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)static gfx::Rect TranslateRectToTargetSpace(const LayerType& ancestor_layer,
156424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                                            const LayerType& descendant_layer,
157424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                                            gfx::Rect rect,
158424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                                            TranslateRectDirection direction) {
1598bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  gfx::Vector2dF translation = ComputeChangeOfBasisTranslation<LayerType>(
1608bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      ancestor_layer, descendant_layer);
161424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  if (direction == TranslateRectDirectionToDescendant)
162424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    translation.Scale(-1.f);
163424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  return gfx::ToEnclosingRect(
164424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      gfx::RectF(rect.origin() + translation, rect.size()));
165424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}
166424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
167424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// Attempts to update the clip rects for the given layer. If the layer has a
168424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// clip_parent, it may not inherit its immediate ancestor's clip.
1698bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)template <typename LayerType>
170424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)static void UpdateClipRectsForClipChild(
171424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    const LayerType* layer,
172424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    gfx::Rect* clip_rect_in_parent_target_space,
173424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    bool* subtree_should_be_clipped) {
174424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // If the layer has no clip_parent, or the ancestor is the same as its actual
175424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // parent, then we don't need special clip rects. Bail now and leave the out
176424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // parameters untouched.
1778bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  const LayerType* clip_parent = layer->scroll_parent();
1788bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1798bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  if (!clip_parent)
1808bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    clip_parent = layer->clip_parent();
1818bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
182424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  if (!clip_parent || clip_parent == layer->parent())
183424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    return;
184424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
185424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // The root layer is never a clip child.
186424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  DCHECK(layer->parent());
187424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
188424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // Grab the cached values.
189424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  *clip_rect_in_parent_target_space = clip_parent->clip_rect();
190424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  *subtree_should_be_clipped = clip_parent->is_clipped();
191424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
192424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // We may have to project the clip rect into our parent's target space. Note,
193424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // it must be our parent's target space, not ours. For one, we haven't
194424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // computed our transforms, so we couldn't put it in our space yet even if we
195424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // wanted to. But more importantly, this matches the expectations of
196424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // CalculateDrawPropertiesInternal. If we, say, create a render surface, these
197424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // clip rects will want to be in its target space, not ours.
1988bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  if (clip_parent == layer->clip_parent()) {
1998bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    *clip_rect_in_parent_target_space = TranslateRectToTargetSpace<LayerType>(
2008bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        *clip_parent,
2018bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        *layer->parent(),
2028bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        *clip_rect_in_parent_target_space,
2038bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        TranslateRectDirectionToDescendant);
2048bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  } else {
2058bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    // If we're being clipped by our scroll parent, we must translate through
2068bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    // our common ancestor. This happens to be our parent, so it is sufficent to
2078bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    // translate from our clip parent's space to the space of its ancestor (our
2088bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    // parent).
2098bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    *clip_rect_in_parent_target_space =
2108bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        TranslateRectToTargetSpace<LayerType>(*layer->parent(),
2118bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                                              *clip_parent,
2128bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                                              *clip_rect_in_parent_target_space,
2138bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                                              TranslateRectDirectionToAncestor);
2148bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  }
215424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}
216424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
217424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// We collect an accumulated drawable content rect per render surface.
218424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// Typically, a layer will contribute to only one surface, the surface
219424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// associated with its render target. Clip children, however, may affect
220424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// several surfaces since there may be several surfaces between the clip child
221424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// and its parent.
222424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)//
223424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)// NB: we accumulate the layer's *clipped* drawable content rect.
224424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)template <typename LayerType>
225424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)struct AccumulatedSurfaceState {
226424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  explicit AccumulatedSurfaceState(LayerType* render_target)
227424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      : render_target(render_target) {}
228424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
229424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // The accumulated drawable content rect for the surface associated with the
230424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // given |render_target|.
231424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  gfx::Rect drawable_content_rect;
232424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
233424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // The target owning the surface. (We hang onto the target rather than the
234424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // surface so that we can DCHECK that the surface's draw transform is simply
235424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // a translation when |render_target| reports that it has no unclipped
236424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // descendants).
237424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  LayerType* render_target;
238424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)};
239424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
2408bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)template <typename LayerType>
241424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)void UpdateAccumulatedSurfaceState(
242424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    LayerType* layer,
243424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    gfx::Rect drawable_content_rect,
244424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    std::vector<AccumulatedSurfaceState<LayerType> >*
245424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        accumulated_surface_state) {
246424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  if (IsRootLayer(layer))
247424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    return;
248424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
249424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // We will apply our drawable content rect to the accumulated rects for all
250424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // surfaces between us and |render_target| (inclusive). This is either our
251424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // clip parent's target if we are a clip child, or else simply our parent's
252424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // target. We use our parent's target because we're either the owner of a
253424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // render surface and we'll want to add our rect to our *surface's* target, or
254424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // we're not and our target is the same as our parent's. In both cases, the
255424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // parent's target gives us what we want.
256424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  LayerType* render_target = layer->clip_parent()
257424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                                 ? layer->clip_parent()->render_target()
258424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                                 : layer->parent()->render_target();
259424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
260424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // If the layer owns a surface, then the content rect is in the wrong space.
261424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // Instead, we will use the surface's DrawableContentRect which is in target
262424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // space as required.
263424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  gfx::Rect target_rect = drawable_content_rect;
264424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  if (layer->render_surface()) {
265424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    target_rect =
266424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        gfx::ToEnclosedRect(layer->render_surface()->DrawableContentRect());
267424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  }
268424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
269424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  if (render_target->is_clipped()) {
270424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    gfx::Rect clip_rect = render_target->clip_rect();
271424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    // If the layer has a clip parent, the clip rect may be in the wrong space,
272424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    // so we'll need to transform it before it is applied.
273424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    if (layer->clip_parent()) {
2748bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      clip_rect = TranslateRectToTargetSpace<LayerType>(
275424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)          *layer->clip_parent(),
276424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)          *layer,
277424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)          clip_rect,
278424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)          TranslateRectDirectionToDescendant);
279424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    }
280424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    target_rect.Intersect(clip_rect);
281424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  }
282424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
283424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // We must have at least one entry in the vector for the root.
284424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  DCHECK_LT(0ul, accumulated_surface_state->size());
285424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
286424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  typedef typename std::vector<AccumulatedSurfaceState<LayerType> >
287424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      AccumulatedSurfaceStateVector;
288424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  typedef typename AccumulatedSurfaceStateVector::reverse_iterator
289424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      AccumulatedSurfaceStateIterator;
290424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  AccumulatedSurfaceStateIterator current_state =
291424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      accumulated_surface_state->rbegin();
292424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
293424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // Add this rect to the accumulated content rect for all surfaces until we
294424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // reach the target surface.
295424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  bool found_render_target = false;
296424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  for (; current_state != accumulated_surface_state->rend(); ++current_state) {
297424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    current_state->drawable_content_rect.Union(target_rect);
298424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
299424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    // If we've reached |render_target| our work is done and we can bail.
300424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    if (current_state->render_target == render_target) {
301424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      found_render_target = true;
302424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      break;
303424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    }
304424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
305424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    // Transform rect from the current target's space to the next.
306424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    LayerType* current_target = current_state->render_target;
307424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    DCHECK(current_target->render_surface());
308424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    const gfx::Transform& current_draw_transform =
309424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)         current_target->render_surface()->draw_transform();
310424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
311424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    // If we have unclipped descendants, the draw transform is a translation.
312424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    DCHECK(current_target->num_unclipped_descendants() == 0 ||
313424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)           current_draw_transform.IsIdentityOrTranslation());
314424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
315424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    target_rect = gfx::ToEnclosingRect(
316424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        MathUtil::MapClippedRect(current_draw_transform, target_rect));
317424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  }
318424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
319424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // It is an error to not reach |render_target|. If this happens, it means that
320424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // either the clip parent is not an ancestor of the clip child or the surface
321424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // state vector is empty, both of which should be impossible.
322424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  DCHECK(found_render_target);
323424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}
324424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
3252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)template <typename LayerType> static inline bool IsRootLayer(LayerType* layer) {
3262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return !layer->parent();
3272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
3282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)template <typename LayerType>
3302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)static inline bool LayerIsInExisting3DRenderingContext(LayerType* layer) {
3312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // According to current W3C spec on CSS transforms, a layer is part of an
3322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // established 3d rendering context if its parent has transform-style of
3332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // preserves-3d.
3342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return layer->parent() && layer->parent()->preserves_3d();
3352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
3362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)template <typename LayerType>
3382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)static bool IsRootLayerOfNewRenderingContext(LayerType* layer) {
3392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // According to current W3C spec on CSS transforms (Section 6.1), a layer is
3402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // the beginning of 3d rendering context if its parent does not have
3412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // transform-style: preserve-3d, but this layer itself does.
3422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (layer->parent())
3432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return !layer->parent()->preserves_3d() && layer->preserves_3d();
3442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return layer->preserves_3d();
3462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
3472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)template <typename LayerType>
3492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)static bool IsLayerBackFaceVisible(LayerType* layer) {
3502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // The current W3C spec on CSS transforms says that backface visibility should
3512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // be determined differently depending on whether the layer is in a "3d
3522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // rendering context" or not. For Chromium code, we can determine whether we
3532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // are in a 3d rendering context by checking if the parent preserves 3d.
3542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (LayerIsInExisting3DRenderingContext(layer))
3562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return layer->draw_transform().IsBackFaceVisible();
3572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // In this case, either the layer establishes a new 3d rendering context, or
3592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // is not in a 3d rendering context at all.
3602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return layer->transform().IsBackFaceVisible();
3612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
3622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)template <typename LayerType>
3642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)static bool IsSurfaceBackFaceVisible(LayerType* layer,
3652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                     const gfx::Transform& draw_transform) {
3662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (LayerIsInExisting3DRenderingContext(layer))
3672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return draw_transform.IsBackFaceVisible();
3682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (IsRootLayerOfNewRenderingContext(layer))
3702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return layer->transform().IsBackFaceVisible();
3712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // If the render_surface is not part of a new or existing rendering context,
3732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // then the layers that contribute to this surface will decide back-face
3742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // visibility for themselves.
3752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return false;
3762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
3772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)template <typename LayerType>
3792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)static inline bool LayerClipsSubtree(LayerType* layer) {
3802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return layer->masks_to_bounds() || layer->mask_layer();
3812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
3822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)template <typename LayerType>
3842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)static gfx::Rect CalculateVisibleContentRect(
3852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    LayerType* layer,
386a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch    gfx::Rect clip_rect_of_target_surface_in_target_space,
3872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    gfx::Rect layer_rect_in_target_space) {
3882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(layer->render_target());
3892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Nothing is visible if the layer bounds are empty.
3912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!layer->DrawsContent() || layer->content_bounds().IsEmpty() ||
3922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      layer->drawable_content_rect().IsEmpty())
3932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return gfx::Rect();
3942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Compute visible bounds in target surface space.
3962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  gfx::Rect visible_rect_in_target_surface_space =
3972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      layer->drawable_content_rect();
3982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!layer->render_target()->render_surface()->clip_rect().IsEmpty()) {
400a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch    // The |layer| L has a target T which owns a surface Ts. The surface Ts
401a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch    // has a target TsT.
402a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch    //
403a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch    // In this case the target surface Ts does clip the layer L that contributes
404a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch    // to it. So, we have to convert the clip rect of Ts from the target space
405a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch    // of Ts (that is the space of TsT), to the current render target's space
406a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch    // (that is the space of T). This conversion is done outside this function
407a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch    // so that it can be cached instead of computing it redundantly for every
408a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch    // layer.
4092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    visible_rect_in_target_surface_space.Intersect(
410a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch        clip_rect_of_target_surface_in_target_space);
4112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
4122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (visible_rect_in_target_surface_space.IsEmpty())
4142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return gfx::Rect();
4152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return CalculateVisibleRectWithCachedLayerRect(
4172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      visible_rect_in_target_surface_space,
4182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      gfx::Rect(layer->content_bounds()),
4192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      layer_rect_in_target_space,
4202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      layer->draw_transform());
4212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
4222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)static inline bool TransformToParentIsKnown(LayerImpl* layer) { return true; }
4242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)static inline bool TransformToParentIsKnown(Layer* layer) {
4262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return !layer->TransformIsAnimating();
4272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
4282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)static inline bool TransformToScreenIsKnown(LayerImpl* layer) { return true; }
4302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)static inline bool TransformToScreenIsKnown(Layer* layer) {
4322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return !layer->screen_space_transform_is_animating();
4332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
4342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)template <typename LayerType>
4367dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochstatic bool LayerShouldBeSkipped(LayerType* layer,
4377dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                                 bool layer_is_visible) {
4382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Layers can be skipped if any of these conditions are met.
4397dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  //   - is not visible due to it or one of its ancestors being hidden.
4402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //   - has empty bounds
4412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //   - the layer is not double-sided, but its back face is visible.
4424e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  //   - is transparent
4434e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  //   - does not draw content and does not participate in hit testing.
4442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
4452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Some additional conditions need to be computed at a later point after the
4462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // recursion is finished.
4472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //   - the intersection of render_surface content and layer clip_rect is empty
4482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //   - the visible_content_rect is empty
4492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
4502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Note, if the layer should not have been drawn due to being fully
4512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // transparent, we would have skipped the entire subtree and never made it
4522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // into this function, so it is safe to omit this check here.
4532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4547dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if (!layer_is_visible)
4557dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    return true;
4567dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
4574e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  if (layer->bounds().IsEmpty())
4582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return true;
4592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  LayerType* backface_test_layer = layer;
4612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (layer->use_parent_backface_visibility()) {
4622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DCHECK(layer->parent());
4632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DCHECK(!layer->parent()->use_parent_backface_visibility());
4642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    backface_test_layer = layer->parent();
4652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
4662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // The layer should not be drawn if (1) it is not double-sided and (2) the
4682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // back of the layer is known to be facing the screen.
4692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!backface_test_layer->double_sided() &&
4702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      TransformToScreenIsKnown(backface_test_layer) &&
4712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      IsLayerBackFaceVisible(backface_test_layer))
4722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return true;
4732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4744e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // The layer is visible to events.  If it's subject to hit testing, then
4754e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // we can't skip it.
4764e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  bool can_accept_input = !layer->touch_event_handler_region().IsEmpty() ||
4774e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      layer->have_wheel_event_handlers();
4784e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  if (!layer->DrawsContent() && !can_accept_input)
4794e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    return true;
4804e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
4812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return false;
4822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
4832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4847dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochstatic inline bool SubtreeShouldBeSkipped(LayerImpl* layer,
4857dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                                          bool layer_is_visible) {
4867dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // When we need to do a readback/copy of a layer's output, we can not skip
4877dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // it or any of its ancestors.
4887dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if (layer->draw_properties().layer_or_descendant_has_copy_request)
4897dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    return false;
4907dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
4917dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // If the layer is not visible, then skip it and its subtree.
4927dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if (!layer_is_visible)
493eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    return true;
494eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
495c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // If layer is on the pending tree and opacity is being animated then
496c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // this subtree can't be skipped as we need to create, prioritize and
497c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // include tiles for this layer when deciding if tree can be activated.
498c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (layer->layer_tree_impl()->IsPendingTree() && layer->OpacityIsAnimating())
499c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return false;
500c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
5012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // The opacity of a layer always applies to its children (either implicitly
5022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // via a render surface or explicitly if the parent preserves 3D), so the
5032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // entire subtree can be skipped if this layer is fully transparent.
5044e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // TODO(sad): Don't skip layers used for hit testing crbug.com/295295.
5052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return !layer->opacity();
5062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
5072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5087dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochstatic inline bool SubtreeShouldBeSkipped(Layer* layer,
5097dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch                                          bool layer_is_visible) {
5107dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // When we need to do a readback/copy of a layer's output, we can not skip
5117dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // it or any of its ancestors.
5127dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if (layer->draw_properties().layer_or_descendant_has_copy_request)
5137dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    return false;
5147dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
5157dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // If the layer is not visible, then skip it and its subtree.
5167dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if (!layer_is_visible)
517eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    return true;
518eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
5192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // If the opacity is being animated then the opacity on the main thread is
5202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // unreliable (since the impl thread may be using a different opacity), so it
5212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // should not be trusted.
5222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // In particular, it should not cause the subtree to be skipped.
5232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Similarly, for layers that might animate opacity using an impl-only
5242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // animation, their subtree should also not be skipped.
5254e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // TODO(sad): Don't skip layers used for hit testing crbug.com/295295.
5262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return !layer->opacity() && !layer->OpacityIsAnimating() &&
5272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)         !layer->OpacityCanAnimateOnImplThread();
5282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
5292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
530c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)static inline void SavePaintPropertiesLayer(LayerImpl* layer) {}
531c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
532c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)static inline void SavePaintPropertiesLayer(Layer* layer) {
533c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  layer->SavePaintProperties();
534c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
535c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (layer->mask_layer())
536c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    layer->mask_layer()->SavePaintProperties();
537c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (layer->replica_layer() && layer->replica_layer()->mask_layer())
538c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    layer->replica_layer()->mask_layer()->SavePaintProperties();
539c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
540c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
5412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)template <typename LayerType>
5422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)static bool SubtreeShouldRenderToSeparateSurface(
5432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    LayerType* layer,
5442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    bool axis_aligned_with_respect_to_parent) {
5452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
5462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // A layer and its descendants should render onto a new RenderSurfaceImpl if
5472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // any of these rules hold:
5482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
5492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
550b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  // The root layer owns a render surface, but it never acts as a contributing
551b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  // surface to another render target. Compositor features that are applied via
552b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  // a contributing surface can not be applied to the root layer. In order to
553b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  // use these effects, another child of the root would need to be introduced
554b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  // in order to act as a contributing surface to the root layer's surface.
555b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  bool is_root = IsRootLayer(layer);
556c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
5572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // If the layer uses a mask.
558b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  if (layer->mask_layer()) {
559b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    DCHECK(!is_root);
5602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return true;
561b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  }
5622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // If the layer has a reflection.
564b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  if (layer->replica_layer()) {
565b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    DCHECK(!is_root);
5662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return true;
567b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  }
5682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // If the layer uses a CSS filter.
57068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  if (!layer->filters().IsEmpty() || !layer->background_filters().IsEmpty()) {
571b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    DCHECK(!is_root);
5722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return true;
573b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  }
5742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  int num_descendants_that_draw_content =
5762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      layer->draw_properties().num_descendants_that_draw_content;
5772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // If the layer flattens its subtree (i.e. the layer doesn't preserve-3d), but
5792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // it is treated as a 3D object by its parent (i.e. parent does preserve-3d).
5802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (LayerIsInExisting3DRenderingContext(layer) && !layer->preserves_3d() &&
5812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      num_descendants_that_draw_content > 0) {
5822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    TRACE_EVENT_INSTANT0(
5832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        "cc",
584c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        "LayerTreeHostCommon::SubtreeShouldRenderToSeparateSurface flattening",
585c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        TRACE_EVENT_SCOPE_THREAD);
586b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    DCHECK(!is_root);
5872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return true;
5882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
5892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
590f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // If the layer has blending.
591f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // TODO(rosca): this is temporary, until blending is implemented for other
592f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // types of quads than RenderPassDrawQuad. Layers having descendants that draw
593f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // content will still create a separate rendering surface.
594f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (!layer->uses_default_blend_mode()) {
595f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    TRACE_EVENT_INSTANT0(
596f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        "cc",
597f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        "LayerTreeHostCommon::SubtreeShouldRenderToSeparateSurface blending",
598f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        TRACE_EVENT_SCOPE_THREAD);
599f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    DCHECK(!is_root);
600f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    return true;
601f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
602f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
6032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // If the layer clips its descendants but it is not axis-aligned with respect
6042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // to its parent.
6052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool layer_clips_external_content =
6062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      LayerClipsSubtree(layer) || layer->HasDelegatedContent();
6072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (layer_clips_external_content && !axis_aligned_with_respect_to_parent &&
6084e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      num_descendants_that_draw_content > 0) {
6092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    TRACE_EVENT_INSTANT0(
6102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        "cc",
611c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        "LayerTreeHostCommon::SubtreeShouldRenderToSeparateSurface clipping",
612c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        TRACE_EVENT_SCOPE_THREAD);
613b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    DCHECK(!is_root);
6142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return true;
6152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
6162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // If the layer has some translucency and does not have a preserves-3d
6182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // transform style.  This condition only needs a render surface if two or more
6192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // layers in the subtree overlap. But checking layer overlaps is unnecessarily
6202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // costly so instead we conservatively create a surface whenever at least two
6212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // layers draw content for this subtree.
6222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool at_least_two_layers_in_subtree_draw_content =
6232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      num_descendants_that_draw_content > 0 &&
6242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      (layer->DrawsContent() || num_descendants_that_draw_content > 1);
6252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (layer->opacity() != 1.f && !layer->preserves_3d() &&
6272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      at_least_two_layers_in_subtree_draw_content) {
6282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    TRACE_EVENT_INSTANT0(
6292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        "cc",
630c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        "LayerTreeHostCommon::SubtreeShouldRenderToSeparateSurface opacity",
631c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        TRACE_EVENT_SCOPE_THREAD);
632b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    DCHECK(!is_root);
6332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return true;
6342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
6352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
636b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  // The root layer should always have a render_surface.
637b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  if (is_root)
638b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    return true;
639b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
640b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  //
641b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  // These are allowed on the root surface, as they don't require the surface to
642b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  // be used as a contributing surface in order to apply correctly.
643b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  //
644b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
645f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // If the layer has isolation.
646f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // TODO(rosca): to be optimized - create separate rendering surface only when
647f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // the blending descendants might have access to the content behind this layer
648f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // (layer has transparent background or descendants overflow).
649f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // https://code.google.com/p/chromium/issues/detail?id=301738
650f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (layer->is_root_for_isolated_group()) {
651f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    TRACE_EVENT_INSTANT0(
652f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        "cc",
653f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        "LayerTreeHostCommon::SubtreeShouldRenderToSeparateSurface isolation",
654f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        TRACE_EVENT_SCOPE_THREAD);
655f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    return true;
656f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
657f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
658b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  // If we force it.
659b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  if (layer->force_render_surface())
660b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    return true;
661b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
662b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  // If we'll make a copy of the layer's contents.
66390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  if (layer->HasCopyRequest())
664b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    return true;
665b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
6662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return false;
6672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
6682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
669c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// This function returns a translation matrix that can be applied on a vector
670c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// that's in the layer's target surface coordinate, while the position offset is
671c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)// specified in some ancestor layer's coordinate.
672c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)gfx::Transform ComputeSizeDeltaCompensation(
673c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    LayerImpl* layer,
674c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    LayerImpl* container,
675c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    gfx::Vector2dF position_offset) {
676c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  gfx::Transform result_transform;
677c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
678c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // To apply a translate in the container's layer space,
679c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // the following steps need to be done:
680c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  //     Step 1a. transform from target surface space to the container's target
681c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  //              surface space
682c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  //     Step 1b. transform from container's target surface space to the
683c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  //              container's layer space
684c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  //     Step 2. apply the compensation
685c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  //     Step 3. transform back to target surface space
686c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
687c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  gfx::Transform target_surface_space_to_container_layer_space;
688c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Calculate step 1a
689868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  LayerImpl* container_target_surface = container->render_target();
690c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  for (LayerImpl* current_target_surface = NextTargetSurface(layer);
691c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      current_target_surface &&
692c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          current_target_surface != container_target_surface;
693c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      current_target_surface = NextTargetSurface(current_target_surface)) {
694c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    // Note: Concat is used here to convert the result coordinate space from
695c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    //       current render surface to the next render surface.
696c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    target_surface_space_to_container_layer_space.ConcatTransform(
697c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        current_target_surface->render_surface()->draw_transform());
698c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
699c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Calculate step 1b
700868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  gfx::Transform container_layer_space_to_container_target_surface_space =
701868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      container->draw_transform();
702868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  container_layer_space_to_container_target_surface_space.Scale(
703868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      container->contents_scale_x(), container->contents_scale_y());
704868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
705868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  gfx::Transform container_target_surface_space_to_container_layer_space;
706868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (container_layer_space_to_container_target_surface_space.GetInverse(
707868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      &container_target_surface_space_to_container_layer_space)) {
708868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    // Note: Again, Concat is used to conver the result coordinate space from
709868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    //       the container render surface to the container layer.
710868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    target_surface_space_to_container_layer_space.ConcatTransform(
711868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        container_target_surface_space_to_container_layer_space);
712c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
713c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
714c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Apply step 3
715c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  gfx::Transform container_layer_space_to_target_surface_space;
716c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (target_surface_space_to_container_layer_space.GetInverse(
717c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          &container_layer_space_to_target_surface_space)) {
718c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    result_transform.PreconcatTransform(
719c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        container_layer_space_to_target_surface_space);
720c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  } else {
721eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    // TODO(shawnsingh): A non-invertible matrix could still make meaningful
722eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    // projection.  For example ScaleZ(0) is non-invertible but the layer is
723eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    // still visible.
724c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return gfx::Transform();
725c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
726c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
727c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Apply step 2
728c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  result_transform.Translate(position_offset.x(), position_offset.y());
729c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
730c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Apply step 1
731c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  result_transform.PreconcatTransform(
732c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      target_surface_space_to_container_layer_space);
733c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
734c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return result_transform;
735c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
736c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
737c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void ApplyPositionAdjustment(
738c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    Layer* layer,
739c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    Layer* container,
740c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const gfx::Transform& scroll_compensation,
741c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    gfx::Transform* combined_transform) {}
742c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void ApplyPositionAdjustment(
743c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    LayerImpl* layer,
744c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    LayerImpl* container,
745c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const gfx::Transform& scroll_compensation,
746c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    gfx::Transform* combined_transform) {
747c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (!layer->position_constraint().is_fixed_position())
748c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return;
749c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
750c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Special case: this layer is a composited fixed-position layer; we need to
751c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // explicitly compensate for all ancestors' nonzero scroll_deltas to keep
752c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // this layer fixed correctly.
753c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Note carefully: this is Concat, not Preconcat
754c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // (current_scroll_compensation * combined_transform).
755c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  combined_transform->ConcatTransform(scroll_compensation);
756c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
757c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // For right-edge or bottom-edge anchored fixed position layers,
758c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // the layer should relocate itself if the container changes its size.
759c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  bool fixed_to_right_edge =
760c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      layer->position_constraint().is_fixed_to_right_edge();
761c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  bool fixed_to_bottom_edge =
762c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      layer->position_constraint().is_fixed_to_bottom_edge();
763868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  gfx::Vector2dF position_offset = container->fixed_container_size_delta();
764c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  position_offset.set_x(fixed_to_right_edge ? position_offset.x() : 0);
765c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  position_offset.set_y(fixed_to_bottom_edge ? position_offset.y() : 0);
766c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (position_offset.IsZero())
767c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return;
768c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
769c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Note: Again, this is Concat. The compensation matrix will be applied on
770c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  //       the vector in target surface space.
771c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  combined_transform->ConcatTransform(
772c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      ComputeSizeDeltaCompensation(layer, container, position_offset));
773c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
774c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
7752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)gfx::Transform ComputeScrollCompensationForThisLayer(
7762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    LayerImpl* scrolling_layer,
7771e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    const gfx::Transform& parent_matrix,
7781e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    gfx::Vector2dF scroll_delta) {
7792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // For every layer that has non-zero scroll_delta, we have to compute a
7802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // transform that can undo the scroll_delta translation. In particular, we
7812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // want this matrix to premultiply a fixed-position layer's parent_matrix, so
7822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // we design this transform in three steps as follows. The steps described
7832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // here apply from right-to-left, so Step 1 would be the right-most matrix:
7842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
7852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //     Step 1. transform from target surface space to the exact space where
7862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //           scroll_delta is actually applied.
7879ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  //           -- this is inverse of parent_matrix
7882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //     Step 2. undo the scroll_delta
7892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //           -- this is just a translation by scroll_delta.
7902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //     Step 3. transform back to target surface space.
7919ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  //           -- this transform is the parent_matrix
7922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
7932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // These steps create a matrix that both start and end in target surface
7942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // space. So this matrix can pre-multiply any fixed-position layer's
7952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // draw_transform to undo the scroll_deltas -- as long as that fixed position
7962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // layer is fixed onto the same render_target as this scrolling_layer.
7972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
7982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
7999ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  gfx::Transform scroll_compensation_for_this_layer = parent_matrix;  // Step 3
8002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scroll_compensation_for_this_layer.Translate(
801424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      scroll_delta.x(),
802424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      scroll_delta.y());  // Step 2
8032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
8049ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  gfx::Transform inverse_parent_matrix(gfx::Transform::kSkipInitialization);
8059ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch  if (!parent_matrix.GetInverse(&inverse_parent_matrix)) {
8062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // TODO(shawnsingh): Either we need to handle uninvertible transforms
8072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // here, or DCHECK that the transform is invertible.
8082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
8092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scroll_compensation_for_this_layer.PreconcatTransform(
8109ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch      inverse_parent_matrix);  // Step 1
8112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return scroll_compensation_for_this_layer;
8122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
8132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
8142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)gfx::Transform ComputeScrollCompensationMatrixForChildren(
8152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    Layer* current_layer,
8162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const gfx::Transform& current_parent_matrix,
8171e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    const gfx::Transform& current_scroll_compensation,
8181e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    gfx::Vector2dF scroll_delta) {
8192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // The main thread (i.e. Layer) does not need to worry about scroll
8202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // compensation.  So we can just return an identity matrix here.
8212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return gfx::Transform();
8222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
8232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
8242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)gfx::Transform ComputeScrollCompensationMatrixForChildren(
8252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    LayerImpl* layer,
8262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const gfx::Transform& parent_matrix,
8271e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    const gfx::Transform& current_scroll_compensation_matrix,
8281e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    gfx::Vector2dF scroll_delta) {
8292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // "Total scroll compensation" is the transform needed to cancel out all
8302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // scroll_delta translations that occurred since the nearest container layer,
8312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // even if there are render_surfaces in-between.
8322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
8332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // There are some edge cases to be aware of, that are not explicit in the
8342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // code:
8352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //  - A layer that is both a fixed-position and container should not be its
8362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //  own container, instead, that means it is fixed to an ancestor, and is a
8372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //  container for any fixed-position descendants.
8382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //  - A layer that is a fixed-position container and has a render_surface
8392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //  should behave the same as a container without a render_surface, the
8402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //  render_surface is irrelevant in that case.
8412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //  - A layer that does not have an explicit container is simply fixed to the
8422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //  viewport.  (i.e. the root render_surface.)
8432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //  - If the fixed-position layer has its own render_surface, then the
8442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //  render_surface is the one who gets fixed.
8452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
8462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // This function needs to be called AFTER layers create their own
8472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // render_surfaces.
8482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
8492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
8504e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // Scroll compensation restarts from identity under two possible conditions:
8514e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  //  - the current layer is a container for fixed-position descendants
8524e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  //  - the current layer is fixed-position itself, so any fixed-position
8534e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  //    descendants are positioned with respect to this layer. Thus, any
8544e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  //    fixed position descendants only need to compensate for scrollDeltas
8554e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  //    that occur below this layer.
8564e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  bool current_layer_resets_scroll_compensation_for_descendants =
8574e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      layer->IsContainerForFixedPositionLayers() ||
8584e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      layer->position_constraint().is_fixed_position();
8594e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
8602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Avoid the overheads (including stack allocation and matrix
8612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // initialization/copy) if we know that the scroll compensation doesn't need
8622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // to be reset or adjusted.
8634e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  if (!current_layer_resets_scroll_compensation_for_descendants &&
864424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      scroll_delta.IsZero() && !layer->render_surface())
8652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return current_scroll_compensation_matrix;
8662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
8672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Start as identity matrix.
8682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  gfx::Transform next_scroll_compensation_matrix;
8692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
8704e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // If this layer does not reset scroll compensation, then it inherits the
8714e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // existing scroll compensations.
8724e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  if (!current_layer_resets_scroll_compensation_for_descendants)
8732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    next_scroll_compensation_matrix = current_scroll_compensation_matrix;
8742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
8752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // If the current layer has a non-zero scroll_delta, then we should compute
8762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // its local scroll compensation and accumulate it to the
8772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // next_scroll_compensation_matrix.
878424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  if (!scroll_delta.IsZero()) {
8792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    gfx::Transform scroll_compensation_for_this_layer =
880c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        ComputeScrollCompensationForThisLayer(
8811e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)            layer, parent_matrix, scroll_delta);
8822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    next_scroll_compensation_matrix.PreconcatTransform(
8832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        scroll_compensation_for_this_layer);
8842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
8852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
8862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // If the layer created its own render_surface, we have to adjust
8872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // next_scroll_compensation_matrix.  The adjustment allows us to continue
8882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // using the scroll compensation on the next surface.
8892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //  Step 1 (right-most in the math): transform from the new surface to the
8902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //  original ancestor surface
8912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //  Step 2: apply the scroll compensation
8922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //  Step 3: transform back to the new surface.
8932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (layer->render_surface() &&
8942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      !next_scroll_compensation_matrix.IsIdentity()) {
8952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    gfx::Transform inverse_surface_draw_transform(
8962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        gfx::Transform::kSkipInitialization);
8972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (!layer->render_surface()->draw_transform().GetInverse(
8982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            &inverse_surface_draw_transform)) {
8992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // TODO(shawnsingh): Either we need to handle uninvertible transforms
9002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // here, or DCHECK that the transform is invertible.
9012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
9022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    next_scroll_compensation_matrix =
9032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        inverse_surface_draw_transform * next_scroll_compensation_matrix *
9042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        layer->render_surface()->draw_transform();
9052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
9062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
9072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return next_scroll_compensation_matrix;
9082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
9092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
9102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)template <typename LayerType>
9112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)static inline void CalculateContentsScale(LayerType* layer,
9122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                          float contents_scale,
913b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)                                          float device_scale_factor,
914b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)                                          float page_scale_factor,
9152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                          bool animating_transform_to_screen) {
9162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  layer->CalculateContentsScale(contents_scale,
917b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)                                device_scale_factor,
918b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)                                page_scale_factor,
9192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                animating_transform_to_screen,
9202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                &layer->draw_properties().contents_scale_x,
9212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                &layer->draw_properties().contents_scale_y,
9222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                &layer->draw_properties().content_bounds);
9232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
9242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  LayerType* mask_layer = layer->mask_layer();
9252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (mask_layer) {
9262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    mask_layer->CalculateContentsScale(
9272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        contents_scale,
928b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)        device_scale_factor,
929b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)        page_scale_factor,
9302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        animating_transform_to_screen,
9312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        &mask_layer->draw_properties().contents_scale_x,
9322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        &mask_layer->draw_properties().contents_scale_y,
9332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        &mask_layer->draw_properties().content_bounds);
9342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
9352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
9362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  LayerType* replica_mask_layer =
9372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      layer->replica_layer() ? layer->replica_layer()->mask_layer() : NULL;
9382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (replica_mask_layer) {
9392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    replica_mask_layer->CalculateContentsScale(
9402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        contents_scale,
941b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)        device_scale_factor,
942b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)        page_scale_factor,
9432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        animating_transform_to_screen,
9442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        &replica_mask_layer->draw_properties().contents_scale_x,
9452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        &replica_mask_layer->draw_properties().contents_scale_y,
9462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        &replica_mask_layer->draw_properties().content_bounds);
9472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
9482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
9492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
9502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)static inline void UpdateLayerContentsScale(
9512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    LayerImpl* layer,
952b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    bool can_adjust_raster_scale,
953c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    float ideal_contents_scale,
9542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    float device_scale_factor,
9552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    float page_scale_factor,
9562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    bool animating_transform_to_screen) {
957c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  CalculateContentsScale(layer,
958c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                         ideal_contents_scale,
959b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)                         device_scale_factor,
960b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)                         page_scale_factor,
961c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                         animating_transform_to_screen);
9622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
9632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
9642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)static inline void UpdateLayerContentsScale(
9652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    Layer* layer,
966b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    bool can_adjust_raster_scale,
967c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    float ideal_contents_scale,
9682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    float device_scale_factor,
9692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    float page_scale_factor,
9702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    bool animating_transform_to_screen) {
971b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  if (can_adjust_raster_scale) {
972c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    float ideal_raster_scale =
973c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)        ideal_contents_scale / (device_scale_factor * page_scale_factor);
9742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
975b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    bool need_to_set_raster_scale = layer->raster_scale_is_unknown();
9762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
9772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // If we've previously saved a raster_scale but the ideal changes, things
9782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // are unpredictable and we should just use 1.
979b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    if (!need_to_set_raster_scale && layer->raster_scale() != 1.f &&
980b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)        ideal_raster_scale != layer->raster_scale()) {
9812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      ideal_raster_scale = 1.f;
9822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      need_to_set_raster_scale = true;
9832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
9842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
9852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (need_to_set_raster_scale) {
9862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      bool use_and_save_ideal_scale =
9872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          ideal_raster_scale >= 1.f && !animating_transform_to_screen;
988b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)      if (use_and_save_ideal_scale)
989b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)        layer->set_raster_scale(ideal_raster_scale);
9902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
9912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
9922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
993b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  float raster_scale = 1.f;
994b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  if (!layer->raster_scale_is_unknown())
995b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    raster_scale = layer->raster_scale();
996b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
9972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
998c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  float contents_scale = raster_scale * device_scale_factor * page_scale_factor;
999b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  CalculateContentsScale(layer,
1000b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)                         contents_scale,
1001b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)                         device_scale_factor,
1002b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)                         page_scale_factor,
1003b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)                         animating_transform_to_screen);
10042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
10052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1006ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdochstatic inline RenderSurface* CreateOrReuseRenderSurface(Layer* layer) {
1007ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  // The render surface should always be new on the main thread, as the
1008ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  // RenderSurfaceLayerList should be a new empty list when given to
1009ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  // CalculateDrawProperties.
1010ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  DCHECK(!layer->render_surface());
1011ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  layer->CreateRenderSurface();
1012ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  return layer->render_surface();
1013ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch}
1014ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
1015ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdochstatic inline RenderSurfaceImpl* CreateOrReuseRenderSurface(LayerImpl* layer) {
1016ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  if (!layer->render_surface()) {
1017ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    layer->CreateRenderSurface();
1018ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch    return layer->render_surface();
1019ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  }
1020ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
1021ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  layer->render_surface()->ClearLayerLists();
1022ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  return layer->render_surface();
1023ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch}
1024ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch
10258bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)template <typename LayerType>
10262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)static inline void RemoveSurfaceForEarlyExit(
10272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    LayerType* layer_to_remove,
10288bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    typename LayerType::RenderSurfaceListType* render_surface_layer_list) {
10292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(layer_to_remove->render_surface());
10302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Technically, we know that the layer we want to remove should be
10312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // at the back of the render_surface_layer_list. However, we have had
10322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // bugs before that added unnecessary layers here
10332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // (https://bugs.webkit.org/show_bug.cgi?id=74147), but that causes
10342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // things to crash. So here we proactively remove any additional
10352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // layers from the end of the list.
10362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  while (render_surface_layer_list->back() != layer_to_remove) {
10372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    render_surface_layer_list->back()->ClearRenderSurface();
10382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    render_surface_layer_list->pop_back();
10392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
10402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK_EQ(render_surface_layer_list->back(), layer_to_remove);
10412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  render_surface_layer_list->pop_back();
10422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  layer_to_remove->ClearRenderSurface();
10432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
10442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
10457dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochstruct PreCalculateMetaInformationRecursiveData {
10467dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  bool layer_or_descendant_has_copy_request;
1047424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  int num_unclipped_descendants;
10487dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
10497dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  PreCalculateMetaInformationRecursiveData()
1050424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      : layer_or_descendant_has_copy_request(false),
1051424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        num_unclipped_descendants(0) {}
10527dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
10537dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  void Merge(const PreCalculateMetaInformationRecursiveData& data) {
10547dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    layer_or_descendant_has_copy_request |=
10557dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch        data.layer_or_descendant_has_copy_request;
1056424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    num_unclipped_descendants +=
1057424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        data.num_unclipped_descendants;
10587dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  }
10597dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch};
10607dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
10612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Recursively walks the layer tree to compute any information that is needed
10622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// before doing the main recursion.
10632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)template <typename LayerType>
10647dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochstatic void PreCalculateMetaInformation(
10657dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    LayerType* layer,
10667dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    PreCalculateMetaInformationRecursiveData* recursive_data) {
10677dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  bool has_delegated_content = layer->HasDelegatedContent();
10682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  int num_descendants_that_draw_content = 0;
10697dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
10707dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if (has_delegated_content) {
10717dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    // Layers with delegated content need to be treated as if they have as
10727dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    // many children as the number of layers they own delegated quads for.
10737dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    // Since we don't know this number right now, we choose one that acts like
10747dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    // infinity for our purposes.
10757dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    num_descendants_that_draw_content = 1000;
10767dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  }
10772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
10788bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  layer->draw_properties().sorted_for_recursion = false;
10798bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  layer->draw_properties().has_child_with_a_scroll_parent = false;
10808bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1081424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  if (layer->clip_parent())
1082424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    recursive_data->num_unclipped_descendants++;
1083424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
10842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  for (size_t i = 0; i < layer->children().size(); ++i) {
1085868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    LayerType* child_layer =
1086868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)        LayerTreeHostCommon::get_child_as_raw_ptr(layer->children(), i);
10872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
10887dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    PreCalculateMetaInformationRecursiveData data_for_child;
10897dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    PreCalculateMetaInformation(child_layer, &data_for_child);
10907dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
10914e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    num_descendants_that_draw_content += child_layer->DrawsContent() ? 1 : 0;
10924e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    num_descendants_that_draw_content +=
10934e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)        child_layer->draw_properties().num_descendants_that_draw_content;
10947dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
10958bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    if (child_layer->scroll_parent())
10968bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      layer->draw_properties().has_child_with_a_scroll_parent = true;
10977dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    recursive_data->Merge(data_for_child);
10982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
10992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1100424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  if (layer->clip_children()) {
1101424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    int num_clip_children = layer->clip_children()->size();
1102424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    DCHECK_GE(recursive_data->num_unclipped_descendants, num_clip_children);
1103424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    recursive_data->num_unclipped_descendants -= num_clip_children;
1104424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  }
1105424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
11067dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if (layer->HasCopyRequest())
11077dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    recursive_data->layer_or_descendant_has_copy_request = true;
11087dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
11092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  layer->draw_properties().num_descendants_that_draw_content =
11102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      num_descendants_that_draw_content;
1111424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  layer->draw_properties().num_unclipped_descendants =
1112424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      recursive_data->num_unclipped_descendants;
11137dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  layer->draw_properties().layer_or_descendant_has_copy_request =
11147dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      recursive_data->layer_or_descendant_has_copy_request;
11152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
11162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
11172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)static void RoundTranslationComponents(gfx::Transform* transform) {
111868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  transform->matrix().set(0, 3, MathUtil::Round(transform->matrix().get(0, 3)));
111968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  transform->matrix().set(1, 3, MathUtil::Round(transform->matrix().get(1, 3)));
11202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
11212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
11222385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdochtemplate <typename LayerType>
11232385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdochstruct SubtreeGlobals {
11242385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  LayerSorter* layer_sorter;
11252385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  int max_texture_size;
11262385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  float device_scale_factor;
11272385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  float page_scale_factor;
11281e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  const LayerType* page_scale_application_layer;
11292385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  bool can_adjust_raster_scales;
11304e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  bool can_render_to_separate_surface;
11312385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch};
11322385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch
11338bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)template<typename LayerType>
11342385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdochstruct DataForRecursion {
11352385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  // The accumulated sequence of transforms a layer will use to determine its
11362385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  // own draw transform.
11372385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  gfx::Transform parent_matrix;
11382385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch
11392385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  // The accumulated sequence of transforms a layer will use to determine its
11402385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  // own screen-space transform.
11412385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  gfx::Transform full_hierarchy_matrix;
11422385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch
11432385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  // The transform that removes all scrolling that may have occurred between a
11442385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  // fixed-position layer and its container, so that the layer actually does
11452385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  // remain fixed.
11462385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  gfx::Transform scroll_compensation_matrix;
11472385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch
11482385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  // The ancestor that would be the container for any fixed-position / sticky
11492385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  // layers.
11502385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  LayerType* fixed_container;
11512385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch
11522385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  // This is the normal clip rect that is propagated from parent to child.
11532385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  gfx::Rect clip_rect_in_target_space;
11542385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch
11552385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  // When the layer's children want to compute their visible content rect, they
11562385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  // want to know what their target surface's clip rect will be. BUT - they
11572385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  // want to know this clip rect represented in their own target space. This
11582385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  // requires inverse-projecting the surface's clip rect from the surface's
11592385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  // render target space down to the surface's own space. Instead of computing
11602385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  // this value redundantly for each child layer, it is computed only once
11612385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  // while dealing with the parent layer, and then this precomputed value is
11622385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  // passed down the recursion to the children that actually use it.
11632385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  gfx::Rect clip_rect_of_target_surface_in_target_space;
11642385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch
11652385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  bool ancestor_clips_subtree;
11668bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  typename LayerType::RenderSurfaceType*
1167f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      nearest_occlusion_immune_ancestor_surface;
11682385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  bool in_subtree_of_page_scale_application_layer;
11692385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  bool subtree_can_use_lcd_text;
11702385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  bool subtree_is_visible_from_ancestor;
11712385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch};
11722385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch
11738bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)template <typename LayerType>
11748bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)static LayerType* GetChildContainingLayer(const LayerType& parent,
11758bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                                          LayerType* layer) {
11768bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  for (LayerType* ancestor = layer; ancestor; ancestor = ancestor->parent()) {
11778bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    if (ancestor->parent() == &parent)
11788bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      return ancestor;
11798bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  }
11808bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  NOTREACHED();
11818bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  return 0;
11828bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
11838bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
11848bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)template <typename LayerType>
11858bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)static void AddScrollParentChain(std::vector<LayerType*>* out,
11868bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                                 const LayerType& parent,
11878bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                                 LayerType* layer) {
11888bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // At a high level, this function walks up the chain of scroll parents
11898bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // recursively, and once we reach the end of the chain, we add the child
11908bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // of |parent| containing each scroll ancestor as we unwind. The result is
11918bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // an ordering of parent's children that ensures that scroll parents are
11928bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // visited before their descendants.
11938bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // Take for example this layer tree:
11948bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  //
11958bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // + stacking_context
11968bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  //   + scroll_child (1)
11978bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  //   + scroll_parent_graphics_layer (*)
11988bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  //   | + scroll_parent_scrolling_layer
11998bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  //   |   + scroll_parent_scrolling_content_layer (2)
12008bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  //   + scroll_grandparent_graphics_layer (**)
12018bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  //     + scroll_grandparent_scrolling_layer
12028bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  //       + scroll_grandparent_scrolling_content_layer (3)
12038bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  //
12048bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // The scroll child is (1), its scroll parent is (2) and its scroll
12058bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // grandparent is (3). Note, this doesn't mean that (2)'s scroll parent is
12068bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // (3), it means that (*)'s scroll parent is (3). We don't want our list to
12078bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // look like [ (3), (2), (1) ], even though that does have the ancestor chain
12088bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // in the right order. Instead, we want [ (**), (*), (1) ]. That is, only want
12098bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // (1)'s siblings in the list, but we want them to appear in such an order
12108bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // that the scroll ancestors get visited in the correct order.
12118bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  //
12128bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // So our first task at this step of the recursion is to determine the layer
12138bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // that we will potentionally add to the list. That is, the child of parent
12148bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // containing |layer|.
12158bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  LayerType* child = GetChildContainingLayer(parent, layer);
12168bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  if (child->draw_properties().sorted_for_recursion)
12178bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    return;
12188bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
12198bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  if (LayerType* scroll_parent = child->scroll_parent())
12208bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    AddScrollParentChain(out, parent, scroll_parent);
12218bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
12228bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  out->push_back(child);
12238bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  child->draw_properties().sorted_for_recursion = true;
12248bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
12258bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
12268bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)template <typename LayerType>
12278bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)static bool SortChildrenForRecursion(std::vector<LayerType*>* out,
12288bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                                     const LayerType& parent) {
12298bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  out->reserve(parent.children().size());
12308bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  bool order_changed = false;
12318bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  for (size_t i = 0; i < parent.children().size(); ++i) {
12328bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    LayerType* current =
12338bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        LayerTreeHostCommon::get_child_as_raw_ptr(parent.children(), i);
12348bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
12358bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    if (current->draw_properties().sorted_for_recursion) {
12368bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      order_changed = true;
12378bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      continue;
12388bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    }
12398bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
12408bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    AddScrollParentChain(out, parent, current);
12418bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  }
12428bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
12438bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  DCHECK_EQ(parent.children().size(), out->size());
12448bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  return order_changed;
12458bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
12468bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
12478bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)template <typename LayerType>
12488bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)static void GetNewDescendantsStartIndexAndCount(LayerType* layer,
12498bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                                                size_t* start_index,
12508bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                                                size_t* count) {
12518bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  *start_index = layer->draw_properties().index_of_first_descendants_addition;
12528bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  *count = layer->draw_properties().num_descendants_added;
12538bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
12548bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
12558bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)template <typename LayerType>
12568bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)static void GetNewRenderSurfacesStartIndexAndCount(LayerType* layer,
12578bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                                                   size_t* start_index,
12588bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                                                   size_t* count) {
12598bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  *start_index = layer->draw_properties()
12608bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                     .index_of_first_render_surface_layer_list_addition;
12618bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  *count = layer->draw_properties().num_render_surfaces_added;
12628bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
12638bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
12648bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)template <typename LayerType,
12658bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)          typename GetIndexAndCountType>
12668bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)static void SortLayerListContributions(
12678bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    const LayerType& parent,
12688bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    typename LayerType::RenderSurfaceListType* unsorted,
12698bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    size_t start_index_for_all_contributions,
12708bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    GetIndexAndCountType get_index_and_count) {
12718bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
12728bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  typename LayerType::LayerListType buffer;
12738bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  for (size_t i = 0; i < parent.children().size(); ++i) {
12748bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    LayerType* child =
12758bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        LayerTreeHostCommon::get_child_as_raw_ptr(parent.children(), i);
12768bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
12778bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    size_t start_index = 0;
12788bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    size_t count = 0;
12798bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    get_index_and_count(child, &start_index, &count);
12808bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    for (size_t j = start_index; j < start_index + count; ++j)
12818bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      buffer.push_back(unsorted->at(j));
12828bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  }
12838bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
12848bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  DCHECK_EQ(buffer.size(),
12858bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)            unsorted->size() - start_index_for_all_contributions);
12868bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
12878bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  for (size_t i = 0; i < buffer.size(); ++i)
12888bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    (*unsorted)[i + start_index_for_all_contributions] = buffer[i];
12898bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
12908bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
12912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Recursively walks the layer tree starting at the given node and computes all
12922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// the necessary transformations, clip rects, render surfaces, etc.
12938bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)template <typename LayerType>
12942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)static void CalculateDrawPropertiesInternal(
12952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    LayerType* layer,
12962385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch    const SubtreeGlobals<LayerType>& globals,
12978bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    const DataForRecursion<LayerType>& data_from_ancestor,
12988bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    typename LayerType::RenderSurfaceListType* render_surface_layer_list,
12998bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    typename LayerType::RenderSurfaceListType* layer_list,
1300424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    std::vector<AccumulatedSurfaceState<LayerType> >*
1301424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        accumulated_surface_state) {
13022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // This function computes the new matrix transformations recursively for this
13032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // layer and all its descendants. It also computes the appropriate render
13042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // surfaces.
13052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Some important points to remember:
13062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
13072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // 0. Here, transforms are notated in Matrix x Vector order, and in words we
13082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // describe what the transform does from left to right.
13092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
13102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // 1. In our terminology, the "layer origin" refers to the top-left corner of
13112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // a layer, and the positive Y-axis points downwards. This interpretation is
13122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // valid because the orthographic projection applied at draw time flips the Y
13132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // axis appropriately.
13142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
13152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // 2. The anchor point, when given as a PointF object, is specified in "unit
13162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // layer space", where the bounds of the layer map to [0, 1]. However, as a
13172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Transform object, the transform to the anchor point is specified in "layer
13182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // space", where the bounds of the layer map to [bounds.width(),
13192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // bounds.height()].
13202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
13212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // 3. Definition of various transforms used:
13222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //        M[parent] is the parent matrix, with respect to the nearest render
13232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //        surface, passed down recursively.
13242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
13252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //        M[root] is the full hierarchy, with respect to the root, passed down
13262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //        recursively.
13272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
13282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //        Tr[origin] is the translation matrix from the parent's origin to
13292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //        this layer's origin.
13302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
13312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //        Tr[origin2anchor] is the translation from the layer's origin to its
13322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //        anchor point
13332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
13342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //        Tr[origin2center] is the translation from the layer's origin to its
13352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //        center
13362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
13372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //        M[layer] is the layer's matrix (applied at the anchor point)
13382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
13392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //        M[sublayer] is the layer's sublayer transform (also applied at the
13402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //        layer's anchor point)
13412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
13422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //        S[layer2content] is the ratio of a layer's content_bounds() to its
13432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //        Bounds().
13442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
13452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //    Some composite transforms can help in understanding the sequence of
13462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //    transforms:
13472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //        composite_layer_transform = Tr[origin2anchor] * M[layer] *
13482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //        Tr[origin2anchor].inverse()
13492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
13502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //        composite_sublayer_transform = Tr[origin2anchor] * M[sublayer] *
13512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //        Tr[origin2anchor].inverse()
13522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
13532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // 4. When a layer (or render surface) is drawn, it is drawn into a "target
13542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // render surface". Therefore the draw transform does not necessarily
13552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // transform from screen space to local layer space. Instead, the draw
13562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // transform is the transform between the "target render surface space" and
13572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // local layer space. Note that render surfaces, except for the root, also
13582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // draw themselves into a different target render surface, and so their draw
13592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // transform and origin transforms are also described with respect to the
13602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // target.
13612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
13622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Using these definitions, then:
13632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
13642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // The draw transform for the layer is:
13652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //        M[draw] = M[parent] * Tr[origin] * composite_layer_transform *
13662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //            S[layer2content] = M[parent] * Tr[layer->position() + anchor] *
13672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //            M[layer] * Tr[anchor2origin] * S[layer2content]
13682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
13692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //        Interpreting the math left-to-right, this transforms from the
13702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //        layer's render surface to the origin of the layer in content space.
13712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
13722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // The screen space transform is:
13732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //        M[screenspace] = M[root] * Tr[origin] * composite_layer_transform *
13742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //            S[layer2content]
13752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //                       = M[root] * Tr[layer->position() + anchor] * M[layer]
13762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //                           * Tr[anchor2origin] * S[layer2content]
13772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
13782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //        Interpreting the math left-to-right, this transforms from the root
13792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //        render surface's content space to the origin of the layer in content
13802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //        space.
13812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
13822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // The transform hierarchy that is passed on to children (i.e. the child's
13832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // parent_matrix) is:
13842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //        M[parent]_for_child = M[parent] * Tr[origin] *
13852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //            composite_layer_transform * composite_sublayer_transform
13862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //                            = M[parent] * Tr[layer->position() + anchor] *
13872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //                              M[layer] * Tr[anchor2origin] *
13882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //                              composite_sublayer_transform
13892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
13902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //        and a similar matrix for the full hierarchy with respect to the
13912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //        root.
13922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
13932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Finally, note that the final matrix used by the shader for the layer is P *
13942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // M[draw] * S . This final product is computed in drawTexturedQuad(), where:
13952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //        P is the projection matrix
13962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //        S is the scale adjustment (to scale up a canonical quad to the
13972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //            layer's size)
13982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
13992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // When a render surface has a replica layer, that layer's transform is used
14002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // to draw a second copy of the surface.  gfx::Transforms named here are
14012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // relative to the surface, unless they specify they are relative to the
14022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // replica layer.
14032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
14042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // We will denote a scale by device scale S[deviceScale]
14052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
14062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // The render surface draw transform to its target surface origin is:
14072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //        M[surfaceDraw] = M[owningLayer->Draw]
14082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
14092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // The render surface origin transform to its the root (screen space) origin
14102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // is:
14112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //        M[surface2root] =  M[owningLayer->screenspace] *
14122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //            S[deviceScale].inverse()
14132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
14142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // The replica draw transform to its target surface origin is:
14152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //        M[replicaDraw] = S[deviceScale] * M[surfaceDraw] *
14162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //            Tr[replica->position() + replica->anchor()] * Tr[replica] *
14172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //            Tr[origin2anchor].inverse() * S[contents_scale].inverse()
14182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
14192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // The replica draw transform to the root (screen space) origin is:
14202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //        M[replica2root] = M[surface2root] * Tr[replica->position()] *
14212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //            Tr[replica] * Tr[origin2anchor].inverse()
14222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
14232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1424c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // It makes no sense to have a non-unit page_scale_factor without specifying
1425c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // which layer roots the subtree the scale is applied to.
14262385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  DCHECK(globals.page_scale_application_layer ||
14272385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch         (globals.page_scale_factor == 1.f));
1428c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
14298bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  DataForRecursion<LayerType> data_for_children;
14308bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  typename LayerType::RenderSurfaceType*
1431f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      nearest_occlusion_immune_ancestor_surface =
1432f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)          data_from_ancestor.nearest_occlusion_immune_ancestor_surface;
14332385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  data_for_children.in_subtree_of_page_scale_application_layer =
14342385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch      data_from_ancestor.in_subtree_of_page_scale_application_layer;
14352385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  data_for_children.subtree_can_use_lcd_text =
14362385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch      data_from_ancestor.subtree_can_use_lcd_text;
14372385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch
14387dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // Layers with a copy request are always visible, as well as un-hiding their
14397dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // subtree. Otherise, layers that are marked as hidden will hide themselves
14407dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  // and their subtree.
14417dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  bool layer_is_visible =
14422385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch      data_from_ancestor.subtree_is_visible_from_ancestor &&
14432385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch      !layer->hide_layer_and_subtree();
14447dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if (layer->HasCopyRequest())
14457dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch    layer_is_visible = true;
14467dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
14472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // The root layer cannot skip CalcDrawProperties.
1448424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  if (!IsRootLayer(layer) && SubtreeShouldBeSkipped(layer, layer_is_visible)) {
1449424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    if (layer->render_surface())
1450424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      layer->ClearRenderSurface();
14512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
1452424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  }
1453424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
1454424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // We need to circumvent the normal recursive flow of information for clip
1455424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // children (they don't inherit their direct ancestor's clip information).
1456424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // This is unfortunate, and would be unnecessary if we were to formally
1457424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // separate the clipping hierarchy from the layer hierarchy.
1458424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  bool ancestor_clips_subtree = data_from_ancestor.ancestor_clips_subtree;
1459424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  gfx::Rect ancestor_clip_rect_in_target_space =
1460424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      data_from_ancestor.clip_rect_in_target_space;
1461424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
1462424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // Update our clipping state. If we have a clip parent we will need to pull
1463424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // from the clip state cache rather than using the clip state passed from our
1464424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // immediate ancestor.
14658bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  UpdateClipRectsForClipChild<LayerType>(
1466424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      layer, &ancestor_clip_rect_in_target_space, &ancestor_clips_subtree);
14672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
14682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // As this function proceeds, these are the properties for the current
14692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // layer that actually get computed. To avoid unnecessary copies
14702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // (particularly for matrices), we do computations directly on these values
14712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // when possible.
14728bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  DrawProperties<LayerType>& layer_draw_properties = layer->draw_properties();
14732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1474a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch  gfx::Rect clip_rect_in_target_space;
1475a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch  bool layer_or_ancestor_clips_descendants = false;
14762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
14772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // This value is cached on the stack so that we don't have to inverse-project
14782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // the surface's clip rect redundantly for every layer. This value is the
1479a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch  // same as the target surface's clip rect, except that instead of being
1480a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch  // described in the target surface's target's space, it is described in the
1481a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch  // current render target's space.
1482a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch  gfx::Rect clip_rect_of_target_surface_in_target_space;
14832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
14842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  float accumulated_draw_opacity = layer->opacity();
14852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool animating_opacity_to_target = layer->OpacityIsAnimating();
14862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool animating_opacity_to_screen = animating_opacity_to_target;
14872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (layer->parent()) {
14882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    accumulated_draw_opacity *= layer->parent()->draw_opacity();
14892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    animating_opacity_to_target |= layer->parent()->draw_opacity_is_animating();
14902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    animating_opacity_to_screen |=
14912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        layer->parent()->screen_space_opacity_is_animating();
14922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
14932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
14942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool animating_transform_to_target = layer->TransformIsAnimating();
14952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool animating_transform_to_screen = animating_transform_to_target;
14962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (layer->parent()) {
14972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    animating_transform_to_target |=
14982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        layer->parent()->draw_transform_is_animating();
14992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    animating_transform_to_screen |=
15002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        layer->parent()->screen_space_transform_is_animating();
15012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
15022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
15032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  gfx::Size bounds = layer->bounds();
15042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  gfx::PointF anchor_point = layer->anchor_point();
1505424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  gfx::Vector2dF scroll_offset = GetEffectiveTotalScrollOffset(layer);
1506424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  gfx::PointF position = layer->position() - scroll_offset;
15072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
15082385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  gfx::Transform combined_transform = data_from_ancestor.parent_matrix;
15092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!layer->transform().IsIdentity()) {
15102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // LT = Tr[origin] * Tr[origin2anchor]
15112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    combined_transform.Translate3d(
15122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        position.x() + anchor_point.x() * bounds.width(),
15132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        position.y() + anchor_point.y() * bounds.height(),
15142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        layer->anchor_point_z());
15152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // LT = Tr[origin] * Tr[origin2anchor] * M[layer]
15162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    combined_transform.PreconcatTransform(layer->transform());
15172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // LT = Tr[origin] * Tr[origin2anchor] * M[layer] * Tr[anchor2origin]
15182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    combined_transform.Translate3d(-anchor_point.x() * bounds.width(),
15192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                   -anchor_point.y() * bounds.height(),
15202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                   -layer->anchor_point_z());
15212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  } else {
15222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    combined_transform.Translate(position.x(), position.y());
15232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
15242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
15251e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  gfx::Vector2dF effective_scroll_delta = GetEffectiveScrollDelta(layer);
15262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!animating_transform_to_target && layer->scrollable() &&
15272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      combined_transform.IsScaleOrTranslation()) {
15282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Align the scrollable layer's position to screen space pixels to avoid
15292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // blurriness.  To avoid side-effects, do this only if the transform is
15302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // simple.
15311e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    gfx::Vector2dF previous_translation = combined_transform.To2dTranslation();
15322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    RoundTranslationComponents(&combined_transform);
15331e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    gfx::Vector2dF current_translation = combined_transform.To2dTranslation();
15341e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
15351e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    // This rounding changes the scroll delta, and so must be included
15361e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    // in the scroll compensation matrix.
15371e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    effective_scroll_delta -= current_translation - previous_translation;
15382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
15392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1540c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Apply adjustment from position constraints.
15412385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  ApplyPositionAdjustment(layer, data_from_ancestor.fixed_container,
15422385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch      data_from_ancestor.scroll_compensation_matrix, &combined_transform);
1543c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
1544c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Compute the 2d scale components of the transform hierarchy up to the target
1545c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // surface. From there, we can decide on a contents scale for the layer.
15462385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  float layer_scale_factors = globals.device_scale_factor;
15472385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  if (data_from_ancestor.in_subtree_of_page_scale_application_layer)
15482385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch    layer_scale_factors *= globals.page_scale_factor;
1549c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  gfx::Vector2dF combined_transform_scales =
1550c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      MathUtil::ComputeTransform2dScaleComponents(
1551c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          combined_transform,
1552b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)          layer_scale_factors);
1553b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
1554c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  float ideal_contents_scale =
15552385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch      globals.can_adjust_raster_scales
1556b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)      ? std::max(combined_transform_scales.x(),
1557b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)                 combined_transform_scales.y())
1558b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)      : layer_scale_factors;
1559c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  UpdateLayerContentsScale(
1560c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      layer,
15612385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch      globals.can_adjust_raster_scales,
1562c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      ideal_contents_scale,
15632385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch      globals.device_scale_factor,
15642385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch      data_from_ancestor.in_subtree_of_page_scale_application_layer ?
15652385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch          globals.page_scale_factor : 1.f,
1566c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      animating_transform_to_screen);
15672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
15682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // The draw_transform that gets computed below is effectively the layer's
15692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // draw_transform, unless the layer itself creates a render_surface. In that
15702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // case, the render_surface re-parents the transforms.
15712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  layer_draw_properties.target_space_transform = combined_transform;
15722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // M[draw] = M[parent] * LT * S[layer2content]
157358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  layer_draw_properties.target_space_transform.Scale(
157458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      SK_MScalar1 / layer->contents_scale_x(),
157558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      SK_MScalar1 / layer->contents_scale_y());
15762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
15772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // The layer's screen_space_transform represents the transform between root
15782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // layer's "screen space" and local content space.
15792385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  layer_draw_properties.screen_space_transform =
15802385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch      data_from_ancestor.full_hierarchy_matrix;
15812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!layer->preserves_3d())
15822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    layer_draw_properties.screen_space_transform.FlattenTo2d();
15832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  layer_draw_properties.screen_space_transform.PreconcatTransform
15842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      (layer_draw_properties.target_space_transform);
15852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
15862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Adjusting text AA method during animation may cause repaints, which in-turn
15872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // causes jank.
15882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool adjust_text_aa =
15892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      !animating_opacity_to_screen && !animating_transform_to_screen;
15902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // To avoid color fringing, LCD text should only be used on opaque layers with
15912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // just integral translation.
15922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool layer_can_use_lcd_text =
15932385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch      data_from_ancestor.subtree_can_use_lcd_text &&
15942385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch      accumulated_draw_opacity == 1.f &&
15952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      layer_draw_properties.target_space_transform.
15962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          IsIdentityOrIntegerTranslation();
15972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
15982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  gfx::RectF content_rect(layer->content_bounds());
15992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
16002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // full_hierarchy_matrix is the matrix that transforms objects between screen
16012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // space (except projection matrix) and the most recent RenderSurfaceImpl's
16022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // space.  next_hierarchy_matrix will only change if this layer uses a new
16032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // RenderSurfaceImpl, otherwise remains the same.
16042385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  data_for_children.full_hierarchy_matrix =
16052385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch      data_from_ancestor.full_hierarchy_matrix;
16062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1607b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  // If the subtree will scale layer contents by the transform hierarchy, then
1608b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  // we should scale things into the render surface by the transform hierarchy
1609b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  // to take advantage of that.
1610b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)  gfx::Vector2dF render_surface_sublayer_scale =
16112385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch      globals.can_adjust_raster_scales
1612b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)      ? combined_transform_scales
1613b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)      : gfx::Vector2dF(layer_scale_factors, layer_scale_factors);
16142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
16154e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  bool render_to_separate_surface;
16164e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  if (globals.can_render_to_separate_surface) {
16174e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    render_to_separate_surface = SubtreeShouldRenderToSeparateSurface(
16184e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)          layer, combined_transform.Preserves2dAxisAlignment());
16194e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  } else {
16204e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    render_to_separate_surface = IsRootLayer(layer);
16214e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  }
16224e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  if (render_to_separate_surface) {
16232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Check back-face visibility before continuing with this surface and its
16242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // subtree
16252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (!layer->double_sided() && TransformToParentIsKnown(layer) &&
1626424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        IsSurfaceBackFaceVisible(layer, combined_transform)) {
1627424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      layer->ClearRenderSurface();
16282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return;
1629424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    }
16302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
16318bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    typename LayerType::RenderSurfaceType* render_surface =
16328bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        CreateOrReuseRenderSurface(layer);
16332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1634b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    if (IsRootLayer(layer)) {
1635868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      // The root layer's render surface size is predetermined and so the root
1636868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      // layer can't directly support non-identity transforms.  It should just
1637868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      // forward top-level transforms to the rest of the tree.
16382385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch      data_for_children.parent_matrix = combined_transform;
16397dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
16407dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      // The root surface does not contribute to any other surface, it has no
16417dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      // target.
16427dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      layer->render_surface()->set_contributes_to_drawn_surface(false);
1643868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    } else {
1644868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      // The owning layer's draw transform has a scale from content to layer
1645868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      // space which we do not want; so here we use the combined_transform
1646868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      // instead of the draw_transform. However, we do need to add a different
1647868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      // scale factor that accounts for the surface's pixel dimensions.
1648868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      combined_transform.Scale(1.0 / render_surface_sublayer_scale.x(),
1649868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                               1.0 / render_surface_sublayer_scale.y());
1650868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      render_surface->SetDrawTransform(combined_transform);
1651868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1652868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      // The owning layer's transform was re-parented by the surface, so the
1653868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      // layer's new draw_transform only needs to scale the layer to surface
1654868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      // space.
1655868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      layer_draw_properties.target_space_transform.MakeIdentity();
1656868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      layer_draw_properties.target_space_transform.
1657868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)          Scale(render_surface_sublayer_scale.x() / layer->contents_scale_x(),
1658868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                render_surface_sublayer_scale.y() / layer->contents_scale_y());
1659868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
1660868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      // Inside the surface's subtree, we scale everything to the owning layer's
1661868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      // scale.  The sublayer matrix transforms layer rects into target surface
1662868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      // content space.  Conceptually, all layers in the subtree inherit the
1663868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      // scale at the point of the render surface in the transform hierarchy,
1664868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      // but we apply it explicitly to the owning layer and the remainder of the
1665868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      // subtree independently.
16662385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch      DCHECK(data_for_children.parent_matrix.IsIdentity());
16672385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch      data_for_children.parent_matrix.Scale(render_surface_sublayer_scale.x(),
1668868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                            render_surface_sublayer_scale.y());
16697dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch
16707dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch      layer->render_surface()->set_contributes_to_drawn_surface(
16712385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch          data_from_ancestor.subtree_is_visible_from_ancestor &&
16722385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch          layer_is_visible);
1673b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)    }
1674b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)
16752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // The opacity value is moved from the layer to its surface, so that the
16762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // entire subtree properly inherits opacity.
16772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    render_surface->SetDrawOpacity(accumulated_draw_opacity);
16782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    render_surface->SetDrawOpacityIsAnimating(animating_opacity_to_target);
16792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    animating_opacity_to_target = false;
16802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    layer_draw_properties.opacity = 1.f;
16812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    layer_draw_properties.opacity_is_animating = animating_opacity_to_target;
16822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    layer_draw_properties.screen_space_opacity_is_animating =
16832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        animating_opacity_to_screen;
16842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
16852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    render_surface->SetTargetSurfaceTransformsAreAnimating(
16862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        animating_transform_to_target);
16872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    render_surface->SetScreenSpaceTransformsAreAnimating(
16882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        animating_transform_to_screen);
16892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    animating_transform_to_target = false;
16902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    layer_draw_properties.target_space_transform_is_animating =
16912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        animating_transform_to_target;
16922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    layer_draw_properties.screen_space_transform_is_animating =
16932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        animating_transform_to_screen;
16942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
16952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Update the aggregate hierarchy matrix to include the transform of the
16962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // newly created RenderSurfaceImpl.
16972385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch    data_for_children.full_hierarchy_matrix.PreconcatTransform(
16982385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch        render_surface->draw_transform());
16992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
17002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (layer->mask_layer()) {
17018bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      DrawProperties<LayerType>& mask_layer_draw_properties =
17022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          layer->mask_layer()->draw_properties();
17032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      mask_layer_draw_properties.render_target = layer;
17042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      mask_layer_draw_properties.visible_content_rect =
17052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          gfx::Rect(layer->content_bounds());
17062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
17072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
17082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (layer->replica_layer() && layer->replica_layer()->mask_layer()) {
17098bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      DrawProperties<LayerType>& replica_mask_draw_properties =
17102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          layer->replica_layer()->mask_layer()->draw_properties();
17112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      replica_mask_draw_properties.render_target = layer;
17122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      replica_mask_draw_properties.visible_content_rect =
17132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          gfx::Rect(layer->content_bounds());
17142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
17152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1716f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // Ignore occlusion from outside the surface when surface contents need to
1717f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // be fully drawn. Layers with copy-request need to be complete.
1718f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // We could be smarter about layers with replica and exclude regions
1719f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // where both layer and the replica are occluded, but this seems like an
1720f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // overkill. The same is true for layers with filters that move pixels.
17212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // TODO(senorblanco): make this smarter for the SkImageFilter case (check
17222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // for pixel-moving filters)
1723f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    if (layer->HasCopyRequest() ||
1724f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        layer->has_replica() ||
1725f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        layer->filters().HasReferenceFilter() ||
1726f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        layer->filters().HasFilterThatMovesPixels()) {
1727f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      nearest_occlusion_immune_ancestor_surface = render_surface;
1728f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    }
1729f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    render_surface->SetNearestOcclusionImmuneAncestor(
1730f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        nearest_occlusion_immune_ancestor_surface);
17312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1732424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    layer_or_ancestor_clips_descendants = false;
1733424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    bool subtree_is_clipped_by_surface_bounds = false;
1734424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    if (ancestor_clips_subtree) {
1735424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      // It may be the layer or the surface doing the clipping of the subtree,
1736424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      // but in either case, we'll be clipping to the projected clip rect of our
1737424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      // ancestor.
17382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      gfx::Transform inverse_surface_draw_transform(
17392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          gfx::Transform::kSkipInitialization);
17402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      if (!render_surface->draw_transform().GetInverse(
17412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)              &inverse_surface_draw_transform)) {
17422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        // TODO(shawnsingh): Either we need to handle uninvertible transforms
17432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        // here, or DCHECK that the transform is invertible.
17442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      }
1745424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
1746424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      gfx::Rect projected_surface_rect = gfx::ToEnclosingRect(
1747424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)          MathUtil::ProjectClippedRect(inverse_surface_draw_transform,
1748424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                                       ancestor_clip_rect_in_target_space));
1749424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
1750424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      if (layer_draw_properties.num_unclipped_descendants > 0) {
1751424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        // If we have unclipped descendants, we cannot count on the render
1752424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        // surface's bounds clipping our subtree: the unclipped descendants
1753424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        // could cause us to expand our bounds. In this case, we must rely on
1754424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        // layer clipping for correctess. NB: since we can only encounter
1755424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        // translations between a clip child and its clip parent, clipping is
1756424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        // guaranteed to be exact in this case.
1757424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        layer_or_ancestor_clips_descendants = true;
1758424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        clip_rect_in_target_space = projected_surface_rect;
1759424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      } else {
1760424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        // The new render_surface here will correctly clip the entire subtree.
1761424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        // So, we do not need to continue propagating the clipping state further
1762424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        // down the tree. This way, we can avoid transforming clip rects from
1763424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        // ancestor target surface space to current target surface space that
1764424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        // could cause more w < 0 headaches. The render surface clip rect is
1765424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        // expressed in the space where this surface draws, i.e. the same space
1766424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        // as clip_rect_from_ancestor_in_ancestor_target_space.
1767424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        render_surface->SetClipRect(ancestor_clip_rect_in_target_space);
1768424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        clip_rect_of_target_surface_in_target_space = projected_surface_rect;
1769424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        subtree_is_clipped_by_surface_bounds = true;
1770424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      }
1771424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    }
1772424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
1773424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    DCHECK(layer->render_surface());
1774424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    DCHECK(!layer->parent() || layer->parent()->render_target() ==
1775424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)           accumulated_surface_state->back().render_target);
1776424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
1777424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    accumulated_surface_state->push_back(
1778424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        AccumulatedSurfaceState<LayerType>(layer));
1779424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
1780424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    render_surface->SetIsClipped(subtree_is_clipped_by_surface_bounds);
1781424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    if (!subtree_is_clipped_by_surface_bounds) {
17822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      render_surface->SetClipRect(gfx::Rect());
1783a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch      clip_rect_of_target_surface_in_target_space =
17842385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch          data_from_ancestor.clip_rect_of_target_surface_in_target_space;
17852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
17862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
17872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // If the new render surface is drawn translucent or with a non-integral
17882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // translation then the subtree that gets drawn on this render surface
17892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // cannot use LCD text.
17902385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch    data_for_children.subtree_can_use_lcd_text = layer_can_use_lcd_text;
17912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
17922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    render_surface_layer_list->push_back(layer);
17932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  } else {
17942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DCHECK(layer->parent());
17952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
17962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Note: layer_draw_properties.target_space_transform is computed above,
17972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // before this if-else statement.
17982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    layer_draw_properties.target_space_transform_is_animating =
17992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        animating_transform_to_target;
18002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    layer_draw_properties.screen_space_transform_is_animating =
18012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        animating_transform_to_screen;
18022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    layer_draw_properties.opacity = accumulated_draw_opacity;
18032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    layer_draw_properties.opacity_is_animating = animating_opacity_to_target;
18042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    layer_draw_properties.screen_space_opacity_is_animating =
18052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        animating_opacity_to_screen;
18062385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch    data_for_children.parent_matrix = combined_transform;
18072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
18082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    layer->ClearRenderSurface();
18092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
18102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Layers without render_surfaces directly inherit the ancestor's clip
18112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // status.
1812424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    layer_or_ancestor_clips_descendants = ancestor_clips_subtree;
1813424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    if (ancestor_clips_subtree) {
1814a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch      clip_rect_in_target_space =
1815424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)          ancestor_clip_rect_in_target_space;
1816a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch    }
18172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
18182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // The surface's cached clip rect value propagates regardless of what
18192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // clipping goes on between layers here.
1820a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch    clip_rect_of_target_surface_in_target_space =
18212385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch        data_from_ancestor.clip_rect_of_target_surface_in_target_space;
18222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
18232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Layers that are not their own render_target will render into the target
18242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // of their nearest ancestor.
18252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    layer_draw_properties.render_target = layer->parent()->render_target();
18262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
18272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
182890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Mark whether a layer could be drawn directly to the back buffer, for
182990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // example when it could use LCD text even though it's in a non-contents
183090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // opaque layer.  This means that it can't be drawn to an intermediate
183190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // render target and also that no blending is applied to the layer as a whole
183290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // (meaning that its contents don't have to be pre-composited into a bitmap or
183390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // a render target).
183490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  //
183590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Ignoring animations is an optimization,
183690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // as it means that we're going to need some retained resources for this
183790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // layer in the near future even if its opacity is 1 now.
183890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  layer_draw_properties.can_draw_directly_to_backbuffer =
183990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      IsRootLayer(layer_draw_properties.render_target) &&
184090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      layer->draw_properties().opacity == 1.f &&
184190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)      !animating_opacity_to_screen;
184290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
18432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (adjust_text_aa)
18442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    layer_draw_properties.can_use_lcd_text = layer_can_use_lcd_text;
18452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
18462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  gfx::Rect rect_in_target_space = ToEnclosingRect(
18472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      MathUtil::MapClippedRect(layer->draw_transform(), content_rect));
18482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
18492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (LayerClipsSubtree(layer)) {
1850a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch    layer_or_ancestor_clips_descendants = true;
1851424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    if (ancestor_clips_subtree && !layer->render_surface()) {
1852a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch      // A layer without render surface shares the same target as its ancestor.
1853a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch      clip_rect_in_target_space =
1854424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)          ancestor_clip_rect_in_target_space;
1855a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch      clip_rect_in_target_space.Intersect(rect_in_target_space);
18562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    } else {
1857a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch      clip_rect_in_target_space = rect_in_target_space;
18582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
18592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
18602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1861424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // Tell the layer the rect that it's clipped by. In theory we could use a
1862424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // tighter clip rect here (drawable_content_rect), but that actually does not
1863424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // reduce how much would be drawn, and instead it would create unnecessary
1864424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // changes to scissor state affecting GPU performance. Our clip information
1865424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // is used in the recursion below, so we must set it beforehand.
1866424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  layer_draw_properties.is_clipped = layer_or_ancestor_clips_descendants;
1867424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  if (layer_or_ancestor_clips_descendants) {
1868424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    layer_draw_properties.clip_rect = clip_rect_in_target_space;
1869424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  } else {
1870424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    // Initialize the clip rect to a safe value that will not clip the
1871424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    // layer, just in case clipping is still accidentally used.
1872424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    layer_draw_properties.clip_rect = rect_in_target_space;
1873424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  }
1874424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
18758bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  typename LayerType::RenderSurfaceListType& descendants =
18762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      (layer->render_surface() ? layer->render_surface()->layer_list()
18772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                               : *layer_list);
18782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
18792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Any layers that are appended after this point are in the layer's subtree
18802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // and should be included in the sorting process.
18812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  size_t sorting_start_index = descendants.size();
18822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
18837dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  if (!LayerShouldBeSkipped(layer, layer_is_visible))
18842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    descendants.push_back(layer);
18852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
18868bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // Any layers that are appended after this point may need to be sorted if we
18878bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // visit the children out of order.
18888bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  size_t render_surface_layer_list_child_sorting_start_index =
18898bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      render_surface_layer_list->size();
18908bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  size_t layer_list_child_sorting_start_index = descendants.size();
18918bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
18923551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  if (!layer->children().empty()) {
18933551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    if (layer == globals.page_scale_application_layer) {
18943551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      data_for_children.parent_matrix.Scale(
18953551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)          globals.page_scale_factor,
18963551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)          globals.page_scale_factor);
18973551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      data_for_children.in_subtree_of_page_scale_application_layer = true;
18983551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    }
18993551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
19003551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    // Flatten to 2D if the layer doesn't preserve 3D.
19013551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    if (!layer->preserves_3d())
19023551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      data_for_children.parent_matrix.FlattenTo2d();
19033551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
19043551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    // Apply the sublayer transform at the anchor point of the layer.
19053551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    if (!layer->sublayer_transform().IsIdentity()) {
19063551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      data_for_children.parent_matrix.Translate(
19073551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)          layer->anchor_point().x() * bounds.width(),
19083551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)          layer->anchor_point().y() * bounds.height());
19093551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      data_for_children.parent_matrix.PreconcatTransform(
19103551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)          layer->sublayer_transform());
19113551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      data_for_children.parent_matrix.Translate(
19123551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)          -layer->anchor_point().x() * bounds.width(),
19133551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)          -layer->anchor_point().y() * bounds.height());
19143551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    }
19153551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
19163551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    data_for_children.scroll_compensation_matrix =
19173551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)        ComputeScrollCompensationMatrixForChildren(
19183551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)            layer,
19193551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)            data_from_ancestor.parent_matrix,
19201e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)            data_from_ancestor.scroll_compensation_matrix,
19211e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)            effective_scroll_delta);
19223551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    data_for_children.fixed_container =
19233551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)        layer->IsContainerForFixedPositionLayers() ?
19243551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)            layer : data_from_ancestor.fixed_container;
19253551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
19263551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    data_for_children.clip_rect_in_target_space = clip_rect_in_target_space;
19273551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    data_for_children.clip_rect_of_target_surface_in_target_space =
19283551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)        clip_rect_of_target_surface_in_target_space;
19293551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    data_for_children.ancestor_clips_subtree =
19303551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)        layer_or_ancestor_clips_descendants;
1931f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    data_for_children.nearest_occlusion_immune_ancestor_surface =
1932f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        nearest_occlusion_immune_ancestor_surface;
19333551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    data_for_children.subtree_is_visible_from_ancestor = layer_is_visible;
19343551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  }
19352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
19368bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  std::vector<LayerType*> sorted_children;
19378bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  bool child_order_changed = false;
19388bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  if (layer_draw_properties.has_child_with_a_scroll_parent)
19398bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    child_order_changed = SortChildrenForRecursion(&sorted_children, *layer);
19408bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
19412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  for (size_t i = 0; i < layer->children().size(); ++i) {
19428bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    // If one of layer's children has a scroll parent, then we may have to
19438bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    // visit the children out of order. The new order is stored in
19448bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    // sorted_children. Otherwise, we'll grab the child directly from the
19458bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    // layer's list of children.
19462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    LayerType* child =
19478bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        layer_draw_properties.has_child_with_a_scroll_parent
19488bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)            ? sorted_children[i]
19498bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)            : LayerTreeHostCommon::get_child_as_raw_ptr(layer->children(), i);
19508bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
19518bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    child->draw_properties().index_of_first_descendants_addition =
19528bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        descendants.size();
19538bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    child->draw_properties().index_of_first_render_surface_layer_list_addition =
19548bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        render_surface_layer_list->size();
19558bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
19568bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    CalculateDrawPropertiesInternal<LayerType>(child,
19578bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                                               globals,
19588bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                                               data_for_children,
19598bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                                               render_surface_layer_list,
19608bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                                               &descendants,
19618bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                                               accumulated_surface_state);
1962424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    if (child->render_surface() &&
1963424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        !child->render_surface()->content_rect().IsEmpty()) {
1964424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      descendants.push_back(child);
19652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
19668bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
19678bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    child->draw_properties().num_descendants_added =
19688bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        descendants.size() -
19698bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        child->draw_properties().index_of_first_descendants_addition;
19708bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    child->draw_properties().num_render_surfaces_added =
19718bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        render_surface_layer_list->size() -
19728bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        child->draw_properties()
19738bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)            .index_of_first_render_surface_layer_list_addition;
19748bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  }
19758bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
19768bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // Add the unsorted layer list contributions, if necessary.
19778bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  if (child_order_changed) {
19788bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    SortLayerListContributions(
19798bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        *layer,
19808bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        render_surface_layer_list,
19818bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        render_surface_layer_list_child_sorting_start_index,
19828bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        &GetNewRenderSurfacesStartIndexAndCount<LayerType>);
19838bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
19848bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    SortLayerListContributions(
19858bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        *layer,
19868bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        &descendants,
19878bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        layer_list_child_sorting_start_index,
19888bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        &GetNewDescendantsStartIndexAndCount<LayerType>);
19892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
19902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1991424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // Compute the total drawable_content_rect for this subtree (the rect is in
1992424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // target surface space).
1993424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  gfx::Rect local_drawable_content_rect_of_subtree =
1994424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      accumulated_surface_state->back().drawable_content_rect;
1995424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  if (layer->render_surface()) {
1996424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    DCHECK(accumulated_surface_state->back().render_target == layer);
1997424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    accumulated_surface_state->pop_back();
1998424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  }
1999424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
20002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (layer->render_surface() && !IsRootLayer(layer) &&
20012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      layer->render_surface()->layer_list().empty()) {
20022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    RemoveSurfaceForEarlyExit(layer, render_surface_layer_list);
20032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
20042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
20052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
20062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Compute the layer's drawable content rect (the rect is in target surface
20072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // space).
20082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  layer_draw_properties.drawable_content_rect = rect_in_target_space;
2009a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch  if (layer_or_ancestor_clips_descendants) {
2010f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    layer_draw_properties.drawable_content_rect.Intersect(
2011f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        clip_rect_in_target_space);
2012f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
2013f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (layer->DrawsContent()) {
2014f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    local_drawable_content_rect_of_subtree.Union(
2015f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)        layer_draw_properties.drawable_content_rect);
20162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
20172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2018a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch  // Compute the layer's visible content rect (the rect is in content space).
20192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  layer_draw_properties.visible_content_rect = CalculateVisibleContentRect(
2020a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch      layer, clip_rect_of_target_surface_in_target_space, rect_in_target_space);
20212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
20222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Compute the remaining properties for the render surface, if the layer has
20232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // one.
20242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (IsRootLayer(layer)) {
20252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // The root layer's surface's content_rect is always the entire viewport.
20262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DCHECK(layer->render_surface());
2027a3f7b4e666c476898878fa745f637129375cd889Ben Murdoch    layer->render_surface()->SetContentRect(
2028424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)        ancestor_clip_rect_in_target_space);
20298bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  } else if (layer->render_surface()) {
20308bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    typename LayerType::RenderSurfaceType* render_surface =
20318bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        layer->render_surface();
20322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    gfx::Rect clipped_content_rect = local_drawable_content_rect_of_subtree;
20332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
20342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Don't clip if the layer is reflected as the reflection shouldn't be
20352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // clipped. If the layer is animating, then the surface's transform to
20362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // its target is not known on the main thread, and we should not use it
20372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // to clip.
20382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (!layer->replica_layer() && TransformToParentIsKnown(layer)) {
20392385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch      // Note, it is correct to use data_from_ancestor.ancestor_clips_subtree
20402385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch      // here, because we are looking at this layer's render_surface, not the
20412385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch      // layer itself.
2042424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      if (render_surface->is_clipped() && !clipped_content_rect.IsEmpty()) {
20432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        gfx::Rect surface_clip_rect = LayerTreeHostCommon::CalculateVisibleRect(
20442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            render_surface->clip_rect(),
20452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            clipped_content_rect,
20462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            render_surface->draw_transform());
20472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        clipped_content_rect.Intersect(surface_clip_rect);
20482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      }
20492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
20502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
20512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // The RenderSurfaceImpl backing texture cannot exceed the maximum supported
20522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // texture size.
20532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    clipped_content_rect.set_width(
20542385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch        std::min(clipped_content_rect.width(), globals.max_texture_size));
20552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    clipped_content_rect.set_height(
20562385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch        std::min(clipped_content_rect.height(), globals.max_texture_size));
20572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
20582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (clipped_content_rect.IsEmpty()) {
20592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      RemoveSurfaceForEarlyExit(layer, render_surface_layer_list);
20602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return;
20612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
20622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2063f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // Layers having a non-default blend mode will blend with the content
2064f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // inside its parent's render target. This render target should be
2065f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // either root_for_isolated_group, or the root of the layer tree.
2066f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // Otherwise, this layer will use an incomplete backdrop, limited to its
2067f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    // render target and the blending result will be incorrect.
2068f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    DCHECK(layer->uses_default_blend_mode() || IsRootLayer(layer) ||
2069f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)           !layer->parent()->render_target() ||
2070f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)           IsRootLayer(layer->parent()->render_target()) ||
2071f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)           layer->parent()->render_target()->is_root_for_isolated_group());
2072f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
20732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    render_surface->SetContentRect(clipped_content_rect);
20742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
20752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // The owning layer's screen_space_transform has a scale from content to
20762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // layer space which we need to undo and replace with a scale from the
20772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // surface's subtree into layer space.
20782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    gfx::Transform screen_space_transform = layer->screen_space_transform();
20792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    screen_space_transform.Scale(
20802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        layer->contents_scale_x() / render_surface_sublayer_scale.x(),
20812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        layer->contents_scale_y() / render_surface_sublayer_scale.y());
20822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    render_surface->SetScreenSpaceTransform(screen_space_transform);
20832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
20842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (layer->replica_layer()) {
20852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      gfx::Transform surface_origin_to_replica_origin_transform;
20862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      surface_origin_to_replica_origin_transform.Scale(
20872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          render_surface_sublayer_scale.x(), render_surface_sublayer_scale.y());
20882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      surface_origin_to_replica_origin_transform.Translate(
20892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          layer->replica_layer()->position().x() +
20902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          layer->replica_layer()->anchor_point().x() * bounds.width(),
20912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          layer->replica_layer()->position().y() +
20922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          layer->replica_layer()->anchor_point().y() * bounds.height());
20932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      surface_origin_to_replica_origin_transform.PreconcatTransform(
20942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          layer->replica_layer()->transform());
20952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      surface_origin_to_replica_origin_transform.Translate(
20962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          -layer->replica_layer()->anchor_point().x() * bounds.width(),
20972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          -layer->replica_layer()->anchor_point().y() * bounds.height());
20982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      surface_origin_to_replica_origin_transform.Scale(
20992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          1.0 / render_surface_sublayer_scale.x(),
21002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          1.0 / render_surface_sublayer_scale.y());
21012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
21022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // Compute the replica's "originTransform" that maps from the replica's
21032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // origin space to the target surface origin space.
21042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      gfx::Transform replica_origin_transform =
21052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          layer->render_surface()->draw_transform() *
21062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          surface_origin_to_replica_origin_transform;
21072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      render_surface->SetReplicaDrawTransform(replica_origin_transform);
21082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
21092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // Compute the replica's "screen_space_transform" that maps from the
21102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      // replica's origin space to the screen's origin space.
21112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      gfx::Transform replica_screen_space_transform =
21122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          layer->render_surface()->screen_space_transform() *
21132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          surface_origin_to_replica_origin_transform;
21142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      render_surface->SetReplicaScreenSpaceTransform(
21152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          replica_screen_space_transform);
21162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
21172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
21182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2119eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  SavePaintPropertiesLayer(layer);
21202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
21212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // If neither this layer nor any of its children were added, early out.
212258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  if (sorting_start_index == descendants.size()) {
212358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    DCHECK(!layer->render_surface() || IsRootLayer(layer));
21242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
212558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  }
21262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
21272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // If preserves-3d then sort all the descendants in 3D so that they can be
21282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // drawn from back to front. If the preserves-3d property is also set on the
21292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // parent then skip the sorting as the parent will sort all the descendants
21302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // anyway.
21312385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  if (globals.layer_sorter && descendants.size() && layer->preserves_3d() &&
21322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      (!layer->parent() || !layer->parent()->preserves_3d())) {
21332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    SortLayers(descendants.begin() + sorting_start_index,
21342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)               descendants.end(),
21352385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch               globals.layer_sorter);
21362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
21372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
21388bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  UpdateAccumulatedSurfaceState<LayerType>(
2139424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      layer, local_drawable_content_rect_of_subtree, accumulated_surface_state);
21402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
21412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (layer->HasContributingDelegatedRenderPasses()) {
21422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    layer->render_target()->render_surface()->
21432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        AddContributingDelegatedRenderPassLayer(layer);
21442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
21452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
21462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
21472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void LayerTreeHostCommon::CalculateDrawProperties(
2148558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch    CalcDrawPropsMainInputs* inputs) {
2149558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch  DCHECK(inputs->root_layer);
2150558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch  DCHECK(IsRootLayer(inputs->root_layer));
2151558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch  DCHECK(inputs->render_surface_layer_list);
21522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  gfx::Transform identity_matrix;
2153558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch  gfx::Transform scaled_device_transform = inputs->device_transform;
2154558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch  scaled_device_transform.Scale(inputs->device_scale_factor,
2155558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch                                inputs->device_scale_factor);
2156ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  RenderSurfaceLayerList dummy_layer_list;
21572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
21582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // The root layer's render_surface should receive the device viewport as the
21592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // initial clip rect.
2160558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch  gfx::Rect device_viewport_rect(inputs->device_viewport_size);
21612385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch
21622385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  SubtreeGlobals<Layer> globals;
21632385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  globals.layer_sorter = NULL;
21642385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  globals.max_texture_size = inputs->max_texture_size;
21652385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  globals.device_scale_factor = inputs->device_scale_factor;
21662385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  globals.page_scale_factor = inputs->page_scale_factor;
21672385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  globals.page_scale_application_layer = inputs->page_scale_application_layer;
21684e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  globals.can_render_to_separate_surface =
21694e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      inputs->can_render_to_separate_surface;
21702385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  globals.can_adjust_raster_scales = inputs->can_adjust_raster_scales;
21712385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch
21728bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  DataForRecursion<Layer> data_for_recursion;
21732385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  data_for_recursion.parent_matrix = scaled_device_transform;
21742385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  data_for_recursion.full_hierarchy_matrix = identity_matrix;
21752385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  data_for_recursion.scroll_compensation_matrix = identity_matrix;
21762385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  data_for_recursion.fixed_container = inputs->root_layer;
21772385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  data_for_recursion.clip_rect_in_target_space = device_viewport_rect;
21782385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  data_for_recursion.clip_rect_of_target_surface_in_target_space =
21792385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch      device_viewport_rect;
21802385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  data_for_recursion.ancestor_clips_subtree = true;
2181f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  data_for_recursion.nearest_occlusion_immune_ancestor_surface = NULL;
21822385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  data_for_recursion.in_subtree_of_page_scale_application_layer = false;
21832385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  data_for_recursion.subtree_can_use_lcd_text = inputs->can_use_lcd_text;
21842385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  data_for_recursion.subtree_is_visible_from_ancestor = true;
21852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
21867dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  PreCalculateMetaInformationRecursiveData recursive_data;
2187558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch  PreCalculateMetaInformation(inputs->root_layer, &recursive_data);
2188424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  std::vector<AccumulatedSurfaceState<Layer> > accumulated_surface_state;
21898bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  CalculateDrawPropertiesInternal<Layer>(inputs->root_layer,
21908bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                                         globals,
21918bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                                         data_for_recursion,
21928bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                                         inputs->render_surface_layer_list,
21938bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                                         &dummy_layer_list,
21948bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                                         &accumulated_surface_state);
21952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
21962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // The dummy layer list should not have been used.
2197c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DCHECK_EQ(0u, dummy_layer_list.size());
21982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // A root layer render_surface should always exist after
21992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // CalculateDrawProperties.
2200558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch  DCHECK(inputs->root_layer->render_surface());
22012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
22022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
22032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void LayerTreeHostCommon::CalculateDrawProperties(
2204558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch    CalcDrawPropsImplInputs* inputs) {
2205558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch  DCHECK(inputs->root_layer);
2206558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch  DCHECK(IsRootLayer(inputs->root_layer));
2207558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch  DCHECK(inputs->render_surface_layer_list);
2208558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch
22092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  gfx::Transform identity_matrix;
2210558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch  gfx::Transform scaled_device_transform = inputs->device_transform;
2211558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch  scaled_device_transform.Scale(inputs->device_scale_factor,
2212558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch                                inputs->device_scale_factor);
2213c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  LayerImplList dummy_layer_list;
22142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  LayerSorter layer_sorter;
22152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
22162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // The root layer's render_surface should receive the device viewport as the
22172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // initial clip rect.
2218558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch  gfx::Rect device_viewport_rect(inputs->device_viewport_size);
22192385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch
22202385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  SubtreeGlobals<LayerImpl> globals;
22212385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  globals.layer_sorter = &layer_sorter;
22222385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  globals.max_texture_size = inputs->max_texture_size;
22232385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  globals.device_scale_factor = inputs->device_scale_factor;
22242385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  globals.page_scale_factor = inputs->page_scale_factor;
22252385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  globals.page_scale_application_layer = inputs->page_scale_application_layer;
22264e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  globals.can_render_to_separate_surface =
22274e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      inputs->can_render_to_separate_surface;
22282385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  globals.can_adjust_raster_scales = inputs->can_adjust_raster_scales;
22292385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch
22308bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  DataForRecursion<LayerImpl> data_for_recursion;
22312385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  data_for_recursion.parent_matrix = scaled_device_transform;
22322385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  data_for_recursion.full_hierarchy_matrix = identity_matrix;
22332385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  data_for_recursion.scroll_compensation_matrix = identity_matrix;
22342385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  data_for_recursion.fixed_container = inputs->root_layer;
22352385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  data_for_recursion.clip_rect_in_target_space = device_viewport_rect;
22362385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  data_for_recursion.clip_rect_of_target_surface_in_target_space =
22372385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch      device_viewport_rect;
22382385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  data_for_recursion.ancestor_clips_subtree = true;
2239f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  data_for_recursion.nearest_occlusion_immune_ancestor_surface = NULL;
22402385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  data_for_recursion.in_subtree_of_page_scale_application_layer = false;
22412385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  data_for_recursion.subtree_can_use_lcd_text = inputs->can_use_lcd_text;
22422385ea399aae016c0806a4f9ef3c9cfe3d2a39dfBen Murdoch  data_for_recursion.subtree_is_visible_from_ancestor = true;
22432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
22447dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdoch  PreCalculateMetaInformationRecursiveData recursive_data;
2245558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch  PreCalculateMetaInformation(inputs->root_layer, &recursive_data);
2246424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  std::vector<AccumulatedSurfaceState<LayerImpl> >
2247424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      accumulated_surface_state;
22488bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  CalculateDrawPropertiesInternal<LayerImpl>(inputs->root_layer,
22498bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                                             globals,
22508bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                                             data_for_recursion,
22518bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                                             inputs->render_surface_layer_list,
22528bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                                             &dummy_layer_list,
22538bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                                             &accumulated_surface_state);
22542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
22552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // The dummy layer list should not have been used.
2256c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  DCHECK_EQ(0u, dummy_layer_list.size());
22572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // A root layer render_surface should always exist after
22582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // CalculateDrawProperties.
2259558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch  DCHECK(inputs->root_layer->render_surface());
22602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
22612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
22622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)static bool PointHitsRect(
22632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    gfx::PointF screen_space_point,
22642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const gfx::Transform& local_space_to_screen_space_transform,
22652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    gfx::RectF local_space_rect) {
22662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // If the transform is not invertible, then assume that this point doesn't hit
22672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // this rect.
22682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  gfx::Transform inverse_local_space_to_screen_space(
22692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      gfx::Transform::kSkipInitialization);
22702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!local_space_to_screen_space_transform.GetInverse(
22712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          &inverse_local_space_to_screen_space))
22722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return false;
22732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
22742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Transform the hit test point from screen space to the local space of the
22752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // given rect.
22762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool clipped = false;
22772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  gfx::PointF hit_test_point_in_local_space = MathUtil::ProjectPoint(
22782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      inverse_local_space_to_screen_space, screen_space_point, &clipped);
22792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
22802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // If ProjectPoint could not project to a valid value, then we assume that
22812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // this point doesn't hit this rect.
22822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (clipped)
22832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return false;
22842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
22852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return local_space_rect.Contains(hit_test_point_in_local_space);
22862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
22872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
22882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)static bool PointHitsRegion(gfx::PointF screen_space_point,
22892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                            const gfx::Transform& screen_space_transform,
22902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                            const Region& layer_space_region,
22912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                            float layer_content_scale_x,
22922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                            float layer_content_scale_y) {
22932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // If the transform is not invertible, then assume that this point doesn't hit
22942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // this region.
22952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  gfx::Transform inverse_screen_space_transform(
22962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      gfx::Transform::kSkipInitialization);
22972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!screen_space_transform.GetInverse(&inverse_screen_space_transform))
22982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return false;
22992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
23002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Transform the hit test point from screen space to the local space of the
23012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // given region.
23022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool clipped = false;
23032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  gfx::PointF hit_test_point_in_content_space = MathUtil::ProjectPoint(
23042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      inverse_screen_space_transform, screen_space_point, &clipped);
23052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  gfx::PointF hit_test_point_in_layer_space =
23062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      gfx::ScalePoint(hit_test_point_in_content_space,
23072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                      1.f / layer_content_scale_x,
23082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                      1.f / layer_content_scale_y);
23092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
23102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // If ProjectPoint could not project to a valid value, then we assume that
23112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // this point doesn't hit this region.
23122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (clipped)
23132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return false;
23142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
23152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return layer_space_region.Contains(
23162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      gfx::ToRoundedPoint(hit_test_point_in_layer_space));
23172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
23182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
23192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)static bool PointIsClippedBySurfaceOrClipRect(gfx::PointF screen_space_point,
23202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                              LayerImpl* layer) {
23212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  LayerImpl* current_layer = layer;
23222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
23232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Walk up the layer tree and hit-test any render_surfaces and any layer
23242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // clip rects that are active.
23252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  while (current_layer) {
23262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (current_layer->render_surface() &&
23272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        !PointHitsRect(
23282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            screen_space_point,
23292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            current_layer->render_surface()->screen_space_transform(),
23302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            current_layer->render_surface()->content_rect()))
23312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return true;
23322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
23332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Note that drawable content rects are actually in target surface space, so
23342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // the transform we have to provide is the target surface's
23352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // screen_space_transform.
23362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    LayerImpl* render_target = current_layer->render_target();
23372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (LayerClipsSubtree(current_layer) &&
23382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        !PointHitsRect(
23392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            screen_space_point,
23402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            render_target->render_surface()->screen_space_transform(),
23412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)            current_layer->drawable_content_rect()))
23422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return true;
23432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
23442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    current_layer = current_layer->parent();
23452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
23462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
23472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // If we have finished walking all ancestors without having already exited,
23482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // then the point is not clipped by any ancestors.
23492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return false;
23502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
23512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
23522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)LayerImpl* LayerTreeHostCommon::FindLayerThatIsHitByPoint(
23532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    gfx::PointF screen_space_point,
2354c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const LayerImplList& render_surface_layer_list) {
23552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  LayerImpl* found_layer = NULL;
23562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
23572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  typedef LayerIterator<LayerImpl,
2358c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                        LayerImplList,
23592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                        RenderSurfaceImpl,
23602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                        LayerIteratorActions::FrontToBack> LayerIteratorType;
23612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  LayerIteratorType end = LayerIteratorType::End(&render_surface_layer_list);
23622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
23632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  for (LayerIteratorType
23642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)           it = LayerIteratorType::Begin(&render_surface_layer_list);
23652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)       it != end;
23662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)       ++it) {
23672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // We don't want to consider render_surfaces for hit testing.
23682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (!it.represents_itself())
23692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      continue;
23702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
23712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    LayerImpl* current_layer = (*it);
23722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
23732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    gfx::RectF content_rect(current_layer->content_bounds());
23742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (!PointHitsRect(screen_space_point,
23752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                       current_layer->screen_space_transform(),
23762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                       content_rect))
23772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      continue;
23782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
23792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // At this point, we think the point does hit the layer, but we need to walk
23802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // up the parents to ensure that the layer was not clipped in such a way
23812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // that the hit point actually should not hit the layer.
23822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (PointIsClippedBySurfaceOrClipRect(screen_space_point, current_layer))
23832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      continue;
23842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
23852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Skip the HUD layer.
23862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (current_layer == current_layer->layer_tree_impl()->hud_layer())
23872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      continue;
23882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
23892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    found_layer = current_layer;
23902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    break;
23912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
23922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
23932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // This can potentially return NULL, which means the screen_space_point did
23942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // not successfully hit test any layers, not even the root layer.
23952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return found_layer;
23962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
23972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
23982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)LayerImpl* LayerTreeHostCommon::FindLayerThatIsHitByPointInTouchHandlerRegion(
23992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    gfx::PointF screen_space_point,
2400c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    const LayerImplList& render_surface_layer_list) {
24011e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // First find out which layer was hit from the saved list of visible layers
24021e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // in the most recent frame.
24031e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  LayerImpl* layer_impl = LayerTreeHostCommon::FindLayerThatIsHitByPoint(
24041e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      screen_space_point,
24051e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      render_surface_layer_list);
24061e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
24071e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // Walk up the hierarchy and look for a layer with a touch event handler
24081e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // region that the given point hits.
24091e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // This walk may not be necessary anymore: http://crbug.com/310817
24101e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  for (; layer_impl; layer_impl = layer_impl->parent()) {
24111e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    if (LayerTreeHostCommon::LayerHasTouchEventHandlersAt(screen_space_point,
24121e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                                                          layer_impl))
24131e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      break;
24142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
24151e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  return layer_impl;
24162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
24172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
24182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool LayerTreeHostCommon::LayerHasTouchEventHandlersAt(
24192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    gfx::PointF screen_space_point,
24202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    LayerImpl* layer_impl) {
24212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (layer_impl->touch_event_handler_region().IsEmpty())
24222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return false;
24232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
24242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!PointHitsRegion(screen_space_point,
24252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                       layer_impl->screen_space_transform(),
24262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                       layer_impl->touch_event_handler_region(),
24272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                       layer_impl->contents_scale_x(),
24282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                       layer_impl->contents_scale_y()))
24292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return false;
24302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
24312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // At this point, we think the point does hit the touch event handler region
24322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // on the layer, but we need to walk up the parents to ensure that the layer
24332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // was not clipped in such a way that the hit point actually should not hit
24342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // the layer.
24352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (PointIsClippedBySurfaceOrClipRect(screen_space_point, layer_impl))
24362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return false;
24372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
24382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return true;
24392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
24402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}  // namespace cc
2441