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/SkottieValue.h"
11 #include "modules/sksg/include/SkSGColorFilter.h"
12 #include "modules/sksg/include/SkSGPaint.h"
13 #include "src/utils/SkJSON.h"
14
15 namespace skottie {
16 namespace internal {
17
attachFillEffect(const skjson::ArrayValue & jprops,sk_sp<sksg::RenderNode> layer) const18 sk_sp<sksg::RenderNode> EffectBuilder::attachFillEffect(const skjson::ArrayValue& jprops,
19 sk_sp<sksg::RenderNode> layer) const {
20 enum : size_t {
21 kFillMask_Index = 0,
22 kAllMasks_Index = 1,
23 kColor_Index = 2,
24 kInvert_Index = 3,
25 kHFeather_Index = 4,
26 kVFeather_Index = 5,
27 kOpacity_Index = 6,
28
29 kMax_Index = kOpacity_Index,
30 };
31
32 if (jprops.size() <= kMax_Index) {
33 return nullptr;
34 }
35
36 const skjson::ObjectValue* color_prop = jprops[ kColor_Index];
37 const skjson::ObjectValue* opacity_prop = jprops[kOpacity_Index];
38 if (!color_prop || !opacity_prop) {
39 return nullptr;
40 }
41 sk_sp<sksg::Color> color_node = fBuilder->attachColor(*color_prop, "v");
42 if (!color_node) {
43 return nullptr;
44 }
45
46 fBuilder->bindProperty<ScalarValue>((*opacity_prop)["v"],
47 [color_node](const ScalarValue& o) {
48 const auto c = color_node->getColor();
49 const auto a = sk_float_round2int_no_saturate(SkTPin(o, 0.0f, 1.0f) * 255);
50 color_node->setColor(SkColorSetA(c, a));
51 });
52
53 return sksg::ModeColorFilter::Make(std::move(layer),
54 std::move(color_node),
55 SkBlendMode::kSrcIn);
56 }
57
58 } // namespace internal
59 } // namespace skottie
60