summaryrefslogtreecommitdiffstats
path: root/gfx/2d/DrawTargetD2D1.cpp
blob: ca361a94945a975d1e469977ab5b4a564f801734 (plain)
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
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
/* -*- 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 <optional>

#include <initguid.h>
#include "DrawTargetD2D1.h"
#include "FilterNodeSoftware.h"
#include "GradientStopsD2D.h"
#include "SourceSurfaceD2D1.h"
#include "ConicGradientEffectD2D1.h"
#include "RadialGradientEffectD2D1.h"

#include "HelpersD2D.h"
#include "FilterNodeD2D1.h"
#include "ExtendInputEffectD2D1.h"
#include "nsAppRunner.h"
#include "MainThreadUtils.h"

#include "mozilla/Mutex.h"

// decltype is not usable for overloaded functions.
typedef HRESULT(WINAPI* D2D1CreateFactoryFunc)(
    D2D1_FACTORY_TYPE factoryType, REFIID iid,
    CONST D2D1_FACTORY_OPTIONS* pFactoryOptions, void** factory);

namespace mozilla {
namespace gfx {

uint64_t DrawTargetD2D1::mVRAMUsageDT;
uint64_t DrawTargetD2D1::mVRAMUsageSS;
StaticRefPtr<ID2D1Factory1> DrawTargetD2D1::mFactory;

const D2D1_MATRIX_5X4_F kLuminanceMatrix =
    D2D1::Matrix5x4F(0, 0, 0, 0.2125f, 0, 0, 0, 0.7154f, 0, 0, 0, 0.0721f, 0, 0,
                     0, 0, 0, 0, 0, 0);

RefPtr<ID2D1Factory1> D2DFactory() { return DrawTargetD2D1::factory(); }

DrawTargetD2D1::DrawTargetD2D1()
    : mPushedLayers(1),
      mSnapshotLock(std::make_shared<Mutex>("DrawTargetD2D1::mSnapshotLock")),
      mUsedCommandListsSincePurge(0),
      mTransformedGlyphsSinceLastPurge(0),
      mComplexBlendsWithListInList(0),
      mDeviceSeq(0),
      mInitState(InitState::Uninitialized) {}

DrawTargetD2D1::~DrawTargetD2D1() {
  PopAllClips();

  if (mSnapshot) {
    MutexAutoLock lock(*mSnapshotLock);
    // We may hold the only reference. MarkIndependent will clear mSnapshot;
    // keep the snapshot object alive so it doesn't get destroyed while
    // MarkIndependent is running.
    RefPtr<SourceSurfaceD2D1> deathGrip = mSnapshot;
    // mSnapshot can be treated as independent of this DrawTarget since we know
    // this DrawTarget won't change again.
    deathGrip->MarkIndependent();
    // mSnapshot will be cleared now.
  }

  if (mDC && IsDeviceContextValid()) {
    // The only way mDC can be null is if Init failed, but it can happen and the
    // destructor is the only place where we need to check for it since the
    // DrawTarget will destroyed right after Init fails.
    mDC->EndDraw();
  }

  {
    // Until this point in the destructor it -must- still be valid for
    // FlushInternal to be called on this.
    StaticMutexAutoLock lock(Factory::mDTDependencyLock);
    // Targets depending on us can break that dependency, since we're obviously
    // not going to be modified in the future.
    for (auto iter = mDependentTargets.begin(); iter != mDependentTargets.end();
         iter++) {
      (*iter)->mDependingOnTargets.erase(this);
    }
    // Our dependencies on other targets no longer matter.
    for (TargetSet::iterator iter = mDependingOnTargets.begin();
         iter != mDependingOnTargets.end(); iter++) {
      (*iter)->mDependentTargets.erase(this);
    }
  }
}

bool DrawTargetD2D1::IsValid() const {
  if (mInitState != InitState::Uninitialized && !IsDeviceContextValid()) {
    return false;
  }
  if (NS_IsMainThread()) {
    // Uninitialized DTs are considered valid.
    return mInitState != InitState::Failure;
  } else {
    return const_cast<DrawTargetD2D1*>(this)->EnsureInitialized();
  }
}

already_AddRefed<SourceSurface> DrawTargetD2D1::Snapshot() {
  if (!EnsureInitialized()) {
    return nullptr;
  }

  MutexAutoLock lock(*mSnapshotLock);
  if (mSnapshot) {
    RefPtr<SourceSurface> snapshot(mSnapshot);
    return snapshot.forget();
  }
  PopAllClips();

  Flush();

  mSnapshot = new SourceSurfaceD2D1(mBitmap, mDC, mFormat, mSize, this);

  RefPtr<SourceSurface> snapshot(mSnapshot);
  return snapshot.forget();
}

bool DrawTargetD2D1::EnsureLuminanceEffect() {
  if (mLuminanceEffect.get()) {
    return true;
  }

  HRESULT hr = mDC->CreateEffect(CLSID_D2D1ColorMatrix,
                                 getter_AddRefs(mLuminanceEffect));
  if (FAILED(hr)) {
    gfxCriticalError() << "Failed to create luminance effect. Code: "
                       << hexa(hr);
    return false;
  }

  mLuminanceEffect->SetValue(D2D1_COLORMATRIX_PROP_COLOR_MATRIX,
                             kLuminanceMatrix);
  mLuminanceEffect->SetValue(D2D1_COLORMATRIX_PROP_ALPHA_MODE,
                             D2D1_COLORMATRIX_ALPHA_MODE_STRAIGHT);
  return true;
}

already_AddRefed<SourceSurface> DrawTargetD2D1::IntoLuminanceSource(
    LuminanceType aLuminanceType, float aOpacity) {
  if (!EnsureInitialized()) {
    return nullptr;
  }
  if ((aLuminanceType != LuminanceType::LUMINANCE) ||
      // See bug 1372577, some race condition where we get invalid
      // results with D2D in the parent process. Fallback in that case.
      XRE_IsParentProcess()) {
    return DrawTarget::IntoLuminanceSource(aLuminanceType, aOpacity);
  }

  // Create the luminance effect
  if (!EnsureLuminanceEffect()) {
    return DrawTarget::IntoLuminanceSource(aLuminanceType, aOpacity);
  }

  Flush();

  {
    D2D1_MATRIX_5X4_F matrix = kLuminanceMatrix;
    matrix._14 *= aOpacity;
    matrix._24 *= aOpacity;
    matrix._34 *= aOpacity;

    mLuminanceEffect->SetValue(D2D1_COLORMATRIX_PROP_COLOR_MATRIX, matrix);
  }

  mLuminanceEffect->SetInput(0, mBitmap);

  RefPtr<ID2D1Image> luminanceOutput;
  mLuminanceEffect->GetOutput(getter_AddRefs(luminanceOutput));

  return MakeAndAddRef<SourceSurfaceD2D1>(luminanceOutput, mDC,
                                          SurfaceFormat::B8G8R8A8, mSize);
}

// Command lists are kept around by device contexts until EndDraw is called,
// this can cause issues with memory usage (see bug 1238328). EndDraw/BeginDraw
// are expensive though, especially relatively when little work is done, so
// we try to reduce the amount of times we execute these purges.
static const uint32_t kPushedLayersBeforePurge = 25;
// Rendering glyphs with different transforms causes the glyph cache to grow
// very large (see bug 1474883) so we must call EndDraw every so often.
static const uint32_t kTransformedGlyphsBeforePurge = 1000;

void DrawTargetD2D1::Flush() { FlushInternal(); }

void DrawTargetD2D1::DrawSurface(SourceSurface* aSurface, const Rect& aDest,
                                 const Rect& aSource,
                                 const DrawSurfaceOptions& aSurfOptions,
                                 const DrawOptions& aOptions) {
  if (!PrepareForDrawing(aOptions.mCompositionOp,
                         ColorPattern(DeviceColor()))) {
    return;
  }

  Rect source = aSource - aSurface->GetRect().TopLeft();

  D2D1_RECT_F samplingBounds;

  if (aSurfOptions.mSamplingBounds == SamplingBounds::BOUNDED) {
    samplingBounds = D2DRect(source);
  } else {
    samplingBounds = D2D1::RectF(0, 0, Float(aSurface->GetSize().width),
                                 Float(aSurface->GetSize().height));
  }

  Float xScale = aDest.Width() / source.Width();
  Float yScale = aDest.Height() / source.Height();

  RefPtr<ID2D1ImageBrush> brush;

  // Here we scale the source pattern up to the size and position where we want
  // it to be.
  Matrix transform;
  transform.PreTranslate(aDest.X() - source.X() * xScale,
                         aDest.Y() - source.Y() * yScale);
  transform.PreScale(xScale, yScale);

  RefPtr<ID2D1Image> image =
      GetImageForSurface(aSurface, transform, ExtendMode::CLAMP);

  if (!image) {
    gfxWarning() << *this << ": Unable to get D2D image for surface.";
    return;
  }

  RefPtr<ID2D1Bitmap> bitmap;
  HRESULT hr = E_FAIL;
  if (aSurface->GetType() == SurfaceType::D2D1_1_IMAGE) {
    // If this is called with a DataSourceSurface it might do a partial upload
    // that our DrawBitmap call doesn't support.
    hr = image->QueryInterface((ID2D1Bitmap**)getter_AddRefs(bitmap));
  }

  if (SUCCEEDED(hr) && bitmap &&
      aSurfOptions.mSamplingBounds == SamplingBounds::UNBOUNDED) {
    mDC->DrawBitmap(bitmap, D2DRect(aDest), aOptions.mAlpha,
                    D2DFilter(aSurfOptions.mSamplingFilter), D2DRect(source));
  } else {
    // This has issues ignoring the alpha channel on windows 7 with images
    // marked opaque.
    MOZ_ASSERT(aSurface->GetFormat() != SurfaceFormat::B8G8R8X8);

    // Bug 1275478 - D2D1 cannot draw A8 surface correctly.
    MOZ_ASSERT(aSurface->GetFormat() != SurfaceFormat::A8);

    mDC->CreateImageBrush(
        image,
        D2D1::ImageBrushProperties(
            samplingBounds, D2D1_EXTEND_MODE_CLAMP, D2D1_EXTEND_MODE_CLAMP,
            D2DInterpolationMode(aSurfOptions.mSamplingFilter)),
        D2D1::BrushProperties(aOptions.mAlpha, D2DMatrix(transform)),
        getter_AddRefs(brush));
    mDC->FillRectangle(D2DRect(aDest), brush);
  }

  FinalizeDrawing(aOptions.mCompositionOp, ColorPattern(DeviceColor()));
}

void DrawTargetD2D1::DrawFilter(FilterNode* aNode, const Rect& aSourceRect,
                                const Point& aDestPoint,
                                const DrawOptions& aOptions) {
  if (aNode->GetBackendType() != FILTER_BACKEND_DIRECT2D1_1) {
    gfxWarning() << *this << ": Incompatible filter passed to DrawFilter.";
    return;
  }

  if (!PrepareForDrawing(aOptions.mCompositionOp,
                         ColorPattern(DeviceColor()))) {
    return;
  }

  mDC->SetAntialiasMode(D2DAAMode(aOptions.mAntialiasMode));

  FilterNodeD2D1* node = static_cast<FilterNodeD2D1*>(aNode);
  node->WillDraw(this);

  if (aOptions.mAlpha == 1.0f) {
    mDC->DrawImage(node->OutputEffect(), D2DPoint(aDestPoint),
                   D2DRect(aSourceRect));
  } else {
    RefPtr<ID2D1Image> image;
    node->OutputEffect()->GetOutput(getter_AddRefs(image));

    Matrix mat = Matrix::Translation(aDestPoint);

    RefPtr<ID2D1ImageBrush> imageBrush;
    mDC->CreateImageBrush(
        image, D2D1::ImageBrushProperties(D2DRect(aSourceRect)),
        D2D1::BrushProperties(aOptions.mAlpha, D2DMatrix(mat)),
        getter_AddRefs(imageBrush));
    mDC->FillRectangle(D2D1::RectF(aDestPoint.x, aDestPoint.y,
                                   aDestPoint.x + aSourceRect.width,
                                   aDestPoint.y + aSourceRect.height),
                       imageBrush);
  }

  FinalizeDrawing(aOptions.mCompositionOp, ColorPattern(DeviceColor()));
}

void DrawTargetD2D1::DrawSurfaceWithShadow(SourceSurface* aSurface,
                                           const Point& aDest,
                                           const ShadowOptions& aShadow,
                                           CompositionOp aOperator) {
  if (!EnsureInitialized()) {
    return;
  }
  MarkChanged();

  Matrix mat;
  RefPtr<ID2D1Image> image =
      GetImageForSurface(aSurface, mat, ExtendMode::CLAMP, nullptr, false);

  if (!image) {
    gfxWarning() << "Couldn't get image for surface.";
    return;
  }

  if (!mat.IsIdentity()) {
    gfxDebug() << *this
               << ": At this point complex partial uploads are not supported "
                  "for Shadow surfaces.";
    return;
  }

  if (!PrepareForDrawing(aOperator, ColorPattern(aShadow.mColor))) {
    return;
  }

  mDC->SetTransform(D2D1::IdentityMatrix());
  mTransformDirty = true;

  RefPtr<ID2D1Effect> shadowEffect;
  HRESULT hr = mDC->CreateEffect(
      mFormat == SurfaceFormat::A8 ? CLSID_D2D1GaussianBlur : CLSID_D2D1Shadow,
      getter_AddRefs(shadowEffect));
  if (SUCCEEDED(hr) && shadowEffect) {
    shadowEffect->SetInput(0, image);
    if (mFormat == SurfaceFormat::A8) {
      shadowEffect->SetValue(D2D1_GAUSSIANBLUR_PROP_STANDARD_DEVIATION,
                             aShadow.mSigma);
      shadowEffect->SetValue(D2D1_GAUSSIANBLUR_PROP_BORDER_MODE,
                             D2D1_BORDER_MODE_HARD);
    } else {
      shadowEffect->SetValue(D2D1_SHADOW_PROP_BLUR_STANDARD_DEVIATION,
                             aShadow.mSigma);
      D2D1_VECTOR_4F color = {aShadow.mColor.r, aShadow.mColor.g,
                              aShadow.mColor.b, aShadow.mColor.a};
      shadowEffect->SetValue(D2D1_SHADOW_PROP_COLOR, color);
    }

    D2D1_POINT_2F shadowPoint = D2DPoint(aDest + aShadow.mOffset);
    mDC->DrawImage(shadowEffect, &shadowPoint, nullptr,
                   D2D1_INTERPOLATION_MODE_LINEAR,
                   D2D1_COMPOSITE_MODE_SOURCE_OVER);
  } else {
    gfxWarning() << "Failed to create shadow effect. Code: " << hexa(hr);
  }

  if (aSurface->GetFormat() != SurfaceFormat::A8) {
    D2D1_POINT_2F imgPoint = D2DPoint(aDest);
    mDC->DrawImage(image, &imgPoint, nullptr, D2D1_INTERPOLATION_MODE_LINEAR,
                   D2D1_COMPOSITE_MODE_SOURCE_OVER);
  }

  FinalizeDrawing(aOperator, ColorPattern(aShadow.mColor));
}

void DrawTargetD2D1::ClearRect(const Rect& aRect) {
  if (!EnsureInitialized()) {
    return;
  }

  if (aRect.IsEmpty()) {
    // Nothing to be done.
    return;
  }

  MarkChanged();

  PopAllClips();

  PushClipRect(aRect);

  if (mTransformDirty || !mTransform.IsIdentity()) {
    mDC->SetTransform(D2D1::IdentityMatrix());
    mTransformDirty = true;
  }

  D2D1_RECT_F clipRect;
  bool isPixelAligned;
  if (mTransform.IsRectilinear() &&
      GetDeviceSpaceClipRect(clipRect, isPixelAligned)) {
    mDC->PushAxisAlignedClip(clipRect, isPixelAligned
                                           ? D2D1_ANTIALIAS_MODE_ALIASED
                                           : D2D1_ANTIALIAS_MODE_PER_PRIMITIVE);
    mDC->Clear();
    mDC->PopAxisAlignedClip();

    PopClip();
    return;
  }

  RefPtr<ID2D1CommandList> list;
  mUsedCommandListsSincePurge++;
  mDC->CreateCommandList(getter_AddRefs(list));
  mDC->SetTarget(list);

  IntRect addClipRect;
  RefPtr<ID2D1Geometry> geom = GetClippedGeometry(&addClipRect);

  RefPtr<ID2D1SolidColorBrush> brush;
  mDC->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::White),
                             getter_AddRefs(brush));
  mDC->PushAxisAlignedClip(
      D2D1::RectF(addClipRect.X(), addClipRect.Y(), addClipRect.XMost(),
                  addClipRect.YMost()),
      D2D1_ANTIALIAS_MODE_PER_PRIMITIVE);
  mDC->FillGeometry(geom, brush);
  mDC->PopAxisAlignedClip();

  mDC->SetTarget(CurrentTarget());
  list->Close();

  mDC->DrawImage(list, D2D1_INTERPOLATION_MODE_NEAREST_NEIGHBOR,
                 D2D1_COMPOSITE_MODE_DESTINATION_OUT);

  PopClip();

  return;
}

void DrawTargetD2D1::MaskSurface(const Pattern& aSource, SourceSurface* aMask,
                                 Point aOffset, const DrawOptions& aOptions) {
  if (!EnsureInitialized()) {
    return;
  }
  MarkChanged();

  RefPtr<ID2D1Bitmap> bitmap;

  Matrix mat = Matrix::Translation(aOffset);
  RefPtr<ID2D1Image> image =
      GetImageForSurface(aMask, mat, ExtendMode::CLAMP, nullptr);

  MOZ_ASSERT(!mat.HasNonTranslation());
  aOffset.x = mat._31;
  aOffset.y = mat._32;

  if (!image) {
    gfxWarning() << "Failed to get image for surface.";
    return;
  }

  if (!PrepareForDrawing(aOptions.mCompositionOp, aSource)) {
    return;
  }

  IntSize size =
      IntSize::Truncate(aMask->GetSize().width, aMask->GetSize().height);
  Rect dest =
      Rect(aOffset.x + aMask->GetRect().x, aOffset.y + aMask->GetRect().y,
           Float(size.width), Float(size.height));

  HRESULT hr = image->QueryInterface((ID2D1Bitmap**)getter_AddRefs(bitmap));
  if (!bitmap || FAILED(hr)) {
    // D2D says if we have an actual ID2D1Image and not a bitmap underlying the
    // object, we can't query for a bitmap. Instead, Push/PopLayer
    gfxWarning() << "FillOpacityMask only works with Bitmap source surfaces. "
                    "Falling back to push/pop layer";

    RefPtr<ID2D1Brush> source = CreateBrushForPattern(aSource, aOptions.mAlpha);
    RefPtr<ID2D1ImageBrush> maskBrush;
    hr = mDC->CreateImageBrush(
        image,
        D2D1::ImageBrushProperties(D2D1::RectF(0, 0, size.width, size.height)),
        D2D1::BrushProperties(
            1.0f, D2D1::Matrix3x2F::Translation(aMask->GetRect().x,
                                                aMask->GetRect().y)),
        getter_AddRefs(maskBrush));
    MOZ_ASSERT(SUCCEEDED(hr));

    mDC->PushLayer(
        D2D1::LayerParameters1(D2D1::InfiniteRect(), nullptr,
                               D2D1_ANTIALIAS_MODE_PER_PRIMITIVE,
                               D2D1::Matrix3x2F::Translation(
                                   aMask->GetRect().x, aMask->GetRect().y),
                               1.0f, maskBrush, D2D1_LAYER_OPTIONS1_NONE),
        nullptr);

    mDC->FillRectangle(D2DRect(dest), source);
    mDC->PopLayer();

    FinalizeDrawing(aOptions.mCompositionOp, aSource);
    return;
  } else {
    // If this is a data source surface, we might have created a partial bitmap
    // for this surface and only uploaded part of the mask. In that case,
    // we have to fixup our sizes here.
    size.width = bitmap->GetSize().width;
    size.height = bitmap->GetSize().height;
    dest.SetWidth(size.width);
    dest.SetHeight(size.height);
  }

  // FillOpacityMask only works if the antialias mode is MODE_ALIASED
  mDC->SetAntialiasMode(D2D1_ANTIALIAS_MODE_ALIASED);

  Rect maskRect = Rect(aMask->GetRect().x, aMask->GetRect().y,
                       Float(size.width), Float(size.height));
  RefPtr<ID2D1Brush> brush = CreateBrushForPattern(aSource, aOptions.mAlpha);
  mDC->FillOpacityMask(bitmap, brush, D2D1_OPACITY_MASK_CONTENT_GRAPHICS,
                       D2DRect(dest), D2DRect(maskRect));

  mDC->SetAntialiasMode(D2D1_ANTIALIAS_MODE_PER_PRIMITIVE);

  FinalizeDrawing(aOptions.mCompositionOp, aSource);
}

void DrawTargetD2D1::CopySurface(SourceSurface* aSurface,
                                 const IntRect& aSourceRect,
                                 const IntPoint& aDestination) {
  if (!EnsureInitialized()) {
    return;
  }
  MarkChanged();

  PopAllClips();

  mDC->SetTransform(D2D1::IdentityMatrix());
  mTransformDirty = true;

  Matrix mat = Matrix::Translation(aDestination.x - aSourceRect.X(),
                                   aDestination.y - aSourceRect.Y());
  RefPtr<ID2D1Image> image =
      GetImageForSurface(aSurface, mat, ExtendMode::CLAMP, nullptr, false);

  if (!image) {
    gfxWarning() << "Couldn't get image for surface.";
    return;
  }

  if (mat.HasNonIntegerTranslation()) {
    gfxDebug() << *this
               << ": At this point scaled partial uploads are not supported "
                  "for CopySurface.";
    return;
  }

  IntRect sourceRect = aSourceRect;
  sourceRect.SetLeftEdge(sourceRect.X() + (aDestination.x - aSourceRect.X()) -
                         mat._31);
  sourceRect.SetTopEdge(sourceRect.Y() + (aDestination.y - aSourceRect.Y()) -
                        mat._32);

  RefPtr<ID2D1Bitmap> bitmap;
  HRESULT hr = image->QueryInterface((ID2D1Bitmap**)getter_AddRefs(bitmap));

  if (SUCCEEDED(hr) && bitmap && mFormat == SurfaceFormat::A8) {
    RefPtr<ID2D1SolidColorBrush> brush;
    mDC->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::White),
                               D2D1::BrushProperties(), getter_AddRefs(brush));
    mDC->SetAntialiasMode(D2D1_ANTIALIAS_MODE_ALIASED);
    mDC->SetPrimitiveBlend(D2D1_PRIMITIVE_BLEND_COPY);
    mDC->FillOpacityMask(bitmap, brush, D2D1_OPACITY_MASK_CONTENT_GRAPHICS);
    mDC->SetAntialiasMode(D2D1_ANTIALIAS_MODE_PER_PRIMITIVE);
    mDC->SetPrimitiveBlend(D2D1_PRIMITIVE_BLEND_SOURCE_OVER);
    return;
  }

  Rect srcRect(Float(sourceRect.X()), Float(sourceRect.Y()),
               Float(aSourceRect.Width()), Float(aSourceRect.Height()));

  Rect dstRect(Float(aDestination.x), Float(aDestination.y),
               Float(aSourceRect.Width()), Float(aSourceRect.Height()));

  if (SUCCEEDED(hr) && bitmap) {
    mDC->SetPrimitiveBlend(D2D1_PRIMITIVE_BLEND_COPY);
    mDC->DrawBitmap(bitmap, D2DRect(dstRect), 1.0f,
                    D2D1_BITMAP_INTERPOLATION_MODE_NEAREST_NEIGHBOR,
                    D2DRect(srcRect));
    mDC->SetPrimitiveBlend(D2D1_PRIMITIVE_BLEND_SOURCE_OVER);
    return;
  }

  mDC->DrawImage(image,
                 D2D1::Point2F(Float(aDestination.x), Float(aDestination.y)),
                 D2DRect(srcRect), D2D1_INTERPOLATION_MODE_NEAREST_NEIGHBOR,
                 D2D1_COMPOSITE_MODE_BOUNDED_SOURCE_COPY);
}

void DrawTargetD2D1::FillRect(const Rect& aRect, const Pattern& aPattern,
                              const DrawOptions& aOptions) {
  if (!PrepareForDrawing(aOptions.mCompositionOp, aPattern)) {
    return;
  }

  mDC->SetAntialiasMode(D2DAAMode(aOptions.mAntialiasMode));

  RefPtr<ID2D1Brush> brush = CreateBrushForPattern(aPattern, aOptions.mAlpha);
  mDC->FillRectangle(D2DRect(aRect), brush);

  FinalizeDrawing(aOptions.mCompositionOp, aPattern);
}

void DrawTargetD2D1::FillRoundedRect(const RoundedRect& aRect,
                                     const Pattern& aPattern,
                                     const DrawOptions& aOptions) {
  if (!aRect.corners.AreRadiiSame()) {
    return DrawTarget::FillRoundedRect(aRect, aPattern, aOptions);
  }

  if (!PrepareForDrawing(aOptions.mCompositionOp, aPattern)) {
    return;
  }

  mDC->SetAntialiasMode(D2DAAMode(aOptions.mAntialiasMode));

  RefPtr<ID2D1Brush> brush = CreateBrushForPattern(aPattern, aOptions.mAlpha);
  mDC->FillRoundedRectangle(D2DRoundedRect(aRect), brush);

  FinalizeDrawing(aOptions.mCompositionOp, aPattern);
}

void DrawTargetD2D1::StrokeRect(const Rect& aRect, const Pattern& aPattern,
                                const StrokeOptions& aStrokeOptions,
                                const DrawOptions& aOptions) {
  if (!PrepareForDrawing(aOptions.mCompositionOp, aPattern)) {
    return;
  }

  mDC->SetAntialiasMode(D2DAAMode(aOptions.mAntialiasMode));

  RefPtr<ID2D1Brush> brush = CreateBrushForPattern(aPattern, aOptions.mAlpha);
  RefPtr<ID2D1StrokeStyle> strokeStyle =
      CreateStrokeStyleForOptions(aStrokeOptions);

  mDC->DrawRectangle(D2DRect(aRect), brush, aStrokeOptions.mLineWidth,
                     strokeStyle);

  FinalizeDrawing(aOptions.mCompositionOp, aPattern);
}

void DrawTargetD2D1::StrokeLine(const Point& aStart, const Point& aEnd,
                                const Pattern& aPattern,
                                const StrokeOptions& aStrokeOptions,
                                const DrawOptions& aOptions) {
  if (!PrepareForDrawing(aOptions.mCompositionOp, aPattern)) {
    return;
  }

  mDC->SetAntialiasMode(D2DAAMode(aOptions.mAntialiasMode));

  RefPtr<ID2D1Brush> brush = CreateBrushForPattern(aPattern, aOptions.mAlpha);
  RefPtr<ID2D1StrokeStyle> strokeStyle =
      CreateStrokeStyleForOptions(aStrokeOptions);

  mDC->DrawLine(D2DPoint(aStart), D2DPoint(aEnd), brush,
                aStrokeOptions.mLineWidth, strokeStyle);

  FinalizeDrawing(aOptions.mCompositionOp, aPattern);
}

void DrawTargetD2D1::Stroke(const Path* aPath, const Pattern& aPattern,
                            const StrokeOptions& aStrokeOptions,
                            const DrawOptions& aOptions) {
  const Path* path = aPath;
  if (path->GetBackendType() != BackendType::DIRECT2D1_1) {
    gfxDebug() << *this << ": Ignoring drawing call for incompatible path.";
    return;
  }
  const PathD2D* d2dPath = static_cast<const PathD2D*>(path);

  if (!PrepareForDrawing(aOptions.mCompositionOp, aPattern)) {
    return;
  }

  mDC->SetAntialiasMode(D2DAAMode(aOptions.mAntialiasMode));

  RefPtr<ID2D1Brush> brush = CreateBrushForPattern(aPattern, aOptions.mAlpha);
  RefPtr<ID2D1StrokeStyle> strokeStyle =
      CreateStrokeStyleForOptions(aStrokeOptions);

  mDC->DrawGeometry(d2dPath->mGeometry, brush, aStrokeOptions.mLineWidth,
                    strokeStyle);

  FinalizeDrawing(aOptions.mCompositionOp, aPattern);
}

void DrawTargetD2D1::Fill(const Path* aPath, const Pattern& aPattern,
                          const DrawOptions& aOptions) {
  const Path* path = aPath;
  if (!path || path->GetBackendType() != BackendType::DIRECT2D1_1) {
    gfxDebug() << *this << ": Ignoring drawing call for incompatible path.";
    return;
  }
  const PathD2D* d2dPath = static_cast<const PathD2D*>(path);

  if (!PrepareForDrawing(aOptions.mCompositionOp, aPattern)) {
    return;
  }

  mDC->SetAntialiasMode(D2DAAMode(aOptions.mAntialiasMode));

  RefPtr<ID2D1Brush> brush = CreateBrushForPattern(aPattern, aOptions.mAlpha);

  mDC->FillGeometry(d2dPath->mGeometry, brush);

  FinalizeDrawing(aOptions.mCompositionOp, aPattern);
}

void DrawTargetD2D1::FillGlyphs(ScaledFont* aFont, const GlyphBuffer& aBuffer,
                                const Pattern& aPattern,
                                const DrawOptions& aOptions) {
  if (aFont->GetType() != FontType::DWRITE) {
    gfxDebug() << *this << ": Ignoring drawing call for incompatible font.";
    return;
  }

  ScaledFontDWrite* font = static_cast<ScaledFontDWrite*>(aFont);

  // May be null, if we failed to initialize the default rendering params.
  RefPtr<IDWriteRenderingParams> params =
      font->DWriteSettings().RenderingParams();

  AntialiasMode aaMode = font->GetDefaultAAMode();

  if (aOptions.mAntialiasMode != AntialiasMode::DEFAULT) {
    aaMode = aOptions.mAntialiasMode;
  }

  if (!PrepareForDrawing(aOptions.mCompositionOp, aPattern)) {
    return;
  }

  bool forceClearType = false;
  if (!CurrentLayer().mIsOpaque && mPermitSubpixelAA &&
      aOptions.mCompositionOp == CompositionOp::OP_OVER &&
      aaMode == AntialiasMode::SUBPIXEL) {
    forceClearType = true;
  }

  D2D1_TEXT_ANTIALIAS_MODE d2dAAMode = D2D1_TEXT_ANTIALIAS_MODE_DEFAULT;

  switch (aaMode) {
    case AntialiasMode::NONE:
      d2dAAMode = D2D1_TEXT_ANTIALIAS_MODE_ALIASED;
      break;
    case AntialiasMode::GRAY:
      d2dAAMode = D2D1_TEXT_ANTIALIAS_MODE_GRAYSCALE;
      break;
    case AntialiasMode::SUBPIXEL:
      d2dAAMode = D2D1_TEXT_ANTIALIAS_MODE_CLEARTYPE;
      break;
    default:
      d2dAAMode = D2D1_TEXT_ANTIALIAS_MODE_DEFAULT;
  }

  if (d2dAAMode == D2D1_TEXT_ANTIALIAS_MODE_CLEARTYPE &&
      !CurrentLayer().mIsOpaque && !forceClearType) {
    d2dAAMode = D2D1_TEXT_ANTIALIAS_MODE_GRAYSCALE;
  }

  mDC->SetTextAntialiasMode(d2dAAMode);

  if (params != mTextRenderingParams) {
    // According to
    // https://docs.microsoft.com/en-us/windows/win32/api/d2d1/nf-d2d1-id2d1rendertarget-settextrenderingparams
    // it's OK to pass null for params here; it will just "clear current text
    // rendering options".
    mDC->SetTextRenderingParams(params);
    mTextRenderingParams = params;
  }

  RefPtr<ID2D1Brush> brush = CreateBrushForPattern(aPattern, aOptions.mAlpha);

  AutoDWriteGlyphRun autoRun;
  DWriteGlyphRunFromGlyphs(aBuffer, font, &autoRun);

  bool needsRepushedLayers = false;
  if (forceClearType) {
    D2D1_RECT_F rect;
    bool isAligned;
    needsRepushedLayers = CurrentLayer().mPushedClips.size() &&
                          !GetDeviceSpaceClipRect(rect, isAligned);

    // If we have a complex clip in our stack and we have a transparent
    // background, and subpixel AA is permitted, we need to repush our layer
    // stack limited by the glyph run bounds initializing our layers for
    // subpixel AA.
    if (needsRepushedLayers) {
      mDC->GetGlyphRunWorldBounds(D2D1::Point2F(), &autoRun,
                                  DWRITE_MEASURING_MODE_NATURAL, &rect);
      rect.left = std::floor(rect.left);
      rect.right = std::ceil(rect.right);
      rect.top = std::floor(rect.top);
      rect.bottom = std::ceil(rect.bottom);

      PopAllClips();

      if (!mTransform.IsRectilinear()) {
        // We must limit the pixels we touch to the -user space- bounds of
        // the glyphs being drawn. In order not to get transparent pixels
        // copied up in our pushed layer stack.
        D2D1_RECT_F userRect;
        mDC->SetTransform(D2D1::IdentityMatrix());
        mDC->GetGlyphRunWorldBounds(D2D1::Point2F(), &autoRun,
                                    DWRITE_MEASURING_MODE_NATURAL, &userRect);

        RefPtr<ID2D1PathGeometry> path;
        factory()->CreatePathGeometry(getter_AddRefs(path));
        RefPtr<ID2D1GeometrySink> sink;
        path->Open(getter_AddRefs(sink));
        AddRectToSink(sink, userRect);
        sink->Close();

        mDC->PushLayer(
            D2D1::LayerParameters1(
                D2D1::InfiniteRect(), path, D2D1_ANTIALIAS_MODE_ALIASED,
                D2DMatrix(mTransform), 1.0f, nullptr,
                D2D1_LAYER_OPTIONS1_INITIALIZE_FROM_BACKGROUND |
                    D2D1_LAYER_OPTIONS1_IGNORE_ALPHA),
            nullptr);
      }

      PushClipsToDC(mDC, true, rect);
      mDC->SetTransform(D2DMatrix(mTransform));
    }
  }

  if (brush) {
    mDC->DrawGlyphRun(D2D1::Point2F(), &autoRun, brush);
  }

  if (mTransform.HasNonTranslation()) {
    mTransformedGlyphsSinceLastPurge += aBuffer.mNumGlyphs;
  }

  if (needsRepushedLayers) {
    PopClipsFromDC(mDC);

    if (!mTransform.IsRectilinear()) {
      mDC->PopLayer();
    }
  }

  FinalizeDrawing(aOptions.mCompositionOp, aPattern);
}

void DrawTargetD2D1::Mask(const Pattern& aSource, const Pattern& aMask,
                          const DrawOptions& aOptions) {
  if (!PrepareForDrawing(aOptions.mCompositionOp, aSource)) {
    return;
  }

  RefPtr<ID2D1Brush> source = CreateBrushForPattern(aSource, aOptions.mAlpha);
  RefPtr<ID2D1Brush> mask = CreateBrushForPattern(aMask, 1.0f);
  mDC->PushLayer(D2D1::LayerParameters(D2D1::InfiniteRect(), nullptr,
                                       D2D1_ANTIALIAS_MODE_PER_PRIMITIVE,
                                       D2D1::IdentityMatrix(), 1.0f, mask),
                 nullptr);

  Rect rect(0, 0, (Float)mSize.width, (Float)mSize.height);
  Matrix mat = mTransform;
  mat.Invert();

  mDC->FillRectangle(D2DRect(mat.TransformBounds(rect)), source);

  mDC->PopLayer();

  FinalizeDrawing(aOptions.mCompositionOp, aSource);
}

void DrawTargetD2D1::PushClipGeometry(ID2D1Geometry* aGeometry,
                                      const D2D1_MATRIX_3X2_F& aTransform,
                                      bool aPixelAligned) {
  mCurrentClippedGeometry = nullptr;

  PushedClip clip;
  clip.mGeometry = aGeometry;
  clip.mTransform = aTransform;
  clip.mIsPixelAligned = aPixelAligned;

  aGeometry->GetBounds(aTransform, &clip.mBounds);

  CurrentLayer().mPushedClips.push_back(clip);

  // The transform of clips is relative to the world matrix, since we use the
  // total transform for the clips, make the world matrix identity.
  mDC->SetTransform(D2D1::IdentityMatrix());
  mTransformDirty = true;

  if (CurrentLayer().mClipsArePushed) {
    PushD2DLayer(mDC, clip.mGeometry, clip.mTransform, clip.mIsPixelAligned);
  }
}

void DrawTargetD2D1::PushClip(const Path* aPath) {
  const Path* path = aPath;
  if (path->GetBackendType() != BackendType::DIRECT2D1_1) {
    gfxDebug() << *this << ": Ignoring clipping call for incompatible path.";
    return;
  }
  if (!EnsureInitialized()) {
    return;
  }

  RefPtr<PathD2D> pathD2D = static_cast<PathD2D*>(const_cast<Path*>(path));

  PushClipGeometry(pathD2D->GetGeometry(), D2DMatrix(mTransform));
}

void DrawTargetD2D1::PushClipRect(const Rect& aRect) {
  if (!EnsureInitialized()) {
    return;
  }
  if (!mTransform.IsRectilinear()) {
    // Whoops, this isn't a rectangle in device space, Direct2D will not deal
    // with this transform the way we want it to.
    // See remarks:
    // http://msdn.microsoft.com/en-us/library/dd316860%28VS.85%29.aspx
    RefPtr<ID2D1Geometry> geom = ConvertRectToGeometry(D2DRect(aRect));
    return PushClipGeometry(geom, D2DMatrix(mTransform));
  }

  mCurrentClippedGeometry = nullptr;

  PushedClip clip;
  Rect rect = mTransform.TransformBounds(aRect);
  IntRect intRect;
  clip.mIsPixelAligned = rect.ToIntRect(&intRect);

  // Do not store the transform, just store the device space rectangle directly.
  clip.mBounds = D2DRect(rect);

  CurrentLayer().mPushedClips.push_back(clip);

  mDC->SetTransform(D2D1::IdentityMatrix());
  mTransformDirty = true;

  if (CurrentLayer().mClipsArePushed) {
    mDC->PushAxisAlignedClip(
        clip.mBounds, clip.mIsPixelAligned ? D2D1_ANTIALIAS_MODE_ALIASED
                                           : D2D1_ANTIALIAS_MODE_PER_PRIMITIVE);
  }
}

void DrawTargetD2D1::PushDeviceSpaceClipRects(const IntRect* aRects,
                                              uint32_t aCount) {
  if (!EnsureInitialized()) {
    return;
  }
  // Build a path for the union of the rects.
  RefPtr<ID2D1PathGeometry> path;
  factory()->CreatePathGeometry(getter_AddRefs(path));
  RefPtr<ID2D1GeometrySink> sink;
  path->Open(getter_AddRefs(sink));
  sink->SetFillMode(D2D1_FILL_MODE_WINDING);
  for (uint32_t i = 0; i < aCount; i++) {
    const IntRect& rect = aRects[i];
    sink->BeginFigure(D2DPoint(rect.TopLeft()), D2D1_FIGURE_BEGIN_FILLED);
    D2D1_POINT_2F lines[3] = {D2DPoint(rect.TopRight()),
                              D2DPoint(rect.BottomRight()),
                              D2DPoint(rect.BottomLeft())};
    sink->AddLines(lines, 3);
    sink->EndFigure(D2D1_FIGURE_END_CLOSED);
  }
  sink->Close();

  // The path is in device-space, so there is no transform needed,
  // and all rects are pixel aligned.
  PushClipGeometry(path, D2D1::IdentityMatrix(), true);
}

void DrawTargetD2D1::PopClip() {
  if (!EnsureInitialized()) {
    return;
  }
  mCurrentClippedGeometry = nullptr;
  if (CurrentLayer().mPushedClips.empty()) {
    gfxDevCrash(LogReason::UnbalancedClipStack)
        << "DrawTargetD2D1::PopClip: No clip to pop.";
    return;
  }

  if (CurrentLayer().mClipsArePushed) {
    if (CurrentLayer().mPushedClips.back().mGeometry) {
      mDC->PopLayer();
    } else {
      mDC->PopAxisAlignedClip();
    }
  }
  CurrentLayer().mPushedClips.pop_back();
}

bool DrawTargetD2D1::RemoveAllClips() {
  if (!EnsureInitialized()) {
    return false;
  }
  mCurrentClippedGeometry = nullptr;
  while (!CurrentLayer().mPushedClips.empty()) {
    PopClip();
  }
  return true;
}

void DrawTargetD2D1::PushLayer(bool aOpaque, Float aOpacity,
                               SourceSurface* aMask,
                               const Matrix& aMaskTransform,
                               const IntRect& aBounds, bool aCopyBackground) {
  if (!EnsureInitialized()) {
    return;
  }
  D2D1_LAYER_OPTIONS1 options = D2D1_LAYER_OPTIONS1_NONE;

  if (aOpaque) {
    options |= D2D1_LAYER_OPTIONS1_IGNORE_ALPHA;
  }
  if (aCopyBackground) {
    options |= D2D1_LAYER_OPTIONS1_INITIALIZE_FROM_BACKGROUND;
  }

  RefPtr<ID2D1ImageBrush> mask;
  Matrix maskTransform = aMaskTransform;
  RefPtr<ID2D1PathGeometry> clip;

  if (aMask) {
    RefPtr<ID2D1Image> image =
        GetImageForSurface(aMask, maskTransform, ExtendMode::CLAMP);
    mDC->SetTransform(D2D1::IdentityMatrix());
    mTransformDirty = true;

    maskTransform =
        maskTransform.PreTranslate(aMask->GetRect().X(), aMask->GetRect().Y());
    // The mask is given in user space. Our layer will apply it in device space.
    maskTransform = maskTransform * mTransform;

    if (image) {
      IntSize maskSize = aMask->GetSize();
      HRESULT hr = mDC->CreateImageBrush(
          image,
          D2D1::ImageBrushProperties(
              D2D1::RectF(0, 0, maskSize.width, maskSize.height)),
          D2D1::BrushProperties(1.0f, D2DMatrix(maskTransform)),
          getter_AddRefs(mask));
      if (FAILED(hr)) {
        gfxWarning() << "[D2D1.1] Failed to create a ImageBrush, code: "
                     << hexa(hr);
      }

      factory()->CreatePathGeometry(getter_AddRefs(clip));
      RefPtr<ID2D1GeometrySink> sink;
      clip->Open(getter_AddRefs(sink));
      AddRectToSink(sink, D2D1::RectF(0, 0, aMask->GetSize().width,
                                      aMask->GetSize().height));
      sink->Close();
    } else {
      gfxCriticalError() << "Failed to get image for mask surface!";
    }
  }

  PushAllClips();

  mDC->PushLayer(D2D1::LayerParameters1(
                     D2D1::InfiniteRect(), clip, D2D1_ANTIALIAS_MODE_ALIASED,
                     D2DMatrix(maskTransform), aOpacity, mask, options),
                 nullptr);
  PushedLayer pushedLayer;
  pushedLayer.mClipsArePushed = false;
  pushedLayer.mIsOpaque = aOpaque;
  pushedLayer.mOldPermitSubpixelAA = mPermitSubpixelAA;
  mPermitSubpixelAA = aOpaque;

  mDC->CreateCommandList(getter_AddRefs(pushedLayer.mCurrentList));
  mPushedLayers.push_back(pushedLayer);

  mDC->SetTarget(CurrentTarget());

  mUsedCommandListsSincePurge++;
}

void DrawTargetD2D1::PopLayer() {
  // We must have at least one layer at all times.
  MOZ_ASSERT(mPushedLayers.size() > 1);
  MOZ_ASSERT(CurrentLayer().mPushedClips.size() == 0);
  if (!EnsureInitialized() || mPushedLayers.size() <= 1) {
    return;
  }
  RefPtr<ID2D1CommandList> list = CurrentLayer().mCurrentList;
  mPermitSubpixelAA = CurrentLayer().mOldPermitSubpixelAA;

  mPushedLayers.pop_back();
  mDC->SetTarget(CurrentTarget());

  list->Close();
  mDC->SetTransform(D2D1::IdentityMatrix());
  mTransformDirty = true;

  DCCommandSink sink(mDC);
  list->Stream(&sink);

  mComplexBlendsWithListInList = 0;

  mDC->PopLayer();
}

already_AddRefed<SourceSurface> DrawTargetD2D1::CreateSourceSurfaceFromData(
    unsigned char* aData, const IntSize& aSize, int32_t aStride,
    SurfaceFormat aFormat) const {
  RefPtr<ID2D1Bitmap1> bitmap;

  RefPtr<ID2D1DeviceContext> dc = Factory::GetD2DDeviceContext();
  if (!dc) {
    return nullptr;
  }

  HRESULT hr =
      dc->CreateBitmap(D2DIntSize(aSize), aData, aStride,
                       D2D1::BitmapProperties1(D2D1_BITMAP_OPTIONS_NONE,
                                               D2DPixelFormat(aFormat)),
                       getter_AddRefs(bitmap));

  if (FAILED(hr) || !bitmap) {
    gfxCriticalError(
        CriticalLog::DefaultOptions(Factory::ReasonableSurfaceSize(aSize)))
        << "[D2D1.1] 1CreateBitmap failure " << aSize << " Code: " << hexa(hr)
        << " format " << (int)aFormat;
    return nullptr;
  }

  return MakeAndAddRef<SourceSurfaceD2D1>(bitmap.get(), dc.get(), aFormat,
                                          aSize);
}

already_AddRefed<DrawTarget> DrawTargetD2D1::CreateSimilarDrawTarget(
    const IntSize& aSize, SurfaceFormat aFormat) const {
  RefPtr<DrawTargetD2D1> dt = new DrawTargetD2D1();

  if (!dt->Init(aSize, aFormat)) {
    return nullptr;
  }

  return dt.forget();
}

bool DrawTargetD2D1::CanCreateSimilarDrawTarget(const IntSize& aSize,
                                                SurfaceFormat aFormat) const {
  RefPtr<ID2D1DeviceContext> dc = Factory::GetD2DDeviceContext();
  if (!dc) {
    return false;
  }
  return (dc->GetMaximumBitmapSize() >= UINT32(aSize.width) &&
          dc->GetMaximumBitmapSize() >= UINT32(aSize.height));
}

RefPtr<DrawTarget> DrawTargetD2D1::CreateClippedDrawTarget(
    const Rect& aBounds, SurfaceFormat aFormat) {
  RefPtr<DrawTarget> result;

  if (!aBounds.IsEmpty()) {
    PushClipRect(aBounds);
  }

  D2D1_RECT_F clipRect;
  bool isAligned;
  GetDeviceSpaceClipRect(clipRect, isAligned);
  IntRect rect = RoundedOut(ToRect(clipRect));

  RefPtr<DrawTarget> dt = CreateSimilarDrawTarget(rect.Size(), aFormat);
  result = gfx::Factory::CreateOffsetDrawTarget(dt, rect.TopLeft());
  result->SetTransform(mTransform);
  if (!aBounds.IsEmpty()) {
    PopClip();
  }

  return result;
}

already_AddRefed<PathBuilder> DrawTargetD2D1::CreatePathBuilder(
    FillRule aFillRule) const {
  RefPtr<ID2D1PathGeometry> path;
  HRESULT hr = factory()->CreatePathGeometry(getter_AddRefs(path));

  if (FAILED(hr)) {
    gfxWarning() << *this << ": Failed to create Direct2D Path Geometry. Code: "
                 << hexa(hr);
    return nullptr;
  }

  RefPtr<ID2D1GeometrySink> sink;
  hr = path->Open(getter_AddRefs(sink));
  if (FAILED(hr)) {
    gfxWarning() << *this << ": Failed to access Direct2D Path Geometry. Code: "
                 << hexa(hr);
    return nullptr;
  }

  if (aFillRule == FillRule::FILL_WINDING) {
    sink->SetFillMode(D2D1_FILL_MODE_WINDING);
  }

  return MakeAndAddRef<PathBuilderD2D>(sink, path, aFillRule,
                                       BackendType::DIRECT2D1_1);
}

already_AddRefed<GradientStops> DrawTargetD2D1::CreateGradientStops(
    GradientStop* rawStops, uint32_t aNumStops, ExtendMode aExtendMode) const {
  if (aNumStops == 0) {
    gfxWarning() << *this
                 << ": Failed to create GradientStopCollection with no stops.";
    return nullptr;
  }

  D2D1_GRADIENT_STOP* stops = new D2D1_GRADIENT_STOP[aNumStops];

  for (uint32_t i = 0; i < aNumStops; i++) {
    stops[i].position = rawStops[i].offset;
    stops[i].color = D2DColor(rawStops[i].color);
  }

  RefPtr<ID2D1GradientStopCollection1> stopCollection;

  RefPtr<ID2D1DeviceContext> dc = Factory::GetD2DDeviceContext();

  if (!dc) {
    return nullptr;
  }

  HRESULT hr = dc->CreateGradientStopCollection(
      stops, aNumStops, D2D1_COLOR_SPACE_SRGB, D2D1_COLOR_SPACE_SRGB,
      D2D1_BUFFER_PRECISION_8BPC_UNORM, D2DExtend(aExtendMode, Axis::BOTH),
      D2D1_COLOR_INTERPOLATION_MODE_PREMULTIPLIED,
      getter_AddRefs(stopCollection));
  delete[] stops;

  if (FAILED(hr)) {
    gfxWarning() << *this << ": Failed to create GradientStopCollection. Code: "
                 << hexa(hr);
    return nullptr;
  }

  RefPtr<ID3D11Device> device = Factory::GetDirect3D11Device();
  return MakeAndAddRef<GradientStopsD2D>(stopCollection, device);
}

already_AddRefed<FilterNode> DrawTargetD2D1::CreateFilter(FilterType aType) {
  if (!EnsureInitialized()) {
    return nullptr;
  }
  return FilterNodeD2D1::Create(mDC, aType);
}

bool DrawTargetD2D1::Init(ID3D11Texture2D* aTexture, SurfaceFormat aFormat) {
  RefPtr<ID2D1Device> device = Factory::GetD2D1Device(&mDeviceSeq);
  if (!device) {
    gfxCriticalNote << "[D2D1.1] Failed to obtain a device for "
                       "DrawTargetD2D1::Init(ID3D11Texture2D*, SurfaceFormat).";
    return false;
  }

  aTexture->QueryInterface(__uuidof(IDXGISurface),
                           (void**)((IDXGISurface**)getter_AddRefs(mSurface)));
  if (!mSurface) {
    gfxCriticalError() << "[D2D1.1] Failed to obtain a DXGI surface.";
    return false;
  }

  mFormat = aFormat;

  D3D11_TEXTURE2D_DESC desc;
  aTexture->GetDesc(&desc);
  mSize.width = desc.Width;
  mSize.height = desc.Height;

  return true;
}

bool DrawTargetD2D1::Init(const IntSize& aSize, SurfaceFormat aFormat) {
  RefPtr<ID2D1Device> device = Factory::GetD2D1Device(&mDeviceSeq);
  if (!device) {
    gfxCriticalNote << "[D2D1.1] Failed to obtain a device for "
                       "DrawTargetD2D1::Init(IntSize, SurfaceFormat).";
    return false;
  }

  if (!CanCreateSimilarDrawTarget(aSize, aFormat)) {
    // Size unsupported.
    return false;
  }

  mFormat = aFormat;
  mSize = aSize;

  return true;
}

/**
 * Private helpers.
 */
