Skip to content

Commit 85bd0d6

Browse files
author
Jeff Brown
committed
More VelocityTracker refactoring.
Bug: 6413587 Change-Id: Ida1152e7a34d5fe5caab5e6b5e1bc79f6c7a25e6
1 parent 0d607fb commit 85bd0d6

File tree

5 files changed

+155
-85
lines changed

5 files changed

+155
-85
lines changed

core/java/android/view/VelocityTracker.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@ public void onReleased(VelocityTracker element) {
6060
private static native void nativeComputeCurrentVelocity(int ptr, int units, float maxVelocity);
6161
private static native float nativeGetXVelocity(int ptr, int id);
6262
private static native float nativeGetYVelocity(int ptr, int id);
63-
private static native boolean nativeGetEstimator(int ptr, int id,
64-
int degree, int horizonMillis, Estimator outEstimator);
63+
private static native boolean nativeGetEstimator(int ptr, int id, Estimator outEstimator);
6564

6665
/**
6766
* Retrieve a new VelocityTracker object to watch the velocity of a
@@ -227,21 +226,17 @@ public float getYVelocity(int id) {
227226
* this method.
228227
*
229228
* @param id Which pointer's velocity to return.
230-
* @param degree The desired polynomial degree. The actual estimator may have
231-
* a lower degree than what is requested here. If -1, uses the default degree.
232-
* @param horizonMillis The maximum age of the oldest sample to consider, in milliseconds.
233-
* If -1, uses the default horizon.
234229
* @param outEstimator The estimator to populate.
235230
* @return True if an estimator was obtained, false if there is no information
236231
* available about the pointer.
237232
*
238233
* @hide For internal use only. Not a final API.
239234
*/
240-
public boolean getEstimator(int id, int degree, int horizonMillis, Estimator outEstimator) {
235+
public boolean getEstimator(int id, Estimator outEstimator) {
241236
if (outEstimator == null) {
242237
throw new IllegalArgumentException("outEstimator must not be null");
243238
}
244-
return nativeGetEstimator(mPtr, id, degree, horizonMillis, outEstimator);
239+
return nativeGetEstimator(mPtr, id, outEstimator);
245240
}
246241

247242
/**

core/java/com/android/internal/widget/PointerLocationView.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ public void addPointerEvent(MotionEvent event) {
527527
ps.addTrace(coords.x, coords.y);
528528
ps.mXVelocity = mVelocity.getXVelocity(id);
529529
ps.mYVelocity = mVelocity.getYVelocity(id);
530-
mVelocity.getEstimator(id, -1, -1, ps.mEstimator);
530+
mVelocity.getEstimator(id, ps.mEstimator);
531531
ps.mToolType = event.getToolType(i);
532532
}
533533
}

core/jni/android_view_VelocityTracker.cpp

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ class VelocityTrackerState {
4848
void addMovement(const MotionEvent* event);
4949
void computeCurrentVelocity(int32_t units, float maxVelocity);
5050
void getVelocity(int32_t id, float* outVx, float* outVy);
51-
bool getEstimator(int32_t id, uint32_t degree, nsecs_t horizon,
52-
VelocityTracker::Estimator* outEstimator);
51+
bool getEstimator(int32_t id, VelocityTracker::Estimator* outEstimator);
5352

5453
private:
5554
struct Velocity {
@@ -129,9 +128,8 @@ void VelocityTrackerState::getVelocity(int32_t id, float* outVx, float* outVy) {
129128
}
130129
}
131130

132-
bool VelocityTrackerState::getEstimator(int32_t id, uint32_t degree, nsecs_t horizon,
133-
VelocityTracker::Estimator* outEstimator) {
134-
return mVelocityTracker.getEstimator(id, degree, horizon, outEstimator);
131+
bool VelocityTrackerState::getEstimator(int32_t id, VelocityTracker::Estimator* outEstimator) {
132+
return mVelocityTracker.getEstimator(id, outEstimator);
135133
}
136134

137135

@@ -186,14 +184,10 @@ static jfloat android_view_VelocityTracker_nativeGetYVelocity(JNIEnv* env, jclas
186184
}
187185

188186
static jboolean android_view_VelocityTracker_nativeGetEstimator(JNIEnv* env, jclass clazz,
189-
jint ptr, jint id, jint degree, jint horizonMillis, jobject outEstimatorObj) {
187+
jint ptr, jint id, jobject outEstimatorObj) {
190188
VelocityTrackerState* state = reinterpret_cast<VelocityTrackerState*>(ptr);
191189
VelocityTracker::Estimator estimator;
192-
bool result = state->getEstimator(id,
193-
degree < 0 ? VelocityTracker::DEFAULT_DEGREE : uint32_t(degree),
194-
horizonMillis < 0 ? VelocityTracker::DEFAULT_HORIZON :
195-
nsecs_t(horizonMillis) * 1000000L,
196-
&estimator);
190+
bool result = state->getEstimator(id, &estimator);
197191

198192
jfloatArray xCoeffObj = jfloatArray(env->GetObjectField(outEstimatorObj,
199193
gEstimatorClassInfo.xCoeff));
@@ -236,7 +230,7 @@ static JNINativeMethod gVelocityTrackerMethods[] = {
236230
"(II)F",
237231
(void*)android_view_VelocityTracker_nativeGetYVelocity },
238232
{ "nativeGetEstimator",
239-
"(IIIILandroid/view/VelocityTracker$Estimator;)Z",
233+
"(IILandroid/view/VelocityTracker$Estimator;)Z",
240234
(void*)android_view_VelocityTracker_nativeGetEstimator },
241235
};
242236

include/androidfw/VelocityTracker.h

Lines changed: 56 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,13 @@
2323

2424
namespace android {
2525

26+
class VelocityTrackerStrategy;
27+
2628
/*
2729
* Calculates the velocity of pointer movements over time.
2830
*/
2931
class VelocityTracker {
3032
public:
31-
// Default polynomial degree. (used by getVelocity)
32-
static const uint32_t DEFAULT_DEGREE = 2;
33-
34-
// Default sample horizon. (used by getVelocity)
35-
// We don't use too much history by default since we want to react to quick
36-
// changes in direction.
37-
static const nsecs_t DEFAULT_HORIZON = 100 * 1000000; // 100 ms
38-
3933
struct Position {
4034
float x, y;
4135
};
@@ -64,6 +58,8 @@ class VelocityTracker {
6458
};
6559

6660
VelocityTracker();
61+
VelocityTracker(VelocityTrackerStrategy* strategy);
62+
~VelocityTracker();
6763

6864
// Resets the velocity tracker state.
6965
void clear();
@@ -88,35 +84,80 @@ class VelocityTracker {
8884
// insufficient movement information for the pointer.
8985
bool getVelocity(uint32_t id, float* outVx, float* outVy) const;
9086

91-
// Gets a quadratic estimator for the movements of the specified pointer id.
87+
// Gets an estimator for the recent movements of the specified pointer id.
9288
// Returns false and clears the estimator if there is no information available
9389
// about the pointer.
94-
bool getEstimator(uint32_t id, uint32_t degree, nsecs_t horizon,
95-
Estimator* outEstimator) const;
90+
bool getEstimator(uint32_t id, Estimator* outEstimator) const;
9691

9792
// Gets the active pointer id, or -1 if none.
9893
inline int32_t getActivePointerId() const { return mActivePointerId; }
9994

10095
// Gets a bitset containing all pointer ids from the most recent movement.
101-
inline BitSet32 getCurrentPointerIdBits() const { return mMovements[mIndex].idBits; }
96+
inline BitSet32 getCurrentPointerIdBits() const { return mCurrentPointerIdBits; }
97+
98+
private:
99+
BitSet32 mCurrentPointerIdBits;
100+
int32_t mActivePointerId;
101+
VelocityTrackerStrategy* mStrategy;
102+
};
103+
104+
105+
/*
106+
* Implements a particular velocity tracker algorithm.
107+
*/
108+
class VelocityTrackerStrategy {
109+
protected:
110+
VelocityTrackerStrategy() { }
111+
112+
public:
113+
virtual ~VelocityTrackerStrategy() { }
114+
115+
virtual void clear() = 0;
116+
virtual void clearPointers(BitSet32 idBits) = 0;
117+
virtual void addMovement(nsecs_t eventTime, BitSet32 idBits,
118+
const VelocityTracker::Position* positions) = 0;
119+
virtual bool getEstimator(uint32_t id, VelocityTracker::Estimator* outEstimator) const = 0;
120+
};
121+
122+
123+
/*
124+
* Velocity tracker algorithm based on least-squares linear regression.
125+
*/
126+
class LeastSquaresVelocityTrackerStrategy : public VelocityTrackerStrategy {
127+
public:
128+
LeastSquaresVelocityTrackerStrategy();
129+
virtual ~LeastSquaresVelocityTrackerStrategy();
130+
131+
virtual void clear();
132+
virtual void clearPointers(BitSet32 idBits);
133+
virtual void addMovement(nsecs_t eventTime, BitSet32 idBits,
134+
const VelocityTracker::Position* positions);
135+
virtual bool getEstimator(uint32_t id, VelocityTracker::Estimator* outEstimator) const;
102136

103137
private:
138+
// Polynomial degree. Must be less than or equal to Estimator::MAX_DEGREE.
139+
static const uint32_t DEGREE = 2;
140+
141+
// Sample horizon.
142+
// We don't use too much history by default since we want to react to quick
143+
// changes in direction.
144+
static const nsecs_t HORIZON = 100 * 1000000; // 100 ms
145+
104146
// Number of samples to keep.
105147
static const uint32_t HISTORY_SIZE = 20;
106148

107149
struct Movement {
108150
nsecs_t eventTime;
109151
BitSet32 idBits;
110-
Position positions[MAX_POINTERS];
152+
VelocityTracker::Position positions[MAX_POINTERS];
111153

112-
inline const Position& getPosition(uint32_t id) const {
154+
inline const VelocityTracker::Position& getPosition(uint32_t id) const {
113155
return positions[idBits.getIndexOfBit(id)];
114156
}
115157
};
116158

117159
uint32_t mIndex;
118160
Movement mMovements[HISTORY_SIZE];
119-
int32_t mActivePointerId;
120161
};
121162

122163
} // namespace android

0 commit comments

Comments
 (0)