bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 GrShape_DEFINED |
| 9 | #define GrShape_DEFINED |
| 10 | |
| 11 | #include "GrStyle.h" |
| 12 | #include "SkPath.h" |
bsalomon | ee29564 | 2016-06-06 14:01:25 -0700 | [diff] [blame] | 13 | #include "SkPathPriv.h" |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 14 | #include "SkRRect.h" |
| 15 | #include "SkTemplates.h" |
| 16 | #include "SkTLazy.h" |
| 17 | |
| 18 | /** |
| 19 | * Represents a geometric shape (rrect or path) and the GrStyle that it should be rendered with. |
| 20 | * It is possible to apply the style to the GrShape to produce a new GrShape where the geometry |
| 21 | * reflects the styling information (e.g. is stroked). It is also possible to apply just the |
| 22 | * path effect from the style. In this case the resulting shape will include any remaining |
| 23 | * stroking information that is to be applied after the path effect. |
| 24 | * |
| 25 | * Shapes can produce keys that represent only the geometry information, not the style. Note that |
| 26 | * when styling information is applied to produce a new shape then the style has been converted |
| 27 | * to geometric information and is included in the new shape's key. When the same style is applied |
| 28 | * to two shapes that reflect the same underlying geometry the computed keys of the stylized shapes |
| 29 | * will be the same. |
| 30 | * |
bsalomon | ee29564 | 2016-06-06 14:01:25 -0700 | [diff] [blame] | 31 | * Currently this can only be constructed from a path, rect, or rrect though it can become a path |
| 32 | * applying style to the geometry. The idea is to expand this to cover most or all of the geometries |
Brian Salomon | 4f40caf | 2017-09-01 09:00:45 -0400 | [diff] [blame] | 33 | * that have fast paths in the GPU backend. |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 34 | */ |
| 35 | class GrShape { |
| 36 | public: |
bsalomon | 67fa4e3 | 2016-09-21 08:26:57 -0700 | [diff] [blame] | 37 | // Keys for paths may be extracted from the path data for small paths. Clients aren't supposed |
| 38 | // to have to worry about this. This value is exposed for unit tests. |
| 39 | static constexpr int kMaxKeyFromDataVerbCnt = 10; |
| 40 | |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 41 | GrShape() { this->initType(Type::kEmpty); } |
bsalomon | 5e410b4 | 2016-04-28 09:30:46 -0700 | [diff] [blame] | 42 | |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 43 | explicit GrShape(const SkPath& path) : GrShape(path, GrStyle::SimpleFill()) {} |
bsalomon | 72dc51c | 2016-04-27 06:46:23 -0700 | [diff] [blame] | 44 | |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 45 | explicit GrShape(const SkRRect& rrect) : GrShape(rrect, GrStyle::SimpleFill()) {} |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 46 | |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 47 | explicit GrShape(const SkRect& rect) : GrShape(rect, GrStyle::SimpleFill()) {} |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 48 | |
Brian Salomon | da6d072 | 2018-01-03 13:54:35 -0500 | [diff] [blame] | 49 | GrShape(const SkPath& path, const GrStyle& style) : fStyle(style) { |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 50 | this->initType(Type::kPath, &path); |
bsalomon | 1b28c1a | 2016-06-20 12:28:17 -0700 | [diff] [blame] | 51 | this->attemptToSimplifyPath(); |
bsalomon | 72dc51c | 2016-04-27 06:46:23 -0700 | [diff] [blame] | 52 | } |
| 53 | |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 54 | GrShape(const SkRRect& rrect, const GrStyle& style) |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 55 | : fStyle(style) { |
| 56 | this->initType(Type::kRRect); |
| 57 | fRRectData.fRRect = rrect; |
| 58 | fRRectData.fInverted = false; |
| 59 | fRRectData.fStart = DefaultRRectDirAndStartIndex(rrect, style.hasPathEffect(), |
| 60 | &fRRectData.fDir); |
bsalomon | 1b28c1a | 2016-06-20 12:28:17 -0700 | [diff] [blame] | 61 | this->attemptToSimplifyRRect(); |
bsalomon | ee29564 | 2016-06-06 14:01:25 -0700 | [diff] [blame] | 62 | } |
| 63 | |
bsalomon | 7049396 | 2016-06-10 08:05:14 -0700 | [diff] [blame] | 64 | GrShape(const SkRRect& rrect, SkPath::Direction dir, unsigned start, bool inverted, |
| 65 | const GrStyle& style) |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 66 | : fStyle(style) { |
| 67 | this->initType(Type::kRRect); |
| 68 | fRRectData.fRRect = rrect; |
| 69 | fRRectData.fInverted = inverted; |
bsalomon | ee29564 | 2016-06-06 14:01:25 -0700 | [diff] [blame] | 70 | if (style.pathEffect()) { |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 71 | fRRectData.fDir = dir; |
| 72 | fRRectData.fStart = start; |
| 73 | if (fRRectData.fRRect.getType() == SkRRect::kRect_Type) { |
| 74 | fRRectData.fStart = (fRRectData.fStart + 1) & 0b110; |
| 75 | } else if (fRRectData.fRRect.getType() == SkRRect::kOval_Type) { |
| 76 | fRRectData.fStart &= 0b110; |
bsalomon | 7049396 | 2016-06-10 08:05:14 -0700 | [diff] [blame] | 77 | } |
bsalomon | ee29564 | 2016-06-06 14:01:25 -0700 | [diff] [blame] | 78 | } else { |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 79 | fRRectData.fStart = DefaultRRectDirAndStartIndex(rrect, false, &fRRectData.fDir); |
bsalomon | ee29564 | 2016-06-06 14:01:25 -0700 | [diff] [blame] | 80 | } |
bsalomon | 1b28c1a | 2016-06-20 12:28:17 -0700 | [diff] [blame] | 81 | this->attemptToSimplifyRRect(); |
bsalomon | 5e410b4 | 2016-04-28 09:30:46 -0700 | [diff] [blame] | 82 | } |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 83 | |
| 84 | GrShape(const SkRect& rect, const GrStyle& style) |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 85 | : fStyle(style) { |
| 86 | this->initType(Type::kRRect); |
| 87 | fRRectData.fRRect = SkRRect::MakeRect(rect); |
| 88 | fRRectData.fInverted = false; |
| 89 | fRRectData.fStart = DefaultRectDirAndStartIndex(rect, style.hasPathEffect(), |
| 90 | &fRRectData.fDir); |
bsalomon | 1b28c1a | 2016-06-20 12:28:17 -0700 | [diff] [blame] | 91 | this->attemptToSimplifyRRect(); |
bsalomon | 5e410b4 | 2016-04-28 09:30:46 -0700 | [diff] [blame] | 92 | } |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 93 | |
Brian Salomon | da6d072 | 2018-01-03 13:54:35 -0500 | [diff] [blame] | 94 | GrShape(const SkPath& path, const SkPaint& paint) : fStyle(paint) { |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 95 | this->initType(Type::kPath, &path); |
bsalomon | 1b28c1a | 2016-06-20 12:28:17 -0700 | [diff] [blame] | 96 | this->attemptToSimplifyPath(); |
bsalomon | 72dc51c | 2016-04-27 06:46:23 -0700 | [diff] [blame] | 97 | } |
| 98 | |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 99 | GrShape(const SkRRect& rrect, const SkPaint& paint) |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 100 | : fStyle(paint) { |
| 101 | this->initType(Type::kRRect); |
| 102 | fRRectData.fRRect = rrect; |
| 103 | fRRectData.fInverted = false; |
| 104 | fRRectData.fStart = DefaultRRectDirAndStartIndex(rrect, fStyle.hasPathEffect(), |
| 105 | &fRRectData.fDir); |
bsalomon | 1b28c1a | 2016-06-20 12:28:17 -0700 | [diff] [blame] | 106 | this->attemptToSimplifyRRect(); |
bsalomon | 5e410b4 | 2016-04-28 09:30:46 -0700 | [diff] [blame] | 107 | } |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 108 | |
| 109 | GrShape(const SkRect& rect, const SkPaint& paint) |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 110 | : fStyle(paint) { |
| 111 | this->initType(Type::kRRect); |
| 112 | fRRectData.fRRect = SkRRect::MakeRect(rect); |
| 113 | fRRectData.fInverted = false; |
| 114 | fRRectData.fStart = DefaultRectDirAndStartIndex(rect, fStyle.hasPathEffect(), |
| 115 | &fRRectData.fDir); |
bsalomon | 1b28c1a | 2016-06-20 12:28:17 -0700 | [diff] [blame] | 116 | this->attemptToSimplifyRRect(); |
bsalomon | 5e410b4 | 2016-04-28 09:30:46 -0700 | [diff] [blame] | 117 | } |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 118 | |
Brian Salomon | 8a98bc9 | 2018-04-25 11:21:39 -0400 | [diff] [blame^] | 119 | static GrShape MakeArc(const SkRect& oval, SkScalar startAngleDegrees, |
| 120 | SkScalar sweepAngleDegrees, bool useCenter, const GrStyle& style); |
| 121 | |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 122 | GrShape(const GrShape&); |
| 123 | GrShape& operator=(const GrShape& that); |
| 124 | |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 125 | ~GrShape() { this->changeType(Type::kEmpty); } |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 126 | |
Brian Salomon | 4f40caf | 2017-09-01 09:00:45 -0400 | [diff] [blame] | 127 | /** |
| 128 | * Informs MakeFilled on how to modify that shape's fill rule when making a simple filled |
| 129 | * version of the shape. |
| 130 | */ |
| 131 | enum class FillInversion { |
| 132 | kPreserve, |
| 133 | kFlip, |
| 134 | kForceNoninverted, |
| 135 | kForceInverted |
| 136 | }; |
| 137 | /** |
| 138 | * Makes a filled shape from the pre-styled original shape and optionally modifies whether |
| 139 | * the fill is inverted or not. It's important to note that the original shape's geometry |
| 140 | * may already have been modified if doing so was neutral with respect to its style |
| 141 | * (e.g. filled paths are always closed when stored in a shape and dashed paths are always |
| 142 | * made non-inverted since dashing ignores inverseness). |
| 143 | */ |
| 144 | static GrShape MakeFilled(const GrShape& original, FillInversion = FillInversion::kPreserve); |
| 145 | |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 146 | const GrStyle& style() const { return fStyle; } |
| 147 | |
bsalomon | 97fd2d4 | 2016-05-09 13:02:01 -0700 | [diff] [blame] | 148 | /** |
| 149 | * Returns a shape that has either applied the path effect or path effect and stroking |
| 150 | * information from this shape's style to its geometry. Scale is used when approximating the |
| 151 | * output geometry and typically is computed from the view matrix |
| 152 | */ |
bsalomon | 425c27f | 2016-06-23 13:18:45 -0700 | [diff] [blame] | 153 | GrShape applyStyle(GrStyle::Apply apply, SkScalar scale) const { |
bsalomon | 97fd2d4 | 2016-05-09 13:02:01 -0700 | [diff] [blame] | 154 | return GrShape(*this, apply, scale); |
| 155 | } |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 156 | |
bsalomon | 7c73a53 | 2016-05-11 15:15:56 -0700 | [diff] [blame] | 157 | /** Returns the unstyled geometry as a rrect if possible. */ |
bsalomon | 7049396 | 2016-06-10 08:05:14 -0700 | [diff] [blame] | 158 | bool asRRect(SkRRect* rrect, SkPath::Direction* dir, unsigned* start, bool* inverted) const { |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 159 | if (Type::kRRect != fType) { |
| 160 | return false; |
| 161 | } |
| 162 | if (rrect) { |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 163 | *rrect = fRRectData.fRRect; |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 164 | } |
bsalomon | ee29564 | 2016-06-06 14:01:25 -0700 | [diff] [blame] | 165 | if (dir) { |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 166 | *dir = fRRectData.fDir; |
bsalomon | ee29564 | 2016-06-06 14:01:25 -0700 | [diff] [blame] | 167 | } |
| 168 | if (start) { |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 169 | *start = fRRectData.fStart; |
bsalomon | ee29564 | 2016-06-06 14:01:25 -0700 | [diff] [blame] | 170 | } |
bsalomon | 7049396 | 2016-06-10 08:05:14 -0700 | [diff] [blame] | 171 | if (inverted) { |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 172 | *inverted = fRRectData.fInverted; |
bsalomon | 7049396 | 2016-06-10 08:05:14 -0700 | [diff] [blame] | 173 | } |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 174 | return true; |
| 175 | } |
| 176 | |
bsalomon | 398e3f4 | 2016-06-13 10:22:48 -0700 | [diff] [blame] | 177 | /** |
| 178 | * If the unstyled shape is a straight line segment, returns true and sets pts to the endpoints. |
| 179 | * An inverse filled line path is still considered a line. |
| 180 | */ |
bsalomon | 0a0f67e | 2016-06-28 11:56:42 -0700 | [diff] [blame] | 181 | bool asLine(SkPoint pts[2], bool* inverted) const { |
| 182 | if (fType != Type::kLine) { |
| 183 | return false; |
| 184 | } |
| 185 | if (pts) { |
| 186 | pts[0] = fLineData.fPts[0]; |
| 187 | pts[1] = fLineData.fPts[1]; |
| 188 | } |
| 189 | if (inverted) { |
| 190 | *inverted = fLineData.fInverted; |
| 191 | } |
| 192 | return true; |
| 193 | } |
bsalomon | 398e3f4 | 2016-06-13 10:22:48 -0700 | [diff] [blame] | 194 | |
bsalomon | 7c73a53 | 2016-05-11 15:15:56 -0700 | [diff] [blame] | 195 | /** Returns the unstyled geometry as a path. */ |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 196 | void asPath(SkPath* out) const { |
| 197 | switch (fType) { |
bsalomon | 0607756 | 2016-05-04 13:50:29 -0700 | [diff] [blame] | 198 | case Type::kEmpty: |
| 199 | out->reset(); |
| 200 | break; |
Brian Salomon | 085c086 | 2017-08-31 15:44:51 -0400 | [diff] [blame] | 201 | case Type::kInvertedEmpty: |
| 202 | out->reset(); |
| 203 | out->setFillType(kDefaultPathInverseFillType); |
| 204 | break; |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 205 | case Type::kRRect: |
| 206 | out->reset(); |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 207 | out->addRRect(fRRectData.fRRect, fRRectData.fDir, fRRectData.fStart); |
bsalomon | 93f66bc | 2016-06-21 08:35:49 -0700 | [diff] [blame] | 208 | // Below matches the fill type that attemptToSimplifyPath uses. |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 209 | if (fRRectData.fInverted) { |
bsalomon | a4817af | 2016-06-23 11:48:26 -0700 | [diff] [blame] | 210 | out->setFillType(kDefaultPathInverseFillType); |
bsalomon | 93f66bc | 2016-06-21 08:35:49 -0700 | [diff] [blame] | 211 | } else { |
bsalomon | a4817af | 2016-06-23 11:48:26 -0700 | [diff] [blame] | 212 | out->setFillType(kDefaultPathFillType); |
bsalomon | 7049396 | 2016-06-10 08:05:14 -0700 | [diff] [blame] | 213 | } |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 214 | break; |
Brian Salomon | 8a98bc9 | 2018-04-25 11:21:39 -0400 | [diff] [blame^] | 215 | case Type::kArc: |
| 216 | SkPathPriv::CreateDrawArcPath(out, fArcData.fOval, fArcData.fStartAngleDegrees, |
| 217 | fArcData.fSweepAngleDegrees, fArcData.fUseCenter, |
| 218 | fStyle.isSimpleFill()); |
| 219 | if (fArcData.fInverted) { |
| 220 | out->setFillType(kDefaultPathInverseFillType); |
| 221 | } else { |
| 222 | out->setFillType(kDefaultPathFillType); |
| 223 | } |
| 224 | break; |
bsalomon | 0a0f67e | 2016-06-28 11:56:42 -0700 | [diff] [blame] | 225 | case Type::kLine: |
| 226 | out->reset(); |
| 227 | out->moveTo(fLineData.fPts[0]); |
| 228 | out->lineTo(fLineData.fPts[1]); |
| 229 | if (fLineData.fInverted) { |
| 230 | out->setFillType(kDefaultPathInverseFillType); |
| 231 | } else { |
| 232 | out->setFillType(kDefaultPathFillType); |
| 233 | } |
| 234 | break; |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 235 | case Type::kPath: |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 236 | *out = this->path(); |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 237 | break; |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | |
| 241 | /** |
bsalomon | 7c73a53 | 2016-05-11 15:15:56 -0700 | [diff] [blame] | 242 | * Returns whether the geometry is empty. Note that applying the style could produce a |
Brian Salomon | 085c086 | 2017-08-31 15:44:51 -0400 | [diff] [blame] | 243 | * non-empty shape. It also may have an inverse fill. |
bsalomon | 7c73a53 | 2016-05-11 15:15:56 -0700 | [diff] [blame] | 244 | */ |
Brian Salomon | 085c086 | 2017-08-31 15:44:51 -0400 | [diff] [blame] | 245 | bool isEmpty() const { return Type::kEmpty == fType || Type::kInvertedEmpty == fType; } |
bsalomon | 7c73a53 | 2016-05-11 15:15:56 -0700 | [diff] [blame] | 246 | |
bsalomon | 7049396 | 2016-06-10 08:05:14 -0700 | [diff] [blame] | 247 | /** |
| 248 | * Gets the bounds of the geometry without reflecting the shape's styling. This ignores |
| 249 | * the inverse fill nature of the geometry. |
| 250 | */ |
bsalomon | 0a0f67e | 2016-06-28 11:56:42 -0700 | [diff] [blame] | 251 | SkRect bounds() const; |
bsalomon | 9fb4203 | 2016-05-13 09:23:38 -0700 | [diff] [blame] | 252 | |
bsalomon | 7049396 | 2016-06-10 08:05:14 -0700 | [diff] [blame] | 253 | /** |
| 254 | * Gets the bounds of the geometry reflecting the shape's styling (ignoring inverse fill |
| 255 | * status). |
| 256 | */ |
bsalomon | 0a0f67e | 2016-06-28 11:56:42 -0700 | [diff] [blame] | 257 | SkRect styledBounds() const; |
bsalomon | 9fb4203 | 2016-05-13 09:23:38 -0700 | [diff] [blame] | 258 | |
bsalomon | 7c73a53 | 2016-05-11 15:15:56 -0700 | [diff] [blame] | 259 | /** |
bsalomon | 425c27f | 2016-06-23 13:18:45 -0700 | [diff] [blame] | 260 | * Is this shape known to be convex, before styling is applied. An unclosed but otherwise |
| 261 | * convex path is considered to be closed if they styling reflects a fill and not otherwise. |
| 262 | * This is because filling closes all contours in the path. |
| 263 | */ |
| 264 | bool knownToBeConvex() const { |
| 265 | switch (fType) { |
| 266 | case Type::kEmpty: |
| 267 | return true; |
Brian Salomon | 085c086 | 2017-08-31 15:44:51 -0400 | [diff] [blame] | 268 | case Type::kInvertedEmpty: |
| 269 | return true; |
bsalomon | 425c27f | 2016-06-23 13:18:45 -0700 | [diff] [blame] | 270 | case Type::kRRect: |
| 271 | return true; |
Brian Salomon | 8a98bc9 | 2018-04-25 11:21:39 -0400 | [diff] [blame^] | 272 | case Type::kArc: |
| 273 | return SkPathPriv::DrawArcIsConvex(fArcData.fSweepAngleDegrees, |
| 274 | SkToBool(fArcData.fUseCenter), |
| 275 | fStyle.isSimpleFill()); |
bsalomon | 0a0f67e | 2016-06-28 11:56:42 -0700 | [diff] [blame] | 276 | case Type::kLine: |
| 277 | return true; |
bsalomon | 425c27f | 2016-06-23 13:18:45 -0700 | [diff] [blame] | 278 | case Type::kPath: |
| 279 | // SkPath.isConvex() really means "is this path convex were it to be closed" and |
| 280 | // thus doesn't give the correct answer for stroked paths, hence we also check |
| 281 | // whether the path is either filled or closed. Convex paths may only have one |
| 282 | // contour hence isLastContourClosed() is a sufficient for a convex path. |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 283 | return (this->style().isSimpleFill() || this->path().isLastContourClosed()) && |
| 284 | this->path().isConvex(); |
bsalomon | 425c27f | 2016-06-23 13:18:45 -0700 | [diff] [blame] | 285 | } |
| 286 | return false; |
| 287 | } |
| 288 | |
| 289 | /** Is the pre-styled geometry inverse filled? */ |
| 290 | bool inverseFilled() const { |
| 291 | bool ret = false; |
| 292 | switch (fType) { |
| 293 | case Type::kEmpty: |
| 294 | ret = false; |
| 295 | break; |
Brian Salomon | 085c086 | 2017-08-31 15:44:51 -0400 | [diff] [blame] | 296 | case Type::kInvertedEmpty: |
| 297 | ret = true; |
| 298 | break; |
bsalomon | 425c27f | 2016-06-23 13:18:45 -0700 | [diff] [blame] | 299 | case Type::kRRect: |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 300 | ret = fRRectData.fInverted; |
bsalomon | 425c27f | 2016-06-23 13:18:45 -0700 | [diff] [blame] | 301 | break; |
Brian Salomon | 8a98bc9 | 2018-04-25 11:21:39 -0400 | [diff] [blame^] | 302 | case Type::kArc: |
| 303 | ret = fArcData.fInverted; |
| 304 | break; |
bsalomon | 0a0f67e | 2016-06-28 11:56:42 -0700 | [diff] [blame] | 305 | case Type::kLine: |
| 306 | ret = fLineData.fInverted; |
| 307 | break; |
bsalomon | 425c27f | 2016-06-23 13:18:45 -0700 | [diff] [blame] | 308 | case Type::kPath: |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 309 | ret = this->path().isInverseFillType(); |
bsalomon | 425c27f | 2016-06-23 13:18:45 -0700 | [diff] [blame] | 310 | break; |
| 311 | } |
| 312 | // Dashing ignores inverseness. We should have caught this earlier. skbug.com/5421 |
| 313 | SkASSERT(!(ret && this->style().isDashed())); |
| 314 | return ret; |
| 315 | } |
| 316 | |
| 317 | /** |
| 318 | * Might applying the styling to the geometry produce an inverse fill. The "may" part comes in |
| 319 | * because an arbitrary path effect could produce an inverse filled path. In other cases this |
| 320 | * can be thought of as "inverseFilledAfterStyling()". |
| 321 | */ |
| 322 | bool mayBeInverseFilledAfterStyling() const { |
| 323 | // An arbitrary path effect can produce an arbitrary output path, which may be inverse |
| 324 | // filled. |
| 325 | if (this->style().hasNonDashPathEffect()) { |
| 326 | return true; |
| 327 | } |
| 328 | return this->inverseFilled(); |
| 329 | } |
| 330 | |
| 331 | /** |
bsalomon | 7c73a53 | 2016-05-11 15:15:56 -0700 | [diff] [blame] | 332 | * Is it known that the unstyled geometry has no unclosed contours. This means that it will |
| 333 | * not have any caps if stroked (modulo the effect of any path effect). |
bsalomon | 0607756 | 2016-05-04 13:50:29 -0700 | [diff] [blame] | 334 | */ |
| 335 | bool knownToBeClosed() const { |
| 336 | switch (fType) { |
| 337 | case Type::kEmpty: |
| 338 | return true; |
Brian Salomon | 085c086 | 2017-08-31 15:44:51 -0400 | [diff] [blame] | 339 | case Type::kInvertedEmpty: |
| 340 | return true; |
bsalomon | 0607756 | 2016-05-04 13:50:29 -0700 | [diff] [blame] | 341 | case Type::kRRect: |
| 342 | return true; |
Brian Salomon | 8a98bc9 | 2018-04-25 11:21:39 -0400 | [diff] [blame^] | 343 | case Type::kArc: |
| 344 | return fArcData.fUseCenter; |
bsalomon | 0a0f67e | 2016-06-28 11:56:42 -0700 | [diff] [blame] | 345 | case Type::kLine: |
| 346 | return false; |
bsalomon | 0607756 | 2016-05-04 13:50:29 -0700 | [diff] [blame] | 347 | case Type::kPath: |
bsalomon | 425c27f | 2016-06-23 13:18:45 -0700 | [diff] [blame] | 348 | // SkPath doesn't keep track of the closed status of each contour. |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 349 | return SkPathPriv::IsClosedSingleContour(this->path()); |
bsalomon | 0607756 | 2016-05-04 13:50:29 -0700 | [diff] [blame] | 350 | } |
| 351 | return false; |
| 352 | } |
| 353 | |
bsalomon | 06115ee | 2016-06-07 06:28:51 -0700 | [diff] [blame] | 354 | uint32_t segmentMask() const { |
| 355 | switch (fType) { |
| 356 | case Type::kEmpty: |
| 357 | return 0; |
Brian Salomon | 085c086 | 2017-08-31 15:44:51 -0400 | [diff] [blame] | 358 | case Type::kInvertedEmpty: |
| 359 | return 0; |
bsalomon | 06115ee | 2016-06-07 06:28:51 -0700 | [diff] [blame] | 360 | case Type::kRRect: |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 361 | if (fRRectData.fRRect.getType() == SkRRect::kOval_Type) { |
bsalomon | 06115ee | 2016-06-07 06:28:51 -0700 | [diff] [blame] | 362 | return SkPath::kConic_SegmentMask; |
Brian Salomon | 2fad74a | 2017-12-20 13:28:55 -0500 | [diff] [blame] | 363 | } else if (fRRectData.fRRect.getType() == SkRRect::kRect_Type || |
| 364 | fRRectData.fRRect.getType() == SkRRect::kEmpty_Type) { |
bsalomon | 06115ee | 2016-06-07 06:28:51 -0700 | [diff] [blame] | 365 | return SkPath::kLine_SegmentMask; |
| 366 | } |
| 367 | return SkPath::kLine_SegmentMask | SkPath::kConic_SegmentMask; |
Brian Salomon | 8a98bc9 | 2018-04-25 11:21:39 -0400 | [diff] [blame^] | 368 | case Type::kArc: |
| 369 | if (fArcData.fUseCenter) { |
| 370 | return SkPath::kConic_SegmentMask | SkPath::kLine_SegmentMask; |
| 371 | } |
| 372 | return SkPath::kConic_SegmentMask; |
bsalomon | 0a0f67e | 2016-06-28 11:56:42 -0700 | [diff] [blame] | 373 | case Type::kLine: |
| 374 | return SkPath::kLine_SegmentMask; |
bsalomon | 06115ee | 2016-06-07 06:28:51 -0700 | [diff] [blame] | 375 | case Type::kPath: |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 376 | return this->path().getSegmentMasks(); |
bsalomon | 06115ee | 2016-06-07 06:28:51 -0700 | [diff] [blame] | 377 | } |
| 378 | return 0; |
| 379 | } |
| 380 | |
bsalomon | 0607756 | 2016-05-04 13:50:29 -0700 | [diff] [blame] | 381 | /** |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 382 | * Gets the size of the key for the shape represented by this GrShape (ignoring its styling). |
| 383 | * A negative value is returned if the shape has no key (shouldn't be cached). |
| 384 | */ |
| 385 | int unstyledKeySize() const; |
| 386 | |
bsalomon | 425c27f | 2016-06-23 13:18:45 -0700 | [diff] [blame] | 387 | bool hasUnstyledKey() const { return this->unstyledKeySize() >= 0; } |
| 388 | |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 389 | /** |
| 390 | * Writes unstyledKeySize() bytes into the provided pointer. Assumes that there is enough |
| 391 | * space allocated for the key and that unstyledKeySize() does not return a negative value |
| 392 | * for this shape. |
| 393 | */ |
| 394 | void writeUnstyledKey(uint32_t* key) const; |
| 395 | |
Brian Osman | f6f7cf6 | 2017-09-25 16:49:55 -0400 | [diff] [blame] | 396 | /** |
| 397 | * Adds a listener to the *original* path. Typically used to invalidate cached resources when |
| 398 | * a path is no longer in-use. If the shape started out as something other than a path, this |
| 399 | * does nothing (but will delete the listener). |
| 400 | */ |
| 401 | void addGenIDChangeListener(SkPathRef::GenIDChangeListener* listener) const; |
| 402 | |
| 403 | /** |
Brian Osman | b379dcd | 2017-10-04 15:44:05 -0400 | [diff] [blame] | 404 | * Helpers that are only exposed for unit tests, to determine if the shape is a path, and get |
Brian Salomon | da6d072 | 2018-01-03 13:54:35 -0500 | [diff] [blame] | 405 | * the generation ID of the *original* path. This is the path that will receive |
| 406 | * GenIDChangeListeners added to this shape. |
Brian Osman | f6f7cf6 | 2017-09-25 16:49:55 -0400 | [diff] [blame] | 407 | */ |
| 408 | uint32_t testingOnly_getOriginalGenerationID() const; |
Brian Osman | b379dcd | 2017-10-04 15:44:05 -0400 | [diff] [blame] | 409 | bool testingOnly_isPath() const; |
Brian Salomon | da6d072 | 2018-01-03 13:54:35 -0500 | [diff] [blame] | 410 | bool testingOnly_isNonVolatilePath() const; |
Brian Osman | f6f7cf6 | 2017-09-25 16:49:55 -0400 | [diff] [blame] | 411 | |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 412 | private: |
bsalomon | 72dc51c | 2016-04-27 06:46:23 -0700 | [diff] [blame] | 413 | enum class Type { |
| 414 | kEmpty, |
Brian Salomon | 085c086 | 2017-08-31 15:44:51 -0400 | [diff] [blame] | 415 | kInvertedEmpty, |
bsalomon | 72dc51c | 2016-04-27 06:46:23 -0700 | [diff] [blame] | 416 | kRRect, |
Brian Salomon | 8a98bc9 | 2018-04-25 11:21:39 -0400 | [diff] [blame^] | 417 | kArc, |
bsalomon | 0a0f67e | 2016-06-28 11:56:42 -0700 | [diff] [blame] | 418 | kLine, |
bsalomon | 72dc51c | 2016-04-27 06:46:23 -0700 | [diff] [blame] | 419 | kPath, |
| 420 | }; |
| 421 | |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 422 | void initType(Type type, const SkPath* path = nullptr) { |
| 423 | fType = Type::kEmpty; |
| 424 | this->changeType(type, path); |
| 425 | } |
| 426 | |
| 427 | void changeType(Type type, const SkPath* path = nullptr) { |
| 428 | bool wasPath = Type::kPath == fType; |
| 429 | fType = type; |
| 430 | bool isPath = Type::kPath == type; |
| 431 | SkASSERT(!path || isPath); |
| 432 | if (wasPath && !isPath) { |
| 433 | fPathData.fPath.~SkPath(); |
| 434 | } else if (!wasPath && isPath) { |
| 435 | if (path) { |
| 436 | new (&fPathData.fPath) SkPath(*path); |
| 437 | } else { |
| 438 | new (&fPathData.fPath) SkPath(); |
| 439 | } |
| 440 | } else if (isPath && path) { |
| 441 | fPathData.fPath = *path; |
| 442 | } |
| 443 | // Whether or not we use the path's gen ID is decided in attemptToSimplifyPath. |
| 444 | fPathData.fGenID = 0; |
| 445 | } |
| 446 | |
| 447 | SkPath& path() { |
| 448 | SkASSERT(Type::kPath == fType); |
| 449 | return fPathData.fPath; |
| 450 | } |
| 451 | |
| 452 | const SkPath& path() const { |
| 453 | SkASSERT(Type::kPath == fType); |
| 454 | return fPathData.fPath; |
| 455 | } |
| 456 | |
bsalomon | 97fd2d4 | 2016-05-09 13:02:01 -0700 | [diff] [blame] | 457 | /** Constructor used by the applyStyle() function */ |
| 458 | GrShape(const GrShape& parentShape, GrStyle::Apply, SkScalar scale); |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 459 | |
| 460 | /** |
| 461 | * Determines the key we should inherit from the input shape's geometry and style when |
| 462 | * we are applying the style to create a new shape. |
| 463 | */ |
bsalomon | 97fd2d4 | 2016-05-09 13:02:01 -0700 | [diff] [blame] | 464 | void setInheritedKey(const GrShape& parentShape, GrStyle::Apply, SkScalar scale); |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 465 | |
bsalomon | 1b28c1a | 2016-06-20 12:28:17 -0700 | [diff] [blame] | 466 | void attemptToSimplifyPath(); |
bsalomon | 1b28c1a | 2016-06-20 12:28:17 -0700 | [diff] [blame] | 467 | void attemptToSimplifyRRect(); |
bsalomon | 0a0f67e | 2016-06-28 11:56:42 -0700 | [diff] [blame] | 468 | void attemptToSimplifyLine(); |
Brian Salomon | 8a98bc9 | 2018-04-25 11:21:39 -0400 | [diff] [blame^] | 469 | void attemptToSimplifyArc(); |
bsalomon | ee29564 | 2016-06-06 14:01:25 -0700 | [diff] [blame] | 470 | |
Brian Salomon | 72f78c3 | 2017-12-21 11:56:42 -0500 | [diff] [blame] | 471 | bool attemptToSimplifyStrokedLineToRRect(); |
| 472 | |
Brian Salomon | da6d072 | 2018-01-03 13:54:35 -0500 | [diff] [blame] | 473 | /** Gets the path that gen id listeners should be added to. */ |
| 474 | const SkPath* originalPathForListeners() const; |
| 475 | |
bsalomon | a4817af | 2016-06-23 11:48:26 -0700 | [diff] [blame] | 476 | // Defaults to use when there is no distinction between even/odd and winding fills. |
| 477 | static constexpr SkPath::FillType kDefaultPathFillType = SkPath::kEvenOdd_FillType; |
| 478 | static constexpr SkPath::FillType kDefaultPathInverseFillType = |
| 479 | SkPath::kInverseEvenOdd_FillType; |
| 480 | |
bsalomon | ee29564 | 2016-06-06 14:01:25 -0700 | [diff] [blame] | 481 | static constexpr SkPath::Direction kDefaultRRectDir = SkPath::kCW_Direction; |
| 482 | static constexpr unsigned kDefaultRRectStart = 0; |
| 483 | |
| 484 | static unsigned DefaultRectDirAndStartIndex(const SkRect& rect, bool hasPathEffect, |
| 485 | SkPath::Direction* dir) { |
| 486 | *dir = kDefaultRRectDir; |
| 487 | // This comes from SkPath's interface. The default for adding a SkRect is counter clockwise |
| 488 | // beginning at index 0 (which happens to correspond to rrect index 0 or 7). |
| 489 | if (!hasPathEffect) { |
| 490 | // It doesn't matter what start we use, just be consistent to avoid redundant keys. |
| 491 | return kDefaultRRectStart; |
bsalomon | 72dc51c | 2016-04-27 06:46:23 -0700 | [diff] [blame] | 492 | } |
bsalomon | ee29564 | 2016-06-06 14:01:25 -0700 | [diff] [blame] | 493 | // In SkPath a rect starts at index 0 by default. This is the top left corner. However, |
| 494 | // we store rects as rrects. RRects don't preserve the invertedness, but rather sort the |
| 495 | // rect edges. Thus, we may need to modify the rrect's start index to account for the sort. |
| 496 | bool swapX = rect.fLeft > rect.fRight; |
| 497 | bool swapY = rect.fTop > rect.fBottom; |
| 498 | if (swapX && swapY) { |
| 499 | // 0 becomes start index 2 and times 2 to convert from rect the rrect indices. |
| 500 | return 2 * 2; |
| 501 | } else if (swapX) { |
| 502 | *dir = SkPath::kCCW_Direction; |
| 503 | // 0 becomes start index 1 and times 2 to convert from rect the rrect indices. |
| 504 | return 2 * 1; |
| 505 | } else if (swapY) { |
| 506 | *dir = SkPath::kCCW_Direction; |
| 507 | // 0 becomes start index 3 and times 2 to convert from rect the rrect indices. |
| 508 | return 2 * 3; |
bsalomon | 72dc51c | 2016-04-27 06:46:23 -0700 | [diff] [blame] | 509 | } |
bsalomon | ee29564 | 2016-06-06 14:01:25 -0700 | [diff] [blame] | 510 | return 0; |
| 511 | } |
| 512 | |
| 513 | static unsigned DefaultRRectDirAndStartIndex(const SkRRect& rrect, bool hasPathEffect, |
| 514 | SkPath::Direction* dir) { |
| 515 | // This comes from SkPath's interface. The default for adding a SkRRect to a path is |
| 516 | // clockwise beginning at starting index 6. |
| 517 | static constexpr unsigned kPathRRectStartIdx = 6; |
| 518 | *dir = kDefaultRRectDir; |
| 519 | if (!hasPathEffect) { |
| 520 | // It doesn't matter what start we use, just be consistent to avoid redundant keys. |
| 521 | return kDefaultRRectStart; |
bsalomon | 72dc51c | 2016-04-27 06:46:23 -0700 | [diff] [blame] | 522 | } |
bsalomon | ee29564 | 2016-06-06 14:01:25 -0700 | [diff] [blame] | 523 | return kPathRRectStartIdx; |
bsalomon | 72dc51c | 2016-04-27 06:46:23 -0700 | [diff] [blame] | 524 | } |
| 525 | |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 526 | union { |
| 527 | struct { |
Brian Salomon | 8a98bc9 | 2018-04-25 11:21:39 -0400 | [diff] [blame^] | 528 | SkRRect fRRect; |
| 529 | SkPath::Direction fDir; |
| 530 | unsigned fStart; |
| 531 | bool fInverted; |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 532 | } fRRectData; |
| 533 | struct { |
Brian Salomon | 8a98bc9 | 2018-04-25 11:21:39 -0400 | [diff] [blame^] | 534 | SkRect fOval; |
| 535 | SkScalar fStartAngleDegrees; |
| 536 | SkScalar fSweepAngleDegrees; |
| 537 | int16_t fUseCenter; |
| 538 | int16_t fInverted; |
| 539 | } fArcData; |
| 540 | struct { |
| 541 | SkPath fPath; |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 542 | // Gen ID of the original path (fPath may be modified) |
Brian Salomon | 8a98bc9 | 2018-04-25 11:21:39 -0400 | [diff] [blame^] | 543 | int32_t fGenID; |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 544 | } fPathData; |
bsalomon | 0a0f67e | 2016-06-28 11:56:42 -0700 | [diff] [blame] | 545 | struct { |
Brian Salomon | 8a98bc9 | 2018-04-25 11:21:39 -0400 | [diff] [blame^] | 546 | SkPoint fPts[2]; |
| 547 | bool fInverted; |
bsalomon | 0a0f67e | 2016-06-28 11:56:42 -0700 | [diff] [blame] | 548 | } fLineData; |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 549 | }; |
Brian Salomon | 8a98bc9 | 2018-04-25 11:21:39 -0400 | [diff] [blame^] | 550 | GrStyle fStyle; |
| 551 | SkTLazy<SkPath> fInheritedPathForListeners; |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 552 | SkAutoSTArray<8, uint32_t> fInheritedKey; |
Brian Salomon | 8a98bc9 | 2018-04-25 11:21:39 -0400 | [diff] [blame^] | 553 | Type fType; |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 554 | }; |
| 555 | #endif |