787f0a028194d792ab2a23e2542f8e7e00a6c0b6
[WebKit-https.git] / Source / WebCore / page / animation / CSSPropertyAnimation.cpp
1 /*
2  * Copyright (C) 2007, 2008, 2009, 2013 Apple Inc. All rights reserved.
3  * Copyright (C) 2012, 2013 Adobe Systems Incorporated. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1.  Redistributions of source code must retain the above copyright
10  *     notice, this list of conditions and the following disclaimer. 
11  * 2.  Redistributions in binary form must reproduce the above copyright
12  *     notice, this list of conditions and the following disclaimer in the
13  *     documentation and/or other materials provided with the distribution. 
14  * 3.  Neither the name of Apple Inc. ("Apple") nor the names of
15  *     its contributors may be used to endorse or promote products derived
16  *     from this software without specific prior written permission. 
17  *
18  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
19  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
22  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include "config.h"
31 #include "CSSPropertyAnimation.h"
32
33 #include "AnimationBase.h"
34 #include "CSSComputedStyleDeclaration.h"
35 #include "CSSCrossfadeValue.h"
36 #include "CSSFilterImageValue.h"
37 #include "CSSImageGeneratorValue.h"
38 #include "CSSImageValue.h"
39 #include "CSSPrimitiveValue.h"
40 #include "CSSPropertyNames.h"
41 #include "CachedImage.h"
42 #include "ClipPathOperation.h"
43 #include "FloatConversion.h"
44 #include "IdentityTransformOperation.h"
45 #include "Matrix3DTransformOperation.h"
46 #include "MatrixTransformOperation.h"
47 #include "RenderBox.h"
48 #include "RenderStyle.h"
49 #include "StyleCachedImage.h"
50 #include "StyleGeneratedImage.h"
51 #include "StylePropertyShorthand.h"
52 #include "StyleResolver.h"
53 #include <algorithm>
54 #include <memory>
55 #include <wtf/MathExtras.h>
56 #include <wtf/Noncopyable.h>
57 #include <wtf/PointerComparison.h>
58 #include <wtf/RefCounted.h>
59
60 namespace WebCore {
61
62 static inline int blendFunc(const AnimationBase*, int from, int to, double progress)
63 {
64     return blend(from, to, progress);
65 }
66
67 static inline double blendFunc(const AnimationBase*, double from, double to, double progress)
68 {
69     return blend(from, to, progress);
70 }
71
72 static inline float blendFunc(const AnimationBase*, float from, float to, double progress)
73 {
74     return narrowPrecisionToFloat(from + (to - from) * progress);
75 }
76
77 static inline Color blendFunc(const AnimationBase*, const Color& from, const Color& to, double progress)
78 {
79     return blend(from, to, progress);
80 }
81
82 static inline Length blendFunc(const AnimationBase*, const Length& from, const Length& to, double progress)
83 {
84     return to.blend(from, progress);
85 }
86
87 static inline LengthSize blendFunc(const AnimationBase* anim, const LengthSize& from, const LengthSize& to, double progress)
88 {
89     return LengthSize(blendFunc(anim, from.width(), to.width(), progress),
90                       blendFunc(anim, from.height(), to.height(), progress));
91 }
92
93 static inline ShadowStyle blendFunc(const AnimationBase* anim, ShadowStyle from, ShadowStyle to, double progress)
94 {
95     if (from == to)
96         return to;
97
98     double fromVal = from == Normal ? 1 : 0;
99     double toVal = to == Normal ? 1 : 0;
100     double result = blendFunc(anim, fromVal, toVal, progress);
101     return result > 0 ? Normal : Inset;
102 }
103
104 static inline std::unique_ptr<ShadowData> blendFunc(const AnimationBase* anim, const ShadowData* from, const ShadowData* to, double progress)
105 {
106     ASSERT(from && to);
107     if (from->style() != to->style())
108         return std::make_unique<ShadowData>(*to);
109
110     return std::make_unique<ShadowData>(blend(from->location(), to->location(), progress),
111         blend(from->radius(), to->radius(), progress),
112         blend(from->spread(), to->spread(), progress),
113         blendFunc(anim, from->style(), to->style(), progress),
114         from->isWebkitBoxShadow(),
115         blend(from->color(), to->color(), progress));
116 }
117
118 static inline TransformOperations blendFunc(const AnimationBase* animation, const TransformOperations& from, const TransformOperations& to, double progress)
119 {
120     if (animation->transformFunctionListsMatch())
121         return to.blendByMatchingOperations(from, progress);
122     return to.blendByUsingMatrixInterpolation(from, progress, is<RenderBox>(*animation->renderer()) ? downcast<RenderBox>(*animation->renderer()).borderBoxRect().size() : LayoutSize());
123 }
124
125 static inline PassRefPtr<ClipPathOperation> blendFunc(const AnimationBase*, ClipPathOperation* from, ClipPathOperation* to, double progress)
126 {
127     if (!from || !to)
128         return to;
129
130     // Other clip-path operations than BasicShapes can not be animated.
131     if (from->type() != ClipPathOperation::Shape || to->type() != ClipPathOperation::Shape)
132         return to;
133
134     const BasicShape& fromShape = downcast<ShapeClipPathOperation>(*from).basicShape();
135     const BasicShape& toShape = downcast<ShapeClipPathOperation>(*to).basicShape();
136
137     if (!fromShape.canBlend(toShape))
138         return to;
139
140     return ShapeClipPathOperation::create(toShape.blend(fromShape, progress));
141 }
142
143 #if ENABLE(CSS_SHAPES)
144 static inline PassRefPtr<ShapeValue> blendFunc(const AnimationBase*, ShapeValue* from, ShapeValue* to, double progress)
145 {
146     if (!from || !to)
147         return to;
148
149     if (from->type() != ShapeValue::Type::Shape || to->type() != ShapeValue::Type::Shape)
150         return to;
151
152     if (from->cssBox() != to->cssBox())
153         return to;
154
155     const BasicShape& fromShape = *from->shape();
156     const BasicShape& toShape = *to->shape();
157
158     if (!fromShape.canBlend(toShape))
159         return to;
160
161     return ShapeValue::createShapeValue(toShape.blend(fromShape, progress), to->cssBox());
162 }
163 #endif
164
165 static inline PassRefPtr<FilterOperation> blendFunc(const AnimationBase* animation, FilterOperation* fromOp, FilterOperation* toOp, double progress, bool blendToPassthrough = false)
166 {
167     ASSERT(toOp);
168     if (toOp->blendingNeedsRendererSize()) {
169         LayoutSize size = is<RenderBox>(*animation->renderer()) ? downcast<RenderBox>(*animation->renderer()).borderBoxRect().size() : LayoutSize();
170         return toOp->blend(fromOp, progress, size, blendToPassthrough);
171     }
172     return toOp->blend(fromOp, progress, blendToPassthrough);
173 }
174
175 static inline FilterOperations blendFilterOperations(const AnimationBase* anim,  const FilterOperations& from, const FilterOperations& to, double progress)
176 {
177     FilterOperations result;
178     size_t fromSize = from.operations().size();
179     size_t toSize = to.operations().size();
180     size_t size = std::max(fromSize, toSize);
181     for (size_t i = 0; i < size; i++) {
182         RefPtr<FilterOperation> fromOp = (i < fromSize) ? from.operations()[i].get() : 0;
183         RefPtr<FilterOperation> toOp = (i < toSize) ? to.operations()[i].get() : 0;
184         RefPtr<FilterOperation> blendedOp = toOp ? blendFunc(anim, fromOp.get(), toOp.get(), progress) : (fromOp ? blendFunc(anim, 0, fromOp.get(), progress, true) : 0);
185         if (blendedOp)
186             result.operations().append(blendedOp);
187         else {
188             RefPtr<FilterOperation> identityOp = PassthroughFilterOperation::create();
189             if (progress > 0.5)
190                 result.operations().append(toOp ? toOp : identityOp);
191             else
192                 result.operations().append(fromOp ? fromOp : identityOp);
193         }
194     }
195     return result;
196 }
197
198 static inline FilterOperations blendFunc(const AnimationBase* anim, const FilterOperations& from, const FilterOperations& to, double progress, bool animatingBackdropFilter = false)
199 {
200     FilterOperations result;
201
202     // If we have a filter function list, use that to do a per-function animation.
203 #if ENABLE(FILTERS_LEVEL_2)
204     if ((!animatingBackdropFilter && anim->filterFunctionListsMatch()) || (animatingBackdropFilter && anim->backdropFilterFunctionListsMatch()))
205 #else
206     UNUSED_PARAM(animatingBackdropFilter);
207     if (anim->filterFunctionListsMatch())
208 #endif
209
210         result = blendFilterOperations(anim, from, to, progress);
211     else {
212         // If the filter function lists don't match, we could try to cross-fade, but don't yet have a way to represent that in CSS.
213         // For now we'll just fail to animate.
214         result = to;
215     }
216
217     return result;
218 }
219
220 static inline PassRefPtr<StyleImage> blendFilter(const AnimationBase* anim, CachedImage* image, const FilterOperations& from, const FilterOperations& to, double progress)
221 {
222     ASSERT(image);
223     FilterOperations filterResult = blendFilterOperations(anim, from, to, progress);
224
225     RefPtr<StyleCachedImage> styledImage = StyleCachedImage::create(image);
226     auto imageValue = CSSImageValue::create(image->url(), styledImage.get());
227     auto filterValue = ComputedStyleExtractor::valueForFilter(anim->renderer()->style(), filterResult, DoNotAdjustPixelValues);
228
229     auto result = CSSFilterImageValue::create(WTFMove(imageValue), WTFMove(filterValue));
230     result.get().setFilterOperations(filterResult);
231     return StyleGeneratedImage::create(WTFMove(result));
232 }
233
234 static inline EVisibility blendFunc(const AnimationBase* anim, EVisibility from, EVisibility to, double progress)
235 {
236     // Any non-zero result means we consider the object to be visible. Only at 0 do we consider the object to be
237     // invisible. The invisible value we use (HIDDEN vs. COLLAPSE) depends on the specified from/to values.
238     double fromVal = from == VISIBLE ? 1. : 0.;
239     double toVal = to == VISIBLE ? 1. : 0.;
240     if (fromVal == toVal)
241         return to;
242     double result = blendFunc(anim, fromVal, toVal, progress);
243     return result > 0. ? VISIBLE : (to != VISIBLE ? to : from);
244 }
245
246 static inline LengthBox blendFunc(const AnimationBase* anim, const LengthBox& from, const LengthBox& to, double progress)
247 {
248     LengthBox result(blendFunc(anim, from.top(), to.top(), progress),
249                      blendFunc(anim, from.right(), to.right(), progress),
250                      blendFunc(anim, from.bottom(), to.bottom(), progress),
251                      blendFunc(anim, from.left(), to.left(), progress));
252     return result;
253 }
254
255 static inline SVGLength blendFunc(const AnimationBase*, const SVGLength& from, const SVGLength& to, double progress)
256 {
257     return to.blend(from, narrowPrecisionToFloat(progress));
258 }
259
260 static inline Vector<SVGLength> blendFunc(const AnimationBase*, const Vector<SVGLength>& from, const Vector<SVGLength>& to, double progress)
261 {
262     size_t fromLength = from.size();
263     size_t toLength = to.size();
264     if (!fromLength)
265         return !progress ? from : to;
266     if (!toLength)
267         return progress == 1 ? from : to;
268     size_t resultLength = fromLength;
269     if (fromLength != toLength) {
270         if (!remainder(std::max(fromLength, toLength), std::min(fromLength, toLength)))
271             resultLength = std::max(fromLength, toLength);
272         else
273             resultLength = fromLength * toLength;
274     }
275     Vector<SVGLength> result(resultLength);
276     for (size_t i = 0; i < resultLength; ++i)
277         result[i] = to[i % toLength].blend(from[i % fromLength], narrowPrecisionToFloat(progress));
278     return result;
279 }
280
281 static inline PassRefPtr<StyleImage> crossfadeBlend(const AnimationBase*, StyleCachedImage* fromStyleImage, StyleCachedImage* toStyleImage, double progress)
282 {
283     // If progress is at one of the extremes, we want getComputedStyle to show the image,
284     // not a completed cross-fade, so we hand back one of the existing images.
285     if (!progress)
286         return fromStyleImage;
287     if (progress == 1)
288         return toStyleImage;
289
290     auto fromImageValue = CSSImageValue::create(fromStyleImage->cachedImage()->url(), fromStyleImage);
291     auto toImageValue = CSSImageValue::create(toStyleImage->cachedImage()->url(), toStyleImage);
292
293     auto crossfadeValue = CSSCrossfadeValue::create(WTFMove(fromImageValue), WTFMove(toImageValue));
294     crossfadeValue.get().setPercentage(CSSPrimitiveValue::create(progress, CSSPrimitiveValue::CSS_NUMBER));
295     return StyleGeneratedImage::create(WTFMove(crossfadeValue));
296 }
297
298 static inline PassRefPtr<StyleImage> blendFunc(const AnimationBase* anim, StyleImage* from, StyleImage* to, double progress)
299 {
300     if (!from || !to)
301         return to;
302
303     // Animation between two generated images. Cross fade for all other cases.
304     if (is<StyleGeneratedImage>(*from) && is<StyleGeneratedImage>(*to)) {
305         CSSImageGeneratorValue& fromGenerated = downcast<StyleGeneratedImage>(*from).imageValue();
306         CSSImageGeneratorValue& toGenerated = downcast<StyleGeneratedImage>(*to).imageValue();
307
308         if (is<CSSFilterImageValue>(fromGenerated) && is<CSSFilterImageValue>(toGenerated)) {
309             // Animation of generated images just possible if input images are equal.
310             // Otherwise fall back to cross fade animation.
311             CSSFilterImageValue& fromFilter = downcast<CSSFilterImageValue>(fromGenerated);
312             CSSFilterImageValue& toFilter = downcast<CSSFilterImageValue>(toGenerated);
313             if (fromFilter.equalInputImages(toFilter) && fromFilter.cachedImage())
314                 return blendFilter(anim, fromFilter.cachedImage(), fromFilter.filterOperations(), toFilter.filterOperations(), progress);
315         }
316
317         if (is<CSSCrossfadeValue>(fromGenerated) && is<CSSCrossfadeValue>(toGenerated)) {
318             CSSCrossfadeValue& fromCrossfade = downcast<CSSCrossfadeValue>(fromGenerated);
319             CSSCrossfadeValue& toCrossfade = downcast<CSSCrossfadeValue>(toGenerated);
320             if (fromCrossfade.equalInputImages(toCrossfade))
321                 return StyleGeneratedImage::create(*toCrossfade.blend(fromCrossfade, progress));
322         }
323
324         // FIXME: Add support for animation between two *gradient() functions.
325         // https://bugs.webkit.org/show_bug.cgi?id=119956
326     } else if (is<StyleGeneratedImage>(*from) && is<StyleCachedImage>(*to)) {
327         CSSImageGeneratorValue& fromGenerated = downcast<StyleGeneratedImage>(*from).imageValue();
328         if (is<CSSFilterImageValue>(fromGenerated)) {
329             CSSFilterImageValue& fromFilter = downcast<CSSFilterImageValue>(fromGenerated);
330             if (fromFilter.cachedImage() && downcast<StyleCachedImage>(*to).cachedImage() == fromFilter.cachedImage())
331                 return blendFilter(anim, fromFilter.cachedImage(), fromFilter.filterOperations(), FilterOperations(), progress);
332         }
333         // FIXME: Add interpolation between cross-fade and image source.
334     } else if (is<StyleCachedImage>(*from) && is<StyleGeneratedImage>(*to)) {
335         CSSImageGeneratorValue& toGenerated = downcast<StyleGeneratedImage>(*to).imageValue();
336         if (is<CSSFilterImageValue>(toGenerated)) {
337             CSSFilterImageValue& toFilter = downcast<CSSFilterImageValue>(toGenerated);
338             if (toFilter.cachedImage() && downcast<StyleCachedImage>(*from).cachedImage() == toFilter.cachedImage())
339                 return blendFilter(anim, toFilter.cachedImage(), FilterOperations(), toFilter.filterOperations(), progress);
340         }
341         // FIXME: Add interpolation between image source and cross-fade.
342     }
343
344     // FIXME: Add support cross fade between cached and generated images.
345     // https://bugs.webkit.org/show_bug.cgi?id=78293
346     if (is<StyleCachedImage>(*from) && is<StyleCachedImage>(*to))
347         return crossfadeBlend(anim, downcast<StyleCachedImage>(from), downcast<StyleCachedImage>(to), progress);
348
349     return to;
350 }
351
352 static inline NinePieceImage blendFunc(const AnimationBase* anim, const NinePieceImage& from, const NinePieceImage& to, double progress)
353 {
354     if (!from.hasImage() || !to.hasImage())
355         return to;
356
357     // FIXME (74112): Support transitioning between NinePieceImages that differ by more than image content.
358
359     if (from.imageSlices() != to.imageSlices() || from.borderSlices() != to.borderSlices() || from.outset() != to.outset() || from.fill() != to.fill() || from.horizontalRule() != to.horizontalRule() || from.verticalRule() != to.verticalRule())
360         return to;
361
362     if (from.image()->imageSize(anim->renderer(), 1.0) != to.image()->imageSize(anim->renderer(), 1.0))
363         return to;
364
365     RefPtr<StyleImage> newContentImage = blendFunc(anim, from.image(), to.image(), progress);
366
367     return NinePieceImage(newContentImage, from.imageSlices(), from.fill(), from.borderSlices(), from.outset(), from.horizontalRule(), from.verticalRule());
368 }
369
370 class AnimationPropertyWrapperBase {
371     WTF_MAKE_NONCOPYABLE(AnimationPropertyWrapperBase);
372     WTF_MAKE_FAST_ALLOCATED;
373 public:
374     AnimationPropertyWrapperBase(CSSPropertyID prop)
375         : m_prop(prop)
376     {
377     }
378
379     virtual ~AnimationPropertyWrapperBase() { }
380
381     virtual bool isShorthandWrapper() const { return false; }
382     virtual bool equals(const RenderStyle* a, const RenderStyle* b) const = 0;
383     virtual void blend(const AnimationBase*, RenderStyle*, const RenderStyle*, const RenderStyle*, double) const = 0;
384
385     CSSPropertyID property() const { return m_prop; }
386
387     virtual bool animationIsAccelerated() const { return false; }
388
389 private:
390     CSSPropertyID m_prop;
391 };
392
393 template <typename T>
394 class PropertyWrapperGetter : public AnimationPropertyWrapperBase {
395     WTF_MAKE_FAST_ALLOCATED;
396 public:
397     PropertyWrapperGetter(CSSPropertyID prop, T (RenderStyle::*getter)() const)
398         : AnimationPropertyWrapperBase(prop)
399         , m_getter(getter)
400     {
401     }
402
403     virtual bool equals(const RenderStyle* a, const RenderStyle* b) const
404     {
405         if (a == b)
406             return true;
407         if (!a || !b)
408             return false;
409         return (a->*m_getter)() == (b->*m_getter)();
410     }
411
412 protected:
413     T (RenderStyle::*m_getter)() const;
414 };
415
416 template <typename T>
417 class PropertyWrapper : public PropertyWrapperGetter<T> {
418     WTF_MAKE_FAST_ALLOCATED;
419 public:
420     PropertyWrapper(CSSPropertyID prop, T (RenderStyle::*getter)() const, void (RenderStyle::*setter)(T))
421         : PropertyWrapperGetter<T>(prop, getter)
422         , m_setter(setter)
423     {
424     }
425
426     virtual void blend(const AnimationBase* anim, RenderStyle* dst, const RenderStyle* a, const RenderStyle* b, double progress) const
427     {
428         (dst->*m_setter)(blendFunc(anim, (a->*PropertyWrapperGetter<T>::m_getter)(), (b->*PropertyWrapperGetter<T>::m_getter)(), progress));
429     }
430
431 protected:
432     void (RenderStyle::*m_setter)(T);
433 };
434
435 template <typename T>
436 class RefCountedPropertyWrapper : public PropertyWrapperGetter<T*> {
437     WTF_MAKE_FAST_ALLOCATED;
438 public:
439     RefCountedPropertyWrapper(CSSPropertyID prop, T* (RenderStyle::*getter)() const, void (RenderStyle::*setter)(PassRefPtr<T>))
440         : PropertyWrapperGetter<T*>(prop, getter)
441         , m_setter(setter)
442     {
443     }
444
445     virtual void blend(const AnimationBase* anim, RenderStyle* dst, const RenderStyle* a, const RenderStyle* b, double progress) const
446     {
447         (dst->*m_setter)(blendFunc(anim, (a->*PropertyWrapperGetter<T*>::m_getter)(), (b->*PropertyWrapperGetter<T*>::m_getter)(), progress));
448     }
449
450 protected:
451     void (RenderStyle::*m_setter)(PassRefPtr<T>);
452 };
453
454 template <typename T>
455 class LengthPropertyWrapper : public PropertyWrapperGetter<const T&> {
456     WTF_MAKE_FAST_ALLOCATED;
457 public:
458     LengthPropertyWrapper(CSSPropertyID prop, const T& (RenderStyle::*getter)() const, void (RenderStyle::*setter)(T))
459         : PropertyWrapperGetter<const T&>(prop, getter)
460         , m_setter(setter)
461     {
462     }
463
464     virtual void blend(const AnimationBase* anim, RenderStyle* dst, const RenderStyle* a, const RenderStyle* b, double progress) const
465     {
466         (dst->*m_setter)(blendFunc(anim, (a->*PropertyWrapperGetter<const T&>::m_getter)(), (b->*PropertyWrapperGetter<const T&>::m_getter)(), progress));
467     }
468
469 protected:
470     void (RenderStyle::*m_setter)(T);
471 };
472
473 class PropertyWrapperClipPath : public RefCountedPropertyWrapper<ClipPathOperation> {
474     WTF_MAKE_FAST_ALLOCATED;
475 public:
476     PropertyWrapperClipPath(CSSPropertyID prop, ClipPathOperation* (RenderStyle::*getter)() const, void (RenderStyle::*setter)(PassRefPtr<ClipPathOperation>))
477         : RefCountedPropertyWrapper<ClipPathOperation>(prop, getter, setter)
478     {
479     }
480
481     virtual bool equals(const RenderStyle* a, const RenderStyle* b) const
482     {
483         // If the style pointers are the same, don't bother doing the test.
484         // If either is null, return false. If both are null, return true.
485         if (a == b)
486             return true;
487         if (!a || !b)
488             return false;
489
490         ClipPathOperation* clipPathA = (a->*m_getter)();
491         ClipPathOperation* clipPathB = (b->*m_getter)();
492         if (clipPathA == clipPathB)
493             return true;
494         if (!clipPathA || !clipPathB)
495             return false;
496         return *clipPathA == *clipPathB;
497     }
498 };
499
500 #if ENABLE(CSS_SHAPES)
501 class PropertyWrapperShape : public RefCountedPropertyWrapper<ShapeValue> {
502     WTF_MAKE_FAST_ALLOCATED;
503 public:
504     PropertyWrapperShape(CSSPropertyID prop, ShapeValue* (RenderStyle::*getter)() const, void (RenderStyle::*setter)(PassRefPtr<ShapeValue>))
505         : RefCountedPropertyWrapper<ShapeValue>(prop, getter, setter)
506     {
507     }
508
509     virtual bool equals(const RenderStyle* a, const RenderStyle* b) const
510     {
511         // If the style pointers are the same, don't bother doing the test.
512         // If either is null, return false. If both are null, return true.
513         if (a == b)
514             return true;
515         if (!a || !b)
516             return false;
517
518         ShapeValue* shapeA = (a->*m_getter)();
519         ShapeValue* shapeB = (b->*m_getter)();
520         if (shapeA == shapeB)
521             return true;
522         if (!shapeA || !shapeB)
523             return false;
524         return *shapeA == *shapeB;
525     }
526 };
527 #endif
528
529 class StyleImagePropertyWrapper : public RefCountedPropertyWrapper<StyleImage> {
530     WTF_MAKE_FAST_ALLOCATED;
531 public:
532     StyleImagePropertyWrapper(CSSPropertyID prop, StyleImage* (RenderStyle::*getter)() const, void (RenderStyle::*setter)(PassRefPtr<StyleImage>))
533         : RefCountedPropertyWrapper<StyleImage>(prop, getter, setter)
534     {
535     }
536
537     virtual bool equals(const RenderStyle* a, const RenderStyle* b) const
538     {
539        if (a == b)
540            return true;
541        if (!a || !b)
542             return false;
543
544         StyleImage* imageA = (a->*m_getter)();
545         StyleImage* imageB = (b->*m_getter)();
546         return arePointingToEqualData(imageA, imageB);
547     }
548 };
549
550 class PropertyWrapperColor : public PropertyWrapperGetter<Color> {
551     WTF_MAKE_FAST_ALLOCATED;
552 public:
553     PropertyWrapperColor(CSSPropertyID prop, Color (RenderStyle::*getter)() const, void (RenderStyle::*setter)(const Color&))
554         : PropertyWrapperGetter<Color>(prop, getter)
555         , m_setter(setter)
556     {
557     }
558
559     virtual void blend(const AnimationBase* anim, RenderStyle* dst, const RenderStyle* a, const RenderStyle* b, double progress) const
560     {
561         (dst->*m_setter)(blendFunc(anim, (a->*PropertyWrapperGetter<Color>::m_getter)(), (b->*PropertyWrapperGetter<Color>::m_getter)(), progress));
562     }
563
564 protected:
565     void (RenderStyle::*m_setter)(const Color&);
566 };
567
568
569 class PropertyWrapperAcceleratedOpacity : public PropertyWrapper<float> {
570     WTF_MAKE_FAST_ALLOCATED;
571 public:
572     PropertyWrapperAcceleratedOpacity()
573         : PropertyWrapper<float>(CSSPropertyOpacity, &RenderStyle::opacity, &RenderStyle::setOpacity)
574     {
575     }
576
577     virtual bool animationIsAccelerated() const { return true; }
578
579     virtual void blend(const AnimationBase* anim, RenderStyle* dst, const RenderStyle* a, const RenderStyle* b, double progress) const
580     {
581         float fromOpacity = a->opacity();
582
583         // This makes sure we put the object being animated into a RenderLayer during the animation
584         dst->setOpacity(blendFunc(anim, (fromOpacity == 1) ? 0.999999f : fromOpacity, b->opacity(), progress));
585     }
586 };
587
588 class PropertyWrapperAcceleratedTransform : public PropertyWrapper<const TransformOperations&> {
589     WTF_MAKE_FAST_ALLOCATED;
590 public:
591     PropertyWrapperAcceleratedTransform()
592         : PropertyWrapper<const TransformOperations&>(CSSPropertyTransform, &RenderStyle::transform, &RenderStyle::setTransform)
593     {
594     }
595
596     virtual bool animationIsAccelerated() const { return true; }
597
598     virtual void blend(const AnimationBase* anim, RenderStyle* dst, const RenderStyle* a, const RenderStyle* b, double progress) const
599     {
600         dst->setTransform(blendFunc(anim, a->transform(), b->transform(), progress));
601     }
602 };
603
604 class PropertyWrapperAcceleratedFilter : public PropertyWrapper<const FilterOperations&> {
605     WTF_MAKE_FAST_ALLOCATED;
606 public:
607     PropertyWrapperAcceleratedFilter()
608         : PropertyWrapper<const FilterOperations&>(CSSPropertyFilter, &RenderStyle::filter, &RenderStyle::setFilter)
609     {
610     }
611
612     virtual bool animationIsAccelerated() const { return true; }
613
614     virtual void blend(const AnimationBase* anim, RenderStyle* dst, const RenderStyle* a, const RenderStyle* b, double progress) const
615     {
616         dst->setFilter(blendFunc(anim, a->filter(), b->filter(), progress));
617     }
618 };
619
620 #if ENABLE(FILTERS_LEVEL_2)
621 class PropertyWrapperAcceleratedBackdropFilter : public PropertyWrapper<const FilterOperations&> {
622     WTF_MAKE_FAST_ALLOCATED;
623 public:
624     PropertyWrapperAcceleratedBackdropFilter()
625         : PropertyWrapper<const FilterOperations&>(CSSPropertyWebkitBackdropFilter, &RenderStyle::backdropFilter, &RenderStyle::setBackdropFilter)
626     {
627     }
628
629     virtual bool animationIsAccelerated() const { return true; }
630
631     virtual void blend(const AnimationBase* anim, RenderStyle* dst, const RenderStyle* a, const RenderStyle* b, double progress) const
632     {
633         dst->setBackdropFilter(blendFunc(anim, a->backdropFilter(), b->backdropFilter(), progress, true));
634     }
635 };
636 #endif
637
638 static inline size_t shadowListLength(const ShadowData* shadow)
639 {
640     size_t count;
641     for (count = 0; shadow; shadow = shadow->next())
642         ++count;
643     return count;
644 }
645
646 static inline const ShadowData* shadowForBlending(const ShadowData* srcShadow, const ShadowData* otherShadow)
647 {
648     DEPRECATED_DEFINE_STATIC_LOCAL(ShadowData, defaultShadowData, (IntPoint(), 0, 0, Normal, false, Color::transparent));
649     DEPRECATED_DEFINE_STATIC_LOCAL(ShadowData, defaultInsetShadowData, (IntPoint(), 0, 0, Inset, false, Color::transparent));
650
651     DEPRECATED_DEFINE_STATIC_LOCAL(ShadowData, defaultWebKitBoxShadowData, (IntPoint(), 0, 0, Normal, true, Color::transparent));
652     DEPRECATED_DEFINE_STATIC_LOCAL(ShadowData, defaultInsetWebKitBoxShadowData, (IntPoint(), 0, 0, Inset, true, Color::transparent));
653
654     if (srcShadow)
655         return srcShadow;
656
657     if (otherShadow->style() == Inset)
658         return otherShadow->isWebkitBoxShadow() ? &defaultInsetWebKitBoxShadowData : &defaultInsetShadowData;
659
660     return otherShadow->isWebkitBoxShadow() ? &defaultWebKitBoxShadowData : &defaultShadowData;
661 }
662
663 class PropertyWrapperShadow : public AnimationPropertyWrapperBase {
664     WTF_MAKE_FAST_ALLOCATED;
665 public:
666     PropertyWrapperShadow(CSSPropertyID prop, const ShadowData* (RenderStyle::*getter)() const, void (RenderStyle::*setter)(std::unique_ptr<ShadowData>, bool))
667         : AnimationPropertyWrapperBase(prop)
668         , m_getter(getter)
669         , m_setter(setter)
670     {
671     }
672
673     virtual bool equals(const RenderStyle* a, const RenderStyle* b) const
674     {
675         if (a == b)
676             return true;
677         if (!a || !b)
678             return false;
679
680         const ShadowData* shadowA = (a->*m_getter)();
681         const ShadowData* shadowB = (b->*m_getter)();
682
683         while (true) {
684             // end of both lists
685             if (!shadowA && !shadowB)
686                 return true;
687
688             // end of just one of the lists
689             if (!shadowA || !shadowB)
690                 return false;
691
692             if (*shadowA != *shadowB)
693                 return false;
694
695             shadowA = shadowA->next();
696             shadowB = shadowB->next();
697         }
698
699         return true;
700     }
701
702     virtual void blend(const AnimationBase* anim, RenderStyle* dst, const RenderStyle* a, const RenderStyle* b, double progress) const
703     {
704         const ShadowData* shadowA = (a->*m_getter)();
705         const ShadowData* shadowB = (b->*m_getter)();
706
707         int fromLength = shadowListLength(shadowA);
708         int toLength = shadowListLength(shadowB);
709
710         if (fromLength == toLength || (fromLength <= 1 && toLength <= 1)) {
711             (dst->*m_setter)(blendSimpleOrMatchedShadowLists(anim, progress, shadowA, shadowB), false);
712             return;
713         }
714
715         (dst->*m_setter)(blendMismatchedShadowLists(anim, progress, shadowA, shadowB, fromLength, toLength), false);
716     }
717
718 private:
719     std::unique_ptr<ShadowData> blendSimpleOrMatchedShadowLists(const AnimationBase* anim, double progress, const ShadowData* shadowA, const ShadowData* shadowB) const
720     {
721         std::unique_ptr<ShadowData> newShadowData;
722         ShadowData* lastShadow = 0;
723
724         while (shadowA || shadowB) {
725             const ShadowData* srcShadow = shadowForBlending(shadowA, shadowB);
726             const ShadowData* dstShadow = shadowForBlending(shadowB, shadowA);
727
728             std::unique_ptr<ShadowData> blendedShadow = blendFunc(anim, srcShadow, dstShadow, progress);
729             ShadowData* blendedShadowPtr = blendedShadow.get();
730
731             if (!lastShadow)
732                 newShadowData = WTFMove(blendedShadow);
733             else
734                 lastShadow->setNext(WTFMove(blendedShadow));
735
736             lastShadow = blendedShadowPtr;
737
738             shadowA = shadowA ? shadowA->next() : 0;
739             shadowB = shadowB ? shadowB->next() : 0;
740         }
741
742         return newShadowData;
743     }
744
745     std::unique_ptr<ShadowData> blendMismatchedShadowLists(const AnimationBase* anim, double progress, const ShadowData* shadowA, const ShadowData* shadowB, int fromLength, int toLength) const
746     {
747         // The shadows in ShadowData are stored in reverse order, so when animating mismatched lists,
748         // reverse them and match from the end.
749         Vector<const ShadowData*, 4> fromShadows(fromLength);
750         for (int i = fromLength - 1; i >= 0; --i) {
751             fromShadows[i] = shadowA;
752             shadowA = shadowA->next();
753         }
754
755         Vector<const ShadowData*, 4> toShadows(toLength);
756         for (int i = toLength - 1; i >= 0; --i) {
757             toShadows[i] = shadowB;
758             shadowB = shadowB->next();
759         }
760
761         std::unique_ptr<ShadowData> newShadowData;
762
763         int maxLength = std::max(fromLength, toLength);
764         for (int i = 0; i < maxLength; ++i) {
765             const ShadowData* fromShadow = i < fromLength ? fromShadows[i] : 0;
766             const ShadowData* toShadow = i < toLength ? toShadows[i] : 0;
767
768             const ShadowData* srcShadow = shadowForBlending(fromShadow, toShadow);
769             const ShadowData* dstShadow = shadowForBlending(toShadow, fromShadow);
770
771             std::unique_ptr<ShadowData> blendedShadow = blendFunc(anim, srcShadow, dstShadow, progress);
772             // Insert at the start of the list to preserve the order.
773             blendedShadow->setNext(WTFMove(newShadowData));
774             newShadowData = WTFMove(blendedShadow);
775         }
776
777         return newShadowData;
778     }
779
780     const ShadowData* (RenderStyle::*m_getter)() const;
781     void (RenderStyle::*m_setter)(std::unique_ptr<ShadowData>, bool);
782 };
783
784 class PropertyWrapperMaybeInvalidColor : public AnimationPropertyWrapperBase {
785     WTF_MAKE_FAST_ALLOCATED;
786 public:
787     PropertyWrapperMaybeInvalidColor(CSSPropertyID prop, Color (RenderStyle::*getter)() const, void (RenderStyle::*setter)(const Color&))
788         : AnimationPropertyWrapperBase(prop)
789         , m_getter(getter)
790         , m_setter(setter)
791     {
792     }
793
794     virtual bool equals(const RenderStyle* a, const RenderStyle* b) const
795     {
796         if (a == b)
797             return true;
798         if (!a || !b)
799             return false;
800
801         Color fromColor = (a->*m_getter)();
802         Color toColor = (b->*m_getter)();
803
804         if (!fromColor.isValid() && !toColor.isValid())
805             return true;
806
807         if (!fromColor.isValid())
808             fromColor = a->color();
809         if (!toColor.isValid())
810             toColor = b->color();
811
812         return fromColor == toColor;
813     }
814
815     virtual void blend(const AnimationBase* anim, RenderStyle* dst, const RenderStyle* a, const RenderStyle* b, double progress) const
816     {
817         Color fromColor = (a->*m_getter)();
818         Color toColor = (b->*m_getter)();
819
820         if (!fromColor.isValid() && !toColor.isValid())
821             return;
822
823         if (!fromColor.isValid())
824             fromColor = a->color();
825         if (!toColor.isValid())
826             toColor = b->color();
827         (dst->*m_setter)(blendFunc(anim, fromColor, toColor, progress));
828     }
829
830 private:
831     Color (RenderStyle::*m_getter)() const;
832     void (RenderStyle::*m_setter)(const Color&);
833 };
834
835
836 enum MaybeInvalidColorTag { MaybeInvalidColor };
837 class PropertyWrapperVisitedAffectedColor : public AnimationPropertyWrapperBase {
838     WTF_MAKE_FAST_ALLOCATED;
839 public:
840     PropertyWrapperVisitedAffectedColor(CSSPropertyID prop, Color (RenderStyle::*getter)() const, void (RenderStyle::*setter)(const Color&),
841                                         Color (RenderStyle::*visitedGetter)() const, void (RenderStyle::*visitedSetter)(const Color&))
842         : AnimationPropertyWrapperBase(prop)
843         , m_wrapper(std::make_unique<PropertyWrapperColor>(prop, getter, setter))
844         , m_visitedWrapper(std::make_unique<PropertyWrapperColor>(prop, visitedGetter, visitedSetter))
845     {
846     }
847     PropertyWrapperVisitedAffectedColor(CSSPropertyID prop, MaybeInvalidColorTag, Color (RenderStyle::*getter)() const, void (RenderStyle::*setter)(const Color&),
848                                         Color (RenderStyle::*visitedGetter)() const, void (RenderStyle::*visitedSetter)(const Color&))
849         : AnimationPropertyWrapperBase(prop)
850         , m_wrapper(std::make_unique<PropertyWrapperMaybeInvalidColor>(prop, getter, setter))
851         , m_visitedWrapper(std::make_unique<PropertyWrapperMaybeInvalidColor>(prop, visitedGetter, visitedSetter))
852     {
853     }
854     virtual bool equals(const RenderStyle* a, const RenderStyle* b) const
855     {
856         return m_wrapper->equals(a, b) && m_visitedWrapper->equals(a, b);
857     }
858     virtual void blend(const AnimationBase* anim, RenderStyle* dst, const RenderStyle* a, const RenderStyle* b, double progress) const
859     {
860         m_wrapper->blend(anim, dst, a, b, progress);
861         m_visitedWrapper->blend(anim, dst, a, b, progress);
862     }
863
864 private:
865     std::unique_ptr<AnimationPropertyWrapperBase> m_wrapper;
866     std::unique_ptr<AnimationPropertyWrapperBase> m_visitedWrapper;
867 };
868
869 // Wrapper base class for an animatable property in a FillLayer
870 class FillLayerAnimationPropertyWrapperBase {
871     WTF_MAKE_FAST_ALLOCATED;
872 public:
873     FillLayerAnimationPropertyWrapperBase()
874     {
875     }
876
877     virtual ~FillLayerAnimationPropertyWrapperBase() { }
878
879     virtual bool equals(const FillLayer*, const FillLayer*) const = 0;
880     virtual void blend(const AnimationBase*, FillLayer*, const FillLayer*, const FillLayer*, double) const = 0;
881 };
882
883 template <typename T>
884 class FillLayerPropertyWrapperGetter : public FillLayerAnimationPropertyWrapperBase {
885     WTF_MAKE_FAST_ALLOCATED;
886     WTF_MAKE_NONCOPYABLE(FillLayerPropertyWrapperGetter);
887 public:
888     FillLayerPropertyWrapperGetter(T (FillLayer::*getter)() const)
889         : m_getter(getter)
890     {
891     }
892
893     virtual bool equals(const FillLayer* a, const FillLayer* b) const
894     {
895         if (a == b)
896             return true;
897         if (!a || !b)
898             return false;
899         return (a->*m_getter)() == (b->*m_getter)();
900     }
901
902 protected:
903     T (FillLayer::*m_getter)() const;
904 };
905
906 template <typename T>
907 class FillLayerPropertyWrapper : public FillLayerPropertyWrapperGetter<const T&> {
908     WTF_MAKE_FAST_ALLOCATED;
909 public:
910     FillLayerPropertyWrapper(const T& (FillLayer::*getter)() const, void (FillLayer::*setter)(T))
911         : FillLayerPropertyWrapperGetter<const T&>(getter)
912         , m_setter(setter)
913     {
914     }
915
916     virtual void blend(const AnimationBase* anim, FillLayer* dst, const FillLayer* a, const FillLayer* b, double progress) const
917     {
918         (dst->*m_setter)(blendFunc(anim, (a->*FillLayerPropertyWrapperGetter<const T&>::m_getter)(), (b->*FillLayerPropertyWrapperGetter<const T&>::m_getter)(), progress));
919     }
920
921 protected:
922     void (FillLayer::*m_setter)(T);
923 };
924
925 template <typename T>
926 class FillLayerRefCountedPropertyWrapper : public FillLayerPropertyWrapperGetter<T*> {
927     WTF_MAKE_FAST_ALLOCATED;
928 public:
929     FillLayerRefCountedPropertyWrapper(T* (FillLayer::*getter)() const, void (FillLayer::*setter)(PassRefPtr<T>))
930         : FillLayerPropertyWrapperGetter<T*>(getter)
931         , m_setter(setter)
932     {
933     }
934
935     virtual void blend(const AnimationBase* anim, FillLayer* dst, const FillLayer* a, const FillLayer* b, double progress) const
936     {
937         (dst->*m_setter)(blendFunc(anim, (a->*FillLayerPropertyWrapperGetter<T*>::m_getter)(), (b->*FillLayerPropertyWrapperGetter<T*>::m_getter)(), progress));
938     }
939
940 protected:
941     void (FillLayer::*m_setter)(PassRefPtr<T>);
942 };
943
944 class FillLayerStyleImagePropertyWrapper : public FillLayerRefCountedPropertyWrapper<StyleImage> {
945     WTF_MAKE_FAST_ALLOCATED;
946 public:
947     FillLayerStyleImagePropertyWrapper(StyleImage* (FillLayer::*getter)() const, void (FillLayer::*setter)(PassRefPtr<StyleImage>))
948         : FillLayerRefCountedPropertyWrapper<StyleImage>(getter, setter)
949     {
950     }
951
952     virtual bool equals(const FillLayer* a, const FillLayer* b) const
953     {
954        if (a == b)
955            return true;
956        if (!a || !b)
957             return false;
958
959         StyleImage* imageA = (a->*m_getter)();
960         StyleImage* imageB = (b->*m_getter)();
961         return arePointingToEqualData(imageA, imageB);
962     }
963 };
964
965 class FillLayersPropertyWrapper : public AnimationPropertyWrapperBase {
966     WTF_MAKE_FAST_ALLOCATED;
967 public:
968     typedef const FillLayer* (RenderStyle::*LayersGetter)() const;
969     typedef FillLayer& (RenderStyle::*LayersAccessor)();
970
971     FillLayersPropertyWrapper(CSSPropertyID prop, LayersGetter getter, LayersAccessor accessor)
972         : AnimationPropertyWrapperBase(prop)
973         , m_layersGetter(getter)
974         , m_layersAccessor(accessor)
975     {
976         switch (prop) {
977         case CSSPropertyBackgroundPositionX:
978         case CSSPropertyWebkitMaskPositionX:
979             m_fillLayerPropertyWrapper = std::make_unique<FillLayerPropertyWrapper<Length>>(&FillLayer::xPosition, &FillLayer::setXPosition);
980             break;
981         case CSSPropertyBackgroundPositionY:
982         case CSSPropertyWebkitMaskPositionY:
983             m_fillLayerPropertyWrapper = std::make_unique<FillLayerPropertyWrapper<Length>>(&FillLayer::yPosition, &FillLayer::setYPosition);
984             break;
985         case CSSPropertyBackgroundSize:
986         case CSSPropertyWebkitBackgroundSize:
987         case CSSPropertyWebkitMaskSize:
988             m_fillLayerPropertyWrapper = std::make_unique<FillLayerPropertyWrapper<LengthSize>>(&FillLayer::sizeLength, &FillLayer::setSizeLength);
989             break;
990         case CSSPropertyBackgroundImage:
991             m_fillLayerPropertyWrapper = std::make_unique<FillLayerStyleImagePropertyWrapper>(&FillLayer::image, &FillLayer::setImage);
992             break;
993         default:
994             break;
995         }
996     }
997
998     virtual bool equals(const RenderStyle* a, const RenderStyle* b) const
999     {
1000         if (a == b)
1001             return true;
1002         if (!a || !b)
1003             return false;
1004
1005         const FillLayer* fromLayer = (a->*m_layersGetter)();
1006         const FillLayer* toLayer = (b->*m_layersGetter)();
1007
1008         while (fromLayer && toLayer) {
1009             if (!m_fillLayerPropertyWrapper->equals(fromLayer, toLayer))
1010                 return false;
1011
1012             fromLayer = fromLayer->next();
1013             toLayer = toLayer->next();
1014         }
1015
1016         return true;
1017     }
1018
1019     virtual void blend(const AnimationBase* anim, RenderStyle* dst, const RenderStyle* a, const RenderStyle* b, double progress) const
1020     {
1021         const FillLayer* aLayer = (a->*m_layersGetter)();
1022         const FillLayer* bLayer = (b->*m_layersGetter)();
1023         FillLayer* dstLayer = &(dst->*m_layersAccessor)();
1024
1025         while (aLayer && bLayer && dstLayer) {
1026             m_fillLayerPropertyWrapper->blend(anim, dstLayer, aLayer, bLayer, progress);
1027             aLayer = aLayer->next();
1028             bLayer = bLayer->next();
1029             dstLayer = dstLayer->next();
1030         }
1031     }
1032
1033 private:
1034     std::unique_ptr<FillLayerAnimationPropertyWrapperBase> m_fillLayerPropertyWrapper;
1035
1036     LayersGetter m_layersGetter;
1037     LayersAccessor m_layersAccessor;
1038 };
1039
1040 class ShorthandPropertyWrapper : public AnimationPropertyWrapperBase {
1041     WTF_MAKE_FAST_ALLOCATED;
1042 public:
1043     ShorthandPropertyWrapper(CSSPropertyID property, Vector<AnimationPropertyWrapperBase*> longhandWrappers)
1044         : AnimationPropertyWrapperBase(property)
1045         , m_propertyWrappers(WTFMove(longhandWrappers))
1046     {
1047     }
1048
1049     virtual bool isShorthandWrapper() const { return true; }
1050
1051     virtual bool equals(const RenderStyle* a, const RenderStyle* b) const
1052     {
1053         if (a == b)
1054             return true;
1055         if (!a || !b)
1056             return false;
1057
1058         for (auto& wrapper : m_propertyWrappers) {
1059             if (!wrapper->equals(a, b))
1060                 return false;
1061         }
1062         return true;
1063     }
1064
1065     virtual void blend(const AnimationBase* anim, RenderStyle* dst, const RenderStyle* a, const RenderStyle* b, double progress) const
1066     {
1067         for (auto& wrapper : m_propertyWrappers)
1068             wrapper->blend(anim, dst, a, b, progress);
1069     }
1070
1071     const Vector<AnimationPropertyWrapperBase*>& propertyWrappers() const { return m_propertyWrappers; }
1072
1073 private:
1074     Vector<AnimationPropertyWrapperBase*> m_propertyWrappers;
1075 };
1076
1077 class PropertyWrapperFlex : public AnimationPropertyWrapperBase {
1078     WTF_MAKE_FAST_ALLOCATED;
1079 public:
1080     PropertyWrapperFlex()
1081         : AnimationPropertyWrapperBase(CSSPropertyFlex)
1082     {
1083     }
1084
1085     virtual bool equals(const RenderStyle* a, const RenderStyle* b) const
1086     {
1087         if (a == b)
1088             return true;
1089         if (!a || !b)
1090             return false;
1091
1092         return a->flexBasis() == b->flexBasis() && a->flexGrow() == b->flexGrow() && a->flexShrink() == b->flexShrink();
1093     }
1094
1095     virtual void blend(const AnimationBase* anim, RenderStyle* dst, const RenderStyle* a, const RenderStyle* b, double progress) const
1096     {
1097         dst->setFlexBasis(blendFunc(anim, a->flexBasis(), b->flexBasis(), progress));
1098         dst->setFlexGrow(blendFunc(anim, a->flexGrow(), b->flexGrow(), progress));
1099         dst->setFlexShrink(blendFunc(anim, a->flexShrink(), b->flexShrink(), progress));
1100     }
1101 };
1102
1103 class PropertyWrapperSVGPaint : public AnimationPropertyWrapperBase {
1104     WTF_MAKE_FAST_ALLOCATED;
1105 public:
1106     PropertyWrapperSVGPaint(CSSPropertyID prop, const SVGPaint::SVGPaintType& (RenderStyle::*paintTypeGetter)() const, Color (RenderStyle::*getter)() const, void (RenderStyle::*setter)(const Color&))
1107         : AnimationPropertyWrapperBase(prop)
1108         , m_paintTypeGetter(paintTypeGetter)
1109         , m_getter(getter)
1110         , m_setter(setter)
1111     {
1112     }
1113
1114     virtual bool equals(const RenderStyle* a, const RenderStyle* b) const
1115     {
1116         if (a == b)
1117             return true;
1118         if (!a || !b)
1119             return false;
1120
1121         if ((a->*m_paintTypeGetter)() != (b->*m_paintTypeGetter)())
1122             return false;
1123
1124         // We only support animations between SVGPaints that are pure Color values.
1125         // For everything else we must return true for this method, otherwise
1126         // we will try to animate between values forever.
1127         if ((a->*m_paintTypeGetter)() == SVGPaint::SVG_PAINTTYPE_RGBCOLOR) {
1128             Color fromColor = (a->*m_getter)();
1129             Color toColor = (b->*m_getter)();
1130
1131             if (!fromColor.isValid() && !toColor.isValid())
1132                 return true;
1133
1134             if (!fromColor.isValid())
1135                 fromColor = Color();
1136             if (!toColor.isValid())
1137                 toColor = Color();
1138
1139             return fromColor == toColor;
1140         }
1141         return true;
1142     }
1143
1144     virtual void blend(const AnimationBase* anim, RenderStyle* dst, const RenderStyle* a, const RenderStyle* b, double progress) const
1145     {
1146         if ((a->*m_paintTypeGetter)() != SVGPaint::SVG_PAINTTYPE_RGBCOLOR
1147             || (b->*m_paintTypeGetter)() != SVGPaint::SVG_PAINTTYPE_RGBCOLOR)
1148             return;
1149
1150         Color fromColor = (a->*m_getter)();
1151         Color toColor = (b->*m_getter)();
1152
1153         if (!fromColor.isValid() && !toColor.isValid())
1154             return;
1155
1156         if (!fromColor.isValid())
1157             fromColor = Color();
1158         if (!toColor.isValid())
1159             toColor = Color();
1160         (dst->*m_setter)(blendFunc(anim, fromColor, toColor, progress));
1161     }
1162
1163 private:
1164     const SVGPaint::SVGPaintType& (RenderStyle::*m_paintTypeGetter)() const;
1165     Color (RenderStyle::*m_getter)() const;
1166     void (RenderStyle::*m_setter)(const Color&);
1167 };
1168
1169 class CSSPropertyAnimationWrapperMap {
1170     WTF_MAKE_FAST_ALLOCATED;
1171 public:
1172     static CSSPropertyAnimationWrapperMap& singleton()
1173     {
1174         // FIXME: This data is never destroyed. Maybe we should ref count it and toss it when the last AnimationController is destroyed?
1175         static NeverDestroyed<CSSPropertyAnimationWrapperMap> map;
1176         return map;
1177     }
1178
1179     AnimationPropertyWrapperBase* wrapperForProperty(CSSPropertyID propertyID)
1180     {
1181         if (propertyID < firstCSSProperty || propertyID > lastCSSProperty)
1182             return 0;
1183
1184         unsigned wrapperIndex = indexFromPropertyID(propertyID);
1185         if (wrapperIndex == cInvalidPropertyWrapperIndex)
1186             return 0;
1187
1188         return m_propertyWrappers[wrapperIndex].get();
1189     }
1190
1191     AnimationPropertyWrapperBase* wrapperForIndex(unsigned index)
1192     {
1193         ASSERT(index < m_propertyWrappers.size());
1194         return m_propertyWrappers[index].get();
1195     }
1196
1197     unsigned size()
1198     {
1199         return m_propertyWrappers.size();
1200     }
1201
1202 private:
1203     CSSPropertyAnimationWrapperMap();
1204     ~CSSPropertyAnimationWrapperMap() = delete;
1205
1206     unsigned char& indexFromPropertyID(CSSPropertyID propertyID)
1207     {
1208         return m_propertyToIdMap[propertyID - firstCSSProperty];
1209     }
1210
1211     Vector<std::unique_ptr<AnimationPropertyWrapperBase>> m_propertyWrappers;
1212     unsigned char m_propertyToIdMap[numCSSProperties];
1213
1214     static const unsigned char cInvalidPropertyWrapperIndex = UCHAR_MAX;
1215
1216     friend class WTF::NeverDestroyed<CSSPropertyAnimationWrapperMap>;
1217 };
1218
1219 CSSPropertyAnimationWrapperMap::CSSPropertyAnimationWrapperMap()
1220 {
1221     // build the list of property wrappers to do the comparisons and blends
1222     AnimationPropertyWrapperBase* animatableLonghandPropertyWrappers[] = {
1223         new LengthPropertyWrapper<Length>(CSSPropertyLeft, &RenderStyle::left, &RenderStyle::setLeft),
1224         new LengthPropertyWrapper<Length>(CSSPropertyRight, &RenderStyle::right, &RenderStyle::setRight),
1225         new LengthPropertyWrapper<Length>(CSSPropertyTop, &RenderStyle::top, &RenderStyle::setTop),
1226         new LengthPropertyWrapper<Length>(CSSPropertyBottom, &RenderStyle::bottom, &RenderStyle::setBottom),
1227
1228         new LengthPropertyWrapper<Length>(CSSPropertyWidth, &RenderStyle::width, &RenderStyle::setWidth),
1229         new LengthPropertyWrapper<Length>(CSSPropertyMinWidth, &RenderStyle::minWidth, &RenderStyle::setMinWidth),
1230         new LengthPropertyWrapper<Length>(CSSPropertyMaxWidth, &RenderStyle::maxWidth, &RenderStyle::setMaxWidth),
1231
1232         new LengthPropertyWrapper<Length>(CSSPropertyHeight, &RenderStyle::height, &RenderStyle::setHeight),
1233         new LengthPropertyWrapper<Length>(CSSPropertyMinHeight, &RenderStyle::minHeight, &RenderStyle::setMinHeight),
1234         new LengthPropertyWrapper<Length>(CSSPropertyMaxHeight, &RenderStyle::maxHeight, &RenderStyle::setMaxHeight),
1235
1236         new PropertyWrapperFlex(),
1237
1238         new PropertyWrapper<float>(CSSPropertyBorderLeftWidth, &RenderStyle::borderLeftWidth, &RenderStyle::setBorderLeftWidth),
1239         new PropertyWrapper<float>(CSSPropertyBorderRightWidth, &RenderStyle::borderRightWidth, &RenderStyle::setBorderRightWidth),
1240         new PropertyWrapper<float>(CSSPropertyBorderTopWidth, &RenderStyle::borderTopWidth, &RenderStyle::setBorderTopWidth),
1241         new PropertyWrapper<float>(CSSPropertyBorderBottomWidth, &RenderStyle::borderBottomWidth, &RenderStyle::setBorderBottomWidth),
1242         new LengthPropertyWrapper<Length>(CSSPropertyMarginLeft, &RenderStyle::marginLeft, &RenderStyle::setMarginLeft),
1243         new LengthPropertyWrapper<Length>(CSSPropertyMarginRight, &RenderStyle::marginRight, &RenderStyle::setMarginRight),
1244         new LengthPropertyWrapper<Length>(CSSPropertyMarginTop, &RenderStyle::marginTop, &RenderStyle::setMarginTop),
1245         new LengthPropertyWrapper<Length>(CSSPropertyMarginBottom, &RenderStyle::marginBottom, &RenderStyle::setMarginBottom),
1246         new LengthPropertyWrapper<Length>(CSSPropertyPaddingLeft, &RenderStyle::paddingLeft, &RenderStyle::setPaddingLeft),
1247         new LengthPropertyWrapper<Length>(CSSPropertyPaddingRight, &RenderStyle::paddingRight, &RenderStyle::setPaddingRight),
1248         new LengthPropertyWrapper<Length>(CSSPropertyPaddingTop, &RenderStyle::paddingTop, &RenderStyle::setPaddingTop),
1249         new LengthPropertyWrapper<Length>(CSSPropertyPaddingBottom, &RenderStyle::paddingBottom, &RenderStyle::setPaddingBottom),
1250         new PropertyWrapperVisitedAffectedColor(CSSPropertyColor, &RenderStyle::color, &RenderStyle::setColor, &RenderStyle::visitedLinkColor, &RenderStyle::setVisitedLinkColor),
1251
1252         new PropertyWrapperVisitedAffectedColor(CSSPropertyBackgroundColor, &RenderStyle::backgroundColor, &RenderStyle::setBackgroundColor, &RenderStyle::visitedLinkBackgroundColor, &RenderStyle::setVisitedLinkBackgroundColor),
1253
1254         new FillLayersPropertyWrapper(CSSPropertyBackgroundImage, &RenderStyle::backgroundLayers, &RenderStyle::ensureBackgroundLayers),
1255         new StyleImagePropertyWrapper(CSSPropertyListStyleImage, &RenderStyle::listStyleImage, &RenderStyle::setListStyleImage),
1256         new StyleImagePropertyWrapper(CSSPropertyWebkitMaskImage, &RenderStyle::maskImage, &RenderStyle::setMaskImage),
1257
1258         new StyleImagePropertyWrapper(CSSPropertyBorderImageSource, &RenderStyle::borderImageSource, &RenderStyle::setBorderImageSource),
1259         new LengthPropertyWrapper<LengthBox>(CSSPropertyBorderImageSlice, &RenderStyle::borderImageSlices, &RenderStyle::setBorderImageSlices),
1260         new LengthPropertyWrapper<LengthBox>(CSSPropertyBorderImageWidth, &RenderStyle::borderImageWidth, &RenderStyle::setBorderImageWidth),
1261         new LengthPropertyWrapper<LengthBox>(CSSPropertyBorderImageOutset, &RenderStyle::borderImageOutset, &RenderStyle::setBorderImageOutset),
1262
1263         new StyleImagePropertyWrapper(CSSPropertyWebkitMaskBoxImageSource, &RenderStyle::maskBoxImageSource, &RenderStyle::setMaskBoxImageSource),
1264         new PropertyWrapper<const NinePieceImage&>(CSSPropertyWebkitMaskBoxImage, &RenderStyle::maskBoxImage, &RenderStyle::setMaskBoxImage),
1265
1266         new FillLayersPropertyWrapper(CSSPropertyBackgroundPositionX, &RenderStyle::backgroundLayers, &RenderStyle::ensureBackgroundLayers),
1267         new FillLayersPropertyWrapper(CSSPropertyBackgroundPositionY, &RenderStyle::backgroundLayers, &RenderStyle::ensureBackgroundLayers),
1268         new FillLayersPropertyWrapper(CSSPropertyBackgroundSize, &RenderStyle::backgroundLayers, &RenderStyle::ensureBackgroundLayers),
1269         new FillLayersPropertyWrapper(CSSPropertyWebkitBackgroundSize, &RenderStyle::backgroundLayers, &RenderStyle::ensureBackgroundLayers),
1270
1271         new FillLayersPropertyWrapper(CSSPropertyWebkitMaskPositionX, &RenderStyle::maskLayers, &RenderStyle::ensureMaskLayers),
1272         new FillLayersPropertyWrapper(CSSPropertyWebkitMaskPositionY, &RenderStyle::maskLayers, &RenderStyle::ensureMaskLayers),
1273         new FillLayersPropertyWrapper(CSSPropertyWebkitMaskSize, &RenderStyle::maskLayers, &RenderStyle::ensureMaskLayers),
1274
1275         new PropertyWrapper<float>(CSSPropertyFontSize,
1276             // Must pass a specified size to setFontSize if Text Autosizing is enabled, but a computed size
1277             // if text zoom is enabled (if neither is enabled it's irrelevant as they're probably the same).
1278             // FIXME: Find some way to assert that text zoom isn't activated when Text Autosizing is compiled in.
1279 #if ENABLE(TEXT_AUTOSIZING)
1280             &RenderStyle::specifiedFontSize,
1281 #else
1282             &RenderStyle::computedFontSize,
1283 #endif
1284             &RenderStyle::setFontSize),
1285         new PropertyWrapper<unsigned short>(CSSPropertyColumnRuleWidth, &RenderStyle::columnRuleWidth, &RenderStyle::setColumnRuleWidth),
1286         new PropertyWrapper<float>(CSSPropertyColumnGap, &RenderStyle::columnGap, &RenderStyle::setColumnGap),
1287         new PropertyWrapper<unsigned short>(CSSPropertyColumnCount, &RenderStyle::columnCount, &RenderStyle::setColumnCount),
1288         new PropertyWrapper<float>(CSSPropertyColumnWidth, &RenderStyle::columnWidth, &RenderStyle::setColumnWidth),
1289         new PropertyWrapper<short>(CSSPropertyWebkitBorderHorizontalSpacing, &RenderStyle::horizontalBorderSpacing, &RenderStyle::setHorizontalBorderSpacing),
1290         new PropertyWrapper<short>(CSSPropertyWebkitBorderVerticalSpacing, &RenderStyle::verticalBorderSpacing, &RenderStyle::setVerticalBorderSpacing),
1291         new PropertyWrapper<int>(CSSPropertyZIndex, &RenderStyle::zIndex, &RenderStyle::setZIndex),
1292         new PropertyWrapper<short>(CSSPropertyOrphans, &RenderStyle::orphans, &RenderStyle::setOrphans),
1293         new PropertyWrapper<short>(CSSPropertyWidows, &RenderStyle::widows, &RenderStyle::setWidows),
1294         new LengthPropertyWrapper<Length>(CSSPropertyLineHeight, &RenderStyle::specifiedLineHeight, &RenderStyle::setLineHeight),
1295         new PropertyWrapper<float>(CSSPropertyOutlineOffset, &RenderStyle::outlineOffset, &RenderStyle::setOutlineOffset),
1296         new PropertyWrapper<float>(CSSPropertyOutlineWidth, &RenderStyle::outlineWidth, &RenderStyle::setOutlineWidth),
1297         new PropertyWrapper<float>(CSSPropertyLetterSpacing, &RenderStyle::letterSpacing, &RenderStyle::setLetterSpacing),
1298         new LengthPropertyWrapper<Length>(CSSPropertyWordSpacing, &RenderStyle::wordSpacing, &RenderStyle::setWordSpacing),
1299         new LengthPropertyWrapper<Length>(CSSPropertyTextIndent, &RenderStyle::textIndent, &RenderStyle::setTextIndent),
1300
1301         new PropertyWrapper<float>(CSSPropertyPerspective, &RenderStyle::perspective, &RenderStyle::setPerspective),
1302         new LengthPropertyWrapper<Length>(CSSPropertyPerspectiveOriginX, &RenderStyle::perspectiveOriginX, &RenderStyle::setPerspectiveOriginX),
1303         new LengthPropertyWrapper<Length>(CSSPropertyPerspectiveOriginY, &RenderStyle::perspectiveOriginY, &RenderStyle::setPerspectiveOriginY),
1304         new LengthPropertyWrapper<Length>(CSSPropertyTransformOriginX, &RenderStyle::transformOriginX, &RenderStyle::setTransformOriginX),
1305         new LengthPropertyWrapper<Length>(CSSPropertyTransformOriginY, &RenderStyle::transformOriginY, &RenderStyle::setTransformOriginY),
1306         new PropertyWrapper<float>(CSSPropertyTransformOriginZ, &RenderStyle::transformOriginZ, &RenderStyle::setTransformOriginZ),
1307         new LengthPropertyWrapper<LengthSize>(CSSPropertyBorderTopLeftRadius, &RenderStyle::borderTopLeftRadius, &RenderStyle::setBorderTopLeftRadius),
1308         new LengthPropertyWrapper<LengthSize>(CSSPropertyBorderTopRightRadius, &RenderStyle::borderTopRightRadius, &RenderStyle::setBorderTopRightRadius),
1309         new LengthPropertyWrapper<LengthSize>(CSSPropertyBorderBottomLeftRadius, &RenderStyle::borderBottomLeftRadius, &RenderStyle::setBorderBottomLeftRadius),
1310         new LengthPropertyWrapper<LengthSize>(CSSPropertyBorderBottomRightRadius, &RenderStyle::borderBottomRightRadius, &RenderStyle::setBorderBottomRightRadius),
1311         new PropertyWrapper<EVisibility>(CSSPropertyVisibility, &RenderStyle::visibility, &RenderStyle::setVisibility),
1312         new PropertyWrapper<float>(CSSPropertyZoom, &RenderStyle::zoom, &RenderStyle::setZoomWithoutReturnValue),
1313
1314         new LengthPropertyWrapper<LengthBox>(CSSPropertyClip, &RenderStyle::clip, &RenderStyle::setClip),
1315
1316         new PropertyWrapperAcceleratedOpacity(),
1317         new PropertyWrapperAcceleratedTransform(),
1318         new PropertyWrapperAcceleratedFilter(),
1319 #if ENABLE(FILTERS_LEVEL_2)
1320         new PropertyWrapperAcceleratedBackdropFilter(),
1321 #endif
1322         new PropertyWrapperClipPath(CSSPropertyWebkitClipPath, &RenderStyle::clipPath, &RenderStyle::setClipPath),
1323
1324 #if ENABLE(CSS_SHAPES)
1325         new PropertyWrapperShape(CSSPropertyWebkitShapeOutside, &RenderStyle::shapeOutside, &RenderStyle::setShapeOutside),
1326         new LengthPropertyWrapper<Length>(CSSPropertyWebkitShapeMargin, &RenderStyle::shapeMargin, &RenderStyle::setShapeMargin),
1327         new PropertyWrapper<float>(CSSPropertyWebkitShapeImageThreshold, &RenderStyle::shapeImageThreshold, &RenderStyle::setShapeImageThreshold),
1328 #endif
1329
1330         new PropertyWrapperVisitedAffectedColor(CSSPropertyColumnRuleColor, MaybeInvalidColor, &RenderStyle::columnRuleColor, &RenderStyle::setColumnRuleColor, &RenderStyle::visitedLinkColumnRuleColor, &RenderStyle::setVisitedLinkColumnRuleColor),
1331         new PropertyWrapperVisitedAffectedColor(CSSPropertyWebkitTextStrokeColor, MaybeInvalidColor, &RenderStyle::textStrokeColor, &RenderStyle::setTextStrokeColor, &RenderStyle::visitedLinkTextStrokeColor, &RenderStyle::setVisitedLinkTextStrokeColor),
1332         new PropertyWrapperVisitedAffectedColor(CSSPropertyWebkitTextFillColor, MaybeInvalidColor, &RenderStyle::textFillColor, &RenderStyle::setTextFillColor, &RenderStyle::visitedLinkTextFillColor, &RenderStyle::setVisitedLinkTextFillColor),
1333         new PropertyWrapperVisitedAffectedColor(CSSPropertyBorderLeftColor, MaybeInvalidColor, &RenderStyle::borderLeftColor, &RenderStyle::setBorderLeftColor, &RenderStyle::visitedLinkBorderLeftColor, &RenderStyle::setVisitedLinkBorderLeftColor),
1334         new PropertyWrapperVisitedAffectedColor(CSSPropertyBorderRightColor, MaybeInvalidColor, &RenderStyle::borderRightColor, &RenderStyle::setBorderRightColor, &RenderStyle::visitedLinkBorderRightColor, &RenderStyle::setVisitedLinkBorderRightColor),
1335         new PropertyWrapperVisitedAffectedColor(CSSPropertyBorderTopColor, MaybeInvalidColor, &RenderStyle::borderTopColor, &RenderStyle::setBorderTopColor, &RenderStyle::visitedLinkBorderTopColor, &RenderStyle::setVisitedLinkBorderTopColor),
1336         new PropertyWrapperVisitedAffectedColor(CSSPropertyBorderBottomColor, MaybeInvalidColor, &RenderStyle::borderBottomColor, &RenderStyle::setBorderBottomColor, &RenderStyle::visitedLinkBorderBottomColor, &RenderStyle::setVisitedLinkBorderBottomColor),
1337         new PropertyWrapperVisitedAffectedColor(CSSPropertyOutlineColor, MaybeInvalidColor, &RenderStyle::outlineColor, &RenderStyle::setOutlineColor, &RenderStyle::visitedLinkOutlineColor, &RenderStyle::setVisitedLinkOutlineColor),
1338
1339         new PropertyWrapperShadow(CSSPropertyBoxShadow, &RenderStyle::boxShadow, &RenderStyle::setBoxShadow),
1340         new PropertyWrapperShadow(CSSPropertyWebkitBoxShadow, &RenderStyle::boxShadow, &RenderStyle::setBoxShadow),
1341         new PropertyWrapperShadow(CSSPropertyTextShadow, &RenderStyle::textShadow, &RenderStyle::setTextShadow),
1342
1343         new PropertyWrapperSVGPaint(CSSPropertyFill, &RenderStyle::fillPaintType, &RenderStyle::fillPaintColor, &RenderStyle::setFillPaintColor),
1344         new PropertyWrapper<float>(CSSPropertyFillOpacity, &RenderStyle::fillOpacity, &RenderStyle::setFillOpacity),
1345
1346         new PropertyWrapperSVGPaint(CSSPropertyStroke, &RenderStyle::strokePaintType, &RenderStyle::strokePaintColor, &RenderStyle::setStrokePaintColor),
1347         new PropertyWrapper<float>(CSSPropertyStrokeOpacity, &RenderStyle::strokeOpacity, &RenderStyle::setStrokeOpacity),
1348         new PropertyWrapper< Vector<SVGLength>>(CSSPropertyStrokeDasharray, &RenderStyle::strokeDashArray, &RenderStyle::setStrokeDashArray),
1349         new PropertyWrapper<float>(CSSPropertyStrokeMiterlimit, &RenderStyle::strokeMiterLimit, &RenderStyle::setStrokeMiterLimit),
1350
1351         new LengthPropertyWrapper<Length>(CSSPropertyCx, &RenderStyle::cx, &RenderStyle::setCx),
1352         new LengthPropertyWrapper<Length>(CSSPropertyCy, &RenderStyle::cy, &RenderStyle::setCy),
1353         new LengthPropertyWrapper<Length>(CSSPropertyR, &RenderStyle::r, &RenderStyle::setR),
1354         new LengthPropertyWrapper<Length>(CSSPropertyRx, &RenderStyle::rx, &RenderStyle::setRx),
1355         new LengthPropertyWrapper<Length>(CSSPropertyRy, &RenderStyle::ry, &RenderStyle::setRy),
1356         new LengthPropertyWrapper<Length>(CSSPropertyStrokeDashoffset, &RenderStyle::strokeDashOffset, &RenderStyle::setStrokeDashOffset),
1357         new LengthPropertyWrapper<Length>(CSSPropertyStrokeWidth, &RenderStyle::strokeWidth, &RenderStyle::setStrokeWidth),
1358         new LengthPropertyWrapper<Length>(CSSPropertyX, &RenderStyle::x, &RenderStyle::setX),
1359         new LengthPropertyWrapper<Length>(CSSPropertyY, &RenderStyle::y, &RenderStyle::setY),
1360
1361         new PropertyWrapper<float>(CSSPropertyFloodOpacity, &RenderStyle::floodOpacity, &RenderStyle::setFloodOpacity),
1362         new PropertyWrapperMaybeInvalidColor(CSSPropertyFloodColor, &RenderStyle::floodColor, &RenderStyle::setFloodColor),
1363
1364         new PropertyWrapper<float>(CSSPropertyStopOpacity, &RenderStyle::stopOpacity, &RenderStyle::setStopOpacity),
1365         new PropertyWrapperMaybeInvalidColor(CSSPropertyStopColor, &RenderStyle::stopColor, &RenderStyle::setStopColor),
1366
1367         new PropertyWrapperMaybeInvalidColor(CSSPropertyLightingColor, &RenderStyle::lightingColor, &RenderStyle::setLightingColor),
1368
1369         new PropertyWrapper<SVGLength>(CSSPropertyBaselineShift, &RenderStyle::baselineShiftValue, &RenderStyle::setBaselineShiftValue),
1370         new PropertyWrapper<SVGLength>(CSSPropertyKerning, &RenderStyle::kerning, &RenderStyle::setKerning),
1371     };
1372     const unsigned animatableLonghandPropertiesCount = WTF_ARRAY_LENGTH(animatableLonghandPropertyWrappers);
1373
1374     static const CSSPropertyID animatableShorthandProperties[] = {
1375         CSSPropertyBackground, // for background-color, background-position, background-image
1376         CSSPropertyBackgroundPosition,
1377         CSSPropertyFont, // for font-size, font-weight
1378         CSSPropertyWebkitMask, // for mask-position
1379         CSSPropertyWebkitMaskPosition,
1380         CSSPropertyBorderTop, CSSPropertyBorderRight, CSSPropertyBorderBottom, CSSPropertyBorderLeft,
1381         CSSPropertyBorderColor,
1382         CSSPropertyBorderRadius,
1383         CSSPropertyBorderWidth,
1384         CSSPropertyBorder,
1385         CSSPropertyBorderImage,
1386         CSSPropertyBorderSpacing,
1387         CSSPropertyListStyle, // for list-style-image
1388         CSSPropertyMargin,
1389         CSSPropertyOutline,
1390         CSSPropertyPadding,
1391         CSSPropertyWebkitTextStroke,
1392         CSSPropertyColumnRule,
1393         CSSPropertyWebkitBorderRadius,
1394         CSSPropertyTransformOrigin
1395     };
1396     const unsigned animatableShorthandPropertiesCount = WTF_ARRAY_LENGTH(animatableShorthandProperties);
1397
1398     // TODO:
1399     //
1400     //  CSSPropertyVerticalAlign
1401     //
1402     // Compound properties that have components that should be animatable:
1403     //
1404     //  CSSPropertyColumns
1405     //  CSSPropertyWebkitBoxReflect
1406
1407     // Make sure unused slots have a value
1408     for (int i = 0; i < numCSSProperties; ++i)
1409         m_propertyToIdMap[i] = cInvalidPropertyWrapperIndex;
1410
1411     COMPILE_ASSERT(animatableLonghandPropertiesCount + animatableShorthandPropertiesCount < UCHAR_MAX, numberOfAnimatablePropertiesMustBeLessThanUCharMax);
1412     m_propertyWrappers.reserveInitialCapacity(animatableLonghandPropertiesCount + animatableShorthandPropertiesCount);
1413
1414     // First we put the non-shorthand property wrappers into the map, so the shorthand-building
1415     // code can find them.
1416
1417     for (unsigned i = 0; i < animatableLonghandPropertiesCount; ++i) {
1418         AnimationPropertyWrapperBase* wrapper = animatableLonghandPropertyWrappers[i];
1419         m_propertyWrappers.uncheckedAppend(std::unique_ptr<AnimationPropertyWrapperBase>(wrapper));
1420         indexFromPropertyID(wrapper->property()) = i;
1421     }
1422
1423     for (size_t i = 0; i < animatableShorthandPropertiesCount; ++i) {
1424         CSSPropertyID propertyID = animatableShorthandProperties[i];
1425         StylePropertyShorthand shorthand = shorthandForProperty(propertyID);
1426         if (!shorthand.length())
1427             continue;
1428
1429         Vector<AnimationPropertyWrapperBase*> longhandWrappers;
1430         longhandWrappers.reserveInitialCapacity(shorthand.length());
1431         const CSSPropertyID* properties = shorthand.properties();
1432         for (unsigned j = 0; j < shorthand.length(); ++j) {
1433             unsigned wrapperIndex = indexFromPropertyID(properties[j]);
1434             if (wrapperIndex == cInvalidPropertyWrapperIndex)
1435                 continue;
1436             ASSERT(m_propertyWrappers[wrapperIndex]);
1437             longhandWrappers.uncheckedAppend(m_propertyWrappers[wrapperIndex].get());
1438         }
1439
1440         m_propertyWrappers.uncheckedAppend(std::make_unique<ShorthandPropertyWrapper>(propertyID, WTFMove(longhandWrappers)));
1441         indexFromPropertyID(propertyID) = animatableLonghandPropertiesCount + i;
1442     }
1443 }
1444
1445 static bool gatherEnclosingShorthandProperties(CSSPropertyID property, AnimationPropertyWrapperBase* wrapper, HashSet<CSSPropertyID>& propertySet)
1446 {
1447     if (!wrapper->isShorthandWrapper())
1448         return false;
1449
1450     ShorthandPropertyWrapper* shorthandWrapper = static_cast<ShorthandPropertyWrapper*>(wrapper);
1451     bool contained = false;
1452     for (auto& currWrapper : shorthandWrapper->propertyWrappers()) {
1453         if (gatherEnclosingShorthandProperties(property, currWrapper, propertySet) || currWrapper->property() == property)
1454             contained = true;
1455     }
1456
1457     if (contained)
1458         propertySet.add(wrapper->property());
1459
1460     return contained;
1461 }
1462
1463 // Returns true if we need to start animation timers
1464 bool CSSPropertyAnimation::blendProperties(const AnimationBase* anim, CSSPropertyID prop, RenderStyle* dst, const RenderStyle* a, const RenderStyle* b, double progress)
1465 {
1466     ASSERT(prop != CSSPropertyInvalid);
1467
1468     AnimationPropertyWrapperBase* wrapper = CSSPropertyAnimationWrapperMap::singleton().wrapperForProperty(prop);
1469     if (wrapper) {
1470         wrapper->blend(anim, dst, a, b, progress);
1471         return !wrapper->animationIsAccelerated() || !anim->isAccelerated();
1472     }
1473     return false;
1474 }
1475
1476 bool CSSPropertyAnimation::animationOfPropertyIsAccelerated(CSSPropertyID prop)
1477 {
1478     AnimationPropertyWrapperBase* wrapper = CSSPropertyAnimationWrapperMap::singleton().wrapperForProperty(prop);
1479     return wrapper ? wrapper->animationIsAccelerated() : false;
1480 }
1481
1482 // Note: this is inefficient. It's only called from pauseTransitionAtTime().
1483 HashSet<CSSPropertyID> CSSPropertyAnimation::animatableShorthandsAffectingProperty(CSSPropertyID property)
1484 {
1485     CSSPropertyAnimationWrapperMap& map = CSSPropertyAnimationWrapperMap::singleton();
1486
1487     HashSet<CSSPropertyID> foundProperties;
1488     for (unsigned i = 0; i < map.size(); ++i)
1489         gatherEnclosingShorthandProperties(property, map.wrapperForIndex(i), foundProperties);
1490
1491     return foundProperties;
1492 }
1493
1494 bool CSSPropertyAnimation::propertiesEqual(CSSPropertyID prop, const RenderStyle* a, const RenderStyle* b)
1495 {
1496     AnimationPropertyWrapperBase* wrapper = CSSPropertyAnimationWrapperMap::singleton().wrapperForProperty(prop);
1497     if (wrapper)
1498         return wrapper->equals(a, b);
1499     return true;
1500 }
1501
1502 CSSPropertyID CSSPropertyAnimation::getPropertyAtIndex(int i, bool& isShorthand)
1503 {
1504     CSSPropertyAnimationWrapperMap& map = CSSPropertyAnimationWrapperMap::singleton();
1505
1506     if (i < 0 || static_cast<unsigned>(i) >= map.size())
1507         return CSSPropertyInvalid;
1508
1509     AnimationPropertyWrapperBase* wrapper = map.wrapperForIndex(i);
1510     isShorthand = wrapper->isShorthandWrapper();
1511     return wrapper->property();
1512 }
1513
1514 int CSSPropertyAnimation::getNumProperties()
1515 {
1516     return CSSPropertyAnimationWrapperMap::singleton().size();
1517 }
1518
1519 }