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