DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (b6057e17f856)

VCS Links

Line Code
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
/* -*- 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/. */

#ifdef _MSC_VER
#define _USE_MATH_DEFINES
#endif
#include <math.h>

#include "mozilla/Alignment.h"

#include "cairo.h"

#include "gfxContext.h"

#include "gfxColor.h"
#include "gfxMatrix.h"
#include "gfxASurface.h"
#include "gfxPattern.h"
#include "gfxPlatform.h"
#include "gfxTeeSurface.h"
#include "GeckoProfiler.h"
#include "gfx2DGlue.h"
#include "mozilla/gfx/PathHelpers.h"
#include <algorithm>

#if CAIRO_HAS_DWRITE_FONT
#include "gfxWindowsPlatform.h"
#endif

using namespace mozilla;
using namespace mozilla::gfx;

UserDataKey gfxContext::sDontUseAsSourceKey;

/* This class lives on the stack and allows gfxContext users to easily, and
 * performantly get a gfx::Pattern to use for drawing in their current context.
 */
class GeneralPattern
{
public:    
  GeneralPattern(gfxContext *aContext) : mContext(aContext), mPattern(nullptr) {}
  ~GeneralPattern() { if (mPattern) { mPattern->~Pattern(); } }

  operator mozilla::gfx::Pattern&()
  {
    gfxContext::AzureState &state = mContext->CurrentState();

    if (state.pattern) {
      return *state.pattern->GetPattern(mContext->mDT, state.patternTransformChanged ? &state.patternTransform : nullptr);
    } else if (state.sourceSurface) {
      Matrix transform = state.surfTransform;

      if (state.patternTransformChanged) {
        Matrix mat = mContext->GetDTTransform();
        mat.Invert();

        transform = transform * state.patternTransform * mat;
      }

      mPattern = new (mSurfacePattern.addr())
        SurfacePattern(state.sourceSurface, ExtendMode::CLAMP, transform);
      return *mPattern;
    } else {
      mPattern = new (mColorPattern.addr())
        ColorPattern(state.color);
      return *mPattern;
    }
  }

private:
  union {
    mozilla::AlignedStorage2<mozilla::gfx::ColorPattern> mColorPattern;
    mozilla::AlignedStorage2<mozilla::gfx::SurfacePattern> mSurfacePattern;
  };

  gfxContext *mContext;
  Pattern *mPattern;
};

gfxContext::gfxContext(gfxASurface *surface)
  : mRefCairo(nullptr)
  , mSurface(surface)
{
  MOZ_COUNT_CTOR(gfxContext);

  mCairo = cairo_create(surface->CairoSurface());
  mFlags = surface->GetDefaultContextFlags();
  if (mSurface->GetRotateForLandscape()) {
    // Rotate page 90 degrees to draw landscape page on portrait paper
    gfxIntSize size = mSurface->GetSize();
    Translate(gfxPoint(0, size.width));
    gfxMatrix matrix(0, -1,
                      1,  0,
                      0,  0);
    Multiply(matrix);
  }
}

gfxContext::gfxContext(DrawTarget *aTarget, const Point& aDeviceOffset)
  : mPathIsRect(false)
  , mTransformChanged(false)
  , mCairo(nullptr)
  , mRefCairo(nullptr)
  , mSurface(nullptr)
  , mFlags(0)
  , mDT(aTarget)
  , mOriginalDT(aTarget)
{
  MOZ_COUNT_CTOR(gfxContext);

  mStateStack.SetLength(1);
  CurrentState().drawTarget = mDT;
  CurrentState().deviceOffset = aDeviceOffset;
  mDT->SetTransform(Matrix());
}

/* static */ already_AddRefed<gfxContext>
gfxContext::ContextForDrawTarget(DrawTarget* aTarget)
{
  Matrix transform = aTarget->GetTransform();
  nsRefPtr<gfxContext> result = new gfxContext(aTarget);
  result->SetMatrix(ThebesMatrix(transform));
  return result.forget();
}

gfxContext::~gfxContext()
{
  if (mCairo) {
    cairo_destroy(mCairo);
  }
  if (mRefCairo) {
    cairo_destroy(mRefCairo);
  }
  if (mDT) {
    for (int i = mStateStack.Length() - 1; i >= 0; i--) {
      for (unsigned int c = 0; c < mStateStack[i].pushedClips.Length(); c++) {
        mDT->PopClip();
      }

      if (mStateStack[i].clipWasReset) {
        break;
      }
    }
    mDT->Flush();
  }
  MOZ_COUNT_DTOR(gfxContext);
}

gfxASurface *
gfxContext::OriginalSurface()
{
    if (mCairo || mSurface) {
        return mSurface;
    }

    if (mOriginalDT && mOriginalDT->GetType() == BackendType::CAIRO) {
        cairo_surface_t *s =
            (cairo_surface_t*)mOriginalDT->GetNativeSurface(NativeSurfaceType::CAIRO_SURFACE);
        if (s) {
            mSurface = gfxASurface::Wrap(s);
            return mSurface;
        }
    }
    return nullptr;
}

already_AddRefed<gfxASurface>
gfxContext::CurrentSurface(gfxFloat *dx, gfxFloat *dy)
{
  if (mCairo) {
    cairo_surface_t *s = cairo_get_group_target(mCairo);
    if (s == mSurface->CairoSurface()) {
        if (dx && dy)
            cairo_surface_get_device_offset(s, dx, dy);
        nsRefPtr<gfxASurface> ret = mSurface;
        return ret.forget();
    }

    if (dx && dy)
        cairo_surface_get_device_offset(s, dx, dy);
    return gfxASurface::Wrap(s);
  } else {
    if (mDT->GetType() == BackendType::CAIRO) {
        cairo_surface_t *s =
            (cairo_surface_t*)mDT->GetNativeSurface(NativeSurfaceType::CAIRO_SURFACE);
        if (s) {
            if (dx && dy) {
                *dx = -CurrentState().deviceOffset.x;
                *dy = -CurrentState().deviceOffset.y;
            }
            return gfxASurface::Wrap(s);
        }
    }

    if (dx && dy) {
      *dx = *dy = 0;
    }
    // An Azure context doesn't have a surface backing it.
    return nullptr;
  }
}

cairo_t *
gfxContext::GetCairo()
{
  if (mCairo) {
    return mCairo;
  }

  if (mDT->GetType() == BackendType::CAIRO) {
    cairo_t *ctx =
      (cairo_t*)mDT->GetNativeSurface(NativeSurfaceType::CAIRO_CONTEXT);
    if (ctx) {
      return ctx;
    }
  }

  if (mRefCairo) {
    // Set transform!
    return mRefCairo;
  }

  mRefCairo = cairo_create(gfxPlatform::GetPlatform()->ScreenReferenceSurface()->CairoSurface()); 

  return mRefCairo;
}

void
gfxContext::Save()
{
  if (mCairo) {
    cairo_save(mCairo);
  } else {
    CurrentState().transform = mTransform;
    mStateStack.AppendElement(AzureState(CurrentState()));
    CurrentState().clipWasReset = false;
    CurrentState().pushedClips.Clear();
  }
}

void
gfxContext::Restore()
{
  if (mCairo) {
    cairo_restore(mCairo);
  } else {
    for (unsigned int c = 0; c < CurrentState().pushedClips.Length(); c++) {
      mDT->PopClip();
    }

    if (CurrentState().clipWasReset &&
        CurrentState().drawTarget == mStateStack[mStateStack.Length() - 2].drawTarget) {
      PushClipsToDT(mDT);
    }

    mStateStack.RemoveElementAt(mStateStack.Length() - 1);

    mDT = CurrentState().drawTarget;

    ChangeTransform(CurrentState().transform, false);
  }
}

// drawing
void
gfxContext::NewPath()
{
  if (mCairo) {
    cairo_new_path(mCairo);
  } else {
    mPath = nullptr;
    mPathBuilder = nullptr;
    mPathIsRect = false;
    mTransformChanged = false;
  }
}

void
gfxContext::ClosePath()
{
  if (mCairo) {
    cairo_close_path(mCairo);
  } else {
    EnsurePathBuilder();
    mPathBuilder->Close();
  }
}

already_AddRefed<gfxPath> gfxContext::CopyPath()
{
  nsRefPtr<gfxPath> path;
  if (mCairo) {
    path = new gfxPath(cairo_copy_path(mCairo));
  } else {
    EnsurePath();
    path = new gfxPath(mPath);
  }
  return path.forget();
}

void gfxContext::SetPath(gfxPath* path)
{
  if (mCairo) {
    cairo_new_path(mCairo);
    if (path->mPath->status == CAIRO_STATUS_SUCCESS && path->mPath->num_data != 0)
        cairo_append_path(mCairo, path->mPath);
  } else {
    MOZ_ASSERT(path->mMoz2DPath, "Can't mix cairo and azure paths!");
    MOZ_ASSERT(path->mMoz2DPath->GetBackendType() == mDT->GetType());
    mPath = path->mMoz2DPath;
    mPathBuilder = nullptr;
    mPathIsRect = false;
    mTransformChanged = false;
  }
}

gfxPoint
gfxContext::CurrentPoint()
{
  if (mCairo) {
    double x, y;
    cairo_get_current_point(mCairo, &x, &y);
    return gfxPoint(x, y);
  } else {
    EnsurePathBuilder();
    return ThebesPoint(mPathBuilder->CurrentPoint());
  }
}

