Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(123)

Side by Side Diff: cc/animation/timing_function.cc

Issue 143413020: Use a bezier timing function for the overview mode animation (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « cc/animation/timing_function.h ('k') | cc/animation/timing_function_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 #include <cmath>
7
8 #include "base/logging.h" 5 #include "base/logging.h"
9 #include "cc/animation/timing_function.h" 6 #include "cc/animation/timing_function.h"
10 7
11 namespace cc { 8 namespace cc {
12 9
13 namespace {
14
15 static const double kBezierEpsilon = 1e-7;
16 static const int MAX_STEPS = 30;
17
18 static double eval_bezier(double x1, double x2, double t) {
19 const double x1_times_3 = 3.0 * x1;
20 const double x2_times_3 = 3.0 * x2;
21 const double h3 = x1_times_3;
22 const double h1 = x1_times_3 - x2_times_3 + 1.0;
23 const double h2 = x2_times_3 - 6.0 * x1;
24 return t * (t * (t * h1 + h2) + h3);
25 }
26
27 static double bezier_interp(double x1,
28 double y1,
29 double x2,
30 double y2,
31 double x) {
32 DCHECK_GE(1.0, x1);
33 DCHECK_LE(0.0, x1);
34 DCHECK_GE(1.0, x2);
35 DCHECK_LE(0.0, x2);
36
37 x1 = std::min(std::max(x1, 0.0), 1.0);
38 x2 = std::min(std::max(x2, 0.0), 1.0);
39 x = std::min(std::max(x, 0.0), 1.0);
40
41 // Step 1. Find the t corresponding to the given x. I.e., we want t such that
42 // eval_bezier(x1, x2, t) = x. There is a unique solution if x1 and x2 lie
43 // within (0, 1).
44 //
45 // We're just going to do bisection for now (for simplicity), but we could
46 // easily do some newton steps if this turns out to be a bottleneck.
47 double t = 0.0;
48 double step = 1.0;
49 for (int i = 0; i < MAX_STEPS; ++i, step *= 0.5) {
50 const double error = eval_bezier(x1, x2, t) - x;
51 if (std::abs(error) < kBezierEpsilon)
52 break;
53 t += error > 0.0 ? -step : step;
54 }
55
56 // We should have terminated the above loop because we got close to x, not
57 // because we exceeded MAX_STEPS. Do a DCHECK here to confirm.
58 DCHECK_GT(kBezierEpsilon, std::abs(eval_bezier(x1, x2, t) - x));
59
60 // Step 2. Return the interpolated y values at the t we computed above.
61 return eval_bezier(y1, y2, t);
62 }
63
64 } // namespace
65
66 TimingFunction::TimingFunction() {} 10 TimingFunction::TimingFunction() {}
67 11
68 TimingFunction::~TimingFunction() {} 12 TimingFunction::~TimingFunction() {}
69 13
70 double TimingFunction::Duration() const { 14 double TimingFunction::Duration() const {
71 return 1.0; 15 return 1.0;
72 } 16 }
73 17
74 scoped_ptr<CubicBezierTimingFunction> CubicBezierTimingFunction::Create( 18 scoped_ptr<CubicBezierTimingFunction> CubicBezierTimingFunction::Create(
75 double x1, double y1, double x2, double y2) { 19 double x1, double y1, double x2, double y2) {
76 return make_scoped_ptr(new CubicBezierTimingFunction(x1, y1, x2, y2)); 20 return make_scoped_ptr(new CubicBezierTimingFunction(x1, y1, x2, y2));
77 } 21 }
78 22
79 CubicBezierTimingFunction::CubicBezierTimingFunction(double x1, 23 CubicBezierTimingFunction::CubicBezierTimingFunction(double x1,
80 double y1, 24 double y1,
81 double x2, 25 double x2,
82 double y2) 26 double y2)
83 : x1_(x1), y1_(y1), x2_(x2), y2_(y2) {} 27 : bezier_(x1, y1, x2, y2) {}
84 28
85 CubicBezierTimingFunction::~CubicBezierTimingFunction() {} 29 CubicBezierTimingFunction::~CubicBezierTimingFunction() {}
86 30
87 float CubicBezierTimingFunction::GetValue(double x) const { 31 float CubicBezierTimingFunction::GetValue(double x) const {
88 return static_cast<float>(bezier_interp(x1_, y1_, x2_, y2_, x)); 32 return static_cast<float>(bezier_.Solve(x));
89 } 33 }
90 34
91 scoped_ptr<AnimationCurve> CubicBezierTimingFunction::Clone() const { 35 scoped_ptr<AnimationCurve> CubicBezierTimingFunction::Clone() const {
92 return make_scoped_ptr( 36 return make_scoped_ptr(
93 new CubicBezierTimingFunction(*this)).PassAs<AnimationCurve>(); 37 new CubicBezierTimingFunction(*this)).PassAs<AnimationCurve>();
94 } 38 }
95 39
96 void CubicBezierTimingFunction::Range(float* min, float* max) const { 40 void CubicBezierTimingFunction::Range(float* min, float* max) const {
97 *min = 0.f; 41 double min_d = 0;
98 *max = 1.f; 42 double max_d = 1;
99 if (0.f <= y1_ && y1_ < 1.f && 0.f <= y2_ && y2_ <= 1.f) 43 bezier_.Range(&min_d, &max_d);
100 return; 44 *min = static_cast<float>(min_d);
101 45 *max = static_cast<float>(max_d);
102 // Represent the function's derivative in the form at^2 + bt + c.
103 float a = 3.f * (y1_ - y2_) + 1.f;
104 float b = 2.f * (y2_ - 2.f * y1_);
105 float c = y1_;
106
107 // Check if the derivative is constant.
108 if (std::abs(a) < kBezierEpsilon &&
109 std::abs(b) < kBezierEpsilon)
110 return;
111
112 // Zeros of the function's derivative.
113 float t_1 = 0.f;
114 float t_2 = 0.f;
115
116 if (std::abs(a) < kBezierEpsilon) {
117 // The function's derivative is linear.
118 t_1 = -c / b;
119 } else {
120 // The function's derivative is a quadratic. We find the zeros of this
121 // quadratic using the quadratic formula.
122 float discriminant = b * b - 4 * a * c;
123 if (discriminant < 0.f)
124 return;
125 float discriminant_sqrt = sqrt(discriminant);
126 t_1 = (-b + discriminant_sqrt) / (2.f * a);
127 t_2 = (-b - discriminant_sqrt) / (2.f * a);
128 }
129
130 float sol_1 = 0.f;
131 float sol_2 = 0.f;
132
133 if (0.f < t_1 && t_1 < 1.f)
134 sol_1 = eval_bezier(y1_, y2_, t_1);
135
136 if (0.f < t_2 && t_2 < 1.f)
137 sol_2 = eval_bezier(y1_, y2_, t_2);
138
139 *min = std::min(std::min(*min, sol_1), sol_2);
140 *max = std::max(std::max(*max, sol_1), sol_2);
141 } 46 }
142 47
143 // These numbers come from 48 // These numbers come from
144 // http://www.w3.org/TR/css3-transitions/#transition-timing-function_tag. 49 // http://www.w3.org/TR/css3-transitions/#transition-timing-function_tag.
145 scoped_ptr<TimingFunction> EaseTimingFunction::Create() { 50 scoped_ptr<TimingFunction> EaseTimingFunction::Create() {
146 return CubicBezierTimingFunction::Create( 51 return CubicBezierTimingFunction::Create(
147 0.25, 0.1, 0.25, 1.0).PassAs<TimingFunction>(); 52 0.25, 0.1, 0.25, 1.0).PassAs<TimingFunction>();
148 } 53 }
149 54
150 scoped_ptr<TimingFunction> EaseInTimingFunction::Create() { 55 scoped_ptr<TimingFunction> EaseInTimingFunction::Create() {
151 return CubicBezierTimingFunction::Create( 56 return CubicBezierTimingFunction::Create(
152 0.42, 0.0, 1.0, 1.0).PassAs<TimingFunction>(); 57 0.42, 0.0, 1.0, 1.0).PassAs<TimingFunction>();
153 } 58 }
154 59
155 scoped_ptr<TimingFunction> EaseOutTimingFunction::Create() { 60 scoped_ptr<TimingFunction> EaseOutTimingFunction::Create() {
156 return CubicBezierTimingFunction::Create( 61 return CubicBezierTimingFunction::Create(
157 0.0, 0.0, 0.58, 1.0).PassAs<TimingFunction>(); 62 0.0, 0.0, 0.58, 1.0).PassAs<TimingFunction>();
158 } 63 }
159 64
160 scoped_ptr<TimingFunction> EaseInOutTimingFunction::Create() { 65 scoped_ptr<TimingFunction> EaseInOutTimingFunction::Create() {
161 return CubicBezierTimingFunction::Create( 66 return CubicBezierTimingFunction::Create(
162 0.42, 0.0, 0.58, 1).PassAs<TimingFunction>(); 67 0.42, 0.0, 0.58, 1).PassAs<TimingFunction>();
163 } 68 }
164 69
165 } // namespace cc 70 } // namespace cc
OLDNEW
« no previous file with comments | « cc/animation/timing_function.h ('k') | cc/animation/timing_function_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698