diff options
author | Frederik Gladhorn <frederik.gladhorn@theqtcompany.com> | 2016-07-11 22:30:12 +0200 |
---|---|---|
committer | Shawn Rutledge <shawn.rutledge@qt.io> | 2016-07-13 14:54:30 +0000 |
commit | eb3496167543753b6e8f5e85c69a1a77f95674b0 (patch) | |
tree | aa8adc97befebb121c059e0ac7acadbd125c6d83 /src/quick/items/qquickevents_p_p.h | |
parent | 6adc36115f4fc658fb907ee8af3013f2609ae761 (diff) |
QQuickPointerEvent: store pointCount separately
In 649b309ea9ed7d03aa74565d51edb416c23460d9 it was correct that the
pool did not need to be static, because the whole event is a singleton;
but it allowed the actual m_touchPoints vector to keep growing,
rather than resizing to the current number of points. When one finger
is pressed, then two, then one is released, pointCount must go from
1 -> 2 -> 1. And yet we'd like to avoid heap-allocating a fresh
QQuickEventTouchPoint object every time a touchpoint is pressed. The
choices are then to 1) exploit implementation details of QVector to
allow its count() to be different than its stored size but still
guarantee that every pointer it stores points to a long-lived
QQuickEventTouchPoint (the docs do say "Since Qt 5.6, resize() doesn't
shrink the capacity anymore"), which is what we thought we were doing;
2) store the pointCount in a separate variable, so that the prefix of
m_touchPoints are the valid ones while the remainder is the "pool", or
3) hold a separate reusable pool of touchpoints. The separate
m_pointCount is cheaper than the pool, so let's go with option 2.
Change-Id: Ic59b5393e9f4b05e4ec27db15d6f06253aa820d5
Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
Reviewed-by: Frederik Gladhorn <frederik.gladhorn@qt.io>
Diffstat (limited to 'src/quick/items/qquickevents_p_p.h')
-rw-r--r-- | src/quick/items/qquickevents_p_p.h | 10 |
1 files changed, 8 insertions, 2 deletions
diff --git a/src/quick/items/qquickevents_p_p.h b/src/quick/items/qquickevents_p_p.h index ae55f2f399..0ce395d7df 100644 --- a/src/quick/items/qquickevents_p_p.h +++ b/src/quick/items/qquickevents_p_p.h @@ -395,6 +395,7 @@ public: , m_event(nullptr) , m_button(Qt::NoButton) , m_pressedButtons(Qt::NoButton) + , m_pointCount(0) , m_mousePoint(nullptr) { } /** Reset the current event to \a ev. @@ -424,11 +425,15 @@ public: bool isTabletEvent() const; bool isValid() const { return m_event != nullptr; } - int pointCount() const { return isTouchEvent() ? m_touchPoints.count() : 1; } + int pointCount() const { return m_pointCount; } const QQuickEventPoint *point(int i) const { + if (Q_UNLIKELY(i < 0 || i >= m_pointCount)) + return nullptr; if (isTouchEvent()) return m_touchPoints.at(i); - return i == 0 ? m_mousePoint : nullptr; + if (isMouseEvent()) + return m_mousePoint; + return nullptr; } const QTouchEvent::TouchPoint *touchPointById(int pointId) const; @@ -440,6 +445,7 @@ protected: QInputEvent *m_event; // original event as received by QQuickWindow Qt::MouseButton m_button; Qt::MouseButtons m_pressedButtons; + int m_pointCount; QVector<QQuickEventTouchPoint *> m_touchPoints; QQuickEventPoint *m_mousePoint; |