void
gfxContext::Stroke()
{
  if (mCairo) {
    cairo_stroke_preserve(mCairo);
  } else {
    AzureState &state = CurrentState();
    if (mPathIsRect) {
      MOZ_ASSERT(!mTransformChanged);

      mDT->StrokeRect(mRect, GeneralPattern(this),
                      state.strokeOptions,
                      DrawOptions(1.0f, GetOp(), state.aaMode));
    } else {
      EnsurePath();

      mDT->Stroke(mPath, GeneralPattern(this), state.strokeOptions,
                  DrawOptions(1.0f, GetOp(), state.aaMode));
    }
  }
}

void
gfxContext::Fill()
{
  PROFILER_LABEL("gfxContext", "Fill",
    js::ProfileEntry::Category::GRAPHICS);

  if (mCairo) {
    cairo_fill_preserve(mCairo);
  } else {
    FillAzure(1.0f);
  }
}

void
gfxContext::FillWithOpacity(gfxFloat aOpacity)
{
  if (mCairo) {
    // This method exists in the hope that one day cairo gets a direct
    // API for this, and then we would change this method to use that
    // API instead.
    if (aOpacity != 1.0) {
      gfxContextAutoSaveRestore saveRestore(this);
      Clip();
      Paint(aOpacity);
    } else {
      Fill();
    }
  } else {
    FillAzure(Float(aOpacity));
  }
}

void
gfxContext::MoveTo(const gfxPoint& pt)
{
  if (mCairo) {
    cairo_move_to(mCairo, pt.x, pt.y);
  } else {
    EnsurePathBuilder();
    mPathBuilder->MoveTo(ToPoint(pt));
  }
}

void
gfxContext::NewSubPath()
{
  if (mCairo) {
    cairo_new_sub_path(mCairo);
  } else {
    // XXX - This has no users, we should kill it, it should be equivelant to a
    // MoveTo to the path's current point.
  }
}

void
gfxContext::LineTo(const gfxPoint& pt)
{
  if (mCairo) {
    cairo_line_to(mCairo, pt.x, pt.y);
  } else {
    EnsurePathBuilder();
    mPathBuilder->LineTo(ToPoint(pt));
  }
}

void
gfxContext::CurveTo(const gfxPoint& pt1, const gfxPoint& pt2, const gfxPoint& pt3)
{
  if (mCairo) {
    cairo_curve_to(mCairo, pt1.x, pt1.y, pt2.x, pt2.y, pt3.x, pt3.y);
  } else {
    EnsurePathBuilder();
    mPathBuilder->BezierTo(ToPoint(pt1), ToPoint(pt2), ToPoint(pt3));
  }
}

void
gfxContext::QuadraticCurveTo(const gfxPoint& pt1, const gfxPoint& pt2)
{
  if (mCairo) {
    double cx, cy;
    cairo_get_current_point(mCairo, &cx, &cy);
    cairo_curve_to(mCairo,
                   (cx + pt1.x * 2.0) / 3.0,
                   (cy + pt1.y * 2.0) / 3.0,
                   (pt1.x * 2.0 + pt2.x) / 3.0,
                   (pt1.y * 2.0 + pt2.y) / 3.0,
                   pt2.x,
                   pt2.y);
  } else {
    EnsurePathBuilder();
    mPathBuilder->QuadraticBezierTo(ToPoint(pt1), ToPoint(pt2));
  }
}

void
gfxContext::Arc(const gfxPoint& center, gfxFloat radius,
                gfxFloat angle1, gfxFloat angle2)
{
  if (mCairo) {
    cairo_arc(mCairo, center.x, center.y, radius, angle1, angle2);
  } else {
    EnsurePathBuilder();
    mPathBuilder->Arc(ToPoint(center), Float(radius), Float(angle1), Float(angle2));
  }
}

void
gfxContext::NegativeArc(const gfxPoint& center, gfxFloat radius,
                        gfxFloat angle1, gfxFloat angle2)
{
  if (mCairo) {
    cairo_arc_negative(mCairo, center.x, center.y, radius, angle1, angle2);
  } else {
    EnsurePathBuilder();
    mPathBuilder->Arc(ToPoint(center), Float(radius), Float(angle2), Float(angle1));
  }
}

void
gfxContext::Line(const gfxPoint& start, const gfxPoint& end)
{
  if (mCairo) {
    MoveTo(start);
    LineTo(end);
  } else {
    EnsurePathBuilder();
    mPathBuilder->MoveTo(ToPoint(start));
    mPathBuilder->LineTo(ToPoint(end));
  }
}

// XXX snapToPixels is only valid when snapping for filled
// rectangles and for even-width stroked rectangles.
// For odd-width stroked rectangles, we need to offset x/y by
// 0.5...
void
gfxContext::Rectangle(const gfxRect& rect, bool snapToPixels)
{
  if (mCairo) {
    if (snapToPixels) {
        gfxRect snappedRect(rect);

        if (UserToDevicePixelSnapped(snappedRect, true))
        {
            cairo_matrix_t mat;
            cairo_get_matrix(mCairo, &mat);
            cairo_identity_matrix(mCairo);
            Rectangle(snappedRect);
            cairo_set_matrix(mCairo, &mat);

            return;
        }
    }

    cairo_rectangle(mCairo, rect.X(), rect.Y(), rect.Width(), rect.Height());
  } else {
    Rect rec = ToRect(rect);

    if (snapToPixels) {
      gfxRect newRect(rect);
      if (UserToDevicePixelSnapped(newRect, true)) {
        gfxMatrix mat = ThebesMatrix(mTransform);
        mat.Invert();

        // We need the user space rect.
        rec = ToRect(mat.TransformBounds(newRect));
      }
    }

    if (!mPathBuilder && !mPathIsRect) {
      mPathIsRect = true;
      mRect = rec;
      return;
    }

    EnsurePathBuilder();

    mPathBuilder->MoveTo(rec.TopLeft());
    mPathBuilder->LineTo(rec.TopRight());
    mPathBuilder->LineTo(rec.BottomRight());
    mPathBuilder->LineTo(rec.BottomLeft());
    mPathBuilder->Close();
  }
}

void
gfxContext::Ellipse(const gfxPoint& center, const gfxSize& dimensions)
{
  gfxSize halfDim = dimensions / 2.0;
  gfxRect r(center - gfxPoint(halfDim.width, halfDim.height), dimensions);
  gfxCornerSizes c(halfDim, halfDim, halfDim, halfDim);

  RoundedRectangle (r, c);
}

void
gfxContext::Polygon(const gfxPoint *points, uint32_t numPoints)
{
  if (mCairo) {
    if (numPoints == 0)
        return;

    cairo_move_to(mCairo, points[0].x, points[0].y);
    for (uint32_t i = 1; i < numPoints; ++i) {
        cairo_line_to(mCairo, points[i].x, points[i].y);
    }
  } else {
    if (numPoints == 0) {
      return;
    }

    EnsurePathBuilder();

    mPathBuilder->MoveTo(ToPoint(points[0]));
    for (uint32_t i = 1; i < numPoints; i++) {
      mPathBuilder->LineTo(ToPoint(points[i]));
    }
  }
}

void
gfxContext::DrawSurface(gfxASurface *surface, const gfxSize& size)
{
  if (mCairo) {
    cairo_save(mCairo);
    cairo_set_source_surface(mCairo, surface->CairoSurface(), 0, 0);
    cairo_new_path(mCairo);

    // pixel-snap this
    Rectangle(gfxRect(gfxPoint(0.0, 0.0), size), true);

    cairo_fill(mCairo);
    cairo_restore(mCairo);
  } else {
    // Lifetime needs to be limited here since we may wrap surface's data.
    RefPtr<SourceSurface> surf =
      gfxPlatform::GetPlatform()->GetSourceSurfaceForSurface(mDT, surface);

    if (!surf) {
      return;
    }

    Rect rect(0, 0, Float(size.width), Float(size.height));
    rect.Intersect(Rect(0, 0, Float(surf->GetSize().width), Float(surf->GetSize().height)));

    // XXX - Should fix pixel snapping.
    mDT->DrawSurface(surf, rect, rect);
  }
}

// transform stuff
void
gfxContext::Translate(const gfxPoint& pt)
{
  if (mCairo) {
    cairo_translate(mCairo, pt.x, pt.y);
  } else {
    Matrix newMatrix = mTransform;

    ChangeTransform(newMatrix.Translate(Float(pt.x), Float(pt.y)));
  }
}

void
gfxContext::Scale(gfxFloat x, gfxFloat y)
{
  if (mCairo) {
    cairo_scale(mCairo, x, y);
  } else {
    Matrix newMatrix = mTransform;

    ChangeTransform(newMatrix.Scale(Float(x), Float(y)));
  }
}

void
gfxContext::Rotate(gfxFloat angle)
{
  if (mCairo) {
    cairo_rotate(mCairo, angle);
  } else {
    Matrix rotation = Matrix::Rotation(Float(angle));
    ChangeTransform(rotation * mTransform);
  }
}

void
gfxContext::Multiply(const gfxMatrix& matrix)
{
  if (mCairo) {
    const cairo_matrix_t& mat = reinterpret_cast<const cairo_matrix_t&>(matrix);
    cairo_transform(mCairo, &mat);
  } else {
    ChangeTransform(ToMatrix(matrix) * mTransform);
  }
}

void
gfxContext::MultiplyAndNudgeToIntegers(const gfxMatrix& matrix)
{
  if (mCairo) {
    const cairo_matrix_t& mat = reinterpret_cast<const cairo_matrix_t&>(matrix);
    cairo_transform(mCairo, &mat);
    // XXX nudging to integers not currently supported for Thebes
  } else {
    Matrix transform = ToMatrix(matrix) * mTransform;
    transform.NudgeToIntegers();
    ChangeTransform(transform);
  }
}

