/
CSSPropertyAnimation.cpp
3975 lines (3399 loc) · 182 KB
/
CSSPropertyAnimation.cpp
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2007, 2008, 2009, 2013, 2016 Apple Inc. All rights reserved.
* Copyright (C) 2012, 2013 Adobe Systems Incorporated. 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 "CSSPropertyAnimation.h"
#include "AnimationUtilities.h"
#include "CSSPrimitiveValue.h"
#include "CSSPropertyBlendingClient.h"
#include "CSSPropertyNames.h"
#include "CachedImage.h"
#include "CalculationValue.h"
#include "ColorBlending.h"
#include "ComputedStyleExtractor.h"
#include "ContentData.h"
#include "CounterDirectives.h"
#include "FloatConversion.h"
#include "FontCascade.h"
#include "FontSelectionAlgorithm.h"
#include "FontTaggedSettings.h"
#include "GapLength.h"
#include "GridPositionsResolver.h"
#include "IdentityTransformOperation.h"
#include "LengthPoint.h"
#include "Logging.h"
#include "Matrix3DTransformOperation.h"
#include "MatrixTransformOperation.h"
#include "OffsetRotation.h"
#include "PathOperation.h"
#include "QuotesData.h"
#include "RenderBox.h"
#include "RenderStyle.h"
#include "StyleCachedImage.h"
#include "StyleCrossfadeImage.h"
#include "StyleFilterImage.h"
#include "StylePropertyShorthand.h"
#include "StyleResolver.h"
#include "TabSize.h"
#include <algorithm>
#include <memory>
#include <wtf/MathExtras.h>
#include <wtf/NeverDestroyed.h>
#include <wtf/Noncopyable.h>
#include <wtf/PointerComparison.h>
#include <wtf/RefCounted.h>
#include <wtf/text/TextStream.h>
namespace WebCore {
#if !LOG_DISABLED
static TextStream& operator<<(TextStream& stream, CSSPropertyID property)
{
return stream << nameLiteral(property);
}
#endif
struct CSSPropertyBlendingContext : BlendingContext {
const CSSPropertyBlendingClient* client { nullptr };
CSSPropertyBlendingContext(double progress, bool isDiscrete, CompositeOperation compositeOperation, const CSSPropertyBlendingClient* client, IterationCompositeOperation iterationCompositeOperation = IterationCompositeOperation::Replace, double currentIteration = 0)
: BlendingContext(progress, isDiscrete, compositeOperation, iterationCompositeOperation, currentIteration)
, client(client)
{
}
};
static inline int blendFunc(int from, int to, const CSSPropertyBlendingContext& context)
{
return blend(from, to, context);
}
static inline double blendFunc(double from, double to, const CSSPropertyBlendingContext& context)
{
return blend(from, to, context);
}
static inline float blendFunc(float from, float to, const CSSPropertyBlendingContext& context)
{
if (context.iterationCompositeOperation == IterationCompositeOperation::Accumulate && context.currentIteration) {
auto iterationIncrement = context.currentIteration * to;
from += iterationIncrement;
to += iterationIncrement;
}
if (context.compositeOperation == CompositeOperation::Replace)
return narrowPrecisionToFloat(from + (to - from) * context.progress);
return narrowPrecisionToFloat(from + from + (to - from) * context.progress);
}
static inline Color blendFunc(const Color& from, const Color& to, const CSSPropertyBlendingContext& context)
{
return blend(from, to, context);
}
static inline Length blendFunc(const Length& from, const Length& to, const CSSPropertyBlendingContext& context, ValueRange valueRange = ValueRange::All)
{
return blend(from, to, context, valueRange);
}
static inline GapLength blendFunc(const GapLength& from, const GapLength& to, const CSSPropertyBlendingContext& context)
{
if (from.isNormal() || to.isNormal())
return context.progress < 0.5 ? from : to;
return blend(from.length(), to.length(), context, ValueRange::NonNegative);
}
static inline TabSize blendFunc(const TabSize& from, const TabSize& to, const CSSPropertyBlendingContext& context)
{
auto blendedValue = blend(from.value(), to.value(), context);
return { blendedValue < 0 ? 0 : blendedValue, from.isSpaces() ? SpaceValueType : LengthValueType };
}
static inline LengthSize blendFunc(const LengthSize& from, const LengthSize& to, const CSSPropertyBlendingContext& context)
{
return blend(from, to, context, ValueRange::NonNegative);
}
static inline LengthPoint blendFunc(const LengthPoint& from, const LengthPoint& to, const CSSPropertyBlendingContext& context)
{
return blend(from, to, context);
}
static inline ShadowStyle blendFunc(ShadowStyle from, ShadowStyle to, const CSSPropertyBlendingContext& context)
{
if (from == to)
return to;
double fromVal = from == ShadowStyle::Normal ? 1 : 0;
double toVal = to == ShadowStyle::Normal ? 1 : 0;
double result = blendFunc(fromVal, toVal, context);
return result > 0 ? ShadowStyle::Normal : ShadowStyle::Inset;
}
static inline std::unique_ptr<ShadowData> blendFunc(const ShadowData* from, const ShadowData* to, const CSSPropertyBlendingContext& context)
{
ASSERT(from && to);
ASSERT(from->style() == to->style());
return makeUnique<ShadowData>(blend(from->location(), to->location(), context),
blend(from->radius(), to->radius(), context, ValueRange::NonNegative),
blend(from->spread(), to->spread(), context),
blendFunc(from->style(), to->style(), context),
from->isWebkitBoxShadow(),
blend(from->color(), to->color(), context));
}
static inline TransformOperations blendFunc(const TransformOperations& from, const TransformOperations& to, const CSSPropertyBlendingContext& context)
{
if (context.compositeOperation == CompositeOperation::Add) {
ASSERT(context.progress == 1.0);
TransformOperations resultOperations;
resultOperations.operations().appendVector(from.operations());
resultOperations.operations().appendVector(to.operations());
return resultOperations;
}
auto prefix = [&]() -> std::optional<unsigned> {
// We cannot use the pre-computed prefix when dealing with accumulation
// since the values used to accumulate may be different than those held
// in the initial keyframe list.
if (context.compositeOperation == CompositeOperation::Accumulate)
return std::nullopt;
return context.client->transformFunctionListPrefix();
};
auto boxSize = is<RenderBox>(context.client->renderer()) ? downcast<RenderBox>(*context.client->renderer()).borderBoxRect().size() : LayoutSize();
return to.blend(from, context, boxSize, prefix());
}
static RefPtr<ScaleTransformOperation> blendFunc(ScaleTransformOperation* from, ScaleTransformOperation* to, const CSSPropertyBlendingContext& context)
{
if (!from && !to)
return nullptr;
RefPtr<ScaleTransformOperation> identity;
if (!from) {
identity = ScaleTransformOperation::create(1, 1, 1, to->type());
from = identity.get();
} else if (!to) {
identity = ScaleTransformOperation::create(1, 1, 1, from->type());
to = identity.get();
}
// Ensure the two transforms have the same type.
if (!from->isSameType(*to)) {
RefPtr<ScaleTransformOperation> normalizedFrom;
RefPtr<ScaleTransformOperation> normalizedTo;
if (from->is3DOperation() || to->is3DOperation()) {
normalizedFrom = ScaleTransformOperation::create(from->x(), from->y(), from->z(), TransformOperation::SCALE_3D);
normalizedTo = ScaleTransformOperation::create(to->x(), to->y(), to->z(), TransformOperation::SCALE_3D);
} else {
normalizedFrom = ScaleTransformOperation::create(from->x(), from->y(), TransformOperation::SCALE);
normalizedTo = ScaleTransformOperation::create(to->x(), to->y(), TransformOperation::SCALE);
}
return blendFunc(normalizedFrom.get(), normalizedTo.get(), context);
}
auto blendedOperation = to->blend(from, context);
if (is<ScaleTransformOperation>(blendedOperation)) {
auto& scale = downcast<ScaleTransformOperation>(blendedOperation.get());
return ScaleTransformOperation::create(scale.x(), scale.y(), scale.z(), scale.type());
}
return nullptr;
}
static RefPtr<RotateTransformOperation> blendFunc(RotateTransformOperation* from, RotateTransformOperation* to, const CSSPropertyBlendingContext& context)
{
if (!from && !to)
return nullptr;
RefPtr<RotateTransformOperation> identity;
if (!from) {
identity = RotateTransformOperation::create(0, to->type());
from = identity.get();
} else if (!to) {
identity = RotateTransformOperation::create(0, from->type());
to = identity.get();
}
// Ensure the two transforms have the same type.
if (!from->isSameType(*to)) {
RefPtr<RotateTransformOperation> normalizedFrom;
RefPtr<RotateTransformOperation> normalizedTo;
if (from->is3DOperation() || to->is3DOperation()) {
normalizedFrom = RotateTransformOperation::create(from->x(), from->y(), from->z(), from->angle(), TransformOperation::ROTATE_3D);
normalizedTo = RotateTransformOperation::create(to->x(), to->y(), to->z(), to->angle(), TransformOperation::ROTATE_3D);
} else {
normalizedFrom = RotateTransformOperation::create(from->angle(), TransformOperation::ROTATE);
normalizedTo = RotateTransformOperation::create(to->angle(), TransformOperation::ROTATE);
}
return blendFunc(normalizedFrom.get(), normalizedTo.get(), context);
}
auto blendedOperation = to->blend(from, context);
if (is<RotateTransformOperation>(blendedOperation)) {
auto& rotate = downcast<RotateTransformOperation>(blendedOperation.get());
return RotateTransformOperation::create(rotate.x(), rotate.y(), rotate.z(), rotate.angle(), rotate.type());
}
return nullptr;
}
static RefPtr<TranslateTransformOperation> blendFunc(TranslateTransformOperation* from, TranslateTransformOperation* to, const CSSPropertyBlendingContext& context)
{
if (!from && !to)
return nullptr;
RefPtr<TranslateTransformOperation> identity;
if (!from) {
identity = TranslateTransformOperation::create(Length(0, LengthType::Fixed), Length(0, LengthType::Fixed), Length(0, LengthType::Fixed), to->type());
from = identity.get();
} else if (!to) {
identity = TranslateTransformOperation::create(Length(0, LengthType::Fixed), Length(0, LengthType::Fixed), Length(0, LengthType::Fixed), from->type());
to = identity.get();
}
// Ensure the two transforms have the same type.
if (!from->isSameType(*to)) {
RefPtr<TranslateTransformOperation> normalizedFrom;
RefPtr<TranslateTransformOperation> normalizedTo;
if (from->is3DOperation() || to->is3DOperation()) {
normalizedFrom = TranslateTransformOperation::create(from->x(), from->y(), from->z(), TransformOperation::TRANSLATE_3D);
normalizedTo = TranslateTransformOperation::create(to->x(), to->y(), to->z(), TransformOperation::TRANSLATE_3D);
} else {
normalizedFrom = TranslateTransformOperation::create(from->x(), from->y(), TransformOperation::TRANSLATE);
normalizedTo = TranslateTransformOperation::create(to->x(), to->y(), TransformOperation::TRANSLATE);
}
return blendFunc(normalizedFrom.get(), normalizedTo.get(), context);
}
Ref<TransformOperation> blendedOperation = to->blend(from, context);
if (is<TranslateTransformOperation>(blendedOperation)) {
TranslateTransformOperation& translate = downcast<TranslateTransformOperation>(blendedOperation.get());
return TranslateTransformOperation::create(translate.x(), translate.y(), translate.z(), translate.type());
}
return nullptr;
}
static inline RefPtr<PathOperation> blendFunc(PathOperation* from, PathOperation* to, const CSSPropertyBlendingContext& context)
{
if (is<ShapePathOperation>(from) && is<ShapePathOperation>(to)) {
auto& fromShape = downcast<ShapePathOperation>(*from).basicShape();
auto& toShape = downcast<ShapePathOperation>(*to).basicShape();
if (fromShape.canBlend(toShape))
return ShapePathOperation::create(toShape.blend(fromShape, context));
}
if (is<RayPathOperation>(from) && is<RayPathOperation>(to)) {
auto& fromRay = downcast<RayPathOperation>(*from);
auto& toRay = downcast<RayPathOperation>(*to);
if (fromRay.canBlend(toRay))
return fromRay.blend(toRay, context);
}
// fall back to discrete animation.
return context.progress < 0.5 ? from : to;
}
static inline RefPtr<ShapeValue> blendFunc(ShapeValue* from, ShapeValue* to, const CSSPropertyBlendingContext& context)
{
if (context.isDiscrete) {
ASSERT(!context.progress || context.progress == 1);
return context.progress ? to : from;
}
ASSERT(from && to);
const BasicShape& fromShape = *from->shape();
const BasicShape& toShape = *to->shape();
return ShapeValue::create(toShape.blend(fromShape, context), to->cssBox());
}
static inline RefPtr<FilterOperation> blendFunc(FilterOperation* from, FilterOperation* to, const CSSPropertyBlendingContext& context, bool blendToPassthrough = false)
{
ASSERT(to);
return to->blend(from, context, blendToPassthrough);
}
static inline FilterOperations blendFilterOperations(const FilterOperations& from, const FilterOperations& to, const CSSPropertyBlendingContext& context)
{
if (context.compositeOperation == CompositeOperation::Add) {
ASSERT(context.progress == 1.0);
FilterOperations resultOperations;
resultOperations.operations().appendVector(from.operations());
resultOperations.operations().appendVector(to.operations());
return resultOperations;
}
if (context.isDiscrete) {
ASSERT(!context.progress || context.progress == 1.0);
return context.progress ? to : from;
}
FilterOperations result;
size_t fromSize = from.operations().size();
size_t toSize = to.operations().size();
size_t size = std::max(fromSize, toSize);
for (size_t i = 0; i < size; i++) {
RefPtr<FilterOperation> fromOp = (i < fromSize) ? from.operations()[i].get() : nullptr;
RefPtr<FilterOperation> toOp = (i < toSize) ? to.operations()[i].get() : nullptr;
RefPtr<FilterOperation> blendedOp = toOp ? blendFunc(fromOp.get(), toOp.get(), context) : (fromOp ? blendFunc(0, fromOp.get(), context, true) : nullptr);
if (blendedOp)
result.operations().append(blendedOp);
else {
auto identityOp = PassthroughFilterOperation::create();
if (context.progress > 0.5)
result.operations().append(toOp ? toOp : WTFMove(identityOp));
else
result.operations().append(fromOp ? fromOp : WTFMove(identityOp));
}
}
return result;
}
static inline FilterOperations blendFunc(const FilterOperations& from, const FilterOperations& to, const CSSPropertyBlendingContext& context)
{
return blendFilterOperations(from, to, context);
}
static inline RefPtr<StyleImage> blendFilter(RefPtr<StyleImage> inputImage, const FilterOperations& from, const FilterOperations& to, const CSSPropertyBlendingContext& context)
{
auto filterResult = blendFilterOperations(from, to, context);
return StyleFilterImage::create(WTFMove(inputImage), WTFMove(filterResult));
}
static inline Visibility blendFunc(Visibility from, Visibility to, const CSSPropertyBlendingContext& context)
{
if (from != Visibility::Visible && to != Visibility::Visible)
return context.progress < 0.5 ? from : to;
// Any non-zero result means we consider the object to be visible. Only at 0 do we consider the object to be
// invisible. The invisible value we use (Visibility::Hidden vs. Visibility::Collapse) depends on the specified from/to values.
double fromVal = from == Visibility::Visible ? 1. : 0.;
double toVal = to == Visibility::Visible ? 1. : 0.;
if (fromVal == toVal)
return to;
// The composite operation here is irrelevant.
double result = blendFunc(fromVal, toVal, { context.progress, false, CompositeOperation::Replace, nullptr });
return result > 0. ? Visibility::Visible : (to != Visibility::Visible ? to : from);
}
static inline TextUnderlineOffset blendFunc(const TextUnderlineOffset& from, const TextUnderlineOffset& to, const CSSPropertyBlendingContext& context)
{
if (from.isLength() && to.isLength())
return TextUnderlineOffset::createWithLength(blendFunc(from.lengthValue(), to.lengthValue(), context));
return TextUnderlineOffset::createWithAuto();
}
static inline TextDecorationThickness blendFunc(const TextDecorationThickness& from, const TextDecorationThickness& to, const CSSPropertyBlendingContext& context)
{
if (from.isLength() && to.isLength())
return TextDecorationThickness::createWithLength(blendFunc(from.lengthValue(), to.lengthValue(), context));
return TextDecorationThickness::createWithAuto();
}
static inline LengthBox blendFunc(const LengthBox& from, const LengthBox& to, const CSSPropertyBlendingContext& context, ValueRange valueRange = ValueRange::NonNegative)
{
LengthBox result(blendFunc(from.top(), to.top(), context, valueRange),
blendFunc(from.right(), to.right(), context, valueRange),
blendFunc(from.bottom(), to.bottom(), context, valueRange),
blendFunc(from.left(), to.left(), context, valueRange));
return result;
}
static inline SVGLengthValue blendFunc(const SVGLengthValue& from, const SVGLengthValue& to, const CSSPropertyBlendingContext& context)
{
return SVGLengthValue::blend(from, to, narrowPrecisionToFloat(context.progress));
}
static inline Vector<SVGLengthValue> blendFunc(const Vector<SVGLengthValue>& from, const Vector<SVGLengthValue>& to, const CSSPropertyBlendingContext& context)
{
size_t fromLength = from.size();
size_t toLength = to.size();
if (!fromLength || !toLength)
return context.progress < 0.5 ? from : to;
size_t resultLength = fromLength;
if (fromLength != toLength) {
if (!remainder(std::max(fromLength, toLength), std::min(fromLength, toLength)))
resultLength = std::max(fromLength, toLength);
else
resultLength = fromLength * toLength;
}
Vector<SVGLengthValue> result(resultLength);
for (size_t i = 0; i < resultLength; ++i)
result[i] = SVGLengthValue::blend(from[i % fromLength], to[i % toLength], narrowPrecisionToFloat(context.progress));
return result;
}
static inline RefPtr<StyleImage> crossfadeBlend(StyleCachedImage& fromStyleImage, StyleCachedImage& toStyleImage, const CSSPropertyBlendingContext& context)
{
// If progress is at one of the extremes, we want getComputedStyle to show the image,
// not a completed cross-fade, so we hand back one of the existing images.
if (!context.progress)
return &fromStyleImage;
if (context.progress == 1)
return &toStyleImage;
if (!fromStyleImage.cachedImage() || !toStyleImage.cachedImage())
return &toStyleImage;
return StyleCrossfadeImage::create(&fromStyleImage, &toStyleImage, context.progress, false);
}
static inline RefPtr<StyleImage> blendFunc(StyleImage* from, StyleImage* to, const CSSPropertyBlendingContext& context)
{
if (!context.progress)
return from;
if (context.progress == 1.0)
return to;
ASSERT(from && to);
from = from->selectedImage();
to = to->selectedImage();
if (!from || !to)
return to;
// Animation between two generated images. Cross fade for all other cases.
if (is<StyleFilterImage>(*from) && is<StyleFilterImage>(*to)) {
// Animation of generated images just possible if input images are equal.
// Otherwise fall back to cross fade animation.
auto& fromFilter = downcast<StyleFilterImage>(*from);
auto& toFilter = downcast<StyleFilterImage>(*to);
if (fromFilter.equalInputImages(toFilter) && is<StyleCachedImage>(fromFilter.inputImage()))
return blendFilter(fromFilter.inputImage(), fromFilter.filterOperations(), toFilter.filterOperations(), context);
} else if (is<StyleCrossfadeImage>(*from) && is<StyleCrossfadeImage>(*to)) {
auto& fromCrossfade = downcast<StyleCrossfadeImage>(*from);
auto& toCrossfade = downcast<StyleCrossfadeImage>(*to);
if (fromCrossfade.equalInputImages(toCrossfade)) {
if (auto crossfadeBlend = toCrossfade.blend(fromCrossfade, context))
return crossfadeBlend;
}
} else if (is<StyleFilterImage>(*from) && is<StyleCachedImage>(*to)) {
auto& fromFilter = downcast<StyleFilterImage>(*from);
auto fromFilterInputImage = fromFilter.inputImage();
if (is<StyleCachedImage>(fromFilterInputImage) && downcast<StyleCachedImage>(*to).equals(downcast<StyleCachedImage>(*fromFilterInputImage)))
return blendFilter(WTFMove(fromFilterInputImage), fromFilter.filterOperations(), FilterOperations(), context);
} else if (is<StyleCachedImage>(*from) && is<StyleFilterImage>(*to)) {
auto& toFilter = downcast<StyleFilterImage>(*to);
auto toFilterInputImage = toFilter.inputImage();
if (is<StyleCachedImage>(toFilterInputImage) && downcast<StyleCachedImage>(*from).equals(downcast<StyleCachedImage>(*toFilterInputImage)))
return blendFilter(WTFMove(toFilterInputImage), FilterOperations(), toFilter.filterOperations(), context);
}
if (is<StyleCachedImage>(*from) && is<StyleCachedImage>(*to))
return crossfadeBlend(downcast<StyleCachedImage>(*from), downcast<StyleCachedImage>(*to), context);
// FIXME: Add support for animation between two *gradient() functions.
// https://bugs.webkit.org/show_bug.cgi?id=119956
// FIXME: Add support cross fade between cached and generated images.
// https://bugs.webkit.org/show_bug.cgi?id=78293
return to;
}
static inline NinePieceImage blendFunc(const NinePieceImage& from, const NinePieceImage& to, const CSSPropertyBlendingContext& context)
{
if (!from.hasImage() || !to.hasImage())
return to;
// FIXME (74112): Support transitioning between NinePieceImages that differ by more than image content.
if (from.imageSlices() != to.imageSlices() || from.borderSlices() != to.borderSlices() || from.outset() != to.outset() || from.fill() != to.fill() || from.overridesBorderWidths() != to.overridesBorderWidths() || from.horizontalRule() != to.horizontalRule() || from.verticalRule() != to.verticalRule())
return to;
if (auto* renderer = context.client->renderer()) {
if (from.image()->imageSize(renderer, 1.0) != to.image()->imageSize(renderer, 1.0))
return to;
}
return NinePieceImage(blendFunc(from.image(), to.image(), context),
from.imageSlices(), from.fill(), from.borderSlices(), from.overridesBorderWidths(), from.outset(), from.horizontalRule(), from.verticalRule());
}
#if ENABLE(VARIATION_FONTS)
static inline FontVariationSettings blendFunc(const FontVariationSettings& from, const FontVariationSettings& to, const CSSPropertyBlendingContext& context)
{
if (context.isDiscrete) {
ASSERT(!context.progress || context.progress == 1.0);
return context.progress ? to : from;
}
ASSERT(from.size() == to.size());
FontVariationSettings result;
unsigned size = from.size();
for (unsigned i = 0; i < size; ++i) {
auto& fromItem = from.at(i);
auto& toItem = to.at(i);
ASSERT(fromItem.tag() == toItem.tag());
float interpolated = blendFunc(fromItem.value(), toItem.value(), context);
result.insert({ fromItem.tag(), interpolated });
}
return result;
}
#endif
static inline FontSelectionValue blendFunc(FontSelectionValue from, FontSelectionValue to, const CSSPropertyBlendingContext& context)
{
return FontSelectionValue(std::max(0.0f, blendFunc(static_cast<float>(from), static_cast<float>(to), context)));
}
static inline std::optional<FontSelectionValue> blendFunc(std::optional<FontSelectionValue> from, std::optional<FontSelectionValue> to, const CSSPropertyBlendingContext& context)
{
return blendFunc(*from, *to, context);
}
static inline OffsetRotation blendFunc(const OffsetRotation& from, const OffsetRotation& to, const CSSPropertyBlendingContext& context)
{
if (context.isDiscrete) {
ASSERT(!context.progress || context.progress == 1.0);
return context.progress ? to : from;
}
ASSERT(from.hasAuto() == to.hasAuto());
return OffsetRotation(from.hasAuto(), clampTo<float>(blend(from.angle(), to.angle(), context)));
}
static inline bool canInterpolate(const GridTrackList& from, const GridTrackList& to)
{
if (from.size() != to.size())
return false;
size_t i = 0;
auto visitor = WTF::makeVisitor([&](const GridTrackSize&) {
return std::holds_alternative<GridTrackSize>(to[i]);
}, [&](const Vector<String>&) {
return std::holds_alternative<Vector<String>>(to[i]);
}, [&](const GridTrackEntryRepeat& repeat) {
if (!std::holds_alternative<GridTrackEntryRepeat>(to[i]))
return false;
const auto& toEntry = std::get<GridTrackEntryRepeat>(to[i]);
return repeat.repeats == toEntry.repeats && repeat.list.size() == toEntry.list.size();
}, [&](const GridTrackEntryAutoRepeat&) {
return false;
}, [&](const GridTrackEntrySubgrid&) {
return false;
}, [&](const GridTrackEntryMasonry&) {
return false;
});
for (i = 0; i < from.size(); i++) {
if (!std::visit(visitor, from[i]))
return false;
}
return true;
}
static inline GridLength blendFunc(const GridLength& from, const GridLength& to, const CSSPropertyBlendingContext& context)
{
if (from.isFlex() != to.isFlex())
return context.progress < 0.5 ? from : to;
if (from.isFlex())
return GridLength(blend(from.flex(), to.flex(), context));
return GridLength(blendFunc(from.length(), to.length(), context));
}
static inline GridTrackSize blendFunc(const GridTrackSize& from, const GridTrackSize& to, const CSSPropertyBlendingContext& context)
{
if (from.type() != to.type())
return context.progress < 0.5 ? from : to;
if (from.type() == LengthTrackSizing) {
auto length = blendFunc(from.minTrackBreadth(), to.minTrackBreadth(), context);
return GridTrackSize(length, LengthTrackSizing);
}
if (from.type() == MinMaxTrackSizing) {
auto minTrackBreadth = blendFunc(from.minTrackBreadth(), to.minTrackBreadth(), context);
auto maxTrackBreadth = blendFunc(from.maxTrackBreadth(), to.maxTrackBreadth(), context);
return GridTrackSize(minTrackBreadth, maxTrackBreadth);
}
auto fitContentBreadth = blendFunc(from.fitContentTrackBreadth(), to.fitContentTrackBreadth(), context);
return GridTrackSize(fitContentBreadth, FitContentTrackSizing);
}
static inline RepeatTrackList blendFunc(const RepeatTrackList& from, const RepeatTrackList& to, const CSSPropertyBlendingContext& context)
{
RepeatTrackList result;
size_t i = 0;
auto visitor = WTF::makeVisitor([&](const GridTrackSize& size) {
result.append(blendFunc(size, std::get<GridTrackSize>(to[i]), context));
}, [&](const Vector<String>& names) {
if (context.progress < 0.5)
result.append(names);
else {
const Vector<String>& toNames = std::get<Vector<String>>(to[i]);
result.append(toNames);
}
});
for (i = 0; i < from.size(); i++)
std::visit(visitor, from[i]);
return result;
}
static inline GridTrackList blendFunc(const GridTrackList& from, const GridTrackList& to, const CSSPropertyBlendingContext& context)
{
if (!canInterpolate(from, to))
return context.progress < 0.5 ? from : to;
GridTrackList result;
size_t i = 0;
auto visitor = WTF::makeVisitor([&](const GridTrackSize& size) {
result.append(blendFunc(size, std::get<GridTrackSize>(to[i]), context));
}, [&](const Vector<String>& names) {
if (context.progress < 0.5)
result.append(names);
else {
const Vector<String>& toNames = std::get<Vector<String>>(to[i]);
result.append(toNames);
}
}, [&](const GridTrackEntryRepeat& repeatFrom) {
const auto& repeatTo = std::get<GridTrackEntryRepeat>(to[i]);
GridTrackEntryRepeat repeatResult;
repeatResult.repeats = repeatFrom.repeats;
repeatResult.list = blendFunc(repeatFrom.list, repeatTo.list, context);
result.append(WTFMove(repeatResult));
}, [&](const GridTrackEntryAutoRepeat& repeatFrom) {
const auto& repeatTo = std::get<GridTrackEntryAutoRepeat>(to[i]);
GridTrackEntryAutoRepeat repeatResult;
repeatResult.type = repeatFrom.type;
repeatResult.list = blendFunc(repeatFrom.list, repeatTo.list, context);
result.append(WTFMove(repeatResult));
}, [&](const GridTrackEntrySubgrid&) {
}, [&](const GridTrackEntryMasonry&) {
});
for (i = 0; i < from.size(); i++)
std::visit(visitor, from[i]);
return result;
}
class AnimationPropertyWrapperBase {
WTF_MAKE_NONCOPYABLE(AnimationPropertyWrapperBase);
WTF_MAKE_FAST_ALLOCATED;
public:
explicit AnimationPropertyWrapperBase(CSSPropertyID property)
: m_property(property)
{
}
virtual ~AnimationPropertyWrapperBase() = default;
virtual bool isShorthandWrapper() const { return false; }
virtual bool isAdditiveOrCumulative() const { return true; }
virtual bool requiresBlendingForAccumulativeIteration(const RenderStyle&, const RenderStyle&) const { return false; }
virtual bool equals(const RenderStyle&, const RenderStyle&) const = 0;
virtual bool canInterpolate(const RenderStyle&, const RenderStyle&, CompositeOperation) const { return true; }
virtual void blend(RenderStyle&, const RenderStyle&, const RenderStyle&, const CSSPropertyBlendingContext&) const = 0;
#if !LOG_DISABLED
virtual void logBlend(const RenderStyle&, const RenderStyle&, const RenderStyle&, double) const = 0;
#endif
CSSPropertyID property() const { return m_property; }
virtual bool animationIsAccelerated() const { return false; }
private:
CSSPropertyID m_property;
};
template <typename T>
class PropertyWrapperGetter : public AnimationPropertyWrapperBase {
WTF_MAKE_FAST_ALLOCATED;
public:
PropertyWrapperGetter(CSSPropertyID property, T (RenderStyle::*getter)() const)
: AnimationPropertyWrapperBase(property)
, m_getter(getter)
{
}
T value(const RenderStyle& style) const
{
return (style.*m_getter)();
}
bool equals(const RenderStyle& a, const RenderStyle& b) const override
{
if (&a == &b)
return true;
return value(a) == value(b);
}
#if !LOG_DISABLED
void logBlend(const RenderStyle& from, const RenderStyle& to, const RenderStyle& destination, double progress) const final
{
LOG_WITH_STREAM(Animations, stream << " blending " << property() << " from " << value(from) << " to " << value(to) << " at " << TextStream::FormatNumberRespectingIntegers(progress) << " -> " << value(destination));
}
#endif
private:
T (RenderStyle::*m_getter)() const;
};
template <typename T>
class PropertyWrapper : public PropertyWrapperGetter<T> {
WTF_MAKE_FAST_ALLOCATED;
public:
PropertyWrapper(CSSPropertyID property, T (RenderStyle::*getter)() const, void (RenderStyle::*setter)(T))
: PropertyWrapperGetter<T>(property, getter)
, m_setter(setter)
{
}
void blend(RenderStyle& destination, const RenderStyle& from, const RenderStyle& to, const CSSPropertyBlendingContext& context) const override
{
(destination.*m_setter)(blendFunc(this->value(from), this->value(to), context));
}
protected:
void (RenderStyle::*m_setter)(T);
};
class OffsetRotatePropertyWrapper final : public PropertyWrapperGetter<OffsetRotation> {
WTF_MAKE_FAST_ALLOCATED;
public:
OffsetRotatePropertyWrapper(CSSPropertyID property, OffsetRotation (RenderStyle::*getter)() const, void (RenderStyle::*setter)(OffsetRotation&&))
: PropertyWrapperGetter(property, getter)
, m_setter(setter)
{
}
bool canInterpolate(const RenderStyle& from, const RenderStyle& to, CompositeOperation) const final
{
return value(from).hasAuto() == value(to).hasAuto();
}
void blend(RenderStyle& destination, const RenderStyle& from, const RenderStyle& to, const CSSPropertyBlendingContext& context) const final
{
(destination.*m_setter)(blendFunc(value(from), value(to), context));
}
private:
void (RenderStyle::*m_setter)(OffsetRotation&&);
};
template <typename T>
class PositivePropertyWrapper final : public PropertyWrapper<T> {
WTF_MAKE_FAST_ALLOCATED;
public:
PositivePropertyWrapper(CSSPropertyID property, T (RenderStyle::*getter)() const, void (RenderStyle::*setter)(T))
: PropertyWrapper<T>(property, getter, setter)
{
}
private:
void blend(RenderStyle& destination, const RenderStyle& from, const RenderStyle& to, const CSSPropertyBlendingContext& context) const final
{
auto blendedValue = blendFunc(this->value(from), this->value(to), context);
(destination.*this->m_setter)(blendedValue > 1 ? blendedValue : 1);
}
};
template <typename T>
class DiscretePropertyWrapper : public PropertyWrapperGetter<T> {
WTF_MAKE_FAST_ALLOCATED;
public:
DiscretePropertyWrapper(CSSPropertyID property, T (RenderStyle::*getter)() const, void (RenderStyle::*setter)(T))
: PropertyWrapperGetter<T>(property, getter)
, m_setter(setter)
{
}
void blend(RenderStyle& destination, const RenderStyle& from, const RenderStyle& to, const CSSPropertyBlendingContext& context) const override
{
ASSERT(!context.progress || context.progress == 1.0);
(destination.*this->m_setter)(this->value(context.progress ? to : from));
}
private:
bool canInterpolate(const RenderStyle&, const RenderStyle&, CompositeOperation) const final { return false; }
void (RenderStyle::*m_setter)(T);
};
class GridTemplatePropertyWrapper final : public PropertyWrapper<const GridTrackList&> {
WTF_MAKE_FAST_ALLOCATED;
public:
GridTemplatePropertyWrapper(CSSPropertyID property, const GridTrackList& (RenderStyle::*getter)() const, void (RenderStyle::*setter)(const GridTrackList&))
: PropertyWrapper(property, getter, setter)
{
}
private:
void blend(RenderStyle& destination, const RenderStyle& from, const RenderStyle& to, const CSSPropertyBlendingContext& context) const final
{
(destination.*m_setter)(blendFunc(this->value(from), this->value(to), context));
}
bool canInterpolate(const RenderStyle& from, const RenderStyle& to, CompositeOperation) const final
{
return WebCore::canInterpolate(this->value(from), this->value(to));
}
};
class BorderImageRepeatWrapper final : public AnimationPropertyWrapperBase {
WTF_MAKE_FAST_ALLOCATED;
public:
BorderImageRepeatWrapper()
: AnimationPropertyWrapperBase(CSSPropertyBorderImageRepeat)
, m_horizontalWrapper(DiscretePropertyWrapper<NinePieceImageRule>(CSSPropertyBorderImageRepeat, &RenderStyle::borderImageHorizontalRule, &RenderStyle::setBorderImageHorizontalRule))
, m_verticalWrapper(DiscretePropertyWrapper<NinePieceImageRule>(CSSPropertyBorderImageRepeat, &RenderStyle::borderImageVerticalRule, &RenderStyle::setBorderImageVerticalRule))
{
}
private:
bool canInterpolate(const RenderStyle&, const RenderStyle&, CompositeOperation) const final { return false; }
bool equals(const RenderStyle& a, const RenderStyle& b) const override
{
return m_horizontalWrapper.equals(a, b) && m_verticalWrapper.equals(a, b);
}
void blend(RenderStyle& destination, const RenderStyle& from, const RenderStyle& to, const CSSPropertyBlendingContext& context) const final
{
m_horizontalWrapper.blend(destination, from, to, context);
m_verticalWrapper.blend(destination, from, to, context);
}
#if !LOG_DISABLED
void logBlend(const RenderStyle& from, const RenderStyle& to, const RenderStyle& destination, double progress) const final
{
m_horizontalWrapper.logBlend(from, to, destination, progress);
m_verticalWrapper.logBlend(from, to, destination, progress);
}
#endif
DiscretePropertyWrapper<NinePieceImageRule> m_horizontalWrapper;
DiscretePropertyWrapper<NinePieceImageRule> m_verticalWrapper;
};
template <typename T>
class RefCountedPropertyWrapper : public PropertyWrapperGetter<T*> {
WTF_MAKE_FAST_ALLOCATED;
public:
RefCountedPropertyWrapper(CSSPropertyID property, T* (RenderStyle::*getter)() const, void (RenderStyle::*setter)(RefPtr<T>&&))
: PropertyWrapperGetter<T*>(property, getter)
, m_setter(setter)
{
}
private:
void blend(RenderStyle& destination, const RenderStyle& from, const RenderStyle& to, const CSSPropertyBlendingContext& context) const final
{
(destination.*this->m_setter)(blendFunc(this->value(from), this->value(to), context));
}
void (RenderStyle::*m_setter)(RefPtr<T>&&);
};
static bool canInterpolateLengths(const Length& from, const Length& to, bool isLengthPercentage)
{
if (from.type() == to.type())
return true;
// Some properties allow for <length-percentage> and <number> values. We must allow animating
// between a <length> and a <percentage>, but exclude animating between a <number> and either
// a <length> or <percentage>. We can use Length::isRelative() to determine whether we are
// dealing with a <number> as opposed to a <length> or <percentage>.
if (isLengthPercentage) {
return (from.isFixed() || from.isPercentOrCalculated() || from.isRelative())
&& (to.isFixed() || to.isPercentOrCalculated() || to.isRelative())
&& from.isRelative() == to.isRelative();
}
if (from.isCalculated())
return to.isFixed() || to.isPercentOrCalculated();
if (to.isCalculated())
return from.isFixed() || from.isPercentOrCalculated();
return false;
}
static bool lengthsRequireBlendingForAccumulativeIteration(const Length& from, const Length& to)
{
// If blending the values can yield a calc() value, we must go through the blending code for iterationComposite.
return from.isCalculated() || to.isCalculated() || from.type() != to.type();
}
class LengthPropertyWrapper : public PropertyWrapperGetter<const Length&> {
WTF_MAKE_FAST_ALLOCATED;
public:
enum class Flags {
IsLengthPercentage = 1 << 0,
NegativeLengthsAreInvalid = 1 << 1,
};
LengthPropertyWrapper(CSSPropertyID property, const Length& (RenderStyle::*getter)() const, void (RenderStyle::*setter)(Length&&), OptionSet<Flags> flags = { })
: PropertyWrapperGetter(property, getter)
, m_setter(setter)
, m_flags(flags)
{
}
protected:
bool canInterpolate(const RenderStyle& from, const RenderStyle& to, CompositeOperation) const override
{
return canInterpolateLengths(value(from), value(to), m_flags.contains(Flags::IsLengthPercentage));
}
bool requiresBlendingForAccumulativeIteration(const RenderStyle& from, const RenderStyle& to) const final
{
return lengthsRequireBlendingForAccumulativeIteration(value(from), value(to));
}
void blend(RenderStyle& destination, const RenderStyle& from, const RenderStyle& to, const CSSPropertyBlendingContext& context) const override
{
auto valueRange = m_flags.contains(Flags::NegativeLengthsAreInvalid) ? ValueRange::NonNegative : ValueRange::All;
(destination.*m_setter)(blendFunc(value(from), value(to), context, valueRange));
}
private:
void (RenderStyle::*m_setter)(Length&&);
OptionSet<Flags> m_flags;
};
static bool canInterpolateLengthVariants(const LengthSize& from, const LengthSize& to)