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); |
Brian Osman | f6f7cf6 | 2017-09-25 16:49:55 -0400 | [diff] [blame^] | 389 | fOriginalPath = parent.fOriginalPath; |
bsalomon | 0ae36a2 | 2016-07-18 07:31:13 -0700 | [diff] [blame] | 390 | return; |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 391 | } |
bsalomon | 97fd2d4 | 2016-05-09 13:02:01 -0700 | [diff] [blame] | 392 | // A path effect has access to change the res scale but we aren't expecting it to and it |
| 393 | // would mess up our key computation. |
| 394 | SkASSERT(scale == strokeRec.getResScale()); |
bsalomon | 1b28c1a | 2016-06-20 12:28:17 -0700 | [diff] [blame] | 395 | if (GrStyle::Apply::kPathEffectAndStrokeRec == apply && strokeRec.needToApply()) { |
| 396 | // The intermediate shape may not be a general path. If we we're just applying |
| 397 | // the path effect then attemptToReduceFromPath would catch it. This means that |
| 398 | // when we subsequently applied the remaining strokeRec we would have a non-path |
| 399 | // parent shape that would be used to determine the the stroked path's key. |
| 400 | // We detect that case here and change parentForKey to a temporary that represents |
| 401 | // the simpler shape so that applying both path effect and the strokerec all at |
| 402 | // once produces the same key. |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 403 | tmpParent.init(this->path(), GrStyle(strokeRec, nullptr)); |
bsalomon | 1b28c1a | 2016-06-20 12:28:17 -0700 | [diff] [blame] | 404 | tmpParent.get()->setInheritedKey(parent, GrStyle::Apply::kPathEffectOnly, scale); |
| 405 | if (!tmpPath.isValid()) { |
| 406 | tmpPath.init(); |
bsalomon | 72dc51c | 2016-04-27 06:46:23 -0700 | [diff] [blame] | 407 | } |
bsalomon | 1b28c1a | 2016-06-20 12:28:17 -0700 | [diff] [blame] | 408 | tmpParent.get()->asPath(tmpPath.get()); |
| 409 | SkStrokeRec::InitStyle fillOrHairline; |
bsalomon | 0ae36a2 | 2016-07-18 07:31:13 -0700 | [diff] [blame] | 410 | // The parent shape may have simplified away the strokeRec, check for that here. |
| 411 | if (tmpParent.get()->style().applies()) { |
| 412 | SkAssertResult(tmpParent.get()->style().applyToPath(&this->path(), &fillOrHairline, |
| 413 | *tmpPath.get(), scale)); |
| 414 | } else if (tmpParent.get()->style().isSimpleFill()) { |
| 415 | fillOrHairline = SkStrokeRec::kFill_InitStyle; |
| 416 | } else { |
| 417 | SkASSERT(tmpParent.get()->style().isSimpleHairline()); |
| 418 | fillOrHairline = SkStrokeRec::kHairline_InitStyle; |
| 419 | } |
bsalomon | 1b28c1a | 2016-06-20 12:28:17 -0700 | [diff] [blame] | 420 | fStyle.resetToInitStyle(fillOrHairline); |
| 421 | parentForKey = tmpParent.get(); |
bsalomon | fb08327 | 2016-05-04 08:27:41 -0700 | [diff] [blame] | 422 | } else { |
| 423 | fStyle = GrStyle(strokeRec, nullptr); |
bsalomon | 72dc51c | 2016-04-27 06:46:23 -0700 | [diff] [blame] | 424 | } |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 425 | } else { |
bsalomon | 97fd2d4 | 2016-05-09 13:02:01 -0700 | [diff] [blame] | 426 | const SkPath* srcForParentStyle; |
bsalomon | fb08327 | 2016-05-04 08:27:41 -0700 | [diff] [blame] | 427 | if (parent.fType == Type::kPath) { |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 428 | srcForParentStyle = &parent.path(); |
bsalomon | fb08327 | 2016-05-04 08:27:41 -0700 | [diff] [blame] | 429 | } else { |
bsalomon | 97fd2d4 | 2016-05-09 13:02:01 -0700 | [diff] [blame] | 430 | srcForParentStyle = tmpPath.init(); |
bsalomon | fb08327 | 2016-05-04 08:27:41 -0700 | [diff] [blame] | 431 | parent.asPath(tmpPath.get()); |
| 432 | } |
bsalomon | 97fd2d4 | 2016-05-09 13:02:01 -0700 | [diff] [blame] | 433 | SkStrokeRec::InitStyle fillOrHairline; |
| 434 | SkASSERT(parent.fStyle.applies()); |
| 435 | SkASSERT(!parent.fStyle.pathEffect()); |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 436 | SkAssertResult(parent.fStyle.applyToPath(&this->path(), &fillOrHairline, *srcForParentStyle, |
bsalomon | 97fd2d4 | 2016-05-09 13:02:01 -0700 | [diff] [blame] | 437 | scale)); |
| 438 | fStyle.resetToInitStyle(fillOrHairline); |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 439 | } |
Brian Osman | f6f7cf6 | 2017-09-25 16:49:55 -0400 | [diff] [blame^] | 440 | fOriginalPath = parent.fOriginalPath; |
bsalomon | 1b28c1a | 2016-06-20 12:28:17 -0700 | [diff] [blame] | 441 | this->attemptToSimplifyPath(); |
bsalomon | 97fd2d4 | 2016-05-09 13:02:01 -0700 | [diff] [blame] | 442 | this->setInheritedKey(*parentForKey, apply, scale); |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 443 | } |
bsalomon | ee29564 | 2016-06-06 14:01:25 -0700 | [diff] [blame] | 444 | |
bsalomon | 1b28c1a | 2016-06-20 12:28:17 -0700 | [diff] [blame] | 445 | void GrShape::attemptToSimplifyPath() { |
bsalomon | ee29564 | 2016-06-06 14:01:25 -0700 | [diff] [blame] | 446 | SkRect rect; |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 447 | SkRRect rrect; |
| 448 | SkPath::Direction rrectDir; |
| 449 | unsigned rrectStart; |
| 450 | bool inverted = this->path().isInverseFillType(); |
bsalomon | 0a0f67e | 2016-06-28 11:56:42 -0700 | [diff] [blame] | 451 | SkPoint pts[2]; |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 452 | if (this->path().isEmpty()) { |
Brian Salomon | 085c086 | 2017-08-31 15:44:51 -0400 | [diff] [blame] | 453 | // Dashing ignores inverseness skbug.com/5421. |
| 454 | this->changeType(inverted && !this->style().isDashed() ? Type::kInvertedEmpty |
| 455 | : Type::kEmpty); |
bsalomon | 0a0f67e | 2016-06-28 11:56:42 -0700 | [diff] [blame] | 456 | } else if (this->path().isLine(pts)) { |
| 457 | this->changeType(Type::kLine); |
| 458 | fLineData.fPts[0] = pts[0]; |
| 459 | fLineData.fPts[1] = pts[1]; |
| 460 | fLineData.fInverted = inverted; |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 461 | } else if (this->path().isRRect(&rrect, &rrectDir, &rrectStart)) { |
| 462 | this->changeType(Type::kRRect); |
| 463 | fRRectData.fRRect = rrect; |
| 464 | fRRectData.fDir = rrectDir; |
| 465 | fRRectData.fStart = rrectStart; |
| 466 | fRRectData.fInverted = inverted; |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 467 | SkASSERT(!fRRectData.fRRect.isEmpty()); |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 468 | } else if (this->path().isOval(&rect, &rrectDir, &rrectStart)) { |
| 469 | this->changeType(Type::kRRect); |
| 470 | fRRectData.fRRect.setOval(rect); |
| 471 | fRRectData.fDir = rrectDir; |
| 472 | fRRectData.fInverted = inverted; |
bsalomon | 1b28c1a | 2016-06-20 12:28:17 -0700 | [diff] [blame] | 473 | // convert from oval indexing to rrect indexiing. |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 474 | fRRectData.fStart = 2 * rrectStart; |
| 475 | } else if (SkPathPriv::IsSimpleClosedRect(this->path(), &rect, &rrectDir, &rrectStart)) { |
| 476 | this->changeType(Type::kRRect); |
bsalomon | 1b28c1a | 2016-06-20 12:28:17 -0700 | [diff] [blame] | 477 | // When there is a path effect we restrict rect detection to the narrower API that |
| 478 | // gives us the starting position. Otherwise, we will retry with the more aggressive |
| 479 | // isRect(). |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 480 | fRRectData.fRRect.setRect(rect); |
| 481 | fRRectData.fInverted = inverted; |
| 482 | fRRectData.fDir = rrectDir; |
bsalomon | 1b28c1a | 2016-06-20 12:28:17 -0700 | [diff] [blame] | 483 | // convert from rect indexing to rrect indexiing. |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 484 | fRRectData.fStart = 2 * rrectStart; |
bsalomon | 1b28c1a | 2016-06-20 12:28:17 -0700 | [diff] [blame] | 485 | } else if (!this->style().hasPathEffect()) { |
bsalomon | ee29564 | 2016-06-06 14:01:25 -0700 | [diff] [blame] | 486 | bool closed; |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 487 | if (this->path().isRect(&rect, &closed, nullptr)) { |
bsalomon | 1b28c1a | 2016-06-20 12:28:17 -0700 | [diff] [blame] | 488 | if (closed || this->style().isSimpleFill()) { |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 489 | this->changeType(Type::kRRect); |
| 490 | fRRectData.fRRect.setRect(rect); |
bsalomon | ee29564 | 2016-06-06 14:01:25 -0700 | [diff] [blame] | 491 | // Since there is no path effect the dir and start index is immaterial. |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 492 | fRRectData.fDir = kDefaultRRectDir; |
| 493 | fRRectData.fStart = kDefaultRRectStart; |
bsalomon | 1b28c1a | 2016-06-20 12:28:17 -0700 | [diff] [blame] | 494 | // There isn't dashing so we will have to preserver inverseness. |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 495 | fRRectData.fInverted = inverted; |
bsalomon | ee29564 | 2016-06-06 14:01:25 -0700 | [diff] [blame] | 496 | } |
| 497 | } |
| 498 | } |
bsalomon | 1b28c1a | 2016-06-20 12:28:17 -0700 | [diff] [blame] | 499 | if (Type::kPath != fType) { |
bsalomon | 1b28c1a | 2016-06-20 12:28:17 -0700 | [diff] [blame] | 500 | fInheritedKey.reset(0); |
| 501 | if (Type::kRRect == fType) { |
| 502 | this->attemptToSimplifyRRect(); |
bsalomon | 0a0f67e | 2016-06-28 11:56:42 -0700 | [diff] [blame] | 503 | } else if (Type::kLine == fType) { |
| 504 | this->attemptToSimplifyLine(); |
bsalomon | 1b28c1a | 2016-06-20 12:28:17 -0700 | [diff] [blame] | 505 | } |
bsalomon | 93f66bc | 2016-06-21 08:35:49 -0700 | [diff] [blame] | 506 | } else { |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 507 | if (fInheritedKey.count() || this->path().isVolatile()) { |
| 508 | fPathData.fGenID = 0; |
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 | fPathData.fGenID = this->path().getGenerationID(); |
bsalomon | 93f66bc | 2016-06-21 08:35:49 -0700 | [diff] [blame] | 511 | } |
bsalomon | a4817af | 2016-06-23 11:48:26 -0700 | [diff] [blame] | 512 | if (!this->style().hasNonDashPathEffect()) { |
| 513 | if (this->style().strokeRec().getStyle() == SkStrokeRec::kStroke_Style || |
| 514 | this->style().strokeRec().getStyle() == SkStrokeRec::kHairline_Style) { |
| 515 | // Stroke styles don't differentiate between winding and even/odd. |
| 516 | // Moreover, dashing ignores inverseness (skbug.com/5421) |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 517 | bool inverse = !this->style().isDashed() && this->path().isInverseFillType(); |
bsalomon | a4817af | 2016-06-23 11:48:26 -0700 | [diff] [blame] | 518 | if (inverse) { |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 519 | this->path().setFillType(kDefaultPathInverseFillType); |
bsalomon | a4817af | 2016-06-23 11:48:26 -0700 | [diff] [blame] | 520 | } else { |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 521 | this->path().setFillType(kDefaultPathFillType); |
bsalomon | a4817af | 2016-06-23 11:48:26 -0700 | [diff] [blame] | 522 | } |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 523 | } else if (this->path().isConvex()) { |
bsalomon | a4817af | 2016-06-23 11:48:26 -0700 | [diff] [blame] | 524 | // There is no distinction between even/odd and non-zero winding count for convex |
| 525 | // paths. |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 526 | if (this->path().isInverseFillType()) { |
| 527 | this->path().setFillType(kDefaultPathInverseFillType); |
bsalomon | a4817af | 2016-06-23 11:48:26 -0700 | [diff] [blame] | 528 | } else { |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 529 | this->path().setFillType(kDefaultPathFillType); |
bsalomon | a4817af | 2016-06-23 11:48:26 -0700 | [diff] [blame] | 530 | } |
bsalomon | 93f66bc | 2016-06-21 08:35:49 -0700 | [diff] [blame] | 531 | } |
| 532 | } |
bsalomon | 1b28c1a | 2016-06-20 12:28:17 -0700 | [diff] [blame] | 533 | } |
| 534 | } |
| 535 | |
| 536 | void GrShape::attemptToSimplifyRRect() { |
| 537 | SkASSERT(Type::kRRect == fType); |
| 538 | SkASSERT(!fInheritedKey.count()); |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 539 | if (fRRectData.fRRect.isEmpty()) { |
Brian Salomon | 085c086 | 2017-08-31 15:44:51 -0400 | [diff] [blame] | 540 | // Dashing ignores the inverseness currently. skbug.com/5421 |
| 541 | fType = fRRectData.fInverted && !fStyle.isDashed() ? Type::kInvertedEmpty : Type::kEmpty; |
bsalomon | 1b28c1a | 2016-06-20 12:28:17 -0700 | [diff] [blame] | 542 | return; |
| 543 | } |
| 544 | if (!this->style().hasPathEffect()) { |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 545 | fRRectData.fDir = kDefaultRRectDir; |
| 546 | fRRectData.fStart = kDefaultRRectStart; |
bsalomon | 1b28c1a | 2016-06-20 12:28:17 -0700 | [diff] [blame] | 547 | } else if (fStyle.isDashed()) { |
| 548 | // Dashing ignores the inverseness (currently). skbug.com/5421 |
bsalomon | 728b0f7 | 2016-06-27 10:00:19 -0700 | [diff] [blame] | 549 | fRRectData.fInverted = false; |
bsalomon | 1b28c1a | 2016-06-20 12:28:17 -0700 | [diff] [blame] | 550 | } |
bsalomon | 487f8d3 | 2016-07-20 07:15:44 -0700 | [diff] [blame] | 551 | // Turn a stroke-and-filled miter rect into a filled rect. TODO: more rrect stroke shortcuts. |
| 552 | if (!fStyle.hasPathEffect() && |
| 553 | fStyle.strokeRec().getStyle() == SkStrokeRec::kStrokeAndFill_Style && |
| 554 | fStyle.strokeRec().getJoin() == SkPaint::kMiter_Join && |
| 555 | fStyle.strokeRec().getMiter() >= SK_ScalarSqrt2 && |
| 556 | fRRectData.fRRect.isRect()) { |
| 557 | SkScalar r = fStyle.strokeRec().getWidth() / 2; |
| 558 | fRRectData.fRRect = SkRRect::MakeRect(fRRectData.fRRect.rect().makeOutset(r, r)); |
| 559 | fStyle = GrStyle::SimpleFill(); |
| 560 | } |
bsalomon | ee29564 | 2016-06-06 14:01:25 -0700 | [diff] [blame] | 561 | } |
bsalomon | 0a0f67e | 2016-06-28 11:56:42 -0700 | [diff] [blame] | 562 | |
| 563 | void GrShape::attemptToSimplifyLine() { |
bsalomon | 0ae36a2 | 2016-07-18 07:31:13 -0700 | [diff] [blame] | 564 | SkASSERT(Type::kLine == fType); |
| 565 | SkASSERT(!fInheritedKey.count()); |
| 566 | if (fStyle.isDashed()) { |
| 567 | // Dashing ignores inverseness. |
| 568 | fLineData.fInverted = false; |
| 569 | return; |
| 570 | } else if (fStyle.hasPathEffect()) { |
| 571 | return; |
| 572 | } |
| 573 | if (fStyle.strokeRec().getStyle() == SkStrokeRec::kStrokeAndFill_Style) { |
| 574 | // Make stroke + fill be stroke since the fill is empty. |
| 575 | SkStrokeRec rec = fStyle.strokeRec(); |
| 576 | rec.setStrokeStyle(fStyle.strokeRec().getWidth(), false); |
| 577 | fStyle = GrStyle(rec, nullptr); |
| 578 | } |
Brian Salomon | 085c086 | 2017-08-31 15:44:51 -0400 | [diff] [blame] | 579 | if (fStyle.isSimpleFill()) { |
| 580 | this->changeType(fLineData.fInverted ? Type::kInvertedEmpty : Type::kEmpty); |
bsalomon | 0ae36a2 | 2016-07-18 07:31:13 -0700 | [diff] [blame] | 581 | return; |
| 582 | } |
| 583 | SkPoint* pts = fLineData.fPts; |
| 584 | if (fStyle.strokeRec().getStyle() == SkStrokeRec::kStroke_Style) { |
| 585 | // If it is horizontal or vertical we will turn it into a filled rrect. |
| 586 | SkRect rect; |
| 587 | rect.fLeft = SkTMin(pts[0].fX, pts[1].fX); |
| 588 | rect.fRight = SkTMax(pts[0].fX, pts[1].fX); |
| 589 | rect.fTop = SkTMin(pts[0].fY, pts[1].fY); |
| 590 | rect.fBottom = SkTMax(pts[0].fY, pts[1].fY); |
| 591 | bool eqX = rect.fLeft == rect.fRight; |
| 592 | bool eqY = rect.fTop == rect.fBottom; |
| 593 | if (eqX || eqY) { |
| 594 | SkScalar r = fStyle.strokeRec().getWidth() / 2; |
| 595 | bool inverted = fLineData.fInverted; |
| 596 | this->changeType(Type::kRRect); |
| 597 | switch (fStyle.strokeRec().getCap()) { |
| 598 | case SkPaint::kButt_Cap: |
| 599 | if (eqX && eqY) { |
| 600 | this->changeType(Type::kEmpty); |
| 601 | return; |
| 602 | } |
| 603 | if (eqX) { |
| 604 | rect.outset(r, 0); |
| 605 | } else { |
| 606 | rect.outset(0, r); |
| 607 | } |
| 608 | fRRectData.fRRect = SkRRect::MakeRect(rect); |
| 609 | break; |
| 610 | case SkPaint::kSquare_Cap: |
| 611 | rect.outset(r, r); |
| 612 | fRRectData.fRRect = SkRRect::MakeRect(rect); |
| 613 | break; |
| 614 | case SkPaint::kRound_Cap: |
| 615 | rect.outset(r, r); |
| 616 | fRRectData.fRRect = SkRRect::MakeRectXY(rect, r, r); |
| 617 | break; |
bsalomon | 0a0f67e | 2016-06-28 11:56:42 -0700 | [diff] [blame] | 618 | } |
bsalomon | 0ae36a2 | 2016-07-18 07:31:13 -0700 | [diff] [blame] | 619 | fRRectData.fInverted = inverted; |
| 620 | fRRectData.fDir = kDefaultRRectDir; |
| 621 | fRRectData.fStart = kDefaultRRectStart; |
| 622 | if (fRRectData.fRRect.isEmpty()) { |
| 623 | // 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] | 624 | this->changeType(inverted ? Type::kInvertedEmpty : Type::kEmpty); |
bsalomon | 0ae36a2 | 2016-07-18 07:31:13 -0700 | [diff] [blame] | 625 | return; |
| 626 | } |
| 627 | fStyle = GrStyle::SimpleFill(); |
| 628 | return; |
bsalomon | 0a0f67e | 2016-06-28 11:56:42 -0700 | [diff] [blame] | 629 | } |
| 630 | } |
bsalomon | 0ae36a2 | 2016-07-18 07:31:13 -0700 | [diff] [blame] | 631 | // Only path effects could care about the order of the points. Otherwise canonicalize |
| 632 | // the point order. |
| 633 | if (pts[1].fY < pts[0].fY || (pts[1].fY == pts[0].fY && pts[1].fX < pts[0].fX)) { |
| 634 | SkTSwap(pts[0], pts[1]); |
| 635 | } |
bsalomon | 0a0f67e | 2016-06-28 11:56:42 -0700 | [diff] [blame] | 636 | } |