File: StyleInterpolation.cpp

package info (click to toggle)
webkit2gtk 2.51.2-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 457,708 kB
  • sloc: cpp: 3,884,629; javascript: 198,661; ansic: 165,298; python: 49,171; asm: 21,849; ruby: 18,095; perl: 16,914; xml: 4,623; sh: 2,397; yacc: 2,356; java: 2,019; lex: 1,330; pascal: 372; makefile: 197
file content (387 lines) | stat: -rw-r--r-- 18,211 bytes parent folder | download | duplicates (2)
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*
 * Copyright (C) 2007-2023 Apple Inc. All rights reserved.
 * Copyright (C) 2012, 2013 Adobe Systems Incorporated. All rights reserved.
 * Copyright (C) 2025 Sam Weinig. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1.  Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer. 
 * 2.  Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution. 
 * 3.  Neither the name of Apple Inc. ("Apple") nor the names of
 *     its contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission. 
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "config.h"
#include "StyleInterpolation.h"

#include "CSSRegisteredCustomProperty.h"
#include "StyleCustomProperty.h"
#include "StyleCustomPropertyRegistry.h"
#include "StyleInterpolationClient.h"
#include "StyleInterpolationFunctions.h"
#include "StyleInterpolationWrapperBase.h"
#include "StyleInterpolationWrapperMap.h"
#include <wtf/ZippedRange.h>

