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