Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / cc / trees / property_tree_builder.cc
blob066222bc03ea372d8b953962c2fe5c6da49e3020
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 static const int kInvalidPropertyTreeNodeId = -1;
24 static const int kRootPropertyTreeNodeId = 0;
25 static const int kUnclippedRootClipTreeNodeId = 0;
27 template <typename LayerType>
28 struct DataForRecursion {
29 TransformTree* transform_tree;
30 ClipTree* clip_tree;
31 EffectTree* effect_tree;
32 LayerType* transform_tree_parent;
33 LayerType* transform_fixed_parent;
34 LayerType* render_target;
35 int clip_tree_parent;
36 int effect_tree_parent;
37 const LayerType* page_scale_layer;
38 const LayerType* inner_viewport_scroll_layer;
39 const LayerType* outer_viewport_scroll_layer;
40 float page_scale_factor;
41 float device_scale_factor;
42 bool in_subtree_of_page_scale_layer;
43 bool affected_by_inner_viewport_bounds_delta;
44 bool affected_by_outer_viewport_bounds_delta;
45 bool should_flatten;
46 bool ancestor_clips_subtree;
47 const gfx::Transform* device_transform;
48 gfx::Vector2dF scroll_compensation_adjustment;
49 int sequence_number;
52 template <typename LayerType>
53 static LayerType* GetTransformParent(const DataForRecursion<LayerType>& data,
54 LayerType* layer) {
55 return layer->position_constraint().is_fixed_position()
56 ? data.transform_fixed_parent
57 : data.transform_tree_parent;
60 template <typename LayerType>
61 static ClipNode* GetClipParent(const DataForRecursion<LayerType>& data,
62 LayerType* layer) {
63 const bool inherits_clip = !layer->parent() || !layer->clip_parent();
64 const int id = inherits_clip ? data.clip_tree_parent
65 : layer->clip_parent()->clip_tree_index();
66 return data.clip_tree->Node(id);
69 template <typename LayerType>
70 static bool RequiresClipNode(LayerType* layer,
71 const DataForRecursion<LayerType>& data,
72 int parent_transform_id,
73 bool is_clipped) {
74 const bool render_surface_applies_clip =
75 layer->render_surface() && is_clipped;
76 const bool render_surface_may_grow_due_to_clip_children =
77 layer->render_surface() && layer->num_unclipped_descendants() > 0;
79 if (layer->masks_to_bounds() || layer->mask_layer() ||
80 render_surface_may_grow_due_to_clip_children)
81 return true;
83 if (!render_surface_applies_clip)
84 return false;
86 return true;
89 template <typename LayerType>
90 static bool LayerClipsSubtree(LayerType* layer) {
91 return layer->masks_to_bounds() || layer->mask_layer();
94 template <typename LayerType>
95 void AddClipNodeIfNeeded(const DataForRecursion<LayerType>& data_from_ancestor,
96 LayerType* layer,
97 bool created_transform_node,
98 DataForRecursion<LayerType>* data_for_children) {
99 ClipNode* parent = GetClipParent(data_from_ancestor, layer);
100 int parent_id = parent->id;
102 bool ancestor_clips_subtree =
103 data_from_ancestor.ancestor_clips_subtree ||
104 (layer->clip_parent() && layer->clip_parent()->is_clipped());
106 data_for_children->ancestor_clips_subtree = false;
107 bool has_unclipped_surface = false;
109 if (layer->has_render_surface()) {
110 if (ancestor_clips_subtree && layer->num_unclipped_descendants() > 0)
111 data_for_children->ancestor_clips_subtree = true;
112 else if (!ancestor_clips_subtree && !layer->num_unclipped_descendants())
113 has_unclipped_surface = true;
114 } else {
115 data_for_children->ancestor_clips_subtree = ancestor_clips_subtree;
118 bool layer_clips_subtree = LayerClipsSubtree(layer);
119 if (layer_clips_subtree)
120 data_for_children->ancestor_clips_subtree = true;
122 if (has_unclipped_surface)
123 parent_id = kUnclippedRootClipTreeNodeId;
125 if (!RequiresClipNode(layer, data_from_ancestor, parent->data.transform_id,
126 ancestor_clips_subtree)) {
127 // Unclipped surfaces reset the clip rect.
128 data_for_children->clip_tree_parent = parent_id;
129 } else {
130 LayerType* transform_parent = data_for_children->transform_tree_parent;
131 if (layer->position_constraint().is_fixed_position() &&
132 !created_transform_node) {
133 transform_parent = data_for_children->transform_fixed_parent;
135 ClipNode node;
136 node.data.clip = gfx::RectF(
137 gfx::PointF() + layer->offset_to_transform_parent(), layer->bounds());
138 node.data.transform_id = transform_parent->transform_tree_index();
139 node.data.target_id =
140 data_for_children->render_target->transform_tree_index();
141 node.owner_id = layer->id();
142 node.data.inherit_parent_target_space_clip = !layer_clips_subtree &&
143 layer->has_render_surface() &&
144 ancestor_clips_subtree;
145 node.data.requires_tight_clip_rect =
146 ancestor_clips_subtree &&
147 (!layer->render_surface() ||
148 (layer->has_render_surface() && !layer_clips_subtree &&
149 layer->num_unclipped_descendants()));
151 node.data.render_surface_is_clipped = layer->has_render_surface() &&
152 ancestor_clips_subtree &&
153 !layer->num_unclipped_descendants();
155 data_for_children->clip_tree_parent =
156 data_for_children->clip_tree->Insert(node, parent_id);
159 layer->SetClipTreeIndex(has_unclipped_surface
160 ? kUnclippedRootClipTreeNodeId
161 : data_for_children->clip_tree_parent);
163 layer->set_is_clipped(data_for_children->ancestor_clips_subtree);
165 // TODO(awoloszyn): Right now when we hit a node with a replica, we reset the
166 // clip for all children since we may need to draw. We need to figure out a
167 // better way, since we will need both the clipped and unclipped versions.
170 template <typename LayerType>
171 bool AddTransformNodeIfNeeded(
172 const DataForRecursion<LayerType>& data_from_ancestor,
173 LayerType* layer,
174 DataForRecursion<LayerType>* data_for_children) {
175 const bool is_root = !layer->parent();
176 const bool is_page_scale_layer = layer == data_from_ancestor.page_scale_layer;
177 const bool is_scrollable = layer->scrollable();
178 const bool is_fixed = layer->position_constraint().is_fixed_position();
180 const bool has_significant_transform =
181 !layer->transform().IsIdentityOr2DTranslation();
183 const bool has_potentially_animated_transform =
184 layer->HasPotentiallyRunningTransformAnimation();
186 // A transform node is needed even for a finished animation, since differences
187 // in the timing of animation state updates can mean that an animation that's
188 // in the Finished state at tree-building time on the main thread is still in
189 // the Running state right after commit on the compositor thread.
190 const bool has_any_transform_animation =
191 layer->HasAnyAnimationTargetingProperty(Animation::TRANSFORM);
193 const bool has_surface = !!layer->render_surface();
195 bool requires_node = is_root || is_scrollable || has_significant_transform ||
196 has_any_transform_animation || has_surface || is_fixed ||
197 is_page_scale_layer;
199 LayerType* transform_parent = GetTransformParent(data_from_ancestor, layer);
200 DCHECK_IMPLIES(!is_root, transform_parent);
202 int parent_index = kRootPropertyTreeNodeId;
203 if (transform_parent)
204 parent_index = transform_parent->transform_tree_index();
206 int source_index = parent_index;
208 gfx::Vector2dF source_offset;
209 if (transform_parent) {
210 if (layer->scroll_parent()) {
211 LayerType* source = layer->parent();
212 source_offset += source->offset_to_transform_parent();
213 source_index = source->transform_tree_index();
214 } else if (!is_fixed) {
215 source_offset = transform_parent->offset_to_transform_parent();
216 } else {
217 source_offset = data_from_ancestor.transform_tree_parent
218 ->offset_to_transform_parent();
219 source_index =
220 data_from_ancestor.transform_tree_parent->transform_tree_index();
221 source_offset += data_from_ancestor.scroll_compensation_adjustment;
225 if (layer->IsContainerForFixedPositionLayers() || is_root) {
226 data_for_children->affected_by_inner_viewport_bounds_delta =
227 layer == data_from_ancestor.inner_viewport_scroll_layer;
228 data_for_children->affected_by_outer_viewport_bounds_delta =
229 layer == data_from_ancestor.outer_viewport_scroll_layer;
230 if (is_scrollable) {
231 DCHECK(!is_root);
232 DCHECK(layer->transform().IsIdentity());
233 data_for_children->transform_fixed_parent = layer->parent();
234 } else {
235 data_for_children->transform_fixed_parent = layer;
238 data_for_children->transform_tree_parent = layer;
240 if (layer->IsContainerForFixedPositionLayers() || is_fixed)
241 data_for_children->scroll_compensation_adjustment = gfx::Vector2dF();
243 if (!requires_node) {
244 data_for_children->should_flatten |= layer->should_flatten_transform();
245 gfx::Vector2dF local_offset = layer->position().OffsetFromOrigin() +
246 layer->transform().To2dTranslation();
247 gfx::Vector2dF source_to_parent;
248 if (source_index != parent_index) {
249 gfx::Transform to_parent;
250 data_from_ancestor.transform_tree->ComputeTransform(
251 source_index, parent_index, &to_parent);
252 source_to_parent = to_parent.To2dTranslation();
254 layer->set_offset_to_transform_parent(source_offset + source_to_parent +
255 local_offset);
256 layer->set_should_flatten_transform_from_property_tree(
257 data_from_ancestor.should_flatten);
258 layer->SetTransformTreeIndex(parent_index);
259 return false;
262 data_for_children->transform_tree->Insert(TransformNode(), parent_index);
264 TransformNode* node = data_for_children->transform_tree->back();
265 layer->SetTransformTreeIndex(node->id);
267 node->data.scrolls = is_scrollable;
268 node->data.flattens_inherited_transform = data_for_children->should_flatten;
270 // Surfaces inherently flatten transforms.
271 data_for_children->should_flatten =
272 layer->should_flatten_transform() || has_surface;
273 node->data.target_id =
274 data_from_ancestor.render_target->transform_tree_index();
275 node->data.content_target_id =
276 data_for_children->render_target->transform_tree_index();
277 DCHECK_NE(node->data.target_id, kInvalidPropertyTreeNodeId);
278 node->data.is_animated = has_potentially_animated_transform;
280 float post_local_scale_factor = 1.0f;
281 if (is_root) {
282 node->data.post_local = *data_from_ancestor.device_transform;
283 post_local_scale_factor = data_from_ancestor.device_scale_factor;
286 if (is_page_scale_layer)
287 post_local_scale_factor *= data_from_ancestor.page_scale_factor;
289 if (has_surface && !is_root) {
290 node->data.needs_sublayer_scale = true;
291 node->data.layer_scale_factor = data_from_ancestor.device_scale_factor;
292 if (data_from_ancestor.in_subtree_of_page_scale_layer)
293 node->data.layer_scale_factor *= data_from_ancestor.page_scale_factor;
296 node->data.source_node_id = source_index;
297 if (is_root) {
298 node->data.post_local.Scale(post_local_scale_factor,
299 post_local_scale_factor);
300 node->data.post_local.Translate(layer->position().x(),
301 layer->position().y());
302 } else {
303 node->data.post_local_scale_factor = post_local_scale_factor;
304 node->data.source_offset = source_offset;
305 node->data.update_post_local_transform(layer->position(),
306 layer->transform_origin());
309 if (!layer->scroll_parent())
310 node->data.scroll_offset = layer->CurrentScrollOffset();
312 if (is_fixed) {
313 if (data_from_ancestor.affected_by_inner_viewport_bounds_delta) {
314 node->data.affected_by_inner_viewport_bounds_delta_x =
315 layer->position_constraint().is_fixed_to_right_edge();
316 node->data.affected_by_inner_viewport_bounds_delta_y =
317 layer->position_constraint().is_fixed_to_bottom_edge();
318 if (node->data.affected_by_inner_viewport_bounds_delta_x ||
319 node->data.affected_by_inner_viewport_bounds_delta_y) {
320 data_for_children->transform_tree
321 ->AddNodeAffectedByInnerViewportBoundsDelta(node->id);
323 } else if (data_from_ancestor.affected_by_outer_viewport_bounds_delta) {
324 node->data.affected_by_outer_viewport_bounds_delta_x =
325 layer->position_constraint().is_fixed_to_right_edge();
326 node->data.affected_by_outer_viewport_bounds_delta_y =
327 layer->position_constraint().is_fixed_to_bottom_edge();
328 if (node->data.affected_by_outer_viewport_bounds_delta_x ||
329 node->data.affected_by_outer_viewport_bounds_delta_y) {
330 data_for_children->transform_tree
331 ->AddNodeAffectedByOuterViewportBoundsDelta(node->id);
336 node->data.local = layer->transform();
337 node->data.update_pre_local_transform(layer->transform_origin());
339 node->data.needs_local_transform_update = true;
340 data_from_ancestor.transform_tree->UpdateTransforms(node->id);
342 layer->set_offset_to_transform_parent(gfx::Vector2dF());
344 // Flattening (if needed) will be handled by |node|.
345 layer->set_should_flatten_transform_from_property_tree(false);
347 data_for_children->scroll_compensation_adjustment +=
348 layer->ScrollCompensationAdjustment() - node->data.scroll_snap;
350 node->owner_id = layer->id();
352 return true;
355 bool IsAnimatingOpacity(Layer* layer) {
356 return layer->HasPotentiallyRunningOpacityAnimation() ||
357 layer->OpacityCanAnimateOnImplThread();
360 bool IsAnimatingOpacity(LayerImpl* layer) {
361 return layer->HasPotentiallyRunningOpacityAnimation();
364 template <typename LayerType>
365 void AddEffectNodeIfNeeded(
366 const DataForRecursion<LayerType>& data_from_ancestor,
367 LayerType* layer,
368 DataForRecursion<LayerType>* data_for_children) {
369 const bool is_root = !layer->parent();
370 const bool has_transparency = layer->opacity() != 1.f;
371 const bool has_animated_opacity = IsAnimatingOpacity(layer);
372 bool requires_node = is_root || has_transparency || has_animated_opacity;
374 int parent_id = data_from_ancestor.effect_tree_parent;
376 if (!requires_node) {
377 layer->SetEffectTreeIndex(parent_id);
378 data_for_children->effect_tree_parent = parent_id;
379 return;
382 EffectNode node;
383 node.owner_id = layer->id();
384 node.data.opacity = layer->opacity();
385 node.data.screen_space_opacity = layer->opacity();
386 if (!is_root)
387 node.data.screen_space_opacity *=
388 data_from_ancestor.effect_tree->Node(parent_id)
389 ->data.screen_space_opacity;
390 data_for_children->effect_tree_parent =
391 data_for_children->effect_tree->Insert(node, parent_id);
392 layer->SetEffectTreeIndex(data_for_children->effect_tree_parent);
395 template <typename LayerType>
396 void BuildPropertyTreesInternal(
397 LayerType* layer,
398 const DataForRecursion<LayerType>& data_from_parent) {
399 layer->set_property_tree_sequence_number(data_from_parent.sequence_number);
400 DataForRecursion<LayerType> data_for_children(data_from_parent);
401 if (layer->render_surface()) {
402 data_for_children.render_target = layer;
403 layer->set_draw_blend_mode(SkXfermode::kSrcOver_Mode);
404 } else {
405 layer->set_draw_blend_mode(layer->blend_mode());
408 bool created_transform_node =
409 AddTransformNodeIfNeeded(data_from_parent, layer, &data_for_children);
410 AddClipNodeIfNeeded(data_from_parent, layer, created_transform_node,
411 &data_for_children);
412 AddEffectNodeIfNeeded(data_from_parent, layer, &data_for_children);
414 if (layer == data_from_parent.page_scale_layer)
415 data_for_children.in_subtree_of_page_scale_layer = true;
417 for (size_t i = 0; i < layer->children().size(); ++i) {
418 if (!layer->child_at(i)->scroll_parent()) {
419 BuildPropertyTreesInternal(layer->child_at(i), data_for_children);
420 } else {
421 // The child should be included in its scroll parent's list of scroll
422 // children.
423 DCHECK(layer->child_at(i)->scroll_parent()->scroll_children()->count(
424 layer->child_at(i)));
428 if (layer->scroll_children()) {
429 for (LayerType* scroll_child : *layer->scroll_children()) {
430 DCHECK_EQ(scroll_child->scroll_parent(), layer);
431 BuildPropertyTreesInternal(scroll_child, data_for_children);
435 if (layer->has_replica())
436 BuildPropertyTreesInternal(layer->replica_layer(), data_for_children);
439 } // namespace
441 template <typename LayerType>
442 void BuildPropertyTreesTopLevelInternal(
443 LayerType* root_layer,
444 const LayerType* page_scale_layer,
445 const LayerType* inner_viewport_scroll_layer,
446 const LayerType* outer_viewport_scroll_layer,
447 float page_scale_factor,
448 float device_scale_factor,
449 const gfx::Rect& viewport,
450 const gfx::Transform& device_transform,
451 PropertyTrees* property_trees) {
452 if (!property_trees->needs_rebuild)
453 return;
455 property_trees->sequence_number++;
457 DataForRecursion<LayerType> data_for_recursion;
458 data_for_recursion.transform_tree = &property_trees->transform_tree;
459 data_for_recursion.clip_tree = &property_trees->clip_tree;
460 data_for_recursion.effect_tree = &property_trees->effect_tree;
461 data_for_recursion.transform_tree_parent = nullptr;
462 data_for_recursion.transform_fixed_parent = nullptr;
463 data_for_recursion.render_target = root_layer;
464 data_for_recursion.clip_tree_parent = kUnclippedRootClipTreeNodeId;
465 data_for_recursion.effect_tree_parent = kInvalidPropertyTreeNodeId;
466 data_for_recursion.page_scale_layer = page_scale_layer;
467 data_for_recursion.inner_viewport_scroll_layer = inner_viewport_scroll_layer;
468 data_for_recursion.outer_viewport_scroll_layer = outer_viewport_scroll_layer;
469 data_for_recursion.page_scale_factor = page_scale_factor;
470 data_for_recursion.device_scale_factor = device_scale_factor;
471 data_for_recursion.in_subtree_of_page_scale_layer = false;
472 data_for_recursion.affected_by_inner_viewport_bounds_delta = false;
473 data_for_recursion.affected_by_outer_viewport_bounds_delta = false;
474 data_for_recursion.should_flatten = false;
475 data_for_recursion.ancestor_clips_subtree = true;
476 data_for_recursion.device_transform = &device_transform;
478 data_for_recursion.transform_tree->clear();
479 data_for_recursion.clip_tree->clear();
480 data_for_recursion.effect_tree->clear();
481 data_for_recursion.sequence_number = property_trees->sequence_number;
483 ClipNode root_clip;
484 root_clip.data.clip = viewport;
485 root_clip.data.transform_id = kRootPropertyTreeNodeId;
486 data_for_recursion.clip_tree_parent = data_for_recursion.clip_tree->Insert(
487 root_clip, kUnclippedRootClipTreeNodeId);
488 BuildPropertyTreesInternal(root_layer, data_for_recursion);
489 property_trees->needs_rebuild = false;
491 // The transform tree is kept up-to-date as it is built, but the
492 // combined_clips stored in the clip tree aren't computed during tree
493 // building.
494 property_trees->transform_tree.set_needs_update(false);
495 property_trees->clip_tree.set_needs_update(true);
496 property_trees->effect_tree.set_needs_update(false);
499 void PropertyTreeBuilder::BuildPropertyTrees(
500 Layer* root_layer,
501 const Layer* page_scale_layer,
502 const Layer* inner_viewport_scroll_layer,
503 const Layer* outer_viewport_scroll_layer,
504 float page_scale_factor,
505 float device_scale_factor,
506 const gfx::Rect& viewport,
507 const gfx::Transform& device_transform,
508 PropertyTrees* property_trees) {
509 BuildPropertyTreesTopLevelInternal(
510 root_layer, page_scale_layer, inner_viewport_scroll_layer,
511 outer_viewport_scroll_layer, page_scale_factor, device_scale_factor,
512 viewport, device_transform, property_trees);
515 void PropertyTreeBuilder::BuildPropertyTrees(
516 LayerImpl* root_layer,
517 const LayerImpl* page_scale_layer,
518 const LayerImpl* inner_viewport_scroll_layer,
519 const LayerImpl* outer_viewport_scroll_layer,
520 float page_scale_factor,
521 float device_scale_factor,
522 const gfx::Rect& viewport,
523 const gfx::Transform& device_transform,
524 PropertyTrees* property_trees) {
525 BuildPropertyTreesTopLevelInternal(
526 root_layer, page_scale_layer, inner_viewport_scroll_layer,
527 outer_viewport_scroll_layer, page_scale_factor, device_scale_factor,
528 viewport, device_transform, property_trees);
531 } // namespace cc