diff options
author | picksi@chromium.org <picksi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-05-28 13:42:09 +0000 |
---|---|---|
committer | picksi@chromium.org <picksi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-05-28 13:42:09 +0000 |
commit | 8a3f8a5c27f34143d38f48e9556828480effe323 (patch) | |
tree | 29b71efe6d01ed52f690a354ec979f63cbc2c0bd /ui/events | |
parent | 67bfbce1a5b63fc56e5228e5711aa96882a3a08d (diff) | |
download | chromium_src-8a3f8a5c27f34143d38f48e9556828480effe323.zip chromium_src-8a3f8a5c27f34143d38f48e9556828480effe323.tar.gz chromium_src-8a3f8a5c27f34143d38f48e9556828480effe323.tar.bz2 |
Code previously duplicated three times has been wrapped into a function 'ResetScaleWithSpan()' for general good-practice and to reduce memory footprint.
Four divisions used for calculation of averages have been replaced by a single reciprocal generation and four multiplies for speed.
A repeated constant addition has been moved out of a for loop to improve speed and reduce (minor) rounding errors.
BUG=332418
Review URL: https://codereview.chromium.org/295903003
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@273201 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui/events')
-rw-r--r-- | ui/events/gesture_detection/scale_gesture_detector.cc | 40 | ||||
-rw-r--r-- | ui/events/gesture_detection/scale_gesture_detector.h | 2 |
2 files changed, 24 insertions, 18 deletions
diff --git a/ui/events/gesture_detection/scale_gesture_detector.cc b/ui/events/gesture_detection/scale_gesture_detector.cc index 232a70e..3f8ba17 100644 --- a/ui/events/gesture_detection/scale_gesture_detector.cc +++ b/ui/events/gesture_detection/scale_gesture_detector.cc @@ -110,13 +110,9 @@ bool ScaleGestureDetector::OnTouchEvent(const MotionEvent& event) { // This means the app probably didn't give us all the events. Shame on it. if (in_progress_) { listener_->OnScaleEnd(*this, event); - in_progress_ = false; - initial_span_ = 0; - double_tap_mode_ = DOUBLE_TAP_MODE_NONE; + ResetScaleWithSpan(0); } else if (InDoubleTapMode() && stream_complete) { - in_progress_ = false; - initial_span_ = 0; - double_tap_mode_ = DOUBLE_TAP_MODE_NONE; + ResetScaleWithSpan(0); } if (stream_complete) { @@ -135,7 +131,9 @@ bool ScaleGestureDetector::OnTouchEvent(const MotionEvent& event) { // Determine focal point. float sum_x = 0, sum_y = 0; const int count = static_cast<int>(event.GetPointerCount()); - const int div = pointer_up ? count - 1 : count; + const int unreleased_point_count = pointer_up ? count - 1 : count; + const float inverse_unreleased_point_count = 1.0f / unreleased_point_count; + float focus_x; float focus_y; if (InDoubleTapMode()) { @@ -156,8 +154,8 @@ bool ScaleGestureDetector::OnTouchEvent(const MotionEvent& event) { sum_y += event.GetY(i); } - focus_x = sum_x / div; - focus_y = sum_y / div; + focus_x = sum_x * inverse_unreleased_point_count; + focus_y = sum_y * inverse_unreleased_point_count; } AddTouchHistory(event); @@ -168,13 +166,15 @@ bool ScaleGestureDetector::OnTouchEvent(const MotionEvent& event) { if (skip_index == i) continue; - // Convert the resulting diameter into a radius. - const float touch_size = touch_history_last_accepted_ / 2; - dev_sum_x += std::abs(event.GetX(i) - focus_x) + touch_size; - dev_sum_y += std::abs(event.GetY(i) - focus_y) + touch_size; + dev_sum_x += std::abs(event.GetX(i) - focus_x); + dev_sum_y += std::abs(event.GetY(i) - focus_y); } - const float dev_x = dev_sum_x / div; - const float dev_y = dev_sum_y / div; + // Convert the resulting diameter into a radius, to include touch + // radius in overall deviation. + const float touch_size = touch_history_last_accepted_ / 2; + + const float dev_x = (dev_sum_x * inverse_unreleased_point_count) + touch_size; + const float dev_y = (dev_sum_y * inverse_unreleased_point_count) + touch_size; // Span is the average distance between touch points through the focal point; // i.e. the diameter of the circle with a radius of the average deviation from @@ -197,9 +197,7 @@ bool ScaleGestureDetector::OnTouchEvent(const MotionEvent& event) { if (!InDoubleTapMode() && in_progress_ && (span < min_span_ || config_changed)) { listener_->OnScaleEnd(*this, event); - in_progress_ = false; - initial_span_ = span; - double_tap_mode_ = DOUBLE_TAP_MODE_NONE; + ResetScaleWithSpan(span); } if (config_changed) { prev_span_x_ = curr_span_x_ = span_x; @@ -373,4 +371,10 @@ void ScaleGestureDetector::ClearTouchHistory() { touch_history_last_accepted_time_ = base::TimeTicks(); } +void ScaleGestureDetector::ResetScaleWithSpan(float span) { + in_progress_ = false; + initial_span_ = span; + double_tap_mode_ = DOUBLE_TAP_MODE_NONE; +} + } // namespace ui diff --git a/ui/events/gesture_detection/scale_gesture_detector.h b/ui/events/gesture_detection/scale_gesture_detector.h index 4464200..e2de266 100644 --- a/ui/events/gesture_detection/scale_gesture_detector.h +++ b/ui/events/gesture_detection/scale_gesture_detector.h @@ -111,6 +111,8 @@ class ScaleGestureDetector : public GestureDetector::SimpleGestureListener { void AddTouchHistory(const MotionEvent& ev); void ClearTouchHistory(); + void ResetScaleWithSpan(float span); + ScaleGestureListener* const listener_; Config config_; |