uint32_t DrawTargetD2D1::GetByteSize() const {
  return mSize.width * mSize.height * BytesPerPixel(mFormat);
}

RefPtr<ID2D1Factory1> DrawTargetD2D1::factory() {
  StaticMutexAutoLock lock(Factory::mDeviceLock);

  if (mFactory || !NS_IsMainThread()) {
    return mFactory;
  }

  // We don't allow initializing the factory off the main thread.
  MOZ_RELEASE_ASSERT(NS_IsMainThread());

  RefPtr<ID2D1Factory> factory;
  D2D1CreateFactoryFunc createD2DFactory;
  HMODULE d2dModule = LoadLibraryW(L"d2d1.dll");
  createD2DFactory =
      (D2D1CreateFactoryFunc)GetProcAddress(d2dModule, "D2D1CreateFactory");

  if (!createD2DFactory) {
    gfxWarning() << "Failed to locate D2D1CreateFactory function.";
    return nullptr;
  }

  D2D1_FACTORY_OPTIONS options;
#ifdef _DEBUG
  options.debugLevel = D2D1_DEBUG_LEVEL_WARNING;
#else
  options.debugLevel = D2D1_DEBUG_LEVEL_NONE;
#endif
  // options.debugLevel = D2D1_DEBUG_LEVEL_INFORMATION;

  HRESULT hr =
      createD2DFactory(D2D1_FACTORY_TYPE_MULTI_THREADED, __uuidof(ID2D1Factory),
                       &options, getter_AddRefs(factory));

  if (FAILED(hr) || !factory) {
    gfxCriticalNote << "Failed to create a D2D1 content device: " << hexa(hr);
    return nullptr;
  }

  RefPtr<ID2D1Factory1> factory1;
  hr = factory->QueryInterface(__uuidof(ID2D1Factory1),
                               getter_AddRefs(factory1));
  if (FAILED(hr) || !factory1) {
    return nullptr;
  }

  mFactory = factory1;

  ExtendInputEffectD2D1::Register(mFactory);
  ConicGradientEffectD2D1::Register(mFactory);
  RadialGradientEffectD2D1::Register(mFactory);

  return mFactory;
}

