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