blob: e3a380411dd726834f828533b362713ca8aceb59 [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
commit-bot@chromium.orgaf5346a2014-03-18 17:38:34 +00008#include "Test.h"
9
10#include "SkDashPathEffect.h"
11#include "SkWriteBuffer.h"
halcanary435657f2015-09-15 12:53:07 -070012#include "SkStrokeRec.h"
lsalzmanf41ae2f2016-07-21 09:37:59 -070013#include "SkCanvas.h"
14#include "SkSurface.h"
commit-bot@chromium.orgaf5346a2014-03-18 17:38:34 +000015
16// crbug.com/348821 was rooted in SkDashPathEffect refusing to flatten and unflatten itself when
caryclarkeb75c7d2016-03-18 06:04:26 -070017// the effect is nonsense. Here we test that it fails when passed nonsense parameters.
commit-bot@chromium.orgaf5346a2014-03-18 17:38:34 +000018
19DEF_TEST(DashPathEffectTest_crbug_348821, r) {
20 SkScalar intervals[] = { 1.76934361e+36f, 2.80259693e-45f }; // Values from bug.
21 const int count = 2;
caryclarkeb75c7d2016-03-18 06:04:26 -070022 SkScalar phase = SK_ScalarInfinity; // Used to force a nonsense effect.
reeda4393342016-03-18 11:22:57 -070023 sk_sp<SkPathEffect> dash(SkDashPathEffect::Make(intervals, count, phase));
commit-bot@chromium.orgaf5346a2014-03-18 17:38:34 +000024
caryclarkeb75c7d2016-03-18 06:04:26 -070025 REPORTER_ASSERT(r, dash == nullptr);
commit-bot@chromium.orgaf5346a2014-03-18 17:38:34 +000026}
robertphillips2b9ee632014-11-05 08:06:40 -080027
28// Test out the asPoint culling behavior.
29DEF_TEST(DashPathEffectTest_asPoints, r) {
30
31 const SkScalar intervals[] = { 1.0f, 1.0f };
32 const int count = 2;
reeda4393342016-03-18 11:22:57 -070033 sk_sp<SkPathEffect> dash(SkDashPathEffect::Make(intervals, count, 0.0f));
robertphillips2b9ee632014-11-05 08:06:40 -080034
35 SkRect cull = SkRect::MakeWH(1.0f, 1.0f);
36
37 const struct {
38 SkPoint fPts[2];
39 bool fExpectedResult;
40 } testCases[] = {
41 { { { -5.0f, 0.5f }, { -4.0f, 0.5f } }, false }, // off to the left
42 { { { 4.0f, 0.5f }, { 5.0f, 0.5f } }, false }, // off to the right
43 { { { 0.5f, 4.0f }, { 0.5f, 5.0f } }, false }, // off the bottom
44 { { { 0.5f, -5.0f }, { 0.5f, -4.0f } }, false }, // off the top
45 { { { 0.5f, 0.2f }, { 0.5f, 0.8f } }, true }, // entirely inside vertical
46 { { { 0.2f, 0.5f }, { 0.8f, 0.5f } }, true }, // entirely inside horizontal
47 { { { 0.5f, -5.0f }, { 0.5f, 5.0f } }, true }, // straddles both sides vertically
48 { { { -5.0f, 0.5f }, { 5.0f, 0.5f } }, true }, // straddles both sides horizontally
49 { { { 0.5f, -5.0f }, { 0.5f, 0.5f } }, true }, // straddles top
50 { { { 0.5f, 5.0f }, { 0.5f, 0.5f } }, true }, // straddles bottom
51 { { { -5.0f, 0.5f }, { 0.5f, 0.5f } }, true }, // straddles left
52 { { { 5.0f, 0.5f }, { 0.5f, 0.5f } }, true }, // straddles right
53 { { { 0.5f, 0.5f }, { 0.5f, 0.5f } }, false }, // zero length
54 };
55
56 SkPaint paint;
57 paint.setStyle(SkPaint::kStroke_Style);
58 paint.setStrokeWidth(1.0f);
59 SkStrokeRec rec(paint);
60
61 static const int kNumMats = 3;
62 SkMatrix mats[kNumMats];
63 mats[0].reset();
64 mats[1].setRotate(90, 0.5f, 0.5f);
65 mats[2].setTranslate(10.0f, 10.0f);
66
67 for (int i = 0; i < kNumMats; ++i) {
68 for (int j = 0; j < (int)SK_ARRAY_COUNT(testCases); ++j) {
69 for (int k = 0; k < 2; ++k) { // exercise alternating endpoints
70 SkPathEffect::PointData results;
71 SkPath src;
72
73 src.moveTo(testCases[j].fPts[k]);
74 src.lineTo(testCases[j].fPts[(k+1)%2]);
75
76 bool actualResult = dash->asPoints(&results, src, rec, mats[i], &cull);
77 if (i < 2) {
78 REPORTER_ASSERT(r, actualResult == testCases[j].fExpectedResult);
79 } else {
80 // On the third pass all the lines should be outside the translated cull rect
81 REPORTER_ASSERT(r, !actualResult);
82 }
83 }
84 }
85 }
86}
caryclark703348f2016-01-29 09:54:20 -080087
88DEF_TEST(DashPath_bug4871, r) {
89 SkPath path;
90 path.moveTo(30, 24);
91 path.cubicTo(30.002f, 24, 30, 24, 30, 24);
92 path.close();
93
94 SkScalar intervals[2] = { 1, 1 };
reeda4393342016-03-18 11:22:57 -070095 sk_sp<SkPathEffect> dash(SkDashPathEffect::Make(intervals, 2, 0));
caryclark703348f2016-01-29 09:54:20 -080096
97 SkPaint paint;
98 paint.setStyle(SkPaint::kStroke_Style);
99 paint.setPathEffect(dash);
100
101 SkPath fill;
102 paint.getFillPath(path, &fill);
103}
lsalzmanf41ae2f2016-07-21 09:37:59 -0700104
105// Verify that long lines with many dashes don't cause overflows/OOMs.
106DEF_TEST(DashPathEffectTest_asPoints_limit, r) {
107 sk_sp<SkSurface> surface(SkSurface::MakeRaster(SkImageInfo::MakeN32Premul(256, 256)));
108 SkCanvas* canvas = surface->getCanvas();
109
110 SkPaint p;
111 p.setStyle(SkPaint::kStroke_Style);
112 // force the bounds to outset by a large amount
113 p.setStrokeWidth(5.0e10f);
114 const SkScalar intervals[] = { 1, 1 };
115 p.setPathEffect(SkDashPathEffect::Make(intervals, SK_ARRAY_COUNT(intervals), 0));
116 canvas->drawLine(1, 1, 1, 5.0e10f, p);
117}