void DrawTargetD2D1::CleanupD2D() {
  MOZ_RELEASE_ASSERT(NS_IsMainThread());
  Factory::mDeviceLock.AssertCurrentThreadOwns();

  if (mFactory) {
    RadialGradientEffectD2D1::Unregister(mFactory);
    ConicGradientEffectD2D1::Unregister(mFactory);
    ExtendInputEffectD2D1::Unregister(mFactory);
    mFactory = nullptr;
  }
}

void DrawTargetD2D1::FlushInternal(bool aHasDependencyMutex /* = false */) {
  if (IsDeviceContextValid()) {
    if ((mUsedCommandListsSincePurge >= kPushedLayersBeforePurge ||
         mTransformedGlyphsSinceLastPurge >= kTransformedGlyphsBeforePurge) &&
        mPushedLayers.size() == 1) {
      // It's important to pop all clips as otherwise layers can forget about
      // their clip when doing an EndDraw. When we have layers pushed we cannot
      // easily pop all underlying clips to delay the purge until we have no
      // layers pushed.
      PopAllClips();
      mUsedCommandListsSincePurge = 0;
      mTransformedGlyphsSinceLastPurge = 0;
      mDC->EndDraw();
      mDC->BeginDraw();
    } else {
      mDC->Flush();
    }
  }

  Maybe<StaticMutexAutoLock> lock;

  if (!aHasDependencyMutex) {
    lock.emplace(Factory::mDTDependencyLock);
  }

  Factory::mDTDependencyLock.AssertCurrentThreadOwns();
  // We no longer depend on any target.
  for (TargetSet::iterator iter = mDependingOnTargets.begin();
       iter != mDependingOnTargets.end(); iter++) {
    (*iter)->mDependentTargets.erase(this);
  }
  mDependingOnTargets.clear();
}

