SimpleVelocityTracker.h (1935B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef mozilla_layers_VelocityTracker_h 8 #define mozilla_layers_VelocityTracker_h 9 10 #include <utility> 11 12 #include "Axis.h" 13 #include "mozilla/Attributes.h" 14 #include "nsTArray.h" 15 16 namespace mozilla { 17 namespace layers { 18 19 class SimpleVelocityTracker : public VelocityTracker { 20 public: 21 explicit SimpleVelocityTracker(Axis* aAxis); 22 void StartTracking(ParentLayerCoord aPos, TimeStamp aTimestamp) override; 23 Maybe<float> AddPosition(ParentLayerCoord aPos, 24 TimeStamp aTimestamp) override; 25 Maybe<float> ComputeVelocity(TimeStamp aTimestamp) override; 26 void Clear() override; 27 28 private: 29 void AddVelocityToQueue(TimeStamp aTimestamp, float aVelocity); 30 float ApplyFlingCurveToVelocity(float aVelocity) const; 31 32 // The Axis that uses this velocity tracker. 33 // This is a raw pointer because the Axis owns the velocity tracker 34 // by UniquePtr, so the velocity tracker cannot outlive the Axis. 35 Axis* MOZ_NON_OWNING_REF mAxis; 36 37 // A queue of (timestamp, velocity) pairs; these are the historical 38 // velocities at the given timestamps. Velocities are in screen pixels per ms. 39 // This member can only be accessed on the controller/UI thread. 40 nsTArray<std::pair<TimeStamp, float>> mVelocityQueue; 41 42 // mVelocitySampleTime and mVelocitySamplePos are the time and position 43 // used in the last velocity sampling. They get updated when a new sample is 44 // taken (which may not happen on every input event, if the time delta is too 45 // small). 46 TimeStamp mVelocitySampleTime; 47 ParentLayerCoord mVelocitySamplePos; 48 }; 49 50 } // namespace layers 51 } // namespace mozilla 52 53 #endif