2 * Copyright (C) 2000 Lars Knoll (knoll@kde.org)
3 * (C) 2000 Antti Koivisto (koivisto@kde.org)
4 * (C) 2000 Dirk Mueller (mueller@kde.org)
5 * Copyright (C) 2003, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
6 * Copyright (C) 2006 Graham Dennis (graham.dennis@gmail.com)
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public License
19 * along with this library; see the file COPYING.LIB. If not, write to
20 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
25 #ifndef TimingFunction_h
26 #define TimingFunction_h
28 #include "platform/animation/AnimationUtilities.h" // For blend()
29 #include "platform/animation/UnitBezier.h"
30 #include "platform/heap/Handle.h"
31 #include "platform/heap/Heap.h"
32 #include "wtf/OwnPtr.h"
33 #include "wtf/PassOwnPtr.h"
34 #include "wtf/PassRefPtr.h"
35 #include "wtf/RefCounted.h"
36 #include "wtf/StdLibExtras.h"
37 #include "wtf/text/StringBuilder.h"
38 #include "wtf/text/WTFString.h"
42 class PLATFORM_EXPORT TimingFunction
: public RefCounted
<TimingFunction
> {
46 LinearFunction
, CubicBezierFunction
, StepsFunction
49 virtual ~TimingFunction() { }
51 Type
type() const { return m_type
; }
53 virtual String
toString() const = 0;
55 // Evaluates the timing function at the given fraction. The accuracy parameter provides a hint as to the required
56 // accuracy and is not guaranteed.
57 virtual double evaluate(double fraction
, double accuracy
) const = 0;
59 // This function returns the minimum and maximum values obtainable when
60 // calling evaluate();
61 virtual void range(double* minValue
, double* maxValue
) const = 0;
64 Lower
, // Timing function values < 0.5
65 Upper
// Timing function values >= 0.5
68 struct PartitionRegion
{
70 double start
; // inclusive
71 double end
; // exclusive
73 PartitionRegion(RangeHalf half
, double start
, double end
)
80 // Partitions the timing function into a number of regions,
81 // representing the ranges in which the function's value is < 0.5
82 // and >= 0.5, and hence whether interpolation 0 or 1 should be
84 virtual void partition(Vector
<PartitionRegion
>& regions
) const = 0;
87 TimingFunction(Type type
)
96 class PLATFORM_EXPORT LinearTimingFunction final
: public TimingFunction
{
98 static LinearTimingFunction
* shared()
100 DEFINE_STATIC_REF(LinearTimingFunction
, linear
, (adoptRef(new LinearTimingFunction())));
104 ~LinearTimingFunction() override
{ }
106 String
toString() const override
;
108 double evaluate(double fraction
, double) const override
;
109 void range(double* minValue
, double* maxValue
) const override
;
110 void partition(Vector
<PartitionRegion
>& regions
) const override
;
112 LinearTimingFunction()
113 : TimingFunction(LinearFunction
)
118 class PLATFORM_EXPORT CubicBezierTimingFunction final
: public TimingFunction
{
128 static PassRefPtr
<CubicBezierTimingFunction
> create(double x1
, double y1
, double x2
, double y2
)
130 return adoptRef(new CubicBezierTimingFunction(Custom
, x1
, y1
, x2
, y2
));
133 static CubicBezierTimingFunction
* preset(SubType subType
)
138 DEFINE_STATIC_REF(CubicBezierTimingFunction
, ease
, (adoptRef(new CubicBezierTimingFunction(Ease
, 0.25, 0.1, 0.25, 1.0))));
143 DEFINE_STATIC_REF(CubicBezierTimingFunction
, easeIn
, (adoptRef(new CubicBezierTimingFunction(EaseIn
, 0.42, 0.0, 1.0, 1.0))));
148 DEFINE_STATIC_REF(CubicBezierTimingFunction
, easeOut
, (adoptRef(new CubicBezierTimingFunction(EaseOut
, 0.0, 0.0, 0.58, 1.0))));
153 DEFINE_STATIC_REF(CubicBezierTimingFunction
, easeInOut
, (adoptRef(new CubicBezierTimingFunction(EaseInOut
, 0.42, 0.0, 0.58, 1.0))));
157 ASSERT_NOT_REACHED();
162 ~CubicBezierTimingFunction() override
{ }
164 String
toString() const override
;
166 double evaluate(double fraction
, double accuracy
) const override
;
167 void range(double* minValue
, double* maxValue
) const override
;
168 void partition(Vector
<PartitionRegion
>& regions
) const override
;
170 double x1() const { return m_x1
; }
171 double y1() const { return m_y1
; }
172 double x2() const { return m_x2
; }
173 double y2() const { return m_y2
; }
175 SubType
subType() const { return m_subType
; }
178 explicit CubicBezierTimingFunction(SubType subType
, double x1
, double y1
, double x2
, double y2
)
179 : TimingFunction(CubicBezierFunction
)
188 // Finds points on the cubic bezier that cross the given horizontal
189 // line, storing their x values in solution1-3 and returning the
190 // number of solutions found.
191 size_t findIntersections(double intersectionY
, double& solution1
, double& solution2
, double& solution3
) const;
198 mutable OwnPtr
<UnitBezier
> m_bezier
;
201 class PLATFORM_EXPORT StepsTimingFunction final
: public TimingFunction
{
203 enum StepAtPosition
{
209 static PassRefPtr
<StepsTimingFunction
> create(int steps
, StepAtPosition stepAtPosition
)
211 return adoptRef(new StepsTimingFunction(steps
, stepAtPosition
));
214 static StepsTimingFunction
* preset(StepAtPosition position
)
216 DEFINE_STATIC_REF(StepsTimingFunction
, start
, create(1, Start
));
217 DEFINE_STATIC_REF(StepsTimingFunction
, middle
, create(1, Middle
));
218 DEFINE_STATIC_REF(StepsTimingFunction
, end
, create(1, End
));
227 ASSERT_NOT_REACHED();
233 ~StepsTimingFunction() override
{ }
235 String
toString() const override
;
237 double evaluate(double fraction
, double) const override
;
238 void range(double* minValue
, double* maxValue
) const override
;
239 void partition(Vector
<PartitionRegion
>& regions
) const override
;
241 int numberOfSteps() const { return m_steps
; }
242 StepAtPosition
stepAtPosition() const { return m_stepAtPosition
; }
245 StepsTimingFunction(int steps
, StepAtPosition stepAtPosition
)
246 : TimingFunction(StepsFunction
)
248 , m_stepAtPosition(stepAtPosition
)
253 StepAtPosition m_stepAtPosition
;
256 PLATFORM_EXPORT
bool operator==(const LinearTimingFunction
&, const TimingFunction
&);
257 PLATFORM_EXPORT
bool operator==(const CubicBezierTimingFunction
&, const TimingFunction
&);
258 PLATFORM_EXPORT
bool operator==(const StepsTimingFunction
&, const TimingFunction
&);
260 PLATFORM_EXPORT
bool operator==(const TimingFunction
&, const TimingFunction
&);
261 PLATFORM_EXPORT
bool operator!=(const TimingFunction
&, const TimingFunction
&);
263 #define DEFINE_TIMING_FUNCTION_TYPE_CASTS(typeName) \
265 typeName##TimingFunction, TimingFunction, value, \
266 value->type() == TimingFunction::typeName##Function, \
267 value.type() == TimingFunction::typeName##Function)
269 DEFINE_TIMING_FUNCTION_TYPE_CASTS(Linear
);
270 DEFINE_TIMING_FUNCTION_TYPE_CASTS(CubicBezier
);
271 DEFINE_TIMING_FUNCTION_TYPE_CASTS(Steps
);
275 #endif // TimingFunction_h