bool DrawTargetD2D1::EnsureInitialized() {
  if (mInitState != InitState::Uninitialized) {
    return mInitState == InitState::Success;
  }

  // Don't retry.
  mInitState = InitState::Failure;

  HRESULT hr;

  RefPtr<ID2D1Device> device = Factory::GetD2D1Device(&mDeviceSeq);
  if (!device) {
    gfxCriticalNote << "[D2D1.1] Failed to obtain a device for "
                       "DrawTargetD2D1::EnsureInitialized().";
    return false;
  }

  hr = device->CreateDeviceContext(
      D2D1_DEVICE_CONTEXT_OPTIONS_ENABLE_MULTITHREADED_OPTIMIZATIONS,
      getter_AddRefs(mDC));

  if (FAILED(hr)) {
    gfxCriticalError() << "[D2D1.1] 2Failed to create a DeviceContext, code: "
                       << hexa(hr) << " format " << (int)mFormat;
    return false;
  }

  if (!mSurface) {
    if (mDC->GetMaximumBitmapSize() < UINT32(mSize.width) ||
        mDC->GetMaximumBitmapSize() < UINT32(mSize.height)) {
      // This is 'ok', so don't assert
      gfxCriticalNote << "[D2D1.1] Attempt to use unsupported surface size "
                      << mSize;
      return false;
    }

    D2D1_BITMAP_PROPERTIES1 props;
    props.dpiX = 96;
    props.dpiY = 96;
    props.pixelFormat = D2DPixelFormat(mFormat);
    props.colorContext = nullptr;
    props.bitmapOptions = D2D1_BITMAP_OPTIONS_TARGET;
    hr = mDC->CreateBitmap(D2DIntSize(mSize), nullptr, 0, props,
                           (ID2D1Bitmap1**)getter_AddRefs(mBitmap));

    if (FAILED(hr)) {
      gfxCriticalError() << "[D2D1.1] 3CreateBitmap failure " << mSize
                         << " Code: " << hexa(hr) << " format " << (int)mFormat;
      return false;
    }
  } else {
    D2D1_BITMAP_PROPERTIES1 props;
    props.dpiX = 96;
    props.dpiY = 96;
    props.pixelFormat = D2DPixelFormat(mFormat);
    props.colorContext = nullptr;
    props.bitmapOptions = D2D1_BITMAP_OPTIONS_TARGET;
    hr = mDC->CreateBitmapFromDxgiSurface(
        mSurface, props, (ID2D1Bitmap1**)getter_AddRefs(mBitmap));

    if (FAILED(hr)) {
      gfxCriticalError()
          << "[D2D1.1] CreateBitmapFromDxgiSurface failure Code: " << hexa(hr)
          << " format " << (int)mFormat;
      return false;
    }
  }

  mDC->SetTarget(CurrentTarget());

  hr = mDC->CreateSolidColorBrush(D2D1::ColorF(0, 0),
                                  getter_AddRefs(mSolidColorBrush));

  if (FAILED(hr)) {
    gfxCriticalError() << "[D2D1.1] Failure creating solid color brush (I2).";
    return false;
  }

  mDC->BeginDraw();

  CurrentLayer().mIsOpaque = mFormat == SurfaceFormat::B8G8R8X8;

  if (!mSurface) {
    mDC->Clear();
  }

  mInitState = InitState::Success;

  return true;
}

