OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef CONTENT_BROWSER_RENDERER_HOST_INPUT_GESTURES_VELOCITY_TRACKER_H_ |
| 6 #define CONTENT_BROWSER_RENDERER_HOST_INPUT_GESTURES_VELOCITY_TRACKER_H_ |
| 7 |
| 8 #include "base/basictypes.h" |
| 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/time/time.h" |
| 11 #include "content/browser/renderer_host/input/gestures/bitset_32.h" |
| 12 |
| 13 namespace content { |
| 14 |
| 15 class MotionEvent; |
| 16 class VelocityTrackerStrategy; |
| 17 |
| 18 namespace { |
| 19 struct Estimator; |
| 20 } |
| 21 |
| 22 // Port of VelocityTracker from Android |
| 23 // * platform/frameworks/native/include/input/VelocityTracker.h |
| 24 // * Change-Id: I4983db61b53e28479fc90d9211fafff68f7f49a6 |
| 25 // * Changes to this class should be cosmetic only, easing fork maintenance. |
| 26 class VelocityTracker { |
| 27 public: |
| 28 struct Position { |
| 29 float x, y; |
| 30 }; |
| 31 |
| 32 enum { |
| 33 MAX_POINTERS = 16, |
| 34 MAX_POINTER_ID = 31 |
| 35 }; |
| 36 |
| 37 // Creates a velocity tracker using the default strategy for the platform. |
| 38 VelocityTracker(); |
| 39 |
| 40 // Creates a velocity tracker using the specified strategy. |
| 41 // If strategy is NULL, uses the default strategy for the platform. |
| 42 explicit VelocityTracker(const char* strategy); |
| 43 |
| 44 ~VelocityTracker(); |
| 45 |
| 46 // Resets the velocity tracker state. |
| 47 void Clear(); |
| 48 |
| 49 // Adds movement information for all pointers in a MotionEvent, including |
| 50 // historical samples. |
| 51 void AddMovement(const MotionEvent& event); |
| 52 |
| 53 // Gets the velocity of the specified pointer id in position units per second. |
| 54 // Returns false and sets the velocity components to zero if there is |
| 55 // insufficient movement information for the pointer. |
| 56 bool GetVelocity(uint32_t id, float* outVx, float* outVy) const; |
| 57 |
| 58 // Gets the active pointer id, or -1 if none. |
| 59 inline int32_t GetActivePointerId() const { return active_pointer_id_; } |
| 60 |
| 61 // Gets a bitset containing all pointer ids from the most recent movement. |
| 62 inline BitSet32 GetCurrentPointerIdBits() const { |
| 63 return current_pointer_id_bits_; |
| 64 } |
| 65 |
| 66 private: |
| 67 // Resets the velocity tracker state for specific pointers. |
| 68 // Call this method when some pointers have changed and may be reusing |
| 69 // an id that was assigned to a different pointer earlier. |
| 70 void ClearPointers(BitSet32 id_bits); |
| 71 |
| 72 // Adds movement information for a set of pointers. |
| 73 // The id_bits bitfield specifies the pointer ids of the pointers whose |
| 74 // positions |
| 75 // are included in the movement. |
| 76 // The positions array contains position information for each pointer in order |
| 77 // by |
| 78 // increasing id. Its size should be equal to the number of one bits in |
| 79 // id_bits. |
| 80 void AddMovement(const base::TimeTicks& event_time, |
| 81 BitSet32 id_bits, |
| 82 const Position* positions); |
| 83 |
| 84 // Gets an estimator for the recent movements of the specified pointer id. |
| 85 // Returns false and clears the estimator if there is no information available |
| 86 // about the pointer. |
| 87 bool GetEstimator(uint32_t id, Estimator* out_estimator) const; |
| 88 |
| 89 bool ConfigureStrategy(const char* strategy); |
| 90 |
| 91 static VelocityTrackerStrategy* CreateStrategy(const char* strategy); |
| 92 |
| 93 base::TimeTicks last_event_time_; |
| 94 BitSet32 current_pointer_id_bits_; |
| 95 int32_t active_pointer_id_; |
| 96 scoped_ptr<VelocityTrackerStrategy> strategy_; |
| 97 }; |
| 98 |
| 99 // Implements a particular velocity tracker algorithm. |
| 100 class VelocityTrackerStrategy { |
| 101 public: |
| 102 virtual ~VelocityTrackerStrategy() {} |
| 103 |
| 104 virtual void Clear() = 0; |
| 105 virtual void ClearPointers(BitSet32 id_bits) = 0; |
| 106 virtual void AddMovement(const base::TimeTicks& event_time, |
| 107 BitSet32 id_bits, |
| 108 const VelocityTracker::Position* positions) = 0; |
| 109 virtual bool GetEstimator(uint32_t id, Estimator* out_estimator) const = 0; |
| 110 |
| 111 protected: |
| 112 VelocityTrackerStrategy() {} |
| 113 }; |
| 114 |
| 115 } // namespace content |
| 116 |
| 117 #endif // CONTENT_BROWSER_RENDERER_HOST_INPUT_GESTURES_VELOCITY_TRACKER_H_ |
OLD | NEW |