transform_operations.cc revision 3551c9c881056c480085172ff9840cab31610854
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.
4
5#include "cc/animation/transform_operations.h"
6
7#include <algorithm>
8
9#include "ui/gfx/box_f.h"
10#include "ui/gfx/transform_util.h"
11#include "ui/gfx/vector3d_f.h"
12
13namespace cc {
14
15TransformOperations::TransformOperations()
16    : decomposed_transform_dirty_(true) {
17}
18
19TransformOperations::TransformOperations(const TransformOperations& other) {
20  operations_ = other.operations_;
21  decomposed_transform_dirty_ = other.decomposed_transform_dirty_;
22  if (!decomposed_transform_dirty_) {
23    decomposed_transform_.reset(
24        new gfx::DecomposedTransform(*other.decomposed_transform_.get()));
25  }
26}
27
28TransformOperations::~TransformOperations() {
29}
30
31gfx::Transform TransformOperations::Apply() const {
32  gfx::Transform to_return;
33  for (size_t i = 0; i < operations_.size(); ++i)
34    to_return.PreconcatTransform(operations_[i].matrix);
35  return to_return;
36}
37
38gfx::Transform TransformOperations::Blend(
39    const TransformOperations& from, double progress) const {
40  gfx::Transform to_return;
41  BlendInternal(from, progress, &to_return);
42  return to_return;
43}
44
45bool TransformOperations::BlendedBoundsForBox(const gfx::BoxF& box,
46                                              const TransformOperations& from,
47                                              double min_progress,
48                                              double max_progress,
49                                              gfx::BoxF* bounds) const {
50  *bounds = box;
51
52  bool from_identity = from.IsIdentity();
53  bool to_identity = IsIdentity();
54  if (from_identity && to_identity)
55    return true;
56
57  if (!MatchesTypes(from))
58    return false;
59
60  size_t num_operations =
61      std::max(from_identity ? 0 : from.operations_.size(),
62               to_identity ? 0 : operations_.size());
63  for (size_t i = 0; i < num_operations; ++i) {
64    gfx::BoxF bounds_for_operation;
65    const TransformOperation* from_op =
66        from_identity ? NULL : &from.operations_[i];
67    const TransformOperation* to_op = to_identity ? NULL : &operations_[i];
68    if (!TransformOperation::BlendedBoundsForBox(*bounds,
69                                                 from_op,
70                                                 to_op,
71                                                 min_progress,
72                                                 max_progress,
73                                                 &bounds_for_operation))
74      return false;
75    *bounds = bounds_for_operation;
76  }
77
78  return true;
79}
80
81bool TransformOperations::MatchesTypes(const TransformOperations& other) const {
82  if (IsIdentity() || other.IsIdentity())
83    return true;
84
85  if (operations_.size() != other.operations_.size())
86    return false;
87
88  for (size_t i = 0; i < operations_.size(); ++i) {
89    if (operations_[i].type != other.operations_[i].type
90      && !operations_[i].IsIdentity()
91      && !other.operations_[i].IsIdentity())
92      return false;
93  }
94
95  return true;
96}
97
98bool TransformOperations::CanBlendWith(
99    const TransformOperations& other) const {
100  gfx::Transform dummy;
101  return BlendInternal(other, 0.5, &dummy);
102}
103
104void TransformOperations::AppendTranslate(double x, double y, double z) {
105  TransformOperation to_add;
106  to_add.matrix.Translate3d(x, y, z);
107  to_add.type = TransformOperation::TransformOperationTranslate;
108  to_add.translate.x = x;
109  to_add.translate.y = y;
110  to_add.translate.z = z;
111  operations_.push_back(to_add);
112  decomposed_transform_dirty_ = true;
113}
114
115void TransformOperations::AppendRotate(double x, double y, double z,
116                                       double degrees) {
117  TransformOperation to_add;
118  to_add.matrix.RotateAbout(gfx::Vector3dF(x, y, z), degrees);
119  to_add.type = TransformOperation::TransformOperationRotate;
120  to_add.rotate.axis.x = x;
121  to_add.rotate.axis.y = y;
122  to_add.rotate.axis.z = z;
123  to_add.rotate.angle = degrees;
124  operations_.push_back(to_add);
125  decomposed_transform_dirty_ = true;
126}
127
128void TransformOperations::AppendScale(double x, double y, double z) {
129  TransformOperation to_add;
130  to_add.matrix.Scale3d(x, y, z);
131  to_add.type = TransformOperation::TransformOperationScale;
132  to_add.scale.x = x;
133  to_add.scale.y = y;
134  to_add.scale.z = z;
135  operations_.push_back(to_add);
136  decomposed_transform_dirty_ = true;
137}
138
139void TransformOperations::AppendSkew(double x, double y) {
140  TransformOperation to_add;
141  to_add.matrix.SkewX(x);
142  to_add.matrix.SkewY(y);
143  to_add.type = TransformOperation::TransformOperationSkew;
144  to_add.skew.x = x;
145  to_add.skew.y = y;
146  operations_.push_back(to_add);
147  decomposed_transform_dirty_ = true;
148}
149
150void TransformOperations::AppendPerspective(double depth) {
151  TransformOperation to_add;
152  to_add.matrix.ApplyPerspectiveDepth(depth);
153  to_add.type = TransformOperation::TransformOperationPerspective;
154  to_add.perspective_depth = depth;
155  operations_.push_back(to_add);
156  decomposed_transform_dirty_ = true;
157}
158
159void TransformOperations::AppendMatrix(const gfx::Transform& matrix) {
160  TransformOperation to_add;
161  to_add.matrix = matrix;
162  to_add.type = TransformOperation::TransformOperationMatrix;
163  operations_.push_back(to_add);
164  decomposed_transform_dirty_ = true;
165}
166
167void TransformOperations::AppendIdentity() {
168  operations_.push_back(TransformOperation());
169}
170
171bool TransformOperations::IsIdentity() const {
172  for (size_t i = 0; i < operations_.size(); ++i) {
173    if (!operations_[i].IsIdentity())
174      return false;
175  }
176  return true;
177}
178
179bool TransformOperations::BlendInternal(const TransformOperations& from,
180                                        double progress,
181                                        gfx::Transform* result) const {
182  bool from_identity = from.IsIdentity();
183  bool to_identity = IsIdentity();
184  if (from_identity && to_identity)
185    return true;
186
187  if (MatchesTypes(from)) {
188    size_t num_operations =
189        std::max(from_identity ? 0 : from.operations_.size(),
190                 to_identity ? 0 : operations_.size());
191    for (size_t i = 0; i < num_operations; ++i) {
192      gfx::Transform blended;
193      if (!TransformOperation::BlendTransformOperations(
194          from_identity ? 0 : &from.operations_[i],
195          to_identity ? 0 : &operations_[i],
196          progress,
197          &blended))
198          return false;
199      result->PreconcatTransform(blended);
200    }
201    return true;
202  }
203
204  if (!ComputeDecomposedTransform() || !from.ComputeDecomposedTransform())
205    return false;
206
207  gfx::DecomposedTransform to_return;
208  if (!gfx::BlendDecomposedTransforms(&to_return,
209                                      *decomposed_transform_.get(),
210                                      *from.decomposed_transform_.get(),
211                                      progress))
212    return false;
213
214  *result = ComposeTransform(to_return);
215  return true;
216}
217
218bool TransformOperations::ComputeDecomposedTransform() const {
219  if (decomposed_transform_dirty_) {
220    if (!decomposed_transform_)
221      decomposed_transform_.reset(new gfx::DecomposedTransform());
222    gfx::Transform transform = Apply();
223    if (!gfx::DecomposeTransform(decomposed_transform_.get(), transform))
224      return false;
225    decomposed_transform_dirty_ = false;
226  }
227  return true;
228}
229
230}  // namespace cc
231