1 /*
2 * Copyright 2021 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/core/SkImageFilter.h"
9 #include "include/core/SkMatrix.h"
10 #include "include/core/SkRefCnt.h"
11 #include "include/core/SkSamplingOptions.h"
12 #include "include/effects/SkImageFilters.h"
13 #include "modules/skottie/src/Adapter.h"
14 #include "modules/skottie/src/SkottiePriv.h"
15 #include "modules/skottie/src/SkottieValue.h"
16 #include "modules/skottie/src/effects/Effects.h"
17 #include "modules/sksg/include/SkSGRenderEffect.h"
18 #include "modules/sksg/include/SkSGRenderNode.h"
19
20 #include <cstddef>
21 #include <utility>
22
23 namespace skjson {
24 class ArrayValue;
25 }
26
27 namespace skottie::internal {
28
29 namespace {
30
31 class DirectionalBlurAdapter final : public DiscardableAdapterBase<DirectionalBlurAdapter,
32 sksg::ExternalImageFilter> {
33 public:
DirectionalBlurAdapter(const skjson::ArrayValue & jprops,const AnimationBuilder & abuilder)34 DirectionalBlurAdapter(const skjson::ArrayValue& jprops,
35 const AnimationBuilder& abuilder)
36 : INHERITED(sksg::ExternalImageFilter::Make())
37 {
38 enum : size_t {
39 kDirection_Index = 0,
40 kBlurLength_Index = 1,
41 };
42
43
44 EffectBinder(jprops, abuilder, this)
45 .bind( kDirection_Index, fDirection)
46 .bind( kBlurLength_Index, fBlurLength);
47 }
48 private:
onSync()49 void onSync() override {
50 const auto rot = fDirection - 90;
51 auto filter =
52 SkImageFilters::MatrixTransform(SkMatrix::RotateDeg(rot),
53 SkSamplingOptions(SkFilterMode::kLinear),
54 SkImageFilters::Blur(fBlurLength * kBlurSizeToSigma, 0,
55 SkImageFilters::MatrixTransform(SkMatrix::RotateDeg(-rot),
56 SkSamplingOptions(SkFilterMode::kLinear), nullptr)));
57 this->node()->setImageFilter(std::move(filter));
58 }
59
60 ScalarValue fDirection = 0;
61 ScalarValue fBlurLength = 0;
62
63 using INHERITED = DiscardableAdapterBase<DirectionalBlurAdapter, sksg::ExternalImageFilter>;
64 };
65
66 } // namespace
67
attachDirectionalBlurEffect(const skjson::ArrayValue & jprops,sk_sp<sksg::RenderNode> layer) const68 sk_sp<sksg::RenderNode> EffectBuilder::attachDirectionalBlurEffect(const skjson::ArrayValue& jprops,
69 sk_sp<sksg::RenderNode> layer) const {
70 auto imageFilterNode = fBuilder->attachDiscardableAdapter<DirectionalBlurAdapter>(jprops,
71 *fBuilder);
72 return sksg::ImageFilterEffect::Make(std::move(layer), std::move(imageFilterNode));
73 }
74
75 } // namespace skottie::internal
76