12a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
22a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
32a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// found in the LICENSE file.
42a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
52a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "cc/animation/transform_operations.h"
6c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
7c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)#include <algorithm>
8c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
9c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch#include "ui/gfx/animation/tween.h"
103551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#include "ui/gfx/box_f.h"
112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ui/gfx/transform_util.h"
122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ui/gfx/vector3d_f.h"
132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace cc {
152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)TransformOperations::TransformOperations()
172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    : decomposed_transform_dirty_(true) {
182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)TransformOperations::TransformOperations(const TransformOperations& other) {
212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  operations_ = other.operations_;
222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  decomposed_transform_dirty_ = other.decomposed_transform_dirty_;
232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!decomposed_transform_dirty_) {
242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    decomposed_transform_.reset(
252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        new gfx::DecomposedTransform(*other.decomposed_transform_.get()));
262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)TransformOperations::~TransformOperations() {
302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)gfx::Transform TransformOperations::Apply() const {
332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  gfx::Transform to_return;
342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  for (size_t i = 0; i < operations_.size(); ++i)
352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    to_return.PreconcatTransform(operations_[i].matrix);
362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return to_return;
372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)gfx::Transform TransformOperations::Blend(const TransformOperations& from,
4058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                                          SkMScalar progress) const {
412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  gfx::Transform to_return;
422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  BlendInternal(from, progress, &to_return);
432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return to_return;
442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
463551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)bool TransformOperations::BlendedBoundsForBox(const gfx::BoxF& box,
473551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                                              const TransformOperations& from,
4858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                                              SkMScalar min_progress,
4958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                                              SkMScalar max_progress,
503551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                                              gfx::BoxF* bounds) const {
513551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  *bounds = box;
523551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
533551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  bool from_identity = from.IsIdentity();
543551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  bool to_identity = IsIdentity();
553551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  if (from_identity && to_identity)
563551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    return true;
573551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
583551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  if (!MatchesTypes(from))
593551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    return false;
603551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
613551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  size_t num_operations =
623551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      std::max(from_identity ? 0 : from.operations_.size(),
633551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)               to_identity ? 0 : operations_.size());
64cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
65cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Because we are squashing all of the matrices together when applying
66cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // them to the animation, we must apply them in reverse order when
67cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // not squashing them.
68cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  for (int i = num_operations - 1; i >= 0; --i) {
693551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    gfx::BoxF bounds_for_operation;
703551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    const TransformOperation* from_op =
713551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)        from_identity ? NULL : &from.operations_[i];
723551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    const TransformOperation* to_op = to_identity ? NULL : &operations_[i];
733551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    if (!TransformOperation::BlendedBoundsForBox(*bounds,
743551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                                                 from_op,
753551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                                                 to_op,
763551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                                                 min_progress,
773551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                                                 max_progress,
783551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                                                 &bounds_for_operation))
793551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      return false;
803551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    *bounds = bounds_for_operation;
813551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  }
823551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
833551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  return true;
843551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)}
853551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
86e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdochbool TransformOperations::AffectsScale() const {
87e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  for (size_t i = 0; i < operations_.size(); ++i) {
88e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch    if (operations_[i].type == TransformOperation::TransformOperationScale)
89e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch      return true;
90e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch    if (operations_[i].type == TransformOperation::TransformOperationMatrix &&
91e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch        !operations_[i].matrix.IsIdentityOrTranslation())
92e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch      return true;
93e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  }
94e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  return false;
95e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch}
96e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
97c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdochbool TransformOperations::IsTranslation() const {
98c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  for (size_t i = 0; i < operations_.size(); ++i) {
99c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    switch (operations_[i].type) {
100c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch      case TransformOperation::TransformOperationIdentity:
101c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch      case TransformOperation::TransformOperationTranslate:
102c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch        continue;
103c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch      case TransformOperation::TransformOperationMatrix:
104c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch        if (!operations_[i].matrix.IsIdentityOrTranslation())
105c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch          return false;
106c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch        continue;
107c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch      case TransformOperation::TransformOperationRotate:
108c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch      case TransformOperation::TransformOperationScale:
109c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch      case TransformOperation::TransformOperationSkew:
110c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch      case TransformOperation::TransformOperationPerspective:
111c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch        return false;
112c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    }
113c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  }
114c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  return true;
115c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch}
116c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
117c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdochbool TransformOperations::ScaleComponent(gfx::Vector3dF* scale) const {
118c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  *scale = gfx::Vector3dF(1.f, 1.f, 1.f);
119c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  bool has_scale_component = false;
120c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  for (size_t i = 0; i < operations_.size(); ++i) {
121c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    switch (operations_[i].type) {
122c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch      case TransformOperation::TransformOperationIdentity:
123c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch      case TransformOperation::TransformOperationTranslate:
124c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch        continue;
125c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch      case TransformOperation::TransformOperationMatrix:
126c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch        if (!operations_[i].matrix.IsIdentityOrTranslation())
127c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch          return false;
128c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch        continue;
129c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch      case TransformOperation::TransformOperationRotate:
130c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch      case TransformOperation::TransformOperationSkew:
131c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch      case TransformOperation::TransformOperationPerspective:
132c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch        return false;
133c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch      case TransformOperation::TransformOperationScale:
134c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch        if (has_scale_component)
135c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch          return false;
136c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch        has_scale_component = true;
137c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch        scale->Scale(operations_[i].scale.x,
138c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch                     operations_[i].scale.y,
139c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch                     operations_[i].scale.z);
140c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch    }
141c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  }
142c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  return true;
143c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch}
144c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
1452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool TransformOperations::MatchesTypes(const TransformOperations& other) const {
1462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (IsIdentity() || other.IsIdentity())
1472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return true;
1482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (operations_.size() != other.operations_.size())
1502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return false;
1512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  for (size_t i = 0; i < operations_.size(); ++i) {
1532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (operations_[i].type != other.operations_[i].type
1542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      && !operations_[i].IsIdentity()
1552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      && !other.operations_[i].IsIdentity())
1562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return false;
1572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
1582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return true;
1602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool TransformOperations::CanBlendWith(
1632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const TransformOperations& other) const {
1642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  gfx::Transform dummy;
1652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return BlendInternal(other, 0.5, &dummy);
1662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
16858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)void TransformOperations::AppendTranslate(SkMScalar x,
16958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                                          SkMScalar y,
17058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                                          SkMScalar z) {
1712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  TransformOperation to_add;
1722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  to_add.matrix.Translate3d(x, y, z);
1732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  to_add.type = TransformOperation::TransformOperationTranslate;
1742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  to_add.translate.x = x;
1752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  to_add.translate.y = y;
1762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  to_add.translate.z = z;
1772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  operations_.push_back(to_add);
1782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  decomposed_transform_dirty_ = true;
1792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
18158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)void TransformOperations::AppendRotate(SkMScalar x,
18258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                                       SkMScalar y,
18358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                                       SkMScalar z,
18458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                                       SkMScalar degrees) {
1852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  TransformOperation to_add;
1862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  to_add.matrix.RotateAbout(gfx::Vector3dF(x, y, z), degrees);
1872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  to_add.type = TransformOperation::TransformOperationRotate;
1882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  to_add.rotate.axis.x = x;
1892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  to_add.rotate.axis.y = y;
1902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  to_add.rotate.axis.z = z;
1912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  to_add.rotate.angle = degrees;
1922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  operations_.push_back(to_add);
1932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  decomposed_transform_dirty_ = true;
1942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
19658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)void TransformOperations::AppendScale(SkMScalar x, SkMScalar y, SkMScalar z) {
1972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  TransformOperation to_add;
1982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  to_add.matrix.Scale3d(x, y, z);
1992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  to_add.type = TransformOperation::TransformOperationScale;
2002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  to_add.scale.x = x;
2012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  to_add.scale.y = y;
2022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  to_add.scale.z = z;
2032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  operations_.push_back(to_add);
2042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  decomposed_transform_dirty_ = true;
2052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
20758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)void TransformOperations::AppendSkew(SkMScalar x, SkMScalar y) {
2082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  TransformOperation to_add;
2092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  to_add.matrix.SkewX(x);
2102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  to_add.matrix.SkewY(y);
2112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  to_add.type = TransformOperation::TransformOperationSkew;
2122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  to_add.skew.x = x;
2132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  to_add.skew.y = y;
2142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  operations_.push_back(to_add);
2152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  decomposed_transform_dirty_ = true;
2162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
21858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)void TransformOperations::AppendPerspective(SkMScalar depth) {
2192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  TransformOperation to_add;
2202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  to_add.matrix.ApplyPerspectiveDepth(depth);
2212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  to_add.type = TransformOperation::TransformOperationPerspective;
2222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  to_add.perspective_depth = depth;
2232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  operations_.push_back(to_add);
2242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  decomposed_transform_dirty_ = true;
2252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void TransformOperations::AppendMatrix(const gfx::Transform& matrix) {
2282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  TransformOperation to_add;
2292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  to_add.matrix = matrix;
2302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  to_add.type = TransformOperation::TransformOperationMatrix;
2312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  operations_.push_back(to_add);
2322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  decomposed_transform_dirty_ = true;
2332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void TransformOperations::AppendIdentity() {
2362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  operations_.push_back(TransformOperation());
2372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool TransformOperations::IsIdentity() const {
2402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  for (size_t i = 0; i < operations_.size(); ++i) {
2412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (!operations_[i].IsIdentity())
2422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return false;
2432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
2442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return true;
2452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool TransformOperations::BlendInternal(const TransformOperations& from,
24858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                                        SkMScalar progress,
2492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                        gfx::Transform* result) const {
2502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool from_identity = from.IsIdentity();
2512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool to_identity = IsIdentity();
2522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (from_identity && to_identity)
2532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return true;
2542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (MatchesTypes(from)) {
2562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    size_t num_operations =
2572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        std::max(from_identity ? 0 : from.operations_.size(),
2582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                 to_identity ? 0 : operations_.size());
2592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    for (size_t i = 0; i < num_operations; ++i) {
2602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      gfx::Transform blended;
2612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      if (!TransformOperation::BlendTransformOperations(
2622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          from_identity ? 0 : &from.operations_[i],
2632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          to_identity ? 0 : &operations_[i],
2642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          progress,
265c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)          &blended))
2662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          return false;
2672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      result->PreconcatTransform(blended);
2682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
2692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return true;
2702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
2712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!ComputeDecomposedTransform() || !from.ComputeDecomposedTransform())
2732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return false;
2742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  gfx::DecomposedTransform to_return;
2762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!gfx::BlendDecomposedTransforms(&to_return,
2772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                      *decomposed_transform_.get(),
2782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                      *from.decomposed_transform_.get(),
2792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                      progress))
2802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return false;
2812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  *result = ComposeTransform(to_return);
2832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return true;
2842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool TransformOperations::ComputeDecomposedTransform() const {
2872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (decomposed_transform_dirty_) {
2882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (!decomposed_transform_)
2892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      decomposed_transform_.reset(new gfx::DecomposedTransform());
2902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    gfx::Transform transform = Apply();
2912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (!gfx::DecomposeTransform(decomposed_transform_.get(), transform))
2922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return false;
2932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    decomposed_transform_dirty_ = false;
2942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
2952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return true;
2962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}  // namespace cc
299