summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/animation/css_rotate_interpolation_type.cc
blob: 73f2e2e773869d30998ea21f94abb14c67f7d10d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/renderer/core/animation/css_rotate_interpolation_type.h"

#include <memory>

#include "base/memory/ptr_util.h"
#include "third_party/blink/renderer/core/css/resolver/style_builder_converter.h"
#include "third_party/blink/renderer/core/style/computed_style.h"
#include "third_party/blink/renderer/platform/transforms/rotate_transform_operation.h"
#include "third_party/blink/renderer/platform/transforms/rotation.h"

namespace blink {

class OptionalRotation {
 public:
  OptionalRotation() : is_none_(true) {}

  explicit OptionalRotation(Rotation rotation)
      : rotation_(rotation), is_none_(false) {}

  bool IsNone() const { return is_none_; }
  const Rotation& GetRotation() const {
    DCHECK(!is_none_);
    return rotation_;
  }

  static OptionalRotation Add(const OptionalRotation& a,
                              const OptionalRotation& b) {
    if (a.IsNone())
      return b;
    if (b.IsNone())
      return a;
    return OptionalRotation(Rotation::Add(a.GetRotation(), b.GetRotation()));
  }
  static OptionalRotation Slerp(const OptionalRotation& from,
                                const OptionalRotation& to,
                                double progress) {
    if (from.IsNone() && to.IsNone())
      return OptionalRotation();

    return OptionalRotation(
        Rotation::Slerp(from.IsNone() ? Rotation() : from.GetRotation(),
                        to.IsNone() ? Rotation() : to.GetRotation(), progress));
  }

 private:
  Rotation rotation_;
  bool is_none_;
};

class CSSRotateNonInterpolableValue : public NonInterpolableValue {
 public:
  static scoped_refptr<CSSRotateNonInterpolableValue> Create(
      const OptionalRotation& rotation) {
    return base::AdoptRef(new CSSRotateNonInterpolableValue(
        true, rotation, OptionalRotation(), false, false));
  }

  static scoped_refptr<CSSRotateNonInterpolableValue> Create(
      const CSSRotateNonInterpolableValue& start,
      const CSSRotateNonInterpolableValue& end) {
    return base::AdoptRef(new CSSRotateNonInterpolableValue(
        false, start.GetOptionalRotation(), end.GetOptionalRotation(),
        start.IsAdditive(), end.IsAdditive()));
  }

  scoped_refptr<CSSRotateNonInterpolableValue> Composite(
      const CSSRotateNonInterpolableValue& other,
      double other_progress) {
    DCHECK(is_single_ && !is_start_additive_);
    if (other.is_single_) {
      DCHECK_EQ(other_progress, 0);
      DCHECK(other.IsAdditive());
      return Create(OptionalRotation::Add(GetOptionalRotation(),
                                          other.GetOptionalRotation()));
    }

    DCHECK(other.is_start_additive_ || other.is_end_additive_);
    OptionalRotation start =
        other.is_start_additive_
            ? OptionalRotation::Add(GetOptionalRotation(), other.start_)
            : other.start_;
    OptionalRotation end =
        other.is_end_additive_
            ? OptionalRotation::Add(GetOptionalRotation(), other.end_)
            : other.end_;
    return Create(OptionalRotation::Slerp(start, end, other_progress));
  }

  void SetSingleAdditive() {
    DCHECK(is_single_);
    is_start_additive_ = true;
  }

  OptionalRotation SlerpedRotation(double progress) const {
    DCHECK(!is_start_additive_ && !is_end_additive_);
    DCHECK(!is_single_ || progress == 0);
    if (progress == 0)
      return start_;
    if (progress == 1)
      return end_;
    return OptionalRotation::Slerp(start_, end_, progress);
  }

  DECLARE_NON_INTERPOLABLE_VALUE_TYPE();

 private:
  CSSRotateNonInterpolableValue(bool is_single,
                                const OptionalRotation& start,
                                const OptionalRotation& end,
                                bool is_start_additive,
                                bool is_end_additive)
      : is_single_(is_single),
        start_(start),
        end_(end),
        is_start_additive_(is_start_additive),
        is_end_additive_(is_end_additive) {}

  const OptionalRotation& GetOptionalRotation() const {
    DCHECK(is_single_);
    return start_;
  }
  bool IsAdditive() const {
    DCHECK(is_single_);
    return is_start_additive_;
  }

