Bug 1457586 - Implement AndroidVelocityTrakcer::HandleDynamicToolbarMovement() usefully. r=kats
MozReview-Commit-ID: JYqiViaucmY
--- 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