Bug 1457586 - Implement AndroidVelocityTrakcer::HandleDynamicToolbarMovement() usefully. r=kats
authorBotond Ballo <botond@mozilla.com>
Fri, 05 Oct 2018 16:51:14 +0000
changeset 495575 77ace67c85de8eb18f5a364a2ffdd0f31e34454a
parent 495574 f266ca6d096a31445b53bcf314befaef06cae6c2
child 495576 67a3276d3b4e3ce09600ee0c060efa99f0717720
push id9984
push userffxbld-merge
push dateMon, 15 Oct 2018 21:07:35 +0000
treeherdermozilla-beta@183d27ea8570 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerskats
bugs1457586
milestone64.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1457586 - Implement AndroidVelocityTrakcer::HandleDynamicToolbarMovement() usefully. r=kats MozReview-Commit-ID: JYqiViaucmY Depends on D7659 Differential Revision: https://phabricator.services.mozilla.com/D7660
gfx/layers/apz/src/AndroidVelocityTracker.cpp
gfx/layers/apz/src/AndroidVelocityTracker.h
--- a/gfx/layers/apz/src/AndroidVelocityTracker.cpp
+++ b/gfx/layers/apz/src/AndroidVelocityTracker.cpp
@@ -28,16 +28,17 @@ static const uint8_t kDegree = 2;
 // This should be the degree of the approximation plus one.
 static const uint8_t kPolyDegree = kDegree + 1;
 
 // Maximum size of position history.
 static const uint8_t kHistorySize = 20;
 
 AndroidVelocityTracker::AndroidVelocityTracker()
   : mLastEventTime(0)
+  , mAdditionalDelta(0)
 {
 }
 
 void
 AndroidVelocityTracker::StartTracking(ParentLayerCoord aPos, uint32_t aTimestampMs)
 {
   Clear();
   mLastEventTime = aTimestampMs;
@@ -52,20 +53,20 @@ AndroidVelocityTracker::AddPosition(Pare
     Clear();
   }
 
   mLastEventTime = aTimestampMs;
 
   // If we are axis-locked, adjust the position to reflect the fact that
   // no movement is happening.
   if (aIsAxisLocked && !mHistory.IsEmpty()) {
-    aPos = mHistory[mHistory.Length() - 1].second;
+    aPos = mHistory[mHistory.Length() - 1].second - mAdditionalDelta;
   }
 
-  mHistory.AppendElement(std::make_pair(aTimestampMs, aPos));
+  mHistory.AppendElement(std::make_pair(aTimestampMs, aPos + mAdditionalDelta));
   if (mHistory.Length() > kHistorySize) {
     mHistory.RemoveElementAt(0);
   }
 
   if (mHistory.Length() < 2) {
     return Nothing();
   }
 
@@ -74,17 +75,26 @@ AndroidVelocityTracker::AddPosition(Pare
   return Some((end.second - start.second) / (end.first - start.first));
 }
 
 float
 AndroidVelocityTracker::HandleDynamicToolbarMovement(uint32_t aStartTimestampMs,
                                                      uint32_t aEndTimestampMs,
                                                      ParentLayerCoord aDelta)
 {
-  // TODO: Implement fully.
+  // If the dynamic toolbar is moving, the page content is moving relative
+  // to the screen. The positions passed to AddPosition() reflect the position
+  // of the finger relative to the page content, but we want the velocity we
+  // compute to be based on the physical movement of the finger (that is, its
+  // position relative to the screen). To accomplish this, we maintain
+  // |mAdditionalDelta|, a delta representing the amount by which the page has
+  // moved relative to the screen, and add it to every position recorded in
+  // the history in AddPosition().
+  mAdditionalDelta += aDelta;
+
   float timeDelta = aEndTimestampMs - aStartTimestampMs;
   MOZ_ASSERT(timeDelta != 0);
   return aDelta / timeDelta;
 }
 
 static float VectorDot(const float* a, const float* b, uint32_t m) {
   float r = 0;
   while (m--) {
@@ -295,14 +305,15 @@ AndroidVelocityTracker::ComputeVelocity(
   // touch positions, and the direction of scrolling is opposite to the
   // direction of the finger's movement.
   return -velocity / 1000.0f;  // convert to pixels per millisecond
 }
 
 void
 AndroidVelocityTracker::Clear()
 {
+  mAdditionalDelta = 0;
   mHistory.Clear();
 }
 
 }
 }
 
--- a/gfx/layers/apz/src/AndroidVelocityTracker.h
+++ b/gfx/layers/apz/src/AndroidVelocityTracker.h
@@ -32,14 +32,17 @@ public:
   void Clear() override;
 private:
   // A queue of (timestamp, position) pairs; these are the historical
   // positions at the given timestamps. Timestamps are in milliseconds.
   nsTArray<std::pair<uint32_t, ParentLayerCoord>> mHistory;
   // The last time an event was added to the tracker (in milliseconds),
   // or zero if no events have been added.
   uint32_t mLastEventTime;
+  // The amount by which the page has moved relative to the screen (caused
+  // by dynamic toolbar movement) since we have started tracking velocity.
+  ParentLayerCoord mAdditionalDelta;
 };
 
 } // namespace layers
 } // namespace mozilla
 
 #endif