void DrawTargetD2D1::MarkChanged() {
  if (mSnapshot) {
    MutexAutoLock lock(*mSnapshotLock);
    if (mSnapshot->hasOneRef()) {
      // Just destroy it, since no-one else knows about it.
      mSnapshot = nullptr;
    } else {
      mSnapshot->DrawTargetWillChange();
      // The snapshot will no longer depend on this target.
      MOZ_ASSERT(!mSnapshot);
    }
  }

  {
    StaticMutexAutoLock lock(Factory::mDTDependencyLock);
    if (mDependentTargets.size()) {
      // Copy mDependentTargets since the Flush()es below will modify it.
      TargetSet tmpTargets = mDependentTargets;
      for (TargetSet::iterator iter = tmpTargets.begin();
           iter != tmpTargets.end(); iter++) {
        (*iter)->FlushInternal(true);
      }
      // The Flush() should have broken all dependencies on this target.
      MOZ_ASSERT(!mDependentTargets.size());
    }
  }
}

bool DrawTargetD2D1::ShouldClipTemporarySurfaceDrawing(CompositionOp aOp,
                                                       const Pattern& aPattern,
                                                       bool aClipIsComplex) {
  bool patternSupported = IsPatternSupportedByD2D(aPattern);
  return patternSupported && !CurrentLayer().mIsOpaque &&
         D2DSupportsCompositeMode(aOp) && IsOperatorBoundByMask(aOp) &&
         aClipIsComplex;
}

