blob: 379cd3188ea76ef238f58e88927a5cb42c3d49a5 [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"
Mike Reedec9d0e82021-05-21 17:42:14 -040022#include "src/core/SkPathEffectBase.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "tests/Test.h"
Mike Kleinedf84492018-05-22 12:23:12 +000024
commit-bot@chromium.orgaf5346a2014-03-18 17:38:34 +000025// crbug.com/348821 was rooted in SkDashPathEffect refusing to flatten and unflatten itself when
caryclarkeb75c7d2016-03-18 06:04:26 -070026// the effect is nonsense. Here we test that it fails when passed nonsense parameters.
commit-bot@chromium.orgaf5346a2014-03-18 17:38:34 +000027
28DEF_TEST(DashPathEffectTest_crbug_348821, r) {
29 SkScalar intervals[] = { 1.76934361e+36f, 2.80259693e-45f }; // Values from bug.
30 const int count = 2;
caryclarkeb75c7d2016-03-18 06:04:26 -070031 SkScalar phase = SK_ScalarInfinity; // Used to force a nonsense effect.
reeda4393342016-03-18 11:22:57 -070032 sk_sp<SkPathEffect> dash(SkDashPathEffect::Make(intervals, count, phase));
commit-bot@chromium.orgaf5346a2014-03-18 17:38:34 +000033
caryclarkeb75c7d2016-03-18 06:04:26 -070034 REPORTER_ASSERT(r, dash == nullptr);
commit-bot@chromium.orgaf5346a2014-03-18 17:38:34 +000035}
robertphillips2b9ee632014-11-05 08:06:40 -080036
37// Test out the asPoint culling behavior.
38DEF_TEST(DashPathEffectTest_asPoints, r) {
39
40 const SkScalar intervals[] = { 1.0f, 1.0f };
41 const int count = 2;
reeda4393342016-03-18 11:22:57 -070042 sk_sp<SkPathEffect> dash(SkDashPathEffect::Make(intervals, count, 0.0f));
robertphillips2b9ee632014-11-05 08:06:40 -080043
44 SkRect cull = SkRect::MakeWH(1.0f, 1.0f);
45
46 const struct {
47 SkPoint fPts[2];
48 bool fExpectedResult;
49 } testCases[] = {
50 { { { -5.0f, 0.5f }, { -4.0f, 0.5f } }, false }, // off to the left
51 { { { 4.0f, 0.5f }, { 5.0f, 0.5f } }, false }, // off to the right
52 { { { 0.5f, 4.0f }, { 0.5f, 5.0f } }, false }, // off the bottom
53 { { { 0.5f, -5.0f }, { 0.5f, -4.0f } }, false }, // off the top
54 { { { 0.5f, 0.2f }, { 0.5f, 0.8f } }, true }, // entirely inside vertical
55 { { { 0.2f, 0.5f }, { 0.8f, 0.5f } }, true }, // entirely inside horizontal
56 { { { 0.5f, -5.0f }, { 0.5f, 5.0f } }, true }, // straddles both sides vertically
57 { { { -5.0f, 0.5f }, { 5.0f, 0.5f } }, true }, // straddles both sides horizontally
58 { { { 0.5f, -5.0f }, { 0.5f, 0.5f } }, true }, // straddles top
59 { { { 0.5f, 5.0f }, { 0.5f, 0.5f } }, true }, // straddles bottom
60 { { { -5.0f, 0.5f }, { 0.5f, 0.5f } }, true }, // straddles left
61 { { { 5.0f, 0.5f }, { 0.5f, 0.5f } }, true }, // straddles right
62 { { { 0.5f, 0.5f }, { 0.5f, 0.5f } }, false }, // zero length
63 };
64
65 SkPaint paint;
66 paint.setStyle(SkPaint::kStroke_Style);
67 paint.setStrokeWidth(1.0f);
68 SkStrokeRec rec(paint);
69
70 static const int kNumMats = 3;
71 SkMatrix mats[kNumMats];
72 mats[0].reset();
73 mats[1].setRotate(90, 0.5f, 0.5f);
74 mats[2].setTranslate(10.0f, 10.0f);
75
76 for (int i = 0; i < kNumMats; ++i) {
77 for (int j = 0; j < (int)SK_ARRAY_COUNT(testCases); ++j) {
78 for (int k = 0; k < 2; ++k) { // exercise alternating endpoints
Mike Reedec9d0e82021-05-21 17:42:14 -040079 SkPathEffectBase::PointData results;
robertphillips2b9ee632014-11-05 08:06:40 -080080 SkPath src;
81
82 src.moveTo(testCases[j].fPts[k]);
83 src.lineTo(testCases[j].fPts[(k+1)%2]);
84
Mike Reedec9d0e82021-05-21 17:42:14 -040085 bool actualResult = as_PEB(dash)->asPoints(&results, src, rec, mats[i], &cull);
robertphillips2b9ee632014-11-05 08:06:40 -080086 if (i < 2) {
87 REPORTER_ASSERT(r, actualResult == testCases[j].fExpectedResult);
88 } else {
89 // On the third pass all the lines should be outside the translated cull rect
90 REPORTER_ASSERT(r, !actualResult);
91 }
92 }
93 }
94 }
95}
caryclark703348f2016-01-29 09:54:20 -080096
97DEF_TEST(DashPath_bug4871, r) {
98 SkPath path;
99 path.moveTo(30, 24);
100 path.cubicTo(30.002f, 24, 30, 24, 30, 24);
101 path.close();
102
103 SkScalar intervals[2] = { 1, 1 };
reeda4393342016-03-18 11:22:57 -0700104 sk_sp<SkPathEffect> dash(SkDashPathEffect::Make(intervals, 2, 0));
caryclark703348f2016-01-29 09:54:20 -0800105
106 SkPaint paint;
107 paint.setStyle(SkPaint::kStroke_Style);
108 paint.setPathEffect(dash);
109
110 SkPath fill;
111 paint.getFillPath(path, &fill);
112}
lsalzmanf41ae2f2016-07-21 09:37:59 -0700113
114// Verify that long lines with many dashes don't cause overflows/OOMs.
115DEF_TEST(DashPathEffectTest_asPoints_limit, r) {
116 sk_sp<SkSurface> surface(SkSurface::MakeRaster(SkImageInfo::MakeN32Premul(256, 256)));
117 SkCanvas* canvas = surface->getCanvas();
118
119 SkPaint p;
120 p.setStyle(SkPaint::kStroke_Style);
121 // force the bounds to outset by a large amount
122 p.setStrokeWidth(5.0e10f);
123 const SkScalar intervals[] = { 1, 1 };
124 p.setPathEffect(SkDashPathEffect::Make(intervals, SK_ARRAY_COUNT(intervals), 0));
125 canvas->drawLine(1, 1, 1, 5.0e10f, p);
126}
Mike Reed4c651442018-08-23 12:47:59 -0400127
128// This used to cause SkDashImpl to walk off the end of the intervals array, due to underflow
129// trying to substract a smal value from a large one in floats.
130DEF_TEST(DashCrazy_crbug_875494, r) {
131 SkScalar vals[] = { 98, 94, 2888458849.f, 227, 0, 197 };
132 const int N = SK_ARRAY_COUNT(vals);
133
134 SkRect cull = SkRect::MakeXYWH(43,236,57,149);
135 SkPath path;
136 path.addRect(cull);
137
138 SkPath path2;
139 SkPaint paint;
140 paint.setStyle(SkPaint::kStroke_Style);
141 paint.setPathEffect(SkDashPathEffect::Make(vals, N, 222));
142 paint.getFillPath(path, &path2, &cull);
143}