xref: /aosp_15_r20/external/skia/modules/skottie/src/Transform.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 #ifndef SkottieTransform_DEFINED
9 #define SkottieTransform_DEFINED
10 
11 #include "include/core/SkM44.h"
12 #include "include/core/SkMatrix.h"
13 #include "include/core/SkPoint.h"
14 #include "modules/skottie/src/Adapter.h"
15 #include "modules/skottie/src/SkottieValue.h"
16 #include "modules/sksg/include/SkSGTransform.h"
17 
18 #include <utility>
19 
20 namespace skjson {
21 class ObjectValue;
22 
23 }  // namespace skjson
24 
25 namespace skottie {
26 namespace internal {
27 class AnimationBuilder;
28 
29 class TransformAdapter2D final : public DiscardableAdapterBase<TransformAdapter2D,
30                                                                sksg::Matrix<SkMatrix>> {
31 public:
32     TransformAdapter2D(const AnimationBuilder&,
33                        const skjson::ObjectValue* janchor_point,
34                        const skjson::ObjectValue* jposition,
35                        const skjson::ObjectValue* jscale,
36                        const skjson::ObjectValue* jrotation,
37                        const skjson::ObjectValue* jskew,
38                        const skjson::ObjectValue* jskew_axis,
39                        bool auto_orient = false);
40     ~TransformAdapter2D() override;
41 
42     // Accessors needed for public property APIs.
43     // TODO: introduce a separate public type.
44     SkPoint getAnchorPoint() const;
45     void    setAnchorPoint(const SkPoint&);
46 
47     SkPoint getPosition() const;
48     void    setPosition(const SkPoint&);
49 
50     SkVector getScale() const;
51     void     setScale(const SkVector&);
52 
getRotation()53     float getRotation() const  { return fRotation; }
54     void  setRotation(float r);
55 
getSkew()56     float getSkew() const   { return fSkew; }
57     void  setSkew(float sk);
58 
getSkewAxis()59     float getSkewAxis() const    { return fSkewAxis; }
60     void  setSkewAxis(float sa );
61 
62     SkMatrix totalMatrix() const;
63 
64 private:
65     void onSync() override;
66 
67     Vec2Value   fAnchorPoint = {   0,   0 },
68                 fPosition    = {   0,   0 },
69                 fScale       = { 100, 100 };
70     ScalarValue fRotation    = 0,
71                 fSkew        = 0,
72                 fSkewAxis    = 0,
73                 fOrientation = 0; // additional rotation component controlled by auto-orient
74 
75     using INHERITED = DiscardableAdapterBase<TransformAdapter2D, sksg::Matrix<SkMatrix>>;
76 };
77 
78 class TransformAdapter3D : public DiscardableAdapterBase<TransformAdapter3D, sksg::Matrix<SkM44>> {
79 public:
80     TransformAdapter3D(const skjson::ObjectValue&, const AnimationBuilder&);
81     ~TransformAdapter3D() override;
82 
83     virtual SkM44 totalMatrix() const;
84 
85 protected:
86     SkV3 anchor_point() const;
87     SkV3 position() const;
88     SkV3 rotation() const;
89 
90 private:
91     void onSync() final;
92 
93     VectorValue fAnchorPoint,
94                 fPosition,
95                 fOrientation,
96                 fScale = { 100, 100, 100 };
97     ScalarValue fRx = 0,
98                 fRy = 0,
99                 fRz = 0;
100 
101     using INHERITED = DiscardableAdapterBase<TransformAdapter3D, sksg::Matrix<SkM44>>;
102 };
103 
104 } // namespace internal
105 } // namespace skottie
106 
107 #endif // SkottieTransform_DEFINED
108