Skip to content

Commit 8a90e6e

Browse files
author
Jeff Brown
committed
Minor refactoring before starting on velocity tracker changes.
Bug: 6413587 Change-Id: I5eba2bb57193bff78cb3740de5f87aca0b31d154
1 parent 2f09576 commit 8a90e6e

File tree

11 files changed

+811
-685
lines changed

11 files changed

+811
-685
lines changed

core/java/android/view/VelocityTracker.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,24 @@ public float estimateY(float time) {
298298
return estimate(time, yCoeff);
299299
}
300300

301+
/**
302+
* Gets the X coefficient with the specified index.
303+
* @param index The index of the coefficient to return.
304+
* @return The X coefficient, or 0 if the index is greater than the degree.
305+
*/
306+
public float getXCoeff(int index) {
307+
return index <= degree ? xCoeff[index] : 0;
308+
}
309+
310+
/**
311+
* Gets the Y coefficient with the specified index.
312+
* @param index The index of the coefficient to return.
313+
* @return The Y coefficient, or 0 if the index is greater than the degree.
314+
*/
315+
public float getYCoeff(int index) {
316+
return index <= degree ? yCoeff[index] : 0;
317+
}
318+
301319
private float estimate(float time, float[] c) {
302320
float a = 0;
303321
float scale = 1;

core/jni/android_view_VelocityTracker.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <android_runtime/AndroidRuntime.h>
2222
#include <utils/Log.h>
2323
#include <androidfw/Input.h>
24+
#include <androidfw/VelocityTracker.h>
2425
#include "android_view_MotionEvent.h"
2526

2627

include/androidfw/Input.h

Lines changed: 0 additions & 177 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
#include <utils/Timers.h>
2828
#include <utils/RefBase.h>
2929
#include <utils/String8.h>
30-
#include <utils/BitSet.h>
3130

3231
#ifdef HAVE_ANDROID_OS
3332
class SkMatrix;
@@ -607,182 +606,6 @@ class PooledInputEventFactory : public InputEventFactoryInterface {
607606
Vector<MotionEvent*> mMotionEventPool;
608607
};
609608

610-
/*
611-
* Calculates the velocity of pointer movements over time.
612-
*/
613-
class VelocityTracker {
614-
public:
615-
// Default polynomial degree. (used by getVelocity)
616-
static const uint32_t DEFAULT_DEGREE = 2;
617-
618-
// Default sample horizon. (used by getVelocity)
619-
// We don't use too much history by default since we want to react to quick
620-
// changes in direction.
621-
static const nsecs_t DEFAULT_HORIZON = 100 * 1000000; // 100 ms
622-
623-
struct Position {
624-
float x, y;
625-
};
626-
627-
struct Estimator {
628-
static const size_t MAX_DEGREE = 2;
629-
630-
// Polynomial coefficients describing motion in X and Y.
631-
float xCoeff[MAX_DEGREE + 1], yCoeff[MAX_DEGREE + 1];
632-
633-
// Polynomial degree (number of coefficients), or zero if no information is
634-
// available.
635-
uint32_t degree;
636-
637-
// Confidence (coefficient of determination), between 0 (no fit) and 1 (perfect fit).
638-
float confidence;
639-
640-
inline void clear() {
641-
degree = 0;
642-
confidence = 0;
643-
for (size_t i = 0; i <= MAX_DEGREE; i++) {
644-
xCoeff[i] = 0;
645-
yCoeff[i] = 0;
646-
}
647-
}
648-
};
649-
650-
VelocityTracker();
651-
652-
// Resets the velocity tracker state.
653-
void clear();
654-
655-
// Resets the velocity tracker state for specific pointers.
656-
// Call this method when some pointers have changed and may be reusing
657-
// an id that was assigned to a different pointer earlier.
658-
void clearPointers(BitSet32 idBits);
659-
660-
// Adds movement information for a set of pointers.
661-
// The idBits bitfield specifies the pointer ids of the pointers whose positions
662-
// are included in the movement.
663-
// The positions array contains position information for each pointer in order by
664-
// increasing id. Its size should be equal to the number of one bits in idBits.
665-
void addMovement(nsecs_t eventTime, BitSet32 idBits, const Position* positions);
666-
667-
// Adds movement information for all pointers in a MotionEvent, including historical samples.
668-
void addMovement(const MotionEvent* event);
669-
670-
// Gets the velocity of the specified pointer id in position units per second.
671-
// Returns false and sets the velocity components to zero if there is
672-
// insufficient movement information for the pointer.
673-
bool getVelocity(uint32_t id, float* outVx, float* outVy) const;
674-
675-
// Gets a quadratic estimator for the movements of the specified pointer id.
676-
// Returns false and clears the estimator if there is no information available
677-
// about the pointer.
678-
bool getEstimator(uint32_t id, uint32_t degree, nsecs_t horizon,
679-
Estimator* outEstimator) const;
680-
681-
// Gets the active pointer id, or -1 if none.
682-
inline int32_t getActivePointerId() const { return mActivePointerId; }
683-
684-
// Gets a bitset containing all pointer ids from the most recent movement.
685-
inline BitSet32 getCurrentPointerIdBits() const { return mMovements[mIndex].idBits; }
686-
687-
private:
688-
// Number of samples to keep.
689-
static const uint32_t HISTORY_SIZE = 20;
690-
691-
struct Movement {
692-
nsecs_t eventTime;
693-
BitSet32 idBits;
694-
Position positions[MAX_POINTERS];
695-
696-
inline const Position& getPosition(uint32_t id) const {
697-
return positions[idBits.getIndexOfBit(id)];
698-
}
699-
};
700-
701-
uint32_t mIndex;
702-
Movement mMovements[HISTORY_SIZE];
703-
int32_t mActivePointerId;
704-
};
705-
706-
707-
/*
708-
* Specifies parameters that govern pointer or wheel acceleration.
709-
*/
710-
struct VelocityControlParameters {
711-
// A scale factor that is multiplied with the raw velocity deltas
712-
// prior to applying any other velocity control factors. The scale
713-
// factor should be used to adapt the input device resolution
714-
// (eg. counts per inch) to the output device resolution (eg. pixels per inch).
715-
//
716-
// Must be a positive value.
717-
// Default is 1.0 (no scaling).
718-
float scale;
719-
720-
// The scaled speed at which acceleration begins to be applied.
721-
// This value establishes the upper bound of a low speed regime for
722-
// small precise motions that are performed without any acceleration.
723-
//
724-
// Must be a non-negative value.
725-
// Default is 0.0 (no low threshold).
726-
float lowThreshold;
727-
728-
// The scaled speed at which maximum acceleration is applied.
729-
// The difference between highThreshold and lowThreshold controls
730-
// the range of speeds over which the acceleration factor is interpolated.
731-
// The wider the range, the smoother the acceleration.
732-
//
733-
// Must be a non-negative value greater than or equal to lowThreshold.
734-
// Default is 0.0 (no high threshold).
735-
float highThreshold;
736-
737-
// The acceleration factor.
738-
// When the speed is above the low speed threshold, the velocity will scaled
739-
// by an interpolated value between 1.0 and this amount.
740-
//
741-
// Must be a positive greater than or equal to 1.0.
742-
// Default is 1.0 (no acceleration).
743-
float acceleration;
744-
745-
VelocityControlParameters() :
746-
scale(1.0f), lowThreshold(0.0f), highThreshold(0.0f), acceleration(1.0f) {
747-
}
748-
749-
VelocityControlParameters(float scale, float lowThreshold,
750-
float highThreshold, float acceleration) :
751-
scale(scale), lowThreshold(lowThreshold),
752-
highThreshold(highThreshold), acceleration(acceleration) {
753-
}
754-
};
755-
756-
/*
757-
* Implements mouse pointer and wheel speed control and acceleration.
758-
*/
759-
class VelocityControl {
760-
public:
761-
VelocityControl();
762-
763-
/* Sets the various parameters. */
764-
void setParameters(const VelocityControlParameters& parameters);
765-
766-
/* Resets the current movement counters to zero.
767-
* This has the effect of nullifying any acceleration. */
768-
void reset();
769-
770-
/* Translates a raw movement delta into an appropriately
771-
* scaled / accelerated delta based on the current velocity. */
772-
void move(nsecs_t eventTime, float* deltaX, float* deltaY);
773-
774-
private:
775-
// If no movements are received within this amount of time,
776-
// we assume the movement has stopped and reset the movement counters.
777-
static const nsecs_t STOP_TIME = 500 * 1000000; // 500 ms
778-
779-
VelocityControlParameters mParameters;
780-
781-
nsecs_t mLastMovementTime;
782-
VelocityTracker::Position mRawPosition;
783-
VelocityTracker mVelocityTracker;
784-
};
785-
786609
} // namespace android
787610

