Bug 1351783 part 14 - Create a base class for WheelScrollAnimation. r=botond
authorRyan Hunt <rhunt@eqrion.net>
Thu, 15 Jun 2017 04:31:50 -0400
changeset 600762 a63081b75178ca54485cb63b38aee19938c56dbf
parent 600761 c4a493398777eac3d29ea203371e88e6c6b9d58e
child 600763 f163a2ba051b0f96736799fb08ee2cb460408eeb
push id65868
push userbmo:rail@mozilla.com
push dateTue, 27 Jun 2017 20:33:55 +0000
reviewersbotond
bugs1351783
milestone56.0a1
Bug 1351783 part 14 - Create a base class for WheelScrollAnimation. r=botond MozReview-Commit-ID: BtUJo5NAiTR
gfx/layers/apz/src/AsyncPanZoomController.cpp
gfx/layers/apz/src/AsyncPanZoomController.h
gfx/layers/apz/src/GenericScrollAnimation.cpp
gfx/layers/apz/src/GenericScrollAnimation.h
gfx/layers/apz/src/WheelScrollAnimation.cpp
gfx/layers/apz/src/WheelScrollAnimation.h
gfx/layers/moz.build
--- a/gfx/layers/apz/src/AsyncPanZoomController.cpp
+++ b/gfx/layers/apz/src/AsyncPanZoomController.cpp
@@ -1845,17 +1845,17 @@ nsEventStatus AsyncPanZoomController::On
       nsPoint deltaInAppUnits =
         CSSPoint::ToAppUnits(delta / mFrameMetrics.GetZoom());
       // Cast velocity from ParentLayerPoints/ms to CSSPoints/ms then convert to
       // appunits/second
       nsPoint velocity =
         CSSPoint::ToAppUnits(CSSPoint(mX.GetVelocity(), mY.GetVelocity())) * 1000.0f;
 
       WheelScrollAnimation* animation = mAnimation->AsWheelScrollAnimation();
-      animation->Update(aEvent.mTimeStamp, deltaInAppUnits, nsSize(velocity.x, velocity.y));
+      animation->UpdateDelta(aEvent.mTimeStamp, deltaInAppUnits, nsSize(velocity.x, velocity.y));
       break;
     }
 
     case ScrollWheelInput::SCROLLMODE_SENTINEL: {
       MOZ_ASSERT_UNREACHABLE("Invalid ScrollMode.");
       break;
     }
   }
--- a/gfx/layers/apz/src/AsyncPanZoomController.h
+++ b/gfx/layers/apz/src/AsyncPanZoomController.h
@@ -931,16 +931,17 @@ public:
    */
   bool AttemptFling(FlingHandoffState& aHandoffState);
 
 private:
   friend class AndroidFlingAnimation;
   friend class GenericFlingAnimation;
   friend class OverscrollAnimation;
   friend class SmoothScrollAnimation;
+  friend class GenericScrollAnimation;
   friend class WheelScrollAnimation;
 
   friend class GenericOverscrollEffect;
   friend class WidgetOverscrollEffect;
 
   // The initial velocity of the most recent fling.
   ParentLayerPoint mLastFlingVelocity;
   // The time at which the most recent fling started.