void
gfxContext::SetMatrix(const gfxMatrix& matrix)
{
  if (mCairo) {
    const cairo_matrix_t& mat = reinterpret_cast<const cairo_matrix_t&>(matrix);
    cairo_set_matrix(mCairo, &mat);
  } else {
    ChangeTransform(ToMatrix(matrix));
  }
}

void
gfxContext::IdentityMatrix()
{
  if (mCairo) {
    cairo_identity_matrix(mCairo);
  } else {
    ChangeTransform(Matrix());
  }
}

gfxMatrix
gfxContext::CurrentMatrix() const
{
  if (mCairo) {
    cairo_matrix_t mat;
    cairo_get_matrix(mCairo, &mat);
    return gfxMatrix(*reinterpret_cast<gfxMatrix*>(&mat));
  } else {
    return ThebesMatrix(mTransform);
  }
}

void
gfxContext::NudgeCurrentMatrixToIntegers()
{
  if (mCairo) {
    cairo_matrix_t mat;
    cairo_get_matrix(mCairo, &mat);
    gfxMatrix(*reinterpret_cast<gfxMatrix*>(&mat)).NudgeToIntegers();
    cairo_set_matrix(mCairo, &mat);
  } else {
    gfxMatrix matrix = ThebesMatrix(mTransform);
    matrix.NudgeToIntegers();
    ChangeTransform(ToMatrix(matrix));
  }
}

gfxPoint
gfxContext::DeviceToUser(const gfxPoint& point) const
{
  if (mCairo) {
    gfxPoint ret = point;
    cairo_device_to_user(mCairo, &ret.x, &ret.y);
    return ret;
  } else {
    Matrix matrix = mTransform;

    matrix.Invert();

    return ThebesPoint(matrix * ToPoint(point));
  }
}

gfxSize
gfxContext::DeviceToUser(const gfxSize& size) const
{
  if (mCairo) {
    gfxSize ret = size;
    cairo_device_to_user_distance(mCairo, &ret.width, &ret.height);
    return ret;
  } else {
    Matrix matrix = mTransform;

    matrix.Invert();

    return ThebesSize(matrix * ToSize(size));
  }
}

gfxRect
gfxContext::DeviceToUser(const gfxRect& rect) const
{
  if (mCairo) {
    gfxRect ret = rect;
    cairo_device_to_user(mCairo, &ret.x, &ret.y);
    cairo_device_to_user_distance(mCairo, &ret.width, &ret.height);
    return ret;
  } else {
    Matrix matrix = mTransform;

    matrix.Invert();

    return ThebesRect(matrix.TransformBounds(ToRect(rect)));
  }
}

gfxPoint
gfxContext::UserToDevice(const gfxPoint& point) const
{
  if (mCairo) {
    gfxPoint ret = point;
    cairo_user_to_device(mCairo, &ret.x, &ret.y);
    return ret;
  } else {
    return ThebesPoint(mTransform * ToPoint(point));
  }
}

gfxSize
gfxContext::UserToDevice(const gfxSize& size) const
{
  if (mCairo) {
    gfxSize ret = size;
    cairo_user_to_device_distance(mCairo, &ret.width, &ret.height);
    return ret;
  } else {
    const Matrix &matrix = mTransform;

    gfxSize newSize;
    newSize.width = size.width * matrix._11 + size.height * matrix._12;
    newSize.height = size.width * matrix._21 + size.height * matrix._22;
    return newSize;
  }
}

gfxRect
gfxContext::UserToDevice(const gfxRect& rect) const
{
  if (mCairo) {
    double xmin = rect.X(), ymin = rect.Y(), xmax = rect.XMost(), ymax = rect.YMost();

    double x[3], y[3];
    x[0] = xmin;  y[0] = ymax;
    x[1] = xmax;  y[1] = ymax;
    x[2] = xmax;  y[2] = ymin;

    cairo_user_to_device(mCairo, &xmin, &ymin);
    xmax = xmin;
    ymax = ymin;
    for (int i = 0; i < 3; i++) {
        cairo_user_to_device(mCairo, &x[i], &y[i]);
        xmin = std::min(xmin, x[i]);
        xmax = std::max(xmax, x[i]);
        ymin = std::min(ymin, y[i]);
        ymax = std::max(ymax, y[i]);
    }

    return gfxRect(xmin, ymin, xmax - xmin, ymax - ymin);
  } else {
    const Matrix &matrix = mTransform;
    return ThebesRect(matrix.TransformBounds(ToRect(rect)));
  }
}

bool
gfxContext::UserToDevicePixelSnapped(gfxRect& rect, bool ignoreScale) const
{
  if (GetFlags() & FLAG_DISABLE_SNAPPING)
      return false;

  // if we're not at 1.0 scale, don't snap, unless we're
  // ignoring the scale.  If we're not -just- a scale,
  // never snap.
  const gfxFloat epsilon = 0.0000001;
#define WITHIN_E(a,b) (fabs((a)-(b)) < epsilon)
  if (mCairo) {
    cairo_matrix_t mat;
    cairo_get_matrix(mCairo, &mat);
    if (!ignoreScale &&
        (!WITHIN_E(mat.xx,1.0) || !WITHIN_E(mat.yy,1.0) ||
          !WITHIN_E(mat.xy,0.0) || !WITHIN_E(mat.yx,0.0)))
        return false;
  } else {
    Matrix mat = mTransform;
    if (!ignoreScale &&
        (!WITHIN_E(mat._11,1.0) || !WITHIN_E(mat._22,1.0) ||
          !WITHIN_E(mat._12,0.0) || !WITHIN_E(mat._21,0.0)))
        return false;
  }
#undef WITHIN_E

  gfxPoint p1 = UserToDevice(rect.TopLeft());
  gfxPoint p2 = UserToDevice(rect.TopRight());
  gfxPoint p3 = UserToDevice(rect.BottomRight());

  // Check that the rectangle is axis-aligned. For an axis-aligned rectangle,
  // two opposite corners define the entire rectangle. So check if
  // the axis-aligned rectangle with opposite corners p1 and p3
  // define an axis-aligned rectangle whose other corners are p2 and p4.
  // We actually only need to check one of p2 and p4, since an affine
  // transform maps parallelograms to parallelograms.
  if (p2 == gfxPoint(p1.x, p3.y) || p2 == gfxPoint(p3.x, p1.y)) {
      p1.Round();
      p3.Round();

      rect.MoveTo(gfxPoint(std::min(p1.x, p3.x), std::min(p1.y, p3.y)));
      rect.SizeTo(gfxSize(std::max(p1.x, p3.x) - rect.X(),
                          std::max(p1.y, p3.y) - rect.Y()));
      return true;
  }

  return false;
}

bool
gfxContext::UserToDevicePixelSnapped(gfxPoint& pt, bool ignoreScale) const
{
  if (GetFlags() & FLAG_DISABLE_SNAPPING)
      return false;

  // if we're not at 1.0 scale, don't snap, unless we're
  // ignoring the scale.  If we're not -just- a scale,
  // never snap.
  const gfxFloat epsilon = 0.0000001;
#define WITHIN_E(a,b) (fabs((a)-(b)) < epsilon)
  if (mCairo) {
    cairo_matrix_t mat;
    cairo_get_matrix(mCairo, &mat);
    if (!ignoreScale &&
        (!WITHIN_E(mat.xx,1.0) || !WITHIN_E(mat.yy,1.0) ||
          !WITHIN_E(mat.xy,0.0) || !WITHIN_E(mat.yx,0.0)))
        return false;
  } else {
    Matrix mat = mTransform;
    if (!ignoreScale &&
        (!WITHIN_E(mat._11,1.0) || !WITHIN_E(mat._22,1.0) ||
          !WITHIN_E(mat._12,0.0) || !WITHIN_E(mat._21,0.0)))
        return false;
  }
#undef WITHIN_E

  pt = UserToDevice(pt);
  pt.Round();
  return true;
}

void
gfxContext::PixelSnappedRectangleAndSetPattern(const gfxRect& rect,
                                               gfxPattern *pattern)
{
  gfxRect r(rect);

  // Bob attempts to pixel-snap the rectangle, and returns true if
  // the snapping succeeds.  If it does, we need to set up an
  // identity matrix, because the rectangle given back is in device
  // coordinates.
  //
  // We then have to call a translate to dr.pos afterwards, to make
  // sure the image lines up in the right place with our pixel
  // snapped rectangle.
  //
  // If snapping wasn't successful, we just translate to where the
  // pattern would normally start (in app coordinates) and do the
  // same thing.
  Rectangle(r, true);
  SetPattern(pattern);
}

void
gfxContext::SetAntialiasMode(AntialiasMode mode)
{
  if (mCairo) {
    if (mode == MODE_ALIASED) {
        cairo_set_antialias(mCairo, CAIRO_ANTIALIAS_NONE);
    } else if (mode == MODE_COVERAGE) {
        cairo_set_antialias(mCairo, CAIRO_ANTIALIAS_DEFAULT);
    }
  } else {
    if (mode == MODE_ALIASED) {
      CurrentState().aaMode = gfx::AntialiasMode::NONE;
    } else if (mode == MODE_COVERAGE) {
      CurrentState().aaMode = gfx::AntialiasMode::SUBPIXEL;
    }
  }
}

