OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
| 5 #include <algorithm> |
| 6 |
| 7 #include "base/logging.h" |
5 #include "cc/animation/timing_function.h" | 8 #include "cc/animation/timing_function.h" |
6 | 9 |
7 #include "third_party/skia/include/core/SkMath.h" | 10 namespace cc { |
8 | 11 |
9 // TODO(danakj) These methods come from SkInterpolator.cpp. When such a method | |
10 // is available in the public Skia API, we should switch to using that. | |
11 // http://crbug.com/159735 | |
12 namespace { | 12 namespace { |
13 | 13 |
14 // Dot14 has 14 bits for decimal places, and the remainder for whole numbers. | 14 static const double BEZIER_EPSILON = 1e-7; |
15 typedef int Dot14; | 15 static const int MAX_STEPS = 30; |
16 #define DOT14_ONE (1 << 14) | |
17 #define DOT14_HALF (1 << 13) | |
18 | 16 |
19 static inline Dot14 Dot14Mul(Dot14 a, Dot14 b) { | 17 static double eval_bezier(double x1, double x2, double t) { |
20 return (a * b + DOT14_HALF) >> 14; | 18 const double x1_times_3 = 3.0 * x1; |
| 19 const double x2_times_3 = 3.0 * x2; |
| 20 const double h3 = x1_times_3; |
| 21 const double h1 = x1_times_3 - x2_times_3 + 1.0; |
| 22 const double h2 = x2_times_3 - 6.0 * x1; |
| 23 return t * (t * (t * h1 + h2) + h3); |
21 } | 24 } |
22 | 25 |
23 static inline Dot14 EvalCubic(Dot14 t, Dot14 A, Dot14 B, Dot14 C) { | 26 static double bezier_interp(double x1, |
24 return Dot14Mul(Dot14Mul(Dot14Mul(C, t) + B, t) + A, t); | 27 double y1, |
25 } | 28 double x2, |
| 29 double y2, |
| 30 double x) { |
| 31 DCHECK_GE(1.0, x1); |
| 32 DCHECK_LE(0.0, x1); |
| 33 DCHECK_GE(1.0, x2); |
| 34 DCHECK_LE(0.0, x2); |
26 | 35 |
27 static inline Dot14 PinAndConvert(SkScalar x) { | 36 x1 = std::min(std::max(x1, 0.0), 1.0); |
28 if (x <= 0) | 37 x2 = std::min(std::max(x2, 0.0), 1.0); |
29 return 0; | 38 x = std::min(std::max(x, 0.0), 1.0); |
30 if (x >= SK_Scalar1) | |
31 return DOT14_ONE; | |
32 return SkScalarToFixed(x) >> 2; | |
33 } | |
34 | 39 |
35 SkScalar SkUnitCubicInterp(SkScalar bx, | 40 // Step 1. Find the t corresponding to the given x. I.e., we want t such that |
36 SkScalar by, | 41 // eval_bezier(x1, x2, t) = x. There is a unique solution if x1 and x2 lie |
37 SkScalar cx, | 42 // within (0, 1). |
38 SkScalar cy, | 43 // |
39 SkScalar value) { | 44 // We're just going to do bisection for now (for simplicity), but we could |
40 Dot14 x = PinAndConvert(value); | 45 // easily do some newton steps if this turns out to be a bottleneck. |
41 | 46 double t = 0.0; |
42 if (x == 0) | 47 double step = 1.0; |
43 return 0; | 48 for (int i = 0; i < MAX_STEPS; ++i, step *= 0.5) { |
44 if (x == DOT14_ONE) | 49 const double error = eval_bezier(x1, x2, t) - x; |
45 return SK_Scalar1; | 50 if (fabs(error) < BEZIER_EPSILON) |
46 | 51 break; |
47 Dot14 b = PinAndConvert(bx); | 52 t += error > 0.0 ? -step : step; |
48 Dot14 c = PinAndConvert(cx); | |
49 | |
50 // Now compute our coefficients from the control points. | |
51 // t -> 3b | |
52 // t^2 -> 3c - 6b | |
53 // t^3 -> 3b - 3c + 1 | |
54 Dot14 A = 3 * b; | |
55 Dot14 B = 3 * (c - 2 * b); | |
56 Dot14 C = 3 * (b - c) + DOT14_ONE; | |
57 | |
58 // Now search for a t value given x. | |
59 Dot14 t = DOT14_HALF; | |
60 Dot14 dt = DOT14_HALF; | |
61 for (int i = 0; i < 13; i++) { | |
62 dt >>= 1; | |
63 Dot14 guess = EvalCubic(t, A, B, C); | |
64 if (x < guess) | |
65 t -= dt; | |
66 else | |
67 t += dt; | |
68 } | 53 } |
69 | 54 |
70 // Now we have t, so compute the coefficient for Y and evaluate. | 55 // We should have terminated the above loop because we got close to x, not |
71 b = PinAndConvert(by); | 56 // because we exceeded MAX_STEPS. Do a DCHECK here to confirm. |
72 c = PinAndConvert(cy); | 57 DCHECK_GT(BEZIER_EPSILON, fabs(eval_bezier(x1, x2, t) - x)); |
73 A = 3 * b; | 58 |
74 B = 3 * (c - 2 * b); | 59 // Step 2. Return the interpolated y values at the t we computed above. |
75 C = 3 * (b - c) + DOT14_ONE; | 60 return eval_bezier(y1, y2, t); |
76 return SkFixedToScalar(EvalCubic(t, A, B, C) << 2); | |
77 } | 61 } |
78 | 62 |
79 } // namespace | 63 } // namespace |
80 | 64 |
81 namespace cc { | |
82 | |
83 TimingFunction::TimingFunction() {} | 65 TimingFunction::TimingFunction() {} |
84 | 66 |
85 TimingFunction::~TimingFunction() {} | 67 TimingFunction::~TimingFunction() {} |
86 | 68 |
87 double TimingFunction::Duration() const { | 69 double TimingFunction::Duration() const { |
88 return 1.0; | 70 return 1.0; |
89 } | 71 } |
90 | 72 |
91 scoped_ptr<CubicBezierTimingFunction> CubicBezierTimingFunction::Create( | 73 scoped_ptr<CubicBezierTimingFunction> CubicBezierTimingFunction::Create( |
92 double x1, | 74 double x1, double y1, double x2, double y2) { |
93 double y1, | |
94 double x2, | |
95 double y2) { | |
96 return make_scoped_ptr(new CubicBezierTimingFunction(x1, y1, x2, y2)); | 75 return make_scoped_ptr(new CubicBezierTimingFunction(x1, y1, x2, y2)); |
97 } | 76 } |
98 | 77 |
99 CubicBezierTimingFunction::CubicBezierTimingFunction(double x1, | 78 CubicBezierTimingFunction::CubicBezierTimingFunction(double x1, |
100 double y1, | 79 double y1, |
101 double x2, | 80 double x2, |
102 double y2) | 81 double y2) |
103 : x1_(SkDoubleToScalar(x1)), | 82 : x1_(x1), y1_(y1), x2_(x2), y2_(y2) {} |
104 y1_(SkDoubleToScalar(y1)), | |
105 x2_(SkDoubleToScalar(x2)), | |
106 y2_(SkDoubleToScalar(y2)) {} | |
107 | 83 |
108 CubicBezierTimingFunction::~CubicBezierTimingFunction() {} | 84 CubicBezierTimingFunction::~CubicBezierTimingFunction() {} |
109 | 85 |
110 float CubicBezierTimingFunction::GetValue(double x) const { | 86 float CubicBezierTimingFunction::GetValue(double x) const { |
111 SkScalar value = SkUnitCubicInterp(x1_, y1_, x2_, y2_, x); | 87 return static_cast<float>(bezier_interp(x1_, y1_, x2_, y2_, x)); |
112 return SkScalarToFloat(value); | |
113 } | 88 } |
114 | 89 |
115 scoped_ptr<AnimationCurve> CubicBezierTimingFunction::Clone() const { | 90 scoped_ptr<AnimationCurve> CubicBezierTimingFunction::Clone() const { |
116 return make_scoped_ptr( | 91 return make_scoped_ptr( |
117 new CubicBezierTimingFunction(*this)).PassAs<AnimationCurve>(); | 92 new CubicBezierTimingFunction(*this)).PassAs<AnimationCurve>(); |
118 } | 93 } |
119 | 94 |
120 // These numbers come from | 95 // These numbers come from |
121 // http://www.w3.org/TR/css3-transitions/#transition-timing-function_tag. | 96 // http://www.w3.org/TR/css3-transitions/#transition-timing-function_tag. |
122 scoped_ptr<TimingFunction> EaseTimingFunction::Create() { | 97 scoped_ptr<TimingFunction> EaseTimingFunction::Create() { |
(...skipping 10 matching lines...) Expand all Loading... |
133 return CubicBezierTimingFunction::Create( | 108 return CubicBezierTimingFunction::Create( |
134 0.0, 0.0, 0.58, 1.0).PassAs<TimingFunction>(); | 109 0.0, 0.0, 0.58, 1.0).PassAs<TimingFunction>(); |
135 } | 110 } |
136 | 111 |
137 scoped_ptr<TimingFunction> EaseInOutTimingFunction::Create() { | 112 scoped_ptr<TimingFunction> EaseInOutTimingFunction::Create() { |
138 return CubicBezierTimingFunction::Create( | 113 return CubicBezierTimingFunction::Create( |
139 0.42, 0.0, 0.58, 1).PassAs<TimingFunction>(); | 114 0.42, 0.0, 0.58, 1).PassAs<TimingFunction>(); |
140 } | 115 } |
141 | 116 |
142 } // namespace cc | 117 } // namespace cc |
OLD | NEW |