blob: 3adb5540266684424a54fda193eca798e969885c [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"
10#include "modules/svg/include/SkSVGFeTurbulence.h"
11#include "modules/svg/include/SkSVGFilterContext.h"
12#include "modules/svg/include/SkSVGRenderContext.h"
13#include "modules/svg/include/SkSVGValue.h"
14
15void SkSVGFeTurbulence::onSetAttribute(SkSVGAttribute attr, const SkSVGValue& v) {
16 switch (attr) {
17 case SkSVGAttribute::kFeTurbulenceBaseFrequency:
18 if (const auto* f = v.as<SkSVGFeTurbulenceBaseFrequencyValue>()) {
19 this->setBaseFrequency(*f);
20 }
21 break;
22 case SkSVGAttribute::kFeTurbulenceNumOctaves:
23 if (const auto* n = v.as<SkSVGIntegerValue>()) {
24 this->setNumOctaves(*n);
25 }
26 break;
27 case SkSVGAttribute::kFeTurbulenceSeed:
28 if (const auto* s = v.as<SkSVGNumberValue>()) {
29 this->setSeed(*s);
30 }
31 break;
32 case SkSVGAttribute::kFeTurbulenceType:
33 if (const auto* t = v.as<SkSVGFeTurbulenceTypeValue>()) {
34 this->setTurbulenceType(*t);
35 }
36 break;
37 default:
38 this->INHERITED::onSetAttribute(attr, v);
39 }
40}
41
42sk_sp<SkImageFilter> SkSVGFeTurbulence::onMakeImageFilter(const SkSVGRenderContext& ctx,
43 const SkSVGFilterContext& fctx) const {
44 const SkISize* tileSize = nullptr; // TODO: needs filter element subregion properties
45
46 sk_sp<SkShader> shader;
47 switch (fTurbulenceType.fType) {
48 case SkSVGFeTurbulenceType::Type::kTurbulence:
49 shader = SkPerlinNoiseShader::MakeTurbulence(
50 fBaseFrequency.freqX(), fBaseFrequency.freqY(), fNumOctaves, fSeed, tileSize);
51 break;
52 case SkSVGFeTurbulenceType::Type::kFractalNoise:
53 shader = SkPerlinNoiseShader::MakeFractalNoise(
54 fBaseFrequency.freqX(), fBaseFrequency.freqY(), fNumOctaves, fSeed, tileSize);
55 break;
56 }
57
58 return SkImageFilters::Shader(shader, fctx.filterEffectsRegion());
59}