robertphillips | 2b9ee63 | 2014-11-05 08:06:40 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkCanvas.h" |
| 9 | #include "include/core/SkImageInfo.h" |
| 10 | #include "include/core/SkMatrix.h" |
| 11 | #include "include/core/SkPaint.h" |
| 12 | #include "include/core/SkPath.h" |
| 13 | #include "include/core/SkPathEffect.h" |
| 14 | #include "include/core/SkPoint.h" |
| 15 | #include "include/core/SkRect.h" |
| 16 | #include "include/core/SkRefCnt.h" |
| 17 | #include "include/core/SkScalar.h" |
| 18 | #include "include/core/SkStrokeRec.h" |
| 19 | #include "include/core/SkSurface.h" |
| 20 | #include "include/core/SkTypes.h" |
| 21 | #include "include/effects/SkDashPathEffect.h" |
| 22 | #include "tests/Test.h" |
Mike Klein | edf8449 | 2018-05-22 12:23:12 +0000 | [diff] [blame] | 23 | |
commit-bot@chromium.org | af5346a | 2014-03-18 17:38:34 +0000 | [diff] [blame] | 24 | // crbug.com/348821 was rooted in SkDashPathEffect refusing to flatten and unflatten itself when |
caryclark | eb75c7d | 2016-03-18 06:04:26 -0700 | [diff] [blame] | 25 | // the effect is nonsense. Here we test that it fails when passed nonsense parameters. |
commit-bot@chromium.org | af5346a | 2014-03-18 17:38:34 +0000 | [diff] [blame] | 26 | |
| 27 | DEF_TEST(DashPathEffectTest_crbug_348821, r) { |
| 28 | SkScalar intervals[] = { 1.76934361e+36f, 2.80259693e-45f }; // Values from bug. |
| 29 | const int count = 2; |
caryclark | eb75c7d | 2016-03-18 06:04:26 -0700 | [diff] [blame] | 30 | SkScalar phase = SK_ScalarInfinity; // Used to force a nonsense effect. |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 31 | sk_sp<SkPathEffect> dash(SkDashPathEffect::Make(intervals, count, phase)); |
commit-bot@chromium.org | af5346a | 2014-03-18 17:38:34 +0000 | [diff] [blame] | 32 | |
caryclark | eb75c7d | 2016-03-18 06:04:26 -0700 | [diff] [blame] | 33 | REPORTER_ASSERT(r, dash == nullptr); |
commit-bot@chromium.org | af5346a | 2014-03-18 17:38:34 +0000 | [diff] [blame] | 34 | } |
robertphillips | 2b9ee63 | 2014-11-05 08:06:40 -0800 | [diff] [blame] | 35 | |
| 36 | // Test out the asPoint culling behavior. |
| 37 | DEF_TEST(DashPathEffectTest_asPoints, r) { |
| 38 | |
| 39 | const SkScalar intervals[] = { 1.0f, 1.0f }; |
| 40 | const int count = 2; |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 41 | sk_sp<SkPathEffect> dash(SkDashPathEffect::Make(intervals, count, 0.0f)); |
robertphillips | 2b9ee63 | 2014-11-05 08:06:40 -0800 | [diff] [blame] | 42 | |
| 43 | SkRect cull = SkRect::MakeWH(1.0f, 1.0f); |
| 44 | |
| 45 | const struct { |
| 46 | SkPoint fPts[2]; |
| 47 | bool fExpectedResult; |
| 48 | } testCases[] = { |
| 49 | { { { -5.0f, 0.5f }, { -4.0f, 0.5f } }, false }, // off to the left |
| 50 | { { { 4.0f, 0.5f }, { 5.0f, 0.5f } }, false }, // off to the right |
| 51 | { { { 0.5f, 4.0f }, { 0.5f, 5.0f } }, false }, // off the bottom |
| 52 | { { { 0.5f, -5.0f }, { 0.5f, -4.0f } }, false }, // off the top |
| 53 | { { { 0.5f, 0.2f }, { 0.5f, 0.8f } }, true }, // entirely inside vertical |
| 54 | { { { 0.2f, 0.5f }, { 0.8f, 0.5f } }, true }, // entirely inside horizontal |
| 55 | { { { 0.5f, -5.0f }, { 0.5f, 5.0f } }, true }, // straddles both sides vertically |
| 56 | { { { -5.0f, 0.5f }, { 5.0f, 0.5f } }, true }, // straddles both sides horizontally |
| 57 | { { { 0.5f, -5.0f }, { 0.5f, 0.5f } }, true }, // straddles top |
| 58 | { { { 0.5f, 5.0f }, { 0.5f, 0.5f } }, true }, // straddles bottom |
| 59 | { { { -5.0f, 0.5f }, { 0.5f, 0.5f } }, true }, // straddles left |
| 60 | { { { 5.0f, 0.5f }, { 0.5f, 0.5f } }, true }, // straddles right |
| 61 | { { { 0.5f, 0.5f }, { 0.5f, 0.5f } }, false }, // zero length |
| 62 | }; |
| 63 | |
| 64 | SkPaint paint; |
| 65 | paint.setStyle(SkPaint::kStroke_Style); |
| 66 | paint.setStrokeWidth(1.0f); |
| 67 | SkStrokeRec rec(paint); |
| 68 | |
| 69 | static const int kNumMats = 3; |
| 70 | SkMatrix mats[kNumMats]; |
| 71 | mats[0].reset(); |
| 72 | mats[1].setRotate(90, 0.5f, 0.5f); |
| 73 | mats[2].setTranslate(10.0f, 10.0f); |
| 74 | |
| 75 | for (int i = 0; i < kNumMats; ++i) { |
| 76 | for (int j = 0; j < (int)SK_ARRAY_COUNT(testCases); ++j) { |
| 77 | for (int k = 0; k < 2; ++k) { // exercise alternating endpoints |
| 78 | SkPathEffect::PointData results; |
| 79 | SkPath src; |
| 80 | |
| 81 | src.moveTo(testCases[j].fPts[k]); |
| 82 | src.lineTo(testCases[j].fPts[(k+1)%2]); |
| 83 | |
| 84 | bool actualResult = dash->asPoints(&results, src, rec, mats[i], &cull); |
| 85 | if (i < 2) { |
| 86 | REPORTER_ASSERT(r, actualResult == testCases[j].fExpectedResult); |
| 87 | } else { |
| 88 | // On the third pass all the lines should be outside the translated cull rect |
| 89 | REPORTER_ASSERT(r, !actualResult); |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | } |
caryclark | 703348f | 2016-01-29 09:54:20 -0800 | [diff] [blame] | 95 | |
| 96 | DEF_TEST(DashPath_bug4871, r) { |
| 97 | SkPath path; |
| 98 | path.moveTo(30, 24); |
| 99 | path.cubicTo(30.002f, 24, 30, 24, 30, 24); |
| 100 | path.close(); |
| 101 | |
| 102 | SkScalar intervals[2] = { 1, 1 }; |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 103 | sk_sp<SkPathEffect> dash(SkDashPathEffect::Make(intervals, 2, 0)); |
caryclark | 703348f | 2016-01-29 09:54:20 -0800 | [diff] [blame] | 104 | |
| 105 | SkPaint paint; |
| 106 | paint.setStyle(SkPaint::kStroke_Style); |
| 107 | paint.setPathEffect(dash); |
| 108 | |
| 109 | SkPath fill; |
| 110 | paint.getFillPath(path, &fill); |
| 111 | } |
lsalzman | f41ae2f | 2016-07-21 09:37:59 -0700 | [diff] [blame] | 112 | |
| 113 | // Verify that long lines with many dashes don't cause overflows/OOMs. |
| 114 | DEF_TEST(DashPathEffectTest_asPoints_limit, r) { |
| 115 | sk_sp<SkSurface> surface(SkSurface::MakeRaster(SkImageInfo::MakeN32Premul(256, 256))); |
| 116 | SkCanvas* canvas = surface->getCanvas(); |
| 117 | |
| 118 | SkPaint p; |
| 119 | p.setStyle(SkPaint::kStroke_Style); |
| 120 | // force the bounds to outset by a large amount |
| 121 | p.setStrokeWidth(5.0e10f); |
| 122 | const SkScalar intervals[] = { 1, 1 }; |
| 123 | p.setPathEffect(SkDashPathEffect::Make(intervals, SK_ARRAY_COUNT(intervals), 0)); |
| 124 | canvas->drawLine(1, 1, 1, 5.0e10f, p); |
| 125 | } |
Mike Reed | 4c65144 | 2018-08-23 12:47:59 -0400 | [diff] [blame] | 126 | |
| 127 | // This used to cause SkDashImpl to walk off the end of the intervals array, due to underflow |
| 128 | // trying to substract a smal value from a large one in floats. |
| 129 | DEF_TEST(DashCrazy_crbug_875494, r) { |
| 130 | SkScalar vals[] = { 98, 94, 2888458849.f, 227, 0, 197 }; |
| 131 | const int N = SK_ARRAY_COUNT(vals); |
| 132 | |
| 133 | SkRect cull = SkRect::MakeXYWH(43,236,57,149); |
| 134 | SkPath path; |
| 135 | path.addRect(cull); |
| 136 | |
| 137 | SkPath path2; |
| 138 | SkPaint paint; |
| 139 | paint.setStyle(SkPaint::kStroke_Style); |
| 140 | paint.setPathEffect(SkDashPathEffect::Make(vals, N, 222)); |
| 141 | paint.getFillPath(path, &path2, &cull); |
| 142 | } |