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