Implements RLZTrackerDelegate on iOS.
[chromium-blink-merge.git] / cc / animation / transform_operations.cc
blob6631a2a980b0eefa426ed4582ec68a231156fd8c
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
3 // found in the LICENSE file.
5 #include "cc/animation/transform_operations.h"
7 #include <algorithm>
9 #include "ui/gfx/animation/tween.h"
10 #include "ui/gfx/geometry/box_f.h"
11 #include "ui/gfx/geometry/vector3d_f.h"
12 #include "ui/gfx/transform_util.h"
14 namespace cc {
16 TransformOperations::TransformOperations()
17 : decomposed_transform_dirty_(true) {
20 TransformOperations::TransformOperations(const TransformOperations& other) {
21 operations_ = other.operations_;
22 decomposed_transform_dirty_ = other.decomposed_transform_dirty_;
23 if (!decomposed_transform_dirty_) {
24 decomposed_transform_.reset(
25 new gfx::DecomposedTransform(*other.decomposed_transform_.get()));
29 TransformOperations::~TransformOperations() {
32 gfx::Transform TransformOperations::Apply() const {
33 gfx::Transform to_return;
34 for (size_t i = 0; i < operations_.size(); ++i)
35 to_return.PreconcatTransform(operations_[i].matrix);
36 return to_return;
39 gfx::Transform TransformOperations::Blend(const TransformOperations& from,
40 SkMScalar progress) const {
41 gfx::Transform to_return;
42 BlendInternal(from, progress, &to_return);
43 return to_return;
46 bool TransformOperations::BlendedBoundsForBox(const gfx::BoxF& box,
47 const TransformOperations& from,
48 SkMScalar min_progress,
49 SkMScalar max_progress,
50 gfx::BoxF* bounds) const {
51 *bounds = box;
53 bool from_identity = from.IsIdentity();
54 bool to_identity = IsIdentity();
55 if (from_identity && to_identity)
56 return true;
58 if (!MatchesTypes(from))
59 return false;
61 size_t num_operations = std::max(from_identity ? 0 : from.operations_.size(),
62 to_identity ? 0 : operations_.size());
64 // Because we are squashing all of the matrices together when applying
65 // them to the animation, we must apply them in reverse order when
66 // not squashing them.
67 for (size_t i = 0; i < num_operations; ++i) {
68 size_t operation_index = num_operations - 1 - i;
69 gfx::BoxF bounds_for_operation;
70 const TransformOperation* from_op =
71 from_identity ? nullptr : &from.operations_[operation_index];
72 const TransformOperation* to_op =
73 to_identity ? nullptr : &operations_[operation_index];
74 if (!TransformOperation::BlendedBoundsForBox(*bounds, from_op, to_op,
75 min_progress, max_progress,
76 &bounds_for_operation)) {
77 return false;
79 *bounds = bounds_for_operation;
82 return true;
85 bool TransformOperations::AffectsScale() const {
86 for (size_t i = 0; i < operations_.size(); ++i) {
87 if (operations_[i].type == TransformOperation::TRANSFORM_OPERATION_SCALE)
88 return true;
89 if (operations_[i].type == TransformOperation::TRANSFORM_OPERATION_MATRIX &&
90 !operations_[i].matrix.IsIdentityOrTranslation())
91 return true;
93 return false;
96 bool TransformOperations::PreservesAxisAlignment() const {
97 for (size_t i = 0; i < operations_.size(); ++i) {
98 switch (operations_[i].type) {
99 case TransformOperation::TRANSFORM_OPERATION_IDENTITY:
100 case TransformOperation::TRANSFORM_OPERATION_TRANSLATE:
101 case TransformOperation::TRANSFORM_OPERATION_SCALE:
102 continue;
103 case TransformOperation::TRANSFORM_OPERATION_MATRIX:
104 if (!operations_[i].matrix.IsIdentity() &&
105 !operations_[i].matrix.IsScaleOrTranslation())
106 return false;
107 continue;
108 case TransformOperation::TRANSFORM_OPERATION_ROTATE:
109 case TransformOperation::TRANSFORM_OPERATION_SKEW:
110 case TransformOperation::TRANSFORM_OPERATION_PERSPECTIVE:
111 return false;
114 return true;
117 bool TransformOperations::IsTranslation() const {
118 for (size_t i = 0; i < operations_.size(); ++i) {
119 switch (operations_[i].type) {
120 case TransformOperation::TRANSFORM_OPERATION_IDENTITY:
121 case TransformOperation::TRANSFORM_OPERATION_TRANSLATE:
122 continue;
123 case TransformOperation::TRANSFORM_OPERATION_MATRIX:
124 if (!operations_[i].matrix.IsIdentityOrTranslation())
125 return false;
126 continue;
127 case TransformOperation::TRANSFORM_OPERATION_ROTATE:
128 case TransformOperation::TRANSFORM_OPERATION_SCALE:
129 case TransformOperation::TRANSFORM_OPERATION_SKEW:
130 case TransformOperation::TRANSFORM_OPERATION_PERSPECTIVE:
131 return false;
134 return true;
137 bool TransformOperations::ScaleComponent(gfx::Vector3dF* scale) const {
138 *scale = gfx::Vector3dF(1.f, 1.f, 1.f);
139 bool has_scale_component = false;
140 for (size_t i = 0; i < operations_.size(); ++i) {
141 switch (operations_[i].type) {
142 case TransformOperation::TRANSFORM_OPERATION_IDENTITY:
143 case TransformOperation::TRANSFORM_OPERATION_TRANSLATE:
144 continue;
145 case TransformOperation::TRANSFORM_OPERATION_MATRIX:
146 if (!operations_[i].matrix.IsIdentityOrTranslation())
147 return false;
148 continue;
149 case TransformOperation::TRANSFORM_OPERATION_ROTATE:
150 case TransformOperation::TRANSFORM_OPERATION_SKEW:
151 case TransformOperation::TRANSFORM_OPERATION_PERSPECTIVE:
152 return false;
153 case TransformOperation::TRANSFORM_OPERATION_SCALE:
154 if (has_scale_component)
155 return false;
156 has_scale_component = true;
157 scale->Scale(operations_[i].scale.x,
158 operations_[i].scale.y,
159 operations_[i].scale.z);
162 return true;
165 bool TransformOperations::MatchesTypes(const TransformOperations& other) const {
166 if (operations_.size() == 0 || other.operations_.size() == 0)
167 return true;
169 if (operations_.size() != other.operations_.size())
170 return false;
172 for (size_t i = 0; i < operations_.size(); ++i) {
173 if (operations_[i].type != other.operations_[i].type)
174 return false;
177 return true;
180 bool TransformOperations::CanBlendWith(
181 const TransformOperations& other) const {
182 gfx::Transform dummy;
183 return BlendInternal(other, 0.5, &dummy);
186 void TransformOperations::AppendTranslate(SkMScalar x,
187 SkMScalar y,
188 SkMScalar z) {
189 TransformOperation to_add;
190 to_add.matrix.Translate3d(x, y, z);
191 to_add.type = TransformOperation::TRANSFORM_OPERATION_TRANSLATE;
192 to_add.translate.x = x;
193 to_add.translate.y = y;
194 to_add.translate.z = z;
195 operations_.push_back(to_add);
196 decomposed_transform_dirty_ = true;
199 void TransformOperations::AppendRotate(SkMScalar x,
200 SkMScalar y,
201 SkMScalar z,
202 SkMScalar degrees) {
203 TransformOperation to_add;
204 to_add.matrix.RotateAbout(gfx::Vector3dF(x, y, z), degrees);
205 to_add.type = TransformOperation::TRANSFORM_OPERATION_ROTATE;
206 to_add.rotate.axis.x = x;
207 to_add.rotate.axis.y = y;
208 to_add.rotate.axis.z = z;
209 to_add.rotate.angle = degrees;
210 operations_.push_back(to_add);
211 decomposed_transform_dirty_ = true;
214 void TransformOperations::AppendScale(SkMScalar x, SkMScalar y, SkMScalar z) {
215 TransformOperation to_add;
216 to_add.matrix.Scale3d(x, y, z);
217 to_add.type = TransformOperation::TRANSFORM_OPERATION_SCALE;
218 to_add.scale.x = x;
219 to_add.scale.y = y;
220 to_add.scale.z = z;
221 operations_.push_back(to_add);
222 decomposed_transform_dirty_ = true;
225 void TransformOperations::AppendSkew(SkMScalar x, SkMScalar y) {
226 TransformOperation to_add;
227 to_add.matrix.SkewX(x);
228 to_add.matrix.SkewY(y);
229 to_add.type = TransformOperation::TRANSFORM_OPERATION_SKEW;
230 to_add.skew.x = x;
231 to_add.skew.y = y;
232 operations_.push_back(to_add);
233 decomposed_transform_dirty_ = true;
236 void TransformOperations::AppendPerspective(SkMScalar depth) {
237 TransformOperation to_add;
238 to_add.matrix.ApplyPerspectiveDepth(depth);
239 to_add.type = TransformOperation::TRANSFORM_OPERATION_PERSPECTIVE;
240 to_add.perspective_depth = depth;
241 operations_.push_back(to_add);
242 decomposed_transform_dirty_ = true;
245 void TransformOperations::AppendMatrix(const gfx::Transform& matrix) {
246 TransformOperation to_add;
247 to_add.matrix = matrix;
248 to_add.type = TransformOperation::TRANSFORM_OPERATION_MATRIX;
249 operations_.push_back(to_add);
250 decomposed_transform_dirty_ = true;
253 void TransformOperations::AppendIdentity() {
254 operations_.push_back(TransformOperation());
257 bool TransformOperations::IsIdentity() const {
258 for (size_t i = 0; i < operations_.size(); ++i) {
259 if (!operations_[i].IsIdentity())
260 return false;
262 return true;
265 bool TransformOperations::BlendInternal(const TransformOperations& from,
266 SkMScalar progress,
267 gfx::Transform* result) const {
268 bool from_identity = from.IsIdentity();
269 bool to_identity = IsIdentity();
270 if (from_identity && to_identity)
271 return true;
273 if (MatchesTypes(from)) {
274 size_t num_operations =
275 std::max(from_identity ? 0 : from.operations_.size(),
276 to_identity ? 0 : operations_.size());
277 for (size_t i = 0; i < num_operations; ++i) {
278 gfx::Transform blended;
279 if (!TransformOperation::BlendTransformOperations(
280 from.operations_.size() <= i ? 0 : &from.operations_[i],
281 operations_.size() <= i ? 0 : &operations_[i], progress,
282 &blended))
283 return false;
284 result->PreconcatTransform(blended);
286 return true;
289 if (!ComputeDecomposedTransform() || !from.ComputeDecomposedTransform())
290 return false;
292 gfx::DecomposedTransform to_return;
293 if (!gfx::BlendDecomposedTransforms(&to_return,
294 *decomposed_transform_.get(),
295 *from.decomposed_transform_.get(),
296 progress))
297 return false;
299 *result = ComposeTransform(to_return);
300 return true;
303 bool TransformOperations::ComputeDecomposedTransform() const {
304 if (decomposed_transform_dirty_) {
305 if (!decomposed_transform_)
306 decomposed_transform_.reset(new gfx::DecomposedTransform());
307 gfx::Transform transform = Apply();
308 if (!gfx::DecomposeTransform(decomposed_transform_.get(), transform))
309 return false;
310 decomposed_transform_dirty_ = false;
312 return true;
315 } // namespace cc