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