| OLD | NEW |
| 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/input/scroll_elasticity_helper.h" | 5 #include "cc/input/scroll_elasticity_helper.h" |
| 6 | 6 |
| 7 #include "base/macros.h" |
| 8 #include "base/memory/ptr_util.h" |
| 7 #include "cc/layers/layer_impl.h" | 9 #include "cc/layers/layer_impl.h" |
| 8 #include "cc/trees/layer_tree_host_impl.h" | 10 #include "cc/trees/layer_tree_host_impl.h" |
| 9 #include "cc/trees/layer_tree_impl.h" | 11 #include "cc/trees/layer_tree_impl.h" |
| 10 | 12 |
| 11 namespace cc { | 13 namespace cc { |
| 14 namespace { |
| 12 | 15 |
| 13 class ScrollElasticityHelperImpl : public ScrollElasticityHelper { | 16 class ScrollElasticityHelperImpl : public ScrollElasticityHelper { |
| 14 public: | 17 public: |
| 15 explicit ScrollElasticityHelperImpl(LayerTreeHostImpl* layer_tree_host_impl); | 18 explicit ScrollElasticityHelperImpl(LayerTreeHostImpl* layer_tree_host_impl); |
| 16 ~ScrollElasticityHelperImpl() override; | 19 ~ScrollElasticityHelperImpl() override; |
| 17 | 20 |
| 18 bool IsUserScrollable() const override; | 21 bool IsUserScrollable() const override; |
| 19 gfx::Vector2dF StretchAmount() const override; | 22 gfx::Vector2dF StretchAmount() const override; |
| 20 void SetStretchAmount(const gfx::Vector2dF& stretch_amount) override; | 23 void SetStretchAmount(const gfx::Vector2dF& stretch_amount) override; |
| 21 gfx::ScrollOffset ScrollOffset() const override; | 24 gfx::ScrollOffset ScrollOffset() const override; |
| 22 gfx::ScrollOffset MaxScrollOffset() const override; | 25 gfx::ScrollOffset MaxScrollOffset() const override; |
| 23 void ScrollBy(const gfx::Vector2dF& delta) override; | 26 void ScrollBy(const gfx::Vector2dF& delta) override; |
| 24 void RequestOneBeginFrame() override; | 27 void RequestOneBeginFrame() override; |
| 25 | 28 |
| 29 base::WeakPtr<ScrollElasticityHelper> GetWeakPtr() override { |
| 30 return weak_ptr_factory_.GetWeakPtr(); |
| 31 } |
| 32 |
| 26 private: | 33 private: |
| 27 LayerTreeHostImpl* layer_tree_host_impl_; | 34 LayerTreeHostImpl* layer_tree_host_impl_; |
| 35 |
| 36 base::WeakPtrFactory<ScrollElasticityHelperImpl> weak_ptr_factory_; |
| 37 |
| 38 DISALLOW_COPY_AND_ASSIGN(ScrollElasticityHelperImpl); |
| 28 }; | 39 }; |
| 29 | 40 |
| 30 ScrollElasticityHelperImpl::ScrollElasticityHelperImpl( | 41 ScrollElasticityHelperImpl::ScrollElasticityHelperImpl( |
| 31 LayerTreeHostImpl* layer_tree) | 42 LayerTreeHostImpl* layer_tree) |
| 32 : layer_tree_host_impl_(layer_tree) { | 43 : layer_tree_host_impl_(layer_tree), weak_ptr_factory_(this) {} |
| 33 } | |
| 34 | 44 |
| 35 ScrollElasticityHelperImpl::~ScrollElasticityHelperImpl() { | 45 ScrollElasticityHelperImpl::~ScrollElasticityHelperImpl() { |
| 36 } | 46 } |
| 37 | 47 |
| 38 bool ScrollElasticityHelperImpl::IsUserScrollable() const { | 48 bool ScrollElasticityHelperImpl::IsUserScrollable() const { |
| 39 LayerImpl* layer = layer_tree_host_impl_->OuterViewportScrollLayer(); | 49 LayerImpl* layer = layer_tree_host_impl_->OuterViewportScrollLayer(); |
| 40 if (!layer) | 50 if (!layer) |
| 41 return false; | 51 return false; |
| 42 return layer->user_scrollable_horizontal() || | 52 return layer->user_scrollable_horizontal() || |
| 43 layer->user_scrollable_vertical(); | 53 layer->user_scrollable_vertical(); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 ? layer_tree_host_impl_->OuterViewportScrollLayer() | 85 ? layer_tree_host_impl_->OuterViewportScrollLayer() |
| 76 : layer_tree_host_impl_->InnerViewportScrollLayer(); | 86 : layer_tree_host_impl_->InnerViewportScrollLayer(); |
| 77 if (root_scroll_layer) | 87 if (root_scroll_layer) |
| 78 root_scroll_layer->ScrollBy(delta); | 88 root_scroll_layer->ScrollBy(delta); |
| 79 } | 89 } |
| 80 | 90 |
| 81 void ScrollElasticityHelperImpl::RequestOneBeginFrame() { | 91 void ScrollElasticityHelperImpl::RequestOneBeginFrame() { |
| 82 layer_tree_host_impl_->SetNeedsOneBeginImplFrame(); | 92 layer_tree_host_impl_->SetNeedsOneBeginImplFrame(); |
| 83 } | 93 } |
| 84 | 94 |
| 95 class LayerScrollElasticityHelperImpl : public ScrollElasticityHelper { |
| 96 public: |
| 97 LayerScrollElasticityHelperImpl(int layer_scroll_layer_id, |
| 98 LayerTreeHostImpl* layer_tree_host_impl); |
| 99 |
| 100 bool IsUserScrollable() const override; |
| 101 gfx::Vector2dF StretchAmount() const override; |
| 102 void SetStretchAmount(const gfx::Vector2dF& stretch_amount) override; |
| 103 gfx::ScrollOffset ScrollOffset() const override; |
| 104 gfx::ScrollOffset MaxScrollOffset() const override; |
| 105 void ScrollBy(const gfx::Vector2dF& delta) override; |
| 106 void RequestOneBeginFrame() override; |
| 107 |
| 108 base::WeakPtr<ScrollElasticityHelper> GetWeakPtr() override { |
| 109 return weak_ptr_factory_.GetWeakPtr(); |
| 110 } |
| 111 |
| 112 private: |
| 113 LayerImpl* layer() const; |
| 114 |
| 115 LayerTreeHostImpl* layer_tree_host_impl_; |
| 116 int layer_scroll_layer_id_; |
| 117 |
| 118 base::WeakPtrFactory<LayerScrollElasticityHelperImpl> weak_ptr_factory_; |
| 119 |
| 120 DISALLOW_COPY_AND_ASSIGN(LayerScrollElasticityHelperImpl); |
| 121 }; |
| 122 |
| 123 LayerScrollElasticityHelperImpl::LayerScrollElasticityHelperImpl( |
| 124 int layer_scroll_layer_id, |
| 125 LayerTreeHostImpl* layer_tree_host_impl) |
| 126 : layer_tree_host_impl_(layer_tree_host_impl), |
| 127 layer_scroll_layer_id_(layer_scroll_layer_id), |
| 128 weak_ptr_factory_(this) {} |
| 129 |
| 130 bool LayerScrollElasticityHelperImpl::IsUserScrollable() const { |
| 131 // LayerTreeHostImpl wouldn't have created |this| in the first place if the |
| 132 // layer wasn't user-scrollable. |
| 133 return true; |
| 134 } |
| 135 |
| 136 gfx::Vector2dF LayerScrollElasticityHelperImpl::StretchAmount() const { |
| 137 return ScrollOffsetToVector2dF(layer()->CurrentOverscroll()); |
| 138 } |
| 139 |
| 140 void LayerScrollElasticityHelperImpl::SetStretchAmount( |
| 141 const gfx::Vector2dF& stretch_amount) { |
| 142 if (stretch_amount == StretchAmount()) |
| 143 return; |
| 144 |
| 145 layer()->SetCurrentOverscroll(gfx::ScrollOffset(stretch_amount)); |
| 146 |
| 147 // For now, also inform the LayerTreeImpl of the stretch amount. This will |
| 148 // then get redistributed over the scrolling layer. |
| 149 // TODO(tapted): Remove this - it shouldn't be necessary. |
| 150 layer_tree_host_impl_->active_tree()->elastic_overscroll()->SetCurrent( |
| 151 stretch_amount); |
| 152 layer_tree_host_impl_->SetNeedsCommit(); |
| 153 } |
| 154 |
| 155 gfx::ScrollOffset LayerScrollElasticityHelperImpl::ScrollOffset() const { |
| 156 return layer()->CurrentScrollOffset(); |
| 157 } |
| 158 |
| 159 gfx::ScrollOffset LayerScrollElasticityHelperImpl::MaxScrollOffset() const { |
| 160 return layer()->MaxScrollOffset(); |
| 161 } |
| 162 |
| 163 void LayerScrollElasticityHelperImpl::ScrollBy(const gfx::Vector2dF& delta) { |
| 164 layer()->ScrollBy(delta); |
| 165 } |
| 166 |
| 167 void LayerScrollElasticityHelperImpl::RequestOneBeginFrame() { |
| 168 layer_tree_host_impl_->SetNeedsOneBeginImplFrame(); |
| 169 } |
| 170 |
| 171 LayerImpl* LayerScrollElasticityHelperImpl::layer() const { |
| 172 LayerImpl* scrolling_layer = |
| 173 layer_tree_host_impl_->active_tree()->FindActiveTreeLayerById( |
| 174 layer_scroll_layer_id_); |
| 175 return scrolling_layer; |
| 176 } |
| 177 |
| 178 } // namespace |
| 179 |
| 180 // static |
| 181 std::unique_ptr<ScrollElasticityHelper> ScrollElasticityHelper::CreateForLayer( |
| 182 int layer_scroll_layer_id, |
| 183 LayerTreeHostImpl* layer_tree_host_impl) { |
| 184 return base::MakeUnique<LayerScrollElasticityHelperImpl>( |
| 185 layer_scroll_layer_id, layer_tree_host_impl); |
| 186 } |
| 187 |
| 85 // static | 188 // static |
| 86 ScrollElasticityHelper* ScrollElasticityHelper::CreateForLayerTreeHostImpl( | 189 ScrollElasticityHelper* ScrollElasticityHelper::CreateForLayerTreeHostImpl( |
| 87 LayerTreeHostImpl* layer_tree_host_impl) { | 190 LayerTreeHostImpl* layer_tree_host_impl) { |
| 88 return new ScrollElasticityHelperImpl(layer_tree_host_impl); | 191 return new ScrollElasticityHelperImpl(layer_tree_host_impl); |
| 89 } | 192 } |
| 90 | 193 |
| 91 } // namespace cc | 194 } // namespace cc |
| OLD | NEW |