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
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
|
/* -*- 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 "Layers.h"
#include <inttypes.h> // for PRIu64
#include <stdio.h> // for stderr
#include <algorithm> // for max, min
#include <list> // for list
#include <set> // for set
#include <string> // for char_traits, string, basic_string
#include <type_traits> // for remove_reference<>::type
#include "CompositableHost.h" // for CompositableHost
#include "GeckoProfiler.h" // for profiler_can_accept_markers, PROFILER_MARKER_TEXT
#include "ImageLayers.h" // for ImageLayer
#include "LayerUserData.h" // for LayerUserData
#include "ReadbackLayer.h" // for ReadbackLayer
#include "TreeTraversal.h" // for ForwardIterator, ForEachNode, DepthFirstSearch, TraversalFlag, TraversalFl...
#include "UnitTransforms.h" // for ViewAs, PixelCastJustification, PixelCastJustification::RenderTargetIsPare...
#include "apz/src/AsyncPanZoomController.h" // for AsyncPanZoomController
#include "gfx2DGlue.h" // for ThebesMatrix, ToPoint, ThebesRect
#include "gfxEnv.h" // for gfxEnv
#include "gfxMatrix.h" // for gfxMatrix
#include "gfxUtils.h" // for gfxUtils, gfxUtils::sDumpPaintFile
#include "mozilla/ArrayIterator.h" // for ArrayIterator
#include "mozilla/BaseProfilerMarkersPrerequisites.h" // for MarkerTiming
#include "mozilla/DebugOnly.h" // for DebugOnly
#include "mozilla/Logging.h" // for LogLevel, LogLevel::Debug, MOZ_LOG_TEST
#include "mozilla/ScrollPositionUpdate.h" // for ScrollPositionUpdate
#include "mozilla/Telemetry.h" // for AccumulateTimeDelta
#include "mozilla/TelemetryHistogramEnums.h" // for KEYPRESS_PRESENT_LATENCY, SCROLL_PRESENT_LATENCY
#include "mozilla/ToString.h" // for ToString
#include "mozilla/gfx/2D.h" // for SourceSurface, DrawTarget, DataSourceSurface
#include "mozilla/gfx/BasePoint3D.h" // for BasePoint3D<>::(anonymous union)::(anonymous), BasePoint3D<>::(anonymous)
#include "mozilla/gfx/BaseRect.h" // for operator<<, BaseRect (ptr only)
#include "mozilla/gfx/BaseSize.h" // for operator<<, BaseSize<>::(anonymous union)::(anonymous), BaseSize<>::(anony...
#include "mozilla/gfx/Matrix.h" // for Matrix4x4, Matrix, Matrix4x4Typed<>::(anonymous union)::(anonymous), Matri...
#include "mozilla/gfx/MatrixFwd.h" // for Float
#include "mozilla/gfx/Polygon.h" // for Polygon, PolygonTyped
#include "mozilla/layers/BSPTree.h" // for LayerPolygon, BSPTree
#include "mozilla/layers/CompositableClient.h" // for CompositableClient
#include "mozilla/layers/Compositor.h" // for Compositor
#include "mozilla/layers/LayerManagerComposite.h" // for HostLayer
#include "mozilla/layers/LayersMessages.h" // for SpecificLayerAttributes, CompositorAnimations (ptr only), ContainerLayerAt...
#include "mozilla/layers/LayersTypes.h" // for EventRegions, operator<<, CompositionPayload, CSSTransformMatrix, MOZ_LAYE...
#include "mozilla/layers/ShadowLayers.h" // for ShadowableLayer
#include "nsBaseHashtable.h" // for nsBaseHashtable<>::Iterator, nsBaseHashtable<>::LookupResult
#include "nsISupportsUtils.h" // for NS_ADDREF, NS_RELEASE
#include "nsPrintfCString.h" // for nsPrintfCString
#include "nsRegionFwd.h" // for IntRegion
#include "nsString.h" // for nsTSubstring
#include "protobuf/LayerScopePacket.pb.h" // for LayersPacket::Layer, LayersPacket, LayersPacket_Layer::Matrix, LayersPacke...
// Undo the damage done by mozzconf.h
#undef compress
#include "mozilla/Compression.h"
namespace mozilla {
namespace layers {
typedef ScrollableLayerGuid::ViewID ViewID;
using namespace mozilla::gfx;
using namespace mozilla::Compression;
//--------------------------------------------------
// Layer
Layer::Layer(LayerManager* aManager, void* aImplData)
: mManager(aManager),
mParent(nullptr),
mNextSibling(nullptr),
mPrevSibling(nullptr),
mImplData(aImplData),
mUseTileSourceRect(false)
#ifdef DEBUG
,
mDebugColorIndex(0)
#endif
{
}
Layer::~Layer() = default;
void Layer::SetEventRegions(const EventRegions& aRegions) {
if (mEventRegions != aRegions) {
MOZ_LAYERS_LOG_IF_SHADOWABLE(
this, ("Layer::Mutated(%p) eventregions were %s, now %s", this,
ToString(mEventRegions).c_str(), ToString(aRegions).c_str()));
mEventRegions = aRegions;
Mutated();
}
}
void Layer::SetCompositorAnimations(
const LayersId& aLayersId,
const CompositorAnimations& aCompositorAnimations) {
MOZ_LAYERS_LOG_IF_SHADOWABLE(
this, ("Layer::Mutated(%p) SetCompositorAnimations with id=%" PRIu64,
this, mAnimationInfo.GetCompositorAnimationsId()));
mAnimationInfo.SetCompositorAnimations(aLayersId, aCompositorAnimations);
Mutated();
}
void Layer::ClearCompositorAnimations() {
MOZ_LAYERS_LOG_IF_SHADOWABLE(
this, ("Layer::Mutated(%p) ClearCompositorAnimations with id=%" PRIu64,
this, mAnimationInfo.GetCompositorAnimationsId()));
mAnimationInfo.ClearAnimations();
Mutated();
}
void Layer::StartPendingAnimations(const TimeStamp& aReadyTime) {
ForEachNode<ForwardIterator>(this, [&aReadyTime](Layer* layer) {
if (layer->mAnimationInfo.StartPendingAnimations(aReadyTime)) {
layer->Mutated();
}
});
}
void Layer::SetAsyncPanZoomController(uint32_t aIndex,
AsyncPanZoomController* controller) {
MOZ_ASSERT(aIndex < GetScrollMetadataCount());
// We should never be setting an APZC on a non-scrollable layer
MOZ_ASSERT(!controller || GetFrameMetrics(aIndex).IsScrollable());
mApzcs[aIndex] = controller;
}
AsyncPanZoomController* Layer::GetAsyncPanZoomController(
uint32_t aIndex) const {
MOZ_ASSERT(aIndex < GetScrollMetadataCount());
#ifdef DEBUG
if (mApzcs[aIndex]) {
MOZ_ASSERT(GetFrameMetrics(aIndex).IsScrollable());
}
#endif
return mApzcs[aIndex];
}
void Layer::ScrollMetadataChanged() {
mApzcs.SetLength(GetScrollMetadataCount());
}
std::unordered_set<ScrollableLayerGuid::ViewID>
Layer::ApplyPendingUpdatesToSubtree() {
ForEachNode<ForwardIterator>(this, [](Layer* layer) {
layer->ApplyPendingUpdatesForThisTransaction();
});
// Once we're done recursing through the whole tree, clear the pending
// updates from the manager.
return Manager()->ClearPendingScrollInfoUpdate();
}
bool Layer::IsOpaqueForVisibility() {
return GetEffectiveOpacity() == 1.0f &&
GetEffectiveMixBlendMode() == CompositionOp::OP_OVER;
}
bool Layer::CanUseOpaqueSurface() {
// If the visible content in the layer is opaque, there is no need
// for an alpha channel.
if (GetContentFlags() & CONTENT_OPAQUE) return true;
// Also, if this layer is the bottommost layer in a container which
// doesn't need an alpha channel, we can use an opaque surface for this
// layer too. Any transparent areas must be covered by something else
// in the container.
ContainerLayer* parent = GetParent();
return parent && parent->GetFirstChild() == this &&
parent->CanUseOpaqueSurface();
}
// NB: eventually these methods will be defined unconditionally, and
// can be moved into Layers.h
const Maybe<ParentLayerIntRect>& Layer::GetLocalClipRect() {
if (HostLayer* shadow = AsHostLayer()) {
return shadow->GetShadowClipRect();
}
return GetClipRect();
}
const LayerIntRegion& Layer::GetLocalVisibleRegion() {
if (HostLayer* shadow = AsHostLayer()) {
return shadow->GetShadowVisibleRegion();
}
return GetVisibleRegion();
}
Matrix4x4 Layer::SnapTransformTranslation(const Matrix4x4& aTransform,
Matrix* aResidualTransform) {
if (aResidualTransform) {
*aResidualTransform = Matrix();
}
if (!mManager->IsSnappingEffectiveTransforms()) {
return aTransform;
}
Matrix matrix2D;
if (aTransform.CanDraw2D(&matrix2D) && !matrix2D.HasNonTranslation() &&
matrix2D.HasNonIntegerTranslation()) {
auto snappedTranslation = IntPoint::Round(matrix2D.GetTranslation());
Matrix snappedMatrix =
Matrix::Translation(snappedTranslation.x, snappedTranslation.y);
Matrix4x4 result = Matrix4x4::From2D(snappedMatrix);
if (aResidualTransform) {
// set aResidualTransform so that aResidual * snappedMatrix == matrix2D.
// (I.e., appying snappedMatrix after aResidualTransform gives the
// ideal transform.)
*aResidualTransform =
Matrix::Translation(matrix2D._31 - snappedTranslation.x,
matrix2D._32 - snappedTranslation.y);
}
return result;
}
return SnapTransformTranslation3D(aTransform, aResidualTransform);
}
Matrix4x4 Layer::SnapTransformTranslation3D(const Matrix4x4& aTransform,
Matrix* aResidualTransform) {
if (aTransform.IsSingular() || aTransform.HasPerspectiveComponent() ||
aTransform.HasNonTranslation() ||
!aTransform.HasNonIntegerTranslation()) {
// For a singular transform, there is no reversed matrix, so we
// don't snap it.
// For a perspective transform, the content is transformed in
// non-linear, so we don't snap it too.
return aTransform;
}
// Snap for 3D Transforms
Point3D transformedOrigin = aTransform.TransformPoint(Point3D());
// Compute the transformed snap by rounding the values of
// transformed origin.
auto transformedSnapXY =
IntPoint::Round(transformedOrigin.x, transformedOrigin.y);
Matrix4x4 inverse = aTransform;
inverse.Invert();
// see Matrix4x4::ProjectPoint()
Float transformedSnapZ =
inverse._33 == 0 ? 0
: (-(transformedSnapXY.x * inverse._13 +
transformedSnapXY.y * inverse._23 + inverse._43) /
inverse._33);
Point3D transformedSnap =
Point3D(transformedSnapXY.x, transformedSnapXY.y, transformedSnapZ);
if (transformedOrigin == transformedSnap) {
return aTransform;
}
// Compute the snap from the transformed snap.
Point3D snap = inverse.TransformPoint(transformedSnap);
if (snap.z > 0.001 || snap.z < -0.001) {
// Allow some level of accumulated computation error.
MOZ_ASSERT(inverse._33 == 0.0);
return aTransform;
}
// The difference between the origin and snap is the residual transform.
if (aResidualTransform) {
// The residual transform is to translate the snap to the origin
// of the content buffer.
*aResidualTransform = Matrix::Translation(-snap.x, -snap.y);
}
// Translate transformed origin to transformed snap since the
// residual transform would trnslate the snap to the origin.
Point3D transformedShift = transformedSnap - transformedOrigin;
Matrix4x4 result = aTransform;
result.PostTranslate(transformedShift.x, transformedShift.y,
transformedShift.z);
// For non-2d transform, residual translation could be more than
// 0.5 pixels for every axis.
return result;
}
Matrix4x4 Layer::SnapTransform(const Matrix4x4& aTransform,
const gfxRect& aSnapRect,
Matrix* aResidualTransform) {
if (aResidualTransform) {
*aResidualTransform = Matrix();
}
Matrix matrix2D;
Matrix4x4 result;
if (mManager->IsSnappingEffectiveTransforms() && aTransform.Is2D(&matrix2D) &&
gfxSize(1.0, 1.0) <= aSnapRect.Size() &&
matrix2D.PreservesAxisAlignedRectangles()) {
auto transformedTopLeft =
IntPoint::Round(matrix2D.TransformPoint(ToPoint(aSnapRect.TopLeft())));
auto transformedTopRight =
IntPoint::Round(matrix2D.TransformPoint(ToPoint(aSnapRect.TopRight())));
auto transformedBottomRight = IntPoint::Round(
matrix2D.TransformPoint(ToPoint(aSnapRect.BottomRight())));
Matrix snappedMatrix = gfxUtils::TransformRectToRect(
aSnapRect, transformedTopLeft, transformedTopRight,
transformedBottomRight);
result = Matrix4x4::From2D(snappedMatrix);
if (aResidualTransform && !snappedMatrix.IsSingular()) {
// set aResidualTransform so that aResidual * snappedMatrix == matrix2D.
// (i.e., appying snappedMatrix after aResidualTransform gives the
// ideal transform.
Matrix snappedMatrixInverse = snappedMatrix;
snappedMatrixInverse.Invert();
*aResidualTransform = matrix2D * snappedMatrixInverse;
}
} else {
result = aTransform;
}
return result;
}
static bool AncestorLayerMayChangeTransform(Layer* aLayer) {
for (Layer* l = aLayer; l; l = l->GetParent()) {
if (l->GetContentFlags() & Layer::CONTENT_MAY_CHANGE_TRANSFORM) {
return true;
}
if (l->GetParent() && l->GetParent()->AsRefLayer()) {
return false;
}
}
return false;
}
bool Layer::MayResample() {
Matrix transform2d;
return !GetEffectiveTransform().Is2D(&transform2d) ||
ThebesMatrix(transform2d).HasNonIntegerTranslation() ||
AncestorLayerMayChangeTransform(this);
}
RenderTargetIntRect Layer::CalculateScissorRect(
const RenderTargetIntRect& aCurrentScissorRect) {
ContainerLayer* container = GetParent();
ContainerLayer* containerChild = nullptr;
NS_ASSERTION(GetParent(), "This can't be called on the root!");
// Find the layer creating the 3D context.
while (container->Extend3DContext() && !container->UseIntermediateSurface()) {
containerChild = container;
container = container->GetParent();
MOZ_ASSERT(container);
}
// Find the nearest layer with a clip, or this layer.
// ContainerState::SetupScrollingMetadata() may install a clip on
// the layer.
Layer* clipLayer = containerChild && containerChild->GetLocalClipRect()
? containerChild
: this;
// Establish initial clip rect: it's either the one passed in, or
// if the parent has an intermediate surface, it's the extents of that
// surface.
RenderTargetIntRect currentClip;
if (container->UseIntermediateSurface()) {
currentClip.SizeTo(container->GetIntermediateSurfaceRect().Size());
} else {
currentClip = aCurrentScissorRect;
}
if (!clipLayer->GetLocalClipRect()) {
return currentClip;
}
if (GetLocalVisibleRegion().IsEmpty()) {
// When our visible region is empty, our parent may not have created the
// intermediate surface that we would require for correct clipping; however,
// this does not matter since we are invisible.
// Make sure we still compute a clip rect if we want to draw checkboarding
// for this layer, since we want to do this even if the layer is invisible.
return RenderTargetIntRect(currentClip.TopLeft(),
RenderTargetIntSize(0, 0));
}
const RenderTargetIntRect clipRect = ViewAs<RenderTargetPixel>(
*clipLayer->GetLocalClipRect(),
PixelCastJustification::RenderTargetIsParentLayerForRoot);
if (clipRect.IsEmpty()) {
// We might have a non-translation transform in the container so we can't
// use the code path below.
return RenderTargetIntRect(currentClip.TopLeft(),
RenderTargetIntSize(0, 0));
}
RenderTargetIntRect scissor = clipRect;
if (!container->UseIntermediateSurface()) {
gfx::Matrix matrix;
DebugOnly<bool> is2D = container->GetEffectiveTransform().Is2D(&matrix);
// See DefaultComputeEffectiveTransforms below
NS_ASSERTION(is2D && matrix.PreservesAxisAlignedRectangles(),
"Non preserves axis aligned transform with clipped child "
"should have forced intermediate surface");
gfx::Rect r(scissor.X(), scissor.Y(), scissor.Width(), scissor.Height());
gfxRect trScissor = gfx::ThebesRect(matrix.TransformBounds(r));
trScissor.Round();
IntRect tmp;
if (!gfxUtils::GfxRectToIntRect(trScissor, &tmp)) {
return RenderTargetIntRect(currentClip.TopLeft(),
RenderTargetIntSize(0, 0));
}
scissor = ViewAs<RenderTargetPixel>(tmp);
// Find the nearest ancestor with an intermediate surface
do {
container = container->GetParent();
} while (container && !container->UseIntermediateSurface());
}
if (container) {
scissor.MoveBy(-container->GetIntermediateSurfaceRect().TopLeft());
}
return currentClip.Intersect(scissor);
}
Maybe<ParentLayerIntRect> Layer::GetScrolledClipRect() const {
const Maybe<LayerClip> clip = mSimpleAttrs.GetScrolledClip();
return clip ? Some(clip->GetClipRect()) : Nothing();
}
const ScrollMetadata& Layer::GetScrollMetadata(uint32_t aIndex) const {
MOZ_ASSERT(aIndex < GetScrollMetadataCount());
return mScrollMetadata[aIndex];
}
const FrameMetrics& Layer::GetFrameMetrics(uint32_t aIndex) const {
return GetScrollMetadata(aIndex).GetMetrics();
}
bool Layer::HasScrollableFrameMetrics() const {
for (uint32_t i = 0; i < GetScrollMetadataCount(); i++) {
if (GetFrameMetrics(i).IsScrollable()) {
return true;
}
}
return false;
}
bool Layer::IsScrollableWithoutContent() const {
// A scrollable container layer with no children
return AsContainerLayer() && HasScrollableFrameMetrics() && !GetFirstChild();
}
Matrix4x4 Layer::GetTransform() const {
Matrix4x4 transform = mSimpleAttrs.GetTransform();
transform.PostScale(GetPostXScale(), GetPostYScale(), 1.0f);
if (const ContainerLayer* c = AsContainerLayer()) {
transform.PreScale(c->GetPreXScale(), c->GetPreYScale(), 1.0f);
}
return transform;
}
const CSSTransformMatrix Layer::GetTransformTyped() const {
return ViewAs<CSSTransformMatrix>(GetTransform());
}
Matrix4x4 Layer::GetLocalTransform() {
if (HostLayer* shadow = AsHostLayer()) {
return shadow->GetShadowTransform();
}
return GetTransform();
}
const LayerToParentLayerMatrix4x4 Layer::GetLocalTransformTyped() {
return ViewAs<LayerToParentLayerMatrix4x4>(GetLocalTransform());
}
bool Layer::IsScrollbarContainer() const {
const ScrollbarData& data = GetScrollbarData();
return (data.mScrollbarLayerType == ScrollbarLayerType::Container)
? data.mDirection.isSome()
: false;
}
bool Layer::HasTransformAnimation() const {
return mAnimationInfo.HasTransformAnimation();
}
void Layer::ApplyPendingUpdatesForThisTransaction() {
if (mPendingTransform && *mPendingTransform != mSimpleAttrs.GetTransform()) {
MOZ_LAYERS_LOG_IF_SHADOWABLE(
this, ("Layer::Mutated(%p) PendingUpdatesForThisTransaction", this));
mSimpleAttrs.SetTransform(*mPendingTransform);
MutatedSimple();
}
mPendingTransform = nullptr;
if (mAnimationInfo.ApplyPendingUpdatesForThisTransaction()) {
MOZ_LAYERS_LOG_IF_SHADOWABLE(
this, ("Layer::Mutated(%p) PendingUpdatesForThisTransaction", this));
Mutated();
}
for (size_t i = 0; i < mScrollMetadata.Length(); i++) {
FrameMetrics& fm = mScrollMetadata[i].GetMetrics();
ScrollableLayerGuid::ViewID scrollId = fm.GetScrollId();
Maybe<nsTArray<ScrollPositionUpdate>> update =
Manager()->GetPendingScrollInfoUpdate(scrollId);
if (update) {
nsTArray<ScrollPositionUpdate> infos = update.extract();
mScrollMetadata[i].UpdatePendingScrollInfo(std::move(infos));
Mutated();
}
}
}
float Layer::GetLocalOpacity() {
float opacity = mSimpleAttrs.GetOpacity();
if (HostLayer* shadow = AsHostLayer()) opacity = shadow->GetShadowOpacity();
return std::min(std::max(opacity, 0.0f), 1.0f);
}
float Layer::GetEffectiveOpacity() {
float opacity = GetLocalOpacity();
for (ContainerLayer* c = GetParent(); c && !c->UseIntermediateSurface();
c = c->GetParent()) {
opacity *= c->GetLocalOpacity();
}
return opacity;
}
CompositionOp Layer::GetEffectiveMixBlendMode() {
if (mSimpleAttrs.GetMixBlendMode() != CompositionOp::OP_OVER)
return mSimpleAttrs.GetMixBlendMode();
for (ContainerLayer* c = GetParent(); c && !c->UseIntermediateSurface();
c = c->GetParent()) {
if (c->mSimpleAttrs.GetMixBlendMode() != CompositionOp::OP_OVER)
return c->mSimpleAttrs.GetMixBlendMode();
}
return mSimpleAttrs.GetMixBlendMode();
}
Matrix4x4 Layer::ComputeTransformToPreserve3DRoot() {
Matrix4x4 transform = GetLocalTransform();
for (Layer* layer = GetParent(); layer && layer->Extend3DContext();
layer = layer->GetParent()) {
transform = transform * layer->GetLocalTransform();
}
return transform;
}
void Layer::ComputeEffectiveTransformForMaskLayers(
const gfx::Matrix4x4& aTransformToSurface) {
if (GetMaskLayer()) {
ComputeEffectiveTransformForMaskLayer(GetMaskLayer(), aTransformToSurface);
}
for (size_t i = 0; i < GetAncestorMaskLayerCount(); i++) {
Layer* maskLayer = GetAncestorMaskLayerAt(i);
ComputeEffectiveTransformForMaskLayer(maskLayer, aTransformToSurface);
}
}
/* static */
void Layer::ComputeEffectiveTransformForMaskLayer(
Layer* aMaskLayer, const gfx::Matrix4x4& aTransformToSurface) {
#ifdef DEBUG
bool maskIs2D = aMaskLayer->GetTransform().CanDraw2D();
NS_ASSERTION(maskIs2D, "How did we end up with a 3D transform here?!");
#endif
// The mask layer can have an async transform applied to it in some
// situations, so be sure to use its GetLocalTransform() rather than
// its GetTransform().
aMaskLayer->mEffectiveTransform = aMaskLayer->SnapTransformTranslation(
aMaskLayer->GetLocalTransform() * aTransformToSurface, nullptr);
}
RenderTargetRect Layer::TransformRectToRenderTarget(const LayerIntRect& aRect) {
LayerRect rect(aRect);
RenderTargetRect quad = RenderTargetRect::FromUnknownRect(
GetEffectiveTransform().TransformBounds(rect.ToUnknownRect()));
return quad;
}
bool Layer::GetVisibleRegionRelativeToRootLayer(nsIntRegion& aResult,
IntPoint* aLayerOffset) {
MOZ_ASSERT(aLayerOffset, "invalid offset pointer");
if (!GetParent()) {
return false;
}
IntPoint offset;
aResult = GetLocalVisibleRegion().ToUnknownRegion();
for (Layer* layer = this; layer; layer = layer->GetParent()) {
gfx::Matrix matrix;
if (!layer->GetLocalTransform().Is2D(&matrix) || !matrix.IsTranslation()) {
return false;
}
// The offset of |layer| to its parent.
auto currentLayerOffset = IntPoint::Round(matrix.GetTranslation());
// Translate the accumulated visible region of |this| by the offset of
// |layer|.
aResult.MoveBy(currentLayerOffset.x, currentLayerOffset.y);
// If the parent layer clips its lower layers, clip the visible region
// we're accumulating.
if (layer->GetLocalClipRect()) {
aResult.AndWith(layer->GetLocalClipRect()->ToUnknownRect());
}
// Now we need to walk across the list of siblings for this parent layer,
// checking to see if any of these layer trees obscure |this|. If so,
// remove these areas from the visible region as well. This will pick up
// chrome overlays like a tab modal prompt.
Layer* sibling;
for (sibling = layer->GetNextSibling(); sibling;
sibling = sibling->GetNextSibling()) {
gfx::Matrix siblingMatrix;
if (!sibling->GetLocalTransform().Is2D(&siblingMatrix) ||
!siblingMatrix.IsTranslation()) {
continue;
}
// Retreive the translation from sibling to |layer|. The accumulated
// visible region is currently oriented with |layer|.
auto siblingOffset = IntPoint::Round(siblingMatrix.GetTranslation());
nsIntRegion siblingVisibleRegion(
sibling->GetLocalVisibleRegion().ToUnknownRegion());
// Translate the siblings region to |layer|'s origin.
siblingVisibleRegion.MoveBy(-siblingOffset.x, -siblingOffset.y);
// Apply the sibling's clip.
// Layer clip rects are not affected by the layer's transform.
Maybe<ParentLayerIntRect> clipRect = sibling->GetLocalClipRect();
if (clipRect) {
siblingVisibleRegion.AndWith(clipRect->ToUnknownRect());
}
// Subtract the sibling visible region from the visible region of |this|.
aResult.SubOut(siblingVisibleRegion);
}
// Keep track of the total offset for aLayerOffset. We use this in plugin
// positioning code.
offset += currentLayerOffset;
}
*aLayerOffset = IntPoint(offset.x, offset.y);
return true;
}
Maybe<ParentLayerIntRect> Layer::GetCombinedClipRect() const {
Maybe<ParentLayerIntRect> clip = GetClipRect();
clip = IntersectMaybeRects(clip, GetScrolledClipRect());
for (size_t i = 0; i < mScrollMetadata.Length(); i++) {
clip = IntersectMaybeRects(clip, mScrollMetadata[i].GetClipRect());
}
return clip;
}
ContainerLayer::ContainerLayer(LayerManager* aManager, void* aImplData)
: Layer(aManager, aImplData),
mFirstChild(nullptr),
mLastChild(nullptr),
mPreXScale(1.0f),
mPreYScale(1.0f),
mInheritedXScale(1.0f),
mInheritedYScale(1.0f),
mPresShellResolution(1.0f),
mUseIntermediateSurface(false),
mSupportsComponentAlphaChildren(false),
mMayHaveReadbackChild(false),
mChildrenChanged(false) {}
ContainerLayer::~ContainerLayer() = default;
bool ContainerLayer::InsertAfter(Layer* aChild, Layer* aAfter) {
if (aChild->Manager() != Manager()) {
NS_ERROR("Child has wrong manager");
return false;
}
if (aChild->GetParent()) {
NS_ERROR("aChild already in the tree");
return false;
}
if (aChild->GetNextSibling() || aChild->GetPrevSibling()) {
NS_ERROR("aChild already has siblings?");
return false;
}
if (aAfter &&
(aAfter->Manager() != Manager() || aAfter->GetParent() != this)) {
NS_ERROR("aAfter is not our child");
return false;
}
aChild->SetParent(this);
if (aAfter == mLastChild) {
mLastChild = aChild;
}
if (!aAfter) {
aChild->SetNextSibling(mFirstChild);
if (mFirstChild) {
mFirstChild->SetPrevSibling(aChild);
}
mFirstChild = aChild;
NS_ADDREF(aChild);
DidInsertChild(aChild);
return true;
}
Layer* next = aAfter->GetNextSibling();
aChild->SetNextSibling(next);
aChild->SetPrevSibling(aAfter);
if (next) {
next->SetPrevSibling(aChild);
}
aAfter->SetNextSibling(aChild);
NS_ADDREF(aChild);
DidInsertChild(aChild);
return true;
}
void ContainerLayer::RemoveAllChildren() {
// Optimizes "while (mFirstChild) ContainerLayer::RemoveChild(mFirstChild);"
Layer* current = mFirstChild;
// This is inlining DidRemoveChild() on each layer; we can skip the calls
// to NotifyPaintedLayerRemoved as it gets taken care of when as we call
// NotifyRemoved prior to removing any layers.
while (current) {
Layer* next = current->GetNextSibling();
if (current->GetType() == TYPE_READBACK) {
static_cast<ReadbackLayer*>(current)->NotifyRemoved();
}
current = next;
}
current = mFirstChild;
mFirstChild = nullptr;
while (current) {
MOZ_ASSERT(!current->GetPrevSibling());
Layer* next = current->GetNextSibling();
current->SetParent(nullptr);
current->SetNextSibling(nullptr);
if (next) {
next->SetPrevSibling(nullptr);
}
NS_RELEASE(current);
current = next;
}
}
// Note that ContainerLayer::RemoveAllChildren is an optimized
// version of this code; if you make changes to ContainerLayer::RemoveChild
// consider whether the matching changes need to be made to
// ContainerLayer::RemoveAllChildren
bool ContainerLayer::RemoveChild(Layer* aChild) {
if (aChild->Manager() != Manager()) {
NS_ERROR("Child has wrong manager");
return false;
}
if (aChild->GetParent() != this) {
NS_ERROR("aChild not our child");
return false;
}
Layer* prev = aChild->GetPrevSibling();
Layer* next = aChild->GetNextSibling();
if (prev) {
prev->SetNextSibling(next);
} else {
this->mFirstChild = next;
}
if (next) {
next->SetPrevSibling(prev);
} else {
this->mLastChild = prev;
}
aChild->SetNextSibling(nullptr);
aChild->SetPrevSibling(nullptr);
aChild->SetParent(nullptr);
this->DidRemoveChild(aChild);
NS_RELEASE(aChild);
return true;
}
bool ContainerLayer::RepositionChild(Layer* aChild, Layer* aAfter) {
if (aChild->Manager() != Manager()) {
NS_ERROR("Child has wrong manager");
return false;
}
if (aChild->GetParent() != this) {
NS_ERROR("aChild not our child");
return false;
}
if (aAfter &&
(aAfter->Manager() != Manager() || aAfter->GetParent() != this)) {
NS_ERROR("aAfter is not our child");
return false;
}
if (aChild == aAfter) {
NS_ERROR("aChild cannot be the same as aAfter");
return false;
}
Layer* prev = aChild->GetPrevSibling();
Layer* next = aChild->GetNextSibling();
if (prev == aAfter) {
// aChild is already in the correct position, nothing to do.
return true;
}
if (prev) {
prev->SetNextSibling(next);
} else {
mFirstChild = next;
}
if (next) {
next->SetPrevSibling(prev);
} else {
mLastChild = prev;
}
if (!aAfter) {
aChild->SetPrevSibling(nullptr);
aChild->SetNextSibling(mFirstChild);
if (mFirstChild) {
mFirstChild->SetPrevSibling(aChild);
}
mFirstChild = aChild;
return true;
}
Layer* afterNext = aAfter->GetNextSibling();
if (afterNext) {
afterNext->SetPrevSibling(aChild);
} else {
mLastChild = aChild;
}
aAfter->SetNextSibling(aChild);
aChild->SetPrevSibling(aAfter);
aChild->SetNextSibling(afterNext);
return true;
}
void ContainerLayer::FillSpecificAttributes(SpecificLayerAttributes& aAttrs) {
aAttrs = ContainerLayerAttributes(mPreXScale, mPreYScale, mInheritedXScale,
mInheritedYScale, mPresShellResolution);
}
bool ContainerLayer::Creates3DContextWithExtendingChildren() {
if (Extend3DContext()) {
return false;
}
for (Layer* child = GetFirstChild(); child; child = child->GetNextSibling()) {
if (child->Extend3DContext()) {
return true;
}
}
return false;
}
RenderTargetIntRect ContainerLayer::GetIntermediateSurfaceRect() {
NS_ASSERTION(mUseIntermediateSurface, "Must have intermediate surface");
LayerIntRect bounds = GetLocalVisibleRegion().GetBounds();
return RenderTargetIntRect::FromUnknownRect(bounds.ToUnknownRect());
}
bool ContainerLayer::HasMultipleChildren() {
uint32_t count = 0;
for (Layer* child = GetFirstChild(); child; child = child->GetNextSibling()) {
const Maybe<ParentLayerIntRect>& clipRect = child->GetLocalClipRect();
if (clipRect && clipRect->IsEmpty()) continue;
if (!child->Extend3DContext() && child->GetLocalVisibleRegion().IsEmpty())
continue;
++count;
if (count > 1) return true;
}
return false;
}
/**
* Collect all leaf descendants of the current 3D context.
*/
void ContainerLayer::Collect3DContextLeaves(nsTArray<Layer*>& aToSort) {
ForEachNode<ForwardIterator>((Layer*)this, [this, &aToSort](Layer* layer) {
ContainerLayer* container = layer->AsContainerLayer();
if (layer == this || (container && container->Extend3DContext() &&
!container->UseIntermediateSurface())) {
return TraversalFlag::Continue;
}
aToSort.AppendElement(layer);
return TraversalFlag::Skip;
});
}
static nsTArray<LayerPolygon> SortLayersWithBSPTree(nsTArray<Layer*>& aArray) {
std::list<LayerPolygon> inputLayers;
// Build a list of polygons to be sorted.
for (Layer* layer : aArray) {
// Ignore invisible layers.
if (!layer->IsVisible()) {
continue;
}
const gfx::IntRect& bounds =
layer->GetLocalVisibleRegion().GetBounds().ToUnknownRect();
const gfx::Matrix4x4& transform = layer->GetEffectiveTransform();
if (transform.IsSingular()) {
// Transform cannot be inverted.
continue;
}
gfx::Polygon polygon = gfx::Polygon::FromRect(gfx::Rect(bounds));
// Transform the polygon to screen space.
polygon.TransformToScreenSpace(transform);
if (polygon.GetPoints().Length() >= 3) {
inputLayers.push_back(LayerPolygon(layer, std::move(polygon)));
}
}
if (inputLayers.empty()) {
return nsTArray<LayerPolygon>();
}
// Build a BSP tree from the list of polygons.
BSPTree tree(inputLayers);
nsTArray<LayerPolygon> orderedLayers(tree.GetDrawOrder());
// Transform the polygons back to layer space.
for (LayerPolygon& layerPolygon : orderedLayers) {
gfx::Matrix4x4 inverse =
layerPolygon.layer->GetEffectiveTransform().Inverse();
MOZ_ASSERT(layerPolygon.geometry);
layerPolygon.geometry->TransformToLayerSpace(inverse);
}
return orderedLayers;
}
static nsTArray<LayerPolygon> StripLayerGeometry(
const nsTArray<LayerPolygon>& aLayers) {
nsTArray<LayerPolygon> layers;
std::set<Layer*> uniqueLayers;
for (const LayerPolygon& layerPolygon : aLayers) {
auto result = uniqueLayers.insert(layerPolygon.layer);
if (result.second) {
// Layer was added to the set.
layers.AppendElement(LayerPolygon(layerPolygon.layer));
}
}
return layers;
}
nsTArray<LayerPolygon> ContainerLayer::SortChildrenBy3DZOrder(
SortMode aSortMode) {
AutoTArray<Layer*, 10> toSort;
nsTArray<LayerPolygon> drawOrder;
for (Layer* layer = GetFirstChild(); layer; layer = layer->GetNextSibling()) {
ContainerLayer* container = layer->AsContainerLayer();
if (container && container->Extend3DContext() &&
!container->UseIntermediateSurface()) {
// Collect 3D layers in toSort array.
container->Collect3DContextLeaves(toSort);
// Sort the 3D layers.
if (toSort.Length() > 0) {
nsTArray<LayerPolygon> sorted = SortLayersWithBSPTree(toSort);
drawOrder.AppendElements(std::move(sorted));
toSort.ClearAndRetainStorage();
}
continue;
}
drawOrder.AppendElement(LayerPolygon(layer));
}
if (aSortMode == SortMode::WITHOUT_GEOMETRY) {
// Compositor does not support arbitrary layers, strip the layer geometry
// and duplicate layers.
return StripLayerGeometry(drawOrder);
}
return drawOrder;
}
bool ContainerLayer::AnyAncestorOrThisIs3DContextLeaf() {
Layer* parent = this;
while (parent != nullptr) {
if (parent->Is3DContextLeaf()) {
return true;
}
parent = parent->GetParent();
}
return false;
}
void ContainerLayer::DefaultComputeEffectiveTransforms(
const Matrix4x4& aTransformToSurface) {
Matrix residual;
Matrix4x4 idealTransform = GetLocalTransform() * aTransformToSurface;
// Keep 3D transforms for leaves to keep z-order sorting correct.
if (!Extend3DContext() && !Is3DContextLeaf()) {
idealTransform.ProjectTo2D();
}
bool useIntermediateSurface;
if (HasMaskLayers() || GetForceIsolatedGroup()) {
useIntermediateSurface = true;
#ifdef MOZ_DUMP_PAINTING
} else if (gfxEnv::DumpPaintIntermediate() && !Extend3DContext()) {
useIntermediateSurface = true;
#endif
} else {
/* Don't use an intermediate surface for opacity when it's within a 3d
* context, since we'd rather keep the 3d effects. This matches the
* WebKit/blink behaviour, but is changing in the latest spec.
*/
float opacity = GetEffectiveOpacity();
CompositionOp blendMode = GetEffectiveMixBlendMode();
if ((HasMultipleChildren() || Creates3DContextWithExtendingChildren()) &&
((opacity != 1.0f && !Extend3DContext()) ||
(blendMode != CompositionOp::OP_OVER))) {
useIntermediateSurface = true;
} else if ((!idealTransform.Is2D() || AnyAncestorOrThisIs3DContextLeaf()) &&
Creates3DContextWithExtendingChildren()) {
useIntermediateSurface = true;
} else if (blendMode != CompositionOp::OP_OVER &&
Manager()->BlendingRequiresIntermediateSurface()) {
useIntermediateSurface = true;
} else {
useIntermediateSurface = false;
gfx::Matrix contTransform;
bool checkClipRect = false;
bool checkMaskLayers = false;
if (!idealTransform.Is2D(&contTransform)) {
// In 3D case, always check if we should use IntermediateSurface.
checkClipRect = true;
checkMaskLayers = true;
} else {
contTransform.NudgeToIntegers();
#ifdef MOZ_GFX_OPTIMIZE_MOBILE
if (!contTransform.PreservesAxisAlignedRectangles()) {
#else
if (gfx::ThebesMatrix(contTransform).HasNonIntegerTranslation()) {
#endif
checkClipRect = true;
}
/* In 2D case, only translation and/or positive scaling can be done w/o
* using IntermediateSurface. Otherwise, when rotation or flip happen,
* we should check whether to use IntermediateSurface.
*/
if (contTransform.HasNonAxisAlignedTransform() ||
contTransform.HasNegativeScaling()) {
checkMaskLayers = true;
}
}
if (checkClipRect || checkMaskLayers) {
for (Layer* child = GetFirstChild(); child;
child = child->GetNextSibling()) {
const Maybe<ParentLayerIntRect>& clipRect = child->GetLocalClipRect();
/* We can't (easily) forward our transform to children with a
* non-empty clip rect since it would need to be adjusted for the
* transform. See the calculations performed by CalculateScissorRect
* above. Nor for a child with a mask layer.
*/
if (checkClipRect && (clipRect && !clipRect->IsEmpty() &&
(child->Extend3DContext() ||
!child->GetLocalVisibleRegion().IsEmpty()))) {
useIntermediateSurface = true;
break;
}
if (checkMaskLayers && child->HasMaskLayers()) {
useIntermediateSurface = true;
break;
}
}
}
}
}
NS_ASSERTION(!Extend3DContext() || !useIntermediateSurface,
"Can't have an intermediate surface with preserve-3d!");
if (useIntermediateSurface) {
mEffectiveTransform = SnapTransformTranslation(idealTransform, &residual);
} else {
mEffectiveTransform = idealTransform;
}
// For layers extending 3d context, its ideal transform should be
// applied on children.
if (!Extend3DContext()) {
// Without this projection, non-container children would get a 3D
// transform while 2D is expected.
idealTransform.ProjectTo2D();
}
mUseIntermediateSurface = useIntermediateSurface;
if (useIntermediateSurface) {
ComputeEffectiveTransformsForChildren(Matrix4x4::From2D(residual));
} else {
ComputeEffectiveTransformsForChildren(idealTransform);
}
ComputeEffectiveTransformForMaskLayers(aTransformToSurface);
}
void ContainerLayer::DefaultComputeSupportsComponentAlphaChildren(
bool* aNeedsSurfaceCopy) {
if (!(GetContentFlags() & Layer::CONTENT_COMPONENT_ALPHA_DESCENDANT) ||
!Manager()->AreComponentAlphaLayersEnabled()) {
mSupportsComponentAlphaChildren = false;
if (aNeedsSurfaceCopy) {
*aNeedsSurfaceCopy = false;
}
return;
}
mSupportsComponentAlphaChildren = false;
bool needsSurfaceCopy = false;
CompositionOp blendMode = GetEffectiveMixBlendMode();
if (UseIntermediateSurface()) {
if (GetLocalVisibleRegion().GetNumRects() == 1 &&
(GetContentFlags() & Layer::CONTENT_OPAQUE)) {
mSupportsComponentAlphaChildren = true;
} else {
gfx::Matrix transform;
if (HasOpaqueAncestorLayer(this) &&
GetEffectiveTransform().Is2D(&transform) &&
!gfx::ThebesMatrix(transform).HasNonIntegerTranslation() &&
blendMode == gfx::CompositionOp::OP_OVER) {
mSupportsComponentAlphaChildren = true;
needsSurfaceCopy = true;
}
}
} else if (blendMode == gfx::CompositionOp::OP_OVER) {
mSupportsComponentAlphaChildren =
(GetContentFlags() & Layer::CONTENT_OPAQUE) ||
(GetParent() && GetParent()->SupportsComponentAlphaChildren());
}
if (aNeedsSurfaceCopy) {
*aNeedsSurfaceCopy = mSupportsComponentAlphaChildren && needsSurfaceCopy;
}
}
void ContainerLayer::ComputeEffectiveTransformsForChildren(
const Matrix4x4& aTransformToSurface) {
for (Layer* l = mFirstChild; l; l = l->GetNextSibling()) {
l->ComputeEffectiveTransforms(aTransformToSurface);
}
}
/* static */
bool ContainerLayer::HasOpaqueAncestorLayer(Layer* aLayer) {
for (Layer* l = aLayer->GetParent(); l; l = l->GetParent()) {
if (l->GetContentFlags() & Layer::CONTENT_OPAQUE) return true;
}
return false;
}
// Note that ContainerLayer::RemoveAllChildren contains an optimized
// version of this code; if you make changes to ContainerLayer::DidRemoveChild
// consider whether the matching changes need to be made to
// ContainerLayer::RemoveAllChildren
void ContainerLayer::DidRemoveChild(Layer* aLayer) {
PaintedLayer* tl = aLayer->AsPaintedLayer();
if (tl && tl->UsedForReadback()) {
for (Layer* l = mFirstChild; l; l = l->GetNextSibling()) {
if (l->GetType() == TYPE_READBACK) {
static_cast<ReadbackLayer*>(l)->NotifyPaintedLayerRemoved(tl);
}
}
}
if (aLayer->GetType() == TYPE_READBACK) {
static_cast<ReadbackLayer*>(aLayer)->NotifyRemoved();
}
}
void ContainerLayer::DidInsertChild(Layer* aLayer) {
if (aLayer->GetType() == TYPE_READBACK) {
mMayHaveReadbackChild = true;
}
}
void RefLayer::FillSpecificAttributes(SpecificLayerAttributes& aAttrs) {
aAttrs = RefLayerAttributes(GetReferentId(), mEventRegionsOverride,
mRemoteDocumentSize);
}
/**
* StartFrameTimeRecording, together with StopFrameTimeRecording
* enable recording of frame intervals.
*
* To allow concurrent consumers, a cyclic array is used which serves all
* consumers, practically stateless with regard to consumers.
*
* To save resources, the buffer is allocated on first call to
* StartFrameTimeRecording and recording is paused if no consumer which called
* StartFrameTimeRecording is able to get valid results (because the cyclic
* buffer was overwritten since that call).
*
* To determine availability of the data upon StopFrameTimeRecording:
* - mRecording.mNextIndex increases on each RecordFrame, and never resets.
* - Cyclic buffer position is realized as mNextIndex % bufferSize.
* - StartFrameTimeRecording returns mNextIndex. When StopFrameTimeRecording is
* called, the required start index is passed as an arg, and we're able to
* calculate the required length. If this length is bigger than bufferSize, it
* means data was overwritten. otherwise, we can return the entire sequence.
* - To determine if we need to pause, mLatestStartIndex is updated to
* mNextIndex on each call to StartFrameTimeRecording. If this index gets
* overwritten, it means that all earlier start indices obtained via
* StartFrameTimeRecording were also overwritten, hence, no point in
* recording, so pause.
* - mCurrentRunStartIndex indicates the oldest index of the recording after
* which the recording was not paused. If StopFrameTimeRecording is invoked
* with a start index older than this, it means that some frames were not
* recorded, so data is invalid.
*/
uint32_t FrameRecorder::StartFrameTimeRecording(int32_t aBufferSize) {
if (mRecording.mIsPaused) {
mRecording.mIsPaused = false;
if (!mRecording.mIntervals.Length()) { // Initialize recording buffers
mRecording.mIntervals.SetLength(aBufferSize);
}
// After being paused, recent values got invalid. Update them to now.
mRecording.mLastFrameTime = TimeStamp::Now();
// Any recording which started before this is invalid, since we were paused.
mRecording.mCurrentRunStartIndex = mRecording.mNextIndex;
}
// If we'll overwrite this index, there are no more consumers with aStartIndex
// for which we're able to provide the full recording, so no point in keep
// recording.
mRecording.mLatestStartIndex = mRecording.mNextIndex;
return mRecording.mNextIndex;
}
void FrameRecorder::RecordFrame() {
if (!mRecording.mIsPaused) {
TimeStamp now = TimeStamp::Now();
uint32_t i = mRecording.mNextIndex % mRecording.mIntervals.Length();
mRecording.mIntervals[i] =
static_cast<float>((now - mRecording.mLastFrameTime).ToMilliseconds());
mRecording.mNextIndex++;
mRecording.mLastFrameTime = now;
if (mRecording.mNextIndex >
(mRecording.mLatestStartIndex + mRecording.mIntervals.Length())) {
// We've just overwritten the most recent recording start -> pause.
mRecording.mIsPaused = true;
}
}
}
void FrameRecorder::StopFrameTimeRecording(uint32_t aStartIndex,
nsTArray<float>& aFrameIntervals) {
uint32_t bufferSize = mRecording.mIntervals.Length();
uint32_t length = mRecording.mNextIndex - aStartIndex;
if (mRecording.mIsPaused || length > bufferSize ||
aStartIndex < mRecording.mCurrentRunStartIndex) {
// aStartIndex is too old. Also if aStartIndex was issued before
// mRecordingNextIndex overflowed (uint32_t)
// and stopped after the overflow (would happen once every 828 days of
// constant 60fps).
length = 0;
}
if (!length) {
aFrameIntervals.Clear();
return; // empty recording, return empty arrays.
}
// Set length in advance to avoid possibly repeated reallocations
aFrameIntervals.SetLength(length);
uint32_t cyclicPos = aStartIndex % bufferSize;
for (uint32_t i = 0; i < length; i++, cyclicPos++) {
if (cyclicPos == bufferSize) {
cyclicPos = 0;
}
aFrameIntervals[i] = mRecording.mIntervals[cyclicPos];
}
}
static void PrintInfo(std::stringstream& aStream, HostLayer* aLayerComposite);
#ifdef MOZ_DUMP_PAINTING
template <typename T>
void WriteSnapshotToDumpFile_internal(T* aObj, DataSourceSurface* aSurf) {
nsCString string(aObj->Name());
string.Append('-');
string.AppendInt((uint64_t)aObj);
if (gfxUtils::sDumpPaintFile != stderr) {
fprintf_stderr(gfxUtils::sDumpPaintFile, R"(array["%s"]=")",
string.BeginReading());
}
gfxUtils::DumpAsDataURI(aSurf, gfxUtils::sDumpPaintFile);
if (gfxUtils::sDumpPaintFile != stderr) {
fprintf_stderr(gfxUtils::sDumpPaintFile, R"(";)");
}
}
void WriteSnapshotToDumpFile(Layer* aLayer, DataSourceSurface* aSurf) {
WriteSnapshotToDumpFile_internal(aLayer, aSurf);
}
void WriteSnapshotToDumpFile(LayerManager* aManager, DataSourceSurface* aSurf) {
WriteSnapshotToDumpFile_internal(aManager, aSurf);
}
void WriteSnapshotToDumpFile(Compositor* aCompositor, DrawTarget* aTarget) {
RefPtr<SourceSurface> surf = aTarget->Snapshot();
RefPtr<DataSourceSurface> dSurf = surf->GetDataSurface();
WriteSnapshotToDumpFile_internal(aCompositor, dSurf);
}
#endif
void Layer::Dump(std::stringstream& aStream, const char* aPrefix,
bool aDumpHtml, bool aSorted,
const Maybe<gfx::Polygon>& aGeometry) {
#ifdef MOZ_DUMP_PAINTING
bool dumpCompositorTexture = gfxEnv::DumpCompositorTextures() &&
AsHostLayer() &&
AsHostLayer()->GetCompositableHost();
bool dumpClientTexture = gfxEnv::DumpPaint() && AsShadowableLayer() &&
AsShadowableLayer()->GetCompositableClient();
nsCString layerId(Name());
layerId.Append('-');
layerId.AppendInt((uint64_t)this);
#endif
if (aDumpHtml) {
aStream << nsPrintfCString(R"(<li><a id="%p" )", this).get();
#ifdef MOZ_DUMP_PAINTING
if (dumpCompositorTexture || dumpClientTexture) {
aStream << nsPrintfCString(R"lit(href="javascript:ViewImage('%s')")lit",
layerId.BeginReading())
.get();
}
#endif
aStream << ">";
}
DumpSelf(aStream, aPrefix, aGeometry);
#ifdef MOZ_DUMP_PAINTING
if (dumpCompositorTexture) {
AsHostLayer()->GetCompositableHost()->Dump(aStream, aPrefix, aDumpHtml);
} else if (dumpClientTexture) {
if (aDumpHtml) {
aStream << nsPrintfCString(R"(<script>array["%s"]=")",
layerId.BeginReading())
.get();
}
AsShadowableLayer()->GetCompositableClient()->Dump(
aStream, aPrefix, aDumpHtml, TextureDumpMode::DoNotCompress);
if (aDumpHtml) {
aStream << R"(";</script>)";
}
}
#endif
if (aDumpHtml) {
aStream << "</a>";
#ifdef MOZ_DUMP_PAINTING
if (dumpClientTexture) {
aStream << nsPrintfCString("<br><img id=\"%s\">\n",
layerId.BeginReading())
.get();
}
#endif
}
if (Layer* mask = GetMaskLayer()) {
aStream << nsPrintfCString("%s Mask layer:\n", aPrefix).get();
nsAutoCString pfx(aPrefix);
pfx += " ";
mask->Dump(aStream, pfx.get(), aDumpHtml);
}
for (size_t i = 0; i < GetAncestorMaskLayerCount(); i++) {
aStream << nsPrintfCString("%s Ancestor mask layer %d:\n", aPrefix,
uint32_t(i))
.get();
nsAutoCString pfx(aPrefix);
pfx += " ";
GetAncestorMaskLayerAt(i)->Dump(aStream, pfx.get(), aDumpHtml);
}
#ifdef MOZ_DUMP_PAINTING
for (size_t i = 0; i < mExtraDumpInfo.Length(); i++) {
const nsCString& str = mExtraDumpInfo[i];
aStream << aPrefix << " Info:\n" << str.get();
}
#endif
if (ContainerLayer* container = AsContainerLayer()) {
nsTArray<LayerPolygon> children;
if (aSorted) {
children = container->SortChildrenBy3DZOrder(
ContainerLayer::SortMode::WITH_GEOMETRY);
} else {
for (Layer* l = container->GetFirstChild(); l; l = l->GetNextSibling()) {
children.AppendElement(LayerPolygon(l));
}
}
nsAutoCString pfx(aPrefix);
pfx += " ";
if (aDumpHtml) {
aStream << "<ul>";
}
for (LayerPolygon& child : children) {
child.layer->Dump(aStream, pfx.get(), aDumpHtml, aSorted, child.geometry);
}
if (aDumpHtml) {
aStream << "</ul>";
}
}
if (aDumpHtml) {
aStream << "</li>";
}
}
static void DumpGeometry(std::stringstream& aStream,
const Maybe<gfx::Polygon>& aGeometry) {
aStream << " [geometry=[";
const nsTArray<gfx::Point4D>& points = aGeometry->GetPoints();
for (size_t i = 0; i < points.Length(); ++i) {
const gfx::IntPoint point = TruncatedToInt(points[i].As2DPoint());
aStream << point;
if (i != points.Length() - 1) {
aStream << ",";
}
}
aStream << "]]";
}
void Layer::DumpSelf(std::stringstream& aStream, const char* aPrefix,
const Maybe<gfx::Polygon>& aGeometry) {
PrintInfo(aStream, aPrefix);
if (aGeometry) {
DumpGeometry(aStream, aGeometry);
}
aStream << "\n";
}
void Layer::Dump(layerscope::LayersPacket* aPacket, const void* aParent) {
DumpPacket(aPacket, aParent);
if (Layer* kid = GetFirstChild()) {
kid->Dump(aPacket, this);
}
if (Layer* next = GetNextSibling()) {
next->Dump(aPacket, aParent);
}
}
void Layer::SetDisplayListLog(const char* log) {
if (gfxUtils::DumpDisplayList()) {
mDisplayListLog = log;
}
}
void Layer::GetDisplayListLog(nsCString& log) {
log.SetLength(0);
if (gfxUtils::DumpDisplayList()) {
// This function returns a plain text string which consists of two things
// 1. DisplayList log.
// 2. Memory address of this layer.
// We know the target layer of each display item by information in #1.
// Here is an example of a Text display item line log in #1
// Text p=0xa9850c00 f=0x0xaa405b00(.....
// f keeps the address of the target client layer of a display item.
// For LayerScope, display-item-to-client-layer mapping is not enough since
// LayerScope, which lives in the chrome process, knows only composite
// layers. As so, we need display-item-to-client-layer-to-layer-composite
// mapping. That's the reason we insert #2 into the log
log.AppendPrintf("0x%p\n%s", (void*)this, mDisplayListLog.get());
}
}
void Layer::Log(const char* aPrefix) {
if (!IsLogEnabled()) return;
LogSelf(aPrefix);
if (Layer* kid = GetFirstChild()) {
nsAutoCString pfx(aPrefix);
pfx += " ";
kid->Log(pfx.get());
}
if (Layer* next = GetNextSibling()) next->Log(aPrefix);
}
void Layer::LogSelf(const char* aPrefix) {
if (!IsLogEnabled()) return;
std::stringstream ss;
PrintInfo(ss, aPrefix);
MOZ_LAYERS_LOG(("%s", ss.str().c_str()));
if (mMaskLayer) {
nsAutoCString pfx(aPrefix);
pfx += R"( \ MaskLayer )";
mMaskLayer->LogSelf(pfx.get());
}
}
void Layer::PrintInfo(std::stringstream& aStream, const char* aPrefix) {
aStream << aPrefix;
aStream
<< nsPrintfCString("%s%s (0x%p)", mManager->Name(), Name(), this).get();
layers::PrintInfo(aStream, AsHostLayer());
if (mClipRect) {
aStream << " [clip=" << *mClipRect << "]";
}
if (mSimpleAttrs.GetScrolledClip()) {
aStream << " [scrolled-clip="
<< mSimpleAttrs.GetScrolledClip()->GetClipRect() << "]";
if (const Maybe<size_t>& ix =
mSimpleAttrs.GetScrolledClip()->GetMaskLayerIndex()) {
aStream << " [scrolled-mask=" << ix.value() << "]";
}
}
if (1.0 != mSimpleAttrs.GetPostXScale() ||
1.0 != mSimpleAttrs.GetPostYScale()) {
aStream << nsPrintfCString(" [postScale=%g, %g]",
mSimpleAttrs.GetPostXScale(),
mSimpleAttrs.GetPostYScale())
.get();
}
if (!GetBaseTransform().IsIdentity()) {
aStream << " [transform=" << GetBaseTransform() << "]";
}
if (!GetEffectiveTransform().IsIdentity()) {
aStream << " [effective-transform=" << GetEffectiveTransform() << "]";
}
if (GetTransformIsPerspective()) {
aStream << " [perspective]";
}
if (!mVisibleRegion.IsEmpty()) {
aStream << " [visible=" << mVisibleRegion << "]";
} else {
aStream << " [not visible]";
}
if (!mEventRegions.IsEmpty()) {
aStream << " " << mEventRegions;
}
if (1.0 != GetOpacity()) {
aStream << nsPrintfCString(" [opacity=%g]", GetOpacity()).get();
}
if (IsOpaque()) {
aStream << " [opaqueContent]";
}
if (GetContentFlags() & CONTENT_COMPONENT_ALPHA) {
aStream << " [componentAlpha]";
}
if (GetContentFlags() & CONTENT_BACKFACE_HIDDEN) {
aStream << " [backfaceHidden]";
}
if (Extend3DContext()) {
aStream << " [extend3DContext]";
}
if (Combines3DTransformWithAncestors()) {
aStream << " [combines3DTransformWithAncestors]";
}
if (Is3DContextLeaf()) {
aStream << " [is3DContextLeaf]";
}
if (Maybe<FrameMetrics::ViewID> viewId = IsAsyncZoomContainer()) {
aStream << nsPrintfCString(" [asyncZoomContainer scrollId=%" PRIu64 "]",
*viewId)
.get();
}
if (IsScrollbarContainer()) {
aStream << " [scrollbar]";
}
if (GetScrollbarData().IsThumb()) {
if (Maybe<ScrollDirection> thumbDirection = GetScrollbarData().mDirection) {
if (*thumbDirection == ScrollDirection::eVertical) {
aStream << nsPrintfCString(" [vscrollbar=%" PRIu64 "]",
GetScrollbarData().mTargetViewId)
.get();
}
if (*thumbDirection == ScrollDirection::eHorizontal) {
aStream << nsPrintfCString(" [hscrollbar=%" PRIu64 "]",
GetScrollbarData().mTargetViewId)
.get();
}
}
}
if (GetIsFixedPosition()) {
LayerPoint anchor = GetFixedPositionAnchor();
aStream << nsPrintfCString(
" [isFixedPosition scrollId=%" PRIu64
" sides=0x%x anchor=%s]",
GetFixedPositionScrollContainerId(),
static_cast<unsigned int>(GetFixedPositionSides()),
ToString(anchor).c_str())
.get();
}
if (GetIsStickyPosition()) {
aStream << nsPrintfCString(" [isStickyPosition scrollId=%" PRIu64
" outer=(%.3f,%.3f)-(%.3f,%.3f) "
"inner=(%.3f,%.3f)-(%.3f,%.3f)]",
GetStickyScrollContainerId(),
GetStickyScrollRangeOuter().X(),
GetStickyScrollRangeOuter().Y(),
GetStickyScrollRangeOuter().XMost(),
GetStickyScrollRangeOuter().YMost(),
GetStickyScrollRangeInner().X(),
GetStickyScrollRangeInner().Y(),
GetStickyScrollRangeInner().XMost(),
GetStickyScrollRangeInner().YMost())
.get();
}
if (mMaskLayer) {
aStream << nsPrintfCString(" [mMaskLayer=%p]", mMaskLayer.get()).get();
}
for (uint32_t i = 0; i < mScrollMetadata.Length(); i++) {
if (!mScrollMetadata[i].IsDefault()) {
aStream << " [metrics" << i << "=" << mScrollMetadata[i] << "]";
}
}
// FIXME: On the compositor thread, we don't set mAnimationInfo::mAnimations,
// All animations are transformed by AnimationHelper::ExtractAnimations() into
// mAnimationInfo.mPropertyAnimationGroups, instead. So if we want to check
// if layer trees are properly synced up across processes, we should dump
// mAnimationInfo.mPropertyAnimationGroups for the compositor thread.
// (See AnimationInfo.h for more details.)
if (!mAnimationInfo.GetAnimations().IsEmpty()) {
aStream << nsPrintfCString(" [%d animations with id=%" PRIu64 " ]",
(int)mAnimationInfo.GetAnimations().Length(),
mAnimationInfo.GetCompositorAnimationsId())
.get();
}
}
// The static helper function sets the transform matrix into the packet
static void DumpTransform(layerscope::LayersPacket::Layer::Matrix* aLayerMatrix,
const Matrix4x4& aMatrix) {
aLayerMatrix->set_is2d(aMatrix.Is2D());
if (aMatrix.Is2D()) {
Matrix m = aMatrix.As2D();
aLayerMatrix->set_isid(m.IsIdentity());
if (!m.IsIdentity()) {
aLayerMatrix->add_m(m._11);
aLayerMatrix->add_m(m._12);
aLayerMatrix->add_m(m._21);
aLayerMatrix->add_m(m._22);
aLayerMatrix->add_m(m._31);
aLayerMatrix->add_m(m._32);
}
} else {
aLayerMatrix->add_m(aMatrix._11);
aLayerMatrix->add_m(aMatrix._12);
aLayerMatrix->add_m(aMatrix._13);
aLayerMatrix->add_m(aMatrix._14);
aLayerMatrix->add_m(aMatrix._21);
aLayerMatrix->add_m(aMatrix._22);
aLayerMatrix->add_m(aMatrix._23);
aLayerMatrix->add_m(aMatrix._24);
aLayerMatrix->add_m(aMatrix._31);
aLayerMatrix->add_m(aMatrix._32);
aLayerMatrix->add_m(aMatrix._33);
aLayerMatrix->add_m(aMatrix._34);
aLayerMatrix->add_m(aMatrix._41);
aLayerMatrix->add_m(aMatrix._42);
aLayerMatrix->add_m(aMatrix._43);
aLayerMatrix->add_m(aMatrix._44);
}
}
// The static helper function sets the IntRect into the packet
template <typename T, typename Sub, typename Point, typename SizeT,
typename MarginT>
static void DumpRect(layerscope::LayersPacket::Layer::Rect* aLayerRect,
const BaseRect<T, Sub, Point, SizeT, MarginT>& aRect) {
aLayerRect->set_x(aRect.X());
aLayerRect->set_y(aRect.Y());
aLayerRect->set_w(aRect.Width());
aLayerRect->set_h(aRect.Height());
}
// The static helper function sets the nsIntRegion into the packet
static void DumpRegion(layerscope::LayersPacket::Layer::Region* aLayerRegion,
const nsIntRegion& aRegion) {
for (auto iter = aRegion.RectIter(); !iter.Done(); iter.Next()) {
DumpRect(aLayerRegion->add_r(), iter.Get());
}
}
void Layer::DumpPacket(layerscope::LayersPacket* aPacket, const void* aParent) {
// Add a new layer (UnknownLayer)
using namespace layerscope;
LayersPacket::Layer* layer = aPacket->add_layer();
// Basic information
layer->set_type(LayersPacket::Layer::UnknownLayer);
layer->set_ptr(reinterpret_cast<uint64_t>(this));
layer->set_parentptr(reinterpret_cast<uint64_t>(aParent));
// Shadow
if (HostLayer* lc = AsHostLayer()) {
LayersPacket::Layer::Shadow* s = layer->mutable_shadow();
if (const Maybe<ParentLayerIntRect>& clipRect = lc->GetShadowClipRect()) {
DumpRect(s->mutable_clip(), *clipRect);
}
if (!lc->GetShadowBaseTransform().IsIdentity()) {
DumpTransform(s->mutable_transform(), lc->GetShadowBaseTransform());
}
if (!lc->GetShadowVisibleRegion().IsEmpty()) {
DumpRegion(s->mutable_vregion(),
lc->GetShadowVisibleRegion().ToUnknownRegion());
}
}
// Clip
if (mClipRect) {
DumpRect(layer->mutable_clip(), *mClipRect);
}
// Transform
if (!GetBaseTransform().IsIdentity()) {
DumpTransform(layer->mutable_transform(), GetBaseTransform());
}
// Visible region
if (!mVisibleRegion.ToUnknownRegion().IsEmpty()) {
DumpRegion(layer->mutable_vregion(), mVisibleRegion.ToUnknownRegion());
}
// EventRegions
if (!mEventRegions.IsEmpty()) {
const EventRegions& e = mEventRegions;
if (!e.mHitRegion.IsEmpty()) {
DumpRegion(layer->mutable_hitregion(), e.mHitRegion);
}
if (!e.mDispatchToContentHitRegion.IsEmpty()) {
DumpRegion(layer->mutable_dispatchregion(),
e.mDispatchToContentHitRegion);
}
if (!e.mNoActionRegion.IsEmpty()) {
DumpRegion(layer->mutable_noactionregion(), e.mNoActionRegion);
}
if (!e.mHorizontalPanRegion.IsEmpty()) {
DumpRegion(layer->mutable_hpanregion(), e.mHorizontalPanRegion);
}
if (!e.mVerticalPanRegion.IsEmpty()) {
DumpRegion(layer->mutable_vpanregion(), e.mVerticalPanRegion);
}
}
// Opacity
layer->set_opacity(GetOpacity());
// Content opaque
layer->set_copaque(static_cast<bool>(GetContentFlags() & CONTENT_OPAQUE));
// Component alpha
layer->set_calpha(
static_cast<bool>(GetContentFlags() & CONTENT_COMPONENT_ALPHA));
// Vertical or horizontal bar
if (GetScrollbarData().mScrollbarLayerType ==
layers::ScrollbarLayerType::Thumb) {
layer->set_direct(*GetScrollbarData().mDirection ==
ScrollDirection::eVertical
? LayersPacket::Layer::VERTICAL
: LayersPacket::Layer::HORIZONTAL);
layer->set_barid(GetScrollbarData().mTargetViewId);
}
// Mask layer
if (mMaskLayer) {
layer->set_mask(reinterpret_cast<uint64_t>(mMaskLayer.get()));
}
// DisplayList log.
if (mDisplayListLog.Length() > 0) {
layer->set_displaylistloglength(mDisplayListLog.Length());
auto compressedData =
MakeUnique<char[]>(LZ4::maxCompressedSize(mDisplayListLog.Length()));
int compressedSize =
LZ4::compress((char*)mDisplayListLog.get(), mDisplayListLog.Length(),
compressedData.get());
layer->set_displaylistlog(compressedData.get(), compressedSize);
}
}
bool Layer::IsBackfaceHidden() {
if (GetContentFlags() & CONTENT_BACKFACE_HIDDEN) {
Layer* container = AsContainerLayer() ? this : GetParent();
if (container) {
// The effective transform can include non-preserve-3d parent
// transforms, since we don't always require an intermediate.
if (container->Extend3DContext() || container->Is3DContextLeaf()) {
return container->GetEffectiveTransform().IsBackfaceVisible();
}
return container->GetBaseTransform().IsBackfaceVisible();
}
}
return false;
}
UniquePtr<LayerUserData> Layer::RemoveUserData(void* aKey) {
UniquePtr<LayerUserData> d(static_cast<LayerUserData*>(
mUserData.Remove(static_cast<gfx::UserDataKey*>(aKey))));
return d;
}
void Layer::SetManager(LayerManager* aManager, HostLayer* aSelf) {
// No one should be calling this for weird reasons.
MOZ_ASSERT(aSelf);
MOZ_ASSERT(aSelf->GetLayer() == this);
mManager = aManager;
}
void PaintedLayer::PrintInfo(std::stringstream& aStream, const char* aPrefix) {
Layer::PrintInfo(aStream, aPrefix);
nsIntRegion validRegion = GetValidRegion();
if (!validRegion.IsEmpty()) {
aStream << " [valid=" << validRegion << "]";
}
}
void PaintedLayer::DumpPacket(layerscope::LayersPacket* aPacket,
const void* aParent) {
Layer::DumpPacket(aPacket, aParent);
// get this layer data
using namespace layerscope;
LayersPacket::Layer* layer =
aPacket->mutable_layer(aPacket->layer_size() - 1);
layer->set_type(LayersPacket::Layer::PaintedLayer);
nsIntRegion validRegion = GetValidRegion();
if (!validRegion.IsEmpty()) {
DumpRegion(layer->mutable_valid(), validRegion);
}
}
void ContainerLayer::PrintInfo(std::stringstream& aStream,
const char* aPrefix) {
Layer::PrintInfo(aStream, aPrefix);
if (UseIntermediateSurface()) {
aStream << " [usesTmpSurf]";
}
if (1.0 != mPreXScale || 1.0 != mPreYScale) {
aStream
<< nsPrintfCString(" [preScale=%g, %g]", mPreXScale, mPreYScale).get();
}
aStream << nsPrintfCString(" [presShellResolution=%g]", mPresShellResolution)
.get();
}
void ContainerLayer::DumpPacket(layerscope::LayersPacket* aPacket,
const void* aParent) {
Layer::DumpPacket(aPacket, aParent);
// Get this layer data
using namespace layerscope;
LayersPacket::Layer* layer =
aPacket->mutable_layer(aPacket->layer_size() - 1);
layer->set_type(LayersPacket::Layer::ContainerLayer);
}
void ColorLayer::PrintInfo(std::stringstream& aStream, const char* aPrefix) {
Layer::PrintInfo(aStream, aPrefix);
aStream << " [color=" << mColor << "] [bounds=" << mBounds << "]";
}
void ColorLayer::DumpPacket(layerscope::LayersPacket* aPacket,
const void* aParent) {
Layer::DumpPacket(aPacket, aParent);
// Get this layer data
using namespace layerscope;
LayersPacket::Layer* layer =
aPacket->mutable_layer(aPacket->layer_size() - 1);
layer->set_type(LayersPacket::Layer::ColorLayer);
layer->set_color(mColor.ToABGR());
}
CanvasLayer::CanvasLayer(LayerManager* aManager, void* aImplData)
: Layer(aManager, aImplData), mSamplingFilter(SamplingFilter::GOOD) {}
CanvasLayer::~CanvasLayer() = default;
void CanvasLayer::PrintInfo(std::stringstream& aStream, const char* aPrefix) {
Layer::PrintInfo(aStream, aPrefix);
if (mSamplingFilter != SamplingFilter::GOOD) {
aStream << " [filter=" << mSamplingFilter << "]";
}
}
// This help function is used to assign the correct enum value
// to the packet
static void DumpFilter(layerscope::LayersPacket::Layer* aLayer,
const SamplingFilter& aSamplingFilter) {
using namespace layerscope;
switch (aSamplingFilter) {
case SamplingFilter::GOOD:
aLayer->set_filter(LayersPacket::Layer::FILTER_GOOD);
break;
case SamplingFilter::LINEAR:
aLayer->set_filter(LayersPacket::Layer::FILTER_LINEAR);
break;
case SamplingFilter::POINT:
aLayer->set_filter(LayersPacket::Layer::FILTER_POINT);
break;
default:
// ignore it
break;
}
}
void CanvasLayer::DumpPacket(layerscope::LayersPacket* aPacket,
const void* aParent) {
Layer::DumpPacket(aPacket, aParent);
// Get this layer data
using namespace layerscope;
LayersPacket::Layer* layer =
aPacket->mutable_layer(aPacket->layer_size() - 1);
layer->set_type(LayersPacket::Layer::CanvasLayer);
DumpFilter(layer, mSamplingFilter);
}
RefPtr<CanvasRenderer> CanvasLayer::CreateOrGetCanvasRenderer() {
if (!mCanvasRenderer) {
mCanvasRenderer = CreateCanvasRendererInternal();
}
return mCanvasRenderer;
}
void ImageLayer::PrintInfo(std::stringstream& aStream, const char* aPrefix) {
Layer::PrintInfo(aStream, aPrefix);
if (mSamplingFilter != SamplingFilter::GOOD) {
aStream << " [filter=" << mSamplingFilter << "]";
}
}
void ImageLayer::DumpPacket(layerscope::LayersPacket* aPacket,
const void* aParent) {
Layer::DumpPacket(aPacket, aParent);
// Get this layer data
using namespace layerscope;
LayersPacket::Layer* layer =
aPacket->mutable_layer(aPacket->layer_size() - 1);
layer->set_type(LayersPacket::Layer::ImageLayer);
DumpFilter(layer, mSamplingFilter);
}
void RefLayer::PrintInfo(std::stringstream& aStream, const char* aPrefix) {
ContainerLayer::PrintInfo(aStream, aPrefix);
if (mId.IsValid()) {
aStream << " [id=" << mId << "]";
}
if (mEventRegionsOverride & EventRegionsOverride::ForceDispatchToContent) {
aStream << " [force-dtc]";
}
if (mEventRegionsOverride & EventRegionsOverride::ForceEmptyHitRegion) {
aStream << " [force-ehr]";
}
}
void RefLayer::DumpPacket(layerscope::LayersPacket* aPacket,
const void* aParent) {
Layer::DumpPacket(aPacket, aParent);
// Get this layer data
using namespace layerscope;
LayersPacket::Layer* layer =
aPacket->mutable_layer(aPacket->layer_size() - 1);
layer->set_type(LayersPacket::Layer::RefLayer);
layer->set_refid(uint64_t(mId));
}
void ReadbackLayer::PrintInfo(std::stringstream& aStream, const char* aPrefix) {
Layer::PrintInfo(aStream, aPrefix);
aStream << " [size=" << mSize << "]";
if (mBackgroundLayer) {
aStream << " [backgroundLayer="
<< nsPrintfCString("%p", mBackgroundLayer).get() << "]";
aStream << " [backgroundOffset=" << mBackgroundLayerOffset << "]";
} else if (mBackgroundColor.a == 1.f) {
aStream << " [backgroundColor=" << mBackgroundColor << "]";
} else {
aStream << " [nobackground]";
}
}
void ReadbackLayer::DumpPacket(layerscope::LayersPacket* aPacket,
const void* aParent) {
Layer::DumpPacket(aPacket, aParent);
// Get this layer data
using namespace layerscope;
LayersPacket::Layer* layer =
aPacket->mutable_layer(aPacket->layer_size() - 1);
layer->set_type(LayersPacket::Layer::ReadbackLayer);
LayersPacket::Layer::Size* size = layer->mutable_size();
size->set_w(mSize.width);
size->set_h(mSize.height);
}
//--------------------------------------------------
// LayerManager
void LayerManager::Dump(std::stringstream& aStream, const char* aPrefix,
bool aDumpHtml, bool aSorted) {
#ifdef MOZ_DUMP_PAINTING
if (aDumpHtml) {
aStream << "<ul><li>";
}
#endif
DumpSelf(aStream, aPrefix, aSorted);
nsAutoCString pfx(aPrefix);
pfx += " ";
if (!GetRoot()) {
aStream << nsPrintfCString("%s(null)\n", pfx.get()).get();
if (aDumpHtml) {
aStream << "</li></ul>";
}
return;
}
if (aDumpHtml) {
aStream << "<ul>";
}
GetRoot()->Dump(aStream, pfx.get(), aDumpHtml, aSorted);
if (aDumpHtml) {
aStream << "</ul></li></ul>";
}
aStream << "\n";
}
void LayerManager::DumpSelf(std::stringstream& aStream, const char* aPrefix,
bool aSorted) {
PrintInfo(aStream, aPrefix);
aStream << " --- in "
<< (aSorted ? "3D-sorted rendering order" : "content order");
aStream << "\n";
}
void LayerManager::Dump(bool aSorted) {
std::stringstream ss;
Dump(ss, "", false, aSorted);
print_stderr(ss);
}
void LayerManager::Dump(layerscope::LayersPacket* aPacket) {
DumpPacket(aPacket);
if (GetRoot()) {
GetRoot()->Dump(aPacket, this);
}
}
void LayerManager::Log(const char* aPrefix) {
if (!IsLogEnabled()) return;
LogSelf(aPrefix);
nsAutoCString pfx(aPrefix);
pfx += " ";
if (!GetRoot()) {
MOZ_LAYERS_LOG(("%s(null)", pfx.get()));
return;
}
GetRoot()->Log(pfx.get());
}
void LayerManager::LogSelf(const char* aPrefix) {
nsAutoCString str;
std::stringstream ss;
PrintInfo(ss, aPrefix);
MOZ_LAYERS_LOG(("%s", ss.str().c_str()));
}
void LayerManager::PrintInfo(std::stringstream& aStream, const char* aPrefix) {
aStream << aPrefix
<< nsPrintfCString("%sLayerManager (0x%p)", Name(), this).get();
}
void LayerManager::DumpPacket(layerscope::LayersPacket* aPacket) {
using namespace layerscope;
// Add a new layer data (LayerManager)
LayersPacket::Layer* layer = aPacket->add_layer();
layer->set_type(LayersPacket::Layer::LayerManager);
layer->set_ptr(reinterpret_cast<uint64_t>(this));
// Layer Tree Root
layer->set_parentptr(0);
}
/*static*/
bool LayerManager::IsLogEnabled() {
return MOZ_LOG_TEST(GetLog(), LogLevel::Debug);
}
bool LayerManager::AddPendingScrollUpdateForNextTransaction(
ScrollableLayerGuid::ViewID aScrollId,
const ScrollPositionUpdate& aUpdateInfo) {
Layer* withPendingTransform = DepthFirstSearch<ForwardIterator>(
GetRoot(), [](Layer* aLayer) { return aLayer->HasPendingTransform(); });
if (withPendingTransform) {
return false;
}
mPendingScrollUpdates.GetOrInsert(aScrollId).AppendElement(aUpdateInfo);
return true;
}
Maybe<nsTArray<ScrollPositionUpdate>> LayerManager::GetPendingScrollInfoUpdate(
ScrollableLayerGuid::ViewID aScrollId) {
auto p = mPendingScrollUpdates.Lookup(aScrollId);
if (!p) {
return Nothing();
}
// We could have this function return a CopyableTArray or something, but it
// seems better to avoid implicit copies and just do the one explicit copy
// where we need it, here.
nsTArray<ScrollPositionUpdate> copy;
copy.AppendElements(p.Data());
return Some(std::move(copy));
}
std::unordered_set<ScrollableLayerGuid::ViewID>
LayerManager::ClearPendingScrollInfoUpdate() {
std::unordered_set<ScrollableLayerGuid::ViewID> scrollIds;
for (auto it = mPendingScrollUpdates.Iter(); !it.Done(); it.Next()) {
scrollIds.insert(it.Key());
}
mPendingScrollUpdates.Clear();
return scrollIds;
}
void PrintInfo(std::stringstream& aStream, HostLayer* aLayerComposite) {
if (!aLayerComposite) {
return;
}
if (const Maybe<ParentLayerIntRect>& clipRect =
aLayerComposite->GetShadowClipRect()) {
aStream << " [shadow-clip=" << *clipRect << "]";
}
if (!aLayerComposite->GetShadowBaseTransform().IsIdentity()) {
aStream << " [shadow-transform="
<< aLayerComposite->GetShadowBaseTransform() << "]";
}
if (!aLayerComposite->GetLayer()->Extend3DContext() &&
!aLayerComposite->GetShadowVisibleRegion().IsEmpty()) {
aStream << " [shadow-visible=" << aLayerComposite->GetShadowVisibleRegion()
<< "]";
}
}
void SetAntialiasingFlags(Layer* aLayer, DrawTarget* aTarget) {
bool permitSubpixelAA =
!(aLayer->GetContentFlags() & Layer::CONTENT_DISABLE_SUBPIXEL_AA);
if (aTarget->IsCurrentGroupOpaque()) {
aTarget->SetPermitSubpixelAA(permitSubpixelAA);
return;
}
const IntRect& bounds =
aLayer->GetVisibleRegion().GetBounds().ToUnknownRect();
gfx::Rect transformedBounds = aTarget->GetTransform().TransformBounds(
gfx::Rect(Float(bounds.X()), Float(bounds.Y()), Float(bounds.Width()),
Float(bounds.Height())));
transformedBounds.RoundOut();
IntRect intTransformedBounds;
transformedBounds.ToIntRect(&intTransformedBounds);
permitSubpixelAA &=
!(aLayer->GetContentFlags() & Layer::CONTENT_COMPONENT_ALPHA) ||
aTarget->GetOpaqueRect().Contains(intTransformedBounds);
aTarget->SetPermitSubpixelAA(permitSubpixelAA);
}
IntRect ToOutsideIntRect(const gfxRect& aRect) {
return IntRect::RoundOut(aRect.X(), aRect.Y(), aRect.Width(), aRect.Height());
}
void RecordCompositionPayloadsPresented(
const TimeStamp& aCompositionEndTime,
const nsTArray<CompositionPayload>& aPayloads) {
if (aPayloads.Length()) {
TimeStamp presented = aCompositionEndTime;
for (const CompositionPayload& payload : aPayloads) {
#if MOZ_GECKO_PROFILER
if (profiler_can_accept_markers()) {
MOZ_RELEASE_ASSERT(payload.mType <= kHighestCompositionPayloadType);
nsAutoCString name(
kCompositionPayloadTypeNames[uint8_t(payload.mType)]);
name.AppendLiteral(" Payload Presented");
// This doesn't really need to be a text marker. Once we have a version
// of profiler_add_marker that accepts both a start time and an end
// time, we could use that here.
nsPrintfCString text(
"Latency: %dms",
int32_t((presented - payload.mTimeStamp).ToMilliseconds()));
PROFILER_MARKER_TEXT(
name, GRAPHICS,
MarkerTiming::Interval(payload.mTimeStamp, presented), text);
}
#endif
if (payload.mType == CompositionPayloadType::eKeyPress) {
Telemetry::AccumulateTimeDelta(
mozilla::Telemetry::KEYPRESS_PRESENT_LATENCY, payload.mTimeStamp,
presented);
} else if (payload.mType == CompositionPayloadType::eAPZScroll) {
Telemetry::AccumulateTimeDelta(
mozilla::Telemetry::SCROLL_PRESENT_LATENCY, payload.mTimeStamp,
presented);
}
}
}
}
} // namespace layers
} // namespace mozilla
|