Vectorize website settings icons in omnibox
[chromium-blink-merge.git] / cc / trees / property_tree_builder.cc
blob76e12e053888b3066082540b3fcade6ccf2dc1c5
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->has_render_surface() && is_clipped;
76 const bool render_surface_may_grow_due_to_clip_children =
77 layer->has_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->has_render_surface() ||
148 (!layer_clips_subtree && layer->num_unclipped_descendants()));
150 node.data.render_surface_is_clipped = layer->has_render_surface() &&
151 ancestor_clips_subtree &&
152 !layer->num_unclipped_descendants();
154 data_for_children->clip_tree_parent =
155 data_for_children->clip_tree->Insert(node, parent_id);
158 layer->SetClipTreeIndex(has_unclipped_surface
159 ? kUnclippedRootClipTreeNodeId
160 : data_for_children->clip_tree_parent);
162 layer->set_is_clipped(data_for_children->ancestor_clips_subtree);
164 // TODO(awoloszyn): Right now when we hit a node with a replica, we reset the
165 // clip for all children since we may need to draw. We need to figure out a
166 // better way, since we will need both the clipped and unclipped versions.
169 template <typename LayerType>
170 bool AddTransformNodeIfNeeded(
171 const DataForRecursion<LayerType>& data_from_ancestor,
172 LayerType* layer,
173 DataForRecursion<LayerType>* data_for_children) {
174 const bool is_root = !layer->parent();
175 const bool is_page_scale_layer = layer == data_from_ancestor.page_scale_layer;
176 const bool is_scrollable = layer->scrollable();
177 const bool is_fixed = layer->position_constraint().is_fixed_position();
179 const bool has_significant_transform =
180 !layer->transform().IsIdentityOr2DTranslation();
182 const bool has_potentially_animated_transform =
183 layer->HasPotentiallyRunningTransformAnimation();
185 // A transform node is needed even for a finished animation, since differences
186 // in the timing of animation state updates can mean that an animation that's
187 // in the Finished state at tree-building time on the main thread is still in
188 // the Running state right after commit on the compositor thread.
189 const bool has_any_transform_animation =
190 layer->HasAnyAnimationTargetingProperty(Animation::TRANSFORM);
192 const bool has_surface = layer->has_render_surface();
194 bool requires_node = is_root || is_scrollable || has_significant_transform ||
195 has_any_transform_animation || has_surface || is_fixed ||
196 is_page_scale_layer;
198 LayerType* transform_parent = GetTransformParent(data_from_ancestor, layer);
199 DCHECK_IMPLIES(!is_root, transform_parent);
201 int parent_index = kRootPropertyTreeNodeId;
202 if (transform_parent)
203 parent_index = transform_parent->transform_tree_index();
205 int source_index = parent_index;
207 gfx::Vector2dF source_offset;
208 if (transform_parent) {
209 if (layer->scroll_parent()) {
210 LayerType* source = layer->parent();
211 source_offset += source->offset_to_transform_parent();
212 source_index = source->transform_tree_index();
213 } else if (!is_fixed) {
214 source_offset = transform_parent->offset_to_transform_parent();
215 } else {
216 source_offset = data_from_ancestor.transform_tree_parent
217 ->offset_to_transform_parent();
218 source_index =
219 data_from_ancestor.transform_tree_parent->transform_tree_index();
220 source_offset += data_from_ancestor.scroll_compensation_adjustment;
224 if (layer->IsContainerForFixedPositionLayers() || is_root) {
225 data_for_children->affected_by_inner_viewport_bounds_delta =
226 layer == data_from_ancestor.inner_viewport_scroll_layer;
227 data_for_children->affected_by_outer_viewport_bounds_delta =
228 layer == data_from_ancestor.outer_viewport_scroll_layer;
229 if (is_scrollable) {
230 DCHECK(!is_root);
231 DCHECK(layer->transform().IsIdentity());
232 data_for_children->transform_fixed_parent = layer->parent();
233 } else {
234 data_for_children->transform_fixed_parent = layer;
237 data_for_children->transform_tree_parent = layer;
239 if (layer->IsContainerForFixedPositionLayers() || is_fixed)
240 data_for_children->scroll_compensation_adjustment = gfx::Vector2dF();
242 if (!requires_node) {
243 data_for_children->should_flatten |= layer->should_flatten_transform();
244 gfx::Vector2dF local_offset = layer->position().OffsetFromOrigin() +
245 layer->transform().To2dTranslation();
246 gfx::Vector2dF source_to_parent;
247 if (source_index != parent_index) {
248 gfx::Transform to_parent;
249 data_from_ancestor.transform_tree->ComputeTransform(
250 source_index, parent_index, &to_parent);
251 source_to_parent = to_parent.To2dTranslation();
253 layer->set_offset_to_transform_parent(source_offset + source_to_parent +
254 local_offset);
255 layer->set_should_flatten_transform_from_property_tree(
256 data_from_ancestor.should_flatten);
257 layer->SetTransformTreeIndex(parent_index);
258 return false;
261 data_for_children->transform_tree->Insert(TransformNode(), parent_index);
263 TransformNode* node = data_for_children->transform_tree->back();
264 layer->SetTransformTreeIndex(node->id);
266 node->data.scrolls = is_scrollable;
267 node->data.flattens_inherited_transform = data_for_children->should_flatten;
269 // Surfaces inherently flatten transforms.
270 data_for_children->should_flatten =
271 layer->should_flatten_transform() || has_surface;
272 node->data.target_id =
273 data_from_ancestor.render_target->transform_tree_index();
274 node->data.content_target_id =
275 data_for_children->render_target->transform_tree_index();
276 DCHECK_NE(node->data.target_id, kInvalidPropertyTreeNodeId);
277 node->data.is_animated = has_potentially_animated_transform;
278 if (has_potentially_animated_transform) {
279 float maximum_animation_target_scale = 0.f;
280 if (layer->MaximumTargetScale(&maximum_animation_target_scale)) {
281 node->data.local_maximum_animation_target_scale =
282 maximum_animation_target_scale;
285 float starting_animation_scale = 0.f;
286 if (layer->AnimationStartScale(&starting_animation_scale)) {
287 node->data.local_starting_animation_scale = starting_animation_scale;
290 node->data.has_only_translation_animations =
291 layer->HasOnlyTranslationTransforms();
294 float post_local_scale_factor = 1.0f;
295 if (is_root) {
296 node->data.post_local = *data_from_ancestor.device_transform;
297 post_local_scale_factor = data_from_ancestor.device_scale_factor;
300 if (is_page_scale_layer)
301 post_local_scale_factor *= data_from_ancestor.page_scale_factor;
303 if (has_surface && !is_root) {
304 node->data.needs_sublayer_scale = true;
305 node->data.layer_scale_factor = data_from_ancestor.device_scale_factor;
306 if (data_from_ancestor.in_subtree_of_page_scale_layer)
307 node->data.layer_scale_factor *= data_from_ancestor.page_scale_factor;
310 node->data.source_node_id = source_index;
311 if (is_root) {
312 node->data.post_local.Scale(post_local_scale_factor,
313 post_local_scale_factor);
314 node->data.post_local.Translate(layer->position().x(),
315 layer->position().y());
316 } else {
317 node->data.post_local_scale_factor = post_local_scale_factor;
318 node->data.source_offset = source_offset;
319 node->data.update_post_local_transform(layer->position(),
320 layer->transform_origin());
323 if (!layer->scroll_parent())
324 node->data.scroll_offset = layer->CurrentScrollOffset();
326 if (is_fixed) {
327 if (data_from_ancestor.affected_by_inner_viewport_bounds_delta) {
328 node->data.affected_by_inner_viewport_bounds_delta_x =
329 layer->position_constraint().is_fixed_to_right_edge();
330 node->data.affected_by_inner_viewport_bounds_delta_y =
331 layer->position_constraint().is_fixed_to_bottom_edge();
332 if (node->data.affected_by_inner_viewport_bounds_delta_x ||
333 node->data.affected_by_inner_viewport_bounds_delta_y) {
334 data_for_children->transform_tree
335 ->AddNodeAffectedByInnerViewportBoundsDelta(node->id);
337 } else if (data_from_ancestor.affected_by_outer_viewport_bounds_delta) {
338 node->data.affected_by_outer_viewport_bounds_delta_x =
339 layer->position_constraint().is_fixed_to_right_edge();
340 node->data.affected_by_outer_viewport_bounds_delta_y =
341 layer->position_constraint().is_fixed_to_bottom_edge();
342 if (node->data.affected_by_outer_viewport_bounds_delta_x ||
343 node->data.affected_by_outer_viewport_bounds_delta_y) {
344 data_for_children->transform_tree
345 ->AddNodeAffectedByOuterViewportBoundsDelta(node->id);
350 node->data.local = layer->transform();
351 node->data.update_pre_local_transform(layer->transform_origin());
353 node->data.needs_local_transform_update = true;
354 data_from_ancestor.transform_tree->UpdateTransforms(node->id);
356 layer->set_offset_to_transform_parent(gfx::Vector2dF());
358 // Flattening (if needed) will be handled by |node|.
359 layer->set_should_flatten_transform_from_property_tree(false);
361 data_for_children->scroll_compensation_adjustment +=
362 layer->ScrollCompensationAdjustment() - node->data.scroll_snap;
364 node->owner_id = layer->id();
366 return true;
369 bool IsAnimatingOpacity(Layer* layer) {
370 return layer->HasPotentiallyRunningOpacityAnimation() ||
371 layer->OpacityCanAnimateOnImplThread();
374 bool IsAnimatingOpacity(LayerImpl* layer) {
375 return layer->HasPotentiallyRunningOpacityAnimation();
378 template <typename LayerType>
379 void AddEffectNodeIfNeeded(
380 const DataForRecursion<LayerType>& data_from_ancestor,
381 LayerType* layer,
382 DataForRecursion<LayerType>* data_for_children) {
383 const bool is_root = !layer->parent();
384 const bool has_transparency = layer->opacity() != 1.f;
385 const bool has_animated_opacity = IsAnimatingOpacity(layer);
386 bool requires_node = is_root || has_transparency || has_animated_opacity;
388 int parent_id = data_from_ancestor.effect_tree_parent;
390 if (!requires_node) {
391 layer->SetEffectTreeIndex(parent_id);
392 data_for_children->effect_tree_parent = parent_id;
393 return;
396 EffectNode node;
397 node.owner_id = layer->id();
398 node.data.opacity = layer->opacity();
399 node.data.screen_space_opacity = layer->opacity();
400 if (!is_root)
401 node.data.screen_space_opacity *=
402 data_from_ancestor.effect_tree->Node(parent_id)
403 ->data.screen_space_opacity;
404 data_for_children->effect_tree_parent =
405 data_for_children->effect_tree->Insert(node, parent_id);
406 layer->SetEffectTreeIndex(data_for_children->effect_tree_parent);
409 template <typename LayerType>
410 void BuildPropertyTreesInternal(
411 LayerType* layer,
412 const DataForRecursion<LayerType>& data_from_parent) {
413 layer->set_property_tree_sequence_number(data_from_parent.sequence_number);
414 DataForRecursion<LayerType> data_for_children(data_from_parent);
415 if (layer->has_render_surface()) {
416 data_for_children.render_target = layer;
417 layer->set_draw_blend_mode(SkXfermode::kSrcOver_Mode);
418 } else {
419 layer->set_draw_blend_mode(layer->blend_mode());
422 bool created_transform_node =
423 AddTransformNodeIfNeeded(data_from_parent, layer, &data_for_children);
424 AddClipNodeIfNeeded(data_from_parent, layer, created_transform_node,
425 &data_for_children);
426 AddEffectNodeIfNeeded(data_from_parent, layer, &data_for_children);
428 if (layer == data_from_parent.page_scale_layer)
429 data_for_children.in_subtree_of_page_scale_layer = true;
431 for (size_t i = 0; i < layer->children().size(); ++i) {
432 if (!layer->child_at(i)->scroll_parent()) {
433 BuildPropertyTreesInternal(layer->child_at(i), data_for_children);
434 } else {
435 // The child should be included in its scroll parent's list of scroll
436 // children.
437 DCHECK(layer->child_at(i)->scroll_parent()->scroll_children()->count(
438 layer->child_at(i)));
442 if (layer->scroll_children()) {
443 for (LayerType* scroll_child : *layer->scroll_children()) {
444 DCHECK_EQ(scroll_child->scroll_parent(), layer);
445 BuildPropertyTreesInternal(scroll_child, data_for_children);
449 if (layer->has_replica())
450 BuildPropertyTreesInternal(layer->replica_layer(), data_for_children);
453 } // namespace
455 template <typename LayerType>
456 void BuildPropertyTreesTopLevelInternal(
457 LayerType* root_layer,
458 const LayerType* page_scale_layer,
459 const LayerType* inner_viewport_scroll_layer,
460 const LayerType* outer_viewport_scroll_layer,
461 float page_scale_factor,
462 float device_scale_factor,
463 const gfx::Rect& viewport,
464 const gfx::Transform& device_transform,
465 PropertyTrees* property_trees) {
466 if (!property_trees->needs_rebuild)
467 return;
469 property_trees->sequence_number++;
471 DataForRecursion<LayerType> data_for_recursion;
472 data_for_recursion.transform_tree = &property_trees->transform_tree;
473 data_for_recursion.clip_tree = &property_trees->clip_tree;
474 data_for_recursion.effect_tree = &property_trees->effect_tree;
475 data_for_recursion.transform_tree_parent = nullptr;
476 data_for_recursion.transform_fixed_parent = nullptr;
477 data_for_recursion.render_target = root_layer;
478 data_for_recursion.clip_tree_parent = kUnclippedRootClipTreeNodeId;
479 data_for_recursion.effect_tree_parent = kInvalidPropertyTreeNodeId;
480 data_for_recursion.page_scale_layer = page_scale_layer;
481 data_for_recursion.inner_viewport_scroll_layer = inner_viewport_scroll_layer;
482 data_for_recursion.outer_viewport_scroll_layer = outer_viewport_scroll_layer;
483 data_for_recursion.page_scale_factor = page_scale_factor;
484 data_for_recursion.device_scale_factor = device_scale_factor;
485 data_for_recursion.in_subtree_of_page_scale_layer = false;
486 data_for_recursion.affected_by_inner_viewport_bounds_delta = false;
487 data_for_recursion.affected_by_outer_viewport_bounds_delta = false;
488 data_for_recursion.should_flatten = false;
489 data_for_recursion.ancestor_clips_subtree = true;
490 data_for_recursion.device_transform = &device_transform;
492 data_for_recursion.transform_tree->clear();
493 data_for_recursion.clip_tree->clear();
494 data_for_recursion.effect_tree->clear();
495 data_for_recursion.sequence_number = property_trees->sequence_number;
497 ClipNode root_clip;
498 root_clip.data.clip = viewport;
499 root_clip.data.transform_id = kRootPropertyTreeNodeId;
500 data_for_recursion.clip_tree_parent = data_for_recursion.clip_tree->Insert(
501 root_clip, kUnclippedRootClipTreeNodeId);
502 BuildPropertyTreesInternal(root_layer, data_for_recursion);
503 property_trees->needs_rebuild = false;
505 // The transform tree is kept up-to-date as it is built, but the
506 // combined_clips stored in the clip tree aren't computed during tree
507 // building.
508 property_trees->transform_tree.set_needs_update(false);
509 property_trees->clip_tree.set_needs_update(true);
510 property_trees->effect_tree.set_needs_update(false);
513 void PropertyTreeBuilder::BuildPropertyTrees(
514 Layer* root_layer,
515 const Layer* page_scale_layer,
516 const Layer* inner_viewport_scroll_layer,
517 const Layer* outer_viewport_scroll_layer,
518 float page_scale_factor,
519 float device_scale_factor,
520 const gfx::Rect& viewport,
521 const gfx::Transform& device_transform,
522 PropertyTrees* property_trees) {
523 BuildPropertyTreesTopLevelInternal(
524 root_layer, page_scale_layer, inner_viewport_scroll_layer,
525 outer_viewport_scroll_layer, page_scale_factor, device_scale_factor,
526 viewport, device_transform, property_trees);
529 void PropertyTreeBuilder::BuildPropertyTrees(
530 LayerImpl* root_layer,
531 const LayerImpl* page_scale_layer,
532 const LayerImpl* inner_viewport_scroll_layer,
533 const LayerImpl* outer_viewport_scroll_layer,
534 float page_scale_factor,
535 float device_scale_factor,
536 const gfx::Rect& viewport,
537 const gfx::Transform& device_transform,
538 PropertyTrees* property_trees) {
539 BuildPropertyTreesTopLevelInternal(
540 root_layer, page_scale_layer, inner_viewport_scroll_layer,
541 outer_viewport_scroll_layer, page_scale_factor, device_scale_factor,
542 viewport, device_transform, property_trees);
545 } // namespace cc