on xenial help sign in
Fix and refactor DirectionalDragArea::ActiveTouchesInfo

- Minimize constructions and destructions of ActiveTouchInfo structs - Fix mostRecentStartTime()

commit4bdd3ee40a8fb2f724635458dcfb5fc774930711 authorDaniel d'Andrada <daniel.dandrada@canonical.com> dateWed 23 Oct 2013, 11:09 (13 years ago) parented2e6b1
2 files changed+51−13
plugins/Ubuntu/Gestures/DirectionalDragArea.cpp+44−12 plugins/Ubuntu/Gestures/DirectionalDragArea.h+7−1
plugins/Ubuntu/Gestures/DirectionalDragArea.cpp+44−12
@@ -602,12 +602,12 @@ bool DirectionalDragArea::isWithinTouchCompositionWindow()
602602//************************** ActiveTouchesInfo **************************
603603
604604DirectionalDragArea::ActiveTouchesInfo::ActiveTouchesInfo(const SharedTimeSource &timeSource)
605− : m_timeSource(timeSource)
605+ : m_timeSource(timeSource), m_lastUsedIndex(-1)
606606{
607607 // Estimate of the maximum number of active touches we might reach.
608608 // Not a problem if it ends up being an underestimate as this is just
609609 // an optimization.
610− reserve(10);
610+ m_vector.resize(3);
611611}
612612
613613void DirectionalDragArea::ActiveTouchesInfo::update(QTouchEvent *event)
@@ -630,33 +630,65 @@ void DirectionalDragArea::ActiveTouchesInfo::update(QTouchEvent *event)
630630
631631void DirectionalDragArea::ActiveTouchesInfo::addTouchPoint(const QTouchEvent::TouchPoint &touchPoint)
632632{
633− ActiveTouchInfo activeTouchInfo;
633+ ActiveTouchInfo &activeTouchInfo = getEmptySlot();
634634 activeTouchInfo.id = touchPoint.id();
635635 activeTouchInfo.startTime = m_timeSource->msecsSinceReference();
636− append(activeTouchInfo);
637636}
638637
639638void DirectionalDragArea::ActiveTouchesInfo::removeTouchPoint(const QTouchEvent::TouchPoint &touchPoint)
640639{
641− for (int i = 0; i < count(); ++i) {
642− if (touchPoint.id() == at(i).id) {
643− remove(i);
640+ for (int i = 0; i <= m_lastUsedIndex; ++i) {
641+ if (touchPoint.id() == m_vector.at(i).id) {
642+ freeSlot(i);
644643 return;
645644 }
646645 }
647646 Q_ASSERT(false); // shouldn't reach this point
648647}
649648
649+DirectionalDragArea::ActiveTouchInfo &DirectionalDragArea::ActiveTouchesInfo::getEmptySlot()
650+{
651+ Q_ASSERT(m_lastUsedIndex < m_vector.size());
652+
653+ // Look for an in-between vacancy first
654+ for (int i = 0; i < m_lastUsedIndex; ++i) {
655+ ActiveTouchInfo &activeTouchInfo = m_vector[i];
656+ if (!activeTouchInfo.isValid()) {
657+ return activeTouchInfo;
658+ }
659+ }
660+
661+ ++m_lastUsedIndex;
662+ if (m_lastUsedIndex >= m_vector.size()) {
663+ m_vector.resize(m_lastUsedIndex + 1);
664+ }
665+
666+ return m_vector[m_lastUsedIndex];
667+}
668+
669+void DirectionalDragArea::ActiveTouchesInfo::freeSlot(int index)
670+{
671+ m_vector[index].reset();
672+ if (index == m_lastUsedIndex) {
673+ do {
674+ --m_lastUsedIndex;
675+ } while (m_lastUsedIndex >= 0 && !m_vector.at(m_lastUsedIndex).isValid());
676+ }
677+}
678+
650679qint64 DirectionalDragArea::ActiveTouchesInfo::mostRecentStartTime()
651680{
652− Q_ASSERT(count() > 0);
681+ Q_ASSERT(m_lastUsedIndex >= 0);
653682
654− qint64 highestStartTime;
655− int i = 0;
683+ qint64 highestStartTime = m_vector.at(0).startTime;
684+ int i = 1;
656685 do {
657− highestStartTime = at(i).startTime;
686+ const ActiveTouchInfo &activeTouchInfo = m_vector.at(i);
687+ if (activeTouchInfo.isValid() && activeTouchInfo.startTime > highestStartTime) {
688+ highestStartTime = activeTouchInfo.startTime;
689+ }
658690 ++i;
659− } while (i < count());
691+ } while (i < m_lastUsedIndex);
660692
661693 return highestStartTime;
662694}
plugins/Ubuntu/Gestures/DirectionalDragArea.h+7−1
@@ -263,19 +263,25 @@ private:
263263 struct ActiveTouchInfo {
264264 ActiveTouchInfo() : id(-1), startTime(-1) {}
265265 bool isValid() const { return id != -1; }
266+ void reset() { id = -1; }
266267 int id;
267268 qint64 startTime;
268269 };
269− class ActiveTouchesInfo : public QVector<struct ActiveTouchInfo> {
270+ class ActiveTouchesInfo {
270271 public:
271272 ActiveTouchesInfo(const UbuntuGestures::SharedTimeSource &timeSource);
272273 void update(QTouchEvent *event);
273274 ActiveTouchInfo &touchInfo(int id);
274275 qint64 mostRecentStartTime();
275276 UbuntuGestures::SharedTimeSource m_timeSource;
277+ bool isEmpty() const { return m_lastUsedIndex == -1; }
276278 private:
277279 void addTouchPoint(const QTouchEvent::TouchPoint &touchPoint);
280+ ActiveTouchInfo &getEmptySlot();
281+ void freeSlot(int index);
278282 void removeTouchPoint(const QTouchEvent::TouchPoint &touchPoint);
283+ QVector<struct ActiveTouchInfo> m_vector;
284+ int m_lastUsedIndex;
279285 } m_activeTouches;
280286};
281287