Skip to content

Commit

Permalink
Use smart pointers for members of SVG classes
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=273061

Reviewed by Chris Dumez.

Use smart pointers for members of SVG classes instead of raw pointers.

* Source/WebCore/rendering/svg/SVGBoundingBoxComputation.cpp:
(WebCore::SVGBoundingBoxComputation::handleShapeOrTextOrInline const):
(WebCore::SVGBoundingBoxComputation::handleRootOrContainer const):
(WebCore::SVGBoundingBoxComputation::handleForeignObjectOrImage const):
(WebCore::SVGBoundingBoxComputation::adjustBoxForClippingAndEffects const):
* Source/WebCore/rendering/svg/SVGBoundingBoxComputation.h:
* Source/WebCore/rendering/svg/SVGContainerLayout.cpp:
(WebCore::SVGContainerLayout::layoutChildren):
(WebCore::SVGContainerLayout::positionChildrenRelativeToContainer):
(WebCore::SVGContainerLayout::layoutSizeOfNearestViewportChanged const):
* Source/WebCore/rendering/svg/SVGContainerLayout.h:
* Source/WebCore/rendering/svg/SVGLayerTransformComputation.h:
(WebCore::SVGLayerTransformComputation::computeAccumulatedTransform const):
(WebCore::SVGLayerTransformComputation::calculateScreenFontSizeScalingFactor const):
* Source/WebCore/rendering/svg/SVGLayerTransformUpdater.h:
(WebCore::SVGLayerTransformUpdater::SVGLayerTransformUpdater):
(WebCore::SVGLayerTransformUpdater::~SVGLayerTransformUpdater):
(WebCore::SVGLayerTransformUpdater::layerTransformChanged const):
* Source/WebCore/rendering/svg/SVGTextLayoutAttributes.cpp:
(WebCore::SVGTextLayoutAttributes::context):
(WebCore::SVGTextLayoutAttributes::context const):
* Source/WebCore/rendering/svg/SVGTextLayoutAttributes.h:
(WebCore::SVGTextLayoutAttributes::context): Deleted.
(WebCore::SVGTextLayoutAttributes::context const): Deleted.
* Source/WebCore/rendering/svg/SVGTextMetricsBuilder.cpp:
(WebCore::SVGTextMetricsBuilder::SVGTextMetricsBuilder):
* Source/WebCore/rendering/svg/SVGTextMetricsBuilder.h:

Canonical link: https://commits.webkit.org/278697@main
  • Loading branch information
