Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 2a317774 authored by Android Build Coastguard Worker's avatar Android Build Coastguard Worker
Browse files

Snap for 9495384 from 68950fb9 to udc-release

Change-Id: I88a7aa64f3bba608f011d83f54fc552a4a2954d0
parents 4f1fb448 68950fb9
Loading
Loading
Loading
Loading
+49 −78
Original line number Diff line number Diff line
@@ -51,33 +51,24 @@ public:
        static const size_t MAX_DEGREE = 4;

        // Estimator time base.
        nsecs_t time;
        nsecs_t time = 0;

        // Polynomial coefficients describing motion.
        float coeff[MAX_DEGREE + 1];
        std::array<float, MAX_DEGREE + 1> coeff{};

        // Polynomial degree (number of coefficients), or zero if no information is
        // available.
        uint32_t degree;
        uint32_t degree = 0;

        // Confidence (coefficient of determination), between 0 (no fit) and 1 (perfect fit).
        float confidence;

        inline void clear() {
            time = 0;
            degree = 0;
            confidence = 0;
            for (size_t i = 0; i <= MAX_DEGREE; i++) {
                coeff[i] = 0;
            }
        }
        float confidence = 0;
    };

    /*
     * Contains all available velocity data from a VelocityTracker.
     */
    struct ComputedVelocity {
        inline std::optional<float> getVelocity(int32_t axis, uint32_t id) const {
        inline std::optional<float> getVelocity(int32_t axis, int32_t id) const {
            const auto& axisVelocities = mVelocities.find(axis);
            if (axisVelocities == mVelocities.end()) {
                return {};
@@ -91,7 +82,7 @@ public:
            return axisIdVelocity->second;
        }

        inline void addVelocity(int32_t axis, uint32_t id, float velocity) {
        inline void addVelocity(int32_t axis, int32_t id, float velocity) {
            mVelocities[axis][id] = velocity;
        }

@@ -112,19 +103,13 @@ public:
    // Resets the velocity tracker state.
    void clear();

    // Resets the velocity tracker state for specific pointers.
    // Resets the velocity tracker state for a specific pointer.
    // Call this method when some pointers have changed and may be reusing
    // an id that was assigned to a different pointer earlier.
    void clearPointers(BitSet32 idBits);
    void clearPointer(int32_t pointerId);

    // Adds movement information for a set of pointers.
    // The idBits bitfield specifies the pointer ids of the pointers whose data points
    // are included in the movement.
    // The positions map contains a mapping of an axis to positions array.
    // The positions arrays contain information for each pointer in order by increasing id.
    // Each array's size should be equal to the number of one bits in idBits.
    void addMovement(nsecs_t eventTime, BitSet32 idBits,
                     const std::map<int32_t, std::vector<float>>& positions);
    // Adds movement information for a pointer for a specific axis
    void addMovement(nsecs_t eventTime, int32_t pointerId, int32_t axis, float position);

    // Adds movement information for all pointers in a MotionEvent, including historical samples.
    void addMovement(const MotionEvent* event);
@@ -132,7 +117,7 @@ public:
    // Returns the velocity of the specified pointer id and axis in position units per second.
    // Returns empty optional if there is insufficient movement information for the pointer, or if
    // the given axis is not supported for velocity tracking.
    std::optional<float> getVelocity(int32_t axis, uint32_t id) const;
    std::optional<float> getVelocity(int32_t axis, int32_t pointerId) const;

    // Returns a ComputedVelocity instance with all available velocity data, using the given units
    // (reference: units == 1 means "per millisecond"), and clamping each velocity between
@@ -142,15 +127,15 @@ public:
    // Gets an estimator for the recent movements of the specified pointer id for the given axis.
    // Returns false and clears the estimator if there is no information available
    // about the pointer.
    bool getEstimator(int32_t axis, uint32_t id, Estimator* outEstimator) const;
    std::optional<Estimator> getEstimator(int32_t axis, int32_t pointerId) const;

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

private:
    nsecs_t mLastEventTime;
    BitSet32 mCurrentPointerIdBits;
    int32_t mActivePointerId;
    std::optional<int32_t> mActivePointerId;

    // An override strategy passed in the constructor to be used for all axes.
    // This strategy will apply to all axes, unless the default strategy is specified here.
@@ -182,10 +167,9 @@ protected:
public:
    virtual ~VelocityTrackerStrategy() { }

    virtual void clearPointers(BitSet32 idBits) = 0;
    virtual void addMovement(nsecs_t eventTime, BitSet32 idBits,
                             const std::vector<float>& positions) = 0;
    virtual bool getEstimator(uint32_t id, VelocityTracker::Estimator* outEstimator) const = 0;
    virtual void clearPointer(int32_t pointerId) = 0;
    virtual void addMovement(nsecs_t eventTime, int32_t pointerId, float position) = 0;
    virtual std::optional<VelocityTracker::Estimator> getEstimator(int32_t pointerId) const = 0;
};


@@ -194,29 +178,28 @@ public:
 */
class LeastSquaresVelocityTrackerStrategy : public VelocityTrackerStrategy {
public:
    enum Weighting {
    enum class Weighting {
        // No weights applied.  All data points are equally reliable.
        WEIGHTING_NONE,
        NONE,

        // Weight by time delta.  Data points clustered together are weighted less.
        WEIGHTING_DELTA,
        DELTA,

        // Weight such that points within a certain horizon are weighed more than those
        // outside of that horizon.
        WEIGHTING_CENTRAL,
        CENTRAL,

        // Weight such that points older than a certain amount are weighed less.
        WEIGHTING_RECENT,
        RECENT,
    };

    // Degree must be no greater than Estimator::MAX_DEGREE.
    LeastSquaresVelocityTrackerStrategy(uint32_t degree, Weighting weighting = WEIGHTING_NONE);
    virtual ~LeastSquaresVelocityTrackerStrategy();
    LeastSquaresVelocityTrackerStrategy(uint32_t degree, Weighting weighting = Weighting::NONE);
    ~LeastSquaresVelocityTrackerStrategy() override;

    virtual void clearPointers(BitSet32 idBits);
    void addMovement(nsecs_t eventTime, BitSet32 idBits,
                     const std::vector<float>& positions) override;
    virtual bool getEstimator(uint32_t id, VelocityTracker::Estimator* outEstimator) const;
    void clearPointer(int32_t pointerId) override;
    void addMovement(nsecs_t eventTime, int32_t pointerId, float position) override;
    std::optional<VelocityTracker::Estimator> getEstimator(int32_t pointerId) const override;

private:
    // Sample horizon.
@@ -229,18 +212,15 @@ private:

    struct Movement {
        nsecs_t eventTime;
        BitSet32 idBits;
        float positions[MAX_POINTERS];

        inline float getPosition(uint32_t id) const { return positions[idBits.getIndexOfBit(id)]; }
        float position;
    };

    float chooseWeight(uint32_t index) const;
    float chooseWeight(int32_t pointerId, uint32_t index) const;

    const uint32_t mDegree;
    const Weighting mWeighting;
    uint32_t mIndex;
    Movement mMovements[HISTORY_SIZE];
    std::map<int32_t /*pointerId*/, size_t /*positionInArray*/> mIndex;
    std::map<int32_t /*pointerId*/, std::array<Movement, HISTORY_SIZE>> mMovements;
};


@@ -251,12 +231,11 @@ class IntegratingVelocityTrackerStrategy : public VelocityTrackerStrategy {
public:
    // Degree must be 1 or 2.
    IntegratingVelocityTrackerStrategy(uint32_t degree);
    ~IntegratingVelocityTrackerStrategy();
    ~IntegratingVelocityTrackerStrategy() override;

    virtual void clearPointers(BitSet32 idBits);
    void addMovement(nsecs_t eventTime, BitSet32 idBits,
                     const std::vector<float>& positions) override;
    virtual bool getEstimator(uint32_t id, VelocityTracker::Estimator* outEstimator) const;
    void clearPointer(int32_t pointerId) override;
    void addMovement(nsecs_t eventTime, int32_t pointerId, float positions) override;
    std::optional<VelocityTracker::Estimator> getEstimator(int32_t pointerId) const override;

private:
    // Current state estimate for a particular pointer.
@@ -283,12 +262,11 @@ private:
class LegacyVelocityTrackerStrategy : public VelocityTrackerStrategy {
public:
    LegacyVelocityTrackerStrategy();
    virtual ~LegacyVelocityTrackerStrategy();
    ~LegacyVelocityTrackerStrategy() override;

    virtual void clearPointers(BitSet32 idBits);
    void addMovement(nsecs_t eventTime, BitSet32 idBits,
                     const std::vector<float>& positions) override;
    virtual bool getEstimator(uint32_t id, VelocityTracker::Estimator* outEstimator) const;
    void clearPointer(int32_t pointerId) override;
    void addMovement(nsecs_t eventTime, int32_t pointerId, float position) override;
    std::optional<VelocityTracker::Estimator> getEstimator(int32_t pointerId) const override;

private:
    // Oldest sample to consider when calculating the velocity.
@@ -302,25 +280,21 @@ private:

    struct Movement {
        nsecs_t eventTime;
        BitSet32 idBits;
        float positions[MAX_POINTERS];

        inline float getPosition(uint32_t id) const { return positions[idBits.getIndexOfBit(id)]; }
        float position;
    };

    uint32_t mIndex;
    Movement mMovements[HISTORY_SIZE];
    std::map<int32_t /*pointerId*/, size_t /*positionInArray*/> mIndex;
    std::map<int32_t /*pointerId*/, std::array<Movement, HISTORY_SIZE>> mMovements;
};

class ImpulseVelocityTrackerStrategy : public VelocityTrackerStrategy {
public:
    ImpulseVelocityTrackerStrategy(bool deltaValues);
    virtual ~ImpulseVelocityTrackerStrategy();
    ~ImpulseVelocityTrackerStrategy() override;

    virtual void clearPointers(BitSet32 idBits);
    void addMovement(nsecs_t eventTime, BitSet32 idBits,
                     const std::vector<float>& positions) override;
    virtual bool getEstimator(uint32_t id, VelocityTracker::Estimator* outEstimator) const;
    void clearPointer(int32_t pointerId) override;
    void addMovement(nsecs_t eventTime, int32_t pointerId, float position) override;
    std::optional<VelocityTracker::Estimator> getEstimator(int32_t pointerId) const override;

private:
    // Sample horizon.
@@ -333,10 +307,7 @@ private:

    struct Movement {
        nsecs_t eventTime;
        BitSet32 idBits;
        float positions[MAX_POINTERS];

        inline float getPosition(uint32_t id) const { return positions[idBits.getIndexOfBit(id)]; }
        float position;
    };

    // Whether or not the input movement values for the strategy come in the form of delta values.
@@ -344,8 +315,8 @@ private:
    // velocity calculation.
    const bool mDeltaValues;

    size_t mIndex;
    Movement mMovements[HISTORY_SIZE];
    std::map<int32_t /*pointerId*/, size_t /*positionInArray*/> mIndex;
    std::map<int32_t /*pointerId*/, std::array<Movement, HISTORY_SIZE>> mMovements;
};

} // namespace android
+4 −3
Original line number Diff line number Diff line
@@ -70,9 +70,10 @@ void VelocityControl::move(nsecs_t eventTime, float* deltaX, float* deltaY) {
        if (deltaY) {
            mRawPositionY += *deltaY;
        }
        mVelocityTracker.addMovement(eventTime, BitSet32(BitSet32::valueForBit(0)),
                                     {{AMOTION_EVENT_AXIS_X, {mRawPositionX}},
                                      {AMOTION_EVENT_AXIS_Y, {mRawPositionY}}});
        mVelocityTracker.addMovement(eventTime, /*pointerId=*/0, AMOTION_EVENT_AXIS_X,
                                     mRawPositionX);
        mVelocityTracker.addMovement(eventTime, /*pointerId=*/0, AMOTION_EVENT_AXIS_Y,
                                     mRawPositionY);

        std::optional<float> vx = mVelocityTracker.getVelocity(AMOTION_EVENT_AXIS_X, 0);
        std::optional<float> vy = mVelocityTracker.getVelocity(AMOTION_EVENT_AXIS_Y, 0);
+337 −334

File changed.

Preview size limit exceeded, changes collapsed.

+8 −9
Original line number Diff line number Diff line
@@ -288,13 +288,13 @@ static void computeAndCheckQuadraticEstimate(const std::vector<PlanarMotionEvent
    for (MotionEvent event : events) {
        vt.addMovement(&event);
    }
    VelocityTracker::Estimator estimatorX;
    VelocityTracker::Estimator estimatorY;
    EXPECT_TRUE(vt.getEstimator(AMOTION_EVENT_AXIS_X, 0, &estimatorX));
    EXPECT_TRUE(vt.getEstimator(AMOTION_EVENT_AXIS_Y, 0, &estimatorY));
    std::optional<VelocityTracker::Estimator> estimatorX = vt.getEstimator(AMOTION_EVENT_AXIS_X, 0);
    std::optional<VelocityTracker::Estimator> estimatorY = vt.getEstimator(AMOTION_EVENT_AXIS_Y, 0);
    EXPECT_TRUE(estimatorX);
    EXPECT_TRUE(estimatorY);
    for (size_t i = 0; i< coefficients.size(); i++) {
        checkCoefficient(estimatorX.coeff[i], coefficients[i]);
        checkCoefficient(estimatorY.coeff[i], coefficients[i]);
        checkCoefficient((*estimatorX).coeff[i], coefficients[i]);
        checkCoefficient((*estimatorY).coeff[i], coefficients[i]);
    }
}

@@ -420,8 +420,7 @@ TEST_F(VelocityTrackerTest, TestApiInteractionsWithNoMotionEvents) {

    EXPECT_FALSE(vt.getVelocity(AMOTION_EVENT_AXIS_X, DEFAULT_POINTER_ID));

    VelocityTracker::Estimator estimator;
    EXPECT_FALSE(vt.getEstimator(AMOTION_EVENT_AXIS_X, DEFAULT_POINTER_ID, &estimator));
    EXPECT_FALSE(vt.getEstimator(AMOTION_EVENT_AXIS_X, DEFAULT_POINTER_ID));

    VelocityTracker::ComputedVelocity computedVelocity = vt.getComputedVelocity(1000, 1000);
    for (uint32_t id = 0; id <= MAX_POINTER_ID; id++) {
@@ -432,7 +431,7 @@ TEST_F(VelocityTrackerTest, TestApiInteractionsWithNoMotionEvents) {
    EXPECT_EQ(-1, vt.getActivePointerId());

    // Make sure that the clearing functions execute without an issue.
    vt.clearPointers(BitSet32(7U));
    vt.clearPointer(7U);
    vt.clear();
}

+4 −7
Original line number Diff line number Diff line
@@ -2703,18 +2703,15 @@ bool TouchInputMapper::preparePointerGestures(nsecs_t when, bool* outCancelPrevi

    // Update the velocity tracker.
    {
        std::vector<float> positionsX;
        std::vector<float> positionsY;
        for (BitSet32 idBits(mCurrentCookedState.fingerIdBits); !idBits.isEmpty();) {
            uint32_t id = idBits.clearFirstMarkedBit();
            const RawPointerData::Pointer& pointer =
                    mCurrentRawState.rawPointerData.pointerForId(id);
            positionsX.push_back(pointer.x * mPointerXMovementScale);
            positionsY.push_back(pointer.y * mPointerYMovementScale);
            const float x = pointer.x * mPointerXMovementScale;
            const float y = pointer.y * mPointerYMovementScale;
            mPointerGesture.velocityTracker.addMovement(when, id, AMOTION_EVENT_AXIS_X, x);
            mPointerGesture.velocityTracker.addMovement(when, id, AMOTION_EVENT_AXIS_Y, y);
        }
        mPointerGesture.velocityTracker.addMovement(when, mCurrentCookedState.fingerIdBits,
                                                    {{AMOTION_EVENT_AXIS_X, positionsX},
                                                     {AMOTION_EVENT_AXIS_Y, positionsY}});
    }

    // If the gesture ever enters a mode other than TAP, HOVER or TAP_DRAG, without first returning
Loading