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 | #include "GrShape.h" |
| 9 | |
| 10 | GrShape& GrShape::operator=(const GrShape& that) { |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 11 | fStyle = that.fStyle; |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 12 | this->changeType(that.fType, Type::kPath == that.fType ? &that.path() : nullptr); |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 13 | switch (fType) { |
| 14 | case Type::kEmpty: |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 15 | break; |
| 16 | case Type::kRRect: |
bsalomon | 0a0f67e | 2016-06-28 11:56:42 -0700 | [diff] [blame] | 17 | fRRectData = that.fRRectData; |
| 18 | break; |
| 19 | case Type::kLine: |
| 20 | fLineData = that.fLineData; |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 21 | break; |
| 22 | case Type::kPath: |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 23 | fPathData.fGenID = that.fPathData.fGenID; |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 24 | break; |
| 25 | } |
| 26 | fInheritedKey.reset(that.fInheritedKey.count()); |
benjaminwagner | d9cca4a | 2016-05-04 11:06:19 -0700 | [diff] [blame] | 27 | sk_careful_memcpy(fInheritedKey.get(), that.fInheritedKey.get(), |
| 28 | sizeof(uint32_t) * fInheritedKey.count()); |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 29 | return *this; |
| 30 | } |
| 31 | |
bsalomon | 0a0f67e | 2016-06-28 11:56:42 -0700 | [diff] [blame] | 32 | SkRect GrShape::bounds() const { |
bsalomon | 0ae36a2 | 2016-07-18 07:31:13 -0700 | [diff] [blame] | 33 | // Bounds where left == bottom or top == right can indicate a line or point shape. We return |
| 34 | // inverted bounds for a truly empty shape. |
| 35 | static constexpr SkRect kInverted = SkRect::MakeLTRB(1, 1, -1, -1); |
bsalomon | 9fb4203 | 2016-05-13 09:23:38 -0700 | [diff] [blame] | 36 | switch (fType) { |
| 37 | case Type::kEmpty: |
bsalomon | 0ae36a2 | 2016-07-18 07:31:13 -0700 | [diff] [blame] | 38 | return kInverted; |
bsalomon | 0a0f67e | 2016-06-28 11:56:42 -0700 | [diff] [blame] | 39 | case Type::kLine: { |
| 40 | SkRect bounds; |
| 41 | if (fLineData.fPts[0].fX < fLineData.fPts[1].fX) { |
| 42 | bounds.fLeft = fLineData.fPts[0].fX; |
| 43 | bounds.fRight = fLineData.fPts[1].fX; |
| 44 | } else { |
| 45 | bounds.fLeft = fLineData.fPts[1].fX; |
| 46 | bounds.fRight = fLineData.fPts[0].fX; |
| 47 | } |
| 48 | if (fLineData.fPts[0].fY < fLineData.fPts[1].fY) { |
| 49 | bounds.fTop = fLineData.fPts[0].fY; |
| 50 | bounds.fBottom = fLineData.fPts[1].fY; |
| 51 | } else { |
| 52 | bounds.fTop = fLineData.fPts[1].fY; |
| 53 | bounds.fBottom = fLineData.fPts[0].fY; |
| 54 | } |
| 55 | return bounds; |
| 56 | } |
bsalomon | 9fb4203 | 2016-05-13 09:23:38 -0700 | [diff] [blame] | 57 | case Type::kRRect: |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 58 | return fRRectData.fRRect.getBounds(); |
bsalomon | 9fb4203 | 2016-05-13 09:23:38 -0700 | [diff] [blame] | 59 | case Type::kPath: |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 60 | return this->path().getBounds(); |
bsalomon | 9fb4203 | 2016-05-13 09:23:38 -0700 | [diff] [blame] | 61 | } |
| 62 | SkFAIL("Unknown shape type"); |
bsalomon | 0ae36a2 | 2016-07-18 07:31:13 -0700 | [diff] [blame] | 63 | return kInverted; |
bsalomon | 9fb4203 | 2016-05-13 09:23:38 -0700 | [diff] [blame] | 64 | } |
| 65 | |
bsalomon | 0a0f67e | 2016-06-28 11:56:42 -0700 | [diff] [blame] | 66 | SkRect GrShape::styledBounds() const { |
bsalomon | 9fb4203 | 2016-05-13 09:23:38 -0700 | [diff] [blame] | 67 | if (Type::kEmpty == fType && !fStyle.hasNonDashPathEffect()) { |
bsalomon | 0a0f67e | 2016-06-28 11:56:42 -0700 | [diff] [blame] | 68 | return SkRect::MakeEmpty(); |
bsalomon | 9fb4203 | 2016-05-13 09:23:38 -0700 | [diff] [blame] | 69 | } |
bsalomon | 0a0f67e | 2016-06-28 11:56:42 -0700 | [diff] [blame] | 70 | SkRect bounds; |
| 71 | fStyle.adjustBounds(&bounds, this->bounds()); |
| 72 | return bounds; |
bsalomon | 9fb4203 | 2016-05-13 09:23:38 -0700 | [diff] [blame] | 73 | } |
| 74 | |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 75 | int GrShape::unstyledKeySize() const { |
| 76 | if (fInheritedKey.count()) { |
| 77 | return fInheritedKey.count(); |
| 78 | } |
| 79 | switch (fType) { |
| 80 | case Type::kEmpty: |
| 81 | return 1; |
| 82 | case Type::kRRect: |
| 83 | SkASSERT(!fInheritedKey.count()); |
| 84 | SkASSERT(0 == SkRRect::kSizeInMemory % sizeof(uint32_t)); |
bsalomon | 7049396 | 2016-06-10 08:05:14 -0700 | [diff] [blame] | 85 | // + 1 for the direction, start index, and inverseness. |
bsalomon | ee29564 | 2016-06-06 14:01:25 -0700 | [diff] [blame] | 86 | return SkRRect::kSizeInMemory / sizeof(uint32_t) + 1; |
bsalomon | 0a0f67e | 2016-06-28 11:56:42 -0700 | [diff] [blame] | 87 | case Type::kLine: |
| 88 | GR_STATIC_ASSERT(2 * sizeof(uint32_t) == sizeof(SkPoint)); |
| 89 | // 4 for the end points and 1 for the inverseness |
| 90 | return 5; |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 91 | case Type::kPath: |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 92 | if (0 == fPathData.fGenID) { |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 93 | return -1; |
| 94 | } else { |
bsalomon | 7049396 | 2016-06-10 08:05:14 -0700 | [diff] [blame] | 95 | // The key is the path ID and fill type. |
| 96 | return 2; |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | SkFAIL("Should never get here."); |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | void GrShape::writeUnstyledKey(uint32_t* key) const { |
| 104 | SkASSERT(this->unstyledKeySize()); |
| 105 | SkDEBUGCODE(uint32_t* origKey = key;) |
| 106 | if (fInheritedKey.count()) { |
| 107 | memcpy(key, fInheritedKey.get(), sizeof(uint32_t) * fInheritedKey.count()); |
| 108 | SkDEBUGCODE(key += fInheritedKey.count();) |
| 109 | } else { |
| 110 | switch (fType) { |
| 111 | case Type::kEmpty: |
| 112 | *key++ = 1; |
| 113 | break; |
| 114 | case Type::kRRect: |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 115 | fRRectData.fRRect.writeToMemory(key); |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 116 | key += SkRRect::kSizeInMemory / sizeof(uint32_t); |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 117 | *key = (fRRectData.fDir == SkPath::kCCW_Direction) ? (1 << 31) : 0; |
| 118 | *key |= fRRectData.fInverted ? (1 << 30) : 0; |
| 119 | *key++ |= fRRectData.fStart; |
| 120 | SkASSERT(fRRectData.fStart < 8); |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 121 | break; |
bsalomon | 0a0f67e | 2016-06-28 11:56:42 -0700 | [diff] [blame] | 122 | case Type::kLine: |
| 123 | memcpy(key, fLineData.fPts, 2 * sizeof(SkPoint)); |
| 124 | key += 4; |
| 125 | *key++ = fLineData.fInverted ? 1 : 0; |
| 126 | break; |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 127 | case Type::kPath: |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 128 | SkASSERT(fPathData.fGenID); |
| 129 | *key++ = fPathData.fGenID; |
bsalomon | 7049396 | 2016-06-10 08:05:14 -0700 | [diff] [blame] | 130 | // We could canonicalize the fill rule for paths that don't differentiate between |
| 131 | // even/odd or winding fill (e.g. convex). |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 132 | *key++ = this->path().getFillType(); |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 133 | break; |
| 134 | } |
| 135 | } |
| 136 | SkASSERT(key - origKey == this->unstyledKeySize()); |
| 137 | } |
| 138 | |
bsalomon | 97fd2d4 | 2016-05-09 13:02:01 -0700 | [diff] [blame] | 139 | void GrShape::setInheritedKey(const GrShape &parent, GrStyle::Apply apply, SkScalar scale) { |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 140 | SkASSERT(!fInheritedKey.count()); |
| 141 | // If the output shape turns out to be simple, then we will just use its geometric key |
| 142 | if (Type::kPath == fType) { |
| 143 | // We want ApplyFullStyle(ApplyPathEffect(shape)) to have the same key as |
| 144 | // ApplyFullStyle(shape). |
| 145 | // The full key is structured as (geo,path_effect,stroke). |
| 146 | // If we do ApplyPathEffect we get get,path_effect as the inherited key. If we then |
| 147 | // do ApplyFullStyle we'll memcpy geo,path_effect into the new inherited key |
| 148 | // and then append the style key (which should now be stroke only) at the end. |
| 149 | int parentCnt = parent.fInheritedKey.count(); |
| 150 | bool useParentGeoKey = !parentCnt; |
| 151 | if (useParentGeoKey) { |
| 152 | parentCnt = parent.unstyledKeySize(); |
bsalomon | 72dc51c | 2016-04-27 06:46:23 -0700 | [diff] [blame] | 153 | if (parentCnt < 0) { |
| 154 | // The parent's geometry has no key so we will have no key. |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 155 | fPathData.fGenID = 0; |
bsalomon | 72dc51c | 2016-04-27 06:46:23 -0700 | [diff] [blame] | 156 | return; |
| 157 | } |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 158 | } |
bsalomon | 0607756 | 2016-05-04 13:50:29 -0700 | [diff] [blame] | 159 | uint32_t styleKeyFlags = 0; |
| 160 | if (parent.knownToBeClosed()) { |
| 161 | styleKeyFlags |= GrStyle::kClosed_KeyFlag; |
| 162 | } |
bsalomon | 0ae36a2 | 2016-07-18 07:31:13 -0700 | [diff] [blame] | 163 | if (parent.asLine(nullptr, nullptr)) { |
| 164 | styleKeyFlags |= GrStyle::kNoJoins_KeyFlag; |
| 165 | } |
bsalomon | 0607756 | 2016-05-04 13:50:29 -0700 | [diff] [blame] | 166 | int styleCnt = GrStyle::KeySize(parent.fStyle, apply, styleKeyFlags); |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 167 | if (styleCnt < 0) { |
bsalomon | 93f66bc | 2016-06-21 08:35:49 -0700 | [diff] [blame] | 168 | // The style doesn't allow a key, set the path gen ID to 0 so that we fail when |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 169 | // we try to get a key for the shape. |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 170 | fPathData.fGenID = 0; |
bsalomon | 72dc51c | 2016-04-27 06:46:23 -0700 | [diff] [blame] | 171 | return; |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 172 | } |
bsalomon | 72dc51c | 2016-04-27 06:46:23 -0700 | [diff] [blame] | 173 | fInheritedKey.reset(parentCnt + styleCnt); |
| 174 | if (useParentGeoKey) { |
| 175 | // This will be the geo key. |
| 176 | parent.writeUnstyledKey(fInheritedKey.get()); |
| 177 | } else { |
| 178 | // This should be (geo,path_effect). |
| 179 | memcpy(fInheritedKey.get(), parent.fInheritedKey.get(), |
| 180 | parentCnt * sizeof(uint32_t)); |
| 181 | } |
| 182 | // Now turn (geo,path_effect) or (geo) into (geo,path_effect,stroke) |
bsalomon | 97fd2d4 | 2016-05-09 13:02:01 -0700 | [diff] [blame] | 183 | GrStyle::WriteKey(fInheritedKey.get() + parentCnt, parent.fStyle, apply, scale, |
| 184 | styleKeyFlags); |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 188 | GrShape::GrShape(const GrShape& that) : fStyle(that.fStyle) { |
| 189 | const SkPath* thatPath = Type::kPath == that.fType ? &that.fPathData.fPath : nullptr; |
| 190 | this->initType(that.fType, thatPath); |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 191 | switch (fType) { |
| 192 | case Type::kEmpty: |
bsalomon | 93f66bc | 2016-06-21 08:35:49 -0700 | [diff] [blame] | 193 | break; |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 194 | case Type::kRRect: |
bsalomon | 0a0f67e | 2016-06-28 11:56:42 -0700 | [diff] [blame] | 195 | fRRectData = that.fRRectData; |
| 196 | break; |
| 197 | case Type::kLine: |
| 198 | fLineData = that.fLineData; |
bsalomon | 93f66bc | 2016-06-21 08:35:49 -0700 | [diff] [blame] | 199 | break; |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 200 | case Type::kPath: |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 201 | fPathData.fGenID = that.fPathData.fGenID; |
bsalomon | 93f66bc | 2016-06-21 08:35:49 -0700 | [diff] [blame] | 202 | break; |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 203 | } |
| 204 | fInheritedKey.reset(that.fInheritedKey.count()); |
bsalomon | 93f66bc | 2016-06-21 08:35:49 -0700 | [diff] [blame] | 205 | sk_careful_memcpy(fInheritedKey.get(), that.fInheritedKey.get(), |
| 206 | sizeof(uint32_t) * fInheritedKey.count()); |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 207 | } |
| 208 | |
bsalomon | 97fd2d4 | 2016-05-09 13:02:01 -0700 | [diff] [blame] | 209 | GrShape::GrShape(const GrShape& parent, GrStyle::Apply apply, SkScalar scale) { |
| 210 | // TODO: Add some quantization of scale for better cache performance here or leave that up |
| 211 | // to caller? |
| 212 | // TODO: For certain shapes and stroke params we could ignore the scale. (e.g. miter or bevel |
| 213 | // stroke of a rect). |
bsalomon | fb08327 | 2016-05-04 08:27:41 -0700 | [diff] [blame] | 214 | if (!parent.style().applies() || |
| 215 | (GrStyle::Apply::kPathEffectOnly == apply && !parent.style().pathEffect())) { |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 216 | this->initType(Type::kEmpty); |
bsalomon | fb08327 | 2016-05-04 08:27:41 -0700 | [diff] [blame] | 217 | *this = parent; |
| 218 | return; |
| 219 | } |
| 220 | |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 221 | SkPathEffect* pe = parent.fStyle.pathEffect(); |
bsalomon | fb08327 | 2016-05-04 08:27:41 -0700 | [diff] [blame] | 222 | SkTLazy<SkPath> tmpPath; |
| 223 | const GrShape* parentForKey = &parent; |
| 224 | SkTLazy<GrShape> tmpParent; |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 225 | this->initType(Type::kPath); |
| 226 | fPathData.fGenID = 0; |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 227 | if (pe) { |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 228 | const SkPath* srcForPathEffect; |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 229 | if (parent.fType == Type::kPath) { |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 230 | srcForPathEffect = &parent.path(); |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 231 | } else { |
bsalomon | fb08327 | 2016-05-04 08:27:41 -0700 | [diff] [blame] | 232 | srcForPathEffect = tmpPath.init(); |
| 233 | parent.asPath(tmpPath.get()); |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 234 | } |
| 235 | // Should we consider bounds? Would have to include in key, but it'd be nice to know |
| 236 | // if the bounds actually modified anything before including in key. |
bsalomon | fb08327 | 2016-05-04 08:27:41 -0700 | [diff] [blame] | 237 | SkStrokeRec strokeRec = parent.fStyle.strokeRec(); |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 238 | if (!parent.fStyle.applyPathEffectToPath(&this->path(), &strokeRec, *srcForPathEffect, |
bsalomon | 398e3f4 | 2016-06-13 10:22:48 -0700 | [diff] [blame] | 239 | scale)) { |
bsalomon | 0ae36a2 | 2016-07-18 07:31:13 -0700 | [diff] [blame] | 240 | tmpParent.init(*srcForPathEffect, GrStyle(strokeRec, nullptr)); |
| 241 | *this = tmpParent.get()->applyStyle(apply, scale); |
| 242 | return; |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 243 | } |
bsalomon | 97fd2d4 | 2016-05-09 13:02:01 -0700 | [diff] [blame] | 244 | // A path effect has access to change the res scale but we aren't expecting it to and it |
| 245 | // would mess up our key computation. |
| 246 | SkASSERT(scale == strokeRec.getResScale()); |
bsalomon | 1b28c1a | 2016-06-20 12:28:17 -0700 | [diff] [blame] | 247 | if (GrStyle::Apply::kPathEffectAndStrokeRec == apply && strokeRec.needToApply()) { |
| 248 | // The intermediate shape may not be a general path. If we we're just applying |
| 249 | // the path effect then attemptToReduceFromPath would catch it. This means that |
| 250 | // when we subsequently applied the remaining strokeRec we would have a non-path |
| 251 | // parent shape that would be used to determine the the stroked path's key. |
| 252 | // We detect that case here and change parentForKey to a temporary that represents |
| 253 | // the simpler shape so that applying both path effect and the strokerec all at |
| 254 | // once produces the same key. |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 255 | tmpParent.init(this->path(), GrStyle(strokeRec, nullptr)); |
bsalomon | 1b28c1a | 2016-06-20 12:28:17 -0700 | [diff] [blame] | 256 | tmpParent.get()->setInheritedKey(parent, GrStyle::Apply::kPathEffectOnly, scale); |
| 257 | if (!tmpPath.isValid()) { |
| 258 | tmpPath.init(); |
bsalomon | 72dc51c | 2016-04-27 06:46:23 -0700 | [diff] [blame] | 259 | } |
bsalomon | 1b28c1a | 2016-06-20 12:28:17 -0700 | [diff] [blame] | 260 | tmpParent.get()->asPath(tmpPath.get()); |
| 261 | SkStrokeRec::InitStyle fillOrHairline; |
bsalomon | 0ae36a2 | 2016-07-18 07:31:13 -0700 | [diff] [blame] | 262 | // The parent shape may have simplified away the strokeRec, check for that here. |
| 263 | if (tmpParent.get()->style().applies()) { |
| 264 | SkAssertResult(tmpParent.get()->style().applyToPath(&this->path(), &fillOrHairline, |
| 265 | *tmpPath.get(), scale)); |
| 266 | } else if (tmpParent.get()->style().isSimpleFill()) { |
| 267 | fillOrHairline = SkStrokeRec::kFill_InitStyle; |
| 268 | } else { |
| 269 | SkASSERT(tmpParent.get()->style().isSimpleHairline()); |
| 270 | fillOrHairline = SkStrokeRec::kHairline_InitStyle; |
| 271 | } |
bsalomon | 1b28c1a | 2016-06-20 12:28:17 -0700 | [diff] [blame] | 272 | fStyle.resetToInitStyle(fillOrHairline); |
| 273 | parentForKey = tmpParent.get(); |
bsalomon | fb08327 | 2016-05-04 08:27:41 -0700 | [diff] [blame] | 274 | } else { |
| 275 | fStyle = GrStyle(strokeRec, nullptr); |
bsalomon | 72dc51c | 2016-04-27 06:46:23 -0700 | [diff] [blame] | 276 | } |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 277 | } else { |
bsalomon | 97fd2d4 | 2016-05-09 13:02:01 -0700 | [diff] [blame] | 278 | const SkPath* srcForParentStyle; |
bsalomon | fb08327 | 2016-05-04 08:27:41 -0700 | [diff] [blame] | 279 | if (parent.fType == Type::kPath) { |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 280 | srcForParentStyle = &parent.path(); |
bsalomon | fb08327 | 2016-05-04 08:27:41 -0700 | [diff] [blame] | 281 | } else { |
bsalomon | 97fd2d4 | 2016-05-09 13:02:01 -0700 | [diff] [blame] | 282 | srcForParentStyle = tmpPath.init(); |
bsalomon | fb08327 | 2016-05-04 08:27:41 -0700 | [diff] [blame] | 283 | parent.asPath(tmpPath.get()); |
| 284 | } |
bsalomon | 97fd2d4 | 2016-05-09 13:02:01 -0700 | [diff] [blame] | 285 | SkStrokeRec::InitStyle fillOrHairline; |
| 286 | SkASSERT(parent.fStyle.applies()); |
| 287 | SkASSERT(!parent.fStyle.pathEffect()); |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 288 | SkAssertResult(parent.fStyle.applyToPath(&this->path(), &fillOrHairline, *srcForParentStyle, |
bsalomon | 97fd2d4 | 2016-05-09 13:02:01 -0700 | [diff] [blame] | 289 | scale)); |
| 290 | fStyle.resetToInitStyle(fillOrHairline); |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 291 | } |
bsalomon | 1b28c1a | 2016-06-20 12:28:17 -0700 | [diff] [blame] | 292 | this->attemptToSimplifyPath(); |
bsalomon | 97fd2d4 | 2016-05-09 13:02:01 -0700 | [diff] [blame] | 293 | this->setInheritedKey(*parentForKey, apply, scale); |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 294 | } |
bsalomon | ee29564 | 2016-06-06 14:01:25 -0700 | [diff] [blame] | 295 | |
bsalomon | 1b28c1a | 2016-06-20 12:28:17 -0700 | [diff] [blame] | 296 | void GrShape::attemptToSimplifyPath() { |
bsalomon | ee29564 | 2016-06-06 14:01:25 -0700 | [diff] [blame] | 297 | SkRect rect; |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 298 | SkRRect rrect; |
| 299 | SkPath::Direction rrectDir; |
| 300 | unsigned rrectStart; |
| 301 | bool inverted = this->path().isInverseFillType(); |
bsalomon | 0a0f67e | 2016-06-28 11:56:42 -0700 | [diff] [blame] | 302 | SkPoint pts[2]; |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 303 | if (this->path().isEmpty()) { |
| 304 | this->changeType(Type::kEmpty); |
bsalomon | 0a0f67e | 2016-06-28 11:56:42 -0700 | [diff] [blame] | 305 | } else if (this->path().isLine(pts)) { |
| 306 | this->changeType(Type::kLine); |
| 307 | fLineData.fPts[0] = pts[0]; |
| 308 | fLineData.fPts[1] = pts[1]; |
| 309 | fLineData.fInverted = inverted; |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 310 | } else if (this->path().isRRect(&rrect, &rrectDir, &rrectStart)) { |
| 311 | this->changeType(Type::kRRect); |
| 312 | fRRectData.fRRect = rrect; |
| 313 | fRRectData.fDir = rrectDir; |
| 314 | fRRectData.fStart = rrectStart; |
| 315 | fRRectData.fInverted = inverted; |
bsalomon | 1b28c1a | 2016-06-20 12:28:17 -0700 | [diff] [blame] | 316 | // Currently SkPath does not acknowledge that empty, rect, or oval subtypes as rrects. |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 317 | SkASSERT(!fRRectData.fRRect.isEmpty()); |
| 318 | SkASSERT(fRRectData.fRRect.getType() != SkRRect::kRect_Type); |
| 319 | SkASSERT(fRRectData.fRRect.getType() != SkRRect::kOval_Type); |
| 320 | } else if (this->path().isOval(&rect, &rrectDir, &rrectStart)) { |
| 321 | this->changeType(Type::kRRect); |
| 322 | fRRectData.fRRect.setOval(rect); |
| 323 | fRRectData.fDir = rrectDir; |
| 324 | fRRectData.fInverted = inverted; |
bsalomon | 1b28c1a | 2016-06-20 12:28:17 -0700 | [diff] [blame] | 325 | // convert from oval indexing to rrect indexiing. |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 326 | fRRectData.fStart = 2 * rrectStart; |
| 327 | } else if (SkPathPriv::IsSimpleClosedRect(this->path(), &rect, &rrectDir, &rrectStart)) { |
| 328 | this->changeType(Type::kRRect); |
bsalomon | 1b28c1a | 2016-06-20 12:28:17 -0700 | [diff] [blame] | 329 | // When there is a path effect we restrict rect detection to the narrower API that |
| 330 | // gives us the starting position. Otherwise, we will retry with the more aggressive |
| 331 | // isRect(). |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 332 | fRRectData.fRRect.setRect(rect); |
| 333 | fRRectData.fInverted = inverted; |
| 334 | fRRectData.fDir = rrectDir; |
bsalomon | 1b28c1a | 2016-06-20 12:28:17 -0700 | [diff] [blame] | 335 | // convert from rect indexing to rrect indexiing. |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 336 | fRRectData.fStart = 2 * rrectStart; |
bsalomon | 1b28c1a | 2016-06-20 12:28:17 -0700 | [diff] [blame] | 337 | } else if (!this->style().hasPathEffect()) { |
bsalomon | ee29564 | 2016-06-06 14:01:25 -0700 | [diff] [blame] | 338 | bool closed; |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 339 | if (this->path().isRect(&rect, &closed, nullptr)) { |
bsalomon | 1b28c1a | 2016-06-20 12:28:17 -0700 | [diff] [blame] | 340 | if (closed || this->style().isSimpleFill()) { |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 341 | this->changeType(Type::kRRect); |
| 342 | fRRectData.fRRect.setRect(rect); |
bsalomon | ee29564 | 2016-06-06 14:01:25 -0700 | [diff] [blame] | 343 | // Since there is no path effect the dir and start index is immaterial. |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 344 | fRRectData.fDir = kDefaultRRectDir; |
| 345 | fRRectData.fStart = kDefaultRRectStart; |
bsalomon | 1b28c1a | 2016-06-20 12:28:17 -0700 | [diff] [blame] | 346 | // There isn't dashing so we will have to preserver inverseness. |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 347 | fRRectData.fInverted = inverted; |
bsalomon | ee29564 | 2016-06-06 14:01:25 -0700 | [diff] [blame] | 348 | } |
| 349 | } |
| 350 | } |
bsalomon | 1b28c1a | 2016-06-20 12:28:17 -0700 | [diff] [blame] | 351 | if (Type::kPath != fType) { |
bsalomon | 1b28c1a | 2016-06-20 12:28:17 -0700 | [diff] [blame] | 352 | fInheritedKey.reset(0); |
| 353 | if (Type::kRRect == fType) { |
| 354 | this->attemptToSimplifyRRect(); |
bsalomon | 0a0f67e | 2016-06-28 11:56:42 -0700 | [diff] [blame] | 355 | } else if (Type::kLine == fType) { |
| 356 | this->attemptToSimplifyLine(); |
bsalomon | 1b28c1a | 2016-06-20 12:28:17 -0700 | [diff] [blame] | 357 | } |
bsalomon | 93f66bc | 2016-06-21 08:35:49 -0700 | [diff] [blame] | 358 | } else { |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 359 | if (fInheritedKey.count() || this->path().isVolatile()) { |
| 360 | fPathData.fGenID = 0; |
bsalomon | 93f66bc | 2016-06-21 08:35:49 -0700 | [diff] [blame] | 361 | } else { |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 362 | fPathData.fGenID = this->path().getGenerationID(); |
bsalomon | 93f66bc | 2016-06-21 08:35:49 -0700 | [diff] [blame] | 363 | } |
fmalita | e918417 | 2016-09-20 14:06:57 -0700 | [diff] [blame] | 364 | if (this->style().isSimpleFill()) { |
| 365 | this->path().close(); |
| 366 | this->path().setIsVolatile(true); |
| 367 | } |
bsalomon | a4817af | 2016-06-23 11:48:26 -0700 | [diff] [blame] | 368 | if (!this->style().hasNonDashPathEffect()) { |
| 369 | if (this->style().strokeRec().getStyle() == SkStrokeRec::kStroke_Style || |
| 370 | this->style().strokeRec().getStyle() == SkStrokeRec::kHairline_Style) { |
| 371 | // Stroke styles don't differentiate between winding and even/odd. |
| 372 | // Moreover, dashing ignores inverseness (skbug.com/5421) |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 373 | bool inverse = !this->style().isDashed() && this->path().isInverseFillType(); |
bsalomon | a4817af | 2016-06-23 11:48:26 -0700 | [diff] [blame] | 374 | if (inverse) { |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 375 | this->path().setFillType(kDefaultPathInverseFillType); |
bsalomon | a4817af | 2016-06-23 11:48:26 -0700 | [diff] [blame] | 376 | } else { |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 377 | this->path().setFillType(kDefaultPathFillType); |
bsalomon | a4817af | 2016-06-23 11:48:26 -0700 | [diff] [blame] | 378 | } |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 379 | } else if (this->path().isConvex()) { |
bsalomon | a4817af | 2016-06-23 11:48:26 -0700 | [diff] [blame] | 380 | // There is no distinction between even/odd and non-zero winding count for convex |
| 381 | // paths. |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 382 | if (this->path().isInverseFillType()) { |
| 383 | this->path().setFillType(kDefaultPathInverseFillType); |
bsalomon | a4817af | 2016-06-23 11:48:26 -0700 | [diff] [blame] | 384 | } else { |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 385 | this->path().setFillType(kDefaultPathFillType); |
bsalomon | a4817af | 2016-06-23 11:48:26 -0700 | [diff] [blame] | 386 | } |
bsalomon | 93f66bc | 2016-06-21 08:35:49 -0700 | [diff] [blame] | 387 | } |
| 388 | } |
bsalomon | 1b28c1a | 2016-06-20 12:28:17 -0700 | [diff] [blame] | 389 | } |
| 390 | } |
| 391 | |
| 392 | void GrShape::attemptToSimplifyRRect() { |
| 393 | SkASSERT(Type::kRRect == fType); |
| 394 | SkASSERT(!fInheritedKey.count()); |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 395 | if (fRRectData.fRRect.isEmpty()) { |
bsalomon | 1b28c1a | 2016-06-20 12:28:17 -0700 | [diff] [blame] | 396 | fType = Type::kEmpty; |
| 397 | return; |
| 398 | } |
| 399 | if (!this->style().hasPathEffect()) { |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 400 | fRRectData.fDir = kDefaultRRectDir; |
| 401 | fRRectData.fStart = kDefaultRRectStart; |
bsalomon | 1b28c1a | 2016-06-20 12:28:17 -0700 | [diff] [blame] | 402 | } else if (fStyle.isDashed()) { |
| 403 | // Dashing ignores the inverseness (currently). skbug.com/5421 |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 404 | fRRectData.fInverted = false; |
bsalomon | 1b28c1a | 2016-06-20 12:28:17 -0700 | [diff] [blame] | 405 | } |
bsalomon | 487f8d3 | 2016-07-20 07:15:44 -0700 | [diff] [blame] | 406 | // Turn a stroke-and-filled miter rect into a filled rect. TODO: more rrect stroke shortcuts. |
| 407 | if (!fStyle.hasPathEffect() && |
| 408 | fStyle.strokeRec().getStyle() == SkStrokeRec::kStrokeAndFill_Style && |
| 409 | fStyle.strokeRec().getJoin() == SkPaint::kMiter_Join && |
| 410 | fStyle.strokeRec().getMiter() >= SK_ScalarSqrt2 && |
| 411 | fRRectData.fRRect.isRect()) { |
| 412 | SkScalar r = fStyle.strokeRec().getWidth() / 2; |
| 413 | fRRectData.fRRect = SkRRect::MakeRect(fRRectData.fRRect.rect().makeOutset(r, r)); |
| 414 | fStyle = GrStyle::SimpleFill(); |
| 415 | } |
bsalomon | ee29564 | 2016-06-06 14:01:25 -0700 | [diff] [blame] | 416 | } |
bsalomon | 0a0f67e | 2016-06-28 11:56:42 -0700 | [diff] [blame] | 417 | |
| 418 | void GrShape::attemptToSimplifyLine() { |
bsalomon | 0ae36a2 | 2016-07-18 07:31:13 -0700 | [diff] [blame] | 419 | SkASSERT(Type::kLine == fType); |
| 420 | SkASSERT(!fInheritedKey.count()); |
| 421 | if (fStyle.isDashed()) { |
| 422 | // Dashing ignores inverseness. |
| 423 | fLineData.fInverted = false; |
| 424 | return; |
| 425 | } else if (fStyle.hasPathEffect()) { |
| 426 | return; |
| 427 | } |
| 428 | if (fStyle.strokeRec().getStyle() == SkStrokeRec::kStrokeAndFill_Style) { |
| 429 | // Make stroke + fill be stroke since the fill is empty. |
| 430 | SkStrokeRec rec = fStyle.strokeRec(); |
| 431 | rec.setStrokeStyle(fStyle.strokeRec().getWidth(), false); |
| 432 | fStyle = GrStyle(rec, nullptr); |
| 433 | } |
bsalomon | 0a0f67e | 2016-06-28 11:56:42 -0700 | [diff] [blame] | 434 | if (fStyle.isSimpleFill() && !fLineData.fInverted) { |
| 435 | this->changeType(Type::kEmpty); |
bsalomon | 0ae36a2 | 2016-07-18 07:31:13 -0700 | [diff] [blame] | 436 | return; |
| 437 | } |
| 438 | SkPoint* pts = fLineData.fPts; |
| 439 | if (fStyle.strokeRec().getStyle() == SkStrokeRec::kStroke_Style) { |
| 440 | // If it is horizontal or vertical we will turn it into a filled rrect. |
| 441 | SkRect rect; |
| 442 | rect.fLeft = SkTMin(pts[0].fX, pts[1].fX); |
| 443 | rect.fRight = SkTMax(pts[0].fX, pts[1].fX); |
| 444 | rect.fTop = SkTMin(pts[0].fY, pts[1].fY); |
| 445 | rect.fBottom = SkTMax(pts[0].fY, pts[1].fY); |
| 446 | bool eqX = rect.fLeft == rect.fRight; |
| 447 | bool eqY = rect.fTop == rect.fBottom; |
| 448 | if (eqX || eqY) { |
| 449 | SkScalar r = fStyle.strokeRec().getWidth() / 2; |
| 450 | bool inverted = fLineData.fInverted; |
| 451 | this->changeType(Type::kRRect); |
| 452 | switch (fStyle.strokeRec().getCap()) { |
| 453 | case SkPaint::kButt_Cap: |
| 454 | if (eqX && eqY) { |
| 455 | this->changeType(Type::kEmpty); |
| 456 | return; |
| 457 | } |
| 458 | if (eqX) { |
| 459 | rect.outset(r, 0); |
| 460 | } else { |
| 461 | rect.outset(0, r); |
| 462 | } |
| 463 | fRRectData.fRRect = SkRRect::MakeRect(rect); |
| 464 | break; |
| 465 | case SkPaint::kSquare_Cap: |
| 466 | rect.outset(r, r); |
| 467 | fRRectData.fRRect = SkRRect::MakeRect(rect); |
| 468 | break; |
| 469 | case SkPaint::kRound_Cap: |
| 470 | rect.outset(r, r); |
| 471 | fRRectData.fRRect = SkRRect::MakeRectXY(rect, r, r); |
| 472 | break; |
bsalomon | 0a0f67e | 2016-06-28 11:56:42 -0700 | [diff] [blame] | 473 | } |
bsalomon | 0ae36a2 | 2016-07-18 07:31:13 -0700 | [diff] [blame] | 474 | fRRectData.fInverted = inverted; |
| 475 | fRRectData.fDir = kDefaultRRectDir; |
| 476 | fRRectData.fStart = kDefaultRRectStart; |
| 477 | if (fRRectData.fRRect.isEmpty()) { |
| 478 | // This can happen when r is very small relative to the rect edges. |
| 479 | this->changeType(Type::kEmpty); |
| 480 | return; |
| 481 | } |
| 482 | fStyle = GrStyle::SimpleFill(); |
| 483 | return; |
bsalomon | 0a0f67e | 2016-06-28 11:56:42 -0700 | [diff] [blame] | 484 | } |
| 485 | } |
bsalomon | 0ae36a2 | 2016-07-18 07:31:13 -0700 | [diff] [blame] | 486 | // Only path effects could care about the order of the points. Otherwise canonicalize |
| 487 | // the point order. |
| 488 | if (pts[1].fY < pts[0].fY || (pts[1].fY == pts[0].fY && pts[1].fX < pts[0].fX)) { |
| 489 | SkTSwap(pts[0], pts[1]); |
| 490 | } |
bsalomon | 0a0f67e | 2016-06-28 11:56:42 -0700 | [diff] [blame] | 491 | } |