788611
#endif // _ANDROIDFW_INPUT_H
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
* Copyright (C) 2012 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef _ANDROIDFW_VELOCITY_CONTROL_H
18+
#define _ANDROIDFW_VELOCITY_CONTROL_H
19+
20+
#include <androidfw/Input.h>
21+
#include <androidfw/VelocityTracker.h>
22+
#include <utils/Timers.h>
23+
24+
namespace android {
25+
26+
/*
27+
* Specifies parameters that govern pointer or wheel acceleration.
28+
*/
29+
struct VelocityControlParameters {
30+
// A scale factor that is multiplied with the raw velocity deltas
31+
// prior to applying any other velocity control factors. The scale
32+
// factor should be used to adapt the input device resolution
33+
// (eg. counts per inch) to the output device resolution (eg. pixels per inch).
34+
//
35+
// Must be a positive value.
36+
// Default is 1.0 (no scaling).
37+
float scale;
38+
39+
// The scaled speed at which acceleration begins to be applied.
40+
// This value establishes the upper bound of a low speed regime for
41+
// small precise motions that are performed without any acceleration.
42+
//
43+
// Must be a non-negative value.
44+
// Default is 0.0 (no low threshold).
45+
float lowThreshold;
46+
47+
// The scaled speed at which maximum acceleration is applied.
48+
// The difference between highThreshold and lowThreshold controls
49+
// the range of speeds over which the acceleration factor is interpolated.
50+
// The wider the range, the smoother the acceleration.
51+
//
52+
// Must be a non-negative value greater than or equal to lowThreshold.
53+
// Default is 0.0 (no high threshold).
54+
float highThreshold;
55+
56+
// The acceleration factor.
57+
// When the speed is above the low speed threshold, the velocity will scaled
58+
// by an interpolated value between 1.0 and this amount.
59+
//
60+
// Must be a positive greater than or equal to 1.0.
61+
// Default is 1.0 (no acceleration).
62+
float acceleration;
63+
64+
VelocityControlParameters() :
65+
scale(1.0f), lowThreshold(0.0f), highThreshold(0.0f), acceleration(1.0f) {
66+
}
67+
68+
VelocityControlParameters(float scale, float lowThreshold,
69+
float highThreshold, float acceleration) :
70+
scale(scale), lowThreshold(lowThreshold),
71+
highThreshold(highThreshold), acceleration(acceleration) {
72+
}
73+
};
74+
75+
/*
76+
* Implements mouse pointer and wheel speed control and acceleration.
77+
*/
78+
class VelocityControl {
79+
public:
80+
VelocityControl();
81+
82+
/* Sets the various parameters. */
83+
void setParameters(const VelocityControlParameters& parameters);
84+
85+
/* Resets the current movement counters to zero.
86+
* This has the effect of nullifying any acceleration. */
87+
void reset();
88+
89+
/* Translates a raw movement delta into an appropriately
90+
* scaled / accelerated delta based on the current velocity. */
91+
void move(nsecs_t eventTime, float* deltaX, float* deltaY);
92+
93+
private:
94+
// If no movements are received within this amount of time,
95+
// we assume the movement has stopped and reset the movement counters.
96+
static const nsecs_t STOP_TIME = 500 * 1000000; // 500 ms
97+
98+
VelocityControlParameters mParameters;
99+
100+
nsecs_t mLastMovementTime;
101+
VelocityTracker::Position mRawPosition;
102+
VelocityTracker mVelocityTracker;
103+
};
104+
105+
} // namespace android
106+
107+
#endif // _ANDROIDFW_VELOCITY_CONTROL_H

0 commit comments

Comments
 (0)