OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 <cmath> | 5 #include <cmath> |
6 #include <limits> | 6 #include <limits> |
7 | 7 |
8 #include "cc/transform_operation.h" | 8 #include "cc/transform_operation.h" |
9 | 9 #include "ui/gfx/vector3d_f.h" |
10 using WebKit::WebTransformationMatrix; | |
11 | 10 |
12 namespace { | 11 namespace { |
13 const double kAngleEpsilon = 1e-4; | 12 const double kAngleEpsilon = 1e-4; |
14 } | 13 } |
15 | 14 |
16 namespace cc { | 15 namespace cc { |
17 | 16 |
18 bool TransformOperation::IsIdentity() const { | 17 bool TransformOperation::IsIdentity() const { |
19 return matrix.isIdentity(); | 18 return matrix.IsIdentity(); |
20 } | 19 } |
21 | 20 |
22 static bool IsOperationIdentity(const TransformOperation* operation) { | 21 static bool IsOperationIdentity(const TransformOperation* operation) { |
23 return !operation || operation->IsIdentity(); | 22 return !operation || operation->IsIdentity(); |
24 } | 23 } |
25 | 24 |
26 static bool ShareSameAxis(const TransformOperation* from, | 25 static bool ShareSameAxis(const TransformOperation* from, |
27 const TransformOperation* to, | 26 const TransformOperation* to, |
28 double& axis_x, double& axis_y, double& axis_z, | 27 double& axis_x, double& axis_y, double& axis_z, |
29 double& angle_from) { | 28 double& angle_from) { |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 axis_y = to->rotate.axis.y; | 65 axis_y = to->rotate.axis.y; |
67 axis_z = to->rotate.axis.z; | 66 axis_z = to->rotate.axis.z; |
68 // If the axes are pointing in opposite directions, we need to reverse | 67 // If the axes are pointing in opposite directions, we need to reverse |
69 // the angle. | 68 // the angle. |
70 angle_from = dot > 0 ? from->rotate.angle : -from->rotate.angle; | 69 angle_from = dot > 0 ? from->rotate.angle : -from->rotate.angle; |
71 } | 70 } |
72 return result; | 71 return result; |
73 } | 72 } |
74 | 73 |
75 static double BlendDoubles(double from, double to, double progress) { | 74 static double BlendDoubles(double from, double to, double progress) { |
| 75 if (progress <= 0.0) |
| 76 return from; |
| 77 |
| 78 if (progress >= 1.0) |
| 79 return to; |
| 80 |
76 return from * (1 - progress) + to * progress; | 81 return from * (1 - progress) + to * progress; |
77 } | 82 } |
78 | 83 |
79 bool TransformOperation::BlendTransformOperations( | 84 bool TransformOperation::BlendTransformOperations( |
80 const TransformOperation* from, | 85 const TransformOperation* from, |
81 const TransformOperation* to, | 86 const TransformOperation* to, |
82 double progress, | 87 double progress, |
83 WebTransformationMatrix& result) { | 88 gfx::Transform& result) { |
84 if (IsOperationIdentity(from) && IsOperationIdentity(to)) | 89 if (IsOperationIdentity(from) && IsOperationIdentity(to)) |
85 return true; | 90 return true; |
86 | 91 |
87 TransformOperation::Type interpolation_type = | 92 TransformOperation::Type interpolation_type = |
88 TransformOperation::TransformOperationIdentity; | 93 TransformOperation::TransformOperationIdentity; |
89 if (IsOperationIdentity(to)) | 94 if (IsOperationIdentity(to)) |
90 interpolation_type = from->type; | 95 interpolation_type = from->type; |
91 else | 96 else |
92 interpolation_type = to->type; | 97 interpolation_type = to->type; |
93 | 98 |
94 switch (interpolation_type) { | 99 switch (interpolation_type) { |
95 case TransformOperation::TransformOperationTranslate: { | 100 case TransformOperation::TransformOperationTranslate: { |
96 double from_x = IsOperationIdentity(from) ? 0 : from->translate.x; | 101 double from_x = IsOperationIdentity(from) ? 0 : from->translate.x; |
97 double from_y = IsOperationIdentity(from) ? 0 : from->translate.y; | 102 double from_y = IsOperationIdentity(from) ? 0 : from->translate.y; |
98 double from_z = IsOperationIdentity(from) ? 0 : from->translate.z; | 103 double from_z = IsOperationIdentity(from) ? 0 : from->translate.z; |
99 double to_x = IsOperationIdentity(to) ? 0 : to->translate.x; | 104 double to_x = IsOperationIdentity(to) ? 0 : to->translate.x; |
100 double to_y = IsOperationIdentity(to) ? 0 : to->translate.y; | 105 double to_y = IsOperationIdentity(to) ? 0 : to->translate.y; |
101 double to_z = IsOperationIdentity(to) ? 0 : to->translate.z; | 106 double to_z = IsOperationIdentity(to) ? 0 : to->translate.z; |
102 result.translate3d(BlendDoubles(from_x, to_x, progress), | 107 result.Translate3d(BlendDoubles(from_x, to_x, progress), |
103 BlendDoubles(from_y, to_y, progress), | 108 BlendDoubles(from_y, to_y, progress), |
104 BlendDoubles(from_z, to_z, progress)); | 109 BlendDoubles(from_z, to_z, progress)); |
105 break; | 110 break; |
106 } | 111 } |
107 case TransformOperation::TransformOperationRotate: { | 112 case TransformOperation::TransformOperationRotate: { |
108 double axis_x = 0; | 113 double axis_x = 0; |
109 double axis_y = 0; | 114 double axis_y = 0; |
110 double axis_z = 1; | 115 double axis_z = 1; |
111 double from_angle = 0; | 116 double from_angle = 0; |
112 double to_angle = IsOperationIdentity(to) ? 0 : to->rotate.angle; | 117 double to_angle = IsOperationIdentity(to) ? 0 : to->rotate.angle; |
113 if (ShareSameAxis(from, to, axis_x, axis_y, axis_z, from_angle)) | 118 if (ShareSameAxis(from, to, axis_x, axis_y, axis_z, from_angle)) |
114 result.rotate3d(axis_x, axis_y, axis_z, | 119 result.RotateAbout(gfx::Vector3dF(axis_x, axis_y, axis_z), |
115 BlendDoubles(from_angle, to_angle, progress)); | 120 BlendDoubles(from_angle, to_angle, progress)); |
116 else { | 121 else { |
117 WebTransformationMatrix to_matrix; | 122 gfx::Transform to_matrix; |
118 if (!IsOperationIdentity(to)) | 123 if (!IsOperationIdentity(to)) |
119 to_matrix = to->matrix; | 124 to_matrix = to->matrix; |
120 WebTransformationMatrix from_matrix; | 125 gfx::Transform from_matrix; |
121 if (!IsOperationIdentity(from)) | 126 if (!IsOperationIdentity(from)) |
122 from_matrix = from->matrix; | 127 from_matrix = from->matrix; |
123 result = to_matrix; | 128 result = to_matrix; |
124 if (!result.blend(from_matrix, progress)) | 129 if (!result.Blend(from_matrix, progress)) |
125 return false; | 130 return false; |
126 } | 131 } |
127 break; | 132 break; |
128 } | 133 } |
129 case TransformOperation::TransformOperationScale: { | 134 case TransformOperation::TransformOperationScale: { |
130 double from_x = IsOperationIdentity(from) ? 1 : from->scale.x; | 135 double from_x = IsOperationIdentity(from) ? 1 : from->scale.x; |
131 double from_y = IsOperationIdentity(from) ? 1 : from->scale.y; | 136 double from_y = IsOperationIdentity(from) ? 1 : from->scale.y; |
132 double from_z = IsOperationIdentity(from) ? 1 : from->scale.z; | 137 double from_z = IsOperationIdentity(from) ? 1 : from->scale.z; |
133 double to_x = IsOperationIdentity(to) ? 1 : to->scale.x; | 138 double to_x = IsOperationIdentity(to) ? 1 : to->scale.x; |
134 double to_y = IsOperationIdentity(to) ? 1 : to->scale.y; | 139 double to_y = IsOperationIdentity(to) ? 1 : to->scale.y; |
135 double to_z = IsOperationIdentity(to) ? 1 : to->scale.z; | 140 double to_z = IsOperationIdentity(to) ? 1 : to->scale.z; |
136 result.scale3d(BlendDoubles(from_x, to_x, progress), | 141 result.Scale3d(BlendDoubles(from_x, to_x, progress), |
137 BlendDoubles(from_y, to_y, progress), | 142 BlendDoubles(from_y, to_y, progress), |
138 BlendDoubles(from_z, to_z, progress)); | 143 BlendDoubles(from_z, to_z, progress)); |
139 break; | 144 break; |
140 } | 145 } |
141 case TransformOperation::TransformOperationSkew: { | 146 case TransformOperation::TransformOperationSkew: { |
142 double from_x = IsOperationIdentity(from) ? 0 : from->skew.x; | 147 double from_x = IsOperationIdentity(from) ? 0 : from->skew.x; |
143 double from_y = IsOperationIdentity(from) ? 0 : from->skew.y; | 148 double from_y = IsOperationIdentity(from) ? 0 : from->skew.y; |
144 double to_x = IsOperationIdentity(to) ? 0 : to->skew.x; | 149 double to_x = IsOperationIdentity(to) ? 0 : to->skew.x; |
145 double to_y = IsOperationIdentity(to) ? 0 : to->skew.y; | 150 double to_y = IsOperationIdentity(to) ? 0 : to->skew.y; |
146 result.skewX(BlendDoubles(from_x, to_x, progress)); | 151 result.SkewX(BlendDoubles(from_x, to_x, progress)); |
147 result.skewY(BlendDoubles(from_y, to_y, progress)); | 152 result.SkewY(BlendDoubles(from_y, to_y, progress)); |
148 break; | 153 break; |
149 } | 154 } |
150 case TransformOperation::TransformOperationPerspective: { | 155 case TransformOperation::TransformOperationPerspective: { |
151 double from_perspective_depth = IsOperationIdentity(from) ? | 156 double from_perspective_depth = IsOperationIdentity(from) ? |
152 std::numeric_limits<double>::max() : from->perspective_depth; | 157 std::numeric_limits<double>::max() : from->perspective_depth; |
153 double to_perspective_depth = IsOperationIdentity(to) ? | 158 double to_perspective_depth = IsOperationIdentity(to) ? |
154 std::numeric_limits<double>::max() : to->perspective_depth; | 159 std::numeric_limits<double>::max() : to->perspective_depth; |
155 result.applyPerspective( | 160 result.ApplyPerspectiveDepth( |
156 BlendDoubles(from_perspective_depth, to_perspective_depth, progress)); | 161 BlendDoubles(from_perspective_depth, to_perspective_depth, progress)); |
157 break; | 162 break; |
158 } | 163 } |
159 case TransformOperation::TransformOperationMatrix: { | 164 case TransformOperation::TransformOperationMatrix: { |
160 WebTransformationMatrix to_matrix; | 165 gfx::Transform to_matrix; |
161 if (!IsOperationIdentity(to)) | 166 if (!IsOperationIdentity(to)) |
162 to_matrix = to->matrix; | 167 to_matrix = to->matrix; |
163 WebTransformationMatrix from_matrix; | 168 gfx::Transform from_matrix; |
164 if (!IsOperationIdentity(from)) | 169 if (!IsOperationIdentity(from)) |
165 from_matrix = from->matrix; | 170 from_matrix = from->matrix; |
166 result = to_matrix; | 171 result = to_matrix; |
167 if (!result.blend(from_matrix, progress)) | 172 if (!result.Blend(from_matrix, progress)) |
168 return false; | 173 return false; |
169 break; | 174 break; |
170 } | 175 } |
171 case TransformOperation::TransformOperationIdentity: | 176 case TransformOperation::TransformOperationIdentity: |
172 // Do nothing. | 177 // Do nothing. |
173 break; | 178 break; |
174 } | 179 } |
175 | 180 |
176 return true; | 181 return true; |
177 } | 182 } |
178 | 183 |
179 } // namespace cc | 184 } // namespace cc |
OLD | NEW |