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 "include/core/SkPath.h" 12 #include "include/core/SkPathTypes.h" 13 #include "include/core/SkRRect.h" 14 #include "include/core/SkRect.h" 15 #include "include/core/SkRefCnt.h" 16 #include "include/core/SkScalar.h" 17 #include "include/private/base/SkTo.h" 18 #include "modules/sksg/include/SkSGGeometryNode.h" 19 #include "modules/sksg/include/SkSGNode.h" 20 21 #include <cstdint> 22 23 class SkCanvas; 24 class SkMatrix; 25 class SkPaint; 26 struct SkPoint; 27 28 namespace sksg { 29 class InvalidationController; 30 31 /** 32 * Concrete Geometry node, wrapping an SkRect. 33 */ 34 class Rect final : public GeometryNode { 35 public: Make()36 static sk_sp<Rect> Make() { return sk_sp<Rect>(new Rect(SkRect::MakeEmpty())); } Make(const SkRect & r)37 static sk_sp<Rect> Make(const SkRect& r) { return sk_sp<Rect>(new Rect(r)); } 38 39 SG_ATTRIBUTE(L, SkScalar, fRect.fLeft ) 40 SG_ATTRIBUTE(T, SkScalar, fRect.fTop ) 41 SG_ATTRIBUTE(R, SkScalar, fRect.fRight ) 42 SG_ATTRIBUTE(B, SkScalar, fRect.fBottom) 43 44 SG_MAPPED_ATTRIBUTE(Direction , SkPathDirection, fAttrContaier) 45 SG_MAPPED_ATTRIBUTE(InitialPointIndex, uint8_t , fAttrContaier) 46 47 protected: 48 void onClip(SkCanvas*, bool antiAlias) const override; 49 void onDraw(SkCanvas*, const SkPaint&) const override; 50 bool onContains(const SkPoint&) const override; 51 52 SkRect onRevalidate(InvalidationController*, const SkMatrix&) override; 53 SkPath onAsPath() const override; 54 55 private: 56 explicit Rect(const SkRect&); 57 58 SkRect fRect; 59 60 struct AttrContainer { 61 uint8_t fDirection : 1; 62 uint8_t fInitialPointIndex : 2; 63 getDirectionAttrContainer64 SkPathDirection getDirection() const { 65 return static_cast<SkPathDirection>(fDirection); 66 } setDirectionAttrContainer67 void setDirection(SkPathDirection dir) { fDirection = SkTo<uint8_t>(dir); } 68 getInitialPointIndexAttrContainer69 uint8_t getInitialPointIndex() const { return fInitialPointIndex; } setInitialPointIndexAttrContainer70 void setInitialPointIndex(uint8_t idx) { fInitialPointIndex = idx; } 71 }; 72 AttrContainer fAttrContaier = { (int)SkPathDirection::kCW, 0 }; 73 74 using INHERITED = GeometryNode; 75 }; 76 77 /** 78 * Concrete Geometry node, wrapping an SkRRect. 79 */ 80 class RRect final : public GeometryNode { 81 public: Make()82 static sk_sp<RRect> Make() { return sk_sp<RRect>(new RRect(SkRRect())); } Make(const SkRRect & rr)83 static sk_sp<RRect> Make(const SkRRect& rr) { return sk_sp<RRect>(new RRect(rr)); } 84 85 SG_ATTRIBUTE(RRect, SkRRect, fRRect) 86 87 SG_MAPPED_ATTRIBUTE(Direction , SkPathDirection, fAttrContaier) 88 SG_MAPPED_ATTRIBUTE(InitialPointIndex, uint8_t , fAttrContaier) 89 90 protected: 91 void onClip(SkCanvas*, bool antiAlias) const override; 92 void onDraw(SkCanvas*, const SkPaint&) const override; 93 bool onContains(const SkPoint&) const override; 94 95 SkRect onRevalidate(InvalidationController*, const SkMatrix&) override; 96 SkPath onAsPath() const override; 97 98 private: 99 explicit RRect(const SkRRect&); 100 101 SkRRect fRRect; 102 103 struct AttrContainer { 104 uint8_t fDirection : 1; 105 uint8_t fInitialPointIndex : 2; 106 getDirectionAttrContainer107 SkPathDirection getDirection() const { 108 return static_cast<SkPathDirection>(fDirection); 109 } setDirectionAttrContainer110 void setDirection(SkPathDirection dir) { fDirection = SkTo<uint8_t>(dir); } 111 getInitialPointIndexAttrContainer112 uint8_t getInitialPointIndex() const { return fInitialPointIndex; } setInitialPointIndexAttrContainer113 void setInitialPointIndex(uint8_t idx) { fInitialPointIndex = idx; } 114 }; 115 AttrContainer fAttrContaier = { (int)SkPathDirection::kCW, 0 }; 116 117 using INHERITED = GeometryNode; 118 }; 119 120 } // namespace sksg 121 122 #endif // SkSGRect_DEFINED 123