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