Reland of Add channel-specific product logo to chrome://help (patchset #1 id:1 of...
[chromium-blink-merge.git] / cc / trees / property_tree_builder.cc
blobb42e844d521aa2ac0e61239966107c82ff502d5b
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 const LayerType* inner_viewport_scroll_layer;
35 const LayerType* outer_viewport_scroll_layer;
36 float page_scale_factor;
37 float device_scale_factor;
38 bool in_subtree_of_page_scale_layer;
39 bool affected_by_inner_viewport_bounds_delta;
40 bool affected_by_outer_viewport_bounds_delta;
41 bool should_flatten;
42 bool ancestor_clips_subtree;
43 const gfx::Transform* device_transform;
44 gfx::Vector2dF scroll_compensation_adjustment;
45 int sequence_number;
48 template <typename LayerType>
49 static LayerType* GetTransformParent(const DataForRecursion<LayerType>& data,
50 LayerType* layer) {
51 return layer->position_constraint().is_fixed_position()
52 ? data.transform_fixed_parent
53 : data.transform_tree_parent;
56 template <typename LayerType>
57 static ClipNode* GetClipParent(const DataForRecursion<LayerType>& data,
58 LayerType* layer) {
59 const bool inherits_clip = !layer->parent() || !layer->clip_parent();
60 const int id = inherits_clip ? data.clip_tree_parent
61 : layer->clip_parent()->clip_tree_index();
62 return data.clip_tree->Node(id);
65 template <typename LayerType>
66 static bool RequiresClipNode(LayerType* layer,
67 const DataForRecursion<LayerType>& data,
68 int parent_transform_id,
69 bool is_clipped) {
70 const bool render_surface_applies_clip =
71 layer->render_surface() && is_clipped;
72 const bool render_surface_may_grow_due_to_clip_children =
73 layer->render_surface() && layer->num_unclipped_descendants() > 0;
75 if (layer->masks_to_bounds() || layer->mask_layer() ||
76 render_surface_may_grow_due_to_clip_children)
77 return true;
79 if (!render_surface_applies_clip)
80 return false;
82 return !!layer->parent();
85 template <typename LayerType>
86 static bool LayerClipsSubtree(LayerType* layer) {
87 return layer->masks_to_bounds() || layer->mask_layer();
90 template <typename LayerType>
91 void AddClipNodeIfNeeded(const DataForRecursion<LayerType>& data_from_ancestor,
92 LayerType* layer,
93 bool created_transform_node,
94 DataForRecursion<LayerType>* data_for_children) {
95 ClipNode* parent = GetClipParent(data_from_ancestor, layer);
96 int parent_id = parent->id;
98 bool ancestor_clips_subtree =
99 data_from_ancestor.ancestor_clips_subtree ||
100 (layer->clip_parent() && layer->clip_parent()->is_clipped());
102 data_for_children->ancestor_clips_subtree = false;
103 bool has_unclipped_surface = false;
105 if (layer->has_render_surface()) {
106 if (ancestor_clips_subtree && layer->num_unclipped_descendants() > 0)
107 data_for_children->ancestor_clips_subtree = true;
108 else if (!ancestor_clips_subtree && !layer->num_unclipped_descendants())
109 has_unclipped_surface = true;
110 } else {
111 data_for_children->ancestor_clips_subtree = ancestor_clips_subtree;
114 if (LayerClipsSubtree(layer))
115 data_for_children->ancestor_clips_subtree = true;
117 if (has_unclipped_surface)
118 parent_id = 0;
120 if (!RequiresClipNode(layer, data_from_ancestor, parent->data.transform_id,
121 ancestor_clips_subtree)) {
122 // Unclipped surfaces reset the clip rect.
123 data_for_children->clip_tree_parent = parent_id;
124 } else {
125 LayerType* transform_parent = data_for_children->transform_tree_parent;
126 if (layer->position_constraint().is_fixed_position() &&
127 !created_transform_node) {
128 transform_parent = data_for_children->transform_fixed_parent;
130 ClipNode node;
131 node.data.clip = gfx::RectF(
132 gfx::PointF() + layer->offset_to_transform_parent(), layer->bounds());
133 node.data.transform_id = transform_parent->transform_tree_index();
134 node.data.target_id =
135 data_for_children->render_target->transform_tree_index();
136 node.owner_id = layer->id();
137 node.data.inherit_parent_target_space_clip =
138 !data_for_children->ancestor_clips_subtree &&
139 layer->has_render_surface() && ancestor_clips_subtree;
140 data_for_children->clip_tree_parent =
141 data_for_children->clip_tree->Insert(node, parent_id);
144 layer->SetClipTreeIndex(
145 has_unclipped_surface ? 0 : data_for_children->clip_tree_parent);
147 layer->set_is_clipped(data_for_children->ancestor_clips_subtree);
149 // TODO(awoloszyn): Right now when we hit a node with a replica, we reset the
150 // clip for all children since we may need to draw. We need to figure out a
151 // better way, since we will need both the clipped and unclipped versions.
154 template <typename LayerType>
155 bool AddTransformNodeIfNeeded(
156 const DataForRecursion<LayerType>& data_from_ancestor,
157 LayerType* layer,
158 DataForRecursion<LayerType>* data_for_children) {
159 const bool is_root = !layer->parent();
160 const bool is_page_scale_layer = layer == data_from_ancestor.page_scale_layer;
161 const bool is_scrollable = layer->scrollable();
162 const bool is_fixed = layer->position_constraint().is_fixed_position();
164 const bool has_significant_transform =
165 !layer->transform().IsIdentityOr2DTranslation();
167 const bool has_potentially_animated_transform =
168 layer->HasPotentiallyRunningTransformAnimation();
170 // A transform node is needed even for a finished animation, since differences
171 // in the timing of animation state updates can mean that an animation that's
172 // in the Finished state at tree-building time on the main thread is still in
173 // the Running state right after commit on the compositor thread.
174 const bool has_any_transform_animation =
175 layer->HasAnyAnimationTargetingProperty(Animation::TRANSFORM);
177 const bool has_surface = !!layer->render_surface();
179 bool requires_node = is_root || is_scrollable || has_significant_transform ||
180 has_any_transform_animation || has_surface || is_fixed ||
181 is_page_scale_layer;
183 LayerType* transform_parent = GetTransformParent(data_from_ancestor, layer);
184 DCHECK_IMPLIES(!is_root, transform_parent);
186 int parent_index = 0;
187 if (transform_parent)
188 parent_index = transform_parent->transform_tree_index();
190 int source_index = parent_index;
192 gfx::Vector2dF source_offset;
193 if (transform_parent) {
194 if (layer->scroll_parent()) {
195 LayerType* source = layer->parent();
196 source_offset += source->offset_to_transform_parent();
197 source_index = source->transform_tree_index();
198 } else if (!is_fixed) {
199 source_offset = transform_parent->offset_to_transform_parent();
200 } else {
201 source_offset = data_from_ancestor.transform_tree_parent
202 ->offset_to_transform_parent();
203 source_index =
204 data_from_ancestor.transform_tree_parent->transform_tree_index();
205 source_offset += data_from_ancestor.scroll_compensation_adjustment;
209 if (layer->IsContainerForFixedPositionLayers() || is_root) {
210 data_for_children->affected_by_inner_viewport_bounds_delta =
211 layer == data_from_ancestor.inner_viewport_scroll_layer;
212 data_for_children->affected_by_outer_viewport_bounds_delta =
213 layer == data_from_ancestor.outer_viewport_scroll_layer;
214 if (is_scrollable) {
215 DCHECK(!is_root);
216 DCHECK(layer->transform().IsIdentity());
217 data_for_children->transform_fixed_parent = layer->parent();
218 } else {
219 data_for_children->transform_fixed_parent = layer;
222 data_for_children->transform_tree_parent = layer;
224 if (layer->IsContainerForFixedPositionLayers() || is_fixed)
225 data_for_children->scroll_compensation_adjustment = gfx::Vector2dF();
227 if (!requires_node) {
228 data_for_children->should_flatten |= layer->should_flatten_transform();
229 gfx::Vector2dF local_offset = layer->position().OffsetFromOrigin() +
230 layer->transform().To2dTranslation();
231 gfx::Vector2dF source_to_parent;
232 if (source_index != parent_index) {
233 gfx::Transform to_parent;
234 data_from_ancestor.transform_tree->ComputeTransform(
235 source_index, parent_index, &to_parent);
236 source_to_parent = to_parent.To2dTranslation();
238 layer->set_offset_to_transform_parent(source_offset + source_to_parent +
239 local_offset);
240 layer->set_should_flatten_transform_from_property_tree(
241 data_from_ancestor.should_flatten);
242 layer->SetTransformTreeIndex(parent_index);
243 return false;
246 data_for_children->transform_tree->Insert(TransformNode(), parent_index);
248 TransformNode* node = data_for_children->transform_tree->back();
249 layer->SetTransformTreeIndex(node->id);
251 node->data.scrolls = is_scrollable;
252 node->data.flattens_inherited_transform = data_for_children->should_flatten;
254 // Surfaces inherently flatten transforms.
255 data_for_children->should_flatten =
256 layer->should_flatten_transform() || has_surface;
257 node->data.target_id =
258 data_from_ancestor.render_target->transform_tree_index();
259 node->data.content_target_id =
260 data_for_children->render_target->transform_tree_index();
261 DCHECK_NE(node->data.target_id, -1);
262 node->data.is_animated = has_potentially_animated_transform;
264 float post_local_scale_factor = 1.0f;
265 if (is_root) {
266 node->data.post_local = *data_from_ancestor.device_transform;
267 post_local_scale_factor = data_from_ancestor.device_scale_factor;
270 if (is_page_scale_layer)
271 post_local_scale_factor *= data_from_ancestor.page_scale_factor;
273 if (has_surface && !is_root) {
274 node->data.needs_sublayer_scale = true;
275 node->data.layer_scale_factor = data_from_ancestor.device_scale_factor;
276 if (data_from_ancestor.in_subtree_of_page_scale_layer)
277 node->data.layer_scale_factor *= data_from_ancestor.page_scale_factor;
280 node->data.source_node_id = source_index;
281 if (is_root) {
282 node->data.post_local.Scale(post_local_scale_factor,
283 post_local_scale_factor);
284 node->data.post_local.Translate(layer->position().x(),
285 layer->position().y());
286 } else {
287 node->data.post_local_scale_factor = post_local_scale_factor;
288 node->data.source_offset = source_offset;
289 node->data.update_post_local_transform(layer->position(),
290 layer->transform_origin());
293 if (!layer->scroll_parent())
294 node->data.scroll_offset = layer->CurrentScrollOffset();
296 if (is_fixed) {
297 if (data_from_ancestor.affected_by_inner_viewport_bounds_delta) {
298 node->data.affected_by_inner_viewport_bounds_delta_x =
299 layer->position_constraint().is_fixed_to_right_edge();
300 node->data.affected_by_inner_viewport_bounds_delta_y =
301 layer->position_constraint().is_fixed_to_bottom_edge();
302 if (node->data.affected_by_inner_viewport_bounds_delta_x ||
303 node->data.affected_by_inner_viewport_bounds_delta_y) {
304 data_for_children->transform_tree
305 ->AddNodeAffectedByInnerViewportBoundsDelta(node->id);
307 } else if (data_from_ancestor.affected_by_outer_viewport_bounds_delta) {
308 node->data.affected_by_outer_viewport_bounds_delta_x =
309 layer->position_constraint().is_fixed_to_right_edge();
310 node->data.affected_by_outer_viewport_bounds_delta_y =
311 layer->position_constraint().is_fixed_to_bottom_edge();
312 if (node->data.affected_by_outer_viewport_bounds_delta_x ||
313 node->data.affected_by_outer_viewport_bounds_delta_y) {
314 data_for_children->transform_tree
315 ->AddNodeAffectedByOuterViewportBoundsDelta(node->id);
320 node->data.local = layer->transform();
321 node->data.update_pre_local_transform(layer->transform_origin());
323 node->data.needs_local_transform_update = true;
324 data_from_ancestor.transform_tree->UpdateTransforms(node->id);
326 layer->set_offset_to_transform_parent(gfx::Vector2dF());
328 // Flattening (if needed) will be handled by |node|.
329 layer->set_should_flatten_transform_from_property_tree(false);
331 data_for_children->scroll_compensation_adjustment +=
332 layer->ScrollCompensationAdjustment() - node->data.scroll_snap;
334 node->owner_id = layer->id();
336 return true;
339 bool IsAnimatingOpacity(Layer* layer) {
340 return layer->HasPotentiallyRunningOpacityAnimation() ||
341 layer->OpacityCanAnimateOnImplThread();
344 bool IsAnimatingOpacity(LayerImpl* layer) {
345 return layer->HasPotentiallyRunningOpacityAnimation();
348 template <typename LayerType>
349 void AddOpacityNodeIfNeeded(
350 const DataForRecursion<LayerType>& data_from_ancestor,
351 LayerType* layer,
352 DataForRecursion<LayerType>* data_for_children) {
353 const bool is_root = !layer->parent();
354 const bool has_transparency = layer->opacity() != 1.f;
355 const bool has_animated_opacity = IsAnimatingOpacity(layer);
356 bool requires_node = is_root || has_transparency || has_animated_opacity;
358 int parent_id = data_from_ancestor.opacity_tree_parent;
360 if (!requires_node) {
361 layer->SetOpacityTreeIndex(parent_id);
362 data_for_children->opacity_tree_parent = parent_id;
363 return;
366 OpacityNode node;
367 node.owner_id = layer->id();
368 node.data.opacity = layer->opacity();
369 node.data.screen_space_opacity = layer->opacity();
370 if (!is_root)
371 node.data.screen_space_opacity *=
372 data_from_ancestor.opacity_tree->Node(parent_id)
373 ->data.screen_space_opacity;
374 data_for_children->opacity_tree_parent =
375 data_for_children->opacity_tree->Insert(node, parent_id);
376 layer->SetOpacityTreeIndex(data_for_children->opacity_tree_parent);
379 template <typename LayerType>
380 void BuildPropertyTreesInternal(
381 LayerType* layer,
382 const DataForRecursion<LayerType>& data_from_parent) {
383 layer->set_property_tree_sequence_number(data_from_parent.sequence_number);
384 DataForRecursion<LayerType> data_for_children(data_from_parent);
385 if (layer->render_surface())
386 data_for_children.render_target = layer;
388 bool created_transform_node =
389 AddTransformNodeIfNeeded(data_from_parent, layer, &data_for_children);
390 AddClipNodeIfNeeded(data_from_parent, layer, created_transform_node,
391 &data_for_children);
393 if (data_from_parent.opacity_tree)
394 AddOpacityNodeIfNeeded(data_from_parent, layer, &data_for_children);
396 if (layer == data_from_parent.page_scale_layer)
397 data_for_children.in_subtree_of_page_scale_layer = true;
399 for (size_t i = 0; i < layer->children().size(); ++i) {
400 if (!layer->child_at(i)->scroll_parent()) {
401 BuildPropertyTreesInternal(layer->child_at(i), data_for_children);
402 } else {
403 // The child should be included in its scroll parent's list of scroll
404 // children.
405 DCHECK(layer->child_at(i)->scroll_parent()->scroll_children()->count(
406 layer->child_at(i)));
410 if (layer->scroll_children()) {
411 for (LayerType* scroll_child : *layer->scroll_children()) {
412 DCHECK_EQ(scroll_child->scroll_parent(), layer);
413 BuildPropertyTreesInternal(scroll_child, data_for_children);
417 if (layer->has_replica())
418 BuildPropertyTreesInternal(layer->replica_layer(), data_for_children);
421 } // namespace
423 template <typename LayerType>
424 void BuildPropertyTreesTopLevelInternal(
425 LayerType* root_layer,
426 const LayerType* page_scale_layer,
427 const LayerType* inner_viewport_scroll_layer,
428 const LayerType* outer_viewport_scroll_layer,
429 float page_scale_factor,
430 float device_scale_factor,
431 const gfx::Rect& viewport,
432 const gfx::Transform& device_transform,
433 PropertyTrees* property_trees) {
434 if (!property_trees->needs_rebuild)
435 return;
437 property_trees->sequence_number++;
439 DataForRecursion<LayerType> data_for_recursion;
440 data_for_recursion.transform_tree = &property_trees->transform_tree;
441 data_for_recursion.clip_tree = &property_trees->clip_tree;
442 data_for_recursion.opacity_tree = &property_trees->opacity_tree;
443 data_for_recursion.transform_tree_parent = nullptr;
444 data_for_recursion.transform_fixed_parent = nullptr;
445 data_for_recursion.render_target = root_layer;
446 data_for_recursion.clip_tree_parent = 0;
447 data_for_recursion.opacity_tree_parent = -1;
448 data_for_recursion.page_scale_layer = page_scale_layer;
449 data_for_recursion.inner_viewport_scroll_layer = inner_viewport_scroll_layer;
450 data_for_recursion.outer_viewport_scroll_layer = outer_viewport_scroll_layer;
451 data_for_recursion.page_scale_factor = page_scale_factor;
452 data_for_recursion.device_scale_factor = device_scale_factor;
453 data_for_recursion.in_subtree_of_page_scale_layer = false;
454 data_for_recursion.affected_by_inner_viewport_bounds_delta = false;
455 data_for_recursion.affected_by_outer_viewport_bounds_delta = false;
456 data_for_recursion.should_flatten = false;
457 data_for_recursion.ancestor_clips_subtree = true;
458 data_for_recursion.device_transform = &device_transform;
460 data_for_recursion.transform_tree->clear();
461 data_for_recursion.clip_tree->clear();
462 data_for_recursion.opacity_tree->clear();
463 data_for_recursion.sequence_number = property_trees->sequence_number;
465 ClipNode root_clip;
466 root_clip.data.clip = viewport;
467 root_clip.data.transform_id = 0;
468 data_for_recursion.clip_tree_parent =
469 data_for_recursion.clip_tree->Insert(root_clip, 0);
470 BuildPropertyTreesInternal(root_layer, data_for_recursion);
471 property_trees->needs_rebuild = false;
473 // The transform tree is kept up-to-date as it is built, but the
474 // combined_clips stored in the clip tree aren't computed during tree
475 // building.
476 property_trees->transform_tree.set_needs_update(false);
477 property_trees->clip_tree.set_needs_update(true);
478 property_trees->opacity_tree.set_needs_update(false);
481 void PropertyTreeBuilder::BuildPropertyTrees(
482 Layer* root_layer,
483 const Layer* page_scale_layer,
484 const Layer* inner_viewport_scroll_layer,
485 const Layer* outer_viewport_scroll_layer,
486 float page_scale_factor,
487 float device_scale_factor,
488 const gfx::Rect& viewport,
489 const gfx::Transform& device_transform,
490 PropertyTrees* property_trees) {
491 BuildPropertyTreesTopLevelInternal(
492 root_layer, page_scale_layer, inner_viewport_scroll_layer,
493 outer_viewport_scroll_layer, page_scale_factor, device_scale_factor,
494 viewport, device_transform, property_trees);
497 void PropertyTreeBuilder::BuildPropertyTrees(
498 LayerImpl* root_layer,
499 const LayerImpl* page_scale_layer,
500 const LayerImpl* inner_viewport_scroll_layer,
501 const LayerImpl* outer_viewport_scroll_layer,
502 float page_scale_factor,
503 float device_scale_factor,
504 const gfx::Rect& viewport,
505 const gfx::Transform& device_transform,
506 PropertyTrees* property_trees) {
507 BuildPropertyTreesTopLevelInternal(
508 root_layer, page_scale_layer, inner_viewport_scroll_layer,
509 outer_viewport_scroll_layer, page_scale_factor, device_scale_factor,
510 viewport, device_transform, property_trees);
513 } // namespace cc