ipc: Convert k(u)int*max types from basictypes.h to std::numeric_limits variants.
[chromium-blink-merge.git] / cc / trees / property_tree_builder.cc
blob57950302e62404410d4abc2cfac110b052fbb9cc
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(data_for_children->clip_tree_parent);
160 layer->set_is_clipped(data_for_children->ancestor_clips_subtree);
162 // TODO(awoloszyn): Right now when we hit a node with a replica, we reset the
163 // clip for all children since we may need to draw. We need to figure out a
164 // better way, since we will need both the clipped and unclipped versions.
167 template <typename LayerType>
168 bool AddTransformNodeIfNeeded(
169 const DataForRecursion<LayerType>& data_from_ancestor,
170 LayerType* layer,
171 DataForRecursion<LayerType>* data_for_children) {
172 const bool is_root = !layer->parent();
173 const bool is_page_scale_layer = layer == data_from_ancestor.page_scale_layer;
174 const bool is_scrollable = layer->scrollable();
175 const bool is_fixed = layer->position_constraint().is_fixed_position();
177 const bool has_significant_transform =
178 !layer->transform().IsIdentityOr2DTranslation();
180 const bool has_potentially_animated_transform =
181 layer->HasPotentiallyRunningTransformAnimation();
183 // A transform node is needed even for a finished animation, since differences
184 // in the timing of animation state updates can mean that an animation that's
185 // in the Finished state at tree-building time on the main thread is still in
186 // the Running state right after commit on the compositor thread.
187 const bool has_any_transform_animation =
188 layer->HasAnyAnimationTargetingProperty(Animation::TRANSFORM);
190 const bool has_surface = layer->has_render_surface();
192 bool requires_node = is_root || is_scrollable || has_significant_transform ||
193 has_any_transform_animation || has_surface || is_fixed ||
194 is_page_scale_layer;
196 LayerType* transform_parent = GetTransformParent(data_from_ancestor, layer);
197 DCHECK_IMPLIES(!is_root, transform_parent);
199 int parent_index = kRootPropertyTreeNodeId;
200 if (transform_parent)
201 parent_index = transform_parent->transform_tree_index();
203 int source_index = parent_index;
205 gfx::Vector2dF source_offset;
206 if (transform_parent) {
207 if (layer->scroll_parent()) {
208 LayerType* source = layer->parent();
209 source_offset += source->offset_to_transform_parent();
210 source_index = source->transform_tree_index();
211 } else if (!is_fixed) {
212 source_offset = transform_parent->offset_to_transform_parent();
213 } else {
214 source_offset = data_from_ancestor.transform_tree_parent
215 ->offset_to_transform_parent();
216 source_index =
217 data_from_ancestor.transform_tree_parent->transform_tree_index();
218 source_offset += data_from_ancestor.scroll_compensation_adjustment;
222 if (layer->IsContainerForFixedPositionLayers() || is_root) {
223 data_for_children->affected_by_inner_viewport_bounds_delta =
224 layer == data_from_ancestor.inner_viewport_scroll_layer;
225 data_for_children->affected_by_outer_viewport_bounds_delta =
226 layer == data_from_ancestor.outer_viewport_scroll_layer;
227 if (is_scrollable) {
228 DCHECK(!is_root);
229 DCHECK(layer->transform().IsIdentity());
230 data_for_children->transform_fixed_parent = layer->parent();
231 } else {
232 data_for_children->transform_fixed_parent = layer;
235 data_for_children->transform_tree_parent = layer;
237 if (layer->IsContainerForFixedPositionLayers() || is_fixed)
238 data_for_children->scroll_compensation_adjustment = gfx::Vector2dF();
240 if (!requires_node) {
241 data_for_children->should_flatten |= layer->should_flatten_transform();
242 gfx::Vector2dF local_offset = layer->position().OffsetFromOrigin() +
243 layer->transform().To2dTranslation();
244 gfx::Vector2dF source_to_parent;
245 if (source_index != parent_index) {
246 gfx::Transform to_parent;
247 data_from_ancestor.transform_tree->ComputeTransform(
248 source_index, parent_index, &to_parent);
249 source_to_parent = to_parent.To2dTranslation();
251 layer->set_offset_to_transform_parent(source_offset + source_to_parent +
252 local_offset);
253 layer->set_should_flatten_transform_from_property_tree(
254 data_from_ancestor.should_flatten);
255 layer->SetTransformTreeIndex(parent_index);
256 return false;
259 data_for_children->transform_tree->Insert(TransformNode(), parent_index);
261 TransformNode* node = data_for_children->transform_tree->back();
262 layer->SetTransformTreeIndex(node->id);
264 node->data.scrolls = is_scrollable;
265 node->data.flattens_inherited_transform = data_for_children->should_flatten;
267 // Surfaces inherently flatten transforms.
268 data_for_children->should_flatten =
269 layer->should_flatten_transform() || has_surface;
270 node->data.target_id =
271 data_from_ancestor.render_target->transform_tree_index();
272 node->data.content_target_id =
273 data_for_children->render_target->transform_tree_index();
274 DCHECK_NE(node->data.target_id, kInvalidPropertyTreeNodeId);
275 node->data.is_animated = has_potentially_animated_transform;
276 if (has_potentially_animated_transform) {
277 float maximum_animation_target_scale = 0.f;
278 if (layer->MaximumTargetScale(&maximum_animation_target_scale)) {
279 node->data.local_maximum_animation_target_scale =
280 maximum_animation_target_scale;
283 float starting_animation_scale = 0.f;
284 if (layer->AnimationStartScale(&starting_animation_scale)) {
285 node->data.local_starting_animation_scale = starting_animation_scale;
288 node->data.has_only_translation_animations =
289 layer->HasOnlyTranslationTransforms();
292 float post_local_scale_factor = 1.0f;
293 if (is_root) {
294 node->data.post_local = *data_from_ancestor.device_transform;
295 post_local_scale_factor = data_from_ancestor.device_scale_factor;
298 if (is_page_scale_layer)
299 post_local_scale_factor *= data_from_ancestor.page_scale_factor;
301 if (has_surface && !is_root) {
302 node->data.needs_sublayer_scale = true;
303 node->data.layer_scale_factor = data_from_ancestor.device_scale_factor;
304 if (data_from_ancestor.in_subtree_of_page_scale_layer)
305 node->data.layer_scale_factor *= data_from_ancestor.page_scale_factor;
308 node->data.source_node_id = source_index;
309 if (is_root) {
310 node->data.post_local.Scale(post_local_scale_factor,
311 post_local_scale_factor);
312 node->data.post_local.Translate(layer->position().x(),
313 layer->position().y());
314 } else {
315 node->data.post_local_scale_factor = post_local_scale_factor;
316 node->data.source_offset = source_offset;
317 node->data.update_post_local_transform(layer->position(),
318 layer->transform_origin());
321 if (!layer->scroll_parent())
322 node->data.scroll_offset = layer->CurrentScrollOffset();
324 if (is_fixed) {
325 if (data_from_ancestor.affected_by_inner_viewport_bounds_delta) {
326 node->data.affected_by_inner_viewport_bounds_delta_x =
327 layer->position_constraint().is_fixed_to_right_edge();
328 node->data.affected_by_inner_viewport_bounds_delta_y =
329 layer->position_constraint().is_fixed_to_bottom_edge();
330 if (node->data.affected_by_inner_viewport_bounds_delta_x ||
331 node->data.affected_by_inner_viewport_bounds_delta_y) {
332 data_for_children->transform_tree
333 ->AddNodeAffectedByInnerViewportBoundsDelta(node->id);
335 } else if (data_from_ancestor.affected_by_outer_viewport_bounds_delta) {
336 node->data.affected_by_outer_viewport_bounds_delta_x =
337 layer->position_constraint().is_fixed_to_right_edge();
338 node->data.affected_by_outer_viewport_bounds_delta_y =
339 layer->position_constraint().is_fixed_to_bottom_edge();
340 if (node->data.affected_by_outer_viewport_bounds_delta_x ||
341 node->data.affected_by_outer_viewport_bounds_delta_y) {
342 data_for_children->transform_tree
343 ->AddNodeAffectedByOuterViewportBoundsDelta(node->id);
348 node->data.local = layer->transform();
349 node->data.update_pre_local_transform(layer->transform_origin());
351 node->data.needs_local_transform_update = true;
352 data_from_ancestor.transform_tree->UpdateTransforms(node->id);
354 layer->set_offset_to_transform_parent(gfx::Vector2dF());
356 // Flattening (if needed) will be handled by |node|.
357 layer->set_should_flatten_transform_from_property_tree(false);
359 data_for_children->scroll_compensation_adjustment +=
360 layer->ScrollCompensationAdjustment() - node->data.scroll_snap;
362 node->owner_id = layer->id();
364 return true;
367 bool IsAnimatingOpacity(Layer* layer) {
368 return layer->HasPotentiallyRunningOpacityAnimation() ||
369 layer->OpacityCanAnimateOnImplThread();
372 bool IsAnimatingOpacity(LayerImpl* layer) {
373 return layer->HasPotentiallyRunningOpacityAnimation();
376 template <typename LayerType>
377 void AddEffectNodeIfNeeded(
378 const DataForRecursion<LayerType>& data_from_ancestor,
379 LayerType* layer,
380 DataForRecursion<LayerType>* data_for_children) {
381 const bool is_root = !layer->parent();
382 const bool has_transparency = layer->opacity() != 1.f;
383 const bool has_animated_opacity = IsAnimatingOpacity(layer);
384 bool requires_node = is_root || has_transparency || has_animated_opacity;
386 int parent_id = data_from_ancestor.effect_tree_parent;
388 if (!requires_node) {
389 layer->SetEffectTreeIndex(parent_id);
390 data_for_children->effect_tree_parent = parent_id;
391 return;
394 EffectNode node;
395 node.owner_id = layer->id();
396 node.data.opacity = layer->opacity();
397 node.data.screen_space_opacity = layer->opacity();
398 if (!is_root)
399 node.data.screen_space_opacity *=
400 data_from_ancestor.effect_tree->Node(parent_id)
401 ->data.screen_space_opacity;
402 data_for_children->effect_tree_parent =
403 data_for_children->effect_tree->Insert(node, parent_id);
404 layer->SetEffectTreeIndex(data_for_children->effect_tree_parent);
407 template <typename LayerType>
408 void BuildPropertyTreesInternal(
409 LayerType* layer,
410 const DataForRecursion<LayerType>& data_from_parent) {
411 layer->set_property_tree_sequence_number(data_from_parent.sequence_number);
412 DataForRecursion<LayerType> data_for_children(data_from_parent);
413 if (layer->has_render_surface()) {
414 data_for_children.render_target = layer;
415 layer->set_draw_blend_mode(SkXfermode::kSrcOver_Mode);
416 } else {
417 layer->set_draw_blend_mode(layer->blend_mode());
420 bool created_transform_node =
421 AddTransformNodeIfNeeded(data_from_parent, layer, &data_for_children);
422 AddClipNodeIfNeeded(data_from_parent, layer, created_transform_node,
423 &data_for_children);
424 AddEffectNodeIfNeeded(data_from_parent, layer, &data_for_children);
426 if (layer == data_from_parent.page_scale_layer)
427 data_for_children.in_subtree_of_page_scale_layer = true;
429 for (size_t i = 0; i < layer->children().size(); ++i) {
430 if (!layer->child_at(i)->scroll_parent()) {
431 BuildPropertyTreesInternal(layer->child_at(i), data_for_children);
432 } else {
433 // The child should be included in its scroll parent's list of scroll
434 // children.
435 DCHECK(layer->child_at(i)->scroll_parent()->scroll_children()->count(
436 layer->child_at(i)));
440 if (layer->scroll_children()) {
441 for (LayerType* scroll_child : *layer->scroll_children()) {
442 DCHECK_EQ(scroll_child->scroll_parent(), layer);
443 BuildPropertyTreesInternal(scroll_child, data_for_children);
447 if (layer->has_replica())
448 BuildPropertyTreesInternal(layer->replica_layer(), data_for_children);
451 } // namespace
453 template <typename LayerType>
454 void BuildPropertyTreesTopLevelInternal(
455 LayerType* root_layer,
456 const LayerType* page_scale_layer,
457 const LayerType* inner_viewport_scroll_layer,
458 const LayerType* outer_viewport_scroll_layer,
459 float page_scale_factor,
460 float device_scale_factor,
461 const gfx::Rect& viewport,
462 const gfx::Transform& device_transform,
463 PropertyTrees* property_trees) {
464 if (!property_trees->needs_rebuild)
465 return;
467 property_trees->sequence_number++;
469 DataForRecursion<LayerType> data_for_recursion;
470 data_for_recursion.transform_tree = &property_trees->transform_tree;
471 data_for_recursion.clip_tree = &property_trees->clip_tree;
472 data_for_recursion.effect_tree = &property_trees->effect_tree;
473 data_for_recursion.transform_tree_parent = nullptr;
474 data_for_recursion.transform_fixed_parent = nullptr;
475 data_for_recursion.render_target = root_layer;
476 data_for_recursion.clip_tree_parent = kUnclippedRootClipTreeNodeId;
477 data_for_recursion.effect_tree_parent = kInvalidPropertyTreeNodeId;
478 data_for_recursion.page_scale_layer = page_scale_layer;
479 data_for_recursion.inner_viewport_scroll_layer = inner_viewport_scroll_layer;
480 data_for_recursion.outer_viewport_scroll_layer = outer_viewport_scroll_layer;
481 data_for_recursion.page_scale_factor = page_scale_factor;
482 data_for_recursion.device_scale_factor = device_scale_factor;
483 data_for_recursion.in_subtree_of_page_scale_layer = false;
484 data_for_recursion.affected_by_inner_viewport_bounds_delta = false;
485 data_for_recursion.affected_by_outer_viewport_bounds_delta = false;
486 data_for_recursion.should_flatten = false;
487 data_for_recursion.ancestor_clips_subtree = true;
488 data_for_recursion.device_transform = &device_transform;
490 data_for_recursion.transform_tree->clear();
491 data_for_recursion.clip_tree->clear();
492 data_for_recursion.effect_tree->clear();
493 data_for_recursion.sequence_number = property_trees->sequence_number;
495 ClipNode root_clip;
496 root_clip.data.clip = gfx::RectF(viewport);
497 root_clip.data.transform_id = kRootPropertyTreeNodeId;
498 data_for_recursion.clip_tree_parent = data_for_recursion.clip_tree->Insert(
499 root_clip, kUnclippedRootClipTreeNodeId);
500 BuildPropertyTreesInternal(root_layer, data_for_recursion);
501 property_trees->needs_rebuild = false;
503 // The transform tree is kept up-to-date as it is built, but the
504 // combined_clips stored in the clip tree aren't computed during tree
505 // building.
506 property_trees->transform_tree.set_needs_update(false);
507 property_trees->clip_tree.set_needs_update(true);
508 property_trees->effect_tree.set_needs_update(false);
511 void PropertyTreeBuilder::BuildPropertyTrees(
512 Layer* root_layer,
513 const Layer* page_scale_layer,
514 const Layer* inner_viewport_scroll_layer,
515 const Layer* outer_viewport_scroll_layer,
516 float page_scale_factor,
517 float device_scale_factor,
518 const gfx::Rect& viewport,
519 const gfx::Transform& device_transform,
520 PropertyTrees* property_trees) {
521 BuildPropertyTreesTopLevelInternal(
522 root_layer, page_scale_layer, inner_viewport_scroll_layer,
523 outer_viewport_scroll_layer, page_scale_factor, device_scale_factor,
524 viewport, device_transform, property_trees);
527 void PropertyTreeBuilder::BuildPropertyTrees(
528 LayerImpl* root_layer,
529 const LayerImpl* page_scale_layer,
530 const LayerImpl* inner_viewport_scroll_layer,
531 const LayerImpl* outer_viewport_scroll_layer,
532 float page_scale_factor,
533 float device_scale_factor,
534 const gfx::Rect& viewport,
535 const gfx::Transform& device_transform,
536 PropertyTrees* property_trees) {
537 BuildPropertyTreesTopLevelInternal(
538 root_layer, page_scale_layer, inner_viewport_scroll_layer,
539 outer_viewport_scroll_layer, page_scale_factor, device_scale_factor,
540 viewport, device_transform, property_trees);
543 } // namespace cc