bool DrawTargetD2D1::PrepareForDrawing(CompositionOp aOp,
                                       const Pattern& aPattern) {
  if (!EnsureInitialized()) {
    return false;
  }

  MarkChanged();

  bool patternSupported = IsPatternSupportedByD2D(aPattern);

  if (D2DSupportsPrimitiveBlendMode(aOp) && patternSupported) {
    // It's important to do this before FlushTransformToDC! As this will cause
    // the transform to become dirty.
    PushAllClips();

    FlushTransformToDC();

    if (aOp != CompositionOp::OP_OVER) {
      mDC->SetPrimitiveBlend(D2DPrimitiveBlendMode(aOp));
    }

    return true;
  }

  HRESULT result = mDC->CreateCommandList(getter_AddRefs(mCommandList));
  mDC->SetTarget(mCommandList);
  mUsedCommandListsSincePurge++;

  // This is where we should have a valid command list.  If we don't, something
  // is wrong, and it's likely an OOM.
  if (!mCommandList) {
    gfxDevCrash(LogReason::InvalidCommandList)
        << "Invalid D2D1.1 command list on creation "
        << mUsedCommandListsSincePurge << ", " << gfx::hexa(result);
  }

  D2D1_RECT_F rect;
  bool isAligned;
  bool clipIsComplex = CurrentLayer().mPushedClips.size() &&
                       !GetDeviceSpaceClipRect(rect, isAligned);

  if (ShouldClipTemporarySurfaceDrawing(aOp, aPattern, clipIsComplex)) {
    PushClipsToDC(mDC);
  }

  FlushTransformToDC();

  return true;
}

void DrawTargetD2D1::FinalizeDrawing(CompositionOp aOp,
                                     const Pattern& aPattern) {
  bool patternSupported = IsPatternSupportedByD2D(aPattern);

  if (D2DSupportsPrimitiveBlendMode(aOp) && patternSupported) {
    if (aOp != CompositionOp::OP_OVER)
      mDC->SetPrimitiveBlend(D2D1_PRIMITIVE_BLEND_SOURCE_OVER);
    return;
  }

  D2D1_RECT_F rect;
  bool isAligned;
  bool clipIsComplex = CurrentLayer().mPushedClips.size() &&
                       !GetDeviceSpaceClipRect(rect, isAligned);

  if (ShouldClipTemporarySurfaceDrawing(aOp, aPattern, clipIsComplex)) {
    PopClipsFromDC(mDC);
  }

  mDC->SetTarget(CurrentTarget());
  if (!mCommandList) {
    gfxDevCrash(LogReason::InvalidCommandList)
        << "Invalid D21.1 command list on finalize";
    return;
  }
  mCommandList->Close();

  RefPtr<ID2D1CommandList> source = mCommandList;
  mCommandList = nullptr;

  mDC->SetTransform(D2D1::IdentityMatrix());
  mTransformDirty = true;

  if (patternSupported) {
    if (D2DSupportsCompositeMode(aOp)) {
      RefPtr<ID2D1Image> tmpImage;
      if (clipIsComplex) {
        PopAllClips();
        if (!IsOperatorBoundByMask(aOp)) {
          tmpImage = GetImageForLayerContent();
        }
      }
      mDC->DrawImage(source, D2D1_INTERPOLATION_MODE_NEAREST_NEIGHBOR,
                     D2DCompositionMode(aOp));

      if (tmpImage) {
        RefPtr<ID2D1ImageBrush> brush;
        RefPtr<ID2D1Geometry> inverseGeom = GetInverseClippedGeometry();
        mDC->CreateImageBrush(tmpImage,
                              D2D1::ImageBrushProperties(
                                  D2D1::RectF(0, 0, mSize.width, mSize.height)),
                              getter_AddRefs(brush));

        mDC->SetPrimitiveBlend(D2D1_PRIMITIVE_BLEND_COPY);
        mDC->FillGeometry(inverseGeom, brush);
        mDC->SetPrimitiveBlend(D2D1_PRIMITIVE_BLEND_SOURCE_OVER);
      }
      return;
    }

    RefPtr<ID2D1Effect> blendEffect;
    HRESULT hr =
        mDC->CreateEffect(CLSID_D2D1Blend, getter_AddRefs(blendEffect));

    if (FAILED(hr) || !blendEffect) {
      gfxWarning() << "Failed to create blend effect!";
      return;
    }

    IntRect bounds(IntPoint(), mSize);
    RefPtr<ID2D1Geometry> geom;
    if (CurrentLayer().mPushedClips.size() > 0) {
      geom = GetClippedGeometry(&bounds);
    }
    RefPtr<ID2D1Image> tmpImage = GetImageForLayerContent(&bounds, bool(geom));
    if (!tmpImage) {
      return;
    }

    blendEffect->SetInput(0, tmpImage);
    blendEffect->SetInput(1, source);
    blendEffect->SetValue(D2D1_BLEND_PROP_MODE, D2DBlendMode(aOp));

    if (geom) {
      RefPtr<ID2D1Image> blendOutput;
      blendEffect->GetOutput(getter_AddRefs(blendOutput));

      RefPtr<ID2D1ImageBrush> brush;
      mDC->CreateImageBrush(
          blendOutput, D2D1::ImageBrushProperties(D2DRect(bounds)),
          D2D1::BrushProperties(
              1.0f, D2D1::Matrix3x2F::Translation(bounds.x, bounds.y)),
          getter_AddRefs(brush));

      mDC->SetPrimitiveBlend(D2D1_PRIMITIVE_BLEND_COPY);
      mDC->FillGeometry(geom, brush);
      mDC->SetPrimitiveBlend(D2D1_PRIMITIVE_BLEND_SOURCE_OVER);
    } else {
      mDC->DrawImage(blendEffect, D2D1_INTERPOLATION_MODE_NEAREST_NEIGHBOR,
                     D2D1_COMPOSITE_MODE_BOUNDED_SOURCE_COPY);
    }

    mComplexBlendsWithListInList++;
    return;
  }

  if (aPattern.GetType() == PatternType::CONIC_GRADIENT) {
    const ConicGradientPattern* pat =
        static_cast<const ConicGradientPattern*>(&aPattern);

    if (!pat->mStops) {
      // Draw nothing because of no color stops
      return;
    }
    RefPtr<ID2D1Effect> conicGradientEffect;

    HRESULT hr = mDC->CreateEffect(CLSID_ConicGradientEffect,
                                   getter_AddRefs(conicGradientEffect));
    if (FAILED(hr) || !conicGradientEffect) {
      gfxWarning() << "Failed to create conic gradient effect. Code: "
                   << hexa(hr);
      return;
    }

    PushAllClips();

    conicGradientEffect->SetValue(
        CONIC_PROP_STOP_COLLECTION,
        static_cast<const GradientStopsD2D*>(pat->mStops.get())
            ->mStopCollection);
    conicGradientEffect->SetValue(
        CONIC_PROP_CENTER, D2D1::Vector2F(pat->mCenter.x, pat->mCenter.y));
    conicGradientEffect->SetValue(CONIC_PROP_ANGLE, pat->mAngle);
    conicGradientEffect->SetValue(CONIC_PROP_START_OFFSET, pat->mStartOffset);
    conicGradientEffect->SetValue(CONIC_PROP_END_OFFSET, pat->mEndOffset);
    conicGradientEffect->SetValue(CONIC_PROP_TRANSFORM,
                                  D2DMatrix(pat->mMatrix * mTransform));
    conicGradientEffect->SetInput(0, source);

    mDC->DrawImage(conicGradientEffect,
                   D2D1_INTERPOLATION_MODE_NEAREST_NEIGHBOR,
                   D2DCompositionMode(aOp));
    return;
  }

  MOZ_ASSERT(aPattern.GetType() == PatternType::RADIAL_GRADIENT);

  const RadialGradientPattern* pat =
      static_cast<const RadialGradientPattern*>(&aPattern);
  if (pat->mCenter1 == pat->mCenter2 && pat->mRadius1 == pat->mRadius2) {
    // Draw nothing!
    return;
  }

  if (!pat->mStops) {
    // Draw nothing because of no color stops
    return;
  }

  RefPtr<ID2D1Effect> radialGradientEffect;

  HRESULT hr = mDC->CreateEffect(CLSID_RadialGradientEffect,
                                 getter_AddRefs(radialGradientEffect));
  if (FAILED(hr) || !radialGradientEffect) {
    gfxWarning() << "Failed to create radial gradient effect. Code: "
                 << hexa(hr);
    return;
  }

  PushAllClips();

  radialGradientEffect->SetValue(
      RADIAL_PROP_STOP_COLLECTION,
      static_cast<const GradientStopsD2D*>(pat->mStops.get())->mStopCollection);
  radialGradientEffect->SetValue(
      RADIAL_PROP_CENTER_1, D2D1::Vector2F(pat->mCenter1.x, pat->mCenter1.y));
  radialGradientEffect->SetValue(
      RADIAL_PROP_CENTER_2, D2D1::Vector2F(pat->mCenter2.x, pat->mCenter2.y));
  radialGradientEffect->SetValue(RADIAL_PROP_RADIUS_1, pat->mRadius1);
  radialGradientEffect->SetValue(RADIAL_PROP_RADIUS_2, pat->mRadius2);
  radialGradientEffect->SetValue(RADIAL_PROP_RADIUS_2, pat->mRadius2);
  radialGradientEffect->SetValue(RADIAL_PROP_TRANSFORM,
                                 D2DMatrix(pat->mMatrix * mTransform));
  radialGradientEffect->SetInput(0, source);

  mDC->DrawImage(radialGradientEffect, D2D1_INTERPOLATION_MODE_NEAREST_NEIGHBOR,
                 D2DCompositionMode(aOp));
}

void DrawTargetD2D1::AddDependencyOnSource(SourceSurfaceD2D1* aSource) {
  Maybe<MutexAutoLock> snapshotLock;
  // We grab the SnapshotLock as well, this guaranteeds aSource->mDrawTarget
  // cannot be cleared in between the if statement and the dereference.
  if (aSource->mSnapshotLock) {
    snapshotLock.emplace(*aSource->mSnapshotLock);
  }
  {
    StaticMutexAutoLock lock(Factory::mDTDependencyLock);
    if (aSource->mDrawTarget &&
        !mDependingOnTargets.count(aSource->mDrawTarget)) {
      aSource->mDrawTarget->mDependentTargets.insert(this);
      mDependingOnTargets.insert(aSource->mDrawTarget);
    }
  }
}

static D2D1_RECT_F IntersectRect(const D2D1_RECT_F& aRect1,
                                 const D2D1_RECT_F& aRect2) {
  D2D1_RECT_F result;
  result.left = std::max(aRect1.left, aRect2.left);
  result.top = std::max(aRect1.top, aRect2.top);
  result.right = std::min(aRect1.right, aRect2.right);
  result.bottom = std::min(aRect1.bottom, aRect2.bottom);

  result.right = std::max(result.right, result.left);
  result.bottom = std::max(result.bottom, result.top);

  return result;
}

