Flutter Engine
The Flutter Engine
TransformEffect.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;
26class ObjectValue;
27} // namespace skjson
28
29namespace skottie {
30namespace internal {
31
32namespace {
33
34class TransformEffectAdapter final : public DiscardableAdapterBase<TransformEffectAdapter,
35 sksg::OpacityEffect> {
36public:
37 TransformEffectAdapter(const AnimationBuilder& abuilder,
38 const skjson::ObjectValue* jopacity,
39 const skjson::ObjectValue* jscale_uniform,
40 const skjson::ObjectValue* jscale_width,
41 const skjson::ObjectValue* jscale_height,
44 : INHERITED(sksg::OpacityEffect::Make(std::move(child)))
45 , fTransformAdapter(std::move(tadapter)) {
46 this->bind(abuilder, jopacity , fOpacity );
47 this->bind(abuilder, jscale_uniform, fUniformScale);
48 this->bind(abuilder, jscale_width , fScaleWidth );
49 this->bind(abuilder, jscale_height , fScaleHeight );
50
51 this->attachDiscardableAdapter(fTransformAdapter);
52 }
53
54private:
55 void onSync() override {
56 this->node()->setOpacity(fOpacity * 0.01f);
57
58 // In uniform mode, the scale is based solely in ScaleHeight.
59 const auto scale = SkVector::Make(SkScalarRoundToInt(fUniformScale) ? fScaleHeight
60 : fScaleWidth,
61 fScaleHeight);
62
63 // NB: this triggers an transform adapter -> SG sync.
64 fTransformAdapter->setScale(scale);
65 }
66
67 const sk_sp<TransformAdapter2D> fTransformAdapter;
68
69 ScalarValue fOpacity = 100,
70 fUniformScale = 0, // bool
71 fScaleWidth = 100,
72 fScaleHeight = 100;
73
74 using INHERITED = DiscardableAdapterBase<TransformEffectAdapter, sksg::OpacityEffect>;
75};
76
77} // namespace
78
79sk_sp<sksg::RenderNode> EffectBuilder::attachTransformEffect(const skjson::ArrayValue& jprops,
80 sk_sp<sksg::RenderNode> layer) const {
81 enum : size_t {
82 kAnchorPoint_Index = 0,
83 kPosition_Index = 1,
84 kUniformScale_Index = 2,
85 kScaleHeight_Index = 3,
86 kScaleWidth_Index = 4,
87 kSkew_Index = 5,
88 kSkewAxis_Index = 6,
89 kRotation_Index = 7,
90 kOpacity_Index = 8,
91 // kUseCompShutterAngle_Index = 9,
92 // kShutterAngle_Index = 10,
93 // kSampling_Index = 11,
94 };
95
96 auto transform_adapter = TransformAdapter2D::Make(*fBuilder,
97 GetPropValue(jprops, kAnchorPoint_Index),
98 GetPropValue(jprops, kPosition_Index),
99 nullptr, // scale is handled externally
100 GetPropValue(jprops, kRotation_Index),
101 GetPropValue(jprops, kSkew_Index),
102 GetPropValue(jprops, kSkewAxis_Index));
103 if (!transform_adapter) {
104 return nullptr;
105 }
106
107 auto transform_effect_node = sksg::TransformEffect::Make(std::move(layer),
108 transform_adapter->node());
109 return fBuilder->attachDiscardableAdapter<TransformEffectAdapter>
110 (*fBuilder,
111 GetPropValue(jprops, kOpacity_Index),
112 GetPropValue(jprops, kUniformScale_Index),
113 GetPropValue(jprops, kScaleWidth_Index),
114 GetPropValue(jprops, kScaleHeight_Index),
115 std::move(transform_adapter),
116 std::move(transform_effect_node)
117 );
118}
119
120} // namespace internal
121} // namespace skottie
#define INHERITED(method,...)
Definition: SkRecorder.cpp:128
#define SkScalarRoundToInt(x)
Definition: SkScalar.h:37
void attachDiscardableAdapter(sk_sp< T > adapter) const
Definition: SkottiePriv.h:139
static const skjson::Value & GetPropValue(const skjson::ArrayValue &jprops, size_t prop_index)
Definition: Effects.cpp:181
static sk_sp< TransformEffect > Make(sk_sp< RenderNode > child, sk_sp< Transform > transform)
Definition: SkSGTransform.h:97
SK_API sk_sp< SkDocument > Make(SkWStream *dst, const SkSerialProcs *=nullptr, std::function< void(const SkPicture *)> onEndPage=nullptr)
SkScalar ScalarValue
Definition: SkottieValue.h:22
Definition: Skottie.h:32
Definition: ref_ptr.h:256
const Scalar scale
static constexpr SkPoint Make(float x, float y)
Definition: SkPoint_impl.h:173