From ba131617361f8c1bc228cb8a6d5ee426f9588628 Mon Sep 17 00:00:00 2001 From: wangzezhen Date: Wed, 24 Aug 2022 21:36:25 +0800 Subject: [PATCH] optimize pinchGesture Signed-off-by: wangzezhen Change-Id: Ie7abf00ecdba4ee67c04337e780864fc2ada34e1 --- frameworks/core/common/event_manager.cpp | 24 +++++++++++++++++++ frameworks/core/common/event_manager.h | 2 ++ frameworks/core/event/touch_event.h | 2 ++ .../core/gestures/exclusive_recognizer.cpp | 23 +++++++++++++----- .../core/gestures/exclusive_recognizer.h | 2 ++ .../core/gestures/parallel_recognizer.cpp | 14 +++++++++++ .../core/gestures/parallel_recognizer.h | 2 ++ frameworks/core/gestures/pinch_recognizer.cpp | 15 ++++++++++-- frameworks/core/gestures/pinch_recognizer.h | 3 +++ .../core/gestures/sequenced_recognizer.cpp | 14 +++++++++++ .../core/gestures/sequenced_recognizer.h | 2 ++ frameworks/core/pipeline/pipeline_context.cpp | 17 ++++++++++--- 12 files changed, 109 insertions(+), 11 deletions(-) diff --git a/frameworks/core/common/event_manager.cpp b/frameworks/core/common/event_manager.cpp index 735ccf1f4c6..9d900ded9c9 100644 --- a/frameworks/core/common/event_manager.cpp +++ b/frameworks/core/common/event_manager.cpp @@ -170,6 +170,30 @@ void EventManager::TouchTest( renderNode->TouchTest(point, point, touchRestrict, axisTouchTestResult_); } +void EventManager::FlushTouchEventsBegin(const std::list& touchEvents) +{ + for (auto iter = touchEvents.begin(); iter != touchEvents.end(); ++iter) { + const auto result = touchTestResults_.find((*iter).id); + if (result != touchTestResults_.end()) { + for (auto entry = result->second.rbegin(); entry != result->second.rend(); ++entry) { + (*entry)->OnFlushTouchEventsBegin(); + } + } + } +} + +void EventManager::FlushTouchEventsEnd(const std::list& touchEvents) +{ + for (auto iter = touchEvents.begin(); iter != touchEvents.end(); ++iter) { + const auto result = touchTestResults_.find((*iter).id); + if (result != touchTestResults_.end()) { + for (auto entry = result->second.rbegin(); entry != result->second.rend(); ++entry) { + (*entry)->OnFlushTouchEventsEnd(); + } + } + } +} + bool EventManager::DispatchTouchEvent(const TouchEvent& point) { ContainerScope scope(instanceId_); diff --git a/frameworks/core/common/event_manager.h b/frameworks/core/common/event_manager.h index 9cb930a38ab..77f9a5376a6 100644 --- a/frameworks/core/common/event_manager.h +++ b/frameworks/core/common/event_manager.h @@ -69,6 +69,8 @@ public: bool DispatchTouchEvent(const TouchEvent& point); bool DispatchTouchEvent(const AxisEvent& event); + void FlushTouchEventsBegin(const std::list& touchEvents); + void FlushTouchEventsEnd(const std::list& touchEvents); // Distribute the key event to the corresponding root node. If the root node is not processed, return false and the // platform will handle it. diff --git a/frameworks/core/event/touch_event.h b/frameworks/core/event/touch_event.h index d255be152f0..1191e7f9cd1 100644 --- a/frameworks/core/event/touch_event.h +++ b/frameworks/core/event/touch_event.h @@ -360,6 +360,8 @@ public: { return true; } + virtual void OnFlushTouchEventsBegin() {} + virtual void OnFlushTouchEventsEnd() {} void SetTouchRestrict(const TouchRestrict& touchRestrict) { diff --git a/frameworks/core/gestures/exclusive_recognizer.cpp b/frameworks/core/gestures/exclusive_recognizer.cpp index b487d334a49..bca3ca0bb04 100644 --- a/frameworks/core/gestures/exclusive_recognizer.cpp +++ b/frameworks/core/gestures/exclusive_recognizer.cpp @@ -20,9 +20,8 @@ #include "base/geometry/offset.h" #include "base/log/log.h" #include "base/memory/ace_type.h" +#include "core/gestures/click_recognizer.h" #include "core/gestures/gesture_referee.h" -#include "core/gestures/pan_recognizer.h" -#include "core/gestures/pinch_recognizer.h" namespace OHOS::Ace { @@ -51,11 +50,9 @@ void ExclusiveRecognizer::OnAccepted(size_t touchId) } } - if (AceType::InstanceOf(activeRecognizer_) || - AceType::InstanceOf(activeRecognizer_)) { - return; + if (AceType::InstanceOf(activeRecognizer_)) { + Reset(); } - Reset(); } void ExclusiveRecognizer::OnRejected(size_t touchId) @@ -121,6 +118,20 @@ bool ExclusiveRecognizer::HandleEvent(const TouchEvent& point) return true; } +void ExclusiveRecognizer::OnFlushTouchEventsBegin() +{ + for (auto& recognizer : recognizers_) { + recognizer->OnFlushTouchEventsBegin(); + } +} + +void ExclusiveRecognizer::OnFlushTouchEventsEnd() +{ + for (auto& recognizer : recognizers_) { + recognizer->OnFlushTouchEventsEnd(); + } +} + void ExclusiveRecognizer::BatchAdjudicate( const std::set& touchIds, const RefPtr& recognizer, GestureDisposal disposal) { diff --git a/frameworks/core/gestures/exclusive_recognizer.h b/frameworks/core/gestures/exclusive_recognizer.h index 5632032f4a6..c6c226ac381 100644 --- a/frameworks/core/gestures/exclusive_recognizer.h +++ b/frameworks/core/gestures/exclusive_recognizer.h @@ -50,6 +50,8 @@ public: void OnRejected(size_t touchId) override; void OnPending(size_t touchId) override; bool HandleEvent(const TouchEvent& point) override; + void OnFlushTouchEventsBegin() override; + void OnFlushTouchEventsEnd() override; private: void HandleTouchDownEvent(const TouchEvent& event) override {}; diff --git a/frameworks/core/gestures/parallel_recognizer.cpp b/frameworks/core/gestures/parallel_recognizer.cpp index 2a6bc31714c..3394fb57fe2 100644 --- a/frameworks/core/gestures/parallel_recognizer.cpp +++ b/frameworks/core/gestures/parallel_recognizer.cpp @@ -101,6 +101,20 @@ bool ParallelRecognizer::HandleEvent(const TouchEvent& point) return true; } +void ParallelRecognizer::OnFlushTouchEventsBegin() +{ + for (auto& recognizer : recognizers_) { + recognizer->OnFlushTouchEventsBegin(); + } +} + +void ParallelRecognizer::OnFlushTouchEventsEnd() +{ + for (auto& recognizer : recognizers_) { + recognizer->OnFlushTouchEventsEnd(); + } +} + bool ParallelRecognizer::IsRecognizeEnd(const RefPtr& recognizer) { DetectState currState = recognizer->GetDetectState(); diff --git a/frameworks/core/gestures/parallel_recognizer.h b/frameworks/core/gestures/parallel_recognizer.h index 2e18996a960..3965c06a23e 100644 --- a/frameworks/core/gestures/parallel_recognizer.h +++ b/frameworks/core/gestures/parallel_recognizer.h @@ -50,6 +50,8 @@ public: void OnRejected(size_t touchId) override; void OnPending(size_t touchId) override; bool HandleEvent(const TouchEvent& point) override; + void OnFlushTouchEventsBegin() override; + void OnFlushTouchEventsEnd() override; private: void HandleTouchDownEvent(const TouchEvent& event) override {}; diff --git a/frameworks/core/gestures/pinch_recognizer.cpp b/frameworks/core/gestures/pinch_recognizer.cpp index 79b52d3d151..94ca22ff209 100644 --- a/frameworks/core/gestures/pinch_recognizer.cpp +++ b/frameworks/core/gestures/pinch_recognizer.cpp @@ -31,7 +31,6 @@ constexpr int32_t AXIS_PINCH_FINGERS = 2; void PinchRecognizer::OnAccepted() { SendCallbackMsg(onActionStart_); - SendCallbackMsg(onActionUpdate_); if (pendingEnd_) { SendCallbackMsg(onActionEnd_); @@ -143,10 +142,22 @@ void PinchRecognizer::HandleTouchMoveEvent(const TouchEvent& event) } } else if (state_ == DetectState::DETECTED && refereeState_ == RefereeState::SUCCEED) { scale_ = currentDev_ / initialDev_; - SendCallbackMsg(onActionUpdate_); + if (isFlushTouchEventsEnd_) { + SendCallbackMsg(onActionUpdate_); + } } } +void PinchRecognizer::OnFlushTouchEventsBegin() +{ + isFlushTouchEventsEnd_ = false; +} + +void PinchRecognizer::OnFlushTouchEventsEnd() +{ + isFlushTouchEventsEnd_ = true; +} + void PinchRecognizer::HandleTouchMoveEvent(const AxisEvent& event) { LOGD("pinch recognizer receives touch move event"); diff --git a/frameworks/core/gestures/pinch_recognizer.h b/frameworks/core/gestures/pinch_recognizer.h index 500ca734f93..4ff0f62df83 100644 --- a/frameworks/core/gestures/pinch_recognizer.h +++ b/frameworks/core/gestures/pinch_recognizer.h @@ -51,6 +51,8 @@ private: void Reset(); void SendCallbackMsg(const std::unique_ptr& callback); Offset ComputePinchCenter(); + void OnFlushTouchEventsBegin() override; + void OnFlushTouchEventsEnd() override; double distance_ = 0.0; double initialDev_ = 0.0; @@ -61,6 +63,7 @@ private: std::map touchPoints_; bool pendingEnd_ = false; bool pendingCancel_ = false; + bool isFlushTouchEventsEnd_ = false; }; } // namespace OHOS::Ace diff --git a/frameworks/core/gestures/sequenced_recognizer.cpp b/frameworks/core/gestures/sequenced_recognizer.cpp index d632d32ca07..679637ba7f8 100644 --- a/frameworks/core/gestures/sequenced_recognizer.cpp +++ b/frameworks/core/gestures/sequenced_recognizer.cpp @@ -131,6 +131,20 @@ bool SequencedRecognizer::HandleEvent(const TouchEvent& point) return true; } +void SequencedRecognizer::OnFlushTouchEventsBegin() +{ + for (auto& recognizer : recognizers_) { + recognizer->OnFlushTouchEventsBegin(); + } +} + +void SequencedRecognizer::OnFlushTouchEventsEnd() +{ + for (auto& recognizer : recognizers_) { + recognizer->OnFlushTouchEventsEnd(); + } +} + void SequencedRecognizer::BatchAdjudicate( const std::set& touchIds, const RefPtr& recognizer, GestureDisposal disposal) { diff --git a/frameworks/core/gestures/sequenced_recognizer.h b/frameworks/core/gestures/sequenced_recognizer.h index 90c8d87b40c..98e74beef79 100644 --- a/frameworks/core/gestures/sequenced_recognizer.h +++ b/frameworks/core/gestures/sequenced_recognizer.h @@ -43,6 +43,8 @@ public: void OnRejected() override; bool HandleEvent(const TouchEvent& point) override; void OnPending(size_t touchId) override; + void OnFlushTouchEventsBegin() override; + void OnFlushTouchEventsEnd() override; private: void HandleTouchDownEvent(const TouchEvent& event) override {}; diff --git a/frameworks/core/pipeline/pipeline_context.cpp b/frameworks/core/pipeline/pipeline_context.cpp index 3a40a3f5777..2df9f52a63b 100644 --- a/frameworks/core/pipeline/pipeline_context.cpp +++ b/frameworks/core/pipeline/pipeline_context.cpp @@ -1670,20 +1670,31 @@ void PipelineContext::FlushTouchEvents() return; } { + eventManager_->FlushTouchEventsBegin(touchEvents_); std::unordered_set moveEventIds; decltype(touchEvents_) touchEvents(std::move(touchEvents_)); if (touchEvents.empty()) { return; } + std::list touchPoints; for (auto iter = touchEvents.rbegin(); iter != touchEvents.rend(); ++iter) { auto scalePoint = (*iter).CreateScalePoint(GetViewScale()); auto result = moveEventIds.emplace(scalePoint.id); if (result.second) { - ResSchedReport::GetInstance().DispatchTouchEventStart(scalePoint.type); - eventManager_->DispatchTouchEvent(scalePoint); - ResSchedReport::GetInstance().DispatchTouchEventEnd(); + touchPoints.emplace_front(scalePoint); } } + + auto maxSize = touchPoints.size(); + for (auto iter = touchPoints.rbegin(); iter != touchPoints.rend(); ++iter) { + maxSize--; + if (maxSize == 0) { + eventManager_->FlushTouchEventsEnd(touchPoints); + } + ResSchedReport::GetInstance().DispatchTouchEventStart((*iter).type); + eventManager_->DispatchTouchEvent(*iter); + ResSchedReport::GetInstance().DispatchTouchEventEnd(); + } } }