Tyler Denniston | dada960 | 2020-11-03 10:04:25 -0500 | [diff] [blame] | 1 | /* |
| 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 Denniston | 5715499 | 2020-11-04 16:08:30 -0500 | [diff] [blame] | 10 | #include "modules/svg/include/SkSVGAttributeParser.h" |
Tyler Denniston | dada960 | 2020-11-03 10:04:25 -0500 | [diff] [blame] | 11 | #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 Denniston | 5715499 | 2020-11-04 16:08:30 -0500 | [diff] [blame] | 16 | bool 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>( |
Florin Malita | 401321d | 2020-11-05 11:27:24 -0500 | [diff] [blame] | 22 | "baseFrequency", name, value)) || |
Tyler Denniston | 5715499 | 2020-11-04 16:08:30 -0500 | [diff] [blame] | 23 | this->setTurbulenceType(SkSVGAttributeParser::parse<SkSVGFeTurbulenceType>( |
Florin Malita | 401321d | 2020-11-05 11:27:24 -0500 | [diff] [blame] | 24 | "type", name, value)); |
Tyler Denniston | 5715499 | 2020-11-04 16:08:30 -0500 | [diff] [blame] | 25 | } |
| 26 | |
Florin Malita | 401321d | 2020-11-05 11:27:24 -0500 | [diff] [blame] | 27 | template <> |
| 28 | bool SkSVGAttributeParser::parse<SkSVGFeTurbulenceBaseFrequency>( |
| 29 | SkSVGFeTurbulenceBaseFrequency* freq) { |
Tyler Denniston | 5715499 | 2020-11-04 16:08:30 -0500 | [diff] [blame] | 30 | SkSVGNumberType freqX; |
Tyler Denniston | 4c6f57a | 2020-11-30 15:31:32 -0500 | [diff] [blame] | 31 | if (!this->parse(&freqX)) { |
Tyler Denniston | 5715499 | 2020-11-04 16:08:30 -0500 | [diff] [blame] | 32 | return false; |
Tyler Denniston | dada960 | 2020-11-03 10:04:25 -0500 | [diff] [blame] | 33 | } |
Tyler Denniston | 5715499 | 2020-11-04 16:08:30 -0500 | [diff] [blame] | 34 | |
| 35 | SkSVGNumberType freqY; |
Florin Malita | 401321d | 2020-11-05 11:27:24 -0500 | [diff] [blame] | 36 | this->parseCommaWspToken(); |
Tyler Denniston | 4c6f57a | 2020-11-30 15:31:32 -0500 | [diff] [blame] | 37 | if (this->parse(&freqY)) { |
Tyler Denniston | 5715499 | 2020-11-04 16:08:30 -0500 | [diff] [blame] | 38 | *freq = SkSVGFeTurbulenceBaseFrequency(freqX, freqY); |
| 39 | } else { |
| 40 | *freq = SkSVGFeTurbulenceBaseFrequency(freqX, freqX); |
| 41 | } |
| 42 | |
Florin Malita | 401321d | 2020-11-05 11:27:24 -0500 | [diff] [blame] | 43 | return this->parseEOSToken(); |
Tyler Denniston | 5715499 | 2020-11-04 16:08:30 -0500 | [diff] [blame] | 44 | } |
| 45 | |
Florin Malita | 401321d | 2020-11-05 11:27:24 -0500 | [diff] [blame] | 46 | template <> |
| 47 | bool SkSVGAttributeParser::parse<SkSVGFeTurbulenceType>(SkSVGFeTurbulenceType* type) { |
Tyler Denniston | 5715499 | 2020-11-04 16:08:30 -0500 | [diff] [blame] | 48 | bool parsedValue = false; |
| 49 | |
Florin Malita | 401321d | 2020-11-05 11:27:24 -0500 | [diff] [blame] | 50 | if (this->parseExpectedStringToken("fractalNoise")) { |
Tyler Denniston | 5715499 | 2020-11-04 16:08:30 -0500 | [diff] [blame] | 51 | *type = SkSVGFeTurbulenceType(SkSVGFeTurbulenceType::kFractalNoise); |
| 52 | parsedValue = true; |
Florin Malita | 401321d | 2020-11-05 11:27:24 -0500 | [diff] [blame] | 53 | } else if (this->parseExpectedStringToken("turbulence")) { |
Tyler Denniston | 5715499 | 2020-11-04 16:08:30 -0500 | [diff] [blame] | 54 | *type = SkSVGFeTurbulenceType(SkSVGFeTurbulenceType::kTurbulence); |
| 55 | parsedValue = true; |
| 56 | } |
| 57 | |
Florin Malita | 401321d | 2020-11-05 11:27:24 -0500 | [diff] [blame] | 58 | return parsedValue && this->parseEOSToken(); |
Tyler Denniston | dada960 | 2020-11-03 10:04:25 -0500 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | sk_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 | |
Tyler Denniston | 62a683e | 2020-12-11 11:47:55 -0500 | [diff] [blame] | 77 | return SkImageFilters::Shader(shader, this->resolveFilterSubregion(ctx, fctx)); |
Tyler Denniston | dada960 | 2020-11-03 10:04:25 -0500 | [diff] [blame] | 78 | } |