summaryrefslogtreecommitdiffstats
path: root/gfx/2d/DrawTargetCairo.cpp
blob: c0e4f0af26a720de17dac410d3b00a4342bac446 (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
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 * 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 "DrawTargetCairo.h"

#include "SourceSurfaceCairo.h"
#include "PathCairo.h"
#include "HelpersCairo.h"
#include "ScaledFontBase.h"
#include "BorrowedContext.h"
#include "FilterNodeSoftware.h"
#include "mozilla/Scoped.h"
#include "mozilla/UniquePtr.h"
#include "mozilla/Vector.h"

#include "cairo.h"
#include "cairo-tee.h"
#include <string.h>

#include "Blur.h"
#include "Logging.h"
#include "Tools.h"

#ifdef CAIRO_HAS_QUARTZ_SURFACE
#include "cairo-quartz.h"
#ifdef MOZ_WIDGET_COCOA
#include <ApplicationServices/ApplicationServices.h>
#endif
#endif

#ifdef CAIRO_HAS_XLIB_SURFACE
#include "cairo-xlib.h"
#include "cairo-xlib-xrender.h"
#endif

#ifdef CAIRO_HAS_WIN32_SURFACE
#include "cairo-win32.h"
#endif

#define PIXMAN_DONT_DEFINE_STDINT
#include "pixman.h"

#include <algorithm>

// 2^23
#define CAIRO_COORD_MAX (Float(0x7fffff))

namespace mozilla {

MOZ_TYPE_SPECIFIC_SCOPED_POINTER_TEMPLATE(ScopedCairoSurface, cairo_surface_t, cairo_surface_destroy);

namespace gfx {

cairo_surface_t *DrawTargetCairo::mDummySurface;

namespace {

// An RAII class to prepare to draw a context and optional path. Saves and
// restores the context on construction/destruction.
class AutoPrepareForDrawing
{
public:
  AutoPrepareForDrawing(DrawTargetCairo* dt, cairo_t* ctx)
    : mCtx(ctx)
  {
    dt->PrepareForDrawing(ctx);
    cairo_save(mCtx);
    MOZ_ASSERT(cairo_status(mCtx) || dt->GetTransform() == GetTransform());
  }

  AutoPrepareForDrawing(DrawTargetCairo* dt, cairo_t* ctx, const Path* path)
    : mCtx(ctx)
  {
    dt->PrepareForDrawing(ctx, path);
    cairo_save(mCtx);
    MOZ_ASSERT(cairo_status(mCtx) || dt->GetTransform() == GetTransform());
  }

  ~AutoPrepareForDrawing()
  {
    cairo_restore(mCtx);
    cairo_status_t status = cairo_status(mCtx);
    if (status) {
      gfxWarning() << "DrawTargetCairo context in error state: " << cairo_status_to_string(status) << "(" << status << ")";
    }
  }

private:
#ifdef DEBUG
  Matrix GetTransform()
  {
    cairo_matrix_t mat;
    cairo_get_matrix(mCtx, &mat);
    return Matrix(mat.xx, mat.yx, mat.xy, mat.yy, mat.x0, mat.y0);
  }
#endif

  cairo_t* mCtx;
};

/* Clamp r to (0,0) (2^23,2^23)
 * these are to be device coordinates.
 *
 * Returns false if the rectangle is completely out of bounds,
 * true otherwise.
 *
 * This function assumes that it will be called with a rectangle being
 * drawn into a surface with an identity transformation matrix; that
 * is, anything above or to the left of (0,0) will be offscreen.
 *
 * First it checks if the rectangle is entirely beyond
 * CAIRO_COORD_MAX; if so, it can't ever appear on the screen --
 * false is returned.
 *
 * Then it shifts any rectangles with x/y < 0 so that x and y are = 0,
 * and adjusts the width and height appropriately.  For example, a
 * rectangle from (0,-5) with dimensions (5,10) will become a
 * rectangle from (0,0) with dimensions (5,5).
 *
 * If after negative x/y adjustment to 0, either the width or height
 * is negative, then the rectangle is completely offscreen, and
 * nothing is drawn -- false is returned.
 *
 * Finally, if x+width or y+height are greater than CAIRO_COORD_MAX,
 * the width and height are clamped such x+width or y+height are equal
 * to CAIRO_COORD_MAX, and true is returned.
 */
static bool
ConditionRect(Rect& r) {
  // if either x or y is way out of bounds;
  // note that we don't handle negative w/h here
  if (r.X() > CAIRO_COORD_MAX || r.Y() > CAIRO_COORD_MAX)
    return false;

  if (r.X() < 0.f) {
    r.width += r.X();
    if (r.width < 0.f)
      return false;
    r.x = 0.f;
  }

  if (r.XMost() > CAIRO_COORD_MAX) {
    r.width = CAIRO_COORD_MAX - r.X();
  }

  if (r.Y() < 0.f) {
    r.height += r.Y();
    if (r.Height() < 0.f)
      return false;

    r.y = 0.f;
  }

  if (r.YMost() > CAIRO_COORD_MAX) {
    r.height = CAIRO_COORD_MAX - r.Y();
  }
  return true;
}

} // end anonymous namespace

static bool
SupportsSelfCopy(cairo_surface_t* surface)
{
  switch (cairo_surface_get_type(surface))
  {
#ifdef CAIRO_HAS_QUARTZ_SURFACE
    case CAIRO_SURFACE_TYPE_QUARTZ:
      return true;
#endif
#ifdef CAIRO_HAS_WIN32_SURFACE
    case CAIRO_SURFACE_TYPE_WIN32:
    case CAIRO_SURFACE_TYPE_WIN32_PRINTING:
      return true;
#endif
    default:
      return false;
  }
}

static bool
PatternIsCompatible(const Pattern& aPattern)
{
  switch (aPattern.GetType())
  {
    case PatternType::LINEAR_GRADIENT:
    {
      const LinearGradientPattern& pattern = static_cast<const LinearGradientPattern&>(aPattern);
      return pattern.mStops->GetBackendType() == BackendType::CAIRO;
    }
    case PatternType::RADIAL_GRADIENT:
    {
      const RadialGradientPattern& pattern = static_cast<const RadialGradientPattern&>(aPattern);
      return pattern.mStops->GetBackendType() == BackendType::CAIRO;
    }
    default:
      return true;
  }
}

static cairo_user_data_key_t surfaceDataKey;

void
ReleaseData(void* aData)
{
  DataSourceSurface *data = static_cast<DataSourceSurface*>(aData);
  data->Unmap();
  data->Release();
}

cairo_surface_t*
CopyToImageSurface(unsigned char *aData,
                   const IntRect &aRect,
                   int32_t aStride,
                   SurfaceFormat aFormat)
{
  MOZ_ASSERT(aData);

  cairo_surface_t* surf = cairo_image_surface_create(GfxFormatToCairoFormat(aFormat),
                                                     aRect.width,
                                                     aRect.height);
  // In certain scenarios, requesting larger than 8k image fails.  Bug 803568
  // covers the details of how to run into it, but the full detailed
  // investigation hasn't been done to determine the underlying cause.  We
  // will just handle the failure to allocate the surface to avoid a crash.
  if (cairo_surface_status(surf)) {
    gfxWarning() << "Invalid surface DTC " << cairo_surface_status(surf);
    return nullptr;
  }

  unsigned char* surfData = cairo_image_surface_get_data(surf);
  int surfStride = cairo_image_surface_get_stride(surf);
  int32_t pixelWidth = BytesPerPixel(aFormat);

  unsigned char* source = aData +
                          aRect.y * aStride +
                          aRect.x * pixelWidth;

  MOZ_ASSERT(aStride >= aRect.width * pixelWidth);
  for (int32_t y = 0; y < aRect.height; ++y) {
    memcpy(surfData + y * surfStride,
           source + y * aStride,
           aRect.width * pixelWidth);
  }
  cairo_surface_mark_dirty(surf);
  return surf;
}

/**
 * If aSurface can be represented as a surface of type
 * CAIRO_SURFACE_TYPE_IMAGE then returns that surface. Does
 * not add a reference.
 */
cairo_surface_t* GetAsImageSurface(cairo_surface_t* aSurface)
{
  if (cairo_surface_get_type(aSurface) == CAIRO_SURFACE_TYPE_IMAGE) {
    return aSurface;
#ifdef CAIRO_HAS_WIN32_SURFACE
  } else if (cairo_surface_get_type(aSurface) == CAIRO_SURFACE_TYPE_WIN32) {
    return cairo_win32_surface_get_image(aSurface);
#endif
  }

  return nullptr;
}

cairo_surface_t* CreateSubImageForData(unsigned char* aData,
                                       const IntRect& aRect,
                                       int aStride,
                                       SurfaceFormat aFormat)
{
  if (!aData) {
    gfxWarning() << "DrawTargetCairo.CreateSubImageForData null aData";
    return nullptr;
  }
  unsigned char *data = aData +
                        aRect.y * aStride +
                        aRect.x * BytesPerPixel(aFormat);

  cairo_surface_t *image =
    cairo_image_surface_create_for_data(data,
                                        GfxFormatToCairoFormat(aFormat),
                                        aRect.width,
                                        aRect.height,
                                        aStride);
  cairo_surface_set_device_offset(image, -aRect.x, -aRect.y);
  return image;
}

/**
 * Returns a referenced cairo_surface_t representing the
 * sub-image specified by aSubImage.
 */
cairo_surface_t* ExtractSubImage(cairo_surface_t* aSurface,
                                 const IntRect& aSubImage,
                                 SurfaceFormat aFormat)
{
  // No need to worry about retaining a reference to the original
  // surface since the only caller of this function guarantees
  // that aSurface will stay alive as long as the result

  cairo_surface_t* image = GetAsImageSurface(aSurface);
  if (image) {
    image = CreateSubImageForData(cairo_image_surface_get_data(image),
                                  aSubImage,
                                  cairo_image_surface_get_stride(image),
                                  aFormat);
    return image;
  }

  cairo_surface_t* similar =
    cairo_surface_create_similar(aSurface,
                                 cairo_surface_get_content(aSurface),
                                 aSubImage.width, aSubImage.height);

  cairo_t* ctx = cairo_create(similar);
  cairo_set_operator(ctx, CAIRO_OPERATOR_SOURCE);
  cairo_set_source_surface(ctx, aSurface, -aSubImage.x, -aSubImage.y);
  cairo_paint(ctx);
  cairo_destroy(ctx);

  cairo_surface_set_device_offset(similar, -aSubImage.x, -aSubImage.y);
  return similar;
}

/**
 * Returns cairo surface for the given SourceSurface.
 * If possible, it will use the cairo_surface associated with aSurface,
 * otherwise, it will create a new cairo_surface.
 * In either case, the caller must call cairo_surface_destroy on the
 * result when it is done with it.
 */
cairo_surface_t*
GetCairoSurfaceForSourceSurface(SourceSurface *aSurface,
                                bool aExistingOnly = false,
                                const IntRect& aSubImage = IntRect())
{
  if (!aSurface) {
    return nullptr;
  }

  IntRect subimage = IntRect(IntPoint(), aSurface->GetSize());
  if (!aSubImage.IsEmpty()) {
    MOZ_ASSERT(!aExistingOnly);
    MOZ_ASSERT(subimage.Contains(aSubImage));
    subimage = aSubImage;
  }

  if (aSurface->GetType() == SurfaceType::CAIRO) {
    cairo_surface_t* surf = static_cast<SourceSurfaceCairo*>(aSurface)->GetSurface();
    if (aSubImage.IsEmpty()) {
      cairo_surface_reference(surf);
    } else {
      surf = ExtractSubImage(surf, subimage, aSurface->GetFormat());
    }
    return surf;
  }

  if (aSurface->GetType() == SurfaceType::CAIRO_IMAGE) {
    cairo_surface_t* surf =
      static_cast<const DataSourceSurfaceCairo*>(aSurface)->GetSurface();
    if (aSubImage.IsEmpty()) {
      cairo_surface_reference(surf);
    } else {
      surf = ExtractSubImage(surf, subimage, aSurface->GetFormat());
    }
    return surf;
  }

  if (aExistingOnly) {
    return nullptr;
  }

  RefPtr<DataSourceSurface> data = aSurface->GetDataSurface();
  if (!data) {
    return nullptr;
  }

  DataSourceSurface::MappedSurface map;
  if (!data->Map(DataSourceSurface::READ, &map)) {
    return nullptr;
  }

  cairo_surface_t* surf =
    CreateSubImageForData(map.mData, subimage,
                          map.mStride, data->GetFormat());

  // In certain scenarios, requesting larger than 8k image fails.  Bug 803568
  // covers the details of how to run into it, but the full detailed
  // investigation hasn't been done to determine the underlying cause.  We
  // will just handle the failure to allocate the surface to avoid a crash.
  if (!surf || cairo_surface_status(surf)) {
    if (surf && (cairo_surface_status(surf) == CAIRO_STATUS_INVALID_STRIDE)) {
      // If we failed because of an invalid stride then copy into
      // a new surface with a stride that cairo chooses. No need to
      // set user data since we're not dependent on the original
      // data.
      cairo_surface_t* result =
        CopyToImageSurface(map.mData,
                           subimage,
                           map.mStride,
                           data->GetFormat());
      data->Unmap();
      return result;
    }
    data->Unmap();
    return nullptr;
  }

  cairo_surface_set_user_data(surf,
                              &surfaceDataKey,
                              data.forget().take(),
                              ReleaseData);
  return surf;
}

// An RAII class to temporarily clear any device offset set
// on a surface. Note that this does not take a reference to the
// surface.
class AutoClearDeviceOffset
{
public:
  explicit AutoClearDeviceOffset(SourceSurface* aSurface)
    : mSurface(nullptr)
    , mX(0)
    , mY(0)
  {
    Init(aSurface);
  }

  explicit AutoClearDeviceOffset(const Pattern& aPattern)
    : mSurface(nullptr)
  {
    if (aPattern.GetType() == PatternType::SURFACE) {
      const SurfacePattern& pattern = static_cast<const SurfacePattern&>(aPattern);
      Init(pattern.mSurface);
    }
  }

  ~AutoClearDeviceOffset()
  {
    if (mSurface) {
      cairo_surface_set_device_offset(mSurface, mX, mY);
    }
  }

private:
  void Init(SourceSurface* aSurface)
  {
    cairo_surface_t* surface = GetCairoSurfaceForSourceSurface(aSurface, true);
    if (surface) {
      Init(surface);
      cairo_surface_destroy(surface);
    }
  }

  void Init(cairo_surface_t *aSurface)
  {
    mSurface = aSurface;
    cairo_surface_get_device_offset(mSurface, &mX, &mY);
    cairo_surface_set_device_offset(mSurface, 0, 0);
  }

  cairo_surface_t* mSurface;
  double mX;
  double mY;
};

static inline void
CairoPatternAddGradientStop(cairo_pattern_t* aPattern,
                            const GradientStop &aStop,
                            Float aNudge = 0)
{
  cairo_pattern_add_color_stop_rgba(aPattern, aStop.offset + aNudge,
                                    aStop.color.r, aStop.color.g, aStop.color.b,
                                    aStop.color.a);

}

// Never returns nullptr. As such, you must always pass in Cairo-compatible
// patterns, most notably gradients with a GradientStopCairo.
// The pattern returned must have cairo_pattern_destroy() called on it by the
// caller.
// As the cairo_pattern_t returned may depend on the Pattern passed in, the
// lifetime of the cairo_pattern_t returned must not exceed the lifetime of the
// Pattern passed in.
static cairo_pattern_t*
GfxPatternToCairoPattern(const Pattern& aPattern,
                         Float aAlpha,
                         const Matrix& aTransform)
{
  cairo_pattern_t* pat;
  const Matrix* matrix = nullptr;

  switch (aPattern.GetType())
  {
    case PatternType::COLOR:
    {
      Color color = static_cast<const ColorPattern&>(aPattern).mColor;
      pat = cairo_pattern_create_rgba(color.r, color.g, color.b, color.a * aAlpha);
      break;
    }

    case PatternType::SURFACE:
    {
      const SurfacePattern& pattern = static_cast<const SurfacePattern&>(aPattern);
      cairo_surface_t* surf = GetCairoSurfaceForSourceSurface(pattern.mSurface,
                                                              false,
                                                              pattern.mSamplingRect);
      if (!surf)
        return nullptr;

      pat = cairo_pattern_create_for_surface(surf);

      matrix = &pattern.mMatrix;

      cairo_pattern_set_filter(pat, GfxSamplingFilterToCairoFilter(pattern.mSamplingFilter));
      cairo_pattern_set_extend(pat, GfxExtendToCairoExtend(pattern.mExtendMode));

      cairo_surface_destroy(surf);
      break;
    }
    case PatternType::LINEAR_GRADIENT:
    {
      const LinearGradientPattern& pattern = static_cast<const LinearGradientPattern&>(aPattern);

      pat = cairo_pattern_create_linear(pattern.mBegin.x, pattern.mBegin.y,
                                        pattern.mEnd.x, pattern.mEnd.y);

      MOZ_ASSERT(pattern.mStops->GetBackendType() == BackendType::CAIRO);
      GradientStopsCairo* cairoStops = static_cast<GradientStopsCairo*>(pattern.mStops.get());
      cairo_pattern_set_extend(pat, GfxExtendToCairoExtend(cairoStops->GetExtendMode()));

      matrix = &pattern.mMatrix;

      const std::vector<GradientStop>& stops = cairoStops->GetStops();
      for (size_t i = 0; i < stops.size(); ++i) {
        CairoPatternAddGradientStop(pat, stops[i]);
      }

      break;
    }
    case PatternType::RADIAL_GRADIENT:
    {
      const RadialGradientPattern& pattern = static_cast<const RadialGradientPattern&>(aPattern);

      pat = cairo_pattern_create_radial(pattern.mCenter1.x, pattern.mCenter1.y, pattern.mRadius1,
                                        pattern.mCenter2.x, pattern.mCenter2.y, pattern.mRadius2);

      MOZ_ASSERT(pattern.mStops->GetBackendType() == BackendType::CAIRO);
      GradientStopsCairo* cairoStops = static_cast<GradientStopsCairo*>(pattern.mStops.get());
      cairo_pattern_set_extend(pat, GfxExtendToCairoExtend(cairoStops->GetExtendMode()));

      matrix = &pattern.mMatrix;

      const std::vector<GradientStop>& stops = cairoStops->GetStops();
      for (size_t i = 0; i < stops.size(); ++i) {
        CairoPatternAddGradientStop(pat, stops[i]);
      }

      break;
    }
    default:
    {
      // We should support all pattern types!
      MOZ_ASSERT(false);
    }
  }

  // The pattern matrix is a matrix that transforms the pattern into user
  // space. Cairo takes a matrix that converts from user space to pattern
  // space. Cairo therefore needs the inverse.
  if (matrix) {
    cairo_matrix_t mat;
    GfxMatrixToCairoMatrix(*matrix, mat);
    cairo_matrix_invert(&mat);
    cairo_pattern_set_matrix(pat, &mat);
  }

  return pat;
}

static bool
NeedIntermediateSurface(const Pattern& aPattern, const DrawOptions& aOptions)
{
  // We pre-multiply colours' alpha by the global alpha, so we don't need to
  // use an intermediate surface for them.
  if (aPattern.GetType() == PatternType::COLOR)
    return false;

  if (aOptions.mAlpha == 1.0)
    return false;

  return true;
}

DrawTargetCairo::DrawTargetCairo()
  : mContext(nullptr)
  , mSurface(nullptr)
  , mTransformSingular(false)
  , mLockedBits(nullptr)
  , mFontOptions(nullptr)
{
}

DrawTargetCairo::~DrawTargetCairo()
{
  cairo_destroy(mContext);
  if (mSurface) {
    cairo_surface_destroy(mSurface);
    mSurface = nullptr;
  }
  if (mFontOptions) {
    cairo_font_options_destroy(mFontOptions);
    mFontOptions = nullptr;
  }
  MOZ_ASSERT(!mLockedBits);
}

bool
DrawTargetCairo::IsValid() const
{
  return mSurface && !cairo_surface_status(mSurface) &&
         mContext && !cairo_surface_status(cairo_get_group_target(mContext));
}

DrawTargetType
DrawTargetCairo::GetType() const
{
  if (mContext) {
    cairo_surface_type_t type = cairo_surface_get_type(mSurface);
    if (type == CAIRO_SURFACE_TYPE_TEE) {
      type = cairo_surface_get_type(cairo_tee_surface_index(mSurface, 0));
      MOZ_ASSERT(type != CAIRO_SURFACE_TYPE_TEE, "C'mon!");
      MOZ_ASSERT(type == cairo_surface_get_type(cairo_tee_surface_index(mSurface, 1)),
                 "What should we do here?");
    }
    switch (type) {
    case CAIRO_SURFACE_TYPE_PDF:
    case CAIRO_SURFACE_TYPE_PS:
    case CAIRO_SURFACE_TYPE_SVG:
    case CAIRO_SURFACE_TYPE_WIN32_PRINTING:
    case CAIRO_SURFACE_TYPE_XML:
      return DrawTargetType::VECTOR;

    case CAIRO_SURFACE_TYPE_VG:
    case CAIRO_SURFACE_TYPE_GL:
    case CAIRO_SURFACE_TYPE_GLITZ:
    case CAIRO_SURFACE_TYPE_QUARTZ:
    case CAIRO_SURFACE_TYPE_DIRECTFB:
      return DrawTargetType::HARDWARE_RASTER;

    case CAIRO_SURFACE_TYPE_SKIA:
    case CAIRO_SURFACE_TYPE_QT:
      MOZ_FALLTHROUGH_ASSERT("Can't determine actual DrawTargetType for DrawTargetCairo - assuming SOFTWARE_RASTER");
    case CAIRO_SURFACE_TYPE_IMAGE:
    case CAIRO_SURFACE_TYPE_XLIB:
    case CAIRO_SURFACE_TYPE_XCB:
    case CAIRO_SURFACE_TYPE_WIN32:
    case CAIRO_SURFACE_TYPE_BEOS:
    case CAIRO_SURFACE_TYPE_OS2:
    case CAIRO_SURFACE_TYPE_QUARTZ_IMAGE:
    case CAIRO_SURFACE_TYPE_SCRIPT:
    case CAIRO_SURFACE_TYPE_RECORDING:
    case CAIRO_SURFACE_TYPE_DRM:
    case CAIRO_SURFACE_TYPE_SUBSURFACE:
    case CAIRO_SURFACE_TYPE_TEE: // included to silence warning about unhandled enum value
      return DrawTargetType::SOFTWARE_RASTER;
    default:
      MOZ_CRASH("GFX: Unsupported cairo surface type");
    }
  }
  MOZ_ASSERT(false, "Could not determine DrawTargetType for DrawTargetCairo");
  return DrawTargetType::SOFTWARE_RASTER;
}

IntSize
DrawTargetCairo::GetSize()
{
  return mSize;
}

SurfaceFormat
GfxFormatForCairoSurface(cairo_surface_t* surface)
{
  cairo_surface_type_t type = cairo_surface_get_type(surface);
  if (type == CAIRO_SURFACE_TYPE_IMAGE) {
    return CairoFormatToGfxFormat(cairo_image_surface_get_format(surface));
  }
#ifdef CAIRO_HAS_XLIB_SURFACE
  // xlib is currently the only Cairo backend that creates 16bpp surfaces
  if (type == CAIRO_SURFACE_TYPE_XLIB &&
      cairo_xlib_surface_get_depth(surface) == 16) {
    return SurfaceFormat::R5G6B5_UINT16;
  }
#endif
  return CairoContentToGfxFormat(cairo_surface_get_content(surface));
}

already_AddRefed<SourceSurface>
DrawTargetCairo::Snapshot()
{
  if (!IsValid()) {
    gfxCriticalNote << "DrawTargetCairo::Snapshot with bad surface " << cairo_surface_status(mSurface);
    return nullptr;
  }
  if (mSnapshot) {
    RefPtr<SourceSurface> snapshot(mSnapshot);
    return snapshot.forget();
  }

  IntSize size = GetSize();

  mSnapshot = new SourceSurfaceCairo(mSurface,
                                     size,
                                     GfxFormatForCairoSurface(mSurface),
                                     this);
  RefPtr<SourceSurface> snapshot(mSnapshot);
  return snapshot.forget();
}

bool
DrawTargetCairo::LockBits(uint8_t** aData, IntSize* aSize,
                          int32_t* aStride, SurfaceFormat* aFormat,
                          IntPoint* aOrigin)
{
  cairo_surface_t* target = cairo_get_group_target(mContext);
  cairo_surface_t* surf = target;
#ifdef CAIRO_HAS_WIN32_SURFACE
  if (cairo_surface_get_type(surf) == CAIRO_SURFACE_TYPE_WIN32) {
    cairo_surface_t* imgsurf = cairo_win32_surface_get_image(surf);
    if (imgsurf) {
      surf = imgsurf;
    }
  }
#endif
  if (cairo_surface_get_type(surf) == CAIRO_SURFACE_TYPE_IMAGE &&
      cairo_surface_status(surf) == CAIRO_STATUS_SUCCESS) {
    PointDouble offset;
    cairo_surface_get_device_offset(target, &offset.x, &offset.y);
    // verify the device offset can be converted to integers suitable for a bounds rect
    IntPoint origin(int32_t(-offset.x), int32_t(-offset.y));
    if (-PointDouble(origin) != offset ||
        (!aOrigin && origin != IntPoint())) {
      return false;
    }

    WillChange();
    Flush();

    mLockedBits = cairo_image_surface_get_data(surf);
    *aData = mLockedBits;
    *aSize = IntSize(cairo_image_surface_get_width(surf),
                     cairo_image_surface_get_height(surf));
    *aStride = cairo_image_surface_get_stride(surf);
    *aFormat = CairoFormatToGfxFormat(cairo_image_surface_get_format(surf));
    if (aOrigin) {
      *aOrigin = origin;
    }
    return true;
  }

  return false;
}

void
DrawTargetCairo::ReleaseBits(uint8_t* aData)
{
  MOZ_ASSERT(mLockedBits == aData);
  mLockedBits = nullptr;
  cairo_surface_t* surf = cairo_get_group_target(mContext);
#ifdef CAIRO_HAS_WIN32_SURFACE
  if (cairo_surface_get_type(surf) == CAIRO_SURFACE_TYPE_WIN32) {
    cairo_surface_t* imgsurf = cairo_win32_surface_get_image(surf);
    if (imgsurf) {
      cairo_surface_mark_dirty(imgsurf);
    }
  }
#endif
  cairo_surface_mark_dirty(surf);
}

void
DrawTargetCairo::Flush()
{
  cairo_surface_t* surf = cairo_get_group_target(mContext);
  cairo_surface_flush(surf);
}

void
DrawTargetCairo::PrepareForDrawing(cairo_t* aContext, const Path* aPath /* = nullptr */)
{
  WillChange(aPath);
}

cairo_surface_t*
DrawTargetCairo::GetDummySurface()
{
  if (mDummySurface) {
    return mDummySurface;
  }

  mDummySurface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 1, 1);

  return mDummySurface;
}

static void
PaintWithAlpha(cairo_t* aContext, const DrawOptions& aOptions)
{
  if (aOptions.mCompositionOp == CompositionOp::OP_SOURCE) {
    // Cairo treats the source operator like a lerp when alpha is < 1.
    // Approximate the desired operator by: out = 0; out += src*alpha;
    if (aOptions.mAlpha == 1) {
      cairo_set_operator(aContext, CAIRO_OPERATOR_SOURCE);
      cairo_paint(aContext);
    } else {
      cairo_set_operator(aContext, CAIRO_OPERATOR_CLEAR);
      cairo_paint(aContext);
      cairo_set_operator(aContext, CAIRO_OPERATOR_ADD);
      cairo_paint_with_alpha(aContext, aOptions.mAlpha);
    }
  } else {
    cairo_set_operator(aContext, GfxOpToCairoOp(aOptions.mCompositionOp));
    cairo_paint_with_alpha(aContext, aOptions.mAlpha);
  }
}

void
DrawTargetCairo::DrawSurface(SourceSurface *aSurface,
                             const Rect &aDest,
                             const Rect &aSource,
                             const DrawSurfaceOptions &aSurfOptions,
                             const DrawOptions &aOptions)
{
  if (mTransformSingular || aDest.IsEmpty()) {
    return;
  }

  if (!IsValid() || !aSurface) {
    gfxCriticalNote << "DrawSurface with bad surface " << cairo_surface_status(cairo_get_group_target(mContext));
    return;
  }

  AutoPrepareForDrawing prep(this, mContext);
  AutoClearDeviceOffset clear(aSurface);

  float sx = aSource.Width() / aDest.Width();
  float sy = aSource.Height() / aDest.Height();

  cairo_matrix_t src_mat;
  cairo_matrix_init_translate(&src_mat, aSource.X(), aSource.Y());
  cairo_matrix_scale(&src_mat, sx, sy);

  cairo_surface_t* surf = GetCairoSurfaceForSourceSurface(aSurface);
  if (!surf) {
    gfxWarning() << "Failed to create cairo surface for DrawTargetCairo::DrawSurface";
    return;
  }
  cairo_pattern_t* pat = cairo_pattern_create_for_surface(surf);
  cairo_surface_destroy(surf);

  cairo_pattern_set_matrix(pat, &src_mat);
  cairo_pattern_set_filter(pat, GfxSamplingFilterToCairoFilter(aSurfOptions.mSamplingFilter));
  cairo_pattern_set_extend(pat, CAIRO_EXTEND_PAD);

  cairo_set_antialias(mContext, GfxAntialiasToCairoAntialias(aOptions.mAntialiasMode));

  // If the destination rect covers the entire clipped area, then unbounded and bounded
  // operations are identical, and we don't need to push a group.
  bool needsGroup = !IsOperatorBoundByMask(aOptions.mCompositionOp) &&
                    !aDest.Contains(GetUserSpaceClip());

  cairo_translate(mContext, aDest.X(), aDest.Y());

  if (needsGroup) {
    cairo_push_group(mContext);
      cairo_new_path(mContext);
      cairo_rectangle(mContext, 0, 0, aDest.Width(), aDest.Height());
      cairo_set_source(mContext, pat);
      cairo_fill(mContext);
    cairo_pop_group_to_source(mContext);
  } else {
    cairo_new_path(mContext);
    cairo_rectangle(mContext, 0, 0, aDest.Width(), aDest.Height());
    cairo_clip(mContext);
    cairo_set_source(mContext, pat);
  }

  PaintWithAlpha(mContext, aOptions);

  cairo_pattern_destroy(pat);
}

void
DrawTargetCairo::DrawFilter(FilterNode *aNode,
                            const Rect &aSourceRect,
                            const Point &aDestPoint,
                            const DrawOptions &aOptions)
{
  FilterNodeSoftware* filter = static_cast<FilterNodeSoftware*>(aNode);
  filter->Draw(this, aSourceRect, aDestPoint, aOptions);
}

void
DrawTargetCairo::DrawSurfaceWithShadow(SourceSurface *aSurface,
                                       const Point &aDest,
                                       const Color &aColor,
                                       const Point &aOffset,
                                       Float aSigma,
                                       CompositionOp aOperator)
{
  if (aSurface->GetType() != SurfaceType::CAIRO) {
    return;
  }

  AutoClearDeviceOffset clear(aSurface);

  Float width = Float(aSurface->GetSize().width);
  Float height = Float(aSurface->GetSize().height);

  SourceSurfaceCairo* source = static_cast<SourceSurfaceCairo*>(aSurface);
  cairo_surface_t* sourcesurf = source->GetSurface();
  cairo_surface_t* blursurf;
  cairo_surface_t* surf;

  // We only use the A8 surface for blurred shadows. Unblurred shadows can just
  // use the RGBA surface directly.
  if (cairo_surface_get_type(sourcesurf) == CAIRO_SURFACE_TYPE_TEE) {
    blursurf = cairo_tee_surface_index(sourcesurf, 0);
    surf = cairo_tee_surface_index(sourcesurf, 1);

    MOZ_ASSERT(cairo_surface_get_type(blursurf) == CAIRO_SURFACE_TYPE_IMAGE);
    Rect extents(0, 0, width, height);
    AlphaBoxBlur blur(extents,
                      cairo_image_surface_get_stride(blursurf),
                      aSigma, aSigma);
    blur.Blur(cairo_image_surface_get_data(blursurf));
  } else {
    blursurf = sourcesurf;
    surf = sourcesurf;
  }

  WillChange();
  ClearSurfaceForUnboundedSource(aOperator);

  cairo_save(mContext);
  cairo_set_operator(mContext, GfxOpToCairoOp(aOperator));
  cairo_identity_matrix(mContext);
  cairo_translate(mContext, aDest.x, aDest.y);

  if (IsOperatorBoundByMask(aOperator)){
    cairo_set_source_rgba(mContext, aColor.r, aColor.g, aColor.b, aColor.a);
    cairo_mask_surface(mContext, blursurf, aOffset.x, aOffset.y);

    // Now that the shadow has been drawn, we can draw the surface on top.
    cairo_set_source_surface(mContext, surf, 0, 0);
    cairo_new_path(mContext);
    cairo_rectangle(mContext, 0, 0, width, height);
    cairo_fill(mContext);
  } else {
    cairo_push_group(mContext);
      cairo_set_source_rgba(mContext, aColor.r, aColor.g, aColor.b, aColor.a);
      cairo_mask_surface(mContext, blursurf, aOffset.x, aOffset.y);

      // Now that the shadow has been drawn, we can draw the surface on top.
      cairo_set_source_surface(mContext, surf, 0, 0);
      cairo_new_path(mContext);
      cairo_rectangle(mContext, 0, 0, width, height);
      cairo_fill(mContext);
    cairo_pop_group_to_source(mContext);
    cairo_paint(mContext);
  }

  cairo_restore(mContext);
}

void
DrawTargetCairo::DrawPattern(const Pattern& aPattern,
                             const StrokeOptions& aStrokeOptions,
                             const DrawOptions& aOptions,
                             DrawPatternType aDrawType,
                             bool aPathBoundsClip)
{
  if (!PatternIsCompatible(aPattern)) {
    return;
  }

  AutoClearDeviceOffset clear(aPattern);

  cairo_pattern_t* pat = GfxPatternToCairoPattern(aPattern, aOptions.mAlpha, GetTransform());
  if (!pat) {
    return;
  }
  if (cairo_pattern_status(pat)) {
    cairo_pattern_destroy(pat);
    gfxWarning() << "Invalid pattern";
    return;
  }

  cairo_set_source(mContext, pat);

  cairo_set_antialias(mContext, GfxAntialiasToCairoAntialias(aOptions.mAntialiasMode));

  if (NeedIntermediateSurface(aPattern, aOptions) ||
      (!IsOperatorBoundByMask(aOptions.mCompositionOp) && !aPathBoundsClip)) {
    cairo_push_group_with_content(mContext, CAIRO_CONTENT_COLOR_ALPHA);

    // Don't want operators to be applied twice
    cairo_set_operator(mContext, CAIRO_OPERATOR_OVER);

    if (aDrawType == DRAW_STROKE) {
      SetCairoStrokeOptions(mContext, aStrokeOptions);
      cairo_stroke_preserve(mContext);
    } else {
      cairo_fill_preserve(mContext);
    }

    cairo_pop_group_to_source(mContext);

    // Now draw the content using the desired operator
    PaintWithAlpha(mContext, aOptions);
  } else {
    cairo_set_operator(mContext, GfxOpToCairoOp(aOptions.mCompositionOp));

    if (aDrawType == DRAW_STROKE) {
      SetCairoStrokeOptions(mContext, aStrokeOptions);
      cairo_stroke_preserve(mContext);
    } else {
      cairo_fill_preserve(mContext);
    }
  }

  cairo_pattern_destroy(pat);
}

void
DrawTargetCairo::FillRect(const Rect &aRect,
                          const Pattern &aPattern,
                          const DrawOptions &aOptions)
{
  if (mTransformSingular) {
    return;
  }

  AutoPrepareForDrawing prep(this, mContext);

  bool restoreTransform = false;
  Matrix mat;
  Rect r = aRect;

  /* Clamp coordinates to work around a design bug in cairo */
  if (r.width > CAIRO_COORD_MAX ||
      r.height > CAIRO_COORD_MAX ||
      r.x < -CAIRO_COORD_MAX ||
      r.x > CAIRO_COORD_MAX ||
      r.y < -CAIRO_COORD_MAX ||
      r.y > CAIRO_COORD_MAX)
  {
    if (!mat.IsRectilinear()) {
      gfxWarning() << "DrawTargetCairo::FillRect() misdrawing huge Rect "
                      "with non-rectilinear transform";
    }

    mat = GetTransform();
    r = mat.TransformBounds(r);

    if (!ConditionRect(r)) {
      gfxWarning() << "Ignoring DrawTargetCairo::FillRect() call with "
                      "out-of-bounds Rect";
      return;
    }

    restoreTransform = true;
    SetTransform(Matrix());
  }

  cairo_new_path(mContext);
  cairo_rectangle(mContext, r.x, r.y, r.Width(), r.Height());

  bool pathBoundsClip = false;

  if (r.Contains(GetUserSpaceClip())) {
    pathBoundsClip = true;
  }

  DrawPattern(aPattern, StrokeOptions(), aOptions, DRAW_FILL, pathBoundsClip);

  if (restoreTransform) {
    SetTransform(mat);
  }
}

void
DrawTargetCairo::CopySurfaceInternal(cairo_surface_t* aSurface,
                                     const IntRect &aSource,
                                     const IntPoint &aDest)
{
  if (cairo_surface_status(aSurface)) {
    gfxWarning() << "Invalid surface" << cairo_surface_status(aSurface);
    return;
  }

  cairo_identity_matrix(mContext);

  cairo_set_source_surface(mContext, aSurface, aDest.x - aSource.x, aDest.y - aSource.y);
  cairo_set_operator(mContext, CAIRO_OPERATOR_SOURCE);
  cairo_set_antialias(mContext, CAIRO_ANTIALIAS_NONE);

  cairo_reset_clip(mContext);
  cairo_new_path(mContext);
  cairo_rectangle(mContext, aDest.x, aDest.y, aSource.width, aSource.height);
  cairo_fill(mContext);
}

void
DrawTargetCairo::CopySurface(SourceSurface *aSurface,
                             const IntRect &aSource,
                             const IntPoint &aDest)
{
  if (mTransformSingular) {
    return;
  }

  AutoPrepareForDrawing prep(this, mContext);
  AutoClearDeviceOffset clear(aSurface);

  if (!aSurface) {
    gfxWarning() << "Unsupported surface type specified";
    return;
  }

  cairo_surface_t* surf = GetCairoSurfaceForSourceSurface(aSurface);
  if (!surf) {
    gfxWarning() << "Unsupported surface type specified";
    return;
  }

  CopySurfaceInternal(surf, aSource, aDest);
  cairo_surface_destroy(surf);
}

void
DrawTargetCairo::CopyRect(const IntRect &aSource,
                          const IntPoint &aDest)
{
  if (mTransformSingular) {
    return;
  }

  AutoPrepareForDrawing prep(this, mContext);

  IntRect source = aSource;
  cairo_surface_t* surf = mSurface;

  if (!SupportsSelfCopy(mSurface) &&
      aDest.y >= aSource.y &&
      aDest.y < aSource.YMost()) {
    cairo_surface_t* similar = cairo_surface_create_similar(mSurface,
                                                            GfxFormatToCairoContent(GetFormat()),
                                                            aSource.width, aSource.height);
    cairo_t* ctx = cairo_create(similar);
    cairo_set_operator(ctx, CAIRO_OPERATOR_SOURCE);
    cairo_set_source_surface(ctx, surf, -aSource.x, -aSource.y);
    cairo_paint(ctx);
    cairo_destroy(ctx);

    source.x = 0;
    source.y = 0;
    surf = similar;
  }

  CopySurfaceInternal(surf, source, aDest);

  if (surf != mSurface) {
    cairo_surface_destroy(surf);
  }
}

void
DrawTargetCairo::ClearRect(const Rect& aRect)
{
  if (mTransformSingular) {
    return;
  }

  AutoPrepareForDrawing prep(this, mContext);

  if (!mContext || aRect.Width() < 0 || aRect.Height() < 0 ||
      !IsFinite(aRect.X()) || !IsFinite(aRect.Width()) ||
      !IsFinite(aRect.Y()) || !IsFinite(aRect.Height())) {
    gfxCriticalNote << "ClearRect with invalid argument " << gfx::hexa(mContext) << " with " << aRect.Width() << "x" << aRect.Height() << " [" << aRect.X() << ", " << aRect.Y() << "]";
  }

  cairo_set_antialias(mContext, CAIRO_ANTIALIAS_NONE);
  cairo_new_path(mContext);
  cairo_set_operator(mContext, CAIRO_OPERATOR_CLEAR);
  cairo_rectangle(mContext, aRect.X(), aRect.Y(),
                  aRect.Width(), aRect.Height());
  cairo_fill(mContext);
}

void
DrawTargetCairo::StrokeRect(const Rect &aRect,
                            const Pattern &aPattern,
                            const StrokeOptions &aStrokeOptions /* = StrokeOptions() */,
                            const DrawOptions &aOptions /* = DrawOptions() */)
{
  if (mTransformSingular) {
    return;
  }

  AutoPrepareForDrawing prep(this, mContext);

  cairo_new_path(mContext);
  cairo_rectangle(mContext, aRect.x, aRect.y, aRect.Width(), aRect.Height());

  DrawPattern(aPattern, aStrokeOptions, aOptions, DRAW_STROKE);
}

void
DrawTargetCairo::StrokeLine(const Point &aStart,
                            const Point &aEnd,
                            const Pattern &aPattern,
                            const StrokeOptions &aStrokeOptions /* = StrokeOptions() */,
                            const DrawOptions &aOptions /* = DrawOptions() */)
{
  if (mTransformSingular) {
    return;
  }

  AutoPrepareForDrawing prep(this, mContext);

  cairo_new_path(mContext);
  cairo_move_to(mContext, aStart.x, aStart.y);
  cairo_line_to(mContext, aEnd.x, aEnd.y);

  DrawPattern(aPattern, aStrokeOptions, aOptions, DRAW_STROKE);
}

void
DrawTargetCairo::Stroke(const Path *aPath,
                        const Pattern &aPattern,
                        const StrokeOptions &aStrokeOptions /* = StrokeOptions() */,
                        const DrawOptions &aOptions /* = DrawOptions() */)
{
  if (mTransformSingular) {
    return;
  }

  AutoPrepareForDrawing prep(this, mContext, aPath);

  if (aPath->GetBackendType() != BackendType::CAIRO)
    return;

  PathCairo* path = const_cast<PathCairo*>(static_cast<const PathCairo*>(aPath));
  path->SetPathOnContext(mContext);

  DrawPattern(aPattern, aStrokeOptions, aOptions, DRAW_STROKE);
}

void
DrawTargetCairo::Fill(const Path *aPath,
                      const Pattern &aPattern,
                      const DrawOptions &aOptions /* = DrawOptions() */)
{
  if (mTransformSingular) {
    return;
  }

  AutoPrepareForDrawing prep(this, mContext, aPath);

  if (aPath->GetBackendType() != BackendType::CAIRO)
    return;

  PathCairo* path = const_cast<PathCairo*>(static_cast<const PathCairo*>(aPath));
  path->SetPathOnContext(mContext);

  DrawPattern(aPattern, StrokeOptions(), aOptions, DRAW_FILL);
}

bool
DrawTargetCairo::IsCurrentGroupOpaque()
{
  cairo_surface_t* surf = cairo_get_group_target(mContext);

  if (!surf) {
    return false;
  }

  return cairo_surface_get_content(surf) == CAIRO_CONTENT_COLOR;
}

void
DrawTargetCairo::SetFontOptions()
{
  //   This will attempt to detect if the currently set scaled font on the
  // context has enabled subpixel AA. If it is not permitted, then it will
  // downgrade to grayscale AA.
  //   This only currently works effectively for the cairo-ft backend relative
  // to system defaults, as only cairo-ft reflect system defaults in the scaled
  // font state. However, this will work for cairo-ft on both tree Cairo and
  // system Cairo.
  //   Other backends leave the CAIRO_ANTIALIAS_DEFAULT setting untouched while
  // potentially interpreting it as subpixel or even other types of AA that
  // can't be safely equivocated with grayscale AA. For this reason we don't
  // try to also detect and modify the default AA setting, only explicit
  // subpixel AA. These other backends must instead rely on tree Cairo's
  // cairo_surface_set_subpixel_antialiasing extension.

  // If allowing subpixel AA, then leave Cairo's default AA state.
  if (mPermitSubpixelAA) {
    return;
  }

  if (!mFontOptions) {
    mFontOptions = cairo_font_options_create();
    if (!mFontOptions) {
      gfxWarning() << "Failed allocating Cairo font options";
      return;
    }
  }

  // If the current font requests subpixel AA, force it to gray since we don't
  // allow subpixel AA.
  cairo_get_font_options(mContext, mFontOptions);
  cairo_antialias_t antialias = cairo_font_options_get_antialias(mFontOptions);
  if (antialias == CAIRO_ANTIALIAS_SUBPIXEL) {
    cairo_font_options_set_antialias(mFontOptions, CAIRO_ANTIALIAS_GRAY);
    cairo_set_font_options(mContext, mFontOptions);
  }
}

void
DrawTargetCairo::SetPermitSubpixelAA(bool aPermitSubpixelAA)
{
  DrawTarget::SetPermitSubpixelAA(aPermitSubpixelAA);
#ifdef MOZ_TREE_CAIRO
  cairo_surface_set_subpixel_antialiasing(cairo_get_group_target(mContext),
    aPermitSubpixelAA ? CAIRO_SUBPIXEL_ANTIALIASING_ENABLED : CAIRO_SUBPIXEL_ANTIALIASING_DISABLED);
#endif
}

void
DrawTargetCairo::FillGlyphs(ScaledFont *aFont,
                            const GlyphBuffer &aBuffer,
                            const Pattern &aPattern,
                            const DrawOptions &aOptions,
                            const GlyphRenderingOptions*)
{
  if (mTransformSingular) {
    return;
  }

  if (!IsValid()) {
    gfxDebug() << "FillGlyphs bad surface " << cairo_surface_status(cairo_get_group_target(mContext));
    return;
  }

  if (!aFont) {
    gfxDevCrash(LogReason::InvalidFont) << "Invalid scaled font";
    return;
  }

  AutoPrepareForDrawing prep(this, mContext);
  AutoClearDeviceOffset clear(aPattern);

  ScaledFontBase* scaledFont = static_cast<ScaledFontBase*>(aFont);
  cairo_set_scaled_font(mContext, scaledFont->GetCairoScaledFont());

  cairo_pattern_t* pat = GfxPatternToCairoPattern(aPattern, aOptions.mAlpha, GetTransform());
  if (!pat)
    return;

  cairo_set_source(mContext, pat);
  cairo_pattern_destroy(pat);

  cairo_set_antialias(mContext, GfxAntialiasToCairoAntialias(aOptions.mAntialiasMode));

  // Override any font-specific options as necessary.
  SetFontOptions();

  // Convert our GlyphBuffer into a vector of Cairo glyphs. This code can
  // execute millions of times in short periods, so we want to avoid heap
  // allocation whenever possible. So we use an inline vector capacity of 1024
  // bytes (the maximum allowed by mozilla::Vector), which gives an inline
  // length of 1024 / 24 = 42 elements, which is enough to typically avoid heap
  // allocation in ~99% of cases.
  Vector<cairo_glyph_t, 1024 / sizeof(cairo_glyph_t)> glyphs;
  if (!glyphs.resizeUninitialized(aBuffer.mNumGlyphs)) {
    gfxDevCrash(LogReason::GlyphAllocFailedCairo) << "glyphs allocation failed";
    return;
  }
  for (uint32_t i = 0; i < aBuffer.mNumGlyphs; ++i) {
    glyphs[i].index = aBuffer.mGlyphs[i].mIndex;
    glyphs[i].x = aBuffer.mGlyphs[i].mPosition.x;
    glyphs[i].y = aBuffer.mGlyphs[i].mPosition.y;
  }

  cairo_show_glyphs(mContext, &glyphs[0], aBuffer.mNumGlyphs);

  if (cairo_surface_status(cairo_get_group_target(mContext))) {
    gfxDebug() << "Ending FillGlyphs with a bad surface " << cairo_surface_status(cairo_get_group_target(mContext));
  }
}

void
DrawTargetCairo::Mask(const Pattern &aSource,
                      const Pattern &aMask,
                      const DrawOptions &aOptions /* = DrawOptions() */)
{
  if (mTransformSingular) {
    return;
  }

  AutoPrepareForDrawing prep(this, mContext);
  AutoClearDeviceOffset clearSource(aSource);
  AutoClearDeviceOffset clearMask(aMask);

  cairo_set_antialias(mContext, GfxAntialiasToCairoAntialias(aOptions.mAntialiasMode));

  cairo_pattern_t* source = GfxPatternToCairoPattern(aSource, aOptions.mAlpha, GetTransform());
  if (!source) {
    return;
  }

  cairo_pattern_t* mask = GfxPatternToCairoPattern(aMask, aOptions.mAlpha, GetTransform());
  if (!mask) {
    cairo_pattern_destroy(source);
    return;
  }

  if (cairo_pattern_status(source) || cairo_pattern_status(mask)) {
    cairo_pattern_destroy(source);
    cairo_pattern_destroy(mask);
    gfxWarning() << "Invalid pattern";
    return;
  }

  cairo_set_source(mContext, source);
  cairo_set_operator(mContext, GfxOpToCairoOp(aOptions.mCompositionOp));
  cairo_mask(mContext, mask);

  cairo_pattern_destroy(mask);
  cairo_pattern_destroy(source);
}

void
DrawTargetCairo::MaskSurface(const Pattern &aSource,
                             SourceSurface *aMask,
                             Point aOffset,
                             const DrawOptions &aOptions)
{
  if (mTransformSingular) {
    return;
  }

  AutoPrepareForDrawing prep(this, mContext);
  AutoClearDeviceOffset clearSource(aSource);
  AutoClearDeviceOffset clearMask(aMask);

  if (!PatternIsCompatible(aSource)) {
    return;
  }

  cairo_set_antialias(mContext, GfxAntialiasToCairoAntialias(aOptions.mAntialiasMode));

  cairo_pattern_t* pat = GfxPatternToCairoPattern(aSource, aOptions.mAlpha, GetTransform());
  if (!pat) {
    return;
  }

  if (cairo_pattern_status(pat)) {
    cairo_pattern_destroy(pat);
    gfxWarning() << "Invalid pattern";
    return;
  }

  cairo_set_source(mContext, pat);

  if (NeedIntermediateSurface(aSource, aOptions)) {
    cairo_push_group_with_content(mContext, CAIRO_CONTENT_COLOR_ALPHA);

    // Don't want operators to be applied twice
    cairo_set_operator(mContext, CAIRO_OPERATOR_OVER);

    // Now draw the content using the desired operator
    cairo_paint_with_alpha(mContext, aOptions.mAlpha);

    cairo_pop_group_to_source(mContext);
  }

  cairo_surface_t* surf = GetCairoSurfaceForSourceSurface(aMask);
  if (!surf) {
    cairo_pattern_destroy(pat);
    return;
  }
  cairo_pattern_t* mask = cairo_pattern_create_for_surface(surf);
  cairo_matrix_t matrix;

  cairo_matrix_init_translate (&matrix, -aOffset.x, -aOffset.y);
  cairo_pattern_set_matrix (mask, &matrix);

  cairo_set_operator(mContext, GfxOpToCairoOp(aOptions.mCompositionOp));

  cairo_mask(mContext, mask);

  cairo_surface_destroy(surf);
  cairo_pattern_destroy(mask);
  cairo_pattern_destroy(pat);
}

void
DrawTargetCairo::PushClip(const Path *aPath)
{
  if (aPath->GetBackendType() != BackendType::CAIRO) {
    return;
  }

  WillChange(aPath);
  cairo_save(mContext);

  PathCairo* path = const_cast<PathCairo*>(static_cast<const PathCairo*>(aPath));

  if (mTransformSingular) {
    cairo_new_path(mContext);
    cairo_rectangle(mContext, 0, 0, 0, 0);
  } else {
    path->SetPathOnContext(mContext);
  }
  cairo_clip_preserve(mContext);
}

void
DrawTargetCairo::PushClipRect(const Rect& aRect)
{
  WillChange();
  cairo_save(mContext);

  cairo_new_path(mContext);
  if (mTransformSingular) {
    cairo_rectangle(mContext, 0, 0, 0, 0);
  } else {
    cairo_rectangle(mContext, aRect.X(), aRect.Y(), aRect.Width(), aRect.Height());
  }
  cairo_clip_preserve(mContext);
}

void
DrawTargetCairo::PopClip()
{
  // save/restore does not affect the path, so no need to call WillChange()

  // cairo_restore will restore the transform too and we don't want to do that
  // so we'll save it now and restore it after the cairo_restore
  cairo_matrix_t mat;
  cairo_get_matrix(mContext, &mat);

  cairo_restore(mContext);

  cairo_set_matrix(mContext, &mat);
}
 
void
DrawTargetCairo::PushLayer(bool aOpaque, Float aOpacity, SourceSurface* aMask,
                          const Matrix& aMaskTransform, const IntRect& aBounds,
                          bool aCopyBackground)
{
  cairo_content_t content = CAIRO_CONTENT_COLOR_ALPHA;

  if (mFormat == SurfaceFormat::A8) {
    content = CAIRO_CONTENT_ALPHA;
  } else if (aOpaque) {
    content = CAIRO_CONTENT_COLOR;
  }

  if (aCopyBackground) {
    cairo_surface_t* source = cairo_get_group_target(mContext);
    cairo_push_group_with_content(mContext, content);
    cairo_surface_t* dest = cairo_get_group_target(mContext);
    cairo_t* ctx = cairo_create(dest);
    cairo_set_source_surface(ctx, source, 0, 0);
    cairo_set_operator(ctx, CAIRO_OPERATOR_SOURCE);
    cairo_paint(ctx);
    cairo_destroy(ctx);
  } else {
    cairo_push_group_with_content(mContext, content);
  }

  PushedLayer layer(aOpacity, mPermitSubpixelAA);

  if (aMask) {
    cairo_surface_t* surf = GetCairoSurfaceForSourceSurface(aMask);
    if (surf) {
      layer.mMaskPattern = cairo_pattern_create_for_surface(surf);
      cairo_matrix_t mat;
      GfxMatrixToCairoMatrix(aMaskTransform, mat);
      cairo_matrix_invert(&mat);
      cairo_pattern_set_matrix(layer.mMaskPattern, &mat);
      cairo_surface_destroy(surf);
    } else {
      gfxCriticalError() << "Failed to get cairo surface for mask surface!";
    }
  }

  mPushedLayers.push_back(layer);

  SetPermitSubpixelAA(aOpaque);
}

void
DrawTargetCairo::PopLayer()
{
  MOZ_ASSERT(mPushedLayers.size());

  cairo_set_operator(mContext, CAIRO_OPERATOR_OVER);

  cairo_pop_group_to_source(mContext);

  PushedLayer layer = mPushedLayers.back();
  mPushedLayers.pop_back();

  if (!layer.mMaskPattern) {
    cairo_paint_with_alpha(mContext, layer.mOpacity);
  } else {
    if (layer.mOpacity != Float(1.0)) {
      cairo_push_group_with_content(mContext, CAIRO_CONTENT_COLOR_ALPHA);

      // Now draw the content using the desired operator
      cairo_paint_with_alpha(mContext, layer.mOpacity);

      cairo_pop_group_to_source(mContext);
    }
    cairo_mask(mContext, layer.mMaskPattern);
  }

  cairo_matrix_t mat;
  GfxMatrixToCairoMatrix(mTransform, mat);
  cairo_set_matrix(mContext, &mat);

  cairo_pattern_destroy(layer.mMaskPattern);
  SetPermitSubpixelAA(layer.mWasPermittingSubpixelAA);
}

already_AddRefed<PathBuilder>
DrawTargetCairo::CreatePathBuilder(FillRule aFillRule /* = FillRule::FILL_WINDING */) const
{
  return MakeAndAddRef<PathBuilderCairo>(aFillRule);
}

void
DrawTargetCairo::ClearSurfaceForUnboundedSource(const CompositionOp &aOperator)
{
  if (aOperator != CompositionOp::OP_SOURCE)
    return;
  cairo_set_operator(mContext, CAIRO_OPERATOR_CLEAR);
  // It doesn't really matter what the source is here, since Paint
  // isn't bounded by the source and the mask covers the entire clip
  // region.
  cairo_paint(mContext);
}


already_AddRefed<GradientStops>
DrawTargetCairo::CreateGradientStops(GradientStop *aStops, uint32_t aNumStops,
                                     ExtendMode aExtendMode) const
{
  return MakeAndAddRef<GradientStopsCairo>(aStops, aNumStops, aExtendMode);
}

already_AddRefed<FilterNode>
DrawTargetCairo::CreateFilter(FilterType aType)
{
  return FilterNodeSoftware::Create(aType);
}

void
DrawTargetCairo::GetGlyphRasterizationMetrics(ScaledFont *aScaledFont, const uint16_t* aGlyphIndices,
                                              uint32_t aNumGlyphs, GlyphMetrics* aGlyphMetrics)
{
  for (uint32_t i = 0; i < aNumGlyphs; i++) {
    cairo_glyph_t glyph;
    cairo_text_extents_t extents;
    glyph.index = aGlyphIndices[i];
    glyph.x = 0;
    glyph.y = 0;
    cairo_glyph_extents(mContext, &glyph, 1, &extents);

    aGlyphMetrics[i].mXBearing = extents.x_bearing;
    aGlyphMetrics[i].mXAdvance = extents.x_advance;
    aGlyphMetrics[i].mYBearing = extents.y_bearing;
    aGlyphMetrics[i].mYAdvance = extents.y_advance;
    aGlyphMetrics[i].mWidth = extents.width;
    aGlyphMetrics[i].mHeight = extents.height;
  }
}

already_AddRefed<SourceSurface>
DrawTargetCairo::CreateSourceSurfaceFromData(unsigned char *aData,
                                             const IntSize &aSize,
                                             int32_t aStride,
                                             SurfaceFormat aFormat) const
{
  if (!aData) {
    gfxWarning() << "DrawTargetCairo::CreateSourceSurfaceFromData null aData";
    return nullptr;
  }

  cairo_surface_t* surf = CopyToImageSurface(aData, IntRect(IntPoint(), aSize),
                                             aStride, aFormat);
  if (!surf) {
    return nullptr;
  }

  RefPtr<SourceSurfaceCairo> source_surf = new SourceSurfaceCairo(surf, aSize, aFormat);
  cairo_surface_destroy(surf);

  return source_surf.forget();
}

#ifdef CAIRO_HAS_XLIB_SURFACE
static cairo_user_data_key_t gDestroyPixmapKey;

struct DestroyPixmapClosure {
  DestroyPixmapClosure(Drawable d, Screen *s) : mPixmap(d), mScreen(s) {}
  ~DestroyPixmapClosure() {
    XFreePixmap(DisplayOfScreen(mScreen), mPixmap);
  }
  Drawable mPixmap;
  Screen *mScreen;
};

static void
DestroyPixmap(void *data)
{
  delete static_cast<DestroyPixmapClosure*>(data);
}
#endif

already_AddRefed<SourceSurface>
DrawTargetCairo::OptimizeSourceSurface(SourceSurface *aSurface) const
{
  RefPtr<SourceSurface> surface(aSurface);
#ifdef CAIRO_HAS_XLIB_SURFACE
  cairo_surface_type_t ctype = cairo_surface_get_type(mSurface);
  if (aSurface->GetType() == SurfaceType::CAIRO &&
      cairo_surface_get_type(
        static_cast<SourceSurfaceCairo*>(aSurface)->GetSurface()) == ctype) {
    return surface.forget();
  }

  if (ctype != CAIRO_SURFACE_TYPE_XLIB) {
    return surface.forget();
  }

  IntSize size = aSurface->GetSize();
  if (!size.width || !size.height) {
    return surface.forget();
  }

  // Although the dimension parameters in the xCreatePixmapReq wire protocol are
  // 16-bit unsigned integers, the server's CreatePixmap returns BadAlloc if
  // either dimension cannot be represented by a 16-bit *signed* integer.
  #define XLIB_IMAGE_SIDE_SIZE_LIMIT 0x7fff

  if (size.width > XLIB_IMAGE_SIDE_SIZE_LIMIT ||
      size.height > XLIB_IMAGE_SIDE_SIZE_LIMIT) {
    return surface.forget();
  }

  SurfaceFormat format = aSurface->GetFormat();
  Screen *screen = cairo_xlib_surface_get_screen(mSurface);
  Display *dpy = DisplayOfScreen(screen);
  XRenderPictFormat* xrenderFormat = nullptr;
  switch (format) {
  case SurfaceFormat::A8R8G8B8_UINT32:
    xrenderFormat = XRenderFindStandardFormat(dpy, PictStandardARGB32);
    break;
  case SurfaceFormat::X8R8G8B8_UINT32:
    xrenderFormat = XRenderFindStandardFormat(dpy, PictStandardRGB24);
    break;
  case SurfaceFormat::A8:
    xrenderFormat = XRenderFindStandardFormat(dpy, PictStandardA8);
    break;
  default:
    return surface.forget();
  }
  if (!xrenderFormat) {
    return surface.forget();
  }

  Drawable pixmap = XCreatePixmap(dpy, RootWindowOfScreen(screen),
                                  size.width, size.height,
                                  xrenderFormat->depth);
  if (!pixmap) {
    return surface.forget();
  }

  auto closure = MakeUnique<DestroyPixmapClosure>(pixmap, screen);

  ScopedCairoSurface csurf(
    cairo_xlib_surface_create_with_xrender_format(dpy, pixmap,
                                                  screen, xrenderFormat,
                                                  size.width, size.height));
  if (!csurf || cairo_surface_status(csurf)) {
    return surface.forget();
  }

  cairo_surface_set_user_data(csurf, &gDestroyPixmapKey,
                              closure.release(), DestroyPixmap);

  RefPtr<DrawTargetCairo> dt = new DrawTargetCairo();
  if (!dt->Init(csurf, size, &format)) {
    return surface.forget();
  }

  dt->CopySurface(aSurface,
                  IntRect(0, 0, size.width, size.height),
                  IntPoint(0, 0));
  dt->Flush();

  surface = new SourceSurfaceCairo(csurf, size, format);
#endif

  return surface.forget();
}

already_AddRefed<SourceSurface>
DrawTargetCairo::CreateSourceSurfaceFromNativeSurface(const NativeSurface &aSurface) const
{
  return nullptr;
}

already_AddRefed<DrawTarget>
DrawTargetCairo::CreateSimilarDrawTarget(const IntSize &aSize, SurfaceFormat aFormat) const
{
  if (cairo_surface_status(cairo_get_group_target(mContext))) {
    RefPtr<DrawTargetCairo> target = new DrawTargetCairo();
    if (target->Init(aSize, aFormat)) {
      return target.forget();
    }
  }

  cairo_surface_t* similar;
  switch (cairo_surface_get_type(mSurface)) {
#ifdef CAIRO_HAS_WIN32_SURFACE
    case CAIRO_SURFACE_TYPE_WIN32:
      similar = cairo_win32_surface_create_with_dib(
        GfxFormatToCairoFormat(aFormat), aSize.width, aSize.height);
      break;
#endif
#ifdef CAIRO_HAS_QUARTZ_SURFACE
    case CAIRO_SURFACE_TYPE_QUARTZ:
      similar = cairo_quartz_surface_create_cg_layer(
        mSurface, GfxFormatToCairoContent(aFormat), aSize.width, aSize.height);
      break;
#endif
    default:
      similar = cairo_surface_create_similar(mSurface,
                                             GfxFormatToCairoContent(aFormat),
                                             aSize.width, aSize.height);
      break;
  }

  if (!cairo_surface_status(similar)) {
    RefPtr<DrawTargetCairo> target = new DrawTargetCairo();
    if (target->InitAlreadyReferenced(similar, aSize)) {
      return target.forget();
    }
  }

  gfxCriticalError(CriticalLog::DefaultOptions(Factory::ReasonableSurfaceSize(aSize))) << "Failed to create similar cairo surface! Size: " << aSize << " Status: " << cairo_surface_status(similar) << cairo_surface_status(cairo_get_group_target(mContext)) << " format " << (int)aFormat;
  cairo_surface_destroy(similar);

  return nullptr;
}

bool
DrawTargetCairo::InitAlreadyReferenced(cairo_surface_t* aSurface, const IntSize& aSize, SurfaceFormat* aFormat)
{
  if (cairo_surface_status(aSurface)) {
    gfxCriticalNote
      << "Attempt to create DrawTarget for invalid surface. "
      << aSize << " Cairo Status: " << cairo_surface_status(aSurface);
    cairo_surface_destroy(aSurface);
    return false;
  }

  mContext = cairo_create(aSurface);
  mSurface = aSurface;
  mSize = aSize;
  mFormat = aFormat ? *aFormat : GfxFormatForCairoSurface(aSurface);

  // Cairo image surface have a bug where they will allocate a mask surface (for clipping)
  // the size of the clip extents, and don't take the surface extents into account.
  // Add a manual clip to the surface extents to prevent this.
  cairo_new_path(mContext);
  cairo_rectangle(mContext, 0, 0, mSize.width, mSize.height);
  cairo_clip(mContext);

  if (mFormat == SurfaceFormat::A8R8G8B8_UINT32 ||
      mFormat == SurfaceFormat::R8G8B8A8) {
    SetPermitSubpixelAA(false);
  } else {
    SetPermitSubpixelAA(true);
  }

  return true;
}

already_AddRefed<DrawTarget>
DrawTargetCairo::CreateShadowDrawTarget(const IntSize &aSize, SurfaceFormat aFormat,
                                        float aSigma) const
{
  cairo_surface_t* similar = cairo_surface_create_similar(cairo_get_target(mContext),
                                                          GfxFormatToCairoContent(aFormat),
                                                          aSize.width, aSize.height);

  if (cairo_surface_status(similar)) {
    return nullptr;
  }

  // If we don't have a blur then we can use the RGBA mask and keep all the
  // operations in graphics memory.
  if (aSigma == 0.0F) {
    RefPtr<DrawTargetCairo> target = new DrawTargetCairo();
    if (target->InitAlreadyReferenced(similar, aSize)) {
      return target.forget();
    } else {
      return nullptr;
    }
  }

  cairo_surface_t* blursurf = cairo_image_surface_create(CAIRO_FORMAT_A8,
                                                         aSize.width,
                                                         aSize.height);

  if (cairo_surface_status(blursurf)) {
    return nullptr;
  }

  cairo_surface_t* tee = cairo_tee_surface_create(blursurf);
  cairo_surface_destroy(blursurf);
  if (cairo_surface_status(tee)) {
    cairo_surface_destroy(similar);
    return nullptr;
  }

  cairo_tee_surface_add(tee, similar);
  cairo_surface_destroy(similar);

  RefPtr<DrawTargetCairo> target = new DrawTargetCairo();
  if (target->InitAlreadyReferenced(tee, aSize)) {
    return target.forget();
  }
  return nullptr;
}

static inline pixman_format_code_t
GfxFormatToPixmanFormat(SurfaceFormat aFormat)
{
  switch (aFormat) {
  case SurfaceFormat::A8R8G8B8_UINT32:
    return PIXMAN_a8r8g8b8;
  case SurfaceFormat::X8R8G8B8_UINT32:
    return PIXMAN_x8r8g8b8;
  case SurfaceFormat::R5G6B5_UINT16:
    return PIXMAN_r5g6b5;
  case SurfaceFormat::A8:
    return PIXMAN_a8;
  default:
    // Allow both BGRA and ARGB formats to be passed through unmodified,
    // even though even though we are actually rendering to A8R8G8B8_UINT32.
    if (aFormat == SurfaceFormat::B8G8R8A8 ||
        aFormat == SurfaceFormat::A8R8G8B8) {
      return PIXMAN_a8r8g8b8;
    }
    return (pixman_format_code_t)0;
  }
}

static inline bool
GfxMatrixToPixmanTransform(const Matrix4x4 &aMatrix, pixman_transform* aResult)
{
  pixman_f_transform fTransform = {{
    { aMatrix._11, aMatrix._21, aMatrix._41 },
    { aMatrix._12, aMatrix._22, aMatrix._42 },
    { aMatrix._14, aMatrix._24, aMatrix._44 }
  }};
  return pixman_transform_from_pixman_f_transform(aResult, &fTransform);
}

#ifndef USE_SKIA
bool
DrawTarget::Draw3DTransformedSurface(SourceSurface* aSurface, const Matrix4x4& aMatrix)
{
  // Composite the 3D transform with the DT's transform.
  Matrix4x4 fullMat = aMatrix * Matrix4x4::From2D(mTransform);
  // Transform the surface bounds and clip to this DT.
  IntRect xformBounds =
    RoundedOut(
      fullMat.TransformAndClipBounds(Rect(Point(0, 0), Size(aSurface->GetSize())),
                                     Rect(Point(0, 0), Size(GetSize()))));
  if (xformBounds.IsEmpty()) {
    return true;
  }
  // Offset the matrix by the transformed origin.
  fullMat.PostTranslate(-xformBounds.x, -xformBounds.y, 0);
  // Invert the matrix into a pattern matrix for pixman.
  if (!fullMat.Invert()) {
    return false;
  }
  pixman_transform xform;
  if (!GfxMatrixToPixmanTransform(fullMat, &xform)) {
    return false;
  }

  // Read in the source data.
  RefPtr<DataSourceSurface> srcSurf = aSurface->GetDataSurface();
  pixman_format_code_t srcFormat = GfxFormatToPixmanFormat(srcSurf->GetFormat());
  if (!srcFormat) {
    return false;
  }
  DataSourceSurface::ScopedMap srcMap(srcSurf, DataSourceSurface::READ);
  if (!srcMap.IsMapped()) {
    return false;
  }

  // Set up an intermediate destination surface only the size of the transformed bounds.
  // Try to pass through the source's format unmodified in both the BGRA and ARGB cases.
  RefPtr<DataSourceSurface> dstSurf =
    Factory::CreateDataSourceSurface(xformBounds.Size(),
                                     srcFormat == PIXMAN_a8r8g8b8 ?
                                       srcSurf->GetFormat() : SurfaceFormat::A8R8G8B8_UINT32);
  if (!dstSurf) {
    return false;
  }

  // Wrap the surfaces in pixman images and do the transform.
  pixman_image_t* dst =
    pixman_image_create_bits(PIXMAN_a8r8g8b8,
                             xformBounds.width, xformBounds.height,
                             (uint32_t*)dstSurf->GetData(), dstSurf->Stride());
  if (!dst) {
    return false;
  }
  pixman_image_t* src =
    pixman_image_create_bits(srcFormat,
                             srcSurf->GetSize().width, srcSurf->GetSize().height,
                             (uint32_t*)srcMap.GetData(), srcMap.GetStride());
  if (!src) {
    pixman_image_unref(dst);
    return false;
  }

  pixman_image_set_filter(src, PIXMAN_FILTER_BILINEAR, nullptr, 0);
  pixman_image_set_transform(src, &xform);

  pixman_image_composite32(PIXMAN_OP_SRC,
                           src, nullptr, dst,
                           0, 0, 0, 0, 0, 0,
                           xformBounds.width, xformBounds.height);

  pixman_image_unref(dst);
  pixman_image_unref(src);

  // Temporarily reset the DT's transform, since it has already been composed above.
  Matrix origTransform = mTransform;
  SetTransform(Matrix());

  // Draw the transformed surface within the transformed bounds.
  DrawSurface(dstSurf, Rect(xformBounds), Rect(Point(0, 0), Size(xformBounds.Size())));

  SetTransform(origTransform);

  return true;
}
#endif

#ifdef CAIRO_HAS_XLIB_SURFACE
static bool gXRenderInitialized = false;
static bool gXRenderHasTransform = false;

static bool
SupportsXRender(cairo_surface_t* surface)
{
  if (!surface ||
      cairo_surface_get_type(surface) != CAIRO_SURFACE_TYPE_XLIB ||
      !cairo_xlib_surface_get_xrender_format(surface)) {
    return false;
  }

  if (gXRenderInitialized) {
    return true;
  }
  gXRenderInitialized = true;

  cairo_device_t* device = cairo_surface_get_device(surface);
  if (cairo_device_acquire(device) != CAIRO_STATUS_SUCCESS) {
    return false;
  }

  Display* display = cairo_xlib_surface_get_display(surface);
  int major, minor;
  if (XRenderQueryVersion(display, &major, &minor)) {
    if (major > 0 || (major == 0 && minor >= 6)) {
      gXRenderHasTransform = true;
    }
  }

  cairo_device_release(device);

  return true;
}
#endif

bool
DrawTargetCairo::Draw3DTransformedSurface(SourceSurface* aSurface, const Matrix4x4& aMatrix)
{
#if CAIRO_HAS_XLIB_SURFACE
  cairo_surface_t* srcSurf =
    aSurface->GetType() == SurfaceType::CAIRO ?
      static_cast<SourceSurfaceCairo*>(aSurface)->GetSurface() : nullptr;
  if (!SupportsXRender(srcSurf) || !gXRenderHasTransform) {
    return DrawTarget::Draw3DTransformedSurface(aSurface, aMatrix);
  }

  Matrix4x4 fullMat = aMatrix * Matrix4x4::From2D(mTransform);
  IntRect xformBounds =
    RoundedOut(
      fullMat.TransformAndClipBounds(Rect(Point(0, 0), Size(aSurface->GetSize())),
                                     Rect(Point(0, 0), Size(GetSize()))));
  if (xformBounds.IsEmpty()) {
    return true;
  }
  fullMat.PostTranslate(-xformBounds.x, -xformBounds.y, 0);
  if (!fullMat.Invert()) {
    return false;
  }
  pixman_transform xform;
  if (!GfxMatrixToPixmanTransform(fullMat, &xform)) {
    return false;
  }

  cairo_surface_t* xformSurf =
    cairo_surface_create_similar(srcSurf, CAIRO_CONTENT_COLOR_ALPHA,
                                 xformBounds.width, xformBounds.height);
  if (!SupportsXRender(xformSurf)) {
    cairo_surface_destroy(xformSurf);
    return false;
  }
  cairo_device_t* device = cairo_surface_get_device(xformSurf);
  if (cairo_device_acquire(device) != CAIRO_STATUS_SUCCESS) {
    cairo_surface_destroy(xformSurf);
    return false;
  }

  Display* display = cairo_xlib_surface_get_display(xformSurf);

  Picture srcPict = XRenderCreatePicture(display,
                                         cairo_xlib_surface_get_drawable(srcSurf),
                                         cairo_xlib_surface_get_xrender_format(srcSurf),
                                         0, nullptr);
  XRenderSetPictureFilter(display, srcPict, FilterBilinear, nullptr, 0);
  XRenderSetPictureTransform(display, srcPict, (XTransform*)&xform);

  Picture dstPict = XRenderCreatePicture(display,
                                         cairo_xlib_surface_get_drawable(xformSurf),
                                         cairo_xlib_surface_get_xrender_format(xformSurf),
                                         0, nullptr);

  XRenderComposite(display, PictOpSrc,
                   srcPict, X11None, dstPict,
                   0, 0, 0, 0, 0, 0,
                   xformBounds.width, xformBounds.height);

  XRenderFreePicture(display, srcPict);
  XRenderFreePicture(display, dstPict);

  cairo_device_release(device);
  cairo_surface_mark_dirty(xformSurf);

  AutoPrepareForDrawing(this, mContext);

  cairo_identity_matrix(mContext);

  cairo_set_operator(mContext, CAIRO_OPERATOR_OVER);
  cairo_set_antialias(mContext, CAIRO_ANTIALIAS_DEFAULT);
  cairo_set_source_surface(mContext, xformSurf, xformBounds.x, xformBounds.y);

  cairo_new_path(mContext);
  cairo_rectangle(mContext, xformBounds.x, xformBounds.y, xformBounds.width, xformBounds.height);
  cairo_fill(mContext);

  cairo_surface_destroy(xformSurf);

  return true;
#else
  return DrawTarget::Draw3DTransformedSurface(aSurface, aMatrix);
#endif
}

bool
DrawTargetCairo::Init(cairo_surface_t* aSurface, const IntSize& aSize, SurfaceFormat* aFormat)
{
  cairo_surface_reference(aSurface);
  return InitAlreadyReferenced(aSurface, aSize, aFormat);
}

bool
DrawTargetCairo::Init(const IntSize& aSize, SurfaceFormat aFormat)
{
  cairo_surface_t *surf = cairo_image_surface_create(GfxFormatToCairoFormat(aFormat), aSize.width, aSize.height);
  return InitAlreadyReferenced(surf, aSize);
}

bool
DrawTargetCairo::Init(unsigned char* aData, const IntSize &aSize, int32_t aStride, SurfaceFormat aFormat)
{
  cairo_surface_t* surf =
    cairo_image_surface_create_for_data(aData,
                                        GfxFormatToCairoFormat(aFormat),
                                        aSize.width,
                                        aSize.height,
                                        aStride);
  return InitAlreadyReferenced(surf, aSize);
}

void *
DrawTargetCairo::GetNativeSurface(NativeSurfaceType aType)
{
  if (aType == NativeSurfaceType::CAIRO_CONTEXT) {
    return mContext;
  }

  return nullptr;
}

void
DrawTargetCairo::MarkSnapshotIndependent()
{
  if (mSnapshot) {
    if (mSnapshot->refCount() > 1) {
      // We only need to worry about snapshots that someone else knows about
      mSnapshot->DrawTargetWillChange();
    }
    mSnapshot = nullptr;
  }
}

void
DrawTargetCairo::WillChange(const Path* aPath /* = nullptr */)
{
  MarkSnapshotIndependent();
  MOZ_ASSERT(!mLockedBits);
}

void
DrawTargetCairo::SetTransform(const Matrix& aTransform)
{
  DrawTarget::SetTransform(aTransform);

  mTransformSingular = aTransform.IsSingular();
  if (!mTransformSingular) {
    cairo_matrix_t mat;
    GfxMatrixToCairoMatrix(mTransform, mat);
    cairo_set_matrix(mContext, &mat);
  }
}

Rect
DrawTargetCairo::GetUserSpaceClip()
{
  double clipX1, clipY1, clipX2, clipY2;
  cairo_clip_extents(mContext, &clipX1, &clipY1, &clipX2, &clipY2);
  return Rect(clipX1, clipY1, clipX2 - clipX1, clipY2 - clipY1); // Narrowing of doubles to floats
}

cairo_t*
BorrowedCairoContext::BorrowCairoContextFromDrawTarget(DrawTarget* aDT)
{
  if (aDT->GetBackendType() != BackendType::CAIRO ||
      aDT->IsDualDrawTarget() ||
      aDT->IsTiledDrawTarget()) {
    return nullptr;
  }
  DrawTargetCairo* cairoDT = static_cast<DrawTargetCairo*>(aDT);

  cairoDT->WillChange();

  // save the state to make it easier for callers to avoid mucking with things
  cairo_save(cairoDT->mContext);

  // Neuter the DrawTarget while the context is being borrowed
  cairo_t* cairo = cairoDT->mContext;
  cairoDT->mContext = nullptr;

  return cairo;
}

void
BorrowedCairoContext::ReturnCairoContextToDrawTarget(DrawTarget* aDT,
                                                     cairo_t* aCairo)
{
  if (aDT->GetBackendType() != BackendType::CAIRO ||
      aDT->IsDualDrawTarget() ||
      aDT->IsTiledDrawTarget()) {
    return;
  }
  DrawTargetCairo* cairoDT = static_cast<DrawTargetCairo*>(aDT);

  cairo_restore(aCairo);
  cairoDT->mContext = aCairo;
}

#ifdef MOZ_X11
bool
BorrowedXlibDrawable::Init(DrawTarget* aDT)
{
  MOZ_ASSERT(aDT, "Caller should check for nullptr");
  MOZ_ASSERT(!mDT, "Can't initialize twice!");
  mDT = aDT;
  mDrawable = X11None;

#ifdef CAIRO_HAS_XLIB_SURFACE
  if (aDT->GetBackendType() != BackendType::CAIRO ||
      aDT->IsDualDrawTarget() ||
      aDT->IsTiledDrawTarget()) {
    return false;
  }

  DrawTargetCairo* cairoDT = static_cast<DrawTargetCairo*>(aDT);
  cairo_surface_t* surf = cairo_get_group_target(cairoDT->mContext);
  if (cairo_surface_get_type(surf) != CAIRO_SURFACE_TYPE_XLIB) {
    return false;
  }
  cairo_surface_flush(surf);

  cairoDT->WillChange();

  mDisplay = cairo_xlib_surface_get_display(surf);
  mDrawable = cairo_xlib_surface_get_drawable(surf);
  mScreen = cairo_xlib_surface_get_screen(surf);
  mVisual = cairo_xlib_surface_get_visual(surf);
  mXRenderFormat = cairo_xlib_surface_get_xrender_format(surf);
  mSize.width = cairo_xlib_surface_get_width(surf);
  mSize.height = cairo_xlib_surface_get_height(surf);

  double x = 0, y = 0;
  cairo_surface_get_device_offset(surf, &x, &y);
  mOffset = Point(x, y);

  return true;
#else
  return false;
#endif
}

void
BorrowedXlibDrawable::Finish()
{
  DrawTargetCairo* cairoDT = static_cast<DrawTargetCairo*>(mDT);
  cairo_surface_t* surf = cairo_get_group_target(cairoDT->mContext);
  cairo_surface_mark_dirty(surf);
  if (mDrawable) {
    mDrawable = X11None;
  }
}
#endif

} // namespace gfx
} // namespace mozilla