1 /*
2 * Copyright 2013 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/SkTypes.h"
9
10 #include "include/core/SkBitmap.h"
11 #include "include/core/SkCanvas.h"
12 #include "include/core/SkColor.h"
13 #include "include/core/SkPaint.h"
14 #include "include/core/SkPath.h"
15 #include "include/core/SkRRect.h"
16 #include "include/core/SkRect.h"
17 #include "include/core/SkSurface.h"
18 #include "include/effects/SkDashPathEffect.h"
19 #include "include/gpu/GrDirectContext.h"
20 #include "src/gpu/geometry/GrStyledShape.h"
21 #include "tests/Test.h"
22
23 #include <initializer_list>
24
test_drawPathEmpty(skiatest::Reporter *,SkCanvas * canvas)25 static void test_drawPathEmpty(skiatest::Reporter*, SkCanvas* canvas) {
26 // Filling an empty path should not crash.
27 SkPaint paint;
28 SkRect emptyRect = SkRect::MakeEmpty();
29 canvas->drawRect(emptyRect, paint);
30 canvas->drawPath(SkPath(), paint);
31 canvas->drawOval(emptyRect, paint);
32 canvas->drawRect(emptyRect, paint);
33 canvas->drawRRect(SkRRect::MakeRect(emptyRect), paint);
34
35 // Stroking an empty path should not crash.
36 paint.setAntiAlias(true);
37 paint.setStyle(SkPaint::kStroke_Style);
38 paint.setColor(SK_ColorGRAY);
39 paint.setStrokeWidth(SkIntToScalar(20));
40 paint.setStrokeJoin(SkPaint::kRound_Join);
41 canvas->drawRect(emptyRect, paint);
42 canvas->drawPath(SkPath(), paint);
43 canvas->drawOval(emptyRect, paint);
44 canvas->drawRect(emptyRect, paint);
45 canvas->drawRRect(SkRRect::MakeRect(emptyRect), paint);
46 }
47
fill_and_stroke(SkCanvas * canvas,const SkPath & p1,const SkPath & p2,sk_sp<SkPathEffect> effect)48 static void fill_and_stroke(SkCanvas* canvas, const SkPath& p1, const SkPath& p2,
49 sk_sp<SkPathEffect> effect) {
50 SkPaint paint;
51 paint.setAntiAlias(true);
52 paint.setPathEffect(effect);
53
54 canvas->drawPath(p1, paint);
55 canvas->drawPath(p2, paint);
56
57 paint.setStyle(SkPaint::kStroke_Style);
58 canvas->drawPath(p1, paint);
59 canvas->drawPath(p2, paint);
60 }
61
test_drawSameRectOvals(skiatest::Reporter *,SkCanvas * canvas)62 static void test_drawSameRectOvals(skiatest::Reporter*, SkCanvas* canvas) {
63 // Drawing ovals with similar bounds but different points order should not crash.
64
65 SkPath oval1, oval2;
66 const SkRect rect = SkRect::MakeWH(100, 50);
67 oval1.addOval(rect, SkPathDirection::kCW);
68 oval2.addOval(rect, SkPathDirection::kCCW);
69
70 fill_and_stroke(canvas, oval1, oval2, nullptr);
71
72 const SkScalar intervals[] = { 1, 1 };
73 fill_and_stroke(canvas, oval1, oval2, SkDashPathEffect::Make(intervals, 2, 0));
74 }
75
DEF_GPUTEST_FOR_ALL_GL_CONTEXTS(GpuDrawPath,reporter,ctxInfo)76 DEF_GPUTEST_FOR_ALL_GL_CONTEXTS(GpuDrawPath, reporter, ctxInfo) {
77 for (auto& test_func : { &test_drawPathEmpty, &test_drawSameRectOvals }) {
78 for (auto& sampleCount : {1, 4, 16}) {
79 SkImageInfo info = SkImageInfo::MakeN32Premul(255, 255);
80 auto surface(
81 SkSurface::MakeRenderTarget(ctxInfo.directContext(), SkBudgeted::kNo, info,
82 sampleCount, nullptr));
83 if (!surface) {
84 continue;
85 }
86 test_func(reporter, surface->getCanvas());
87 }
88 }
89 }
90
DEF_GPUTEST_FOR_ALL_CONTEXTS(GrDrawCollapsedPath,reporter,ctxInfo)91 DEF_GPUTEST_FOR_ALL_CONTEXTS(GrDrawCollapsedPath, reporter, ctxInfo) {
92 // From https://bugs.fuchsia.dev/p/fuchsia/issues/detail?id=37330, it's possible for a convex
93 // path to be accepted by AAConvexPathRenderer, then be transformed to something without a
94 // computable first direction by a perspective matrix.
95 SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
96 auto surface(SkSurface::MakeRenderTarget(ctxInfo.directContext(), SkBudgeted::kNo, info));
97
98 SkPaint paint;
99 paint.setAntiAlias(true);
100
101 SkPath path;
102 path.moveTo(0, 0);
103 path.lineTo(50, 0);
104 path.lineTo(0, 50);
105 path.close();
106
107 SkMatrix m;
108 m.setAll( 0.966006875f , -0.125156224f , 72.0899811f,
109 -0.00885376986f , -0.112347461f , 64.7121124f,
110 -8.94321693e-06f, -0.00173384184f, 0.998692870f);
111 surface->getCanvas()->setMatrix(m);
112 surface->getCanvas()->drawPath(path, paint);
113 surface->flushAndSubmit();
114 }
115
DEF_GPUTEST_FOR_ALL_CONTEXTS(PathTest_CrBug1232834,reporter,ctxInfo)116 DEF_GPUTEST_FOR_ALL_CONTEXTS(PathTest_CrBug1232834, reporter, ctxInfo) {
117 // AAHairlinePathRenderer chops this path to quads that include infinities (and then NaNs).
118 // It used to trigger asserts, now the degenerate quad segments should cause it to be rejected.
119 SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256);
120 auto surface(SkSurface::MakeRenderTarget(ctxInfo.directContext(), SkBudgeted::kNo, info));
121
122 SkPaint paint;
123 paint.setAntiAlias(true);
124 paint.setStyle(SkPaint::kStroke_Style);
125
126 SkPath path;
127 path.moveTo(9.0072E15f, 60);
128 path.cubicTo(0, 3.40282e+38f, 0, 3.40282e+38f, 0, 0);
129
130 surface->getCanvas()->drawPath(path, paint);
131 surface->flushAndSubmit();
132 }
133