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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "mozilla/dom/KeyframeEffect.h"
#include "mozilla/dom/Animation.h"
#include "mozilla/dom/KeyframeAnimationOptionsBinding.h"
// For UnrestrictedDoubleOrKeyframeAnimationOptions;
#include "mozilla/dom/KeyframeEffectBinding.h"
#include "mozilla/dom/MutationObservers.h"
#include "mozilla/layers/AnimationInfo.h"
#include "mozilla/AnimationUtils.h"
#include "mozilla/AutoRestore.h"
#include "mozilla/ComputedStyleInlines.h"
#include "mozilla/EffectSet.h"
#include "mozilla/FloatingPoint.h" // For IsFinite
#include "mozilla/LayerAnimationInfo.h"
#include "mozilla/LookAndFeel.h" // For LookAndFeel::GetInt
#include "mozilla/KeyframeUtils.h"
#include "mozilla/PresShell.h"
#include "mozilla/PresShellInlines.h"
#include "mozilla/ServoBindings.h"
#include "mozilla/StaticPrefs_dom.h"
#include "mozilla/StaticPrefs_gfx.h"
#include "mozilla/StaticPrefs_layers.h"
#include "nsComputedDOMStyle.h" // nsComputedDOMStyle::GetComputedStyle
#include "nsContentUtils.h"
#include "nsCSSPropertyIDSet.h"
#include "nsCSSProps.h" // For nsCSSProps::PropHasFlags
#include "nsCSSPseudoElements.h" // For PseudoStyleType
#include "nsDOMMutationObserver.h" // For nsAutoAnimationMutationBatch
#include "nsIFrame.h"
#include "nsIFrameInlines.h"
#include "nsIScrollableFrame.h"
#include "nsPresContextInlines.h"
#include "nsRefreshDriver.h"
#include "js/PropertyAndElement.h" // JS_DefineProperty
#include "WindowRenderer.h"
namespace mozilla {
void AnimationProperty::SetPerformanceWarning(
const AnimationPerformanceWarning& aWarning, const dom::Element* aElement) {
if (mPerformanceWarning && *mPerformanceWarning == aWarning) {
return;
}
mPerformanceWarning = Some(aWarning);
nsAutoString localizedString;
if (StaticPrefs::layers_offmainthreadcomposition_log_animations() &&
mPerformanceWarning->ToLocalizedString(localizedString)) {
nsAutoCString logMessage = NS_ConvertUTF16toUTF8(localizedString);
AnimationUtils::LogAsyncAnimationFailure(logMessage, aElement);
}
}
bool PropertyValuePair::operator==(const PropertyValuePair& aOther) const {
if (mProperty != aOther.mProperty) {
return false;
}
if (mServoDeclarationBlock == aOther.mServoDeclarationBlock) {
return true;
}
if (!mServoDeclarationBlock || !aOther.mServoDeclarationBlock) {
return false;
}
return Servo_DeclarationBlock_Equals(mServoDeclarationBlock,
aOther.mServoDeclarationBlock);
}
namespace dom {
NS_IMPL_CYCLE_COLLECTION_INHERITED(KeyframeEffect, AnimationEffect,
mTarget.mElement)
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(KeyframeEffect, AnimationEffect)
NS_IMPL_CYCLE_COLLECTION_TRACE_END
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(KeyframeEffect)
NS_INTERFACE_MAP_END_INHERITING(AnimationEffect)
NS_IMPL_ADDREF_INHERITED(KeyframeEffect, AnimationEffect)
NS_IMPL_RELEASE_INHERITED(KeyframeEffect, AnimationEffect)
KeyframeEffect::KeyframeEffect(Document* aDocument,
OwningAnimationTarget&& aTarget,
TimingParams&& aTiming,
const KeyframeEffectParams& aOptions)
: AnimationEffect(aDocument, std::move(aTiming)),
mTarget(std::move(aTarget)),
mEffectOptions(aOptions) {}
KeyframeEffect::KeyframeEffect(Document* aDocument,
OwningAnimationTarget&& aTarget,
const KeyframeEffect& aOther)
: AnimationEffect(aDocument, TimingParams{aOther.SpecifiedTiming()}),
mTarget(std::move(aTarget)),
mEffectOptions{aOther.IterationComposite(), aOther.Composite(),
mTarget.mPseudoType},
mKeyframes(aOther.mKeyframes.Clone()),
mProperties(aOther.mProperties.Clone()),
mBaseValues(aOther.mBaseValues.Clone()) {}
JSObject* KeyframeEffect::WrapObject(JSContext* aCx,
JS::Handle<JSObject*> aGivenProto) {
return KeyframeEffect_Binding::Wrap(aCx, this, aGivenProto);
}
IterationCompositeOperation KeyframeEffect::IterationComposite() const {
return mEffectOptions.mIterationComposite;
}
void KeyframeEffect::SetIterationComposite(
const IterationCompositeOperation& aIterationComposite) {
if (mEffectOptions.mIterationComposite == aIterationComposite) {
return;
}
if (mAnimation && mAnimation->IsRelevant()) {
MutationObservers::NotifyAnimationChanged(mAnimation);
}
mEffectOptions.mIterationComposite = aIterationComposite;
RequestRestyle(EffectCompositor::RestyleType::Layer);
}
CompositeOperation KeyframeEffect::Composite() const {
return mEffectOptions.mComposite;
}
void KeyframeEffect::SetComposite(const CompositeOperation& aComposite) {
if (mEffectOptions.mComposite == aComposite) {
return;
}
mEffectOptions.mComposite = aComposite;
if (mAnimation && mAnimation->IsRelevant()) {
MutationObservers::NotifyAnimationChanged(mAnimation);
}
if (mTarget) {
RefPtr<const ComputedStyle> computedStyle =
GetTargetComputedStyle(Flush::None);
if (computedStyle) {
UpdateProperties(computedStyle);
}
}
}
void KeyframeEffect::NotifySpecifiedTimingUpdated() {
// Use the same document for a pseudo element and its parent element.
// Use nullptr if we don't have mTarget, so disable the mutation batch.
nsAutoAnimationMutationBatch mb(mTarget ? mTarget.mElement->OwnerDoc()
: nullptr);
if (mAnimation) {
mAnimation->NotifyEffectTimingUpdated();
if (mAnimation->IsRelevant()) {
MutationObservers::NotifyAnimationChanged(mAnimation);
}
RequestRestyle(EffectCompositor::RestyleType::Layer);
}
}
void KeyframeEffect::NotifyAnimationTimingUpdated(
PostRestyleMode aPostRestyle) {
UpdateTargetRegistration();
// If the effect is not relevant it will be removed from the target
// element's effect set. However, effects not in the effect set
// will not be included in the set of candidate effects for running on
// the compositor and hence they won't have their compositor status
// updated. As a result, we need to make sure we clear their compositor
// status here.
bool isRelevant = mAnimation && mAnimation->IsRelevant();
if (!isRelevant) {
ResetIsRunningOnCompositor();
}
// Request restyle if necessary.
if (aPostRestyle == PostRestyleMode::IfNeeded && mAnimation &&
!mProperties.IsEmpty() && HasComputedTimingChanged()) {
EffectCompositor::RestyleType restyleType =
CanThrottle() ? EffectCompositor::RestyleType::Throttled
: EffectCompositor::RestyleType::Standard;
RequestRestyle(restyleType);
}
// Detect changes to "in effect" status since we need to recalculate the
// animation cascade for this element whenever that changes.
// Note that updating mInEffectOnLastAnimationTimingUpdate has to be done
// after above CanThrottle() call since the function uses the flag inside it.
bool inEffect = IsInEffect();
if (inEffect != mInEffectOnLastAnimationTimingUpdate) {
MarkCascadeNeedsUpdate();
mInEffectOnLastAnimationTimingUpdate = inEffect;
}
// If we're no longer "in effect", our ComposeStyle method will never be
// called and we will never have a chance to update mProgressOnLastCompose
// and mCurrentIterationOnLastCompose.
// We clear them here to ensure that if we later become "in effect" we will
// request a restyle (above).
if (!inEffect) {
mProgressOnLastCompose.SetNull();
mCurrentIterationOnLastCompose = 0;
}
}
static bool KeyframesEqualIgnoringComputedOffsets(
const nsTArray<Keyframe>& aLhs, const nsTArray<Keyframe>& aRhs) {
if (aLhs.Length() != aRhs.Length()) {
return false;
}
for (size_t i = 0, len = aLhs.Length(); i < len; ++i) {
const Keyframe& a = aLhs[i];
const Keyframe& b = aRhs[i];
if (a.mOffset != b.mOffset || a.mTimingFunction != b.mTimingFunction ||
a.mPropertyValues != b.mPropertyValues) {
return false;
}
}
return true;
}
// https://drafts.csswg.org/web-animations/#dom-keyframeeffect-setkeyframes
void KeyframeEffect::SetKeyframes(JSContext* aContext,
JS::Handle<JSObject*> aKeyframes,
ErrorResult& aRv) {
nsTArray<Keyframe> keyframes = KeyframeUtils::GetKeyframesFromObject(
aContext, mDocument, aKeyframes, "KeyframeEffect.setKeyframes", aRv);
if (aRv.Failed()) {
return;
}
RefPtr<const ComputedStyle> style = GetTargetComputedStyle(Flush::None);
SetKeyframes(std::move(keyframes), style, nullptr /* AnimationTimeline */);
}
void KeyframeEffect::SetKeyframes(nsTArray<Keyframe>&& aKeyframes,
const ComputedStyle* aStyle,
const AnimationTimeline* aTimeline) {
if (KeyframesEqualIgnoringComputedOffsets(aKeyframes, mKeyframes)) {
return;
}
mKeyframes = std::move(aKeyframes);
KeyframeUtils::DistributeKeyframes(mKeyframes);
if (mAnimation && mAnimation->IsRelevant()) {
MutationObservers::NotifyAnimationChanged(mAnimation);
}
// We need to call UpdateProperties() unless the target element doesn't have
// style (e.g. the target element is not associated with any document).
if (aStyle) {
UpdateProperties(aStyle, aTimeline);
}
}
void KeyframeEffect::ReplaceTransitionStartValue(AnimationValue&& aStartValue) {
if (!aStartValue.mServo) {
return;
}
// A typical transition should have a single property and a single segment.
//
// (And for atypical transitions, that is, those updated by script, we don't
// apply the replacing behavior.)
if (mProperties.Length() != 1 || mProperties[0].mSegments.Length() != 1) {
return;
}
// Likewise, check that the keyframes are of the expected shape.
if (mKeyframes.Length() != 2 || mKeyframes[0].mPropertyValues.Length() != 1) {
return;
}
// Check that the value we are about to substitute in is actually for the
// same property.
if (Servo_AnimationValue_GetPropertyId(aStartValue.mServo) !=
mProperties[0].mProperty) {
return;
}
mKeyframes[0].mPropertyValues[0].mServoDeclarationBlock =
Servo_AnimationValue_Uncompute(aStartValue.mServo).Consume();
mProperties[0].mSegments[0].mFromValue = std::move(aStartValue);
}
static bool IsEffectiveProperty(const EffectSet& aEffects,
nsCSSPropertyID aProperty) {
return !aEffects.PropertiesWithImportantRules().HasProperty(aProperty) ||
!aEffects.PropertiesForAnimationsLevel().HasProperty(aProperty);
}
const AnimationProperty* KeyframeEffect::GetEffectiveAnimationOfProperty(
nsCSSPropertyID aProperty, const EffectSet& aEffects) const {
MOZ_ASSERT(mTarget && &aEffects == EffectSet::Get(mTarget.mElement,
mTarget.mPseudoType));
for (const AnimationProperty& property : mProperties) {
if (aProperty != property.mProperty) {
continue;
}
const AnimationProperty* result = nullptr;
// Only include the property if it is not overridden by !important rules in
// the transitions level.
if (IsEffectiveProperty(aEffects, property.mProperty)) {
result = &property;
}
return result;
}
return nullptr;
}
bool KeyframeEffect::HasEffectiveAnimationOfPropertySet(
const nsCSSPropertyIDSet& aPropertySet, const EffectSet& aEffectSet) const {
for (const AnimationProperty& property : mProperties) {
if (aPropertySet.HasProperty(property.mProperty) &&
IsEffectiveProperty(aEffectSet, property.mProperty)) {
return true;
}
}
return false;
}
nsCSSPropertyIDSet KeyframeEffect::GetPropertiesForCompositor(
EffectSet& aEffects, const nsIFrame* aFrame) const {
MOZ_ASSERT(&aEffects ==
EffectSet::Get(mTarget.mElement, mTarget.mPseudoType));
nsCSSPropertyIDSet properties;
if (!mAnimation || !mAnimation->IsRelevant()) {
return properties;
}
static constexpr nsCSSPropertyIDSet compositorAnimatables =
nsCSSPropertyIDSet::CompositorAnimatables();
static constexpr nsCSSPropertyIDSet transformLikeProperties =
nsCSSPropertyIDSet::TransformLikeProperties();
nsCSSPropertyIDSet transformSet;
AnimationPerformanceWarning::Type dummyWarning;
for (const AnimationProperty& property : mProperties) {
if (!compositorAnimatables.HasProperty(property.mProperty)) {
continue;
}
// Transform-like properties are combined together on the compositor so we
// need to evaluate them as a group. We build up a separate set here then
// evaluate it as a separate step below.
if (transformLikeProperties.HasProperty(property.mProperty)) {
transformSet.AddProperty(property.mProperty);
continue;
}
KeyframeEffect::MatchForCompositor matchResult = IsMatchForCompositor(
nsCSSPropertyIDSet{property.mProperty}, aFrame, aEffects, dummyWarning);
if (matchResult ==
KeyframeEffect::MatchForCompositor::NoAndBlockThisProperty ||
matchResult == KeyframeEffect::MatchForCompositor::No) {
continue;
}
properties.AddProperty(property.mProperty);
}
if (!transformSet.IsEmpty()) {
KeyframeEffect::MatchForCompositor matchResult =
IsMatchForCompositor(transformSet, aFrame, aEffects, dummyWarning);
if (matchResult == KeyframeEffect::MatchForCompositor::Yes ||
matchResult == KeyframeEffect::MatchForCompositor::IfNeeded) {
properties |= transformSet;
}
}
return properties;
}
nsCSSPropertyIDSet KeyframeEffect::GetPropertySet() const {
nsCSSPropertyIDSet result;
for (const AnimationProperty& property : mProperties) {
result.AddProperty(property.mProperty);
}
return result;
}
#ifdef DEBUG
bool SpecifiedKeyframeArraysAreEqual(const nsTArray<Keyframe>& aA,
const nsTArray<Keyframe>& aB) {
if (aA.Length() != aB.Length()) {
return false;
}
for (size_t i = 0; i < aA.Length(); i++) {
const Keyframe& a = aA[i];
const Keyframe& b = aB[i];
if (a.mOffset != b.mOffset || a.mTimingFunction != b.mTimingFunction ||
a.mPropertyValues != b.mPropertyValues) {
return false;
}
}
return true;
}
#endif
static bool HasCurrentColor(
const nsTArray<AnimationPropertySegment>& aSegments) {
for (const AnimationPropertySegment& segment : aSegments) {
if ((!segment.mFromValue.IsNull() && segment.mFromValue.IsCurrentColor()) ||
(!segment.mToValue.IsNull() && segment.mToValue.IsCurrentColor())) {
return true;
}
}
return false;
}
void KeyframeEffect::UpdateProperties(const ComputedStyle* aStyle,
const AnimationTimeline* aTimeline) {
MOZ_ASSERT(aStyle);
nsTArray<AnimationProperty> properties = BuildProperties(aStyle);
bool propertiesChanged = mProperties != properties;
// We need to update base styles even if any properties are not changed at all
// since base styles might have been changed due to parent style changes, etc.
bool baseStylesChanged = false;
EnsureBaseStyles(aStyle, properties, aTimeline,
!propertiesChanged ? &baseStylesChanged : nullptr);
if (!propertiesChanged) {
if (baseStylesChanged) {
RequestRestyle(EffectCompositor::RestyleType::Layer);
}
// Check if we need to update the cumulative change hint because we now have
// style data.
if (mNeedsStyleData && mTarget && mTarget.mElement->HasServoData()) {
CalculateCumulativeChangeHint(aStyle);
}
return;
}
// Preserve the state of the mIsRunningOnCompositor flag.
nsCSSPropertyIDSet runningOnCompositorProperties;
for (const AnimationProperty& property : mProperties) {
if (property.mIsRunningOnCompositor) {
runningOnCompositorProperties.AddProperty(property.mProperty);
}
}
mProperties = std::move(properties);
UpdateEffectSet();
mHasCurrentColor = false;
for (AnimationProperty& property : mProperties) {
property.mIsRunningOnCompositor =
runningOnCompositorProperties.HasProperty(property.mProperty);
if (property.mProperty == eCSSProperty_background_color &&
!mHasCurrentColor) {
if (HasCurrentColor(property.mSegments)) {
mHasCurrentColor = true;
break;
}
}
}
CalculateCumulativeChangeHint(aStyle);
MarkCascadeNeedsUpdate();
if (mAnimation) {
mAnimation->NotifyEffectPropertiesUpdated();
}
RequestRestyle(EffectCompositor::RestyleType::Layer);
}
void KeyframeEffect::EnsureBaseStyles(
const ComputedStyle* aComputedValues,
const nsTArray<AnimationProperty>& aProperties,
const AnimationTimeline* aTimeline, bool* aBaseStylesChanged) {
if (aBaseStylesChanged != nullptr) {
*aBaseStylesChanged = false;
}
if (!mTarget) {
return;
}
BaseValuesHashmap previousBaseStyles;
if (aBaseStylesChanged != nullptr) {
previousBaseStyles = std::move(mBaseValues);
}
mBaseValues.Clear();
nsPresContext* presContext =
nsContentUtils::GetContextForContent(mTarget.mElement);
// If |aProperties| is empty we're not going to dereference |presContext| so
// we don't care if it is nullptr.
//
// We could just return early when |aProperties| is empty and save looking up
// the pres context, but that won't save any effort normally since we don't
// call this function if we have no keyframes to begin with. Furthermore, the
// case where |presContext| is nullptr is so rare (we've only ever seen in
// fuzzing, and even then we've never been able to reproduce it reliably)
// it's not worth the runtime cost of an extra branch.
MOZ_ASSERT(presContext || aProperties.IsEmpty(),
"Typically presContext should not be nullptr but if it is"
" we should have also failed to calculate the computed values"
" passed-in as aProperties");
if (!aTimeline) {
// If we pass a valid timeline, we use it (note: this happens when we create
// a new animation or replace the old one, for CSS Animations and CSS
// Transitions). Otherwise, we check the timeline from |mAnimation|.
aTimeline = mAnimation ? mAnimation->GetTimeline() : nullptr;
}
RefPtr<const ComputedStyle> baseComputedStyle;
for (const AnimationProperty& property : aProperties) {
EnsureBaseStyle(property, presContext, aComputedValues, aTimeline,
baseComputedStyle);
}
if (aBaseStylesChanged != nullptr &&
std::any_of(
mBaseValues.cbegin(), mBaseValues.cend(), [&](const auto& entry) {
return AnimationValue(entry.GetData()) !=
AnimationValue(previousBaseStyles.Get(entry.GetKey()));
})) {
*aBaseStylesChanged = true;
}
}
void KeyframeEffect::EnsureBaseStyle(
const AnimationProperty& aProperty, nsPresContext* aPresContext,
const ComputedStyle* aComputedStyle, const AnimationTimeline* aTimeline,
RefPtr<const ComputedStyle>& aBaseComputedStyle) {
auto needBaseStyleForScrollTimeline =
[this](const AnimationProperty& aProperty,
const AnimationTimeline* aTimeline) {
static constexpr TimeDuration zeroDuration;
const TimingParams& timing = NormalizedTiming();
// For scroll-timeline with a positive delay, it's possible to scroll
// back and forth between delay phase and active phase, so we need to
// keep its base style and maybe use it to override the animations in
// delay on the compositor.
return aTimeline && aTimeline->IsScrollTimeline() &&
nsCSSPropertyIDSet::CompositorAnimatables().HasProperty(
aProperty.mProperty) &&
(timing.Delay() > zeroDuration ||
timing.EndDelay() > zeroDuration);
};
auto hasAdditiveValues = [](const AnimationProperty& aProperty) {
for (const AnimationPropertySegment& segment : aProperty.mSegments) {
if (!segment.HasReplaceableValues()) {
return true;
}
}
return false;
};
// Note: Check base style for compositor (i.e. for scroll-driven animations)
// first because it is much cleaper.
const bool needBaseStyle =
needBaseStyleForScrollTimeline(aProperty, aTimeline) ||
hasAdditiveValues(aProperty);
if (!needBaseStyle) {
return;
}
if (!aBaseComputedStyle) {
MOZ_ASSERT(mTarget, "Should have a valid target");
Element* animatingElement = AnimationUtils::GetElementForRestyle(
mTarget.mElement, mTarget.mPseudoType);
if (!animatingElement) {
return;
}
aBaseComputedStyle = aPresContext->StyleSet()->GetBaseContextForElement(
animatingElement, aComputedStyle);
}
RefPtr<StyleAnimationValue> baseValue =
Servo_ComputedValues_ExtractAnimationValue(aBaseComputedStyle,
aProperty.mProperty)
.Consume();
mBaseValues.InsertOrUpdate(aProperty.mProperty, std::move(baseValue));
}
void KeyframeEffect::WillComposeStyle() {
ComputedTiming computedTiming = GetComputedTiming();
mProgressOnLastCompose = computedTiming.mProgress;
mCurrentIterationOnLastCompose = computedTiming.mCurrentIteration;
}
void KeyframeEffect::ComposeStyleRule(StyleAnimationValueMap& aAnimationValues,
const AnimationProperty& aProperty,
const AnimationPropertySegment& aSegment,
const ComputedTiming& aComputedTiming) {
auto* opaqueTable =
reinterpret_cast<RawServoAnimationValueTable*>(&mBaseValues);
Servo_AnimationCompose(&aAnimationValues, opaqueTable, aProperty.mProperty,
&aSegment, &aProperty.mSegments.LastElement(),
&aComputedTiming, mEffectOptions.mIterationComposite);
}
void KeyframeEffect::ComposeStyle(StyleAnimationValueMap& aComposeResult,
const nsCSSPropertyIDSet& aPropertiesToSkip) {
ComputedTiming computedTiming = GetComputedTiming();
// If the progress is null, we don't have fill data for the current
// time so we shouldn't animate.
if (computedTiming.mProgress.IsNull()) {
return;
}
for (size_t propIdx = 0, propEnd = mProperties.Length(); propIdx != propEnd;
++propIdx) {
const AnimationProperty& prop = mProperties[propIdx];
MOZ_ASSERT(prop.mSegments[0].mFromKey == 0.0, "incorrect first from key");
MOZ_ASSERT(prop.mSegments[prop.mSegments.Length() - 1].mToKey == 1.0,
"incorrect last to key");
if (aPropertiesToSkip.HasProperty(prop.mProperty)) {
continue;
}
MOZ_ASSERT(prop.mSegments.Length() > 0,
"property should not be in animations if it has no segments");
// FIXME: Maybe cache the current segment?
const AnimationPropertySegment *segment = prop.mSegments.Elements(),
*segmentEnd =
segment + prop.mSegments.Length();
while (segment->mToKey <= computedTiming.mProgress.Value()) {
MOZ_ASSERT(segment->mFromKey <= segment->mToKey, "incorrect keys");
if ((segment + 1) == segmentEnd) {
break;
}
++segment;
MOZ_ASSERT(segment->mFromKey == (segment - 1)->mToKey, "incorrect keys");
}
MOZ_ASSERT(segment->mFromKey <= segment->mToKey, "incorrect keys");
MOZ_ASSERT(segment >= prop.mSegments.Elements() &&
size_t(segment - prop.mSegments.Elements()) <
prop.mSegments.Length(),
"out of array bounds");
ComposeStyleRule(aComposeResult, prop, *segment, computedTiming);
}
// If the animation produces a change hint that affects the overflow region,
// we need to record the current time to unthrottle the animation
// periodically when the animation is being throttled because it's scrolled
// out of view.
if (HasPropertiesThatMightAffectOverflow()) {
nsPresContext* presContext =
nsContentUtils::GetContextForContent(mTarget.mElement);
EffectSet* effectSet =
EffectSet::Get(mTarget.mElement, mTarget.mPseudoType);
if (presContext && effectSet) {
TimeStamp now = presContext->RefreshDriver()->MostRecentRefresh();
effectSet->UpdateLastOverflowAnimationSyncTime(now);
}
}
}
bool KeyframeEffect::IsRunningOnCompositor() const {
// We consider animation is running on compositor if there is at least
// one property running on compositor.
// Animation.IsRunningOnCompotitor will return more fine grained
// information in bug 1196114.
for (const AnimationProperty& property : mProperties) {
if (property.mIsRunningOnCompositor) {
return true;
}
}
return false;
}
void KeyframeEffect::SetIsRunningOnCompositor(nsCSSPropertyID aProperty,
bool aIsRunning) {
MOZ_ASSERT(
nsCSSProps::PropHasFlags(aProperty, CSSPropFlags::CanAnimateOnCompositor),
"Property being animated on compositor is a recognized "
"compositor-animatable property");
for (AnimationProperty& property : mProperties) {
if (property.mProperty == aProperty) {
property.mIsRunningOnCompositor = aIsRunning;
// We currently only set a performance warning message when animations
// cannot be run on the compositor, so if this animation is running
// on the compositor we don't need a message.
if (aIsRunning) {
property.mPerformanceWarning.reset();
} else if (mAnimation && mAnimation->IsPartialPrerendered()) {
ResetPartialPrerendered();
}
return;
}
}
}
void KeyframeEffect::SetIsRunningOnCompositor(
const nsCSSPropertyIDSet& aPropertySet, bool aIsRunning) {
for (AnimationProperty& property : mProperties) {
if (aPropertySet.HasProperty(property.mProperty)) {
MOZ_ASSERT(nsCSSProps::PropHasFlags(property.mProperty,
CSSPropFlags::CanAnimateOnCompositor),
"Property being animated on compositor is a recognized "
"compositor-animatable property");
property.mIsRunningOnCompositor = aIsRunning;
// We currently only set a performance warning message when animations
// cannot be run on the compositor, so if this animation is running
// on the compositor we don't need a message.
if (aIsRunning) {
property.mPerformanceWarning.reset();
}
}
}
if (!aIsRunning && mAnimation && mAnimation->IsPartialPrerendered()) {
ResetPartialPrerendered();
}
}
void KeyframeEffect::ResetIsRunningOnCompositor() {
for (AnimationProperty& property : mProperties) {
property.mIsRunningOnCompositor = false;
}
if (mAnimation && mAnimation->IsPartialPrerendered()) {
ResetPartialPrerendered();
}
}
void KeyframeEffect::ResetPartialPrerendered() {
MOZ_ASSERT(mAnimation && mAnimation->IsPartialPrerendered());
nsIFrame* frame = GetPrimaryFrame();
if (!frame) {
return;
}
nsIWidget* widget = frame->GetNearestWidget();
if (!widget) {
return;
}
if (WindowRenderer* windowRenderer = widget->GetWindowRenderer()) {
windowRenderer->RemovePartialPrerenderedAnimation(
mAnimation->IdOnCompositor(), mAnimation);
}
}
static const KeyframeEffectOptions& KeyframeEffectOptionsFromUnion(
const UnrestrictedDoubleOrKeyframeEffectOptions& aOptions) {
MOZ_ASSERT(aOptions.IsKeyframeEffectOptions());
return aOptions.GetAsKeyframeEffectOptions();
}
static const KeyframeEffectOptions& KeyframeEffectOptionsFromUnion(
const UnrestrictedDoubleOrKeyframeAnimationOptions& aOptions) {
MOZ_ASSERT(aOptions.IsKeyframeAnimationOptions());
return aOptions.GetAsKeyframeAnimationOptions();
}
template <class OptionsType>
static KeyframeEffectParams KeyframeEffectParamsFromUnion(
const OptionsType& aOptions, CallerType aCallerType, ErrorResult& aRv) {
KeyframeEffectParams result;
if (aOptions.IsUnrestrictedDouble()) {
return result;
}
const KeyframeEffectOptions& options =
KeyframeEffectOptionsFromUnion(aOptions);
// If dom.animations-api.compositing.enabled is turned off,
// iterationComposite and composite are the default value 'replace' in the
// dictionary.
result.mIterationComposite = options.mIterationComposite;
result.mComposite = options.mComposite;
result.mPseudoType = PseudoStyleType::NotPseudo;
if (DOMStringIsNull(options.mPseudoElement)) {
return result;
}
Maybe<PseudoStyleType> pseudoType =
nsCSSPseudoElements::GetPseudoType(options.mPseudoElement);
if (!pseudoType) {
// Per the spec, we throw SyntaxError for syntactically invalid pseudos.
aRv.ThrowSyntaxError(
nsPrintfCString("'%s' is a syntactically invalid pseudo-element.",
NS_ConvertUTF16toUTF8(options.mPseudoElement).get()));
return result;
}
result.mPseudoType = *pseudoType;
if (!AnimationUtils::IsSupportedPseudoForAnimations(result.mPseudoType)) {
// Per the spec, we throw SyntaxError for unsupported pseudos.
aRv.ThrowSyntaxError(
nsPrintfCString("'%s' is an unsupported pseudo-element.",
NS_ConvertUTF16toUTF8(options.mPseudoElement).get()));
}
return result;
}
template <class OptionsType>
/* static */
already_AddRefed<KeyframeEffect> KeyframeEffect::ConstructKeyframeEffect(
const GlobalObject& aGlobal, Element* aTarget,
JS::Handle<JSObject*> aKeyframes, const OptionsType& aOptions,
ErrorResult& aRv) {
// We should get the document from `aGlobal` instead of the current Realm
// to make this works in Xray case.
//
// In all non-Xray cases, `aGlobal` matches the current Realm, so this
// matches the spec behavior.
//
// In Xray case, the new objects should be created using the document of
// the target global, but the KeyframeEffect constructors are called in the
// caller's compartment to access `aKeyframes` object.
Document* doc = AnimationUtils::GetDocumentFromGlobal(aGlobal.Get());
if (!doc) {
aRv.Throw(NS_ERROR_FAILURE);
return nullptr;
}
KeyframeEffectParams effectOptions =
KeyframeEffectParamsFromUnion(aOptions, aGlobal.CallerType(), aRv);
// An invalid Pseudo-element aborts all further steps.
if (aRv.Failed()) {
return nullptr;
}
TimingParams timingParams = TimingParams::FromOptionsUnion(aOptions, aRv);
if (aRv.Failed()) {
return nullptr;
}
RefPtr<KeyframeEffect> effect = new KeyframeEffect(
doc, OwningAnimationTarget(aTarget, effectOptions.mPseudoType),
std::move(timingParams), effectOptions);
effect->SetKeyframes(aGlobal.Context(), aKeyframes, aRv);
if (aRv.Failed()) {
return nullptr;
}
return effect.forget();
}
nsTArray<AnimationProperty> KeyframeEffect::BuildProperties(
const ComputedStyle* aStyle) {
MOZ_ASSERT(aStyle);
nsTArray<AnimationProperty> result;
// If mTarget is false (i.e. mTarget.mElement is null), return an empty
// property array.
if (!mTarget) {
return result;
}
// When GetComputedKeyframeValues or GetAnimationPropertiesFromKeyframes
// calculate computed values from |mKeyframes|, they could possibly
// trigger a subsequent restyle in which we rebuild animations. If that
// happens we could find that |mKeyframes| is overwritten while it is
// being iterated over. Normally that shouldn't happen but just in case we
// make a copy of |mKeyframes| first and iterate over that instead.
auto keyframesCopy(mKeyframes.Clone());
result = KeyframeUtils::GetAnimationPropertiesFromKeyframes(
keyframesCopy, mTarget.mElement, mTarget.mPseudoType, aStyle,
mEffectOptions.mComposite);
#ifdef DEBUG
MOZ_ASSERT(SpecifiedKeyframeArraysAreEqual(mKeyframes, keyframesCopy),
"Apart from the computed offset members, the keyframes array"
" should not be modified");
#endif
mKeyframes = std::move(keyframesCopy);
return result;
}
template <typename FrameEnumFunc>
static void EnumerateContinuationsOrIBSplitSiblings(nsIFrame* aFrame,
FrameEnumFunc&& aFunc) {
while (aFrame) {
aFunc(aFrame);
aFrame = nsLayoutUtils::GetNextContinuationOrIBSplitSibling(aFrame);
}
}
void KeyframeEffect::UpdateTarget(Element* aElement,
PseudoStyleType aPseudoType) {
OwningAnimationTarget newTarget(aElement, aPseudoType);
if (mTarget == newTarget) {
// Assign the same target, skip it.
return;
}
if (mTarget) {
// Call ResetIsRunningOnCompositor() prior to UnregisterTarget() since
// ResetIsRunningOnCompositor() might try to get the EffectSet associated
// with this keyframe effect to remove partial pre-render animation from
// the layer manager.
ResetIsRunningOnCompositor();
UnregisterTarget();
RequestRestyle(EffectCompositor::RestyleType::Layer);
nsAutoAnimationMutationBatch mb(mTarget.mElement->OwnerDoc());
if (mAnimation) {
MutationObservers::NotifyAnimationRemoved(mAnimation);
}
}
mTarget = newTarget;
if (mTarget) {
UpdateTargetRegistration();
RefPtr<const ComputedStyle> computedStyle =
GetTargetComputedStyle(Flush::None);
if (computedStyle) {
UpdateProperties(computedStyle);
}
RequestRestyle(EffectCompositor::RestyleType::Layer);
nsAutoAnimationMutationBatch mb(mTarget.mElement->OwnerDoc());
if (mAnimation) {
MutationObservers::NotifyAnimationAdded(mAnimation);
mAnimation->ReschedulePendingTasks();
}
}
if (mAnimation) {
mAnimation->NotifyEffectTargetUpdated();
}
}
void KeyframeEffect::UpdateTargetRegistration() {
if (!mTarget) {
return;
}
bool isRelevant = mAnimation && mAnimation->IsRelevant();
// Animation::IsRelevant() returns a cached value. It only updates when
// something calls Animation::UpdateRelevance. Whenever our timing changes,
// we should be notifying our Animation before calling this, so
// Animation::IsRelevant() should be up-to-date by the time we get here.
MOZ_ASSERT(isRelevant ==
((IsCurrent() || IsInEffect()) && mAnimation &&
mAnimation->ReplaceState() != AnimationReplaceState::Removed),
"Out of date Animation::IsRelevant value");
if (isRelevant && !mInEffectSet) {
EffectSet* effectSet =
EffectSet::GetOrCreate(mTarget.mElement, mTarget.mPseudoType);
effectSet->AddEffect(*this);
mInEffectSet = true;
UpdateEffectSet(effectSet);
nsIFrame* frame = GetPrimaryFrame();
EnumerateContinuationsOrIBSplitSiblings(
frame, [](nsIFrame* aFrame) { aFrame->MarkNeedsDisplayItemRebuild(); });
} else if (!isRelevant && mInEffectSet) {
UnregisterTarget();
}
}
void KeyframeEffect::UnregisterTarget() {
if (!mInEffectSet) {
return;
}
EffectSet* effectSet = EffectSet::Get(mTarget.mElement, mTarget.mPseudoType);
MOZ_ASSERT(effectSet,
"If mInEffectSet is true, there must be an EffectSet"
" on the target element");
mInEffectSet = false;
if (effectSet) {
effectSet->RemoveEffect(*this);
if (effectSet->IsEmpty()) {
EffectSet::DestroyEffectSet(mTarget.mElement, mTarget.mPseudoType);
}
}
nsIFrame* frame = GetPrimaryFrame();
EnumerateContinuationsOrIBSplitSiblings(
frame, [](nsIFrame* aFrame) { aFrame->MarkNeedsDisplayItemRebuild(); });
}
void KeyframeEffect::RequestRestyle(
EffectCompositor::RestyleType aRestyleType) {
if (!mTarget) {
return;
}
nsPresContext* presContext =
nsContentUtils::GetContextForContent(mTarget.mElement);
if (presContext && mAnimation) {
presContext->EffectCompositor()->RequestRestyle(
mTarget.mElement, mTarget.mPseudoType, aRestyleType,
mAnimation->CascadeLevel());
}
}
already_AddRefed<const ComputedStyle> KeyframeEffect::GetTargetComputedStyle(
Flush aFlushType) const {
if (!GetRenderedDocument()) {
return nullptr;
}
MOZ_ASSERT(mTarget,
"Should only have a document when we have a target element");
OwningAnimationTarget kungfuDeathGrip(mTarget.mElement, mTarget.mPseudoType);
return aFlushType == Flush::Style
? nsComputedDOMStyle::GetComputedStyle(mTarget.mElement,
mTarget.mPseudoType)
: nsComputedDOMStyle::GetComputedStyleNoFlush(mTarget.mElement,
mTarget.mPseudoType);
}
#ifdef DEBUG
void DumpAnimationProperties(
const StylePerDocumentStyleData* aRawData,
nsTArray<AnimationProperty>& aAnimationProperties) {
for (auto& p : aAnimationProperties) {
printf("%s\n", nsCString(nsCSSProps::GetStringValue(p.mProperty)).get());
for (auto& s : p.mSegments) {
nsAutoCString fromValue, toValue;
s.mFromValue.SerializeSpecifiedValue(p.mProperty, aRawData, fromValue);
s.mToValue.SerializeSpecifiedValue(p.mProperty, aRawData, toValue);
printf(" %f..%f: %s..%s\n", s.mFromKey, s.mToKey, fromValue.get(),
toValue.get());
}
}
}
#endif
/* static */
already_AddRefed<KeyframeEffect> KeyframeEffect::Constructor(
const GlobalObject& aGlobal, Element* aTarget,
JS::Handle<JSObject*> aKeyframes,
const UnrestrictedDoubleOrKeyframeEffectOptions& aOptions,
ErrorResult& aRv) {
return ConstructKeyframeEffect(aGlobal, aTarget, aKeyframes, aOptions, aRv);
}
/* static */
already_AddRefed<KeyframeEffect> KeyframeEffect::Constructor(
const GlobalObject& aGlobal, Element* aTarget,
JS::Handle<JSObject*> aKeyframes,
const UnrestrictedDoubleOrKeyframeAnimationOptions& aOptions,
ErrorResult& aRv) {
return ConstructKeyframeEffect(aGlobal, aTarget, aKeyframes, aOptions, aRv);
}
/* static */
already_AddRefed<KeyframeEffect> KeyframeEffect::Constructor(
const GlobalObject& aGlobal, KeyframeEffect& aSource, ErrorResult& aRv) {
Document* doc = AnimationUtils::GetCurrentRealmDocument(aGlobal.Context());
if (!doc) {
aRv.Throw(NS_ERROR_FAILURE);
return nullptr;
}
// Create a new KeyframeEffect object with aSource's target,
// iteration composite operation, composite operation, and spacing mode.
// The constructor creates a new AnimationEffect object by
// aSource's TimingParams.
// Note: we don't need to re-throw exceptions since the value specified on
// aSource's timing object can be assumed valid.
RefPtr<KeyframeEffect> effect =
new KeyframeEffect(doc, OwningAnimationTarget{aSource.mTarget}, aSource);
// Copy cumulative change hint. mCumulativeChangeHint should be the same as
// the source one because both of targets are the same.
effect->mCumulativeChangeHint = aSource.mCumulativeChangeHint;
return effect.forget();
}
void KeyframeEffect::SetPseudoElement(const nsAString& aPseudoElement,
ErrorResult& aRv) {
if (DOMStringIsNull(aPseudoElement)) {
UpdateTarget(mTarget.mElement, PseudoStyleType::NotPseudo);
return;
}
// Note: GetPseudoType() returns Some(NotPseudo) for the null string,
// so we handle null case before this.
Maybe<PseudoStyleType> pseudoType =
nsCSSPseudoElements::GetPseudoType(aPseudoElement);
if (!pseudoType || *pseudoType == PseudoStyleType::NotPseudo) {
// Per the spec, we throw SyntaxError for syntactically invalid pseudos.
aRv.ThrowSyntaxError(
nsPrintfCString("'%s' is a syntactically invalid pseudo-element.",
NS_ConvertUTF16toUTF8(aPseudoElement).get()));
return;
}
if (!AnimationUtils::IsSupportedPseudoForAnimations(*pseudoType)) {
// Per the spec, we throw SyntaxError for unsupported pseudos.
aRv.ThrowSyntaxError(
nsPrintfCString("'%s' is an unsupported pseudo-element.",
NS_ConvertUTF16toUTF8(aPseudoElement).get()));
return;
}
UpdateTarget(mTarget.mElement, *pseudoType);
}
static void CreatePropertyValue(
nsCSSPropertyID aProperty, float aOffset,
const Maybe<StyleComputedTimingFunction>& aTimingFunction,
const AnimationValue& aValue, dom::CompositeOperation aComposite,
const StylePerDocumentStyleData* aRawData,
AnimationPropertyValueDetails& aResult) {
aResult.mOffset = aOffset;
if (!aValue.IsNull()) {
nsAutoCString stringValue;
aValue.SerializeSpecifiedValue(aProperty, aRawData, stringValue);
aResult.mValue.Construct(stringValue);
}
if (aTimingFunction) {
aResult.mEasing.Construct();
aTimingFunction->AppendToString(aResult.mEasing.Value());
} else {
aResult.mEasing.Construct("linear"_ns);
}
aResult.mComposite = aComposite;
}
void KeyframeEffect::GetProperties(
nsTArray<AnimationPropertyDetails>& aProperties, ErrorResult& aRv) const {
const StylePerDocumentStyleData* rawData =
mDocument->StyleSetForPresShellOrMediaQueryEvaluation()->RawData();
for (const AnimationProperty& property : mProperties) {
AnimationPropertyDetails propertyDetails;
propertyDetails.mProperty =
NS_ConvertASCIItoUTF16(nsCSSProps::GetStringValue(property.mProperty));
propertyDetails.mRunningOnCompositor = property.mIsRunningOnCompositor;
nsAutoString localizedString;
if (property.mPerformanceWarning &&
property.mPerformanceWarning->ToLocalizedString(localizedString)) {
propertyDetails.mWarning.Construct(localizedString);
}
if (!propertyDetails.mValues.SetCapacity(property.mSegments.Length(),
mozilla::fallible)) {
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
return;
}
for (size_t segmentIdx = 0, segmentLen = property.mSegments.Length();
segmentIdx < segmentLen; segmentIdx++) {
const AnimationPropertySegment& segment = property.mSegments[segmentIdx];
binding_detail::FastAnimationPropertyValueDetails fromValue;
CreatePropertyValue(property.mProperty, segment.mFromKey,
segment.mTimingFunction, segment.mFromValue,
segment.mFromComposite, rawData, fromValue);
// We don't apply timing functions for zero-length segments, so
// don't return one here.
if (segment.mFromKey == segment.mToKey) {
fromValue.mEasing.Reset();
}
// Even though we called SetCapacity before, this could fail, since we
// might add multiple elements to propertyDetails.mValues for an element
// of property.mSegments in the cases mentioned below.
if (!propertyDetails.mValues.AppendElement(fromValue,
mozilla::fallible)) {
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
return;
}
// Normally we can ignore the to-value for this segment since it is
// identical to the from-value from the next segment. However, we need
// to add it if either:
// a) this is the last segment, or
// b) the next segment's from-value differs.
if (segmentIdx == segmentLen - 1 ||
property.mSegments[segmentIdx + 1].mFromValue != segment.mToValue) {
binding_detail::FastAnimationPropertyValueDetails toValue;
CreatePropertyValue(property.mProperty, segment.mToKey, Nothing(),
segment.mToValue, segment.mToComposite, rawData,
toValue);
// It doesn't really make sense to have a timing function on the
// last property value or before a sudden jump so we just drop the
// easing property altogether.
toValue.mEasing.Reset();
if (!propertyDetails.mValues.AppendElement(toValue,
mozilla::fallible)) {
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
return;
}
}
}
aProperties.AppendElement(propertyDetails);
}
}
void KeyframeEffect::GetKeyframes(JSContext* aCx, nsTArray<JSObject*>& aResult,
ErrorResult& aRv) const {
MOZ_ASSERT(aResult.IsEmpty());
MOZ_ASSERT(!aRv.Failed());
if (!aResult.SetCapacity(mKeyframes.Length(), mozilla::fallible)) {
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
return;
}
bool isCSSAnimation = mAnimation && mAnimation->AsCSSAnimation();
// For Servo, when we have CSS Animation @keyframes with variables, we convert
// shorthands to longhands if needed, and store a reference to the unparsed
// value. When it comes time to serialize, however, what do you serialize for
// a longhand that comes from a variable reference in a shorthand? Servo says,
// "an empty string" which is not particularly helpful.
//
// We should just store shorthands as-is (bug 1391537) and then return the
// variable references, but for now, since we don't do that, and in order to
// be consistent with Gecko, we just expand the variables (assuming we have
// enough context to do so). For that we need to grab the ComputedStyle so we
// know what custom property values to provide.
RefPtr<const ComputedStyle> computedStyle;
if (isCSSAnimation) {
// The following will flush style but that's ok since if you update
// a variable's computed value, you expect to see that updated value in the
// result of getKeyframes().
//
// If we don't have a target, the following will return null. In that case
// we might end up returning variables as-is or empty string. That should be
// acceptable however, since such a case is rare and this is only
// short-term (and unshipped) behavior until bug 1391537 is fixed.
computedStyle = GetTargetComputedStyle(Flush::Style);
}
const StylePerDocumentStyleData* rawData =
mDocument->StyleSetForPresShellOrMediaQueryEvaluation()->RawData();
for (const Keyframe& keyframe : mKeyframes) {
// Set up a dictionary object for the explicit members
BaseComputedKeyframe keyframeDict;
if (keyframe.mOffset) {
keyframeDict.mOffset.SetValue(keyframe.mOffset.value());
}
MOZ_ASSERT(keyframe.mComputedOffset != Keyframe::kComputedOffsetNotSet,
"Invalid computed offset");
keyframeDict.mComputedOffset.Construct(keyframe.mComputedOffset);
if (keyframe.mTimingFunction) {
keyframeDict.mEasing.Truncate();
keyframe.mTimingFunction.ref().AppendToString(keyframeDict.mEasing);
} // else if null, leave easing as its default "linear".
// With the pref off (i.e. dom.animations-api.compositing.enabled:false),
// the dictionary-to-JS conversion will skip this member entirely.
keyframeDict.mComposite = keyframe.mComposite;
JS::Rooted<JS::Value> keyframeJSValue(aCx);
if (!ToJSValue(aCx, keyframeDict, &keyframeJSValue)) {
aRv.Throw(NS_ERROR_FAILURE);
return;
}
RefPtr<StyleLockedDeclarationBlock> customProperties;
// A workaround for CSS Animations in servo backend, custom properties in
// keyframe are stored in a servo's declaration block. Find the declaration
// block to resolve CSS variables in the keyframe.
// This workaround will be solved by bug 1391537.
if (isCSSAnimation) {
for (const PropertyValuePair& propertyValue : keyframe.mPropertyValues) {
if (propertyValue.mProperty ==
nsCSSPropertyID::eCSSPropertyExtra_variable) {
customProperties = propertyValue.mServoDeclarationBlock;
break;
}
}
}
JS::Rooted<JSObject*> keyframeObject(aCx, &keyframeJSValue.toObject());
for (const PropertyValuePair& propertyValue : keyframe.mPropertyValues) {
nsAutoCString stringValue;
// Don't serialize the custom properties for this keyframe.
if (propertyValue.mProperty ==
nsCSSPropertyID::eCSSPropertyExtra_variable) {
continue;
}
if (propertyValue.mServoDeclarationBlock) {
Servo_DeclarationBlock_SerializeOneValue(
propertyValue.mServoDeclarationBlock, propertyValue.mProperty,
&stringValue, computedStyle, customProperties, rawData);
} else {
if (auto* value = mBaseValues.GetWeak(propertyValue.mProperty)) {
Servo_AnimationValue_Serialize(value, propertyValue.mProperty,
rawData, &stringValue);
}
}
// Basically, we need to do the mapping:
// * eCSSProperty_offset => "cssOffset"
// * eCSSProperty_float => "cssFloat"
// This means if property refers to the CSS "offset"/"float" property,
// return the string "cssOffset"/"cssFloat". (So avoid overlapping
// "offset" property in BaseKeyframe.)
// https://drafts.csswg.org/web-animations/#property-name-conversion
const char* name = nullptr;
switch (propertyValue.mProperty) {
case nsCSSPropertyID::eCSSProperty_offset:
name = "cssOffset";
break;
case nsCSSPropertyID::eCSSProperty_float:
// FIXME: Bug 1582314: Should handle cssFloat manually if we remove it
// from nsCSSProps::PropertyIDLName().
default:
name = nsCSSProps::PropertyIDLName(propertyValue.mProperty);
}
JS::Rooted<JS::Value> value(aCx);
if (!NonVoidUTF8StringToJsval(aCx, stringValue, &value) ||
!JS_DefineProperty(aCx, keyframeObject, name, value,
JSPROP_ENUMERATE)) {
aRv.Throw(NS_ERROR_FAILURE);
return;
}
}
aResult.AppendElement(keyframeObject);
}
}
/* static */ const TimeDuration
KeyframeEffect::OverflowRegionRefreshInterval() {
// The amount of time we can wait between updating throttled animations
// on the main thread that influence the overflow region.
static const TimeDuration kOverflowRegionRefreshInterval =
TimeDuration::FromMilliseconds(200);
return kOverflowRegionRefreshInterval;
}
static bool IsDefinitivelyInvisibleDueToOpacity(const nsIFrame& aFrame) {
if (!aFrame.Style()->IsInOpacityZeroSubtree()) {
return false;
}
// Find the root of the opacity: 0 subtree.
const nsIFrame* root = &aFrame;
while (true) {
auto* parent = root->GetInFlowParent();
if (!parent || !parent->Style()->IsInOpacityZeroSubtree()) {
break;
}
root = parent;
}
MOZ_ASSERT(root && root->Style()->IsInOpacityZeroSubtree());
// If aFrame is the root of the opacity: zero subtree, we can't prove we can
// optimize it away, because it may have an opacity animation itself.
if (root == &aFrame) {
return false;
}
// Even if we're in an opacity: zero subtree, if the root of the subtree may
// have an opacity animation, we can't optimize us away, as we may become
// visible ourselves.
return !root->HasAnimationOfOpacity();
}
static bool CanOptimizeAwayDueToOpacity(const KeyframeEffect& aEffect,
const nsIFrame& aFrame) {
if (!aFrame.Style()->IsInOpacityZeroSubtree()) {
return false;
}
if (IsDefinitivelyInvisibleDueToOpacity(aFrame)) {
return true;
}
return !aEffect.HasOpacityChange() && !aFrame.HasAnimationOfOpacity();
}
bool KeyframeEffect::CanThrottleIfNotVisible(nsIFrame& aFrame) const {
// Unless we are newly in-effect, we can throttle the animation if the
// animation is paint only and the target frame is out of view or the document
// is in background tabs.
if (!mInEffectOnLastAnimationTimingUpdate || !CanIgnoreIfNotVisible()) {
return false;
}
PresShell* presShell = GetPresShell();
if (presShell && !presShell->IsActive()) {
return true;
}
const bool isVisibilityHidden =
!aFrame.IsVisibleOrMayHaveVisibleDescendants();
const bool canOptimizeAwayVisibility =
isVisibilityHidden && !HasVisibilityChange();
const bool invisible = canOptimizeAwayVisibility ||
CanOptimizeAwayDueToOpacity(*this, aFrame) ||
aFrame.IsScrolledOutOfView();
if (!invisible) {
return false;
}
// If there are no overflow change hints, we don't need to worry about
// unthrottling the animation periodically to update scrollbar positions for
// the overflow region.
if (!HasPropertiesThatMightAffectOverflow()) {
return true;
}
// Don't throttle finite animations since the animation might suddenly
// come into view and if it was throttled it will be out-of-sync.
if (HasFiniteActiveDuration()) {
return false;
}
return isVisibilityHidden ? CanThrottleOverflowChangesInScrollable(aFrame)
: CanThrottleOverflowChanges(aFrame);
}
bool KeyframeEffect::CanThrottle() const {
// Unthrottle if we are not in effect or current. This will be the case when
// our owning animation has finished, is idle, or when we are in the delay
// phase (but without a backwards fill). In each case the computed progress
// value produced on each tick will be the same so we will skip requesting
// unnecessary restyles in NotifyAnimationTimingUpdated. Any calls we *do* get
// here will be because of a change in state (e.g. we are newly finished or
// newly no longer in effect) in which case we shouldn't throttle the sample.
if (!IsInEffect() || !IsCurrent()) {
return false;
}
nsIFrame* const frame = GetStyleFrame();
if (!frame) {
// There are two possible cases here.
// a) No target element
// b) The target element has no frame, e.g. because it is in a display:none
// subtree.
// In either case we can throttle the animation because there is no
// need to update on the main thread.
return true;
}
// Do not throttle any animations during print preview.
if (frame->PresContext()->IsPrintingOrPrintPreview()) {
return false;
}
if (CanThrottleIfNotVisible(*frame)) {
return true;
}
EffectSet* effectSet = nullptr;
for (const AnimationProperty& property : mProperties) {
if (!property.mIsRunningOnCompositor) {
return false;
}
MOZ_ASSERT(nsCSSPropertyIDSet::CompositorAnimatables().HasProperty(
property.mProperty),
"The property should be able to run on the compositor");
if (!effectSet) {
effectSet = EffectSet::Get(mTarget.mElement, mTarget.mPseudoType);
MOZ_ASSERT(effectSet,
"CanThrottle should be called on an effect "
"associated with a target element");
}
MOZ_ASSERT(HasEffectiveAnimationOfProperty(property.mProperty, *effectSet),
"There should be an effective animation of the property while "
"it is marked as being run on the compositor");
DisplayItemType displayItemType =
LayerAnimationInfo::GetDisplayItemTypeForProperty(property.mProperty);
// Note that AnimationInfo::GetGenarationFromFrame() is supposed to work
// with the primary frame instead of the style frame.
Maybe<uint64_t> generation = layers::AnimationInfo::GetGenerationFromFrame(
GetPrimaryFrame(), displayItemType);
// Unthrottle if the animation needs to be brought up to date
if (!generation || effectSet->GetAnimationGeneration() != *generation) {
return false;
}
// If this is a transform animation that affects the overflow region,
// we should unthrottle the animation periodically.
if (HasPropertiesThatMightAffectOverflow() &&
!CanThrottleOverflowChangesInScrollable(*frame)) {
return false;
}
}
return true;
}
bool KeyframeEffect::CanThrottleOverflowChanges(const nsIFrame& aFrame) const {
TimeStamp now = aFrame.PresContext()->RefreshDriver()->MostRecentRefresh();
EffectSet* effectSet = EffectSet::Get(mTarget.mElement, mTarget.mPseudoType);
MOZ_ASSERT(effectSet,
"CanOverflowTransformChanges is expected to be called"
" on an effect in an effect set");
MOZ_ASSERT(mAnimation,
"CanOverflowTransformChanges is expected to be called"
" on an effect with a parent animation");
TimeStamp lastSyncTime = effectSet->LastOverflowAnimationSyncTime();
// If this animation can cause overflow, we can throttle some of the ticks.
return (!lastSyncTime.IsNull() &&
(now - lastSyncTime) < OverflowRegionRefreshInterval());
}
bool KeyframeEffect::CanThrottleOverflowChangesInScrollable(
nsIFrame& aFrame) const {
// If the target element is not associated with any documents, we don't care
// it.
Document* doc = GetRenderedDocument();
if (!doc) {
return true;
}
// If we know that the animation cannot cause overflow,
// we can just disable flushes for this animation.
// If we have no intersection observers, we don't care about overflow.
if (!doc->HasIntersectionObservers()) {
return true;
}
if (CanThrottleOverflowChanges(aFrame)) {
return true;
}
// If the nearest scrollable ancestor has overflow:hidden,
// we don't care about overflow.
nsIScrollableFrame* scrollable =
nsLayoutUtils::GetNearestScrollableFrame(&aFrame);
if (!scrollable) {
return true;
}
ScrollStyles ss = scrollable->GetScrollStyles();
if (ss.mVertical == StyleOverflow::Hidden &&
ss.mHorizontal == StyleOverflow::Hidden &&
scrollable->GetLogicalScrollPosition() == nsPoint(0, 0)) {
return true;
}
return false;
}
nsIFrame* KeyframeEffect::GetStyleFrame() const {
nsIFrame* frame = GetPrimaryFrame();
if (!frame) {
return nullptr;
}
return nsLayoutUtils::GetStyleFrame(frame);
}
nsIFrame* KeyframeEffect::GetPrimaryFrame() const {
nsIFrame* frame = nullptr;
if (!mTarget) {
return frame;
}
if (mTarget.mPseudoType == PseudoStyleType::before) {
frame = nsLayoutUtils::GetBeforeFrame(mTarget.mElement);
} else if (mTarget.mPseudoType == PseudoStyleType::after) {
frame = nsLayoutUtils::GetAfterFrame(mTarget.mElement);
} else if (mTarget.mPseudoType == PseudoStyleType::marker) {
frame = nsLayoutUtils::GetMarkerFrame(mTarget.mElement);
} else {
frame = mTarget.mElement->GetPrimaryFrame();
MOZ_ASSERT(mTarget.mPseudoType == PseudoStyleType::NotPseudo,
"unknown mTarget.mPseudoType");
}
return frame;
}
Document* KeyframeEffect::GetRenderedDocument() const {
if (!mTarget) {
return nullptr;
}
return mTarget.mElement->GetComposedDoc();
}
PresShell* KeyframeEffect::GetPresShell() const {
Document* doc = GetRenderedDocument();
if (!doc) {
return nullptr;
}
return doc->GetPresShell();
}
/* static */
bool KeyframeEffect::IsGeometricProperty(const nsCSSPropertyID aProperty) {
MOZ_ASSERT(!nsCSSProps::IsShorthand(aProperty),
"Property should be a longhand property");
switch (aProperty) {
case eCSSProperty_bottom:
case eCSSProperty_height:
case eCSSProperty_left:
case eCSSProperty_margin_bottom:
case eCSSProperty_margin_left:
case eCSSProperty_margin_right:
case eCSSProperty_margin_top:
case eCSSProperty_padding_bottom:
case eCSSProperty_padding_left:
case eCSSProperty_padding_right:
case eCSSProperty_padding_top:
case eCSSProperty_right:
case eCSSProperty_top:
case eCSSProperty_width:
return true;
default:
return false;
}
}
/* static */
bool KeyframeEffect::CanAnimateTransformOnCompositor(
const nsIFrame* aFrame,
AnimationPerformanceWarning::Type& aPerformanceWarning /* out */) {
// In some cases, such as when we are simply collecting all the compositor
// animations regardless of the frame on which they run in order to calculate
// change hints, |aFrame| will be the style frame. However, even in that case
// we should look at the primary frame since that is where the transform will
// be applied.
const nsIFrame* primaryFrame =
nsLayoutUtils::GetPrimaryFrameFromStyleFrame(aFrame);
// Note that testing BackfaceIsHidden() is not a sufficient test for
// what we need for animating backface-visibility correctly if we
// remove the above test for Extend3DContext(); that would require
// looking at backface-visibility on descendants as well. See bug 1186204.
if (primaryFrame->BackfaceIsHidden()) {
aPerformanceWarning =
AnimationPerformanceWarning::Type::TransformBackfaceVisibilityHidden;
return false;
}
// Async 'transform' animations of aFrames with SVG transforms is not
// supported. See bug 779599.
if (primaryFrame->IsSVGTransformed()) {
aPerformanceWarning = AnimationPerformanceWarning::Type::TransformSVG;
return false;
}
return true;
}
bool KeyframeEffect::ShouldBlockAsyncTransformAnimations(
const nsIFrame* aFrame, const nsCSSPropertyIDSet& aPropertySet,
AnimationPerformanceWarning::Type& aPerformanceWarning /* out */) const {
EffectSet* effectSet = EffectSet::Get(mTarget.mElement, mTarget.mPseudoType);
// The various transform properties ('transform', 'scale' etc.) get combined
// on the compositor.
//
// As a result, if we have an animation of 'scale' and 'translate', but the
// 'translate' property is covered by an !important rule, we will not be
// able to combine the result on the compositor since we won't have the
// !important rule to incorporate. In that case we should run all the
// transform-related animations on the main thread (where we have the
// !important rule).
nsCSSPropertyIDSet blockedProperties =
effectSet->PropertiesWithImportantRules().Intersect(
effectSet->PropertiesForAnimationsLevel());
if (blockedProperties.Intersects(aPropertySet)) {
aPerformanceWarning =
AnimationPerformanceWarning::Type::TransformIsBlockedByImportantRules;
return true;
}
MOZ_ASSERT(mAnimation);
// Note: If the geometric animations are using scroll-timeline, we don't need
// to synchronize transform animations with them.
const bool enableMainthreadSynchronizationWithGeometricAnimations =
StaticPrefs::
dom_animations_mainthread_synchronization_with_geometric_animations() &&
!mAnimation->UsingScrollTimeline();
for (const AnimationProperty& property : mProperties) {
// If there is a property for animations level that is overridden by
// !important rules, it should not block other animations from running
// on the compositor.
// NOTE: We don't currently check for !important rules for properties that
// don't run on the compositor. As result such properties (e.g. margin-left)
// can still block async animations even if they are overridden by
// !important rules.
if (effectSet &&
effectSet->PropertiesWithImportantRules().HasProperty(
property.mProperty) &&
effectSet->PropertiesForAnimationsLevel().HasProperty(
property.mProperty)) {
continue;
}
// Check for geometric properties
if (enableMainthreadSynchronizationWithGeometricAnimations &&
IsGeometricProperty(property.mProperty)) {
aPerformanceWarning =
AnimationPerformanceWarning::Type::TransformWithGeometricProperties;
return true;
}
// Check for unsupported transform animations
if (LayerAnimationInfo::GetCSSPropertiesFor(DisplayItemType::TYPE_TRANSFORM)
.HasProperty(property.mProperty)) {
if (!CanAnimateTransformOnCompositor(aFrame, aPerformanceWarning)) {
return true;
}
}
}
return false;
}
bool KeyframeEffect::HasGeometricProperties() const {
for (const AnimationProperty& property : mProperties) {
if (IsGeometricProperty(property.mProperty)) {
return true;
}
}
return false;
}
void KeyframeEffect::SetPerformanceWarning(
const nsCSSPropertyIDSet& aPropertySet,
const AnimationPerformanceWarning& aWarning) {
nsCSSPropertyIDSet curr = aPropertySet;
for (AnimationProperty& property : mProperties) {
if (!curr.HasProperty(property.mProperty)) {
continue;
}
property.SetPerformanceWarning(aWarning, mTarget.mElement);
curr.RemoveProperty(property.mProperty);
if (curr.IsEmpty()) {
return;
}
}
}
already_AddRefed<const ComputedStyle>
KeyframeEffect::CreateComputedStyleForAnimationValue(
nsCSSPropertyID aProperty, const AnimationValue& aValue,
nsPresContext* aPresContext, const ComputedStyle* aBaseComputedStyle) {
MOZ_ASSERT(aBaseComputedStyle,
"CreateComputedStyleForAnimationValue needs to be called "
"with a valid ComputedStyle");
Element* elementForResolve = AnimationUtils::GetElementForRestyle(
mTarget.mElement, mTarget.mPseudoType);
// The element may be null if, for example, we target a pseudo-element that no
// longer exists.
if (!elementForResolve) {
return nullptr;
}
ServoStyleSet* styleSet = aPresContext->StyleSet();
return styleSet->ResolveServoStyleByAddingAnimation(
elementForResolve, aBaseComputedStyle, aValue.mServo);
}
void KeyframeEffect::CalculateCumulativeChangeHint(
const ComputedStyle* aComputedStyle) {
mCumulativeChangeHint = nsChangeHint(0);
mNeedsStyleData = false;
nsPresContext* presContext =
mTarget ? nsContentUtils::GetContextForContent(mTarget.mElement)
: nullptr;
if (!presContext) {
// Change hints make no sense if we're not rendered.
//
// Actually, we cannot even post them anywhere.
mNeedsStyleData = true;
return;
}
for (const AnimationProperty& property : mProperties) {
// For opacity property we don't produce any change hints that are not
// included in nsChangeHint_Hints_CanIgnoreIfNotVisible so we can throttle
// opacity animations regardless of the change they produce. This
// optimization is particularly important since it allows us to throttle
// opacity animations with missing 0%/100% keyframes.
if (property.mProperty == eCSSProperty_opacity) {
continue;
}
for (const AnimationPropertySegment& segment : property.mSegments) {
// In case composite operation is not 'replace' or value is null,
// we can't throttle animations which will not cause any layout changes
// on invisible elements because we can't calculate the change hint for
// such properties until we compose it.
if (!segment.HasReplaceableValues()) {
if (!nsCSSPropertyIDSet::TransformLikeProperties().HasProperty(
property.mProperty)) {
mCumulativeChangeHint = ~nsChangeHint_Hints_CanIgnoreIfNotVisible;
return;
}
// We try a little harder to optimize transform animations simply
// because they are so common (the second-most commonly animated
// property at the time of writing). So if we encounter a transform
// segment that needs composing with the underlying value, we just add
// all the change hints a transform animation is known to be able to
// generate.
mCumulativeChangeHint |=
nsChangeHint_ComprehensiveAddOrRemoveTransform |
nsChangeHint_UpdatePostTransformOverflow |
nsChangeHint_UpdateTransformLayer;
continue;
}
RefPtr<const ComputedStyle> fromContext =
CreateComputedStyleForAnimationValue(property.mProperty,
segment.mFromValue, presContext,
aComputedStyle);
if (!fromContext) {
mCumulativeChangeHint = ~nsChangeHint_Hints_CanIgnoreIfNotVisible;
mNeedsStyleData = true;
return;
}
RefPtr<const ComputedStyle> toContext =
CreateComputedStyleForAnimationValue(property.mProperty,
segment.mToValue, presContext,
aComputedStyle);
if (!toContext) {
mCumulativeChangeHint = ~nsChangeHint_Hints_CanIgnoreIfNotVisible;
mNeedsStyleData = true;
return;
}
uint32_t equalStructs = 0;
nsChangeHint changeHint =
fromContext->CalcStyleDifference(*toContext, &equalStructs);
mCumulativeChangeHint |= changeHint;
}
}
}
void KeyframeEffect::SetAnimation(Animation* aAnimation) {
if (mAnimation == aAnimation) {
return;
}
// Restyle for the old animation.
RequestRestyle(EffectCompositor::RestyleType::Layer);
mAnimation = aAnimation;
UpdateNormalizedTiming();
// The order of these function calls is important:
// NotifyAnimationTimingUpdated() need the updated mIsRelevant flag to check
// if it should create the effectSet or not, and MarkCascadeNeedsUpdate()
// needs a valid effectSet, so we should call them in this order.
if (mAnimation) {
mAnimation->UpdateRelevance();
}
NotifyAnimationTimingUpdated(PostRestyleMode::IfNeeded);
if (mAnimation) {
MarkCascadeNeedsUpdate();
}
}
bool KeyframeEffect::CanIgnoreIfNotVisible() const {
if (!StaticPrefs::dom_animations_offscreen_throttling()) {
return false;
}
// FIXME: For further sophisticated optimization we need to check
// change hint on the segment corresponding to computedTiming.progress.
return NS_IsHintSubset(mCumulativeChangeHint,
nsChangeHint_Hints_CanIgnoreIfNotVisible);
}
void KeyframeEffect::MarkCascadeNeedsUpdate() {
if (!mTarget) {
return;
}
EffectSet* effectSet = EffectSet::Get(mTarget.mElement, mTarget.mPseudoType);
if (!effectSet) {
return;
}
effectSet->MarkCascadeNeedsUpdate();
}
/* static */
bool KeyframeEffect::HasComputedTimingChanged(
const ComputedTiming& aComputedTiming,
IterationCompositeOperation aIterationComposite,
const Nullable<double>& aProgressOnLastCompose,
uint64_t aCurrentIterationOnLastCompose) {
// Typically we don't need to request a restyle if the progress hasn't
// changed since the last call to ComposeStyle. The one exception is if the
// iteration composite mode is 'accumulate' and the current iteration has
// changed, since that will often produce a different result.
return aComputedTiming.mProgress != aProgressOnLastCompose ||
(aIterationComposite == IterationCompositeOperation::Accumulate &&
aComputedTiming.mCurrentIteration != aCurrentIterationOnLastCompose);
}
bool KeyframeEffect::HasComputedTimingChanged() const {
ComputedTiming computedTiming = GetComputedTiming();
return HasComputedTimingChanged(
computedTiming, mEffectOptions.mIterationComposite,
mProgressOnLastCompose, mCurrentIterationOnLastCompose);
}
bool KeyframeEffect::ContainsAnimatedScale(const nsIFrame* aFrame) const {
// For display:table content, transform animations run on the table wrapper
// frame. If we are being passed a frame that doesn't support transforms
// (i.e. the inner table frame) we could just return false, but it possibly
// means we looked up the wrong EffectSet so for now we just assert instead.
MOZ_ASSERT(aFrame && aFrame->IsFrameOfType(nsIFrame::eSupportsCSSTransforms),
"We should be passed a frame that supports transforms");
if (!IsCurrent()) {
return false;
}
if (!mAnimation ||
mAnimation->ReplaceState() == AnimationReplaceState::Removed) {
return false;
}
for (const AnimationProperty& prop : mProperties) {
if (prop.mProperty != eCSSProperty_transform &&
prop.mProperty != eCSSProperty_scale &&
prop.mProperty != eCSSProperty_rotate) {
continue;
}
AnimationValue baseStyle = BaseStyle(prop.mProperty);
if (!baseStyle.IsNull()) {
gfx::MatrixScales size = baseStyle.GetScaleValue(aFrame);
if (size != gfx::MatrixScales()) {
return true;
}
}
// This is actually overestimate because there are some cases that combining
// the base value and from/to value produces 1:1 scale. But it doesn't
// really matter.
for (const AnimationPropertySegment& segment : prop.mSegments) {
if (!segment.mFromValue.IsNull()) {
gfx::MatrixScales from = segment.mFromValue.GetScaleValue(aFrame);
if (from != gfx::MatrixScales()) {
return true;
}
}
if (!segment.mToValue.IsNull()) {
gfx::MatrixScales to = segment.mToValue.GetScaleValue(aFrame);
if (to != gfx::MatrixScales()) {
return true;
}
}
}
}
return false;
}
void KeyframeEffect::UpdateEffectSet(EffectSet* aEffectSet) const {
if (!mInEffectSet) {
return;
}
EffectSet* effectSet =
aEffectSet ? aEffectSet
: EffectSet::Get(mTarget.mElement, mTarget.mPseudoType);
if (!effectSet) {
return;
}
nsIFrame* styleFrame = GetStyleFrame();
if (HasAnimationOfPropertySet(nsCSSPropertyIDSet::OpacityProperties())) {
effectSet->SetMayHaveOpacityAnimation();
EnumerateContinuationsOrIBSplitSiblings(styleFrame, [](nsIFrame* aFrame) {
aFrame->SetMayHaveOpacityAnimation();
});
}
nsIFrame* primaryFrame = GetPrimaryFrame();
if (HasAnimationOfPropertySet(
nsCSSPropertyIDSet::TransformLikeProperties())) {
effectSet->SetMayHaveTransformAnimation();
// For table frames, it's not clear if we should iterate over the
// continuations of the table wrapper or the inner table frame.
//
// Fortunately, this is not currently an issue because we only split tables
// when printing (page breaks) where we don't run animations.
EnumerateContinuationsOrIBSplitSiblings(primaryFrame, [](nsIFrame* aFrame) {
aFrame->SetMayHaveTransformAnimation();
});
}
}
KeyframeEffect::MatchForCompositor KeyframeEffect::IsMatchForCompositor(
const nsCSSPropertyIDSet& aPropertySet, const nsIFrame* aFrame,
const EffectSet& aEffects,
AnimationPerformanceWarning::Type& aPerformanceWarning /* out */) const {
MOZ_ASSERT(mAnimation);
if (!mAnimation->IsRelevant()) {
return KeyframeEffect::MatchForCompositor::No;
}
if (mAnimation->ShouldBeSynchronizedWithMainThread(aPropertySet, aFrame,
aPerformanceWarning)) {
// For a given |aFrame|, we don't want some animations of |aProperty| to
// run on the compositor and others to run on the main thread, so if any
// need to be synchronized with the main thread, run them all there.
return KeyframeEffect::MatchForCompositor::NoAndBlockThisProperty;
}
if (mAnimation->UsingScrollTimeline()) {
const ScrollTimeline* scrollTimeline =
mAnimation->GetTimeline()->AsScrollTimeline();
// We don't send this animation to the compositor if
// 1. the APZ is disabled entirely or for the source, or
// 2. the associated scroll-timeline is inactive, or
// 3. the scrolling direction is not available (i.e. no scroll range).
// 4. the scroll style of the scroller is overflow:hidden.
if (!scrollTimeline->APZIsActiveForSource() ||
!scrollTimeline->IsActive() ||
!scrollTimeline->ScrollingDirectionIsAvailable() ||
scrollTimeline->SourceScrollStyle() == StyleOverflow::Hidden) {
return KeyframeEffect::MatchForCompositor::No;
}
// FIXME: Bug 1818346. Support OMTA for view-timeline. We disable it for now
// because we need to make view-timeline-inset animations run on the OMTA as
// well before enable this.
if (scrollTimeline->IsViewTimeline()) {
return KeyframeEffect::MatchForCompositor::No;
}
}
if (!HasEffectiveAnimationOfPropertySet(aPropertySet, aEffects)) {
return KeyframeEffect::MatchForCompositor::No;
}
// If we know that the animation is not visible, we don't need to send the
// animation to the compositor.
if (!aFrame->IsVisibleOrMayHaveVisibleDescendants() ||
CanOptimizeAwayDueToOpacity(*this, *aFrame) ||
aFrame->IsScrolledOutOfView()) {
return KeyframeEffect::MatchForCompositor::NoAndBlockThisProperty;
}
if (aPropertySet.HasProperty(eCSSProperty_background_color)) {
if (!StaticPrefs::gfx_omta_background_color()) {
return KeyframeEffect::MatchForCompositor::No;
}
// We don't yet support off-main-thread background-color animations on
// canvas frame or on <html> or <body> which genarate
// nsDisplayCanvasBackgroundColor or nsDisplaySolidColor display item.
if (aFrame->IsCanvasFrame() ||
(aFrame->GetContent() &&
(aFrame->GetContent()->IsHTMLElement(nsGkAtoms::body) ||
aFrame->GetContent()->IsHTMLElement(nsGkAtoms::html)))) {
return KeyframeEffect::MatchForCompositor::No;
}
}
// We can't run this background color animation on the compositor if there
// is any `current-color` keyframe.
if (mHasCurrentColor) {
aPerformanceWarning = AnimationPerformanceWarning::Type::HasCurrentColor;
return KeyframeEffect::MatchForCompositor::NoAndBlockThisProperty;
}
return mAnimation->IsPlaying() ? KeyframeEffect::MatchForCompositor::Yes
: KeyframeEffect::MatchForCompositor::IfNeeded;
}
} // namespace dom
} // namespace mozilla
|