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