1 /*
2  * Copyright 2017 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 SkSGRect_DEFINED
9 #define SkSGRect_DEFINED
10 
11 #include "modules/sksg/include/SkSGGeometryNode.h"
12 
13 #include "include/core/SkPath.h"
14 #include "include/core/SkRRect.h"
15 #include "include/core/SkRect.h"
16 
17 class SkCanvas;
18 class SkPaint;
19 
20 namespace sksg {
21 
22 /**
23  * Concrete Geometry node, wrapping an SkRect.
24  */
25 class Rect final : public GeometryNode {
26 public:
Make()27     static sk_sp<Rect> Make()                { return sk_sp<Rect>(new Rect(SkRect::MakeEmpty())); }
Make(const SkRect & r)28     static sk_sp<Rect> Make(const SkRect& r) { return sk_sp<Rect>(new Rect(r)); }
29 
30     SG_ATTRIBUTE(L, SkScalar, fRect.fLeft  )
31     SG_ATTRIBUTE(T, SkScalar, fRect.fTop   )
32     SG_ATTRIBUTE(R, SkScalar, fRect.fRight )
33     SG_ATTRIBUTE(B, SkScalar, fRect.fBottom)
34 
35     SG_MAPPED_ATTRIBUTE(Direction        , SkPathDirection, fAttrContaier)
36     SG_MAPPED_ATTRIBUTE(InitialPointIndex, uint8_t        , fAttrContaier)
37 
38 protected:
39     void onClip(SkCanvas*, bool antiAlias) const override;
40     void onDraw(SkCanvas*, const SkPaint&) const override;
41     bool onContains(const SkPoint&)        const override;
42 
43     SkRect onRevalidate(InvalidationController*, const SkMatrix&) override;
44     SkPath onAsPath() const override;
45 
46 private:
47     explicit Rect(const SkRect&);
48 
49     SkRect   fRect;
50 
51     struct AttrContainer {
52         uint8_t fDirection         : 1;
53         uint8_t fInitialPointIndex : 2;
54 
getDirectionAttrContainer55         SkPathDirection getDirection() const {
56             return static_cast<SkPathDirection>(fDirection);
57         }
setDirectionAttrContainer58         void setDirection(SkPathDirection dir) { fDirection = SkTo<uint8_t>(dir); }
59 
getInitialPointIndexAttrContainer60         uint8_t getInitialPointIndex() const { return fInitialPointIndex; }
setInitialPointIndexAttrContainer61         void setInitialPointIndex(uint8_t idx) { fInitialPointIndex = idx; }
62     };
63     AttrContainer fAttrContaier = { (int)SkPathDirection::kCW, 0 };
64 
65     using INHERITED = GeometryNode;
66 };
67 
68 /**
69  * Concrete Geometry node, wrapping an SkRRect.
70  */
71 class RRect final : public GeometryNode {
72 public:
Make()73     static sk_sp<RRect> Make()                  { return sk_sp<RRect>(new RRect(SkRRect())); }
Make(const SkRRect & rr)74     static sk_sp<RRect> Make(const SkRRect& rr) { return sk_sp<RRect>(new RRect(rr)); }
75 
76     SG_ATTRIBUTE(RRect, SkRRect, fRRect)
77 
78     SG_MAPPED_ATTRIBUTE(Direction        , SkPathDirection, fAttrContaier)
79     SG_MAPPED_ATTRIBUTE(InitialPointIndex, uint8_t          , fAttrContaier)
80 
81 protected:
82     void onClip(SkCanvas*, bool antiAlias) const override;
83     void onDraw(SkCanvas*, const SkPaint&) const override;
84     bool onContains(const SkPoint&)        const override;
85 
86     SkRect onRevalidate(InvalidationController*, const SkMatrix&) override;
87     SkPath onAsPath() const override;
88 
89 private:
90     explicit RRect(const SkRRect&);
91 
92     SkRRect fRRect;
93 
94     struct AttrContainer {
95         uint8_t fDirection         : 1;
96         uint8_t fInitialPointIndex : 2;
97 
getDirectionAttrContainer98         SkPathDirection getDirection() const {
99             return static_cast<SkPathDirection>(fDirection);
100         }
setDirectionAttrContainer101         void setDirection(SkPathDirection dir) { fDirection = SkTo<uint8_t>(dir); }
102 
getInitialPointIndexAttrContainer103         uint8_t getInitialPointIndex() const { return fInitialPointIndex; }
setInitialPointIndexAttrContainer104         void setInitialPointIndex(uint8_t idx) { fInitialPointIndex = idx; }
105     };
106     AttrContainer fAttrContaier = { (int)SkPathDirection::kCW, 0 };
107 
108     using INHERITED = GeometryNode;
109 };
110 
111 } // namespace sksg
112 
113 #endif // SkSGRect_DEFINED
114