Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(452)

Side by Side Diff: cc/trees/draw_property_utils.cc

Issue 2067783002: cc: Calculate visible rect inside non root copy request (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@rectF
Patch Set: edit comment to be more explicit Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | cc/trees/layer_tree_host_common_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "cc/trees/draw_property_utils.h" 5 #include "cc/trees/draw_property_utils.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <vector> 9 #include <vector>
10 10
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
85 target_node_id, &clip_to_target)) { 85 target_node_id, &clip_to_target)) {
86 *clip_rect_in_target_space = MathUtil::ProjectClippedRect( 86 *clip_rect_in_target_space = MathUtil::ProjectClippedRect(
87 clip_to_target, clip_node->data.clip_in_target_space); 87 clip_to_target, clip_node->data.clip_in_target_space);
88 } else { 88 } else {
89 return false; 89 return false;
90 } 90 }
91 } 91 }
92 return true; 92 return true;
93 } 93 }
94 94
95 struct ConditionalClip {
96 bool is_clipped;
97 gfx::RectF clip_rect;
98 };
99
100 static ConditionalClip ComputeRectInTargetSpace(
101 gfx::RectF rect,
102 const TransformTree& transform_tree,
103 int current_transform_id,
104 int target_transform_id) {
105 gfx::Transform current_to_target;
106 if (!transform_tree.ComputeTransformWithDestinationSublayerScale(
107 current_transform_id, target_transform_id, &current_to_target))
108 // If transform is not invertible, cannot apply clip.
109 return ConditionalClip{false, gfx::RectF()};
110 if (current_transform_id > target_transform_id)
111 return ConditionalClip{true, // is_clipped.
112 MathUtil::MapClippedRect(current_to_target, rect)};
113
114 return ConditionalClip{true, // is_clipped.
115 MathUtil::ProjectClippedRect(current_to_target, rect)};
116 }
117
118 static ConditionalClip ComputeCurrentClip(const ClipNode* clip_node,
119 const TransformTree& transform_tree,
120 int target_transform_id) {
121 if (clip_node->data.transform_id != target_transform_id)
122 return ComputeRectInTargetSpace(clip_node->data.clip, transform_tree,
123 clip_node->data.transform_id,
124 target_transform_id);
125
126 gfx::RectF current_clip = clip_node->data.clip;
127 gfx::Vector2dF sublayer_scale =
128 transform_tree.Node(target_transform_id)->data.sublayer_scale;
129 if (sublayer_scale.x() > 0 && sublayer_scale.y() > 0)
130 current_clip.Scale(sublayer_scale.x(), sublayer_scale.y());
131 return ConditionalClip{true /* is_clipped */, current_clip};
132 }
133
134 static ConditionalClip ComputeAccumulatedClip(
135 const ClipTree& clip_tree,
136 int local_clip_id,
137 const EffectTree& effect_tree,
138 int target_id,
139 const TransformTree& transform_tree) {
140 const ClipNode* clip_node = clip_tree.Node(local_clip_id);
141 const EffectNode* target_node = effect_tree.Node(target_id);
142 int target_transform_id = target_node->data.transform_id;
143 bool is_clipped = false;
144
145 // Collect all the clips that need to be accumulated.
146 std::stack<const ClipNode*> parent_chain;
147
148 // If target is not direct ancestor of clip, this will find least common
149 // ancestor between the target and the clip.
150 while (target_node->id >= 0 && clip_node->id >= 0) {
151 while (target_node->data.clip_id > clip_node->id ||
152 target_node->data.has_unclipped_descendants) {
153 target_node = effect_tree.Node(target_node->data.target_id);
154 }
155 if (target_node->data.clip_id == clip_node->id)
156 break;
157 while (target_node->data.clip_id < clip_node->id) {
158 parent_chain.push(clip_node);
159 clip_node = clip_tree.parent(clip_node);
160 }
161 if (target_node->data.clip_id == clip_node->id) {
162 // Target is responsible for applying this clip_node (id equals to
163 // target_node's clip id), no need to accumulate this as part of clip
164 // rect.
165 clip_node = parent_chain.top();
166 parent_chain.pop();
167 break;
168 }
169 }
170
171 // TODO(weiliangc): If we don't create clip for render surface, we don't need
172 // to check applies_local_clip.
173 while (!clip_node->data.applies_local_clip && parent_chain.size() > 0) {
174 clip_node = parent_chain.top();
175 parent_chain.pop();
176 }
177
178 if (!clip_node->data.applies_local_clip)
179 // No clip node applying clip in between.
180 return ConditionalClip{false, gfx::RectF()};
181
182 ConditionalClip current_clip =
183 ComputeCurrentClip(clip_node, transform_tree, target_transform_id);
184 is_clipped = current_clip.is_clipped;
185 gfx::RectF accumulated_clip = current_clip.clip_rect;
186
187 while (parent_chain.size() > 0) {
188 clip_node = parent_chain.top();
189 parent_chain.pop();
190 if (!clip_node->data.applies_local_clip) {
191 continue;
192 }
193 ConditionalClip current_clip =
194 ComputeCurrentClip(clip_node, transform_tree, target_transform_id);
195
196 // If transform is not invertible, no clip will be applied.
197 if (!current_clip.is_clipped)
198 return ConditionalClip{false, gfx::RectF()};
199
200 is_clipped = true;
201 accumulated_clip =
202 gfx::IntersectRects(accumulated_clip, current_clip.clip_rect);
203 }
204
205 return ConditionalClip{
206 is_clipped, accumulated_clip.IsEmpty() ? gfx::RectF() : accumulated_clip};
207 }
208
95 template <typename LayerType> 209 template <typename LayerType>
96 void CalculateClipRects( 210 void CalculateClipRects(
97 const typename LayerType::LayerListType& visible_layer_list, 211 const typename LayerType::LayerListType& visible_layer_list,
98 const ClipTree& clip_tree, 212 const ClipTree& clip_tree,
99 const TransformTree& transform_tree, 213 const TransformTree& transform_tree,
100 const EffectTree& effect_tree, 214 const EffectTree& effect_tree,
101 bool non_root_surfaces_enabled) { 215 bool non_root_surfaces_enabled) {
102 for (auto& layer : visible_layer_list) { 216 for (auto& layer : visible_layer_list) {
103 const ClipNode* clip_node = clip_tree.Node(layer->clip_tree_index()); 217 const ClipNode* clip_node = clip_tree.Node(layer->clip_tree_index());
104 // The entire layer is visible if it has copy requests. 218 // The entire layer is visible if it has copy requests.
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
177 template <typename LayerType> 291 template <typename LayerType>
178 void CalculateVisibleRects( 292 void CalculateVisibleRects(
179 const typename LayerType::LayerListType& visible_layer_list, 293 const typename LayerType::LayerListType& visible_layer_list,
180 const ClipTree& clip_tree, 294 const ClipTree& clip_tree,
181 const TransformTree& transform_tree, 295 const TransformTree& transform_tree,
182 const EffectTree& effect_tree, 296 const EffectTree& effect_tree,
183 bool non_root_surfaces_enabled) { 297 bool non_root_surfaces_enabled) {
184 for (auto& layer : visible_layer_list) { 298 for (auto& layer : visible_layer_list) {
185 gfx::Size layer_bounds = layer->bounds(); 299 gfx::Size layer_bounds = layer->bounds();
186 300
187 const EffectNode* effect_node = 301 int effect_ancestor_with_copy_request =
188 effect_tree.Node(layer->effect_tree_index()); 302 effect_tree.ClosestAncestorWithCopyRequest(layer->effect_tree_index());
189 if (effect_node->data.has_copy_request && 303 if (effect_ancestor_with_copy_request > 1) {
190 effect_node->owner_id == layer->id()) { 304 // Non root copy request.
191 layer->set_visible_layer_rect(gfx::Rect(layer_bounds)); 305 ConditionalClip accumulated_clip_rect = ComputeAccumulatedClip(
306 clip_tree, layer->clip_tree_index(), effect_tree,
307 effect_ancestor_with_copy_request, transform_tree);
308 if (!accumulated_clip_rect.is_clipped) {
309 layer->set_visible_layer_rect(gfx::Rect(layer_bounds));
310 continue;
311 }
312
313 gfx::RectF accumulated_clip_in_copy_request_space =
314 accumulated_clip_rect.clip_rect;
315
316 const EffectNode* copy_request_effect_node =
317 effect_tree.Node(effect_ancestor_with_copy_request);
318 ConditionalClip clip_in_layer_space = ComputeRectInTargetSpace(
319 accumulated_clip_in_copy_request_space, transform_tree,
320 copy_request_effect_node->data.transform_id,
321 layer->transform_tree_index());
322 if (clip_in_layer_space.is_clipped) {
323 gfx::RectF clip_rect = clip_in_layer_space.clip_rect;
324 clip_rect.Offset(-layer->offset_to_transform_parent());
325 gfx::Rect visible_rect = gfx::ToEnclosingRect(clip_rect);
326 visible_rect.Intersect(gfx::Rect(layer_bounds));
327 layer->set_visible_layer_rect(visible_rect);
328 } else {
329 layer->set_visible_layer_rect(gfx::Rect(layer_bounds));
330 }
192 continue; 331 continue;
193 } 332 }
194 333
195 const ClipNode* clip_node = clip_tree.Node(layer->clip_tree_index()); 334 const ClipNode* clip_node = clip_tree.Node(layer->clip_tree_index());
196 const TransformNode* transform_node = 335 const TransformNode* transform_node =
197 transform_tree.Node(layer->transform_tree_index()); 336 transform_tree.Node(layer->transform_tree_index());
198 if (!non_root_surfaces_enabled) { 337 if (!non_root_surfaces_enabled) {
199 // When we only have a root surface, the clip node and the layer must 338 // When we only have a root surface, the clip node and the layer must
200 // necessarily have the same target (the root). 339 // necessarily have the same target (the root).
201 if (transform_node->data.ancestors_are_invertible) { 340 if (transform_node->data.ancestors_are_invertible) {
(...skipping 469 matching lines...) Expand 10 before | Expand all | Expand 10 after
671 } 810 }
672 811
673 void ComputeEffects(EffectTree* effect_tree) { 812 void ComputeEffects(EffectTree* effect_tree) {
674 if (!effect_tree->needs_update()) 813 if (!effect_tree->needs_update())
675 return; 814 return;
676 for (int i = 1; i < static_cast<int>(effect_tree->size()); ++i) 815 for (int i = 1; i < static_cast<int>(effect_tree->size()); ++i)
677 effect_tree->UpdateEffects(i); 816 effect_tree->UpdateEffects(i);
678 effect_tree->set_needs_update(false); 817 effect_tree->set_needs_update(false);
679 } 818 }
680 819
681 static gfx::RectF ComputeCurrentClip(const ClipNode* clip_node,
682 const TransformTree& transform_tree,
683 int target_transform_id) {
684 if (clip_node->data.transform_id != target_transform_id) {
685 gfx::Transform current_to_target;
686 if (!transform_tree.ComputeTransformWithDestinationSublayerScale(
687 clip_node->data.transform_id, target_transform_id,
688 &current_to_target))
689 return gfx::RectF();
690 if (clip_node->data.transform_id > target_transform_id)
691 return MathUtil::MapClippedRect(current_to_target, clip_node->data.clip);
692 else
693 return MathUtil::ProjectClippedRect(current_to_target,
694 clip_node->data.clip);
695 } else {
696 gfx::RectF current_clip = clip_node->data.clip;
697 gfx::Vector2dF sublayer_scale =
698 transform_tree.Node(target_transform_id)->data.sublayer_scale;
699 if (sublayer_scale.x() > 0 && sublayer_scale.y() > 0) {
700 current_clip.Scale(sublayer_scale.x(), sublayer_scale.y());
701 }
702 return current_clip;
703 }
704 }
705
706 static gfx::RectF ComputeAccumulatedClip(const ClipTree& clip_tree,
707 int local_clip_id,
708 const EffectTree& effect_tree,
709 int target_id,
710 const TransformTree& transform_tree) {
711 const ClipNode* clip_node = clip_tree.Node(local_clip_id);
712 const EffectNode* target_node = effect_tree.Node(target_id);
713 int target_transform_id = target_node->data.transform_id;
714
715 // Collect all the clips that need to be accumulated.
716 std::stack<const ClipNode*> parent_chain;
717
718 // If target is not direct ancestor of clip, this will find least common
719 // ancestor between the target and the clip.
720 while (target_node->id >= 0 && clip_node->id >= 0) {
721 while (target_node->data.clip_id > clip_node->id ||
722 target_node->data.has_unclipped_descendants) {
723 target_node = effect_tree.Node(target_node->data.target_id);
724 }
725 if (target_node->data.clip_id == clip_node->id)
726 break;
727 while (target_node->data.clip_id < clip_node->id) {
728 parent_chain.push(clip_node);
729 clip_node = clip_tree.parent(clip_node);
730 }
731 if (target_node->data.clip_id == clip_node->id) {
732 clip_node = parent_chain.top();
733 parent_chain.pop();
734 break;
735 }
736 }
737
738 // TODO(weiliangc): If we don't create clip for render surface, we don't need
739 // to check applies_local_clip.
740 while (!clip_node->data.applies_local_clip && parent_chain.size() > 0) {
741 clip_node = parent_chain.top();
742 parent_chain.pop();
743 }
744
745 if (!clip_node->data.applies_local_clip)
746 return gfx::RectF();
747
748 gfx::RectF accumulated_clip =
749 ComputeCurrentClip(clip_node, transform_tree, target_transform_id);
750
751 while (parent_chain.size() > 0) {
752 clip_node = parent_chain.top();
753 parent_chain.pop();
754 if (!clip_node->data.applies_local_clip) {
755 continue;
756 }
757 gfx::RectF current_clip =
758 ComputeCurrentClip(clip_node, transform_tree, target_transform_id);
759
760 if (current_clip.IsEmpty())
761 return gfx::RectF();
762
763 accumulated_clip = gfx::IntersectRects(accumulated_clip, current_clip);
764 }
765
766 return accumulated_clip.IsEmpty() ? gfx::RectF() : accumulated_clip;
767 }
768
769 static void ComputeClipsWithEffectTree(PropertyTrees* property_trees) { 820 static void ComputeClipsWithEffectTree(PropertyTrees* property_trees) {
770 EffectTree* effect_tree = &property_trees->effect_tree; 821 EffectTree* effect_tree = &property_trees->effect_tree;
771 const ClipTree* clip_tree = &property_trees->clip_tree; 822 const ClipTree* clip_tree = &property_trees->clip_tree;
772 const TransformTree* transform_tree = &property_trees->transform_tree; 823 const TransformTree* transform_tree = &property_trees->transform_tree;
773 EffectNode* root_effect_node = effect_tree->Node(1); 824 EffectNode* root_effect_node = effect_tree->Node(1);
774 const RenderSurfaceImpl* root_render_surface = 825 const RenderSurfaceImpl* root_render_surface =
775 root_effect_node->data.render_surface; 826 root_effect_node->data.render_surface;
776 gfx::Rect root_clip = gfx::ToEnclosingRect( 827 gfx::Rect root_clip = gfx::ToEnclosingRect(
777 clip_tree->Node(root_effect_node->data.clip_id)->data.clip); 828 clip_tree->Node(root_effect_node->data.clip_id)->data.clip);
778 if (root_render_surface->is_clipped()) 829 if (root_render_surface->is_clipped())
779 DCHECK(root_clip == root_render_surface->clip_rect()) 830 DCHECK(root_clip == root_render_surface->clip_rect())
780 << "clip on root render surface: " 831 << "clip on root render surface: "
781 << root_render_surface->clip_rect().ToString() 832 << root_render_surface->clip_rect().ToString()
782 << " v.s. root effect node's clip: " << root_clip.ToString(); 833 << " v.s. root effect node's clip: " << root_clip.ToString();
783 for (int i = 2; i < static_cast<int>(effect_tree->size()); ++i) { 834 for (int i = 2; i < static_cast<int>(effect_tree->size()); ++i) {
784 EffectNode* effect_node = effect_tree->Node(i); 835 EffectNode* effect_node = effect_tree->Node(i);
785 const EffectNode* target_node = 836 const EffectNode* target_node =
786 effect_tree->Node(effect_node->data.target_id); 837 effect_tree->Node(effect_node->data.target_id);
787 gfx::RectF accumulated_clip = 838 ConditionalClip accumulated_clip_rect =
788 ComputeAccumulatedClip(*clip_tree, effect_node->data.clip_id, 839 ComputeAccumulatedClip(*clip_tree, effect_node->data.clip_id,
789 *effect_tree, target_node->id, *transform_tree); 840 *effect_tree, target_node->id, *transform_tree);
841 gfx::RectF accumulated_clip = accumulated_clip_rect.clip_rect;
790 const RenderSurfaceImpl* render_surface = effect_node->data.render_surface; 842 const RenderSurfaceImpl* render_surface = effect_node->data.render_surface;
791 if (render_surface && render_surface->is_clipped()) { 843 if (render_surface && render_surface->is_clipped()) {
792 DCHECK(gfx::ToEnclosingRect(accumulated_clip) == 844 DCHECK(gfx::ToEnclosingRect(accumulated_clip) ==
793 render_surface->clip_rect()) 845 render_surface->clip_rect())
794 << " render surface's clip rect: " 846 << " render surface's clip rect: "
795 << render_surface->clip_rect().ToString() 847 << render_surface->clip_rect().ToString()
796 << " v.s. accumulated clip: " 848 << " v.s. accumulated clip: "
797 << gfx::ToEnclosingRect(accumulated_clip).ToString(); 849 << gfx::ToEnclosingRect(accumulated_clip).ToString();
798 } 850 }
799 } 851 }
800 } 852 }
801 853
802 static void ComputeLayerClipRect(const PropertyTrees* property_trees, 854 static void ComputeLayerClipRect(const PropertyTrees* property_trees,
803 const LayerImpl* layer) { 855 const LayerImpl* layer) {
804 const EffectTree* effect_tree = &property_trees->effect_tree; 856 const EffectTree* effect_tree = &property_trees->effect_tree;
805 const ClipTree* clip_tree = &property_trees->clip_tree; 857 const ClipTree* clip_tree = &property_trees->clip_tree;
806 const TransformTree* transform_tree = &property_trees->transform_tree; 858 const TransformTree* transform_tree = &property_trees->transform_tree;
807 const EffectNode* effect_node = effect_tree->Node(layer->effect_tree_index()); 859 const EffectNode* effect_node = effect_tree->Node(layer->effect_tree_index());
808 const EffectNode* target_node = 860 const EffectNode* target_node =
809 effect_node->data.has_render_surface 861 effect_node->data.has_render_surface
810 ? effect_node 862 ? effect_node
811 : effect_tree->Node(effect_node->data.target_id); 863 : effect_tree->Node(effect_node->data.target_id);
812 // TODO(weiliangc): When effect node has up to date render surface info on 864 // TODO(weiliangc): When effect node has up to date render surface info on
813 // compositor thread, no need to check for resourceless draw mode 865 // compositor thread, no need to check for resourceless draw mode
814 if (!property_trees->non_root_surfaces_enabled) { 866 if (!property_trees->non_root_surfaces_enabled) {
815 target_node = effect_tree->Node(1); 867 target_node = effect_tree->Node(1);
816 } 868 }
817 869
818 gfx::RectF accumulated_clip = 870 ConditionalClip accumulated_clip_rect =
819 ComputeAccumulatedClip(*clip_tree, layer->clip_tree_index(), *effect_tree, 871 ComputeAccumulatedClip(*clip_tree, layer->clip_tree_index(), *effect_tree,
820 target_node->id, *transform_tree); 872 target_node->id, *transform_tree);
821 873
874 gfx::RectF accumulated_clip = accumulated_clip_rect.clip_rect;
875
822 if ((!property_trees->non_root_surfaces_enabled && 876 if ((!property_trees->non_root_surfaces_enabled &&
823 clip_tree->Node(layer->clip_tree_index()) 877 clip_tree->Node(layer->clip_tree_index())
824 ->data.layers_are_clipped_when_surfaces_disabled) || 878 ->data.layers_are_clipped_when_surfaces_disabled) ||
825 clip_tree->Node(layer->clip_tree_index())->data.layers_are_clipped) { 879 clip_tree->Node(layer->clip_tree_index())->data.layers_are_clipped) {
826 DCHECK(layer->clip_rect() == gfx::ToEnclosingRect(accumulated_clip)) 880 DCHECK(layer->clip_rect() == gfx::ToEnclosingRect(accumulated_clip))
827 << " layer: " << layer->id() << " clip id: " << layer->clip_tree_index() 881 << " layer: " << layer->id() << " clip id: " << layer->clip_tree_index()
828 << " layer clip: " << layer->clip_rect().ToString() << " v.s. " 882 << " layer clip: " << layer->clip_rect().ToString() << " v.s. "
829 << gfx::ToEnclosingRect(accumulated_clip).ToString() 883 << gfx::ToEnclosingRect(accumulated_clip).ToString()
830 << " and clip node clip: " 884 << " and clip node clip: "
831 << gfx::ToEnclosingRect(clip_tree->Node(layer->clip_tree_index()) 885 << gfx::ToEnclosingRect(clip_tree->Node(layer->clip_tree_index())
(...skipping 486 matching lines...) Expand 10 before | Expand all | Expand 10 after
1318 void UpdateElasticOverscroll(PropertyTrees* property_trees, 1372 void UpdateElasticOverscroll(PropertyTrees* property_trees,
1319 const Layer* overscroll_elasticity_layer, 1373 const Layer* overscroll_elasticity_layer,
1320 const gfx::Vector2dF& elastic_overscroll) { 1374 const gfx::Vector2dF& elastic_overscroll) {
1321 UpdateElasticOverscrollInternal(property_trees, overscroll_elasticity_layer, 1375 UpdateElasticOverscrollInternal(property_trees, overscroll_elasticity_layer,
1322 elastic_overscroll); 1376 elastic_overscroll);
1323 } 1377 }
1324 1378
1325 } // namespace draw_property_utils 1379 } // namespace draw_property_utils
1326 1380
1327 } // namespace cc 1381 } // namespace cc
OLDNEW
« no previous file with comments | « no previous file | cc/trees/layer_tree_host_common_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698