#include "config.h"
#include "RenderSVGRoot.h"
#include "Frame.h"
#include "GraphicsContext.h"
#include "HitTestResult.h"
#include "LayoutRepainter.h"
#include "Page.h"
#include "RenderIterator.h"
#include "RenderLayer.h"
#include "RenderNamedFlowFragment.h"
#include "RenderSVGResource.h"
#include "RenderSVGResourceContainer.h"
#include "RenderSVGResourceFilter.h"
#include "RenderView.h"
#include "SVGImage.h"
#include "SVGRenderingContext.h"
#include "SVGResources.h"
#include "SVGResourcesCache.h"
#include "SVGSVGElement.h"
#include "SVGViewSpec.h"
#include "TransformState.h"
#include <wtf/StackStats.h>
namespace WebCore {
RenderSVGRoot::RenderSVGRoot(SVGSVGElement& element, RenderStyle&& style)
: RenderReplaced(element, WTFMove(style))
, m_objectBoundingBoxValid(false)
, m_isLayoutSizeChanged(false)
, m_needsBoundariesOrTransformUpdate(true)
, m_hasSVGShadow(false)
, m_hasBoxDecorations(false)
{
}
RenderSVGRoot::~RenderSVGRoot()
{
}
SVGSVGElement& RenderSVGRoot::svgSVGElement() const
{
return downcast<SVGSVGElement>(nodeForNonAnonymous());
}
void RenderSVGRoot::computeIntrinsicRatioInformation(FloatSize& intrinsicSize, double& intrinsicRatio) const
{
intrinsicSize.setWidth(floatValueForLength(svgSVGElement().intrinsicWidth(), 0));
intrinsicSize.setHeight(floatValueForLength(svgSVGElement().intrinsicHeight(), 0));
if (!intrinsicSize.isEmpty())
intrinsicRatio = intrinsicSize.width() / static_cast<double>(intrinsicSize.height());
else {
FloatSize viewBoxSize = svgSVGElement().viewBox().size();
if (!viewBoxSize.isEmpty()) {
intrinsicRatio = viewBoxSize.width() / static_cast<double>(viewBoxSize.height());
}
}
}
bool RenderSVGRoot::isEmbeddedThroughSVGImage() const
{
return isInSVGImage(&svgSVGElement());
}
bool RenderSVGRoot::isEmbeddedThroughFrameContainingSVGDocument() const
{
if (!frame().ownerRenderer())
return false;
return frame().document()->isSVGDocument();
}
static inline LayoutUnit resolveLengthAttributeForSVG(const Length& length, float scale, float maxSize)
{
return valueForLength(length, maxSize) * (length.isFixed() ? scale : 1);
}
LayoutUnit RenderSVGRoot::computeReplacedLogicalWidth(ShouldComputePreferred shouldComputePreferred) const
{
if (!m_containerSize.isEmpty())
return m_containerSize.width();
if (isEmbeddedThroughFrameContainingSVGDocument())
return containingBlock()->availableLogicalWidth();
return RenderReplaced::computeReplacedLogicalWidth(shouldComputePreferred);
}
LayoutUnit RenderSVGRoot::computeReplacedLogicalHeight(std::optional<LayoutUnit> estimatedUsedWidth) const
{
if (!m_containerSize.isEmpty())
return m_containerSize.height();
if (isEmbeddedThroughFrameContainingSVGDocument())
return containingBlock()->availableLogicalHeight(IncludeMarginBorderPadding);
return RenderReplaced::computeReplacedLogicalHeight(estimatedUsedWidth);
}
void RenderSVGRoot::layout()
{
StackStats::LayoutCheckPoint layoutCheckPoint;
ASSERT(needsLayout());
m_resourcesNeedingToInvalidateClients.clear();
LayoutStateDisabler layoutStateDisabler(view());
bool needsLayout = selfNeedsLayout();
LayoutRepainter repainter(*this, checkForRepaintDuringLayout() && needsLayout);
LayoutSize oldSize = size();
updateLogicalWidth();
updateLogicalHeight();
buildLocalToBorderBoxTransform();
m_isLayoutSizeChanged = needsLayout || (svgSVGElement().hasRelativeLengths() && oldSize != size());
SVGRenderSupport::layoutChildren(*this, needsLayout || SVGRenderSupport::filtersForceContainerLayout(*this));
if (!m_resourcesNeedingToInvalidateClients.isEmpty()) {
for (auto& resource : m_resourcesNeedingToInvalidateClients) {
resource->removeAllClientsFromCache();
SVGResourcesCache::clientStyleChanged(*resource, StyleDifferenceLayout, resource->style());
}
m_isLayoutSizeChanged = false;
SVGRenderSupport::layoutChildren(*this, false);
}
if (m_needsBoundariesOrTransformUpdate) {
updateCachedBoundaries();
m_needsBoundariesOrTransformUpdate = false;
}
clearOverflow();
if (!shouldApplyViewportClip()) {
FloatRect contentRepaintRect = repaintRectInLocalCoordinates();
contentRepaintRect = m_localToBorderBoxTransform.mapRect(contentRepaintRect);
addVisualOverflow(enclosingLayoutRect(contentRepaintRect));
}
updateLayerTransform();
m_hasBoxDecorations = isDocumentElementRenderer() ? hasVisibleBoxDecorationStyle() : hasVisibleBoxDecorations();
invalidateBackgroundObscurationStatus();
repainter.repaintAfterLayout();
clearNeedsLayout();
}
bool RenderSVGRoot::shouldApplyViewportClip() const
{
return style().overflowX() == OHIDDEN
|| style().overflowX() == OAUTO
|| style().overflowX() == OSCROLL
|| this->isDocumentElementRenderer();
}
void RenderSVGRoot::paintReplaced(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
{
if (borderBoxRect().isEmpty())
return;
if (paintInfo.context().paintingDisabled())
return;
if (paintInfo.phase == PaintPhaseOutline || paintInfo.phase == PaintPhaseSelfOutline)
return;
if (svgSVGElement().hasEmptyViewBox())
return;
if (!firstChild()) {
auto* resources = SVGResourcesCache::cachedResourcesForRenderer(*this);
if (!resources || !resources->filter()) {
if (paintInfo.phase == PaintPhaseForeground)
page().addRelevantUnpaintedObject(this, visualOverflowRect());
return;
}
}
if (paintInfo.phase == PaintPhaseForeground)
page().addRelevantRepaintedObject(this, visualOverflowRect());
PaintInfo childPaintInfo(paintInfo);
childPaintInfo.context().save();
if (shouldApplyViewportClip())
childPaintInfo.context().clip(snappedIntRect(overflowClipRect(paintOffset, currentRenderNamedFlowFragment())));
IntPoint adjustedPaintOffset = roundedIntPoint(paintOffset);
childPaintInfo.applyTransform(AffineTransform::translation(adjustedPaintOffset.x(), adjustedPaintOffset.y()) * localToBorderBoxTransform());
{
SVGRenderingContext renderingContext;
bool continueRendering = true;
if (childPaintInfo.phase == PaintPhaseForeground) {
renderingContext.prepareToRenderSVGContent(*this, childPaintInfo);
continueRendering = renderingContext.isRenderingPrepared();
}
if (continueRendering) {
childPaintInfo.updateSubtreePaintRootForChildren(this);
for (auto& child : childrenOfType<RenderElement>(*this))
child.paint(childPaintInfo, location());
}
}
childPaintInfo.context().restore();
}
void RenderSVGRoot::willBeDestroyed()
{
RenderBlock::removePercentHeightDescendant(const_cast<RenderSVGRoot&>(*this));
SVGResourcesCache::clientDestroyed(*this);
RenderReplaced::willBeDestroyed();
}
void RenderSVGRoot::insertedIntoTree()
{
RenderReplaced::insertedIntoTree();
SVGResourcesCache::clientWasAddedToTree(*this);
}
void RenderSVGRoot::willBeRemovedFromTree()
{
SVGResourcesCache::clientWillBeRemovedFromTree(*this);
RenderReplaced::willBeRemovedFromTree();
}
void RenderSVGRoot::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
{
if (diff == StyleDifferenceLayout)
setNeedsBoundariesUpdate();
if (diff == StyleDifferenceRepaint)
m_hasBoxDecorations = hasVisibleBoxDecorationStyle();
RenderReplaced::styleDidChange(diff, oldStyle);
SVGResourcesCache::clientStyleChanged(*this, diff, style());
}
void RenderSVGRoot::addChild(RenderObject* child, RenderObject* beforeChild)
{
RenderReplaced::addChild(child, beforeChild);
SVGResourcesCache::clientWasAddedToTree(*child);
}
void RenderSVGRoot::removeChild(RenderObject& child)
{
SVGResourcesCache::clientWillBeRemovedFromTree(child);
RenderReplaced::removeChild(child);
}
void RenderSVGRoot::buildLocalToBorderBoxTransform()
{
float scale = style().effectiveZoom();
FloatPoint translate = svgSVGElement().currentTranslateValue();
LayoutSize borderAndPadding(borderLeft() + paddingLeft(), borderTop() + paddingTop());
m_localToBorderBoxTransform = svgSVGElement().viewBoxToViewTransform(contentWidth() / scale, contentHeight() / scale);
if (borderAndPadding.isZero() && scale == 1 && translate == FloatPoint::zero())
return;
m_localToBorderBoxTransform = AffineTransform(scale, 0, 0, scale, borderAndPadding.width() + translate.x(), borderAndPadding.height() + translate.y()) * m_localToBorderBoxTransform;
}
const AffineTransform& RenderSVGRoot::localToParentTransform() const
{
m_localToParentTransform = m_localToBorderBoxTransform;
if (x())
m_localToParentTransform.setE(m_localToParentTransform.e() + roundToInt(x()));
if (y())
m_localToParentTransform.setF(m_localToParentTransform.f() + roundToInt(y()));
return m_localToParentTransform;
}
LayoutRect RenderSVGRoot::clippedOverflowRectForRepaint(const RenderLayerModelObject* repaintContainer) const
{
if (style().visibility() != VISIBLE && !enclosingLayer()->hasVisibleContent())
return LayoutRect();
FloatRect contentRepaintRect = m_localToBorderBoxTransform.mapRect(repaintRectInLocalCoordinates());
contentRepaintRect.intersect(snappedIntRect(borderBoxRect()));
LayoutRect repaintRect = enclosingLayoutRect(contentRepaintRect);
if (m_hasBoxDecorations || hasRenderOverflow())
repaintRect.unite(unionRect(localSelectionRect(false), visualOverflowRect()));
return RenderReplaced::computeRectForRepaint(enclosingIntRect(repaintRect), repaintContainer);
}
FloatRect RenderSVGRoot::computeFloatRectForRepaint(const FloatRect& repaintRect, const RenderLayerModelObject* repaintContainer, bool fixed) const
{
FloatRect adjustedRect = m_localToBorderBoxTransform.mapRect(repaintRect);
const SVGRenderStyle& svgStyle = style().svgStyle();
if (const ShadowData* shadow = svgStyle.shadow())
shadow->adjustRectForShadow(adjustedRect);
if (shouldApplyViewportClip())
adjustedRect.intersect(snappedIntRect(borderBoxRect()));
if (m_hasBoxDecorations || hasRenderOverflow()) {
LayoutRect decoratedRepaintRect = unionRect(localSelectionRect(false), visualOverflowRect());
adjustedRect.unite(decoratedRepaintRect);
}
return RenderReplaced::computeRectForRepaint(enclosingIntRect(adjustedRect), repaintContainer, {fixed, false});
}
void RenderSVGRoot::mapLocalToContainer(const RenderLayerModelObject* repaintContainer, TransformState& transformState, MapCoordinatesFlags mode, bool* wasFixed) const
{
ASSERT(mode & ~IsFixed); ASSERT(mode & UseTransforms);
RenderReplaced::mapLocalToContainer(repaintContainer, transformState, mode | ApplyContainerFlip, wasFixed);
}
const RenderObject* RenderSVGRoot::pushMappingToContainer(const RenderLayerModelObject* ancestorToStopAt, RenderGeometryMap& geometryMap) const
{
return RenderReplaced::pushMappingToContainer(ancestorToStopAt, geometryMap);
}
void RenderSVGRoot::updateCachedBoundaries()
{
SVGRenderSupport::computeContainerBoundingBoxes(*this, m_objectBoundingBox, m_objectBoundingBoxValid, m_strokeBoundingBox, m_repaintBoundingBoxExcludingShadow);
SVGRenderSupport::intersectRepaintRectWithResources(*this, m_repaintBoundingBoxExcludingShadow);
m_repaintBoundingBoxExcludingShadow.inflate(horizontalBorderAndPaddingExtent());
m_repaintBoundingBox = m_repaintBoundingBoxExcludingShadow;
SVGRenderSupport::intersectRepaintRectWithShadows(*this, m_repaintBoundingBox);
}
bool RenderSVGRoot::nodeAtPoint(const HitTestRequest& request, HitTestResult& result, const HitTestLocation& locationInContainer, const LayoutPoint& accumulatedOffset, HitTestAction hitTestAction)
{
LayoutPoint pointInParent = locationInContainer.point() - toLayoutSize(accumulatedOffset);
LayoutPoint pointInBorderBox = pointInParent - toLayoutSize(location());
if (contentBoxRect().contains(pointInBorderBox)) {
FloatPoint localPoint = localToParentTransform().inverse().value_or(AffineTransform()).mapPoint(FloatPoint(pointInParent));
for (RenderObject* child = lastChild(); child; child = child->previousSibling()) {
if (child->nodeAtFloatPoint(request, result, localPoint, hitTestAction)) {
updateHitTestResult(result, pointInBorderBox);
if (!result.addNodeToRectBasedTestResult(child->node(), request, locationInContainer))
return true;
}
}
}
if ((hitTestAction == HitTestBlockBackground || hitTestAction == HitTestChildBlockBackground) && visibleToHitTesting()) {
LayoutRect boundsRect(accumulatedOffset + location(), size());
if (locationInContainer.intersects(boundsRect)) {
updateHitTestResult(result, pointInBorderBox);
if (!result.addNodeToRectBasedTestResult(&svgSVGElement(), request, locationInContainer, boundsRect))
return true;
}
}
return false;
}
bool RenderSVGRoot::hasRelativeDimensions() const
{
return svgSVGElement().intrinsicHeight().isPercentOrCalculated() || svgSVGElement().intrinsicWidth().isPercentOrCalculated();
}
void RenderSVGRoot::addResourceForClientInvalidation(RenderSVGResourceContainer* resource)
{
RenderSVGRoot* svgRoot = SVGRenderSupport::findTreeRootObject(*resource);
if (!svgRoot)
return;
svgRoot->m_resourcesNeedingToInvalidateClients.add(resource);
}
}