gfxContext::AntialiasMode
gfxContext::CurrentAntialiasMode() const
{
  if (mCairo) {
    cairo_antialias_t aa = cairo_get_antialias(mCairo);
    if (aa == CAIRO_ANTIALIAS_NONE)
        return MODE_ALIASED;
    return MODE_COVERAGE;
  } else {
    if (CurrentState().aaMode == gfx::AntialiasMode::NONE) {
      return MODE_ALIASED;
    }
    return MODE_COVERAGE;
  }
}

void
gfxContext::SetDash(gfxLineType ltype)
{
  static double dash[] = {5.0, 5.0};
  static double dot[] = {1.0, 1.0};

  switch (ltype) {
      case gfxLineDashed:
          SetDash(dash, 2, 0.0);
          break;
      case gfxLineDotted:
          SetDash(dot, 2, 0.0);
          break;
      case gfxLineSolid:
      default:
          SetDash(nullptr, 0, 0.0);
          break;
  }
}

void
gfxContext::SetDash(gfxFloat *dashes, int ndash, gfxFloat offset)
{
  if (mCairo) {
    cairo_set_dash(mCairo, dashes, ndash, offset);
  } else {
    AzureState &state = CurrentState();

    state.dashPattern.SetLength(ndash);
    for (int i = 0; i < ndash; i++) {
      state.dashPattern[i] = Float(dashes[i]);
    }
    state.strokeOptions.mDashLength = ndash;
    state.strokeOptions.mDashOffset = Float(offset);
    state.strokeOptions.mDashPattern = ndash ? state.dashPattern.Elements()
                                             : nullptr;
  }
}

bool
gfxContext::CurrentDash(FallibleTArray<gfxFloat>& dashes, gfxFloat* offset) const
{
  if (mCairo) {
    int count = cairo_get_dash_count(mCairo);
    if (count <= 0 || !dashes.SetLength(count)) {
        return false;
    }
    cairo_get_dash(mCairo, dashes.Elements(), offset);
    return true;
  } else {
    const AzureState &state = CurrentState();
    int count = state.strokeOptions.mDashLength;

    if (count <= 0 || !dashes.SetLength(count)) {
      return false;
    }

    for (int i = 0; i < count; i++) {
      dashes[i] = state.dashPattern[i];
    }

    *offset = state.strokeOptions.mDashOffset;

    return true;
  }
}

gfxFloat
gfxContext::CurrentDashOffset() const
{
  if (mCairo) {
    if (cairo_get_dash_count(mCairo) <= 0) {
        return 0.0;
    }
    gfxFloat offset;
    cairo_get_dash(mCairo, nullptr, &offset);
    return offset;
  } else {
    return CurrentState().strokeOptions.mDashOffset;
  }
}

void
gfxContext::SetLineWidth(gfxFloat width)
{
  if (mCairo) {
    cairo_set_line_width(mCairo, width);
  } else {
    CurrentState().strokeOptions.mLineWidth = Float(width);
  }
}

gfxFloat
gfxContext::CurrentLineWidth() const
{
  if (mCairo) {
    return cairo_get_line_width(mCairo);
  } else {
    return CurrentState().strokeOptions.mLineWidth;
  }
}

void
gfxContext::SetOperator(GraphicsOperator op)
{
  if (mCairo) {
    if (mFlags & FLAG_SIMPLIFY_OPERATORS) {
        if (op != OPERATOR_SOURCE &&
            op != OPERATOR_CLEAR &&
            op != OPERATOR_OVER)
            op = OPERATOR_OVER;
    }

    cairo_set_operator(mCairo, (cairo_operator_t)op);
  } else {
    if (op == OPERATOR_CLEAR) {
      CurrentState().opIsClear = true;
      return;
    }
    CurrentState().opIsClear = false;
    CurrentState().op = CompositionOpForOp(op);
  }
}

gfxContext::GraphicsOperator
gfxContext::CurrentOperator() const
{
  if (mCairo) {
    return (GraphicsOperator)cairo_get_operator(mCairo);
  } else {
    return ThebesOp(CurrentState().op);
  }
}

void
gfxContext::SetLineCap(GraphicsLineCap cap)
{
  if (mCairo) {
    cairo_set_line_cap(mCairo, (cairo_line_cap_t)cap);
  } else {
    CurrentState().strokeOptions.mLineCap = ToCapStyle(cap);
  }
}

gfxContext::GraphicsLineCap
gfxContext::CurrentLineCap() const
{
  if (mCairo) {
    return (GraphicsLineCap)cairo_get_line_cap(mCairo);
  } else {
    return ThebesLineCap(CurrentState().strokeOptions.mLineCap);
  }
}

void
gfxContext::SetLineJoin(GraphicsLineJoin join)
{
  if (mCairo) {
    cairo_set_line_join(mCairo, (cairo_line_join_t)join);
  } else {
    CurrentState().strokeOptions.mLineJoin = ToJoinStyle(join);
  }
}

gfxContext::GraphicsLineJoin
gfxContext::CurrentLineJoin() const
{
  if (mCairo) {
    return (GraphicsLineJoin)cairo_get_line_join(mCairo);
  } else {
    return ThebesLineJoin(CurrentState().strokeOptions.mLineJoin);
  }
}

void
gfxContext::SetMiterLimit(gfxFloat limit)
{
  if (mCairo) {
    cairo_set_miter_limit(mCairo, limit);
  } else {
    CurrentState().strokeOptions.mMiterLimit = Float(limit);
  }
}

gfxFloat
gfxContext::CurrentMiterLimit() const
{
  if (mCairo) {
    return cairo_get_miter_limit(mCairo);
  } else {
    return CurrentState().strokeOptions.mMiterLimit;
  }
}

void
gfxContext::SetFillRule(FillRule rule)
{
  if (mCairo) {
    cairo_set_fill_rule(mCairo, (cairo_fill_rule_t)rule);
  } else {
    CurrentState().fillRule = rule == FILL_RULE_WINDING ? gfx::FillRule::FILL_WINDING : gfx::FillRule::FILL_EVEN_ODD;
  }
}

gfxContext::FillRule
gfxContext::CurrentFillRule() const
{
  if (mCairo) {
    return (FillRule)cairo_get_fill_rule(mCairo);
  } else {
    return FILL_RULE_WINDING;
  }
}

// clipping
void
gfxContext::Clip(const gfxRect& rect)
{
  if (mCairo) {
    cairo_new_path(mCairo);
    cairo_rectangle(mCairo, rect.X(), rect.Y(), rect.Width(), rect.Height());
    cairo_clip(mCairo);
  } else {
    AzureState::PushedClip clip = { nullptr, ToRect(rect), mTransform };
    CurrentState().pushedClips.AppendElement(clip);
    mDT->PushClipRect(ToRect(rect));
    NewPath();
  }
}

void
gfxContext::Clip()
{
  if (mCairo) {
    cairo_clip_preserve(mCairo);
  } else {
    if (mPathIsRect) {
      MOZ_ASSERT(!mTransformChanged);

      AzureState::PushedClip clip = { nullptr, mRect, mTransform };
      CurrentState().pushedClips.AppendElement(clip);
      mDT->PushClipRect(mRect);
    } else {
      EnsurePath();
      mDT->PushClip(mPath);
      AzureState::PushedClip clip = { mPath, Rect(), mTransform };
      CurrentState().pushedClips.AppendElement(clip);
    }
  }
}

void
gfxContext::ResetClip()
{
  if (mCairo) {
    cairo_reset_clip(mCairo);
  } else {
    for (int i = mStateStack.Length() - 1; i >= 0; i--) {
      for (unsigned int c = 0; c < mStateStack[i].pushedClips.Length(); c++) {
        mDT->PopClip();
      }

      if (mStateStack[i].clipWasReset) {
        break;
      }
    }
    CurrentState().pushedClips.Clear();
    CurrentState().clipWasReset = true;
  }
}

void
gfxContext::UpdateSurfaceClip()
{
  if (mCairo) {
    NewPath();
    // we paint an empty rectangle to ensure the clip is propagated to
    // the destination surface
    SetDeviceColor(gfxRGBA(0,0,0,0));
    Rectangle(gfxRect(0,1,1,0));
    Fill();
  }
}

gfxRect
gfxContext::GetClipExtents()
{
  if (mCairo) {
    double xmin, ymin, xmax, ymax;
    cairo_clip_extents(mCairo, &xmin, &ymin, &xmax, &ymax);
    return gfxRect(xmin, ymin, xmax - xmin, ymax - ymin);
  } else {
    Rect rect = GetAzureDeviceSpaceClipBounds();

    if (rect.width == 0 || rect.height == 0) {
      return gfxRect(0, 0, 0, 0);
    }

    Matrix mat = mTransform;
    mat.Invert();
    rect = mat.TransformBounds(rect);

    return ThebesRect(rect);
  }
}

