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

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

Issue 2401903002: Compute and include the offset of the sticky box to its enclosing composited layer. (Closed)
Patch Set: Created 4 years, 2 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
OLDNEW
1 // Copyright 2011 The Chromium Authors. All rights reserved. 1 // Copyright 2011 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/layer_tree_host_common.h" 5 #include "cc/trees/layer_tree_host_common.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <algorithm> 9 #include <algorithm>
10 #include <memory> 10 #include <memory>
(...skipping 6775 matching lines...) Expand 10 before | Expand all | Expand 10 after
6786 root->AddChild(container); 6786 root->AddChild(container);
6787 container->AddChild(scroller); 6787 container->AddChild(scroller);
6788 scroller->AddChild(sticky_pos); 6788 scroller->AddChild(sticky_pos);
6789 host()->SetRootLayer(root); 6789 host()->SetRootLayer(root);
6790 scroller->SetScrollClipLayerId(container->id()); 6790 scroller->SetScrollClipLayerId(container->id());
6791 6791
6792 LayerStickyPositionConstraint sticky_position; 6792 LayerStickyPositionConstraint sticky_position;
6793 sticky_position.is_sticky = true; 6793 sticky_position.is_sticky = true;
6794 sticky_position.is_anchored_top = true; 6794 sticky_position.is_anchored_top = true;
6795 sticky_position.top_offset = 10.0f; 6795 sticky_position.top_offset = 10.0f;
6796 sticky_position.parent_relative_sticky_box_offset = gfx::Point(10, 20);
6796 sticky_position.scroll_container_relative_sticky_box_rect = 6797 sticky_position.scroll_container_relative_sticky_box_rect =
6797 gfx::Rect(10, 20, 10, 10); 6798 gfx::Rect(10, 20, 10, 10);
6798 sticky_position.scroll_container_relative_containing_block_rect = 6799 sticky_position.scroll_container_relative_containing_block_rect =
6799 gfx::Rect(0, 0, 50, 50); 6800 gfx::Rect(0, 0, 50, 50);
6800 sticky_pos->SetStickyPositionConstraint(sticky_position); 6801 sticky_pos->SetStickyPositionConstraint(sticky_position);
6801 6802
6802 root->SetBounds(gfx::Size(100, 100)); 6803 root->SetBounds(gfx::Size(100, 100));
6803 container->SetBounds(gfx::Size(100, 100)); 6804 container->SetBounds(gfx::Size(100, 100));
6804 scroller->SetBounds(gfx::Size(1000, 1000)); 6805 scroller->SetBounds(gfx::Size(1000, 1000));
6805 sticky_pos->SetBounds(gfx::Size(10, 10)); 6806 sticky_pos->SetBounds(gfx::Size(10, 10));
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
6857 root->AddChild(container); 6858 root->AddChild(container);
6858 container->AddChild(scroller); 6859 container->AddChild(scroller);
6859 scroller->AddChild(sticky_pos); 6860 scroller->AddChild(sticky_pos);
6860 host()->SetRootLayer(root); 6861 host()->SetRootLayer(root);
6861 scroller->SetScrollClipLayerId(container->id()); 6862 scroller->SetScrollClipLayerId(container->id());
6862 6863
6863 LayerStickyPositionConstraint sticky_position; 6864 LayerStickyPositionConstraint sticky_position;
6864 sticky_position.is_sticky = true; 6865 sticky_position.is_sticky = true;
6865 sticky_position.is_anchored_bottom = true; 6866 sticky_position.is_anchored_bottom = true;
6866 sticky_position.bottom_offset = 10.0f; 6867 sticky_position.bottom_offset = 10.0f;
6868 sticky_position.parent_relative_sticky_box_offset = gfx::Point(0, 150);
6867 sticky_position.scroll_container_relative_sticky_box_rect = 6869 sticky_position.scroll_container_relative_sticky_box_rect =
6868 gfx::Rect(0, 150, 10, 10); 6870 gfx::Rect(0, 150, 10, 10);
6869 sticky_position.scroll_container_relative_containing_block_rect = 6871 sticky_position.scroll_container_relative_containing_block_rect =
6870 gfx::Rect(0, 100, 50, 50); 6872 gfx::Rect(0, 100, 50, 50);
6871 sticky_pos->SetStickyPositionConstraint(sticky_position); 6873 sticky_pos->SetStickyPositionConstraint(sticky_position);
6872 6874
6873 root->SetBounds(gfx::Size(100, 100)); 6875 root->SetBounds(gfx::Size(100, 100));
6874 container->SetBounds(gfx::Size(100, 100)); 6876 container->SetBounds(gfx::Size(100, 100));
6875 scroller->SetBounds(gfx::Size(1000, 1000)); 6877 scroller->SetBounds(gfx::Size(1000, 1000));
6876 sticky_pos->SetBounds(gfx::Size(10, 10)); 6878 sticky_pos->SetBounds(gfx::Size(10, 10));
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
6928 scroller->AddChild(sticky_pos); 6930 scroller->AddChild(sticky_pos);
6929 host()->SetRootLayer(root); 6931 host()->SetRootLayer(root);
6930 scroller->SetScrollClipLayerId(container->id()); 6932 scroller->SetScrollClipLayerId(container->id());
6931 6933
6932 LayerStickyPositionConstraint sticky_position; 6934 LayerStickyPositionConstraint sticky_position;
6933 sticky_position.is_sticky = true; 6935 sticky_position.is_sticky = true;
6934 sticky_position.is_anchored_left = true; 6936 sticky_position.is_anchored_left = true;
6935 sticky_position.is_anchored_right = true; 6937 sticky_position.is_anchored_right = true;
6936 sticky_position.left_offset = 10.f; 6938 sticky_position.left_offset = 10.f;
6937 sticky_position.right_offset = 10.f; 6939 sticky_position.right_offset = 10.f;
6940 sticky_position.parent_relative_sticky_box_offset = gfx::Point(145, 0);
6938 sticky_position.scroll_container_relative_sticky_box_rect = 6941 sticky_position.scroll_container_relative_sticky_box_rect =
6939 gfx::Rect(145, 0, 10, 10); 6942 gfx::Rect(145, 0, 10, 10);
6940 sticky_position.scroll_container_relative_containing_block_rect = 6943 sticky_position.scroll_container_relative_containing_block_rect =
6941 gfx::Rect(100, 0, 100, 100); 6944 gfx::Rect(100, 0, 100, 100);
6942 sticky_pos->SetStickyPositionConstraint(sticky_position); 6945 sticky_pos->SetStickyPositionConstraint(sticky_position);
6943 6946
6944 root->SetBounds(gfx::Size(100, 100)); 6947 root->SetBounds(gfx::Size(100, 100));
6945 container->SetBounds(gfx::Size(100, 100)); 6948 container->SetBounds(gfx::Size(100, 100));
6946 scroller->SetBounds(gfx::Size(1000, 1000)); 6949 scroller->SetBounds(gfx::Size(1000, 1000));
6947 sticky_pos->SetBounds(gfx::Size(10, 10)); 6950 sticky_pos->SetBounds(gfx::Size(10, 10));
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
7030 root->AddChild(container); 7033 root->AddChild(container);
7031 container->AddChild(scroller); 7034 container->AddChild(scroller);
7032 scroller->AddChild(sticky_pos); 7035 scroller->AddChild(sticky_pos);
7033 host()->SetRootLayer(root); 7036 host()->SetRootLayer(root);
7034 scroller->SetScrollClipLayerId(container->id()); 7037 scroller->SetScrollClipLayerId(container->id());
7035 7038
7036 LayerStickyPositionConstraint sticky_position; 7039 LayerStickyPositionConstraint sticky_position;
7037 sticky_position.is_sticky = true; 7040 sticky_position.is_sticky = true;
7038 sticky_position.is_anchored_top = true; 7041 sticky_position.is_anchored_top = true;
7039 sticky_position.top_offset = 10.0f; 7042 sticky_position.top_offset = 10.0f;
7043 sticky_position.parent_relative_sticky_box_offset = gfx::Point(10, 20);
7040 sticky_position.scroll_container_relative_sticky_box_rect = 7044 sticky_position.scroll_container_relative_sticky_box_rect =
7041 gfx::Rect(10, 20, 10, 10); 7045 gfx::Rect(10, 20, 10, 10);
7042 sticky_position.scroll_container_relative_containing_block_rect = 7046 sticky_position.scroll_container_relative_containing_block_rect =
7043 gfx::Rect(0, 0, 50, 50); 7047 gfx::Rect(0, 0, 50, 50);
7044 sticky_pos->SetStickyPositionConstraint(sticky_position); 7048 sticky_pos->SetStickyPositionConstraint(sticky_position);
7045 7049
7046 root->SetBounds(gfx::Size(100, 100)); 7050 root->SetBounds(gfx::Size(100, 100));
7047 container->SetBounds(gfx::Size(100, 100)); 7051 container->SetBounds(gfx::Size(100, 100));
7048 scroller->SetBounds(gfx::Size(1000, 1000)); 7052 scroller->SetBounds(gfx::Size(1000, 1000));
7049 sticky_pos->SetBounds(gfx::Size(10, 10)); 7053 sticky_pos->SetBounds(gfx::Size(10, 10));
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
7101 sticky_pos_impl->ScreenSpaceTransform().To2dTranslation()); 7105 sticky_pos_impl->ScreenSpaceTransform().To2dTranslation());
7102 7106
7103 // And if we scroll a little further it remains there. 7107 // And if we scroll a little further it remains there.
7104 SetScrollOffsetDelta(scroller_impl, gfx::Vector2dF(0.f, 10.f)); 7108 SetScrollOffsetDelta(scroller_impl, gfx::Vector2dF(0.f, 10.f));
7105 ExecuteCalculateDrawProperties(root_impl); 7109 ExecuteCalculateDrawProperties(root_impl);
7106 EXPECT_VECTOR2DF_EQ( 7110 EXPECT_VECTOR2DF_EQ(
7107 gfx::Vector2dF(-5.f, 10.f), 7111 gfx::Vector2dF(-5.f, 10.f),
7108 sticky_pos_impl->ScreenSpaceTransform().To2dTranslation()); 7112 sticky_pos_impl->ScreenSpaceTransform().To2dTranslation());
7109 } 7113 }
7110 7114
7115 // This tests the main thread updates with a composited sticky container. In
7116 // this case the position received from main is relative to the container but
7117 // the constraint rects are relative to the ancestor scroller.
7118 TEST_F(LayerTreeHostCommonTest, StickyPositionCompositedContainer) {
7119 scoped_refptr<Layer> root = Layer::Create();
7120 scoped_refptr<Layer> container = Layer::Create();
7121 scoped_refptr<Layer> scroller = Layer::Create();
7122 scoped_refptr<Layer> sticky_container = Layer::Create();
7123 scoped_refptr<Layer> sticky_pos = Layer::Create();
7124 root->AddChild(container);
7125 container->AddChild(scroller);
7126 scroller->AddChild(sticky_container);
7127 sticky_container->AddChild(sticky_pos);
7128 host()->SetRootLayer(root);
7129 scroller->SetScrollClipLayerId(container->id());
7130
7131 LayerStickyPositionConstraint sticky_position;
7132 sticky_position.is_sticky = true;
7133 sticky_position.is_anchored_top = true;
7134 sticky_position.top_offset = 10.0f;
7135 // The sticky position layer is only offset by (0, 10) from its parent
7136 // layer, this position is used to determine the offset applied by the main
7137 // thread.
7138 sticky_position.parent_relative_sticky_box_offset = gfx::Point(0, 10);
7139 sticky_position.scroll_container_relative_sticky_box_rect =
7140 gfx::Rect(20, 30, 10, 10);
7141 sticky_position.scroll_container_relative_containing_block_rect =
7142 gfx::Rect(20, 20, 30, 30);
7143 sticky_pos->SetStickyPositionConstraint(sticky_position);
7144
7145 root->SetBounds(gfx::Size(100, 100));
7146 container->SetBounds(gfx::Size(100, 100));
7147 scroller->SetBounds(gfx::Size(1000, 1000));
7148 sticky_container->SetPosition(gfx::PointF(20, 20));
7149 sticky_container->SetBounds(gfx::Size(30, 30));
7150 sticky_pos->SetBounds(gfx::Size(10, 10));
7151 sticky_pos->SetPosition(gfx::PointF(0, 10));
7152
7153 ExecuteCalculateDrawProperties(root.get());
7154 host()->host_impl()->CreatePendingTree();
7155 host()->CommitAndCreatePendingTree();
7156 host()->host_impl()->ActivateSyncTree();
7157 LayerTreeImpl* layer_tree_impl = host()->host_impl()->active_tree();
7158
7159 LayerImpl* root_impl = layer_tree_impl->LayerById(root->id());
7160 LayerImpl* scroller_impl = layer_tree_impl->LayerById(scroller->id());
7161 LayerImpl* sticky_pos_impl = layer_tree_impl->LayerById(sticky_pos->id());
7162
7163 ExecuteCalculateDrawProperties(root_impl);
7164 EXPECT_VECTOR2DF_EQ(
7165 gfx::Vector2dF(20.f, 30.f),
7166 sticky_pos_impl->ScreenSpaceTransform().To2dTranslation());
7167
7168 // Scroll less than sticking point, sticky element should move with scroll as
7169 // we haven't gotten to the initial sticky item location yet.
7170 SetScrollOffsetDelta(scroller_impl, gfx::Vector2dF(0.f, 5.f));
7171 ExecuteCalculateDrawProperties(root_impl);
7172 EXPECT_VECTOR2DF_EQ(
7173 gfx::Vector2dF(20.f, 25.f),
7174 sticky_pos_impl->ScreenSpaceTransform().To2dTranslation());
7175
7176 // Scroll past the sticking point, the Y coordinate should now be clamped.
7177 SetScrollOffsetDelta(scroller_impl, gfx::Vector2dF(0.f, 25.f));
7178 ExecuteCalculateDrawProperties(root_impl);
7179 EXPECT_VECTOR2DF_EQ(
7180 gfx::Vector2dF(20.f, 10.f),
7181 sticky_pos_impl->ScreenSpaceTransform().To2dTranslation());
7182
7183 // Now the main thread commits the new position of the sticky element.
7184 scroller->SetScrollOffset(gfx::ScrollOffset(0, 25));
7185 sticky_pos->SetPosition(gfx::PointF(0, 15));
7186 ExecuteCalculateDrawProperties(root.get());
7187 host()->host_impl()->CreatePendingTree();
7188 host()->CommitAndCreatePendingTree();
7189 host()->host_impl()->ActivateSyncTree();
7190 layer_tree_impl = host()->host_impl()->active_tree();
7191 root_impl = layer_tree_impl->LayerById(root->id());
7192 scroller_impl = layer_tree_impl->LayerById(scroller->id());
7193 sticky_pos_impl = layer_tree_impl->LayerById(sticky_pos->id());
7194
7195 // The element should still be where it was before. We reset the delta to
7196 // (0, 0) because we have synced a scroll offset of (15, 15) from the main
ajuma 2016/10/07 21:14:41 (15, 15) should be (0, 25), right?
flackr 2016/10/11 03:53:19 Right, forgot to update the comment.
7197 // thread.
7198 SetScrollOffsetDelta(scroller_impl, gfx::Vector2dF(0.f, 0.f));
7199 ExecuteCalculateDrawProperties(root_impl);
7200 EXPECT_VECTOR2DF_EQ(
7201 gfx::Vector2dF(20.f, 10.f),
7202 sticky_pos_impl->ScreenSpaceTransform().To2dTranslation());
7203
7204 // And if we scroll a little further it remains there.
7205 SetScrollOffsetDelta(scroller_impl, gfx::Vector2dF(0.f, 5.f));
7206 ExecuteCalculateDrawProperties(root_impl);
7207 EXPECT_VECTOR2DF_EQ(
7208 gfx::Vector2dF(20.f, 10.f),
7209 sticky_pos_impl->ScreenSpaceTransform().To2dTranslation());
7210
7211 // And hits the bottom of the container.
7212 SetScrollOffsetDelta(scroller_impl, gfx::Vector2dF(0.f, 10.f));
7213 ExecuteCalculateDrawProperties(root_impl);
7214 EXPECT_VECTOR2DF_EQ(
7215 gfx::Vector2dF(20.f, 5.f),
7216 sticky_pos_impl->ScreenSpaceTransform().To2dTranslation());
7217 }
7218
7111 // A transform on a sticky element should not affect its sticky position. 7219 // A transform on a sticky element should not affect its sticky position.
7112 TEST_F(LayerTreeHostCommonTest, StickyPositionScaledStickyBox) { 7220 TEST_F(LayerTreeHostCommonTest, StickyPositionScaledStickyBox) {
7113 scoped_refptr<Layer> root = Layer::Create(); 7221 scoped_refptr<Layer> root = Layer::Create();
7114 scoped_refptr<Layer> container = Layer::Create(); 7222 scoped_refptr<Layer> container = Layer::Create();
7115 scoped_refptr<Layer> scroller = Layer::Create(); 7223 scoped_refptr<Layer> scroller = Layer::Create();
7116 scoped_refptr<Layer> sticky_pos = Layer::Create(); 7224 scoped_refptr<Layer> sticky_pos = Layer::Create();
7117 root->AddChild(container); 7225 root->AddChild(container);
7118 container->AddChild(scroller); 7226 container->AddChild(scroller);
7119 scroller->AddChild(sticky_pos); 7227 scroller->AddChild(sticky_pos);
7120 host()->SetRootLayer(root); 7228 host()->SetRootLayer(root);
7121 scroller->SetScrollClipLayerId(container->id()); 7229 scroller->SetScrollClipLayerId(container->id());
7122 gfx::Transform t; 7230 gfx::Transform t;
7123 t.Scale(2, 2); 7231 t.Scale(2, 2);
7124 sticky_pos->SetTransform(t); 7232 sticky_pos->SetTransform(t);
7125 7233
7126 LayerStickyPositionConstraint sticky_position; 7234 LayerStickyPositionConstraint sticky_position;
7127 sticky_position.is_sticky = true; 7235 sticky_position.is_sticky = true;
7128 sticky_position.is_anchored_top = true; 7236 sticky_position.is_anchored_top = true;
7129 sticky_position.top_offset = 0.0f; 7237 sticky_position.top_offset = 0.0f;
7238 sticky_position.parent_relative_sticky_box_offset = gfx::Point(0, 20);
7130 sticky_position.scroll_container_relative_sticky_box_rect = 7239 sticky_position.scroll_container_relative_sticky_box_rect =
7131 gfx::Rect(0, 20, 10, 10); 7240 gfx::Rect(0, 20, 10, 10);
7132 sticky_position.scroll_container_relative_containing_block_rect = 7241 sticky_position.scroll_container_relative_containing_block_rect =
7133 gfx::Rect(0, 0, 50, 50); 7242 gfx::Rect(0, 0, 50, 50);
7134 sticky_pos->SetStickyPositionConstraint(sticky_position); 7243 sticky_pos->SetStickyPositionConstraint(sticky_position);
7135 7244
7136 root->SetBounds(gfx::Size(100, 100)); 7245 root->SetBounds(gfx::Size(100, 100));
7137 container->SetBounds(gfx::Size(100, 100)); 7246 container->SetBounds(gfx::Size(100, 100));
7138 scroller->SetBounds(gfx::Size(1000, 1000)); 7247 scroller->SetBounds(gfx::Size(1000, 1000));
7139 sticky_pos->SetBounds(gfx::Size(10, 10)); 7248 sticky_pos->SetBounds(gfx::Size(10, 10));
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
7198 host()->SetRootLayer(root); 7307 host()->SetRootLayer(root);
7199 scroller->SetScrollClipLayerId(container->id()); 7308 scroller->SetScrollClipLayerId(container->id());
7200 gfx::Transform t; 7309 gfx::Transform t;
7201 t.Scale(2, 2); 7310 t.Scale(2, 2);
7202 sticky_container->SetTransform(t); 7311 sticky_container->SetTransform(t);
7203 7312
7204 LayerStickyPositionConstraint sticky_position; 7313 LayerStickyPositionConstraint sticky_position;
7205 sticky_position.is_sticky = true; 7314 sticky_position.is_sticky = true;
7206 sticky_position.is_anchored_top = true; 7315 sticky_position.is_anchored_top = true;
7207 sticky_position.top_offset = 0.0f; 7316 sticky_position.top_offset = 0.0f;
7317 sticky_position.parent_relative_sticky_box_offset = gfx::Point(0, 20);
7208 sticky_position.scroll_container_relative_sticky_box_rect = 7318 sticky_position.scroll_container_relative_sticky_box_rect =
7209 gfx::Rect(0, 20, 10, 10); 7319 gfx::Rect(0, 20, 10, 10);
7210 sticky_position.scroll_container_relative_containing_block_rect = 7320 sticky_position.scroll_container_relative_containing_block_rect =
7211 gfx::Rect(0, 0, 50, 50); 7321 gfx::Rect(0, 0, 50, 50);
7212 sticky_pos->SetStickyPositionConstraint(sticky_position); 7322 sticky_pos->SetStickyPositionConstraint(sticky_position);
7213 7323
7214 root->SetBounds(gfx::Size(100, 100)); 7324 root->SetBounds(gfx::Size(100, 100));
7215 container->SetBounds(gfx::Size(100, 100)); 7325 container->SetBounds(gfx::Size(100, 100));
7216 scroller->SetBounds(gfx::Size(1000, 1000)); 7326 scroller->SetBounds(gfx::Size(1000, 1000));
7217 sticky_container->SetBounds(gfx::Size(50, 50)); 7327 sticky_container->SetBounds(gfx::Size(50, 50));
(...skipping 2911 matching lines...) Expand 10 before | Expand all | Expand 10 after
10129 EXPECT_EQ(scroll_child6.id, grand_child10->scroll_tree_index()); 10239 EXPECT_EQ(scroll_child6.id, grand_child10->scroll_tree_index());
10130 EXPECT_EQ(scroll_root1.id, parent3->scroll_tree_index()); 10240 EXPECT_EQ(scroll_root1.id, parent3->scroll_tree_index());
10131 EXPECT_EQ(scroll_child7.id, child8->scroll_tree_index()); 10241 EXPECT_EQ(scroll_child7.id, child8->scroll_tree_index());
10132 EXPECT_EQ(scroll_root1.id, parent4->scroll_tree_index()); 10242 EXPECT_EQ(scroll_root1.id, parent4->scroll_tree_index());
10133 EXPECT_EQ(scroll_root1.id, child9->scroll_tree_index()); 10243 EXPECT_EQ(scroll_root1.id, child9->scroll_tree_index());
10134 EXPECT_EQ(scroll_root1.id, grand_child12->scroll_tree_index()); 10244 EXPECT_EQ(scroll_root1.id, grand_child12->scroll_tree_index());
10135 } 10245 }
10136 10246
10137 } // namespace 10247 } // namespace
10138 } // namespace cc 10248 } // namespace cc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698