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; |
Brian Osman | f6f7cf6 | 2017-09-25 16:49:55 -0400 | [diff] [blame] | 12 | fOriginalPath = that.fOriginalPath; |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 13 | this->changeType(that.fType, Type::kPath == that.fType ? &that.path() : nullptr); |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 14 | switch (fType) { |
| 15 | case Type::kEmpty: |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 16 | break; |
Brian Salomon | 085c086 | 2017-08-31 15:44:51 -0400 | [diff] [blame] | 17 | case Type::kInvertedEmpty: |
| 18 | break; |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 19 | case Type::kRRect: |
bsalomon | 0a0f67e | 2016-06-28 11:56:42 -0700 | [diff] [blame] | 20 | fRRectData = that.fRRectData; |
| 21 | break; |
| 22 | case Type::kLine: |
| 23 | fLineData = that.fLineData; |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 24 | break; |
| 25 | case Type::kPath: |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 26 | fPathData.fGenID = that.fPathData.fGenID; |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 27 | break; |
| 28 | } |
| 29 | fInheritedKey.reset(that.fInheritedKey.count()); |
benjaminwagner | d9cca4a | 2016-05-04 11:06:19 -0700 | [diff] [blame] | 30 | sk_careful_memcpy(fInheritedKey.get(), that.fInheritedKey.get(), |
| 31 | sizeof(uint32_t) * fInheritedKey.count()); |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 32 | return *this; |
| 33 | } |
| 34 | |
Brian Salomon | 4f40caf | 2017-09-01 09:00:45 -0400 | [diff] [blame] | 35 | static bool flip_inversion(bool originalIsInverted, GrShape::FillInversion inversion) { |
| 36 | switch (inversion) { |
| 37 | case GrShape::FillInversion::kPreserve: |
| 38 | return false; |
| 39 | case GrShape::FillInversion::kFlip: |
| 40 | return true; |
| 41 | case GrShape::FillInversion::kForceInverted: |
| 42 | return !originalIsInverted; |
| 43 | case GrShape::FillInversion::kForceNoninverted: |
| 44 | return originalIsInverted; |
| 45 | } |
| 46 | return false; |
| 47 | } |
| 48 | |
| 49 | static bool is_inverted(bool originalIsInverted, GrShape::FillInversion inversion) { |
| 50 | switch (inversion) { |
| 51 | case GrShape::FillInversion::kPreserve: |
| 52 | return originalIsInverted; |
| 53 | case GrShape::FillInversion::kFlip: |
| 54 | return !originalIsInverted; |
| 55 | case GrShape::FillInversion::kForceInverted: |
| 56 | return true; |
| 57 | case GrShape::FillInversion::kForceNoninverted: |
| 58 | return false; |
| 59 | } |
| 60 | return false; |
| 61 | } |
| 62 | |
| 63 | GrShape GrShape::MakeFilled(const GrShape& original, FillInversion inversion) { |
| 64 | if (original.style().isSimpleFill() && !flip_inversion(original.inverseFilled(), inversion)) { |
| 65 | // By returning the original rather than falling through we can preserve any inherited style |
| 66 | // key. Otherwise, we wipe it out below since the style change invalidates it. |
| 67 | return original; |
| 68 | } |
| 69 | GrShape result; |
Brian Osman | f6f7cf6 | 2017-09-25 16:49:55 -0400 | [diff] [blame] | 70 | result.fOriginalPath = original.fOriginalPath; |
Brian Salomon | 4f40caf | 2017-09-01 09:00:45 -0400 | [diff] [blame] | 71 | switch (original.fType) { |
| 72 | case Type::kRRect: |
| 73 | result.fType = original.fType; |
| 74 | result.fRRectData.fRRect = original.fRRectData.fRRect; |
| 75 | result.fRRectData.fDir = kDefaultRRectDir; |
| 76 | result.fRRectData.fStart = kDefaultRRectStart; |
| 77 | result.fRRectData.fInverted = is_inverted(original.fRRectData.fInverted, inversion); |
| 78 | break; |
| 79 | case Type::kLine: |
| 80 | // Lines don't fill. |
| 81 | if (is_inverted(original.fLineData.fInverted, inversion)) { |
| 82 | result.fType = Type::kInvertedEmpty; |
| 83 | } else { |
| 84 | result.fType = Type::kEmpty; |
| 85 | } |
| 86 | break; |
| 87 | case Type::kEmpty: |
| 88 | result.fType = is_inverted(false, inversion) ? Type::kInvertedEmpty : Type::kEmpty; |
| 89 | break; |
| 90 | case Type::kInvertedEmpty: |
| 91 | result.fType = is_inverted(true, inversion) ? Type::kInvertedEmpty : Type::kEmpty; |
| 92 | break; |
| 93 | case Type::kPath: |
| 94 | result.initType(Type::kPath, &original.fPathData.fPath); |
| 95 | result.fPathData.fGenID = original.fPathData.fGenID; |
| 96 | if (flip_inversion(original.fPathData.fPath.isInverseFillType(), inversion)) { |
| 97 | result.fPathData.fPath.toggleInverseFillType(); |
| 98 | } |
| 99 | if (!original.style().isSimpleFill()) { |
| 100 | // Going from a non-filled style to fill may allow additional simplifications (e.g. |
| 101 | // closing an open rect that wasn't closed in the original shape because it had |
| 102 | // stroke style). |
| 103 | result.attemptToSimplifyPath(); |
| 104 | } |
| 105 | break; |
| 106 | } |
| 107 | // We don't copy the inherited key since it can contain path effect information that we just |
| 108 | // stripped. |
| 109 | return result; |
| 110 | } |
| 111 | |
bsalomon | 0a0f67e | 2016-06-28 11:56:42 -0700 | [diff] [blame] | 112 | SkRect GrShape::bounds() const { |
bsalomon | 0ae36a2 | 2016-07-18 07:31:13 -0700 | [diff] [blame] | 113 | // Bounds where left == bottom or top == right can indicate a line or point shape. We return |
| 114 | // inverted bounds for a truly empty shape. |
| 115 | static constexpr SkRect kInverted = SkRect::MakeLTRB(1, 1, -1, -1); |
bsalomon | 9fb4203 | 2016-05-13 09:23:38 -0700 | [diff] [blame] | 116 | switch (fType) { |
| 117 | case Type::kEmpty: |
bsalomon | 0ae36a2 | 2016-07-18 07:31:13 -0700 | [diff] [blame] | 118 | return kInverted; |
Brian Salomon | 085c086 | 2017-08-31 15:44:51 -0400 | [diff] [blame] | 119 | case Type::kInvertedEmpty: |
| 120 | return kInverted; |
bsalomon | 0a0f67e | 2016-06-28 11:56:42 -0700 | [diff] [blame] | 121 | case Type::kLine: { |
| 122 | SkRect bounds; |
| 123 | if (fLineData.fPts[0].fX < fLineData.fPts[1].fX) { |
| 124 | bounds.fLeft = fLineData.fPts[0].fX; |
| 125 | bounds.fRight = fLineData.fPts[1].fX; |
| 126 | } else { |
| 127 | bounds.fLeft = fLineData.fPts[1].fX; |
| 128 | bounds.fRight = fLineData.fPts[0].fX; |
| 129 | } |
| 130 | if (fLineData.fPts[0].fY < fLineData.fPts[1].fY) { |
| 131 | bounds.fTop = fLineData.fPts[0].fY; |
| 132 | bounds.fBottom = fLineData.fPts[1].fY; |
| 133 | } else { |
| 134 | bounds.fTop = fLineData.fPts[1].fY; |
| 135 | bounds.fBottom = fLineData.fPts[0].fY; |
| 136 | } |
| 137 | return bounds; |
| 138 | } |
bsalomon | 9fb4203 | 2016-05-13 09:23:38 -0700 | [diff] [blame] | 139 | case Type::kRRect: |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 140 | return fRRectData.fRRect.getBounds(); |
bsalomon | 9fb4203 | 2016-05-13 09:23:38 -0700 | [diff] [blame] | 141 | case Type::kPath: |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 142 | return this->path().getBounds(); |
bsalomon | 9fb4203 | 2016-05-13 09:23:38 -0700 | [diff] [blame] | 143 | } |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 144 | SK_ABORT("Unknown shape type"); |
bsalomon | 0ae36a2 | 2016-07-18 07:31:13 -0700 | [diff] [blame] | 145 | return kInverted; |
bsalomon | 9fb4203 | 2016-05-13 09:23:38 -0700 | [diff] [blame] | 146 | } |
| 147 | |
bsalomon | 0a0f67e | 2016-06-28 11:56:42 -0700 | [diff] [blame] | 148 | SkRect GrShape::styledBounds() const { |
Brian Salomon | 085c086 | 2017-08-31 15:44:51 -0400 | [diff] [blame] | 149 | if (this->isEmpty() && !fStyle.hasNonDashPathEffect()) { |
bsalomon | 0a0f67e | 2016-06-28 11:56:42 -0700 | [diff] [blame] | 150 | return SkRect::MakeEmpty(); |
bsalomon | 9fb4203 | 2016-05-13 09:23:38 -0700 | [diff] [blame] | 151 | } |
Brian Salomon | 085c086 | 2017-08-31 15:44:51 -0400 | [diff] [blame] | 152 | |
bsalomon | 0a0f67e | 2016-06-28 11:56:42 -0700 | [diff] [blame] | 153 | SkRect bounds; |
| 154 | fStyle.adjustBounds(&bounds, this->bounds()); |
| 155 | return bounds; |
bsalomon | 9fb4203 | 2016-05-13 09:23:38 -0700 | [diff] [blame] | 156 | } |
| 157 | |
bsalomon | 67fa4e3 | 2016-09-21 08:26:57 -0700 | [diff] [blame] | 158 | // If the path is small enough to be keyed from its data this returns key length, otherwise -1. |
| 159 | static int path_key_from_data_size(const SkPath& path) { |
| 160 | const int verbCnt = path.countVerbs(); |
| 161 | if (verbCnt > GrShape::kMaxKeyFromDataVerbCnt) { |
| 162 | return -1; |
| 163 | } |
| 164 | const int pointCnt = path.countPoints(); |
| 165 | const int conicWeightCnt = SkPathPriv::ConicWeightCnt(path); |
| 166 | |
| 167 | GR_STATIC_ASSERT(sizeof(SkPoint) == 2 * sizeof(uint32_t)); |
| 168 | GR_STATIC_ASSERT(sizeof(SkScalar) == sizeof(uint32_t)); |
| 169 | // 2 is for the verb cnt and a fill type. Each verb is a byte but we'll pad the verb data out to |
| 170 | // a uint32_t length. |
| 171 | return 2 + (SkAlign4(verbCnt) >> 2) + 2 * pointCnt + conicWeightCnt; |
| 172 | } |
| 173 | |
| 174 | // Writes the path data key into the passed pointer. |
bsalomon | 0e4a466 | 2016-09-21 11:23:46 -0700 | [diff] [blame] | 175 | static void write_path_key_from_data(const SkPath& path, uint32_t* origKey) { |
bsalomon | 67fa4e3 | 2016-09-21 08:26:57 -0700 | [diff] [blame] | 176 | uint32_t* key = origKey; |
| 177 | // The check below should take care of negative values casted positive. |
| 178 | const int verbCnt = path.countVerbs(); |
| 179 | const int pointCnt = path.countPoints(); |
| 180 | const int conicWeightCnt = SkPathPriv::ConicWeightCnt(path); |
| 181 | SkASSERT(verbCnt <= GrShape::kMaxKeyFromDataVerbCnt); |
| 182 | SkASSERT(pointCnt && verbCnt); |
| 183 | *key++ = path.getFillType(); |
| 184 | *key++ = verbCnt; |
| 185 | memcpy(key, SkPathPriv::VerbData(path), verbCnt * sizeof(uint8_t)); |
| 186 | int verbKeySize = SkAlign4(verbCnt); |
| 187 | // pad out to uint32_t alignment using value that will stand out when debugging. |
| 188 | uint8_t* pad = reinterpret_cast<uint8_t*>(key)+ verbCnt; |
| 189 | memset(pad, 0xDE, verbKeySize - verbCnt); |
| 190 | key += verbKeySize >> 2; |
| 191 | |
| 192 | memcpy(key, SkPathPriv::PointData(path), sizeof(SkPoint) * pointCnt); |
| 193 | GR_STATIC_ASSERT(sizeof(SkPoint) == 2 * sizeof(uint32_t)); |
| 194 | key += 2 * pointCnt; |
bsalomon | 0e4a466 | 2016-09-21 11:23:46 -0700 | [diff] [blame] | 195 | sk_careful_memcpy(key, SkPathPriv::ConicWeightData(path), sizeof(SkScalar) * conicWeightCnt); |
bsalomon | 67fa4e3 | 2016-09-21 08:26:57 -0700 | [diff] [blame] | 196 | GR_STATIC_ASSERT(sizeof(SkScalar) == sizeof(uint32_t)); |
| 197 | SkDEBUGCODE(key += conicWeightCnt); |
| 198 | SkASSERT(key - origKey == path_key_from_data_size(path)); |
| 199 | } |
| 200 | |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 201 | int GrShape::unstyledKeySize() const { |
| 202 | if (fInheritedKey.count()) { |
| 203 | return fInheritedKey.count(); |
| 204 | } |
| 205 | switch (fType) { |
| 206 | case Type::kEmpty: |
| 207 | return 1; |
Brian Salomon | 085c086 | 2017-08-31 15:44:51 -0400 | [diff] [blame] | 208 | case Type::kInvertedEmpty: |
| 209 | return 1; |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 210 | case Type::kRRect: |
| 211 | SkASSERT(!fInheritedKey.count()); |
| 212 | SkASSERT(0 == SkRRect::kSizeInMemory % sizeof(uint32_t)); |
bsalomon | 7049396 | 2016-06-10 08:05:14 -0700 | [diff] [blame] | 213 | // + 1 for the direction, start index, and inverseness. |
bsalomon | ee29564 | 2016-06-06 14:01:25 -0700 | [diff] [blame] | 214 | return SkRRect::kSizeInMemory / sizeof(uint32_t) + 1; |
bsalomon | 0a0f67e | 2016-06-28 11:56:42 -0700 | [diff] [blame] | 215 | case Type::kLine: |
| 216 | GR_STATIC_ASSERT(2 * sizeof(uint32_t) == sizeof(SkPoint)); |
| 217 | // 4 for the end points and 1 for the inverseness |
| 218 | return 5; |
bsalomon | 67fa4e3 | 2016-09-21 08:26:57 -0700 | [diff] [blame] | 219 | case Type::kPath: { |
bsalomon | aa84064 | 2016-09-23 12:09:16 -0700 | [diff] [blame] | 220 | if (0 == fPathData.fGenID) { |
| 221 | return -1; |
| 222 | } |
bsalomon | 67fa4e3 | 2016-09-21 08:26:57 -0700 | [diff] [blame] | 223 | int dataKeySize = path_key_from_data_size(fPathData.fPath); |
| 224 | if (dataKeySize >= 0) { |
| 225 | return dataKeySize; |
| 226 | } |
bsalomon | 67fa4e3 | 2016-09-21 08:26:57 -0700 | [diff] [blame] | 227 | // The key is the path ID and fill type. |
| 228 | return 2; |
| 229 | } |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 230 | } |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 231 | SK_ABORT("Should never get here."); |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 232 | return 0; |
| 233 | } |
| 234 | |
| 235 | void GrShape::writeUnstyledKey(uint32_t* key) const { |
| 236 | SkASSERT(this->unstyledKeySize()); |
| 237 | SkDEBUGCODE(uint32_t* origKey = key;) |
| 238 | if (fInheritedKey.count()) { |
| 239 | memcpy(key, fInheritedKey.get(), sizeof(uint32_t) * fInheritedKey.count()); |
| 240 | SkDEBUGCODE(key += fInheritedKey.count();) |
| 241 | } else { |
| 242 | switch (fType) { |
| 243 | case Type::kEmpty: |
| 244 | *key++ = 1; |
| 245 | break; |
Brian Salomon | 085c086 | 2017-08-31 15:44:51 -0400 | [diff] [blame] | 246 | case Type::kInvertedEmpty: |
| 247 | *key++ = 2; |
| 248 | break; |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 249 | case Type::kRRect: |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 250 | fRRectData.fRRect.writeToMemory(key); |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 251 | key += SkRRect::kSizeInMemory / sizeof(uint32_t); |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 252 | *key = (fRRectData.fDir == SkPath::kCCW_Direction) ? (1 << 31) : 0; |
| 253 | *key |= fRRectData.fInverted ? (1 << 30) : 0; |
| 254 | *key++ |= fRRectData.fStart; |
| 255 | SkASSERT(fRRectData.fStart < 8); |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 256 | break; |
bsalomon | 0a0f67e | 2016-06-28 11:56:42 -0700 | [diff] [blame] | 257 | case Type::kLine: |
| 258 | memcpy(key, fLineData.fPts, 2 * sizeof(SkPoint)); |
| 259 | key += 4; |
| 260 | *key++ = fLineData.fInverted ? 1 : 0; |
| 261 | break; |
bsalomon | 67fa4e3 | 2016-09-21 08:26:57 -0700 | [diff] [blame] | 262 | case Type::kPath: { |
bsalomon | aa84064 | 2016-09-23 12:09:16 -0700 | [diff] [blame] | 263 | SkASSERT(fPathData.fGenID); |
bsalomon | 67fa4e3 | 2016-09-21 08:26:57 -0700 | [diff] [blame] | 264 | int dataKeySize = path_key_from_data_size(fPathData.fPath); |
| 265 | if (dataKeySize >= 0) { |
bsalomon | 0e4a466 | 2016-09-21 11:23:46 -0700 | [diff] [blame] | 266 | write_path_key_from_data(fPathData.fPath, key); |
bsalomon | 67fa4e3 | 2016-09-21 08:26:57 -0700 | [diff] [blame] | 267 | return; |
| 268 | } |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 269 | *key++ = fPathData.fGenID; |
bsalomon | 7049396 | 2016-06-10 08:05:14 -0700 | [diff] [blame] | 270 | // We could canonicalize the fill rule for paths that don't differentiate between |
| 271 | // even/odd or winding fill (e.g. convex). |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 272 | *key++ = this->path().getFillType(); |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 273 | break; |
bsalomon | 67fa4e3 | 2016-09-21 08:26:57 -0700 | [diff] [blame] | 274 | } |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 275 | } |
| 276 | } |
| 277 | SkASSERT(key - origKey == this->unstyledKeySize()); |
| 278 | } |
| 279 | |
bsalomon | 97fd2d4 | 2016-05-09 13:02:01 -0700 | [diff] [blame] | 280 | void GrShape::setInheritedKey(const GrShape &parent, GrStyle::Apply apply, SkScalar scale) { |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 281 | SkASSERT(!fInheritedKey.count()); |
| 282 | // If the output shape turns out to be simple, then we will just use its geometric key |
| 283 | if (Type::kPath == fType) { |
| 284 | // We want ApplyFullStyle(ApplyPathEffect(shape)) to have the same key as |
| 285 | // ApplyFullStyle(shape). |
| 286 | // The full key is structured as (geo,path_effect,stroke). |
Brian Salomon | 4f40caf | 2017-09-01 09:00:45 -0400 | [diff] [blame] | 287 | // If we do ApplyPathEffect we get geo,path_effect as the inherited key. If we then |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 288 | // do ApplyFullStyle we'll memcpy geo,path_effect into the new inherited key |
| 289 | // and then append the style key (which should now be stroke only) at the end. |
| 290 | int parentCnt = parent.fInheritedKey.count(); |
| 291 | bool useParentGeoKey = !parentCnt; |
| 292 | if (useParentGeoKey) { |
| 293 | parentCnt = parent.unstyledKeySize(); |
bsalomon | 72dc51c | 2016-04-27 06:46:23 -0700 | [diff] [blame] | 294 | if (parentCnt < 0) { |
| 295 | // The parent's geometry has no key so we will have no key. |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 296 | fPathData.fGenID = 0; |
bsalomon | 72dc51c | 2016-04-27 06:46:23 -0700 | [diff] [blame] | 297 | return; |
| 298 | } |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 299 | } |
bsalomon | 0607756 | 2016-05-04 13:50:29 -0700 | [diff] [blame] | 300 | uint32_t styleKeyFlags = 0; |
| 301 | if (parent.knownToBeClosed()) { |
| 302 | styleKeyFlags |= GrStyle::kClosed_KeyFlag; |
| 303 | } |
bsalomon | 0ae36a2 | 2016-07-18 07:31:13 -0700 | [diff] [blame] | 304 | if (parent.asLine(nullptr, nullptr)) { |
| 305 | styleKeyFlags |= GrStyle::kNoJoins_KeyFlag; |
| 306 | } |
bsalomon | 0607756 | 2016-05-04 13:50:29 -0700 | [diff] [blame] | 307 | int styleCnt = GrStyle::KeySize(parent.fStyle, apply, styleKeyFlags); |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 308 | if (styleCnt < 0) { |
bsalomon | 93f66bc | 2016-06-21 08:35:49 -0700 | [diff] [blame] | 309 | // 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] | 310 | // we try to get a key for the shape. |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 311 | fPathData.fGenID = 0; |
bsalomon | 72dc51c | 2016-04-27 06:46:23 -0700 | [diff] [blame] | 312 | return; |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 313 | } |
bsalomon | 72dc51c | 2016-04-27 06:46:23 -0700 | [diff] [blame] | 314 | fInheritedKey.reset(parentCnt + styleCnt); |
| 315 | if (useParentGeoKey) { |
| 316 | // This will be the geo key. |
| 317 | parent.writeUnstyledKey(fInheritedKey.get()); |
| 318 | } else { |
| 319 | // This should be (geo,path_effect). |
| 320 | memcpy(fInheritedKey.get(), parent.fInheritedKey.get(), |
| 321 | parentCnt * sizeof(uint32_t)); |
| 322 | } |
| 323 | // Now turn (geo,path_effect) or (geo) into (geo,path_effect,stroke) |
bsalomon | 97fd2d4 | 2016-05-09 13:02:01 -0700 | [diff] [blame] | 324 | GrStyle::WriteKey(fInheritedKey.get() + parentCnt, parent.fStyle, apply, scale, |
| 325 | styleKeyFlags); |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 326 | } |
| 327 | } |
| 328 | |
Brian Osman | f6f7cf6 | 2017-09-25 16:49:55 -0400 | [diff] [blame] | 329 | void GrShape::addGenIDChangeListener(SkPathRef::GenIDChangeListener* listener) const { |
| 330 | SkPathPriv::AddGenIDChangeListener(fOriginalPath, listener); |
| 331 | } |
| 332 | |
| 333 | GrShape::GrShape(const GrShape& that) : fStyle(that.fStyle), fOriginalPath(that.fOriginalPath) { |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 334 | const SkPath* thatPath = Type::kPath == that.fType ? &that.fPathData.fPath : nullptr; |
| 335 | this->initType(that.fType, thatPath); |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 336 | switch (fType) { |
| 337 | case Type::kEmpty: |
bsalomon | 93f66bc | 2016-06-21 08:35:49 -0700 | [diff] [blame] | 338 | break; |
Brian Salomon | 085c086 | 2017-08-31 15:44:51 -0400 | [diff] [blame] | 339 | case Type::kInvertedEmpty: |
| 340 | break; |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 341 | case Type::kRRect: |
bsalomon | 0a0f67e | 2016-06-28 11:56:42 -0700 | [diff] [blame] | 342 | fRRectData = that.fRRectData; |
| 343 | break; |
| 344 | case Type::kLine: |
| 345 | fLineData = that.fLineData; |
bsalomon | 93f66bc | 2016-06-21 08:35:49 -0700 | [diff] [blame] | 346 | break; |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 347 | case Type::kPath: |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 348 | fPathData.fGenID = that.fPathData.fGenID; |
bsalomon | 93f66bc | 2016-06-21 08:35:49 -0700 | [diff] [blame] | 349 | break; |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 350 | } |
| 351 | fInheritedKey.reset(that.fInheritedKey.count()); |
bsalomon | 93f66bc | 2016-06-21 08:35:49 -0700 | [diff] [blame] | 352 | sk_careful_memcpy(fInheritedKey.get(), that.fInheritedKey.get(), |
| 353 | sizeof(uint32_t) * fInheritedKey.count()); |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 354 | } |
| 355 | |
bsalomon | 97fd2d4 | 2016-05-09 13:02:01 -0700 | [diff] [blame] | 356 | GrShape::GrShape(const GrShape& parent, GrStyle::Apply apply, SkScalar scale) { |
| 357 | // TODO: Add some quantization of scale for better cache performance here or leave that up |
| 358 | // to caller? |
| 359 | // TODO: For certain shapes and stroke params we could ignore the scale. (e.g. miter or bevel |
| 360 | // stroke of a rect). |
bsalomon | fb08327 | 2016-05-04 08:27:41 -0700 | [diff] [blame] | 361 | if (!parent.style().applies() || |
| 362 | (GrStyle::Apply::kPathEffectOnly == apply && !parent.style().pathEffect())) { |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 363 | this->initType(Type::kEmpty); |
bsalomon | fb08327 | 2016-05-04 08:27:41 -0700 | [diff] [blame] | 364 | *this = parent; |
| 365 | return; |
| 366 | } |
| 367 | |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 368 | SkPathEffect* pe = parent.fStyle.pathEffect(); |
bsalomon | fb08327 | 2016-05-04 08:27:41 -0700 | [diff] [blame] | 369 | SkTLazy<SkPath> tmpPath; |
| 370 | const GrShape* parentForKey = &parent; |
| 371 | SkTLazy<GrShape> tmpParent; |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 372 | this->initType(Type::kPath); |
| 373 | fPathData.fGenID = 0; |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 374 | if (pe) { |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 375 | const SkPath* srcForPathEffect; |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 376 | if (parent.fType == Type::kPath) { |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 377 | srcForPathEffect = &parent.path(); |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 378 | } else { |
bsalomon | fb08327 | 2016-05-04 08:27:41 -0700 | [diff] [blame] | 379 | srcForPathEffect = tmpPath.init(); |
| 380 | parent.asPath(tmpPath.get()); |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 381 | } |
| 382 | // Should we consider bounds? Would have to include in key, but it'd be nice to know |
| 383 | // if the bounds actually modified anything before including in key. |
bsalomon | fb08327 | 2016-05-04 08:27:41 -0700 | [diff] [blame] | 384 | SkStrokeRec strokeRec = parent.fStyle.strokeRec(); |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 385 | if (!parent.fStyle.applyPathEffectToPath(&this->path(), &strokeRec, *srcForPathEffect, |
bsalomon | 398e3f4 | 2016-06-13 10:22:48 -0700 | [diff] [blame] | 386 | scale)) { |
bsalomon | 0ae36a2 | 2016-07-18 07:31:13 -0700 | [diff] [blame] | 387 | tmpParent.init(*srcForPathEffect, GrStyle(strokeRec, nullptr)); |
| 388 | *this = tmpParent.get()->applyStyle(apply, scale); |
| 389 | return; |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 390 | } |
bsalomon | 97fd2d4 | 2016-05-09 13:02:01 -0700 | [diff] [blame] | 391 | // A path effect has access to change the res scale but we aren't expecting it to and it |
| 392 | // would mess up our key computation. |
| 393 | SkASSERT(scale == strokeRec.getResScale()); |
bsalomon | 1b28c1a | 2016-06-20 12:28:17 -0700 | [diff] [blame] | 394 | if (GrStyle::Apply::kPathEffectAndStrokeRec == apply && strokeRec.needToApply()) { |
| 395 | // The intermediate shape may not be a general path. If we we're just applying |
| 396 | // the path effect then attemptToReduceFromPath would catch it. This means that |
| 397 | // when we subsequently applied the remaining strokeRec we would have a non-path |
| 398 | // parent shape that would be used to determine the the stroked path's key. |
| 399 | // We detect that case here and change parentForKey to a temporary that represents |
| 400 | // the simpler shape so that applying both path effect and the strokerec all at |
| 401 | // once produces the same key. |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 402 | tmpParent.init(this->path(), GrStyle(strokeRec, nullptr)); |
bsalomon | 1b28c1a | 2016-06-20 12:28:17 -0700 | [diff] [blame] | 403 | tmpParent.get()->setInheritedKey(parent, GrStyle::Apply::kPathEffectOnly, scale); |
| 404 | if (!tmpPath.isValid()) { |
| 405 | tmpPath.init(); |
bsalomon | 72dc51c | 2016-04-27 06:46:23 -0700 | [diff] [blame] | 406 | } |
bsalomon | 1b28c1a | 2016-06-20 12:28:17 -0700 | [diff] [blame] | 407 | tmpParent.get()->asPath(tmpPath.get()); |
| 408 | SkStrokeRec::InitStyle fillOrHairline; |
bsalomon | 0ae36a2 | 2016-07-18 07:31:13 -0700 | [diff] [blame] | 409 | // The parent shape may have simplified away the strokeRec, check for that here. |
| 410 | if (tmpParent.get()->style().applies()) { |
| 411 | SkAssertResult(tmpParent.get()->style().applyToPath(&this->path(), &fillOrHairline, |
| 412 | *tmpPath.get(), scale)); |
| 413 | } else if (tmpParent.get()->style().isSimpleFill()) { |
| 414 | fillOrHairline = SkStrokeRec::kFill_InitStyle; |
| 415 | } else { |
| 416 | SkASSERT(tmpParent.get()->style().isSimpleHairline()); |
| 417 | fillOrHairline = SkStrokeRec::kHairline_InitStyle; |
| 418 | } |
bsalomon | 1b28c1a | 2016-06-20 12:28:17 -0700 | [diff] [blame] | 419 | fStyle.resetToInitStyle(fillOrHairline); |
| 420 | parentForKey = tmpParent.get(); |
bsalomon | fb08327 | 2016-05-04 08:27:41 -0700 | [diff] [blame] | 421 | } else { |
| 422 | fStyle = GrStyle(strokeRec, nullptr); |
bsalomon | 72dc51c | 2016-04-27 06:46:23 -0700 | [diff] [blame] | 423 | } |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 424 | } else { |
bsalomon | 97fd2d4 | 2016-05-09 13:02:01 -0700 | [diff] [blame] | 425 | const SkPath* srcForParentStyle; |
bsalomon | fb08327 | 2016-05-04 08:27:41 -0700 | [diff] [blame] | 426 | if (parent.fType == Type::kPath) { |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 427 | srcForParentStyle = &parent.path(); |
bsalomon | fb08327 | 2016-05-04 08:27:41 -0700 | [diff] [blame] | 428 | } else { |
bsalomon | 97fd2d4 | 2016-05-09 13:02:01 -0700 | [diff] [blame] | 429 | srcForParentStyle = tmpPath.init(); |
bsalomon | fb08327 | 2016-05-04 08:27:41 -0700 | [diff] [blame] | 430 | parent.asPath(tmpPath.get()); |
| 431 | } |
bsalomon | 97fd2d4 | 2016-05-09 13:02:01 -0700 | [diff] [blame] | 432 | SkStrokeRec::InitStyle fillOrHairline; |
| 433 | SkASSERT(parent.fStyle.applies()); |
| 434 | SkASSERT(!parent.fStyle.pathEffect()); |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 435 | SkAssertResult(parent.fStyle.applyToPath(&this->path(), &fillOrHairline, *srcForParentStyle, |
bsalomon | 97fd2d4 | 2016-05-09 13:02:01 -0700 | [diff] [blame] | 436 | scale)); |
| 437 | fStyle.resetToInitStyle(fillOrHairline); |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 438 | } |
Brian Osman | f6f7cf6 | 2017-09-25 16:49:55 -0400 | [diff] [blame] | 439 | fOriginalPath = parent.fOriginalPath; |
bsalomon | 1b28c1a | 2016-06-20 12:28:17 -0700 | [diff] [blame] | 440 | this->attemptToSimplifyPath(); |
bsalomon | 97fd2d4 | 2016-05-09 13:02:01 -0700 | [diff] [blame] | 441 | this->setInheritedKey(*parentForKey, apply, scale); |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 442 | } |
bsalomon | ee29564 | 2016-06-06 14:01:25 -0700 | [diff] [blame] | 443 | |
bsalomon | 1b28c1a | 2016-06-20 12:28:17 -0700 | [diff] [blame] | 444 | void GrShape::attemptToSimplifyPath() { |
bsalomon | ee29564 | 2016-06-06 14:01:25 -0700 | [diff] [blame] | 445 | SkRect rect; |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 446 | SkRRect rrect; |
| 447 | SkPath::Direction rrectDir; |
| 448 | unsigned rrectStart; |
| 449 | bool inverted = this->path().isInverseFillType(); |
bsalomon | 0a0f67e | 2016-06-28 11:56:42 -0700 | [diff] [blame] | 450 | SkPoint pts[2]; |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 451 | if (this->path().isEmpty()) { |
Brian Salomon | 085c086 | 2017-08-31 15:44:51 -0400 | [diff] [blame] | 452 | // Dashing ignores inverseness skbug.com/5421. |
| 453 | this->changeType(inverted && !this->style().isDashed() ? Type::kInvertedEmpty |
| 454 | : Type::kEmpty); |
bsalomon | 0a0f67e | 2016-06-28 11:56:42 -0700 | [diff] [blame] | 455 | } else if (this->path().isLine(pts)) { |
| 456 | this->changeType(Type::kLine); |
| 457 | fLineData.fPts[0] = pts[0]; |
| 458 | fLineData.fPts[1] = pts[1]; |
| 459 | fLineData.fInverted = inverted; |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 460 | } else if (this->path().isRRect(&rrect, &rrectDir, &rrectStart)) { |
| 461 | this->changeType(Type::kRRect); |
| 462 | fRRectData.fRRect = rrect; |
| 463 | fRRectData.fDir = rrectDir; |
| 464 | fRRectData.fStart = rrectStart; |
| 465 | fRRectData.fInverted = inverted; |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 466 | SkASSERT(!fRRectData.fRRect.isEmpty()); |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 467 | } else if (this->path().isOval(&rect, &rrectDir, &rrectStart)) { |
| 468 | this->changeType(Type::kRRect); |
| 469 | fRRectData.fRRect.setOval(rect); |
| 470 | fRRectData.fDir = rrectDir; |
| 471 | fRRectData.fInverted = inverted; |
bsalomon | 1b28c1a | 2016-06-20 12:28:17 -0700 | [diff] [blame] | 472 | // convert from oval indexing to rrect indexiing. |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 473 | fRRectData.fStart = 2 * rrectStart; |
| 474 | } else if (SkPathPriv::IsSimpleClosedRect(this->path(), &rect, &rrectDir, &rrectStart)) { |
| 475 | this->changeType(Type::kRRect); |
bsalomon | 1b28c1a | 2016-06-20 12:28:17 -0700 | [diff] [blame] | 476 | // When there is a path effect we restrict rect detection to the narrower API that |
| 477 | // gives us the starting position. Otherwise, we will retry with the more aggressive |
| 478 | // isRect(). |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 479 | fRRectData.fRRect.setRect(rect); |
| 480 | fRRectData.fInverted = inverted; |
| 481 | fRRectData.fDir = rrectDir; |
bsalomon | 1b28c1a | 2016-06-20 12:28:17 -0700 | [diff] [blame] | 482 | // convert from rect indexing to rrect indexiing. |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 483 | fRRectData.fStart = 2 * rrectStart; |
bsalomon | 1b28c1a | 2016-06-20 12:28:17 -0700 | [diff] [blame] | 484 | } else if (!this->style().hasPathEffect()) { |
bsalomon | ee29564 | 2016-06-06 14:01:25 -0700 | [diff] [blame] | 485 | bool closed; |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 486 | if (this->path().isRect(&rect, &closed, nullptr)) { |
bsalomon | 1b28c1a | 2016-06-20 12:28:17 -0700 | [diff] [blame] | 487 | if (closed || this->style().isSimpleFill()) { |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 488 | this->changeType(Type::kRRect); |
| 489 | fRRectData.fRRect.setRect(rect); |
bsalomon | ee29564 | 2016-06-06 14:01:25 -0700 | [diff] [blame] | 490 | // Since there is no path effect the dir and start index is immaterial. |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 491 | fRRectData.fDir = kDefaultRRectDir; |
| 492 | fRRectData.fStart = kDefaultRRectStart; |
bsalomon | 1b28c1a | 2016-06-20 12:28:17 -0700 | [diff] [blame] | 493 | // There isn't dashing so we will have to preserver inverseness. |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 494 | fRRectData.fInverted = inverted; |
bsalomon | ee29564 | 2016-06-06 14:01:25 -0700 | [diff] [blame] | 495 | } |
| 496 | } |
| 497 | } |
bsalomon | 1b28c1a | 2016-06-20 12:28:17 -0700 | [diff] [blame] | 498 | if (Type::kPath != fType) { |
bsalomon | 1b28c1a | 2016-06-20 12:28:17 -0700 | [diff] [blame] | 499 | fInheritedKey.reset(0); |
Brian Osman | b379dcd | 2017-10-04 15:44:05 -0400 | [diff] [blame] | 500 | // Whenever we simplify to a non-path, break the chain so we no longer refer to the |
| 501 | // original path. This prevents attaching genID listeners to temporary paths created when |
| 502 | // drawing simple shapes. |
| 503 | fOriginalPath.reset(); |
bsalomon | 1b28c1a | 2016-06-20 12:28:17 -0700 | [diff] [blame] | 504 | if (Type::kRRect == fType) { |
| 505 | this->attemptToSimplifyRRect(); |
bsalomon | 0a0f67e | 2016-06-28 11:56:42 -0700 | [diff] [blame] | 506 | } else if (Type::kLine == fType) { |
| 507 | this->attemptToSimplifyLine(); |
bsalomon | 1b28c1a | 2016-06-20 12:28:17 -0700 | [diff] [blame] | 508 | } |
bsalomon | 93f66bc | 2016-06-21 08:35:49 -0700 | [diff] [blame] | 509 | } else { |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 510 | if (fInheritedKey.count() || this->path().isVolatile()) { |
| 511 | fPathData.fGenID = 0; |
bsalomon | 93f66bc | 2016-06-21 08:35:49 -0700 | [diff] [blame] | 512 | } else { |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 513 | fPathData.fGenID = this->path().getGenerationID(); |
bsalomon | 93f66bc | 2016-06-21 08:35:49 -0700 | [diff] [blame] | 514 | } |
bsalomon | a4817af | 2016-06-23 11:48:26 -0700 | [diff] [blame] | 515 | if (!this->style().hasNonDashPathEffect()) { |
| 516 | if (this->style().strokeRec().getStyle() == SkStrokeRec::kStroke_Style || |
| 517 | this->style().strokeRec().getStyle() == SkStrokeRec::kHairline_Style) { |
| 518 | // Stroke styles don't differentiate between winding and even/odd. |
| 519 | // Moreover, dashing ignores inverseness (skbug.com/5421) |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 520 | bool inverse = !this->style().isDashed() && this->path().isInverseFillType(); |
bsalomon | a4817af | 2016-06-23 11:48:26 -0700 | [diff] [blame] | 521 | if (inverse) { |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 522 | this->path().setFillType(kDefaultPathInverseFillType); |
bsalomon | a4817af | 2016-06-23 11:48:26 -0700 | [diff] [blame] | 523 | } else { |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 524 | this->path().setFillType(kDefaultPathFillType); |
bsalomon | a4817af | 2016-06-23 11:48:26 -0700 | [diff] [blame] | 525 | } |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 526 | } else if (this->path().isConvex()) { |
bsalomon | a4817af | 2016-06-23 11:48:26 -0700 | [diff] [blame] | 527 | // There is no distinction between even/odd and non-zero winding count for convex |
| 528 | // paths. |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 529 | if (this->path().isInverseFillType()) { |
| 530 | this->path().setFillType(kDefaultPathInverseFillType); |
bsalomon | a4817af | 2016-06-23 11:48:26 -0700 | [diff] [blame] | 531 | } else { |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 532 | this->path().setFillType(kDefaultPathFillType); |
bsalomon | a4817af | 2016-06-23 11:48:26 -0700 | [diff] [blame] | 533 | } |
bsalomon | 93f66bc | 2016-06-21 08:35:49 -0700 | [diff] [blame] | 534 | } |
| 535 | } |
bsalomon | 1b28c1a | 2016-06-20 12:28:17 -0700 | [diff] [blame] | 536 | } |
| 537 | } |
| 538 | |
| 539 | void GrShape::attemptToSimplifyRRect() { |
| 540 | SkASSERT(Type::kRRect == fType); |
| 541 | SkASSERT(!fInheritedKey.count()); |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 542 | if (fRRectData.fRRect.isEmpty()) { |
Brian Salomon | 085c086 | 2017-08-31 15:44:51 -0400 | [diff] [blame] | 543 | // Dashing ignores the inverseness currently. skbug.com/5421 |
| 544 | fType = fRRectData.fInverted && !fStyle.isDashed() ? Type::kInvertedEmpty : Type::kEmpty; |
bsalomon | 1b28c1a | 2016-06-20 12:28:17 -0700 | [diff] [blame] | 545 | return; |
| 546 | } |
| 547 | if (!this->style().hasPathEffect()) { |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 548 | fRRectData.fDir = kDefaultRRectDir; |
| 549 | fRRectData.fStart = kDefaultRRectStart; |
bsalomon | 1b28c1a | 2016-06-20 12:28:17 -0700 | [diff] [blame] | 550 | } else if (fStyle.isDashed()) { |
| 551 | // Dashing ignores the inverseness (currently). skbug.com/5421 |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 552 | fRRectData.fInverted = false; |
bsalomon | 1b28c1a | 2016-06-20 12:28:17 -0700 | [diff] [blame] | 553 | } |
bsalomon | 487f8d3 | 2016-07-20 07:15:44 -0700 | [diff] [blame] | 554 | // Turn a stroke-and-filled miter rect into a filled rect. TODO: more rrect stroke shortcuts. |
| 555 | if (!fStyle.hasPathEffect() && |
| 556 | fStyle.strokeRec().getStyle() == SkStrokeRec::kStrokeAndFill_Style && |
| 557 | fStyle.strokeRec().getJoin() == SkPaint::kMiter_Join && |
| 558 | fStyle.strokeRec().getMiter() >= SK_ScalarSqrt2 && |
| 559 | fRRectData.fRRect.isRect()) { |
| 560 | SkScalar r = fStyle.strokeRec().getWidth() / 2; |
| 561 | fRRectData.fRRect = SkRRect::MakeRect(fRRectData.fRRect.rect().makeOutset(r, r)); |
| 562 | fStyle = GrStyle::SimpleFill(); |
| 563 | } |
bsalomon | ee29564 | 2016-06-06 14:01:25 -0700 | [diff] [blame] | 564 | } |
bsalomon | 0a0f67e | 2016-06-28 11:56:42 -0700 | [diff] [blame] | 565 | |
| 566 | void GrShape::attemptToSimplifyLine() { |
bsalomon | 0ae36a2 | 2016-07-18 07:31:13 -0700 | [diff] [blame] | 567 | SkASSERT(Type::kLine == fType); |
| 568 | SkASSERT(!fInheritedKey.count()); |
| 569 | if (fStyle.isDashed()) { |
| 570 | // Dashing ignores inverseness. |
| 571 | fLineData.fInverted = false; |
| 572 | return; |
| 573 | } else if (fStyle.hasPathEffect()) { |
| 574 | return; |
| 575 | } |
| 576 | if (fStyle.strokeRec().getStyle() == SkStrokeRec::kStrokeAndFill_Style) { |
| 577 | // Make stroke + fill be stroke since the fill is empty. |
| 578 | SkStrokeRec rec = fStyle.strokeRec(); |
| 579 | rec.setStrokeStyle(fStyle.strokeRec().getWidth(), false); |
| 580 | fStyle = GrStyle(rec, nullptr); |
| 581 | } |
Brian Salomon | 085c086 | 2017-08-31 15:44:51 -0400 | [diff] [blame] | 582 | if (fStyle.isSimpleFill()) { |
| 583 | this->changeType(fLineData.fInverted ? Type::kInvertedEmpty : Type::kEmpty); |
bsalomon | 0ae36a2 | 2016-07-18 07:31:13 -0700 | [diff] [blame] | 584 | return; |
| 585 | } |
| 586 | SkPoint* pts = fLineData.fPts; |
| 587 | if (fStyle.strokeRec().getStyle() == SkStrokeRec::kStroke_Style) { |
| 588 | // If it is horizontal or vertical we will turn it into a filled rrect. |
| 589 | SkRect rect; |
| 590 | rect.fLeft = SkTMin(pts[0].fX, pts[1].fX); |
| 591 | rect.fRight = SkTMax(pts[0].fX, pts[1].fX); |
| 592 | rect.fTop = SkTMin(pts[0].fY, pts[1].fY); |
| 593 | rect.fBottom = SkTMax(pts[0].fY, pts[1].fY); |
| 594 | bool eqX = rect.fLeft == rect.fRight; |
| 595 | bool eqY = rect.fTop == rect.fBottom; |
| 596 | if (eqX || eqY) { |
| 597 | SkScalar r = fStyle.strokeRec().getWidth() / 2; |
| 598 | bool inverted = fLineData.fInverted; |
| 599 | this->changeType(Type::kRRect); |
| 600 | switch (fStyle.strokeRec().getCap()) { |
| 601 | case SkPaint::kButt_Cap: |
| 602 | if (eqX && eqY) { |
| 603 | this->changeType(Type::kEmpty); |
| 604 | return; |
| 605 | } |
| 606 | if (eqX) { |
| 607 | rect.outset(r, 0); |
| 608 | } else { |
| 609 | rect.outset(0, r); |
| 610 | } |
| 611 | fRRectData.fRRect = SkRRect::MakeRect(rect); |
| 612 | break; |
| 613 | case SkPaint::kSquare_Cap: |
| 614 | rect.outset(r, r); |
| 615 | fRRectData.fRRect = SkRRect::MakeRect(rect); |
| 616 | break; |
| 617 | case SkPaint::kRound_Cap: |
| 618 | rect.outset(r, r); |
| 619 | fRRectData.fRRect = SkRRect::MakeRectXY(rect, r, r); |
| 620 | break; |
bsalomon | 0a0f67e | 2016-06-28 11:56:42 -0700 | [diff] [blame] | 621 | } |
bsalomon | 0ae36a2 | 2016-07-18 07:31:13 -0700 | [diff] [blame] | 622 | fRRectData.fInverted = inverted; |
| 623 | fRRectData.fDir = kDefaultRRectDir; |
| 624 | fRRectData.fStart = kDefaultRRectStart; |
| 625 | if (fRRectData.fRRect.isEmpty()) { |
| 626 | // This can happen when r is very small relative to the rect edges. |
Brian Salomon | 085c086 | 2017-08-31 15:44:51 -0400 | [diff] [blame] | 627 | this->changeType(inverted ? Type::kInvertedEmpty : Type::kEmpty); |
bsalomon | 0ae36a2 | 2016-07-18 07:31:13 -0700 | [diff] [blame] | 628 | return; |
| 629 | } |
| 630 | fStyle = GrStyle::SimpleFill(); |
| 631 | return; |
bsalomon | 0a0f67e | 2016-06-28 11:56:42 -0700 | [diff] [blame] | 632 | } |
| 633 | } |
bsalomon | 0ae36a2 | 2016-07-18 07:31:13 -0700 | [diff] [blame] | 634 | // Only path effects could care about the order of the points. Otherwise canonicalize |
| 635 | // the point order. |
| 636 | if (pts[1].fY < pts[0].fY || (pts[1].fY == pts[0].fY && pts[1].fX < pts[0].fX)) { |
| 637 | SkTSwap(pts[0], pts[1]); |
| 638 | } |
bsalomon | 0a0f67e | 2016-06-28 11:56:42 -0700 | [diff] [blame] | 639 | } |