bool
gfxContext::ClipContainsRect(const gfxRect& aRect)
{
  if (mCairo) {
    cairo_rectangle_list_t *clip =
        cairo_copy_clip_rectangle_list(mCairo);

    bool result = false;

    if (clip->status == CAIRO_STATUS_SUCCESS) {
        for (int i = 0; i < clip->num_rectangles; i++) {
            gfxRect rect(clip->rectangles[i].x, clip->rectangles[i].y,
                         clip->rectangles[i].width, clip->rectangles[i].height);
            if (rect.Contains(aRect)) {
                result = true;
                break;
            }
        }
    }

    cairo_rectangle_list_destroy(clip);
    return result;
  } else {
    unsigned int lastReset = 0;
    for (int i = mStateStack.Length() - 2; i > 0; i--) {
      if (mStateStack[i].clipWasReset) {
        lastReset = i;
        break;
      }
    }

    // Since we always return false when the clip list contains a
    // non-rectangular clip or a non-rectilinear transform, our 'total' clip
    // is always a rectangle if we hit the end of this function.
    Rect clipBounds(0, 0, Float(mDT->GetSize().width), Float(mDT->GetSize().height));

    for (unsigned int i = lastReset; i < mStateStack.Length(); i++) {
      for (unsigned int c = 0; c < mStateStack[i].pushedClips.Length(); c++) {
        AzureState::PushedClip &clip = mStateStack[i].pushedClips[c];
        if (clip.path || !clip.transform.IsRectilinear()) {
          // Cairo behavior is we return false if the clip contains a non-
          // rectangle.
          return false;
        } else {
          Rect clipRect = mTransform.TransformBounds(clip.rect);

          clipBounds.IntersectRect(clipBounds, clipRect);
        }
      }
    }

    return clipBounds.Contains(ToRect(aRect));
  }
}

// rendering sources

void
gfxContext::SetColor(const gfxRGBA& c)
{
  if (mCairo) {
    if (gfxPlatform::GetCMSMode() == eCMSMode_All) {

        gfxRGBA cms;
        qcms_transform *transform = gfxPlatform::GetCMSRGBTransform();
        if (transform)
          gfxPlatform::TransformPixel(c, cms, transform);

        // Use the original alpha to avoid unnecessary float->byte->float
        // conversion errors
        cairo_set_source_rgba(mCairo, cms.r, cms.g, cms.b, c.a);
    }
    else
        cairo_set_source_rgba(mCairo, c.r, c.g, c.b, c.a);
  } else {
    CurrentState().pattern = nullptr;
    CurrentState().sourceSurfCairo = nullptr;
    CurrentState().sourceSurface = nullptr;

    if (gfxPlatform::GetCMSMode() == eCMSMode_All) {

        gfxRGBA cms;
        qcms_transform *transform = gfxPlatform::GetCMSRGBTransform();
        if (transform)
          gfxPlatform::TransformPixel(c, cms, transform);

        // Use the original alpha to avoid unnecessary float->byte->float
        // conversion errors
        CurrentState().color = ToColor(cms);
    }
    else
        CurrentState().color = ToColor(c);
  }
}

void
gfxContext::SetDeviceColor(const gfxRGBA& c)
{
  if (mCairo) {
    cairo_set_source_rgba(mCairo, c.r, c.g, c.b, c.a);
  } else {
    CurrentState().pattern = nullptr;
    CurrentState().sourceSurfCairo = nullptr;
    CurrentState().sourceSurface = nullptr;
    CurrentState().color = ToColor(c);
  }
}

bool
gfxContext::GetDeviceColor(gfxRGBA& c)
{
  if (mCairo) {
    return cairo_pattern_get_rgba(cairo_get_source(mCairo),
                                  &c.r,
                                  &c.g,
                                  &c.b,
                                  &c.a) == CAIRO_STATUS_SUCCESS;
  } else {
    if (CurrentState().sourceSurface) {
      return false;
    }
    if (CurrentState().pattern) {
      gfxRGBA color;
      return CurrentState().pattern->GetSolidColor(c);
    }

    c = ThebesRGBA(CurrentState().color);
    return true;
  }
}

void
gfxContext::SetSource(gfxASurface *surface, const gfxPoint& offset)
{
  if (mCairo) {
    NS_ASSERTION(surface->GetAllowUseAsSource(), "Surface not allowed to be used as source!");
    cairo_set_source_surface(mCairo, surface->CairoSurface(), offset.x, offset.y);
  } else {
    CurrentState().surfTransform = Matrix(1.0f, 0, 0, 1.0f, Float(offset.x), Float(offset.y));
    CurrentState().pattern = nullptr;
    CurrentState().patternTransformChanged = false;
    // Keep the underlying cairo surface around while we keep the
    // sourceSurface.
    CurrentState().sourceSurfCairo = surface;
    CurrentState().sourceSurface =
      gfxPlatform::GetPlatform()->GetSourceSurfaceForSurface(mDT, surface);
    CurrentState().color = Color(0, 0, 0, 0);
  }
}

void
gfxContext::SetPattern(gfxPattern *pattern)
{
  if (mCairo) {
    MOZ_ASSERT(!pattern->IsAzure());
    cairo_set_source(mCairo, pattern->CairoPattern());
  } else {
    CurrentState().sourceSurfCairo = nullptr;
    CurrentState().sourceSurface = nullptr;
    CurrentState().patternTransformChanged = false;
    CurrentState().pattern = pattern;
  }
}

already_AddRefed<gfxPattern>
gfxContext::GetPattern()
{
  if (mCairo) {
    cairo_pattern_t *pat = cairo_get_source(mCairo);
    NS_ASSERTION(pat, "I was told this couldn't be null");

    nsRefPtr<gfxPattern> wrapper;
    if (pat)
        wrapper = new gfxPattern(pat);
    else
        wrapper = new gfxPattern(gfxRGBA(0,0,0,0));

    return wrapper.forget();
  } else {
    nsRefPtr<gfxPattern> pat;
    
    AzureState &state = CurrentState();
    if (state.pattern) {
      pat = state.pattern;
    } else if (state.sourceSurface) {
      NS_ASSERTION(false, "Ugh, this isn't good.");
    } else {
      pat = new gfxPattern(ThebesRGBA(state.color));
    }
    return pat.forget();
  }
}


// masking
void
gfxContext::Mask(gfxPattern *pattern)
{
  if (mCairo) {
    MOZ_ASSERT(!pattern->IsAzure());
    cairo_mask(mCairo, pattern->CairoPattern());
  } else {
    if (pattern->Extend() == gfxPattern::EXTEND_NONE) {
      // In this situation the mask will be fully transparent (i.e. nothing
      // will be drawn) outside of the bounds of the surface. We can support
      // that by clipping out drawing to that area.
      Point offset;
      if (pattern->IsAzure()) {
        // This is an Azure pattern. i.e. this was the result of a PopGroup and
        // then the extend mode was changed to EXTEND_NONE.
        // XXX - We may need some additional magic here in theory to support
        // device offsets in these patterns, but no problems have been observed
        // yet because of this. And it would complicate things a little further.
        offset = Point(0.f, 0.f);
      } else if (pattern->GetType() == gfxPattern::PATTERN_SURFACE) {
        nsRefPtr<gfxASurface> asurf = pattern->GetSurface();
        gfxPoint deviceOffset = asurf->GetDeviceOffset();
        offset = Point(-deviceOffset.x, -deviceOffset.y);

        // this lets GetAzureSurface work
        pattern->GetPattern(mDT);
      }

      if (pattern->IsAzure() || pattern->GetType() == gfxPattern::PATTERN_SURFACE) {
        RefPtr<SourceSurface> mask = pattern->GetAzureSurface();
        Matrix mat = ToMatrix(pattern->GetInverseMatrix());
        Matrix old = mTransform;
        // add in the inverse of the pattern transform so that when we
        // MaskSurface we are transformed to the place matching the pattern transform
        mat = mat * mTransform;

        ChangeTransform(mat);
        mDT->MaskSurface(GeneralPattern(this), mask, offset, DrawOptions(1.0f, CurrentState().op, CurrentState().aaMode));
        ChangeTransform(old);
        return;
      }
    }
    mDT->Mask(GeneralPattern(this), *pattern->GetPattern(mDT), DrawOptions(1.0f, CurrentState().op, CurrentState().aaMode));
  }
}

void
gfxContext::Mask(gfxASurface *surface, const gfxPoint& offset)
{
  PROFILER_LABEL("gfxContext", "Mask",
    js::ProfileEntry::Category::GRAPHICS);

  if (mCairo) {
    cairo_mask_surface(mCairo, surface->CairoSurface(), offset.x, offset.y);
  } else {
    // Lifetime needs to be limited here as we may simply wrap surface's data.
    RefPtr<SourceSurface> sourceSurf =
      gfxPlatform::GetPlatform()->GetSourceSurfaceForSurface(mDT, surface);

    if (!sourceSurf) {
      return;
    }

    gfxPoint pt = surface->GetDeviceOffset();

    Mask(sourceSurf, Point(offset.x - pt.x, offset.y - pt.y));
  }
}

void
gfxContext::Mask(SourceSurface *surface, const Point& offset)
{
  MOZ_ASSERT(mDT);


  // We clip here to bind to the mask surface bounds, see above.
  mDT->MaskSurface(GeneralPattern(this),
            surface,
            offset,
            DrawOptions(1.0f, CurrentState().op, CurrentState().aaMode));
}

void
gfxContext::Paint(gfxFloat alpha)
{
  PROFILER_LABEL("gfxContext", "Paint",
    js::ProfileEntry::Category::GRAPHICS);

  if (mCairo) {
    cairo_paint_with_alpha(mCairo, alpha);
  } else {
    AzureState &state = CurrentState();

    if (state.sourceSurface && !state.sourceSurfCairo &&
        !state.patternTransformChanged && !state.opIsClear)
    {
      // This is the case where a PopGroupToSource has been done and this
      // paint is executed without changing the transform or the source.
      Matrix oldMat = mDT->GetTransform();

      IntSize surfSize = state.sourceSurface->GetSize();

      Matrix mat;
      mat.Translate(-state.deviceOffset.x, -state.deviceOffset.y);
      mDT->SetTransform(mat);

      mDT->DrawSurface(state.sourceSurface,
                       Rect(state.sourceSurfaceDeviceOffset, Size(surfSize.width, surfSize.height)),
                       Rect(Point(), Size(surfSize.width, surfSize.height)),
                       DrawSurfaceOptions(), DrawOptions(alpha, GetOp()));
      mDT->SetTransform(oldMat);
      return;
    }

    Matrix mat = mDT->GetTransform();
    mat.Invert();
    Rect paintRect = mat.TransformBounds(Rect(Point(0, 0), Size(mDT->GetSize())));

    if (state.opIsClear) {
      mDT->ClearRect(paintRect);
    } else {
      mDT->FillRect(paintRect, GeneralPattern(this),
                    DrawOptions(Float(alpha), GetOp()));
    }
  }
}

