summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/animation/css_scale_interpolation_type.cc
blob: 9da7670ecb9574c91339c62b8db990b551a1b168 (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
271
272
273
274
275
276
277
278
279
280
281
282
// Copyright 2015 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_scale_interpolation_type.h"

#include <memory>
#include <utility>

#include "base/memory/ptr_util.h"
#include "third_party/blink/renderer/core/css/css_primitive_value.h"
#include "third_party/blink/renderer/core/css/css_value_list.h"
#include "third_party/blink/renderer/core/css/resolver/style_resolver_state.h"
#include "third_party/blink/renderer/core/style/computed_style.h"

namespace blink {

namespace {

struct Scale {
  Scale(double x, double y, double z) { Init(x, y, z, false); }
  explicit Scale() { Init(1, 1, 1, true); }
  explicit Scale(const ScaleTransformOperation* scale) {
    if (scale)
      Init(scale->X(), scale->Y(), scale->Z(), false);
    else
      Init(1, 1, 1, true);
  }
  explicit Scale(const InterpolableValue& value) {
    const auto& list = To<InterpolableList>(value);
    if (list.length() == 0) {
      Init(1, 1, 1, true);
      return;
    }
    Init(To<InterpolableNumber>(*list.Get(0)).Value(),
         To<InterpolableNumber>(*list.Get(1)).Value(),
         To<InterpolableNumber>(*list.Get(2)).Value(), false);
  }

  void Init(double x, double y, double z, bool is_value_none) {
    array[0] = x;
    array[1] = y;
    array[2] = z;
    is_none = is_value_none;
  }

  InterpolationValue CreateInterpolationValue() const;

  bool operator==(const Scale& other) const {
    for (size_t i = 0; i < 3; i++) {
      if (array[i] != other.array[i])
        return false;
    }
    return is_none == other.is_none;
  }

  double array[3];
  bool is_none;
};

std::unique_ptr<InterpolableValue> CreateScaleIdentity() {
  auto list = std::make_unique<InterpolableList>(3);
  for (wtf_size_t i = 0; i < 3; i++)
    list->Set(i, std::make_unique<InterpolableNumber>(1));
  return std::move(list);
}

class InheritedScaleChecker
    : public CSSInterpolationType::CSSConversionChecker {
 public:
  explicit InheritedScaleChecker(const Scale& scale) : scale_(scale) {}

 private:
  bool IsValid(const StyleResolverState& state,
               const InterpolationValue&) const final {
    return scale_ == Scale(state.ParentStyle()->Scale());
  }

  const Scale scale_;
};

}  // namespace

class CSSScaleNonInterpolableValue : public NonInterpolableValue {
 public:
  ~CSSScaleNonInterpolableValue() final = default;

  static scoped_refptr<CSSScaleNonInterpolableValue> Create(
      const Scale& scale) {
    return base::AdoptRef(
        new CSSScaleNonInterpolableValue(scale, scale, false, false));
  }

  static scoped_refptr<CSSScaleNonInterpolableValue> CreateAdditive(
      const CSSScaleNonInterpolableValue& other) {
    const bool is_additive = true;
    return base::AdoptRef(new CSSScaleNonInterpolableValue(
        other.start_, other.end_, is_additive, is_additive));
  }

  static scoped_refptr<CSSScaleNonInterpolableValue> Merge(
      const CSSScaleNonInterpolableValue& start,
      const CSSScaleNonInterpolableValue& end) {
    return base::AdoptRef(new CSSScaleNonInterpolableValue(
        start.Start(), end.end(), start.IsStartAdditive(),
        end.IsEndAdditive()));
  }

  const Scale& Start() const { return start_; }
  const Scale& end() const { return end_; }
  bool IsStartAdditive() const { return is_start_additive_; }
  bool IsEndAdditive() const { return is_end_additive_; }

  DECLARE_NON_INTERPOLABLE_VALUE_TYPE();

 private:
  CSSScaleNonInterpolableValue(const Scale& start,
                               const Scale& end,
                               bool is_start_additive,
                               bool is_end_additive)
      : start_(start),
        end_(end),
        is_start_additive_(is_start_additive),
        is_end_additive_(is_end_additive) {}

  const Scale start_;
  const Scale end_;
  bool is_start_additive_;
  bool is_end_additive_;
};

DEFINE_NON_INTERPOLABLE_VALUE_TYPE(CSSScaleNonInterpolableValue);
template <>
struct DowncastTraits<CSSScaleNonInterpolableValue> {
  static bool AllowFrom(const NonInterpolableValue* value) {
    return value && AllowFrom(*value);
  }
  static bool AllowFrom(const NonInterpolableValue& value) {
    return value.GetType() == CSSScaleNonInterpolableValue::static_type_;
  }
};

InterpolationValue Scale::CreateInterpolationValue() const {
  if (is_none) {
    return InterpolationValue(std::make_unique<InterpolableList>(0),
                              CSSScaleNonInterpolableValue::Create(*this));
  }

  auto list = std::make_unique<InterpolableList>(3);
  for (wtf_size_t i = 0; i < 3; i++)
    list->Set(i, std::make_unique<InterpolableNumber>(array[i]));
  return InterpolationValue(std::move(list),
                            CSSScaleNonInterpolableValue::Create(*this));
}

InterpolationValue CSSScaleInterpolationType::MaybeConvertNeutral(
    const InterpolationValue&,
    ConversionCheckers&) const {
  return Scale(1, 1, 1).CreateInterpolationValue();
}

InterpolationValue CSSScaleInterpolationType::MaybeConvertInitial(
    const StyleResolverState&,
    ConversionCheckers&) const {
  return Scale().CreateInterpolationValue();
}

InterpolationValue CSSScaleInterpolationType::MaybeConvertInherit(
    const StyleResolverState& state,
    ConversionCheckers& conversion_checkers) const {
  Scale inherited_scale(state.ParentStyle()->Scale());
  conversion_checkers.push_back(
      std::make_unique<InheritedScaleChecker>(inherited_scale));
  return inherited_scale.CreateInterpolationValue();
}

InterpolationValue CSSScaleInterpolationType::MaybeConvertValue(
    const CSSValue& value,
    const StyleResolverState*,
    ConversionCheckers&) const {
  if (!value.IsBaseValueList())
    return Scale().CreateInterpolationValue();

  const auto& list = To<CSSValueList>(value);
  DCHECK(list.length() >= 1 && list.length() <= 3);

  if (list.length() == 1) {
    double scale = To<CSSPrimitiveValue>(list.Item(0)).GetDoubleValue();
    // single value defines a 2d scale according to the spec
    // see https://drafts.csswg.org/css-transforms-2/#propdef-scale
    return Scale(scale, scale, 1).CreateInterpolationValue();
  } else if (list.length() == 2) {
    double x_scale = To<CSSPrimitiveValue>(list.Item(0)).GetDoubleValue();
    double y_scale = To<CSSPrimitiveValue>(list.Item(1)).GetDoubleValue();
    return Scale(x_scale, y_scale, 1).CreateInterpolationValue();
  } else {
    double x_scale = To<CSSPrimitiveValue>(list.Item(0)).GetDoubleValue();
    double y_scale = To<CSSPrimitiveValue>(list.Item(1)).GetDoubleValue();
    double z_scale = To<CSSPrimitiveValue>(list.Item(2)).GetDoubleValue();
    return Scale(x_scale, y_scale, z_scale).CreateInterpolationValue();
  }
}

InterpolationValue CSSScaleInterpolationType::PreInterpolationCompositeIfNeeded(
    InterpolationValue value,
    const InterpolationValue& underlying,
    EffectModel::CompositeOperation,
    ConversionCheckers&) const {
  value.non_interpolable_value = CSSScaleNonInterpolableValue::CreateAdditive(
      To<CSSScaleNonInterpolableValue>(*value.non_interpolable_value));
  return value;
}

PairwiseInterpolationValue CSSScaleInterpolationType::MaybeMergeSingles(
    InterpolationValue&& start,
    InterpolationValue&& end) const {
  wtf_size_t start_list_length =
      To<InterpolableList>(*start.interpolable_value).length();
  wtf_size_t end_list_length =
      To<InterpolableList>(*end.interpolable_value).length();
  if (start_list_length < end_list_length)
    start.interpolable_value = CreateScaleIdentity();
  else if (end_list_length < start_list_length)
    end.interpolable_value = CreateScaleIdentity();

  return PairwiseInterpolationValue(
      std::move(start.interpolable_value), std::move(end.interpolable_value),
      CSSScaleNonInterpolableValue::Merge(
          To<CSSScaleNonInterpolableValue>(*start.non_interpolable_value),
          To<CSSScaleNonInterpolableValue>(*end.non_interpolable_value)));
}

InterpolationValue
CSSScaleInterpolationType::MaybeConvertStandardPropertyUnderlyingValue(
    const ComputedStyle& style) const {
  return Scale(style.Scale()).CreateInterpolationValue();
}

void CSSScaleInterpolationType::Composite(
    UnderlyingValueOwner& underlying_value_owner,
    double underlying_fraction,
    const InterpolationValue& value,
    double interpolation_fraction) const {
  if (To<InterpolableList>(
          *underlying_value_owner.MutableValue().interpolable_value)
          .length() == 0) {
    underlying_value_owner.MutableValue().interpolable_value =
        CreateScaleIdentity();
  }

  const auto& metadata =
      To<CSSScaleNonInterpolableValue>(*value.non_interpolable_value);
  DCHECK(metadata.IsStartAdditive() || metadata.IsEndAdditive());

  auto& underlying_list = To<InterpolableList>(
      *underlying_value_owner.MutableValue().interpolable_value);
  for (wtf_size_t i = 0; i < 3; i++) {
    auto& underlying = To<InterpolableNumber>(*underlying_list.GetMutable(i));

    double start = metadata.Start().array[i] *
                   (metadata.IsStartAdditive() ? underlying.Value() : 1);
    double end = metadata.end().array[i] *
                 (metadata.IsEndAdditive() ? underlying.Value() : 1);
    underlying.Set(Blend(start, end, interpolation_fraction));
  }
}

void CSSScaleInterpolationType::ApplyStandardPropertyValue(
    const InterpolableValue& interpolable_value,
    const NonInterpolableValue*,
    StyleResolverState& state) const {
  Scale scale(interpolable_value);
  if (scale.is_none) {
    state.Style()->SetScale(nullptr);
    return;
  }
  state.Style()->SetScale(ScaleTransformOperation::Create(
      scale.array[0], scale.array[1], scale.array[2],
      TransformOperation::kScale3D));
}

}  // namespace blink