1 // Copyright (c) 2012 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 "ui/compositor/transform_animation_curve_adapter.h"
6 
7 #include "base/memory/ptr_util.h"
8 
9 namespace ui {
10 
11 namespace {
12 
WrapTransform(const gfx::Transform & transform)13 static cc::TransformOperations WrapTransform(const gfx::Transform& transform) {
14   cc::TransformOperations operations;
15   operations.AppendMatrix(transform);
16   return operations;
17 }
18 
19 }  // namespace
20 
TransformAnimationCurveAdapter(gfx::Tween::Type tween_type,gfx::Transform initial_value,gfx::Transform target_value,base::TimeDelta duration)21 TransformAnimationCurveAdapter::TransformAnimationCurveAdapter(
22     gfx::Tween::Type tween_type,
23     gfx::Transform initial_value,
24     gfx::Transform target_value,
25     base::TimeDelta duration)
26     : tween_type_(tween_type),
27       initial_value_(initial_value),
28       initial_wrapped_value_(WrapTransform(initial_value)),
29       target_value_(target_value),
30       target_wrapped_value_(WrapTransform(target_value)),
31       duration_(duration) {
32   gfx::DecomposeTransform(&decomposed_initial_value_, initial_value);
33   gfx::DecomposeTransform(&decomposed_target_value_, target_value);
34 }
35 
36 TransformAnimationCurveAdapter::TransformAnimationCurveAdapter(
37     const TransformAnimationCurveAdapter& other) = default;
38 
~TransformAnimationCurveAdapter()39 TransformAnimationCurveAdapter::~TransformAnimationCurveAdapter() {
40 }
41 
Duration() const42 base::TimeDelta TransformAnimationCurveAdapter::Duration() const {
43   return duration_;
44 }
45 
Clone() const46 std::unique_ptr<cc::AnimationCurve> TransformAnimationCurveAdapter::Clone()
47     const {
48   return base::WrapUnique(new TransformAnimationCurveAdapter(
49       tween_type_, initial_value_, target_value_, duration_));
50 }
51 
GetValue(base::TimeDelta t) const52 cc::TransformOperations TransformAnimationCurveAdapter::GetValue(
53     base::TimeDelta t) const {
54   if (t >= duration_)
55     return target_wrapped_value_;
56   if (t <= base::TimeDelta())
57     return initial_wrapped_value_;
58 
59   gfx::DecomposedTransform to_return = gfx::BlendDecomposedTransforms(
60       decomposed_target_value_, decomposed_initial_value_,
61       gfx::Tween::CalculateValue(tween_type_, t / duration_));
62   return WrapTransform(gfx::ComposeTransform(to_return));
63 }
64 
IsTranslation() const65 bool TransformAnimationCurveAdapter::IsTranslation() const {
66   return initial_value_.IsIdentityOrTranslation() &&
67          target_value_.IsIdentityOrTranslation();
68 }
69 
PreservesAxisAlignment() const70 bool TransformAnimationCurveAdapter::PreservesAxisAlignment() const {
71   return (initial_value_.IsIdentity() ||
72           initial_value_.IsScaleOrTranslation()) &&
73          (target_value_.IsIdentity() || target_value_.IsScaleOrTranslation());
74 }
75 
AnimationStartScale(bool forward_direction,float * start_scale) const76 bool TransformAnimationCurveAdapter::AnimationStartScale(
77     bool forward_direction,
78     float* start_scale) const {
79   return false;
80 }
81 
MaximumTargetScale(bool forward_direction,float * max_scale) const82 bool TransformAnimationCurveAdapter::MaximumTargetScale(
83     bool forward_direction,
84     float* max_scale) const {
85   return false;
86 }
87 
InverseTransformCurveAdapter(TransformAnimationCurveAdapter base_curve,gfx::Transform initial_value,base::TimeDelta duration)88 InverseTransformCurveAdapter::InverseTransformCurveAdapter(
89     TransformAnimationCurveAdapter base_curve,
90     gfx::Transform initial_value,
91     base::TimeDelta duration)
92     : base_curve_(base_curve),
93       initial_value_(initial_value),
94       initial_wrapped_value_(WrapTransform(initial_value)),
95       duration_(duration) {
96   effective_initial_value_ =
97       base_curve_.GetValue(base::TimeDelta()).Apply() * initial_value_;
98 }
99 
~InverseTransformCurveAdapter()100 InverseTransformCurveAdapter::~InverseTransformCurveAdapter() {
101 }
102 
Duration() const103 base::TimeDelta InverseTransformCurveAdapter::Duration() const {
104   return duration_;
105 }
106 
Clone() const107 std::unique_ptr<cc::AnimationCurve> InverseTransformCurveAdapter::Clone()
108     const {
109   return base::WrapUnique(
110       new InverseTransformCurveAdapter(base_curve_, initial_value_, duration_));
111 }
112 
GetValue(base::TimeDelta t) const113 cc::TransformOperations InverseTransformCurveAdapter::GetValue(
114     base::TimeDelta t) const {
115   if (t <= base::TimeDelta())
116     return initial_wrapped_value_;
117 
118   gfx::Transform base_transform = base_curve_.GetValue(t).Apply();
119   // Invert base
120   gfx::Transform to_return(gfx::Transform::kSkipInitialization);
121   bool is_invertible = base_transform.GetInverse(&to_return);
122   DCHECK(is_invertible);
123 
124   to_return.PreconcatTransform(effective_initial_value_);
125 
126   return WrapTransform(to_return);
127 }
128 
IsTranslation() const129 bool InverseTransformCurveAdapter::IsTranslation() const {
130   return initial_value_.IsIdentityOrTranslation() &&
131          base_curve_.IsTranslation();
132 }
133 
PreservesAxisAlignment() const134 bool InverseTransformCurveAdapter::PreservesAxisAlignment() const {
135   return (initial_value_.IsIdentity() ||
136           initial_value_.IsScaleOrTranslation()) &&
137          (base_curve_.PreservesAxisAlignment());
138 }
139 
AnimationStartScale(bool forward_direction,float * start_scale) const140 bool InverseTransformCurveAdapter::AnimationStartScale(
141     bool forward_direction,
142     float* start_scale) const {
143   return false;
144 }
145 
MaximumTargetScale(bool forward_direction,float * max_scale) const146 bool InverseTransformCurveAdapter::MaximumTargetScale(bool forward_direction,
147                                                       float* max_scale) const {
148   return false;
149 }
150 
151 }  // namespace ui
152