Disable flaky UnloadTest.CrossSiteInfiniteBeforeUnloadSync on Mac.
[chromium-blink-merge.git] / cc / trees / property_tree_builder.cc
blob6d8c98999714f0ae6bcf55872fa7ac2b38562e89
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "cc/trees/property_tree_builder.h"
7 #include <map>
8 #include <set>
10 #include "cc/base/math_util.h"
11 #include "cc/layers/layer.h"
12 #include "cc/layers/layer_impl.h"
13 #include "cc/trees/layer_tree_host.h"
14 #include "ui/gfx/geometry/point_f.h"
15 #include "ui/gfx/geometry/vector2d_conversions.h"
17 namespace cc {
19 class LayerTreeHost;
21 namespace {
23 template <typename LayerType>
24 struct DataForRecursion {
25 TransformTree* transform_tree;
26 ClipTree* clip_tree;
27 OpacityTree* opacity_tree;
28 LayerType* transform_tree_parent;
29 LayerType* transform_fixed_parent;
30 LayerType* render_target;
31 int clip_tree_parent;
32 int opacity_tree_parent;
33 const LayerType* page_scale_layer;
34 float page_scale_factor;
35 float device_scale_factor;
36 bool in_subtree_of_page_scale_application_layer;
37 bool should_flatten;
38 bool ancestor_clips_subtree;
39 const gfx::Transform* device_transform;
40 gfx::Vector2dF scroll_compensation_adjustment;
43 template <typename LayerType>
44 static LayerType* GetTransformParent(const DataForRecursion<LayerType>& data,
45 LayerType* layer) {
46 return layer->position_constraint().is_fixed_position()
47 ? data.transform_fixed_parent
48 : data.transform_tree_parent;
51 template <typename LayerType>
52 static ClipNode* GetClipParent(const DataForRecursion<LayerType>& data,
53 LayerType* layer) {
54 const bool inherits_clip = !layer->parent() || !layer->clip_parent();
55 const int id = inherits_clip ? data.clip_tree_parent
56 : layer->clip_parent()->clip_tree_index();
57 return data.clip_tree->Node(id);
60 template <typename LayerType>
61 static bool HasPotentiallyRunningAnimation(LayerType* layer,
62 Animation::TargetProperty property) {
63 if (Animation* animation =
64 layer->layer_animation_controller()->GetAnimation(property)) {
65 return !animation->is_finished();
67 return false;
70 template <typename LayerType>
71 static bool RequiresClipNode(LayerType* layer,
72 const DataForRecursion<LayerType>& data,
73 int parent_transform_id,
74 bool is_clipped) {
75 const bool render_surface_applies_clip =
76 layer->render_surface() && is_clipped;
77 const bool render_surface_may_grow_due_to_clip_children =
78 layer->render_surface() && layer->num_unclipped_descendants() > 0;
80 if (!layer->parent() || layer->masks_to_bounds() || layer->mask_layer() ||
81 render_surface_may_grow_due_to_clip_children)
82 return true;
84 if (!render_surface_applies_clip)
85 return false;
87 bool axis_aligned_with_respect_to_parent =
88 data.transform_tree->Are2DAxisAligned(layer->transform_tree_index(),
89 parent_transform_id);
91 return !axis_aligned_with_respect_to_parent;
94 template <typename LayerType>
95 static bool LayerClipsSubtree(LayerType* layer) {
96 return layer->masks_to_bounds() || layer->mask_layer();
99 template <typename LayerType>
100 void AddClipNodeIfNeeded(const DataForRecursion<LayerType>& data_from_ancestor,
101 LayerType* layer,
102 bool created_transform_node,
103 DataForRecursion<LayerType>* data_for_children) {
104 ClipNode* parent = GetClipParent(data_from_ancestor, layer);
105 int parent_id = parent->id;
107 bool ancestor_clips_subtree =
108 data_from_ancestor.ancestor_clips_subtree || layer->clip_parent();
110 data_for_children->ancestor_clips_subtree = false;
111 bool has_unclipped_surface = false;
113 if (layer->has_render_surface()) {
114 if (ancestor_clips_subtree && layer->num_unclipped_descendants() > 0)
115 data_for_children->ancestor_clips_subtree = true;
116 else if (!ancestor_clips_subtree && !layer->num_unclipped_descendants())
117 has_unclipped_surface = true;
118 } else {
119 data_for_children->ancestor_clips_subtree = ancestor_clips_subtree;
122 if (LayerClipsSubtree(layer))
123 data_for_children->ancestor_clips_subtree = true;
125 if (has_unclipped_surface)
126 parent_id = 0;
128 if (!RequiresClipNode(layer, data_from_ancestor, parent->data.transform_id,
129 data_for_children->ancestor_clips_subtree)) {
130 // Unclipped surfaces reset the clip rect.
131 data_for_children->clip_tree_parent = parent_id;
132 } else if (layer->parent()) {
133 // Note the root clip gets handled elsewhere.
134 LayerType* transform_parent = data_for_children->transform_tree_parent;
135 if (layer->position_constraint().is_fixed_position() &&
136 !created_transform_node) {
137 transform_parent = data_for_children->transform_fixed_parent;
139 ClipNode node;
140 node.data.clip = gfx::RectF(
141 gfx::PointF() + layer->offset_to_transform_parent(), layer->bounds());
142 node.data.transform_id = transform_parent->transform_tree_index();
143 node.data.target_id =
144 data_for_children->render_target->transform_tree_index();
145 node.owner_id = layer->id();
147 data_for_children->clip_tree_parent =
148 data_for_children->clip_tree->Insert(node, parent_id);
151 layer->set_clip_tree_index(
152 has_unclipped_surface ? 0 : data_for_children->clip_tree_parent);
154 // TODO(awoloszyn): Right now when we hit a node with a replica, we reset the
155 // clip for all children since we may need to draw. We need to figure out a
156 // better way, since we will need both the clipped and unclipped versions.
159 template <typename LayerType>
160 bool AddTransformNodeIfNeeded(
161 const DataForRecursion<LayerType>& data_from_ancestor,
162 LayerType* layer,
163 DataForRecursion<LayerType>* data_for_children) {
164 const bool is_root = !layer->parent();
165 const bool is_page_scale_application_layer =
166 layer->parent() && layer->parent() == data_from_ancestor.page_scale_layer;
167 const bool is_scrollable = layer->scrollable();
168 const bool is_fixed = layer->position_constraint().is_fixed_position();
170 const bool has_significant_transform =
171 !layer->transform().IsIdentityOr2DTranslation();
173 const bool has_potentially_animated_transform =
174 HasPotentiallyRunningAnimation(layer, Animation::TRANSFORM);
176 const bool has_animated_transform =
177 layer->layer_animation_controller()->IsAnimatingProperty(
178 Animation::TRANSFORM);
180 const bool has_surface = !!layer->render_surface();
182 bool requires_node = is_root || is_scrollable || has_significant_transform ||
183 has_potentially_animated_transform || has_surface ||
184 is_fixed || is_page_scale_application_layer;
186 LayerType* transform_parent = GetTransformParent(data_from_ancestor, layer);
188 int parent_index = 0;
189 if (transform_parent)
190 parent_index = transform_parent->transform_tree_index();
192 int source_index = parent_index;
194 gfx::Vector2dF source_offset;
195 if (transform_parent) {
196 if (layer->scroll_parent()) {
197 LayerType* source = layer->parent();
198 source_offset += source->offset_to_transform_parent();
199 source_index = source->transform_tree_index();
200 } else if (!is_fixed) {
201 source_offset = transform_parent->offset_to_transform_parent();
202 } else {
203 if (data_from_ancestor.transform_tree_parent !=
204 data_from_ancestor.transform_fixed_parent) {
205 source_offset = data_from_ancestor.transform_tree_parent
206 ->offset_to_transform_parent();
207 source_index =
208 data_from_ancestor.transform_tree_parent->transform_tree_index();
210 source_offset += data_from_ancestor.scroll_compensation_adjustment;
214 if (layer->IsContainerForFixedPositionLayers() || is_root)
215 data_for_children->transform_fixed_parent = layer;
216 data_for_children->transform_tree_parent = layer;
218 if (layer->IsContainerForFixedPositionLayers() || is_fixed)
219 data_for_children->scroll_compensation_adjustment = gfx::Vector2dF();
221 if (!requires_node) {
222 data_for_children->should_flatten |= layer->should_flatten_transform();
223 gfx::Vector2dF local_offset = layer->position().OffsetFromOrigin() +
224 layer->transform().To2dTranslation();
225 gfx::Vector2dF source_to_parent;
226 if (source_index != parent_index) {
227 gfx::Transform to_parent;
228 data_from_ancestor.transform_tree->ComputeTransform(
229 source_index, parent_index, &to_parent);
230 source_to_parent = to_parent.To2dTranslation();
232 layer->set_offset_to_transform_parent(source_offset + source_to_parent +
233 local_offset);
234 layer->set_should_flatten_transform_from_property_tree(
235 data_from_ancestor.should_flatten);
236 layer->set_transform_tree_index(parent_index);
237 return false;
240 data_for_children->transform_tree->Insert(TransformNode(), parent_index);
242 TransformNode* node = data_for_children->transform_tree->back();
243 layer->set_transform_tree_index(node->id);
245 node->data.scrolls = is_scrollable;
246 node->data.flattens_inherited_transform = data_for_children->should_flatten;
248 // Surfaces inherently flatten transforms.
249 data_for_children->should_flatten =
250 layer->should_flatten_transform() || has_surface;
251 node->data.target_id =
252 data_from_ancestor.render_target->transform_tree_index();
253 node->data.content_target_id =
254 data_for_children->render_target->transform_tree_index();
255 DCHECK_NE(node->data.target_id, -1);
256 node->data.is_animated = has_animated_transform;
258 float post_local_scale_factor = 1.0f;
259 if (is_root) {
260 node->data.post_local = *data_from_ancestor.device_transform;
261 post_local_scale_factor = data_from_ancestor.device_scale_factor;
264 if (is_page_scale_application_layer)
265 post_local_scale_factor *= data_from_ancestor.page_scale_factor;
267 if (has_surface && !is_root) {
268 node->data.needs_sublayer_scale = true;
269 node->data.layer_scale_factor = data_from_ancestor.device_scale_factor;
270 if (data_from_ancestor.in_subtree_of_page_scale_application_layer)
271 node->data.layer_scale_factor *= data_from_ancestor.page_scale_factor;
274 if (is_root) {
275 node->data.post_local.Scale(post_local_scale_factor,
276 post_local_scale_factor);
277 } else {
278 node->data.post_local_scale_factor = post_local_scale_factor;
279 node->data.source_offset = source_offset;
280 node->data.source_node_id = source_index;
281 node->data.update_post_local_transform(layer->position(),
282 layer->transform_origin());
285 if (!layer->scroll_parent()) {
286 node->data.scroll_offset =
287 gfx::ScrollOffsetToVector2dF(layer->CurrentScrollOffset());
290 node->data.local = layer->transform();
291 node->data.update_pre_local_transform(layer->transform_origin());
293 node->data.needs_local_transform_update = true;
294 data_from_ancestor.transform_tree->UpdateTransforms(node->id);
296 layer->set_offset_to_transform_parent(gfx::Vector2dF());
298 // Flattening (if needed) will be handled by |node|.
299 layer->set_should_flatten_transform_from_property_tree(false);
301 data_for_children->scroll_compensation_adjustment +=
302 layer->ScrollCompensationAdjustment() - node->data.scroll_snap;
304 node->owner_id = layer->id();
306 return true;
309 bool IsAnimatingOpacity(Layer* layer) {
310 return HasPotentiallyRunningAnimation(layer, Animation::OPACITY) ||
311 layer->OpacityCanAnimateOnImplThread();
314 bool IsAnimatingOpacity(LayerImpl* layer) {
315 return HasPotentiallyRunningAnimation(layer, Animation::OPACITY);
318 template <typename LayerType>
319 void AddOpacityNodeIfNeeded(
320 const DataForRecursion<LayerType>& data_from_ancestor,
321 LayerType* layer,
322 DataForRecursion<LayerType>* data_for_children) {
323 const bool is_root = !layer->parent();
324 const bool has_transparency = layer->opacity() != 1.f;
325 const bool has_animated_opacity = IsAnimatingOpacity(layer);
326 bool requires_node = is_root || has_transparency || has_animated_opacity;
328 int parent_id = data_from_ancestor.opacity_tree_parent;
330 if (!requires_node) {
331 layer->set_opacity_tree_index(parent_id);
332 data_for_children->opacity_tree_parent = parent_id;
333 return;
336 OpacityNode node;
337 node.owner_id = layer->id();
338 node.data = layer->opacity();
339 data_for_children->opacity_tree_parent =
340 data_for_children->opacity_tree->Insert(node, parent_id);
341 layer->set_opacity_tree_index(data_for_children->opacity_tree_parent);
344 template <typename LayerType>
345 void BuildPropertyTreesInternal(
346 LayerType* layer,
347 const DataForRecursion<LayerType>& data_from_parent) {
348 DataForRecursion<LayerType> data_for_children(data_from_parent);
349 if (layer->render_surface())
350 data_for_children.render_target = layer;
352 bool created_transform_node =
353 AddTransformNodeIfNeeded(data_from_parent, layer, &data_for_children);
354 AddClipNodeIfNeeded(data_from_parent, layer, created_transform_node,
355 &data_for_children);
357 if (data_from_parent.opacity_tree)
358 AddOpacityNodeIfNeeded(data_from_parent, layer, &data_for_children);
360 if (layer == data_from_parent.page_scale_layer)
361 data_for_children.in_subtree_of_page_scale_application_layer = true;
363 for (size_t i = 0; i < layer->children().size(); ++i) {
364 if (!layer->child_at(i)->scroll_parent())
365 BuildPropertyTreesInternal(layer->child_at(i), data_for_children);
368 if (layer->scroll_children()) {
369 for (LayerType* scroll_child : *layer->scroll_children()) {
370 BuildPropertyTreesInternal(scroll_child, data_for_children);
374 if (layer->has_replica())
375 BuildPropertyTreesInternal(layer->replica_layer(), data_for_children);
378 } // namespace
380 template <typename LayerType>
381 void BuildPropertyTreesTopLevelInternal(LayerType* root_layer,
382 const LayerType* page_scale_layer,
383 float page_scale_factor,
384 float device_scale_factor,
385 const gfx::Rect& viewport,
386 const gfx::Transform& device_transform,
387 PropertyTrees* property_trees) {
388 DataForRecursion<LayerType> data_for_recursion;
389 data_for_recursion.transform_tree = &property_trees->transform_tree;
390 data_for_recursion.clip_tree = &property_trees->clip_tree;
391 data_for_recursion.opacity_tree = &property_trees->opacity_tree;
392 data_for_recursion.transform_tree_parent = nullptr;
393 data_for_recursion.transform_fixed_parent = nullptr;
394 data_for_recursion.render_target = root_layer;
395 data_for_recursion.clip_tree_parent = 0;
396 data_for_recursion.opacity_tree_parent = -1;
397 data_for_recursion.page_scale_layer = page_scale_layer;
398 data_for_recursion.page_scale_factor = page_scale_factor;
399 data_for_recursion.device_scale_factor = device_scale_factor;
400 data_for_recursion.in_subtree_of_page_scale_application_layer = false;
401 data_for_recursion.should_flatten = false;
402 data_for_recursion.ancestor_clips_subtree = true;
403 data_for_recursion.device_transform = &device_transform;
405 data_for_recursion.transform_tree->clear();
406 data_for_recursion.clip_tree->clear();
407 data_for_recursion.opacity_tree->clear();
409 ClipNode root_clip;
410 root_clip.data.clip = viewport;
411 root_clip.data.transform_id = 0;
412 data_for_recursion.clip_tree_parent =
413 data_for_recursion.clip_tree->Insert(root_clip, 0);
414 BuildPropertyTreesInternal(root_layer, data_for_recursion);
415 property_trees->needs_rebuild = false;
417 // The transform tree is kept up-to-date as it is built, but the
418 // combined_clips stored in the clip tree aren't computed during tree
419 // building.
420 property_trees->transform_tree.set_needs_update(false);
421 property_trees->clip_tree.set_needs_update(true);
424 void PropertyTreeBuilder::BuildPropertyTrees(
425 Layer* root_layer,
426 const Layer* page_scale_layer,
427 float page_scale_factor,
428 float device_scale_factor,
429 const gfx::Rect& viewport,
430 const gfx::Transform& device_transform,
431 PropertyTrees* property_trees) {
432 // TODO(enne): hoist this out of here
433 if (!property_trees->needs_rebuild)
434 return;
436 BuildPropertyTreesTopLevelInternal(
437 root_layer, page_scale_layer, page_scale_factor, device_scale_factor,
438 viewport, device_transform, property_trees);
441 void PropertyTreeBuilder::BuildPropertyTrees(
442 LayerImpl* root_layer,
443 const LayerImpl* page_scale_layer,
444 float page_scale_factor,
445 float device_scale_factor,
446 const gfx::Rect& viewport,
447 const gfx::Transform& device_transform,
448 PropertyTrees* property_trees) {
449 BuildPropertyTreesTopLevelInternal(
450 root_layer, page_scale_layer, page_scale_factor, device_scale_factor,
451 viewport, device_transform, property_trees);
454 } // namespace cc