| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 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.h" | 5 #include "cc/trees/layer_tree_host.h" |
| 6 | 6 |
| 7 #include "base/location.h" | 7 #include "base/location.h" |
| 8 #include "base/memory/weak_ptr.h" | 8 #include "base/memory/weak_ptr.h" |
| 9 #include "base/single_thread_task_runner.h" | 9 #include "base/single_thread_task_runner.h" |
| 10 #include "base/threading/thread_task_runner_handle.h" | 10 #include "base/threading/thread_task_runner_handle.h" |
| 11 #include "cc/animation/animation_host.h" | 11 #include "cc/animation/animation_host.h" |
| 12 #include "cc/input/main_thread_scrolling_reason.h" | 12 #include "cc/input/main_thread_scrolling_reason.h" |
| 13 #include "cc/input/scroll_elasticity_helper.h" |
| 13 #include "cc/layers/layer.h" | 14 #include "cc/layers/layer.h" |
| 14 #include "cc/layers/layer_impl.h" | 15 #include "cc/layers/layer_impl.h" |
| 15 #include "cc/layers/picture_layer.h" | 16 #include "cc/layers/picture_layer.h" |
| 16 #include "cc/scheduler/begin_frame_source.h" | 17 #include "cc/scheduler/begin_frame_source.h" |
| 17 #include "cc/test/fake_content_layer_client.h" | 18 #include "cc/test/fake_content_layer_client.h" |
| 18 #include "cc/test/fake_layer_tree_host_client.h" | 19 #include "cc/test/fake_layer_tree_host_client.h" |
| 19 #include "cc/test/fake_picture_layer.h" | 20 #include "cc/test/fake_picture_layer.h" |
| 20 #include "cc/test/fake_picture_layer_impl.h" | 21 #include "cc/test/fake_picture_layer_impl.h" |
| 21 #include "cc/test/geometry_test_utils.h" | 22 #include "cc/test/geometry_test_utils.h" |
| 22 #include "cc/test/layer_tree_test.h" | 23 #include "cc/test/layer_tree_test.h" |
| 23 #include "cc/test/test_shared_bitmap_manager.h" | 24 #include "cc/test/test_shared_bitmap_manager.h" |
| 24 #include "cc/test/test_task_graph_runner.h" | 25 #include "cc/test/test_task_graph_runner.h" |
| 25 #include "cc/trees/layer_tree_impl.h" | 26 #include "cc/trees/layer_tree_impl.h" |
| 26 #include "cc/trees/scroll_node.h" | 27 #include "cc/trees/scroll_node.h" |
| 28 #include "testing/gmock/include/gmock/gmock.h" |
| 27 #include "ui/gfx/geometry/point_conversions.h" | 29 #include "ui/gfx/geometry/point_conversions.h" |
| 28 #include "ui/gfx/geometry/size_conversions.h" | 30 #include "ui/gfx/geometry/size_conversions.h" |
| 29 #include "ui/gfx/geometry/vector2d_conversions.h" | 31 #include "ui/gfx/geometry/vector2d_conversions.h" |
| 30 | 32 |
| 33 using ::testing::Mock; |
| 34 |
| 31 namespace cc { | 35 namespace cc { |
| 32 namespace { | 36 namespace { |
| 33 | 37 |
| 34 std::unique_ptr<ScrollState> BeginState(const gfx::Point& point) { | 38 std::unique_ptr<ScrollState> BeginState(const gfx::Point& point) { |
| 35 ScrollStateData scroll_state_data; | 39 ScrollStateData scroll_state_data; |
| 36 scroll_state_data.is_beginning = true; | 40 scroll_state_data.is_beginning = true; |
| 37 scroll_state_data.position_x = point.x(); | 41 scroll_state_data.position_x = point.x(); |
| 38 scroll_state_data.position_y = point.y(); | 42 scroll_state_data.position_y = point.y(); |
| 39 std::unique_ptr<ScrollState> scroll_state(new ScrollState(scroll_state_data)); | 43 std::unique_ptr<ScrollState> scroll_state(new ScrollState(scroll_state_data)); |
| 40 return scroll_state; | 44 return scroll_state; |
| (...skipping 1748 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1789 int num_will_commits_; | 1793 int num_will_commits_; |
| 1790 int num_did_commits_; | 1794 int num_did_commits_; |
| 1791 int num_impl_commits_; | 1795 int num_impl_commits_; |
| 1792 int num_aborted_commits_; | 1796 int num_aborted_commits_; |
| 1793 int num_impl_scrolls_; | 1797 int num_impl_scrolls_; |
| 1794 int num_draws_; | 1798 int num_draws_; |
| 1795 }; | 1799 }; |
| 1796 | 1800 |
| 1797 MULTI_THREAD_TEST_F(LayerTreeHostScrollTestScrollAbortedCommitMFBA); | 1801 MULTI_THREAD_TEST_F(LayerTreeHostScrollTestScrollAbortedCommitMFBA); |
| 1798 | 1802 |
| 1803 class MockInputHandlerClient : public InputHandlerClient { |
| 1804 public: |
| 1805 MockInputHandlerClient() {} |
| 1806 |
| 1807 MOCK_METHOD0(ReconcileElasticOverscrollAndRootScroll, void()); |
| 1808 |
| 1809 void WillShutdown() override {} |
| 1810 void Animate(base::TimeTicks) override {} |
| 1811 void MainThreadHasStoppedFlinging() override {} |
| 1812 void UpdateRootLayerStateForSynchronousInputHandler( |
| 1813 const gfx::ScrollOffset& total_scroll_offset, |
| 1814 const gfx::ScrollOffset& max_scroll_offset, |
| 1815 const gfx::SizeF& scrollable_size, |
| 1816 float page_scale_factor, |
| 1817 float min_page_scale_factor, |
| 1818 float max_page_scale_factor) override {} |
| 1819 }; |
| 1820 |
| 1821 // This is a regression test, see crbug.com/639046. |
| 1822 class LayerTreeHostScrollTestElasticOverscroll |
| 1823 : public LayerTreeHostScrollTest { |
| 1824 public: |
| 1825 LayerTreeHostScrollTestElasticOverscroll() |
| 1826 : num_begin_main_frames_impl_thread_(0), |
| 1827 scroll_elasticity_helper_(nullptr), |
| 1828 num_begin_main_frames_main_thread_(0) {} |
| 1829 |
| 1830 void InitializeSettings(LayerTreeSettings* settings) override { |
| 1831 settings->enable_elastic_overscroll = true; |
| 1832 } |
| 1833 |
| 1834 void BeginTest() override { |
| 1835 DCHECK(HasImplThread()); |
| 1836 ImplThreadTaskRunner()->PostTask( |
| 1837 FROM_HERE, |
| 1838 base::Bind(&LayerTreeHostScrollTestElasticOverscroll::BindInputHandler, |
| 1839 base::Unretained(this), |
| 1840 layer_tree_host()->GetInputHandler())); |
| 1841 PostSetNeedsCommitToMainThread(); |
| 1842 } |
| 1843 |
| 1844 void BindInputHandler(const base::WeakPtr<InputHandler> input_handler) { |
| 1845 DCHECK(task_runner_provider()->IsImplThread()); |
| 1846 input_handler->BindToClient(&input_handler_client_); |
| 1847 scroll_elasticity_helper_ = input_handler->CreateScrollElasticityHelper(); |
| 1848 DCHECK(scroll_elasticity_helper_); |
| 1849 } |
| 1850 |
| 1851 void ApplyViewportDeltas(const gfx::Vector2dF& inner_delta, |
| 1852 const gfx::Vector2dF& outer_delta, |
| 1853 const gfx::Vector2dF& elastic_overscroll_delta, |
| 1854 float scale, |
| 1855 float top_controls_delta) override { |
| 1856 DCHECK_NE(0, num_begin_main_frames_main_thread_) |
| 1857 << "The first BeginMainFrame has no deltas to report"; |
| 1858 DCHECK_LT(num_begin_main_frames_main_thread_, 5); |
| 1859 |
| 1860 gfx::Vector2dF expected_elastic_overscroll = |
| 1861 elastic_overscroll_test_cases_[num_begin_main_frames_main_thread_]; |
| 1862 current_elastic_overscroll_ += elastic_overscroll_delta; |
| 1863 EXPECT_EQ(expected_elastic_overscroll, current_elastic_overscroll_); |
| 1864 EXPECT_EQ(expected_elastic_overscroll, layer_tree()->elastic_overscroll()); |
| 1865 } |
| 1866 |
| 1867 void WillBeginMainFrame() override { num_begin_main_frames_main_thread_++; } |
| 1868 |
| 1869 void BeginMainFrameAbortedOnThread(LayerTreeHostImpl* host_impl, |
| 1870 CommitEarlyOutReason reason) override { |
| 1871 VerifyBeginMainFrameResultOnImplThread(host_impl, true); |
| 1872 } |
| 1873 |
| 1874 void WillCommitCompleteOnThread(LayerTreeHostImpl* host_impl) override { |
| 1875 VerifyBeginMainFrameResultOnImplThread(host_impl, false); |
| 1876 } |
| 1877 |
| 1878 void VerifyBeginMainFrameResultOnImplThread(LayerTreeHostImpl* host_impl, |
| 1879 bool begin_main_frame_aborted) { |
| 1880 gfx::Vector2dF expected_elastic_overscroll = |
| 1881 elastic_overscroll_test_cases_[num_begin_main_frames_impl_thread_]; |
| 1882 EXPECT_EQ(expected_elastic_overscroll, |
| 1883 scroll_elasticity_helper_->StretchAmount()); |
| 1884 if (!begin_main_frame_aborted) |
| 1885 EXPECT_EQ( |
| 1886 expected_elastic_overscroll, |
| 1887 host_impl->pending_tree()->elastic_overscroll()->Current(false)); |
| 1888 |
| 1889 ++num_begin_main_frames_impl_thread_; |
| 1890 gfx::Vector2dF next_test_case; |
| 1891 if (num_begin_main_frames_impl_thread_ < 5) |
| 1892 next_test_case = |
| 1893 elastic_overscroll_test_cases_[num_begin_main_frames_impl_thread_]; |
| 1894 |
| 1895 switch (num_begin_main_frames_impl_thread_) { |
| 1896 case 1: |
| 1897 // The first BeginMainFrame is never aborted. |
| 1898 EXPECT_FALSE(begin_main_frame_aborted); |
| 1899 scroll_elasticity_helper_->SetStretchAmount(next_test_case); |
| 1900 break; |
| 1901 case 2: |
| 1902 EXPECT_TRUE(begin_main_frame_aborted); |
| 1903 scroll_elasticity_helper_->SetStretchAmount(next_test_case); |
| 1904 |
| 1905 // Since the elastic overscroll is never mutated on the main thread, the |
| 1906 // BeginMainFrame which reports the delta is aborted. Post a commit |
| 1907 // request to the main thread to make sure it goes through. |
| 1908 PostSetNeedsCommitToMainThread(); |
| 1909 break; |
| 1910 case 3: |
| 1911 EXPECT_FALSE(begin_main_frame_aborted); |
| 1912 scroll_elasticity_helper_->SetStretchAmount(next_test_case); |
| 1913 PostSetNeedsCommitToMainThread(); |
| 1914 break; |
| 1915 case 4: |
| 1916 EXPECT_FALSE(begin_main_frame_aborted); |
| 1917 scroll_elasticity_helper_->SetStretchAmount(next_test_case); |
| 1918 break; |
| 1919 case 5: |
| 1920 EXPECT_TRUE(begin_main_frame_aborted); |
| 1921 EndTest(); |
| 1922 break; |
| 1923 default: |
| 1924 NOTREACHED(); |
| 1925 } |
| 1926 } |
| 1927 |
| 1928 void DidActivateTreeOnThread(LayerTreeHostImpl* host_impl) override { |
| 1929 if (num_begin_main_frames_impl_thread_ == 5) |
| 1930 return; |
| 1931 |
| 1932 // Ensure that the elastic overscroll value on the active tree remains |
| 1933 // unmodified after activation. |
| 1934 gfx::Vector2dF expected_elastic_overscroll = |
| 1935 elastic_overscroll_test_cases_[num_begin_main_frames_impl_thread_]; |
| 1936 EXPECT_EQ(expected_elastic_overscroll, |
| 1937 scroll_elasticity_helper_->StretchAmount()); |
| 1938 } |
| 1939 |
| 1940 void WillPrepareToDrawOnThread(LayerTreeHostImpl* host_impl) override { |
| 1941 // The InputHandlerClient must receive a call to reconcile the overscroll |
| 1942 // before each draw. |
| 1943 EXPECT_CALL(input_handler_client_, |
| 1944 ReconcileElasticOverscrollAndRootScroll()) |
| 1945 .Times(1); |
| 1946 } |
| 1947 |
| 1948 DrawResult PrepareToDrawOnThread(LayerTreeHostImpl* host_impl, |
| 1949 LayerTreeHostImpl::FrameData* frame_data, |
| 1950 DrawResult draw_result) override { |
| 1951 Mock::VerifyAndClearExpectations(&input_handler_client_); |
| 1952 return draw_result; |
| 1953 } |
| 1954 |
| 1955 void AfterTest() override { |
| 1956 EXPECT_EQ(num_begin_main_frames_impl_thread_, 5); |
| 1957 EXPECT_EQ(num_begin_main_frames_main_thread_, 5); |
| 1958 gfx::Vector2dF expected_elastic_overscroll = |
| 1959 elastic_overscroll_test_cases_[4]; |
| 1960 EXPECT_EQ(expected_elastic_overscroll, current_elastic_overscroll_); |
| 1961 } |
| 1962 |
| 1963 private: |
| 1964 // These values should be used on the impl thread only. |
| 1965 int num_begin_main_frames_impl_thread_; |
| 1966 MockInputHandlerClient input_handler_client_; |
| 1967 ScrollElasticityHelper* scroll_elasticity_helper_; |
| 1968 |
| 1969 // These values should be used on the main thread only. |
| 1970 int num_begin_main_frames_main_thread_; |
| 1971 gfx::Vector2dF current_elastic_overscroll_; |
| 1972 |
| 1973 const gfx::Vector2dF elastic_overscroll_test_cases_[5] = { |
| 1974 gfx::Vector2dF(0, 0), gfx::Vector2dF(5, 10), gfx::Vector2dF(5, 5), |
| 1975 gfx::Vector2dF(-4, -5), gfx::Vector2dF(0, 0)}; |
| 1976 }; |
| 1977 |
| 1978 MULTI_THREAD_TEST_F(LayerTreeHostScrollTestElasticOverscroll); |
| 1979 |
| 1799 } // namespace | 1980 } // namespace |
| 1800 } // namespace cc | 1981 } // namespace cc |
| OLD | NEW |