Skip to content

Commit

Permalink
Use gfx geometry types for blink filters, masks, and other SVG resources
Browse files Browse the repository at this point in the history
Most changes are simple, but one thing needs attention: the return
value of SVGLengthContext::ResolveRectangle() no longer allows negative
width/height (will be clamped to 0).

Bug: 738465
Change-Id: I45becb1b787a45825a2b15a81929365644e3051e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3294642
Commit-Queue: Xianzhu Wang <wangxianzhu@chromium.org>
Reviewed-by: Philip Rogers <pdr@chromium.org>
Reviewed-by: Fredrik Söderquist <fs@opera.com>
Owners-Override: Philip Rogers <pdr@chromium.org>
Cr-Commit-Position: refs/heads/main@{#945840}
  • Loading branch information
wangxianzhu authored and Chromium LUCI CQ committed Nov 29, 2021
1 parent 6dacb91 commit bcafe09
Show file tree
Hide file tree
Showing 85 changed files with 417 additions and 385 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -771,7 +771,7 @@ void AddKeyframeToCurve(CompositorFilterAnimationCurve& curve,
Keyframe::PropertySpecificKeyframe* keyframe,
const CompositorKeyframeValue* value,
const TimingFunction& keyframe_timing_function) {
FilterEffectBuilder builder(FloatRect(), 1);
FilterEffectBuilder builder(gfx::RectF(), 1);
CompositorFilterKeyframe filter_keyframe(
keyframe->Offset(),
builder.BuildFilterOperations(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ scoped_refptr<BasicShape> BasicShapeForValue(

gfx::PointF PointForCenterCoordinate(const BasicShapeCenterCoordinate& center_x,
const BasicShapeCenterCoordinate& center_y,
FloatSize box_size) {
gfx::SizeF box_size) {
float x = FloatValueForLength(center_x.ComputedLength(), box_size.width());
float y = FloatValueForLength(center_y.ComputedLength(), box_size.height());
return gfx::PointF(x, y);
Expand Down
3 changes: 2 additions & 1 deletion third_party/blink/renderer/core/css/basic_shape_functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

namespace gfx {
class PointF;
class SizeF;
}

namespace blink {
Expand All @@ -50,7 +51,7 @@ CORE_EXPORT scoped_refptr<BasicShape> BasicShapeForValue(
const CSSValue&);
gfx::PointF PointForCenterCoordinate(const BasicShapeCenterCoordinate&,
const BasicShapeCenterCoordinate&,
FloatSize);
gfx::SizeF);

} // namespace blink
#endif // THIRD_PARTY_BLINK_RENDERER_CORE_CSS_BASIC_SHAPE_FUNCTIONS_H_
6 changes: 4 additions & 2 deletions third_party/blink/renderer/core/layout/layout_box.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7190,8 +7190,10 @@ PhysicalRect LayoutBox::PhysicalVisualOverflowRectIncludingFilters() const {
PhysicalRect bounds_rect = PhysicalVisualOverflowRect();
if (!StyleRef().HasFilter())
return bounds_rect;
FloatRect float_rect(bounds_rect);
float_rect.UnionIfNonZero(Layer()->FilterReferenceBox());
gfx::RectF float_rect(bounds_rect);
gfx::RectF filter_reference_box = Layer()->FilterReferenceBox();
if (!filter_reference_box.size().IsZero())
float_rect.UnionEvenIfEmpty(filter_reference_box);
float_rect = Layer()->MapRectForFilter(float_rect);
return PhysicalRect::EnclosingRect(float_rect);
}
Expand Down
6 changes: 3 additions & 3 deletions third_party/blink/renderer/core/layout/shapes/shape.cc
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ std::unique_ptr<Shape> Shape::CreateShape(const BasicShape* basic_shape,
const BasicShapeCircle* circle = To<BasicShapeCircle>(basic_shape);
gfx::PointF center =
PointForCenterCoordinate(circle->CenterX(), circle->CenterY(),
FloatSize(box_width, box_height));
gfx::SizeF(box_width, box_height));
float radius =
circle->FloatValueForRadiusInBox(FloatSize(box_width, box_height));
circle->FloatValueForRadiusInBox(gfx::SizeF(box_width, box_height));
gfx::PointF logical_center = PhysicalPointToLogical(
center, logical_box_size.Height().ToFloat(), writing_mode);

Expand All @@ -126,7 +126,7 @@ std::unique_ptr<Shape> Shape::CreateShape(const BasicShape* basic_shape,
const BasicShapeEllipse* ellipse = To<BasicShapeEllipse>(basic_shape);
gfx::PointF center =
PointForCenterCoordinate(ellipse->CenterX(), ellipse->CenterY(),
FloatSize(box_width, box_height));
gfx::SizeF(box_width, box_height));
float radius_x = ellipse->FloatValueForRadiusInBox(ellipse->RadiusX(),
center.x(), box_width);
float radius_y = ellipse->FloatValueForRadiusInBox(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ void LayoutSVGResourceFilter::RemoveAllClientsFromCache() {
MarkAllClientsForInvalidation(kPaintInvalidation | kFilterCacheInvalidation);
}

FloatRect LayoutSVGResourceFilter::ResourceBoundingBox(
gfx::RectF LayoutSVGResourceFilter::ResourceBoundingBox(
const gfx::RectF& reference_box) const {
NOT_DESTROYED();
const auto* filter_element = To<SVGFilterElement>(GetElement());
return SVGLengthContext::ResolveRectangle(filter_element, FilterUnits(),
FloatRect(reference_box));
reference_box);
}

SVGUnitTypes::SVGUnitType LayoutSVGResourceFilter::FilterUnits() const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class LayoutSVGResourceFilter final : public LayoutSVGResourceContainer {

void RemoveAllClientsFromCache() override;

FloatRect ResourceBoundingBox(const gfx::RectF& reference_box) const;
gfx::RectF ResourceBoundingBox(const gfx::RectF& reference_box) const;

SVGUnitTypes::SVGUnitType FilterUnits() const;
SVGUnitTypes::SVGUnitType PrimitiveUnits() const;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ SVGUnitTypes::SVGUnitType LayoutSVGResourceMasker::MaskContentUnits() const {
->CurrentEnumValue();
}

FloatRect LayoutSVGResourceMasker::ResourceBoundingBox(
gfx::RectF LayoutSVGResourceMasker::ResourceBoundingBox(
const gfx::RectF& reference_box,
float reference_box_zoom) {
NOT_DESTROYED();
Expand All @@ -98,8 +98,8 @@ FloatRect LayoutSVGResourceMasker::ResourceBoundingBox(
DCHECK(mask_element);

SVGUnitTypes::SVGUnitType mask_units = MaskUnits();
FloatRect mask_boundaries = SVGLengthContext::ResolveRectangle(
mask_element, mask_units, FloatRect(reference_box));
gfx::RectF mask_boundaries = SVGLengthContext::ResolveRectangle(
mask_element, mask_units, reference_box);
// If the mask bounds were resolved relative to the current userspace we need
// to adjust/scale with the zoom to get to the same space as the reference
// box.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@

#include "third_party/blink/renderer/core/layout/svg/layout_svg_resource_container.h"
#include "third_party/blink/renderer/core/svg/svg_unit_types.h"
#include "third_party/blink/renderer/platform/geometry/float_rect.h"
#include "third_party/skia/include/core/SkRefCnt.h"
#include "ui/gfx/geometry/rect_f.h"

namespace blink {

Expand All @@ -43,8 +43,8 @@ class LayoutSVGResourceMasker final : public LayoutSVGResourceContainer {

void RemoveAllClientsFromCache() override;

FloatRect ResourceBoundingBox(const gfx::RectF& reference_box,
float reference_box_zoom);
gfx::RectF ResourceBoundingBox(const gfx::RectF& reference_box,
float reference_box_zoom);

SVGUnitTypes::SVGUnitType MaskUnits() const;
SVGUnitTypes::SVGUnitType MaskContentUnits() const;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "third_party/blink/renderer/platform/graphics/paint/paint_record_builder.h"
#include "third_party/blink/renderer/platform/graphics/skia/skia_utils.h"
#include "third_party/blink/renderer/platform/heap/garbage_collected.h"
#include "ui/gfx/geometry/skia_conversions.h"

namespace blink {

Expand Down Expand Up @@ -139,8 +140,8 @@ std::unique_ptr<PatternData> LayoutSVGResourcePattern::BuildPatternData(
return pattern_data;

// Compute tile metrics.
FloatRect tile_bounds = SVGLengthContext::ResolveRectangle(
GetElement(), attributes.PatternUnits(), FloatRect(object_bounding_box),
gfx::RectF tile_bounds = SVGLengthContext::ResolveRectangle(
GetElement(), attributes.PatternUnits(), object_bounding_box,
*attributes.X(), *attributes.Y(), *attributes.Width(),
*attributes.Height());
if (tile_bounds.IsEmpty())
Expand All @@ -153,7 +154,7 @@ std::unique_ptr<PatternData> LayoutSVGResourcePattern::BuildPatternData(
return pattern_data;
tile_transform = SVGFitToViewBox::ViewBoxToViewTransform(
attributes.ViewBox(), attributes.PreserveAspectRatio(),
ToGfxSizeF(tile_bounds.size()));
tile_bounds.size());
} else {
// A viewBox overrides patternContentUnits, per spec.
if (attributes.PatternContentUnits() ==
Expand All @@ -165,7 +166,7 @@ std::unique_ptr<PatternData> LayoutSVGResourcePattern::BuildPatternData(

pattern_data->pattern = Pattern::CreatePaintRecordPattern(
AsPaintRecord(tile_bounds.size(), tile_transform),
FloatRect(gfx::PointF(), tile_bounds.size()));
gfx::RectF(tile_bounds.size()));

// Compute pattern space transformation.
pattern_data->transform.Translate(tile_bounds.x(), tile_bounds.y());
Expand Down Expand Up @@ -201,7 +202,7 @@ bool LayoutSVGResourcePattern::ApplyShader(
}

sk_sp<PaintRecord> LayoutSVGResourcePattern::AsPaintRecord(
const FloatSize& size,
const gfx::SizeF& size,
const AffineTransform& tile_transform) const {
NOT_DESTROYED();
DCHECK(!should_collect_pattern_attributes_);
Expand All @@ -211,9 +212,10 @@ sk_sp<PaintRecord> LayoutSVGResourcePattern::AsPaintRecord(
SVGUnitTypes::kSvgUnitTypeObjectboundingbox)
content_transform = tile_transform;

FloatRect bounds(gfx::PointF(), size);
gfx::RectF bounds(size);
PaintRecorder paint_recorder;
cc::PaintCanvas* canvas = paint_recorder.beginRecording(bounds);
cc::PaintCanvas* canvas =
paint_recorder.beginRecording(gfx::RectFToSkRect(bounds));

auto* pattern_content_element = Attributes().PatternContentElement();
DCHECK(pattern_content_element);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class LayoutSVGResourcePattern final : public LayoutSVGResourcePaintServer {
bool FindCycleFromSelf() const override;
std::unique_ptr<PatternData> BuildPatternData(
const gfx::RectF& object_bounding_box);
sk_sp<PaintRecord> AsPaintRecord(const FloatSize&,
sk_sp<PaintRecord> AsPaintRecord(const gfx::SizeF&,
const AffineTransform&) const;

mutable bool should_collect_pattern_attributes_ : 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ static gfx::RectF MapToSVGRootIncludingFilter(
for (; !parent->IsSVGRoot(); parent = parent->Parent()) {
const ComputedStyle& style = parent->StyleRef();
if (style.HasFilter())
visual_rect = ToGfxRectF(style.Filter().MapRect(FloatRect(visual_rect)));
visual_rect = style.Filter().MapRect(visual_rect);
visual_rect = parent->LocalToSVGParentTransform().MapRect(visual_rect);
}

Expand Down Expand Up @@ -252,8 +252,7 @@ void SVGLayoutSupport::AdjustWithClipPathAndMask(
visual_rect.Intersect(clipper->ResourceBoundingBox(object_bounding_box));
if (auto* masker = GetSVGResourceAsType<LayoutSVGResourceMasker>(
*client, style.MaskerResource())) {
visual_rect.Intersect(
ToGfxRectF(masker->ResourceBoundingBox(object_bounding_box, 1)));
visual_rect.Intersect(masker->ResourceBoundingBox(object_bounding_box, 1));
}
}

Expand Down Expand Up @@ -294,7 +293,7 @@ bool SVGLayoutSupport::IntersectsClipPath(const LayoutObject& object,
if (clip_path_operation->GetType() == ClipPathOperation::kShape) {
ShapeClipPathOperation& clip_path =
To<ShapeClipPathOperation>(*clip_path_operation);
return clip_path.GetPath(FloatRect(reference_box), 1)
return clip_path.GetPath(reference_box, 1)
.Contains(location.TransformedPoint());
}
DCHECK_EQ(clip_path_operation->GetType(), ClipPathOperation::kReference);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ void WriteSVGResourceContainer(WTF::TextStream& ts,
WriteNameValuePair(ts, "primitiveUnits", filter->PrimitiveUnits());
ts << "\n";
// Creating a placeholder filter which is passed to the builder.
FloatRect dummy_rect;
gfx::RectF dummy_rect;
auto* dummy_filter = MakeGarbageCollected<Filter>(dummy_rect, dummy_rect, 1,
Filter::kBoundingBox);
SVGFilterBuilder builder(dummy_filter->GetSourceGraphic());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ void SVGElementResourceClient::UpdateFilterData(
CompositorFilterOperations& operations) {
DCHECK(element_->GetLayoutObject());
const LayoutObject& object = *element_->GetLayoutObject();
FloatRect reference_box(SVGResources::ReferenceBoxForEffects(object));
gfx::RectF reference_box = SVGResources::ReferenceBoxForEffects(object);
if (!operations.IsEmpty() && !filter_data_dirty_ &&
reference_box == operations.ReferenceBox())
return;
Expand Down
6 changes: 3 additions & 3 deletions third_party/blink/renderer/core/paint/box_reflection_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ BoxReflection BoxReflectionForPaintLayer(const PaintLayer& layer,
const StyleReflection* reflect_style = style.BoxReflect();

LayoutRect frame_layout_rect = layer.GetLayoutBox()->FrameRect();
FloatRect frame_rect(frame_layout_rect);
gfx::RectF frame_rect(frame_layout_rect);
BoxReflection::ReflectionDirection direction =
BoxReflection::kVerticalReflection;
float offset = 0;
Expand Down Expand Up @@ -49,7 +49,7 @@ BoxReflection BoxReflectionForPaintLayer(const PaintLayer& layer,

const NinePieceImage& mask_nine_piece = reflect_style->Mask();
if (!mask_nine_piece.HasImage())
return BoxReflection(direction, offset, nullptr, FloatRect());
return BoxReflection(direction, offset, nullptr, gfx::RectF());

PhysicalRect mask_rect(PhysicalOffset(), frame_layout_rect.Size());
PhysicalRect mask_bounding_rect(mask_rect);
Expand All @@ -69,7 +69,7 @@ BoxReflection BoxReflectionForPaintLayer(const PaintLayer& layer,
mask_rect, style, mask_nine_piece);
}
return BoxReflection(direction, offset, builder->EndRecording(),
FloatRect(mask_bounding_rect));
gfx::RectF(mask_bounding_rect));
}

} // namespace blink
5 changes: 2 additions & 3 deletions third_party/blink/renderer/core/paint/clip_path_clipper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,7 @@ absl::optional<gfx::RectF> ClipPathClipper::LocalClipPathBoundingBox(
auto zoom =
UsesZoomedReferenceBox(object) ? object.StyleRef().EffectiveZoom() : 1;
auto& shape = To<ShapeClipPathOperation>(clip_path);
gfx::RectF bounding_box =
shape.GetPath(FloatRect(reference_box), zoom).BoundingRect();
gfx::RectF bounding_box = shape.GetPath(reference_box, zoom).BoundingRect();
bounding_box.Intersect(
gfx::RectF(ToGfxRect(LayoutRect::InfiniteIntRect())));
return bounding_box;
Expand Down Expand Up @@ -200,7 +199,7 @@ static absl::optional<Path> PathBasedClipInternal(
float zoom = uses_zoomed_reference_box
? clip_path_owner.StyleRef().EffectiveZoom()
: 1;
return shape.GetPath(FloatRect(reference_box), zoom);
return shape.GetPath(reference_box, zoom);
}

void ClipPathClipper::PaintClipPathAsMaskImage(
Expand Down
5 changes: 3 additions & 2 deletions third_party/blink/renderer/core/paint/css_mask_painter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "third_party/blink/renderer/core/layout/layout_inline.h"
#include "third_party/blink/renderer/core/layout/svg/layout_svg_resource_masker.h"
#include "third_party/blink/renderer/core/layout/svg/svg_resources.h"
#include "ui/gfx/geometry/rect_conversions.h"

namespace blink {

Expand All @@ -27,8 +28,8 @@ absl::optional<IntRect> CSSMaskPainter::MaskBoundingBox(
SVGResources::ReferenceBoxForEffects(object);
const float reference_box_zoom =
object.IsSVGForeignObject() ? object.StyleRef().EffectiveZoom() : 1;
return EnclosingIntRect(
masker->ResourceBoundingBox(reference_box, reference_box_zoom));
return IntRect(gfx::ToEnclosingRect(
masker->ResourceBoundingBox(reference_box, reference_box_zoom)));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ Vector<float> SepiaMatrix(double amount) {

} // namespace

FilterEffectBuilder::FilterEffectBuilder(const FloatRect& reference_box,
FilterEffectBuilder::FilterEffectBuilder(const gfx::RectF& reference_box,
float zoom,
const PaintFlags* fill_flags,
const PaintFlags* stroke_flags,
Expand Down Expand Up @@ -484,7 +484,7 @@ Filter* FilterEffectBuilder::BuildReferenceFilter(
return nullptr;
if (auto* resource_container = resource->ResourceContainerNoCycleCheck())
resource_container->ClearInvalidationMask();
FloatRect filter_region =
gfx::RectF filter_region =
SVGLengthContext::ResolveRectangle<SVGFilterElement>(
filter_element, filter_element->filterUnits()->CurrentEnumValue(),
reference_box_);
Expand Down
7 changes: 3 additions & 4 deletions third_party/blink/renderer/core/paint/filter_effect_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,26 @@
#define THIRD_PARTY_BLINK_RENDERER_CORE_PAINT_FILTER_EFFECT_BUILDER_H_

#include "third_party/blink/renderer/core/core_export.h"
#include "third_party/blink/renderer/platform/geometry/float_rect.h"
#include "third_party/blink/renderer/platform/graphics/paint/paint_flags.h"
#include "third_party/blink/renderer/platform/heap/handle.h"
#include "third_party/blink/renderer/platform/wtf/allocator/allocator.h"
#include "third_party/skia/include/core/SkTileMode.h"
#include "ui/gfx/geometry/rect_f.h"

namespace blink {

class CompositorFilterOperations;
class Filter;
class FilterEffect;
class FilterOperations;
class FloatRect;
class ReferenceFilterOperation;
class SVGFilterGraphNodeMap;

class CORE_EXPORT FilterEffectBuilder final {
STACK_ALLOCATED();

public:
FilterEffectBuilder(const FloatRect& reference_box,
FilterEffectBuilder(const gfx::RectF& reference_box,
float zoom,
const PaintFlags* fill_flags = nullptr,
const PaintFlags* stroke_flags = nullptr,
Expand All @@ -67,7 +66,7 @@ class CORE_EXPORT FilterEffectBuilder final {
}

private:
const FloatRect reference_box_;
const gfx::RectF reference_box_;
const float zoom_;
float shorthand_scale_; // Scale factor for shorthand filter functions.
const PaintFlags* fill_flags_;
Expand Down
Loading

0 comments on commit bcafe09

Please sign in to comment.