// groups

void
gfxContext::PushGroup(gfxContentType content)
{
  if (mCairo) {
    cairo_push_group_with_content(mCairo, (cairo_content_t)(int) content);
  } else {
    PushNewDT(content);

    PushClipsToDT(mDT);
    mDT->SetTransform(GetDTTransform());
  }
}

static gfxRect
GetRoundOutDeviceClipExtents(gfxContext* aCtx)
{
  gfxContextMatrixAutoSaveRestore save(aCtx);
  aCtx->IdentityMatrix();
  gfxRect r = aCtx->GetClipExtents();
  r.RoundOut();
  return r;
}

/**
 * Copy the contents of aSrc to aDest, translated by aTranslation.
 */
static void
CopySurface(gfxASurface* aSrc, gfxASurface* aDest, const gfxPoint& aTranslation)
{
  cairo_t *cr = cairo_create(aDest->CairoSurface());
  cairo_set_source_surface(cr, aSrc->CairoSurface(), aTranslation.x, aTranslation.y);
  cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
  cairo_paint(cr);
  cairo_destroy(cr);
}

void
gfxContext::PushGroupAndCopyBackground(gfxContentType content)
{
  if (mCairo) {
    if (content == gfxContentType::COLOR_ALPHA &&
      !(GetFlags() & FLAG_DISABLE_COPY_BACKGROUND)) {
      nsRefPtr<gfxASurface> s = CurrentSurface();
      if ((s->GetAllowUseAsSource() || s->GetType() == gfxSurfaceType::Tee) &&
          (s->GetContentType() == gfxContentType::COLOR ||
              s->GetOpaqueRect().Contains(GetRoundOutDeviceClipExtents(this)))) {
        cairo_push_group_with_content(mCairo, CAIRO_CONTENT_COLOR);
        nsRefPtr<gfxASurface> d = CurrentSurface();

        if (d->GetType() == gfxSurfaceType::Tee) {
          NS_ASSERTION(s->GetType() == gfxSurfaceType::Tee, "Mismatched types");
          nsAutoTArray<nsRefPtr<gfxASurface>,2> ss;
          nsAutoTArray<nsRefPtr<gfxASurface>,2> ds;
          static_cast<gfxTeeSurface*>(s.get())->GetSurfaces(&ss);
          static_cast<gfxTeeSurface*>(d.get())->GetSurfaces(&ds);
          NS_ASSERTION(ss.Length() == ds.Length(), "Mismatched lengths");
          gfxPoint translation = d->GetDeviceOffset() - s->GetDeviceOffset();
          for (uint32_t i = 0; i < ss.Length(); ++i) {
              CopySurface(ss[i], ds[i], translation);
          }
        } else {
          CopySurface(s, d, gfxPoint(0, 0));
        }
        d->SetOpaqueRect(s->GetOpaqueRect());
        return;
      }
    }
  } else {
    IntRect clipExtents;
    if (mDT->GetFormat() != SurfaceFormat::B8G8R8X8) {
      gfxRect clipRect = GetRoundOutDeviceClipExtents(this);
      clipExtents = IntRect(clipRect.x, clipRect.y, clipRect.width, clipRect.height);
    }
    if ((mDT->GetFormat() == SurfaceFormat::B8G8R8X8 ||
         mDT->GetOpaqueRect().Contains(clipExtents)) &&
        !mDT->GetUserData(&sDontUseAsSourceKey)) {
      DrawTarget *oldDT = mDT;
      RefPtr<SourceSurface> source = mDT->Snapshot();
      Point oldDeviceOffset = CurrentState().deviceOffset;

      PushNewDT(gfxContentType::COLOR);

      Point offset = CurrentState().deviceOffset - oldDeviceOffset;
      Rect surfRect(0, 0, Float(mDT->GetSize().width), Float(mDT->GetSize().height));
      Rect sourceRect = surfRect;
      sourceRect.x += offset.x;
      sourceRect.y += offset.y;

      mDT->SetTransform(Matrix());
      mDT->DrawSurface(source, surfRect, sourceRect);
      mDT->SetOpaqueRect(oldDT->GetOpaqueRect());

      PushClipsToDT(mDT);
      mDT->SetTransform(GetDTTransform());
      return;
    }
  }
  PushGroup(content);
}

already_AddRefed<gfxPattern>
gfxContext::PopGroup()
{
  if (mCairo) {
    cairo_pattern_t *pat = cairo_pop_group(mCairo);
    nsRefPtr<gfxPattern> wrapper = new gfxPattern(pat);
    cairo_pattern_destroy(pat);
    return wrapper.forget();
  } else {
    RefPtr<SourceSurface> src = mDT->Snapshot();
    Point deviceOffset = CurrentState().deviceOffset;

    Restore();

    Matrix mat = mTransform;
    mat.Invert();

    Matrix deviceOffsetTranslation;
    deviceOffsetTranslation.Translate(deviceOffset.x, deviceOffset.y);

    nsRefPtr<gfxPattern> pat = new gfxPattern(src, deviceOffsetTranslation * mat);

    return pat.forget();
  }
}

void
gfxContext::PopGroupToSource()
{
  if (mCairo) {
    cairo_pop_group_to_source(mCairo);
  } else {
    RefPtr<SourceSurface> src = mDT->Snapshot();
    Point deviceOffset = CurrentState().deviceOffset;
    Restore();
    CurrentState().sourceSurfCairo = nullptr;
    CurrentState().sourceSurface = src;
    CurrentState().sourceSurfaceDeviceOffset = deviceOffset;
    CurrentState().pattern = nullptr;
    CurrentState().patternTransformChanged = false;

    Matrix mat = mTransform;
    mat.Invert();

    Matrix deviceOffsetTranslation;
    deviceOffsetTranslation.Translate(deviceOffset.x, deviceOffset.y);
    CurrentState().surfTransform = deviceOffsetTranslation * mat;
  }
}

bool
gfxContext::PointInFill(const gfxPoint& pt)
{
  if (mCairo) {
    return cairo_in_fill(mCairo, pt.x, pt.y);
  } else {
    EnsurePath();
    return mPath->ContainsPoint(ToPoint(pt), Matrix());
  }
}

bool
gfxContext::PointInStroke(const gfxPoint& pt)
{
  if (mCairo) {
    return cairo_in_stroke(mCairo, pt.x, pt.y);
  } else {
    EnsurePath();
    return mPath->StrokeContainsPoint(CurrentState().strokeOptions,
                                      ToPoint(pt),
                                      Matrix());
  }
}

gfxRect
gfxContext::GetUserPathExtent()
{
  if (mCairo) {
    double xmin, ymin, xmax, ymax;
    cairo_path_extents(mCairo, &xmin, &ymin, &xmax, &ymax);
    return gfxRect(xmin, ymin, xmax - xmin, ymax - ymin);
  } else {
    EnsurePath();
    return ThebesRect(mPath->GetBounds());
  }
}

gfxRect
gfxContext::GetUserFillExtent()
{
  if (mCairo) {
    double xmin, ymin, xmax, ymax;
    cairo_fill_extents(mCairo, &xmin, &ymin, &xmax, &ymax);
    return gfxRect(xmin, ymin, xmax - xmin, ymax - ymin);
  } else {
    EnsurePath();
    return ThebesRect(mPath->GetBounds());
  }
}

gfxRect
gfxContext::GetUserStrokeExtent()
{
  if (mCairo) {
    double xmin, ymin, xmax, ymax;
    cairo_stroke_extents(mCairo, &xmin, &ymin, &xmax, &ymax);
    return gfxRect(xmin, ymin, xmax - xmin, ymax - ymin);
  } else {
    EnsurePath();
    return ThebesRect(mPath->GetStrokedBounds(CurrentState().strokeOptions, mTransform));
  }
}

bool
gfxContext::HasError()
{
  if (mCairo) {
    return cairo_status(mCairo) != CAIRO_STATUS_SUCCESS;
  } else {
    // As far as this is concerned, an Azure context is never in error.
    return false;
  }
}

