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 "include/core/SkCanvas.h"
11 #include "include/core/SkPictureRecorder.h"
12 #include "include/core/SkShader.h"
13 #include "include/effects/SkGradientShader.h"
14 #include "include/private/base/SkTPin.h"
15 #include "modules/skottie/src/Adapter.h"
16 #include "modules/skottie/src/SkottieValue.h"
17 #include "modules/sksg/include/SkSGRenderNode.h"
18 #include "src/utils/SkJSON.h"
19
20 #include <cmath>
21
22 namespace skottie {
23 namespace internal {
24
25 namespace {
26
27 // AE motion tile effect semantics
28 // (https://helpx.adobe.com/after-effects/using/stylize-effects.html#motion_tile_effect):
29 //
30 // - the full content of the layer is mapped to a tile: tile_center, tile_width, tile_height
31 //
32 // - tiles are repeated in both dimensions to fill the output area: output_width, output_height
33 //
34 // - tiling mode is either kRepeat (default) or kMirror (when mirror_edges == true)
35 //
36 // - for a non-zero phase, alternating vertical columns (every other column) are offset by
37 // the specified amount
38 //
39 // - when horizontal_phase is true, the phase is applied to horizontal rows instead of columns
40 //
41 class TileRenderNode final : public sksg::CustomRenderNode {
42 public:
TileRenderNode(const SkSize & size,sk_sp<sksg::RenderNode> layer)43 TileRenderNode(const SkSize& size, sk_sp<sksg::RenderNode> layer)
44 : INHERITED({std::move(layer)})
45 , fLayerSize(size) {}
46
47 SG_ATTRIBUTE(TileCenter , SkPoint , fTileCenter )
48 SG_ATTRIBUTE(TileWidth , SkScalar, fTileW )
49 SG_ATTRIBUTE(TileHeight , SkScalar, fTileH )
50 SG_ATTRIBUTE(OutputWidth , SkScalar, fOutputW )
51 SG_ATTRIBUTE(OutputHeight , SkScalar, fOutputH )
52 SG_ATTRIBUTE(Phase , SkScalar, fPhase )
53 SG_ATTRIBUTE(MirrorEdges , bool , fMirrorEdges )
54 SG_ATTRIBUTE(HorizontalPhase, bool , fHorizontalPhase)
55
56 protected:
onNodeAt(const SkPoint &) const57 const RenderNode* onNodeAt(const SkPoint&) const override { return nullptr; } // no hit-testing
58
onRevalidate(sksg::InvalidationController * ic,const SkMatrix & ctm)59 SkRect onRevalidate(sksg::InvalidationController* ic, const SkMatrix& ctm) override {
60 // Re-record the layer picture if needed.
61 if (!fLayerPicture || this->hasChildrenInval()) {
62 SkASSERT(this->children().size() == 1ul);
63 const auto& layer = this->children()[0];
64
65 layer->revalidate(ic, ctm);
66
67 SkPictureRecorder recorder;
68 layer->render(recorder.beginRecording(fLayerSize.width(), fLayerSize.height()));
69 fLayerPicture = recorder.finishRecordingAsPicture();
70 }
71
72 // tileW and tileH use layer size percentage units.
73 const auto tileW = SkTPin(fTileW, 0.0f, 100.0f) * 0.01f * fLayerSize.width(),
74 tileH = SkTPin(fTileH, 0.0f, 100.0f) * 0.01f * fLayerSize.height();
75 const auto tile_size = SkSize::Make(std::max(tileW, 1.0f),
76 std::max(tileH, 1.0f));
77 const auto tile = SkRect::MakeXYWH(fTileCenter.fX - 0.5f * tile_size.width(),
78 fTileCenter.fY - 0.5f * tile_size.height(),
79 tile_size.width(),
80 tile_size.height());
81
82 const auto layerShaderMatrix = SkMatrix::RectToRect(
83 SkRect::MakeWH(fLayerSize.width(), fLayerSize.height()), tile);
84
85 const auto tm = fMirrorEdges ? SkTileMode::kMirror : SkTileMode::kRepeat;
86 auto layer_shader = fLayerPicture->makeShader(tm, tm, SkFilterMode::kLinear,
87 &layerShaderMatrix, nullptr);
88
89 if (fPhase && layer_shader && tile.isFinite()) {
90 // To implement AE phase semantics, we construct a mask shader for the pass-through
91 // rows/columns. We then draw the layer content through this mask, and then again
92 // through the inverse mask with a phase shift.
93 const auto phase_vec = fHorizontalPhase
94 ? SkVector::Make(tile.width(), 0)
95 : SkVector::Make(0, tile.height());
96 const auto phase_shift = SkVector::Make(phase_vec.fX, phase_vec.fY)
97 * std::fmod(fPhase * (1/360.0f), 1);
98 const auto phase_shader_matrix = SkMatrix::Translate(phase_shift.x(), phase_shift.y());
99
100 // The mask is generated using a step gradient shader, spanning 2 x tile width/height,
101 // and perpendicular to the phase vector.
102 static constexpr SkColor colors[] = { 0xffffffff, 0x00000000 };
103 static constexpr SkScalar pos[] = { 0.5f, 0.5f };
104
105 const SkPoint pts[] = {{ tile.x(), tile.y() },
106 { tile.x() + 2 * (tile.width() - phase_vec.fX),
107 tile.y() + 2 * (tile.height() - phase_vec.fY) }};
108
109 auto mask_shader = SkGradientShader::MakeLinear(pts, colors, pos,
110 std::size(colors),
111 SkTileMode::kRepeat);
112
113 // First drawing pass: in-place masked layer content.
114 fMainPassShader = SkShaders::Blend(SkBlendMode::kSrcIn , mask_shader, layer_shader);
115 // Second pass: phased-shifted layer content, with an inverse mask.
116 fPhasePassShader = SkShaders::Blend(SkBlendMode::kSrcOut, mask_shader, layer_shader)
117 ->makeWithLocalMatrix(phase_shader_matrix);
118 } else {
119 fMainPassShader = std::move(layer_shader);
120 fPhasePassShader = nullptr;
121 }
122
123 // outputW and outputH also use layer size percentage units.
124 const auto outputW = fOutputW * 0.01f * fLayerSize.width(),
125 outputH = fOutputH * 0.01f * fLayerSize.height();
126
127 return SkRect::MakeXYWH((fLayerSize.width() - outputW) * 0.5f,
128 (fLayerSize.height() - outputH) * 0.5f,
129 outputW, outputH);
130 }
131
onRender(SkCanvas * canvas,const RenderContext * ctx) const132 void onRender(SkCanvas* canvas, const RenderContext* ctx) const override {
133 // AE allow one of the tile dimensions to collapse, but not both.
134 if (this->bounds().isEmpty() || (fTileW <= 0 && fTileH <= 0)) {
135 return;
136 }
137
138 SkPaint paint;
139 paint.setAntiAlias(true);
140
141 if (ctx) {
142 // apply any pending paint effects via the shader paint
143 ctx->modulatePaint(canvas->getLocalToDeviceAs3x3(), &paint);
144 }
145
146 paint.setShader(fMainPassShader);
147 canvas->drawRect(this->bounds(), paint);
148
149 if (fPhasePassShader) {
150 paint.setShader(fPhasePassShader);
151 canvas->drawRect(this->bounds(), paint);
152 }
153 }
154
155 private:
156 const SkSize fLayerSize;
157
158 SkPoint fTileCenter = { 0, 0 };
159 SkScalar fTileW = 1,
160 fTileH = 1,
161 fOutputW = 1,
162 fOutputH = 1,
163 fPhase = 0;
164 bool fMirrorEdges = false;
165 bool fHorizontalPhase = false;
166
167 // These are computed/cached on revalidation.
168 sk_sp<SkPicture> fLayerPicture; // cached picture for layer content
169 sk_sp<SkShader> fMainPassShader, // shader for the main tile(s)
170 fPhasePassShader; // shader for the phased tile(s)
171
172 using INHERITED = sksg::CustomRenderNode;
173 };
174
175 class MotionTileAdapter final : public DiscardableAdapterBase<MotionTileAdapter, TileRenderNode> {
176 public:
MotionTileAdapter(const skjson::ArrayValue & jprops,sk_sp<sksg::RenderNode> layer,const AnimationBuilder & abuilder,const SkSize & layer_size)177 MotionTileAdapter(const skjson::ArrayValue& jprops,
178 sk_sp<sksg::RenderNode> layer,
179 const AnimationBuilder& abuilder,
180 const SkSize& layer_size)
181 : INHERITED(sk_make_sp<TileRenderNode>(layer_size, std::move(layer))) {
182
183 enum : size_t {
184 kTileCenter_Index = 0,
185 kTileWidth_Index = 1,
186 kTileHeight_Index = 2,
187 kOutputWidth_Index = 3,
188 kOutputHeight_Index = 4,
189 kMirrorEdges_Index = 5,
190 kPhase_Index = 6,
191 kHorizontalPhaseShift_Index = 7,
192 };
193
194 EffectBinder(jprops, abuilder, this)
195 .bind( kTileCenter_Index, fTileCenter )
196 .bind( kTileWidth_Index, fTileW )
197 .bind( kTileHeight_Index, fTileH )
198 .bind( kOutputWidth_Index, fOutputW )
199 .bind( kOutputHeight_Index, fOutputH )
200 .bind( kMirrorEdges_Index, fMirrorEdges )
201 .bind( kPhase_Index, fPhase )
202 .bind(kHorizontalPhaseShift_Index, fHorizontalPhase);
203 }
204
205 private:
onSync()206 void onSync() override {
207 const auto& tiler = this->node();
208
209 tiler->setTileCenter({fTileCenter.x, fTileCenter.y});
210 tiler->setTileWidth (fTileW);
211 tiler->setTileHeight(fTileH);
212 tiler->setOutputWidth (fOutputW);
213 tiler->setOutputHeight(fOutputH);
214 tiler->setPhase(fPhase);
215 tiler->setMirrorEdges(SkToBool(fMirrorEdges));
216 tiler->setHorizontalPhase(SkToBool(fHorizontalPhase));
217 }
218
219 Vec2Value fTileCenter = {0,0};
220 ScalarValue fTileW = 1,
221 fTileH = 1,
222 fOutputW = 1,
223 fOutputH = 1,
224 fMirrorEdges = 0,
225 fPhase = 0,
226 fHorizontalPhase = 0;
227
228 using INHERITED = DiscardableAdapterBase<MotionTileAdapter, TileRenderNode>;
229 };
230
231 } // namespace
232
attachMotionTileEffect(const skjson::ArrayValue & jprops,sk_sp<sksg::RenderNode> layer) const233 sk_sp<sksg::RenderNode> EffectBuilder::attachMotionTileEffect(const skjson::ArrayValue& jprops,
234 sk_sp<sksg::RenderNode> layer) const {
235 return fBuilder->attachDiscardableAdapter<MotionTileAdapter>(jprops,
236 std::move(layer),
237 *fBuilder,
238 fLayerSize);
239 }
240
241 } // namespace internal
242 } // namespace skottie
243