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) { |
| 11 | bool wasPath = Type::kPath == fType; |
| 12 | fStyle = that.fStyle; |
| 13 | fType = that.fType; |
| 14 | switch (fType) { |
| 15 | case Type::kEmpty: |
| 16 | if (wasPath) { |
| 17 | fPath.reset(); |
| 18 | } |
| 19 | break; |
| 20 | case Type::kRRect: |
| 21 | if (wasPath) { |
| 22 | fPath.reset(); |
| 23 | } |
| 24 | fRRect = that.fRRect; |
| 25 | break; |
| 26 | case Type::kPath: |
| 27 | if (wasPath) { |
| 28 | *fPath.get() = *that.fPath.get(); |
| 29 | } else { |
| 30 | fPath.set(*that.fPath.get()); |
| 31 | } |
| 32 | break; |
| 33 | } |
| 34 | fInheritedKey.reset(that.fInheritedKey.count()); |
benjaminwagner | d9cca4a | 2016-05-04 11:06:19 -0700 | [diff] [blame] | 35 | sk_careful_memcpy(fInheritedKey.get(), that.fInheritedKey.get(), |
| 36 | sizeof(uint32_t) * fInheritedKey.count()); |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 37 | return *this; |
| 38 | } |
| 39 | |
bsalomon | 9fb4203 | 2016-05-13 09:23:38 -0700 | [diff] [blame^] | 40 | const SkRect& GrShape::bounds() const { |
| 41 | static constexpr SkRect kEmpty = SkRect::MakeEmpty(); |
| 42 | switch (fType) { |
| 43 | case Type::kEmpty: |
| 44 | return kEmpty; |
| 45 | case Type::kRRect: |
| 46 | return fRRect.getBounds(); |
| 47 | case Type::kPath: |
| 48 | return fPath.get()->getBounds(); |
| 49 | } |
| 50 | SkFAIL("Unknown shape type"); |
| 51 | return kEmpty; |
| 52 | } |
| 53 | |
| 54 | void GrShape::styledBounds(SkRect* bounds) const { |
| 55 | if (Type::kEmpty == fType && !fStyle.hasNonDashPathEffect()) { |
| 56 | *bounds = SkRect::MakeEmpty(); |
| 57 | } else { |
| 58 | fStyle.adjustBounds(bounds, this->bounds()); |
| 59 | } |
| 60 | } |
| 61 | |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 62 | int GrShape::unstyledKeySize() const { |
| 63 | if (fInheritedKey.count()) { |
| 64 | return fInheritedKey.count(); |
| 65 | } |
| 66 | switch (fType) { |
| 67 | case Type::kEmpty: |
| 68 | return 1; |
| 69 | case Type::kRRect: |
| 70 | SkASSERT(!fInheritedKey.count()); |
| 71 | SkASSERT(0 == SkRRect::kSizeInMemory % sizeof(uint32_t)); |
| 72 | return SkRRect::kSizeInMemory / sizeof(uint32_t); |
| 73 | case Type::kPath: |
| 74 | if (fPath.get()->isVolatile()) { |
| 75 | return -1; |
| 76 | } else { |
| 77 | return 1; |
| 78 | } |
| 79 | } |
| 80 | SkFAIL("Should never get here."); |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | void GrShape::writeUnstyledKey(uint32_t* key) const { |
| 85 | SkASSERT(this->unstyledKeySize()); |
| 86 | SkDEBUGCODE(uint32_t* origKey = key;) |
| 87 | if (fInheritedKey.count()) { |
| 88 | memcpy(key, fInheritedKey.get(), sizeof(uint32_t) * fInheritedKey.count()); |
| 89 | SkDEBUGCODE(key += fInheritedKey.count();) |
| 90 | } else { |
| 91 | switch (fType) { |
| 92 | case Type::kEmpty: |
| 93 | *key++ = 1; |
| 94 | break; |
| 95 | case Type::kRRect: |
| 96 | fRRect.writeToMemory(key); |
| 97 | key += SkRRect::kSizeInMemory / sizeof(uint32_t); |
| 98 | break; |
| 99 | case Type::kPath: |
| 100 | SkASSERT(!fPath.get()->isVolatile()); |
| 101 | *key++ = fPath.get()->getGenerationID(); |
| 102 | break; |
| 103 | } |
| 104 | } |
| 105 | SkASSERT(key - origKey == this->unstyledKeySize()); |
| 106 | } |
| 107 | |
bsalomon | 97fd2d4 | 2016-05-09 13:02:01 -0700 | [diff] [blame] | 108 | void GrShape::setInheritedKey(const GrShape &parent, GrStyle::Apply apply, SkScalar scale) { |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 109 | SkASSERT(!fInheritedKey.count()); |
| 110 | // If the output shape turns out to be simple, then we will just use its geometric key |
| 111 | if (Type::kPath == fType) { |
| 112 | // We want ApplyFullStyle(ApplyPathEffect(shape)) to have the same key as |
| 113 | // ApplyFullStyle(shape). |
| 114 | // The full key is structured as (geo,path_effect,stroke). |
| 115 | // If we do ApplyPathEffect we get get,path_effect as the inherited key. If we then |
| 116 | // do ApplyFullStyle we'll memcpy geo,path_effect into the new inherited key |
| 117 | // and then append the style key (which should now be stroke only) at the end. |
| 118 | int parentCnt = parent.fInheritedKey.count(); |
| 119 | bool useParentGeoKey = !parentCnt; |
| 120 | if (useParentGeoKey) { |
| 121 | parentCnt = parent.unstyledKeySize(); |
bsalomon | 72dc51c | 2016-04-27 06:46:23 -0700 | [diff] [blame] | 122 | if (parentCnt < 0) { |
| 123 | // The parent's geometry has no key so we will have no key. |
| 124 | fPath.get()->setIsVolatile(true); |
| 125 | return; |
| 126 | } |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 127 | } |
bsalomon | 0607756 | 2016-05-04 13:50:29 -0700 | [diff] [blame] | 128 | uint32_t styleKeyFlags = 0; |
| 129 | if (parent.knownToBeClosed()) { |
| 130 | styleKeyFlags |= GrStyle::kClosed_KeyFlag; |
| 131 | } |
| 132 | int styleCnt = GrStyle::KeySize(parent.fStyle, apply, styleKeyFlags); |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 133 | if (styleCnt < 0) { |
| 134 | // The style doesn't allow a key, set the path to volatile so that we fail when |
| 135 | // we try to get a key for the shape. |
| 136 | fPath.get()->setIsVolatile(true); |
bsalomon | 72dc51c | 2016-04-27 06:46:23 -0700 | [diff] [blame] | 137 | return; |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 138 | } |
bsalomon | 72dc51c | 2016-04-27 06:46:23 -0700 | [diff] [blame] | 139 | fInheritedKey.reset(parentCnt + styleCnt); |
| 140 | if (useParentGeoKey) { |
| 141 | // This will be the geo key. |
| 142 | parent.writeUnstyledKey(fInheritedKey.get()); |
| 143 | } else { |
| 144 | // This should be (geo,path_effect). |
| 145 | memcpy(fInheritedKey.get(), parent.fInheritedKey.get(), |
| 146 | parentCnt * sizeof(uint32_t)); |
| 147 | } |
| 148 | // Now turn (geo,path_effect) or (geo) into (geo,path_effect,stroke) |
bsalomon | 97fd2d4 | 2016-05-09 13:02:01 -0700 | [diff] [blame] | 149 | GrStyle::WriteKey(fInheritedKey.get() + parentCnt, parent.fStyle, apply, scale, |
| 150 | styleKeyFlags); |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | |
| 154 | GrShape::GrShape(const GrShape& that) : fType(that.fType), fStyle(that.fStyle) { |
| 155 | switch (fType) { |
| 156 | case Type::kEmpty: |
| 157 | return; |
| 158 | case Type::kRRect: |
| 159 | fRRect = that.fRRect; |
| 160 | return; |
| 161 | case Type::kPath: |
| 162 | fPath.set(*that.fPath.get()); |
| 163 | return; |
| 164 | } |
| 165 | fInheritedKey.reset(that.fInheritedKey.count()); |
| 166 | memcpy(fInheritedKey.get(), that.fInheritedKey.get(), |
| 167 | sizeof(uint32_t) * fInheritedKey.count()); |
| 168 | } |
| 169 | |
bsalomon | 97fd2d4 | 2016-05-09 13:02:01 -0700 | [diff] [blame] | 170 | GrShape::GrShape(const GrShape& parent, GrStyle::Apply apply, SkScalar scale) { |
| 171 | // TODO: Add some quantization of scale for better cache performance here or leave that up |
| 172 | // to caller? |
| 173 | // TODO: For certain shapes and stroke params we could ignore the scale. (e.g. miter or bevel |
| 174 | // stroke of a rect). |
bsalomon | fb08327 | 2016-05-04 08:27:41 -0700 | [diff] [blame] | 175 | if (!parent.style().applies() || |
| 176 | (GrStyle::Apply::kPathEffectOnly == apply && !parent.style().pathEffect())) { |
| 177 | fType = Type::kEmpty; |
| 178 | *this = parent; |
| 179 | return; |
| 180 | } |
| 181 | |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 182 | SkPathEffect* pe = parent.fStyle.pathEffect(); |
bsalomon | fb08327 | 2016-05-04 08:27:41 -0700 | [diff] [blame] | 183 | SkTLazy<SkPath> tmpPath; |
| 184 | const GrShape* parentForKey = &parent; |
| 185 | SkTLazy<GrShape> tmpParent; |
| 186 | fType = Type::kPath; |
| 187 | fPath.init(); |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 188 | if (pe) { |
bsalomon | fb08327 | 2016-05-04 08:27:41 -0700 | [diff] [blame] | 189 | SkPath* srcForPathEffect; |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 190 | if (parent.fType == Type::kPath) { |
bsalomon | fb08327 | 2016-05-04 08:27:41 -0700 | [diff] [blame] | 191 | srcForPathEffect = parent.fPath.get(); |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 192 | } else { |
bsalomon | fb08327 | 2016-05-04 08:27:41 -0700 | [diff] [blame] | 193 | srcForPathEffect = tmpPath.init(); |
| 194 | parent.asPath(tmpPath.get()); |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 195 | } |
| 196 | // Should we consider bounds? Would have to include in key, but it'd be nice to know |
| 197 | // if the bounds actually modified anything before including in key. |
bsalomon | fb08327 | 2016-05-04 08:27:41 -0700 | [diff] [blame] | 198 | SkStrokeRec strokeRec = parent.fStyle.strokeRec(); |
bsalomon | 97fd2d4 | 2016-05-09 13:02:01 -0700 | [diff] [blame] | 199 | strokeRec.setResScale(scale); |
bsalomon | fb08327 | 2016-05-04 08:27:41 -0700 | [diff] [blame] | 200 | if (!pe->filterPath(fPath.get(), *srcForPathEffect, &strokeRec, nullptr)) { |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 201 | // Make an empty unstyled shape if filtering fails. |
| 202 | fType = Type::kEmpty; |
| 203 | fStyle = GrStyle(); |
| 204 | fPath.reset(); |
| 205 | return; |
| 206 | } |
bsalomon | 97fd2d4 | 2016-05-09 13:02:01 -0700 | [diff] [blame] | 207 | // A path effect has access to change the res scale but we aren't expecting it to and it |
| 208 | // would mess up our key computation. |
| 209 | SkASSERT(scale == strokeRec.getResScale()); |
bsalomon | fb08327 | 2016-05-04 08:27:41 -0700 | [diff] [blame] | 210 | if (GrStyle::Apply::kPathEffectAndStrokeRec == apply) { |
| 211 | if (strokeRec.needToApply()) { |
| 212 | // The intermediate shape may not be a general path. If we we're just applying |
| 213 | // the path effect then attemptToReduceFromPath would catch it. This means that |
| 214 | // when we subsequently applied the remaining strokeRec we would have a non-path |
| 215 | // parent shape that would be used to determine the the stroked path's key. |
| 216 | // We detect that case here and change parentForKey to a temporary that represents |
| 217 | // the simpler shape so that applying both path effect and the strokerec all at |
| 218 | // once produces the same key. |
| 219 | SkRRect rrect; |
| 220 | Type parentType = AttemptToReduceFromPathImpl(*fPath.get(), &rrect, nullptr, |
| 221 | strokeRec); |
| 222 | switch (parentType) { |
| 223 | case Type::kEmpty: |
| 224 | tmpParent.init(); |
| 225 | parentForKey = tmpParent.get(); |
| 226 | break; |
| 227 | case Type::kRRect: |
| 228 | tmpParent.init(rrect, GrStyle(strokeRec, nullptr)); |
| 229 | parentForKey = tmpParent.get(); |
| 230 | case Type::kPath: |
| 231 | break; |
| 232 | } |
| 233 | SkAssertResult(strokeRec.applyToPath(fPath.get(), *fPath.get())); |
| 234 | } else { |
| 235 | fStyle = GrStyle(strokeRec, nullptr); |
bsalomon | 72dc51c | 2016-04-27 06:46:23 -0700 | [diff] [blame] | 236 | } |
bsalomon | fb08327 | 2016-05-04 08:27:41 -0700 | [diff] [blame] | 237 | } else { |
| 238 | fStyle = GrStyle(strokeRec, nullptr); |
bsalomon | 72dc51c | 2016-04-27 06:46:23 -0700 | [diff] [blame] | 239 | } |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 240 | } else { |
bsalomon | 97fd2d4 | 2016-05-09 13:02:01 -0700 | [diff] [blame] | 241 | const SkPath* srcForParentStyle; |
bsalomon | fb08327 | 2016-05-04 08:27:41 -0700 | [diff] [blame] | 242 | if (parent.fType == Type::kPath) { |
bsalomon | 97fd2d4 | 2016-05-09 13:02:01 -0700 | [diff] [blame] | 243 | srcForParentStyle = parent.fPath.get(); |
bsalomon | fb08327 | 2016-05-04 08:27:41 -0700 | [diff] [blame] | 244 | } else { |
bsalomon | 97fd2d4 | 2016-05-09 13:02:01 -0700 | [diff] [blame] | 245 | srcForParentStyle = tmpPath.init(); |
bsalomon | fb08327 | 2016-05-04 08:27:41 -0700 | [diff] [blame] | 246 | parent.asPath(tmpPath.get()); |
| 247 | } |
bsalomon | 97fd2d4 | 2016-05-09 13:02:01 -0700 | [diff] [blame] | 248 | SkStrokeRec::InitStyle fillOrHairline; |
| 249 | SkASSERT(parent.fStyle.applies()); |
| 250 | SkASSERT(!parent.fStyle.pathEffect()); |
| 251 | SkAssertResult(parent.fStyle.applyToPath(fPath.get(), &fillOrHairline, *srcForParentStyle, |
| 252 | scale)); |
| 253 | fStyle.resetToInitStyle(fillOrHairline); |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 254 | } |
bsalomon | 72dc51c | 2016-04-27 06:46:23 -0700 | [diff] [blame] | 255 | this->attemptToReduceFromPath(); |
bsalomon | 97fd2d4 | 2016-05-09 13:02:01 -0700 | [diff] [blame] | 256 | this->setInheritedKey(*parentForKey, apply, scale); |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 257 | } |