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