xref: /aosp_15_r20/external/skia/modules/skottie/src/layers/shapelayer/Rectangle.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/SkPathTypes.h"
9 #include "include/core/SkRRect.h"
10 #include "include/core/SkRect.h"
11 #include "include/core/SkRefCnt.h"
12 #include "modules/skottie/src/Adapter.h"
13 #include "modules/skottie/src/SkottieJson.h"
14 #include "modules/skottie/src/SkottiePriv.h"
15 #include "modules/skottie/src/SkottieValue.h"
16 #include "modules/skottie/src/layers/shapelayer/ShapeLayer.h"
17 #include "modules/sksg/include/SkSGRect.h"
18 #include "src/utils/SkJSON.h"
19 
20 namespace sksg {
21 class GeometryNode;
22 }
23 
24 namespace skottie {
25 namespace internal {
26 
27 namespace  {
28 
29 class RectangleGeometryAdapter final :
30         public DiscardableAdapterBase<RectangleGeometryAdapter, sksg::RRect> {
31 public:
RectangleGeometryAdapter(const skjson::ObjectValue & jrect,const AnimationBuilder * abuilder)32     RectangleGeometryAdapter(const skjson::ObjectValue& jrect,
33                              const AnimationBuilder* abuilder) {
34         this->node()->setDirection(ParseDefault(jrect["d"], -1) == 3 ? SkPathDirection::kCCW
35                                                                      : SkPathDirection::kCW);
36         this->node()->setInitialPointIndex(2); // starting point: (Right, Top - radius.y)
37 
38         this->bind(*abuilder, jrect["s"], fSize     );
39         this->bind(*abuilder, jrect["p"], fPosition );
40         this->bind(*abuilder, jrect["r"], fRoundness);
41     }
42 
43 private:
onSync()44     void onSync() override {
45         const auto bounds = SkRect::MakeXYWH(fPosition.x - fSize.x / 2,
46                                              fPosition.y - fSize.y / 2,
47                                              fSize.x, fSize.y);
48 
49         this->node()->setRRect(SkRRect::MakeRectXY(bounds, fRoundness, fRoundness));
50     }
51 
52     Vec2Value   fSize      = {0,0},
53                 fPosition  = {0,0}; // center
54     ScalarValue fRoundness = 0;
55 };
56 
57 } // namespace
58 
AttachRRectGeometry(const skjson::ObjectValue & jrect,const AnimationBuilder * abuilder)59 sk_sp<sksg::GeometryNode> ShapeBuilder::AttachRRectGeometry(const skjson::ObjectValue& jrect,
60                                                             const AnimationBuilder* abuilder) {
61     return abuilder->attachDiscardableAdapter<RectangleGeometryAdapter>(jrect, abuilder);
62 }
63 
64 } // namespace internal
65 } // namespace skottie
66