Line data Source code
1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* This Source Code Form is subject to the terms of the Mozilla Public
3 : * License, v. 2.0. If a copy of the MPL was not distributed with this
4 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 :
6 : #ifndef mozilla_layout_AsyncScrollBase_h_
7 : #define mozilla_layout_AsyncScrollBase_h_
8 :
9 : #include "mozilla/TimeStamp.h"
10 : #include "nsPoint.h"
11 : #include "nsSMILKeySpline.h"
12 :
13 : namespace mozilla {
14 :
15 : // This is the base class for driving scroll wheel animation on both the
16 : // compositor and main thread.
17 : class AsyncScrollBase
18 : {
19 : public:
20 : typedef mozilla::TimeStamp TimeStamp;
21 : typedef mozilla::TimeDuration TimeDuration;
22 :
23 : explicit AsyncScrollBase(nsPoint aStartPos);
24 :
25 : void Update(TimeStamp aTime,
26 : nsPoint aDestination,
27 : const nsSize& aCurrentVelocity);
28 :
29 : // Get the velocity at a point in time in nscoords/sec.
30 : nsSize VelocityAt(TimeStamp aTime) const;
31 :
32 : // Returns the expected scroll position at a given point in time, in app
33 : // units, relative to the scroll frame.
34 : nsPoint PositionAt(TimeStamp aTime) const;
35 :
36 0 : bool IsFinished(TimeStamp aTime) {
37 0 : return aTime > mStartTime + mDuration;
38 : }
39 :
40 : protected:
41 0 : double ProgressAt(TimeStamp aTime) const {
42 0 : return clamped((aTime - mStartTime) / mDuration, 0.0, 1.0);
43 : }
44 :
45 : nscoord VelocityComponent(double aTimeProgress,
46 : const nsSMILKeySpline& aTimingFunction,
47 : nscoord aStart, nscoord aDestination) const;
48 :
49 : // Calculate duration, possibly dynamically according to events rate and
50 : // event origin. (also maintain previous timestamps - which are only used
51 : // here).
52 : TimeDuration ComputeDuration(TimeStamp aTime);
53 :
54 : // Initialize event history.
55 : void InitializeHistory(TimeStamp aTime);
56 :
57 : // Initializes the timing function in such a way that the current velocity is
58 : // preserved.
59 : void InitTimingFunction(nsSMILKeySpline& aTimingFunction,
60 : nscoord aCurrentPos, nscoord aCurrentVelocity,
61 : nscoord aDestination);
62 :
63 : // mPrevEventTime holds previous 3 timestamps for intervals averaging (to
64 : // reduce duration fluctuations). When AsyncScroll is constructed and no
65 : // previous timestamps are available (indicated with mIsFirstIteration),
66 : // initialize mPrevEventTime using imaginary previous timestamps with maximum
67 : // relevant intervals between them.
68 : TimeStamp mPrevEventTime[3];
69 : bool mIsFirstIteration;
70 :
71 : TimeStamp mStartTime;
72 :
73 : // Cached Preferences value.
74 : //
75 : // These values are minimum and maximum animation duration per event origin,
76 : // and a global ratio which defines how longer is the animation's duration
77 : // compared to the average recent events intervals (such that for a relatively
78 : // consistent events rate, the next event arrives before current animation ends)
79 : int32_t mOriginMinMS;
80 : int32_t mOriginMaxMS;
81 : double mIntervalRatio;
82 :
83 : nsPoint mStartPos;
84 : TimeDuration mDuration;
85 : nsPoint mDestination;
86 : nsSMILKeySpline mTimingFunctionX;
87 : nsSMILKeySpline mTimingFunctionY;
88 : };
89 :
90 : // Helper for accelerated wheel deltas. This can be called from the main thread
91 : // or the APZ Controller thread.
92 : static inline double
93 0 : ComputeAcceleratedWheelDelta(double aDelta, int32_t aCounter, int32_t aFactor)
94 : {
95 0 : if (!aDelta) {
96 0 : return aDelta;
97 : }
98 0 : return (aDelta * aCounter * double(aFactor) / 10);
99 : }
100 :
101 : static const uint32_t kScrollSeriesTimeoutMs = 80; // in milliseconds
102 :
103 : } // namespace mozilla
104 :
105 : #endif // mozilla_layout_AsyncScrollBase_h_
|