namespace WebCore::Style::Interpolation {

DEFINE_ALLOCATOR_WITH_HEAP_IDENTIFIER(StyleInterpolationWrapperBase);
// MARK: - Standard property interpolation support

static void interpolateStandardProperty(CSSPropertyID property, RenderStyle& destination, const RenderStyle& from, const RenderStyle& to, double progress, CompositeOperation compositeOperation, IterationCompositeOperation iterationCompositeOperation, double currentIteration, const Client& client)
{
    ASSERT(property != CSSPropertyInvalid && property != CSSPropertyCustom);

    auto* wrapper = WrapperMap::singleton().wrapper(property);
    if (!wrapper)
        return;

    auto isDiscrete = !wrapper->canInterpolate(from, to, compositeOperation);
    Context context { property, progress, isDiscrete, compositeOperation, iterationCompositeOperation, currentIteration, from.color(), to.color(), client };
    if (!CSSProperty::animationUsesNonNormalizedDiscreteInterpolation(property))
        context.normalizeProgress();
    wrapper->interpolate(destination, from, to, context);
#if !LOG_DISABLED
    wrapper->log(from, to, destination, progress);
#endif
}

// MARK: - Custom property interpolation support

static std::optional<CustomProperty::Value> interpolateSyntaxValues(const RenderStyle& fromStyle, const RenderStyle& toStyle, const CustomProperty::Value& from, const CustomProperty::Value& to, const Context& context)
{
    if (from.index() != to.index())
        return { };

    return WTF::switchOn(from,
        [&]<Numeric T>(const T& fromNumeric) -> std::optional<CustomProperty::Value> {
            return blend(fromNumeric, std::get<T>(to), context);
        },
        [&](const Color& fromStyleColor) -> std::optional<CustomProperty::Value> {
            auto& toStyleColor = std::get<Color>(to);
            if (!fromStyleColor.isCurrentColor() || !toStyleColor.isCurrentColor())
                return blendFunc(fromStyle.colorResolvingCurrentColor(fromStyleColor), toStyle.colorResolvingCurrentColor(toStyleColor), context);
            return { };
        },
        [&](const TransformFunction& fromTransform) -> std::optional<CustomProperty::Value> {
            return blend(fromTransform, std::get<TransformFunction>(to), context);
        },
        [&](const auto&) -> std::optional<CustomProperty::Value> {
            return { };
        }
    );
}

static std::optional<CustomProperty::Value> firstValueInSyntaxValueLists(const CustomProperty::ValueList& a, const CustomProperty::ValueList& b)
{
    if (!a.values.isEmpty())
        return a.values[0];
    if (!b.values.isEmpty())
        return b.values[0];
    return std::nullopt;
}

static std::optional<CustomProperty::ValueList> interpolateSyntaxValueLists(const RenderStyle& fromStyle, const RenderStyle& toStyle, const CustomProperty::ValueList& from, const CustomProperty::ValueList& to, const Context& context)
{
    // We should only attempt to interpolate lists containing the same types. Since we know all items in a
    // list are of the same type, it is sufficient to check the first value from each list.
    if (from.values.size() && to.values.size() && from.values.first().index() != to.values.first().index())
        return std::nullopt;

    // https://drafts.css-houdini.org/css-properties-values-api-1/#animation-behavior-of-custom-properties
    auto firstValue = firstValueInSyntaxValueLists(from, to);

    if (!firstValue)
        return std::nullopt;

    // <transform-function> lists are special in that they don't require matching numbers of items.
    if (std::holds_alternative<TransformFunction>(*firstValue)) {
        auto transformListFromSyntaxValueList = [](const CustomProperty::ValueList& list) {
            return TransformList {
                TransformList::Container::map(list.values, [&](auto& syntaxValue) {
                    ASSERT(std::holds_alternative<TransformFunction>(syntaxValue));
                    return std::get<TransformFunction>(syntaxValue);
                })
            };
        };

        auto fromTransformList = transformListFromSyntaxValueList(from);
        auto toTransformList = transformListFromSyntaxValueList(to);
        auto interpolatedTransformList = blend(fromTransformList, toTransformList, context);

        auto interpolatedSyntaxValues = WTF::map(interpolatedTransformList, [](auto& transformFunction) -> CustomProperty::Value {
            return transformFunction;
        });

        return CustomProperty::ValueList { WTFMove(interpolatedSyntaxValues), from.separator };
    }

    // Other lists must have matching sizes.
    if (from.values.size() != to.values.size())
        return std::nullopt;

    Vector<CustomProperty::Value> interpolatedSyntaxValues;
    interpolatedSyntaxValues.reserveInitialCapacity(from.values.size());
    for (auto [fromValue, toValue] : zippedRange(from.values, to.values)) {
        auto interpolatedSyntaxValue = interpolateSyntaxValues(fromStyle, toStyle, fromValue, toValue, context);
        if (!interpolatedSyntaxValue)
            return std::nullopt;
        interpolatedSyntaxValues.append(*interpolatedSyntaxValue);
    }

    return CustomProperty::ValueList { interpolatedSyntaxValues, from.separator };
}

static Ref<const CustomProperty> interpolatedCustomProperty(const RenderStyle& fromStyle, const RenderStyle& toStyle, const CustomProperty& from, const CustomProperty& to, const Context& context)
{
    if (std::holds_alternative<CustomProperty::Value>(from.value()) && std::holds_alternative<CustomProperty::Value>(to.value())) {
        auto& fromSyntaxValue = std::get<CustomProperty::Value>(from.value());
        auto& toSyntaxValue = std::get<CustomProperty::Value>(to.value());
        if (auto interpolatedSyntaxValue = interpolateSyntaxValues(fromStyle, toStyle, fromSyntaxValue, toSyntaxValue, context))
            return CustomProperty::createForValue(from.name(), WTFMove(*interpolatedSyntaxValue));
    }

    if (std::holds_alternative<CustomProperty::ValueList>(from.value()) && std::holds_alternative<CustomProperty::ValueList>(to.value())) {
        auto& fromSyntaxValueList = std::get<CustomProperty::ValueList>(from.value());
        auto& toSyntaxValueList = std::get<CustomProperty::ValueList>(to.value());
        if (auto interpolatedSyntaxValueList = interpolateSyntaxValueLists(fromStyle, toStyle, fromSyntaxValueList, toSyntaxValueList, context))
            return CustomProperty::createForValueList(from.name(), WTFMove(*interpolatedSyntaxValueList));
    }

    // Use a discrete interpolation for all other cases.
    return context.progress < 0.5 ? from : to;
}

static std::pair<const CustomProperty*, const CustomProperty*> customPropertyValuesForInterpolation(const AtomString& customProperty, const RenderStyle& fromStyle, const RenderStyle& toStyle)
{
    return { fromStyle.customPropertyValue(customProperty), toStyle.customPropertyValue(customProperty) };
}

static void interpolateCustomProperty(const AtomString& customProperty, RenderStyle& destination, const RenderStyle& from, const RenderStyle& to, double progress, CompositeOperation compositeOperation, IterationCompositeOperation iterationCompositeOperation, double currentIteration, const Client& client)
{
    Context context { customProperty, progress, false, compositeOperation, iterationCompositeOperation, currentIteration, from.color(), to.color(), client };

    auto [fromValue, toValue] = customPropertyValuesForInterpolation(customProperty, from, to);
    if (!fromValue || !toValue)
        return;

    bool isInherited = client.document()->customPropertyRegistry().isInherited(customProperty);
    destination.setCustomPropertyValue(interpolatedCustomProperty(from, to, *fromValue, *toValue, context), isInherited);
}

static bool syntaxValuesRequireInterpolationForAccumulativeIteration(const CustomProperty::Value& a, const CustomProperty::Value& b, bool isList)
{
    return WTF::switchOn(a,
        [b, isList](const LengthPercentage<>& aLengthPercentage) {
            ASSERT(std::holds_alternative<LengthPercentage<>>(b));
            return !isList && Style::requiresInterpolationForAccumulativeIteration(aLengthPercentage, std::get<LengthPercentage<>>(b));
        },
        [](const RefPtr<TransformOperation>&) {
            return true;
        },
        [](const Color&) {
            return true;
        },
        [](auto&) {
            return false;
        }
    );
}

static bool typeOfSyntaxValueCanBeInterpolated(const CustomProperty::Value& syntaxValue)
{
    return WTF::switchOn(syntaxValue,
        []<Numeric T>(const T&) {
            return true;
        },
        [](const ImageWrapper&) {
            return false;
        },
        [](const Color&) {
            return true;
        },
        [](const URL&) {
            return false;
        },
        [](const CustomIdentifier&) {
            return false;
        },
        [](const String&) {
            return false;
        },
        [](const TransformFunction&) {
            return true;
        }
    );
}

// MARK: - Exposed functions

bool isAdditiveOrCumulative(const AnimatableCSSProperty& property)
{
    return WTF::switchOn(property,
        [](CSSPropertyID propertyId) {
            return !CSSProperty::animationUsesNonAdditiveOrCumulativeInterpolation(propertyId);
        },
        [](const AtomString&) {
            return true;
        }
    );
}

bool isAccelerated(const AnimatableCSSProperty& property, const Settings& settings)
{
    return WTF::switchOn(property,
        [&](CSSPropertyID propertyId) {
            return CSSProperty::animationIsAccelerated(propertyId, settings);
        },
        [](const AtomString&) {
            return false;
        }
    );
}

bool canInterpolate(const AnimatableCSSProperty& property)
{
    return WTF::switchOn(property,
        [](CSSPropertyID propertyId) {
            return propertyId == CSSPropertyCustom || !!WrapperMap::singleton().wrapper(propertyId);
        },
        [](const AtomString&) {
            // FIXME: This should only be true for properties that are registered custom properties.
            return true;
        }
    );
}

bool equals(const AnimatableCSSProperty& property, const RenderStyle& a, const RenderStyle& b, const Document&)
{
    return WTF::switchOn(property,
        [&](CSSPropertyID propertyId) {
            if (auto* wrapper = WrapperMap::singleton().wrapper(propertyId))
                return wrapper->equals(a, b);
            return true;
        },
        [&](const AtomString& customProperty) {
            auto [aCustomPropertyValue, bCustomPropertyValue] = customPropertyValuesForInterpolation(customProperty, a, b);
            if (aCustomPropertyValue && bCustomPropertyValue)
                return *aCustomPropertyValue == *bCustomPropertyValue;
            return !aCustomPropertyValue && !bCustomPropertyValue;
        }
    );
}

bool canInterpolate(const AnimatableCSSProperty& property, const RenderStyle& a, const RenderStyle& b, const Document&)
{
    return WTF::switchOn(property,
        [&](CSSPropertyID propertyId) {
            if (auto* wrapper = WrapperMap::singleton().wrapper(propertyId))
                return wrapper->canInterpolate(a, b, CompositeOperation::Replace);
            return true;
        },
        [&](const AtomString& customProperty) {
            auto [aCustomPropertyValue, bCustomPropertyValue] = customPropertyValuesForInterpolation(customProperty, a, b);
            if (!aCustomPropertyValue || !bCustomPropertyValue || aCustomPropertyValue == bCustomPropertyValue)
                return false;
            auto& aVariantValue = aCustomPropertyValue->value();
            auto& bVariantValue = bCustomPropertyValue->value();
            if (aVariantValue.index() != bVariantValue.index())
                return false;
            return WTF::switchOn(aVariantValue,
                [bVariantValue](const CustomProperty::ValueList& aValueList) {
                    auto bValueList = std::get<CustomProperty::ValueList>(bVariantValue);
                    if (aValueList == bValueList)
                        return false;
                    if (auto firstValue = firstValueInSyntaxValueLists(aValueList, bValueList)) {
                        // List sizes must match except for transform lists.
                        if (!std::holds_alternative<TransformFunction>(*firstValue)
                            && aValueList.values.size() != bValueList.values.size()) {
                            return false;
                        }
                        return typeOfSyntaxValueCanBeInterpolated(*firstValue);
                    }
                    return false;
                },
                [bVariantValue](const CustomProperty::Value& aSyntaxValue) {
                    auto bSyntaxValue = std::get<CustomProperty::Value>(bVariantValue);
                    return aSyntaxValue != bSyntaxValue && typeOfSyntaxValueCanBeInterpolated(aSyntaxValue);
                },
                [](auto&) {
                    return false;
                }
            );
        }
    );
}

void interpolate(const AnimatableCSSProperty& property, RenderStyle& destination, const RenderStyle& from, const RenderStyle& to, double progress, CompositeOperation compositeOperation, IterationCompositeOperation iterationCompositeOperation, double currentIteration, const Client& client)
{
    WTF::switchOn(property,
        [&](CSSPropertyID propertyId) {
            interpolateStandardProperty(propertyId, destination, from, to, progress, compositeOperation, iterationCompositeOperation, currentIteration, client);
        },
        [&](const AtomString& customProperty) {
            interpolateCustomProperty(customProperty, destination, from, to, progress, compositeOperation, iterationCompositeOperation, currentIteration, client);
        }
    );
}

void interpolate(const AnimatableCSSProperty& property, RenderStyle& destination, const RenderStyle& from, const RenderStyle& to, double progress, CompositeOperation compositeOperation, const Client& client)
{
    return interpolate(property, destination, from, to, progress, compositeOperation, IterationCompositeOperation::Replace, 0, client);
}

bool requiresInterpolationForAccumulativeIteration(const AnimatableCSSProperty& property, const RenderStyle& a, const RenderStyle& b, const Client&)
{
    return WTF::switchOn(property,
        [&](CSSPropertyID propertyId) {
            if (auto* wrapper = WrapperMap::singleton().wrapper(propertyId))
                return wrapper->requiresInterpolationForAccumulativeIteration(a, b);
            return false;
        },
        [&](const AtomString& customProperty) {
            auto [from, to] = customPropertyValuesForInterpolation(customProperty, a, b);
            if (!from || !to)
                return false;

            if (std::holds_alternative<CustomProperty::ValueList>(from->value()) && std::holds_alternative<CustomProperty::ValueList>(to->value())) {
                auto& fromSyntaxValues = std::get<CustomProperty::ValueList>(from->value()).values;
                auto& toSyntaxValues = std::get<CustomProperty::ValueList>(to->value()).values;
                if (fromSyntaxValues.size() != toSyntaxValues.size())
                    return false;
                for (auto [fromSyntaxValue, toSyntaxValue] : zippedRange(fromSyntaxValues, toSyntaxValues)) {
                    if (!syntaxValuesRequireInterpolationForAccumulativeIteration(fromSyntaxValue, toSyntaxValue, true))
                        return false;
                }
                return true;
            }

            if (std::holds_alternative<CustomProperty::Value>(from->value()) && std::holds_alternative<CustomProperty::Value>(to->value())) {
                auto& fromSyntaxValue = std::get<CustomProperty::Value>(from->value());
                auto& toSyntaxValue = std::get<CustomProperty::Value>(to->value());
                return syntaxValuesRequireInterpolationForAccumulativeIteration(fromSyntaxValue, toSyntaxValue, false);
            }

            return false;
        }
    );
}

} // namespace WebCore::Style::Interpolation