void
gfxContext::RoundedRectangle(const gfxRect& rect,
                             const gfxCornerSizes& corners,
                             bool draw_clockwise)
{
    //
    // For CW drawing, this looks like:
    //
    //  ...******0**      1    C
    //              ****
    //                  ***    2
    //                     **
    //                       *
    //                        *
    //                         3
    //                         *
    //                         *
    //
    // Where 0, 1, 2, 3 are the control points of the Bezier curve for
    // the corner, and C is the actual corner point.
    //
    // At the start of the loop, the current point is assumed to be
    // the point adjacent to the top left corner on the top
    // horizontal.  Note that corner indices start at the top left and
    // continue clockwise, whereas in our loop i = 0 refers to the top
    // right corner.
    //
    // When going CCW, the control points are swapped, and the first
    // corner that's drawn is the top left (along with the top segment).
    //
    // There is considerable latitude in how one chooses the four
    // control points for a Bezier curve approximation to an ellipse.
    // For the overall path to be continuous and show no corner at the
    // endpoints of the arc, points 0 and 3 must be at the ends of the
    // straight segments of the rectangle; points 0, 1, and C must be
    // collinear; and points 3, 2, and C must also be collinear.  This
    // leaves only two free parameters: the ratio of the line segments
    // 01 and 0C, and the ratio of the line segments 32 and 3C.  See
    // the following papers for extensive discussion of how to choose
    // these ratios:
    //
    //   Dokken, Tor, et al. "Good approximation of circles by
    //      curvature-continuous Bezier curves."  Computer-Aided
    //      Geometric Design 7(1990) 33--41.
    //   Goldapp, Michael. "Approximation of circular arcs by cubic
    //      polynomials." Computer-Aided Geometric Design 8(1991) 227--238.
    //   Maisonobe, Luc. "Drawing an elliptical arc using polylines,
    //      quadratic, or cubic Bezier curves."
    //      http://www.spaceroots.org/documents/ellipse/elliptical-arc.pdf
    //
    // We follow the approach in section 2 of Goldapp (least-error,
    // Hermite-type approximation) and make both ratios equal to
    //
    //          2   2 + n - sqrt(2n + 28)
    //  alpha = - * ---------------------
    //          3           n - 4
    //
    // where n = 3( cbrt(sqrt(2)+1) - cbrt(sqrt(2)-1) ).
    //
    // This is the result of Goldapp's equation (10b) when the angle
    // swept out by the arc is pi/2, and the parameter "a-bar" is the
    // expression given immediately below equation (21).
    //
    // Using this value, the maximum radial error for a circle, as a
    // fraction of the radius, is on the order of 0.2 x 10^-3.
    // Neither Dokken nor Goldapp discusses error for a general
    // ellipse; Maisonobe does, but his choice of control points
    // follows different constraints, and Goldapp's expression for
    // 'alpha' gives much smaller radial error, even for very flat
    // ellipses, than Maisonobe's equivalent.
    //
    // For the various corners and for each axis, the sign of this
    // constant changes, or it might be 0 -- it's multiplied by the
    // appropriate multiplier from the list before using.

  if (mCairo) {
    const gfxFloat alpha = 0.55191497064665766025;

    typedef struct { gfxFloat a, b; } twoFloats;

    twoFloats cwCornerMults[4] = { { -1,  0 },
                                   {  0, -1 },
                                   { +1,  0 },
                                   {  0, +1 } };
    twoFloats ccwCornerMults[4] = { { +1,  0 },
                                    {  0, -1 },
                                    { -1,  0 },
                                    {  0, +1 } };

    twoFloats *cornerMults = draw_clockwise ? cwCornerMults : ccwCornerMults;

    gfxPoint pc, p0, p1, p2, p3;

    if (draw_clockwise)
        cairo_move_to(mCairo, rect.X() + corners[NS_CORNER_TOP_LEFT].width, rect.Y());
    else
        cairo_move_to(mCairo, rect.X() + rect.Width() - corners[NS_CORNER_TOP_RIGHT].width, rect.Y());

    NS_FOR_CSS_CORNERS(i) {
        // the corner index -- either 1 2 3 0 (cw) or 0 3 2 1 (ccw)
        mozilla::css::Corner c = mozilla::css::Corner(draw_clockwise ? ((i+1) % 4) : ((4-i) % 4));

        // i+2 and i+3 respectively.  These are used to index into the corner
        // multiplier table, and were deduced by calculating out the long form
        // of each corner and finding a pattern in the signs and values.
        int i2 = (i+2) % 4;
        int i3 = (i+3) % 4;

        pc = rect.AtCorner(c);

        if (corners[c].width > 0.0 && corners[c].height > 0.0) {
            p0.x = pc.x + cornerMults[i].a * corners[c].width;
            p0.y = pc.y + cornerMults[i].b * corners[c].height;

            p3.x = pc.x + cornerMults[i3].a * corners[c].width;
            p3.y = pc.y + cornerMults[i3].b * corners[c].height;

            p1.x = p0.x + alpha * cornerMults[i2].a * corners[c].width;
            p1.y = p0.y + alpha * cornerMults[i2].b * corners[c].height;

            p2.x = p3.x - alpha * cornerMults[i3].a * corners[c].width;
            p2.y = p3.y - alpha * cornerMults[i3].b * corners[c].height;

            cairo_line_to (mCairo, p0.x, p0.y);
            cairo_curve_to (mCairo,
                            p1.x, p1.y,
                            p2.x, p2.y,
                            p3.x, p3.y);
        } else {
            cairo_line_to (mCairo, pc.x, pc.y);
        }
    }

    cairo_close_path (mCairo);
  } else {
    EnsurePathBuilder();
    Size radii[] = { ToSize(corners[NS_CORNER_TOP_LEFT]),
                     ToSize(corners[NS_CORNER_TOP_RIGHT]),
                     ToSize(corners[NS_CORNER_BOTTOM_RIGHT]),
                     ToSize(corners[NS_CORNER_BOTTOM_LEFT]) };
    AppendRoundedRectToPath(mPathBuilder, ToRect(rect), radii, draw_clockwise);
  }
}

#ifdef MOZ_DUMP_PAINTING
void
gfxContext::WriteAsPNG(const char* aFile)
{ 
  nsRefPtr<gfxASurface> surf = CurrentSurface();
  if (surf) {
    surf->WriteAsPNG(aFile);
  } else {
    NS_WARNING("No surface found!");
  }
}

void 
gfxContext::DumpAsDataURL()
{ 
  nsRefPtr<gfxASurface> surf = CurrentSurface();
  if (surf) {
    surf->DumpAsDataURL();
  } else {
    NS_WARNING("No surface found!");
  }
}

void 
gfxContext::CopyAsDataURL()
{ 
  nsRefPtr<gfxASurface> surf = CurrentSurface();
  if (surf) {
    surf->CopyAsDataURL();
  } else {
    NS_WARNING("No surface found!");
  }
}
#endif

void
gfxContext::EnsurePath()
{
  if (mPathBuilder) {
    mPath = mPathBuilder->Finish();
    mPathBuilder = nullptr;
  }

  if (mPath) {
    if (mTransformChanged) {
      Matrix mat = mTransform;
      mat.Invert();
      mat = mPathTransform * mat;
      mPathBuilder = mPath->TransformedCopyToBuilder(mat, CurrentState().fillRule);
      mPath = mPathBuilder->Finish();
      mPathBuilder = nullptr;

      mTransformChanged = false;
    }

    if (CurrentState().fillRule == mPath->GetFillRule()) {
      return;
    }

    mPathBuilder = mPath->CopyToBuilder(CurrentState().fillRule);

    mPath = mPathBuilder->Finish();
    mPathBuilder = nullptr;
    return;
  }

  EnsurePathBuilder();
  mPath = mPathBuilder->Finish();
  mPathBuilder = nullptr;
}

void
gfxContext::EnsurePathBuilder()
{
  if (mPathBuilder && !mTransformChanged) {
    return;
  }

  if (mPath) {
    if (!mTransformChanged) {
      mPathBuilder = mPath->CopyToBuilder(CurrentState().fillRule);
      mPath = nullptr;
    } else {
      Matrix invTransform = mTransform;
      invTransform.Invert();
      Matrix toNewUS = mPathTransform * invTransform;
      mPathBuilder = mPath->TransformedCopyToBuilder(toNewUS, CurrentState().fillRule);
    }
    return;
  }

  DebugOnly<PathBuilder*> oldPath = mPathBuilder.get();

  if (!mPathBuilder) {
    mPathBuilder = mDT->CreatePathBuilder(CurrentState().fillRule);

    if (mPathIsRect) {
      mPathBuilder->MoveTo(mRect.TopLeft());
      mPathBuilder->LineTo(mRect.TopRight());
      mPathBuilder->LineTo(mRect.BottomRight());
      mPathBuilder->LineTo(mRect.BottomLeft());
      mPathBuilder->Close();
    }
  }

  if (mTransformChanged) {
    // This could be an else if since this should never happen when
    // mPathBuilder is nullptr and mPath is nullptr. But this way we can
    // assert if all the state is as expected.
    MOZ_ASSERT(oldPath);
    MOZ_ASSERT(!mPathIsRect);

    Matrix invTransform = mTransform;
    invTransform.Invert();
    Matrix toNewUS = mPathTransform * invTransform;

    RefPtr<Path> path = mPathBuilder->Finish();
    mPathBuilder = path->TransformedCopyToBuilder(toNewUS, CurrentState().fillRule);
  }

  mPathIsRect = false;
}

void
gfxContext::FillAzure(Float aOpacity)
{
  AzureState &state = CurrentState();

  CompositionOp op = GetOp();

  if (mPathIsRect) {
    MOZ_ASSERT(!mTransformChanged);

    if (state.opIsClear) {
      mDT->ClearRect(mRect);
    } else if (op == CompositionOp::OP_SOURCE) {
      // Emulate cairo operator source which is bound by mask!
      mDT->ClearRect(mRect);
      mDT->FillRect(mRect, GeneralPattern(this), DrawOptions(aOpacity));
    } else {
      mDT->FillRect(mRect, GeneralPattern(this), DrawOptions(aOpacity, op, state.aaMode));
    }
  } else {
    EnsurePath();

    NS_ASSERTION(!state.opIsClear, "We shouldn't be clearing complex paths!");

    mDT->Fill(mPath, GeneralPattern(this), DrawOptions(aOpacity, op, state.aaMode));
  }
}

