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 8 #include "SkCanvas.h" 9 #include "SkDashPathEffect.h" 10 #include "SkImageInfo.h" 11 #include "SkMatrix.h" 12 #include "SkPaint.h" 13 #include "SkPath.h" 14 #include "SkPathEffect.h" 15 #include "SkPoint.h" 16 #include "SkRect.h" 17 #include "SkRefCnt.h" 18 #include "SkScalar.h" 19 #include "SkStrokeRec.h" 20 #include "SkSurface.h" 21 #include "SkTypes.h" 22 #include "Test.h" 23 24 // crbug.com/348821 was rooted in SkDashPathEffect refusing to flatten and unflatten itself when 25 // the effect is nonsense. Here we test that it fails when passed nonsense parameters. 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; 30 SkScalar phase = SK_ScalarInfinity; // Used to force a nonsense effect. 31 sk_sp<SkPathEffect> dash(SkDashPathEffect::Make(intervals, count, phase)); 32 33 REPORTER_ASSERT(r, dash == nullptr); 34 } 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; 41 sk_sp<SkPathEffect> dash(SkDashPathEffect::Make(intervals, count, 0.0f)); 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 } 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 }; 103 sk_sp<SkPathEffect> dash(SkDashPathEffect::Make(intervals, 2, 0)); 104 105 SkPaint paint; 106 paint.setStyle(SkPaint::kStroke_Style); 107 paint.setPathEffect(dash); 108 109 SkPath fill; 110 paint.getFillPath(path, &fill); 111 } 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 } 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 } 143