1/*
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 "modules/skottie/src/effects/Effects.h"
9
10#include "include/core/SkColorFilter.h"
11#include "include/effects/SkColorMatrix.h"
12#include "include/effects/SkImageFilters.h"
13#include "modules/skottie/src/Adapter.h"
14#include "modules/skottie/src/SkottieValue.h"
15#include "modules/sksg/include/SkSGRenderEffect.h"
16#include "src/utils/SkJSON.h"
17
18namespace skottie::internal {
19
20namespace {
21
22class ShadowAdapter final : public DiscardableAdapterBase<ShadowAdapter,
23 sksg::ExternalImageFilter> {
24public:
25 enum Type {
26 kDropShadow,
27 kInnerShadow,
28 };
29
30 ShadowAdapter(const skjson::ObjectValue& jstyle,
31 const AnimationBuilder& abuilder,
32 Type type)
33 : fType(type) {
34 this->bind(abuilder, jstyle["c"], fColor);
35 this->bind(abuilder, jstyle["o"], fOpacity);
36 this->bind(abuilder, jstyle["a"], fAngle);
37 this->bind(abuilder, jstyle["s"], fSize);
38 this->bind(abuilder, jstyle["d"], fDistance);
39 }
40
41private:
42 void onSync() override {
43 const auto rad = SkDegreesToRadians(180 + fAngle), // 0deg -> left (style)
44 sigma = fSize * kBlurSizeToSigma,
45 opacity = SkTPin(fOpacity / 100, 0.0f, 1.0f);
46 const auto color = static_cast<SkColor4f>(fColor);
47 const auto offset = SkV2{ fDistance * SkScalarCos(rad),
48 -fDistance * SkScalarSin(rad)};
49
50 // Shadow effects largely follow the feDropShadow spec [1]:
51 //
52 // 1) isolate source alpha
53 // 2) apply a gaussian blur
54 // 3) apply an offset
55 // 4) modulate with a flood/color generator
56 // 5) composite with the source
57 //
58 // Note: as an optimization, we can fold #1 and #4 into a single color matrix filter.
59 //
60 // Inner shadow differences:
61 //
62 // a) operates on the inverse of source alpha
63 // b) the result is masked against the source
64 // c) composited on top of source
65 //
66 // [1] https://drafts.fxtf.org/filter-effects/#feDropShadowElement
67
68 // Select and colorize the source alpha channel.
69 SkColorMatrix cm{0, 0, 0, 0, color.fR,
70 0, 0, 0, 0, color.fG,
71 0, 0, 0, 0, color.fB,
72 0, 0, 0, opacity * color.fA, 0};
73
74 // Inner shadows use the alpha inverse.
75 if (fType == Type::kInnerShadow) {
76 cm.preConcat({1, 0, 0, 0, 0,
77 0, 1, 0, 0, 0,
78 0, 0, 1, 0, 0,
79 0, 0, 0,-1, 1});
80 }
81 auto f = SkImageFilters::ColorFilter(SkColorFilters::Matrix(cm), nullptr);
82
83 if (sigma > 0) {
84 f = SkImageFilters::Blur(sigma, sigma, std::move(f));
85 }
86
87 if (!SkScalarNearlyZero(offset.x) || !SkScalarNearlyZero(offset.y)) {
88 f = SkImageFilters::Offset(offset.x, offset.y, std::move(f));
89 }
90
91 sk_sp<SkImageFilter> source;
92
93 if (fType == Type::kInnerShadow) {
94 // Inner shadows draw on top of, and are masked with, the source.
95 f = SkImageFilters::Xfermode(SkBlendMode::kDstIn, std::move(f));
96
97 std::swap(source, f);
98 }
99
100 this->node()->setImageFilter(SkImageFilters::Merge(std::move(f),
101 std::move(source)));
102 }
103
104 const Type fType;
105
106 VectorValue fColor;
107 ScalarValue fOpacity = 100, // percentage
108 fAngle = 0, // degrees
109 fSize = 0,
110 fDistance = 0;
111
112 using INHERITED = DiscardableAdapterBase<ShadowAdapter, sksg::ExternalImageFilter>;
113};
114
115static sk_sp<sksg::RenderNode> make_shadow_effect(const skjson::ObjectValue& jstyle,
116 const AnimationBuilder& abuilder,
117 sk_sp<sksg::RenderNode> layer,
118 ShadowAdapter::Type type) {
119 auto filter_node = abuilder.attachDiscardableAdapter<ShadowAdapter>(jstyle, abuilder, type);
120
121 return sksg::ImageFilterEffect::Make(std::move(layer), std::move(filter_node));
122}
123
124} // namespace
125
126sk_sp<sksg::RenderNode> EffectBuilder::attachDropShadowStyle(const skjson::ObjectValue& jstyle,
127 sk_sp<sksg::RenderNode> layer) const {
128 return make_shadow_effect(jstyle, *fBuilder, std::move(layer),
129 ShadowAdapter::Type::kDropShadow);
130}
131
132sk_sp<sksg::RenderNode> EffectBuilder::attachInnerShadowStyle(const skjson::ObjectValue& jstyle,
133 sk_sp<sksg::RenderNode> layer) const {
134 return make_shadow_effect(jstyle, *fBuilder, std::move(layer),
135 ShadowAdapter::Type::kInnerShadow);
136}
137
138} // namespace skottie::internal
139