Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / cc / trees / draw_property_utils.cc
blob1f5f57040a83ad40ef63bfd0ddf863c28e8d5de5
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/draw_property_utils.h"
7 #include <vector>
9 #include "cc/base/math_util.h"
10 #include "cc/layers/layer.h"
11 #include "cc/layers/layer_impl.h"
12 #include "cc/trees/layer_tree_impl.h"
13 #include "cc/trees/property_tree.h"
14 #include "cc/trees/property_tree_builder.h"
15 #include "ui/gfx/geometry/rect_conversions.h"
17 namespace cc {
19 namespace {
21 template <typename LayerType>
22 void CalculateVisibleRects(const std::vector<LayerType*>& visible_layer_list,
23 const ClipTree& clip_tree,
24 const TransformTree& transform_tree) {
25 for (auto& layer : visible_layer_list) {
26 // TODO(ajuma): Compute content_scale rather than using it. Note that for
27 // PictureLayer and PictureImageLayers, content_bounds == bounds and
28 // content_scale_x == content_scale_y == 1.0, so once impl painting is on
29 // everywhere, this code will be unnecessary.
30 gfx::Size layer_bounds = layer->bounds();
31 const bool has_clip = layer->clip_tree_index() > 0;
32 const TransformNode* transform_node =
33 transform_tree.Node(layer->transform_tree_index());
34 if (has_clip) {
35 const ClipNode* clip_node = clip_tree.Node(layer->clip_tree_index());
36 const TransformNode* clip_transform_node =
37 transform_tree.Node(clip_node->data.transform_id);
38 const bool target_is_root_surface =
39 transform_node->data.content_target_id == 1;
40 // When the target is the root surface, we need to include the root
41 // transform by walking up to the root of the transform tree.
42 const int target_id =
43 target_is_root_surface ? 0 : transform_node->data.content_target_id;
44 const TransformNode* target_node = transform_tree.Node(target_id);
46 gfx::Transform content_to_target = transform_node->data.to_target;
48 content_to_target.Translate(layer->offset_to_transform_parent().x(),
49 layer->offset_to_transform_parent().y());
51 gfx::Rect combined_clip_rect_in_target_space;
52 gfx::Rect clip_rect_in_target_space;
53 gfx::Transform clip_to_target;
54 bool success = true;
55 if (clip_transform_node->data.target_id == target_node->id) {
56 clip_to_target = clip_transform_node->data.to_target;
57 } else {
58 success = transform_tree.ComputeTransformWithDestinationSublayerScale(
59 clip_transform_node->id, target_node->id, &clip_to_target);
61 if (target_node->id > clip_node->data.transform_id) {
62 if (!success) {
63 DCHECK(target_node->data.to_screen_is_animated);
65 // An animated singular transform may become non-singular during the
66 // animation, so we still need to compute a visible rect. In this
67 // situation, we treat the entire layer as visible.
68 layer->set_visible_rect_from_property_trees(gfx::Rect(layer_bounds));
69 layer->set_clip_rect_in_target_space_from_property_trees(
70 gfx::ToEnclosingRect(clip_node->data.combined_clip));
71 continue;
74 combined_clip_rect_in_target_space =
75 gfx::ToEnclosingRect(MathUtil::ProjectClippedRect(
76 clip_to_target, clip_node->data.combined_clip));
77 clip_rect_in_target_space = gfx::ToEnclosingRect(
78 MathUtil::ProjectClippedRect(clip_to_target, clip_node->data.clip));
79 } else {
80 // Computing a transform to an ancestor should always succeed.
81 DCHECK(success);
82 combined_clip_rect_in_target_space =
83 gfx::ToEnclosingRect(MathUtil::MapClippedRect(
84 clip_to_target, clip_node->data.combined_clip));
85 clip_rect_in_target_space = gfx::ToEnclosingRect(
86 MathUtil::MapClippedRect(clip_to_target, clip_node->data.clip));
89 if (clip_node->data.requires_tight_clip_rect)
90 layer->set_clip_rect_in_target_space_from_property_trees(
91 combined_clip_rect_in_target_space);
92 else
93 layer->set_clip_rect_in_target_space_from_property_trees(
94 clip_rect_in_target_space);
96 gfx::Rect layer_content_rect = gfx::Rect(layer_bounds);
97 gfx::Rect layer_content_bounds_in_target_space =
98 MathUtil::MapEnclosingClippedRect(content_to_target,
99 layer_content_rect);
100 combined_clip_rect_in_target_space.Intersect(
101 layer_content_bounds_in_target_space);
102 clip_rect_in_target_space.Intersect(layer_content_bounds_in_target_space);
103 if (combined_clip_rect_in_target_space.IsEmpty()) {
104 layer->set_visible_rect_from_property_trees(gfx::Rect());
105 continue;
108 // If the layer is fully contained within the clip, treat it as fully
109 // visible. Since clip_rect_in_target_space has already been intersected
110 // with layer_content_bounds_in_target_space, the layer is fully contained
111 // within the clip iff these rects are equal.
112 if (combined_clip_rect_in_target_space ==
113 layer_content_bounds_in_target_space) {
114 layer->set_visible_rect_from_property_trees(gfx::Rect(layer_bounds));
115 continue;
118 gfx::Transform target_to_content;
119 gfx::Transform target_to_layer;
121 if (transform_node->data.ancestors_are_invertible) {
122 target_to_layer = transform_node->data.from_target;
123 success = true;
124 } else {
125 success = transform_tree.ComputeTransformWithSourceSublayerScale(
126 target_node->id, transform_node->id, &target_to_layer);
129 if (!success) {
130 // An animated singular transform may become non-singular during the
131 // animation, so we still need to compute a visible rect. In this
132 // situation, we treat the entire layer as visible.
133 layer->set_visible_rect_from_property_trees(gfx::Rect(layer_bounds));
134 continue;
137 target_to_content.Translate(-layer->offset_to_transform_parent().x(),
138 -layer->offset_to_transform_parent().y());
139 target_to_content.PreconcatTransform(target_to_layer);
141 gfx::Rect visible_rect = MathUtil::ProjectEnclosingClippedRect(
142 target_to_content, combined_clip_rect_in_target_space);
144 visible_rect.Intersect(gfx::Rect(layer_bounds));
146 layer->set_visible_rect_from_property_trees(visible_rect);
147 } else {
148 layer->set_visible_rect_from_property_trees(gfx::Rect(layer_bounds));
149 layer->set_clip_rect_in_target_space_from_property_trees(
150 gfx::Rect(layer_bounds));
155 template <typename LayerType>
156 static bool IsRootLayerOfNewRenderingContext(LayerType* layer) {
157 if (layer->parent())
158 return !layer->parent()->Is3dSorted() && layer->Is3dSorted();
159 return layer->Is3dSorted();
162 template <typename LayerType>
163 static inline bool LayerIsInExisting3DRenderingContext(LayerType* layer) {
164 return layer->Is3dSorted() && layer->parent() &&
165 layer->parent()->Is3dSorted() &&
166 layer->parent()->sorting_context_id() == layer->sorting_context_id();
169 template <typename LayerType>
170 static bool TransformToScreenIsKnown(LayerType* layer,
171 const TransformTree& tree) {
172 const TransformNode* node = tree.Node(layer->transform_tree_index());
173 return !node->data.to_screen_is_animated;
176 template <typename LayerType>
177 static bool HasSingularTransform(LayerType* layer, const TransformTree& tree) {
178 const TransformNode* node = tree.Node(layer->transform_tree_index());
179 return !node->data.is_invertible || !node->data.ancestors_are_invertible;
182 template <typename LayerType>
183 static bool IsLayerBackFaceVisible(LayerType* layer,
184 const TransformTree& tree) {
185 // The current W3C spec on CSS transforms says that backface visibility should
186 // be determined differently depending on whether the layer is in a "3d
187 // rendering context" or not. For Chromium code, we can determine whether we
188 // are in a 3d rendering context by checking if the parent preserves 3d.
190 if (LayerIsInExisting3DRenderingContext(layer))
191 return DrawTransformFromPropertyTrees(layer, tree).IsBackFaceVisible();
193 // In this case, either the layer establishes a new 3d rendering context, or
194 // is not in a 3d rendering context at all.
195 return layer->transform().IsBackFaceVisible();
198 template <typename LayerType>
199 static bool IsSurfaceBackFaceVisible(LayerType* layer,
200 const TransformTree& tree) {
201 if (LayerIsInExisting3DRenderingContext(layer)) {
202 const TransformNode* node = tree.Node(layer->transform_tree_index());
203 // Draw transform as a contributing render surface.
204 // TODO(enne): we shouldn't walk the tree during a tree walk.
205 gfx::Transform surface_draw_transform;
206 tree.ComputeTransform(node->id, node->data.target_id,
207 &surface_draw_transform);
208 return surface_draw_transform.IsBackFaceVisible();
211 if (IsRootLayerOfNewRenderingContext(layer))
212 return layer->transform().IsBackFaceVisible();
214 // If the render_surface is not part of a new or existing rendering context,
215 // then the layers that contribute to this surface will decide back-face
216 // visibility for themselves.
217 return false;
220 template <typename LayerType>
221 static bool IsAnimatingTransformToScreen(LayerType* layer,
222 const TransformTree& tree) {
223 const TransformNode* node = tree.Node(layer->transform_tree_index());
224 return node->data.to_screen_is_animated;
227 static inline bool TransformToScreenIsKnown(Layer* layer,
228 const TransformTree& tree) {
229 return !IsAnimatingTransformToScreen(layer, tree);
232 static inline bool TransformToScreenIsKnown(LayerImpl* layer,
233 const TransformTree& tree) {
234 return true;
237 template <typename LayerType>
238 static bool HasInvertibleOrAnimatedTransform(LayerType* layer) {
239 return layer->transform_is_invertible() ||
240 layer->HasPotentiallyRunningTransformAnimation();
243 static inline bool SubtreeShouldBeSkipped(LayerImpl* layer,
244 bool layer_is_drawn,
245 const TransformTree& tree) {
246 // If the layer transform is not invertible, it should not be drawn.
247 // TODO(ajuma): Correctly process subtrees with singular transform for the
248 // case where we may animate to a non-singular transform and wish to
249 // pre-raster.
250 if (!HasInvertibleOrAnimatedTransform(layer))
251 return true;
253 // When we need to do a readback/copy of a layer's output, we can not skip
254 // it or any of its ancestors.
255 if (layer->draw_properties().layer_or_descendant_has_copy_request)
256 return false;
258 // If the layer is not drawn, then skip it and its subtree.
259 if (!layer_is_drawn)
260 return true;
262 if (layer->render_surface() && !layer->double_sided() &&
263 IsSurfaceBackFaceVisible(layer, tree))
264 return true;
266 // If layer is on the pending tree and opacity is being animated then
267 // this subtree can't be skipped as we need to create, prioritize and
268 // include tiles for this layer when deciding if tree can be activated.
269 if (layer->layer_tree_impl()->IsPendingTree() &&
270 layer->HasPotentiallyRunningOpacityAnimation())
271 return false;
273 // The opacity of a layer always applies to its children (either implicitly
274 // via a render surface or explicitly if the parent preserves 3D), so the
275 // entire subtree can be skipped if this layer is fully transparent.
276 return !layer->opacity();
279 static inline bool SubtreeShouldBeSkipped(Layer* layer,
280 bool layer_is_drawn,
281 const TransformTree& tree) {
282 // If the layer transform is not invertible, it should not be drawn.
283 if (!layer->transform_is_invertible() &&
284 !layer->HasPotentiallyRunningTransformAnimation())
285 return true;
287 // When we need to do a readback/copy of a layer's output, we can not skip
288 // it or any of its ancestors.
289 if (layer->draw_properties().layer_or_descendant_has_copy_request)
290 return false;
292 // If the layer is not drawn, then skip it and its subtree.
293 if (!layer_is_drawn)
294 return true;
296 if (layer->render_surface() && !layer->double_sided() &&
297 !layer->HasPotentiallyRunningTransformAnimation() &&
298 IsSurfaceBackFaceVisible(layer, tree))
299 return true;
301 // If the opacity is being animated then the opacity on the main thread is
302 // unreliable (since the impl thread may be using a different opacity), so it
303 // should not be trusted.
304 // In particular, it should not cause the subtree to be skipped.
305 // Similarly, for layers that might animate opacity using an impl-only
306 // animation, their subtree should also not be skipped.
307 return !layer->opacity() && !layer->HasPotentiallyRunningOpacityAnimation() &&
308 !layer->OpacityCanAnimateOnImplThread();
311 template <typename LayerType>
312 static bool LayerShouldBeSkipped(LayerType* layer,
313 bool layer_is_drawn,
314 const TransformTree& tree) {
315 // Layers can be skipped if any of these conditions are met.
316 // - is not drawn due to it or one of its ancestors being hidden (or having
317 // no copy requests).
318 // - does not draw content.
319 // - is transparent.
320 // - has empty bounds
321 // - the layer is not double-sided, but its back face is visible.
323 // Some additional conditions need to be computed at a later point after the
324 // recursion is finished.
325 // - the intersection of render_surface content and layer clip_rect is empty
326 // - the visible_layer_rect is empty
328 // Note, if the layer should not have been drawn due to being fully
329 // transparent, we would have skipped the entire subtree and never made it
330 // into this function, so it is safe to omit this check here.
331 if (!layer_is_drawn)
332 return true;
334 if (!layer->DrawsContent() || layer->bounds().IsEmpty())
335 return true;
337 LayerType* backface_test_layer = layer;
338 if (layer->use_parent_backface_visibility()) {
339 DCHECK(layer->parent());
340 DCHECK(!layer->parent()->use_parent_backface_visibility());
341 backface_test_layer = layer->parent();
344 // The layer should not be drawn if (1) it is not double-sided and (2) the
345 // back of the layer is known to be facing the screen.
346 if (!backface_test_layer->double_sided() &&
347 TransformToScreenIsKnown(backface_test_layer, tree) &&
348 IsLayerBackFaceVisible(backface_test_layer, tree))
349 return true;
351 return false;
354 template <typename LayerType>
355 void FindLayersThatNeedUpdates(
356 LayerType* layer,
357 const TransformTree& tree,
358 bool subtree_is_visible_from_ancestor,
359 typename LayerType::LayerListType* update_layer_list,
360 std::vector<LayerType*>* visible_layer_list) {
361 bool layer_is_drawn =
362 layer->HasCopyRequest() ||
363 (subtree_is_visible_from_ancestor && !layer->hide_layer_and_subtree());
365 if (layer->parent() && SubtreeShouldBeSkipped(layer, layer_is_drawn, tree))
366 return;
368 if (!LayerShouldBeSkipped(layer, layer_is_drawn, tree)) {
369 visible_layer_list->push_back(layer);
370 update_layer_list->push_back(layer);
373 // Append mask layers to the update layer list. They don't have valid visible
374 // rects, so need to get added after the above calculation. Replica layers
375 // don't need to be updated.
376 if (LayerType* mask_layer = layer->mask_layer())
377 update_layer_list->push_back(mask_layer);
378 if (LayerType* replica_layer = layer->replica_layer()) {
379 if (LayerType* mask_layer = replica_layer->mask_layer())
380 update_layer_list->push_back(mask_layer);
383 for (size_t i = 0; i < layer->children().size(); ++i) {
384 FindLayersThatNeedUpdates(layer->child_at(i), tree, layer_is_drawn,
385 update_layer_list, visible_layer_list);
389 } // namespace
391 void ComputeClips(ClipTree* clip_tree, const TransformTree& transform_tree) {
392 if (!clip_tree->needs_update())
393 return;
394 for (int i = 0; i < static_cast<int>(clip_tree->size()); ++i) {
395 ClipNode* clip_node = clip_tree->Node(i);
396 const TransformNode* transform_node =
397 transform_tree.Node(clip_node->data.transform_id);
399 // Only descendants of a real clipping layer (i.e., not 0) may have their
400 // clip adjusted due to intersecting with an ancestor clip.
401 const bool is_clipped = clip_node->parent_id > 0;
402 if (!is_clipped) {
403 DCHECK(!clip_node->data.inherit_parent_target_space_clip);
404 clip_node->data.combined_clip = clip_node->data.clip;
405 if (clip_node->id > 0) {
406 gfx::Transform to_target = transform_node->data.to_target;
407 clip_node->data.clip_in_target_space =
408 MathUtil::MapClippedRect(to_target, clip_node->data.combined_clip);
410 continue;
413 ClipNode* parent_clip_node = clip_tree->parent(clip_node);
414 const TransformNode* parent_transform_node =
415 transform_tree.Node(parent_clip_node->data.transform_id);
417 // Clips must be combined in target space. We cannot, for example, combine
418 // clips in the space of the child clip. The reason is non-affine
419 // transforms. Say we have the following tree T->A->B->C, and B clips C, but
420 // draw into target T. It may be the case that A applies a perspective
421 // transform, and B and C are at different z positions. When projected into
422 // target space, the relative sizes and positions of B and C can shift.
423 // Since it's the relationship in target space that matters, that's where we
424 // must combine clips.
425 gfx::Transform parent_to_target;
426 gfx::Transform clip_to_target;
428 gfx::Transform target_to_clip;
429 gfx::Transform parent_to_transform_target;
430 gfx::Transform transform_target_to_target;
432 const bool target_is_root_surface = clip_node->data.target_id == 1;
433 // When the target is the root surface, we need to include the root
434 // transform by walking up to the root of the transform tree.
435 const int target_id =
436 target_is_root_surface ? 0 : clip_node->data.target_id;
438 bool success = true;
439 // When render surface applies clip, we need the clip from the target's
440 // target space. But, as the combined clip is in parent clip's target
441 // space, we need to first transform it from parent's target space to
442 // target's target space.
443 if (clip_node->data.inherit_parent_target_space_clip) {
444 success &= transform_tree.ComputeTransformWithDestinationSublayerScale(
445 parent_transform_node->id, transform_node->data.target_id,
446 &parent_to_transform_target);
447 success &= transform_tree.ComputeTransformWithSourceSublayerScale(
448 transform_node->data.target_id, target_id,
449 &transform_target_to_target);
450 transform_target_to_target.matrix().postScale(
451 transform_node->data.sublayer_scale.x(),
452 transform_node->data.sublayer_scale.y(), 1.0);
453 } else if (parent_transform_node->data.content_target_id ==
454 clip_node->data.target_id) {
455 parent_to_target = parent_transform_node->data.to_target;
456 } else {
457 success &= transform_tree.ComputeTransformWithDestinationSublayerScale(
458 parent_transform_node->id, target_id, &parent_to_target);
461 if (transform_node->data.content_target_id == clip_node->data.target_id) {
462 clip_to_target = transform_node->data.to_target;
463 } else {
464 success &= transform_tree.ComputeTransformWithDestinationSublayerScale(
465 transform_node->id, target_id, &clip_to_target);
468 if (transform_node->data.content_target_id == clip_node->data.target_id &&
469 transform_node->data.ancestors_are_invertible) {
470 target_to_clip = transform_node->data.from_target;
471 } else {
472 success &= clip_to_target.GetInverse(&target_to_clip);
475 // If we can't compute a transform, it's because we had to use the inverse
476 // of a singular transform. We won't draw in this case, so there's no need
477 // to compute clips.
478 if (!success) {
479 continue;
482 // In order to intersect with as small a rect as possible, we do a
483 // preliminary clip in target space so that when we project back, there's
484 // less likelihood of intersecting the view plane.
485 gfx::RectF inherited_clip_in_target_space;
486 if (clip_node->data.inherit_parent_target_space_clip) {
487 gfx::RectF combined_clip_in_transform_target_space;
488 if (parent_transform_node->id > transform_node->data.target_id)
489 combined_clip_in_transform_target_space = MathUtil::MapClippedRect(
490 parent_to_transform_target, parent_clip_node->data.combined_clip);
491 else
492 combined_clip_in_transform_target_space = MathUtil::ProjectClippedRect(
493 parent_to_transform_target, parent_clip_node->data.combined_clip);
494 inherited_clip_in_target_space = MathUtil::ProjectClippedRect(
495 transform_target_to_target, combined_clip_in_transform_target_space);
496 } else if (parent_transform_node->id > target_id) {
497 inherited_clip_in_target_space = MathUtil::MapClippedRect(
498 parent_to_target, parent_clip_node->data.combined_clip);
499 } else {
500 inherited_clip_in_target_space = MathUtil::ProjectClippedRect(
501 parent_to_target, parent_clip_node->data.combined_clip);
504 // When render surface inherits its parent target space clip, the layer
505 // that created the clip node doesn't apply any clip. So, we shouldn't clip
506 // using the clip value stored in the clip node.
507 gfx::RectF intersected_in_target_space;
508 if (!clip_node->data.inherit_parent_target_space_clip) {
509 gfx::RectF clip_in_target_space =
510 MathUtil::MapClippedRect(clip_to_target, clip_node->data.clip);
512 intersected_in_target_space = gfx::IntersectRects(
513 inherited_clip_in_target_space, clip_in_target_space);
514 } else {
515 intersected_in_target_space = inherited_clip_in_target_space;
517 clip_node->data.clip_in_target_space = intersected_in_target_space;
519 clip_node->data.combined_clip = MathUtil::ProjectClippedRect(
520 target_to_clip, intersected_in_target_space);
522 if (!clip_node->data.inherit_parent_target_space_clip)
523 clip_node->data.combined_clip.Intersect(clip_node->data.clip);
525 clip_tree->set_needs_update(false);
528 void ComputeTransforms(TransformTree* transform_tree) {
529 if (!transform_tree->needs_update())
530 return;
531 for (int i = 1; i < static_cast<int>(transform_tree->size()); ++i)
532 transform_tree->UpdateTransforms(i);
533 transform_tree->set_needs_update(false);
536 void ComputeOpacities(EffectTree* effect_tree) {
537 if (!effect_tree->needs_update())
538 return;
539 for (int i = 1; i < static_cast<int>(effect_tree->size()); ++i)
540 effect_tree->UpdateOpacities(i);
541 effect_tree->set_needs_update(false);
544 template <typename LayerType>
545 void ComputeVisibleRectsUsingPropertyTreesInternal(
546 LayerType* root_layer,
547 PropertyTrees* property_trees,
548 typename LayerType::LayerListType* update_layer_list) {
549 if (property_trees->transform_tree.needs_update())
550 property_trees->clip_tree.set_needs_update(true);
551 ComputeTransforms(&property_trees->transform_tree);
552 ComputeClips(&property_trees->clip_tree, property_trees->transform_tree);
553 ComputeOpacities(&property_trees->effect_tree);
555 const bool subtree_is_visible_from_ancestor = true;
556 std::vector<LayerType*> visible_layer_list;
557 FindLayersThatNeedUpdates(root_layer, property_trees->transform_tree,
558 subtree_is_visible_from_ancestor, update_layer_list,
559 &visible_layer_list);
560 CalculateVisibleRects<LayerType>(visible_layer_list,
561 property_trees->clip_tree,
562 property_trees->transform_tree);
565 void BuildPropertyTreesAndComputeVisibleRects(
566 Layer* root_layer,
567 const Layer* page_scale_layer,
568 const Layer* inner_viewport_scroll_layer,
569 const Layer* outer_viewport_scroll_layer,
570 float page_scale_factor,
571 float device_scale_factor,
572 const gfx::Rect& viewport,
573 const gfx::Transform& device_transform,
574 PropertyTrees* property_trees,
575 LayerList* update_layer_list) {
576 PropertyTreeBuilder::BuildPropertyTrees(
577 root_layer, page_scale_layer, inner_viewport_scroll_layer,
578 outer_viewport_scroll_layer, page_scale_factor, device_scale_factor,
579 viewport, device_transform, property_trees);
580 ComputeVisibleRectsUsingPropertyTrees(root_layer, property_trees,
581 update_layer_list);
584 void BuildPropertyTreesAndComputeVisibleRects(
585 LayerImpl* root_layer,
586 const LayerImpl* page_scale_layer,
587 const LayerImpl* inner_viewport_scroll_layer,
588 const LayerImpl* outer_viewport_scroll_layer,
589 float page_scale_factor,
590 float device_scale_factor,
591 const gfx::Rect& viewport,
592 const gfx::Transform& device_transform,
593 PropertyTrees* property_trees,
594 LayerImplList* update_layer_list) {
595 PropertyTreeBuilder::BuildPropertyTrees(
596 root_layer, page_scale_layer, inner_viewport_scroll_layer,
597 outer_viewport_scroll_layer, page_scale_factor, device_scale_factor,
598 viewport, device_transform, property_trees);
599 ComputeVisibleRectsUsingPropertyTrees(root_layer, property_trees,
600 update_layer_list);
603 void ComputeVisibleRectsUsingPropertyTrees(Layer* root_layer,
604 PropertyTrees* property_trees,
605 LayerList* update_layer_list) {
606 ComputeVisibleRectsUsingPropertyTreesInternal(root_layer, property_trees,
607 update_layer_list);
610 void ComputeVisibleRectsUsingPropertyTrees(LayerImpl* root_layer,
611 PropertyTrees* property_trees,
612 LayerImplList* update_layer_list) {
613 ComputeVisibleRectsUsingPropertyTreesInternal(root_layer, property_trees,
614 update_layer_list);
617 template <typename LayerType>
618 gfx::Transform DrawTransformFromPropertyTreesInternal(
619 const LayerType* layer,
620 const TransformTree& tree) {
621 const TransformNode* node = tree.Node(layer->transform_tree_index());
623 gfx::Transform xform;
624 const bool owns_non_root_surface = layer->parent() && layer->render_surface();
625 if (!owns_non_root_surface) {
626 // If you're not the root, or you don't own a surface, you need to apply
627 // your local offset.
628 xform = node->data.to_target;
629 if (layer->should_flatten_transform_from_property_tree())
630 xform.FlattenTo2d();
631 xform.Translate(layer->offset_to_transform_parent().x(),
632 layer->offset_to_transform_parent().y());
633 } else {
634 // Surfaces need to apply their sublayer scale.
635 xform.Scale(node->data.sublayer_scale.x(), node->data.sublayer_scale.y());
637 return xform;
640 gfx::Transform DrawTransformFromPropertyTrees(const Layer* layer,
641 const TransformTree& tree) {
642 return DrawTransformFromPropertyTreesInternal(layer, tree);
645 gfx::Transform DrawTransformFromPropertyTrees(const LayerImpl* layer,
646 const TransformTree& tree) {
647 return DrawTransformFromPropertyTreesInternal(layer, tree);
650 gfx::Transform DrawTransformOfRenderSurfaceFromPropertyTrees(
651 const RenderSurfaceImpl* render_surface,
652 const TransformTree& tree) {
653 const TransformNode* node = tree.Node(render_surface->TransformTreeIndex());
654 gfx::Transform render_surface_transform;
655 // The draw transform of root render surface is identity tranform.
656 if (node->id == 1)
657 return render_surface_transform;
658 const TransformNode* target_node = tree.Node(node->data.target_id);
659 if (target_node->id == 1)
660 target_node = tree.Node(0);
661 tree.ComputeTransformWithDestinationSublayerScale(node->id, target_node->id,
662 &render_surface_transform);
663 if (node->data.sublayer_scale.x() != 0.0 &&
664 node->data.sublayer_scale.y() != 0.0)
665 render_surface_transform.Scale(1.0 / node->data.sublayer_scale.x(),
666 1.0 / node->data.sublayer_scale.y());
667 return render_surface_transform;
670 bool RenderSurfaceIsClippedFromPropertyTrees(
671 const RenderSurfaceImpl* render_surface,
672 const ClipTree& tree) {
673 const ClipNode* node = tree.Node(render_surface->ClipTreeIndex());
674 // If the render surface's owning layer doesn't form a clip node, it is not
675 // clipped.
676 if (render_surface->OwningLayerId() != node->owner_id)
677 return false;
678 return node->data.render_surface_is_clipped;
681 gfx::Rect ClipRectOfRenderSurfaceFromPropertyTrees(
682 const RenderSurfaceImpl* render_surface,
683 const ClipTree& clip_tree) {
684 if (!RenderSurfaceIsClippedFromPropertyTrees(render_surface, clip_tree))
685 return gfx::Rect();
686 const ClipNode* clip_node = clip_tree.Node(render_surface->ClipTreeIndex());
687 const ClipNode* parent_clip_node = clip_tree.parent(clip_node);
688 return gfx::ToEnclosingRect(parent_clip_node->data.clip_in_target_space);
691 gfx::Transform ScreenSpaceTransformOfRenderSurfaceFromPropertyTrees(
692 RenderSurfaceImpl* render_surface,
693 const TransformTree& tree) {
694 const TransformNode* node = tree.Node(render_surface->TransformTreeIndex());
695 gfx::Transform screen_space_transform;
696 // The screen space transform of root render surface is identity tranform.
697 if (node->id == 1)
698 return screen_space_transform;
699 screen_space_transform = node->data.to_screen;
700 if (node->data.sublayer_scale.x() != 0.0 &&
701 node->data.sublayer_scale.y() != 0.0)
702 screen_space_transform.Scale(1.0 / node->data.sublayer_scale.x(),
703 1.0 / node->data.sublayer_scale.y());
704 return screen_space_transform;
707 template <typename LayerType>
708 gfx::Transform ScreenSpaceTransformFromPropertyTreesInternal(
709 LayerType* layer,
710 const TransformTree& tree) {
711 gfx::Transform xform(1, 0, 0, 1, layer->offset_to_transform_parent().x(),
712 layer->offset_to_transform_parent().y());
713 if (layer->transform_tree_index() >= 0) {
714 gfx::Transform ssxform =
715 tree.Node(layer->transform_tree_index())->data.to_screen;
716 xform.ConcatTransform(ssxform);
717 if (layer->should_flatten_transform_from_property_tree())
718 xform.FlattenTo2d();
720 return xform;
723 gfx::Transform ScreenSpaceTransformFromPropertyTrees(
724 const Layer* layer,
725 const TransformTree& tree) {
726 return ScreenSpaceTransformFromPropertyTreesInternal(layer, tree);
729 gfx::Transform ScreenSpaceTransformFromPropertyTrees(
730 const LayerImpl* layer,
731 const TransformTree& tree) {
732 return ScreenSpaceTransformFromPropertyTreesInternal(layer, tree);
735 template <typename LayerType>
736 bool ScreenSpaceTransformIsAnimatingFromPropertyTreesInternal(
737 LayerType* layer,
738 const TransformTree& tree) {
739 return tree.Node(layer->transform_tree_index())->data.to_screen_is_animated;
742 bool ScreenSpaceTransformIsAnimatingFromPropertyTrees(
743 const Layer* layer,
744 const TransformTree& tree) {
745 return ScreenSpaceTransformIsAnimatingFromPropertyTreesInternal(layer, tree);
748 bool ScreenSpaceTransformIsAnimatingFromPropertyTrees(
749 const LayerImpl* layer,
750 const TransformTree& tree) {
751 return ScreenSpaceTransformIsAnimatingFromPropertyTreesInternal(layer, tree);
754 template <typename LayerType>
755 float DrawOpacityFromPropertyTreesInternal(LayerType layer,
756 const EffectTree& tree) {
757 if (!layer->render_target())
758 return 0.f;
760 const EffectNode* target_node =
761 tree.Node(layer->render_target()->effect_tree_index());
762 const EffectNode* node = tree.Node(layer->effect_tree_index());
763 if (node == target_node)
764 return 1.f;
766 float draw_opacity = 1.f;
767 while (node != target_node) {
768 draw_opacity *= node->data.opacity;
769 node = tree.parent(node);
771 return draw_opacity;
774 float DrawOpacityFromPropertyTrees(const Layer* layer, const EffectTree& tree) {
775 return DrawOpacityFromPropertyTreesInternal(layer, tree);
778 float DrawOpacityFromPropertyTrees(const LayerImpl* layer,
779 const EffectTree& tree) {
780 return DrawOpacityFromPropertyTreesInternal(layer, tree);
783 float DrawOpacityOfRenderSurfaceFromPropertyTrees(
784 RenderSurfaceImpl* render_surface,
785 const EffectTree& tree) {
786 const EffectNode* node = tree.Node(render_surface->EffectTreeIndex());
787 float target_opacity_tree_index = render_surface->TargetEffectTreeIndex();
788 if (target_opacity_tree_index < 0)
789 return node->data.screen_space_opacity;
790 const EffectNode* target_node = tree.Node(target_opacity_tree_index);
791 float draw_opacity = 1.f;
792 while (node != target_node) {
793 draw_opacity *= node->data.opacity;
794 node = tree.parent(node);
796 return draw_opacity;
799 bool CanUseLcdTextFromPropertyTrees(const LayerImpl* layer,
800 bool layers_always_allowed_lcd_text,
801 bool can_use_lcd_text,
802 PropertyTrees* property_trees) {
803 if (layers_always_allowed_lcd_text)
804 return true;
805 if (!can_use_lcd_text)
806 return false;
807 if (!layer->contents_opaque())
808 return false;
809 DCHECK(!property_trees->transform_tree.needs_update());
810 DCHECK(!property_trees->effect_tree.needs_update());
812 const EffectNode* opacity_node =
813 property_trees->effect_tree.Node(layer->effect_tree_index());
814 if (opacity_node->data.screen_space_opacity != 1.f)
815 return false;
816 const TransformNode* transform_node =
817 property_trees->transform_tree.Node(layer->transform_tree_index());
818 if (!transform_node->data.node_and_ancestors_have_only_integer_translation)
819 return false;
820 if (static_cast<int>(layer->offset_to_transform_parent().x()) !=
821 layer->offset_to_transform_parent().x())
822 return false;
823 if (static_cast<int>(layer->offset_to_transform_parent().y()) !=
824 layer->offset_to_transform_parent().y())
825 return false;
826 return true;
829 gfx::Rect DrawableContentRectOfSurfaceFromPropertyTrees(
830 const RenderSurfaceImpl* render_surface,
831 const TransformTree& transform_tree) {
832 gfx::Rect drawable_content_rect =
833 gfx::ToEnclosingRect(MathUtil::MapClippedRect(
834 DrawTransformOfRenderSurfaceFromPropertyTrees(render_surface,
835 transform_tree),
836 render_surface->content_rect_from_property_trees()));
837 if (render_surface->HasReplica()) {
838 drawable_content_rect.Union(gfx::ToEnclosingRect(MathUtil::MapClippedRect(
839 render_surface->ReplicaDrawTransform(),
840 render_surface->content_rect_from_property_trees())));
842 return drawable_content_rect;
845 gfx::Rect DrawableContentRectFromPropertyTrees(
846 const LayerImpl* layer,
847 const TransformTree& transform_tree) {
848 gfx::Rect drawable_content_rect = MathUtil::MapEnclosingClippedRect(
849 DrawTransformFromPropertyTrees(layer, transform_tree),
850 gfx::Rect(layer->bounds()));
851 if (layer->is_clipped() && layer->clip_tree_index() > 0) {
852 drawable_content_rect.Intersect(
853 layer->clip_rect_in_target_space_from_property_trees());
855 return drawable_content_rect;
858 gfx::Rect ClipRectFromPropertyTrees(const LayerImpl* layer,
859 const TransformTree& transform_tree) {
860 if (layer->is_clipped() && layer->clip_tree_index() > 0)
861 return layer->clip_rect_in_target_space_from_property_trees();
862 return MathUtil::MapEnclosingClippedRect(
863 DrawTransformFromPropertyTrees(layer, transform_tree),
864 gfx::Rect(layer->bounds()));
867 gfx::Rect ViewportRectFromPropertyTrees(const ClipTree& tree) {
868 return gfx::ToEnclosingRect(tree.Node(1)->data.clip);
871 } // namespace cc