2 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <dali/internal/update/gestures/scene-graph-pan-gesture.h>
25 #include <dali/internal/update/gestures/pan-gesture-profiling.h>
26 #include <dali/integration-api/debug.h>
40 // TODO: Experimental - for changing in code only:
41 const bool TEST_TUNE_ENABLE_OVERSHOOT_PROTECTION = false;
44 const int MAX_GESTURE_AGE = 200; ///< maximum age of a gesture before disallowing its use in algorithm TODO: Possibly make this configurable.
45 const float ACCELERATION_THRESHOLD = 0.1f; ///< minimum pan velocity change to trigger dynamic change of prediction amount.
46 const float OUTPUT_TIME_DIFFERENCE = ( 1000.0f / 60.0f ); ///< This is used to optionally override actual times if they make results worse.
47 const float ACCELERATION_SMOOTHING = 0.44f; ///< Smoothes acceleration changes from one frame to another.
48 const float ACCELERATION_CAP = 0.0004f;///< Limits acceleration changes from one frame to another.
50 // Defaults for Environment Variables:
53 const unsigned int DEFAULT_MAX_PREDICTION_AMOUNT = 32; ///< the upper bound of the range to clamp the prediction interpolation.
54 const unsigned int DEFAULT_MIN_PREDICTION_AMOUNT = 0; ///< the lower bound of the range to clamp the prediction interpolation.
55 const unsigned int DEFAULT_PREDICTION_AMOUNT_ADJUSTMENT = 2; ///< the amount of prediction interpolation to adjust (in milliseconds) each time when pan velocity changes.
58 const bool DEFAULT_USE_ACTUAL_TIMES = false; ///< Disable to optionally override actual times if they make results worse.
59 const int DEFAULT_INTERPOLATION_TIME_RANGE = 255; ///< Time into past history (ms) to use points to interpolate the first point.
60 const bool DEFAULT_SCALAR_ONLY_PREDICTION_ENABLED = false; ///< If enabled, prediction is done using velocity alone (no integration or acceleration).
61 const bool DEFAULT_TWO_POINT_PREDICTION_ENABLED = true; ///< If enabled, a second interpolated point is predicted and combined with the first to get more stable values.
62 const int DEFAULT_TWO_POINT_PAST_INTERPOLATE_TIME = 42; ///< The target time in the past to generate the second interpolated point.
63 const float DEFAULT_TWO_POINT_VELOCITY_BIAS = 0.35f; ///< The ratio of first and second interpolated points to use for velocity. 0.0f = 100% of first point. 1.0f = 100% of second point.
64 const float DEFAULT_TWO_POINT_ACCELERATION_BIAS = 0.10f; ///< The ratio of first and second interpolated points to use for acceleration. 0.0f = 100% of first point. 1.0f = 100% of second point.
65 const int DEFAULT_MULTITAP_SMOOTHING_RANGE = 34; ///< The range in time (ms) of points in the history to smooth the final output against.
67 // Prediction Modes 1 & 2.
68 const unsigned int DEFAULT_PREDICTION_AMOUNT[2] = { 5, 57 }; ///< how much to interpolate pan position and displacement from last vsync time (in milliseconds)
69 const float DEFAULT_SMOOTHING_AMOUNT[2] = { 0.25f, 0.23f }; ///< how much to smooth final result from last vsync time
71 } // unnamed namespace
73 const PanGesture::PredictionMode PanGesture::DEFAULT_PREDICTION_MODE = PanGesture::PREDICTION_NONE;
74 const int PanGesture::NUM_PREDICTION_MODES = PanGesture::PREDICTION_2 + 1;
76 const PanGesture::SmoothingMode PanGesture::DEFAULT_SMOOTHING_MODE = PanGesture::SMOOTHING_LAST_VALUE;
77 const int PanGesture::NUM_SMOOTHING_MODES = PanGesture::SMOOTHING_MULTI_TAP + 1;
79 PanGesture* PanGesture::New()
81 return new PanGesture();
84 PanGesture::~PanGesture()
89 void PanGesture::AddGesture( const Dali::PanGesture& gesture )
91 Dali::Mutex::ScopedLock lock( mMutex );
92 mGestures[ mWritePosition ] = gesture;
94 // Update our write position.
96 mWritePosition %= PAN_GESTURE_HISTORY;
99 void PanGesture::RemoveOldHistory(PanInfoHistory& panHistory, unsigned int currentTime, unsigned int maxAge, unsigned int minEvents)
101 PanInfoHistoryConstIter endIter = panHistory.end();
102 PanInfoHistoryIter iter = panHistory.begin();
103 while( iter != endIter && panHistory.size() > minEvents)
105 PanInfo currentGesture = *iter;
106 if( currentTime < currentGesture.time + maxAge )
110 iter = panHistory.erase(iter);
111 endIter = panHistory.end();
114 // dont want more than 5 previous predictions for smoothing
115 iter = mPredictionHistory.begin();
116 while( mPredictionHistory.size() > 1 && iter != mPredictionHistory.end() )
118 iter = mPredictionHistory.erase(iter);
122 void PanGesture::PredictionMode1(int eventsThisFrame, PanInfo& gestureOut, PanInfoHistory& panHistory, unsigned int lastVSyncTime, unsigned int nextVSyncTime)
124 RemoveOldHistory(panHistory, lastVSyncTime, MAX_GESTURE_AGE, 0);
125 size_t panHistorySize = panHistory.size();
126 if( panHistorySize == 0 )
128 // cant do any prediction without a history
132 PanInfoHistoryConstIter endIter = panHistory.end();
133 PanInfoHistoryIter iter = panHistory.begin();
134 Vector2 screenVelocity = gestureOut.screen.velocity;
135 Vector2 localVelocity = gestureOut.local.velocity;
136 Vector2 screenDisplacement = gestureOut.screen.displacement;
137 Vector2 localDisplacement = gestureOut.local.displacement;
139 bool havePreviousAcceleration = false;
140 bool previousVelocity = false;
141 float previousAccel = 0.0f;
142 unsigned int lastTime(0);
144 unsigned int interpolationTime = lastVSyncTime + mCurrentPredictionAmount;
146 if( interpolationTime > gestureOut.time ) // Guard against the rare case when gestureOut.time > (lastVSyncTime + mCurrentPredictionAmount)
148 interpolationTime -= gestureOut.time;
152 interpolationTime = 0u;
155 while( iter != endIter )
157 PanInfo currentGesture = *iter;
158 if( !previousVelocity )
160 // not yet set a previous velocity
161 screenVelocity = currentGesture.screen.velocity;
162 previousVelocity = true;
163 lastTime = currentGesture.time;
167 float previousValueWeight = ( static_cast< float >( MAX_GESTURE_AGE ) - (lastVSyncTime - lastTime) ) / static_cast< float >( MAX_GESTURE_AGE );
168 float velMag = currentGesture.screen.velocity.Length();
169 float velDiff = velMag - screenVelocity.Length();
170 float acceleration = 0.0f;
173 if (currentGesture.time > lastTime) // Guard against invalid timestamps
175 time = static_cast<float>( currentGesture.time - lastTime );
177 if( time > Math::MACHINE_EPSILON_1 )
179 acceleration = velDiff / time;
182 float newVelMag = 0.0f;
183 int currentInterpolation = interpolationTime;
184 if( !havePreviousAcceleration )
187 havePreviousAcceleration = true;
191 newVelMag = velMag + (((acceleration * (1.0f - previousValueWeight)) + (previousAccel * previousValueWeight)) * currentInterpolation);
194 if( velMag > Math::MACHINE_EPSILON_1 )
196 velMod = newVelMag / velMag;
198 gestureOut.screen.velocity = currentGesture.screen.velocity * velMod;
199 gestureOut.local.velocity = currentGesture.local.velocity * velMod;
200 screenDisplacement = gestureOut.screen.displacement + (gestureOut.screen.velocity * interpolationTime);
201 localDisplacement = gestureOut.local.displacement + (gestureOut.local.velocity * interpolationTime);
202 screenVelocity = currentGesture.screen.velocity;
203 localVelocity = currentGesture.local.velocity;
204 previousAccel = acceleration;
207 // gestureOut's position is currently equal to the last event's position and its displacement is equal to last frame's total displacement
208 // add interpolated distance and position to current
209 // work out interpolated velocity
210 gestureOut.screen.position = (gestureOut.screen.position - gestureOut.screen.displacement) + screenDisplacement;
211 gestureOut.local.position = (gestureOut.local.position - gestureOut.local.displacement) + localDisplacement;
212 gestureOut.screen.displacement = screenDisplacement;
213 gestureOut.local.displacement = localDisplacement;
214 gestureOut.time += interpolationTime;
217 void PanGesture::BlendPoints( PanInfo& gesture, PanInfo& lastGesture, float blendValue )
219 gesture.screen.position -= ( gesture.screen.position - lastGesture.screen.position ) * 0.5f * ( 1.0f - blendValue );
220 gesture.local.position -= ( gesture.local.position - lastGesture.local.position ) * 0.5f * ( 1.0f - blendValue );
221 // Make current displacement relative to previous update-frame now.
222 gesture.screen.displacement = gesture.screen.position - lastGesture.screen.position;
223 gesture.local.displacement = gesture.local.position - lastGesture.local.position;
224 // Calculate velocity relative to previous update-frame
225 float timeDifference( gesture.time - lastGesture.time );
226 gesture.screen.velocity = gesture.screen.displacement / timeDifference;
227 gesture.local.velocity = gesture.local.displacement / timeDifference;
230 bool PanGesture::ReadGestures( FrameGestureInfo& info, unsigned int currentTimestamp )
232 unsigned int previousReadPosition = 0;
233 bool eventFound = false;
234 info.frameGesture = mLastUnmodifiedGesture;
236 while( mReadPosition != mWritePosition )
238 // Copy the gesture first
239 PanInfo currentGesture( mGestures[mReadPosition] );
243 mProfiling->mRawData.push_back( PanGestureProfiling::Position( currentGesture.time, currentGesture.screen.position, currentGesture.screen.displacement, currentGesture.screen.velocity, currentGesture.state ) );
245 info.frameGesture.local.position = currentGesture.local.position;
246 info.frameGesture.local.velocity = currentGesture.local.velocity;
247 info.frameGesture.screen.position = currentGesture.screen.position;
248 info.frameGesture.screen.velocity = currentGesture.screen.velocity;
250 if( info.eventsThisFrame > 0 )
252 info.acceleration = currentGesture.screen.velocity.Length() - mGestures[previousReadPosition].screen.velocity.Length();
257 info.frameGesture.local.displacement = currentGesture.local.displacement;
258 info.frameGesture.screen.displacement = currentGesture.screen.displacement;
263 info.frameGesture.local.displacement += currentGesture.local.displacement;
264 info.frameGesture.screen.displacement += currentGesture.screen.displacement;
266 info.frameGesture.time = currentGesture.time;
268 // add event to history
269 mPanHistory.push_back( currentGesture );
270 if( currentGesture.state == Gesture::Started )
272 info.justStarted = true;
273 // clear just finished as we have started new pan
274 info.justFinished = false;
276 info.justFinished |= ( currentGesture.state == Gesture::Finished || currentGesture.state == Gesture::Cancelled );
278 // Update our read position.
279 previousReadPosition = mReadPosition;
280 ++info.eventsThisFrame;
282 mReadPosition %= PAN_GESTURE_HISTORY;
284 // This code does not determine if the data will be used.
288 bool PanGesture::ReadAndResampleGestures( FrameGestureInfo& info, unsigned int currentTimestamp )
290 PanInfo lastReadGesture;
291 Dali::Mutex::ScopedLock lock( mMutex );
292 while( mReadPosition != mWritePosition )
294 // Copy the gesture first
295 lastReadGesture = mGestures[mReadPosition];
298 mProfiling->mRawData.push_back( PanGestureProfiling::Position( lastReadGesture.time, lastReadGesture.screen.position,
299 lastReadGesture.screen.displacement, lastReadGesture.screen.velocity, lastReadGesture.state ) );
302 info.frameGesture.screen.position += lastReadGesture.screen.position;
303 info.frameGesture.local.position += lastReadGesture.local.position;
304 info.frameGesture.screen.velocity += lastReadGesture.screen.velocity;
305 info.frameGesture.local.velocity += lastReadGesture.local.velocity;
307 if( lastReadGesture.state == Gesture::Started )
309 // Clear just finished as we have started new pan.
310 info.justFinished = false;
311 info.justStarted = true;
315 info.justFinished |= ( lastReadGesture.state == Gesture::Finished || lastReadGesture.state == Gesture::Cancelled );
318 // Add event to history
319 mPanHistory.push_back( lastReadGesture );
321 // Update our read position.
322 ++info.eventsThisFrame;
324 mReadPosition %= PAN_GESTURE_HISTORY;
327 bool updateProperties = false;
328 if( info.eventsThisFrame > 0 )
330 // Some events were read this frame.
331 mTargetGesture = lastReadGesture;
333 if( info.eventsThisFrame > 1 )
335 info.frameGesture.screen.position /= info.eventsThisFrame;
336 info.frameGesture.local.position /= info.eventsThisFrame;
337 info.frameGesture.screen.velocity /= info.eventsThisFrame;
338 info.frameGesture.local.velocity /= info.eventsThisFrame;
340 info.frameGesture.screen.displacement = info.frameGesture.screen.position - mLastGesture.screen.position;
341 info.frameGesture.local.displacement = info.frameGesture.local.position - mLastGesture.local.position;
347 info.frameGesture.screen.displacement = lastReadGesture.screen.displacement;
348 info.frameGesture.local.displacement = lastReadGesture.local.displacement;
351 info.frameGesture.time = currentTimestamp;
353 updateProperties = true;
357 // 0 Events this frame.
360 mNotAtTarget = false;
361 info.frameGesture = mTargetGesture;
362 updateProperties = true;
366 info.frameGesture = mLastGesture;
370 return updateProperties;
373 bool PanGesture::UpdateProperties( unsigned int lastVSyncTime, unsigned int nextVSyncTime )
375 if( mPredictionMode == PREDICTION_2 )
377 // TODO: Have the two prediction modes share more behavior so some parts of mode 2 can
378 // be used with mode 1 etc. Needs code moving and more importantly testing.
379 return NewAlgorithm( lastVSyncTime, nextVSyncTime );
384 // clear current pan history
386 mPredictionHistory.clear();
389 FrameGestureInfo frameInfo;
390 bool updateProperties = false;
393 // If we are using a form of prediction, read all the input as-is.
394 if( mPredictionMode != PREDICTION_NONE )
396 // Read input required for prediction algorithms.
397 updateProperties = ReadGestures( frameInfo, lastVSyncTime );
401 // Read and resample input.
402 updateProperties = ReadAndResampleGestures( frameInfo, lastVSyncTime );
405 PanInfo frameGesture = frameInfo.frameGesture;
406 PanInfo unmodifiedGesture = frameGesture;
408 // Process input data.
409 mInGesture |= frameInfo.justStarted;
415 mProfiling->mLatestData.push_back( PanGestureProfiling::Position( lastVSyncTime, frameGesture.screen.position,
416 frameGesture.screen.displacement, frameGesture.screen.velocity, frameGesture.state ) );
419 // Perform prediction.
420 if( mPredictionMode == PREDICTION_1 )
422 // Dynamically change the prediction amount according to the pan velocity acceleration.
423 if( !frameInfo.justStarted )
425 if( frameInfo.eventsThisFrame <= 1 )
427 frameInfo.acceleration = frameGesture.screen.velocity.Length() - mLastUnmodifiedGesture.screen.velocity.Length();
430 // Ignore tiny velocity fluctuation to avoid unnecessary prediction amount change
431 if( fabsf( frameInfo.acceleration ) > ACCELERATION_THRESHOLD )
433 mCurrentPredictionAmount += mPredictionAmountAdjustment * ( frameInfo.acceleration > Math::MACHINE_EPSILON_0 ? 1.0f : -1.0f );
434 if( mCurrentPredictionAmount > mMaxPredictionAmount + mPredictionAmountAdjustment ) // Guard against unsigned int overflow
436 mCurrentPredictionAmount = 0;
442 if( !mPredictionAmountOverridden )
444 // If the prediction amount has not been modified, default to the correct amount for this algorithm.
445 mPredictionAmount = DEFAULT_PREDICTION_AMOUNT[0];
447 mCurrentPredictionAmount = mPredictionAmount; // Reset the prediction amount for each new gesture
450 mCurrentPredictionAmount = std::max( mMinPredictionAmount, std::min( mCurrentPredictionAmount, mMaxPredictionAmount ) );
452 // Calculate the delta of positions before the prediction
453 Vector2 deltaPosition = frameGesture.screen.position - mLastUnmodifiedGesture.screen.position;
455 // Make latest gesture equal to current gesture before interpolation
456 PredictionMode1( frameInfo.eventsThisFrame, frameGesture, mPanHistory, lastVSyncTime, nextVSyncTime );
458 // Calculate the delta of positions after the prediction.
459 Vector2 deltaPredictedPosition = frameGesture.screen.position - mLastGesture.screen.position;
461 // If the change in the prediction has a different sign than the change in the actual position,
462 // there is overshot (i.e. the current prediction is too large). Return the previous prediction
463 // to give the user's finger a chance to catch up with where we have panned to.
464 bool overshotXAxis = false;
465 bool overshotYAxis = false;
466 if( (deltaPosition.x > Math::MACHINE_EPSILON_0 && deltaPredictedPosition.x < Math::MACHINE_EPSILON_0 )
467 || (deltaPosition.x < Math::MACHINE_EPSILON_0 && deltaPredictedPosition.x > Math::MACHINE_EPSILON_0 ) )
469 overshotXAxis = true;
470 frameGesture.screen.position.x = mLastGesture.screen.position.x;
473 if( (deltaPosition.y > Math::MACHINE_EPSILON_0 && deltaPredictedPosition.y < Math::MACHINE_EPSILON_0 )
474 || (deltaPosition.y < Math::MACHINE_EPSILON_0 && deltaPredictedPosition.y > Math::MACHINE_EPSILON_0 ) )
476 overshotYAxis = true;
477 frameGesture.screen.position.y = mLastGesture.screen.position.y;
480 // If there is overshot in one axis, reduce the possible overshot in the other axis,
481 // and reduce the prediction amount so that it doesn't overshoot as easily next time.
482 if(overshotXAxis || overshotYAxis)
484 mCurrentPredictionAmount -= mPredictionAmountAdjustment;
485 if( mCurrentPredictionAmount > mMaxPredictionAmount + mPredictionAmountAdjustment ) // Guard against unsigned int overflow
487 mCurrentPredictionAmount = 0;
489 mCurrentPredictionAmount = std::max( mMinPredictionAmount, std::min( mCurrentPredictionAmount, mMaxPredictionAmount ) );
491 if( overshotXAxis && !overshotYAxis )
493 frameGesture.screen.position.y = ( mLastGesture.screen.position.y + frameGesture.screen.position.y ) * 0.5f;
496 if( overshotYAxis && !overshotXAxis )
498 frameGesture.screen.position.x = ( mLastGesture.screen.position.x + frameGesture.screen.position.x ) * 0.5f;
502 updateProperties = true;
505 // Perform smoothing.
506 switch( mSmoothingMode )
509 case SMOOTHING_MULTI_TAP:
512 // TODO: Old algorithm to be able to use multitap smoothing.
515 case SMOOTHING_LAST_VALUE:
517 if( !frameInfo.justStarted )
519 if( !mSmoothingAmountOverridden )
521 // If the smoothing amount has not been modified, default to the correct amount for this algorithm.
522 mSmoothingAmount = DEFAULT_SMOOTHING_AMOUNT[0];
524 BlendPoints( frameGesture, mLastGesture, mSmoothingAmount );
530 if( updateProperties )
532 // only update properties if event received
533 // set latest gesture to raw pan info with unchanged time
534 mPanning.Set( mInGesture & !frameInfo.justFinished );
535 mScreenPosition.Set( frameGesture.screen.position );
536 mScreenDisplacement.Set( frameGesture.screen.displacement );
537 mScreenVelocity.Set( frameGesture.screen.velocity );
538 mLocalPosition.Set( frameGesture.local.position );
539 mLocalDisplacement.Set( frameGesture.local.displacement );
540 mLocalVelocity.Set( frameGesture.local.velocity );
545 mProfiling->mAveragedData.push_back( PanGestureProfiling::Position( frameGesture.time, frameGesture.screen.position,
546 frameGesture.screen.displacement, frameGesture.screen.velocity, frameGesture.state ) );
550 mLastGesture = frameGesture;
551 mLastUnmodifiedGesture = unmodifiedGesture;
553 mInGesture &= ~frameInfo.justFinished;
554 if( mProfiling && frameInfo.justFinished )
556 mProfiling->PrintData();
557 mProfiling->ClearData();
560 return updateProperties;
563 const GesturePropertyBool& PanGesture::GetPanningProperty() const
568 const GesturePropertyVector2& PanGesture::GetScreenPositionProperty() const
570 return mScreenPosition;
573 const GesturePropertyVector2& PanGesture::GetScreenVelocityProperty() const
575 return mScreenVelocity;
578 const GesturePropertyVector2& PanGesture::GetScreenDisplacementProperty() const
580 return mScreenDisplacement;
583 const GesturePropertyVector2& PanGesture::GetLocalPositionProperty() const
585 return mLocalPosition;
588 const GesturePropertyVector2& PanGesture::GetLocalDisplacementProperty() const
590 return mLocalDisplacement;
593 const GesturePropertyVector2& PanGesture::GetLocalVelocityProperty() const
595 return mLocalVelocity;
598 void PanGesture::SetPredictionMode(PredictionMode mode)
600 mPredictionMode = mode;
603 void PanGesture::SetPredictionAmount(unsigned int amount)
605 mPredictionAmount = amount;
606 mPredictionAmountOverridden = true;
609 void PanGesture::SetMaximumPredictionAmount(unsigned int amount)
611 mMaxPredictionAmount = amount;
614 void PanGesture::SetMinimumPredictionAmount(unsigned int amount)
616 mMinPredictionAmount = amount;
619 void PanGesture::SetPredictionAmountAdjustment(unsigned int amount)
621 mPredictionAmountAdjustment = amount;
624 void PanGesture::SetSmoothingMode(SmoothingMode mode)
626 mSmoothingMode = mode;
629 void PanGesture::SetSmoothingAmount(float amount)
631 mSmoothingAmount = amount;
632 mSmoothingAmountOverridden = true;
635 void PanGesture::SetUseActualTimes( bool value )
637 mUseActualTimes = value;
640 void PanGesture::SetInterpolationTimeRange( int value )
642 mInterpolationTimeRange = value;
645 void PanGesture::SetScalarOnlyPredictionEnabled( bool value )
647 mScalarOnlyPredictionEnabled = value;
650 void PanGesture::SetTwoPointPredictionEnabled( bool value )
652 mTwoPointPredictionEnabled = value;
655 void PanGesture::SetTwoPointInterpolatePastTime( int value )
657 mTwoPointPastInterpolateTime = value;
660 void PanGesture::SetTwoPointVelocityBias( float value )
662 mTwoPointVelocityBias = value;
665 void PanGesture::SetTwoPointAccelerationBias( float value )
667 mTwoPointAccelerationBias = value;
670 void PanGesture::SetMultitapSmoothingRange( int value )
672 mMultiTapSmoothingRange = value;
675 void PanGesture::EnableProfiling()
679 mProfiling = new PanGestureProfiling();
683 void PanGesture::ResetDefaultProperties( BufferIndex updateBufferIndex )
685 mScreenPosition.Reset();
686 mScreenDisplacement.Reset();
687 mLocalPosition.Reset();
688 mLocalDisplacement.Reset();
692 PanGesture::PanGesture()
697 mNotAtTarget( false ),
699 mPredictionAmountOverridden( false ),
700 mSmoothingAmountOverridden( false ),
703 // Set environment variable defaults:
704 mPredictionMode( DEFAULT_PREDICTION_MODE ),
705 mPredictionAmount( DEFAULT_PREDICTION_AMOUNT[0] ),
706 mCurrentPredictionAmount( DEFAULT_PREDICTION_AMOUNT[0] ),
707 mMaxPredictionAmount( DEFAULT_MAX_PREDICTION_AMOUNT ),
708 mMinPredictionAmount( DEFAULT_MIN_PREDICTION_AMOUNT ),
709 mPredictionAmountAdjustment( DEFAULT_PREDICTION_AMOUNT_ADJUSTMENT ),
710 mSmoothingMode( DEFAULT_SMOOTHING_MODE ),
711 mSmoothingAmount( DEFAULT_SMOOTHING_AMOUNT[0] ),
712 mUseActualTimes( DEFAULT_USE_ACTUAL_TIMES ),
713 mInterpolationTimeRange( DEFAULT_INTERPOLATION_TIME_RANGE ),
714 mScalarOnlyPredictionEnabled( DEFAULT_SCALAR_ONLY_PREDICTION_ENABLED ),
715 mTwoPointPredictionEnabled( DEFAULT_TWO_POINT_PREDICTION_ENABLED ),
716 mTwoPointPastInterpolateTime( DEFAULT_TWO_POINT_PAST_INTERPOLATE_TIME ),
717 mTwoPointVelocityBias( DEFAULT_TWO_POINT_VELOCITY_BIAS ),
718 mTwoPointAccelerationBias( DEFAULT_TWO_POINT_ACCELERATION_BIAS ),
719 mMultiTapSmoothingRange( DEFAULT_MULTITAP_SMOOTHING_RANGE )
724 // Prediction mode 2 related code and functions follow:
726 unsigned int PanGesture::ReadFrameEvents()
728 unsigned int eventsThisFrame;
729 // Copy the events into a linear buffer while holding the mutex.
730 // This is so the lock is not held while any processing is done.
731 Dali::Mutex::ScopedLock lock( mMutex );
732 for( eventsThisFrame = 0; mReadPosition != mWritePosition; ++eventsThisFrame )
734 mReadGestures[ eventsThisFrame ] = mGestures[ mReadPosition ];
736 mReadPosition %= PAN_GESTURE_HISTORY;
738 return eventsThisFrame;
741 // TODO: eventsThisFrame parameter can be removed if we use a smarter container.
742 bool PanGesture::InputRateConversion( PanInfo& rateConvertedGesture, unsigned int eventsThisFrame,
743 unsigned int currentFrameTime, unsigned int lastFrameTime, bool& justStarted, bool& justFinished )
745 // TODO: Lots of variables on the stack. Needs optimizing.
747 PanInfo firstReadGesture;
748 unsigned int eventsKeptThisFrame = 0;
750 for( unsigned int readPosition = 0; readPosition < eventsThisFrame; ++readPosition )
752 // Copy the gesture first
753 readGesture = mReadGestures[ readPosition ];
757 mProfiling->mRawData.push_back( PanGestureProfiling::Position( readGesture.time, readGesture.screen.position,
758 readGesture.screen.displacement, readGesture.screen.velocity, readGesture.state ) );
761 if( readGesture.state == Gesture::Started )
765 mPredictionHistory.clear();
766 mLastAcceleration.local = Vector2::ZERO;
767 mLastAcceleration.screen = Vector2::ZERO;
768 mLastInterpolatedAcceleration.local = Vector2::ZERO;
769 mLastInterpolatedAcceleration.screen = Vector2::ZERO;
770 mLastInitialAcceleration.local = Vector2::ZERO;
771 mLastInitialAcceleration.screen = Vector2::ZERO;
773 mLastGesture = startInfo;
774 mLastSecondInterpolatedPoint = startInfo;
775 mLastPredictedPoint = startInfo;
776 mLastFrameReadGesture = startInfo;
777 rateConvertedGesture = startInfo;
778 firstReadGesture = readGesture;
779 eventsKeptThisFrame = 0;
780 mNotAtTarget = false;
781 justFinished = false;
785 if( !mPredictionAmountOverridden )
787 // If the prediction amount has not been modified, default to the correct amount for this algorithm.
788 mPredictionAmount = DEFAULT_PREDICTION_AMOUNT[1];
790 mCurrentPredictionAmount = mPredictionAmount;
794 justFinished |= ( readGesture.state == Gesture::Finished || readGesture.state == Gesture::Cancelled );
797 rateConvertedGesture.screen.position += readGesture.screen.position;
798 rateConvertedGesture.local.position += readGesture.local.position;
799 rateConvertedGesture.screen.velocity += readGesture.screen.velocity;
800 rateConvertedGesture.local.velocity += readGesture.local.velocity;
801 rateConvertedGesture.screen.displacement += readGesture.screen.displacement;
802 rateConvertedGesture.local.displacement += readGesture.local.displacement;
804 ++eventsKeptThisFrame;
807 bool storeGesture = false;
808 if( eventsKeptThisFrame > 0 )
810 // Some events were read this frame.
811 if( eventsKeptThisFrame > 1 )
813 float eventDivisor( eventsKeptThisFrame );
814 rateConvertedGesture.screen.position /= eventDivisor;
815 rateConvertedGesture.local.position /= eventDivisor;
816 rateConvertedGesture.screen.velocity /= eventDivisor;
817 rateConvertedGesture.local.velocity /= eventDivisor;
818 rateConvertedGesture.screen.displacement /= eventDivisor;
819 rateConvertedGesture.local.displacement /= eventDivisor;
821 mTargetGesture = readGesture;
826 mNotAtTarget = false;
829 rateConvertedGesture.time = currentFrameTime;
834 // We did not get any event this frame.
835 // If we just started (or aren't in a gesture), exit.
836 if( !mInGesture || justStarted )
838 // We cannot guess what the event could be as we have no other events to base the guess from.
842 // As we are currently in a gesture, we can estimate an event.
843 readGesture = mLastFrameReadGesture;
844 readGesture.time = currentFrameTime;
846 // Take the last event, halve the acceleration, and use that.
847 const float accelerationDegrade = 2.0f;
848 Vector2 degradedAccelerationLocal( mLastAcceleration.local /= accelerationDegrade );
849 Vector2 degradedAccelerationScreen( mLastAcceleration.screen /= accelerationDegrade );
851 float outputTimeGranularity( GetDivisibleTimeDifference( currentFrameTime, lastFrameTime, 1.0f, OUTPUT_TIME_DIFFERENCE ) );
853 readGesture.local.velocity = degradedAccelerationLocal * outputTimeGranularity;
854 readGesture.local.displacement = readGesture.local.velocity * outputTimeGranularity;
855 readGesture.local.position = mLastFrameReadGesture.local.position + readGesture.local.displacement;
856 readGesture.screen.velocity = degradedAccelerationScreen * outputTimeGranularity;
857 readGesture.screen.displacement = readGesture.screen.velocity * outputTimeGranularity;
858 readGesture.screen.position = mLastFrameReadGesture.screen.position + readGesture.screen.displacement;
860 rateConvertedGesture = readGesture;
861 eventsKeptThisFrame = 1;
865 if( eventsKeptThisFrame > 0 )
867 // Store last read gesture.
868 readGesture.time = currentFrameTime;
869 mLastFrameReadGesture = readGesture;
871 if( eventsKeptThisFrame > 2 )
873 DALI_LOG_WARNING( "Got events this frame:%d (more than 2 will compromise result)\n", eventsKeptThisFrame );
879 // Store final converted result.
880 mPanHistory.push_back( rateConvertedGesture );
885 bool PanGesture::InterpolatePoint( PanInfoHistory& history, unsigned int currentTime, unsigned int targetTime, unsigned int range,
886 PanInfo& outPoint, RelativeVectors& acceleration, int outputTimeGranularity, bool eraseUnused )
888 unsigned int maxHistoryTime = targetTime - range;
889 unsigned int tapsUsed = 0;
890 outPoint.time = targetTime;
891 float divisor = 0.0f;
892 float accelerationDivisor = 0.0f;
893 PanInfoHistoryIter historyBegin = history.begin();
894 PanInfoHistoryIter lastIt = history.end();
895 bool pointGenerated = false;
896 bool havePreviousPoint = false;
897 RelativeVectors newAcceleration;
899 // Iterate through point history to perform interpolation.
900 for( PanInfoHistoryIter it = historyBegin; it != history.end(); )
902 unsigned int gestureTime = it->time;
904 if( gestureTime < maxHistoryTime )
906 // Too far in the past, discard.
907 // Clean history as we go (if requested).
910 it = history.erase( it );
920 float timeDelta( static_cast<float>( abs( int( targetTime - gestureTime ) ) ) );
921 // Handle low time deltas.
922 if( timeDelta < 1.0f )
927 outPoint.local.position += it->local.position / timeDelta;
928 outPoint.screen.position += it->screen.position / timeDelta;
929 outPoint.local.velocity += it->local.velocity / timeDelta;
930 outPoint.screen.velocity += it->screen.velocity / timeDelta;
931 outPoint.local.displacement += it->local.displacement / timeDelta;
932 outPoint.screen.displacement += it->screen.displacement / timeDelta;
934 divisor += 1.0f / timeDelta;
936 // Acceleration requires a previous point.
937 if( havePreviousPoint )
939 // Time delta of input.
940 float timeDifference( GetDivisibleTimeDifference( it->time, lastIt->time, 1.0f, OUTPUT_TIME_DIFFERENCE ) );
942 newAcceleration.local += ( ( it->local.velocity - lastIt->local.velocity ) / timeDifference ) / timeDelta;
943 newAcceleration.screen += ( ( it->screen.velocity - lastIt->screen.velocity ) / timeDifference ) / timeDelta;
945 accelerationDivisor += 1.0f / timeDelta;
949 havePreviousPoint = true;
958 // Divide results by their respective divisors.
963 outPoint.local.position /= divisor;
964 outPoint.screen.position /= divisor;
965 outPoint.local.velocity /= divisor;
966 outPoint.screen.velocity /= divisor;
967 outPoint.local.displacement /= divisor;
968 outPoint.screen.displacement /= divisor;
973 if( accelerationDivisor > 0.0f )
975 newAcceleration.local /= accelerationDivisor;
976 newAcceleration.screen /= accelerationDivisor;
979 float accelerationSmoothing( ACCELERATION_SMOOTHING );
980 newAcceleration.local = ( acceleration.local * accelerationSmoothing ) + ( newAcceleration.local * ( 1.0f - accelerationSmoothing ) );
981 newAcceleration.screen = ( acceleration.screen * accelerationSmoothing ) + ( newAcceleration.screen * ( 1.0f - accelerationSmoothing ) );
985 // If we just started, last velocity was 0. So difference of zero to current velocity over time gives acceleration of the first point.
986 newAcceleration.local = outPoint.local.velocity / outputTimeGranularity;
987 newAcceleration.screen = outPoint.screen.velocity / outputTimeGranularity;
989 pointGenerated = true;
992 acceleration.local = newAcceleration.local;
993 acceleration.screen = newAcceleration.screen;
994 return pointGenerated;
997 float PanGesture::GetDivisibleTimeDifference( int timeA, int timeB, float minimumDelta, float overrideDifference )
999 float timeDifference( overrideDifference );
1000 if( mUseActualTimes )
1002 timeDifference = static_cast<float>( abs( timeA - timeB ) );
1003 if( timeDifference < minimumDelta )
1005 timeDifference = minimumDelta;
1008 return timeDifference;
1011 void PanGesture::LimitAccelerationChange( RelativeVectors& currentAcceleration, RelativeVectors& lastAcceleration, float changeLimit )
1013 // We don't use the float parameter version of clamp here, as that will create the capping vectors twice in total.
1014 Vector2 capMinimum( -changeLimit, -changeLimit );
1015 Vector2 capMaximum( changeLimit, changeLimit );
1016 Vector2 accelerationDeltaLocal( currentAcceleration.local - lastAcceleration.local );
1017 Vector2 accelerationDeltaScreen( currentAcceleration.screen - lastAcceleration.screen );
1018 accelerationDeltaLocal.Clamp( capMinimum, capMaximum );
1019 accelerationDeltaScreen.Clamp( capMinimum, capMaximum );
1020 currentAcceleration.local = lastAcceleration.local + accelerationDeltaLocal;
1021 currentAcceleration.screen = lastAcceleration.screen + accelerationDeltaScreen;
1024 void PanGesture::PredictionMode2( PanInfo& startPoint, RelativeVectors& accelerationToUse,
1025 PanInfo& predictedPoint, unsigned int currentFrameTime, unsigned int previousFrameTime, bool noPreviousData )
1027 // Do the prediction (based on mode).
1028 if( mScalarOnlyPredictionEnabled )
1030 // We are doing scalar based prediction.
1031 // This divisor is to help tuning by giving the scalar only result
1032 // a similar prediction amount to the integrated result.
1033 float scalarVelocityMultiplier = static_cast<float>( mCurrentPredictionAmount ) / 1.364f;
1034 predictedPoint.local.position = startPoint.local.position + ( startPoint.local.velocity * scalarVelocityMultiplier );
1035 predictedPoint.screen.position = startPoint.screen.position + ( startPoint.screen.velocity * scalarVelocityMultiplier );
1039 // We are doing integration based prediction.
1040 float predictionDelta( mCurrentPredictionAmount );
1042 predictedPoint.local.position = startPoint.local.position + ( startPoint.local.velocity * predictionDelta ) +
1043 ( accelerationToUse.local * ( predictionDelta * predictionDelta * 0.5f ) );
1044 predictedPoint.screen.position = startPoint.screen.position + ( startPoint.screen.velocity * predictionDelta ) +
1045 ( accelerationToUse.screen * ( predictionDelta * predictionDelta * 0.5f ) );
1048 // Calculate remaining gesture data from the result.
1049 float timeDifference( GetDivisibleTimeDifference( currentFrameTime, previousFrameTime, 1.0f, OUTPUT_TIME_DIFFERENCE ) );
1050 if( noPreviousData )
1052 predictedPoint.local.displacement = predictedPoint.local.position - startPoint.local.position;
1053 predictedPoint.screen.displacement = predictedPoint.screen.position - startPoint.screen.position;
1057 predictedPoint.local.displacement = predictedPoint.local.position - mLastPredictedPoint.local.position;
1058 predictedPoint.screen.displacement = predictedPoint.screen.position - mLastPredictedPoint.screen.position;
1060 predictedPoint.local.velocity = predictedPoint.local.displacement / timeDifference;
1061 predictedPoint.screen.velocity = predictedPoint.screen.displacement / timeDifference;
1063 // TODO: Experimental - not used at run time. Left in code for reference only.
1064 if( TEST_TUNE_ENABLE_OVERSHOOT_PROTECTION )
1066 // Overshoot protection
1067 if( !noPreviousData )
1069 if( ( mLastPredictedPoint.local.velocity.x > Math::MACHINE_EPSILON_0 && predictedPoint.local.velocity.x < Math::MACHINE_EPSILON_0 )
1070 || ( mLastPredictedPoint.local.velocity.x < Math::MACHINE_EPSILON_0 && predictedPoint.local.velocity.x > Math::MACHINE_EPSILON_0 ) )
1072 predictedPoint.local.position.x = mLastPredictedPoint.local.position.x;
1073 predictedPoint.screen.position.x = mLastPredictedPoint.screen.position.x;
1074 mPredictionHistory.clear();
1076 if( ( mLastPredictedPoint.local.velocity.y > Math::MACHINE_EPSILON_0 && predictedPoint.local.velocity.y < Math::MACHINE_EPSILON_0 )
1077 || ( mLastPredictedPoint.local.velocity.y < Math::MACHINE_EPSILON_0 && predictedPoint.local.velocity.y > Math::MACHINE_EPSILON_0 ) )
1079 predictedPoint.local.position.y = mLastPredictedPoint.local.position.y;
1080 predictedPoint.screen.position.y = mLastPredictedPoint.screen.position.y;
1081 mPredictionHistory.clear();
1086 predictedPoint.time = currentFrameTime;
1087 mLastPredictedPoint = predictedPoint;
1090 // TODO: This needs a better name! It is called this instead of prediction mode 2 because:
1091 // 1) It is the entire workflow, not just prediction.
1092 // 2) To make it less confusing as there is a function that does prediction alone called PerformPredictionMode2.
1093 // Ultimately we need to combine the old and new code modularly so there is one code path that can optionally run different functions based on configuration.
1094 // At the moment, the differences between the inputs & outputs of these different functions prevent that, but this can be resolved.
1095 bool PanGesture::NewAlgorithm( unsigned int lastVSyncTime, unsigned int nextVSyncTime )
1099 // clear current pan history
1100 mPanHistory.clear();
1101 mPredictionHistory.clear();
1104 /*#########################################################################################
1105 #### Read in all gestures received this frame first (holding a lock for a short time)
1106 #########################################################################################*/
1108 unsigned int eventsThisFrame = ReadFrameEvents();
1110 /*#########################################################################################
1111 #### Perform input rate-conversion on all gestures received this frame.
1112 #### This also populates the pan history.
1113 #########################################################################################*/
1115 bool justStarted = false;
1116 bool justFinished = false;
1117 PanInfo rateConvertedGesture;
1118 if( !InputRateConversion( rateConvertedGesture, eventsThisFrame, nextVSyncTime, lastVSyncTime, justStarted, justFinished ) )
1120 // There's nothing we can do with the input, exit.
1124 /*#########################################################################################
1125 #### If we are in gesture, Get first interpolated point with: target time = current time
1126 #########################################################################################*/
1128 bool performUpdate = false;
1129 RelativeVectors currentAcceleration;
1130 currentAcceleration.local = mLastInitialAcceleration.local;
1131 currentAcceleration.screen = mLastInitialAcceleration.screen;
1133 if( mInGesture || justStarted )
1135 // Get first interpolated point.
1136 // TODO: Erase time should be maximum of both interpolated point ranges in past.
1137 PanInfo targetPoint;
1138 float outputTimeGranularity( GetDivisibleTimeDifference( nextVSyncTime, lastVSyncTime, 1.0f, OUTPUT_TIME_DIFFERENCE ) );
1139 bool pointGenerated = InterpolatePoint( mPanHistory, nextVSyncTime, nextVSyncTime, mInterpolationTimeRange,
1140 targetPoint, currentAcceleration, outputTimeGranularity, true );
1141 if( pointGenerated )
1143 mLastInitialAcceleration.local = currentAcceleration.local;
1144 mLastInitialAcceleration.screen = currentAcceleration.screen;
1145 performUpdate = true;
1149 targetPoint = rateConvertedGesture;
1150 currentAcceleration.local = mLastInitialAcceleration.local;
1151 currentAcceleration.screen = mLastInitialAcceleration.screen;
1152 // TODO: Potentially do something to substitute lack of generated point (and perform update).
1155 /*#########################################################################################
1156 #### Limit the change of acceleration of the first interpolated point since last time
1157 #########################################################################################*/
1161 LimitAccelerationChange( currentAcceleration, mLastAcceleration, ACCELERATION_CAP );
1163 mLastAcceleration.local = currentAcceleration.local;
1164 mLastAcceleration.screen = currentAcceleration.screen;
1166 /*#########################################################################################
1167 #### Get second interpolated point, and blend the resultant velocity and acceleration (optional)
1168 #########################################################################################*/
1171 RelativeVectors interpolatedAcceleration;
1172 if( mTwoPointPredictionEnabled )
1174 // Get second interpolated point with target time = current time - past interpolate time.
1175 unsigned int pastInterpolateTime = nextVSyncTime - mTwoPointPastInterpolateTime;
1177 RelativeVectors interpolatedAcceleration;
1178 interpolatedAcceleration.local = mLastInterpolatedAcceleration.local;
1179 interpolatedAcceleration.screen = mLastInterpolatedAcceleration.screen;
1180 if( !InterpolatePoint( mPanHistory, nextVSyncTime, pastInterpolateTime, mTwoPointPastInterpolateTime,
1181 outPoint, interpolatedAcceleration, outputTimeGranularity, false ) )
1185 outPoint = targetPoint;
1189 outPoint = mLastSecondInterpolatedPoint;
1192 mLastInterpolatedAcceleration.local = interpolatedAcceleration.local;
1193 mLastInterpolatedAcceleration.screen = interpolatedAcceleration.screen;
1194 mLastSecondInterpolatedPoint = outPoint;
1196 // Combine the first interpolated point and the second interpolated point.
1197 // by mixing them with the configured amount. This is done for acceleration and velocity.
1198 // It could be optionally done for position too, but this typically is worse as it means we have to predict further ahead.
1199 float currentVelocityMultiplier( 1.0f - mTwoPointVelocityBias );
1200 float lastVelocityMultiplier( mTwoPointVelocityBias );
1201 targetPoint.local.velocity = ( outPoint.local.velocity * lastVelocityMultiplier ) + ( targetPoint.local.velocity * currentVelocityMultiplier );
1202 targetPoint.screen.velocity = ( outPoint.screen.velocity * lastVelocityMultiplier ) + ( targetPoint.screen.velocity * currentVelocityMultiplier );
1203 float currentAccelerationMultiplier( 1.0f - mTwoPointAccelerationBias );
1204 float lastAccelerationMultiplier( mTwoPointAccelerationBias );
1205 currentAcceleration.local = ( interpolatedAcceleration.local * lastAccelerationMultiplier ) + ( currentAcceleration.local * currentAccelerationMultiplier );
1206 currentAcceleration.screen = ( interpolatedAcceleration.screen * lastAccelerationMultiplier ) + ( currentAcceleration.screen * currentAccelerationMultiplier );
1209 /*#########################################################################################
1210 #### Perform prediction
1211 #########################################################################################*/
1213 PanInfo predictedPoint;
1214 PredictionMode2( targetPoint, currentAcceleration, predictedPoint, nextVSyncTime, lastVSyncTime, justStarted );
1215 targetPoint = predictedPoint;
1217 /*#########################################################################################
1219 #########################################################################################*/
1221 // If we are using multi-tap smoothing, keep a history of predicted results.
1222 if( mSmoothingMode == SMOOTHING_MULTI_TAP )
1224 mPredictionHistory.push_back( targetPoint );
1229 float outputTimeGranularity( GetDivisibleTimeDifference( nextVSyncTime, lastVSyncTime, 1.0f, OUTPUT_TIME_DIFFERENCE ) );
1230 if( mSmoothingMode == SMOOTHING_MULTI_TAP )
1232 // Perform Multi-tap Smoothing.
1233 RelativeVectors blank;
1234 InterpolatePoint( mPredictionHistory, nextVSyncTime, nextVSyncTime, mMultiTapSmoothingRange,
1235 targetPoint, blank, outputTimeGranularity, true );
1239 // Perform Single-tap Smoothing.
1240 if( !mSmoothingAmountOverridden )
1242 // If the smoothing amount has not been modified, default to the correct amount for this algorithm.
1243 mSmoothingAmount = DEFAULT_SMOOTHING_AMOUNT[1];
1245 BlendPoints( targetPoint, mLastGesture, mSmoothingAmount );
1248 /*#########################################################################################
1249 #### Finalize other point data (from position)
1250 #########################################################################################*/
1252 targetPoint.local.displacement = targetPoint.local.position - mLastGesture.local.position;
1253 targetPoint.local.velocity = targetPoint.local.displacement / outputTimeGranularity;
1254 targetPoint.screen.displacement = targetPoint.screen.position - mLastGesture.screen.position;
1255 targetPoint.screen.velocity = targetPoint.screen.displacement / outputTimeGranularity;
1258 /*#########################################################################################
1259 #### Send out the new point, by setting the properties
1260 #### (Constraints will automatically react to this)
1261 #########################################################################################*/
1265 mPanning.Set( mInGesture & !justFinished );
1266 mScreenPosition.Set( targetPoint.screen.position );
1267 mScreenDisplacement.Set( targetPoint.screen.displacement );
1268 mScreenVelocity.Set( targetPoint.screen.velocity );
1269 mLocalPosition.Set( targetPoint.local.position );
1270 mLocalDisplacement.Set( targetPoint.local.displacement );
1271 mLocalVelocity.Set( targetPoint.local.velocity );
1273 mLastGesture = targetPoint;
1277 mProfiling->mAveragedData.push_back( PanGestureProfiling::Position( targetPoint.time, targetPoint.screen.position,
1278 targetPoint.screen.displacement, targetPoint.screen.velocity, targetPoint.state ) );
1283 mInGesture &= ~justFinished;
1285 return performUpdate;
1289 } // namespace SceneGraph
1291 } // namespace Internal