1/*
2 * Copyright 2019 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/SkSGColorFilter.h"
13#include "modules/sksg/include/SkSGPaint.h"
14#include "src/utils/SkJSON.h"
15
16namespace skottie {
17namespace internal {
18
19namespace {
20
21class FillAdapter final : public AnimatablePropertyContainer {
22public:
23 static sk_sp<FillAdapter> Make(const skjson::ArrayValue& jprops,
24 sk_sp<sksg::RenderNode> layer,
25 const AnimationBuilder& abuilder) {
26 return sk_sp<FillAdapter>(new FillAdapter(jprops, std::move(layer), abuilder));
27 }
28
29 const auto& node() const { return fFilterNode; }
30
31private:
32 FillAdapter(const skjson::ArrayValue& jprops,
33 sk_sp<sksg::RenderNode> layer,
34 const AnimationBuilder& abuilder)
35 : fColorNode(sksg::Color::Make(SK_ColorBLACK))
36 , fFilterNode(sksg::ModeColorFilter::Make(std::move(layer),
37 fColorNode,
38 SkBlendMode::kSrcIn)) {
39 enum : size_t {
40 // kFillMask_Index = 0,
41 // kAllMasks_Index = 1,
42 kColor_Index = 2,
43 // kInvert_Index = 3,
44 // kHFeather_Index = 4,
45 // kVFeather_Index = 5,
46 kOpacity_Index = 6,
47 };
48
49 EffectBinder(jprops, abuilder, this)
50 .bind( kColor_Index, fColor )
51 .bind(kOpacity_Index, fOpacity);
52
53 abuilder.dispatchColorProperty(fColorNode);
54 }
55
56 void onSync() override {
57 const auto c = static_cast<SkColor>(fColor);
58 const auto a =
59 sk_float_round2int_no_saturate(SkColorGetA(c) * SkTPin(fOpacity, 0.0f, 1.0f));
60 fColorNode->setColor(SkColorSetA(c, a));
61 }
62
63 const sk_sp<sksg::Color> fColorNode;
64 const sk_sp<sksg::ModeColorFilter> fFilterNode;
65
66 VectorValue fColor;
67 ScalarValue fOpacity = 1;
68};
69
70} // namespace
71
72sk_sp<sksg::RenderNode> EffectBuilder::attachFillEffect(const skjson::ArrayValue& jprops,
73 sk_sp<sksg::RenderNode> layer) const {
74 return fBuilder->attachDiscardableAdapter<FillAdapter>(jprops, std::move(layer), *fBuilder);
75}
76
77} // namespace internal
78} // namespace skottie
79