rwlbuis committed May 13, 2024
1 parent c05a19a commit e05acd9
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 44 deletions.
26 changes: 13 additions & 13 deletions Source/WebCore/rendering/svg/SVGBoundingBoxComputation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ FloatRect SVGBoundingBoxComputation::handleShapeOrTextOrInline(const SVGBounding
//
// Note: The values of the fill, fill-opacity and fill-rule properties do not affect fill-shape.
if (options.contains(DecorationOption::IncludeFillShape))
box = m_renderer.objectBoundingBox();
box = m_renderer->objectBoundingBox();

// 4. If stroke is true and the element's stroke is anything other than none, then set box to be the union of box
// and the tightest rectangle in coordinate system space that contains the stroke shape of the element, with the
Expand All @@ -99,9 +99,9 @@ FloatRect SVGBoundingBoxComputation::handleShapeOrTextOrInline(const SVGBounding
// Note: The values of the stroke-opacity, stroke-dasharray and stroke-dashoffset do not affect the calculation of the stroke shape.
if (options.contains(DecorationOption::IncludeStrokeShape)) {
if (options.contains(DecorationOption::CalculateFastRepaintRect) && is<RenderSVGShape>(m_renderer))
box.unite(downcast<RenderSVGShape>(m_renderer).approximateStrokeBoundingBox());
box.unite(downcast<RenderSVGShape>(m_renderer.get()).approximateStrokeBoundingBox());
else
box.unite(m_renderer.strokeBoundingBox());
box.unite(m_renderer->strokeBoundingBox());
}

// 5. If markers is true, then for each marker marker rendered on the element:
Expand All @@ -111,7 +111,7 @@ FloatRect SVGBoundingBoxComputation::handleShapeOrTextOrInline(const SVGBounding
// - Otherwise, set box to be the union of box and the result of invoking the algorithm to compute a bounding box with child as
// the element, space as the target coordinate space, true for fill, stroke and markers, and clipped for clipped.
if (options.contains(DecorationOption::IncludeMarkers)) {
if (CheckedPtr svgPath = dynamicDowncast<RenderSVGPath>(m_renderer)) {
if (CheckedPtr svgPath = dynamicDowncast<RenderSVGPath>(m_renderer.get())) {
DecorationOptions optionsForMarker = { DecorationOption::IncludeFillShape, DecorationOption::IncludeStrokeShape, DecorationOption::IncludeMarkers };
if (options.contains(DecorationOption::IncludeClippers))
optionsForMarker.add(DecorationOption::IncludeClippers);
Expand Down Expand Up @@ -140,7 +140,7 @@ FloatRect SVGBoundingBoxComputation::handleRootOrContainer(const SVGBoundingBoxC
ASSERT(child.isSVGLayerAwareRenderer());
ASSERT(!child.isRenderSVGRoot());

auto transform = SVGLayerTransformComputation(child).computeAccumulatedTransform(&m_renderer, TransformState::TrackSVGCTMMatrix);
auto transform = SVGLayerTransformComputation(child).computeAccumulatedTransform(m_renderer.ptr(), TransformState::TrackSVGCTMMatrix);
return transform.isIdentity() ? std::nullopt : std::make_optional(WTFMove(transform));
};

Expand Down Expand Up @@ -204,15 +204,15 @@ FloatRect SVGBoundingBoxComputation::handleRootOrContainer(const SVGBoundingBoxC
// system space that contains the intersection of box and the rectangle specified by clip. (TODO!)
adjustBoxForClippingAndEffects(options, box, { DecorationOption::OverrideBoxWithFilterBox });

if (options.contains(DecorationOption::IncludeClippers) && m_renderer.hasNonVisibleOverflow()) {
ASSERT(m_renderer.hasLayer());
if (options.contains(DecorationOption::IncludeClippers) && m_renderer->hasNonVisibleOverflow()) {
ASSERT(m_renderer->hasLayer());

ASSERT(is<RenderSVGViewportContainer>(m_renderer) || is<RenderSVGResourceMarker>(m_renderer) || is<RenderSVGRoot>(m_renderer));

LayoutRect overflowClipRect;
if (CheckedPtr svgModelObject = dynamicDowncast<RenderSVGModelObject>(m_renderer))
if (CheckedPtr svgModelObject = dynamicDowncast<RenderSVGModelObject>(m_renderer.get()))
overflowClipRect = svgModelObject->overflowClipRect(svgModelObject->currentSVGLayoutLocation());
else if (CheckedPtr box = dynamicDowncast<RenderBox>(m_renderer))
else if (CheckedPtr box = dynamicDowncast<RenderBox>(m_renderer.get()))
overflowClipRect = box->overflowClipRect(box->location());
else {
ASSERT_NOT_REACHED();
Expand All @@ -234,7 +234,7 @@ FloatRect SVGBoundingBoxComputation::handleForeignObjectOrImage(const SVGBoundin
// defined by the "x", "y", "width" and "height" geometric properties of the element.
//
// Note: The fill, stroke and markers input arguments to this algorithm do not affect the bounding box returned for these elements.
auto box = m_renderer.objectBoundingBox();
auto box = m_renderer->objectBoundingBox();

// 2. If clipped is true and the value of clip-path on element is not none, then set box to be the tightest rectangle
// in coordinate system space that contains the intersection of box and the clipping path.
Expand All @@ -260,14 +260,14 @@ void SVGBoundingBoxComputation::adjustBoxForClippingAndEffects(const SVGBounding
UNUSED_PARAM(includeFilter);

if (options.contains(DecorationOption::IncludeClippers)) {
if (CheckedPtr referencedClipperRenderer = m_renderer.svgClipperResourceFromStyle()) {
if (CheckedPtr referencedClipperRenderer = m_renderer->svgClipperResourceFromStyle()) {
auto repaintRectCalculation = options.contains(DecorationOption::CalculateFastRepaintRect) ? RepaintRectCalculation::Fast : RepaintRectCalculation::Accurate;
box.intersect(referencedClipperRenderer->resourceBoundingBox(m_renderer, repaintRectCalculation));
}
}

if (options.contains(DecorationOption::IncludeMaskers)) {
if (CheckedPtr referencedMaskerRenderer = m_renderer.svgMaskerResourceFromStyle()) {
if (CheckedPtr referencedMaskerRenderer = m_renderer->svgMaskerResourceFromStyle()) {
// When masks are nested, the inner masks do not affect the outer mask dimension, so skip the computation for inner masks.
static unsigned s_maskBoundingBoxNestingLevel = 0;
NestingLevelIncrementer incrementer { s_maskBoundingBoxNestingLevel };
Expand All @@ -279,7 +279,7 @@ void SVGBoundingBoxComputation::adjustBoxForClippingAndEffects(const SVGBounding
}

if (options.contains(DecorationOption::IncludeOutline))
box.inflate(m_renderer.outlineStyleForRepaint().outlineSize());
box.inflate(m_renderer->outlineStyleForRepaint().outlineSize());
}

}
2 changes: 1 addition & 1 deletion Source/WebCore/rendering/svg/SVGBoundingBoxComputation.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class SVGBoundingBoxComputation {

void adjustBoxForClippingAndEffects(const DecorationOptions&, FloatRect& box, const DecorationOptions& optionsToCheckForFilters = filterBoundingBoxDecoration) const;

const RenderLayerModelObject& m_renderer;
SingleThreadWeakRef<const RenderLayerModelObject> m_renderer;
};

} // namespace WebCore
10 changes: 5 additions & 5 deletions Source/WebCore/rendering/svg/SVGContainerLayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ SVGContainerLayout::SVGContainerLayout(RenderLayerModelObject& container)
void SVGContainerLayout::layoutChildren(bool containerNeedsLayout)
{
bool layoutSizeChanged = layoutSizeOfNearestViewportChanged();
bool transformChanged = transformToRootChanged(&m_container);
bool transformChanged = transformToRootChanged(m_container.ptr());

m_positionedChildren.clear();
for (auto& child : childrenOfType<RenderObject>(m_container)) {
Expand Down Expand Up @@ -133,16 +133,16 @@ void SVGContainerLayout::positionChildrenRelativeToContainer()
// only meaningful for the children of the RenderSVGRoot. RenderSVGRoot itself is positioned according to
// the CSS box model object, where we need to respect border & padding, encoded in the contentBoxLocation().
// -> Position all RenderSVGRoot children relative to the contentBoxLocation() to avoid intruding border/padding area.
if (CheckedPtr svgRoot = dynamicDowncast<RenderSVGRoot>(m_container))
if (CheckedPtr svgRoot = dynamicDowncast<RenderSVGRoot>(m_container.get()))
return -svgRoot->contentBoxLocation();

// For (inner) RenderSVGViewportContainer nominalSVGLayoutLocation() returns the viewport boundaries,
// including the effect of the 'x'/'y' attribute values. Do not subtract the location, otherwise the
// effect of the x/y translation is removed.
if (is<RenderSVGViewportContainer>(m_container) && !m_container.isAnonymous())
if (is<RenderSVGViewportContainer>(m_container) && !m_container->isAnonymous())
return { };

return m_container.nominalSVGLayoutLocation();
return m_container->nominalSVGLayoutLocation();
};

// Arrange layout location for all child renderers relative to the container layout location.
Expand Down Expand Up @@ -204,7 +204,7 @@ void SVGContainerLayout::verifyLayoutLocationConsistency(const RenderLayerModelO

bool SVGContainerLayout::layoutSizeOfNearestViewportChanged() const
{
RenderElement* ancestor = &m_container;
RenderElement* ancestor = m_container.ptr();
while (ancestor && !is<RenderSVGRoot>(ancestor) && !is<RenderSVGViewportContainer>(ancestor))
ancestor = ancestor->parent();

Expand Down
2 changes: 1 addition & 1 deletion Source/WebCore/rendering/svg/SVGContainerLayout.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class SVGContainerLayout {
private:
bool layoutSizeOfNearestViewportChanged() const;

RenderLayerModelObject& m_container;
SingleThreadWeakRef<RenderLayerModelObject> m_container;
Vector<std::reference_wrapper<RenderLayerModelObject>> m_positionedChildren;
};

Expand Down
18 changes: 9 additions & 9 deletions Source/WebCore/rendering/svg/SVGLayerTransformComputation.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class SVGLayerTransformComputation {
// anonymous RenderSVGViewportContainer (most noticeable: viewBox). Therefore we have to start
// calling mapLocalToContainer() starting from the anonymous RenderSVGViewportContainer, and
// not from its parent - RenderSVGRoot.
auto* renderer = &m_renderer;
auto* renderer = m_renderer.ptr();
if (auto* svgRoot = dynamicDowncast<RenderSVGRoot>(renderer)) {
renderer = svgRoot->viewportContainer();
if (trackingMode == TransformState::TrackSVGCTMMatrix)
Expand All @@ -75,7 +75,7 @@ class SVGLayerTransformComputation {
renderer->mapLocalToContainer(ancestorContainer, transformState, { UseTransforms, ApplyContainerFlip });

if (trackingMode == TransformState::TrackSVGCTMMatrix) {
if (auto* svgRoot = dynamicDowncast<RenderSVGRoot>(m_renderer))
if (auto* svgRoot = dynamicDowncast<RenderSVGRoot>(m_renderer.get()))
transformState.move(-toLayoutSize(svgRoot->contentBoxLocation()));
else if (ancestorContainer) {
// Continue to accumulate container offsets, excluding transforms, from the container of the current element ('ancestorContainer') up to RenderSVGRoot.
Expand All @@ -96,17 +96,17 @@ class SVGLayerTransformComputation {

// When we've climbed the ancestor tree up to and including RenderSVGRoot, the CTM is aligned with the top-left of the renderers bounding box (= nominal SVG layout location).
// However, for getCTM/getScreenCTM we're supposed to align by the top-left corner of the enclosing "viewport element" -- correct for that.
if (m_renderer.isRenderSVGRoot())
if (m_renderer->isRenderSVGRoot())
return ctm;

ctm.translate(-toFloatSize(m_renderer.nominalSVGLayoutLocation()));
ctm.translate(-toFloatSize(m_renderer->nominalSVGLayoutLocation()));
return ctm;
}

float calculateScreenFontSizeScalingFactor() const
{
// Walk up the render tree, accumulating transforms
auto* layer = m_renderer.enclosingLayer();
auto* layer = m_renderer->enclosingLayer();

RenderLayer* stopAtLayer = nullptr;
while (layer) {
Expand All @@ -120,14 +120,14 @@ class SVGLayerTransformComputation {
}

auto ctm = computeAccumulatedTransform(stopAtLayer ? &stopAtLayer->renderer() : nullptr, TransformState::TrackSVGScreenCTMMatrix);
ctm.scale(m_renderer.document().deviceScaleFactor());
if (!m_renderer.document().isSVGDocument())
ctm.scale(m_renderer.style().usedZoom());
ctm.scale(m_renderer->document().deviceScaleFactor());
if (!m_renderer->document().isSVGDocument())
ctm.scale(m_renderer->style().usedZoom());
return narrowPrecisionToFloat(std::hypot(ctm.xScale(), ctm.yScale()) / sqrtOfTwoDouble);
}

private:
const RenderLayerModelObject& m_renderer;
SingleThreadWeakRef<const RenderLayerModelObject> m_renderer;
};

} // namespace WebCore
19 changes: 10 additions & 9 deletions Source/WebCore/rendering/svg/SVGLayerTransformUpdater.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include "RenderElementInlines.h"
#include "RenderLayerModelObject.h"
#include <wtf/WeakPtr.h>

namespace WebCore {

Expand All @@ -30,28 +31,28 @@ class SVGLayerTransformUpdater {
SVGLayerTransformUpdater(RenderLayerModelObject& renderer)
: m_renderer(renderer)
{
if (!m_renderer.hasLayer())
if (!m_renderer->hasLayer())
return;

m_transformReferenceBox = m_renderer.transformReferenceBoxRect();
m_layerTransform = m_renderer.layerTransform();
m_transformReferenceBox = m_renderer->transformReferenceBoxRect();
m_layerTransform = m_renderer->layerTransform();

m_renderer.updateLayerTransform();
m_renderer->updateLayerTransform();
}

~SVGLayerTransformUpdater()
{
if (!m_renderer.hasLayer())
if (!m_renderer->hasLayer())
return;
if (m_renderer.transformReferenceBoxRect() == m_transformReferenceBox)
if (m_renderer->transformReferenceBoxRect() == m_transformReferenceBox)
return;

m_renderer.updateLayerTransform();
m_renderer->updateLayerTransform();
}

bool layerTransformChanged() const
{
auto* layerTransform = m_renderer.layerTransform();
auto* layerTransform = m_renderer->layerTransform();

bool hasTransform = !!layerTransform;
bool hadTransform = !!m_layerTransform;
Expand All @@ -62,7 +63,7 @@ class SVGLayerTransformUpdater {
}

private:
RenderLayerModelObject& m_renderer;
SingleThreadWeakRef<RenderLayerModelObject> m_renderer;
FloatRect m_transformReferenceBox;
TransformationMatrix* m_layerTransform { nullptr };
};
Expand Down
2 changes: 2 additions & 0 deletions Source/WebCore/rendering/svg/SVGTextLayoutAttributes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,6 @@ float SVGTextLayoutAttributes::emptyValue()
return s_emptyValue;
}

RenderSVGInlineText& SVGTextLayoutAttributes::context() { return m_context.get(); }
const RenderSVGInlineText& SVGTextLayoutAttributes::context() const { return m_context.get(); }
}
6 changes: 3 additions & 3 deletions Source/WebCore/rendering/svg/SVGTextLayoutAttributes.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,16 @@ class SVGTextLayoutAttributes {
void clear();
static float emptyValue();

RenderSVGInlineText& context() { return m_context; }
const RenderSVGInlineText& context() const { return m_context; }
RenderSVGInlineText& context();
const RenderSVGInlineText& context() const;

SVGCharacterDataMap& characterDataMap() { return m_characterDataMap; }
const SVGCharacterDataMap& characterDataMap() const { return m_characterDataMap; }

Vector<SVGTextMetrics>& textMetricsValues() { return m_textMetricsValues; }

private:
RenderSVGInlineText& m_context;
SingleThreadWeakRef<RenderSVGInlineText> m_context;
SVGCharacterDataMap m_characterDataMap;
Vector<SVGTextMetrics> m_textMetricsValues;
};
Expand Down
4 changes: 2 additions & 2 deletions Source/WebCore/rendering/svg/SVGTextMetricsBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@
#include "RenderSVGInline.h"
#include "RenderSVGInlineText.h"
#include "RenderSVGText.h"
#include <wtf/WeakPtr.h>

namespace WebCore {

SVGTextMetricsBuilder::SVGTextMetricsBuilder()
: m_text(0)
, m_run(StringView())
: m_run(StringView())
, m_textPosition(0)
, m_isComplexText(false)
, m_totalWidth(0)
Expand Down
2 changes: 1 addition & 1 deletion Source/WebCore/rendering/svg/SVGTextMetricsBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class SVGTextMetricsBuilder {
void walkTree(RenderElement&, RenderSVGInlineText* stopAtLeaf, MeasureTextData&);
std::tuple<unsigned, UChar> measureTextRenderer(RenderSVGInlineText&, const MeasureTextData&, std::tuple<unsigned, UChar>);

RenderSVGInlineText* m_text;
SingleThreadWeakPtr<RenderSVGInlineText> m_text;
TextRun m_run;
unsigned m_textPosition;
bool m_isComplexText { false };
Expand Down

0 comments on commit e05acd9

Please sign in to comment.