new file mode 100644
--- /dev/null
+++ b/gfx/layers/apz/src/GenericScrollAnimation.cpp
@@ -0,0 +1,90 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set sw=2 ts=8 et tw=80 : */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#include "GenericScrollAnimation.h"
+
+#include "AsyncPanZoomController.h"
+#include "gfxPrefs.h"
+#include "nsPoint.h"
+
+namespace mozilla {
+namespace layers {
+
+GenericScrollAnimation::GenericScrollAnimation(AsyncPanZoomController& aApzc,
+                                               const nsPoint& aInitialPosition)
+  : AsyncScrollBase(aInitialPosition)
+  , mApzc(aApzc)
+  , mFinalDestination(aInitialPosition)
+  , mForceVerticalOverscroll(false)
+{
+}
+
+void
+GenericScrollAnimation::UpdateDelta(TimeStamp aTime, nsPoint aDelta, const nsSize& aCurrentVelocity)
+{
+  if (mIsFirstIteration) {
+    InitializeHistory(aTime);
+  }
+
+  mFinalDestination += aDelta;
+
+  // Clamp the final destination to the scrollable area.
+  CSSPoint clamped = CSSPoint::FromAppUnits(mFinalDestination);
+  clamped.x = mApzc.mX.ClampOriginToScrollableRect(clamped.x);
+  clamped.y = mApzc.mY.ClampOriginToScrollableRect(clamped.y);
+  mFinalDestination = CSSPoint::ToAppUnits(clamped);
+
+  AsyncScrollBase::Update(aTime, mFinalDestination, aCurrentVelocity);
+}
+
+bool
+GenericScrollAnimation::DoSample(FrameMetrics& aFrameMetrics, const TimeDuration& aDelta)
+{
+  TimeStamp now = mApzc.GetFrameTime();
+  CSSToParentLayerScale2D zoom = aFrameMetrics.GetZoom();
+
+  // If the animation is finished, make sure the final position is correct by
+  // using one last displacement. Otherwise, compute the delta via the timing
+  // function as normal.
+  bool finished = IsFinished(now);
+  nsPoint sampledDest = finished
+                        ? mDestination
+                        : PositionAt(now);
+  ParentLayerPoint displacement =
+    (CSSPoint::FromAppUnits(sampledDest) - aFrameMetrics.GetScrollOffset()) * zoom;
+
+  if (finished) {
+    mApzc.mX.SetVelocity(0);
+    mApzc.mY.SetVelocity(0);
+  } else if (!IsZero(displacement)) {
+    // Velocity is measured in ParentLayerCoords / Milliseconds
+    float xVelocity = displacement.x / aDelta.ToMilliseconds();
+    float yVelocity = displacement.y / aDelta.ToMilliseconds();
+    mApzc.mX.SetVelocity(xVelocity);
+    mApzc.mY.SetVelocity(yVelocity);
+  }
+
+  // Note: we ignore overscroll for generic animations.
+  ParentLayerPoint adjustedOffset, overscroll;
+  mApzc.mX.AdjustDisplacement(displacement.x, adjustedOffset.x, overscroll.x);
+  mApzc.mY.AdjustDisplacement(displacement.y, adjustedOffset.y, overscroll.y,
+                              mForceVerticalOverscroll);
+
+  // If we expected to scroll, but there's no more scroll range on either axis,
+  // then end the animation early. Note that the initial displacement could be 0
+  // if the compositor ran very quickly (<1ms) after the animation was created.
+  // When that happens we want to make sure the animation continues.
+  if (!IsZero(displacement) && IsZero(adjustedOffset)) {
+    // Nothing more to do - end the animation.
+    return false;
+  }
+
+  aFrameMetrics.ScrollBy(adjustedOffset / zoom);
+  return !finished;
+}
+
+} // namespace layers
+} // namespace mozilla
new file mode 100644
--- /dev/null
+++ b/gfx/layers/apz/src/GenericScrollAnimation.h
@@ -0,0 +1,42 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set sw=2 ts=8 et tw=80 : */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#ifndef mozilla_layers_GenericScrollAnimation_h_
+#define mozilla_layers_GenericScrollAnimation_h_
+
+#include "AsyncPanZoomAnimation.h"
+#include "AsyncScrollBase.h"
+
+namespace mozilla {
+namespace layers {
+
+class AsyncPanZoomController;
+
+class GenericScrollAnimation
+  : public AsyncPanZoomAnimation,
+    public AsyncScrollBase
+{
+public:
+  GenericScrollAnimation(AsyncPanZoomController& aApzc,
+                         const nsPoint& aInitialPosition);
+
+  bool DoSample(FrameMetrics& aFrameMetrics, const TimeDuration& aDelta) override;
+  void UpdateDelta(TimeStamp aTime, nsPoint aDelta, const nsSize& aCurrentVelocity);
+
+  CSSPoint GetDestination() const {
+    return CSSPoint::FromAppUnits(mFinalDestination);
+  }
+
+protected:
+  AsyncPanZoomController& mApzc;
+  nsPoint mFinalDestination;
+  bool mForceVerticalOverscroll;
+};
+
+} // namespace layers
+} // namespace mozilla
+
+#endif // mozilla_layers_GenericScrollAnimation_h_
--- a/gfx/layers/apz/src/WheelScrollAnimation.cpp
+++ b/gfx/layers/apz/src/WheelScrollAnimation.cpp
@@ -11,93 +11,21 @@
 #include "nsPoint.h"
 
 namespace mozilla {
 namespace layers {
 
 WheelScrollAnimation::WheelScrollAnimation(AsyncPanZoomController& aApzc,
                                            const nsPoint& aInitialPosition,
                                            ScrollWheelInput::ScrollDeltaType aDeltaType)
-  : AsyncScrollBase(aInitialPosition)
-  , mApzc(aApzc)
-  , mFinalDestination(aInitialPosition)
-  , mDeltaType(aDeltaType)
-{
-}
-
-void
-WheelScrollAnimation::Update(TimeStamp aTime, nsPoint aDelta, const nsSize& aCurrentVelocity)
+  : GenericScrollAnimation(aApzc, aInitialPosition)
 {
-  InitPreferences(aTime);
-
-  mFinalDestination += aDelta;
-
-  // Clamp the final destination to the scrollable area.
-  CSSPoint clamped = CSSPoint::FromAppUnits(mFinalDestination);
-  clamped.x = mApzc.mX.ClampOriginToScrollableRect(clamped.x);
-  clamped.y = mApzc.mY.ClampOriginToScrollableRect(clamped.y);
-  mFinalDestination = CSSPoint::ToAppUnits(clamped);
-
-  AsyncScrollBase::Update(aTime, mFinalDestination, aCurrentVelocity);
-}
-
-bool
-WheelScrollAnimation::DoSample(FrameMetrics& aFrameMetrics, const TimeDuration& aDelta)
-{
-  TimeStamp now = mApzc.GetFrameTime();
-  CSSToParentLayerScale2D zoom = aFrameMetrics.GetZoom();
-
-  // If the animation is finished, make sure the final position is correct by
-  // using one last displacement. Otherwise, compute the delta via the timing
-  // function as normal.
-  bool finished = IsFinished(now);
-  nsPoint sampledDest = finished
-                        ? mDestination
-                        : PositionAt(now);
-  ParentLayerPoint displacement =
-    (CSSPoint::FromAppUnits(sampledDest) - aFrameMetrics.GetScrollOffset()) * zoom;
+  mForceVerticalOverscroll = !mApzc.mScrollMetadata.AllowVerticalScrollWithWheel();
 
-  if (finished) {
-    mApzc.mX.SetVelocity(0);
-    mApzc.mY.SetVelocity(0);
-  } else if (!IsZero(displacement)) {
-    // Velocity is measured in ParentLayerCoords / Milliseconds
-    float xVelocity = displacement.x / aDelta.ToMilliseconds();
-    float yVelocity = displacement.y / aDelta.ToMilliseconds();
-    mApzc.mX.SetVelocity(xVelocity);
-    mApzc.mY.SetVelocity(yVelocity);
-  }
-
-  // Note: we ignore overscroll for wheel animations.
-  ParentLayerPoint adjustedOffset, overscroll;
-  mApzc.mX.AdjustDisplacement(displacement.x, adjustedOffset.x, overscroll.x);
-  mApzc.mY.AdjustDisplacement(displacement.y, adjustedOffset.y, overscroll.y,
-                              !mApzc.mScrollMetadata.AllowVerticalScrollWithWheel());
-
-  // If we expected to scroll, but there's no more scroll range on either axis,
-  // then end the animation early. Note that the initial displacement could be 0
-  // if the compositor ran very quickly (<1ms) after the animation was created.
-  // When that happens we want to make sure the animation continues.
-  if (!IsZero(displacement) && IsZero(adjustedOffset)) {
-    // Nothing more to do - end the animation.
-    return false;
-  }
-
-  aFrameMetrics.ScrollBy(adjustedOffset / zoom);
-  return !finished;
-}
-
-void
-WheelScrollAnimation::InitPreferences(TimeStamp aTime)
-{
-  if (!mIsFirstIteration) {
-    return;
-  }
-
-  switch (mDeltaType) {
+  switch (aDeltaType) {
   case ScrollWheelInput::SCROLLDELTA_PAGE:
     mOriginMaxMS = clamped(gfxPrefs::PageSmoothScrollMaxDurationMs(), 0, 10000);
     mOriginMinMS = clamped(gfxPrefs::PageSmoothScrollMinDurationMs(), 0, mOriginMaxMS);
     break;
   case ScrollWheelInput::SCROLLDELTA_PIXEL:
     mOriginMaxMS = clamped(gfxPrefs::PixelSmoothScrollMaxDurationMs(), 0, 10000);
     mOriginMinMS = clamped(gfxPrefs::PixelSmoothScrollMinDurationMs(), 0, mOriginMaxMS);
     break;
@@ -107,14 +35,12 @@ WheelScrollAnimation::InitPreferences(Ti
     mOriginMaxMS = clamped(gfxPrefs::WheelSmoothScrollMaxDurationMs(), 0, 10000);
     mOriginMinMS = clamped(gfxPrefs::WheelSmoothScrollMinDurationMs(), 0, mOriginMaxMS);
     break;
   }
 
   // The pref is 100-based int percentage, while mIntervalRatio is 1-based ratio
   mIntervalRatio = ((double)gfxPrefs::SmoothScrollDurationToIntervalRatio()) / 100.0;
   mIntervalRatio = std::max(1.0, mIntervalRatio);
-
-  InitializeHistory(aTime);
 }
 
 } // namespace layers
 } // namespace mozilla
--- a/gfx/layers/apz/src/WheelScrollAnimation.h
+++ b/gfx/layers/apz/src/WheelScrollAnimation.h
@@ -2,50 +2,33 @@
 /* vim: set sw=2 ts=8 et tw=80 : */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #ifndef mozilla_layers_WheelScrollAnimation_h_
 #define mozilla_layers_WheelScrollAnimation_h_
 
-#include "AsyncPanZoomAnimation.h"
-#include "AsyncScrollBase.h"
+#include "GenericScrollAnimation.h"
 #include "InputData.h"
 
 namespace mozilla {
 namespace layers {
 
 class AsyncPanZoomController;
 
 class WheelScrollAnimation
-  : public AsyncPanZoomAnimation,
-    public AsyncScrollBase
+  : public GenericScrollAnimation
 {
 public:
   WheelScrollAnimation(AsyncPanZoomController& aApzc,
                        const nsPoint& aInitialPosition,
                        ScrollWheelInput::ScrollDeltaType aDeltaType);
 
-  bool DoSample(FrameMetrics& aFrameMetrics, const TimeDuration& aDelta) override;
-  void Update(TimeStamp aTime, nsPoint aDelta, const nsSize& aCurrentVelocity);
-
   WheelScrollAnimation* AsWheelScrollAnimation() override {
     return this;
   }
-
-  CSSPoint GetDestination() const {
-    return CSSPoint::FromAppUnits(mFinalDestination);
-  }
-
-private:
-  void InitPreferences(TimeStamp aTime);
-
-private:
-  AsyncPanZoomController& mApzc;
-  nsPoint mFinalDestination;
-  ScrollWheelInput::ScrollDeltaType mDeltaType;
 };
 
 } // namespace layers
 } // namespace mozilla
 
 #endif // mozilla_layers_WheelScrollAnimation_h_
--- a/gfx/layers/moz.build
+++ b/gfx/layers/moz.build
@@ -287,16 +287,17 @@ UNIFIED_SOURCES += [
     'apz/public/IAPZCTreeManager.cpp',
     'apz/src/APZCTreeManager.cpp',
     'apz/src/AsyncPanZoomController.cpp',
     'apz/src/Axis.cpp',
     'apz/src/CheckerboardEvent.cpp',
     'apz/src/DragTracker.cpp',
     'apz/src/FocusState.cpp',
     'apz/src/FocusTarget.cpp',
+    'apz/src/GenericScrollAnimation.cpp',
     'apz/src/GestureEventListener.cpp',
     'apz/src/HitTestingTreeNode.cpp',
     'apz/src/InputBlockState.cpp',
     'apz/src/InputQueue.cpp',
     'apz/src/Keyboard.cpp',
     'apz/src/OverscrollHandoffState.cpp',
     'apz/src/PotentialCheckerboardDurationTracker.cpp',
     'apz/src/QueuedInput.cpp',