bool DrawTargetD2D1::GetDeviceSpaceClipRect(D2D1_RECT_F& aClipRect,
                                            bool& aIsPixelAligned) {
  aIsPixelAligned = true;
  aClipRect = D2D1::RectF(0, 0, mSize.width, mSize.height);

  if (!CurrentLayer().mPushedClips.size()) {
    return false;
  }

  for (auto iter = CurrentLayer().mPushedClips.begin();
       iter != CurrentLayer().mPushedClips.end(); iter++) {
    if (iter->mGeometry) {
      return false;
    }
    aClipRect = IntersectRect(aClipRect, iter->mBounds);
    if (!iter->mIsPixelAligned) {
      aIsPixelAligned = false;
    }
  }
  return true;
}

static const uint32_t sComplexBlendsWithListAllowedInList = 4;

already_AddRefed<ID2D1Image> DrawTargetD2D1::GetImageForLayerContent(
    const IntRect* aBounds, bool aShouldPreserveContent) {
  PopAllClips();

  IntRect bounds = aBounds ? *aBounds : IntRect(IntPoint(), mSize);
  IntSize size(bounds.XMost(), bounds.YMost());
  if (!CurrentLayer().mCurrentList) {
    RefPtr<ID2D1Bitmap> tmpBitmap;
    HRESULT hr = mDC->CreateBitmap(
        D2DIntSize(size), D2D1::BitmapProperties(D2DPixelFormat(mFormat)),
        getter_AddRefs(tmpBitmap));
    if (FAILED(hr)) {
      gfxCriticalError(
          CriticalLog::DefaultOptions(Factory::ReasonableSurfaceSize(size)))
          << "[D2D1.1] 6CreateBitmap failure " << size << " Code: " << hexa(hr)
          << " format " << (int)mFormat;
      // If it's a recreate target error, return and handle it elsewhere.
      if (hr == D2DERR_RECREATE_TARGET) {
        mDC->Flush();
        return nullptr;
      }
      // For now, crash in other scenarios; this should happen because tmpBitmap
      // is null and CopyFromBitmap call below dereferences it.
    }
    mDC->Flush();

    D2D1_POINT_2U destOffset = D2D1::Point2U(bounds.x, bounds.y);
    D2D1_RECT_U srcRect =
        D2D1::RectU(bounds.x, bounds.y, bounds.width, bounds.height);
    tmpBitmap->CopyFromBitmap(&destOffset, mBitmap, &srcRect);
    return tmpBitmap.forget();
  } else {
    RefPtr<ID2D1CommandList> list = CurrentLayer().mCurrentList;
    mDC->CreateCommandList(getter_AddRefs(CurrentLayer().mCurrentList));
    mDC->SetTarget(CurrentTarget());
    list->Close();

    RefPtr<ID2D1Bitmap1> tmpBitmap;
    if (mComplexBlendsWithListInList >= sComplexBlendsWithListAllowedInList) {
      D2D1_BITMAP_PROPERTIES1 props = D2D1::BitmapProperties1(
          D2D1_BITMAP_OPTIONS_TARGET,
          D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM,
                            D2D1_ALPHA_MODE_PREMULTIPLIED));
      mDC->CreateBitmap(D2DIntSize(size), nullptr, 0, &props,
                        getter_AddRefs(tmpBitmap));
      mDC->SetTransform(D2D1::IdentityMatrix());
      mTransformDirty = true;
      mDC->SetTarget(tmpBitmap);
      mDC->DrawImage(list, D2D1_INTERPOLATION_MODE_NEAREST_NEIGHBOR,
                     D2D1_COMPOSITE_MODE_BOUNDED_SOURCE_COPY);
      mDC->SetTarget(CurrentTarget());
      mComplexBlendsWithListInList = 0;
    }

    DCCommandSink sink(mDC);

    if (aShouldPreserveContent) {
      list->Stream(&sink);
    }

    if (tmpBitmap) {
      return tmpBitmap.forget();
    }

    return list.forget();
  }
}

already_AddRefed<ID2D1Geometry> DrawTargetD2D1::GetClippedGeometry(
    IntRect* aClipBounds) {
  if (mCurrentClippedGeometry) {
    *aClipBounds = mCurrentClipBounds;
    RefPtr<ID2D1Geometry> clippedGeometry(mCurrentClippedGeometry);
    return clippedGeometry.forget();
  }

  MOZ_ASSERT(CurrentLayer().mPushedClips.size());

  mCurrentClipBounds = IntRect(IntPoint(0, 0), mSize);

  // if pathGeom is null then pathRect represents the path.
  RefPtr<ID2D1Geometry> pathGeom;
  D2D1_RECT_F pathRect;
  bool pathRectIsAxisAligned = false;
  auto iter = CurrentLayer().mPushedClips.begin();

  if (iter->mGeometry) {
    pathGeom = GetTransformedGeometry(iter->mGeometry, iter->mTransform);
  } else {
    pathRect = iter->mBounds;
    pathRectIsAxisAligned = iter->mIsPixelAligned;
  }

  iter++;
  for (; iter != CurrentLayer().mPushedClips.end(); iter++) {
    // Do nothing but add it to the current clip bounds.
    if (!iter->mGeometry && iter->mIsPixelAligned) {
      mCurrentClipBounds.IntersectRect(
          mCurrentClipBounds,
          IntRect(int32_t(iter->mBounds.left), int32_t(iter->mBounds.top),
                  int32_t(iter->mBounds.right - iter->mBounds.left),
                  int32_t(iter->mBounds.bottom - iter->mBounds.top)));
      continue;
    }

    if (!pathGeom) {
      if (pathRectIsAxisAligned) {
        mCurrentClipBounds.IntersectRect(
            mCurrentClipBounds,
            IntRect(int32_t(pathRect.left), int32_t(pathRect.top),
                    int32_t(pathRect.right - pathRect.left),
                    int32_t(pathRect.bottom - pathRect.top)));
      }
      if (iter->mGeometry) {
        // See if pathRect needs to go into the path geometry.
        if (!pathRectIsAxisAligned) {
          pathGeom = ConvertRectToGeometry(pathRect);
        } else {
          pathGeom = GetTransformedGeometry(iter->mGeometry, iter->mTransform);
        }
      } else {
        pathRect = IntersectRect(pathRect, iter->mBounds);
        pathRectIsAxisAligned = false;
        continue;
      }
    }

    RefPtr<ID2D1PathGeometry> newGeom;
    factory()->CreatePathGeometry(getter_AddRefs(newGeom));

    RefPtr<ID2D1GeometrySink> currentSink;
    newGeom->Open(getter_AddRefs(currentSink));

    if (iter->mGeometry) {
      pathGeom->CombineWithGeometry(iter->mGeometry,
                                    D2D1_COMBINE_MODE_INTERSECT,
                                    iter->mTransform, currentSink);
    } else {
      RefPtr<ID2D1Geometry> rectGeom = ConvertRectToGeometry(iter->mBounds);
      pathGeom->CombineWithGeometry(rectGeom, D2D1_COMBINE_MODE_INTERSECT,
                                    D2D1::IdentityMatrix(), currentSink);
    }

    currentSink->Close();

    pathGeom = newGeom.forget();
  }

  // For now we need mCurrentClippedGeometry to always be non-nullptr. This
  // method might seem a little strange but it is just fine, if pathGeom is
  // nullptr pathRect will always still contain 1 clip unaccounted for
  // regardless of mCurrentClipBounds.
  if (!pathGeom) {
    pathGeom = ConvertRectToGeometry(pathRect);
  }
  mCurrentClippedGeometry = pathGeom.forget();
  *aClipBounds = mCurrentClipBounds;
  RefPtr<ID2D1Geometry> clippedGeometry(mCurrentClippedGeometry);
  return clippedGeometry.forget();
}

already_AddRefed<ID2D1Geometry> DrawTargetD2D1::GetInverseClippedGeometry() {
  IntRect bounds;
  RefPtr<ID2D1Geometry> geom = GetClippedGeometry(&bounds);
  RefPtr<ID2D1RectangleGeometry> rectGeom;
  RefPtr<ID2D1PathGeometry> inverseGeom;

  factory()->CreateRectangleGeometry(
      D2D1::RectF(0, 0, mSize.width, mSize.height), getter_AddRefs(rectGeom));
  factory()->CreatePathGeometry(getter_AddRefs(inverseGeom));
  RefPtr<ID2D1GeometrySink> sink;
  inverseGeom->Open(getter_AddRefs(sink));
  rectGeom->CombineWithGeometry(geom, D2D1_COMBINE_MODE_EXCLUDE,
                                D2D1::IdentityMatrix(), sink);
  sink->Close();

  return inverseGeom.forget();
}

void DrawTargetD2D1::PopAllClips() {
  if (CurrentLayer().mClipsArePushed) {
    PopClipsFromDC(mDC);

    CurrentLayer().mClipsArePushed = false;
  }
}

void DrawTargetD2D1::PushAllClips() {
  if (!CurrentLayer().mClipsArePushed) {
    PushClipsToDC(mDC);

    CurrentLayer().mClipsArePushed = true;
  }
}

void DrawTargetD2D1::PushClipsToDC(ID2D1DeviceContext* aDC,
                                   bool aForceIgnoreAlpha,
                                   const D2D1_RECT_F& aMaxRect) {
  mDC->SetTransform(D2D1::IdentityMatrix());
  mTransformDirty = true;

  for (auto iter = CurrentLayer().mPushedClips.begin();
       iter != CurrentLayer().mPushedClips.end(); iter++) {
    if (iter->mGeometry) {
      PushD2DLayer(aDC, iter->mGeometry, iter->mTransform,
                   iter->mIsPixelAligned, aForceIgnoreAlpha, aMaxRect);
    } else {
      mDC->PushAxisAlignedClip(iter->mBounds,
                               iter->mIsPixelAligned
                                   ? D2D1_ANTIALIAS_MODE_ALIASED
                                   : D2D1_ANTIALIAS_MODE_PER_PRIMITIVE);
    }
  }
}

void DrawTargetD2D1::PopClipsFromDC(ID2D1DeviceContext* aDC) {
  for (int i = CurrentLayer().mPushedClips.size() - 1; i >= 0; i--) {
    if (CurrentLayer().mPushedClips[i].mGeometry) {
      aDC->PopLayer();
    } else {
      aDC->PopAxisAlignedClip();
    }
  }
}

already_AddRefed<ID2D1Brush> DrawTargetD2D1::CreateTransparentBlackBrush() {
  return GetSolidColorBrush(D2D1::ColorF(0, 0));
}

already_AddRefed<ID2D1SolidColorBrush> DrawTargetD2D1::GetSolidColorBrush(
    const D2D_COLOR_F& aColor) {
  RefPtr<ID2D1SolidColorBrush> brush = mSolidColorBrush;
  brush->SetColor(aColor);
  return brush.forget();
}

