summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/animation/css_image_interpolation_type.cc
blob: b7d1d7c02f0218b959fc97db75e1a20922516033 (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
283
284
285
286
// 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_image_interpolation_type.h"

#include <memory>

#include "base/memory/ptr_util.h"
#include "third_party/blink/renderer/core/css/css_crossfade_value.h"
#include "third_party/blink/renderer/core/css/css_numeric_literal_value.h"
#include "third_party/blink/renderer/core/css/css_primitive_value.h"
#include "third_party/blink/renderer/core/css/css_property_names.h"
#include "third_party/blink/renderer/core/css/resolver/style_resolver_state.h"
#include "third_party/blink/renderer/core/style/computed_style.h"
#include "third_party/blink/renderer/core/style/style_image.h"

namespace blink {

namespace {
const StyleImage* GetStyleImage(const CSSProperty& property,
                                const ComputedStyle& style) {
  switch (property.PropertyID()) {
    case CSSPropertyID::kBorderImageSource:
      return style.BorderImageSource();
    case CSSPropertyID::kListStyleImage:
      return style.ListStyleImage();
    case CSSPropertyID::kWebkitMaskBoxImageSource:
      return style.MaskBoxImageSource();
    default:
      NOTREACHED();
      return nullptr;
  }
}
}  // namespace

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

  static scoped_refptr<CSSImageNonInterpolableValue> Create(CSSValue* start,
                                                            CSSValue* end) {
    return base::AdoptRef(new CSSImageNonInterpolableValue(start, end));
  }

  bool IsSingle() const { return is_single_; }
  bool Equals(const CSSImageNonInterpolableValue& other) const {
    return DataEquivalent(start_, other.start_) &&
           DataEquivalent(end_, other.end_);
  }

  static scoped_refptr<CSSImageNonInterpolableValue> Merge(
      scoped_refptr<const NonInterpolableValue> start,
      scoped_refptr<const NonInterpolableValue> end);

  CSSValue* Crossfade(double progress) const {
    if (is_single_ || progress <= 0)
      return start_;
    if (progress >= 1)
      return end_;
    return MakeGarbageCollected<cssvalue::CSSCrossfadeValue>(
        start_, end_,
        CSSNumericLiteralValue::Create(progress,
                                       CSSPrimitiveValue::UnitType::kNumber));
  }

  DECLARE_NON_INTERPOLABLE_VALUE_TYPE();

 private:
  CSSImageNonInterpolableValue(CSSValue* start, CSSValue* end)
      : start_(start), end_(end), is_single_(start_ == end_) {
    DCHECK(start_);
    DCHECK(end_);
  }

  Persistent<CSSValue> start_;
  Persistent<CSSValue> end_;
  const bool is_single_;
};

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

scoped_refptr<CSSImageNonInterpolableValue> CSSImageNonInterpolableValue::Merge(
    scoped_refptr<const NonInterpolableValue> start,
    scoped_refptr<const NonInterpolableValue> end) {
  const auto& start_image_pair = To<CSSImageNonInterpolableValue>(*start);
  const auto& end_image_pair = To<CSSImageNonInterpolableValue>(*end);
  DCHECK(start_image_pair.is_single_);
  DCHECK(end_image_pair.is_single_);
  return Create(start_image_pair.start_, end_image_pair.end_);
}

InterpolationValue CSSImageInterpolationType::MaybeConvertStyleImage(
    const StyleImage& style_image,
    bool accept_gradients) {
  return MaybeConvertCSSValue(*style_image.CssValue(), accept_gradients);
}

InterpolationValue CSSImageInterpolationType::MaybeConvertCSSValue(
    const CSSValue& value,
    bool accept_gradients) {
  if (value.IsImageValue() || (value.IsGradientValue() && accept_gradients)) {
    CSSValue* refable_css_value = const_cast<CSSValue*>(&value);
    return InterpolationValue(std::make_unique<InterpolableNumber>(1),
                              CSSImageNonInterpolableValue::Create(
                                  refable_css_value, refable_css_value));
  }
  return nullptr;
}

PairwiseInterpolationValue
CSSImageInterpolationType::StaticMergeSingleConversions(
    InterpolationValue&& start,
    InterpolationValue&& end) {
  if (!To<CSSImageNonInterpolableValue>(*start.non_interpolable_value)
           .IsSingle() ||
      !To<CSSImageNonInterpolableValue>(*end.non_interpolable_value)
           .IsSingle()) {
    return nullptr;
  }
  return PairwiseInterpolationValue(
      std::make_unique<InterpolableNumber>(0),
      std::make_unique<InterpolableNumber>(1),
      CSSImageNonInterpolableValue::Merge(start.non_interpolable_value,
                                          end.non_interpolable_value));
}

const CSSValue* CSSImageInterpolationType::CreateCSSValue(
    const InterpolableValue& interpolable_value,
    const NonInterpolableValue* non_interpolable_value,
    const StyleResolverState&) const {
  return StaticCreateCSSValue(interpolable_value, non_interpolable_value);
}

const CSSValue* CSSImageInterpolationType::StaticCreateCSSValue(
    const InterpolableValue& interpolable_value,
    const NonInterpolableValue* non_interpolable_value) {
  return To<CSSImageNonInterpolableValue>(non_interpolable_value)
      ->Crossfade(To<InterpolableNumber>(interpolable_value).Value());
}

StyleImage* CSSImageInterpolationType::ResolveStyleImage(
    const CSSProperty& property,
    const InterpolableValue& interpolable_value,
    const NonInterpolableValue* non_interpolable_value,
    StyleResolverState& state) {
  const CSSValue* image =
      StaticCreateCSSValue(interpolable_value, non_interpolable_value);
  return state.GetStyleImage(property.PropertyID(), *image);
}

bool CSSImageInterpolationType::EqualNonInterpolableValues(
    const NonInterpolableValue* a,
    const NonInterpolableValue* b) {
  return To<CSSImageNonInterpolableValue>(*a).Equals(
      To<CSSImageNonInterpolableValue>(*b));
}

class UnderlyingImageChecker
    : public CSSInterpolationType::CSSConversionChecker {
 public:
  UnderlyingImageChecker(const InterpolationValue& underlying)
      : underlying_(underlying.Clone()) {}
  ~UnderlyingImageChecker() final = default;

 private:
  bool IsValid(const StyleResolverState&,
               const InterpolationValue& underlying) const final {
    if (!underlying && !underlying_)
      return true;
    if (!underlying || !underlying_)
      return false;
    return underlying_.interpolable_value->Equals(
               *underlying.interpolable_value) &&
           CSSImageInterpolationType::EqualNonInterpolableValues(
               underlying_.non_interpolable_value.get(),
               underlying.non_interpolable_value.get());
  }

  const InterpolationValue underlying_;
};

InterpolationValue CSSImageInterpolationType::MaybeConvertNeutral(
    const InterpolationValue& underlying,
    ConversionCheckers& conversion_checkers) const {
  conversion_checkers.push_back(
      std::make_unique<UnderlyingImageChecker>(underlying));
  return InterpolationValue(underlying.Clone());
}

InterpolationValue CSSImageInterpolationType::MaybeConvertInitial(
    const StyleResolverState&,
    ConversionCheckers& conversion_checkers) const {
  return nullptr;
}

class InheritedImageChecker
    : public CSSInterpolationType::CSSConversionChecker {
 public:
  InheritedImageChecker(const CSSProperty& property,
                        StyleImage* inherited_image)
      : property_(property), inherited_image_(inherited_image) {}
  ~InheritedImageChecker() final = default;

 private:
  bool IsValid(const StyleResolverState& state,
               const InterpolationValue& underlying) const final {
    const StyleImage* inherited_image =
        GetStyleImage(property_, *state.ParentStyle());
    if (!inherited_image_ && !inherited_image)
      return true;
    if (!inherited_image_ || !inherited_image)
      return false;
    return *inherited_image_ == *inherited_image;
  }

  const CSSProperty& property_;
  Persistent<StyleImage> inherited_image_;
};

InterpolationValue CSSImageInterpolationType::MaybeConvertInherit(
    const StyleResolverState& state,
    ConversionCheckers& conversion_checkers) const {
  if (!state.ParentStyle())
    return nullptr;

  const StyleImage* inherited_image =
      GetStyleImage(CssProperty(), *state.ParentStyle());
  StyleImage* refable_image = const_cast<StyleImage*>(inherited_image);
  conversion_checkers.push_back(
      std::make_unique<InheritedImageChecker>(CssProperty(), refable_image));
  return MaybeConvertStyleImage(inherited_image, true);
}

InterpolationValue CSSImageInterpolationType::MaybeConvertValue(
    const CSSValue& value,
    const StyleResolverState*,
    ConversionCheckers&) const {
  return MaybeConvertCSSValue(value, true);
}

InterpolationValue
CSSImageInterpolationType::MaybeConvertStandardPropertyUnderlyingValue(
    const ComputedStyle& style) const {
  return MaybeConvertStyleImage(GetStyleImage(CssProperty(), style), true);
}

void CSSImageInterpolationType::Composite(
    UnderlyingValueOwner& underlying_value_owner,
    double underlying_fraction,
    const InterpolationValue& value,
    double interpolation_fraction) const {
  underlying_value_owner.Set(*this, value);
}

void CSSImageInterpolationType::ApplyStandardPropertyValue(
    const InterpolableValue& interpolable_value,
    const NonInterpolableValue* non_interpolable_value,
    StyleResolverState& state) const {
  StyleImage* image = ResolveStyleImage(CssProperty(), interpolable_value,
                                        non_interpolable_value, state);
  switch (CssProperty().PropertyID()) {
    case CSSPropertyID::kBorderImageSource:
      state.Style()->SetBorderImageSource(image);
      break;
    case CSSPropertyID::kListStyleImage:
      state.Style()->SetListStyleImage(image);
      break;
    case CSSPropertyID::kWebkitMaskBoxImageSource:
      state.Style()->SetMaskBoxImageSource(image);
      break;
    default:
      NOTREACHED();
  }
}

}  // namespace blink