Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
DropShadowEffect.cpp
Go to the documentation of this file.
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
20
21#include <cstddef>
22#include <utility>
23
24namespace skjson {
25class ArrayValue;
26}
27
28namespace skottie {
29namespace internal {
30
31namespace {
32
33class DropShadowAdapter final : public AnimatablePropertyContainer {
34public:
37 const AnimationBuilder& abuilder) {
38 enum : size_t {
39 kShadowColor_Index = 0,
40 kOpacity_Index = 1,
41 kDirection_Index = 2,
42 kDistance_Index = 3,
43 kSoftness_Index = 4,
44 kShadowOnly_Index = 5,
45 };
46
47 sk_sp<DropShadowAdapter> adapter(new DropShadowAdapter(std::move(layer)));
48
49 EffectBinder(jprops, abuilder, adapter.get())
50 .bind(kShadowColor_Index, adapter->fColor )
51 .bind( kOpacity_Index, adapter->fOpacity )
52 .bind( kDirection_Index, adapter->fDirection)
53 .bind( kDistance_Index, adapter->fDistance )
54 .bind( kSoftness_Index, adapter->fSoftness )
55 .bind( kShadowOnly_Index, adapter->fShdwOnly );
56
57 return adapter;
58 }
59
60 const sk_sp<sksg::RenderNode>& node() const { return fImageFilterEffect; }
61
62private:
63 explicit DropShadowAdapter(sk_sp<sksg::RenderNode> layer)
64 : fDropShadow(sksg::DropShadowImageFilter::Make())
65 , fImageFilterEffect(sksg::ImageFilterEffect::Make(std::move(layer), fDropShadow)) {}
66
67 void onSync() override {
68 // fColor -> RGB, fOpacity -> A
69 const SkColor color = fColor;
70 fDropShadow->setColor(SkColorSetA(color, SkTPin(SkScalarRoundToInt(fOpacity), 0, 255)));
71
72 // The offset is specified in terms of a bearing + distance.
73 const auto rad = SkDegreesToRadians(90 - fDirection);
74 fDropShadow->setOffset(SkVector::Make( fDistance * SkScalarCos(rad),
75 -fDistance * SkScalarSin(rad)));
76
77 const auto sigma = fSoftness * kBlurSizeToSigma;
78 fDropShadow->setSigma(SkVector::Make(sigma, sigma));
79
80 fDropShadow->setMode(SkToBool(fShdwOnly)
82 : sksg::DropShadowImageFilter::Mode::kShadowAndForeground);
83 }
84
85 const sk_sp<sksg::DropShadowImageFilter> fDropShadow;
86 const sk_sp<sksg::RenderNode> fImageFilterEffect;
87
88 ColorValue fColor = { 0, 0, 0, 1 };
89 ScalarValue fOpacity = 255,
90 fDirection = 0,
91 fDistance = 0,
92 fSoftness = 0,
93 fShdwOnly = 0;
94};
95
96} // namespace
97
98sk_sp<sksg::RenderNode> EffectBuilder::attachDropShadowEffect(const skjson::ArrayValue& jprops,
99 sk_sp<sksg::RenderNode> layer) const {
100 return fBuilder->attachDiscardableAdapter<DropShadowAdapter>(jprops,
101 std::move(layer),
102 *fBuilder);
103}
104
105} // namespace internal
106} // namespace skottie
SkColor4f color
uint32_t SkColor
Definition SkColor.h:37
static constexpr SkColor SkColorSetA(SkColor c, U8CPU a)
Definition SkColor.h:82
static std::unique_ptr< SkEncoder > Make(SkWStream *dst, const SkPixmap *src, const SkYUVAPixmaps *srcYUVA, const SkColorSpace *srcYUVAColorSpace, const SkJpegEncoder::Options &options)
#define SkDegreesToRadians(degrees)
Definition SkScalar.h:77
#define SkScalarSin(radians)
Definition SkScalar.h:45
#define SkScalarRoundToInt(x)
Definition SkScalar.h:37
#define SkScalarCos(radians)
Definition SkScalar.h:46
static constexpr const T & SkTPin(const T &x, const T &lo, const T &hi)
Definition SkTPin.h:19
static constexpr bool SkToBool(const T &x)
Definition SkTo.h:35
void attachDiscardableAdapter(sk_sp< T > adapter) const
static constexpr float kBlurSizeToSigma
Definition SkottiePriv.h:47
SkScalar ScalarValue
Definition Skottie.h:32
Definition ref_ptr.h:256
static constexpr SkPoint Make(float x, float y)