[svg] Implement feGaussianBlur

This filter implementation should be complete, but note we are still
not quite passing the W3C filters-gauss-* tests because our filters
currently operate in sRGB and not linear RGB (which is quite noticable
in some of the blur tests).

Bug: skia:10841
Change-Id: I706cde879ef6eb47ce586279999536cf67237f13
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/352506
Commit-Queue: Tyler Denniston <tdenniston@google.com>
Reviewed-by: Florin Malita <fmalita@chromium.org>
This commit is contained in:
Tyler Denniston 2021-01-12 09:34:23 -05:00 committed by Skia Commit-Bot
parent e931b838d3
commit 187d8117cb
6 changed files with 96 additions and 1 deletions

View File

@ -19,7 +19,8 @@ class SkSVGFe : public SkSVGHiddenContainer {
public:
static bool IsFilterEffect(const sk_sp<SkSVGNode>& node) {
return node->tag() == SkSVGTag::kFeTurbulence || node->tag() == SkSVGTag::kFeColorMatrix ||
node->tag() == SkSVGTag::kFeComposite || node->tag() == SkSVGTag::kFeFlood;
node->tag() == SkSVGTag::kFeComposite || node->tag() == SkSVGTag::kFeFlood ||
node->tag() == SkSVGTag::kFeGaussianBlur;
}
sk_sp<SkImageFilter> makeImageFilter(const SkSVGRenderContext& ctx,

View File

@ -0,0 +1,41 @@
/*
* Copyright 2020 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#ifndef SkSVGFeGaussianBlur_DEFINED
#define SkSVGFeGaussianBlur_DEFINED
#include "modules/svg/include/SkSVGFe.h"
#include "modules/svg/include/SkSVGTypes.h"
class SkSVGFeGaussianBlur : public SkSVGFe {
public:
struct StdDeviation {
SkSVGNumberType fX;
SkSVGNumberType fY;
};
static sk_sp<SkSVGFeGaussianBlur> Make() {
return sk_sp<SkSVGFeGaussianBlur>(new SkSVGFeGaussianBlur());
}
SVG_ATTR(StdDeviation, StdDeviation, StdDeviation({0, 0}))
protected:
sk_sp<SkImageFilter> onMakeImageFilter(const SkSVGRenderContext&,
const SkSVGFilterContext&) const override;
std::vector<SkSVGFeInputType> getInputs() const override { return {this->getIn()}; }
bool parseAndSetAttribute(const char*, const char*) override;
private:
SkSVGFeGaussianBlur() : INHERITED(SkSVGTag::kFeGaussianBlur) {}
using INHERITED = SkSVGFe;
};
#endif // SkSVGFeGaussianBlur_DEFINED

View File

@ -28,6 +28,7 @@ enum class SkSVGTag {
kFeColorMatrix,
kFeComposite,
kFeFlood,
kFeGaussianBlur,
kFeTurbulence,
kFilter,
kG,

View File

@ -19,6 +19,7 @@
#include "modules/svg/include/SkSVGFeColorMatrix.h"
#include "modules/svg/include/SkSVGFeComposite.h"
#include "modules/svg/include/SkSVGFeFlood.h"
#include "modules/svg/include/SkSVGFeGaussianBlur.h"
#include "modules/svg/include/SkSVGFeTurbulence.h"
#include "modules/svg/include/SkSVGFilter.h"
#include "modules/svg/include/SkSVGG.h"
@ -260,6 +261,7 @@ SortedDictionaryEntry<sk_sp<SkSVGNode>(*)()> gTagFactories[] = {
{ "feColorMatrix" , []() -> sk_sp<SkSVGNode> { return SkSVGFeColorMatrix::Make(); }},
{ "feComposite" , []() -> sk_sp<SkSVGNode> { return SkSVGFeComposite::Make(); }},
{ "feFlood" , []() -> sk_sp<SkSVGNode> { return SkSVGFeFlood::Make(); }},
{ "feGaussianBlur", []() -> sk_sp<SkSVGNode> { return SkSVGFeGaussianBlur::Make(); }},
{ "feTurbulence" , []() -> sk_sp<SkSVGNode> { return SkSVGFeTurbulence::Make(); }},
{ "filter" , []() -> sk_sp<SkSVGNode> { return SkSVGFilter::Make(); }},
{ "g" , []() -> sk_sp<SkSVGNode> { return SkSVGG::Make(); }},

View File

@ -0,0 +1,48 @@
/*
* Copyright 2020 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "include/effects/SkImageFilters.h"
#include "modules/svg/include/SkSVGAttributeParser.h"
#include "modules/svg/include/SkSVGFeGaussianBlur.h"
#include "modules/svg/include/SkSVGFilterContext.h"
#include "modules/svg/include/SkSVGRenderContext.h"
#include "modules/svg/include/SkSVGValue.h"
bool SkSVGFeGaussianBlur::parseAndSetAttribute(const char* name, const char* value) {
return INHERITED::parseAndSetAttribute(name, value) ||
this->setStdDeviation(SkSVGAttributeParser::parse<SkSVGFeGaussianBlur::StdDeviation>(
"stdDeviation", name, value));
}
sk_sp<SkImageFilter> SkSVGFeGaussianBlur::onMakeImageFilter(const SkSVGRenderContext& ctx,
const SkSVGFilterContext& fctx) const {
SkScalar sigmaX = fStdDeviation.fX;
SkScalar sigmaY = fStdDeviation.fY;
if (fctx.primitiveUnits().type() == SkSVGObjectBoundingBoxUnits::Type::kObjectBoundingBox) {
SkASSERT(ctx.node());
const SkRect objBounds = ctx.node()->objectBoundingBox(ctx);
sigmaX *= objBounds.width();
sigmaY *= objBounds.height();
}
return SkImageFilters::Blur(sigmaX, sigmaY,
fctx.resolveInput(ctx, this->getIn()),
this->resolveFilterSubregion(ctx, fctx));
}
template <>
bool SkSVGAttributeParser::parse<SkSVGFeGaussianBlur::StdDeviation>(
SkSVGFeGaussianBlur::StdDeviation* stdDeviation) {
std::vector<SkSVGNumberType> values;
if (!this->parse(&values)) {
return false;
}
stdDeviation->fX = values[0];
stdDeviation->fY = values.size() > 1 ? values[1] : values[0];
return true;
}

View File

@ -20,6 +20,7 @@ skia_svg_public = [
"$_include/SkSVGFeColorMatrix.h",
"$_include/SkSVGFeComposite.h",
"$_include/SkSVGFeFlood.h",
"$_include/SkSVGFeGaussianBlur.h",
"$_include/SkSVGFeTurbulence.h",
"$_include/SkSVGFilter.h",
"$_include/SkSVGFilterContext.h",
@ -58,6 +59,7 @@ skia_svg_sources = [
"$_src/SkSVGFeColorMatrix.cpp",
"$_src/SkSVGFeComposite.cpp",
"$_src/SkSVGFeFlood.cpp",
"$_src/SkSVGFeGaussianBlur.cpp",
"$_src/SkSVGFeTurbulence.cpp",
"$_src/SkSVGFilter.cpp",
"$_src/SkSVGFilterContext.cpp",