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

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

Issue 1038173002: Refactor delegated scrolling in LayerImpl (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Added a new test for Inner/Outer viewport scrolling Created 5 years, 8 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 | « cc/trees/layer_tree_host_impl.cc ('k') | cc/trees/layer_tree_impl.h » ('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 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_impl.h" 5 #include "cc/trees/layer_tree_host_impl.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <cmath> 8 #include <cmath>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after
217 if (scroll_info.scrolls[i].layer_id != id) 217 if (scroll_info.scrolls[i].layer_id != id)
218 continue; 218 continue;
219 times_encountered++; 219 times_encountered++;
220 } 220 }
221 221
222 ASSERT_EQ(0, times_encountered); 222 ASSERT_EQ(0, times_encountered);
223 } 223 }
224 224
225 LayerImpl* CreateScrollAndContentsLayers(LayerTreeImpl* layer_tree_impl, 225 LayerImpl* CreateScrollAndContentsLayers(LayerTreeImpl* layer_tree_impl,
226 const gfx::Size& content_size) { 226 const gfx::Size& content_size) {
227 // Create both an inner viewport scroll layer and an outer viewport scroll
228 // layer. The MaxScrollOffset of the outer viewport scroll layer will be
229 // 0x0, so the scrolls will be applied directly to the inner viewport.
230 const int kOuterViewportClipLayerId = 116;
231 const int kOuterViewportScrollLayerId = 117;
232 const int kContentLayerId = 118;
227 const int kInnerViewportScrollLayerId = 2; 233 const int kInnerViewportScrollLayerId = 2;
228 const int kInnerViewportClipLayerId = 4; 234 const int kInnerViewportClipLayerId = 4;
229 const int kPageScaleLayerId = 5; 235 const int kPageScaleLayerId = 5;
236
230 scoped_ptr<LayerImpl> root = 237 scoped_ptr<LayerImpl> root =
231 LayerImpl::Create(layer_tree_impl, 1); 238 LayerImpl::Create(layer_tree_impl, 1);
232 root->SetBounds(content_size); 239 root->SetBounds(content_size);
233 root->SetContentBounds(content_size); 240 root->SetContentBounds(content_size);
234 root->SetPosition(gfx::PointF()); 241 root->SetPosition(gfx::PointF());
235 root->SetHasRenderSurface(true); 242 root->SetHasRenderSurface(true);
236 243
237 scoped_ptr<LayerImpl> scroll = 244 scoped_ptr<LayerImpl> inner_scroll =
238 LayerImpl::Create(layer_tree_impl, kInnerViewportScrollLayerId); 245 LayerImpl::Create(layer_tree_impl, kInnerViewportScrollLayerId);
239 LayerImpl* scroll_layer = scroll.get(); 246 inner_scroll->SetIsContainerForFixedPositionLayers(true);
240 scroll->SetIsContainerForFixedPositionLayers(true); 247 inner_scroll->PushScrollOffsetFromMainThread(gfx::ScrollOffset());
241 scroll->PushScrollOffsetFromMainThread(gfx::ScrollOffset());
242 248
243 scoped_ptr<LayerImpl> clip = 249 scoped_ptr<LayerImpl> inner_clip =
244 LayerImpl::Create(layer_tree_impl, kInnerViewportClipLayerId); 250 LayerImpl::Create(layer_tree_impl, kInnerViewportClipLayerId);
245 clip->SetBounds( 251 inner_clip->SetBounds(
246 gfx::Size(content_size.width() / 2, content_size.height() / 2)); 252 gfx::Size(content_size.width() / 2, content_size.height() / 2));
247 253
248 scoped_ptr<LayerImpl> page_scale = 254 scoped_ptr<LayerImpl> page_scale =
249 LayerImpl::Create(layer_tree_impl, kPageScaleLayerId); 255 LayerImpl::Create(layer_tree_impl, kPageScaleLayerId);
250 256
251 scroll->SetScrollClipLayer(clip->id()); 257 inner_scroll->SetScrollClipLayer(inner_clip->id());
252 scroll->SetBounds(content_size); 258 inner_scroll->SetBounds(content_size);
253 scroll->SetContentBounds(content_size); 259 inner_scroll->SetContentBounds(content_size);
254 scroll->SetPosition(gfx::PointF()); 260 inner_scroll->SetPosition(gfx::PointF());
255 scroll->SetIsContainerForFixedPositionLayers(true); 261
262 scoped_ptr<LayerImpl> outer_clip =
263 LayerImpl::Create(layer_tree_impl, kOuterViewportClipLayerId);
264 outer_clip->SetBounds(content_size);
265 outer_clip->SetIsContainerForFixedPositionLayers(true);
266
267 scoped_ptr<LayerImpl> outer_scroll =
268 LayerImpl::Create(layer_tree_impl, kOuterViewportScrollLayerId);
269 outer_scroll->SetScrollClipLayer(outer_clip->id());
270 outer_scroll->PushScrollOffsetFromMainThread(gfx::ScrollOffset());
271 outer_scroll->SetBounds(content_size);
272 outer_scroll->SetContentBounds(content_size);
273 outer_scroll->SetPosition(gfx::PointF());
256 274
257 scoped_ptr<LayerImpl> contents = 275 scoped_ptr<LayerImpl> contents =
258 LayerImpl::Create(layer_tree_impl, 3); 276 LayerImpl::Create(layer_tree_impl, kContentLayerId);
259 contents->SetDrawsContent(true); 277 contents->SetDrawsContent(true);
260 contents->SetBounds(content_size); 278 contents->SetBounds(content_size);
261 contents->SetContentBounds(content_size); 279 contents->SetContentBounds(content_size);
262 contents->SetPosition(gfx::PointF()); 280 contents->SetPosition(gfx::PointF());
263 281
264 scroll->AddChild(contents.Pass()); 282 outer_scroll->AddChild(contents.Pass());
265 page_scale->AddChild(scroll.Pass()); 283 outer_clip->AddChild(outer_scroll.Pass());
266 clip->AddChild(page_scale.Pass()); 284 inner_scroll->AddChild(outer_clip.Pass());
267 root->AddChild(clip.Pass()); 285 page_scale->AddChild(inner_scroll.Pass());
286 inner_clip->AddChild(page_scale.Pass());
287 root->AddChild(inner_clip.Pass());
268 288
269 layer_tree_impl->SetRootLayer(root.Pass()); 289 layer_tree_impl->SetRootLayer(root.Pass());
270 layer_tree_impl->SetViewportLayersFromIds( 290 layer_tree_impl->SetViewportLayersFromIds(
271 Layer::INVALID_ID, kPageScaleLayerId, kInnerViewportScrollLayerId, 291 Layer::INVALID_ID, kPageScaleLayerId, kInnerViewportScrollLayerId,
272 Layer::INVALID_ID); 292 kOuterViewportScrollLayerId);
273 293
274 return scroll_layer; 294 layer_tree_impl->DidBecomeActive();
295 return layer_tree_impl->InnerViewportScrollLayer();
275 } 296 }
276 297
277 LayerImpl* SetupScrollAndContentsLayers(const gfx::Size& content_size) { 298 LayerImpl* SetupScrollAndContentsLayers(const gfx::Size& content_size) {
278 LayerImpl* scroll_layer = CreateScrollAndContentsLayers( 299 LayerImpl* scroll_layer = CreateScrollAndContentsLayers(
279 host_impl_->active_tree(), content_size); 300 host_impl_->active_tree(), content_size);
280 host_impl_->active_tree()->DidBecomeActive(); 301 host_impl_->active_tree()->DidBecomeActive();
281 return scroll_layer; 302 return scroll_layer;
282 } 303 }
283 304
284 // TODO(wjmaclean) Add clip-layer pointer to parameters. 305 // TODO(wjmaclean) Add clip-layer pointer to parameters.
(...skipping 1555 matching lines...) Expand 10 before | Expand all | Expand 10 after
1840 host_impl_->MakeCompositorFrameMetadata(); 1861 host_impl_->MakeCompositorFrameMetadata();
1841 EXPECT_EQ(gfx::Vector2dF(0.f, 10.f), metadata.root_scroll_offset); 1862 EXPECT_EQ(gfx::Vector2dF(0.f, 10.f), metadata.root_scroll_offset);
1842 } 1863 }
1843 host_impl_->ScrollEnd(); 1864 host_impl_->ScrollEnd();
1844 { 1865 {
1845 CompositorFrameMetadata metadata = 1866 CompositorFrameMetadata metadata =
1846 host_impl_->MakeCompositorFrameMetadata(); 1867 host_impl_->MakeCompositorFrameMetadata();
1847 EXPECT_EQ(gfx::Vector2dF(0.f, 10.f), metadata.root_scroll_offset); 1868 EXPECT_EQ(gfx::Vector2dF(0.f, 10.f), metadata.root_scroll_offset);
1848 } 1869 }
1849 1870
1850 // Root "overflow: hidden" properties should be reflected. 1871 // Root "overflow: hidden" properties should be reflected on the outer
1872 // viewport scroll layer.
1851 { 1873 {
1852 host_impl_->active_tree() 1874 host_impl_->active_tree()
1853 ->InnerViewportScrollLayer() 1875 ->OuterViewportScrollLayer()
1854 ->set_user_scrollable_horizontal(false); 1876 ->set_user_scrollable_horizontal(false);
1855 CompositorFrameMetadata metadata = 1877 CompositorFrameMetadata metadata =
1856 host_impl_->MakeCompositorFrameMetadata(); 1878 host_impl_->MakeCompositorFrameMetadata();
1857 EXPECT_TRUE(metadata.root_overflow_x_hidden); 1879 EXPECT_TRUE(metadata.root_overflow_x_hidden);
1858 EXPECT_FALSE(metadata.root_overflow_y_hidden); 1880 EXPECT_FALSE(metadata.root_overflow_y_hidden);
1859 1881
1860 host_impl_->active_tree() 1882 host_impl_->active_tree()
1861 ->InnerViewportScrollLayer() 1883 ->OuterViewportScrollLayer()
1862 ->set_user_scrollable_vertical(false); 1884 ->set_user_scrollable_vertical(false);
1863 metadata = host_impl_->MakeCompositorFrameMetadata(); 1885 metadata = host_impl_->MakeCompositorFrameMetadata();
1864 EXPECT_TRUE(metadata.root_overflow_x_hidden); 1886 EXPECT_TRUE(metadata.root_overflow_x_hidden);
1865 EXPECT_TRUE(metadata.root_overflow_y_hidden); 1887 EXPECT_TRUE(metadata.root_overflow_y_hidden);
1866 } 1888 }
1867 1889
1868 // Page scale should update metadata correctly (shrinking only the viewport). 1890 // Page scale should update metadata correctly (shrinking only the viewport).
1869 host_impl_->ScrollBegin(gfx::Point(), InputHandler::GESTURE); 1891 host_impl_->ScrollBegin(gfx::Point(), InputHandler::GESTURE);
1870 host_impl_->PinchGestureBegin(); 1892 host_impl_->PinchGestureBegin();
1871 host_impl_->PinchGestureUpdate(2.f, gfx::Point()); 1893 host_impl_->PinchGestureUpdate(2.f, gfx::Point());
(...skipping 2093 matching lines...) Expand 10 before | Expand all | Expand 10 after
3965 host_impl_->PinchGestureEnd(); 3987 host_impl_->PinchGestureEnd();
3966 host_impl_->ScrollEnd(); 3988 host_impl_->ScrollEnd();
3967 3989
3968 // Scrolling should be relative to the offset as returned by the delegate. 3990 // Scrolling should be relative to the offset as returned by the delegate.
3969 gfx::Vector2dF scroll_delta(0.f, 10.f); 3991 gfx::Vector2dF scroll_delta(0.f, 10.f);
3970 gfx::ScrollOffset current_offset(7.f, 8.f); 3992 gfx::ScrollOffset current_offset(7.f, 8.f);
3971 3993
3972 scroll_delegate.set_getter_return_value(current_offset); 3994 scroll_delegate.set_getter_return_value(current_offset);
3973 EXPECT_EQ(InputHandler::SCROLL_STARTED, 3995 EXPECT_EQ(InputHandler::SCROLL_STARTED,
3974 host_impl_->ScrollBegin(gfx::Point(), InputHandler::GESTURE)); 3996 host_impl_->ScrollBegin(gfx::Point(), InputHandler::GESTURE));
3997 host_impl_->OnRootLayerDelegatedScrollOffsetChanged();
3975 3998
3976 host_impl_->ScrollBy(gfx::Point(), scroll_delta); 3999 host_impl_->ScrollBy(gfx::Point(), scroll_delta);
3977 EXPECT_EQ(ScrollOffsetWithDelta(current_offset, scroll_delta), 4000 EXPECT_EQ(ScrollOffsetWithDelta(current_offset, scroll_delta),
3978 scroll_delegate.last_set_scroll_offset()); 4001 scroll_delegate.last_set_scroll_offset());
3979 4002
3980 current_offset = gfx::ScrollOffset(42.f, 41.f); 4003 current_offset = gfx::ScrollOffset(42.f, 41.f);
3981 scroll_delegate.set_getter_return_value(current_offset); 4004 scroll_delegate.set_getter_return_value(current_offset);
4005 host_impl_->OnRootLayerDelegatedScrollOffsetChanged();
3982 host_impl_->ScrollBy(gfx::Point(), scroll_delta); 4006 host_impl_->ScrollBy(gfx::Point(), scroll_delta);
3983 EXPECT_EQ(current_offset + gfx::ScrollOffset(scroll_delta), 4007 EXPECT_EQ(current_offset + gfx::ScrollOffset(scroll_delta),
3984 scroll_delegate.last_set_scroll_offset()); 4008 scroll_delegate.last_set_scroll_offset());
3985 host_impl_->ScrollEnd(); 4009 host_impl_->ScrollEnd();
3986 scroll_delegate.set_getter_return_value(gfx::ScrollOffset()); 4010 scroll_delegate.set_getter_return_value(gfx::ScrollOffset());
4011 host_impl_->OnRootLayerDelegatedScrollOffsetChanged();
3987 4012
3988 // Forces a full tree synchronization and ensures that the scroll delegate 4013 // Forces a full tree synchronization and ensures that the scroll delegate
3989 // sees the correct size of the new tree. 4014 // sees the correct size of the new tree.
3990 gfx::Size new_size(42, 24); 4015 gfx::Size new_size(42, 24);
3991 host_impl_->CreatePendingTree(); 4016 host_impl_->CreatePendingTree();
3992 CreateScrollAndContentsLayers(host_impl_->pending_tree(), new_size); 4017 CreateScrollAndContentsLayers(host_impl_->pending_tree(), new_size);
3993 host_impl_->ActivateSyncTree(); 4018 host_impl_->ActivateSyncTree();
3994 EXPECT_EQ(new_size, scroll_delegate.scrollable_size()); 4019 EXPECT_EQ(new_size, scroll_delegate.scrollable_size());
3995 4020
3996 // Un-setting the delegate should propagate the delegate's current offset to 4021 // Un-setting the delegate should propagate the delegate's current offset to
3997 // the root scrollable layer. 4022 // the root scrollable layer.
3998 current_offset = gfx::ScrollOffset(13.f, 12.f); 4023 current_offset = gfx::ScrollOffset(13.f, 12.f);
3999 scroll_delegate.set_getter_return_value(current_offset); 4024 scroll_delegate.set_getter_return_value(current_offset);
4025 host_impl_->OnRootLayerDelegatedScrollOffsetChanged();
4000 host_impl_->SetRootLayerScrollOffsetDelegate(NULL); 4026 host_impl_->SetRootLayerScrollOffsetDelegate(NULL);
4001 4027
4002 EXPECT_EQ(current_offset.ToString(), 4028 EXPECT_EQ(current_offset.ToString(),
4003 scroll_layer->CurrentScrollOffset().ToString()); 4029 scroll_layer->CurrentScrollOffset().ToString());
4004 } 4030 }
4005 4031
4006 void CheckLayerScrollDelta(LayerImpl* layer, gfx::Vector2dF scroll_delta) { 4032 void CheckLayerScrollDelta(LayerImpl* layer, gfx::Vector2dF scroll_delta) {
4007 const gfx::Transform target_space_transform = 4033 const gfx::Transform target_space_transform =
4008 layer->draw_properties().target_space_transform; 4034 layer->draw_properties().target_space_transform;
4009 EXPECT_TRUE(target_space_transform.IsScaleOrTranslation()); 4035 EXPECT_TRUE(target_space_transform.IsScaleOrTranslation());
(...skipping 3849 matching lines...) Expand 10 before | Expand all | Expand 10 after
7859 inner_clip->SetHasRenderSurface(true); 7885 inner_clip->SetHasRenderSurface(true);
7860 layer_tree_impl->SetRootLayer(inner_clip.Pass()); 7886 layer_tree_impl->SetRootLayer(inner_clip.Pass());
7861 layer_tree_impl->SetViewportLayersFromIds( 7887 layer_tree_impl->SetViewportLayersFromIds(
7862 Layer::INVALID_ID, kPageScaleLayerId, kInnerViewportScrollLayerId, 7888 Layer::INVALID_ID, kPageScaleLayerId, kInnerViewportScrollLayerId,
7863 kOuterViewportScrollLayerId); 7889 kOuterViewportScrollLayerId);
7864 7890
7865 host_impl_->active_tree()->DidBecomeActive(); 7891 host_impl_->active_tree()->DidBecomeActive();
7866 } 7892 }
7867 }; 7893 };
7868 7894
7895 TEST_F(LayerTreeHostImplVirtualViewportTest, ScrollBothInnerAndOuterLayer) {
7896 gfx::Size content_size = gfx::Size(100, 160);
7897 gfx::Size outer_viewport = gfx::Size(50, 80);
7898 gfx::Size inner_viewport = gfx::Size(25, 40);
7899
7900 SetupVirtualViewportLayers(content_size, outer_viewport, inner_viewport);
7901
7902 TestScrollOffsetDelegate scroll_delegate;
7903 host_impl_->SetRootLayerScrollOffsetDelegate(&scroll_delegate);
7904
7905 LayerImpl* outer_scroll = host_impl_->OuterViewportScrollLayer();
7906 LayerImpl* inner_scroll = host_impl_->InnerViewportScrollLayer();
7907 DrawFrame();
7908 {
7909 gfx::ScrollOffset inner_expected;
7910 gfx::ScrollOffset outer_expected;
7911 EXPECT_EQ(inner_expected, inner_scroll->CurrentScrollOffset());
7912 EXPECT_EQ(outer_expected, outer_scroll->CurrentScrollOffset());
7913
7914 gfx::ScrollOffset current_offset(70.f, 100.f);
7915
7916 scroll_delegate.set_getter_return_value(current_offset);
7917 host_impl_->OnRootLayerDelegatedScrollOffsetChanged();
7918 EXPECT_EQ(gfx::ScrollOffset(25.f, 40.f), inner_scroll->MaxScrollOffset());
7919 EXPECT_EQ(gfx::ScrollOffset(50.f, 80.f), outer_scroll->MaxScrollOffset());
7920
7921 // Outer viewport scrolls first. Then the rest is applied to the inner
7922 // viewport.
7923 EXPECT_EQ(gfx::ScrollOffset(20.f, 20.f),
7924 inner_scroll->CurrentScrollOffset());
7925 EXPECT_EQ(gfx::ScrollOffset(50.f, 80.f),
7926 outer_scroll->CurrentScrollOffset());
7927 }
7928 }
7929
7869 TEST_F(LayerTreeHostImplVirtualViewportTest, FlingScrollBubblesToInner) { 7930 TEST_F(LayerTreeHostImplVirtualViewportTest, FlingScrollBubblesToInner) {
7870 gfx::Size content_size = gfx::Size(100, 160); 7931 gfx::Size content_size = gfx::Size(100, 160);
7871 gfx::Size outer_viewport = gfx::Size(50, 80); 7932 gfx::Size outer_viewport = gfx::Size(50, 80);
7872 gfx::Size inner_viewport = gfx::Size(25, 40); 7933 gfx::Size inner_viewport = gfx::Size(25, 40);
7873 7934
7874 SetupVirtualViewportLayers(content_size, outer_viewport, inner_viewport); 7935 SetupVirtualViewportLayers(content_size, outer_viewport, inner_viewport);
7875 7936
7876 LayerImpl* outer_scroll = host_impl_->OuterViewportScrollLayer(); 7937 LayerImpl* outer_scroll = host_impl_->OuterViewportScrollLayer();
7877 LayerImpl* inner_scroll = host_impl_->InnerViewportScrollLayer(); 7938 LayerImpl* inner_scroll = host_impl_->InnerViewportScrollLayer();
7878 DrawFrame(); 7939 DrawFrame();
(...skipping 586 matching lines...) Expand 10 before | Expand all | Expand 10 after
8465 // surface. 8526 // surface.
8466 EXPECT_EQ(0, num_lost_surfaces_); 8527 EXPECT_EQ(0, num_lost_surfaces_);
8467 host_impl_->DidLoseOutputSurface(); 8528 host_impl_->DidLoseOutputSurface();
8468 EXPECT_EQ(1, num_lost_surfaces_); 8529 EXPECT_EQ(1, num_lost_surfaces_);
8469 host_impl_->DidLoseOutputSurface(); 8530 host_impl_->DidLoseOutputSurface();
8470 EXPECT_LE(1, num_lost_surfaces_); 8531 EXPECT_LE(1, num_lost_surfaces_);
8471 } 8532 }
8472 8533
8473 } // namespace 8534 } // namespace
8474 } // namespace cc 8535 } // namespace cc
OLDNEW
« no previous file with comments | « cc/trees/layer_tree_host_impl.cc ('k') | cc/trees/layer_tree_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698