already_AddRefed<ID2D1Brush> DrawTargetD2D1::CreateBrushForPattern(
    const Pattern& aPattern, Float aAlpha) {
  if (!IsPatternSupportedByD2D(aPattern)) {
    return GetSolidColorBrush(D2D1::ColorF(1.0f, 1.0f, 1.0f, 1.0f));
  }

  if (aPattern.GetType() == PatternType::COLOR) {
    DeviceColor color = static_cast<const ColorPattern*>(&aPattern)->mColor;
    return GetSolidColorBrush(
        D2D1::ColorF(color.r, color.g, color.b, color.a * aAlpha));
  }
  if (aPattern.GetType() == PatternType::LINEAR_GRADIENT) {
    RefPtr<ID2D1LinearGradientBrush> gradBrush;
    const LinearGradientPattern* pat =
        static_cast<const LinearGradientPattern*>(&aPattern);

    GradientStopsD2D* stops = static_cast<GradientStopsD2D*>(pat->mStops.get());

    if (!stops) {
      gfxDebug() << "No stops specified for gradient pattern.";
      return CreateTransparentBlackBrush();
    }

    if (pat->mBegin == pat->mEnd) {
      return CreateTransparentBlackBrush();
    }

    mDC->CreateLinearGradientBrush(
        D2D1::LinearGradientBrushProperties(D2DPoint(pat->mBegin),
                                            D2DPoint(pat->mEnd)),
        D2D1::BrushProperties(aAlpha, D2DMatrix(pat->mMatrix)),
        stops->mStopCollection, getter_AddRefs(gradBrush));

    if (!gradBrush) {
      gfxWarning() << "Couldn't create gradient brush.";
      return CreateTransparentBlackBrush();
    }

    return gradBrush.forget();
  }
  if (aPattern.GetType() == PatternType::RADIAL_GRADIENT) {
    RefPtr<ID2D1RadialGradientBrush> gradBrush;
    const RadialGradientPattern* pat =
        static_cast<const RadialGradientPattern*>(&aPattern);

    GradientStopsD2D* stops = static_cast<GradientStopsD2D*>(pat->mStops.get());

    if (!stops) {
      gfxDebug() << "No stops specified for gradient pattern.";
      return CreateTransparentBlackBrush();
    }

    if (pat->mCenter1 == pat->mCenter2 && pat->mRadius1 == pat->mRadius2) {
      return CreateTransparentBlackBrush();
    }

    // This will not be a complex radial gradient brush.
    mDC->CreateRadialGradientBrush(
        D2D1::RadialGradientBrushProperties(
            D2DPoint(pat->mCenter2), D2DPoint(pat->mCenter1 - pat->mCenter2),
            pat->mRadius2, pat->mRadius2),
        D2D1::BrushProperties(aAlpha, D2DMatrix(pat->mMatrix)),
        stops->mStopCollection, getter_AddRefs(gradBrush));

    if (!gradBrush) {
      gfxWarning() << "Couldn't create gradient brush.";
      return CreateTransparentBlackBrush();
    }

    return gradBrush.forget();
  }
  if (aPattern.GetType() == PatternType::SURFACE) {
    const SurfacePattern* pat = static_cast<const SurfacePattern*>(&aPattern);

    if (!pat->mSurface) {
      gfxDebug() << "No source surface specified for surface pattern";
      return CreateTransparentBlackBrush();
    }

    D2D1_RECT_F samplingBounds;
    Matrix mat = pat->mMatrix;

    MOZ_ASSERT(pat->mSurface->IsValid());

    RefPtr<SourceSurface> surf = pat->mSurface;

    RefPtr<ID2D1Image> image = GetImageForSurface(
        surf, mat, pat->mExtendMode,
        !pat->mSamplingRect.IsEmpty() ? &pat->mSamplingRect : nullptr);

    if (!image) {
      return CreateTransparentBlackBrush();
    }

    if (surf->GetFormat() == SurfaceFormat::A8) {
      // See bug 1251431, at least FillOpacityMask does not appear to allow a
      // source bitmapbrush with source format A8. This creates a BGRA surface
      // with the same alpha values that the A8 surface has.
      RefPtr<ID2D1Bitmap> bitmap;
      HRESULT hr = image->QueryInterface((ID2D1Bitmap**)getter_AddRefs(bitmap));
      if (SUCCEEDED(hr) && bitmap) {
        RefPtr<ID2D1Image> oldTarget;
        RefPtr<ID2D1Bitmap1> tmpBitmap;
        mDC->CreateBitmap(D2D1::SizeU(pat->mSurface->GetSize().width,
                                      pat->mSurface->GetSize().height),
                          nullptr, 0,
                          D2D1::BitmapProperties1(
                              D2D1_BITMAP_OPTIONS_TARGET,
                              D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM,
                                                D2D1_ALPHA_MODE_PREMULTIPLIED)),
                          getter_AddRefs(tmpBitmap));

        if (!tmpBitmap) {
          return CreateTransparentBlackBrush();
        }

        mDC->GetTarget(getter_AddRefs(oldTarget));
        mDC->SetTarget(tmpBitmap);

        RefPtr<ID2D1SolidColorBrush> brush;
        mDC->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::White),
                                   getter_AddRefs(brush));
        mDC->FillOpacityMask(bitmap, brush);
        mDC->SetTarget(oldTarget);
        image = tmpBitmap;
      }
    }

    if (pat->mSamplingRect.IsEmpty()) {
      RefPtr<ID2D1Bitmap> bitmap;
      HRESULT hr = image->QueryInterface((ID2D1Bitmap**)getter_AddRefs(bitmap));
      if (SUCCEEDED(hr) && bitmap) {
        /**
         * Create the brush with the proper repeat modes.
         */
        RefPtr<ID2D1BitmapBrush> bitmapBrush;
        D2D1_EXTEND_MODE xRepeat = D2DExtend(pat->mExtendMode, Axis::X_AXIS);
        D2D1_EXTEND_MODE yRepeat = D2DExtend(pat->mExtendMode, Axis::Y_AXIS);

        mDC->CreateBitmapBrush(
            bitmap,
            D2D1::BitmapBrushProperties(xRepeat, yRepeat,
                                        D2DFilter(pat->mSamplingFilter)),
            D2D1::BrushProperties(aAlpha, D2DMatrix(mat)),
            getter_AddRefs(bitmapBrush));
        if (!bitmapBrush) {
          gfxWarning() << "Couldn't create bitmap brush!";
          return CreateTransparentBlackBrush();
        }
        return bitmapBrush.forget();
      }
    }

    RefPtr<ID2D1ImageBrush> imageBrush;
    if (pat->mSamplingRect.IsEmpty()) {
      samplingBounds = D2D1::RectF(0, 0, Float(pat->mSurface->GetSize().width),
                                   Float(pat->mSurface->GetSize().height));
    } else if (surf->GetType() == SurfaceType::D2D1_1_IMAGE) {
      samplingBounds = D2DRect(pat->mSamplingRect);
      mat.PreTranslate(pat->mSamplingRect.X(), pat->mSamplingRect.Y());
    } else {
      // We will do a partial upload of the sampling restricted area from
      // GetImageForSurface.
      samplingBounds = D2D1::RectF(0, 0, pat->mSamplingRect.Width(),
                                   pat->mSamplingRect.Height());
    }

    D2D1_EXTEND_MODE xRepeat = D2DExtend(pat->mExtendMode, Axis::X_AXIS);
    D2D1_EXTEND_MODE yRepeat = D2DExtend(pat->mExtendMode, Axis::Y_AXIS);

    mDC->CreateImageBrush(
        image,
        D2D1::ImageBrushProperties(samplingBounds, xRepeat, yRepeat,
                                   D2DInterpolationMode(pat->mSamplingFilter)),
        D2D1::BrushProperties(aAlpha, D2DMatrix(mat)),
        getter_AddRefs(imageBrush));

    if (!imageBrush) {
      gfxWarning() << "Couldn't create image brush!";
      return CreateTransparentBlackBrush();
    }

    return imageBrush.forget();
  }

  gfxWarning() << "Invalid pattern type detected.";
  return CreateTransparentBlackBrush();
}

already_AddRefed<ID2D1Image> DrawTargetD2D1::GetImageForSurface(
    SourceSurface* aSurface, Matrix& aSourceTransform, ExtendMode aExtendMode,
    const IntRect* aSourceRect, bool aUserSpace) {
  RefPtr<ID2D1Image> image;
  RefPtr<SourceSurface> surface = aSurface->GetUnderlyingSurface();

  if (!surface) {
    return nullptr;
  }

  switch (surface->GetType()) {
    case SurfaceType::D2D1_1_IMAGE: {
      SourceSurfaceD2D1* surf = static_cast<SourceSurfaceD2D1*>(surface.get());
      image = surf->GetImage();
      AddDependencyOnSource(surf);
    } break;
    default: {
      RefPtr<DataSourceSurface> dataSurf = surface->GetDataSurface();
      if (!dataSurf) {
        gfxWarning() << "Invalid surface type.";
        return nullptr;
      }
      Matrix transform = aUserSpace ? mTransform : Matrix();
      return CreatePartialBitmapForSurface(dataSurf, transform, mSize,
                                           aExtendMode, aSourceTransform, mDC,
                                           aSourceRect);
    } break;
  }

  return image.forget();
}

already_AddRefed<SourceSurface> DrawTargetD2D1::OptimizeSourceSurface(
    SourceSurface* aSurface) const {
  if (aSurface->GetType() == SurfaceType::D2D1_1_IMAGE) {
    RefPtr<SourceSurface> surface(aSurface);
    return surface.forget();
  }

  RefPtr<ID2D1DeviceContext> dc = Factory::GetD2DDeviceContext();
  if (!dc) {
    return nullptr;
  }

  RefPtr<DataSourceSurface> data = aSurface->GetDataSurface();

  std::optional<SurfaceFormat> convertTo;
  switch (data->GetFormat()) {
    case gfx::SurfaceFormat::R8G8B8X8:
      convertTo = SurfaceFormat::B8G8R8X8;
      break;
    case gfx::SurfaceFormat::R8G8B8A8:
      convertTo = SurfaceFormat::B8G8R8X8;
      break;
    default:
      break;
  }

  if (convertTo) {
    const auto size = data->GetSize();
    const RefPtr<DrawTarget> dt =
        Factory::CreateDrawTarget(BackendType::SKIA, size, *convertTo);
    if (!dt) {
      return nullptr;
    }
    dt->CopySurface(data, {{}, size}, {});

    const RefPtr<SourceSurface> snapshot = dt->Snapshot();
    data = snapshot->GetDataSurface();
  }

  RefPtr<ID2D1Bitmap1> bitmap;
  {
    DataSourceSurface::ScopedMap map(data, DataSourceSurface::READ);
    if (MOZ2D_WARN_IF(!map.IsMapped())) {
      return nullptr;
    }

    HRESULT hr = dc->CreateBitmap(
        D2DIntSize(data->GetSize()), map.GetData(), map.GetStride(),
        D2D1::BitmapProperties1(D2D1_BITMAP_OPTIONS_NONE,
                                D2DPixelFormat(data->GetFormat())),
        getter_AddRefs(bitmap));

    if (FAILED(hr)) {
      gfxCriticalError(CriticalLog::DefaultOptions(
          Factory::ReasonableSurfaceSize(data->GetSize())))
          << "[D2D1.1] 4CreateBitmap failure " << data->GetSize()
          << " Code: " << hexa(hr) << " format " << (int)data->GetFormat();
    }
  }

  if (!bitmap) {
    return data.forget();
  }

  return MakeAndAddRef<SourceSurfaceD2D1>(bitmap.get(), dc.get(),
                                          data->GetFormat(), data->GetSize());
}

void DrawTargetD2D1::PushD2DLayer(ID2D1DeviceContext* aDC,
                                  ID2D1Geometry* aGeometry,
                                  const D2D1_MATRIX_3X2_F& aTransform,
                                  bool aPixelAligned, bool aForceIgnoreAlpha,
                                  const D2D1_RECT_F& aMaxRect) {
  D2D1_LAYER_OPTIONS1 options = D2D1_LAYER_OPTIONS1_NONE;

  if (CurrentLayer().mIsOpaque || aForceIgnoreAlpha) {
    options = D2D1_LAYER_OPTIONS1_IGNORE_ALPHA |
              D2D1_LAYER_OPTIONS1_INITIALIZE_FROM_BACKGROUND;
  }

  D2D1_ANTIALIAS_MODE antialias = aPixelAligned
                                      ? D2D1_ANTIALIAS_MODE_ALIASED
                                      : D2D1_ANTIALIAS_MODE_PER_PRIMITIVE;

  mDC->PushLayer(D2D1::LayerParameters1(aMaxRect, aGeometry, antialias,
                                        aTransform, 1.0, nullptr, options),
                 nullptr);
}

bool DrawTargetD2D1::IsDeviceContextValid() const {
  uint32_t seqNo;
  return mDC && Factory::GetD2D1Device(&seqNo) && seqNo == mDeviceSeq;
}

}  // namespace gfx
}  // namespace mozilla