xref: /aosp_15_r20/external/skia/modules/skottie/src/layers/shapelayer/Polystar.cpp (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 #include "include/core/SkM44.h"
9 #include "include/core/SkPathBuilder.h"
10 #include "include/core/SkPoint.h"
11 #include "include/core/SkRefCnt.h"
12 #include "include/core/SkScalar.h"
13 #include "include/private/base/SkFloatingPoint.h"
14 #include "include/private/base/SkTPin.h"
15 #include "include/private/base/SkTo.h"
16 #include "modules/skottie/include/Skottie.h"
17 #include "modules/skottie/src/Adapter.h"
18 #include "modules/skottie/src/SkottieJson.h"
19 #include "modules/skottie/src/SkottiePriv.h"
20 #include "modules/skottie/src/SkottieValue.h"
21 #include "modules/skottie/src/layers/shapelayer/ShapeLayer.h"
22 #include "modules/sksg/include/SkSGPath.h"
23 #include "src/utils/SkJSON.h"
24 
25 #include <array>
26 #include <cmath>
27 #include <cstddef>
28 
29 namespace sksg {
30 class GeometryNode;
31 }
32 
33 namespace skottie {
34 namespace internal {
35 
36 namespace  {
37 
38 class PolystarGeometryAdapter final :
39         public DiscardableAdapterBase<PolystarGeometryAdapter, sksg::Path> {
40 public:
41     enum class Type {
42         kStar, kPoly,
43     };
44 
PolystarGeometryAdapter(const skjson::ObjectValue & jstar,const AnimationBuilder * abuilder,Type t)45     PolystarGeometryAdapter(const skjson::ObjectValue& jstar,
46                             const AnimationBuilder* abuilder, Type t)
47         : fType(t) {
48         this->bind(*abuilder, jstar["pt"], fPointCount    );
49         this->bind(*abuilder, jstar["p" ], fPosition      );
50         this->bind(*abuilder, jstar["r" ], fRotation      );
51         this->bind(*abuilder, jstar["ir"], fInnerRadius   );
52         this->bind(*abuilder, jstar["or"], fOuterRadius   );
53         this->bind(*abuilder, jstar["is"], fInnerRoundness);
54         this->bind(*abuilder, jstar["os"], fOuterRoundness);
55     }
56 
57 private:
onSync()58     void onSync() override {
59         static constexpr int kMaxPointCount = 100000;
60         const auto count = SkToUInt(SkTPin(SkScalarRoundToInt(fPointCount), 0, kMaxPointCount));
61         const auto arc   = sk_ieee_float_divide(SK_ScalarPI * 2, count);
62 
63         const auto pt_on_circle = [](const SkV2& c, SkScalar r, SkScalar a) {
64             return SkPoint::Make(c.x + r * std::cos(a),
65                                  c.y + r * std::sin(a));
66         };
67 
68         // TODO: inner/outer "roundness"?
69 
70         SkPathBuilder poly;
71 
72         auto angle = SkDegreesToRadians(fRotation - 90);
73         poly.moveTo(pt_on_circle(fPosition, fOuterRadius, angle));
74         poly.incReserve(fType == Type::kStar ? count * 2 : count);
75 
76         for (unsigned i = 0; i < count; ++i) {
77             if (fType == Type::kStar) {
78                 poly.lineTo(pt_on_circle(fPosition, fInnerRadius, angle + arc * 0.5f));
79             }
80             angle += arc;
81             poly.lineTo(pt_on_circle(fPosition, fOuterRadius, angle));
82         }
83 
84         poly.close();
85         this->node()->setPath(poly.detach());
86     }
87 
88     const Type fType;
89 
90     Vec2Value   fPosition       = {0,0};
91     ScalarValue fPointCount     = 0,
92                 fRotation       = 0,
93                 fInnerRadius    = 0,
94                 fOuterRadius    = 0,
95                 fInnerRoundness = 0,
96                 fOuterRoundness = 0;
97 };
98 
99 } // namespace
100 
AttachPolystarGeometry(const skjson::ObjectValue & jstar,const AnimationBuilder * abuilder)101 sk_sp<sksg::GeometryNode> ShapeBuilder::AttachPolystarGeometry(const skjson::ObjectValue& jstar,
102                                                                const AnimationBuilder* abuilder) {
103     static constexpr PolystarGeometryAdapter::Type gTypes[] = {
104         PolystarGeometryAdapter::Type::kStar, // "sy": 1
105         PolystarGeometryAdapter::Type::kPoly, // "sy": 2
106     };
107 
108     const auto type = ParseDefault<size_t>(jstar["sy"], 0) - 1;
109     if (type >= std::size(gTypes)) {
110         abuilder->log(Logger::Level::kError, &jstar, "Unknown polystar type.");
111         return nullptr;
112     }
113 
114     return abuilder->attachDiscardableAdapter<PolystarGeometryAdapter>
115                 (jstar, abuilder, gTypes[type]);
116 }
117 
118 } // namespace internal
119 } // namespace skottie
120