1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579
|
/*
* Copyright (C) 2009, 2010, 2011 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "RenderLayerBacking.h"
#include "AnimationController.h"
#include "CanvasRenderingContext.h"
#include "CSSPropertyNames.h"
#include "CachedImage.h"
#include "Chrome.h"
#include "FilterEffectRenderer.h"
#include "FontCache.h"
#include "FrameView.h"
#include "GraphicsContext.h"
#include "GraphicsLayer.h"
#include "HTMLCanvasElement.h"
#include "HTMLIFrameElement.h"
#include "HTMLMediaElement.h"
#include "HTMLNames.h"
#include "HTMLPlugInElement.h"
#include "InspectorInstrumentation.h"
#include "KeyframeList.h"
#include "MainFrame.h"
#include "PluginViewBase.h"
#include "ProgressTracker.h"
#include "RenderFlowThread.h"
#include "RenderIFrame.h"
#include "RenderImage.h"
#include "RenderLayerCompositor.h"
#include "RenderEmbeddedObject.h"
#include "RenderNamedFlowFragment.h"
#include "RenderRegion.h"
#include "RenderVideo.h"
#include "RenderView.h"
#include "ScrollingCoordinator.h"
#include "Settings.h"
#include "StyleResolver.h"
#include "TiledBacking.h"
#include <wtf/text/StringBuilder.h>
#if ENABLE(WEBGL) || ENABLE(ACCELERATED_2D_CANVAS)
#include "GraphicsContext3D.h"
#endif
namespace WebCore {
using namespace HTMLNames;
CanvasCompositingStrategy canvasCompositingStrategy(const RenderObject& renderer)
{
ASSERT(renderer.isCanvas());
const HTMLCanvasElement* canvas = toHTMLCanvasElement(renderer.node());
CanvasRenderingContext* context = canvas->renderingContext();
if (!context || !context->isAccelerated())
return UnacceleratedCanvas;
if (context->is3d())
return CanvasAsLayerContents;
#if ENABLE(ACCELERATED_2D_CANVAS)
return CanvasAsLayerContents;
#else
return CanvasPaintedToLayer; // On Mac and iOS we paint accelerated canvases into their layers.
#endif
}
// Get the scrolling coordinator in a way that works inside RenderLayerBacking's destructor.
static ScrollingCoordinator* scrollingCoordinatorFromLayer(RenderLayer& layer)
{
Page* page = layer.renderer().frame().page();
if (!page)
return 0;
return page->scrollingCoordinator();
}
bool RenderLayerBacking::m_creatingPrimaryGraphicsLayer = false;
RenderLayerBacking::RenderLayerBacking(RenderLayer& layer)
: m_owningLayer(layer)
, m_viewportConstrainedNodeID(0)
, m_scrollingNodeID(0)
, m_artificiallyInflatedBounds(false)
, m_isMainFrameRenderViewLayer(false)
, m_usingTiledCacheLayer(false)
, m_requiresOwnBackingStore(true)
, m_canCompositeFilters(false)
, m_backgroundLayerPaintsFixedRootBackground(false)
{
Page* page = renderer().frame().page();
if (layer.isRootLayer() && page) {
m_isMainFrameRenderViewLayer = renderer().frame().isMainFrame();
m_usingTiledCacheLayer = page->chrome().client().shouldUseTiledBackingForFrameView(renderer().frame().view());
}
createPrimaryGraphicsLayer();
if (m_usingTiledCacheLayer && page) {
TiledBacking* tiledBacking = this->tiledBacking();
tiledBacking->setIsInWindow(page->isInWindow());
if (m_isMainFrameRenderViewLayer)
tiledBacking->setUnparentsOffscreenTiles(true);
tiledBacking->setScrollingPerformanceLoggingEnabled(page->settings().scrollingPerformanceLoggingEnabled());
adjustTiledBackingCoverage();
}
}
RenderLayerBacking::~RenderLayerBacking()
{
updateAncestorClippingLayer(false);
updateDescendantClippingLayer(false);
updateOverflowControlsLayers(false, false, false);
updateForegroundLayer(false);
updateBackgroundLayer(false);
updateMaskLayer(false);
updateScrollingLayers(false);
detachFromScrollingCoordinator();
destroyGraphicsLayers();
}
void RenderLayerBacking::willDestroyLayer(const GraphicsLayer* layer)
{
if (layer && layer->usingTiledBacking())
compositor().layerTiledBackingUsageChanged(layer, false);
}
std::unique_ptr<GraphicsLayer> RenderLayerBacking::createGraphicsLayer(const String& name)
{
GraphicsLayerFactory* graphicsLayerFactory = 0;
if (Page* page = renderer().frame().page())
graphicsLayerFactory = page->chrome().client().graphicsLayerFactory();
std::unique_ptr<GraphicsLayer> graphicsLayer = GraphicsLayer::create(graphicsLayerFactory, *this);
#ifndef NDEBUG
graphicsLayer->setName(name);
#else
UNUSED_PARAM(name);
#endif
#if PLATFORM(COCOA) && USE(CA)
graphicsLayer->setAcceleratesDrawing(compositor().acceleratedDrawingEnabled());
#endif
return graphicsLayer;
}
bool RenderLayerBacking::shouldUseTiledBacking(const GraphicsLayer*) const
{
return m_usingTiledCacheLayer && m_creatingPrimaryGraphicsLayer;
}
void RenderLayerBacking::tiledBackingUsageChanged(const GraphicsLayer* layer, bool usingTiledBacking)
{
compositor().layerTiledBackingUsageChanged(layer, usingTiledBacking);
}
TiledBacking* RenderLayerBacking::tiledBacking() const
{
return m_graphicsLayer->tiledBacking();
}
static TiledBacking::TileCoverage computeTileCoverage(RenderLayerBacking* backing)
{
// FIXME: When we use TiledBacking for overflow, this should look at RenderView scrollability.
FrameView& frameView = backing->owningLayer().renderer().view().frameView();
TiledBacking::TileCoverage tileCoverage = TiledBacking::CoverageForVisibleArea;
bool useMinimalTilesDuringLiveResize = frameView.inLiveResize();
if (frameView.speculativeTilingEnabled() && !useMinimalTilesDuringLiveResize) {
bool clipsToExposedRect = !frameView.exposedRect().isInfinite();
if (frameView.horizontalScrollbarMode() != ScrollbarAlwaysOff || clipsToExposedRect)
tileCoverage |= TiledBacking::CoverageForHorizontalScrolling;
if (frameView.verticalScrollbarMode() != ScrollbarAlwaysOff || clipsToExposedRect)
tileCoverage |= TiledBacking::CoverageForVerticalScrolling;
}
return tileCoverage;
}
void RenderLayerBacking::adjustTiledBackingCoverage()
{
if (!m_usingTiledCacheLayer)
return;
TiledBacking::TileCoverage tileCoverage = computeTileCoverage(this);
tiledBacking()->setTileCoverage(tileCoverage);
}
void RenderLayerBacking::setTiledBackingHasMargins(bool hasExtendedBackgroundOnLeftAndRight, bool hasExtendedBackgroundOnTopAndBottom)
{
if (!m_usingTiledCacheLayer)
return;
int marginLeftAndRightSize = hasExtendedBackgroundOnLeftAndRight ? defaultTileWidth : 0;
int marginTopAndBottomSize = hasExtendedBackgroundOnTopAndBottom ? defaultTileHeight : 0;
tiledBacking()->setTileMargins(marginTopAndBottomSize, marginTopAndBottomSize, marginLeftAndRightSize, marginLeftAndRightSize);
}
void RenderLayerBacking::updateDebugIndicators(bool showBorder, bool showRepaintCounter)
{
m_graphicsLayer->setShowDebugBorder(showBorder);
m_graphicsLayer->setShowRepaintCounter(showRepaintCounter);
if (m_ancestorClippingLayer)
m_ancestorClippingLayer->setShowDebugBorder(showBorder);
if (m_foregroundLayer) {
m_foregroundLayer->setShowDebugBorder(showBorder);
m_foregroundLayer->setShowRepaintCounter(showRepaintCounter);
}
if (m_contentsContainmentLayer)
m_contentsContainmentLayer->setShowDebugBorder(showBorder);
if (m_backgroundLayer) {
m_backgroundLayer->setShowDebugBorder(showBorder);
m_backgroundLayer->setShowRepaintCounter(showRepaintCounter);
}
if (m_maskLayer) {
m_maskLayer->setShowDebugBorder(showBorder);
m_maskLayer->setShowRepaintCounter(showRepaintCounter);
}
if (m_layerForHorizontalScrollbar)
m_layerForHorizontalScrollbar->setShowDebugBorder(showBorder);
if (m_layerForVerticalScrollbar)
m_layerForVerticalScrollbar->setShowDebugBorder(showBorder);
if (m_layerForScrollCorner)
m_layerForScrollCorner->setShowDebugBorder(showBorder);
if (m_scrollingLayer)
m_scrollingLayer->setShowDebugBorder(showBorder);
if (m_scrollingContentsLayer) {
m_scrollingContentsLayer->setShowDebugBorder(showBorder);
m_scrollingContentsLayer->setShowRepaintCounter(showRepaintCounter);
}
}
void RenderLayerBacking::createPrimaryGraphicsLayer()
{
String layerName;
#ifndef NDEBUG
layerName = m_owningLayer.name();
#endif
// The call to createGraphicsLayer ends calling back into here as
// a GraphicsLayerClient to ask if it shouldUseTiledBacking(). We only want
// the tile cache on our main layer. This is pretty ugly, but saves us from
// exposing the API to all clients.
m_creatingPrimaryGraphicsLayer = true;
m_graphicsLayer = createGraphicsLayer(layerName);
m_creatingPrimaryGraphicsLayer = false;
if (m_usingTiledCacheLayer) {
m_childContainmentLayer = createGraphicsLayer("TiledBacking Flattening Layer");
m_graphicsLayer->addChild(m_childContainmentLayer.get());
}
#if !PLATFORM(IOS)
if (m_isMainFrameRenderViewLayer) {
// Page scale is applied above the RenderView on iOS.
m_graphicsLayer->setContentsOpaque(true);
m_graphicsLayer->setAppliesPageScale();
}
#endif
#if PLATFORM(COCOA) && USE(CA)
if (!compositor().acceleratedDrawingEnabled() && renderer().isCanvas()) {
const HTMLCanvasElement* canvas = toHTMLCanvasElement(renderer().element());
if (canvas->shouldAccelerate(canvas->size()))
m_graphicsLayer->setAcceleratesDrawing(true);
}
#endif
updateOpacity(renderer().style());
updateTransform(renderer().style());
updateFilters(renderer().style());
#if ENABLE(CSS_COMPOSITING)
updateBlendMode(renderer().style());
#endif
}
#if PLATFORM(IOS)
void RenderLayerBacking::layerWillBeDestroyed()
{
RenderObject& renderer = this->renderer();
if (renderer.isEmbeddedObject() && toRenderEmbeddedObject(renderer).allowsAcceleratedCompositing()) {
PluginViewBase* pluginViewBase = toPluginViewBase(toRenderWidget(renderer).widget());
if (pluginViewBase && m_graphicsLayer->contentsLayerForMedia())
pluginViewBase->detachPluginLayer();
}
}
#endif
void RenderLayerBacking::destroyGraphicsLayers()
{
if (m_graphicsLayer) {
willDestroyLayer(m_graphicsLayer.get());
m_graphicsLayer->removeFromParent();
}
m_ancestorClippingLayer = nullptr;
m_contentsContainmentLayer = nullptr;
m_graphicsLayer = nullptr;
m_foregroundLayer = nullptr;
m_backgroundLayer = nullptr;
m_childContainmentLayer = nullptr;
m_maskLayer = nullptr;
m_scrollingLayer = nullptr;
m_scrollingContentsLayer = nullptr;
}
void RenderLayerBacking::updateOpacity(const RenderStyle& style)
{
m_graphicsLayer->setOpacity(compositingOpacity(style.opacity()));
}
void RenderLayerBacking::updateTransform(const RenderStyle& style)
{
// FIXME: This could use m_owningLayer.transform(), but that currently has transform-origin
// baked into it, and we don't want that.
TransformationMatrix t;
if (m_owningLayer.hasTransform()) {
RenderBox& renderBox = toRenderBox(renderer());
style.applyTransform(t, snapRectToDevicePixels(renderBox.borderBoxRect(), deviceScaleFactor()), RenderStyle::ExcludeTransformOrigin);
makeMatrixRenderable(t, compositor().canRender3DTransforms());
}
if (m_contentsContainmentLayer) {
m_contentsContainmentLayer->setTransform(t);
m_graphicsLayer->setTransform(TransformationMatrix());
} else
m_graphicsLayer->setTransform(t);
}
void RenderLayerBacking::updateFilters(const RenderStyle& style)
{
m_canCompositeFilters = m_graphicsLayer->setFilters(style.filter());
}
#if ENABLE(CSS_COMPOSITING)
void RenderLayerBacking::updateBlendMode(const RenderStyle& style)
{
// FIXME: where is the blend mode updated when m_ancestorClippingLayers come and go?
if (m_ancestorClippingLayer) {
m_ancestorClippingLayer->setBlendMode(style.blendMode());
m_graphicsLayer->setBlendMode(BlendModeNormal);
} else
m_graphicsLayer->setBlendMode(style.blendMode());
}
#endif
// FIXME: the hasAcceleratedTouchScrolling()/needsCompositedScrolling() concepts need to be merged.
static bool layerOrAncestorIsTransformedOrUsingCompositedScrolling(RenderLayer& layer)
{
for (RenderLayer* curr = &layer; curr; curr = curr->parent()) {
if (curr->hasTransform()
#if PLATFORM(IOS)
|| curr->hasTouchScrollableOverflow()
#else
|| curr->needsCompositedScrolling()
#endif
)
return true;
}
return false;
}
bool RenderLayerBacking::shouldClipCompositedBounds() const
{
#if !PLATFORM(IOS)
// Scrollbar layers use this layer for relative positioning, so don't clip.
if (layerForHorizontalScrollbar() || layerForVerticalScrollbar())
return false;
#endif
if (m_usingTiledCacheLayer)
return false;
if (layerOrAncestorIsTransformedOrUsingCompositedScrolling(m_owningLayer))
return false;
if (m_owningLayer.isFlowThreadCollectingGraphicsLayersUnderRegions())
return false;
return true;
}
static bool hasNonZeroTransformOrigin(const RenderObject& renderer)
{
const RenderStyle& style = renderer.style();
return (style.transformOriginX().type() == Fixed && style.transformOriginX().value())
|| (style.transformOriginY().type() == Fixed && style.transformOriginY().value());
}
void RenderLayerBacking::updateCompositedBounds()
{
LayoutRect layerBounds = m_owningLayer.calculateLayerBounds(&m_owningLayer, LayoutSize(), RenderLayer::DefaultCalculateLayerBoundsFlags | RenderLayer::ExcludeHiddenDescendants | RenderLayer::DontConstrainForMask);
// Clip to the size of the document or enclosing overflow-scroll layer.
// If this or an ancestor is transformed, we can't currently compute the correct rect to intersect with.
// We'd need RenderObject::convertContainerToLocalQuad(), which doesn't yet exist.
if (shouldClipCompositedBounds()) {
RenderView& view = m_owningLayer.renderer().view();
RenderLayer* rootLayer = view.layer();
LayoutRect clippingBounds;
if (renderer().style().position() == FixedPosition && renderer().container() == &view)
clippingBounds = view.frameView().viewportConstrainedVisibleContentRect();
else
clippingBounds = view.unscaledDocumentRect();
if (&m_owningLayer != rootLayer)
clippingBounds.intersect(m_owningLayer.backgroundClipRect(RenderLayer::ClipRectsContext(rootLayer, AbsoluteClipRects)).rect()); // FIXME: Incorrect for CSS regions.
LayoutPoint delta = m_owningLayer.convertToLayerCoords(rootLayer, LayoutPoint(), RenderLayer::AdjustForColumns);
clippingBounds.move(-delta.x(), -delta.y());
layerBounds.intersect(clippingBounds);
}
// If the element has a transform-origin that has fixed lengths, and the renderer has zero size,
// then we need to ensure that the compositing layer has non-zero size so that we can apply
// the transform-origin via the GraphicsLayer anchorPoint (which is expressed as a fractional value).
if (layerBounds.isEmpty() && hasNonZeroTransformOrigin(renderer())) {
layerBounds.setWidth(1);
layerBounds.setHeight(1);
m_artificiallyInflatedBounds = true;
} else
m_artificiallyInflatedBounds = false;
setCompositedBounds(layerBounds);
}
void RenderLayerBacking::updateAfterWidgetResize()
{
if (!renderer().isWidget())
return;
if (RenderLayerCompositor* innerCompositor = RenderLayerCompositor::frameContentsCompositor(toRenderWidget(&renderer()))) {
innerCompositor->frameViewDidChangeSize();
innerCompositor->frameViewDidChangeLocation(flooredIntPoint(contentsBox().location()));
}
}
void RenderLayerBacking::updateAfterLayout(UpdateAfterLayoutFlags flags)
{
if (!compositor().compositingLayersNeedRebuild()) {
// Calling updateGeometry() here gives incorrect results, because the
// position of this layer's GraphicsLayer depends on the position of our compositing
// ancestor's GraphicsLayer. That cannot be determined until all the descendant
// RenderLayers of that ancestor have been processed via updateLayerPositions().
//
// The solution is to update compositing children of this layer here,
// via updateCompositingChildrenGeometry().
updateCompositedBounds();
compositor().updateCompositingDescendantGeometry(m_owningLayer, m_owningLayer, flags & CompositingChildrenOnly);
if (flags & IsUpdateRoot) {
updateGeometry();
compositor().updateRootLayerPosition();
RenderLayer* stackingContainer = m_owningLayer.enclosingStackingContainer();
if (!compositor().compositingLayersNeedRebuild() && stackingContainer && (stackingContainer != &m_owningLayer))
compositor().updateCompositingDescendantGeometry(*stackingContainer, *stackingContainer, flags & CompositingChildrenOnly);
}
}
if (flags & NeedsFullRepaint && !paintsIntoWindow() && !paintsIntoCompositedAncestor())
setContentsNeedDisplay();
}
bool RenderLayerBacking::updateConfiguration()
{
m_owningLayer.updateDescendantDependentFlags();
m_owningLayer.updateZOrderLists();
bool layerConfigChanged = false;
setBackgroundLayerPaintsFixedRootBackground(compositor().needsFixedRootBackgroundLayer(m_owningLayer));
// The background layer is currently only used for fixed root backgrounds.
if (updateBackgroundLayer(m_backgroundLayerPaintsFixedRootBackground))
layerConfigChanged = true;
if (updateForegroundLayer(compositor().needsContentsCompositingLayer(m_owningLayer)))
layerConfigChanged = true;
bool needsDescendentsClippingLayer = compositor().clipsCompositingDescendants(m_owningLayer);
if (!renderer().view().needsLayout()) {
bool usesCompositedScrolling;
#if PLATFORM(IOS)
usesCompositedScrolling = m_owningLayer.hasTouchScrollableOverflow();
#else
usesCompositedScrolling = m_owningLayer.needsCompositedScrolling();
#endif
// Our scrolling layer will clip.
if (usesCompositedScrolling)
needsDescendentsClippingLayer = false;
if (updateScrollingLayers(usesCompositedScrolling))
layerConfigChanged = true;
if (updateDescendantClippingLayer(needsDescendentsClippingLayer))
layerConfigChanged = true;
}
if (updateAncestorClippingLayer(compositor().clippedByAncestor(m_owningLayer)))
layerConfigChanged = true;
if (updateOverflowControlsLayers(requiresHorizontalScrollbarLayer(), requiresVerticalScrollbarLayer(), requiresScrollCornerLayer()))
layerConfigChanged = true;
if (layerConfigChanged)
updateInternalHierarchy();
if (GraphicsLayer* flatteningLayer = tileCacheFlatteningLayer()) {
if (layerConfigChanged || flatteningLayer->parent() != m_graphicsLayer.get())
m_graphicsLayer->addChild(flatteningLayer);
}
updateMaskLayer(renderer().hasMask());
if (m_owningLayer.hasReflection()) {
if (m_owningLayer.reflectionLayer()->backing()) {
GraphicsLayer* reflectionLayer = m_owningLayer.reflectionLayer()->backing()->graphicsLayer();
m_graphicsLayer->setReplicatedByLayer(reflectionLayer);
}
} else
m_graphicsLayer->setReplicatedByLayer(0);
if (!m_owningLayer.isRootLayer()) {
bool isSimpleContainer = isSimpleContainerCompositingLayer();
bool didUpdateContentsRect = false;
updateDirectlyCompositedContents(isSimpleContainer, didUpdateContentsRect);
} else
updateRootLayerConfiguration();
if (isDirectlyCompositedImage())
updateImageContents();
if (renderer().isEmbeddedObject() && toRenderEmbeddedObject(&renderer())->allowsAcceleratedCompositing()) {
PluginViewBase* pluginViewBase = toPluginViewBase(toRenderWidget(&renderer())->widget());
#if PLATFORM(IOS)
if (pluginViewBase && !m_graphicsLayer->contentsLayerForMedia()) {
pluginViewBase->detachPluginLayer();
pluginViewBase->attachPluginLayer();
}
#else
if (!pluginViewBase->shouldNotAddLayer())
m_graphicsLayer->setContentsToPlatformLayer(pluginViewBase->platformLayer(), GraphicsLayer::ContentsLayerForPlugin);
#endif
}
#if ENABLE(VIDEO)
else if (renderer().isVideo()) {
HTMLMediaElement* mediaElement = toHTMLMediaElement(renderer().element());
m_graphicsLayer->setContentsToPlatformLayer(mediaElement->platformLayer(), GraphicsLayer::ContentsLayerForMedia);
}
#endif
#if ENABLE(WEBGL) || ENABLE(ACCELERATED_2D_CANVAS)
else if (renderer().isCanvas() && canvasCompositingStrategy(renderer()) == CanvasAsLayerContents) {
const HTMLCanvasElement* canvas = toHTMLCanvasElement(renderer().element());
if (CanvasRenderingContext* context = canvas->renderingContext())
m_graphicsLayer->setContentsToPlatformLayer(context->platformLayer(), GraphicsLayer::ContentsLayerForCanvas);
layerConfigChanged = true;
}
#endif
if (renderer().isWidget())
layerConfigChanged = RenderLayerCompositor::parentFrameContentLayers(toRenderWidget(&renderer()));
return layerConfigChanged;
}
static LayoutRect clipBox(RenderBox& renderer)
{
LayoutRect result = LayoutRect::infiniteRect();
if (renderer.hasOverflowClip())
result = renderer.overflowClipRect(LayoutPoint(), 0); // FIXME: Incorrect for CSS regions.
if (renderer.hasClip())
result.intersect(renderer.clipRect(LayoutPoint(), 0)); // FIXME: Incorrect for CSS regions.
return result;
}
static FloatSize pixelFractionForLayerPainting(const LayoutPoint& point, float pixelSnappingFactor)
{
LayoutUnit x = point.x();
LayoutUnit y = point.y();
x = x >= 0 ? floorToDevicePixel(x, pixelSnappingFactor) : ceilToDevicePixel(x, pixelSnappingFactor);
y = y >= 0 ? floorToDevicePixel(y, pixelSnappingFactor) : ceilToDevicePixel(y, pixelSnappingFactor);
return point - LayoutPoint(x, y);
}
static void calculateDevicePixelOffsetFromRenderer(const LayoutSize& rendererOffsetFromGraphicsLayer, FloatSize& devicePixelOffsetFromRenderer,
LayoutSize& devicePixelFractionFromRenderer, float deviceScaleFactor)
{
devicePixelFractionFromRenderer = LayoutSize(pixelFractionForLayerPainting(toLayoutPoint(rendererOffsetFromGraphicsLayer), deviceScaleFactor));
devicePixelOffsetFromRenderer = rendererOffsetFromGraphicsLayer - devicePixelFractionFromRenderer;
}
void RenderLayerBacking::updateGeometry()
{
// If we haven't built z-order lists yet, wait until later.
if (m_owningLayer.isStackingContainer() && m_owningLayer.m_zOrderListsDirty)
return;
const RenderStyle& style = renderer().style();
// Set transform property, if it is not animating. We have to do this here because the transform
// is affected by the layer dimensions.
if (!renderer().animation().isRunningAcceleratedAnimationOnRenderer(renderer(), CSSPropertyWebkitTransform, AnimationBase::Running | AnimationBase::Paused | AnimationBase::FillingFowards))
updateTransform(style);
// Set opacity, if it is not animating.
if (!renderer().animation().isRunningAcceleratedAnimationOnRenderer(renderer(), CSSPropertyOpacity, AnimationBase::Running | AnimationBase::Paused | AnimationBase::FillingFowards))
updateOpacity(style);
updateFilters(style);
#if ENABLE(CSS_COMPOSITING)
updateBlendMode(style);
#endif
m_owningLayer.updateDescendantDependentFlags();
// FIXME: reflections should force transform-style to be flat in the style: https://bugs.webkit.org/show_bug.cgi?id=106959
bool preserves3D = style.transformStyle3D() == TransformStyle3DPreserve3D && !renderer().hasReflection();
m_graphicsLayer->setPreserves3D(preserves3D);
m_graphicsLayer->setBackfaceVisibility(style.backfaceVisibility() == BackfaceVisibilityVisible);
RenderLayer* compAncestor = m_owningLayer.ancestorCompositingLayer();
// We compute everything relative to the enclosing compositing layer.
LayoutRect ancestorCompositingBounds;
if (compAncestor) {
ASSERT(compAncestor->backing());
ancestorCompositingBounds = compAncestor->backing()->compositedBounds();
}
/*
* GraphicsLayer: device pixel positioned, enclosing rect.
* RenderLayer: subpixel positioned.
* Offset from renderer (GraphicsLayer <-> RenderLayer::renderer()): subpixel based offset.
*
* relativeCompositingBounds
* _______________________________________
* |\ GraphicsLayer |
* | \ |
* | \ offset from renderer: (device pixel + subpixel)
* | \ |
* | \______________________________ |
* | | localCompositingBounds | |
* | | | |
* | | RenderLayer::renderer() | |
* | | | |
*
* localCompositingBounds: this RenderLayer relative to its renderer().
* relativeCompositingBounds: this RenderLayer relative to its parent compositing layer.
* enclosingRelativeCompositingBounds: this RenderLayer relative to its parent, device pixel enclosing.
* rendererOffsetFromGraphicsLayer: RenderLayer::renderer()'s offset from its enclosing GraphicsLayer.
* devicePixelOffsetFromRenderer: rendererOffsetFromGraphicsLayer's device pixel part. (6.9px -> 6.5px in case of 2x display)
* devicePixelFractionFromRenderer: rendererOffsetFromGraphicsLayer's fractional part (6.9px -> 0.4px in case of 2x display)
*/
float deviceScaleFactor = this->deviceScaleFactor();
LayoutRect localCompositingBounds = compositedBounds();
LayoutRect relativeCompositingBounds(localCompositingBounds);
LayoutPoint offsetFromParent = m_owningLayer.convertToLayerCoords(compAncestor, LayoutPoint(), RenderLayer::AdjustForColumns);
// Device pixel fractions get accumulated through ancestor layers. Our painting offset is layout offset + parent's painting offset.
offsetFromParent = offsetFromParent + (compAncestor ? compAncestor->backing()->devicePixelFractionFromRenderer() : LayoutSize());
relativeCompositingBounds.moveBy(offsetFromParent);
LayoutRect enclosingRelativeCompositingBounds = LayoutRect(encloseRectToDevicePixels(relativeCompositingBounds, deviceScaleFactor));
LayoutSize subpixelOffsetAdjustment = enclosingRelativeCompositingBounds.location() - relativeCompositingBounds.location();
LayoutSize rendererOffsetFromGraphicsLayer = toLayoutSize(localCompositingBounds.location()) + subpixelOffsetAdjustment;
FloatSize devicePixelOffsetFromRenderer;
LayoutSize devicePixelFractionFromRenderer;
calculateDevicePixelOffsetFromRenderer(rendererOffsetFromGraphicsLayer, devicePixelOffsetFromRenderer, devicePixelFractionFromRenderer, deviceScaleFactor);
m_devicePixelFractionFromRenderer = LayoutSize(-devicePixelFractionFromRenderer.width(), -devicePixelFractionFromRenderer.height());
adjustAncestorCompositingBoundsForFlowThread(ancestorCompositingBounds, compAncestor);
LayoutPoint graphicsLayerParentLocation;
if (compAncestor && compAncestor->backing()->hasClippingLayer()) {
// If the compositing ancestor has a layer to clip children, we parent in that, and therefore
// position relative to it.
// FIXME: need to do some pixel snapping here.
LayoutRect clippingBox = clipBox(toRenderBox(compAncestor->renderer()));
graphicsLayerParentLocation = clippingBox.location();
} else if (compAncestor)
graphicsLayerParentLocation = ancestorCompositingBounds.location();
else
graphicsLayerParentLocation = renderer().view().documentRect().location();
#if PLATFORM(IOS)
if (compAncestor && compAncestor->hasTouchScrollableOverflow()) {
RenderBox* renderBox = toRenderBox(&compAncestor->renderer());
LayoutRect paddingBox(renderBox->borderLeft(), renderBox->borderTop(),
renderBox->width() - renderBox->borderLeft() - renderBox->borderRight(),
renderBox->height() - renderBox->borderTop() - renderBox->borderBottom());
IntSize scrollOffset = compAncestor->scrolledContentOffset();
// FIXME: pixel snap the padding box.
graphicsLayerParentLocation = paddingBox.location() - scrollOffset;
}
#else
if (compAncestor && compAncestor->needsCompositedScrolling()) {
RenderBox& renderBox = toRenderBox(compAncestor->renderer());
LayoutSize scrollOffset = compAncestor->scrolledContentOffset();
LayoutPoint scrollOrigin(renderBox.borderLeft(), renderBox.borderTop());
graphicsLayerParentLocation = scrollOrigin - scrollOffset;
}
#endif
if (compAncestor && m_ancestorClippingLayer) {
// Call calculateRects to get the backgroundRect which is what is used to clip the contents of this
// layer. Note that we call it with temporaryClipRects = true because normally when computing clip rects
// for a compositing layer, rootLayer is the layer itself.
ShouldRespectOverflowClip shouldRespectOverflowClip = compAncestor->isolatesCompositedBlending() ? RespectOverflowClip : IgnoreOverflowClip;
RenderLayer::ClipRectsContext clipRectsContext(compAncestor, TemporaryClipRects, IgnoreOverlayScrollbarSize, shouldRespectOverflowClip);
LayoutRect parentClipRect = m_owningLayer.backgroundClipRect(clipRectsContext).rect(); // FIXME: Incorrect for CSS regions.
ASSERT(parentClipRect != LayoutRect::infiniteRect());
m_ancestorClippingLayer->setPosition(FloatPoint(parentClipRect.location() - graphicsLayerParentLocation));
m_ancestorClippingLayer->setSize(parentClipRect.size());
// backgroundRect is relative to compAncestor, so subtract deltaX/deltaY to get back to local coords.
m_ancestorClippingLayer->setOffsetFromRenderer(parentClipRect.location() - offsetFromParent);
// The primary layer is then parented in, and positioned relative to this clipping layer.
graphicsLayerParentLocation = parentClipRect.location();
}
LayoutSize contentsSize = enclosingRelativeCompositingBounds.size();
if (m_contentsContainmentLayer) {
m_contentsContainmentLayer->setPreserves3D(preserves3D);
m_contentsContainmentLayer->setPosition(FloatPoint(enclosingRelativeCompositingBounds.location() - graphicsLayerParentLocation));
// Use the same size as m_graphicsLayer so transforms behave correctly.
m_contentsContainmentLayer->setSize(contentsSize);
graphicsLayerParentLocation = enclosingRelativeCompositingBounds.location();
}
m_graphicsLayer->setPosition(FloatPoint(enclosingRelativeCompositingBounds.location() - graphicsLayerParentLocation));
m_graphicsLayer->setSize(contentsSize);
if (devicePixelOffsetFromRenderer != m_graphicsLayer->offsetFromRenderer()) {
m_graphicsLayer->setOffsetFromRenderer(devicePixelOffsetFromRenderer);
positionOverflowControlsLayers();
}
if (!m_isMainFrameRenderViewLayer) {
// For non-root layers, background is always painted by the primary graphics layer.
ASSERT(!m_backgroundLayer);
bool hadSubpixelRounding = enclosingRelativeCompositingBounds != relativeCompositingBounds;
m_graphicsLayer->setContentsOpaque(!hadSubpixelRounding && m_owningLayer.backgroundIsKnownToBeOpaqueInRect(localCompositingBounds));
}
// If we have a layer that clips children, position it.
LayoutRect clippingBox;
if (GraphicsLayer* clipLayer = clippingLayer()) {
// FIXME: need to do some pixel snapping here.
clippingBox = clipBox(toRenderBox(renderer()));
clipLayer->setPosition(FloatPoint(clippingBox.location() - localCompositingBounds.location()));
clipLayer->setSize(clippingBox.size());
clipLayer->setOffsetFromRenderer(toFloatSize(clippingBox.location()));
}
if (m_maskLayer) {
m_maskLayer->setSize(m_graphicsLayer->size());
m_maskLayer->setPosition(FloatPoint());
m_maskLayer->setOffsetFromRenderer(m_graphicsLayer->offsetFromRenderer());
}
if (m_owningLayer.hasTransform()) {
// Update properties that depend on layer dimensions.
FloatPoint3D transformOrigin = computeTransformOriginForPainting(toRenderBox(renderer()).borderBoxRect());
// Get layout bounds in the coords of compAncestor to match relativeCompositingBounds.
FloatPoint layerOffset = roundPointToDevicePixels(offsetFromParent, deviceScaleFactor);
// Compute the anchor point, which is in the center of the renderer box unless transform-origin is set.
FloatPoint3D anchor(enclosingRelativeCompositingBounds.width() ? ((layerOffset.x() - enclosingRelativeCompositingBounds.x()) + transformOrigin.x())
/ enclosingRelativeCompositingBounds.width() : 0.5, enclosingRelativeCompositingBounds.height() ? ((layerOffset.y() - enclosingRelativeCompositingBounds.y())
+ transformOrigin.y()) / enclosingRelativeCompositingBounds.height() : 0.5, transformOrigin.z());
if (m_contentsContainmentLayer)
m_contentsContainmentLayer->setAnchorPoint(anchor);
else
m_graphicsLayer->setAnchorPoint(anchor);
GraphicsLayer* clipLayer = clippingLayer();
if (style.hasPerspective()) {
TransformationMatrix t = owningLayer().perspectiveTransform();
if (clipLayer) {
clipLayer->setChildrenTransform(t);
m_graphicsLayer->setChildrenTransform(TransformationMatrix());
}
else
m_graphicsLayer->setChildrenTransform(t);
} else {
if (clipLayer)
clipLayer->setChildrenTransform(TransformationMatrix());
else
m_graphicsLayer->setChildrenTransform(TransformationMatrix());
}
} else {
m_graphicsLayer->setAnchorPoint(FloatPoint3D(0.5, 0.5, 0));
if (m_contentsContainmentLayer)
m_contentsContainmentLayer->setAnchorPoint(FloatPoint3D(0.5, 0.5, 0));
}
if (m_foregroundLayer) {
FloatPoint foregroundPosition;
FloatSize foregroundSize = contentsSize;
FloatSize foregroundOffset = m_graphicsLayer->offsetFromRenderer();
if (hasClippingLayer()) {
// If we have a clipping layer (which clips descendants), then the foreground layer is a child of it,
// so that it gets correctly sorted with children. In that case, position relative to the clipping layer.
foregroundSize = FloatSize(clippingBox.size());
foregroundOffset = toFloatSize(clippingBox.location());
}
m_foregroundLayer->setPosition(foregroundPosition);
m_foregroundLayer->setSize(foregroundSize);
m_foregroundLayer->setOffsetFromRenderer(foregroundOffset);
}
if (m_backgroundLayer) {
FloatPoint backgroundPosition;
FloatSize backgroundSize = contentsSize;
if (backgroundLayerPaintsFixedRootBackground()) {
const FrameView& frameView = renderer().view().frameView();
backgroundPosition = toLayoutPoint(frameView.scrollOffsetForFixedPosition());
backgroundSize = frameView.visibleContentRect().size();
}
m_backgroundLayer->setPosition(backgroundPosition);
m_backgroundLayer->setSize(backgroundSize);
m_backgroundLayer->setOffsetFromRenderer(m_graphicsLayer->offsetFromRenderer());
}
if (m_owningLayer.reflectionLayer() && m_owningLayer.reflectionLayer()->isComposited()) {
RenderLayerBacking* reflectionBacking = m_owningLayer.reflectionLayer()->backing();
reflectionBacking->updateGeometry();
// The reflection layer has the bounds of m_owningLayer.reflectionLayer(),
// but the reflected layer is the bounds of this layer, so we need to position it appropriately.
FloatRect layerBounds = compositedBounds();
FloatRect reflectionLayerBounds = reflectionBacking->compositedBounds();
reflectionBacking->graphicsLayer()->setReplicatedLayerPosition(FloatPoint(layerBounds.location() - reflectionLayerBounds.location()));
}
if (m_scrollingLayer) {
ASSERT(m_scrollingContentsLayer);
RenderBox& renderBox = toRenderBox(renderer());
LayoutRect paddingBox(renderBox.borderLeft(), renderBox.borderTop(), renderBox.width() - renderBox.borderLeft() - renderBox.borderRight(), renderBox.height() - renderBox.borderTop() - renderBox.borderBottom());
LayoutSize scrollOffset = m_owningLayer.scrollOffset();
// FIXME: need to do some pixel snapping here.
m_scrollingLayer->setPosition(FloatPoint(paddingBox.location() - localCompositingBounds.location()));
IntSize pixelSnappedClientSize(renderBox.pixelSnappedClientWidth(), renderBox.pixelSnappedClientHeight());
m_scrollingLayer->setSize(pixelSnappedClientSize);
#if PLATFORM(IOS)
FloatSize oldScrollingLayerOffset = m_scrollingLayer->offsetFromRenderer();
m_scrollingLayer->setOffsetFromRenderer(FloatPoint() - paddingBox.location());
bool paddingBoxOffsetChanged = oldScrollingLayerOffset != m_scrollingLayer->offsetFromRenderer();
if (m_owningLayer.isInUserScroll()) {
// If scrolling is happening externally, we don't want to touch the layer bounds origin here because that will cause jitter.
m_scrollingLayer->syncBoundsOrigin(FloatPoint(scrollOffset.width(), scrollOffset.height()));
m_owningLayer.setRequiresScrollBoundsOriginUpdate(true);
} else {
// Note that we implement the contents offset via the bounds origin on this layer, rather than a position on the sublayer.
m_scrollingLayer->setBoundsOrigin(FloatPoint(scrollOffset.width(), scrollOffset.height()));
m_owningLayer.setRequiresScrollBoundsOriginUpdate(false);
}
IntSize scrollSize(m_owningLayer.scrollWidth(), m_owningLayer.scrollHeight());
m_scrollingContentsLayer->setPosition(FloatPoint());
if (scrollSize != m_scrollingContentsLayer->size() || paddingBoxOffsetChanged)
m_scrollingContentsLayer->setNeedsDisplay();
m_scrollingContentsLayer->setSize(scrollSize);
// Scrolling the content layer does not need to trigger a repaint. The offset will be compensated away during painting.
// FIXME: The paint offset and the scroll offset should really be separate concepts.
m_scrollingContentsLayer->setOffsetFromRenderer(paddingBox.location() - IntPoint() - scrollOffset, GraphicsLayer::DontSetNeedsDisplay);
#else
m_scrollingContentsLayer->setPosition(FloatPoint(-scrollOffset.width(), -scrollOffset.height()));
FloatSize oldScrollingLayerOffset = m_scrollingLayer->offsetFromRenderer();
m_scrollingLayer->setOffsetFromRenderer(-toFloatSize(paddingBox.location()));
bool paddingBoxOffsetChanged = oldScrollingLayerOffset != m_scrollingLayer->offsetFromRenderer();
IntSize scrollSize(m_owningLayer.scrollWidth(), m_owningLayer.scrollHeight());
if (scrollSize != m_scrollingContentsLayer->size() || paddingBoxOffsetChanged)
m_scrollingContentsLayer->setNeedsDisplay();
LayoutSize scrollingContentsOffset = toLayoutSize(paddingBox.location() - scrollOffset);
if (scrollingContentsOffset != m_scrollingContentsLayer->offsetFromRenderer() || scrollSize != m_scrollingContentsLayer->size())
compositor().scrollingLayerDidChange(m_owningLayer);
m_scrollingContentsLayer->setSize(scrollSize);
// FIXME: The paint offset and the scroll offset should really be separate concepts.
m_scrollingContentsLayer->setOffsetFromRenderer(scrollingContentsOffset, GraphicsLayer::DontSetNeedsDisplay);
#endif
if (m_foregroundLayer) {
m_foregroundLayer->setSize(m_scrollingContentsLayer->size());
m_foregroundLayer->setOffsetFromRenderer(m_scrollingContentsLayer->offsetFromRenderer());
}
}
// If this layer was created just for clipping or to apply perspective, it doesn't need its own backing store.
setRequiresOwnBackingStore(compositor().requiresOwnBackingStore(m_owningLayer, compAncestor, enclosingRelativeCompositingBounds, ancestorCompositingBounds));
updateAfterWidgetResize();
compositor().updateScrollCoordinatedStatus(m_owningLayer);
}
void RenderLayerBacking::updateAfterDescendants()
{
bool isSimpleContainer = false;
if (!m_owningLayer.isRootLayer()) {
bool didUpdateContentsRect = false;
// FIXME: this duplicates work we did in updateConfiguration().
isSimpleContainer = isSimpleContainerCompositingLayer();
updateDirectlyCompositedContents(isSimpleContainer, didUpdateContentsRect);
if (!didUpdateContentsRect && m_graphicsLayer->usesContentsLayer())
resetContentsRect();
}
updateDrawsContent(isSimpleContainer);
m_graphicsLayer->setContentsVisible(m_owningLayer.hasVisibleContent() || isPaintDestinationForDescendentLayers());
}
void RenderLayerBacking::adjustAncestorCompositingBoundsForFlowThread(LayoutRect& ancestorCompositingBounds, const RenderLayer* compositingAncestor) const
{
if (!m_owningLayer.isInsideFlowThread())
return;
RenderLayer* flowThreadLayer = m_owningLayer.isInsideOutOfFlowThread() ? m_owningLayer.stackingContainer() : nullptr;
if (flowThreadLayer && flowThreadLayer->isRenderFlowThread()) {
if (m_owningLayer.isFlowThreadCollectingGraphicsLayersUnderRegions()) {
// The RenderNamedFlowThread is not composited, as we need it to paint the
// background layer of the regions. We need to compensate for that by manually
// subtracting the position of the flow-thread.
IntPoint flowPosition;
flowThreadLayer->convertToPixelSnappedLayerCoords(compositingAncestor, flowPosition);
ancestorCompositingBounds.moveBy(flowPosition);
}
// Move the ancestor position at the top of the region where the composited layer is going to display.
RenderFlowThread& flowThread = toRenderFlowThread(flowThreadLayer->renderer());
RenderNamedFlowFragment* parentRegion = flowThread.cachedRegionForCompositedLayer(m_owningLayer);
if (!parentRegion)
return;
IntPoint flowDelta;
m_owningLayer.convertToPixelSnappedLayerCoords(flowThreadLayer, flowDelta);
parentRegion->adjustRegionBoundsFromFlowThreadPortionRect(flowDelta, ancestorCompositingBounds);
RenderBoxModelObject& layerOwner = toRenderBoxModelObject(parentRegion->layerOwner());
RenderLayerBacking* layerOwnerBacking = layerOwner.layer()->backing();
if (!layerOwnerBacking)
return;
// Make sure that the region propagates its borders, paddings, outlines or box-shadows to layers inside it.
// Note that the composited bounds of the RenderRegion are already calculated because
// RenderLayerCompositor::rebuildCompositingLayerTree will only iterate on the content of the region after the
// region itself is computed.
ancestorCompositingBounds.moveBy(roundedIntPoint(layerOwnerBacking->compositedBounds().location()));
ancestorCompositingBounds.move(-layerOwner.borderAndPaddingStart(), -layerOwner.borderAndPaddingBefore());
// If there's a clipping GraphicsLayer on the hierarchy (region graphics layer -> clipping graphics layer ->
// composited content graphics layer), substract the offset of the clipping layer, since it's its parent
// that positions us (the graphics layer of the region).
if (layerOwnerBacking->clippingLayer())
ancestorCompositingBounds.moveBy(roundedIntPoint(layerOwnerBacking->clippingLayer()->position()));
}
}
void RenderLayerBacking::updateDirectlyCompositedContents(bool isSimpleContainer, bool& didUpdateContentsRect)
{
if (!m_owningLayer.hasVisibleContent())
return;
// The order of operations here matters, since the last valid type of contents needs
// to also update the contentsRect.
updateDirectlyCompositedBackgroundColor(isSimpleContainer, didUpdateContentsRect);
updateDirectlyCompositedBackgroundImage(isSimpleContainer, didUpdateContentsRect);
}
void RenderLayerBacking::updateInternalHierarchy()
{
// m_foregroundLayer has to be inserted in the correct order with child layers,
// so it's not inserted here.
if (m_ancestorClippingLayer)
m_ancestorClippingLayer->removeAllChildren();
if (m_contentsContainmentLayer) {
m_contentsContainmentLayer->removeAllChildren();
if (m_ancestorClippingLayer)
m_ancestorClippingLayer->addChild(m_contentsContainmentLayer.get());
}
if (m_backgroundLayer)
m_contentsContainmentLayer->addChild(m_backgroundLayer.get());
if (m_contentsContainmentLayer)
m_contentsContainmentLayer->addChild(m_graphicsLayer.get());
else if (m_ancestorClippingLayer)
m_ancestorClippingLayer->addChild(m_graphicsLayer.get());
if (m_childContainmentLayer) {
m_childContainmentLayer->removeFromParent();
m_graphicsLayer->addChild(m_childContainmentLayer.get());
}
if (m_scrollingLayer) {
GraphicsLayer* superlayer = m_childContainmentLayer ? m_childContainmentLayer.get() : m_graphicsLayer.get();
m_scrollingLayer->removeFromParent();
superlayer->addChild(m_scrollingLayer.get());
}
// The clip for child layers does not include space for overflow controls, so they exist as
// siblings of the clipping layer if we have one. Normal children of this layer are set as
// children of the clipping layer.
if (m_layerForHorizontalScrollbar) {
m_layerForHorizontalScrollbar->removeFromParent();
m_graphicsLayer->addChild(m_layerForHorizontalScrollbar.get());
}
if (m_layerForVerticalScrollbar) {
m_layerForVerticalScrollbar->removeFromParent();
m_graphicsLayer->addChild(m_layerForVerticalScrollbar.get());
}
if (m_layerForScrollCorner) {
m_layerForScrollCorner->removeFromParent();
m_graphicsLayer->addChild(m_layerForScrollCorner.get());
}
}
void RenderLayerBacking::resetContentsRect()
{
m_graphicsLayer->setContentsRect(snappedIntRect(contentsBox()));
LayoutRect contentsClippingRect;
if (renderer().isBox())
contentsClippingRect = toRenderBox(renderer()).contentBoxRect();
contentsClippingRect.move(contentOffsetInCompostingLayer());
m_graphicsLayer->setContentsClippingRect(snappedIntRect(contentsClippingRect));
m_graphicsLayer->setContentsTileSize(IntSize());
m_graphicsLayer->setContentsTilePhase(IntPoint());
}
void RenderLayerBacking::updateDrawsContent()
{
updateDrawsContent(isSimpleContainerCompositingLayer());
}
void RenderLayerBacking::updateDrawsContent(bool isSimpleContainer)
{
if (m_scrollingLayer) {
// We don't have to consider overflow controls, because we know that the scrollbars are drawn elsewhere.
// m_graphicsLayer only needs backing store if the non-scrolling parts (background, outlines, borders, shadows etc) need to paint.
// m_scrollingLayer never has backing store.
// m_scrollingContentsLayer only needs backing store if the scrolled contents need to paint.
bool hasNonScrollingPaintedContent = m_owningLayer.hasVisibleContent() && m_owningLayer.hasBoxDecorationsOrBackground();
m_graphicsLayer->setDrawsContent(hasNonScrollingPaintedContent);
bool hasScrollingPaintedContent = m_owningLayer.hasVisibleContent() && (renderer().hasBackground() || paintsChildren());
m_scrollingContentsLayer->setDrawsContent(hasScrollingPaintedContent);
return;
}
bool hasPaintedContent = containsPaintedContent(isSimpleContainer);
// FIXME: we could refine this to only allocate backing for one of these layers if possible.
m_graphicsLayer->setDrawsContent(hasPaintedContent);
if (m_foregroundLayer)
m_foregroundLayer->setDrawsContent(hasPaintedContent);
if (m_backgroundLayer)
m_backgroundLayer->setDrawsContent(hasPaintedContent);
}
// Return true if the layer changed.
bool RenderLayerBacking::updateAncestorClippingLayer(bool needsAncestorClip)
{
bool layersChanged = false;
if (needsAncestorClip) {
if (!m_ancestorClippingLayer) {
m_ancestorClippingLayer = createGraphicsLayer("Ancestor clipping Layer");
m_ancestorClippingLayer->setMasksToBounds(true);
layersChanged = true;
}
} else if (hasAncestorClippingLayer()) {
willDestroyLayer(m_ancestorClippingLayer.get());
m_ancestorClippingLayer->removeFromParent();
m_ancestorClippingLayer = nullptr;
layersChanged = true;
}
return layersChanged;
}
// Return true if the layer changed.
bool RenderLayerBacking::updateDescendantClippingLayer(bool needsDescendantClip)
{
bool layersChanged = false;
if (needsDescendantClip) {
if (!m_childContainmentLayer && !m_usingTiledCacheLayer) {
m_childContainmentLayer = createGraphicsLayer("Child clipping Layer");
m_childContainmentLayer->setMasksToBounds(true);
layersChanged = true;
}
} else if (hasClippingLayer()) {
willDestroyLayer(m_childContainmentLayer.get());
m_childContainmentLayer->removeFromParent();
m_childContainmentLayer = nullptr;
layersChanged = true;
}
return layersChanged;
}
void RenderLayerBacking::setBackgroundLayerPaintsFixedRootBackground(bool backgroundLayerPaintsFixedRootBackground)
{
m_backgroundLayerPaintsFixedRootBackground = backgroundLayerPaintsFixedRootBackground;
}
bool RenderLayerBacking::requiresHorizontalScrollbarLayer() const
{
if (!m_owningLayer.hasOverlayScrollbars() && !m_owningLayer.needsCompositedScrolling())
return false;
return m_owningLayer.horizontalScrollbar();
}
bool RenderLayerBacking::requiresVerticalScrollbarLayer() const
{
if (!m_owningLayer.hasOverlayScrollbars() && !m_owningLayer.needsCompositedScrolling())
return false;
return m_owningLayer.verticalScrollbar();
}
bool RenderLayerBacking::requiresScrollCornerLayer() const
{
if (!m_owningLayer.hasOverlayScrollbars() && !m_owningLayer.needsCompositedScrolling())
return false;
return !m_owningLayer.scrollCornerAndResizerRect().isEmpty();
}
bool RenderLayerBacking::updateOverflowControlsLayers(bool needsHorizontalScrollbarLayer, bool needsVerticalScrollbarLayer, bool needsScrollCornerLayer)
{
bool horizontalScrollbarLayerChanged = false;
if (needsHorizontalScrollbarLayer) {
if (!m_layerForHorizontalScrollbar) {
m_layerForHorizontalScrollbar = createGraphicsLayer("horizontal scrollbar");
horizontalScrollbarLayerChanged = true;
}
} else if (m_layerForHorizontalScrollbar) {
willDestroyLayer(m_layerForHorizontalScrollbar.get());
m_layerForHorizontalScrollbar = nullptr;
horizontalScrollbarLayerChanged = true;
}
bool verticalScrollbarLayerChanged = false;
if (needsVerticalScrollbarLayer) {
if (!m_layerForVerticalScrollbar) {
m_layerForVerticalScrollbar = createGraphicsLayer("vertical scrollbar");
verticalScrollbarLayerChanged = true;
}
} else if (m_layerForVerticalScrollbar) {
willDestroyLayer(m_layerForVerticalScrollbar.get());
m_layerForVerticalScrollbar = nullptr;
verticalScrollbarLayerChanged = true;
}
bool scrollCornerLayerChanged = false;
if (needsScrollCornerLayer) {
if (!m_layerForScrollCorner) {
m_layerForScrollCorner = createGraphicsLayer("scroll corner");
scrollCornerLayerChanged = true;
}
} else if (m_layerForScrollCorner) {
willDestroyLayer(m_layerForScrollCorner.get());
m_layerForScrollCorner = nullptr;
scrollCornerLayerChanged = true;
}
if (ScrollingCoordinator* scrollingCoordinator = scrollingCoordinatorFromLayer(m_owningLayer)) {
if (horizontalScrollbarLayerChanged)
scrollingCoordinator->scrollableAreaScrollbarLayerDidChange(&m_owningLayer, HorizontalScrollbar);
if (verticalScrollbarLayerChanged)
scrollingCoordinator->scrollableAreaScrollbarLayerDidChange(&m_owningLayer, VerticalScrollbar);
}
return horizontalScrollbarLayerChanged || verticalScrollbarLayerChanged || scrollCornerLayerChanged;
}
void RenderLayerBacking::positionOverflowControlsLayers()
{
if (!m_owningLayer.hasScrollbars())
return;
const IntRect borderBox = toRenderBox(renderer()).pixelSnappedBorderBoxRect();
FloatSize offsetFromRenderer = m_graphicsLayer->offsetFromRenderer();
if (GraphicsLayer* layer = layerForHorizontalScrollbar()) {
IntRect hBarRect = m_owningLayer.rectForHorizontalScrollbar(borderBox);
layer->setPosition(hBarRect.location() - offsetFromRenderer);
layer->setSize(hBarRect.size());
if (layer->usesContentsLayer()) {
IntRect barRect = IntRect(IntPoint(), hBarRect.size());
layer->setContentsRect(barRect);
layer->setContentsClippingRect(barRect);
}
layer->setDrawsContent(m_owningLayer.horizontalScrollbar() && !layer->usesContentsLayer());
}
if (GraphicsLayer* layer = layerForVerticalScrollbar()) {
IntRect vBarRect = m_owningLayer.rectForVerticalScrollbar(borderBox);
layer->setPosition(vBarRect.location() - offsetFromRenderer);
layer->setSize(vBarRect.size());
if (layer->usesContentsLayer()) {
IntRect barRect = IntRect(IntPoint(), vBarRect.size());
layer->setContentsRect(barRect);
layer->setContentsClippingRect(barRect);
}
layer->setDrawsContent(m_owningLayer.verticalScrollbar() && !layer->usesContentsLayer());
}
if (GraphicsLayer* layer = layerForScrollCorner()) {
const LayoutRect& scrollCornerAndResizer = m_owningLayer.scrollCornerAndResizerRect();
layer->setPosition(scrollCornerAndResizer.location() - offsetFromRenderer);
layer->setSize(scrollCornerAndResizer.size());
layer->setDrawsContent(!scrollCornerAndResizer.isEmpty());
}
}
bool RenderLayerBacking::hasUnpositionedOverflowControlsLayers() const
{
if (GraphicsLayer* layer = layerForHorizontalScrollbar())
if (!layer->drawsContent())
return true;
if (GraphicsLayer* layer = layerForVerticalScrollbar())
if (!layer->drawsContent())
return true;
if (GraphicsLayer* layer = layerForScrollCorner())
if (!layer->drawsContent())
return true;
return false;
}
bool RenderLayerBacking::updateForegroundLayer(bool needsForegroundLayer)
{
bool layerChanged = false;
if (needsForegroundLayer) {
if (!m_foregroundLayer) {
String layerName;
#ifndef NDEBUG
layerName = m_owningLayer.name() + " (foreground)";
#endif
m_foregroundLayer = createGraphicsLayer(layerName);
m_foregroundLayer->setDrawsContent(true);
m_foregroundLayer->setPaintingPhase(GraphicsLayerPaintForeground);
layerChanged = true;
}
} else if (m_foregroundLayer) {
willDestroyLayer(m_foregroundLayer.get());
m_foregroundLayer->removeFromParent();
m_foregroundLayer = nullptr;
layerChanged = true;
}
if (layerChanged) {
m_graphicsLayer->setNeedsDisplay();
m_graphicsLayer->setPaintingPhase(paintingPhaseForPrimaryLayer());
}
return layerChanged;
}
bool RenderLayerBacking::updateBackgroundLayer(bool needsBackgroundLayer)
{
bool layerChanged = false;
if (needsBackgroundLayer) {
if (!m_backgroundLayer) {
String layerName;
#ifndef NDEBUG
layerName = m_owningLayer.name() + " (background)";
#endif
m_backgroundLayer = createGraphicsLayer(layerName);
m_backgroundLayer->setDrawsContent(true);
m_backgroundLayer->setAnchorPoint(FloatPoint3D());
m_backgroundLayer->setPaintingPhase(GraphicsLayerPaintBackground);
layerChanged = true;
}
if (!m_contentsContainmentLayer) {
String layerName;
#ifndef NDEBUG
layerName = m_owningLayer.name() + " (contents containment)";
#endif
m_contentsContainmentLayer = createGraphicsLayer(layerName);
m_contentsContainmentLayer->setAppliesPageScale(true);
m_graphicsLayer->setAppliesPageScale(false);
layerChanged = true;
}
} else {
if (m_backgroundLayer) {
willDestroyLayer(m_backgroundLayer.get());
m_backgroundLayer->removeFromParent();
m_backgroundLayer = nullptr;
layerChanged = true;
}
if (m_contentsContainmentLayer) {
willDestroyLayer(m_contentsContainmentLayer.get());
m_contentsContainmentLayer->removeFromParent();
m_contentsContainmentLayer = nullptr;
layerChanged = true;
m_graphicsLayer->setAppliesPageScale(true);
}
}
if (layerChanged) {
m_graphicsLayer->setNeedsDisplay();
// This assumes that the background layer is only used for fixed backgrounds, which is currently a correct assumption.
compositor().fixedRootBackgroundLayerChanged();
}
return layerChanged;
}
void RenderLayerBacking::updateMaskLayer(bool needsMaskLayer)
{
bool layerChanged = false;
if (needsMaskLayer) {
if (!m_maskLayer) {
m_maskLayer = createGraphicsLayer("Mask");
m_maskLayer->setDrawsContent(true);
m_maskLayer->setPaintingPhase(GraphicsLayerPaintMask);
layerChanged = true;
m_graphicsLayer->setMaskLayer(m_maskLayer.get());
}
} else if (m_maskLayer) {
m_graphicsLayer->setMaskLayer(nullptr);
willDestroyLayer(m_maskLayer.get());
m_maskLayer = nullptr;
layerChanged = true;
}
if (layerChanged)
m_graphicsLayer->setPaintingPhase(paintingPhaseForPrimaryLayer());
}
bool RenderLayerBacking::updateScrollingLayers(bool needsScrollingLayers)
{
if (needsScrollingLayers == !!m_scrollingLayer)
return false;
if (!m_scrollingLayer) {
// Outer layer which corresponds with the scroll view.
m_scrollingLayer = createGraphicsLayer("Scrolling container");
m_scrollingLayer->setDrawsContent(false);
m_scrollingLayer->setMasksToBounds(true);
#if PLATFORM(IOS)
m_scrollingLayer->setCustomBehavior(GraphicsLayer::CustomScrollingBehavior);
#endif
// Inner layer which renders the content that scrolls.
m_scrollingContentsLayer = createGraphicsLayer("Scrolled Contents");
m_scrollingContentsLayer->setDrawsContent(true);
#if PLATFORM(IOS)
m_scrollingContentsLayer->setCustomBehavior(GraphicsLayer::CustomScrolledContentsBehavior);
#endif
GraphicsLayerPaintingPhase paintPhase = GraphicsLayerPaintOverflowContents | GraphicsLayerPaintCompositedScroll;
if (!m_foregroundLayer)
paintPhase |= GraphicsLayerPaintForeground;
m_scrollingContentsLayer->setPaintingPhase(paintPhase);
m_scrollingLayer->addChild(m_scrollingContentsLayer.get());
} else {
compositor().willRemoveScrollingLayerWithBacking(m_owningLayer, *this);
willDestroyLayer(m_scrollingLayer.get());
willDestroyLayer(m_scrollingContentsLayer.get());
m_scrollingLayer = nullptr;
m_scrollingContentsLayer = nullptr;
}
m_graphicsLayer->setPaintingPhase(paintingPhaseForPrimaryLayer());
m_graphicsLayer->setNeedsDisplay(); // Because painting phases changed.
if (m_scrollingLayer)
compositor().didAddScrollingLayer(m_owningLayer);
return true;
}
void RenderLayerBacking::detachFromScrollingCoordinator()
{
if (!m_scrollingNodeID && !m_viewportConstrainedNodeID)
return;
ScrollingCoordinator* scrollingCoordinator = scrollingCoordinatorFromLayer(m_owningLayer);
if (!scrollingCoordinator)
return;
if (m_scrollingNodeID)
scrollingCoordinator->detachFromStateTree(m_scrollingNodeID);
if (m_viewportConstrainedNodeID)
scrollingCoordinator->detachFromStateTree(m_viewportConstrainedNodeID);
m_scrollingNodeID = 0;
m_viewportConstrainedNodeID = 0;
}
GraphicsLayerPaintingPhase RenderLayerBacking::paintingPhaseForPrimaryLayer() const
{
unsigned phase = 0;
if (!m_backgroundLayer)
phase |= GraphicsLayerPaintBackground;
if (!m_foregroundLayer)
phase |= GraphicsLayerPaintForeground;
if (!m_maskLayer)
phase |= GraphicsLayerPaintMask;
if (m_scrollingContentsLayer) {
phase &= ~GraphicsLayerPaintForeground;
phase |= GraphicsLayerPaintCompositedScroll;
}
return static_cast<GraphicsLayerPaintingPhase>(phase);
}
float RenderLayerBacking::compositingOpacity(float rendererOpacity) const
{
float finalOpacity = rendererOpacity;
for (RenderLayer* curr = m_owningLayer.parent(); curr; curr = curr->parent()) {
// We only care about parents that are stacking contexts.
// Recall that opacity creates stacking context.
if (!curr->isStackingContainer())
continue;
// If we found a compositing layer, we want to compute opacity
// relative to it. So we can break here.
if (curr->isComposited())
break;
finalOpacity *= curr->renderer().opacity();
}
return finalOpacity;
}
// FIXME: Code is duplicated in RenderLayer. Also, we should probably not consider filters a box decoration here.
static inline bool hasBoxDecorations(const RenderStyle& style)
{
return style.hasBorder() || style.hasBorderRadius() || style.hasOutline() || style.hasAppearance() || style.boxShadow() || style.hasFilter();
}
static bool canCreateTiledImage(const RenderStyle& style)
{
const FillLayer* fillLayer = style.backgroundLayers();
if (fillLayer->next())
return false;
if (!fillLayer->imagesAreLoaded())
return false;
if (fillLayer->attachment() != ScrollBackgroundAttachment)
return false;
Color color = style.visitedDependentColor(CSSPropertyBackgroundColor);
// FIXME: Allow color+image compositing when it makes sense.
// For now bailing out.
if (color.isValid() && color.alpha())
return false;
StyleImage* styleImage = fillLayer->image();
// FIXME: support gradients with isGeneratedImage.
if (!styleImage->isCachedImage())
return false;
Image* image = styleImage->cachedImage()->image();
if (!image->isBitmapImage())
return false;
return true;
}
static bool hasBoxDecorationsOrBackgroundImage(const RenderStyle& style)
{
if (hasBoxDecorations(style))
return true;
if (!style.hasBackgroundImage())
return false;
return !GraphicsLayer::supportsContentsTiling() || !canCreateTiledImage(style);
}
static inline bool hasPerspectiveOrPreserves3D(const RenderStyle& style)
{
return style.hasPerspective() || style.preserves3D();
}
Color RenderLayerBacking::rendererBackgroundColor() const
{
const auto& backgroundRenderer = renderer().isRoot() ? renderer().rendererForRootBackground() : renderer();
return backgroundRenderer.style().visitedDependentColor(CSSPropertyBackgroundColor);
}
void RenderLayerBacking::updateDirectlyCompositedBackgroundColor(bool isSimpleContainer, bool& didUpdateContentsRect)
{
if (!isSimpleContainer) {
m_graphicsLayer->setContentsToSolidColor(Color());
return;
}
Color backgroundColor = rendererBackgroundColor();
// An unset (invalid) color will remove the solid color.
m_graphicsLayer->setContentsToSolidColor(backgroundColor);
FloatRect contentsRect = backgroundBoxForPainting();
m_graphicsLayer->setContentsRect(contentsRect);
m_graphicsLayer->setContentsClippingRect(contentsRect);
didUpdateContentsRect = true;
}
void RenderLayerBacking::updateDirectlyCompositedBackgroundImage(bool isSimpleContainer, bool& didUpdateContentsRect)
{
if (!GraphicsLayer::supportsContentsTiling())
return;
if (isDirectlyCompositedImage())
return;
const RenderStyle& style = renderer().style();
if (!isSimpleContainer || !style.hasBackgroundImage()) {
m_graphicsLayer->setContentsToImage(0);
return;
}
FloatRect destRect = backgroundBoxForPainting();
FloatPoint phase;
FloatSize tileSize;
RefPtr<Image> image = style.backgroundLayers()->image()->cachedImage()->image();
toRenderBox(renderer()).getGeometryForBackgroundImage(&m_owningLayer.renderer(), destRect, phase, tileSize);
m_graphicsLayer->setContentsTileSize(tileSize);
m_graphicsLayer->setContentsTilePhase(phase);
m_graphicsLayer->setContentsRect(destRect);
m_graphicsLayer->setContentsClippingRect(destRect);
m_graphicsLayer->setContentsToImage(image.get());
didUpdateContentsRect = true;
}
void RenderLayerBacking::updateRootLayerConfiguration()
{
if (!m_usingTiledCacheLayer)
return;
Color backgroundColor;
bool viewIsTransparent = compositor().viewHasTransparentBackground(&backgroundColor);
if (m_backgroundLayerPaintsFixedRootBackground && m_backgroundLayer) {
m_backgroundLayer->setBackgroundColor(backgroundColor);
m_backgroundLayer->setContentsOpaque(!viewIsTransparent);
m_graphicsLayer->setBackgroundColor(Color());
m_graphicsLayer->setContentsOpaque(false);
} else {
m_graphicsLayer->setBackgroundColor(backgroundColor);
m_graphicsLayer->setContentsOpaque(!viewIsTransparent);
}
}
static bool supportsDirectBoxDecorationsComposition(const RenderLayerModelObject& renderer)
{
if (!GraphicsLayer::supportsBackgroundColorContent())
return false;
const RenderStyle& style = renderer.style();
if (renderer.hasClip())
return false;
if (hasBoxDecorationsOrBackgroundImage(style))
return false;
// FIXME: We can't create a directly composited background if this
// layer will have children that intersect with the background layer.
// A better solution might be to introduce a flattening layer if
// we do direct box decoration composition.
// https://bugs.webkit.org/show_bug.cgi?id=119461
if (hasPerspectiveOrPreserves3D(style))
return false;
// FIXME: we should be able to allow backgroundComposite; However since this is not a common use case it has been deferred for now.
if (style.backgroundComposite() != CompositeSourceOver)
return false;
if (style.backgroundClip() == TextFillBox)
return false;
return true;
}
bool RenderLayerBacking::paintsBoxDecorations() const
{
if (!m_owningLayer.hasVisibleBoxDecorations())
return false;
if (!supportsDirectBoxDecorationsComposition(renderer()))
return true;
return false;
}
bool RenderLayerBacking::paintsChildren() const
{
if (m_owningLayer.hasVisibleContent() && m_owningLayer.hasNonEmptyChildRenderers())
return true;
if (isPaintDestinationForDescendentLayers())
return true;
return false;
}
static bool isRestartedPlugin(RenderObject* renderer)
{
if (!renderer->isEmbeddedObject())
return false;
Element* element = toElement(renderer->node());
if (!element || !element->isPluginElement())
return false;
return toHTMLPlugInElement(element)->isRestartedPlugin();
}
static bool isCompositedPlugin(RenderObject* renderer)
{
return renderer->isEmbeddedObject() && toRenderEmbeddedObject(renderer)->allowsAcceleratedCompositing();
}
// A "simple container layer" is a RenderLayer which has no visible content to render.
// It may have no children, or all its children may be themselves composited.
// This is a useful optimization, because it allows us to avoid allocating backing store.
bool RenderLayerBacking::isSimpleContainerCompositingLayer() const
{
if (renderer().isRenderReplaced() && (!isCompositedPlugin(&renderer()) || isRestartedPlugin(&renderer())))
return false;
if (paintsBoxDecorations() || paintsChildren())
return false;
if (renderer().isRenderNamedFlowFragmentContainer())
return false;
if (renderer().isRenderView()) {
// Look to see if the root object has a non-simple background
RenderObject* rootObject = renderer().document().documentElement() ? renderer().document().documentElement()->renderer() : 0;
if (!rootObject)
return false;
// Reject anything that has a border, a border-radius or outline,
// or is not a simple background (no background, or solid color).
if (hasBoxDecorationsOrBackgroundImage(rootObject->style()))
return false;
// Now look at the body's renderer.
HTMLElement* body = renderer().document().body();
RenderObject* bodyObject = (body && body->hasTagName(bodyTag)) ? body->renderer() : 0;
if (!bodyObject)
return false;
if (hasBoxDecorationsOrBackgroundImage(bodyObject->style()))
return false;
}
return true;
}
static bool compositedWithOwnBackingStore(const RenderLayer* layer)
{
return layer->isComposited() && !layer->backing()->paintsIntoCompositedAncestor();
}
static bool descendentLayerPaintsIntoAncestor(RenderLayer& parent)
{
// FIXME: We shouldn't be called with a stale z-order lists. See bug 85512.
parent.updateLayerListsIfNeeded();
#if !ASSERT_DISABLED
LayerListMutationDetector mutationChecker(&parent);
#endif
if (Vector<RenderLayer*>* normalFlowList = parent.normalFlowList()) {
size_t listSize = normalFlowList->size();
for (size_t i = 0; i < listSize; ++i) {
RenderLayer* curLayer = normalFlowList->at(i);
if (!compositedWithOwnBackingStore(curLayer)
&& (curLayer->isVisuallyNonEmpty() || descendentLayerPaintsIntoAncestor(*curLayer)))
return true;
}
}
if (parent.isStackingContainer()) {
if (!parent.hasVisibleDescendant())
return false;
// Use the m_hasCompositingDescendant bit to optimize?
if (Vector<RenderLayer*>* negZOrderList = parent.negZOrderList()) {
size_t listSize = negZOrderList->size();
for (size_t i = 0; i < listSize; ++i) {
RenderLayer* curLayer = negZOrderList->at(i);
if (!compositedWithOwnBackingStore(curLayer)
&& (curLayer->isVisuallyNonEmpty() || descendentLayerPaintsIntoAncestor(*curLayer)))
return true;
}
}
if (Vector<RenderLayer*>* posZOrderList = parent.posZOrderList()) {
size_t listSize = posZOrderList->size();
for (size_t i = 0; i < listSize; ++i) {
RenderLayer* curLayer = posZOrderList->at(i);
if (!compositedWithOwnBackingStore(curLayer)
&& (curLayer->isVisuallyNonEmpty() || descendentLayerPaintsIntoAncestor(*curLayer)))
return true;
}
}
}
return false;
}
// Conservative test for having no rendered children.
bool RenderLayerBacking::isPaintDestinationForDescendentLayers() const
{
return descendentLayerPaintsIntoAncestor(m_owningLayer);
}
bool RenderLayerBacking::containsPaintedContent(bool isSimpleContainer) const
{
if (isSimpleContainer || paintsIntoWindow() || paintsIntoCompositedAncestor() || m_artificiallyInflatedBounds || m_owningLayer.isReflection())
return false;
if (isDirectlyCompositedImage())
return false;
// FIXME: we could optimize cases where the image, video or canvas is known to fill the border box entirely,
// and set background color on the layer in that case, instead of allocating backing store and painting.
#if ENABLE(VIDEO)
if (renderer().isVideo() && toRenderVideo(renderer()).shouldDisplayVideo())
return m_owningLayer.hasBoxDecorationsOrBackground();
#endif
#if ENABLE(WEBGL) || ENABLE(ACCELERATED_2D_CANVAS)
if (renderer().isCanvas() && canvasCompositingStrategy(renderer()) == CanvasAsLayerContents)
return m_owningLayer.hasBoxDecorationsOrBackground();
#endif
return true;
}
// An image can be directly compositing if it's the sole content of the layer, and has no box decorations
// that require painting. Direct compositing saves backing store.
bool RenderLayerBacking::isDirectlyCompositedImage() const
{
if (!renderer().isRenderImage() || renderer().isMedia() || m_owningLayer.hasBoxDecorationsOrBackground() || renderer().hasClip())
return false;
RenderImage& imageRenderer = toRenderImage(renderer());
if (CachedImage* cachedImage = imageRenderer.cachedImage()) {
if (!cachedImage->hasImage())
return false;
Image* image = cachedImage->imageForRenderer(&imageRenderer);
if (!image->isBitmapImage())
return false;
if (image->orientationForCurrentFrame() != DefaultImageOrientation)
return false;
return m_graphicsLayer->shouldDirectlyCompositeImage(image);
}
return false;
}
void RenderLayerBacking::contentChanged(ContentChangeType changeType)
{
if ((changeType == ImageChanged) && isDirectlyCompositedImage()) {
updateImageContents();
return;
}
if ((changeType == BackgroundImageChanged) && canCreateTiledImage(renderer().style()))
updateGeometry();
if ((changeType == MaskImageChanged) && m_maskLayer) {
// The composited layer bounds relies on box->maskClipRect(), which changes
// when the mask image becomes available.
updateAfterLayout(CompositingChildrenOnly | IsUpdateRoot);
}
#if ENABLE(WEBGL) || ENABLE(ACCELERATED_2D_CANVAS)
if ((changeType == CanvasChanged || changeType == CanvasPixelsChanged) && renderer().isCanvas() && canvasCompositingStrategy(renderer()) == CanvasAsLayerContents) {
m_graphicsLayer->setContentsNeedsDisplay();
return;
}
#endif
}
void RenderLayerBacking::updateImageContents()
{
ASSERT(renderer().isRenderImage());
RenderImage& imageRenderer = toRenderImage(renderer());
CachedImage* cachedImage = imageRenderer.cachedImage();
if (!cachedImage)
return;
Image* image = cachedImage->imageForRenderer(&imageRenderer);
if (!image)
return;
// We have to wait until the image is fully loaded before setting it on the layer.
if (!cachedImage->isLoaded())
return;
// This is a no-op if the layer doesn't have an inner layer for the image.
m_graphicsLayer->setContentsRect(snappedIntRect(contentsBox()));
LayoutRect contentsClippingRect = imageRenderer.contentBoxRect();
contentsClippingRect.move(contentOffsetInCompostingLayer());
m_graphicsLayer->setContentsClippingRect(snappedIntRect(contentsClippingRect));
m_graphicsLayer->setContentsToImage(image);
bool isSimpleContainer = false;
updateDrawsContent(isSimpleContainer);
// Image animation is "lazy", in that it automatically stops unless someone is drawing
// the image. So we have to kick the animation each time; this has the downside that the
// image will keep animating, even if its layer is not visible.
image->startAnimation();
}
FloatPoint3D RenderLayerBacking::computeTransformOriginForPainting(const LayoutRect& borderBox) const
{
const RenderStyle& style = renderer().style();
float deviceScaleFactor = this->deviceScaleFactor();
FloatPoint3D origin;
origin.setX(roundToDevicePixel(floatValueForLength(style.transformOriginX(), borderBox.width()), deviceScaleFactor));
origin.setY(roundToDevicePixel(floatValueForLength(style.transformOriginY(), borderBox.height()), deviceScaleFactor));
origin.setZ(style.transformOriginZ());
return origin;
}
// Return the offset from the top-left of this compositing layer at which the renderer's contents are painted.
LayoutSize RenderLayerBacking::contentOffsetInCompostingLayer() const
{
return LayoutSize(-m_compositedBounds.x(), -m_compositedBounds.y()) + m_devicePixelFractionFromRenderer;
}
LayoutRect RenderLayerBacking::contentsBox() const
{
if (!renderer().isBox())
return LayoutRect();
RenderBox& renderBox = toRenderBox(renderer());
LayoutRect contentsRect;
#if ENABLE(VIDEO)
if (renderBox.isVideo())
contentsRect = toRenderVideo(renderBox).videoBox();
else
#endif
if (renderBox.isRenderReplaced()) {
RenderReplaced& renderReplaced = *toRenderReplaced(&renderBox);
contentsRect = renderReplaced.replacedContentRect(renderBox.intrinsicSize());
} else
contentsRect = renderBox.contentBoxRect();
contentsRect.move(contentOffsetInCompostingLayer());
return contentsRect;
}
static LayoutRect backgroundRectForBox(const RenderBox& box)
{
switch (box.style().backgroundClip()) {
case BorderFillBox:
return box.borderBoxRect();
case PaddingFillBox:
return box.paddingBoxRect();
case ContentFillBox:
return box.contentBoxRect();
case TextFillBox:
break;
}
ASSERT_NOT_REACHED();
return LayoutRect();
}
FloatRect RenderLayerBacking::backgroundBoxForPainting() const
{
if (!renderer().isBox())
return FloatRect();
LayoutRect backgroundBox = backgroundRectForBox(toRenderBox(renderer()));
backgroundBox.move(contentOffsetInCompostingLayer());
return snapRectToDevicePixels(backgroundBox, deviceScaleFactor());
}
GraphicsLayer* RenderLayerBacking::parentForSublayers() const
{
if (m_scrollingContentsLayer)
return m_scrollingContentsLayer.get();
#if PLATFORM(IOS)
// FIXME: Can we remove this iOS-specific code path?
if (GraphicsLayer* clippingLayer = this->clippingLayer())
return clippingLayer;
return m_graphicsLayer.get();
#else
return m_childContainmentLayer ? m_childContainmentLayer.get() : m_graphicsLayer.get();
#endif
}
GraphicsLayer* RenderLayerBacking::childForSuperlayers() const
{
if (m_ancestorClippingLayer)
return m_ancestorClippingLayer.get();
if (m_contentsContainmentLayer)
return m_contentsContainmentLayer.get();
return m_graphicsLayer.get();
}
bool RenderLayerBacking::paintsIntoWindow() const
{
if (m_usingTiledCacheLayer)
return false;
if (m_owningLayer.isRootLayer()) {
#if PLATFORM(IOS) || USE(COORDINATED_GRAPHICS)
if (compositor().inForcedCompositingMode())
return false;
#endif
return compositor().rootLayerAttachment() != RenderLayerCompositor::RootLayerAttachedViaEnclosingFrame;
}
return false;
}
void RenderLayerBacking::setRequiresOwnBackingStore(bool requiresOwnBacking)
{
if (requiresOwnBacking == m_requiresOwnBackingStore)
return;
m_requiresOwnBackingStore = requiresOwnBacking;
// This affects the answer to paintsIntoCompositedAncestor(), which in turn affects
// cached clip rects, so when it changes we have to clear clip rects on descendants.
m_owningLayer.clearClipRectsIncludingDescendants(PaintingClipRects);
m_owningLayer.computeRepaintRectsIncludingDescendants();
compositor().repaintInCompositedAncestor(m_owningLayer, compositedBounds());
}
void RenderLayerBacking::setContentsNeedDisplay(GraphicsLayer::ShouldClipToLayer shouldClip)
{
ASSERT(!paintsIntoCompositedAncestor());
FrameView& frameView = owningLayer().renderer().view().frameView();
if (m_isMainFrameRenderViewLayer && frameView.isTrackingRepaints())
frameView.addTrackedRepaintRect(owningLayer().absoluteBoundingBoxForPainting());
if (m_graphicsLayer && m_graphicsLayer->drawsContent()) {
// By default, setNeedsDisplay will clip to the size of the GraphicsLayer, which does not include margin tiles.
// So if the TiledBacking has a margin that needs to be invalidated, we need to send in a rect to setNeedsDisplayInRect
// that is large enough to include the margin. TiledBacking::bounds() includes the margin.
TiledBacking* tiledBacking = this->tiledBacking();
FloatRect rectToRepaint = tiledBacking ? tiledBacking->bounds() : FloatRect(FloatPoint(0, 0), m_graphicsLayer->size());
m_graphicsLayer->setNeedsDisplayInRect(rectToRepaint, shouldClip);
}
if (m_foregroundLayer && m_foregroundLayer->drawsContent())
m_foregroundLayer->setNeedsDisplay();
if (m_backgroundLayer && m_backgroundLayer->drawsContent())
m_backgroundLayer->setNeedsDisplay();
if (m_maskLayer && m_maskLayer->drawsContent())
m_maskLayer->setNeedsDisplay();
if (m_scrollingContentsLayer && m_scrollingContentsLayer->drawsContent())
m_scrollingContentsLayer->setNeedsDisplay();
}
// r is in the coordinate space of the layer's render object
void RenderLayerBacking::setContentsNeedDisplayInRect(const LayoutRect& r, GraphicsLayer::ShouldClipToLayer shouldClip)
{
ASSERT(!paintsIntoCompositedAncestor());
FloatRect pixelSnappedRectForPainting = snapRectToDevicePixels(r, deviceScaleFactor());
FrameView& frameView = owningLayer().renderer().view().frameView();
if (m_isMainFrameRenderViewLayer && frameView.isTrackingRepaints())
frameView.addTrackedRepaintRect(pixelSnappedRectForPainting);
if (m_graphicsLayer && m_graphicsLayer->drawsContent()) {
FloatRect layerDirtyRect = pixelSnappedRectForPainting;
layerDirtyRect.move(-m_graphicsLayer->offsetFromRenderer() + m_devicePixelFractionFromRenderer);
m_graphicsLayer->setNeedsDisplayInRect(layerDirtyRect, shouldClip);
}
if (m_foregroundLayer && m_foregroundLayer->drawsContent()) {
FloatRect layerDirtyRect = pixelSnappedRectForPainting;
layerDirtyRect.move(-m_foregroundLayer->offsetFromRenderer() + m_devicePixelFractionFromRenderer);
m_foregroundLayer->setNeedsDisplayInRect(layerDirtyRect, shouldClip);
}
// FIXME: need to split out repaints for the background.
if (m_backgroundLayer && m_backgroundLayer->drawsContent()) {
FloatRect layerDirtyRect = pixelSnappedRectForPainting;
layerDirtyRect.move(-m_backgroundLayer->offsetFromRenderer() + m_devicePixelFractionFromRenderer);
m_backgroundLayer->setNeedsDisplayInRect(layerDirtyRect, shouldClip);
}
if (m_maskLayer && m_maskLayer->drawsContent()) {
FloatRect layerDirtyRect = pixelSnappedRectForPainting;
layerDirtyRect.move(-m_maskLayer->offsetFromRenderer() + m_devicePixelFractionFromRenderer);
m_maskLayer->setNeedsDisplayInRect(layerDirtyRect, shouldClip);
}
if (m_scrollingContentsLayer && m_scrollingContentsLayer->drawsContent()) {
FloatRect layerDirtyRect = pixelSnappedRectForPainting;
layerDirtyRect.move(-m_scrollingContentsLayer->offsetFromRenderer() + m_devicePixelFractionFromRenderer);
#if PLATFORM(IOS)
// Account for the fact that RenderLayerBacking::updateGeometry() bakes scrollOffset into offsetFromRenderer on iOS.
layerDirtyRect.move(-m_owningLayer.scrollOffset() + m_devicePixelFractionFromRenderer);
#endif
m_scrollingContentsLayer->setNeedsDisplayInRect(layerDirtyRect, shouldClip);
}
}
void RenderLayerBacking::paintIntoLayer(const GraphicsLayer* graphicsLayer, GraphicsContext* context,
const IntRect& paintDirtyRect, // In the coords of rootLayer.
PaintBehavior paintBehavior, GraphicsLayerPaintingPhase paintingPhase)
{
if (paintsIntoWindow() || paintsIntoCompositedAncestor()) {
#if !PLATFORM(IOS)
// FIXME: Looks like the CALayer tree is out of sync with the GraphicsLayer heirarchy
// when pages are restored from the PageCache.
// <rdar://problem/8712587> ASSERT: When Going Back to Page with Plugins in PageCache
ASSERT_NOT_REACHED();
#endif
return;
}
FontCachePurgePreventer fontCachePurgePreventer;
RenderLayer::PaintLayerFlags paintFlags = 0;
if (paintingPhase & GraphicsLayerPaintBackground)
paintFlags |= RenderLayer::PaintLayerPaintingCompositingBackgroundPhase;
if (paintingPhase & GraphicsLayerPaintForeground)
paintFlags |= RenderLayer::PaintLayerPaintingCompositingForegroundPhase;
if (paintingPhase & GraphicsLayerPaintMask)
paintFlags |= RenderLayer::PaintLayerPaintingCompositingMaskPhase;
if (paintingPhase & GraphicsLayerPaintOverflowContents)
paintFlags |= RenderLayer::PaintLayerPaintingOverflowContents;
if (paintingPhase & GraphicsLayerPaintCompositedScroll)
paintFlags |= RenderLayer::PaintLayerPaintingCompositingScrollingPhase;
if (graphicsLayer == m_backgroundLayer.get())
paintFlags |= (RenderLayer::PaintLayerPaintingRootBackgroundOnly | RenderLayer::PaintLayerPaintingCompositingForegroundPhase); // Need PaintLayerPaintingCompositingForegroundPhase to walk child layers.
else if (compositor().fixedRootBackgroundLayer())
paintFlags |= RenderLayer::PaintLayerPaintingSkipRootBackground;
#ifndef NDEBUG
RenderElement::SetLayoutNeededForbiddenScope forbidSetNeedsLayout(&m_owningLayer.renderer());
#endif
FrameView::PaintingState paintingState;
if (m_owningLayer.isRootLayer())
m_owningLayer.renderer().view().frameView().willPaintContents(context, paintDirtyRect, paintingState);
// FIXME: GraphicsLayers need a way to split for RenderRegions.
RenderLayer::LayerPaintingInfo paintingInfo(&m_owningLayer, paintDirtyRect, paintBehavior, m_devicePixelFractionFromRenderer);
m_owningLayer.paintLayerContents(context, paintingInfo, paintFlags);
if (m_owningLayer.containsDirtyOverlayScrollbars())
m_owningLayer.paintLayerContents(context, paintingInfo, paintFlags | RenderLayer::PaintLayerPaintingOverlayScrollbars);
if (m_owningLayer.isRootLayer())
m_owningLayer.renderer().view().frameView().didPaintContents(context, paintDirtyRect, paintingState);
compositor().didPaintBacking(this);
ASSERT(!m_owningLayer.m_usedTransparency);
}
static void paintScrollbar(Scrollbar* scrollbar, GraphicsContext& context, const IntRect& clip)
{
if (!scrollbar)
return;
context.save();
const IntRect& scrollbarRect = scrollbar->frameRect();
context.translate(-scrollbarRect.x(), -scrollbarRect.y());
IntRect transformedClip = clip;
transformedClip.moveBy(scrollbarRect.location());
scrollbar->paint(&context, transformedClip);
context.restore();
}
// Up-call from compositing layer drawing callback.
void RenderLayerBacking::paintContents(const GraphicsLayer* graphicsLayer, GraphicsContext& context, GraphicsLayerPaintingPhase paintingPhase, const FloatRect& clip)
{
#ifndef NDEBUG
if (Page* page = renderer().frame().page())
page->setIsPainting(true);
#endif
// The dirtyRect is in the coords of the painting root.
FloatRect adjustedClipRect = clip;
adjustedClipRect.move(-m_devicePixelFractionFromRenderer);
IntRect dirtyRect = enclosingIntRect(adjustedClipRect);
if (graphicsLayer == m_graphicsLayer.get()
|| graphicsLayer == m_foregroundLayer.get()
|| graphicsLayer == m_backgroundLayer.get()
|| graphicsLayer == m_maskLayer.get()
|| graphicsLayer == m_scrollingContentsLayer.get()) {
InspectorInstrumentation::willPaint(&renderer());
if (!(paintingPhase & GraphicsLayerPaintOverflowContents))
dirtyRect.intersect(enclosingIntRect(compositedBoundsIncludingMargin()));
// We have to use the same root as for hit testing, because both methods can compute and cache clipRects.
paintIntoLayer(graphicsLayer, &context, dirtyRect, PaintBehaviorNormal, paintingPhase);
InspectorInstrumentation::didPaint(&renderer(), dirtyRect);
} else if (graphicsLayer == layerForHorizontalScrollbar()) {
paintScrollbar(m_owningLayer.horizontalScrollbar(), context, dirtyRect);
} else if (graphicsLayer == layerForVerticalScrollbar()) {
paintScrollbar(m_owningLayer.verticalScrollbar(), context, dirtyRect);
} else if (graphicsLayer == layerForScrollCorner()) {
const LayoutRect& scrollCornerAndResizer = m_owningLayer.scrollCornerAndResizerRect();
context.save();
context.translate(-scrollCornerAndResizer.x(), -scrollCornerAndResizer.y());
LayoutRect transformedClip = LayoutRect(clip);
transformedClip.moveBy(scrollCornerAndResizer.location());
m_owningLayer.paintScrollCorner(&context, IntPoint(), snappedIntRect(transformedClip));
m_owningLayer.paintResizer(&context, IntPoint(), transformedClip);
context.restore();
}
#ifndef NDEBUG
if (Page* page = renderer().frame().page())
page->setIsPainting(false);
#endif
}
float RenderLayerBacking::pageScaleFactor() const
{
return compositor().pageScaleFactor();
}
float RenderLayerBacking::zoomedOutPageScaleFactor() const
{
return compositor().zoomedOutPageScaleFactor();
}
float RenderLayerBacking::deviceScaleFactor() const
{
return compositor().deviceScaleFactor();
}
float RenderLayerBacking::contentsScaleMultiplierForNewTiles(const GraphicsLayer* layer) const
{
return compositor().contentsScaleMultiplierForNewTiles(layer);
}
bool RenderLayerBacking::paintsOpaquelyAtNonIntegralScales(const GraphicsLayer*) const
{
return m_isMainFrameRenderViewLayer;
}
void RenderLayerBacking::didCommitChangesForLayer(const GraphicsLayer* layer) const
{
compositor().didFlushChangesForLayer(m_owningLayer, layer);
}
bool RenderLayerBacking::getCurrentTransform(const GraphicsLayer* graphicsLayer, TransformationMatrix& transform) const
{
GraphicsLayer* transformedLayer = m_contentsContainmentLayer.get() ? m_contentsContainmentLayer.get() : m_graphicsLayer.get();
if (graphicsLayer != transformedLayer)
return false;
if (m_owningLayer.hasTransform()) {
transform = m_owningLayer.currentTransform(RenderStyle::ExcludeTransformOrigin);
return true;
}
return false;
}
bool RenderLayerBacking::isTrackingRepaints() const
{
return static_cast<GraphicsLayerClient&>(compositor()).isTrackingRepaints();
}
bool RenderLayerBacking::shouldSkipLayerInDump(const GraphicsLayer* layer) const
{
// Skip the root tile cache's flattening layer.
return m_isMainFrameRenderViewLayer && layer && layer == m_childContainmentLayer.get();
}
bool RenderLayerBacking::shouldDumpPropertyForLayer(const GraphicsLayer* layer, const char* propertyName) const
{
// For backwards compatibility with WebKit1 and other platforms,
// skip some properties on the root tile cache.
if (m_isMainFrameRenderViewLayer && layer == m_graphicsLayer.get()) {
if (!strcmp(propertyName, "drawsContent"))
return false;
// Background color could be of interest to tests or other dumpers if it's non-white.
if (!strcmp(propertyName, "backgroundColor") && layer->backgroundColor() == Color::white)
return false;
// The root tile cache's repaints will show up at the top with FrameView's,
// so don't dump them twice.
if (!strcmp(propertyName, "repaintRects"))
return false;
}
return true;
}
bool RenderLayerBacking::shouldAggressivelyRetainTiles(const GraphicsLayer*) const
{
// Only the main frame TileController has enough information about in-window state to
// correctly implement aggressive tile retention.
if (!m_isMainFrameRenderViewLayer)
return false;
if (Page* page = renderer().frame().page())
return page->settings().aggressiveTileRetentionEnabled();
return false;
}
bool RenderLayerBacking::shouldTemporarilyRetainTileCohorts(const GraphicsLayer*) const
{
if (Page* page = renderer().frame().page())
return page->settings().temporaryTileCohortRetentionEnabled();
return true;
}
#ifndef NDEBUG
void RenderLayerBacking::verifyNotPainting()
{
ASSERT(!renderer().frame().page() || !renderer().frame().page()->isPainting());
}
#endif
bool RenderLayerBacking::startAnimation(double timeOffset, const Animation* anim, const KeyframeList& keyframes)
{
bool hasOpacity = keyframes.containsProperty(CSSPropertyOpacity);
bool hasTransform = renderer().isBox() && keyframes.containsProperty(CSSPropertyWebkitTransform);
bool hasFilter = keyframes.containsProperty(CSSPropertyWebkitFilter);
if (!hasOpacity && !hasTransform && !hasFilter)
return false;
KeyframeValueList transformVector(AnimatedPropertyWebkitTransform);
KeyframeValueList opacityVector(AnimatedPropertyOpacity);
KeyframeValueList filterVector(AnimatedPropertyWebkitFilter);
size_t numKeyframes = keyframes.size();
for (size_t i = 0; i < numKeyframes; ++i) {
const KeyframeValue& currentKeyframe = keyframes[i];
const RenderStyle* keyframeStyle = currentKeyframe.style();
double key = currentKeyframe.key();
if (!keyframeStyle)
continue;
TimingFunction* tf = currentKeyframe.timingFunction(keyframes.animationName());
bool isFirstOrLastKeyframe = key == 0 || key == 1;
if ((hasTransform && isFirstOrLastKeyframe) || currentKeyframe.containsProperty(CSSPropertyWebkitTransform))
transformVector.insert(TransformAnimationValue::create(key, keyframeStyle->transform(), tf));
if ((hasOpacity && isFirstOrLastKeyframe) || currentKeyframe.containsProperty(CSSPropertyOpacity))
opacityVector.insert(FloatAnimationValue::create(key, keyframeStyle->opacity(), tf));
if ((hasFilter && isFirstOrLastKeyframe) || currentKeyframe.containsProperty(CSSPropertyWebkitFilter))
filterVector.insert(FilterAnimationValue::create(key, keyframeStyle->filter(), tf));
}
if (renderer().frame().page() && !renderer().frame().page()->settings().acceleratedCompositedAnimationsEnabled())
return false;
bool didAnimate = false;
if (hasTransform && m_graphicsLayer->addAnimation(transformVector, toRenderBox(renderer()).pixelSnappedBorderBoxRect().size(), anim, keyframes.animationName(), timeOffset))
didAnimate = true;
if (hasOpacity && m_graphicsLayer->addAnimation(opacityVector, IntSize(), anim, keyframes.animationName(), timeOffset))
didAnimate = true;
if (hasFilter && m_graphicsLayer->addAnimation(filterVector, IntSize(), anim, keyframes.animationName(), timeOffset))
didAnimate = true;
return didAnimate;
}
void RenderLayerBacking::animationPaused(double timeOffset, const String& animationName)
{
m_graphicsLayer->pauseAnimation(animationName, timeOffset);
}
void RenderLayerBacking::animationFinished(const String& animationName)
{
m_graphicsLayer->removeAnimation(animationName);
}
bool RenderLayerBacking::startTransition(double timeOffset, CSSPropertyID property, const RenderStyle* fromStyle, const RenderStyle* toStyle)
{
bool didAnimate = false;
ASSERT(property != CSSPropertyInvalid);
if (property == CSSPropertyOpacity) {
const Animation* opacityAnim = toStyle->transitionForProperty(CSSPropertyOpacity);
if (opacityAnim && !opacityAnim->isEmptyOrZeroDuration()) {
KeyframeValueList opacityVector(AnimatedPropertyOpacity);
opacityVector.insert(FloatAnimationValue::create(0, compositingOpacity(fromStyle->opacity())));
opacityVector.insert(FloatAnimationValue::create(1, compositingOpacity(toStyle->opacity())));
// The boxSize param is only used for transform animations (which can only run on RenderBoxes), so we pass an empty size here.
if (m_graphicsLayer->addAnimation(opacityVector, FloatSize(), opacityAnim, GraphicsLayer::animationNameForTransition(AnimatedPropertyOpacity), timeOffset)) {
// To ensure that the correct opacity is visible when the animation ends, also set the final opacity.
updateOpacity(*toStyle);
didAnimate = true;
}
}
}
if (property == CSSPropertyWebkitTransform && m_owningLayer.hasTransform()) {
const Animation* transformAnim = toStyle->transitionForProperty(CSSPropertyWebkitTransform);
if (transformAnim && !transformAnim->isEmptyOrZeroDuration()) {
KeyframeValueList transformVector(AnimatedPropertyWebkitTransform);
transformVector.insert(TransformAnimationValue::create(0, fromStyle->transform()));
transformVector.insert(TransformAnimationValue::create(1, toStyle->transform()));
if (m_graphicsLayer->addAnimation(transformVector, toRenderBox(renderer()).pixelSnappedBorderBoxRect().size(), transformAnim, GraphicsLayer::animationNameForTransition(AnimatedPropertyWebkitTransform), timeOffset)) {
// To ensure that the correct transform is visible when the animation ends, also set the final transform.
updateTransform(*toStyle);
didAnimate = true;
}
}
}
if (property == CSSPropertyWebkitFilter && m_owningLayer.hasFilter()) {
const Animation* filterAnim = toStyle->transitionForProperty(CSSPropertyWebkitFilter);
if (filterAnim && !filterAnim->isEmptyOrZeroDuration()) {
KeyframeValueList filterVector(AnimatedPropertyWebkitFilter);
filterVector.insert(FilterAnimationValue::create(0, fromStyle->filter()));
filterVector.insert(FilterAnimationValue::create(1, toStyle->filter()));
if (m_graphicsLayer->addAnimation(filterVector, FloatSize(), filterAnim, GraphicsLayer::animationNameForTransition(AnimatedPropertyWebkitFilter), timeOffset)) {
// To ensure that the correct filter is visible when the animation ends, also set the final filter.
updateFilters(*toStyle);
didAnimate = true;
}
}
}
return didAnimate;
}
void RenderLayerBacking::transitionPaused(double timeOffset, CSSPropertyID property)
{
AnimatedPropertyID animatedProperty = cssToGraphicsLayerProperty(property);
if (animatedProperty != AnimatedPropertyInvalid)
m_graphicsLayer->pauseAnimation(GraphicsLayer::animationNameForTransition(animatedProperty), timeOffset);
}
void RenderLayerBacking::transitionFinished(CSSPropertyID property)
{
AnimatedPropertyID animatedProperty = cssToGraphicsLayerProperty(property);
if (animatedProperty != AnimatedPropertyInvalid)
m_graphicsLayer->removeAnimation(GraphicsLayer::animationNameForTransition(animatedProperty));
}
void RenderLayerBacking::notifyAnimationStarted(const GraphicsLayer*, const String&, double time)
{
renderer().animation().notifyAnimationStarted(renderer(), time);
}
void RenderLayerBacking::notifyFlushRequired(const GraphicsLayer* layer)
{
if (renderer().documentBeingDestroyed())
return;
compositor().scheduleLayerFlush(layer->canThrottleLayerFlush());
}
void RenderLayerBacking::notifyFlushBeforeDisplayRefresh(const GraphicsLayer* layer)
{
compositor().notifyFlushBeforeDisplayRefresh(layer);
}
// This is used for the 'freeze' API, for testing only.
void RenderLayerBacking::suspendAnimations(double time)
{
m_graphicsLayer->suspendAnimations(time);
}
void RenderLayerBacking::resumeAnimations()
{
m_graphicsLayer->resumeAnimations();
}
LayoutRect RenderLayerBacking::compositedBounds() const
{
return m_compositedBounds;
}
void RenderLayerBacking::setCompositedBounds(const LayoutRect& bounds)
{
m_compositedBounds = bounds;
}
LayoutRect RenderLayerBacking::compositedBoundsIncludingMargin() const
{
TiledBacking* tiledBacking = this->tiledBacking();
if (!tiledBacking || !tiledBacking->hasMargins())
return compositedBounds();
LayoutRect boundsIncludingMargin = compositedBounds();
LayoutUnit leftMarginWidth = tiledBacking->leftMarginWidth();
LayoutUnit topMarginHeight = tiledBacking->topMarginHeight();
boundsIncludingMargin.moveBy(LayoutPoint(-leftMarginWidth, -topMarginHeight));
boundsIncludingMargin.expand(leftMarginWidth + tiledBacking->rightMarginWidth(), topMarginHeight + tiledBacking->bottomMarginHeight());
return boundsIncludingMargin;
}
CSSPropertyID RenderLayerBacking::graphicsLayerToCSSProperty(AnimatedPropertyID property)
{
CSSPropertyID cssProperty = CSSPropertyInvalid;
switch (property) {
case AnimatedPropertyWebkitTransform:
cssProperty = CSSPropertyWebkitTransform;
break;
case AnimatedPropertyOpacity:
cssProperty = CSSPropertyOpacity;
break;
case AnimatedPropertyBackgroundColor:
cssProperty = CSSPropertyBackgroundColor;
break;
case AnimatedPropertyWebkitFilter:
cssProperty = CSSPropertyWebkitFilter;
break;
case AnimatedPropertyInvalid:
ASSERT_NOT_REACHED();
}
return cssProperty;
}
AnimatedPropertyID RenderLayerBacking::cssToGraphicsLayerProperty(CSSPropertyID cssProperty)
{
switch (cssProperty) {
case CSSPropertyWebkitTransform:
return AnimatedPropertyWebkitTransform;
case CSSPropertyOpacity:
return AnimatedPropertyOpacity;
case CSSPropertyBackgroundColor:
return AnimatedPropertyBackgroundColor;
case CSSPropertyWebkitFilter:
return AnimatedPropertyWebkitFilter;
default:
// It's fine if we see other css properties here; they are just not accelerated.
break;
}
return AnimatedPropertyInvalid;
}
CompositingLayerType RenderLayerBacking::compositingLayerType() const
{
if (m_graphicsLayer->usesContentsLayer())
return MediaCompositingLayer;
if (m_graphicsLayer->drawsContent())
return m_graphicsLayer->usingTiledBacking() ? TiledCompositingLayer : NormalCompositingLayer;
return ContainerCompositingLayer;
}
double RenderLayerBacking::backingStoreMemoryEstimate() const
{
double backingMemory;
// m_ancestorClippingLayer, m_contentsContainmentLayer and m_childContainmentLayer are just used for masking or containment, so have no backing.
backingMemory = m_graphicsLayer->backingStoreMemoryEstimate();
if (m_foregroundLayer)
backingMemory += m_foregroundLayer->backingStoreMemoryEstimate();
if (m_backgroundLayer)
backingMemory += m_backgroundLayer->backingStoreMemoryEstimate();
if (m_maskLayer)
backingMemory += m_maskLayer->backingStoreMemoryEstimate();
if (m_scrollingContentsLayer)
backingMemory += m_scrollingContentsLayer->backingStoreMemoryEstimate();
if (m_layerForHorizontalScrollbar)
backingMemory += m_layerForHorizontalScrollbar->backingStoreMemoryEstimate();
if (m_layerForVerticalScrollbar)
backingMemory += m_layerForVerticalScrollbar->backingStoreMemoryEstimate();
if (m_layerForScrollCorner)
backingMemory += m_layerForScrollCorner->backingStoreMemoryEstimate();
return backingMemory;
}
} // namespace WebCore
|