void
gfxContext::PushClipsToDT(DrawTarget *aDT)
{
  // Tricky, we have to restore all clips -since the last time- the clip
  // was reset. If we didn't reset the clip, just popping the clips we
  // added was fine.
  unsigned int lastReset = 0;
  for (int i = mStateStack.Length() - 2; i > 0; i--) {
    if (mStateStack[i].clipWasReset) {
      lastReset = i;
      break;
    }
  }

  // Don't need to save the old transform, we'll be setting a new one soon!

  // Push all clips from the last state on the stack where the clip was
  // reset to the clip before ours.
  for (unsigned int i = lastReset; i < mStateStack.Length() - 1; i++) {
    for (unsigned int c = 0; c < mStateStack[i].pushedClips.Length(); c++) {
      aDT->SetTransform(mStateStack[i].pushedClips[c].transform * GetDeviceTransform());
      if (mStateStack[i].pushedClips[c].path) {
        aDT->PushClip(mStateStack[i].pushedClips[c].path);
      } else {
        aDT->PushClipRect(mStateStack[i].pushedClips[c].rect);
      }
    }
  }
}

CompositionOp
gfxContext::GetOp()
{
  if (CurrentState().op != CompositionOp::OP_SOURCE) {
    return CurrentState().op;
  }

  AzureState &state = CurrentState();
  if (state.pattern) {
    if (state.pattern->IsOpaque()) {
      return CompositionOp::OP_OVER;
    } else {
      return CompositionOp::OP_SOURCE;
    }
  } else if (state.sourceSurface) {
    if (state.sourceSurface->GetFormat() == SurfaceFormat::B8G8R8X8) {
      return CompositionOp::OP_OVER;
    } else {
      return CompositionOp::OP_SOURCE;
    }
  } else {
    if (state.color.a > 0.999) {
      return CompositionOp::OP_OVER;
    } else {
      return CompositionOp::OP_SOURCE;
    }
  }
}

/* SVG font code can change the transform after having set the pattern on the
 * context. When the pattern is set it is in user space, if the transform is
 * changed after doing so the pattern needs to be converted back into userspace.
 * We just store the old pattern transform here so that we only do the work
 * needed here if the pattern is actually used.
 * We need to avoid doing this when this ChangeTransform comes from a restore,
 * since the current pattern and the current transform are both part of the
 * state we know the new CurrentState()'s values are valid. But if we assume
 * a change they might become invalid since patternTransformChanged is part of
 * the state and might be false for the restored AzureState.
 */
void
gfxContext::ChangeTransform(const Matrix &aNewMatrix, bool aUpdatePatternTransform)
{
  AzureState &state = CurrentState();

  if (aUpdatePatternTransform && (state.pattern || state.sourceSurface)
      && !state.patternTransformChanged) {
    state.patternTransform = GetDTTransform();
    state.patternTransformChanged = true;
  }

  if (mPathIsRect) {
    Matrix invMatrix = aNewMatrix;
    
    invMatrix.Invert();

    Matrix toNewUS = mTransform * invMatrix;

    if (toNewUS.IsRectilinear()) {
      mRect = toNewUS.TransformBounds(mRect);
      mRect.NudgeToIntegers();
    } else {
      mPathBuilder = mDT->CreatePathBuilder(CurrentState().fillRule);
      
      mPathBuilder->MoveTo(toNewUS * mRect.TopLeft());
      mPathBuilder->LineTo(toNewUS * mRect.TopRight());
      mPathBuilder->LineTo(toNewUS * mRect.BottomRight());
      mPathBuilder->LineTo(toNewUS * mRect.BottomLeft());
      mPathBuilder->Close();

      mPathIsRect = false;
    }

    // No need to consider the transform changed now!
    mTransformChanged = false;
  } else if ((mPath || mPathBuilder) && !mTransformChanged) {
    mTransformChanged = true;
    mPathTransform = mTransform;
  }

  mTransform = aNewMatrix;

  mDT->SetTransform(GetDTTransform());
}

Rect
gfxContext::GetAzureDeviceSpaceClipBounds()
{
  unsigned int lastReset = 0;
  for (int i = mStateStack.Length() - 1; i > 0; i--) {
    if (mStateStack[i].clipWasReset) {
      lastReset = i;
      break;
    }
  }

  Rect rect(CurrentState().deviceOffset.x, CurrentState().deviceOffset.y,
            Float(mDT->GetSize().width), Float(mDT->GetSize().height));
  for (unsigned int i = lastReset; i < mStateStack.Length(); i++) {
    for (unsigned int c = 0; c < mStateStack[i].pushedClips.Length(); c++) {
      AzureState::PushedClip &clip = mStateStack[i].pushedClips[c];
      if (clip.path) {
        Rect bounds = clip.path->GetBounds(clip.transform);
        rect.IntersectRect(rect, bounds);
      } else {
        rect.IntersectRect(rect, clip.transform.TransformBounds(clip.rect));
      }
    }
  }

  return rect;
}

Point
gfxContext::GetDeviceOffset() const
{
  return CurrentState().deviceOffset;
}

Matrix
gfxContext::GetDeviceTransform() const
{
  Matrix mat;
  mat.Translate(-CurrentState().deviceOffset.x, -CurrentState().deviceOffset.y);
  return mat;
}

Matrix
gfxContext::GetDTTransform() const
{
  Matrix mat = mTransform;
  mat._31 -= CurrentState().deviceOffset.x;
  mat._32 -= CurrentState().deviceOffset.y;
  return mat;
}

void
gfxContext::PushNewDT(gfxContentType content)
{
  Rect clipBounds = GetAzureDeviceSpaceClipBounds();
  clipBounds.RoundOut();

  clipBounds.width = std::max(1.0f, clipBounds.width);
  clipBounds.height = std::max(1.0f, clipBounds.height);

  SurfaceFormat format = gfxPlatform::GetPlatform()->Optimal2DFormatForContent(content);

  RefPtr<DrawTarget> newDT =
    mDT->CreateSimilarDrawTarget(IntSize(int32_t(clipBounds.width), int32_t(clipBounds.height)),
                                 format);

  if (!newDT) {
    NS_WARNING("Failed to create DrawTarget of sufficient size.");
    newDT = mDT->CreateSimilarDrawTarget(IntSize(64, 64), format);

    if (!newDT) {
      // If even this fails.. we're most likely just out of memory!
      NS_ABORT_OOM(BytesPerPixel(format) * 64 * 64);
    }
  }

  Save();

  CurrentState().drawTarget = newDT;
  CurrentState().deviceOffset = clipBounds.TopLeft();

  mDT = newDT;
}

/**
 * Work out whether cairo will snap inter-glyph spacing to pixels.
 *
 * Layout does not align text to pixel boundaries, so, with font drawing
 * backends that snap glyph positions to pixels, it is important that
 * inter-glyph spacing within words is always an integer number of pixels.
 * This ensures that the drawing backend snaps all of the word's glyphs in the
 * same direction and so inter-glyph spacing remains the same.
 */
void
gfxContext::GetRoundOffsetsToPixels(bool *aRoundX, bool *aRoundY)
{
    *aRoundX = false;
    // Could do something fancy here for ScaleFactors of
    // AxisAlignedTransforms, but we leave things simple.
    // Not much point rounding if a matrix will mess things up anyway.
    // Also return false for non-cairo contexts.
    if (CurrentMatrix().HasNonTranslation()) {
        *aRoundY = false;
        return;
    }

    // All raster backends snap glyphs to pixels vertically.
    // Print backends set CAIRO_HINT_METRICS_OFF.
    *aRoundY = true;

    cairo_t *cr = GetCairo();
    cairo_scaled_font_t *scaled_font = cairo_get_scaled_font(cr);
    // Sometimes hint metrics gets set for us, most notably for printing.
    cairo_font_options_t *font_options = cairo_font_options_create();
    cairo_scaled_font_get_font_options(scaled_font, font_options);
    cairo_hint_metrics_t hint_metrics =
        cairo_font_options_get_hint_metrics(font_options);
    cairo_font_options_destroy(font_options);

    switch (hint_metrics) {
    case CAIRO_HINT_METRICS_OFF:
        *aRoundY = false;
        return;
    case CAIRO_HINT_METRICS_DEFAULT:
        // Here we mimic what cairo surface/font backends do.  Printing
        // surfaces have already been handled by hint_metrics.  The
        // fallback show_glyphs implementation composites pixel-aligned
        // glyph surfaces, so we just pick surface/font combinations that
        // override this.
        switch (cairo_scaled_font_get_type(scaled_font)) {
#if CAIRO_HAS_DWRITE_FONT // dwrite backend is not in std cairo releases yet
        case CAIRO_FONT_TYPE_DWRITE:
            // show_glyphs is implemented on the font and so is used for
            // all surface types; however, it may pixel-snap depending on
            // the dwrite rendering mode
            if (!cairo_dwrite_scaled_font_get_force_GDI_classic(scaled_font) &&
                gfxWindowsPlatform::GetPlatform()->DWriteMeasuringMode() ==
                    DWRITE_MEASURING_MODE_NATURAL) {
                return;
            }
#endif
        case CAIRO_FONT_TYPE_QUARTZ:
            // Quartz surfaces implement show_glyphs for Quartz fonts
            if (cairo_surface_get_type(cairo_get_target(cr)) ==
                CAIRO_SURFACE_TYPE_QUARTZ) {
                return;
            }
        default:
            break;
        }
        // fall through:
    case CAIRO_HINT_METRICS_ON:
        break;
    }
    *aRoundX = true;
    return;
}