#include "config.h"
#include "RenderSVGRoot.h"
#include "Chrome.h"
#include "ChromeClient.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 "SVGLength.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, Ref<RenderStyle>&& style)
: RenderReplaced(element, WTF::move(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();
if (style().logicalWidth().isSpecified() || style().logicalMaxWidth().isSpecified())
return RenderReplaced::computeReplacedLogicalWidth(shouldComputePreferred);
if (svgSVGElement().hasIntrinsicWidth())
return resolveLengthAttributeForSVG(svgSVGElement().intrinsicWidth(), style().effectiveZoom(), containingBlock()->availableLogicalWidth());
return RenderReplaced::computeReplacedLogicalWidth(shouldComputePreferred);
}
LayoutUnit RenderSVGRoot::computeReplacedLogicalHeight() const
{
if (!m_containerSize.isEmpty())
return m_containerSize.height();
if (isEmbeddedThroughFrameContainingSVGDocument())
return containingBlock()->availableLogicalHeight(IncludeMarginBorderPadding);
if (style().logicalHeight().isSpecified() || style().logicalMaxHeight().isSpecified())
return RenderReplaced::computeReplacedLogicalHeight();
if (svgSVGElement().hasIntrinsicHeight())
return resolveLengthAttributeForSVG(svgSVGElement().intrinsicHeight(), style().effectiveZoom(), containingBlock()->availableLogicalHeight(IncludeMarginBorderPadding).toFloat());
return RenderReplaced::computeReplacedLogicalHeight();
}
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()) {
HashSet<RenderSVGResourceContainer*>::iterator end = m_resourcesNeedingToInvalidateClients.end();
for (HashSet<RenderSVGResourceContainer*>::iterator it = m_resourcesNeedingToInvalidateClients.begin(); it != end; ++it)
(*it)->removeAllClientsFromCache();
m_isLayoutSizeChanged = false;
SVGRenderSupport::layoutChildren(*this, false);
}
if (m_needsBoundariesOrTransformUpdate) {
updateCachedBoundaries();
m_needsBoundariesOrTransformUpdate = false;
}
if (!shouldApplyViewportClip()) {
FloatRect contentRepaintRect = repaintRectInLocalCoordinates();
contentRepaintRect = m_localToBorderBoxTransform.mapRect(contentRepaintRect);
addVisualOverflow(enclosingLayoutRect(contentRepaintRect));
}
updateLayerTransform();
m_hasBoxDecorations = isRoot() ? hasBoxDecorationStyle() : hasBoxDecorations();
invalidateBackgroundObscurationStatus();
repainter.repaintAfterLayout();
clearNeedsLayout();
}
bool RenderSVGRoot::shouldApplyViewportClip() const
{
return style().overflowX() == OHIDDEN
|| style().overflowX() == OAUTO
|| style().overflowX() == OSCROLL
|| this->isRoot();
}
void RenderSVGRoot::paintReplaced(PaintInfo& paintInfo, const LayoutPoint& paintOffset)
{
if (pixelSnappedBorderBoxRect().isEmpty())
return;
if (paintInfo.context->paintingDisabled())
return;
if (paintInfo.phase == PaintPhaseOutline || paintInfo.phase == PaintPhaseSelfOutline)
return;
if (svgSVGElement().hasEmptyViewBox())
return;
Page* page = frame().page();
if (!firstChild()) {
auto* resources = SVGResourcesCache::cachedResourcesForRenderer(*this);
if (!resources || !resources->filter()) {
if (page && paintInfo.phase == PaintPhaseForeground)
page->addRelevantUnpaintedObject(this, visualOverflowRect());
return;
}
}
if (page && 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::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
{
if (diff == StyleDifferenceLayout)
setNeedsBoundariesUpdate();
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();
SVGPoint translate = svgSVGElement().currentTranslate();
LayoutSize borderAndPadding(borderLeft() + paddingLeft(), borderTop() + paddingTop());
m_localToBorderBoxTransform = svgSVGElement().viewBoxToViewTransform(contentWidth() / scale, contentHeight() / scale);
if (borderAndPadding.isEmpty() && scale == 1 && translate == SVGPoint::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
{
return SVGRenderSupport::clippedOverflowRectForRepaint(*this, repaintContainer);
}
void RenderSVGRoot::computeFloatRectForRepaint(const RenderLayerModelObject* repaintContainer, FloatRect& repaintRect, bool fixed) const
{
repaintRect = m_localToBorderBoxTransform.mapRect(repaintRect);
const SVGRenderStyle& svgStyle = style().svgStyle();
if (const ShadowData* shadow = svgStyle.shadow())
shadow->adjustRectForShadow(repaintRect);
if (shouldApplyViewportClip())
repaintRect.intersect(pixelSnappedBorderBoxRect());
if (m_hasBoxDecorations || hasRenderOverflow()) {
LayoutRect decoratedRepaintRect = unionRect(localSelectionRect(false), visualOverflowRect());
repaintRect.unite(decoratedRepaintRect);
}
LayoutRect rect = enclosingIntRect(repaintRect);
RenderReplaced::computeRectForRepaint(repaintContainer, rect, fixed);
repaintRect = rect;
}
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().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 && 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)
{
RenderElement* svgRoot = resource->parent();
while (svgRoot && !is<RenderSVGRoot>(*svgRoot))
svgRoot = svgRoot->parent();
if (!svgRoot)
return;
downcast<RenderSVGRoot>(*svgRoot).m_resourcesNeedingToInvalidateClients.add(resource);
}
}