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
|
/*
* Copyright (C) 1999 Lars Knoll (knoll@kde.org)
* (C) 1999 Antti Koivisto (koivisto@kde.org)
* (C) 2005 Allan Sandfeld Jensen (kde@carewolf.com)
* (C) 2005, 2006 Samuel Weinig (sam.weinig@gmail.com)
* Copyright (C) 2005-2022 Apple Inc. All rights reserved.
* Copyright (C) 2010-2013 Google Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*/
#include "config.h"
#include "BackgroundPainter.h"
#include "BorderPainter.h"
#include "BorderShape.h"
#include "CachedImage.h"
#include "ColorBlending.h"
#include "FloatRoundedRect.h"
#include "GeometryUtilities.h"
#include "GraphicsContext.h"
#include "InlineIteratorInlineBox.h"
#include "PaintInfo.h"
#include "RenderBoxModelObjectInlines.h"
#include "RenderElementInlines.h"
#include "RenderImage.h"
#include "RenderLayer.h"
#include "RenderLayerBacking.h"
#include "RenderObjectInlines.h"
#include "RenderTableCell.h"
#include "RenderView.h"
#include "TextBoxPainter.h"
namespace WebCore {
BackgroundImageGeometry::BackgroundImageGeometry(const LayoutRect& destinationRect, const LayoutSize& tileSizeWithoutPixelSnapping, const LayoutSize& tileSize, const LayoutSize& phase, const LayoutSize& spaceSize, bool fixedAttachment)
: destinationRect(destinationRect)
, destinationOrigin(destinationRect.location())
, tileSizeWithoutPixelSnapping(tileSizeWithoutPixelSnapping)
, tileSize(tileSize)
, phase(phase)
, spaceSize(spaceSize)
, hasNonLocalGeometry(fixedAttachment)
{
}
BackgroundPainter::BackgroundPainter(RenderBoxModelObject& renderer, const PaintInfo& paintInfo)
: m_renderer(renderer)
, m_paintInfo(paintInfo)
{
// background-clip has no effect when painting the root background.
// https://www.w3.org/TR/css-backgrounds-3/#background-clip
if (m_renderer.isDocumentElementRenderer())
setOverrideClip(FillBox::BorderBox);
}
void BackgroundPainter::paintBackground(const LayoutRect& paintRect, BleedAvoidance bleedAvoidance) const
{
if (m_renderer.isDocumentElementRenderer()) {
paintRootBoxFillLayers();
return;
}
if (!paintsOwnBackground(m_renderer))
return;
if (m_renderer.backgroundIsKnownToBeObscured(paintRect.location()) && !boxShadowShouldBeAppliedToBackground(m_renderer, paintRect.location(), bleedAvoidance, { }))
return;
auto backgroundColor = m_renderer.style().visitedDependentColorWithColorFilter(CSSPropertyBackgroundColor);
auto compositeOp = document().compositeOperatorForBackgroundColor(backgroundColor, m_renderer);
paintFillLayers(backgroundColor, m_renderer.style().backgroundLayers(), paintRect, bleedAvoidance, compositeOp);
}
void BackgroundPainter::paintRootBoxFillLayers() const
{
ASSERT(m_renderer.isDocumentElementRenderer());
if (m_paintInfo.skipRootBackground())
return;
auto* rootBackgroundRenderer = view().rendererForRootBackground();
if (!rootBackgroundRenderer)
return;
auto& style = rootBackgroundRenderer->style();
auto backgroundColor = style.visitedDependentColorWithColorFilter(CSSPropertyBackgroundColor);
auto compositeOp = document().compositeOperatorForBackgroundColor(backgroundColor, m_renderer);
paintFillLayers(backgroundColor, style.backgroundLayers(), view().backgroundRect(), BleedAvoidance::None, compositeOp, rootBackgroundRenderer);
}
bool BackgroundPainter::paintsOwnBackground(const RenderBoxModelObject& renderer)
{
if (!renderer.isBody())
return true;
if (renderer.shouldApplyAnyContainment())
return true;
// The <body> only paints its background if the root element has defined a background independent of the body,
// or if the <body>'s parent is not the document element's renderer (e.g. inside SVG foreignObject).
auto documentElementRenderer = renderer.document().documentElement()->renderer();
return !documentElementRenderer || documentElementRenderer->shouldApplyAnyContainment() || documentElementRenderer->hasBackground() || documentElementRenderer != renderer.parent();
}
void BackgroundPainter::paintFillLayers(const Color& color, const FillLayer& fillLayer, const LayoutRect& rect, BleedAvoidance bleedAvoidance, CompositeOperator op, RenderElement* backgroundObject) const
{
Vector<const FillLayer*, 8> layers;
bool shouldDrawBackgroundInSeparateBuffer = false;
for (auto* layer = &fillLayer; layer; layer = layer->next()) {
layers.append(layer);
if (layer->blendMode() != BlendMode::Normal)
shouldDrawBackgroundInSeparateBuffer = true;
// Stop traversal when an opaque layer is encountered.
// FIXME: It would be possible for the following occlusion culling test to be more aggressive
// on layers with no repeat by testing whether the image covers the layout rect.
// Testing that here would imply duplicating a lot of calculations that are currently done in
// BackgroundPainter::paintFillLayer. A more efficient solution might be to move
// the layer recursion into paintFillLayer, or to compute the layer geometry here
// and pass it down.
// The clipOccludesNextLayers condition must be evaluated first to avoid short-circuiting.
if (layer->clipOccludesNextLayers(layer == &fillLayer) && layer->hasOpaqueImage(m_renderer) && layer->image()->canRender(&m_renderer, m_renderer.style().usedZoom()) && layer->hasRepeatXY() && layer->blendMode() == BlendMode::Normal && !boxShadowShouldBeAppliedToBackground(m_renderer, rect.location(), bleedAvoidance, { }))
break;
}
auto& context = m_paintInfo.context();
auto baseBgColorUsage = BaseBackgroundColorUse;
if (shouldDrawBackgroundInSeparateBuffer) {
paintFillLayer(color, *layers.last(), rect, bleedAvoidance, { }, { }, op, backgroundObject, BaseBackgroundColorOnly);
baseBgColorUsage = BaseBackgroundColorSkip;
context.beginTransparencyLayer(1);
}
for (auto& layer : makeReversedRange(layers))
paintFillLayer(color, *layer, rect, bleedAvoidance, { }, { }, op, backgroundObject, baseBgColorUsage);
if (shouldDrawBackgroundInSeparateBuffer)
context.endTransparencyLayer();
}
static void applyBoxShadowForBackground(GraphicsContext& context, const RenderStyle& style)
{
const ShadowData* boxShadow = style.boxShadow();
while (boxShadow->style() != ShadowStyle::Normal)
boxShadow = boxShadow->next();
FloatSize shadowOffset(boxShadow->x().value, boxShadow->y().value);
context.setDropShadow({ shadowOffset, boxShadow->radius().value, style.colorWithColorFilter(boxShadow->color()), boxShadow->isWebkitBoxShadow() ? ShadowRadiusMode::Legacy : ShadowRadiusMode::Default });
}
void BackgroundPainter::paintFillLayer(const Color& color, const FillLayer& bgLayer, const LayoutRect& rect,
BleedAvoidance bleedAvoidance, const InlineIterator::InlineBoxIterator& inlineBoxIterator, const LayoutRect& backgroundImageStrip, CompositeOperator op, RenderElement* backgroundObject, BaseBackgroundColorUsage baseBgColorUsage) const
{
GraphicsContext& context = m_paintInfo.context();
if ((context.paintingDisabled() && !context.detectingContentfulPaint()) || rect.isEmpty())
return;
auto closedEdges = inlineBoxIterator
? inlineBoxIterator->closedEdges()
: RectEdges<bool> { true };
auto& style = m_renderer.style();
auto layerClip = m_overrideClip.value_or(bgLayer.clip());
bool hasRoundedBorder = style.hasBorderRadius()
&& (closedEdges.start(style.writingMode()) || closedEdges.end(style.writingMode()));
bool clippedWithLocalScrolling = m_renderer.hasNonVisibleOverflow() && bgLayer.attachment() == FillAttachment::LocalBackground;
bool isBorderFill = layerClip == FillBox::BorderBox;
bool isRoot = m_renderer.isDocumentElementRenderer();
Color bgColor = color;
StyleImage* bgImage = bgLayer.image();
bool shouldPaintBackgroundImage = bgImage && bgImage->canRender(&m_renderer, style.usedZoom());
if (context.detectingContentfulPaint()) {
if (!context.contentfulPaintDetected() && shouldPaintBackgroundImage && bgImage->cachedImage()) {
if (style.backgroundSizeType() != FillSizeType::Size || !style.backgroundSizeLength().isEmpty())
context.setContentfulPaintDetected();
return;
}
}
if (context.invalidatingImagesWithAsyncDecodes()) {
if (shouldPaintBackgroundImage && bgImage->cachedImage()->isClientWaitingForAsyncDecoding(m_renderer))
bgImage->cachedImage()->removeAllClientsWaitingForAsyncDecoding();
return;
}
bool forceBackgroundToWhite = false;
if (document().printing()) {
if (style.printColorAdjust() == PrintColorAdjust::Economy)
forceBackgroundToWhite = true;
if (document().settings().shouldPrintBackgrounds())
forceBackgroundToWhite = false;
}
// When printing backgrounds is disabled or using economy mode,
// change existing background colors and images to a solid white background.
// If there's no bg color or image, leave it untouched to avoid affecting transparency.
// We don't try to avoid loading the background images, because this style flag is only set
// when printing, and at that point we've already loaded the background images anyway. (To avoid
// loading the background images we'd have to do this check when applying styles rather than
// while rendering.)
if (forceBackgroundToWhite) {
// Note that we can't reuse this variable below because the bgColor might be changed
bool shouldPaintBackgroundColor = !bgLayer.next() && bgColor.isVisible();
if (shouldPaintBackgroundImage || shouldPaintBackgroundColor) {
bgColor = Color::white;
shouldPaintBackgroundImage = false;
}
if (layerClip == FillBox::Text)
layerClip = FillBox::BorderBox;
}
bool baseBgColorOnly = (baseBgColorUsage == BaseBackgroundColorOnly);
if (baseBgColorOnly && (!isRoot || bgLayer.next() || bgColor.isOpaque()))
return;
bool colorVisible = bgColor.isVisible();
float deviceScaleFactor = document().deviceScaleFactor();
FloatRect pixelSnappedRect = snapRectToDevicePixels(rect, deviceScaleFactor);
auto borderShapeRespectingBleedAvoidance = [&](RectEdges<bool> closedEdges, bool shrinkForBleedAvoidance = true) {
auto borderRect = rect;
if (shrinkForBleedAvoidance && bleedAvoidance == BleedAvoidance::ShrinkBackground) {
// Ideally we'd use the border rect, but add a device pixel of additional inset to preserve corner shape.
borderRect = shrinkRectByOneDevicePixel(m_paintInfo.context(), borderRect, deviceScaleFactor);
}
return BorderShape::shapeForBorderRect(style, borderRect, closedEdges);
};
// Fast path for drawing simple color backgrounds.
if (!isRoot && !clippedWithLocalScrolling && !shouldPaintBackgroundImage && isBorderFill && !bgLayer.next()) {
if (!colorVisible)
return;
bool applyBoxShadowToBackground = boxShadowShouldBeAppliedToBackground(m_renderer, rect.location(), bleedAvoidance, inlineBoxIterator);
GraphicsContextStateSaver shadowStateSaver(context, applyBoxShadowToBackground);
if (applyBoxShadowToBackground)
applyBoxShadowForBackground(context, style);
if (hasRoundedBorder && bleedAvoidance != BleedAvoidance::UseTransparencyLayer) {
auto borderShape = borderShapeRespectingBleedAvoidance(closedEdges);
auto previousOperator = context.compositeOperation();
bool saveRestoreCompositeOp = op != previousOperator;
if (saveRestoreCompositeOp)
context.setCompositeOperation(op);
if (bleedAvoidance == BleedAvoidance::BackgroundOverBorder)
borderShape.fillInnerShape(context, bgColor, deviceScaleFactor);
else
borderShape.fillOuterShape(context, bgColor, deviceScaleFactor);
if (saveRestoreCompositeOp)
context.setCompositeOperation(previousOperator);
} else
context.fillRect(pixelSnappedRect, bgColor, op);
return;
}
auto clipForBorder = [&]() -> std::optional<FillBox> {
if (!hasRoundedBorder)
return { };
// FillBox::BorderBox radius clipping is taken care of by BleedAvoidance::UseTransparencyLayer
if (layerClip == FillBox::BorderBox && bleedAvoidance == BleedAvoidance::UseTransparencyLayer)
return { };
if (bleedAvoidance == BleedAvoidance::BackgroundOverBorder)
return FillBox::PaddingBox;
return layerClip;
}();
GraphicsContextStateSaver clipToBorderStateSaver(context, clipForBorder.has_value());
if (clipForBorder) {
switch (*clipForBorder) {
case FillBox::BorderBox:
case FillBox::BorderArea:
case FillBox::Text:
case FillBox::NoClip: {
auto borderShape = borderShapeRespectingBleedAvoidance(closedEdges, isBorderFill);
borderShape.clipToOuterShape(context, deviceScaleFactor);
break;
}
case FillBox::PaddingBox: {
auto borderShape = borderShapeRespectingBleedAvoidance(closedEdges, isBorderFill);
borderShape.clipToInnerShape(context, deviceScaleFactor);
break;
}
case FillBox::ContentBox: {
auto borderShape = m_renderer.borderShapeForContentClipping(rect);
borderShape.clipToInnerShape(context, deviceScaleFactor);
break;
}
}
}
LayoutUnit bLeft = closedEdges.left() ? m_renderer.borderLeft() : 0_lu;
LayoutUnit bRight = closedEdges.right() ? m_renderer.borderRight() : 0_lu;
LayoutUnit pLeft = closedEdges.left() ? m_renderer.paddingLeft() : 0_lu;
LayoutUnit pRight = closedEdges.right() ? m_renderer.paddingRight() : 0_lu;
GraphicsContextStateSaver clipWithScrollingStateSaver(context, clippedWithLocalScrolling);
LayoutRect scrolledPaintRect = rect;
if (clippedWithLocalScrolling) {
// Clip to the overflow area.
auto& renderBox = downcast<RenderBox>(m_renderer);
context.clip(renderBox.overflowClipRect(rect.location()));
// Adjust the paint rect to reflect a scrolled content box with borders at the ends.
scrolledPaintRect.moveBy(-renderBox.scrollPosition());
scrolledPaintRect.setWidth(bLeft + renderBox.layer()->scrollWidth() + bRight);
scrolledPaintRect.setHeight(renderBox.borderTop() + renderBox.layer()->scrollHeight() + renderBox.borderBottom());
}
GraphicsContextStateSaver backgroundClipStateSaver(context, false);
auto backgroundClipOuterLayerScope = TransparencyLayerScope(context, 1, false);
auto backgroundClipInnerLayerScope = TransparencyLayerScope(context, 1, false);
auto setupMaskingBackgroundClip = [&](const LayoutRect& borderRect, const std::function<void(GraphicsContext& context, const LayoutRect&, const FloatRect&)>& paintFunction) {
auto transparencyLayerBounds = snapRectToDevicePixels(rect, deviceScaleFactor);
transparencyLayerBounds.intersect(snapRectToDevicePixels(m_paintInfo.rect, deviceScaleFactor));
transparencyLayerBounds.inflate(1);
backgroundClipStateSaver.save();
context.clip(transparencyLayerBounds);
backgroundClipOuterLayerScope.beginLayer(1);
if (context.renderingMode() == RenderingMode::PDFDocument) {
// CG doesn't support some compositing operations when printing, so clip using an imageBuffer instead.
static constexpr auto printBufferResolution = 3.0f; // Chosen to result in output that is not too blurry.
RefPtr maskImage = context.createImageBuffer(transparencyLayerBounds.size(), printBufferResolution);
if (!maskImage)
return;
auto& maskContext = maskImage->context();
maskContext.translate(-transparencyLayerBounds.location());
paintFunction(maskContext, borderRect, transparencyLayerBounds);
context.clipToImageBuffer(*maskImage, transparencyLayerBounds);
} else {
paintFunction(context, borderRect, transparencyLayerBounds);
context.setCompositeOperation(CompositeOperator::SourceIn);
}
backgroundClipInnerLayerScope.beginLayer(1);
context.setCompositeOperation(CompositeOperator::SourceOver);
};
switch (layerClip) {
case FillBox::BorderBox:
break;
case FillBox::PaddingBox:
case FillBox::ContentBox: {
// Clip to the padding or content boxes as necessary.
if (!clipForBorder) {
bool includePadding = layerClip == FillBox::ContentBox;
LayoutRect clipRect = LayoutRect(scrolledPaintRect.x() + bLeft + (includePadding ? pLeft : 0_lu),
scrolledPaintRect.y() + m_renderer.borderTop() + (includePadding ? m_renderer.paddingTop() : 0_lu),
scrolledPaintRect.width() - bLeft - bRight - (includePadding ? pLeft + pRight : 0_lu),
scrolledPaintRect.height() - m_renderer.borderTop() - m_renderer.borderBottom() - (includePadding ? m_renderer.paddingTop() + m_renderer.paddingBottom() : 0_lu));
backgroundClipStateSaver.save();
context.clip(clipRect);
}
break;
}
case FillBox::Text: {
// We have to draw our text into a mask that can then be used to clip background drawing.
// First figure out how big the mask has to be. It should be no bigger than what we need
// to actually render, so we should intersect the dirty rect with the border box of the background.
setupMaskingBackgroundClip(rect, [&](GraphicsContext& context, const LayoutRect&, const FloatRect& paintRect) {
m_renderer.paintMaskForTextFillBox(context, paintRect, inlineBoxIterator, scrolledPaintRect);
});
break;
}
case FillBox::BorderArea: {
auto borderAreaPath = BorderPainter::pathForBorderArea(rect, style, deviceScaleFactor, closedEdges);
if (borderAreaPath) {
backgroundClipStateSaver.save();
context.clipPath(borderAreaPath.value());
break;
}
setupMaskingBackgroundClip(rect, [&](GraphicsContext& context, const LayoutRect& borderRect, const FloatRect& paintRect) {
auto borderPaintInfo = PaintInfo { context, LayoutRect { paintRect }, PaintPhase::BlockBackground, PaintBehavior::ForceBlackBorder };
auto borderPainter = BorderPainter { m_renderer, borderPaintInfo };
borderPainter.paintBorder(borderRect, style);
});
break;
}
case FillBox::NoClip:
break;
}
auto isOpaqueRoot = false;
if (isRoot) {
bool shouldPaintBaseBackground = view().rootElementShouldPaintBaseBackground();
isOpaqueRoot = bgLayer.next() || bgColor.isOpaque() || shouldPaintBaseBackground;
if (!shouldPaintBaseBackground)
baseBgColorUsage = BaseBackgroundColorSkip;
view().frameView().setContentIsOpaque(isOpaqueRoot);
}
// Paint the color first underneath all images, culled if background image occludes it.
// FIXME: In the bgLayer.hasFiniteBounds() case, we could improve the culling test
// by verifying whether the background image covers the entire layout rect.
if (!bgLayer.next()) {
LayoutRect backgroundRect(scrolledPaintRect);
bool applyBoxShadowToBackground = boxShadowShouldBeAppliedToBackground(m_renderer, rect.location(), bleedAvoidance, inlineBoxIterator);
if (applyBoxShadowToBackground || !shouldPaintBackgroundImage || !bgLayer.hasOpaqueImage(m_renderer) || !bgLayer.hasRepeatXY() || bgLayer.isEmpty()) {
if (!applyBoxShadowToBackground)
backgroundRect.intersect(m_paintInfo.rect);
// If we have an alpha and we are painting the root element, blend with the base background color.
Color baseColor;
bool shouldClearBackground = false;
if ((baseBgColorUsage != BaseBackgroundColorSkip) && isOpaqueRoot) {
baseColor = view().frameView().baseBackgroundColor();
if (!baseColor.isVisible())
shouldClearBackground = true;
}
GraphicsContextStateSaver shadowStateSaver(context, applyBoxShadowToBackground);
if (applyBoxShadowToBackground)
applyBoxShadowForBackground(context, style);
FloatRect backgroundRectForPainting = snapRectToDevicePixels(backgroundRect, deviceScaleFactor);
if (baseColor.isVisible()) {
if (!baseBgColorOnly && bgColor.isVisible())
baseColor = blendSourceOver(baseColor, bgColor);
context.fillRect(backgroundRectForPainting, baseColor, CompositeOperator::Copy);
} else if (!baseBgColorOnly && bgColor.isVisible()) {
auto operation = context.compositeOperation();
if (shouldClearBackground) {
if (op == CompositeOperator::DestinationOut) // We're punching out the background.
operation = op;
else
operation = CompositeOperator::Copy;
}
context.fillRect(backgroundRectForPainting, bgColor, operation);
} else if (shouldClearBackground)
context.clearRect(backgroundRectForPainting);
}
}
// no progressive loading of the background image
if (!baseBgColorOnly && shouldPaintBackgroundImage) {
// Multiline inline boxes paint like the image was one long strip spanning lines. The backgroundImageStrip is this fictional rectangle.
auto imageRect = backgroundImageStrip.isEmpty() ? scrolledPaintRect : backgroundImageStrip;
auto paintOffset = backgroundImageStrip.isEmpty() ? rect.location() : backgroundImageStrip.location();
auto geometry = calculateBackgroundImageGeometry(m_renderer, m_paintInfo.paintContainer, bgLayer, paintOffset, imageRect, m_overrideOrigin);
auto& clientForBackgroundImage = backgroundObject ? *backgroundObject : m_renderer;
bgImage->setContainerContextForRenderer(clientForBackgroundImage, geometry.tileSizeWithoutPixelSnapping, m_renderer.style().usedZoom());
geometry.clip(LayoutRect(pixelSnappedRect));
RefPtr<Image> image;
bool isFirstLine = inlineBoxIterator && inlineBoxIterator->lineBox()->isFirst();
if (!geometry.destinationRect.isEmpty() && (image = bgImage->image(backgroundObject ? backgroundObject : &m_renderer, geometry.tileSize, isFirstLine))) {
context.setDrawLuminanceMask(bgLayer.maskMode() == MaskMode::Luminance);
// FIXME: <http://webkit.org/b/288163> Allow HDR display for background images when CSS HDR images are able to set GraphicsLayer::drawHDRContent.
auto headroom = Headroom::None;
ImagePaintingOptions options = {
op == CompositeOperator::SourceOver ? bgLayer.compositeForPainting() : op,
bgLayer.blendMode(),
m_renderer.decodingModeForImageDraw(*image, m_paintInfo),
ImageOrientation::Orientation::FromImage,
m_renderer.chooseInterpolationQuality(context, *image, &bgLayer, geometry.tileSize),
document().settings().imageSubsamplingEnabled() ? AllowImageSubsampling::Yes : AllowImageSubsampling::No,
document().settings().showDebugBorders() ? ShowDebugBackground::Yes : ShowDebugBackground::No,
headroom
};
auto drawResult = context.drawTiledImage(*image, geometry.destinationRect, toLayoutPoint(geometry.relativePhase()), geometry.tileSize, geometry.spaceSize, options);
if (drawResult == ImageDrawResult::DidRequestDecoding) {
ASSERT(bgImage->hasCachedImage());
bgImage->cachedImage()->addClientWaitingForAsyncDecoding(m_renderer);
}
if (m_renderer.element() && !context.paintingDisabled())
m_renderer.element()->setHasEverPaintedImages(true);
}
}
}
void BackgroundPainter::clipRoundedInnerRect(GraphicsContext& context, const FloatRoundedRect& clipRect)
{
if (UNLIKELY(!clipRect.isRenderable())) {
auto adjustedClipRect = clipRect;
adjustedClipRect.adjustRadii();
context.clipRoundedRect(adjustedClipRect);
return;
}
context.clipRoundedRect(clipRect);
}
static inline std::optional<LayoutUnit> getSpace(LayoutUnit areaSize, LayoutUnit tileSize)
{
if (int numberOfTiles = areaSize / tileSize; numberOfTiles > 1)
return (areaSize - numberOfTiles * tileSize) / (numberOfTiles - 1);
return std::nullopt;
}
static LayoutUnit resolveEdgeRelativeLength(const Length& length, Edge edge, LayoutUnit availableSpace, const LayoutSize& areaSize, const LayoutSize& tileSize)
{
LayoutUnit result = minimumValueForLength(length, availableSpace);
if (edge == Edge::Right)
return areaSize.width() - tileSize.width() - result;
if (edge == Edge::Bottom)
return areaSize.height() - tileSize.height() - result;
return result;
}
static void pixelSnapBackgroundImageGeometryForPainting(LayoutRect& destinationRect, LayoutSize& tileSize, LayoutSize& phase, LayoutSize& space, float scaleFactor)
{
tileSize = LayoutSize(snapRectToDevicePixels(LayoutRect(destinationRect.location(), tileSize), scaleFactor).size());
phase = LayoutSize(snapRectToDevicePixels(LayoutRect(destinationRect.location(), phase), scaleFactor).size());
space = LayoutSize(snapRectToDevicePixels(LayoutRect(LayoutPoint(), space), scaleFactor).size());
destinationRect = LayoutRect(snapRectToDevicePixels(destinationRect, scaleFactor));
}
BackgroundImageGeometry BackgroundPainter::calculateBackgroundImageGeometry(const RenderBoxModelObject& renderer, const RenderLayerModelObject* paintContainer, const FillLayer& fillLayer, const LayoutPoint& paintOffset, const LayoutRect& borderBoxRect, std::optional<FillBox> overrideOrigin)
{
auto& view = renderer.view();
LayoutUnit left;
LayoutUnit top;
LayoutSize positioningAreaSize;
// Determine the background positioning area and set destination rect to the background painting area.
// Destination rect will be adjusted later if the background is non-repeating.
auto enclosingLayer = renderer.enclosingLayer();
bool isTransformed = renderer.isTransformed() || (enclosingLayer && enclosingLayer->hasTransformedAncestor());
bool fixedAttachment = fillLayer.attachment() == FillAttachment::FixedBackground && !isTransformed;
LayoutRect destinationRect(borderBoxRect);
float deviceScaleFactor = renderer.document().deviceScaleFactor();
if (!fixedAttachment) {
LayoutUnit right;
LayoutUnit bottom;
// Scroll and Local.
auto fillLayerOrigin = overrideOrigin.value_or(fillLayer.origin());
if (fillLayerOrigin != FillBox::BorderBox) {
left = renderer.borderLeft();
right = renderer.borderRight();
top = renderer.borderTop();
bottom = renderer.borderBottom();
if (fillLayerOrigin == FillBox::ContentBox) {
left += renderer.paddingLeft();
right += renderer.paddingRight();
top += renderer.paddingTop();
bottom += renderer.paddingBottom();
}
}
// The background of the box generated by the root element covers the entire canvas including
// its margins. Since those were added in already, we have to factor them out when computing
// the background positioning area.
if (renderer.isDocumentElementRenderer()) {
positioningAreaSize = downcast<RenderBox>(renderer).size() - LayoutSize(left + right, top + bottom);
positioningAreaSize = LayoutSize(snapSizeToDevicePixel(positioningAreaSize, LayoutPoint(), deviceScaleFactor));
if (view.frameView().hasExtendedBackgroundRectForPainting()) {
LayoutRect extendedBackgroundRect = view.frameView().extendedBackgroundRectForPainting();
left += (renderer.marginLeft() - extendedBackgroundRect.x());
top += (renderer.marginTop() - extendedBackgroundRect.y());
}
} else {
positioningAreaSize = borderBoxRect.size() - LayoutSize(left + right, top + bottom);
positioningAreaSize = LayoutSize(snapRectToDevicePixels(LayoutRect(paintOffset, positioningAreaSize), deviceScaleFactor).size());
}
} else {
LayoutRect viewportRect;
FloatBoxExtent obscuredContentInsets;
if (renderer.settings().fixedBackgroundsPaintRelativeToDocument())
viewportRect = view.unscaledDocumentRect();
else {
LocalFrameView& frameView = view.frameView();
bool useFixedLayout = frameView.useFixedLayout() && !frameView.fixedLayoutSize().isEmpty();
if (useFixedLayout) {
// Use the fixedLayoutSize() when useFixedLayout() because the rendering will scale
// down the frameView to to fit in the current viewport.
viewportRect.setSize(frameView.fixedLayoutSize());
} else
viewportRect.setSize(frameView.sizeForVisibleContent());
if (renderer.fixedBackgroundPaintsInLocalCoordinates()) {
if (!useFixedLayout) {
// Shifting location by the content insets is needed for layout tests which expect
// layout to be shifted when calling window.internals.setObscuredContentInsets().
obscuredContentInsets = frameView.obscuredContentInsets(ScrollView::InsetType::WebCoreOrPlatformInset);
viewportRect.setLocation({ -obscuredContentInsets.left(), -obscuredContentInsets.top() });
}
} else if (useFixedLayout || frameView.frameScaleFactor() != 1) {
// scrollPositionForFixedPosition() is adjusted for page scale and it does not include
// insets so do not add it to the calculation below.
viewportRect.setLocation(frameView.scrollPositionForFixedPosition());
} else {
// documentScrollPositionRelativeToViewOrigin() is already adjusted for content insets
// so we need to account for that in calculating the phase size
obscuredContentInsets = frameView.obscuredContentInsets(ScrollView::InsetType::WebCoreOrPlatformInset);
viewportRect.setLocation(frameView.documentScrollPositionRelativeToViewOrigin());
}
left += obscuredContentInsets.left();
top += obscuredContentInsets.top();
}
if (paintContainer)
viewportRect.moveBy(LayoutPoint(-paintContainer->localToAbsolute(FloatPoint())));
destinationRect = viewportRect;
positioningAreaSize = destinationRect.size();
positioningAreaSize.setWidth(positioningAreaSize.width() - obscuredContentInsets.left());
positioningAreaSize.setHeight(positioningAreaSize.height() - obscuredContentInsets.top());
positioningAreaSize = LayoutSize(snapRectToDevicePixels(LayoutRect(destinationRect.location(), positioningAreaSize), deviceScaleFactor).size());
}
LayoutSize tileSize = calculateFillTileSize(renderer, fillLayer, positioningAreaSize);
FillRepeat backgroundRepeatX = fillLayer.repeat().x;
FillRepeat backgroundRepeatY = fillLayer.repeat().y;
LayoutUnit availableWidth = positioningAreaSize.width() - tileSize.width();
LayoutUnit availableHeight = positioningAreaSize.height() - tileSize.height();
LayoutSize spaceSize;
LayoutSize phase;
LayoutUnit computedXPosition = resolveEdgeRelativeLength(fillLayer.xPosition(), fillLayer.backgroundXOrigin(), availableWidth, positioningAreaSize, tileSize);
if (backgroundRepeatX == FillRepeat::Round && positioningAreaSize.width() > 0 && tileSize.width() > 0) {
int numTiles = std::max(1, roundToInt(positioningAreaSize.width() / tileSize.width()));
if (fillLayer.size().size.height.isAuto() && backgroundRepeatY != FillRepeat::Round)
tileSize.setHeight(tileSize.height() * positioningAreaSize.width() / (numTiles * tileSize.width()));
tileSize.setWidth(positioningAreaSize.width() / numTiles);
phase.setWidth(tileSize.width() ? tileSize.width() - fmodf((computedXPosition + left), tileSize.width()) : 0);
}
LayoutUnit computedYPosition = resolveEdgeRelativeLength(fillLayer.yPosition(), fillLayer.backgroundYOrigin(), availableHeight, positioningAreaSize, tileSize);
if (backgroundRepeatY == FillRepeat::Round && positioningAreaSize.height() > 0 && tileSize.height() > 0) {
int numTiles = std::max(1, roundToInt(positioningAreaSize.height() / tileSize.height()));
if (fillLayer.size().size.width.isAuto() && backgroundRepeatX != FillRepeat::Round)
tileSize.setWidth(tileSize.width() * positioningAreaSize.height() / (numTiles * tileSize.height()));
tileSize.setHeight(positioningAreaSize.height() / numTiles);
phase.setHeight(tileSize.height() ? tileSize.height() - fmodf((computedYPosition + top), tileSize.height()) : 0);
}
if (backgroundRepeatX == FillRepeat::Repeat) {
phase.setWidth(tileSize.width() ? tileSize.width() - fmodf(computedXPosition + left, tileSize.width()) : 0);
spaceSize.setWidth(0);
} else if (backgroundRepeatX == FillRepeat::Space && tileSize.width() > 0) {
if (auto space = getSpace(positioningAreaSize.width(), tileSize.width())) {
LayoutUnit actualWidth = tileSize.width() + *space;
computedXPosition = minimumValueForLength(Length(), availableWidth);
spaceSize.setWidth(*space);
spaceSize.setHeight(0);
phase.setWidth(actualWidth ? actualWidth - fmodf((computedXPosition + left), actualWidth) : 0);
} else
backgroundRepeatX = FillRepeat::NoRepeat;
}
if (backgroundRepeatX == FillRepeat::NoRepeat) {
LayoutUnit xOffset = left + computedXPosition;
if (xOffset > 0)
destinationRect.move(xOffset, 0_lu);
xOffset = std::min<LayoutUnit>(xOffset, 0);
phase.setWidth(-xOffset);
destinationRect.setWidth(tileSize.width() + xOffset);
spaceSize.setWidth(0);
}
if (backgroundRepeatY == FillRepeat::Repeat) {
phase.setHeight(tileSize.height() ? tileSize.height() - fmodf(computedYPosition + top, tileSize.height()) : 0);
spaceSize.setHeight(0);
} else if (backgroundRepeatY == FillRepeat::Space && tileSize.height() > 0) {
if (auto space = getSpace(positioningAreaSize.height(), tileSize.height())) {
LayoutUnit actualHeight = tileSize.height() + *space;
computedYPosition = minimumValueForLength(Length(), availableHeight);
spaceSize.setHeight(*space);
phase.setHeight(actualHeight ? actualHeight - fmodf((computedYPosition + top), actualHeight) : 0);
} else
backgroundRepeatY = FillRepeat::NoRepeat;
}
if (backgroundRepeatY == FillRepeat::NoRepeat) {
LayoutUnit yOffset = top + computedYPosition;
if (yOffset > 0)
destinationRect.move(0_lu, yOffset);
yOffset = std::min<LayoutUnit>(yOffset, 0);
phase.setHeight(-yOffset);
destinationRect.setHeight(tileSize.height() + yOffset);
spaceSize.setHeight(0);
}
if (fixedAttachment) {
LayoutPoint attachmentPoint = borderBoxRect.location();
phase.expand(std::max<LayoutUnit>(attachmentPoint.x() - destinationRect.x(), 0), std::max<LayoutUnit>(attachmentPoint.y() - destinationRect.y(), 0));
}
destinationRect.intersect(borderBoxRect);
auto tileSizeWithoutPixelSnapping = tileSize;
pixelSnapBackgroundImageGeometryForPainting(destinationRect, tileSize, phase, spaceSize, deviceScaleFactor);
return BackgroundImageGeometry(destinationRect, tileSizeWithoutPixelSnapping, tileSize, phase, spaceSize, fixedAttachment);
}
LayoutSize BackgroundPainter::calculateFillTileSize(const RenderBoxModelObject& renderer, const FillLayer& fillLayer, const LayoutSize& positioningAreaSize)
{
StyleImage* image = fillLayer.image();
FillSizeType type = fillLayer.size().type;
auto devicePixelSize = LayoutUnit { 1.0 / renderer.document().deviceScaleFactor() };
LayoutSize imageIntrinsicSize;
if (image) {
imageIntrinsicSize = renderer.calculateImageIntrinsicDimensions(image, positioningAreaSize, RenderBoxModelObject::ScaleByUsedZoom::Yes);
imageIntrinsicSize.scale(1 / image->imageScaleFactor(), 1 / image->imageScaleFactor());
} else
imageIntrinsicSize = positioningAreaSize;
switch (type) {
case FillSizeType::Size: {
LayoutSize tileSize = positioningAreaSize;
Length layerWidth = fillLayer.size().size.width;
Length layerHeight = fillLayer.size().size.height;
if (layerWidth.isFixed())
tileSize.setWidth(layerWidth.value());
else if (layerWidth.isPercentOrCalculated()) {
auto resolvedWidth = valueForLength(layerWidth, positioningAreaSize.width());
// Non-zero resolved value should always produce some content.
tileSize.setWidth(!resolvedWidth ? resolvedWidth : std::max(devicePixelSize, resolvedWidth));
}
if (layerHeight.isFixed())
tileSize.setHeight(layerHeight.value());
else if (layerHeight.isPercentOrCalculated()) {
auto resolvedHeight = valueForLength(layerHeight, positioningAreaSize.height());
// Non-zero resolved value should always produce some content.
tileSize.setHeight(!resolvedHeight ? resolvedHeight : std::max(devicePixelSize, resolvedHeight));
}
// If one of the values is auto we have to use the appropriate
// scale to maintain our aspect ratio.
bool hasNaturalAspectRatio = image && image->imageHasNaturalDimensions();
if (layerWidth.isAuto() && !layerHeight.isAuto()) {
if (hasNaturalAspectRatio && imageIntrinsicSize.height())
tileSize.setWidth(imageIntrinsicSize.width() * tileSize.height() / imageIntrinsicSize.height());
} else if (!layerWidth.isAuto() && layerHeight.isAuto()) {
if (hasNaturalAspectRatio && imageIntrinsicSize.width())
tileSize.setHeight(imageIntrinsicSize.height() * tileSize.width() / imageIntrinsicSize.width());
} else if (layerWidth.isAuto() && layerHeight.isAuto()) {
// If both width and height are auto, use the image's intrinsic size.
tileSize = imageIntrinsicSize;
}
tileSize.clampNegativeToZero();
return tileSize;
}
case FillSizeType::None: {
// If both values are ‘auto’ then the intrinsic width and/or height of the image should be used, if any.
if (!imageIntrinsicSize.isEmpty())
return imageIntrinsicSize;
// If the image has neither an intrinsic width nor an intrinsic height, its size is determined as for ‘contain’.
type = FillSizeType::Contain;
}
FALLTHROUGH;
case FillSizeType::Contain:
case FillSizeType::Cover: {
// Scale computation needs higher precision than what LayoutUnit can offer.
FloatSize localImageIntrinsicSize = imageIntrinsicSize;
FloatSize localPositioningAreaSize = positioningAreaSize;
float horizontalScaleFactor = localImageIntrinsicSize.width() ? (localPositioningAreaSize.width() / localImageIntrinsicSize.width()) : 1;
float verticalScaleFactor = localImageIntrinsicSize.height() ? (localPositioningAreaSize.height() / localImageIntrinsicSize.height()) : 1;
float scaleFactor = type == FillSizeType::Contain ? std::min(horizontalScaleFactor, verticalScaleFactor) : std::max(horizontalScaleFactor, verticalScaleFactor);
if (localImageIntrinsicSize.isEmpty())
return { };
return LayoutSize(localImageIntrinsicSize.scaled(scaleFactor).expandedTo({ devicePixelSize, devicePixelSize }));
}
}
ASSERT_NOT_REACHED();
return { };
}
static LayoutRect areaCastingShadowInHole(const LayoutRect& holeRect, LayoutUnit shadowExtent, LayoutUnit shadowSpread, const LayoutSize& shadowOffset)
{
LayoutRect bounds(holeRect);
bounds.inflate(shadowExtent);
if (shadowSpread < 0)
bounds.inflate(-shadowSpread);
LayoutRect offsetBounds = bounds;
offsetBounds.move(-shadowOffset);
return unionRect(bounds, offsetBounds);
}
void BackgroundPainter::paintBoxShadow(const LayoutRect& paintRect, const RenderStyle& style, ShadowStyle shadowStyle, RectEdges<bool> closedEdges) const
{
// FIXME: Deal with border-image. Would be great to use border-image as a mask.
GraphicsContext& context = m_paintInfo.context();
if (context.paintingDisabled() || !style.boxShadow())
return;
const auto borderShape = BorderShape::shapeForBorderRect(style, paintRect, closedEdges);
bool hasBorderRadius = style.hasBorderRadius();
float deviceScaleFactor = document().deviceScaleFactor();
bool hasOpaqueBackground = style.visitedDependentColorWithColorFilter(CSSPropertyBackgroundColor).isOpaque();
for (const ShadowData* shadow = style.boxShadow(); shadow; shadow = shadow->next()) {
if (shadow->style() != shadowStyle)
continue;
LayoutSize shadowOffset(shadow->x().value, shadow->y().value);
LayoutUnit shadowPaintingExtent = shadow->paintingExtent();
LayoutUnit shadowSpread = LayoutUnit(shadow->spread().value);
auto shadowRadius = shadow->radius().value;
if (shadowOffset.isZero() && !shadowRadius && !shadowSpread)
continue;
auto shadowColor = style.colorWithColorFilter(shadow->color());
auto shouldInflateBorderRect = [&]() {
if (!hasOpaqueBackground)
return false;
// FIXME: The function to decide on the policy based on the transform should be a named function.
// FIXME: It's not clear if this check is right. What about integral scale factors?
auto transform = context.getCTM();
if (transform.a() != 1 || (transform.d() != 1 && transform.d() != -1) || transform.b() || transform.c())
return true;
return false;
};
if (shadow->style() == ShadowStyle::Normal) {
auto shadowShape = borderShape;
shadowShape.inflate(shadowSpread);
if (shadowShape.isEmpty())
continue;
// If the box is opaque, it is unnecessary to clip it out. However, doing so saves time
// when painting the shadow. On the other hand, it introduces subpixel gaps along the
// corners. Those are avoided by insetting the clipping path by one pixel.
auto adjustedBorderShape = borderShape;
if (shouldInflateBorderRect())
adjustedBorderShape.inflate(-1_lu);
auto shadowRect = paintRect;
shadowRect.inflate(shadowPaintingExtent + shadowSpread);
shadowRect.move(shadowOffset);
auto pixelSnappedShadowRect = snapRectToDevicePixels(shadowRect, deviceScaleFactor);
GraphicsContextStateSaver stateSaver(context);
context.clip(pixelSnappedShadowRect);
// Move the fill just outside the clip, adding at least 1 pixel of separation so that the fill does not
// bleed in (due to antialiasing) if the context is transformed.
LayoutUnit xOffset = paintRect.width() + std::max<LayoutUnit>(0, shadowOffset.width()) + shadowPaintingExtent + 2 * shadowSpread + LayoutUnit(1);
LayoutSize extraOffset(xOffset.ceil(), 0);
shadowOffset -= extraOffset;
shadowShape.move(extraOffset);
auto pixelSnappedFillRect = shadowShape.snappedOuterRect(deviceScaleFactor);
LayoutPoint shadowRectOrigin = shadowShape.borderRect().location() + shadowOffset;
FloatPoint snappedShadowOrigin = FloatPoint(roundToDevicePixel(shadowRectOrigin.x(), deviceScaleFactor), roundToDevicePixel(shadowRectOrigin.y(), deviceScaleFactor));
FloatSize snappedShadowOffset = snappedShadowOrigin - pixelSnappedFillRect.location();
context.setDropShadow({ snappedShadowOffset, shadowRadius, shadowColor, shadow->isWebkitBoxShadow() ? ShadowRadiusMode::Legacy : ShadowRadiusMode::Default });
adjustedBorderShape.clipOutOuterShape(context, deviceScaleFactor);
if (hasBorderRadius) {
auto influenceShape = BorderShape::shapeForBorderRect(style, shadowRect);
auto influenceRadii = influenceShape.radii();
influenceRadii.expand(2 * shadowPaintingExtent + shadowSpread);
influenceShape.setRadii(influenceRadii);
if (influenceShape.outerShapeContains(m_paintInfo.rect))
context.fillRect(shadowShape.snappedOuterRect(deviceScaleFactor), Color::black);
else
shadowShape.fillOuterShape(context, Color::black, deviceScaleFactor);
} else
context.fillRect(pixelSnappedFillRect, Color::black);
} else {
// Inset shadow.
auto borderRect = borderShape.deprecatedInnerRoundedRect();
auto holeRect = borderRect.rect();
holeRect.inflate(-shadowSpread);
if (!closedEdges.left())
holeRect.shiftXEdgeBy(-(std::max<LayoutUnit>(shadowOffset.width(), 0) + shadowPaintingExtent + shadowSpread));
if (!closedEdges.top())
holeRect.shiftYEdgeBy(-(std::max<LayoutUnit>(shadowOffset.height(), 0) + shadowPaintingExtent + shadowSpread));
if (!closedEdges.right())
holeRect.setWidth(holeRect.width() - std::min<LayoutUnit>(shadowOffset.width(), 0) + shadowPaintingExtent + shadowSpread);
if (!closedEdges.bottom())
holeRect.setHeight(holeRect.height() - std::min<LayoutUnit>(shadowOffset.height(), 0) + shadowPaintingExtent + shadowSpread);
auto roundedHoleRect = RoundedRect { holeRect, borderRect.radii() };
if (shadowSpread && roundedHoleRect.isRounded()) {
auto rounedRectCorrectingForSpread = [&]() {
LayoutUnit leftWidth { closedEdges.left() ? style.borderLeftWidth() + shadowSpread : 0 };
LayoutUnit rightWidth { closedEdges.right() ? style.borderRightWidth() + shadowSpread : 0 };
LayoutUnit topWidth { closedEdges.top() ? style.borderTopWidth() + shadowSpread : 0 };
LayoutUnit bottomWidth { closedEdges.bottom() ? style.borderBottomWidth() + shadowSpread : 0 };
return style.getRoundedInnerBorderFor(paintRect, topWidth, bottomWidth, leftWidth, rightWidth, closedEdges);
}();
roundedHoleRect.setRadii(rounedRectCorrectingForSpread.radii());
}
auto pixelSnappedHoleRect = roundedHoleRect.pixelSnappedRoundedRectForPainting(deviceScaleFactor);
auto pixelSnappedBorderRect = borderRect.pixelSnappedRoundedRectForPainting(deviceScaleFactor);
if (pixelSnappedHoleRect.isEmpty()) {
if (hasBorderRadius)
context.fillRoundedRect(pixelSnappedBorderRect, shadowColor);
else
context.fillRect(pixelSnappedBorderRect.rect(), shadowColor);
continue;
}
Color fillColor = shadowColor.opaqueColor();
auto shadowCastingRect = areaCastingShadowInHole(borderRect.rect(), shadowPaintingExtent, shadowSpread, shadowOffset);
auto pixelSnappedOuterRect = snapRectToDevicePixels(shadowCastingRect, deviceScaleFactor);
GraphicsContextStateSaver stateSaver(context);
if (hasBorderRadius)
context.clipRoundedRect(pixelSnappedBorderRect);
else
context.clip(pixelSnappedBorderRect.rect());
LayoutUnit xOffset = 2 * paintRect.width() + std::max<LayoutUnit>(0, shadowOffset.width()) + shadowPaintingExtent - 2 * shadowSpread + LayoutUnit(1);
LayoutSize extraOffset(xOffset.ceil(), 0);
context.translate(extraOffset);
shadowOffset -= extraOffset;
auto snappedShadowOffset = roundSizeToDevicePixels(shadowOffset, deviceScaleFactor);
context.setDropShadow({ snappedShadowOffset, shadowRadius, shadowColor, shadow->isWebkitBoxShadow() ? ShadowRadiusMode::Legacy : ShadowRadiusMode::Default });
context.fillRectWithRoundedHole(pixelSnappedOuterRect, pixelSnappedHoleRect, fillColor);
}
}
}
bool BackgroundPainter::boxShadowShouldBeAppliedToBackground(const RenderBoxModelObject& renderer, const LayoutPoint& paintOffset, BleedAvoidance bleedAvoidance, const InlineIterator::InlineBoxIterator& inlineBox)
{
if (bleedAvoidance != BleedAvoidance::None)
return false;
auto& style = renderer.style();
if (style.hasUsedAppearance())
return false;
bool hasOneNormalBoxShadow = false;
for (const ShadowData* currentShadow = style.boxShadow(); currentShadow; currentShadow = currentShadow->next()) {
if (currentShadow->style() != ShadowStyle::Normal)
continue;
if (hasOneNormalBoxShadow)
return false;
hasOneNormalBoxShadow = true;
if (!currentShadow->spread().isZero())
return false;
}
if (!hasOneNormalBoxShadow)
return false;
Color backgroundColor = style.visitedDependentColorWithColorFilter(CSSPropertyBackgroundColor);
if (!backgroundColor.isOpaque())
return false;
auto* lastBackgroundLayer = &style.backgroundLayers();
while (auto* next = lastBackgroundLayer->next())
lastBackgroundLayer = next;
if (lastBackgroundLayer->clip() != FillBox::BorderBox)
return false;
if (lastBackgroundLayer->image() && style.hasBorderRadius())
return false;
auto applyToInlineBox = [&] {
// The checks here match how paintFillLayer() decides whether to clip (if it does, the shadow
// would be clipped out, so it has to be drawn separately).
if (inlineBox->isRootInlineBox())
return true;
if (!inlineBox->nextInlineBoxLineLeftward() && !inlineBox->nextInlineBoxLineRightward())
return true;
auto* image = lastBackgroundLayer->image();
auto& renderer = inlineBox->renderer();
bool hasFillImage = image && image->canRender(&renderer, renderer.style().usedZoom());
return !hasFillImage && !renderer.style().hasBorderRadius();
};
if (inlineBox && !applyToInlineBox())
return false;
if (renderer.hasNonVisibleOverflow() && lastBackgroundLayer->attachment() == FillAttachment::LocalBackground)
return false;
if (is<RenderTableCell>(renderer))
return false;
if (auto* imageRenderer = dynamicDowncast<RenderImage>(renderer))
return !const_cast<RenderImage&>(*imageRenderer).backgroundIsKnownToBeObscured(paintOffset);
return true;
}
const Document& BackgroundPainter::document() const
{
return m_renderer.document();
}
const RenderView& BackgroundPainter::view() const
{
return m_renderer.view();
}
}
|