summaryrefslogtreecommitdiffstats
path: root/chromium/third_party/blink/renderer/core/animation/css_custom_list_interpolation_type.cc
blob: 509839e11f3ad70baf246aad546306f7e764dd1f (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
// Copyright 2018 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_custom_list_interpolation_type.h"

#include "third_party/blink/renderer/core/animation/interpolable_length.h"
#include "third_party/blink/renderer/core/animation/underlying_length_checker.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/platform/wtf/functional.h"

namespace blink {

InterpolationValue CSSCustomListInterpolationType::MaybeConvertNeutral(
    const InterpolationValue& underlying,
    ConversionCheckers& conversion_checkers) const {
  size_t underlying_length =
      UnderlyingLengthChecker::GetUnderlyingLength(underlying);
  conversion_checkers.push_back(
      std::make_unique<UnderlyingLengthChecker>(underlying_length));

  if (underlying_length == 0)
    return nullptr;

  InterpolationValue null_underlying(nullptr);
  ConversionCheckers null_checkers;

  auto convert_inner = [this, &null_underlying, &null_checkers](size_t) {
    return this->inner_interpolation_type_->MaybeConvertNeutral(null_underlying,
                                                                null_checkers);
  };

  return ListInterpolationFunctions::CreateList(underlying_length,
                                                convert_inner);
}

InterpolationValue CSSCustomListInterpolationType::MaybeConvertValue(
    const CSSValue& value,
    const StyleResolverState* state,
    ConversionCheckers&) const {
  const auto* list = DynamicTo<CSSValueList>(value);
  if (!list)
    return nullptr;

  ConversionCheckers null_checkers;

  return ListInterpolationFunctions::CreateList(
      list->length(), [this, list, state, &null_checkers](size_t index) {
        return this->inner_interpolation_type_->MaybeConvertValue(
            list->Item(index), state, null_checkers);
      });
}

const CSSValue* CSSCustomListInterpolationType::CreateCSSValue(
    const InterpolableValue& interpolable_value,
    const NonInterpolableValue* non_interpolable_value,
    const StyleResolverState& state) const {
  const auto& interpolable_list = To<InterpolableList>(interpolable_value);
  const auto* non_interpolable_list =
      DynamicTo<NonInterpolableList>(*non_interpolable_value);

  CSSValueList* list = nullptr;

  switch (syntax_repeat_) {
    default:
      NOTREACHED();
      FALLTHROUGH;
    case CSSSyntaxRepeat::kSpaceSeparated:
      list = CSSValueList::CreateSpaceSeparated();
      break;
    case CSSSyntaxRepeat::kCommaSeparated:
      list = CSSValueList::CreateCommaSeparated();
      break;
  }

  DCHECK(!non_interpolable_list ||
         interpolable_list.length() == non_interpolable_list->length());

  for (size_t i = 0; i < interpolable_list.length(); ++i) {
    const NonInterpolableValue* non_interpolable_single_value =
        non_interpolable_list ? non_interpolable_list->Get(i) : nullptr;
    list->Append(*inner_interpolation_type_->CreateCSSValue(
        *interpolable_list.Get(i), non_interpolable_single_value, state));
  }

  return list;
}

void CSSCustomListInterpolationType::Composite(
    UnderlyingValueOwner& underlying_value_owner,
    double underlying_fraction,
    const InterpolationValue& value,
    double interpolation_fraction) const {
  // This adapts a ListInterpolationFunctions::CompositeItemCallback function
  // such that we can use the InterpolationType::Composite function of the
  // inner interpolation type to get the answer.
  //
  // TODO(andruud): Make InterpolationType::Composite take an UnderlyingValue
  // rather than an UnderlyingValueOwner.
  auto composite_callback =
      [](const CSSInterpolationType* interpolation_type,
         double interpolation_fraction, UnderlyingValue& underlying_value,
         double underlying_fraction,
         const InterpolableValue& interpolable_value,
         const NonInterpolableValue* non_interpolable_value) {
        UnderlyingValueOwner owner;
        owner.Set(*interpolation_type,
                  InterpolationValue(
                      underlying_value.MutableInterpolableValue().Clone(),
                      underlying_value.GetNonInterpolableValue()));

        InterpolationValue interpolation_value(interpolable_value.Clone(),
                                               non_interpolable_value);
        interpolation_type->Composite(owner, underlying_fraction,
                                      interpolation_value,
                                      interpolation_fraction);

        underlying_value.SetInterpolableValue(
            owner.Value().Clone().interpolable_value);
        underlying_value.SetNonInterpolableValue(
            owner.GetNonInterpolableValue());
      };

  ListInterpolationFunctions::Composite(
      underlying_value_owner, underlying_fraction, *this, value,
      ListInterpolationFunctions::LengthMatchingStrategy::kEqual,
      WTF::BindRepeating(
          ListInterpolationFunctions::InterpolableValuesKnownCompatible),
      GetNonInterpolableValuesAreCompatibleCallback(),
      WTF::BindRepeating(composite_callback,
                         WTF::Unretained(inner_interpolation_type_.get()),
                         interpolation_fraction));
}

PairwiseInterpolationValue CSSCustomListInterpolationType::MaybeMergeSingles(
    InterpolationValue&& start,
    InterpolationValue&& end) const {
  return ListInterpolationFunctions::MaybeMergeSingles(
      std::move(start), std::move(end),
      ListInterpolationFunctions::LengthMatchingStrategy::kEqual,
      WTF::BindRepeating(&CSSInterpolationType::MaybeMergeSingles,
                         WTF::Unretained(inner_interpolation_type_.get())));
}

ListInterpolationFunctions::NonInterpolableValuesAreCompatibleCallback
CSSCustomListInterpolationType::GetNonInterpolableValuesAreCompatibleCallback()
    const {
  // TODO(https://crbug.com/981537): Add support for <image> here.
  // TODO(https://crbug.com/981538): Add support for <transform-function> here.
  // TODO(https://crbug.com/981542): Add support for <transform-list> here.
  return WTF::BindRepeating(
      ListInterpolationFunctions::VerifyNoNonInterpolableValues);
}

}  // namespace blink