blob: cc38b1bcd3309595362f3a844748caf6d0fc6fdb [file] [log] [blame]
robertphillips2b9ee632014-11-05 08:06:40 -08001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -05008#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 Kleinedf84492018-05-22 12:23:12 +000023
commit-bot@chromium.orgaf5346a2014-03-18 17:38:34 +000024// crbug.com/348821 was rooted in SkDashPathEffect refusing to flatten and unflatten itself when
caryclarkeb75c7d2016-03-18 06:04:26 -070025// the effect is nonsense. Here we test that it fails when passed nonsense parameters.
commit-bot@chromium.orgaf5346a2014-03-18 17:38:34 +000026
27DEF_TEST(DashPathEffectTest_crbug_348821, r) {
28 SkScalar intervals[] = { 1.76934361e+36f, 2.80259693e-45f }; // Values from bug.
29 const int count = 2;
caryclarkeb75c7d2016-03-18 06:04:26 -070030 SkScalar phase = SK_ScalarInfinity; // Used to force a nonsense effect.
reeda4393342016-03-18 11:22:57 -070031 sk_sp<SkPathEffect> dash(SkDashPathEffect::Make(intervals, count, phase));
commit-bot@chromium.orgaf5346a2014-03-18 17:38:34 +000032
caryclarkeb75c7d2016-03-18 06:04:26 -070033 REPORTER_ASSERT(r, dash == nullptr);
commit-bot@chromium.orgaf5346a2014-03-18 17:38:34 +000034}
robertphillips2b9ee632014-11-05 08:06:40 -080035
36// Test out the asPoint culling behavior.
37DEF_TEST(DashPathEffectTest_asPoints, r) {
38
39 const SkScalar intervals[] = { 1.0f, 1.0f };
40 const int count = 2;
reeda4393342016-03-18 11:22:57 -070041 sk_sp<SkPathEffect> dash(SkDashPathEffect::Make(intervals, count, 0.0f));
robertphillips2b9ee632014-11-05 08:06:40 -080042
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}
caryclark703348f2016-01-29 09:54:20 -080095
96DEF_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 };
reeda4393342016-03-18 11:22:57 -0700103 sk_sp<SkPathEffect> dash(SkDashPathEffect::Make(intervals, 2, 0));
caryclark703348f2016-01-29 09:54:20 -0800104
105 SkPaint paint;
106 paint.setStyle(SkPaint::kStroke_Style);
107 paint.setPathEffect(dash);
108
109 SkPath fill;
110 paint.getFillPath(path, &fill);
111}
lsalzmanf41ae2f2016-07-21 09:37:59 -0700112
113// Verify that long lines with many dashes don't cause overflows/OOMs.
114DEF_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 Reed4c651442018-08-23 12:47:59 -0400126
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.
129DEF_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}