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 <initializer_list> |
| 9 | #include <functional> |
| 10 | #include "Test.h" |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 11 | #include "GrShape.h" |
bsalomon | 9fb4203 | 2016-05-13 09:23:38 -0700 | [diff] [blame] | 12 | #include "SkCanvas.h" |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 13 | #include "SkDashPathEffect.h" |
bsalomon | 9fb4203 | 2016-05-13 09:23:38 -0700 | [diff] [blame] | 14 | #include "SkPath.h" |
bsalomon | ee29564 | 2016-06-06 14:01:25 -0700 | [diff] [blame] | 15 | #include "SkPathOps.h" |
Mike Reed | 185ffe9 | 2018-01-08 17:09:54 -0500 | [diff] [blame] | 16 | #include "SkRectPriv.h" |
bsalomon | 9fb4203 | 2016-05-13 09:23:38 -0700 | [diff] [blame] | 17 | #include "SkSurface.h" |
Mike Reed | ebfce6d | 2016-12-12 10:02:12 -0500 | [diff] [blame] | 18 | #include "SkClipOpPriv.h" |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 19 | |
Ben Wagner | f08d1d0 | 2018-06-18 15:11:00 -0400 | [diff] [blame] | 20 | #include <utility> |
| 21 | |
Brian Osman | f6f7cf6 | 2017-09-25 16:49:55 -0400 | [diff] [blame] | 22 | uint32_t GrShape::testingOnly_getOriginalGenerationID() const { |
Brian Salomon | da6d072 | 2018-01-03 13:54:35 -0500 | [diff] [blame] | 23 | if (const auto* lp = this->originalPathForListeners()) { |
| 24 | return lp->getGenerationID(); |
| 25 | } |
| 26 | return SkPath().getGenerationID(); |
Brian Osman | f6f7cf6 | 2017-09-25 16:49:55 -0400 | [diff] [blame] | 27 | } |
| 28 | |
Brian Osman | b379dcd | 2017-10-04 15:44:05 -0400 | [diff] [blame] | 29 | bool GrShape::testingOnly_isPath() const { |
| 30 | return Type::kPath == fType; |
| 31 | } |
| 32 | |
Brian Salomon | da6d072 | 2018-01-03 13:54:35 -0500 | [diff] [blame] | 33 | bool GrShape::testingOnly_isNonVolatilePath() const { |
| 34 | return Type::kPath == fType && !fPathData.fPath.isVolatile(); |
| 35 | } |
| 36 | |
bsalomon | 72dc51c | 2016-04-27 06:46:23 -0700 | [diff] [blame] | 37 | using Key = SkTArray<uint32_t>; |
| 38 | |
| 39 | static bool make_key(Key* key, const GrShape& shape) { |
| 40 | int size = shape.unstyledKeySize(); |
| 41 | if (size <= 0) { |
| 42 | key->reset(0); |
| 43 | return false; |
| 44 | } |
| 45 | SkASSERT(size); |
| 46 | key->reset(size); |
| 47 | shape.writeUnstyledKey(key->begin()); |
| 48 | return true; |
| 49 | } |
| 50 | |
bsalomon | ee29564 | 2016-06-06 14:01:25 -0700 | [diff] [blame] | 51 | static bool paths_fill_same(const SkPath& a, const SkPath& b) { |
| 52 | SkPath pathXor; |
| 53 | Op(a, b, SkPathOp::kXOR_SkPathOp, &pathXor); |
| 54 | return pathXor.isEmpty(); |
| 55 | } |
| 56 | |
bsalomon | 9fb4203 | 2016-05-13 09:23:38 -0700 | [diff] [blame] | 57 | static bool test_bounds_by_rasterizing(const SkPath& path, const SkRect& bounds) { |
bsalomon | 164fd9f | 2016-08-26 06:45:06 -0700 | [diff] [blame] | 58 | // We test the bounds by rasterizing the path into a kRes by kRes grid. The bounds is |
| 59 | // mapped to the range kRes/4 to 3*kRes/4 in x and y. A difference clip is used to avoid |
| 60 | // rendering within the bounds (with a tolerance). Then we render the path and check that |
| 61 | // everything got clipped out. |
bsalomon | 9fb4203 | 2016-05-13 09:23:38 -0700 | [diff] [blame] | 62 | static constexpr int kRes = 2000; |
| 63 | // This tolerance is in units of 1/kRes fractions of the bounds width/height. |
Brian Salomon | e494940 | 2018-04-26 15:22:04 -0400 | [diff] [blame] | 64 | static constexpr int kTol = 2; |
bsalomon | 9fb4203 | 2016-05-13 09:23:38 -0700 | [diff] [blame] | 65 | GR_STATIC_ASSERT(kRes % 4 == 0); |
| 66 | SkImageInfo info = SkImageInfo::MakeA8(kRes, kRes); |
| 67 | sk_sp<SkSurface> surface = SkSurface::MakeRaster(info); |
| 68 | surface->getCanvas()->clear(0x0); |
| 69 | SkRect clip = SkRect::MakeXYWH(kRes/4, kRes/4, kRes/2, kRes/2); |
| 70 | SkMatrix matrix; |
| 71 | matrix.setRectToRect(bounds, clip, SkMatrix::kFill_ScaleToFit); |
| 72 | clip.outset(SkIntToScalar(kTol), SkIntToScalar(kTol)); |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 73 | surface->getCanvas()->clipRect(clip, kDifference_SkClipOp); |
bsalomon | 9fb4203 | 2016-05-13 09:23:38 -0700 | [diff] [blame] | 74 | surface->getCanvas()->concat(matrix); |
| 75 | SkPaint whitePaint; |
| 76 | whitePaint.setColor(SK_ColorWHITE); |
| 77 | surface->getCanvas()->drawPath(path, whitePaint); |
| 78 | SkPixmap pixmap; |
| 79 | surface->getCanvas()->peekPixels(&pixmap); |
| 80 | #if defined(SK_BUILD_FOR_WIN) |
| 81 | // The static constexpr version in #else causes cl.exe to crash. |
| 82 | const uint8_t* kZeros = reinterpret_cast<uint8_t*>(calloc(kRes, 1)); |
| 83 | #else |
| 84 | static constexpr uint8_t kZeros[kRes] = {0}; |
| 85 | #endif |
bsalomon | 164fd9f | 2016-08-26 06:45:06 -0700 | [diff] [blame] | 86 | for (int y = 0; y < kRes; ++y) { |
bsalomon | 9fb4203 | 2016-05-13 09:23:38 -0700 | [diff] [blame] | 87 | const uint8_t* row = pixmap.addr8(0, y); |
| 88 | if (0 != memcmp(kZeros, row, kRes)) { |
| 89 | return false; |
| 90 | } |
| 91 | } |
| 92 | #ifdef SK_BUILD_FOR_WIN |
| 93 | free(const_cast<uint8_t*>(kZeros)); |
| 94 | #endif |
| 95 | return true; |
| 96 | } |
bsalomon | 72dc51c | 2016-04-27 06:46:23 -0700 | [diff] [blame] | 97 | |
Brian Salomon | 4f40caf | 2017-09-01 09:00:45 -0400 | [diff] [blame] | 98 | static bool can_interchange_winding_and_even_odd_fill(const GrShape& shape) { |
| 99 | SkPath path; |
| 100 | shape.asPath(&path); |
| 101 | if (shape.style().hasNonDashPathEffect()) { |
| 102 | return false; |
| 103 | } |
| 104 | const SkStrokeRec::Style strokeRecStyle = shape.style().strokeRec().getStyle(); |
| 105 | return strokeRecStyle == SkStrokeRec::kStroke_Style || |
| 106 | strokeRecStyle == SkStrokeRec::kHairline_Style || |
| 107 | (shape.style().isSimpleFill() && path.isConvex()); |
| 108 | } |
| 109 | |
| 110 | static void check_equivalence(skiatest::Reporter* r, const GrShape& a, const GrShape& b, |
| 111 | const Key& keyA, const Key& keyB) { |
| 112 | // GrShape only respects the input winding direction and start point for rrect shapes |
| 113 | // when there is a path effect. Thus, if there are two GrShapes representing the same rrect |
| 114 | // but one has a path effect in its style and the other doesn't then asPath() and the unstyled |
| 115 | // key will differ. GrShape will have canonicalized the direction and start point for the shape |
| 116 | // without the path effect. If *both* have path effects then they should have both preserved |
| 117 | // the direction and starting point. |
| 118 | |
| 119 | // The asRRect() output params are all initialized just to silence compiler warnings about |
| 120 | // uninitialized variables. |
| 121 | SkRRect rrectA = SkRRect::MakeEmpty(), rrectB = SkRRect::MakeEmpty(); |
| 122 | SkPath::Direction dirA = SkPath::kCW_Direction, dirB = SkPath::kCW_Direction; |
| 123 | unsigned startA = ~0U, startB = ~0U; |
| 124 | bool invertedA = true, invertedB = true; |
| 125 | |
| 126 | bool aIsRRect = a.asRRect(&rrectA, &dirA, &startA, &invertedA); |
| 127 | bool bIsRRect = b.asRRect(&rrectB, &dirB, &startB, &invertedB); |
| 128 | bool aHasPE = a.style().hasPathEffect(); |
| 129 | bool bHasPE = b.style().hasPathEffect(); |
| 130 | bool allowSameRRectButDiffStartAndDir = (aIsRRect && bIsRRect) && (aHasPE != bHasPE); |
| 131 | // GrShape will close paths with simple fill style. |
| 132 | bool allowedClosednessDiff = (a.style().isSimpleFill() != b.style().isSimpleFill()); |
| 133 | SkPath pathA, pathB; |
| 134 | a.asPath(&pathA); |
| 135 | b.asPath(&pathB); |
| 136 | |
| 137 | // Having a dash path effect can allow 'a' but not 'b' to turn a inverse fill type into a |
| 138 | // non-inverse fill type (or vice versa). |
| 139 | bool ignoreInversenessDifference = false; |
| 140 | if (pathA.isInverseFillType() != pathB.isInverseFillType()) { |
| 141 | const GrShape* s1 = pathA.isInverseFillType() ? &a : &b; |
| 142 | const GrShape* s2 = pathA.isInverseFillType() ? &b : &a; |
| 143 | bool canDropInverse1 = s1->style().isDashed(); |
| 144 | bool canDropInverse2 = s2->style().isDashed(); |
| 145 | ignoreInversenessDifference = (canDropInverse1 != canDropInverse2); |
| 146 | } |
| 147 | bool ignoreWindingVsEvenOdd = false; |
| 148 | if (SkPath::ConvertToNonInverseFillType(pathA.getFillType()) != |
| 149 | SkPath::ConvertToNonInverseFillType(pathB.getFillType())) { |
| 150 | bool aCanChange = can_interchange_winding_and_even_odd_fill(a); |
| 151 | bool bCanChange = can_interchange_winding_and_even_odd_fill(b); |
| 152 | if (aCanChange != bCanChange) { |
| 153 | ignoreWindingVsEvenOdd = true; |
| 154 | } |
| 155 | } |
| 156 | if (allowSameRRectButDiffStartAndDir) { |
| 157 | REPORTER_ASSERT(r, rrectA == rrectB); |
| 158 | REPORTER_ASSERT(r, paths_fill_same(pathA, pathB)); |
| 159 | REPORTER_ASSERT(r, ignoreInversenessDifference || invertedA == invertedB); |
| 160 | } else { |
| 161 | SkPath pA = pathA; |
| 162 | SkPath pB = pathB; |
| 163 | REPORTER_ASSERT(r, a.inverseFilled() == pA.isInverseFillType()); |
| 164 | REPORTER_ASSERT(r, b.inverseFilled() == pB.isInverseFillType()); |
| 165 | if (ignoreInversenessDifference) { |
| 166 | pA.setFillType(SkPath::ConvertToNonInverseFillType(pathA.getFillType())); |
| 167 | pB.setFillType(SkPath::ConvertToNonInverseFillType(pathB.getFillType())); |
| 168 | } |
| 169 | if (ignoreWindingVsEvenOdd) { |
| 170 | pA.setFillType(pA.isInverseFillType() ? SkPath::kInverseEvenOdd_FillType |
| 171 | : SkPath::kEvenOdd_FillType); |
| 172 | pB.setFillType(pB.isInverseFillType() ? SkPath::kInverseEvenOdd_FillType |
| 173 | : SkPath::kEvenOdd_FillType); |
| 174 | } |
| 175 | if (!ignoreInversenessDifference && !ignoreWindingVsEvenOdd) { |
| 176 | REPORTER_ASSERT(r, keyA == keyB); |
| 177 | } else { |
| 178 | REPORTER_ASSERT(r, keyA != keyB); |
| 179 | } |
| 180 | if (allowedClosednessDiff) { |
| 181 | // GrShape will close paths with simple fill style. Make the non-filled path closed |
| 182 | // so that the comparision will succeed. Make sure both are closed before comparing. |
| 183 | pA.close(); |
| 184 | pB.close(); |
| 185 | } |
| 186 | REPORTER_ASSERT(r, pA == pB); |
| 187 | REPORTER_ASSERT(r, aIsRRect == bIsRRect); |
| 188 | if (aIsRRect) { |
| 189 | REPORTER_ASSERT(r, rrectA == rrectB); |
| 190 | REPORTER_ASSERT(r, dirA == dirB); |
| 191 | REPORTER_ASSERT(r, startA == startB); |
| 192 | REPORTER_ASSERT(r, ignoreInversenessDifference || invertedA == invertedB); |
| 193 | } |
| 194 | } |
| 195 | REPORTER_ASSERT(r, a.isEmpty() == b.isEmpty()); |
| 196 | REPORTER_ASSERT(r, allowedClosednessDiff || a.knownToBeClosed() == b.knownToBeClosed()); |
| 197 | // closedness can affect convexity. |
| 198 | REPORTER_ASSERT(r, allowedClosednessDiff || a.knownToBeConvex() == b.knownToBeConvex()); |
| 199 | if (a.knownToBeConvex()) { |
| 200 | REPORTER_ASSERT(r, pathA.isConvex()); |
| 201 | } |
| 202 | if (b.knownToBeConvex()) { |
| 203 | REPORTER_ASSERT(r, pathB.isConvex()); |
| 204 | } |
| 205 | REPORTER_ASSERT(r, a.bounds() == b.bounds()); |
| 206 | REPORTER_ASSERT(r, a.segmentMask() == b.segmentMask()); |
| 207 | // Init these to suppress warnings. |
| 208 | SkPoint pts[4] {{0, 0,}, {0, 0}, {0, 0}, {0, 0}} ; |
| 209 | bool invertedLine[2] {true, true}; |
| 210 | REPORTER_ASSERT(r, a.asLine(pts, &invertedLine[0]) == b.asLine(pts + 2, &invertedLine[1])); |
| 211 | // mayBeInverseFilledAfterStyling() is allowed to differ if one has a arbitrary PE and the other |
| 212 | // doesn't (since the PE can set any fill type on its output path). |
| 213 | // Moreover, dash style explicitly ignores inverseness. So if one is dashed but not the other |
| 214 | // then they may disagree about inverseness. |
| 215 | if (a.style().hasNonDashPathEffect() == b.style().hasNonDashPathEffect() && |
| 216 | a.style().isDashed() == b.style().isDashed()) { |
| 217 | REPORTER_ASSERT(r, a.mayBeInverseFilledAfterStyling() == |
| 218 | b.mayBeInverseFilledAfterStyling()); |
| 219 | } |
| 220 | if (a.asLine(nullptr, nullptr)) { |
| 221 | REPORTER_ASSERT(r, pts[2] == pts[0] && pts[3] == pts[1]); |
| 222 | REPORTER_ASSERT(r, ignoreInversenessDifference || invertedLine[0] == invertedLine[1]); |
| 223 | REPORTER_ASSERT(r, invertedLine[0] == a.inverseFilled()); |
| 224 | REPORTER_ASSERT(r, invertedLine[1] == b.inverseFilled()); |
| 225 | } |
| 226 | REPORTER_ASSERT(r, ignoreInversenessDifference || a.inverseFilled() == b.inverseFilled()); |
| 227 | } |
| 228 | |
Brian Osman | b379dcd | 2017-10-04 15:44:05 -0400 | [diff] [blame] | 229 | static void check_original_path_ids(skiatest::Reporter* r, const GrShape& base, const GrShape& pe, |
| 230 | const GrShape& peStroke, const GrShape& full) { |
Brian Salomon | da6d072 | 2018-01-03 13:54:35 -0500 | [diff] [blame] | 231 | bool baseIsNonVolatilePath = base.testingOnly_isNonVolatilePath(); |
Brian Osman | b379dcd | 2017-10-04 15:44:05 -0400 | [diff] [blame] | 232 | bool peIsPath = pe.testingOnly_isPath(); |
| 233 | bool peStrokeIsPath = peStroke.testingOnly_isPath(); |
| 234 | bool fullIsPath = full.testingOnly_isPath(); |
| 235 | |
| 236 | REPORTER_ASSERT(r, peStrokeIsPath == fullIsPath); |
| 237 | |
| 238 | uint32_t baseID = base.testingOnly_getOriginalGenerationID(); |
| 239 | uint32_t peID = pe.testingOnly_getOriginalGenerationID(); |
| 240 | uint32_t peStrokeID = peStroke.testingOnly_getOriginalGenerationID(); |
| 241 | uint32_t fullID = full.testingOnly_getOriginalGenerationID(); |
| 242 | |
| 243 | // All empty paths have the same gen ID |
| 244 | uint32_t emptyID = SkPath().getGenerationID(); |
| 245 | |
| 246 | // If we started with a real path, then our genID should match that path's gen ID (and not be |
Brian Salomon | da6d072 | 2018-01-03 13:54:35 -0500 | [diff] [blame] | 247 | // empty). If we started with a simple shape or a volatile path, our original path should have |
| 248 | // been reset. |
| 249 | REPORTER_ASSERT(r, baseIsNonVolatilePath == (baseID != emptyID)); |
Brian Osman | b379dcd | 2017-10-04 15:44:05 -0400 | [diff] [blame] | 250 | |
| 251 | // For the derived shapes, if they're simple types, their original paths should have been reset |
| 252 | REPORTER_ASSERT(r, peIsPath || (peID == emptyID)); |
| 253 | REPORTER_ASSERT(r, peStrokeIsPath || (peStrokeID == emptyID)); |
| 254 | REPORTER_ASSERT(r, fullIsPath || (fullID == emptyID)); |
| 255 | |
| 256 | if (!peIsPath) { |
| 257 | // If the path effect produces a simple shape, then there are no unbroken chains to test |
| 258 | return; |
| 259 | } |
| 260 | |
| 261 | // From here on, we know that the path effect produced a shape that was a "real" path |
| 262 | |
Brian Salomon | da6d072 | 2018-01-03 13:54:35 -0500 | [diff] [blame] | 263 | if (baseIsNonVolatilePath) { |
Brian Osman | b379dcd | 2017-10-04 15:44:05 -0400 | [diff] [blame] | 264 | REPORTER_ASSERT(r, baseID == peID); |
| 265 | } |
| 266 | |
| 267 | if (peStrokeIsPath) { |
| 268 | REPORTER_ASSERT(r, peID == peStrokeID); |
| 269 | REPORTER_ASSERT(r, peStrokeID == fullID); |
| 270 | } |
| 271 | |
Brian Salomon | da6d072 | 2018-01-03 13:54:35 -0500 | [diff] [blame] | 272 | if (baseIsNonVolatilePath && peStrokeIsPath) { |
Brian Osman | b379dcd | 2017-10-04 15:44:05 -0400 | [diff] [blame] | 273 | REPORTER_ASSERT(r, baseID == peStrokeID); |
| 274 | REPORTER_ASSERT(r, baseID == fullID); |
| 275 | } |
| 276 | } |
| 277 | |
Brian Salomon | 4f40caf | 2017-09-01 09:00:45 -0400 | [diff] [blame] | 278 | void test_inversions(skiatest::Reporter* r, const GrShape& shape, const Key& shapeKey) { |
| 279 | GrShape preserve = GrShape::MakeFilled(shape, GrShape::FillInversion::kPreserve); |
| 280 | Key preserveKey; |
| 281 | make_key(&preserveKey, preserve); |
| 282 | |
| 283 | GrShape flip = GrShape::MakeFilled(shape, GrShape::FillInversion::kFlip); |
| 284 | Key flipKey; |
| 285 | make_key(&flipKey, flip); |
| 286 | |
| 287 | GrShape inverted = GrShape::MakeFilled(shape, GrShape::FillInversion::kForceInverted); |
| 288 | Key invertedKey; |
| 289 | make_key(&invertedKey, inverted); |
| 290 | |
| 291 | GrShape noninverted = GrShape::MakeFilled(shape, GrShape::FillInversion::kForceNoninverted); |
| 292 | Key noninvertedKey; |
| 293 | make_key(&noninvertedKey, noninverted); |
| 294 | |
| 295 | if (invertedKey.count() || noninvertedKey.count()) { |
| 296 | REPORTER_ASSERT(r, invertedKey != noninvertedKey); |
| 297 | } |
| 298 | if (shape.style().isSimpleFill()) { |
| 299 | check_equivalence(r, shape, preserve, shapeKey, preserveKey); |
| 300 | } |
| 301 | if (shape.inverseFilled()) { |
| 302 | check_equivalence(r, preserve, inverted, preserveKey, invertedKey); |
| 303 | check_equivalence(r, flip, noninverted, flipKey, noninvertedKey); |
| 304 | } else { |
| 305 | check_equivalence(r, preserve, noninverted, preserveKey, noninvertedKey); |
| 306 | check_equivalence(r, flip, inverted, flipKey, invertedKey); |
| 307 | } |
| 308 | |
| 309 | GrShape doubleFlip = GrShape::MakeFilled(flip, GrShape::FillInversion::kFlip); |
| 310 | Key doubleFlipKey; |
| 311 | make_key(&doubleFlipKey, doubleFlip); |
| 312 | // It can be the case that the double flip has no key but preserve does. This happens when the |
| 313 | // original shape has an inherited style key. That gets dropped on the first inversion flip. |
| 314 | if (preserveKey.count() && !doubleFlipKey.count()) { |
| 315 | preserveKey.reset(); |
| 316 | } |
| 317 | check_equivalence(r, preserve, doubleFlip, preserveKey, doubleFlipKey); |
| 318 | } |
| 319 | |
bsalomon | 9fb4203 | 2016-05-13 09:23:38 -0700 | [diff] [blame] | 320 | namespace { |
bsalomon | a395f7c | 2016-08-24 17:47:40 -0700 | [diff] [blame] | 321 | /** |
| 322 | * Geo is a factory for creating a GrShape from another representation. It also answers some |
| 323 | * questions about expected behavior for GrShape given the inputs. |
| 324 | */ |
| 325 | class Geo { |
| 326 | public: |
Mike Klein | fc6c37b | 2016-09-27 09:34:10 -0400 | [diff] [blame] | 327 | virtual ~Geo() {} |
bsalomon | a395f7c | 2016-08-24 17:47:40 -0700 | [diff] [blame] | 328 | virtual GrShape makeShape(const SkPaint&) const = 0; |
| 329 | virtual SkPath path() const = 0; |
| 330 | // These functions allow tests to check for special cases where style gets |
| 331 | // applied by GrShape in its constructor (without calling GrShape::applyStyle). |
| 332 | // These unfortunately rely on knowing details of GrShape's implementation. |
| 333 | // These predicates are factored out here to avoid littering the rest of the |
| 334 | // test code with GrShape implementation details. |
| 335 | virtual bool fillChangesGeom() const { return false; } |
| 336 | virtual bool strokeIsConvertedToFill() const { return false; } |
| 337 | virtual bool strokeAndFillIsConvertedToFill(const SkPaint&) const { return false; } |
| 338 | // Is this something we expect GrShape to recognize as something simpler than a path. |
| 339 | virtual bool isNonPath(const SkPaint& paint) const { return true; } |
| 340 | }; |
| 341 | |
| 342 | class RectGeo : public Geo { |
| 343 | public: |
| 344 | RectGeo(const SkRect& rect) : fRect(rect) {} |
| 345 | |
| 346 | SkPath path() const override { |
| 347 | SkPath path; |
| 348 | path.addRect(fRect); |
| 349 | return path; |
| 350 | } |
| 351 | |
| 352 | GrShape makeShape(const SkPaint& paint) const override { |
| 353 | return GrShape(fRect, paint); |
| 354 | } |
| 355 | |
| 356 | bool strokeAndFillIsConvertedToFill(const SkPaint& paint) const override { |
| 357 | SkASSERT(paint.getStyle() == SkPaint::kStrokeAndFill_Style); |
| 358 | // Converted to an outset rectangle. |
| 359 | return paint.getStrokeJoin() == SkPaint::kMiter_Join && |
| 360 | paint.getStrokeMiter() >= SK_ScalarSqrt2; |
| 361 | } |
| 362 | |
| 363 | private: |
| 364 | SkRect fRect; |
| 365 | }; |
| 366 | |
| 367 | class RRectGeo : public Geo { |
| 368 | public: |
| 369 | RRectGeo(const SkRRect& rrect) : fRRect(rrect) {} |
| 370 | |
| 371 | GrShape makeShape(const SkPaint& paint) const override { |
| 372 | return GrShape(fRRect, paint); |
| 373 | } |
| 374 | |
| 375 | SkPath path() const override { |
| 376 | SkPath path; |
| 377 | path.addRRect(fRRect); |
| 378 | return path; |
| 379 | } |
| 380 | |
| 381 | bool strokeAndFillIsConvertedToFill(const SkPaint& paint) const override { |
| 382 | SkASSERT(paint.getStyle() == SkPaint::kStrokeAndFill_Style); |
| 383 | if (fRRect.isRect()) { |
| 384 | return RectGeo(fRRect.rect()).strokeAndFillIsConvertedToFill(paint); |
| 385 | } |
| 386 | return false; |
| 387 | } |
| 388 | |
| 389 | private: |
| 390 | SkRRect fRRect; |
| 391 | }; |
| 392 | |
Brian Salomon | e494940 | 2018-04-26 15:22:04 -0400 | [diff] [blame] | 393 | class ArcGeo : public Geo { |
| 394 | public: |
| 395 | ArcGeo(const SkRect& oval, SkScalar startAngle, SkScalar sweepAngle, bool useCenter) |
| 396 | : fOval(oval) |
| 397 | , fStartAngle(startAngle) |
| 398 | , fSweepAngle(sweepAngle) |
| 399 | , fUseCenter(useCenter) {} |
| 400 | |
| 401 | SkPath path() const override { |
| 402 | SkPath path; |
| 403 | SkPathPriv::CreateDrawArcPath(&path, fOval, fStartAngle, fSweepAngle, fUseCenter, false); |
| 404 | return path; |
| 405 | } |
| 406 | |
| 407 | GrShape makeShape(const SkPaint& paint) const override { |
| 408 | return GrShape::MakeArc(fOval, fStartAngle, fSweepAngle, fUseCenter, GrStyle(paint)); |
| 409 | } |
| 410 | |
| 411 | // GrShape specializes when created from arc params but it doesn't recognize arcs from SkPath. |
| 412 | bool isNonPath(const SkPaint& paint) const override { return false; } |
| 413 | |
| 414 | private: |
| 415 | SkRect fOval; |
| 416 | SkScalar fStartAngle; |
| 417 | SkScalar fSweepAngle; |
| 418 | bool fUseCenter; |
| 419 | }; |
| 420 | |
bsalomon | a395f7c | 2016-08-24 17:47:40 -0700 | [diff] [blame] | 421 | class PathGeo : public Geo { |
| 422 | public: |
| 423 | enum class Invert { kNo, kYes }; |
| 424 | |
| 425 | PathGeo(const SkPath& path, Invert invert) : fPath(path) { |
| 426 | SkASSERT(!path.isInverseFillType()); |
| 427 | if (Invert::kYes == invert) { |
| 428 | if (fPath.getFillType() == SkPath::kEvenOdd_FillType) { |
| 429 | fPath.setFillType(SkPath::kInverseEvenOdd_FillType); |
| 430 | } else { |
| 431 | SkASSERT(fPath.getFillType() == SkPath::kWinding_FillType); |
| 432 | fPath.setFillType(SkPath::kInverseWinding_FillType); |
| 433 | } |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | GrShape makeShape(const SkPaint& paint) const override { |
| 438 | return GrShape(fPath, paint); |
| 439 | } |
| 440 | |
| 441 | SkPath path() const override { return fPath; } |
| 442 | |
| 443 | bool fillChangesGeom() const override { |
| 444 | // unclosed rects get closed. Lines get turned into empty geometry |
Brian Salomon | 085c086 | 2017-08-31 15:44:51 -0400 | [diff] [blame] | 445 | return this->isUnclosedRect() || fPath.isLine(nullptr); |
bsalomon | a395f7c | 2016-08-24 17:47:40 -0700 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | bool strokeIsConvertedToFill() const override { |
| 449 | return this->isAxisAlignedLine(); |
| 450 | } |
| 451 | |
| 452 | bool strokeAndFillIsConvertedToFill(const SkPaint& paint) const override { |
| 453 | SkASSERT(paint.getStyle() == SkPaint::kStrokeAndFill_Style); |
| 454 | if (this->isAxisAlignedLine()) { |
| 455 | // The fill is ignored (zero area) and the stroke is converted to a rrect. |
| 456 | return true; |
| 457 | } |
| 458 | SkRect rect; |
| 459 | unsigned start; |
| 460 | SkPath::Direction dir; |
| 461 | if (SkPathPriv::IsSimpleClosedRect(fPath, &rect, &dir, &start)) { |
| 462 | return RectGeo(rect).strokeAndFillIsConvertedToFill(paint); |
| 463 | } |
| 464 | return false; |
| 465 | } |
| 466 | |
| 467 | bool isNonPath(const SkPaint& paint) const override { |
| 468 | return fPath.isLine(nullptr) || fPath.isEmpty(); |
| 469 | } |
| 470 | |
| 471 | private: |
| 472 | bool isAxisAlignedLine() const { |
| 473 | SkPoint pts[2]; |
| 474 | if (!fPath.isLine(pts)) { |
| 475 | return false; |
| 476 | } |
| 477 | return pts[0].fX == pts[1].fX || pts[0].fY == pts[1].fY; |
| 478 | } |
| 479 | |
| 480 | bool isUnclosedRect() const { |
| 481 | bool closed; |
| 482 | return fPath.isRect(nullptr, &closed, nullptr) && !closed; |
| 483 | } |
| 484 | |
| 485 | SkPath fPath; |
| 486 | }; |
| 487 | |
| 488 | class RRectPathGeo : public PathGeo { |
| 489 | public: |
| 490 | enum class RRectForStroke { kNo, kYes }; |
| 491 | |
| 492 | RRectPathGeo(const SkPath& path, const SkRRect& equivalentRRect, RRectForStroke rrectForStroke, |
| 493 | Invert invert) |
| 494 | : PathGeo(path, invert) |
| 495 | , fRRect(equivalentRRect) |
| 496 | , fRRectForStroke(rrectForStroke) {} |
| 497 | |
| 498 | RRectPathGeo(const SkPath& path, const SkRect& equivalentRect, RRectForStroke rrectForStroke, |
| 499 | Invert invert) |
| 500 | : RRectPathGeo(path, SkRRect::MakeRect(equivalentRect), rrectForStroke, invert) {} |
| 501 | |
| 502 | bool isNonPath(const SkPaint& paint) const override { |
| 503 | if (SkPaint::kFill_Style == paint.getStyle() || RRectForStroke::kYes == fRRectForStroke) { |
| 504 | return true; |
| 505 | } |
| 506 | return false; |
| 507 | } |
| 508 | |
| 509 | const SkRRect& rrect() const { return fRRect; } |
| 510 | |
| 511 | private: |
| 512 | SkRRect fRRect; |
| 513 | RRectForStroke fRRectForStroke; |
| 514 | }; |
| 515 | |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 516 | class TestCase { |
| 517 | public: |
bsalomon | a395f7c | 2016-08-24 17:47:40 -0700 | [diff] [blame] | 518 | TestCase(const Geo& geo, const SkPaint& paint, skiatest::Reporter* r, |
Brian Salomon | c8cdad7 | 2018-04-16 09:46:09 -0400 | [diff] [blame] | 519 | SkScalar scale = SK_Scalar1) |
| 520 | : fBase(new GrShape(geo.makeShape(paint))) { |
bsalomon | 97fd2d4 | 2016-05-09 13:02:01 -0700 | [diff] [blame] | 521 | this->init(r, scale); |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 522 | } |
| 523 | |
Brian Salomon | c8cdad7 | 2018-04-16 09:46:09 -0400 | [diff] [blame] | 524 | template <typename... ShapeArgs> |
| 525 | TestCase(skiatest::Reporter* r, ShapeArgs... shapeArgs) : fBase(new GrShape(shapeArgs...)) { |
bsalomon | a395f7c | 2016-08-24 17:47:40 -0700 | [diff] [blame] | 526 | this->init(r, SK_Scalar1); |
| 527 | } |
| 528 | |
bsalomon | 7049396 | 2016-06-10 08:05:14 -0700 | [diff] [blame] | 529 | TestCase(const GrShape& shape, skiatest::Reporter* r, SkScalar scale = SK_Scalar1) |
Brian Salomon | c8cdad7 | 2018-04-16 09:46:09 -0400 | [diff] [blame] | 530 | : fBase(new GrShape(shape)) { |
bsalomon | 7049396 | 2016-06-10 08:05:14 -0700 | [diff] [blame] | 531 | this->init(r, scale); |
| 532 | } |
| 533 | |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 534 | struct SelfExpectations { |
| 535 | bool fPEHasEffect; |
| 536 | bool fPEHasValidKey; |
| 537 | bool fStrokeApplies; |
| 538 | }; |
| 539 | |
| 540 | void testExpectations(skiatest::Reporter* reporter, SelfExpectations expectations) const; |
| 541 | |
| 542 | enum ComparisonExpecation { |
| 543 | kAllDifferent_ComparisonExpecation, |
| 544 | kSameUpToPE_ComparisonExpecation, |
| 545 | kSameUpToStroke_ComparisonExpecation, |
| 546 | kAllSame_ComparisonExpecation, |
| 547 | }; |
| 548 | |
| 549 | void compare(skiatest::Reporter*, const TestCase& that, ComparisonExpecation) const; |
| 550 | |
Brian Salomon | c8cdad7 | 2018-04-16 09:46:09 -0400 | [diff] [blame] | 551 | const GrShape& baseShape() const { return *fBase; } |
| 552 | const GrShape& appliedPathEffectShape() const { return *fAppliedPE; } |
| 553 | const GrShape& appliedFullStyleShape() const { return *fAppliedFull; } |
bsalomon | 72dc51c | 2016-04-27 06:46:23 -0700 | [diff] [blame] | 554 | |
| 555 | // The returned array's count will be 0 if the key shape has no key. |
| 556 | const Key& baseKey() const { return fBaseKey; } |
| 557 | const Key& appliedPathEffectKey() const { return fAppliedPEKey; } |
| 558 | const Key& appliedFullStyleKey() const { return fAppliedFullKey; } |
bsalomon | 409ed73 | 2016-04-27 12:36:02 -0700 | [diff] [blame] | 559 | const Key& appliedPathEffectThenStrokeKey() const { return fAppliedPEThenStrokeKey; } |
bsalomon | 72dc51c | 2016-04-27 06:46:23 -0700 | [diff] [blame] | 560 | |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 561 | private: |
bsalomon | 9fb4203 | 2016-05-13 09:23:38 -0700 | [diff] [blame] | 562 | static void CheckBounds(skiatest::Reporter* r, const GrShape& shape, const SkRect& bounds) { |
| 563 | SkPath path; |
| 564 | shape.asPath(&path); |
| 565 | // If the bounds are empty, the path ought to be as well. |
bsalomon | 0ae36a2 | 2016-07-18 07:31:13 -0700 | [diff] [blame] | 566 | if (bounds.fLeft > bounds.fRight || bounds.fTop > bounds.fBottom) { |
bsalomon | 9fb4203 | 2016-05-13 09:23:38 -0700 | [diff] [blame] | 567 | REPORTER_ASSERT(r, path.isEmpty()); |
| 568 | return; |
| 569 | } |
| 570 | if (path.isEmpty()) { |
| 571 | return; |
| 572 | } |
bsalomon | 7049396 | 2016-06-10 08:05:14 -0700 | [diff] [blame] | 573 | // The bounds API explicitly calls out that it does not consider inverseness. |
| 574 | SkPath p = path; |
| 575 | p.setFillType(SkPath::ConvertToNonInverseFillType(path.getFillType())); |
| 576 | REPORTER_ASSERT(r, test_bounds_by_rasterizing(p, bounds)); |
bsalomon | 9fb4203 | 2016-05-13 09:23:38 -0700 | [diff] [blame] | 577 | } |
| 578 | |
bsalomon | 97fd2d4 | 2016-05-09 13:02:01 -0700 | [diff] [blame] | 579 | void init(skiatest::Reporter* r, SkScalar scale) { |
Brian Salomon | c8cdad7 | 2018-04-16 09:46:09 -0400 | [diff] [blame] | 580 | fAppliedPE.reset(new GrShape); |
| 581 | fAppliedPEThenStroke.reset(new GrShape); |
| 582 | fAppliedFull.reset(new GrShape); |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 583 | |
Brian Salomon | c8cdad7 | 2018-04-16 09:46:09 -0400 | [diff] [blame] | 584 | *fAppliedPE = fBase->applyStyle(GrStyle::Apply::kPathEffectOnly, scale); |
| 585 | *fAppliedPEThenStroke = |
| 586 | fAppliedPE->applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec, scale); |
| 587 | *fAppliedFull = fBase->applyStyle(GrStyle::Apply::kPathEffectAndStrokeRec, scale); |
| 588 | |
| 589 | make_key(&fBaseKey, *fBase); |
| 590 | make_key(&fAppliedPEKey, *fAppliedPE); |
| 591 | make_key(&fAppliedPEThenStrokeKey, *fAppliedPEThenStroke); |
| 592 | make_key(&fAppliedFullKey, *fAppliedFull); |
bsalomon | fb08327 | 2016-05-04 08:27:41 -0700 | [diff] [blame] | 593 | |
Brian Osman | f6f7cf6 | 2017-09-25 16:49:55 -0400 | [diff] [blame] | 594 | // All shapes should report the same "original" path, so that path renderers can get to it |
| 595 | // if necessary. |
Brian Salomon | c8cdad7 | 2018-04-16 09:46:09 -0400 | [diff] [blame] | 596 | check_original_path_ids(r, *fBase, *fAppliedPE, *fAppliedPEThenStroke, *fAppliedFull); |
Brian Osman | f6f7cf6 | 2017-09-25 16:49:55 -0400 | [diff] [blame] | 597 | |
bsalomon | fb08327 | 2016-05-04 08:27:41 -0700 | [diff] [blame] | 598 | // Applying the path effect and then the stroke should always be the same as applying |
| 599 | // both in one go. |
| 600 | REPORTER_ASSERT(r, fAppliedPEThenStrokeKey == fAppliedFullKey); |
| 601 | SkPath a, b; |
Brian Salomon | c8cdad7 | 2018-04-16 09:46:09 -0400 | [diff] [blame] | 602 | fAppliedPEThenStroke->asPath(&a); |
| 603 | fAppliedFull->asPath(&b); |
bsalomon | ee29564 | 2016-06-06 14:01:25 -0700 | [diff] [blame] | 604 | // If the output of the path effect is a rrect then it is possible for a and b to be |
| 605 | // different paths that fill identically. The reason is that fAppliedFull will do this: |
| 606 | // base -> apply path effect -> rrect_as_path -> stroke -> stroked_rrect_as_path |
| 607 | // fAppliedPEThenStroke will have converted the rrect_as_path back to a rrect. However, |
| 608 | // now that there is no longer a path effect, the direction and starting index get |
| 609 | // canonicalized before the stroke. |
Brian Salomon | c8cdad7 | 2018-04-16 09:46:09 -0400 | [diff] [blame] | 610 | if (fAppliedPE->asRRect(nullptr, nullptr, nullptr, nullptr)) { |
bsalomon | ee29564 | 2016-06-06 14:01:25 -0700 | [diff] [blame] | 611 | REPORTER_ASSERT(r, paths_fill_same(a, b)); |
| 612 | } else { |
| 613 | REPORTER_ASSERT(r, a == b); |
| 614 | } |
Brian Salomon | c8cdad7 | 2018-04-16 09:46:09 -0400 | [diff] [blame] | 615 | REPORTER_ASSERT(r, fAppliedFull->isEmpty() == fAppliedPEThenStroke->isEmpty()); |
bsalomon | 7c73a53 | 2016-05-11 15:15:56 -0700 | [diff] [blame] | 616 | |
| 617 | SkPath path; |
Brian Salomon | c8cdad7 | 2018-04-16 09:46:09 -0400 | [diff] [blame] | 618 | fBase->asPath(&path); |
| 619 | REPORTER_ASSERT(r, path.isEmpty() == fBase->isEmpty()); |
| 620 | REPORTER_ASSERT(r, path.getSegmentMasks() == fBase->segmentMask()); |
| 621 | fAppliedPE->asPath(&path); |
| 622 | REPORTER_ASSERT(r, path.isEmpty() == fAppliedPE->isEmpty()); |
| 623 | REPORTER_ASSERT(r, path.getSegmentMasks() == fAppliedPE->segmentMask()); |
| 624 | fAppliedFull->asPath(&path); |
| 625 | REPORTER_ASSERT(r, path.isEmpty() == fAppliedFull->isEmpty()); |
| 626 | REPORTER_ASSERT(r, path.getSegmentMasks() == fAppliedFull->segmentMask()); |
bsalomon | fb08327 | 2016-05-04 08:27:41 -0700 | [diff] [blame] | 627 | |
Brian Salomon | c8cdad7 | 2018-04-16 09:46:09 -0400 | [diff] [blame] | 628 | CheckBounds(r, *fBase, fBase->bounds()); |
| 629 | CheckBounds(r, *fAppliedPE, fAppliedPE->bounds()); |
| 630 | CheckBounds(r, *fAppliedPEThenStroke, fAppliedPEThenStroke->bounds()); |
| 631 | CheckBounds(r, *fAppliedFull, fAppliedFull->bounds()); |
| 632 | SkRect styledBounds = fBase->styledBounds(); |
| 633 | CheckBounds(r, *fAppliedFull, styledBounds); |
| 634 | styledBounds = fAppliedPE->styledBounds(); |
| 635 | CheckBounds(r, *fAppliedFull, styledBounds); |
bsalomon | 9fb4203 | 2016-05-13 09:23:38 -0700 | [diff] [blame] | 636 | |
bsalomon | fb08327 | 2016-05-04 08:27:41 -0700 | [diff] [blame] | 637 | // Check that the same path is produced when style is applied by GrShape and GrStyle. |
| 638 | SkPath preStyle; |
| 639 | SkPath postPathEffect; |
| 640 | SkPath postAllStyle; |
| 641 | |
Brian Salomon | c8cdad7 | 2018-04-16 09:46:09 -0400 | [diff] [blame] | 642 | fBase->asPath(&preStyle); |
bsalomon | 1a0b9ed | 2016-05-06 11:07:03 -0700 | [diff] [blame] | 643 | SkStrokeRec postPEStrokeRec(SkStrokeRec::kFill_InitStyle); |
Brian Salomon | c8cdad7 | 2018-04-16 09:46:09 -0400 | [diff] [blame] | 644 | if (fBase->style().applyPathEffectToPath(&postPathEffect, &postPEStrokeRec, preStyle, |
| 645 | scale)) { |
bsalomon | 1a0b9ed | 2016-05-06 11:07:03 -0700 | [diff] [blame] | 646 | // run postPathEffect through GrShape to get any geometry reductions that would have |
| 647 | // occurred to fAppliedPE. |
| 648 | GrShape(postPathEffect, GrStyle(postPEStrokeRec, nullptr)).asPath(&postPathEffect); |
| 649 | |
bsalomon | fb08327 | 2016-05-04 08:27:41 -0700 | [diff] [blame] | 650 | SkPath testPath; |
Brian Salomon | c8cdad7 | 2018-04-16 09:46:09 -0400 | [diff] [blame] | 651 | fAppliedPE->asPath(&testPath); |
bsalomon | fb08327 | 2016-05-04 08:27:41 -0700 | [diff] [blame] | 652 | REPORTER_ASSERT(r, testPath == postPathEffect); |
Brian Salomon | c8cdad7 | 2018-04-16 09:46:09 -0400 | [diff] [blame] | 653 | REPORTER_ASSERT(r, postPEStrokeRec.hasEqualEffect(fAppliedPE->style().strokeRec())); |
bsalomon | fb08327 | 2016-05-04 08:27:41 -0700 | [diff] [blame] | 654 | } |
| 655 | SkStrokeRec::InitStyle fillOrHairline; |
Brian Salomon | c8cdad7 | 2018-04-16 09:46:09 -0400 | [diff] [blame] | 656 | if (fBase->style().applyToPath(&postAllStyle, &fillOrHairline, preStyle, scale)) { |
bsalomon | fb08327 | 2016-05-04 08:27:41 -0700 | [diff] [blame] | 657 | SkPath testPath; |
Brian Salomon | c8cdad7 | 2018-04-16 09:46:09 -0400 | [diff] [blame] | 658 | fAppliedFull->asPath(&testPath); |
| 659 | if (fBase->style().hasPathEffect()) { |
bsalomon | 1b28c1a | 2016-06-20 12:28:17 -0700 | [diff] [blame] | 660 | // Because GrShape always does two-stage application when there is a path effect |
| 661 | // there may be a reduction/canonicalization step between the path effect and |
| 662 | // strokerec not reflected in postAllStyle since it applied both the path effect |
| 663 | // and strokerec without analyzing the intermediate path. |
| 664 | REPORTER_ASSERT(r, paths_fill_same(postAllStyle, testPath)); |
| 665 | } else { |
| 666 | // Make sure that postAllStyle sees any reductions/canonicalizations that GrShape |
| 667 | // would apply. |
| 668 | GrShape(postAllStyle, GrStyle(fillOrHairline)).asPath(&postAllStyle); |
| 669 | REPORTER_ASSERT(r, testPath == postAllStyle); |
| 670 | } |
| 671 | |
bsalomon | fb08327 | 2016-05-04 08:27:41 -0700 | [diff] [blame] | 672 | if (fillOrHairline == SkStrokeRec::kFill_InitStyle) { |
Brian Salomon | c8cdad7 | 2018-04-16 09:46:09 -0400 | [diff] [blame] | 673 | REPORTER_ASSERT(r, fAppliedFull->style().isSimpleFill()); |
bsalomon | fb08327 | 2016-05-04 08:27:41 -0700 | [diff] [blame] | 674 | } else { |
Brian Salomon | c8cdad7 | 2018-04-16 09:46:09 -0400 | [diff] [blame] | 675 | REPORTER_ASSERT(r, fAppliedFull->style().isSimpleHairline()); |
bsalomon | fb08327 | 2016-05-04 08:27:41 -0700 | [diff] [blame] | 676 | } |
| 677 | } |
Brian Salomon | c8cdad7 | 2018-04-16 09:46:09 -0400 | [diff] [blame] | 678 | test_inversions(r, *fBase, fBaseKey); |
| 679 | test_inversions(r, *fAppliedPE, fAppliedPEKey); |
| 680 | test_inversions(r, *fAppliedFull, fAppliedFullKey); |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 681 | } |
| 682 | |
Brian Salomon | c8cdad7 | 2018-04-16 09:46:09 -0400 | [diff] [blame] | 683 | std::unique_ptr<GrShape> fBase; |
| 684 | std::unique_ptr<GrShape> fAppliedPE; |
| 685 | std::unique_ptr<GrShape> fAppliedPEThenStroke; |
| 686 | std::unique_ptr<GrShape> fAppliedFull; |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 687 | |
| 688 | Key fBaseKey; |
| 689 | Key fAppliedPEKey; |
| 690 | Key fAppliedPEThenStrokeKey; |
| 691 | Key fAppliedFullKey; |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 692 | }; |
| 693 | |
| 694 | void TestCase::testExpectations(skiatest::Reporter* reporter, SelfExpectations expectations) const { |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 695 | // The base's key should always be valid (unless the path is volatile) |
bsalomon | 72dc51c | 2016-04-27 06:46:23 -0700 | [diff] [blame] | 696 | REPORTER_ASSERT(reporter, fBaseKey.count()); |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 697 | if (expectations.fPEHasEffect) { |
| 698 | REPORTER_ASSERT(reporter, fBaseKey != fAppliedPEKey); |
bsalomon | 72dc51c | 2016-04-27 06:46:23 -0700 | [diff] [blame] | 699 | REPORTER_ASSERT(reporter, expectations.fPEHasValidKey == SkToBool(fAppliedPEKey.count())); |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 700 | REPORTER_ASSERT(reporter, fBaseKey != fAppliedFullKey); |
bsalomon | 72dc51c | 2016-04-27 06:46:23 -0700 | [diff] [blame] | 701 | REPORTER_ASSERT(reporter, expectations.fPEHasValidKey == SkToBool(fAppliedFullKey.count())); |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 702 | if (expectations.fStrokeApplies && expectations.fPEHasValidKey) { |
| 703 | REPORTER_ASSERT(reporter, fAppliedPEKey != fAppliedFullKey); |
bsalomon | 72dc51c | 2016-04-27 06:46:23 -0700 | [diff] [blame] | 704 | REPORTER_ASSERT(reporter, SkToBool(fAppliedFullKey.count())); |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 705 | } |
| 706 | } else { |
| 707 | REPORTER_ASSERT(reporter, fBaseKey == fAppliedPEKey); |
bsalomon | fb08327 | 2016-05-04 08:27:41 -0700 | [diff] [blame] | 708 | SkPath a, b; |
Brian Salomon | c8cdad7 | 2018-04-16 09:46:09 -0400 | [diff] [blame] | 709 | fBase->asPath(&a); |
| 710 | fAppliedPE->asPath(&b); |
bsalomon | 72dc51c | 2016-04-27 06:46:23 -0700 | [diff] [blame] | 711 | REPORTER_ASSERT(reporter, a == b); |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 712 | if (expectations.fStrokeApplies) { |
| 713 | REPORTER_ASSERT(reporter, fBaseKey != fAppliedFullKey); |
| 714 | } else { |
| 715 | REPORTER_ASSERT(reporter, fBaseKey == fAppliedFullKey); |
| 716 | } |
| 717 | } |
| 718 | } |
| 719 | |
bsalomon | ee29564 | 2016-06-06 14:01:25 -0700 | [diff] [blame] | 720 | void TestCase::compare(skiatest::Reporter* r, const TestCase& that, |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 721 | ComparisonExpecation expectation) const { |
bsalomon | 72dc51c | 2016-04-27 06:46:23 -0700 | [diff] [blame] | 722 | SkPath a, b; |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 723 | switch (expectation) { |
| 724 | case kAllDifferent_ComparisonExpecation: |
bsalomon | ee29564 | 2016-06-06 14:01:25 -0700 | [diff] [blame] | 725 | REPORTER_ASSERT(r, fBaseKey != that.fBaseKey); |
| 726 | REPORTER_ASSERT(r, fAppliedPEKey != that.fAppliedPEKey); |
| 727 | REPORTER_ASSERT(r, fAppliedFullKey != that.fAppliedFullKey); |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 728 | break; |
| 729 | case kSameUpToPE_ComparisonExpecation: |
Brian Salomon | c8cdad7 | 2018-04-16 09:46:09 -0400 | [diff] [blame] | 730 | check_equivalence(r, *fBase, *that.fBase, fBaseKey, that.fBaseKey); |
bsalomon | ee29564 | 2016-06-06 14:01:25 -0700 | [diff] [blame] | 731 | REPORTER_ASSERT(r, fAppliedPEKey != that.fAppliedPEKey); |
| 732 | REPORTER_ASSERT(r, fAppliedFullKey != that.fAppliedFullKey); |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 733 | break; |
| 734 | case kSameUpToStroke_ComparisonExpecation: |
Brian Salomon | c8cdad7 | 2018-04-16 09:46:09 -0400 | [diff] [blame] | 735 | check_equivalence(r, *fBase, *that.fBase, fBaseKey, that.fBaseKey); |
| 736 | check_equivalence(r, *fAppliedPE, *that.fAppliedPE, fAppliedPEKey, that.fAppliedPEKey); |
bsalomon | ee29564 | 2016-06-06 14:01:25 -0700 | [diff] [blame] | 737 | REPORTER_ASSERT(r, fAppliedFullKey != that.fAppliedFullKey); |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 738 | break; |
| 739 | case kAllSame_ComparisonExpecation: |
Brian Salomon | c8cdad7 | 2018-04-16 09:46:09 -0400 | [diff] [blame] | 740 | check_equivalence(r, *fBase, *that.fBase, fBaseKey, that.fBaseKey); |
| 741 | check_equivalence(r, *fAppliedPE, *that.fAppliedPE, fAppliedPEKey, that.fAppliedPEKey); |
| 742 | check_equivalence(r, *fAppliedFull, *that.fAppliedFull, fAppliedFullKey, |
bsalomon | ee29564 | 2016-06-06 14:01:25 -0700 | [diff] [blame] | 743 | that.fAppliedFullKey); |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 744 | break; |
| 745 | } |
| 746 | } |
| 747 | } // namespace |
| 748 | |
| 749 | static sk_sp<SkPathEffect> make_dash() { |
| 750 | static const SkScalar kIntervals[] = { 0.25, 3.f, 0.5, 2.f }; |
| 751 | static const SkScalar kPhase = 0.75; |
| 752 | return SkDashPathEffect::Make(kIntervals, SK_ARRAY_COUNT(kIntervals), kPhase); |
| 753 | } |
| 754 | |
| 755 | static sk_sp<SkPathEffect> make_null_dash() { |
| 756 | static const SkScalar kNullIntervals[] = {0, 0, 0, 0, 0, 0}; |
| 757 | return SkDashPathEffect::Make(kNullIntervals, SK_ARRAY_COUNT(kNullIntervals), 0.f); |
| 758 | } |
| 759 | |
Mike Klein | 4334428 | 2017-08-16 11:56:22 -0400 | [diff] [blame] | 760 | // We make enough TestCases, and they're large enough, that on Google3 builds we exceed |
| 761 | // the maximum stack frame limit. make_TestCase() moves those temporaries over to the heap. |
| 762 | template <typename... Args> |
| 763 | static std::unique_ptr<TestCase> make_TestCase(Args&&... args) { |
| 764 | return std::unique_ptr<TestCase>{ new TestCase(std::forward<Args>(args)...) }; |
| 765 | } |
| 766 | |
bsalomon | a395f7c | 2016-08-24 17:47:40 -0700 | [diff] [blame] | 767 | static void test_basic(skiatest::Reporter* reporter, const Geo& geo) { |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 768 | sk_sp<SkPathEffect> dashPE = make_dash(); |
| 769 | |
| 770 | TestCase::SelfExpectations expectations; |
| 771 | SkPaint fill; |
| 772 | |
bsalomon | fb08327 | 2016-05-04 08:27:41 -0700 | [diff] [blame] | 773 | TestCase fillCase(geo, fill, reporter); |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 774 | expectations.fPEHasEffect = false; |
| 775 | expectations.fPEHasValidKey = false; |
| 776 | expectations.fStrokeApplies = false; |
| 777 | fillCase.testExpectations(reporter, expectations); |
| 778 | // Test that another GrShape instance built from the same primitive is the same. |
Mike Klein | 4334428 | 2017-08-16 11:56:22 -0400 | [diff] [blame] | 779 | make_TestCase(geo, fill, reporter) |
| 780 | ->compare(reporter, fillCase, TestCase::kAllSame_ComparisonExpecation); |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 781 | |
| 782 | SkPaint stroke2RoundBevel; |
| 783 | stroke2RoundBevel.setStyle(SkPaint::kStroke_Style); |
| 784 | stroke2RoundBevel.setStrokeCap(SkPaint::kRound_Cap); |
| 785 | stroke2RoundBevel.setStrokeJoin(SkPaint::kBevel_Join); |
| 786 | stroke2RoundBevel.setStrokeWidth(2.f); |
bsalomon | fb08327 | 2016-05-04 08:27:41 -0700 | [diff] [blame] | 787 | TestCase stroke2RoundBevelCase(geo, stroke2RoundBevel, reporter); |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 788 | expectations.fPEHasValidKey = true; |
| 789 | expectations.fPEHasEffect = false; |
bsalomon | a395f7c | 2016-08-24 17:47:40 -0700 | [diff] [blame] | 790 | expectations.fStrokeApplies = !geo.strokeIsConvertedToFill(); |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 791 | stroke2RoundBevelCase.testExpectations(reporter, expectations); |
Mike Klein | 4334428 | 2017-08-16 11:56:22 -0400 | [diff] [blame] | 792 | make_TestCase(geo, stroke2RoundBevel, reporter) |
| 793 | ->compare(reporter, stroke2RoundBevelCase, TestCase::kAllSame_ComparisonExpecation); |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 794 | |
| 795 | SkPaint stroke2RoundBevelDash = stroke2RoundBevel; |
| 796 | stroke2RoundBevelDash.setPathEffect(make_dash()); |
bsalomon | fb08327 | 2016-05-04 08:27:41 -0700 | [diff] [blame] | 797 | TestCase stroke2RoundBevelDashCase(geo, stroke2RoundBevelDash, reporter); |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 798 | expectations.fPEHasValidKey = true; |
| 799 | expectations.fPEHasEffect = true; |
| 800 | expectations.fStrokeApplies = true; |
| 801 | stroke2RoundBevelDashCase.testExpectations(reporter, expectations); |
Mike Klein | 4334428 | 2017-08-16 11:56:22 -0400 | [diff] [blame] | 802 | make_TestCase(geo, stroke2RoundBevelDash, reporter) |
| 803 | ->compare(reporter, stroke2RoundBevelDashCase, TestCase::kAllSame_ComparisonExpecation); |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 804 | |
bsalomon | a395f7c | 2016-08-24 17:47:40 -0700 | [diff] [blame] | 805 | if (geo.fillChangesGeom() || geo.strokeIsConvertedToFill()) { |
bsalomon | 487f8d3 | 2016-07-20 07:15:44 -0700 | [diff] [blame] | 806 | fillCase.compare(reporter, stroke2RoundBevelCase, |
| 807 | TestCase::kAllDifferent_ComparisonExpecation); |
| 808 | fillCase.compare(reporter, stroke2RoundBevelDashCase, |
| 809 | TestCase::kAllDifferent_ComparisonExpecation); |
| 810 | } else { |
| 811 | fillCase.compare(reporter, stroke2RoundBevelCase, |
| 812 | TestCase::kSameUpToStroke_ComparisonExpecation); |
| 813 | fillCase.compare(reporter, stroke2RoundBevelDashCase, |
| 814 | TestCase::kSameUpToPE_ComparisonExpecation); |
| 815 | } |
bsalomon | a395f7c | 2016-08-24 17:47:40 -0700 | [diff] [blame] | 816 | if (geo.strokeIsConvertedToFill()) { |
bsalomon | 487f8d3 | 2016-07-20 07:15:44 -0700 | [diff] [blame] | 817 | stroke2RoundBevelCase.compare(reporter, stroke2RoundBevelDashCase, |
| 818 | TestCase::kAllDifferent_ComparisonExpecation); |
| 819 | } else { |
| 820 | stroke2RoundBevelCase.compare(reporter, stroke2RoundBevelDashCase, |
| 821 | TestCase::kSameUpToPE_ComparisonExpecation); |
| 822 | } |
bsalomon | 72dc51c | 2016-04-27 06:46:23 -0700 | [diff] [blame] | 823 | |
bsalomon | f0cf355 | 2016-05-05 08:28:30 -0700 | [diff] [blame] | 824 | // Stroke and fill cases |
| 825 | SkPaint stroke2RoundBevelAndFill = stroke2RoundBevel; |
| 826 | stroke2RoundBevelAndFill.setStyle(SkPaint::kStrokeAndFill_Style); |
| 827 | TestCase stroke2RoundBevelAndFillCase(geo, stroke2RoundBevelAndFill, reporter); |
| 828 | expectations.fPEHasValidKey = true; |
| 829 | expectations.fPEHasEffect = false; |
bsalomon | a395f7c | 2016-08-24 17:47:40 -0700 | [diff] [blame] | 830 | expectations.fStrokeApplies = !geo.strokeIsConvertedToFill(); |
bsalomon | f0cf355 | 2016-05-05 08:28:30 -0700 | [diff] [blame] | 831 | stroke2RoundBevelAndFillCase.testExpectations(reporter, expectations); |
Mike Klein | 4334428 | 2017-08-16 11:56:22 -0400 | [diff] [blame] | 832 | make_TestCase(geo, stroke2RoundBevelAndFill, reporter)->compare( |
| 833 | reporter, stroke2RoundBevelAndFillCase, TestCase::kAllSame_ComparisonExpecation); |
bsalomon | f0cf355 | 2016-05-05 08:28:30 -0700 | [diff] [blame] | 834 | |
| 835 | SkPaint stroke2RoundBevelAndFillDash = stroke2RoundBevelDash; |
| 836 | stroke2RoundBevelAndFillDash.setStyle(SkPaint::kStrokeAndFill_Style); |
| 837 | TestCase stroke2RoundBevelAndFillDashCase(geo, stroke2RoundBevelAndFillDash, reporter); |
| 838 | expectations.fPEHasValidKey = true; |
bsalomon | a058786 | 2016-06-09 06:03:38 -0700 | [diff] [blame] | 839 | expectations.fPEHasEffect = false; |
bsalomon | a395f7c | 2016-08-24 17:47:40 -0700 | [diff] [blame] | 840 | expectations.fStrokeApplies = !geo.strokeIsConvertedToFill(); |
bsalomon | f0cf355 | 2016-05-05 08:28:30 -0700 | [diff] [blame] | 841 | stroke2RoundBevelAndFillDashCase.testExpectations(reporter, expectations); |
Mike Klein | 4334428 | 2017-08-16 11:56:22 -0400 | [diff] [blame] | 842 | make_TestCase(geo, stroke2RoundBevelAndFillDash, reporter)->compare( |
bsalomon | f0cf355 | 2016-05-05 08:28:30 -0700 | [diff] [blame] | 843 | reporter, stroke2RoundBevelAndFillDashCase, TestCase::kAllSame_ComparisonExpecation); |
bsalomon | a058786 | 2016-06-09 06:03:38 -0700 | [diff] [blame] | 844 | stroke2RoundBevelAndFillDashCase.compare(reporter, stroke2RoundBevelAndFillCase, |
| 845 | TestCase::kAllSame_ComparisonExpecation); |
bsalomon | f0cf355 | 2016-05-05 08:28:30 -0700 | [diff] [blame] | 846 | |
bsalomon | 72dc51c | 2016-04-27 06:46:23 -0700 | [diff] [blame] | 847 | SkPaint hairline; |
| 848 | hairline.setStyle(SkPaint::kStroke_Style); |
| 849 | hairline.setStrokeWidth(0.f); |
bsalomon | fb08327 | 2016-05-04 08:27:41 -0700 | [diff] [blame] | 850 | TestCase hairlineCase(geo, hairline, reporter); |
bsalomon | 487f8d3 | 2016-07-20 07:15:44 -0700 | [diff] [blame] | 851 | // Since hairline style doesn't change the SkPath data, it is keyed identically to fill (except |
| 852 | // in the line and unclosed rect cases). |
bsalomon | a395f7c | 2016-08-24 17:47:40 -0700 | [diff] [blame] | 853 | if (geo.fillChangesGeom()) { |
bsalomon | 487f8d3 | 2016-07-20 07:15:44 -0700 | [diff] [blame] | 854 | hairlineCase.compare(reporter, fillCase, TestCase::kAllDifferent_ComparisonExpecation); |
| 855 | } else { |
| 856 | hairlineCase.compare(reporter, fillCase, TestCase::kAllSame_ComparisonExpecation); |
| 857 | } |
bsalomon | 9ad5d7c | 2016-05-04 08:44:15 -0700 | [diff] [blame] | 858 | REPORTER_ASSERT(reporter, hairlineCase.baseShape().style().isSimpleHairline()); |
| 859 | REPORTER_ASSERT(reporter, hairlineCase.appliedFullStyleShape().style().isSimpleHairline()); |
| 860 | REPORTER_ASSERT(reporter, hairlineCase.appliedPathEffectShape().style().isSimpleHairline()); |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 861 | |
bsalomon | 0ae36a2 | 2016-07-18 07:31:13 -0700 | [diff] [blame] | 862 | } |
| 863 | |
bsalomon | a395f7c | 2016-08-24 17:47:40 -0700 | [diff] [blame] | 864 | static void test_scale(skiatest::Reporter* reporter, const Geo& geo) { |
bsalomon | 97fd2d4 | 2016-05-09 13:02:01 -0700 | [diff] [blame] | 865 | sk_sp<SkPathEffect> dashPE = make_dash(); |
| 866 | |
| 867 | static const SkScalar kS1 = 1.f; |
| 868 | static const SkScalar kS2 = 2.f; |
| 869 | |
| 870 | SkPaint fill; |
| 871 | TestCase fillCase1(geo, fill, reporter, kS1); |
| 872 | TestCase fillCase2(geo, fill, reporter, kS2); |
| 873 | // Scale doesn't affect fills. |
| 874 | fillCase1.compare(reporter, fillCase2, TestCase::kAllSame_ComparisonExpecation); |
| 875 | |
| 876 | SkPaint hairline; |
| 877 | hairline.setStyle(SkPaint::kStroke_Style); |
| 878 | hairline.setStrokeWidth(0.f); |
| 879 | TestCase hairlineCase1(geo, hairline, reporter, kS1); |
| 880 | TestCase hairlineCase2(geo, hairline, reporter, kS2); |
| 881 | // Scale doesn't affect hairlines. |
| 882 | hairlineCase1.compare(reporter, hairlineCase2, TestCase::kAllSame_ComparisonExpecation); |
| 883 | |
| 884 | SkPaint stroke; |
| 885 | stroke.setStyle(SkPaint::kStroke_Style); |
| 886 | stroke.setStrokeWidth(2.f); |
| 887 | TestCase strokeCase1(geo, stroke, reporter, kS1); |
| 888 | TestCase strokeCase2(geo, stroke, reporter, kS2); |
bsalomon | 0ae36a2 | 2016-07-18 07:31:13 -0700 | [diff] [blame] | 889 | // Scale affects the stroke |
bsalomon | a395f7c | 2016-08-24 17:47:40 -0700 | [diff] [blame] | 890 | if (geo.strokeIsConvertedToFill()) { |
bsalomon | 487f8d3 | 2016-07-20 07:15:44 -0700 | [diff] [blame] | 891 | REPORTER_ASSERT(reporter, !strokeCase1.baseShape().style().applies()); |
bsalomon | 0ae36a2 | 2016-07-18 07:31:13 -0700 | [diff] [blame] | 892 | strokeCase1.compare(reporter, strokeCase2, TestCase::kAllSame_ComparisonExpecation); |
| 893 | } else { |
| 894 | strokeCase1.compare(reporter, strokeCase2, TestCase::kSameUpToStroke_ComparisonExpecation); |
| 895 | } |
bsalomon | 97fd2d4 | 2016-05-09 13:02:01 -0700 | [diff] [blame] | 896 | |
| 897 | SkPaint strokeDash = stroke; |
| 898 | strokeDash.setPathEffect(make_dash()); |
| 899 | TestCase strokeDashCase1(geo, strokeDash, reporter, kS1); |
| 900 | TestCase strokeDashCase2(geo, strokeDash, reporter, kS2); |
| 901 | // Scale affects the dash and the stroke. |
bsalomon | 487f8d3 | 2016-07-20 07:15:44 -0700 | [diff] [blame] | 902 | strokeDashCase1.compare(reporter, strokeDashCase2, |
| 903 | TestCase::kSameUpToPE_ComparisonExpecation); |
bsalomon | 97fd2d4 | 2016-05-09 13:02:01 -0700 | [diff] [blame] | 904 | |
| 905 | // Stroke and fill cases |
| 906 | SkPaint strokeAndFill = stroke; |
| 907 | strokeAndFill.setStyle(SkPaint::kStrokeAndFill_Style); |
| 908 | TestCase strokeAndFillCase1(geo, strokeAndFill, reporter, kS1); |
| 909 | TestCase strokeAndFillCase2(geo, strokeAndFill, reporter, kS2); |
bsalomon | a058786 | 2016-06-09 06:03:38 -0700 | [diff] [blame] | 910 | SkPaint strokeAndFillDash = strokeDash; |
| 911 | strokeAndFillDash.setStyle(SkPaint::kStrokeAndFill_Style); |
| 912 | // Dash is ignored for stroke and fill |
| 913 | TestCase strokeAndFillDashCase1(geo, strokeAndFillDash, reporter, kS1); |
| 914 | TestCase strokeAndFillDashCase2(geo, strokeAndFillDash, reporter, kS2); |
bsalomon | 487f8d3 | 2016-07-20 07:15:44 -0700 | [diff] [blame] | 915 | // Scale affects the stroke, but check to make sure this didn't become a simpler shape (e.g. |
| 916 | // stroke-and-filled rect can become a rect), in which case the scale shouldn't matter and the |
| 917 | // geometries should agree. |
bsalomon | a395f7c | 2016-08-24 17:47:40 -0700 | [diff] [blame] | 918 | if (geo.strokeAndFillIsConvertedToFill(strokeAndFillDash)) { |
bsalomon | 487f8d3 | 2016-07-20 07:15:44 -0700 | [diff] [blame] | 919 | REPORTER_ASSERT(reporter, !strokeAndFillCase1.baseShape().style().applies()); |
bsalomon | 97fd2d4 | 2016-05-09 13:02:01 -0700 | [diff] [blame] | 920 | strokeAndFillCase1.compare(reporter, strokeAndFillCase2, |
| 921 | TestCase::kAllSame_ComparisonExpecation); |
bsalomon | 0ae36a2 | 2016-07-18 07:31:13 -0700 | [diff] [blame] | 922 | strokeAndFillDashCase1.compare(reporter, strokeAndFillDashCase2, |
| 923 | TestCase::kAllSame_ComparisonExpecation); |
bsalomon | 97fd2d4 | 2016-05-09 13:02:01 -0700 | [diff] [blame] | 924 | } else { |
| 925 | strokeAndFillCase1.compare(reporter, strokeAndFillCase2, |
| 926 | TestCase::kSameUpToStroke_ComparisonExpecation); |
| 927 | } |
bsalomon | a058786 | 2016-06-09 06:03:38 -0700 | [diff] [blame] | 928 | strokeAndFillDashCase1.compare(reporter, strokeAndFillCase1, |
| 929 | TestCase::kAllSame_ComparisonExpecation); |
| 930 | strokeAndFillDashCase2.compare(reporter, strokeAndFillCase2, |
| 931 | TestCase::kAllSame_ComparisonExpecation); |
bsalomon | 97fd2d4 | 2016-05-09 13:02:01 -0700 | [diff] [blame] | 932 | } |
| 933 | |
bsalomon | a395f7c | 2016-08-24 17:47:40 -0700 | [diff] [blame] | 934 | template <typename T> |
| 935 | static void test_stroke_param_impl(skiatest::Reporter* reporter, const Geo& geo, |
bsalomon | 0607756 | 2016-05-04 13:50:29 -0700 | [diff] [blame] | 936 | std::function<void(SkPaint*, T)> setter, T a, T b, |
| 937 | bool paramAffectsStroke, |
| 938 | bool paramAffectsDashAndStroke) { |
| 939 | // Set the stroke width so that we don't get hairline. However, call the setter afterward so |
| 940 | // that it can override the stroke width. |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 941 | SkPaint strokeA; |
| 942 | strokeA.setStyle(SkPaint::kStroke_Style); |
| 943 | strokeA.setStrokeWidth(2.f); |
| 944 | setter(&strokeA, a); |
| 945 | SkPaint strokeB; |
| 946 | strokeB.setStyle(SkPaint::kStroke_Style); |
| 947 | strokeB.setStrokeWidth(2.f); |
| 948 | setter(&strokeB, b); |
| 949 | |
bsalomon | fb08327 | 2016-05-04 08:27:41 -0700 | [diff] [blame] | 950 | TestCase strokeACase(geo, strokeA, reporter); |
| 951 | TestCase strokeBCase(geo, strokeB, reporter); |
bsalomon | 0607756 | 2016-05-04 13:50:29 -0700 | [diff] [blame] | 952 | if (paramAffectsStroke) { |
bsalomon | 0ae36a2 | 2016-07-18 07:31:13 -0700 | [diff] [blame] | 953 | // If stroking is immediately incorporated into a geometric transformation then the base |
| 954 | // shapes will differ. |
bsalomon | a395f7c | 2016-08-24 17:47:40 -0700 | [diff] [blame] | 955 | if (geo.strokeIsConvertedToFill()) { |
bsalomon | 0ae36a2 | 2016-07-18 07:31:13 -0700 | [diff] [blame] | 956 | strokeACase.compare(reporter, strokeBCase, |
| 957 | TestCase::kAllDifferent_ComparisonExpecation); |
bsalomon | 487f8d3 | 2016-07-20 07:15:44 -0700 | [diff] [blame] | 958 | } else { |
| 959 | strokeACase.compare(reporter, strokeBCase, |
| 960 | TestCase::kSameUpToStroke_ComparisonExpecation); |
bsalomon | 0ae36a2 | 2016-07-18 07:31:13 -0700 | [diff] [blame] | 961 | } |
bsalomon | 0607756 | 2016-05-04 13:50:29 -0700 | [diff] [blame] | 962 | } else { |
| 963 | strokeACase.compare(reporter, strokeBCase, TestCase::kAllSame_ComparisonExpecation); |
| 964 | } |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 965 | |
bsalomon | f0cf355 | 2016-05-05 08:28:30 -0700 | [diff] [blame] | 966 | SkPaint strokeAndFillA = strokeA; |
| 967 | SkPaint strokeAndFillB = strokeB; |
| 968 | strokeAndFillA.setStyle(SkPaint::kStrokeAndFill_Style); |
| 969 | strokeAndFillB.setStyle(SkPaint::kStrokeAndFill_Style); |
| 970 | TestCase strokeAndFillACase(geo, strokeAndFillA, reporter); |
| 971 | TestCase strokeAndFillBCase(geo, strokeAndFillB, reporter); |
| 972 | if (paramAffectsStroke) { |
bsalomon | 0ae36a2 | 2016-07-18 07:31:13 -0700 | [diff] [blame] | 973 | // If stroking is immediately incorporated into a geometric transformation then the base |
| 974 | // shapes will differ. |
bsalomon | a395f7c | 2016-08-24 17:47:40 -0700 | [diff] [blame] | 975 | if (geo.strokeAndFillIsConvertedToFill(strokeAndFillA) || |
| 976 | geo.strokeAndFillIsConvertedToFill(strokeAndFillB)) { |
bsalomon | 0ae36a2 | 2016-07-18 07:31:13 -0700 | [diff] [blame] | 977 | strokeAndFillACase.compare(reporter, strokeAndFillBCase, |
bsalomon | 487f8d3 | 2016-07-20 07:15:44 -0700 | [diff] [blame] | 978 | TestCase::kAllDifferent_ComparisonExpecation); |
bsalomon | 0ae36a2 | 2016-07-18 07:31:13 -0700 | [diff] [blame] | 979 | } else { |
| 980 | strokeAndFillACase.compare(reporter, strokeAndFillBCase, |
bsalomon | 487f8d3 | 2016-07-20 07:15:44 -0700 | [diff] [blame] | 981 | TestCase::kSameUpToStroke_ComparisonExpecation); |
bsalomon | 0ae36a2 | 2016-07-18 07:31:13 -0700 | [diff] [blame] | 982 | } |
bsalomon | f0cf355 | 2016-05-05 08:28:30 -0700 | [diff] [blame] | 983 | } else { |
| 984 | strokeAndFillACase.compare(reporter, strokeAndFillBCase, |
| 985 | TestCase::kAllSame_ComparisonExpecation); |
| 986 | } |
| 987 | |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 988 | // Make sure stroking params don't affect fill style. |
| 989 | SkPaint fillA = strokeA, fillB = strokeB; |
| 990 | fillA.setStyle(SkPaint::kFill_Style); |
| 991 | fillB.setStyle(SkPaint::kFill_Style); |
bsalomon | fb08327 | 2016-05-04 08:27:41 -0700 | [diff] [blame] | 992 | TestCase fillACase(geo, fillA, reporter); |
| 993 | TestCase fillBCase(geo, fillB, reporter); |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 994 | fillACase.compare(reporter, fillBCase, TestCase::kAllSame_ComparisonExpecation); |
| 995 | |
| 996 | // Make sure just applying the dash but not stroke gives the same key for both stroking |
| 997 | // variations. |
| 998 | SkPaint dashA = strokeA, dashB = strokeB; |
| 999 | dashA.setPathEffect(make_dash()); |
| 1000 | dashB.setPathEffect(make_dash()); |
bsalomon | fb08327 | 2016-05-04 08:27:41 -0700 | [diff] [blame] | 1001 | TestCase dashACase(geo, dashA, reporter); |
| 1002 | TestCase dashBCase(geo, dashB, reporter); |
bsalomon | 0607756 | 2016-05-04 13:50:29 -0700 | [diff] [blame] | 1003 | if (paramAffectsDashAndStroke) { |
bsalomon | 487f8d3 | 2016-07-20 07:15:44 -0700 | [diff] [blame] | 1004 | dashACase.compare(reporter, dashBCase, TestCase::kSameUpToStroke_ComparisonExpecation); |
bsalomon | 0607756 | 2016-05-04 13:50:29 -0700 | [diff] [blame] | 1005 | } else { |
| 1006 | dashACase.compare(reporter, dashBCase, TestCase::kAllSame_ComparisonExpecation); |
| 1007 | } |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 1008 | } |
| 1009 | |
bsalomon | a395f7c | 2016-08-24 17:47:40 -0700 | [diff] [blame] | 1010 | template <typename T> |
| 1011 | static void test_stroke_param(skiatest::Reporter* reporter, const Geo& geo, |
bsalomon | 0607756 | 2016-05-04 13:50:29 -0700 | [diff] [blame] | 1012 | std::function<void(SkPaint*, T)> setter, T a, T b) { |
| 1013 | test_stroke_param_impl(reporter, geo, setter, a, b, true, true); |
| 1014 | }; |
| 1015 | |
bsalomon | a395f7c | 2016-08-24 17:47:40 -0700 | [diff] [blame] | 1016 | static void test_stroke_cap(skiatest::Reporter* reporter, const Geo& geo) { |
| 1017 | SkPaint hairline; |
| 1018 | hairline.setStrokeWidth(0); |
| 1019 | hairline.setStyle(SkPaint::kStroke_Style); |
| 1020 | GrShape shape = geo.makeShape(hairline); |
bsalomon | 0607756 | 2016-05-04 13:50:29 -0700 | [diff] [blame] | 1021 | // The cap should only affect shapes that may be open. |
| 1022 | bool affectsStroke = !shape.knownToBeClosed(); |
| 1023 | // Dashing adds ends that need caps. |
| 1024 | bool affectsDashAndStroke = true; |
bsalomon | a395f7c | 2016-08-24 17:47:40 -0700 | [diff] [blame] | 1025 | test_stroke_param_impl<SkPaint::Cap>( |
bsalomon | 0607756 | 2016-05-04 13:50:29 -0700 | [diff] [blame] | 1026 | reporter, |
| 1027 | geo, |
| 1028 | [](SkPaint* p, SkPaint::Cap c) { p->setStrokeCap(c);}, |
| 1029 | SkPaint::kButt_Cap, SkPaint::kRound_Cap, |
| 1030 | affectsStroke, |
| 1031 | affectsDashAndStroke); |
| 1032 | }; |
| 1033 | |
bsalomon | 0ae36a2 | 2016-07-18 07:31:13 -0700 | [diff] [blame] | 1034 | static bool shape_known_not_to_have_joins(const GrShape& shape) { |
| 1035 | return shape.asLine(nullptr, nullptr) || shape.isEmpty(); |
| 1036 | } |
| 1037 | |
bsalomon | a395f7c | 2016-08-24 17:47:40 -0700 | [diff] [blame] | 1038 | static void test_stroke_join(skiatest::Reporter* reporter, const Geo& geo) { |
| 1039 | SkPaint hairline; |
| 1040 | hairline.setStrokeWidth(0); |
| 1041 | hairline.setStyle(SkPaint::kStroke_Style); |
| 1042 | GrShape shape = geo.makeShape(hairline); |
bsalomon | 0ae36a2 | 2016-07-18 07:31:13 -0700 | [diff] [blame] | 1043 | // GrShape recognizes certain types don't have joins and will prevent the join type from |
| 1044 | // affecting the style key. |
| 1045 | // Dashing doesn't add additional joins. However, GrShape currently loses track of this |
| 1046 | // after applying the dash. |
| 1047 | bool affectsStroke = !shape_known_not_to_have_joins(shape); |
bsalomon | a395f7c | 2016-08-24 17:47:40 -0700 | [diff] [blame] | 1048 | test_stroke_param_impl<SkPaint::Join>( |
bsalomon | 0ae36a2 | 2016-07-18 07:31:13 -0700 | [diff] [blame] | 1049 | reporter, |
| 1050 | geo, |
| 1051 | [](SkPaint* p, SkPaint::Join j) { p->setStrokeJoin(j);}, |
| 1052 | SkPaint::kRound_Join, SkPaint::kBevel_Join, |
| 1053 | affectsStroke, true); |
| 1054 | }; |
| 1055 | |
bsalomon | a395f7c | 2016-08-24 17:47:40 -0700 | [diff] [blame] | 1056 | static void test_miter_limit(skiatest::Reporter* reporter, const Geo& geo) { |
bsalomon | 0607756 | 2016-05-04 13:50:29 -0700 | [diff] [blame] | 1057 | auto setMiterJoinAndLimit = [](SkPaint* p, SkScalar miter) { |
| 1058 | p->setStrokeJoin(SkPaint::kMiter_Join); |
| 1059 | p->setStrokeMiter(miter); |
| 1060 | }; |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 1061 | |
bsalomon | 0607756 | 2016-05-04 13:50:29 -0700 | [diff] [blame] | 1062 | auto setOtherJoinAndLimit = [](SkPaint* p, SkScalar miter) { |
| 1063 | p->setStrokeJoin(SkPaint::kRound_Join); |
| 1064 | p->setStrokeMiter(miter); |
| 1065 | }; |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 1066 | |
bsalomon | a395f7c | 2016-08-24 17:47:40 -0700 | [diff] [blame] | 1067 | SkPaint hairline; |
| 1068 | hairline.setStrokeWidth(0); |
| 1069 | hairline.setStyle(SkPaint::kStroke_Style); |
| 1070 | GrShape shape = geo.makeShape(hairline); |
bsalomon | 0ae36a2 | 2016-07-18 07:31:13 -0700 | [diff] [blame] | 1071 | bool mayHaveJoins = !shape_known_not_to_have_joins(shape); |
| 1072 | |
bsalomon | 0607756 | 2016-05-04 13:50:29 -0700 | [diff] [blame] | 1073 | // The miter limit should affect stroked and dashed-stroked cases when the join type is |
| 1074 | // miter. |
bsalomon | a395f7c | 2016-08-24 17:47:40 -0700 | [diff] [blame] | 1075 | test_stroke_param_impl<SkScalar>( |
bsalomon | 0607756 | 2016-05-04 13:50:29 -0700 | [diff] [blame] | 1076 | reporter, |
| 1077 | geo, |
| 1078 | setMiterJoinAndLimit, |
| 1079 | 0.5f, 0.75f, |
bsalomon | 0ae36a2 | 2016-07-18 07:31:13 -0700 | [diff] [blame] | 1080 | mayHaveJoins, |
bsalomon | 0607756 | 2016-05-04 13:50:29 -0700 | [diff] [blame] | 1081 | true); |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 1082 | |
bsalomon | 0607756 | 2016-05-04 13:50:29 -0700 | [diff] [blame] | 1083 | // The miter limit should not affect stroked and dashed-stroked cases when the join type is |
| 1084 | // not miter. |
bsalomon | a395f7c | 2016-08-24 17:47:40 -0700 | [diff] [blame] | 1085 | test_stroke_param_impl<SkScalar>( |
bsalomon | 0607756 | 2016-05-04 13:50:29 -0700 | [diff] [blame] | 1086 | reporter, |
| 1087 | geo, |
| 1088 | setOtherJoinAndLimit, |
| 1089 | 0.5f, 0.75f, |
| 1090 | false, |
| 1091 | false); |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 1092 | } |
| 1093 | |
bsalomon | a395f7c | 2016-08-24 17:47:40 -0700 | [diff] [blame] | 1094 | static void test_dash_fill(skiatest::Reporter* reporter, const Geo& geo) { |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 1095 | // A dash with no stroke should have no effect |
| 1096 | using DashFactoryFn = sk_sp<SkPathEffect>(*)(); |
| 1097 | for (DashFactoryFn md : {&make_dash, &make_null_dash}) { |
| 1098 | SkPaint dashFill; |
| 1099 | dashFill.setPathEffect((*md)()); |
bsalomon | fb08327 | 2016-05-04 08:27:41 -0700 | [diff] [blame] | 1100 | TestCase dashFillCase(geo, dashFill, reporter); |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 1101 | |
bsalomon | fb08327 | 2016-05-04 08:27:41 -0700 | [diff] [blame] | 1102 | TestCase fillCase(geo, SkPaint(), reporter); |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 1103 | dashFillCase.compare(reporter, fillCase, TestCase::kAllSame_ComparisonExpecation); |
| 1104 | } |
| 1105 | } |
| 1106 | |
bsalomon | a395f7c | 2016-08-24 17:47:40 -0700 | [diff] [blame] | 1107 | void test_null_dash(skiatest::Reporter* reporter, const Geo& geo) { |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 1108 | SkPaint fill; |
| 1109 | SkPaint stroke; |
| 1110 | stroke.setStyle(SkPaint::kStroke_Style); |
| 1111 | stroke.setStrokeWidth(1.f); |
| 1112 | SkPaint dash; |
| 1113 | dash.setStyle(SkPaint::kStroke_Style); |
| 1114 | dash.setStrokeWidth(1.f); |
| 1115 | dash.setPathEffect(make_dash()); |
| 1116 | SkPaint nullDash; |
| 1117 | nullDash.setStyle(SkPaint::kStroke_Style); |
| 1118 | nullDash.setStrokeWidth(1.f); |
| 1119 | nullDash.setPathEffect(make_null_dash()); |
| 1120 | |
bsalomon | fb08327 | 2016-05-04 08:27:41 -0700 | [diff] [blame] | 1121 | TestCase fillCase(geo, fill, reporter); |
| 1122 | TestCase strokeCase(geo, stroke, reporter); |
| 1123 | TestCase dashCase(geo, dash, reporter); |
| 1124 | TestCase nullDashCase(geo, nullDash, reporter); |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 1125 | |
bsalomon | 487f8d3 | 2016-07-20 07:15:44 -0700 | [diff] [blame] | 1126 | // We expect the null dash to be ignored so nullDashCase should match strokeCase, always. |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 1127 | nullDashCase.compare(reporter, strokeCase, TestCase::kAllSame_ComparisonExpecation); |
bsalomon | 487f8d3 | 2016-07-20 07:15:44 -0700 | [diff] [blame] | 1128 | // Check whether the fillCase or strokeCase/nullDashCase would undergo a geometric tranformation |
| 1129 | // on construction in order to determine how to compare the fill and stroke. |
bsalomon | a395f7c | 2016-08-24 17:47:40 -0700 | [diff] [blame] | 1130 | if (geo.fillChangesGeom() || geo.strokeIsConvertedToFill()) { |
bsalomon | 487f8d3 | 2016-07-20 07:15:44 -0700 | [diff] [blame] | 1131 | nullDashCase.compare(reporter, fillCase, TestCase::kAllDifferent_ComparisonExpecation); |
| 1132 | } else { |
| 1133 | nullDashCase.compare(reporter, fillCase, TestCase::kSameUpToStroke_ComparisonExpecation); |
| 1134 | } |
| 1135 | // In the null dash case we may immediately convert to a fill, but not for the normal dash case. |
bsalomon | a395f7c | 2016-08-24 17:47:40 -0700 | [diff] [blame] | 1136 | if (geo.strokeIsConvertedToFill()) { |
bsalomon | 487f8d3 | 2016-07-20 07:15:44 -0700 | [diff] [blame] | 1137 | nullDashCase.compare(reporter, dashCase, TestCase::kAllDifferent_ComparisonExpecation); |
| 1138 | } else { |
| 1139 | nullDashCase.compare(reporter, dashCase, TestCase::kSameUpToPE_ComparisonExpecation); |
| 1140 | } |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 1141 | } |
| 1142 | |
bsalomon | a395f7c | 2016-08-24 17:47:40 -0700 | [diff] [blame] | 1143 | void test_path_effect_makes_rrect(skiatest::Reporter* reporter, const Geo& geo) { |
bsalomon | 72dc51c | 2016-04-27 06:46:23 -0700 | [diff] [blame] | 1144 | /** |
| 1145 | * This path effect takes any input path and turns it into a rrect. It passes through stroke |
| 1146 | * info. |
| 1147 | */ |
| 1148 | class RRectPathEffect : SkPathEffect { |
| 1149 | public: |
| 1150 | static const SkRRect& RRect() { |
| 1151 | static const SkRRect kRRect = SkRRect::MakeRectXY(SkRect::MakeWH(12, 12), 3, 5); |
| 1152 | return kRRect; |
| 1153 | } |
| 1154 | |
| 1155 | bool filterPath(SkPath* dst, const SkPath& src, SkStrokeRec*, |
| 1156 | const SkRect* cullR) const override { |
| 1157 | dst->reset(); |
| 1158 | dst->addRRect(RRect()); |
| 1159 | return true; |
| 1160 | } |
| 1161 | void computeFastBounds(SkRect* dst, const SkRect& src) const override { |
| 1162 | *dst = RRect().getBounds(); |
| 1163 | } |
| 1164 | static sk_sp<SkPathEffect> Make() { return sk_sp<SkPathEffect>(new RRectPathEffect); } |
| 1165 | Factory getFactory() const override { return nullptr; } |
bsalomon | 72dc51c | 2016-04-27 06:46:23 -0700 | [diff] [blame] | 1166 | private: |
| 1167 | RRectPathEffect() {} |
| 1168 | }; |
| 1169 | |
| 1170 | SkPaint fill; |
bsalomon | fb08327 | 2016-05-04 08:27:41 -0700 | [diff] [blame] | 1171 | TestCase fillGeoCase(geo, fill, reporter); |
bsalomon | 72dc51c | 2016-04-27 06:46:23 -0700 | [diff] [blame] | 1172 | |
| 1173 | SkPaint pe; |
| 1174 | pe.setPathEffect(RRectPathEffect::Make()); |
bsalomon | fb08327 | 2016-05-04 08:27:41 -0700 | [diff] [blame] | 1175 | TestCase geoPECase(geo, pe, reporter); |
bsalomon | 72dc51c | 2016-04-27 06:46:23 -0700 | [diff] [blame] | 1176 | |
| 1177 | SkPaint peStroke; |
| 1178 | peStroke.setPathEffect(RRectPathEffect::Make()); |
| 1179 | peStroke.setStrokeWidth(2.f); |
| 1180 | peStroke.setStyle(SkPaint::kStroke_Style); |
bsalomon | fb08327 | 2016-05-04 08:27:41 -0700 | [diff] [blame] | 1181 | TestCase geoPEStrokeCase(geo, peStroke, reporter); |
bsalomon | 72dc51c | 2016-04-27 06:46:23 -0700 | [diff] [blame] | 1182 | |
bsalomon | 487f8d3 | 2016-07-20 07:15:44 -0700 | [diff] [blame] | 1183 | // Check whether constructing the filled case would cause the base shape to have a different |
| 1184 | // geometry (because of a geometric transformation upon initial GrShape construction). |
bsalomon | a395f7c | 2016-08-24 17:47:40 -0700 | [diff] [blame] | 1185 | if (geo.fillChangesGeom()) { |
bsalomon | 487f8d3 | 2016-07-20 07:15:44 -0700 | [diff] [blame] | 1186 | fillGeoCase.compare(reporter, geoPECase, TestCase::kAllDifferent_ComparisonExpecation); |
| 1187 | fillGeoCase.compare(reporter, geoPEStrokeCase, |
| 1188 | TestCase::kAllDifferent_ComparisonExpecation); |
| 1189 | } else { |
| 1190 | fillGeoCase.compare(reporter, geoPECase, TestCase::kSameUpToPE_ComparisonExpecation); |
| 1191 | fillGeoCase.compare(reporter, geoPEStrokeCase, TestCase::kSameUpToPE_ComparisonExpecation); |
| 1192 | } |
bsalomon | 72dc51c | 2016-04-27 06:46:23 -0700 | [diff] [blame] | 1193 | geoPECase.compare(reporter, geoPEStrokeCase, |
| 1194 | TestCase::kSameUpToStroke_ComparisonExpecation); |
| 1195 | |
bsalomon | a395f7c | 2016-08-24 17:47:40 -0700 | [diff] [blame] | 1196 | TestCase rrectFillCase(reporter, RRectPathEffect::RRect(), fill); |
bsalomon | 72dc51c | 2016-04-27 06:46:23 -0700 | [diff] [blame] | 1197 | SkPaint stroke = peStroke; |
| 1198 | stroke.setPathEffect(nullptr); |
bsalomon | a395f7c | 2016-08-24 17:47:40 -0700 | [diff] [blame] | 1199 | TestCase rrectStrokeCase(reporter, RRectPathEffect::RRect(), stroke); |
bsalomon | 72dc51c | 2016-04-27 06:46:23 -0700 | [diff] [blame] | 1200 | |
| 1201 | SkRRect rrect; |
| 1202 | // Applying the path effect should make a SkRRect shape. There is no further stroking in the |
| 1203 | // geoPECase, so the full style should be the same as just the PE. |
bsalomon | 7049396 | 2016-06-10 08:05:14 -0700 | [diff] [blame] | 1204 | REPORTER_ASSERT(reporter, geoPECase.appliedPathEffectShape().asRRect(&rrect, nullptr, nullptr, |
| 1205 | nullptr)); |
bsalomon | 72dc51c | 2016-04-27 06:46:23 -0700 | [diff] [blame] | 1206 | REPORTER_ASSERT(reporter, rrect == RRectPathEffect::RRect()); |
| 1207 | REPORTER_ASSERT(reporter, geoPECase.appliedPathEffectKey() == rrectFillCase.baseKey()); |
| 1208 | |
bsalomon | 7049396 | 2016-06-10 08:05:14 -0700 | [diff] [blame] | 1209 | REPORTER_ASSERT(reporter, geoPECase.appliedFullStyleShape().asRRect(&rrect, nullptr, nullptr, |
| 1210 | nullptr)); |
bsalomon | 72dc51c | 2016-04-27 06:46:23 -0700 | [diff] [blame] | 1211 | REPORTER_ASSERT(reporter, rrect == RRectPathEffect::RRect()); |
| 1212 | REPORTER_ASSERT(reporter, geoPECase.appliedFullStyleKey() == rrectFillCase.baseKey()); |
| 1213 | |
| 1214 | // In the PE+stroke case applying the full style should be the same as just stroking the rrect. |
bsalomon | 7049396 | 2016-06-10 08:05:14 -0700 | [diff] [blame] | 1215 | REPORTER_ASSERT(reporter, geoPEStrokeCase.appliedPathEffectShape().asRRect(&rrect, nullptr, |
| 1216 | nullptr, nullptr)); |
bsalomon | 72dc51c | 2016-04-27 06:46:23 -0700 | [diff] [blame] | 1217 | REPORTER_ASSERT(reporter, rrect == RRectPathEffect::RRect()); |
| 1218 | REPORTER_ASSERT(reporter, geoPEStrokeCase.appliedPathEffectKey() == rrectFillCase.baseKey()); |
| 1219 | |
bsalomon | 7049396 | 2016-06-10 08:05:14 -0700 | [diff] [blame] | 1220 | REPORTER_ASSERT(reporter, !geoPEStrokeCase.appliedFullStyleShape().asRRect(&rrect, nullptr, |
| 1221 | nullptr, nullptr)); |
bsalomon | 72dc51c | 2016-04-27 06:46:23 -0700 | [diff] [blame] | 1222 | REPORTER_ASSERT(reporter, geoPEStrokeCase.appliedFullStyleKey() == |
| 1223 | rrectStrokeCase.appliedFullStyleKey()); |
| 1224 | } |
| 1225 | |
bsalomon | a395f7c | 2016-08-24 17:47:40 -0700 | [diff] [blame] | 1226 | void test_unknown_path_effect(skiatest::Reporter* reporter, const Geo& geo) { |
bsalomon | 72dc51c | 2016-04-27 06:46:23 -0700 | [diff] [blame] | 1227 | /** |
| 1228 | * This path effect just adds two lineTos to the input path. |
| 1229 | */ |
| 1230 | class AddLineTosPathEffect : SkPathEffect { |
| 1231 | public: |
| 1232 | bool filterPath(SkPath* dst, const SkPath& src, SkStrokeRec*, |
| 1233 | const SkRect* cullR) const override { |
| 1234 | *dst = src; |
bsalomon | 67fa4e3 | 2016-09-21 08:26:57 -0700 | [diff] [blame] | 1235 | // To avoid triggering data-based keying of paths with few verbs we add many segments. |
| 1236 | for (int i = 0; i < 100; ++i) { |
| 1237 | dst->lineTo(SkIntToScalar(i), SkIntToScalar(i)); |
| 1238 | } |
bsalomon | 72dc51c | 2016-04-27 06:46:23 -0700 | [diff] [blame] | 1239 | return true; |
| 1240 | } |
| 1241 | void computeFastBounds(SkRect* dst, const SkRect& src) const override { |
| 1242 | *dst = src; |
Mike Reed | 185ffe9 | 2018-01-08 17:09:54 -0500 | [diff] [blame] | 1243 | SkRectPriv::GrowToInclude(dst, {0, 0}); |
| 1244 | SkRectPriv::GrowToInclude(dst, {100, 100}); |
bsalomon | 72dc51c | 2016-04-27 06:46:23 -0700 | [diff] [blame] | 1245 | } |
| 1246 | static sk_sp<SkPathEffect> Make() { return sk_sp<SkPathEffect>(new AddLineTosPathEffect); } |
| 1247 | Factory getFactory() const override { return nullptr; } |
bsalomon | 72dc51c | 2016-04-27 06:46:23 -0700 | [diff] [blame] | 1248 | private: |
| 1249 | AddLineTosPathEffect() {} |
| 1250 | }; |
| 1251 | |
bsalomon | 9ad5d7c | 2016-05-04 08:44:15 -0700 | [diff] [blame] | 1252 | // This path effect should make the keys invalid when it is applied. We only produce a path |
bsalomon | 72dc51c | 2016-04-27 06:46:23 -0700 | [diff] [blame] | 1253 | // effect key for dash path effects. So the only way another arbitrary path effect can produce |
| 1254 | // a styled result with a key is to produce a non-path shape that has a purely geometric key. |
| 1255 | SkPaint peStroke; |
| 1256 | peStroke.setPathEffect(AddLineTosPathEffect::Make()); |
| 1257 | peStroke.setStrokeWidth(2.f); |
| 1258 | peStroke.setStyle(SkPaint::kStroke_Style); |
bsalomon | fb08327 | 2016-05-04 08:27:41 -0700 | [diff] [blame] | 1259 | TestCase geoPEStrokeCase(geo, peStroke, reporter); |
bsalomon | 72dc51c | 2016-04-27 06:46:23 -0700 | [diff] [blame] | 1260 | TestCase::SelfExpectations expectations; |
| 1261 | expectations.fPEHasEffect = true; |
| 1262 | expectations.fPEHasValidKey = false; |
| 1263 | expectations.fStrokeApplies = true; |
| 1264 | geoPEStrokeCase.testExpectations(reporter, expectations); |
| 1265 | } |
| 1266 | |
bsalomon | a395f7c | 2016-08-24 17:47:40 -0700 | [diff] [blame] | 1267 | void test_make_hairline_path_effect(skiatest::Reporter* reporter, const Geo& geo) { |
bsalomon | 9ad5d7c | 2016-05-04 08:44:15 -0700 | [diff] [blame] | 1268 | /** |
| 1269 | * This path effect just changes the stroke rec to hairline. |
| 1270 | */ |
| 1271 | class MakeHairlinePathEffect : SkPathEffect { |
| 1272 | public: |
| 1273 | bool filterPath(SkPath* dst, const SkPath& src, SkStrokeRec* strokeRec, |
| 1274 | const SkRect* cullR) const override { |
| 1275 | *dst = src; |
| 1276 | strokeRec->setHairlineStyle(); |
| 1277 | return true; |
| 1278 | } |
bsalomon | 7049396 | 2016-06-10 08:05:14 -0700 | [diff] [blame] | 1279 | void computeFastBounds(SkRect* dst, const SkRect& src) const override { *dst = src; } |
bsalomon | 9ad5d7c | 2016-05-04 08:44:15 -0700 | [diff] [blame] | 1280 | static sk_sp<SkPathEffect> Make() { |
| 1281 | return sk_sp<SkPathEffect>(new MakeHairlinePathEffect); |
| 1282 | } |
| 1283 | Factory getFactory() const override { return nullptr; } |
bsalomon | 9ad5d7c | 2016-05-04 08:44:15 -0700 | [diff] [blame] | 1284 | private: |
| 1285 | MakeHairlinePathEffect() {} |
| 1286 | }; |
| 1287 | |
| 1288 | SkPaint fill; |
| 1289 | SkPaint pe; |
| 1290 | pe.setPathEffect(MakeHairlinePathEffect::Make()); |
| 1291 | |
| 1292 | TestCase peCase(geo, pe, reporter); |
| 1293 | |
bsalomon | ee29564 | 2016-06-06 14:01:25 -0700 | [diff] [blame] | 1294 | SkPath a, b, c; |
bsalomon | 9ad5d7c | 2016-05-04 08:44:15 -0700 | [diff] [blame] | 1295 | peCase.baseShape().asPath(&a); |
| 1296 | peCase.appliedPathEffectShape().asPath(&b); |
bsalomon | ee29564 | 2016-06-06 14:01:25 -0700 | [diff] [blame] | 1297 | peCase.appliedFullStyleShape().asPath(&c); |
bsalomon | a395f7c | 2016-08-24 17:47:40 -0700 | [diff] [blame] | 1298 | if (geo.isNonPath(pe)) { |
bsalomon | ee29564 | 2016-06-06 14:01:25 -0700 | [diff] [blame] | 1299 | // RRect types can have a change in start index or direction after the PE is applied. This |
| 1300 | // is because once the PE is applied, GrShape may canonicalize the dir and index since it |
| 1301 | // is not germane to the styling any longer. |
| 1302 | // Instead we just check that the paths would fill the same both before and after styling. |
| 1303 | REPORTER_ASSERT(reporter, paths_fill_same(a, b)); |
| 1304 | REPORTER_ASSERT(reporter, paths_fill_same(a, c)); |
bsalomon | 9ad5d7c | 2016-05-04 08:44:15 -0700 | [diff] [blame] | 1305 | } else { |
bsalomon | a4817af | 2016-06-23 11:48:26 -0700 | [diff] [blame] | 1306 | // The base shape cannot perform canonicalization on the path's fill type because of an |
| 1307 | // unknown path effect. However, after the path effect is applied the resulting hairline |
| 1308 | // shape will canonicalize the path fill type since hairlines (and stroking in general) |
| 1309 | // don't distinguish between even/odd and non-zero winding. |
| 1310 | a.setFillType(b.getFillType()); |
bsalomon | ee29564 | 2016-06-06 14:01:25 -0700 | [diff] [blame] | 1311 | REPORTER_ASSERT(reporter, a == b); |
| 1312 | REPORTER_ASSERT(reporter, a == c); |
bsalomon | 67fa4e3 | 2016-09-21 08:26:57 -0700 | [diff] [blame] | 1313 | // If the resulting path is small enough then it will have a key. |
| 1314 | REPORTER_ASSERT(reporter, paths_fill_same(a, b)); |
| 1315 | REPORTER_ASSERT(reporter, paths_fill_same(a, c)); |
bsalomon | aa84064 | 2016-09-23 12:09:16 -0700 | [diff] [blame] | 1316 | REPORTER_ASSERT(reporter, peCase.appliedPathEffectKey().empty()); |
| 1317 | REPORTER_ASSERT(reporter, peCase.appliedFullStyleKey().empty()); |
bsalomon | 9ad5d7c | 2016-05-04 08:44:15 -0700 | [diff] [blame] | 1318 | } |
bsalomon | ee29564 | 2016-06-06 14:01:25 -0700 | [diff] [blame] | 1319 | REPORTER_ASSERT(reporter, peCase.appliedPathEffectShape().style().isSimpleHairline()); |
| 1320 | REPORTER_ASSERT(reporter, peCase.appliedFullStyleShape().style().isSimpleHairline()); |
bsalomon | 9ad5d7c | 2016-05-04 08:44:15 -0700 | [diff] [blame] | 1321 | } |
| 1322 | |
bsalomon | a395f7c | 2016-08-24 17:47:40 -0700 | [diff] [blame] | 1323 | void test_volatile_path(skiatest::Reporter* reporter, const Geo& geo) { |
| 1324 | SkPath vPath = geo.path(); |
bsalomon | 4eeccc9 | 2016-04-27 13:30:25 -0700 | [diff] [blame] | 1325 | vPath.setIsVolatile(true); |
| 1326 | |
| 1327 | SkPaint dashAndStroke; |
| 1328 | dashAndStroke.setPathEffect(make_dash()); |
| 1329 | dashAndStroke.setStrokeWidth(2.f); |
| 1330 | dashAndStroke.setStyle(SkPaint::kStroke_Style); |
bsalomon | a395f7c | 2016-08-24 17:47:40 -0700 | [diff] [blame] | 1331 | TestCase volatileCase(reporter, vPath, dashAndStroke); |
bsalomon | 4eeccc9 | 2016-04-27 13:30:25 -0700 | [diff] [blame] | 1332 | // We expect a shape made from a volatile path to have a key iff the shape is recognized |
bsalomon | aa84064 | 2016-09-23 12:09:16 -0700 | [diff] [blame] | 1333 | // as a specialized geometry. |
| 1334 | if (geo.isNonPath(dashAndStroke)) { |
bsalomon | 4eeccc9 | 2016-04-27 13:30:25 -0700 | [diff] [blame] | 1335 | REPORTER_ASSERT(reporter, SkToBool(volatileCase.baseKey().count())); |
| 1336 | // In this case all the keys should be identical to the non-volatile case. |
bsalomon | a395f7c | 2016-08-24 17:47:40 -0700 | [diff] [blame] | 1337 | TestCase nonVolatileCase(reporter, geo.path(), dashAndStroke); |
bsalomon | 4eeccc9 | 2016-04-27 13:30:25 -0700 | [diff] [blame] | 1338 | volatileCase.compare(reporter, nonVolatileCase, TestCase::kAllSame_ComparisonExpecation); |
| 1339 | } else { |
| 1340 | // None of the keys should be valid. |
| 1341 | REPORTER_ASSERT(reporter, !SkToBool(volatileCase.baseKey().count())); |
| 1342 | REPORTER_ASSERT(reporter, !SkToBool(volatileCase.appliedPathEffectKey().count())); |
| 1343 | REPORTER_ASSERT(reporter, !SkToBool(volatileCase.appliedFullStyleKey().count())); |
| 1344 | REPORTER_ASSERT(reporter, !SkToBool(volatileCase.appliedPathEffectThenStrokeKey().count())); |
| 1345 | } |
| 1346 | } |
| 1347 | |
bsalomon | a395f7c | 2016-08-24 17:47:40 -0700 | [diff] [blame] | 1348 | void test_path_effect_makes_empty_shape(skiatest::Reporter* reporter, const Geo& geo) { |
bsalomon | 409ed73 | 2016-04-27 12:36:02 -0700 | [diff] [blame] | 1349 | /** |
Brian Salomon | 085c086 | 2017-08-31 15:44:51 -0400 | [diff] [blame] | 1350 | * This path effect returns an empty path (possibly inverted) |
bsalomon | 409ed73 | 2016-04-27 12:36:02 -0700 | [diff] [blame] | 1351 | */ |
| 1352 | class EmptyPathEffect : SkPathEffect { |
| 1353 | public: |
| 1354 | bool filterPath(SkPath* dst, const SkPath& src, SkStrokeRec*, |
| 1355 | const SkRect* cullR) const override { |
| 1356 | dst->reset(); |
Brian Salomon | 085c086 | 2017-08-31 15:44:51 -0400 | [diff] [blame] | 1357 | if (fInvert) { |
| 1358 | dst->toggleInverseFillType(); |
| 1359 | } |
bsalomon | 409ed73 | 2016-04-27 12:36:02 -0700 | [diff] [blame] | 1360 | return true; |
| 1361 | } |
| 1362 | void computeFastBounds(SkRect* dst, const SkRect& src) const override { |
| 1363 | dst->setEmpty(); |
| 1364 | } |
Brian Salomon | 085c086 | 2017-08-31 15:44:51 -0400 | [diff] [blame] | 1365 | static sk_sp<SkPathEffect> Make(bool invert) { |
| 1366 | return sk_sp<SkPathEffect>(new EmptyPathEffect(invert)); |
| 1367 | } |
bsalomon | 409ed73 | 2016-04-27 12:36:02 -0700 | [diff] [blame] | 1368 | Factory getFactory() const override { return nullptr; } |
bsalomon | 409ed73 | 2016-04-27 12:36:02 -0700 | [diff] [blame] | 1369 | private: |
Brian Salomon | 085c086 | 2017-08-31 15:44:51 -0400 | [diff] [blame] | 1370 | bool fInvert; |
| 1371 | EmptyPathEffect(bool invert) : fInvert(invert) {} |
bsalomon | 409ed73 | 2016-04-27 12:36:02 -0700 | [diff] [blame] | 1372 | }; |
| 1373 | |
| 1374 | SkPath emptyPath; |
| 1375 | GrShape emptyShape(emptyPath); |
| 1376 | Key emptyKey; |
| 1377 | make_key(&emptyKey, emptyShape); |
bsalomon | 7c73a53 | 2016-05-11 15:15:56 -0700 | [diff] [blame] | 1378 | REPORTER_ASSERT(reporter, emptyShape.isEmpty()); |
bsalomon | 409ed73 | 2016-04-27 12:36:02 -0700 | [diff] [blame] | 1379 | |
Brian Salomon | 085c086 | 2017-08-31 15:44:51 -0400 | [diff] [blame] | 1380 | emptyPath.toggleInverseFillType(); |
| 1381 | GrShape invertedEmptyShape(emptyPath); |
| 1382 | Key invertedEmptyKey; |
| 1383 | make_key(&invertedEmptyKey, invertedEmptyShape); |
| 1384 | REPORTER_ASSERT(reporter, invertedEmptyShape.isEmpty()); |
| 1385 | |
| 1386 | REPORTER_ASSERT(reporter, invertedEmptyKey != emptyKey); |
| 1387 | |
bsalomon | 409ed73 | 2016-04-27 12:36:02 -0700 | [diff] [blame] | 1388 | SkPaint pe; |
Brian Salomon | 085c086 | 2017-08-31 15:44:51 -0400 | [diff] [blame] | 1389 | pe.setPathEffect(EmptyPathEffect::Make(false)); |
| 1390 | TestCase geoPECase(geo, pe, reporter); |
| 1391 | REPORTER_ASSERT(reporter, geoPECase.appliedFullStyleKey() == emptyKey); |
| 1392 | REPORTER_ASSERT(reporter, geoPECase.appliedPathEffectKey() == emptyKey); |
| 1393 | REPORTER_ASSERT(reporter, geoPECase.appliedPathEffectThenStrokeKey() == emptyKey); |
| 1394 | REPORTER_ASSERT(reporter, geoPECase.appliedPathEffectShape().isEmpty()); |
| 1395 | REPORTER_ASSERT(reporter, geoPECase.appliedFullStyleShape().isEmpty()); |
| 1396 | REPORTER_ASSERT(reporter, !geoPECase.appliedPathEffectShape().inverseFilled()); |
| 1397 | REPORTER_ASSERT(reporter, !geoPECase.appliedFullStyleShape().inverseFilled()); |
bsalomon | 409ed73 | 2016-04-27 12:36:02 -0700 | [diff] [blame] | 1398 | |
| 1399 | SkPaint peStroke; |
Brian Salomon | 085c086 | 2017-08-31 15:44:51 -0400 | [diff] [blame] | 1400 | peStroke.setPathEffect(EmptyPathEffect::Make(false)); |
bsalomon | 409ed73 | 2016-04-27 12:36:02 -0700 | [diff] [blame] | 1401 | peStroke.setStrokeWidth(2.f); |
| 1402 | peStroke.setStyle(SkPaint::kStroke_Style); |
bsalomon | fb08327 | 2016-05-04 08:27:41 -0700 | [diff] [blame] | 1403 | TestCase geoPEStrokeCase(geo, peStroke, reporter); |
bsalomon | 409ed73 | 2016-04-27 12:36:02 -0700 | [diff] [blame] | 1404 | REPORTER_ASSERT(reporter, geoPEStrokeCase.appliedFullStyleKey() == emptyKey); |
| 1405 | REPORTER_ASSERT(reporter, geoPEStrokeCase.appliedPathEffectKey() == emptyKey); |
| 1406 | REPORTER_ASSERT(reporter, geoPEStrokeCase.appliedPathEffectThenStrokeKey() == emptyKey); |
bsalomon | 7c73a53 | 2016-05-11 15:15:56 -0700 | [diff] [blame] | 1407 | REPORTER_ASSERT(reporter, geoPEStrokeCase.appliedPathEffectShape().isEmpty()); |
| 1408 | REPORTER_ASSERT(reporter, geoPEStrokeCase.appliedFullStyleShape().isEmpty()); |
Brian Salomon | 085c086 | 2017-08-31 15:44:51 -0400 | [diff] [blame] | 1409 | REPORTER_ASSERT(reporter, !geoPEStrokeCase.appliedPathEffectShape().inverseFilled()); |
| 1410 | REPORTER_ASSERT(reporter, !geoPEStrokeCase.appliedFullStyleShape().inverseFilled()); |
| 1411 | pe.setPathEffect(EmptyPathEffect::Make(true)); |
| 1412 | |
| 1413 | TestCase geoPEInvertCase(geo, pe, reporter); |
| 1414 | REPORTER_ASSERT(reporter, geoPEInvertCase.appliedFullStyleKey() == invertedEmptyKey); |
| 1415 | REPORTER_ASSERT(reporter, geoPEInvertCase.appliedPathEffectKey() == invertedEmptyKey); |
| 1416 | REPORTER_ASSERT(reporter, geoPEInvertCase.appliedPathEffectThenStrokeKey() == invertedEmptyKey); |
| 1417 | REPORTER_ASSERT(reporter, geoPEInvertCase.appliedPathEffectShape().isEmpty()); |
| 1418 | REPORTER_ASSERT(reporter, geoPEInvertCase.appliedFullStyleShape().isEmpty()); |
| 1419 | REPORTER_ASSERT(reporter, geoPEInvertCase.appliedPathEffectShape().inverseFilled()); |
| 1420 | REPORTER_ASSERT(reporter, geoPEInvertCase.appliedFullStyleShape().inverseFilled()); |
| 1421 | |
| 1422 | peStroke.setPathEffect(EmptyPathEffect::Make(true)); |
| 1423 | TestCase geoPEInvertStrokeCase(geo, peStroke, reporter); |
| 1424 | REPORTER_ASSERT(reporter, geoPEInvertStrokeCase.appliedFullStyleKey() == invertedEmptyKey); |
| 1425 | REPORTER_ASSERT(reporter, geoPEInvertStrokeCase.appliedPathEffectKey() == invertedEmptyKey); |
| 1426 | REPORTER_ASSERT(reporter, |
| 1427 | geoPEInvertStrokeCase.appliedPathEffectThenStrokeKey() == invertedEmptyKey); |
| 1428 | REPORTER_ASSERT(reporter, geoPEInvertStrokeCase.appliedPathEffectShape().isEmpty()); |
| 1429 | REPORTER_ASSERT(reporter, geoPEInvertStrokeCase.appliedFullStyleShape().isEmpty()); |
| 1430 | REPORTER_ASSERT(reporter, geoPEInvertStrokeCase.appliedPathEffectShape().inverseFilled()); |
| 1431 | REPORTER_ASSERT(reporter, geoPEInvertStrokeCase.appliedFullStyleShape().inverseFilled()); |
bsalomon | 409ed73 | 2016-04-27 12:36:02 -0700 | [diff] [blame] | 1432 | } |
| 1433 | |
bsalomon | a395f7c | 2016-08-24 17:47:40 -0700 | [diff] [blame] | 1434 | void test_path_effect_fails(skiatest::Reporter* reporter, const Geo& geo) { |
bsalomon | d672384 | 2016-06-07 12:20:15 -0700 | [diff] [blame] | 1435 | /** |
bsalomon | 0ae36a2 | 2016-07-18 07:31:13 -0700 | [diff] [blame] | 1436 | * This path effect always fails to apply. |
bsalomon | d672384 | 2016-06-07 12:20:15 -0700 | [diff] [blame] | 1437 | */ |
| 1438 | class FailurePathEffect : SkPathEffect { |
| 1439 | public: |
| 1440 | bool filterPath(SkPath* dst, const SkPath& src, SkStrokeRec*, |
| 1441 | const SkRect* cullR) const override { |
| 1442 | return false; |
| 1443 | } |
| 1444 | void computeFastBounds(SkRect* dst, const SkRect& src) const override { |
| 1445 | *dst = src; |
| 1446 | } |
| 1447 | static sk_sp<SkPathEffect> Make() { return sk_sp<SkPathEffect>(new FailurePathEffect); } |
| 1448 | Factory getFactory() const override { return nullptr; } |
bsalomon | d672384 | 2016-06-07 12:20:15 -0700 | [diff] [blame] | 1449 | private: |
| 1450 | FailurePathEffect() {} |
| 1451 | }; |
| 1452 | |
| 1453 | SkPaint fill; |
| 1454 | TestCase fillCase(geo, fill, reporter); |
| 1455 | |
| 1456 | SkPaint pe; |
| 1457 | pe.setPathEffect(FailurePathEffect::Make()); |
| 1458 | TestCase peCase(geo, pe, reporter); |
| 1459 | |
| 1460 | SkPaint stroke; |
| 1461 | stroke.setStrokeWidth(2.f); |
| 1462 | stroke.setStyle(SkPaint::kStroke_Style); |
| 1463 | TestCase strokeCase(geo, stroke, reporter); |
| 1464 | |
| 1465 | SkPaint peStroke = stroke; |
| 1466 | peStroke.setPathEffect(FailurePathEffect::Make()); |
| 1467 | TestCase peStrokeCase(geo, peStroke, reporter); |
| 1468 | |
| 1469 | // In general the path effect failure can cause some of the TestCase::compare() tests to fail |
| 1470 | // for at least two reasons: 1) We will initially treat the shape as unkeyable because of the |
| 1471 | // path effect, but then when the path effect fails we can key it. 2) GrShape will change its |
| 1472 | // mind about whether a unclosed rect is actually rect. The path effect initially bars us from |
| 1473 | // closing it but after the effect fails we can (for the fill+pe case). This causes different |
| 1474 | // routes through GrShape to have equivalent but different representations of the path (closed |
| 1475 | // or not) but that fill the same. |
| 1476 | SkPath a; |
| 1477 | SkPath b; |
| 1478 | fillCase.appliedPathEffectShape().asPath(&a); |
| 1479 | peCase.appliedPathEffectShape().asPath(&b); |
| 1480 | REPORTER_ASSERT(reporter, paths_fill_same(a, b)); |
| 1481 | |
| 1482 | fillCase.appliedFullStyleShape().asPath(&a); |
| 1483 | peCase.appliedFullStyleShape().asPath(&b); |
| 1484 | REPORTER_ASSERT(reporter, paths_fill_same(a, b)); |
| 1485 | |
| 1486 | strokeCase.appliedPathEffectShape().asPath(&a); |
| 1487 | peStrokeCase.appliedPathEffectShape().asPath(&b); |
| 1488 | REPORTER_ASSERT(reporter, paths_fill_same(a, b)); |
| 1489 | |
| 1490 | strokeCase.appliedFullStyleShape().asPath(&a); |
| 1491 | peStrokeCase.appliedFullStyleShape().asPath(&b); |
| 1492 | REPORTER_ASSERT(reporter, paths_fill_same(a, b)); |
| 1493 | } |
| 1494 | |
Mike Klein | 4334428 | 2017-08-16 11:56:22 -0400 | [diff] [blame] | 1495 | DEF_TEST(GrShape_empty_shape, reporter) { |
bsalomon | 409ed73 | 2016-04-27 12:36:02 -0700 | [diff] [blame] | 1496 | SkPath emptyPath; |
Brian Salomon | 085c086 | 2017-08-31 15:44:51 -0400 | [diff] [blame] | 1497 | SkPath invertedEmptyPath; |
| 1498 | invertedEmptyPath.toggleInverseFillType(); |
bsalomon | 409ed73 | 2016-04-27 12:36:02 -0700 | [diff] [blame] | 1499 | SkPaint fill; |
bsalomon | a395f7c | 2016-08-24 17:47:40 -0700 | [diff] [blame] | 1500 | TestCase fillEmptyCase(reporter, emptyPath, fill); |
bsalomon | 7c73a53 | 2016-05-11 15:15:56 -0700 | [diff] [blame] | 1501 | REPORTER_ASSERT(reporter, fillEmptyCase.baseShape().isEmpty()); |
| 1502 | REPORTER_ASSERT(reporter, fillEmptyCase.appliedPathEffectShape().isEmpty()); |
| 1503 | REPORTER_ASSERT(reporter, fillEmptyCase.appliedFullStyleShape().isEmpty()); |
Brian Salomon | 085c086 | 2017-08-31 15:44:51 -0400 | [diff] [blame] | 1504 | REPORTER_ASSERT(reporter, !fillEmptyCase.baseShape().inverseFilled()); |
| 1505 | REPORTER_ASSERT(reporter, !fillEmptyCase.appliedPathEffectShape().inverseFilled()); |
| 1506 | REPORTER_ASSERT(reporter, !fillEmptyCase.appliedFullStyleShape().inverseFilled()); |
| 1507 | TestCase fillInvertedEmptyCase(reporter, invertedEmptyPath, fill); |
| 1508 | REPORTER_ASSERT(reporter, fillInvertedEmptyCase.baseShape().isEmpty()); |
| 1509 | REPORTER_ASSERT(reporter, fillInvertedEmptyCase.appliedPathEffectShape().isEmpty()); |
| 1510 | REPORTER_ASSERT(reporter, fillInvertedEmptyCase.appliedFullStyleShape().isEmpty()); |
| 1511 | REPORTER_ASSERT(reporter, fillInvertedEmptyCase.baseShape().inverseFilled()); |
| 1512 | REPORTER_ASSERT(reporter, fillInvertedEmptyCase.appliedPathEffectShape().inverseFilled()); |
| 1513 | REPORTER_ASSERT(reporter, fillInvertedEmptyCase.appliedFullStyleShape().inverseFilled()); |
bsalomon | 409ed73 | 2016-04-27 12:36:02 -0700 | [diff] [blame] | 1514 | |
| 1515 | Key emptyKey(fillEmptyCase.baseKey()); |
| 1516 | REPORTER_ASSERT(reporter, emptyKey.count()); |
Brian Salomon | 085c086 | 2017-08-31 15:44:51 -0400 | [diff] [blame] | 1517 | Key inverseEmptyKey(fillInvertedEmptyCase.baseKey()); |
| 1518 | REPORTER_ASSERT(reporter, inverseEmptyKey.count()); |
bsalomon | 409ed73 | 2016-04-27 12:36:02 -0700 | [diff] [blame] | 1519 | TestCase::SelfExpectations expectations; |
| 1520 | expectations.fStrokeApplies = false; |
| 1521 | expectations.fPEHasEffect = false; |
| 1522 | // This will test whether applying style preserves emptiness |
| 1523 | fillEmptyCase.testExpectations(reporter, expectations); |
Brian Salomon | 085c086 | 2017-08-31 15:44:51 -0400 | [diff] [blame] | 1524 | fillInvertedEmptyCase.testExpectations(reporter, expectations); |
bsalomon | 409ed73 | 2016-04-27 12:36:02 -0700 | [diff] [blame] | 1525 | |
| 1526 | // Stroking an empty path should have no effect |
bsalomon | 409ed73 | 2016-04-27 12:36:02 -0700 | [diff] [blame] | 1527 | SkPaint stroke; |
| 1528 | stroke.setStrokeWidth(2.f); |
| 1529 | stroke.setStyle(SkPaint::kStroke_Style); |
Brian Salomon | 2fad74a | 2017-12-20 13:28:55 -0500 | [diff] [blame] | 1530 | stroke.setStrokeJoin(SkPaint::kRound_Join); |
| 1531 | stroke.setStrokeCap(SkPaint::kRound_Cap); |
Brian Salomon | 085c086 | 2017-08-31 15:44:51 -0400 | [diff] [blame] | 1532 | TestCase strokeEmptyCase(reporter, emptyPath, stroke); |
bsalomon | 409ed73 | 2016-04-27 12:36:02 -0700 | [diff] [blame] | 1533 | strokeEmptyCase.compare(reporter, fillEmptyCase, TestCase::kAllSame_ComparisonExpecation); |
Brian Salomon | 085c086 | 2017-08-31 15:44:51 -0400 | [diff] [blame] | 1534 | TestCase strokeInvertedEmptyCase(reporter, invertedEmptyPath, stroke); |
| 1535 | strokeInvertedEmptyCase.compare(reporter, fillInvertedEmptyCase, |
| 1536 | TestCase::kAllSame_ComparisonExpecation); |
bsalomon | 409ed73 | 2016-04-27 12:36:02 -0700 | [diff] [blame] | 1537 | |
| 1538 | // Dashing and stroking an empty path should have no effect |
bsalomon | 409ed73 | 2016-04-27 12:36:02 -0700 | [diff] [blame] | 1539 | SkPaint dashAndStroke; |
| 1540 | dashAndStroke.setPathEffect(make_dash()); |
| 1541 | dashAndStroke.setStrokeWidth(2.f); |
| 1542 | dashAndStroke.setStyle(SkPaint::kStroke_Style); |
Brian Salomon | 085c086 | 2017-08-31 15:44:51 -0400 | [diff] [blame] | 1543 | TestCase dashAndStrokeEmptyCase(reporter, emptyPath, dashAndStroke); |
bsalomon | 409ed73 | 2016-04-27 12:36:02 -0700 | [diff] [blame] | 1544 | dashAndStrokeEmptyCase.compare(reporter, fillEmptyCase, |
| 1545 | TestCase::kAllSame_ComparisonExpecation); |
Brian Salomon | 085c086 | 2017-08-31 15:44:51 -0400 | [diff] [blame] | 1546 | TestCase dashAndStrokeInvertexEmptyCase(reporter, invertedEmptyPath, dashAndStroke); |
| 1547 | // Dashing ignores inverseness so this is equivalent to the non-inverted empty fill. |
| 1548 | dashAndStrokeInvertexEmptyCase.compare(reporter, fillEmptyCase, |
| 1549 | TestCase::kAllSame_ComparisonExpecation); |
bsalomon | 5e410b4 | 2016-04-28 09:30:46 -0700 | [diff] [blame] | 1550 | |
Brian Salomon | 2fad74a | 2017-12-20 13:28:55 -0500 | [diff] [blame] | 1551 | // A shape made from an empty rrect should behave the same as an empty path when filled but not |
| 1552 | // when stroked. However, dashing an empty rrect produces an empty path leaving nothing to |
| 1553 | // stroke - so equivalent to filling an empty path. |
| 1554 | SkRRect emptyRRect = SkRRect::MakeEmpty(); |
bsalomon | 5e410b4 | 2016-04-28 09:30:46 -0700 | [diff] [blame] | 1555 | REPORTER_ASSERT(reporter, emptyRRect.getType() == SkRRect::kEmpty_Type); |
Brian Salomon | 2fad74a | 2017-12-20 13:28:55 -0500 | [diff] [blame] | 1556 | |
| 1557 | TestCase fillEmptyRRectCase(reporter, emptyRRect, fill); |
| 1558 | fillEmptyRRectCase.compare(reporter, fillEmptyCase, TestCase::kAllSame_ComparisonExpecation); |
| 1559 | |
| 1560 | TestCase strokeEmptyRRectCase(reporter, emptyRRect, stroke); |
| 1561 | strokeEmptyRRectCase.compare(reporter, strokeEmptyCase, |
| 1562 | TestCase::kAllDifferent_ComparisonExpecation); |
| 1563 | |
bsalomon | a395f7c | 2016-08-24 17:47:40 -0700 | [diff] [blame] | 1564 | TestCase dashAndStrokeEmptyRRectCase(reporter, emptyRRect, dashAndStroke); |
bsalomon | 5e410b4 | 2016-04-28 09:30:46 -0700 | [diff] [blame] | 1565 | dashAndStrokeEmptyRRectCase.compare(reporter, fillEmptyCase, |
| 1566 | TestCase::kAllSame_ComparisonExpecation); |
Brian Salomon | 2fad74a | 2017-12-20 13:28:55 -0500 | [diff] [blame] | 1567 | |
Brian Salomon | 085c086 | 2017-08-31 15:44:51 -0400 | [diff] [blame] | 1568 | static constexpr SkPath::Direction kDir = SkPath::kCCW_Direction; |
| 1569 | static constexpr int kStart = 0; |
Brian Salomon | 2fad74a | 2017-12-20 13:28:55 -0500 | [diff] [blame] | 1570 | |
| 1571 | TestCase fillInvertedEmptyRRectCase(reporter, emptyRRect, kDir, kStart, true, GrStyle(fill)); |
| 1572 | fillInvertedEmptyRRectCase.compare(reporter, fillInvertedEmptyCase, |
| 1573 | TestCase::kAllSame_ComparisonExpecation); |
| 1574 | |
| 1575 | TestCase strokeInvertedEmptyRRectCase(reporter, emptyRRect, kDir, kStart, true, |
| 1576 | GrStyle(stroke)); |
| 1577 | strokeInvertedEmptyRRectCase.compare(reporter, strokeInvertedEmptyCase, |
| 1578 | TestCase::kAllDifferent_ComparisonExpecation); |
| 1579 | |
Brian Salomon | 085c086 | 2017-08-31 15:44:51 -0400 | [diff] [blame] | 1580 | TestCase dashAndStrokeEmptyInvertedRRectCase(reporter, emptyRRect, kDir, kStart, true, |
| 1581 | GrStyle(dashAndStroke)); |
Brian Salomon | 085c086 | 2017-08-31 15:44:51 -0400 | [diff] [blame] | 1582 | dashAndStrokeEmptyInvertedRRectCase.compare(reporter, fillEmptyCase, |
| 1583 | TestCase::kAllSame_ComparisonExpecation); |
bsalomon | 5e410b4 | 2016-04-28 09:30:46 -0700 | [diff] [blame] | 1584 | |
| 1585 | // Same for a rect. |
| 1586 | SkRect emptyRect = SkRect::MakeEmpty(); |
Brian Salomon | 2fad74a | 2017-12-20 13:28:55 -0500 | [diff] [blame] | 1587 | TestCase fillEmptyRectCase(reporter, emptyRect, fill); |
| 1588 | fillEmptyRectCase.compare(reporter, fillEmptyCase, TestCase::kAllSame_ComparisonExpecation); |
| 1589 | |
bsalomon | a395f7c | 2016-08-24 17:47:40 -0700 | [diff] [blame] | 1590 | TestCase dashAndStrokeEmptyRectCase(reporter, emptyRect, dashAndStroke); |
bsalomon | 5e410b4 | 2016-04-28 09:30:46 -0700 | [diff] [blame] | 1591 | dashAndStrokeEmptyRectCase.compare(reporter, fillEmptyCase, |
| 1592 | TestCase::kAllSame_ComparisonExpecation); |
Brian Salomon | 2fad74a | 2017-12-20 13:28:55 -0500 | [diff] [blame] | 1593 | |
Brian Salomon | 085c086 | 2017-08-31 15:44:51 -0400 | [diff] [blame] | 1594 | TestCase dashAndStrokeEmptyInvertedRectCase(reporter, SkRRect::MakeRect(emptyRect), kDir, |
| 1595 | kStart, true, GrStyle(dashAndStroke)); |
| 1596 | // Dashing ignores inverseness so this is equivalent to the non-inverted empty fill. |
| 1597 | dashAndStrokeEmptyInvertedRectCase.compare(reporter, fillEmptyCase, |
| 1598 | TestCase::kAllSame_ComparisonExpecation); |
bsalomon | 409ed73 | 2016-04-27 12:36:02 -0700 | [diff] [blame] | 1599 | } |
| 1600 | |
bsalomon | 7049396 | 2016-06-10 08:05:14 -0700 | [diff] [blame] | 1601 | // rect and oval types have rrect start indices that collapse to the same point. Here we select the |
| 1602 | // canonical point in these cases. |
| 1603 | unsigned canonicalize_rrect_start(int s, const SkRRect& rrect) { |
| 1604 | switch (rrect.getType()) { |
| 1605 | case SkRRect::kRect_Type: |
| 1606 | return (s + 1) & 0b110; |
| 1607 | case SkRRect::kOval_Type: |
| 1608 | return s & 0b110; |
| 1609 | default: |
| 1610 | return s; |
| 1611 | } |
| 1612 | } |
| 1613 | |
| 1614 | void test_rrect(skiatest::Reporter* r, const SkRRect& rrect) { |
bsalomon | cadb5a2 | 2016-06-10 18:28:06 -0700 | [diff] [blame] | 1615 | enum Style { |
bsalomon | 7049396 | 2016-06-10 08:05:14 -0700 | [diff] [blame] | 1616 | kFill, |
| 1617 | kStroke, |
| 1618 | kHairline, |
| 1619 | kStrokeAndFill |
| 1620 | }; |
| 1621 | |
| 1622 | // SkStrokeRec has no default cons., so init with kFill before calling the setters below. |
| 1623 | SkStrokeRec strokeRecs[4] { SkStrokeRec::kFill_InitStyle, SkStrokeRec::kFill_InitStyle, |
| 1624 | SkStrokeRec::kFill_InitStyle, SkStrokeRec::kFill_InitStyle}; |
| 1625 | strokeRecs[kFill].setFillStyle(); |
| 1626 | strokeRecs[kStroke].setStrokeStyle(2.f); |
| 1627 | strokeRecs[kHairline].setHairlineStyle(); |
| 1628 | strokeRecs[kStrokeAndFill].setStrokeStyle(3.f, true); |
bsalomon | 487f8d3 | 2016-07-20 07:15:44 -0700 | [diff] [blame] | 1629 | // Use a bevel join to avoid complications of stroke+filled rects becoming filled rects before |
| 1630 | // applyStyle() is called. |
| 1631 | strokeRecs[kStrokeAndFill].setStrokeParams(SkPaint::kButt_Cap, SkPaint::kBevel_Join, 1.f); |
bsalomon | 7049396 | 2016-06-10 08:05:14 -0700 | [diff] [blame] | 1632 | sk_sp<SkPathEffect> dashEffect = make_dash(); |
| 1633 | |
bsalomon | cadb5a2 | 2016-06-10 18:28:06 -0700 | [diff] [blame] | 1634 | static constexpr Style kStyleCnt = static_cast<Style>(SK_ARRAY_COUNT(strokeRecs)); |
| 1635 | |
| 1636 | auto index = [](bool inverted, |
| 1637 | SkPath::Direction dir, |
| 1638 | unsigned start, |
| 1639 | Style style, |
| 1640 | bool dash) -> int { |
| 1641 | return inverted * (2 * 8 * kStyleCnt * 2) + |
| 1642 | dir * ( 8 * kStyleCnt * 2) + |
| 1643 | start * ( kStyleCnt * 2) + |
| 1644 | style * ( 2) + |
| 1645 | dash; |
| 1646 | }; |
| 1647 | static const SkPath::Direction kSecondDirection = static_cast<SkPath::Direction>(1); |
| 1648 | const int cnt = index(true, kSecondDirection, 7, static_cast<Style>(kStyleCnt - 1), true) + 1; |
| 1649 | SkAutoTArray<GrShape> shapes(cnt); |
| 1650 | for (bool inverted : {false, true}) { |
| 1651 | for (SkPath::Direction dir : {SkPath::kCW_Direction, SkPath::kCCW_Direction}) { |
| 1652 | for (unsigned start = 0; start < 8; ++start) { |
| 1653 | for (Style style : {kFill, kStroke, kHairline, kStrokeAndFill}) { |
| 1654 | for (bool dash : {false, true}) { |
Robert Phillips | f809c1e | 2017-01-13 11:02:42 -0500 | [diff] [blame] | 1655 | sk_sp<SkPathEffect> pe = dash ? dashEffect : nullptr; |
bsalomon | cadb5a2 | 2016-06-10 18:28:06 -0700 | [diff] [blame] | 1656 | shapes[index(inverted, dir, start, style, dash)] = |
| 1657 | GrShape(rrect, dir, start, SkToBool(inverted), |
Robert Phillips | f809c1e | 2017-01-13 11:02:42 -0500 | [diff] [blame] | 1658 | GrStyle(strokeRecs[style], std::move(pe))); |
bsalomon | 7049396 | 2016-06-10 08:05:14 -0700 | [diff] [blame] | 1659 | } |
| 1660 | } |
| 1661 | } |
| 1662 | } |
| 1663 | } |
| 1664 | |
bsalomon | fd32df7 | 2016-06-14 14:37:21 -0700 | [diff] [blame] | 1665 | // Get the keys for some example shape instances that we'll use for comparision against the |
| 1666 | // rest. |
| 1667 | static constexpr SkPath::Direction kExamplesDir = SkPath::kCW_Direction; |
| 1668 | static constexpr unsigned kExamplesStart = 0; |
| 1669 | const GrShape& exampleFillCase = shapes[index(false, kExamplesDir, kExamplesStart, kFill, |
| 1670 | false)]; |
bsalomon | 7049396 | 2016-06-10 08:05:14 -0700 | [diff] [blame] | 1671 | Key exampleFillCaseKey; |
| 1672 | make_key(&exampleFillCaseKey, exampleFillCase); |
| 1673 | |
bsalomon | fd32df7 | 2016-06-14 14:37:21 -0700 | [diff] [blame] | 1674 | const GrShape& exampleStrokeAndFillCase = shapes[index(false, kExamplesDir, kExamplesStart, |
| 1675 | kStrokeAndFill, false)]; |
bsalomon | 7049396 | 2016-06-10 08:05:14 -0700 | [diff] [blame] | 1676 | Key exampleStrokeAndFillCaseKey; |
| 1677 | make_key(&exampleStrokeAndFillCaseKey, exampleStrokeAndFillCase); |
| 1678 | |
bsalomon | fd32df7 | 2016-06-14 14:37:21 -0700 | [diff] [blame] | 1679 | const GrShape& exampleInvFillCase = shapes[index(true, kExamplesDir, kExamplesStart, kFill, |
| 1680 | false)]; |
bsalomon | 7049396 | 2016-06-10 08:05:14 -0700 | [diff] [blame] | 1681 | Key exampleInvFillCaseKey; |
| 1682 | make_key(&exampleInvFillCaseKey, exampleInvFillCase); |
| 1683 | |
bsalomon | fd32df7 | 2016-06-14 14:37:21 -0700 | [diff] [blame] | 1684 | const GrShape& exampleInvStrokeAndFillCase = shapes[index(true, kExamplesDir, kExamplesStart, |
| 1685 | kStrokeAndFill, false)]; |
bsalomon | 7049396 | 2016-06-10 08:05:14 -0700 | [diff] [blame] | 1686 | Key exampleInvStrokeAndFillCaseKey; |
| 1687 | make_key(&exampleInvStrokeAndFillCaseKey, exampleInvStrokeAndFillCase); |
| 1688 | |
bsalomon | fd32df7 | 2016-06-14 14:37:21 -0700 | [diff] [blame] | 1689 | const GrShape& exampleStrokeCase = shapes[index(false, kExamplesDir, kExamplesStart, kStroke, |
| 1690 | false)]; |
bsalomon | 7049396 | 2016-06-10 08:05:14 -0700 | [diff] [blame] | 1691 | Key exampleStrokeCaseKey; |
| 1692 | make_key(&exampleStrokeCaseKey, exampleStrokeCase); |
| 1693 | |
bsalomon | fd32df7 | 2016-06-14 14:37:21 -0700 | [diff] [blame] | 1694 | const GrShape& exampleInvStrokeCase = shapes[index(true, kExamplesDir, kExamplesStart, kStroke, |
| 1695 | false)]; |
| 1696 | Key exampleInvStrokeCaseKey; |
| 1697 | make_key(&exampleInvStrokeCaseKey, exampleInvStrokeCase); |
| 1698 | |
| 1699 | const GrShape& exampleHairlineCase = shapes[index(false, kExamplesDir, kExamplesStart, |
| 1700 | kHairline, false)]; |
bsalomon | 7049396 | 2016-06-10 08:05:14 -0700 | [diff] [blame] | 1701 | Key exampleHairlineCaseKey; |
| 1702 | make_key(&exampleHairlineCaseKey, exampleHairlineCase); |
| 1703 | |
bsalomon | fd32df7 | 2016-06-14 14:37:21 -0700 | [diff] [blame] | 1704 | const GrShape& exampleInvHairlineCase = shapes[index(true, kExamplesDir, kExamplesStart, |
| 1705 | kHairline, false)]; |
| 1706 | Key exampleInvHairlineCaseKey; |
| 1707 | make_key(&exampleInvHairlineCaseKey, exampleInvHairlineCase); |
| 1708 | |
bsalomon | 7049396 | 2016-06-10 08:05:14 -0700 | [diff] [blame] | 1709 | // These are dummy initializations to suppress warnings. |
bsalomon | cadb5a2 | 2016-06-10 18:28:06 -0700 | [diff] [blame] | 1710 | SkRRect queryRR = SkRRect::MakeEmpty(); |
| 1711 | SkPath::Direction queryDir = SkPath::kCW_Direction; |
| 1712 | unsigned queryStart = ~0U; |
| 1713 | bool queryInverted = true; |
bsalomon | 7049396 | 2016-06-10 08:05:14 -0700 | [diff] [blame] | 1714 | |
bsalomon | cadb5a2 | 2016-06-10 18:28:06 -0700 | [diff] [blame] | 1715 | REPORTER_ASSERT(r, exampleFillCase.asRRect(&queryRR, &queryDir, &queryStart, &queryInverted)); |
| 1716 | REPORTER_ASSERT(r, queryRR == rrect); |
| 1717 | REPORTER_ASSERT(r, SkPath::kCW_Direction == queryDir); |
| 1718 | REPORTER_ASSERT(r, 0 == queryStart); |
| 1719 | REPORTER_ASSERT(r, !queryInverted); |
bsalomon | 7049396 | 2016-06-10 08:05:14 -0700 | [diff] [blame] | 1720 | |
bsalomon | cadb5a2 | 2016-06-10 18:28:06 -0700 | [diff] [blame] | 1721 | REPORTER_ASSERT(r, exampleInvFillCase.asRRect(&queryRR, &queryDir, &queryStart, |
| 1722 | &queryInverted)); |
| 1723 | REPORTER_ASSERT(r, queryRR == rrect); |
| 1724 | REPORTER_ASSERT(r, SkPath::kCW_Direction == queryDir); |
| 1725 | REPORTER_ASSERT(r, 0 == queryStart); |
| 1726 | REPORTER_ASSERT(r, queryInverted); |
bsalomon | 7049396 | 2016-06-10 08:05:14 -0700 | [diff] [blame] | 1727 | |
bsalomon | cadb5a2 | 2016-06-10 18:28:06 -0700 | [diff] [blame] | 1728 | REPORTER_ASSERT(r, exampleStrokeAndFillCase.asRRect(&queryRR, &queryDir, &queryStart, |
| 1729 | &queryInverted)); |
| 1730 | REPORTER_ASSERT(r, queryRR == rrect); |
| 1731 | REPORTER_ASSERT(r, SkPath::kCW_Direction == queryDir); |
| 1732 | REPORTER_ASSERT(r, 0 == queryStart); |
| 1733 | REPORTER_ASSERT(r, !queryInverted); |
bsalomon | 7049396 | 2016-06-10 08:05:14 -0700 | [diff] [blame] | 1734 | |
bsalomon | cadb5a2 | 2016-06-10 18:28:06 -0700 | [diff] [blame] | 1735 | REPORTER_ASSERT(r, exampleInvStrokeAndFillCase.asRRect(&queryRR, &queryDir, &queryStart, |
| 1736 | &queryInverted)); |
| 1737 | REPORTER_ASSERT(r, queryRR == rrect); |
| 1738 | REPORTER_ASSERT(r, SkPath::kCW_Direction == queryDir); |
| 1739 | REPORTER_ASSERT(r, 0 == queryStart); |
| 1740 | REPORTER_ASSERT(r, queryInverted); |
bsalomon | 7049396 | 2016-06-10 08:05:14 -0700 | [diff] [blame] | 1741 | |
bsalomon | cadb5a2 | 2016-06-10 18:28:06 -0700 | [diff] [blame] | 1742 | REPORTER_ASSERT(r, exampleHairlineCase.asRRect(&queryRR, &queryDir, &queryStart, |
| 1743 | &queryInverted)); |
| 1744 | REPORTER_ASSERT(r, queryRR == rrect); |
| 1745 | REPORTER_ASSERT(r, SkPath::kCW_Direction == queryDir); |
| 1746 | REPORTER_ASSERT(r, 0 == queryStart); |
| 1747 | REPORTER_ASSERT(r, !queryInverted); |
bsalomon | 7049396 | 2016-06-10 08:05:14 -0700 | [diff] [blame] | 1748 | |
bsalomon | fd32df7 | 2016-06-14 14:37:21 -0700 | [diff] [blame] | 1749 | REPORTER_ASSERT(r, exampleInvHairlineCase.asRRect(&queryRR, &queryDir, &queryStart, |
| 1750 | &queryInverted)); |
| 1751 | REPORTER_ASSERT(r, queryRR == rrect); |
| 1752 | REPORTER_ASSERT(r, SkPath::kCW_Direction == queryDir); |
| 1753 | REPORTER_ASSERT(r, 0 == queryStart); |
| 1754 | REPORTER_ASSERT(r, queryInverted); |
| 1755 | |
bsalomon | cadb5a2 | 2016-06-10 18:28:06 -0700 | [diff] [blame] | 1756 | REPORTER_ASSERT(r, exampleStrokeCase.asRRect(&queryRR, &queryDir, &queryStart, &queryInverted)); |
| 1757 | REPORTER_ASSERT(r, queryRR == rrect); |
| 1758 | REPORTER_ASSERT(r, SkPath::kCW_Direction == queryDir); |
| 1759 | REPORTER_ASSERT(r, 0 == queryStart); |
| 1760 | REPORTER_ASSERT(r, !queryInverted); |
bsalomon | 7049396 | 2016-06-10 08:05:14 -0700 | [diff] [blame] | 1761 | |
bsalomon | fd32df7 | 2016-06-14 14:37:21 -0700 | [diff] [blame] | 1762 | REPORTER_ASSERT(r, exampleInvStrokeCase.asRRect(&queryRR, &queryDir, &queryStart, |
| 1763 | &queryInverted)); |
| 1764 | REPORTER_ASSERT(r, queryRR == rrect); |
| 1765 | REPORTER_ASSERT(r, SkPath::kCW_Direction == queryDir); |
| 1766 | REPORTER_ASSERT(r, 0 == queryStart); |
| 1767 | REPORTER_ASSERT(r, queryInverted); |
| 1768 | |
bsalomon | 7049396 | 2016-06-10 08:05:14 -0700 | [diff] [blame] | 1769 | // Remember that the key reflects the geometry before styling is applied. |
| 1770 | REPORTER_ASSERT(r, exampleFillCaseKey != exampleInvFillCaseKey); |
| 1771 | REPORTER_ASSERT(r, exampleFillCaseKey == exampleStrokeAndFillCaseKey); |
| 1772 | REPORTER_ASSERT(r, exampleFillCaseKey != exampleInvStrokeAndFillCaseKey); |
| 1773 | REPORTER_ASSERT(r, exampleFillCaseKey == exampleStrokeCaseKey); |
bsalomon | fd32df7 | 2016-06-14 14:37:21 -0700 | [diff] [blame] | 1774 | REPORTER_ASSERT(r, exampleFillCaseKey != exampleInvStrokeCaseKey); |
bsalomon | 7049396 | 2016-06-10 08:05:14 -0700 | [diff] [blame] | 1775 | REPORTER_ASSERT(r, exampleFillCaseKey == exampleHairlineCaseKey); |
bsalomon | fd32df7 | 2016-06-14 14:37:21 -0700 | [diff] [blame] | 1776 | REPORTER_ASSERT(r, exampleFillCaseKey != exampleInvHairlineCaseKey); |
bsalomon | 7049396 | 2016-06-10 08:05:14 -0700 | [diff] [blame] | 1777 | REPORTER_ASSERT(r, exampleInvStrokeAndFillCaseKey == exampleInvFillCaseKey); |
bsalomon | fd32df7 | 2016-06-14 14:37:21 -0700 | [diff] [blame] | 1778 | REPORTER_ASSERT(r, exampleInvStrokeAndFillCaseKey == exampleInvStrokeCaseKey); |
| 1779 | REPORTER_ASSERT(r, exampleInvStrokeAndFillCaseKey == exampleInvHairlineCaseKey); |
bsalomon | 7049396 | 2016-06-10 08:05:14 -0700 | [diff] [blame] | 1780 | |
bsalomon | cadb5a2 | 2016-06-10 18:28:06 -0700 | [diff] [blame] | 1781 | for (bool inverted : {false, true}) { |
| 1782 | for (SkPath::Direction dir : {SkPath::kCW_Direction, SkPath::kCCW_Direction}) { |
| 1783 | for (unsigned start = 0; start < 8; ++start) { |
| 1784 | for (bool dash : {false, true}) { |
| 1785 | const GrShape& fillCase = shapes[index(inverted, dir, start, kFill, dash)]; |
bsalomon | 7049396 | 2016-06-10 08:05:14 -0700 | [diff] [blame] | 1786 | Key fillCaseKey; |
| 1787 | make_key(&fillCaseKey, fillCase); |
| 1788 | |
bsalomon | cadb5a2 | 2016-06-10 18:28:06 -0700 | [diff] [blame] | 1789 | const GrShape& strokeAndFillCase = shapes[index(inverted, dir, start, |
| 1790 | kStrokeAndFill, dash)]; |
bsalomon | 7049396 | 2016-06-10 08:05:14 -0700 | [diff] [blame] | 1791 | Key strokeAndFillCaseKey; |
| 1792 | make_key(&strokeAndFillCaseKey, strokeAndFillCase); |
| 1793 | |
| 1794 | // Both fill and stroke-and-fill shapes must respect the inverseness and both |
| 1795 | // ignore dashing. |
| 1796 | REPORTER_ASSERT(r, !fillCase.style().pathEffect()); |
| 1797 | REPORTER_ASSERT(r, !strokeAndFillCase.style().pathEffect()); |
| 1798 | TestCase a(fillCase, r); |
| 1799 | TestCase b(inverted ? exampleInvFillCase : exampleFillCase, r); |
| 1800 | TestCase c(strokeAndFillCase, r); |
| 1801 | TestCase d(inverted ? exampleInvStrokeAndFillCase |
| 1802 | : exampleStrokeAndFillCase, r); |
| 1803 | a.compare(r, b, TestCase::kAllSame_ComparisonExpecation); |
| 1804 | c.compare(r, d, TestCase::kAllSame_ComparisonExpecation); |
| 1805 | |
bsalomon | cadb5a2 | 2016-06-10 18:28:06 -0700 | [diff] [blame] | 1806 | const GrShape& strokeCase = shapes[index(inverted, dir, start, kStroke, dash)]; |
| 1807 | const GrShape& hairlineCase = shapes[index(inverted, dir, start, kHairline, |
| 1808 | dash)]; |
bsalomon | 7049396 | 2016-06-10 08:05:14 -0700 | [diff] [blame] | 1809 | |
| 1810 | TestCase e(strokeCase, r); |
bsalomon | 7049396 | 2016-06-10 08:05:14 -0700 | [diff] [blame] | 1811 | TestCase g(hairlineCase, r); |
bsalomon | 7049396 | 2016-06-10 08:05:14 -0700 | [diff] [blame] | 1812 | |
bsalomon | fd32df7 | 2016-06-14 14:37:21 -0700 | [diff] [blame] | 1813 | // Both hairline and stroke shapes must respect the dashing. |
bsalomon | 7049396 | 2016-06-10 08:05:14 -0700 | [diff] [blame] | 1814 | if (dash) { |
bsalomon | fd32df7 | 2016-06-14 14:37:21 -0700 | [diff] [blame] | 1815 | // Dashing always ignores the inverseness. skbug.com/5421 |
| 1816 | TestCase f(exampleStrokeCase, r); |
| 1817 | TestCase h(exampleHairlineCase, r); |
bsalomon | cadb5a2 | 2016-06-10 18:28:06 -0700 | [diff] [blame] | 1818 | unsigned expectedStart = canonicalize_rrect_start(start, rrect); |
bsalomon | 7049396 | 2016-06-10 08:05:14 -0700 | [diff] [blame] | 1819 | REPORTER_ASSERT(r, strokeCase.style().pathEffect()); |
| 1820 | REPORTER_ASSERT(r, hairlineCase.style().pathEffect()); |
| 1821 | |
bsalomon | cadb5a2 | 2016-06-10 18:28:06 -0700 | [diff] [blame] | 1822 | REPORTER_ASSERT(r, strokeCase.asRRect(&queryRR, &queryDir, &queryStart, |
| 1823 | &queryInverted)); |
| 1824 | REPORTER_ASSERT(r, queryRR == rrect); |
| 1825 | REPORTER_ASSERT(r, queryDir == dir); |
| 1826 | REPORTER_ASSERT(r, queryStart == expectedStart); |
| 1827 | REPORTER_ASSERT(r, !queryInverted); |
| 1828 | REPORTER_ASSERT(r, hairlineCase.asRRect(&queryRR, &queryDir, &queryStart, |
| 1829 | &queryInverted)); |
| 1830 | REPORTER_ASSERT(r, queryRR == rrect); |
| 1831 | REPORTER_ASSERT(r, queryDir == dir); |
| 1832 | REPORTER_ASSERT(r, queryStart == expectedStart); |
| 1833 | REPORTER_ASSERT(r, !queryInverted); |
bsalomon | 7049396 | 2016-06-10 08:05:14 -0700 | [diff] [blame] | 1834 | |
| 1835 | // The pre-style case for the dash will match the non-dash example iff the |
| 1836 | // dir and start match (dir=cw, start=0). |
bsalomon | cadb5a2 | 2016-06-10 18:28:06 -0700 | [diff] [blame] | 1837 | if (0 == expectedStart && SkPath::kCW_Direction == dir) { |
bsalomon | 7049396 | 2016-06-10 08:05:14 -0700 | [diff] [blame] | 1838 | e.compare(r, f, TestCase::kSameUpToPE_ComparisonExpecation); |
| 1839 | g.compare(r, h, TestCase::kSameUpToPE_ComparisonExpecation); |
| 1840 | } else { |
| 1841 | e.compare(r, f, TestCase::kAllDifferent_ComparisonExpecation); |
| 1842 | g.compare(r, h, TestCase::kAllDifferent_ComparisonExpecation); |
| 1843 | } |
| 1844 | } else { |
bsalomon | fd32df7 | 2016-06-14 14:37:21 -0700 | [diff] [blame] | 1845 | TestCase f(inverted ? exampleInvStrokeCase : exampleStrokeCase, r); |
| 1846 | TestCase h(inverted ? exampleInvHairlineCase : exampleHairlineCase, r); |
bsalomon | 7049396 | 2016-06-10 08:05:14 -0700 | [diff] [blame] | 1847 | REPORTER_ASSERT(r, !strokeCase.style().pathEffect()); |
| 1848 | REPORTER_ASSERT(r, !hairlineCase.style().pathEffect()); |
| 1849 | e.compare(r, f, TestCase::kAllSame_ComparisonExpecation); |
| 1850 | g.compare(r, h, TestCase::kAllSame_ComparisonExpecation); |
| 1851 | } |
| 1852 | } |
| 1853 | } |
| 1854 | } |
| 1855 | } |
| 1856 | } |
| 1857 | |
Mike Klein | 4334428 | 2017-08-16 11:56:22 -0400 | [diff] [blame] | 1858 | DEF_TEST(GrShape_lines, r) { |
bsalomon | 0a0f67e | 2016-06-28 11:56:42 -0700 | [diff] [blame] | 1859 | static constexpr SkPoint kA { 1, 1}; |
| 1860 | static constexpr SkPoint kB { 5, -9}; |
| 1861 | static constexpr SkPoint kC {-3, 17}; |
| 1862 | |
| 1863 | SkPath lineAB; |
| 1864 | lineAB.moveTo(kA); |
| 1865 | lineAB.lineTo(kB); |
| 1866 | |
| 1867 | SkPath lineBA; |
| 1868 | lineBA.moveTo(kB); |
| 1869 | lineBA.lineTo(kA); |
| 1870 | |
| 1871 | SkPath lineAC; |
| 1872 | lineAC.moveTo(kB); |
| 1873 | lineAC.lineTo(kC); |
| 1874 | |
| 1875 | SkPath invLineAB = lineAB; |
| 1876 | invLineAB.setFillType(SkPath::kInverseEvenOdd_FillType); |
| 1877 | |
| 1878 | SkPaint fill; |
| 1879 | SkPaint stroke; |
| 1880 | stroke.setStyle(SkPaint::kStroke_Style); |
| 1881 | stroke.setStrokeWidth(2.f); |
| 1882 | SkPaint hairline; |
| 1883 | hairline.setStyle(SkPaint::kStroke_Style); |
| 1884 | hairline.setStrokeWidth(0.f); |
| 1885 | SkPaint dash = stroke; |
| 1886 | dash.setPathEffect(make_dash()); |
| 1887 | |
bsalomon | a395f7c | 2016-08-24 17:47:40 -0700 | [diff] [blame] | 1888 | TestCase fillAB(r, lineAB, fill); |
| 1889 | TestCase fillEmpty(r, SkPath(), fill); |
bsalomon | 0a0f67e | 2016-06-28 11:56:42 -0700 | [diff] [blame] | 1890 | fillAB.compare(r, fillEmpty, TestCase::kAllSame_ComparisonExpecation); |
| 1891 | REPORTER_ASSERT(r, !fillAB.baseShape().asLine(nullptr, nullptr)); |
| 1892 | |
Brian Salomon | 085c086 | 2017-08-31 15:44:51 -0400 | [diff] [blame] | 1893 | SkPath path; |
| 1894 | path.toggleInverseFillType(); |
| 1895 | TestCase fillEmptyInverted(r, path, fill); |
| 1896 | TestCase fillABInverted(r, invLineAB, fill); |
| 1897 | fillABInverted.compare(r, fillEmptyInverted, TestCase::kAllSame_ComparisonExpecation); |
| 1898 | REPORTER_ASSERT(r, !fillABInverted.baseShape().asLine(nullptr, nullptr)); |
| 1899 | |
bsalomon | a395f7c | 2016-08-24 17:47:40 -0700 | [diff] [blame] | 1900 | TestCase strokeAB(r, lineAB, stroke); |
| 1901 | TestCase strokeBA(r, lineBA, stroke); |
| 1902 | TestCase strokeAC(r, lineAC, stroke); |
bsalomon | 0a0f67e | 2016-06-28 11:56:42 -0700 | [diff] [blame] | 1903 | |
bsalomon | a395f7c | 2016-08-24 17:47:40 -0700 | [diff] [blame] | 1904 | TestCase hairlineAB(r, lineAB, hairline); |
| 1905 | TestCase hairlineBA(r, lineBA, hairline); |
| 1906 | TestCase hairlineAC(r, lineAC, hairline); |
bsalomon | 0a0f67e | 2016-06-28 11:56:42 -0700 | [diff] [blame] | 1907 | |
bsalomon | a395f7c | 2016-08-24 17:47:40 -0700 | [diff] [blame] | 1908 | TestCase dashAB(r, lineAB, dash); |
| 1909 | TestCase dashBA(r, lineBA, dash); |
| 1910 | TestCase dashAC(r, lineAC, dash); |
bsalomon | 0a0f67e | 2016-06-28 11:56:42 -0700 | [diff] [blame] | 1911 | |
| 1912 | strokeAB.compare(r, fillAB, TestCase::kAllDifferent_ComparisonExpecation); |
| 1913 | |
| 1914 | strokeAB.compare(r, strokeBA, TestCase::kAllSame_ComparisonExpecation); |
| 1915 | strokeAB.compare(r, strokeAC, TestCase::kAllDifferent_ComparisonExpecation); |
| 1916 | |
| 1917 | hairlineAB.compare(r, hairlineBA, TestCase::kAllSame_ComparisonExpecation); |
| 1918 | hairlineAB.compare(r, hairlineAC, TestCase::kAllDifferent_ComparisonExpecation); |
| 1919 | |
| 1920 | dashAB.compare(r, dashBA, TestCase::kAllDifferent_ComparisonExpecation); |
| 1921 | dashAB.compare(r, dashAC, TestCase::kAllDifferent_ComparisonExpecation); |
| 1922 | |
| 1923 | strokeAB.compare(r, hairlineAB, TestCase::kSameUpToStroke_ComparisonExpecation); |
| 1924 | |
| 1925 | // One of dashAB or dashBA should have the same line as strokeAB. It depends upon how |
| 1926 | // GrShape canonicalizes line endpoints (when it can, i.e. when not dashed). |
| 1927 | bool canonicalizeAsAB; |
| 1928 | SkPoint canonicalPts[2] {kA, kB}; |
| 1929 | // Init these to suppress warnings. |
| 1930 | bool inverted = true; |
| 1931 | SkPoint pts[2] {{0, 0}, {0, 0}}; |
| 1932 | REPORTER_ASSERT(r, strokeAB.baseShape().asLine(pts, &inverted) && !inverted); |
| 1933 | if (pts[0] == kA && pts[1] == kB) { |
| 1934 | canonicalizeAsAB = true; |
| 1935 | } else if (pts[1] == kA && pts[0] == kB) { |
| 1936 | canonicalizeAsAB = false; |
Ben Wagner | f08d1d0 | 2018-06-18 15:11:00 -0400 | [diff] [blame] | 1937 | using std::swap; |
| 1938 | swap(canonicalPts[0], canonicalPts[1]); |
bsalomon | 0a0f67e | 2016-06-28 11:56:42 -0700 | [diff] [blame] | 1939 | } else { |
| 1940 | ERRORF(r, "Should return pts (a,b) or (b, a)"); |
| 1941 | return; |
| 1942 | }; |
| 1943 | |
| 1944 | strokeAB.compare(r, canonicalizeAsAB ? dashAB : dashBA, |
| 1945 | TestCase::kSameUpToPE_ComparisonExpecation); |
| 1946 | REPORTER_ASSERT(r, strokeAB.baseShape().asLine(pts, &inverted) && !inverted && |
| 1947 | pts[0] == canonicalPts[0] && pts[1] == canonicalPts[1]); |
| 1948 | REPORTER_ASSERT(r, hairlineAB.baseShape().asLine(pts, &inverted) && !inverted && |
| 1949 | pts[0] == canonicalPts[0] && pts[1] == canonicalPts[1]); |
| 1950 | REPORTER_ASSERT(r, dashAB.baseShape().asLine(pts, &inverted) && !inverted && |
| 1951 | pts[0] == kA && pts[1] == kB); |
| 1952 | REPORTER_ASSERT(r, dashBA.baseShape().asLine(pts, &inverted) && !inverted && |
| 1953 | pts[0] == kB && pts[1] == kA); |
| 1954 | |
| 1955 | |
bsalomon | a395f7c | 2016-08-24 17:47:40 -0700 | [diff] [blame] | 1956 | TestCase strokeInvAB(r, invLineAB, stroke); |
| 1957 | TestCase hairlineInvAB(r, invLineAB, hairline); |
| 1958 | TestCase dashInvAB(r, invLineAB, dash); |
bsalomon | 0a0f67e | 2016-06-28 11:56:42 -0700 | [diff] [blame] | 1959 | strokeInvAB.compare(r, strokeAB, TestCase::kAllDifferent_ComparisonExpecation); |
| 1960 | hairlineInvAB.compare(r, hairlineAB, TestCase::kAllDifferent_ComparisonExpecation); |
| 1961 | // Dashing ignores inverse. |
| 1962 | dashInvAB.compare(r, dashAB, TestCase::kAllSame_ComparisonExpecation); |
| 1963 | |
| 1964 | REPORTER_ASSERT(r, strokeInvAB.baseShape().asLine(pts, &inverted) && inverted && |
| 1965 | pts[0] == canonicalPts[0] && pts[1] == canonicalPts[1]); |
| 1966 | REPORTER_ASSERT(r, hairlineInvAB.baseShape().asLine(pts, &inverted) && inverted && |
| 1967 | pts[0] == canonicalPts[0] && pts[1] == canonicalPts[1]); |
| 1968 | // Dashing ignores inverse. |
| 1969 | REPORTER_ASSERT(r, dashInvAB.baseShape().asLine(pts, &inverted) && !inverted && |
| 1970 | pts[0] == kA && pts[1] == kB); |
| 1971 | |
| 1972 | } |
| 1973 | |
Mike Klein | 4334428 | 2017-08-16 11:56:22 -0400 | [diff] [blame] | 1974 | DEF_TEST(GrShape_stroked_lines, r) { |
Brian Salomon | 72f78c3 | 2017-12-21 11:56:42 -0500 | [diff] [blame] | 1975 | static constexpr SkScalar kIntervals1[] = {1.f, 0.f}; |
| 1976 | auto dash1 = SkDashPathEffect::Make(kIntervals1, SK_ARRAY_COUNT(kIntervals1), 0.f); |
| 1977 | REPORTER_ASSERT(r, dash1); |
| 1978 | static constexpr SkScalar kIntervals2[] = {10.f, 0.f, 5.f, 0.f}; |
| 1979 | auto dash2 = SkDashPathEffect::Make(kIntervals2, SK_ARRAY_COUNT(kIntervals2), 10.f); |
| 1980 | REPORTER_ASSERT(r, dash2); |
bsalomon | 0ae36a2 | 2016-07-18 07:31:13 -0700 | [diff] [blame] | 1981 | |
Brian Salomon | 72f78c3 | 2017-12-21 11:56:42 -0500 | [diff] [blame] | 1982 | sk_sp<SkPathEffect> pathEffects[] = {nullptr, std::move(dash1), std::move(dash2)}; |
bsalomon | 0ae36a2 | 2016-07-18 07:31:13 -0700 | [diff] [blame] | 1983 | |
Brian Salomon | 72f78c3 | 2017-12-21 11:56:42 -0500 | [diff] [blame] | 1984 | for (const auto& pe : pathEffects) { |
| 1985 | // Paints to try |
| 1986 | SkPaint buttCap; |
| 1987 | buttCap.setStyle(SkPaint::kStroke_Style); |
| 1988 | buttCap.setStrokeWidth(4); |
| 1989 | buttCap.setStrokeCap(SkPaint::kButt_Cap); |
| 1990 | buttCap.setPathEffect(pe); |
bsalomon | 0ae36a2 | 2016-07-18 07:31:13 -0700 | [diff] [blame] | 1991 | |
Brian Salomon | 72f78c3 | 2017-12-21 11:56:42 -0500 | [diff] [blame] | 1992 | SkPaint squareCap = buttCap; |
| 1993 | squareCap.setStrokeCap(SkPaint::kSquare_Cap); |
| 1994 | squareCap.setPathEffect(pe); |
bsalomon | 0ae36a2 | 2016-07-18 07:31:13 -0700 | [diff] [blame] | 1995 | |
Brian Salomon | 72f78c3 | 2017-12-21 11:56:42 -0500 | [diff] [blame] | 1996 | SkPaint roundCap = buttCap; |
| 1997 | roundCap.setStrokeCap(SkPaint::kRound_Cap); |
| 1998 | roundCap.setPathEffect(pe); |
bsalomon | 0ae36a2 | 2016-07-18 07:31:13 -0700 | [diff] [blame] | 1999 | |
Brian Salomon | 72f78c3 | 2017-12-21 11:56:42 -0500 | [diff] [blame] | 2000 | // vertical |
| 2001 | SkPath linePath; |
| 2002 | linePath.moveTo(4, 4); |
| 2003 | linePath.lineTo(4, 5); |
bsalomon | 0ae36a2 | 2016-07-18 07:31:13 -0700 | [diff] [blame] | 2004 | |
Brian Salomon | 72f78c3 | 2017-12-21 11:56:42 -0500 | [diff] [blame] | 2005 | SkPaint fill; |
bsalomon | 0ae36a2 | 2016-07-18 07:31:13 -0700 | [diff] [blame] | 2006 | |
Brian Salomon | 72f78c3 | 2017-12-21 11:56:42 -0500 | [diff] [blame] | 2007 | make_TestCase(r, linePath, buttCap)->compare( |
| 2008 | r, TestCase(r, SkRect::MakeLTRB(2, 4, 6, 5), fill), |
| 2009 | TestCase::kAllSame_ComparisonExpecation); |
bsalomon | 0ae36a2 | 2016-07-18 07:31:13 -0700 | [diff] [blame] | 2010 | |
Brian Salomon | 72f78c3 | 2017-12-21 11:56:42 -0500 | [diff] [blame] | 2011 | make_TestCase(r, linePath, squareCap)->compare( |
| 2012 | r, TestCase(r, SkRect::MakeLTRB(2, 2, 6, 7), fill), |
| 2013 | TestCase::kAllSame_ComparisonExpecation); |
bsalomon | 0ae36a2 | 2016-07-18 07:31:13 -0700 | [diff] [blame] | 2014 | |
Brian Salomon | 72f78c3 | 2017-12-21 11:56:42 -0500 | [diff] [blame] | 2015 | make_TestCase(r, linePath, roundCap)->compare(r, |
| 2016 | TestCase(r, SkRRect::MakeRectXY(SkRect::MakeLTRB(2, 2, 6, 7), 2, 2), fill), |
| 2017 | TestCase::kAllSame_ComparisonExpecation); |
bsalomon | 0ae36a2 | 2016-07-18 07:31:13 -0700 | [diff] [blame] | 2018 | |
Brian Salomon | 72f78c3 | 2017-12-21 11:56:42 -0500 | [diff] [blame] | 2019 | // horizontal |
| 2020 | linePath.reset(); |
| 2021 | linePath.moveTo(4, 4); |
| 2022 | linePath.lineTo(5, 4); |
bsalomon | 0ae36a2 | 2016-07-18 07:31:13 -0700 | [diff] [blame] | 2023 | |
Brian Salomon | 72f78c3 | 2017-12-21 11:56:42 -0500 | [diff] [blame] | 2024 | make_TestCase(r, linePath, buttCap)->compare( |
| 2025 | r, TestCase(r, SkRect::MakeLTRB(4, 2, 5, 6), fill), |
| 2026 | TestCase::kAllSame_ComparisonExpecation); |
| 2027 | make_TestCase(r, linePath, squareCap)->compare( |
| 2028 | r, TestCase(r, SkRect::MakeLTRB(2, 2, 7, 6), fill), |
| 2029 | TestCase::kAllSame_ComparisonExpecation); |
| 2030 | make_TestCase(r, linePath, roundCap)->compare( |
| 2031 | r, TestCase(r, SkRRect::MakeRectXY(SkRect::MakeLTRB(2, 2, 7, 6), 2, 2), fill), |
| 2032 | TestCase::kAllSame_ComparisonExpecation); |
| 2033 | |
| 2034 | // point |
| 2035 | linePath.reset(); |
| 2036 | linePath.moveTo(4, 4); |
| 2037 | linePath.lineTo(4, 4); |
| 2038 | |
| 2039 | make_TestCase(r, linePath, buttCap)->compare( |
| 2040 | r, TestCase(r, SkRect::MakeEmpty(), fill), |
| 2041 | TestCase::kAllSame_ComparisonExpecation); |
| 2042 | make_TestCase(r, linePath, squareCap)->compare( |
| 2043 | r, TestCase(r, SkRect::MakeLTRB(2, 2, 6, 6), fill), |
| 2044 | TestCase::kAllSame_ComparisonExpecation); |
| 2045 | make_TestCase(r, linePath, roundCap)->compare( |
| 2046 | r, TestCase(r, SkRRect::MakeRectXY(SkRect::MakeLTRB(2, 2, 6, 6), 2, 2), fill), |
| 2047 | TestCase::kAllSame_ComparisonExpecation); |
| 2048 | } |
bsalomon | 0ae36a2 | 2016-07-18 07:31:13 -0700 | [diff] [blame] | 2049 | } |
| 2050 | |
Mike Klein | 4334428 | 2017-08-16 11:56:22 -0400 | [diff] [blame] | 2051 | DEF_TEST(GrShape_short_path_keys, r) { |
bsalomon | 67fa4e3 | 2016-09-21 08:26:57 -0700 | [diff] [blame] | 2052 | SkPaint paints[4]; |
| 2053 | paints[1].setStyle(SkPaint::kStroke_Style); |
| 2054 | paints[1].setStrokeWidth(5.f); |
| 2055 | paints[2].setStyle(SkPaint::kStroke_Style); |
| 2056 | paints[2].setStrokeWidth(0.f); |
| 2057 | paints[3].setStyle(SkPaint::kStrokeAndFill_Style); |
| 2058 | paints[3].setStrokeWidth(5.f); |
| 2059 | |
bsalomon | aa84064 | 2016-09-23 12:09:16 -0700 | [diff] [blame] | 2060 | auto compare = [r, &paints] (const SkPath& pathA, const SkPath& pathB, |
bsalomon | 67fa4e3 | 2016-09-21 08:26:57 -0700 | [diff] [blame] | 2061 | TestCase::ComparisonExpecation expectation) { |
bsalomon | aa84064 | 2016-09-23 12:09:16 -0700 | [diff] [blame] | 2062 | SkPath volatileA = pathA; |
| 2063 | SkPath volatileB = pathB; |
| 2064 | volatileA.setIsVolatile(true); |
| 2065 | volatileB.setIsVolatile(true); |
bsalomon | 67fa4e3 | 2016-09-21 08:26:57 -0700 | [diff] [blame] | 2066 | for (const SkPaint& paint : paints) { |
bsalomon | aa84064 | 2016-09-23 12:09:16 -0700 | [diff] [blame] | 2067 | REPORTER_ASSERT(r, !GrShape(volatileA, paint).hasUnstyledKey()); |
| 2068 | REPORTER_ASSERT(r, !GrShape(volatileB, paint).hasUnstyledKey()); |
bsalomon | 67fa4e3 | 2016-09-21 08:26:57 -0700 | [diff] [blame] | 2069 | for (PathGeo::Invert invert : {PathGeo::Invert::kNo, PathGeo::Invert::kYes}) { |
bsalomon | aa84064 | 2016-09-23 12:09:16 -0700 | [diff] [blame] | 2070 | TestCase caseA(PathGeo(pathA, invert), paint, r); |
| 2071 | TestCase caseB(PathGeo(pathB, invert), paint, r); |
| 2072 | caseA.compare(r, caseB, expectation); |
bsalomon | 67fa4e3 | 2016-09-21 08:26:57 -0700 | [diff] [blame] | 2073 | } |
| 2074 | } |
| 2075 | }; |
| 2076 | |
| 2077 | SkPath pathA; |
| 2078 | SkPath pathB; |
| 2079 | |
| 2080 | // Two identical paths |
| 2081 | pathA.lineTo(10.f, 10.f); |
| 2082 | pathA.conicTo(20.f, 20.f, 20.f, 30.f, 0.7f); |
| 2083 | |
| 2084 | pathB.lineTo(10.f, 10.f); |
| 2085 | pathB.conicTo(20.f, 20.f, 20.f, 30.f, 0.7f); |
bsalomon | aa84064 | 2016-09-23 12:09:16 -0700 | [diff] [blame] | 2086 | compare(pathA, pathB, TestCase::kAllSame_ComparisonExpecation); |
bsalomon | 67fa4e3 | 2016-09-21 08:26:57 -0700 | [diff] [blame] | 2087 | |
| 2088 | // Give path b a different point |
| 2089 | pathB.reset(); |
| 2090 | pathB.lineTo(10.f, 10.f); |
| 2091 | pathB.conicTo(21.f, 20.f, 20.f, 30.f, 0.7f); |
bsalomon | aa84064 | 2016-09-23 12:09:16 -0700 | [diff] [blame] | 2092 | compare(pathA, pathB, TestCase::kAllDifferent_ComparisonExpecation); |
bsalomon | 67fa4e3 | 2016-09-21 08:26:57 -0700 | [diff] [blame] | 2093 | |
| 2094 | // Give path b a different conic weight |
| 2095 | pathB.reset(); |
| 2096 | pathB.lineTo(10.f, 10.f); |
| 2097 | pathB.conicTo(20.f, 20.f, 20.f, 30.f, 0.6f); |
bsalomon | aa84064 | 2016-09-23 12:09:16 -0700 | [diff] [blame] | 2098 | compare(pathA, pathB, TestCase::kAllDifferent_ComparisonExpecation); |
bsalomon | 67fa4e3 | 2016-09-21 08:26:57 -0700 | [diff] [blame] | 2099 | |
| 2100 | // Give path b an extra lineTo verb |
| 2101 | pathB.reset(); |
| 2102 | pathB.lineTo(10.f, 10.f); |
| 2103 | pathB.conicTo(20.f, 20.f, 20.f, 30.f, 0.6f); |
| 2104 | pathB.lineTo(50.f, 50.f); |
bsalomon | aa84064 | 2016-09-23 12:09:16 -0700 | [diff] [blame] | 2105 | compare(pathA, pathB, TestCase::kAllDifferent_ComparisonExpecation); |
bsalomon | 67fa4e3 | 2016-09-21 08:26:57 -0700 | [diff] [blame] | 2106 | |
| 2107 | // Give path b a close |
| 2108 | pathB.reset(); |
| 2109 | pathB.lineTo(10.f, 10.f); |
| 2110 | pathB.conicTo(20.f, 20.f, 20.f, 30.f, 0.7f); |
| 2111 | pathB.close(); |
bsalomon | aa84064 | 2016-09-23 12:09:16 -0700 | [diff] [blame] | 2112 | compare(pathA, pathB, TestCase::kAllDifferent_ComparisonExpecation); |
bsalomon | 67fa4e3 | 2016-09-21 08:26:57 -0700 | [diff] [blame] | 2113 | } |
| 2114 | |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 2115 | DEF_TEST(GrShape, reporter) { |
bsalomon | a395f7c | 2016-08-24 17:47:40 -0700 | [diff] [blame] | 2116 | SkTArray<std::unique_ptr<Geo>> geos; |
| 2117 | SkTArray<std::unique_ptr<RRectPathGeo>> rrectPathGeos; |
| 2118 | |
bsalomon | ee29564 | 2016-06-06 14:01:25 -0700 | [diff] [blame] | 2119 | for (auto r : { SkRect::MakeWH(10, 20), |
| 2120 | SkRect::MakeWH(-10, -20), |
| 2121 | SkRect::MakeWH(-10, 20), |
| 2122 | SkRect::MakeWH(10, -20)}) { |
bsalomon | a395f7c | 2016-08-24 17:47:40 -0700 | [diff] [blame] | 2123 | geos.emplace_back(new RectGeo(r)); |
| 2124 | SkPath rectPath; |
| 2125 | rectPath.addRect(r); |
| 2126 | geos.emplace_back(new RRectPathGeo(rectPath, r, RRectPathGeo::RRectForStroke::kYes, |
| 2127 | PathGeo::Invert::kNo)); |
| 2128 | geos.emplace_back(new RRectPathGeo(rectPath, r, RRectPathGeo::RRectForStroke::kYes, |
| 2129 | PathGeo::Invert::kYes)); |
| 2130 | rrectPathGeos.emplace_back(new RRectPathGeo(rectPath, r, RRectPathGeo::RRectForStroke::kYes, |
| 2131 | PathGeo::Invert::kNo)); |
bsalomon | ee29564 | 2016-06-06 14:01:25 -0700 | [diff] [blame] | 2132 | } |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 2133 | for (auto rr : { SkRRect::MakeRect(SkRect::MakeWH(10, 10)), |
bsalomon | ee29564 | 2016-06-06 14:01:25 -0700 | [diff] [blame] | 2134 | SkRRect::MakeRectXY(SkRect::MakeWH(10, 10), 3, 4), |
| 2135 | SkRRect::MakeOval(SkRect::MakeWH(20, 20))}) { |
bsalomon | a395f7c | 2016-08-24 17:47:40 -0700 | [diff] [blame] | 2136 | geos.emplace_back(new RRectGeo(rr)); |
bsalomon | 7049396 | 2016-06-10 08:05:14 -0700 | [diff] [blame] | 2137 | test_rrect(reporter, rr); |
bsalomon | a395f7c | 2016-08-24 17:47:40 -0700 | [diff] [blame] | 2138 | SkPath rectPath; |
| 2139 | rectPath.addRRect(rr); |
| 2140 | geos.emplace_back(new RRectPathGeo(rectPath, rr, RRectPathGeo::RRectForStroke::kYes, |
| 2141 | PathGeo::Invert::kNo)); |
| 2142 | geos.emplace_back(new RRectPathGeo(rectPath, rr, RRectPathGeo::RRectForStroke::kYes, |
| 2143 | PathGeo::Invert::kYes)); |
| 2144 | rrectPathGeos.emplace_back(new RRectPathGeo(rectPath, rr, |
| 2145 | RRectPathGeo::RRectForStroke::kYes, |
| 2146 | PathGeo::Invert::kNo)); |
bsalomon | 72dc51c | 2016-04-27 06:46:23 -0700 | [diff] [blame] | 2147 | } |
| 2148 | |
Brian Salomon | e494940 | 2018-04-26 15:22:04 -0400 | [diff] [blame] | 2149 | // Arcs |
| 2150 | geos.emplace_back(new ArcGeo(SkRect::MakeWH(200, 100), 12.f, 110.f, false)); |
| 2151 | geos.emplace_back(new ArcGeo(SkRect::MakeWH(200, 100), 12.f, 110.f, true)); |
| 2152 | |
Mike Klein | 4334428 | 2017-08-16 11:56:22 -0400 | [diff] [blame] | 2153 | { |
| 2154 | SkPath openRectPath; |
| 2155 | openRectPath.moveTo(0, 0); |
| 2156 | openRectPath.lineTo(10, 0); |
| 2157 | openRectPath.lineTo(10, 10); |
| 2158 | openRectPath.lineTo(0, 10); |
| 2159 | geos.emplace_back(new RRectPathGeo( |
| 2160 | openRectPath, SkRect::MakeWH(10, 10), |
| 2161 | RRectPathGeo::RRectForStroke::kNo, PathGeo::Invert::kNo)); |
| 2162 | geos.emplace_back(new RRectPathGeo( |
| 2163 | openRectPath, SkRect::MakeWH(10, 10), |
| 2164 | RRectPathGeo::RRectForStroke::kNo, PathGeo::Invert::kYes)); |
| 2165 | rrectPathGeos.emplace_back(new RRectPathGeo( |
| 2166 | openRectPath, SkRect::MakeWH(10, 10), |
| 2167 | RRectPathGeo::RRectForStroke::kNo, PathGeo::Invert::kNo)); |
| 2168 | } |
bsalomon | 72dc51c | 2016-04-27 06:46:23 -0700 | [diff] [blame] | 2169 | |
Mike Klein | 4334428 | 2017-08-16 11:56:22 -0400 | [diff] [blame] | 2170 | { |
| 2171 | SkPath quadPath; |
| 2172 | quadPath.quadTo(10, 10, 5, 8); |
| 2173 | geos.emplace_back(new PathGeo(quadPath, PathGeo::Invert::kNo)); |
| 2174 | geos.emplace_back(new PathGeo(quadPath, PathGeo::Invert::kYes)); |
| 2175 | } |
bsalomon | 398e3f4 | 2016-06-13 10:22:48 -0700 | [diff] [blame] | 2176 | |
Mike Klein | 4334428 | 2017-08-16 11:56:22 -0400 | [diff] [blame] | 2177 | { |
| 2178 | SkPath linePath; |
| 2179 | linePath.lineTo(10, 10); |
| 2180 | geos.emplace_back(new PathGeo(linePath, PathGeo::Invert::kNo)); |
| 2181 | geos.emplace_back(new PathGeo(linePath, PathGeo::Invert::kYes)); |
| 2182 | } |
bsalomon | 72dc51c | 2016-04-27 06:46:23 -0700 | [diff] [blame] | 2183 | |
bsalomon | 0ae36a2 | 2016-07-18 07:31:13 -0700 | [diff] [blame] | 2184 | // Horizontal and vertical paths become rrects when stroked. |
Mike Klein | 4334428 | 2017-08-16 11:56:22 -0400 | [diff] [blame] | 2185 | { |
| 2186 | SkPath vLinePath; |
| 2187 | vLinePath.lineTo(0, 10); |
| 2188 | geos.emplace_back(new PathGeo(vLinePath, PathGeo::Invert::kNo)); |
| 2189 | geos.emplace_back(new PathGeo(vLinePath, PathGeo::Invert::kYes)); |
| 2190 | } |
bsalomon | 0ae36a2 | 2016-07-18 07:31:13 -0700 | [diff] [blame] | 2191 | |
Mike Klein | 4334428 | 2017-08-16 11:56:22 -0400 | [diff] [blame] | 2192 | { |
| 2193 | SkPath hLinePath; |
| 2194 | hLinePath.lineTo(10, 0); |
| 2195 | geos.emplace_back(new PathGeo(hLinePath, PathGeo::Invert::kNo)); |
| 2196 | geos.emplace_back(new PathGeo(hLinePath, PathGeo::Invert::kYes)); |
| 2197 | } |
bsalomon | 0ae36a2 | 2016-07-18 07:31:13 -0700 | [diff] [blame] | 2198 | |
bsalomon | a395f7c | 2016-08-24 17:47:40 -0700 | [diff] [blame] | 2199 | for (int i = 0; i < geos.count(); ++i) { |
| 2200 | test_basic(reporter, *geos[i]); |
| 2201 | test_scale(reporter, *geos[i]); |
| 2202 | test_dash_fill(reporter, *geos[i]); |
| 2203 | test_null_dash(reporter, *geos[i]); |
| 2204 | // Test modifying various stroke params. |
| 2205 | test_stroke_param<SkScalar>( |
| 2206 | reporter, *geos[i], |
bsalomon | 7049396 | 2016-06-10 08:05:14 -0700 | [diff] [blame] | 2207 | [](SkPaint* p, SkScalar w) { p->setStrokeWidth(w);}, |
| 2208 | SkIntToScalar(2), SkIntToScalar(4)); |
bsalomon | a395f7c | 2016-08-24 17:47:40 -0700 | [diff] [blame] | 2209 | test_stroke_join(reporter, *geos[i]); |
| 2210 | test_stroke_cap(reporter, *geos[i]); |
| 2211 | test_miter_limit(reporter, *geos[i]); |
| 2212 | test_path_effect_makes_rrect(reporter, *geos[i]); |
| 2213 | test_unknown_path_effect(reporter, *geos[i]); |
| 2214 | test_path_effect_makes_empty_shape(reporter, *geos[i]); |
| 2215 | test_path_effect_fails(reporter, *geos[i]); |
| 2216 | test_make_hairline_path_effect(reporter, *geos[i]); |
| 2217 | test_volatile_path(reporter, *geos[i]); |
bsalomon | 7049396 | 2016-06-10 08:05:14 -0700 | [diff] [blame] | 2218 | } |
bsalomon | fd32df7 | 2016-06-14 14:37:21 -0700 | [diff] [blame] | 2219 | |
bsalomon | a395f7c | 2016-08-24 17:47:40 -0700 | [diff] [blame] | 2220 | for (int i = 0; i < rrectPathGeos.count(); ++i) { |
| 2221 | const RRectPathGeo& rrgeo = *rrectPathGeos[i]; |
bsalomon | 72dc51c | 2016-04-27 06:46:23 -0700 | [diff] [blame] | 2222 | SkPaint fillPaint; |
bsalomon | a395f7c | 2016-08-24 17:47:40 -0700 | [diff] [blame] | 2223 | TestCase fillPathCase(reporter, rrgeo.path(), fillPaint); |
bsalomon | 72dc51c | 2016-04-27 06:46:23 -0700 | [diff] [blame] | 2224 | SkRRect rrect; |
bsalomon | a395f7c | 2016-08-24 17:47:40 -0700 | [diff] [blame] | 2225 | REPORTER_ASSERT(reporter, rrgeo.isNonPath(fillPaint) == |
bsalomon | 7049396 | 2016-06-10 08:05:14 -0700 | [diff] [blame] | 2226 | fillPathCase.baseShape().asRRect(&rrect, nullptr, nullptr, |
| 2227 | nullptr)); |
bsalomon | a395f7c | 2016-08-24 17:47:40 -0700 | [diff] [blame] | 2228 | if (rrgeo.isNonPath(fillPaint)) { |
| 2229 | TestCase fillPathCase2(reporter, rrgeo.path(), fillPaint); |
| 2230 | REPORTER_ASSERT(reporter, rrect == rrgeo.rrect()); |
| 2231 | TestCase fillRRectCase(reporter, rrect, fillPaint); |
bsalomon | 7049396 | 2016-06-10 08:05:14 -0700 | [diff] [blame] | 2232 | fillPathCase2.compare(reporter, fillRRectCase, |
| 2233 | TestCase::kAllSame_ComparisonExpecation); |
bsalomon | 72dc51c | 2016-04-27 06:46:23 -0700 | [diff] [blame] | 2234 | } |
bsalomon | 72dc51c | 2016-04-27 06:46:23 -0700 | [diff] [blame] | 2235 | SkPaint strokePaint; |
| 2236 | strokePaint.setStrokeWidth(3.f); |
| 2237 | strokePaint.setStyle(SkPaint::kStroke_Style); |
bsalomon | a395f7c | 2016-08-24 17:47:40 -0700 | [diff] [blame] | 2238 | TestCase strokePathCase(reporter, rrgeo.path(), strokePaint); |
| 2239 | if (rrgeo.isNonPath(strokePaint)) { |
bsalomon | 0ae36a2 | 2016-07-18 07:31:13 -0700 | [diff] [blame] | 2240 | REPORTER_ASSERT(reporter, strokePathCase.baseShape().asRRect(&rrect, nullptr, nullptr, |
| 2241 | nullptr)); |
bsalomon | a395f7c | 2016-08-24 17:47:40 -0700 | [diff] [blame] | 2242 | REPORTER_ASSERT(reporter, rrect == rrgeo.rrect()); |
| 2243 | TestCase strokeRRectCase(reporter, rrect, strokePaint); |
bsalomon | 72dc51c | 2016-04-27 06:46:23 -0700 | [diff] [blame] | 2244 | strokePathCase.compare(reporter, strokeRRectCase, |
bsalomon | ee29564 | 2016-06-06 14:01:25 -0700 | [diff] [blame] | 2245 | TestCase::kAllSame_ComparisonExpecation); |
bsalomon | 72dc51c | 2016-04-27 06:46:23 -0700 | [diff] [blame] | 2246 | } |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 2247 | } |
bsalomon | 409ed73 | 2016-04-27 12:36:02 -0700 | [diff] [blame] | 2248 | |
bsalomon | 4eeccc9 | 2016-04-27 13:30:25 -0700 | [diff] [blame] | 2249 | // Test a volatile empty path. |
bsalomon | a395f7c | 2016-08-24 17:47:40 -0700 | [diff] [blame] | 2250 | test_volatile_path(reporter, PathGeo(SkPath(), PathGeo::Invert::kNo)); |
bsalomon | 47cc769 | 2016-04-26 12:56:00 -0700 | [diff] [blame] | 2251 | } |
| 2252 | |
Brian Salomon | e494940 | 2018-04-26 15:22:04 -0400 | [diff] [blame] | 2253 | DEF_TEST(GrShape_arcs, reporter) { |
| 2254 | SkStrokeRec roundStroke(SkStrokeRec::kFill_InitStyle); |
| 2255 | roundStroke.setStrokeStyle(2.f); |
| 2256 | roundStroke.setStrokeParams(SkPaint::kRound_Cap, SkPaint::kRound_Join, 1.f); |
| 2257 | |
| 2258 | SkStrokeRec squareStroke(roundStroke); |
| 2259 | squareStroke.setStrokeParams(SkPaint::kSquare_Cap, SkPaint::kRound_Join, 1.f); |
| 2260 | |
| 2261 | SkStrokeRec roundStrokeAndFill(roundStroke); |
| 2262 | roundStrokeAndFill.setStrokeStyle(2.f, true); |
| 2263 | |
| 2264 | static constexpr SkScalar kIntervals[] = {1, 2}; |
| 2265 | auto dash = SkDashPathEffect::Make(kIntervals, SK_ARRAY_COUNT(kIntervals), 1.5f); |
| 2266 | |
| 2267 | SkTArray<GrStyle> styles; |
| 2268 | styles.push_back(GrStyle::SimpleFill()); |
| 2269 | styles.push_back(GrStyle::SimpleHairline()); |
| 2270 | styles.push_back(GrStyle(roundStroke, nullptr)); |
| 2271 | styles.push_back(GrStyle(squareStroke, nullptr)); |
| 2272 | styles.push_back(GrStyle(roundStrokeAndFill, nullptr)); |
| 2273 | styles.push_back(GrStyle(roundStroke, dash)); |
| 2274 | |
| 2275 | for (const auto& style : styles) { |
| 2276 | // An empty rect never draws anything according to SkCanvas::drawArc() docs. |
| 2277 | TestCase emptyArc(GrShape::MakeArc(SkRect::MakeEmpty(), 0, 90.f, false, style), reporter); |
| 2278 | TestCase emptyPath(reporter, SkPath(), style); |
| 2279 | emptyArc.compare(reporter, emptyPath, TestCase::kAllSame_ComparisonExpecation); |
| 2280 | |
| 2281 | static constexpr SkRect kOval1{0, 0, 50, 50}; |
| 2282 | static constexpr SkRect kOval2{50, 0, 100, 50}; |
| 2283 | // Test that swapping starting and ending angle doesn't change the shape unless the arc |
| 2284 | // has a path effect. Also test that different ovals produce different shapes. |
| 2285 | TestCase arc1CW(GrShape::MakeArc(kOval1, 0, 90.f, false, style), reporter); |
| 2286 | TestCase arc1CCW(GrShape::MakeArc(kOval1, 90.f, -90.f, false, style), reporter); |
| 2287 | |
| 2288 | TestCase arc1CWWithCenter(GrShape::MakeArc(kOval1, 0, 90.f, true, style), reporter); |
| 2289 | TestCase arc1CCWWithCenter(GrShape::MakeArc(kOval1, 90.f, -90.f, true, style), reporter); |
| 2290 | |
| 2291 | TestCase arc2CW(GrShape::MakeArc(kOval2, 0, 90.f, false, style), reporter); |
| 2292 | TestCase arc2CWWithCenter(GrShape::MakeArc(kOval2, 0, 90.f, true, style), reporter); |
| 2293 | |
| 2294 | auto reversedExepectations = style.hasPathEffect() |
| 2295 | ? TestCase::kAllDifferent_ComparisonExpecation |
| 2296 | : TestCase::kAllSame_ComparisonExpecation; |
| 2297 | arc1CW.compare(reporter, arc1CCW, reversedExepectations); |
| 2298 | arc1CWWithCenter.compare(reporter, arc1CCWWithCenter, reversedExepectations); |
| 2299 | arc1CW.compare(reporter, arc2CW, TestCase::kAllDifferent_ComparisonExpecation); |
| 2300 | arc1CW.compare(reporter, arc1CWWithCenter, TestCase::kAllDifferent_ComparisonExpecation); |
| 2301 | arc1CWWithCenter.compare(reporter, arc2CWWithCenter, |
| 2302 | TestCase::kAllDifferent_ComparisonExpecation); |
| 2303 | |
| 2304 | // Test that two arcs that start at the same angle but specified differently are equivalent. |
| 2305 | TestCase arc3A(GrShape::MakeArc(kOval1, 224.f, 73.f, false, style), reporter); |
| 2306 | TestCase arc3B(GrShape::MakeArc(kOval1, 224.f - 360.f, 73.f, false, style), reporter); |
| 2307 | arc3A.compare(reporter, arc3B, TestCase::kAllDifferent_ComparisonExpecation); |
| 2308 | |
| 2309 | // Test that an arc that traverses the entire oval (and then some) is equivalent to the |
| 2310 | // oval itself unless there is a path effect. |
| 2311 | TestCase ovalArc(GrShape::MakeArc(kOval1, 150.f, -790.f, false, style), reporter); |
| 2312 | TestCase oval(GrShape(SkRRect::MakeOval(kOval1)), reporter); |
| 2313 | auto ovalExpectations = style.hasPathEffect() ? TestCase::kAllDifferent_ComparisonExpecation |
| 2314 | : TestCase::kAllSame_ComparisonExpecation; |
| 2315 | if (style.strokeRec().getWidth() >= 0 && style.strokeRec().getCap() != SkPaint::kButt_Cap) { |
| 2316 | ovalExpectations = TestCase::kAllDifferent_ComparisonExpecation; |
| 2317 | } |
| 2318 | ovalArc.compare(reporter, oval, ovalExpectations); |
| 2319 | |
| 2320 | // If the the arc starts/ends at the center then it is then equivalent to the oval only for |
| 2321 | // simple fills. |
| 2322 | TestCase ovalArcWithCenter(GrShape::MakeArc(kOval1, 304.f, 1225.f, true, style), reporter); |
| 2323 | ovalExpectations = style.isSimpleFill() ? TestCase::kAllSame_ComparisonExpecation |
| 2324 | : TestCase::kAllDifferent_ComparisonExpecation; |
| 2325 | ovalArcWithCenter.compare(reporter, oval, ovalExpectations); |
| 2326 | } |
| 2327 | } |