blob: a88a479a58e167c751b9c9fa93895431bba01eb0 [file] [log] [blame]
Tyler Dennistondada9602020-11-03 10:04:25 -05001/*
2 * Copyright 2020 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "include/effects/SkImageFilters.h"
9#include "include/effects/SkPerlinNoiseShader.h"
Tyler Denniston57154992020-11-04 16:08:30 -050010#include "modules/svg/include/SkSVGAttributeParser.h"
Tyler Dennistondada9602020-11-03 10:04:25 -050011#include "modules/svg/include/SkSVGFeTurbulence.h"
12#include "modules/svg/include/SkSVGFilterContext.h"
13#include "modules/svg/include/SkSVGRenderContext.h"
14#include "modules/svg/include/SkSVGValue.h"
15
Tyler Denniston57154992020-11-04 16:08:30 -050016bool SkSVGFeTurbulence::parseAndSetAttribute(const char* name, const char* value) {
17 return INHERITED::parseAndSetAttribute(name, value) ||
18 this->setNumOctaves(
19 SkSVGAttributeParser::parse<SkSVGIntegerType>("numOctaves", name, value)) ||
20 this->setSeed(SkSVGAttributeParser::parse<SkSVGNumberType>("seed", name, value)) ||
21 this->setBaseFrequency(SkSVGAttributeParser::parse<SkSVGFeTurbulenceBaseFrequency>(
22 "baseFrequency", name, value, SkSVGFeTurbulence::parse)) ||
23 this->setTurbulenceType(SkSVGAttributeParser::parse<SkSVGFeTurbulenceType>(
24 "type", name, value, SkSVGFeTurbulence::parse));
25}
26
27bool SkSVGFeTurbulence::parse(const char* v, SkSVGFeTurbulenceBaseFrequency* freq) {
28 SkSVGAttributeParser parser(v);
29
30 SkSVGNumberType freqX;
31 if (!parser.parseNumber(&freqX)) {
32 return false;
Tyler Dennistondada9602020-11-03 10:04:25 -050033 }
Tyler Denniston57154992020-11-04 16:08:30 -050034
35 SkSVGNumberType freqY;
36 parser.parseCommaWspToken();
37 if (parser.parseNumber(&freqY)) {
38 *freq = SkSVGFeTurbulenceBaseFrequency(freqX, freqY);
39 } else {
40 *freq = SkSVGFeTurbulenceBaseFrequency(freqX, freqX);
41 }
42
43 return parser.parseEOSToken();
44}
45
46bool SkSVGFeTurbulence::parse(const char* v, SkSVGFeTurbulenceType* type) {
47 SkSVGAttributeParser parser(v);
48 bool parsedValue = false;
49
50 if (parser.parseExpectedStringToken("fractalNoise")) {
51 *type = SkSVGFeTurbulenceType(SkSVGFeTurbulenceType::kFractalNoise);
52 parsedValue = true;
53 } else if (parser.parseExpectedStringToken("turbulence")) {
54 *type = SkSVGFeTurbulenceType(SkSVGFeTurbulenceType::kTurbulence);
55 parsedValue = true;
56 }
57
58 return parsedValue && parser.parseEOSToken();
Tyler Dennistondada9602020-11-03 10:04:25 -050059}
60
61sk_sp<SkImageFilter> SkSVGFeTurbulence::onMakeImageFilter(const SkSVGRenderContext& ctx,
62 const SkSVGFilterContext& fctx) const {
63 const SkISize* tileSize = nullptr; // TODO: needs filter element subregion properties
64
65 sk_sp<SkShader> shader;
66 switch (fTurbulenceType.fType) {
67 case SkSVGFeTurbulenceType::Type::kTurbulence:
68 shader = SkPerlinNoiseShader::MakeTurbulence(
69 fBaseFrequency.freqX(), fBaseFrequency.freqY(), fNumOctaves, fSeed, tileSize);
70 break;
71 case SkSVGFeTurbulenceType::Type::kFractalNoise:
72 shader = SkPerlinNoiseShader::MakeFractalNoise(
73 fBaseFrequency.freqX(), fBaseFrequency.freqY(), fNumOctaves, fSeed, tileSize);
74 break;
75 }
76
77 return SkImageFilters::Shader(shader, fctx.filterEffectsRegion());
78}