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/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 "src/core/SkPathEffectBase.h"
23#include "tests/Test.h"
24
25// crbug.com/348821 was rooted in SkDashPathEffect refusing to flatten and unflatten itself when
26// the effect is nonsense.  Here we test that it fails when passed nonsense parameters.
27
28DEF_TEST(DashPathEffectTest_crbug_348821, r) {
29    SkScalar intervals[] = { 1.76934361e+36f, 2.80259693e-45f };  // Values from bug.
30    const int count = 2;
31    SkScalar phase = SK_ScalarInfinity;  // Used to force a nonsense effect.
32    sk_sp<SkPathEffect> dash(SkDashPathEffect::Make(intervals, count, phase));
33
34    REPORTER_ASSERT(r, dash == nullptr);
35}
36
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;
42    sk_sp<SkPathEffect> dash(SkDashPathEffect::Make(intervals, count, 0.0f));
43
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
79                SkPathEffectBase::PointData results;
80                SkPath src;
81
82                src.moveTo(testCases[j].fPts[k]);
83                src.lineTo(testCases[j].fPts[(k+1)%2]);
84
85                bool actualResult = as_PEB(dash)->asPoints(&results, src, rec, mats[i], &cull);
86                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}
96
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 };
104    sk_sp<SkPathEffect> dash(SkDashPathEffect::Make(intervals, 2, 0));
105
106    SkPaint paint;
107    paint.setStyle(SkPaint::kStroke_Style);
108    paint.setPathEffect(dash);
109
110    SkPath fill;
111    paint.getFillPath(path, &fill);
112}
113
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}
127
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}
144