  bool is_single_;
  OptionalRotation start_;
  OptionalRotation end_;
  bool is_start_additive_;
  bool is_end_additive_;
};

DEFINE_NON_INTERPOLABLE_VALUE_TYPE(CSSRotateNonInterpolableValue);
DEFINE_NON_INTERPOLABLE_VALUE_TYPE_CASTS(CSSRotateNonInterpolableValue);

namespace {

OptionalRotation GetRotation(const ComputedStyle& style) {
  if (!style.Rotate())
    return OptionalRotation();
  return OptionalRotation(
      Rotation(style.Rotate()->Axis(), style.Rotate()->Angle()));
}

InterpolationValue ConvertRotation(const OptionalRotation& rotation) {
  return InterpolationValue(InterpolableNumber::Create(0),
                            CSSRotateNonInterpolableValue::Create(rotation));
}

class InheritedRotationChecker
    : public CSSInterpolationType::CSSConversionChecker {
 public:
  static std::unique_ptr<InheritedRotationChecker> Create(
      const OptionalRotation& inherited_rotation) {
    return base::WrapUnique(new InheritedRotationChecker(inherited_rotation));
  }

  bool IsValid(const StyleResolverState& state,
               const InterpolationValue& underlying) const final {
    OptionalRotation inherited_rotation = GetRotation(*state.ParentStyle());
    if (inherited_rotation_.IsNone() || inherited_rotation.IsNone())
      return inherited_rotation_.IsNone() == inherited_rotation.IsNone();
    return inherited_rotation_.GetRotation().axis ==
               inherited_rotation.GetRotation().axis &&
           inherited_rotation_.GetRotation().angle ==
               inherited_rotation.GetRotation().angle;
  }

 private:
  InheritedRotationChecker(const OptionalRotation& inherited_rotation)
      : inherited_rotation_(inherited_rotation) {}

  const OptionalRotation inherited_rotation_;
};

}  // namespace

InterpolationValue CSSRotateInterpolationType::MaybeConvertNeutral(
    const InterpolationValue& underlying,
    ConversionCheckers&) const {
  return ConvertRotation(OptionalRotation(Rotation()));
}

InterpolationValue CSSRotateInterpolationType::MaybeConvertInitial(
    const StyleResolverState&,
    ConversionCheckers&) const {
  return ConvertRotation(OptionalRotation());
}

InterpolationValue CSSRotateInterpolationType::MaybeConvertInherit(
    const StyleResolverState& state,
    ConversionCheckers& conversion_checkers) const {
  OptionalRotation inherited_rotation = GetRotation(*state.ParentStyle());
  conversion_checkers.push_back(
      InheritedRotationChecker::Create(inherited_rotation));
  return ConvertRotation(inherited_rotation);
}

InterpolationValue CSSRotateInterpolationType::MaybeConvertValue(
    const CSSValue& value,
    const StyleResolverState*,
    ConversionCheckers&) const {
  if (!value.IsBaseValueList()) {
    return ConvertRotation(OptionalRotation());
  }

  return ConvertRotation(
      OptionalRotation(StyleBuilderConverter::ConvertRotation(value)));
}

void CSSRotateInterpolationType::AdditiveKeyframeHook(
    InterpolationValue& value) const {
  ToCSSRotateNonInterpolableValue(*value.non_interpolable_value)
      .SetSingleAdditive();
}

PairwiseInterpolationValue CSSRotateInterpolationType::MaybeMergeSingles(
    InterpolationValue&& start,
    InterpolationValue&& end) const {
  return PairwiseInterpolationValue(
      InterpolableNumber::Create(0), InterpolableNumber::Create(1),
      CSSRotateNonInterpolableValue::Create(
          ToCSSRotateNonInterpolableValue(*start.non_interpolable_value),
          ToCSSRotateNonInterpolableValue(*end.non_interpolable_value)));
}

InterpolationValue
CSSRotateInterpolationType::MaybeConvertStandardPropertyUnderlyingValue(
    const ComputedStyle& style) const {
  return ConvertRotation(GetRotation(style));
}

void CSSRotateInterpolationType::Composite(
    UnderlyingValueOwner& underlying_value_owner,
    double underlying_fraction,
    const InterpolationValue& value,
    double interpolation_fraction) const {
  CSSRotateNonInterpolableValue& underlying_non_interpolable_value =
      ToCSSRotateNonInterpolableValue(
          *underlying_value_owner.Value().non_interpolable_value);
  const CSSRotateNonInterpolableValue& non_interpolable_value =
      ToCSSRotateNonInterpolableValue(*value.non_interpolable_value);
  double progress = ToInterpolableNumber(*value.interpolable_value).Value();
  underlying_value_owner.MutableValue().non_interpolable_value =
      underlying_non_interpolable_value.Composite(non_interpolable_value,
                                                  progress);
}

void CSSRotateInterpolationType::ApplyStandardPropertyValue(
    const InterpolableValue& interpolable_value,
    const NonInterpolableValue* untyped_non_interpolable_value,
    StyleResolverState& state) const {
  double progress = ToInterpolableNumber(interpolable_value).Value();
  const CSSRotateNonInterpolableValue& non_interpolable_value =
      ToCSSRotateNonInterpolableValue(*untyped_non_interpolable_value);
  OptionalRotation rotation = non_interpolable_value.SlerpedRotation(progress);
  if (rotation.IsNone()) {
    state.Style()->SetRotate(nullptr);
    return;
  }
  state.Style()->SetRotate(RotateTransformOperation::Create(
      rotation.GetRotation(), TransformOperation::kRotate3D));
}

}  // namespace blink