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
|
/*
* Copyright (C) 2009 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 COMPUTER, 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"
#if USE(ACCELERATED_COMPOSITING)
#include "RenderLayerBacking.h"
#include "AnimationController.h"
#include "CanvasRenderingContext.h"
#include "CanvasRenderingContext2D.h"
#include "CSSPropertyNames.h"
#include "CSSStyleSelector.h"
#include "FrameView.h"
#include "GraphicsContext.h"
#include "GraphicsLayer.h"
#include "HTMLCanvasElement.h"
#include "HTMLElement.h"
#include "HTMLIFrameElement.h"
#include "HTMLMediaElement.h"
#include "HTMLNames.h"
#include "InspectorInstrumentation.h"
#include "KeyframeList.h"
#include "PluginViewBase.h"
#include "RenderApplet.h"
#include "RenderBox.h"
#include "RenderIFrame.h"
#include "RenderImage.h"
#include "RenderLayerCompositor.h"
#include "RenderEmbeddedObject.h"
#include "RenderVideo.h"
#include "RenderView.h"
#include "Settings.h"
#if ENABLE(WEBGL) || ENABLE(ACCELERATED_2D_CANVAS)
#include "GraphicsContext3D.h"
#endif
using namespace std;
namespace WebCore {
using namespace HTMLNames;
static bool hasBorderOutlineOrShadow(const RenderStyle*);
static bool hasBoxDecorationsOrBackground(const RenderObject*);
static bool hasBoxDecorationsOrBackgroundImage(const RenderStyle*);
static IntRect clipBox(RenderBox* renderer);
static inline bool isAcceleratedCanvas(RenderObject* renderer)
{
#if ENABLE(WEBGL) || ENABLE(ACCELERATED_2D_CANVAS)
if (renderer->isCanvas()) {
HTMLCanvasElement* canvas = static_cast<HTMLCanvasElement*>(renderer->node());
if (CanvasRenderingContext* context = canvas->renderingContext())
return context->isAccelerated();
}
#else
UNUSED_PARAM(renderer);
#endif
return false;
}
RenderLayerBacking::RenderLayerBacking(RenderLayer* layer)
: m_owningLayer(layer)
, m_artificiallyInflatedBounds(false)
{
createGraphicsLayer();
}
RenderLayerBacking::~RenderLayerBacking()
{
updateClippingLayers(false, false);
updateOverflowControlsLayers(false, false, false);
updateForegroundLayer(false);
updateMaskLayer(false);
destroyGraphicsLayer();
}
void RenderLayerBacking::createGraphicsLayer()
{
m_graphicsLayer = GraphicsLayer::create(this);
#ifndef NDEBUG
m_graphicsLayer->setName(nameForLayer());
#endif // NDEBUG
#if USE(ACCELERATED_COMPOSITING)
ASSERT(renderer() && renderer()->document() && renderer()->document()->frame());
if (Frame* frame = renderer()->document()->frame())
m_graphicsLayer->setContentsScale(frame->pageScaleFactor());
#endif
updateLayerOpacity(renderer()->style());
updateLayerTransform(renderer()->style());
}
void RenderLayerBacking::destroyGraphicsLayer()
{
if (m_graphicsLayer)
m_graphicsLayer->removeFromParent();
m_graphicsLayer = nullptr;
m_foregroundLayer = nullptr;
m_clippingLayer = nullptr;
m_maskLayer = nullptr;
}
void RenderLayerBacking::updateLayerOpacity(const RenderStyle* style)
{
m_graphicsLayer->setOpacity(compositingOpacity(style->opacity()));
}
void RenderLayerBacking::updateLayerTransform(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()) {
style->applyTransform(t, toRenderBox(renderer())->borderBoxRect().size(), RenderStyle::ExcludeTransformOrigin);
makeMatrixRenderable(t, compositor()->canRender3DTransforms());
}
m_graphicsLayer->setTransform(t);
}
static bool hasNonZeroTransformOrigin(const RenderObject* renderer)
{
RenderStyle* style = renderer->style();
return (style->transformOriginX().type() == Fixed && style->transformOriginX().value())
|| (style->transformOriginY().type() == Fixed && style->transformOriginY().value());
}
static bool layerOrAncestorIsTransformed(RenderLayer* layer)
{
for (RenderLayer* curr = layer; curr; curr = curr->parent()) {
if (curr->hasTransform())
return true;
}
return false;
}
#if ENABLE(FULLSCREEN_API)
static bool layerOrAncestorIsFullScreen(RenderLayer* layer)
{
// Don't traverse through the render layer tree if we do not yet have a full screen renderer.
if (!layer->renderer()->document()->fullScreenRenderer())
return false;
for (RenderLayer* curr = layer; curr; curr = curr->parent()) {
if (curr->renderer()->isRenderFullScreen())
return true;
}
return false;
}
#endif
void RenderLayerBacking::updateCompositedBounds()
{
IntRect layerBounds = compositor()->calculateCompositedBounds(m_owningLayer, m_owningLayer);
// 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 this
// is a fullscreen renderer, don't clip to the viewport, as the renderer will be asked to
// display outside of the viewport bounds.
if (compositor()->compositingConsultsOverlap() && !layerOrAncestorIsTransformed(m_owningLayer)
#if ENABLE(FULLSCREEN_API)
&& !layerOrAncestorIsFullScreen(m_owningLayer)
#endif
) {
RenderView* view = m_owningLayer->renderer()->view();
RenderLayer* rootLayer = view->layer();
// Start by clipping to the view's bounds.
IntRect clippingBounds = view->layoutOverflowRect();
if (m_owningLayer != rootLayer)
clippingBounds.intersect(m_owningLayer->backgroundClipRect(rootLayer, true));
int deltaX = 0;
int deltaY = 0;
m_owningLayer->convertToLayerCoords(rootLayer, deltaX, deltaY);
clippingBounds.move(-deltaX, -deltaY);
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()->isRenderPart()) {
if (RenderLayerCompositor* innerCompositor = RenderLayerCompositor::frameContentsCompositor(toRenderPart(renderer()))) {
innerCompositor->frameViewDidChangeSize();
innerCompositor->frameViewDidChangeLocation(contentsBox().location());
}
}
}
void RenderLayerBacking::updateAfterLayout(UpdateDepth updateDepth, bool isUpdateRoot)
{
RenderLayerCompositor* layerCompositor = compositor();
if (!layerCompositor->compositingLayersNeedRebuild()) {
// Calling updateGraphicsLayerGeometry() 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();
layerCompositor->updateCompositingDescendantGeometry(m_owningLayer, m_owningLayer, updateDepth);
if (isUpdateRoot) {
updateGraphicsLayerGeometry();
layerCompositor->updateRootLayerPosition();
}
}
}
bool RenderLayerBacking::updateGraphicsLayerConfiguration()
{
RenderLayerCompositor* compositor = this->compositor();
RenderObject* renderer = this->renderer();
bool layerConfigChanged = false;
if (updateForegroundLayer(compositor->needsContentsCompositingLayer(m_owningLayer)))
layerConfigChanged = true;
if (updateClippingLayers(compositor->clippedByAncestor(m_owningLayer), compositor->clipsCompositingDescendants(m_owningLayer)))
layerConfigChanged = true;
if (updateOverflowControlsLayers(requiresHorizontalScrollbarLayer(), requiresVerticalScrollbarLayer(), requiresScrollCornerLayer()))
layerConfigChanged = true;
if (layerConfigChanged)
updateInternalHierarchy();
if (updateMaskLayer(renderer->hasMask()))
m_graphicsLayer->setMaskLayer(m_maskLayer.get());
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 (isDirectlyCompositedImage())
updateImageContents();
if ((renderer->isEmbeddedObject() && toRenderEmbeddedObject(renderer)->allowsAcceleratedCompositing())
|| (renderer->isApplet() && toRenderApplet(renderer)->allowsAcceleratedCompositing())) {
PluginViewBase* pluginViewBase = static_cast<PluginViewBase*>(toRenderWidget(renderer)->widget());
m_graphicsLayer->setContentsToMedia(pluginViewBase->platformLayer());
}
#if ENABLE(VIDEO)
else if (renderer->isVideo()) {
HTMLMediaElement* mediaElement = static_cast<HTMLMediaElement*>(renderer->node());
m_graphicsLayer->setContentsToMedia(mediaElement->platformLayer());
}
#endif
#if ENABLE(WEBGL) || ENABLE(ACCELERATED_2D_CANVAS)
else if (isAcceleratedCanvas(renderer)) {
HTMLCanvasElement* canvas = static_cast<HTMLCanvasElement*>(renderer->node());
if (CanvasRenderingContext* context = canvas->renderingContext())
m_graphicsLayer->setContentsToCanvas(context->platformLayer());
layerConfigChanged = true;
}
#endif
if (renderer->isRenderPart())
layerConfigChanged = RenderLayerCompositor::parentFrameContentLayers(toRenderPart(renderer));
return layerConfigChanged;
}
static IntRect clipBox(RenderBox* renderer)
{
IntRect result = PaintInfo::infiniteRect();
if (renderer->hasOverflowClip())
result = renderer->overflowClipRect(0, 0);
if (renderer->hasClip())
result.intersect(renderer->clipRect(0, 0));
return result;
}
void RenderLayerBacking::updateGraphicsLayerGeometry()
{
// If we haven't built z-order lists yet, wait until later.
if (m_owningLayer->isStackingContext() && m_owningLayer->m_zOrderListsDirty)
return;
// 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))
updateLayerTransform(renderer()->style());
// Set opacity, if it is not animating.
if (!renderer()->animation()->isRunningAcceleratedAnimationOnRenderer(renderer(), CSSPropertyOpacity))
updateLayerOpacity(renderer()->style());
RenderStyle* style = renderer()->style();
m_graphicsLayer->setPreserves3D(style->transformStyle3D() == TransformStyle3DPreserve3D && !renderer()->hasReflection());
m_graphicsLayer->setBackfaceVisibility(style->backfaceVisibility() == BackfaceVisibilityVisible);
RenderLayer* compAncestor = m_owningLayer->ancestorCompositingLayer();
// We compute everything relative to the enclosing compositing layer.
IntRect ancestorCompositingBounds;
if (compAncestor) {
ASSERT(compAncestor->backing());
ancestorCompositingBounds = compAncestor->backing()->compositedBounds();
}
IntRect localCompositingBounds = compositedBounds();
IntRect relativeCompositingBounds(localCompositingBounds);
int deltaX = 0, deltaY = 0;
m_owningLayer->convertToLayerCoords(compAncestor, deltaX, deltaY);
relativeCompositingBounds.move(deltaX, deltaY);
IntPoint 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.
IntRect clippingBox = clipBox(toRenderBox(compAncestor->renderer()));
graphicsLayerParentLocation = clippingBox.location();
} else
graphicsLayerParentLocation = ancestorCompositingBounds.location();
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.
IntRect parentClipRect = m_owningLayer->backgroundClipRect(compAncestor, true);
ASSERT(parentClipRect != PaintInfo::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.
IntSize rendererOffset(parentClipRect.location().x() - deltaX, parentClipRect.location().y() - deltaY);
m_ancestorClippingLayer->setOffsetFromRenderer(rendererOffset);
// The primary layer is then parented in, and positioned relative to this clipping layer.
graphicsLayerParentLocation = parentClipRect.location();
}
m_graphicsLayer->setPosition(FloatPoint() + (relativeCompositingBounds.location() - graphicsLayerParentLocation));
IntSize oldOffsetFromRenderer = m_graphicsLayer->offsetFromRenderer();
m_graphicsLayer->setOffsetFromRenderer(localCompositingBounds.location() - IntPoint());
// If the compositing layer offset changes, we need to repaint.
if (oldOffsetFromRenderer != m_graphicsLayer->offsetFromRenderer())
m_graphicsLayer->setNeedsDisplay();
FloatSize oldSize = m_graphicsLayer->size();
FloatSize newSize = relativeCompositingBounds.size();
if (oldSize != newSize) {
m_graphicsLayer->setSize(newSize);
// A bounds change will almost always require redisplay. Usually that redisplay
// will happen because of a repaint elsewhere, but not always:
// e.g. see RenderView::setMaximalOutlineSize()
m_graphicsLayer->setNeedsDisplay();
}
// If we have a layer that clips children, position it.
IntRect clippingBox;
if (m_clippingLayer) {
clippingBox = clipBox(toRenderBox(renderer()));
m_clippingLayer->setPosition(FloatPoint() + (clippingBox.location() - localCompositingBounds.location()));
m_clippingLayer->setSize(clippingBox.size());
m_clippingLayer->setOffsetFromRenderer(clippingBox.location() - IntPoint());
}
if (m_maskLayer) {
if (m_maskLayer->size() != m_graphicsLayer->size()) {
m_maskLayer->setSize(m_graphicsLayer->size());
m_maskLayer->setNeedsDisplay();
}
m_maskLayer->setPosition(FloatPoint());
m_maskLayer->setOffsetFromRenderer(m_graphicsLayer->offsetFromRenderer());
}
if (m_owningLayer->hasTransform()) {
const IntRect borderBox = toRenderBox(renderer())->borderBoxRect();
// Get layout bounds in the coords of compAncestor to match relativeCompositingBounds.
IntRect layerBounds = IntRect(deltaX, deltaY, borderBox.width(), borderBox.height());
// Update properties that depend on layer dimensions
FloatPoint3D transformOrigin = computeTransformOrigin(borderBox);
// Compute the anchor point, which is in the center of the renderer box unless transform-origin is set.
FloatPoint3D anchor(relativeCompositingBounds.width() != 0.0f ? ((layerBounds.x() - relativeCompositingBounds.x()) + transformOrigin.x()) / relativeCompositingBounds.width() : 0.5f,
relativeCompositingBounds.height() != 0.0f ? ((layerBounds.y() - relativeCompositingBounds.y()) + transformOrigin.y()) / relativeCompositingBounds.height() : 0.5f,
transformOrigin.z());
m_graphicsLayer->setAnchorPoint(anchor);
RenderStyle* style = renderer()->style();
if (style->hasPerspective()) {
TransformationMatrix t = owningLayer()->perspectiveTransform();
if (m_clippingLayer) {
m_clippingLayer->setChildrenTransform(t);
m_graphicsLayer->setChildrenTransform(TransformationMatrix());
}
else
m_graphicsLayer->setChildrenTransform(t);
} else {
if (m_clippingLayer)
m_clippingLayer->setChildrenTransform(TransformationMatrix());
else
m_graphicsLayer->setChildrenTransform(TransformationMatrix());
}
} else {
m_graphicsLayer->setAnchorPoint(FloatPoint3D(0.5f, 0.5f, 0));
}
if (m_foregroundLayer) {
FloatPoint foregroundPosition;
FloatSize foregroundSize = newSize;
IntSize foregroundOffset = m_graphicsLayer->offsetFromRenderer();
if (m_clippingLayer) {
// 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 = clippingBox.location() - IntPoint();
}
m_foregroundLayer->setPosition(foregroundPosition);
m_foregroundLayer->setSize(foregroundSize);
m_foregroundLayer->setOffsetFromRenderer(foregroundOffset);
}
if (m_owningLayer->reflectionLayer() && m_owningLayer->reflectionLayer()->isComposited()) {
RenderLayerBacking* reflectionBacking = m_owningLayer->reflectionLayer()->backing();
reflectionBacking->updateGraphicsLayerGeometry();
// 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()));
}
m_graphicsLayer->setContentsRect(contentsBox());
updateDrawsContent();
updateAfterWidgetResize();
}
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();
m_graphicsLayer->removeFromParent();
m_ancestorClippingLayer->addChild(m_graphicsLayer.get());
}
if (m_clippingLayer) {
m_clippingLayer->removeFromParent();
m_graphicsLayer->addChild(m_clippingLayer.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::updateDrawsContent()
{
m_graphicsLayer->setDrawsContent(containsPaintedContent());
}
// Return true if the layers changed.
bool RenderLayerBacking::updateClippingLayers(bool needsAncestorClip, bool needsDescendantClip)
{
bool layersChanged = false;
if (needsAncestorClip) {
if (!m_ancestorClippingLayer) {
m_ancestorClippingLayer = GraphicsLayer::create(this);
#ifndef NDEBUG
m_ancestorClippingLayer->setName("Ancestor clipping Layer");
#endif
m_ancestorClippingLayer->setMasksToBounds(true);
layersChanged = true;
}
} else if (m_ancestorClippingLayer) {
m_ancestorClippingLayer->removeFromParent();
m_ancestorClippingLayer = nullptr;
layersChanged = true;
}
if (needsDescendantClip) {
if (!m_clippingLayer) {
m_clippingLayer = GraphicsLayer::create(this);
#ifndef NDEBUG
m_clippingLayer->setName("Child clipping Layer");
#endif
m_clippingLayer->setMasksToBounds(true);
layersChanged = true;
}
} else if (m_clippingLayer) {
m_clippingLayer->removeFromParent();
m_clippingLayer = nullptr;
layersChanged = true;
}
return layersChanged;
}
bool RenderLayerBacking::requiresHorizontalScrollbarLayer() const
{
#if !PLATFORM(CHROMIUM)
if (!m_owningLayer->hasOverlayScrollbars())
return false;
#endif
return m_owningLayer->horizontalScrollbar();
}
bool RenderLayerBacking::requiresVerticalScrollbarLayer() const
{
#if !PLATFORM(CHROMIUM)
if (!m_owningLayer->hasOverlayScrollbars())
return false;
#endif
return m_owningLayer->verticalScrollbar();
}
bool RenderLayerBacking::requiresScrollCornerLayer() const
{
#if !PLATFORM(CHROMIUM)
if (!m_owningLayer->hasOverlayScrollbars())
return false;
#endif
return !m_owningLayer->scrollCornerAndResizerRect().isEmpty();
}
bool RenderLayerBacking::updateOverflowControlsLayers(bool needsHorizontalScrollbarLayer, bool needsVerticalScrollbarLayer, bool needsScrollCornerLayer)
{
bool layersChanged = false;
if (needsHorizontalScrollbarLayer) {
if (!m_layerForHorizontalScrollbar) {
m_layerForHorizontalScrollbar = GraphicsLayer::create(this);
#ifndef NDEBUG
m_layerForHorizontalScrollbar ->setName("horizontal scrollbar");
#endif
layersChanged = true;
}
} else if (m_layerForHorizontalScrollbar) {
m_layerForHorizontalScrollbar.clear();
layersChanged = true;
}
if (needsVerticalScrollbarLayer) {
if (!m_layerForVerticalScrollbar) {
m_layerForVerticalScrollbar = GraphicsLayer::create(this);
#ifndef NDEBUG
m_layerForVerticalScrollbar->setName("vertical scrollbar");
#endif
layersChanged = true;
}
} else if (m_layerForVerticalScrollbar) {
m_layerForVerticalScrollbar.clear();
layersChanged = true;
}
if (needsScrollCornerLayer) {
if (!m_layerForScrollCorner) {
m_layerForScrollCorner = GraphicsLayer::create(this);
#ifndef NDEBUG
m_layerForScrollCorner->setName("scroll corner");
#endif
layersChanged = true;
}
} else if (m_layerForScrollCorner) {
m_layerForScrollCorner.clear();
layersChanged = true;
}
return layersChanged;
}
bool RenderLayerBacking::updateForegroundLayer(bool needsForegroundLayer)
{
bool layerChanged = false;
if (needsForegroundLayer) {
if (!m_foregroundLayer) {
m_foregroundLayer = GraphicsLayer::create(this);
#ifndef NDEBUG
m_foregroundLayer->setName(nameForLayer() + " (foreground)");
#endif
m_foregroundLayer->setDrawsContent(true);
m_foregroundLayer->setPaintingPhase(GraphicsLayerPaintForeground);
if (Frame* frame = renderer()->document()->frame())
m_foregroundLayer->setContentsScale(frame->pageScaleFactor());
layerChanged = true;
}
} else if (m_foregroundLayer) {
m_foregroundLayer->removeFromParent();
m_foregroundLayer = nullptr;
layerChanged = true;
}
if (layerChanged)
m_graphicsLayer->setPaintingPhase(paintingPhaseForPrimaryLayer());
return layerChanged;
}
bool RenderLayerBacking::updateMaskLayer(bool needsMaskLayer)
{
bool layerChanged = false;
if (needsMaskLayer) {
if (!m_maskLayer) {
m_maskLayer = GraphicsLayer::create(this);
#ifndef NDEBUG
m_maskLayer->setName("Mask");
#endif
m_maskLayer->setDrawsContent(true);
m_maskLayer->setPaintingPhase(GraphicsLayerPaintMask);
if (Frame* frame = renderer()->document()->frame())
m_maskLayer->setContentsScale(frame->pageScaleFactor());
layerChanged = true;
}
} else if (m_maskLayer) {
m_maskLayer = nullptr;
layerChanged = true;
}
if (layerChanged)
m_graphicsLayer->setPaintingPhase(paintingPhaseForPrimaryLayer());
return layerChanged;
}
GraphicsLayerPaintingPhase RenderLayerBacking::paintingPhaseForPrimaryLayer() const
{
unsigned phase = GraphicsLayerPaintBackground;
if (!m_foregroundLayer)
phase |= GraphicsLayerPaintForeground;
if (!m_maskLayer)
phase |= GraphicsLayerPaintMask;
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->isStackingContext())
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;
}
static bool hasBorderOutlineOrShadow(const RenderStyle* style)
{
return style->hasBorder() || style->hasBorderRadius() || style->hasOutline() || style->hasAppearance() || style->boxShadow();
}
static bool hasBoxDecorationsOrBackground(const RenderObject* renderer)
{
return hasBorderOutlineOrShadow(renderer->style()) || renderer->hasBackground();
}
static bool hasBoxDecorationsOrBackgroundImage(const RenderStyle* style)
{
return hasBorderOutlineOrShadow(style) || style->hasBackgroundImage();
}
bool RenderLayerBacking::rendererHasBackground() const
{
// FIXME: share more code here
if (renderer()->node() && renderer()->node()->isDocumentNode()) {
RenderObject* htmlObject = renderer()->firstChild();
if (!htmlObject)
return false;
if (htmlObject->hasBackground())
return true;
RenderObject* bodyObject = htmlObject->firstChild();
if (!bodyObject)
return false;
return bodyObject->hasBackground();
}
return renderer()->hasBackground();
}
const Color RenderLayerBacking::rendererBackgroundColor() const
{
// FIXME: share more code here
if (renderer()->node() && renderer()->node()->isDocumentNode()) {
RenderObject* htmlObject = renderer()->firstChild();
if (htmlObject->hasBackground())
return htmlObject->style()->visitedDependentColor(CSSPropertyBackgroundColor);
RenderObject* bodyObject = htmlObject->firstChild();
return bodyObject->style()->visitedDependentColor(CSSPropertyBackgroundColor);
}
return renderer()->style()->visitedDependentColor(CSSPropertyBackgroundColor);
}
// 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
{
RenderObject* renderObject = renderer();
if (renderObject->isReplaced() || // replaced objects are not containers
renderObject->hasMask()) // masks require special treatment
return false;
RenderStyle* style = renderObject->style();
// Reject anything that has a border, a border-radius or outline,
// or any background (color or image).
// FIXME: we could optimize layers for simple backgrounds.
if (hasBoxDecorationsOrBackground(renderObject))
return false;
if (m_owningLayer->hasOverflowControls())
return false;
// If we have got this far and the renderer has no children, then we're ok.
if (!renderObject->firstChild())
return true;
if (renderObject->node() && renderObject->node()->isDocumentNode()) {
// Look to see if the root object has a non-simple backgound
RenderObject* rootObject = renderObject->document()->documentElement()->renderer();
if (!rootObject)
return false;
style = rootObject->style();
// Reject anything that has a border, a border-radius or outline,
// or is not a simple background (no background, or solid color).
if (hasBoxDecorationsOrBackgroundImage(style))
return false;
// Now look at the body's renderer.
HTMLElement* body = renderObject->document()->body();
RenderObject* bodyObject = (body && body->hasLocalName(bodyTag)) ? body->renderer() : 0;
if (!bodyObject)
return false;
style = bodyObject->style();
if (hasBoxDecorationsOrBackgroundImage(style))
return false;
// Check to see if all the body's children are compositing layers.
if (hasNonCompositingDescendants())
return false;
return true;
}
// Check to see if all the renderer's children are compositing layers.
if (hasNonCompositingDescendants())
return false;
return true;
}
// Conservative test for having no rendered children.
bool RenderLayerBacking::hasNonCompositingDescendants() const
{
// Some HTML can cause whitespace text nodes to have renderers, like:
// <div>
// <img src=...>
// </div>
// so test for 0x0 RenderTexts here
for (RenderObject* child = renderer()->firstChild(); child; child = child->nextSibling()) {
if (!child->hasLayer()) {
if (child->isRenderInline() || !child->isBox())
return true;
if (toRenderBox(child)->width() > 0 || toRenderBox(child)->height() > 0)
return true;
}
}
if (m_owningLayer->isStackingContext()) {
// Use the m_hasCompositingDescendant bit to optimize?
if (Vector<RenderLayer*>* negZOrderList = m_owningLayer->negZOrderList()) {
size_t listSize = negZOrderList->size();
for (size_t i = 0; i < listSize; ++i) {
RenderLayer* curLayer = negZOrderList->at(i);
if (!curLayer->isComposited())
return true;
}
}
if (Vector<RenderLayer*>* posZOrderList = m_owningLayer->posZOrderList()) {
size_t listSize = posZOrderList->size();
for (size_t i = 0; i < listSize; ++i) {
RenderLayer* curLayer = posZOrderList->at(i);
if (!curLayer->isComposited())
return true;
}
}
}
if (Vector<RenderLayer*>* normalFlowList = m_owningLayer->normalFlowList()) {
size_t listSize = normalFlowList->size();
for (size_t i = 0; i < listSize; ++i) {
RenderLayer* curLayer = normalFlowList->at(i);
if (!curLayer->isComposited())
return true;
}
}
return false;
}
bool RenderLayerBacking::containsPaintedContent() const
{
if (isSimpleContainerCompositingLayer() || paintingGoesToWindow() || 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 hasBoxDecorationsOrBackground(renderer());
#endif
#if PLATFORM(MAC) && USE(CA) && !defined(BUILDING_ON_LEOPARD) && !defined(BUILDING_ON_SNOW_LEOPARD)
#elif ENABLE(WEBGL) || ENABLE(ACCELERATED_2D_CANVAS)
if (isAcceleratedCanvas(renderer()))
return hasBoxDecorationsOrBackground(renderer());
#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
{
RenderObject* renderObject = renderer();
if (!renderObject->isImage() || hasBoxDecorationsOrBackground(renderObject) || renderObject->hasClip())
return false;
RenderImage* imageRenderer = toRenderImage(renderObject);
if (CachedImage* cachedImage = imageRenderer->cachedImage()) {
if (cachedImage->hasImage())
return cachedImage->image()->isBitmapImage();
}
return false;
}
void RenderLayerBacking::contentChanged(RenderLayer::ContentChangeType changeType)
{
if ((changeType == RenderLayer::ImageChanged) && isDirectlyCompositedImage()) {
updateImageContents();
return;
}
if ((changeType == RenderLayer::MaskImageChanged) && m_maskLayer) {
// The composited layer bounds relies on box->maskClipRect(), which changes
// when the mask image becomes available.
bool isUpdateRoot = true;
updateAfterLayout(CompositingChildren, isUpdateRoot);
}
#if ENABLE(WEBGL) || ENABLE(ACCELERATED_2D_CANVAS)
if ((changeType == RenderLayer::CanvasChanged) && isAcceleratedCanvas(renderer())) {
m_graphicsLayer->setContentsNeedsDisplay();
return;
}
#endif
}
void RenderLayerBacking::updateImageContents()
{
ASSERT(renderer()->isImage());
RenderImage* imageRenderer = toRenderImage(renderer());
CachedImage* cachedImage = imageRenderer->cachedImage();
if (!cachedImage)
return;
Image* image = cachedImage->image();
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->setContentsToImage(image);
updateDrawsContent();
// 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::computeTransformOrigin(const IntRect& borderBox) const
{
RenderStyle* style = renderer()->style();
FloatPoint3D origin;
origin.setX(style->transformOriginX().calcFloatValue(borderBox.width()));
origin.setY(style->transformOriginY().calcFloatValue(borderBox.height()));
origin.setZ(style->transformOriginZ());
return origin;
}
FloatPoint RenderLayerBacking::computePerspectiveOrigin(const IntRect& borderBox) const
{
RenderStyle* style = renderer()->style();
float boxWidth = borderBox.width();
float boxHeight = borderBox.height();
FloatPoint origin;
origin.setX(style->perspectiveOriginX().calcFloatValue(boxWidth));
origin.setY(style->perspectiveOriginY().calcFloatValue(boxHeight));
return origin;
}
// Return the offset from the top-left of this compositing layer at which the renderer's contents are painted.
IntSize RenderLayerBacking::contentOffsetInCompostingLayer() const
{
return IntSize(-m_compositedBounds.x(), -m_compositedBounds.y());
}
IntRect RenderLayerBacking::contentsBox() const
{
if (!renderer()->isBox())
return IntRect();
IntRect contentsRect;
#if ENABLE(VIDEO)
if (renderer()->isVideo()) {
RenderVideo* videoRenderer = toRenderVideo(renderer());
contentsRect = videoRenderer->videoBox();
} else
#endif
contentsRect = toRenderBox(renderer())->contentBoxRect();
IntSize contentOffset = contentOffsetInCompostingLayer();
contentsRect.move(contentOffset);
return contentsRect;
}
bool RenderLayerBacking::paintingGoesToWindow() const
{
if (m_owningLayer->isRootLayer())
return compositor()->rootLayerAttachment() != RenderLayerCompositor::RootLayerAttachedViaEnclosingFrame;
return false;
}
void RenderLayerBacking::setContentsNeedDisplay()
{
if (m_graphicsLayer && m_graphicsLayer->drawsContent())
m_graphicsLayer->setNeedsDisplay();
if (m_foregroundLayer && m_foregroundLayer->drawsContent())
m_foregroundLayer->setNeedsDisplay();
if (m_maskLayer && m_maskLayer->drawsContent())
m_maskLayer->setNeedsDisplay();
}
// r is in the coordinate space of the layer's render object
void RenderLayerBacking::setContentsNeedDisplayInRect(const IntRect& r)
{
if (m_graphicsLayer && m_graphicsLayer->drawsContent()) {
IntRect layerDirtyRect = r;
layerDirtyRect.move(-m_graphicsLayer->offsetFromRenderer());
m_graphicsLayer->setNeedsDisplayInRect(layerDirtyRect);
}
if (m_foregroundLayer && m_foregroundLayer->drawsContent()) {
IntRect layerDirtyRect = r;
layerDirtyRect.move(-m_foregroundLayer->offsetFromRenderer());
m_foregroundLayer->setNeedsDisplayInRect(layerDirtyRect);
}
if (m_maskLayer && m_maskLayer->drawsContent()) {
IntRect layerDirtyRect = r;
layerDirtyRect.move(-m_maskLayer->offsetFromRenderer());
m_maskLayer->setNeedsDisplayInRect(layerDirtyRect);
}
}
static void setClip(GraphicsContext* p, const IntRect& paintDirtyRect, const IntRect& clipRect)
{
if (paintDirtyRect == clipRect)
return;
p->save();
p->clip(clipRect);
}
static void restoreClip(GraphicsContext* p, const IntRect& paintDirtyRect, const IntRect& clipRect)
{
if (paintDirtyRect == clipRect)
return;
p->restore();
}
// Share this with RenderLayer::paintLayer, which would have to be educated about GraphicsLayerPaintingPhase?
void RenderLayerBacking::paintIntoLayer(RenderLayer* rootLayer, GraphicsContext* context,
const IntRect& paintDirtyRect, // in the coords of rootLayer
PaintBehavior paintBehavior, GraphicsLayerPaintingPhase paintingPhase,
RenderObject* paintingRoot)
{
if (paintingGoesToWindow()) {
ASSERT_NOT_REACHED();
return;
}
m_owningLayer->updateLayerListsIfNeeded();
// Calculate the clip rects we should use.
IntRect layerBounds, damageRect, clipRectToApply, outlineRect;
m_owningLayer->calculateRects(rootLayer, paintDirtyRect, layerBounds, damageRect, clipRectToApply, outlineRect);
int x = layerBounds.x(); // layerBounds is computed relative to rootLayer
int y = layerBounds.y();
int tx = x - m_owningLayer->renderBoxX();
int ty = y - m_owningLayer->renderBoxY();
// If this layer's renderer is a child of the paintingRoot, we render unconditionally, which
// is done by passing a nil paintingRoot down to our renderer (as if no paintingRoot was ever set).
// Else, our renderer tree may or may not contain the painting root, so we pass that root along
// so it will be tested against as we decend through the renderers.
RenderObject *paintingRootForRenderer = 0;
if (paintingRoot && !renderer()->isDescendantOf(paintingRoot))
paintingRootForRenderer = paintingRoot;
bool shouldPaint = (m_owningLayer->hasVisibleContent() || m_owningLayer->hasVisibleDescendant()) && m_owningLayer->isSelfPaintingLayer();
if (shouldPaint && (paintingPhase & GraphicsLayerPaintBackground)) {
// Paint our background first, before painting any child layers.
// Establish the clip used to paint our background.
setClip(context, paintDirtyRect, damageRect);
PaintInfo info(context, damageRect, PaintPhaseBlockBackground, false, paintingRootForRenderer, 0);
renderer()->paint(info, tx, ty);
// Our scrollbar widgets paint exactly when we tell them to, so that they work properly with
// z-index. We paint after we painted the background/border, so that the scrollbars will
// sit above the background/border.
m_owningLayer->paintOverflowControls(context, x, y, damageRect);
// Restore the clip.
restoreClip(context, paintDirtyRect, damageRect);
// Now walk the sorted list of children with negative z-indices. Only RenderLayers without compositing layers will paint.
m_owningLayer->paintList(m_owningLayer->negZOrderList(), rootLayer, context, paintDirtyRect, paintBehavior, paintingRoot, 0, 0);
}
bool forceBlackText = paintBehavior & PaintBehaviorForceBlackText;
bool selectionOnly = paintBehavior & PaintBehaviorSelectionOnly;
if (shouldPaint && (paintingPhase & GraphicsLayerPaintForeground)) {
// Set up the clip used when painting our children.
setClip(context, paintDirtyRect, clipRectToApply);
PaintInfo paintInfo(context, clipRectToApply,
selectionOnly ? PaintPhaseSelection : PaintPhaseChildBlockBackgrounds,
forceBlackText, paintingRootForRenderer, 0);
renderer()->paint(paintInfo, tx, ty);
if (!selectionOnly) {
paintInfo.phase = PaintPhaseFloat;
renderer()->paint(paintInfo, tx, ty);
paintInfo.phase = PaintPhaseForeground;
renderer()->paint(paintInfo, tx, ty);
paintInfo.phase = PaintPhaseChildOutlines;
renderer()->paint(paintInfo, tx, ty);
}
// Now restore our clip.
restoreClip(context, paintDirtyRect, clipRectToApply);
if (!outlineRect.isEmpty()) {
// Paint our own outline
PaintInfo paintInfo(context, outlineRect, PaintPhaseSelfOutline, false, paintingRootForRenderer, 0);
setClip(context, paintDirtyRect, outlineRect);
renderer()->paint(paintInfo, tx, ty);
restoreClip(context, paintDirtyRect, outlineRect);
}
// Paint any child layers that have overflow.
m_owningLayer->paintList(m_owningLayer->normalFlowList(), rootLayer, context, paintDirtyRect, paintBehavior, paintingRoot, 0, 0);
// Now walk the sorted list of children with positive z-indices.
m_owningLayer->paintList(m_owningLayer->posZOrderList(), rootLayer, context, paintDirtyRect, paintBehavior, paintingRoot, 0, 0);
}
if (shouldPaint && (paintingPhase & GraphicsLayerPaintMask)) {
if (renderer()->hasMask() && !selectionOnly && !damageRect.isEmpty()) {
setClip(context, paintDirtyRect, damageRect);
// Paint the mask.
PaintInfo paintInfo(context, damageRect, PaintPhaseMask, false, paintingRootForRenderer, 0);
renderer()->paint(paintInfo, tx, ty);
// Restore the clip.
restoreClip(context, paintDirtyRect, damageRect);
}
}
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.move(scrollbarRect.x(), scrollbarRect.y());
scrollbar->paint(&context, transformedClip);
context.restore();
}
// Up-call from compositing layer drawing callback.
void RenderLayerBacking::paintContents(const GraphicsLayer* graphicsLayer, GraphicsContext& context, GraphicsLayerPaintingPhase paintingPhase, const IntRect& clip)
{
if (graphicsLayer == m_graphicsLayer.get() || graphicsLayer == m_foregroundLayer.get() || graphicsLayer == m_maskLayer.get()) {
InspectorInstrumentationCookie cookie = InspectorInstrumentation::willPaint(m_owningLayer->renderer()->frame(), clip);
IntSize offset = graphicsLayer->offsetFromRenderer();
context.translate(-offset);
IntRect clipRect(clip);
clipRect.move(offset);
// The dirtyRect is in the coords of the painting root.
IntRect dirtyRect = compositedBounds();
dirtyRect.intersect(clipRect);
// We have to use the same root as for hit testing, because both methods can compute and cache clipRects.
paintIntoLayer(m_owningLayer, &context, dirtyRect, PaintBehaviorNormal, paintingPhase, renderer());
InspectorInstrumentation::didPaint(cookie);
} else if (graphicsLayer == layerForHorizontalScrollbar()) {
paintScrollbar(m_owningLayer->horizontalScrollbar(), context, clip);
} else if (graphicsLayer == layerForVerticalScrollbar()) {
paintScrollbar(m_owningLayer->verticalScrollbar(), context, clip);
} else if (graphicsLayer == layerForScrollCorner()) {
const IntRect& scrollCornerAndResizer = m_owningLayer->scrollCornerAndResizerRect();
context.save();
context.translate(-scrollCornerAndResizer.x(), -scrollCornerAndResizer.y());
IntRect transformedClip = clip;
transformedClip.move(scrollCornerAndResizer.x(), scrollCornerAndResizer.y());
m_owningLayer->paintScrollCorner(&context, 0, 0, transformedClip);
m_owningLayer->paintResizer(&context, 0, 0, transformedClip);
context.restore();
}
}
bool RenderLayerBacking::showDebugBorders() const
{
return compositor() ? compositor()->compositorShowDebugBorders() : false;
}
bool RenderLayerBacking::showRepaintCounter() const
{
return compositor() ? compositor()->compositorShowRepaintCounter() : false;
}
bool RenderLayerBacking::startAnimation(double timeOffset, const Animation* anim, const KeyframeList& keyframes)
{
bool hasOpacity = keyframes.containsProperty(CSSPropertyOpacity);
bool hasTransform = renderer()->isBox() && keyframes.containsProperty(CSSPropertyWebkitTransform);
if (!hasOpacity && !hasTransform)
return false;
KeyframeValueList transformVector(AnimatedPropertyWebkitTransform);
KeyframeValueList opacityVector(AnimatedPropertyOpacity);
size_t numKeyframes = keyframes.size();
for (size_t i = 0; i < numKeyframes; ++i) {
const KeyframeValue& currentKeyframe = keyframes[i];
const RenderStyle* keyframeStyle = currentKeyframe.style();
float key = currentKeyframe.key();
if (!keyframeStyle)
continue;
// Get timing function.
RefPtr<TimingFunction> tf = keyframeStyle->hasAnimations() ? (*keyframeStyle->animations()).animation(0)->timingFunction() : 0;
bool isFirstOrLastKeyframe = key == 0 || key == 1;
if ((hasTransform && isFirstOrLastKeyframe) || currentKeyframe.containsProperty(CSSPropertyWebkitTransform))
transformVector.insert(new TransformAnimationValue(key, &(keyframeStyle->transform()), tf));
if ((hasOpacity && isFirstOrLastKeyframe) || currentKeyframe.containsProperty(CSSPropertyOpacity))
opacityVector.insert(new FloatAnimationValue(key, keyframeStyle->opacity(), tf));
}
bool didAnimateTransform = false;
bool didAnimateOpacity = false;
if (hasTransform && m_graphicsLayer->addAnimation(transformVector, toRenderBox(renderer())->borderBoxRect().size(), anim, keyframes.animationName(), timeOffset)) {
didAnimateTransform = true;
compositor()->didStartAcceleratedAnimation(CSSPropertyWebkitTransform);
}
if (hasOpacity && m_graphicsLayer->addAnimation(opacityVector, IntSize(), anim, keyframes.animationName(), timeOffset)) {
didAnimateOpacity = true;
compositor()->didStartAcceleratedAnimation(CSSPropertyOpacity);
}
return didAnimateTransform || didAnimateOpacity;
}
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, int property, const RenderStyle* fromStyle, const RenderStyle* toStyle)
{
bool didAnimateOpacity = false;
bool didAnimateTransform = false;
ASSERT(property != cAnimateAll);
if (property == (int)CSSPropertyOpacity) {
const Animation* opacityAnim = toStyle->transitionForProperty(CSSPropertyOpacity);
if (opacityAnim && !opacityAnim->isEmptyOrZeroDuration()) {
KeyframeValueList opacityVector(AnimatedPropertyOpacity);
opacityVector.insert(new FloatAnimationValue(0, compositingOpacity(fromStyle->opacity())));
opacityVector.insert(new FloatAnimationValue(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, IntSize(), opacityAnim, GraphicsLayer::animationNameForTransition(AnimatedPropertyOpacity), timeOffset)) {
// To ensure that the correct opacity is visible when the animation ends, also set the final opacity.
updateLayerOpacity(toStyle);
didAnimateOpacity = true;
}
}
}
if (property == (int)CSSPropertyWebkitTransform && m_owningLayer->hasTransform()) {
const Animation* transformAnim = toStyle->transitionForProperty(CSSPropertyWebkitTransform);
if (transformAnim && !transformAnim->isEmptyOrZeroDuration()) {
KeyframeValueList transformVector(AnimatedPropertyWebkitTransform);
transformVector.insert(new TransformAnimationValue(0, &fromStyle->transform()));
transformVector.insert(new TransformAnimationValue(1, &toStyle->transform()));
if (m_graphicsLayer->addAnimation(transformVector, toRenderBox(renderer())->borderBoxRect().size(), transformAnim, GraphicsLayer::animationNameForTransition(AnimatedPropertyWebkitTransform), timeOffset)) {
// To ensure that the correct transform is visible when the animation ends, also set the final opacity.
updateLayerTransform(toStyle);
didAnimateTransform = true;
}
}
}
if (didAnimateOpacity)
compositor()->didStartAcceleratedAnimation(CSSPropertyOpacity);
if (didAnimateTransform)
compositor()->didStartAcceleratedAnimation(CSSPropertyWebkitTransform);
return didAnimateOpacity || didAnimateTransform;
}
void RenderLayerBacking::transitionPaused(double timeOffset, int property)
{
AnimatedPropertyID animatedProperty = cssToGraphicsLayerProperty(property);
if (animatedProperty != AnimatedPropertyInvalid)
m_graphicsLayer->pauseAnimation(GraphicsLayer::animationNameForTransition(animatedProperty), timeOffset);
}
void RenderLayerBacking::transitionFinished(int property)
{
AnimatedPropertyID animatedProperty = cssToGraphicsLayerProperty(property);
if (animatedProperty != AnimatedPropertyInvalid)
m_graphicsLayer->removeAnimation(GraphicsLayer::animationNameForTransition(animatedProperty));
}
void RenderLayerBacking::notifyAnimationStarted(const GraphicsLayer*, double time)
{
renderer()->animation()->notifyAnimationStarted(renderer(), time);
}
void RenderLayerBacking::notifySyncRequired(const GraphicsLayer*)
{
if (!renderer()->documentBeingDestroyed())
compositor()->scheduleLayerFlush();
}
// 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();
}
IntRect RenderLayerBacking::compositedBounds() const
{
return m_compositedBounds;
}
void RenderLayerBacking::setCompositedBounds(const IntRect& bounds)
{
m_compositedBounds = bounds;
}
int RenderLayerBacking::graphicsLayerToCSSProperty(AnimatedPropertyID property)
{
int cssProperty = CSSPropertyInvalid;
switch (property) {
case AnimatedPropertyWebkitTransform:
cssProperty = CSSPropertyWebkitTransform;
break;
case AnimatedPropertyOpacity:
cssProperty = CSSPropertyOpacity;
break;
case AnimatedPropertyBackgroundColor:
cssProperty = CSSPropertyBackgroundColor;
break;
case AnimatedPropertyInvalid:
ASSERT_NOT_REACHED();
}
return cssProperty;
}
AnimatedPropertyID RenderLayerBacking::cssToGraphicsLayerProperty(int cssProperty)
{
switch (cssProperty) {
case CSSPropertyWebkitTransform:
return AnimatedPropertyWebkitTransform;
case CSSPropertyOpacity:
return AnimatedPropertyOpacity;
case CSSPropertyBackgroundColor:
return AnimatedPropertyBackgroundColor;
// It's fine if we see other css properties here; they are just not accelerated.
}
return AnimatedPropertyInvalid;
}
#ifndef NDEBUG
String RenderLayerBacking::nameForLayer() const
{
String name = renderer()->renderName();
if (Node* node = renderer()->node()) {
if (node->isElementNode())
name += " " + static_cast<Element*>(node)->tagName();
if (node->hasID())
name += " \'" + static_cast<Element*>(node)->getIdAttribute() + "\'";
}
if (m_owningLayer->isReflection())
name += " (reflection)";
return name;
}
#endif
CompositingLayerType RenderLayerBacking::compositingLayerType() const
{
if (m_graphicsLayer->hasContentsLayer())
return MediaCompositingLayer;
if (m_graphicsLayer->drawsContent())
return m_graphicsLayer->usingTiledLayer() ? TiledCompositingLayer : NormalCompositingLayer;
return ContainerCompositingLayer;
}
void RenderLayerBacking::updateContentsScale(float scale)
{
if (m_graphicsLayer)
m_graphicsLayer->setContentsScale(scale);
if (m_foregroundLayer)
m_foregroundLayer->setContentsScale(scale);
if (m_maskLayer)
m_maskLayer->setContentsScale(scale);
}
} // namespace WebCore
#endif // USE(ACCELERATED_COMPOSITING)
|