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